~depict/d-pict/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
 * ui_editor.vala
 * Copyright (C) 2012, Linus Seelinger <S.Linus@quantentunnel.de>
 *
 * dePict is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * dePict is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// The editor tab

using GLib;
using Clutter;
using Gtk;
using Gee;
//using Poppler;

public class ui_editor {

    public root presentation = new root();
    Stage stage;
    
    const int animation_time = 125;
    GtkClutter.Embed embed;
    
    public ui_editor(){

        
        //Create default structure
        presentation.add_child(new text("Write here"));
        
        //presentation = io.load("/home/linus/source/depict/test.dep");

        //Build Gtk GUI
        var vbox_editor = new VBox(false, 0);

            var toolbarEditor = new Toolbar();
            ToolButton tb_editor_save = new ToolButton.from_stock(Stock.SAVE);
            tb_editor_save.clicked.connect(()=>{
                FileChooserDialog dlg = new FileChooserDialog("Save project", main_window, FileChooserAction.SAVE,
                    Stock.CANCEL, ResponseType.CANCEL, Stock.SAVE, ResponseType.ACCEPT);
                
                ResponseType res = (ResponseType)dlg.run();
                if (res == ResponseType.ACCEPT){
                    io.write(dlg.get_filename(), presentation);
                }
                dlg.destroy();
            });
            toolbarEditor.add(tb_editor_save);
            
            ToolButton tb_editor_open = new ToolButton.from_stock(Stock.OPEN);
            tb_editor_open.clicked.connect(()=>{
                FileChooserDialog dlg = new FileChooserDialog("Open project", main_window, FileChooserAction.OPEN,
                    Stock.CANCEL, ResponseType.CANCEL, Stock.OPEN, ResponseType.ACCEPT);
                
                ResponseType res = (ResponseType)dlg.run();
                if (res == ResponseType.ACCEPT){
                    destroy_current_structure();
                    presentation = io.load(dlg.get_filename());
                    build_tree();
                }
                dlg.destroy();
            });
            toolbarEditor.add(tb_editor_open);

            ToolButton tb_heading = new ToolButton.from_stock(Stock.BOLD);
            tb_heading.clicked.connect(on_btn_heading_clicked);
            toolbarEditor.add(tb_heading);

            ToolButton tb_paste = new ToolButton.from_stock(Stock.PASTE);
            tb_paste.clicked.connect(on_btn_paste_clicked);
            toolbarEditor.add(tb_paste);

            vbox_editor.pack_start(toolbarEditor, false, true);

            embed = new GtkClutter.Embed();
            embed.sensitive = true;
            embed.size_allocate.connect(on_embed_size_allocate);
            stage = (Stage)embed.get_stage();
            stage.reactive = true;
            stage.button_press_event.connect(()=>{
                main_window.set_focus(embed);
                return false;
            });
            embed.key_press_event.connect((event)=>{
                stdout.printf(event.keyval.to_string() + "\n");
                if (event.keyval == 65289){
                    on_btn_forward_clicked();
                    return true;
                }
                if (event.keyval == 65056){
                    on_btn_back_clicked();
                    return true;
                }
                if (event.keyval == 65362){
                    var actor_focus = stage.get_key_focus();
                    element? el = get_element_of_actor(actor_focus);
                    
                    var upper_el = get_upper_element(el);
                    if (upper_el == null)
                        return true;

                    stage.set_key_focus(map_element_actor[upper_el]);
                    return true;
                }
                if (event.keyval == 65364){
                    var actor_focus = stage.get_key_focus();
                    element? el = get_element_of_actor(actor_focus);
                    
                    var lower_el = get_lower_element(el);
                    if (lower_el == null)
                        return true;

                    stage.set_key_focus(map_element_actor[lower_el]);
                    return true;
                }
                return false;
            });
            
            var scrw = new ScrolledWindow(null, null);
            scrw.hscrollbar_policy = PolicyType.NEVER;
            scrw.add_with_viewport(embed);
            //scrw.add(embed);
            vbox_editor.pack_start(scrw, true, true);

        this.widget = vbox_editor;

        actor_drag_handle = new Texture.from_file(Config.DATA_DIR + "/drag-handle.png");
        actor_drag_handle.opacity = 0;
        stage.add(actor_drag_handle);
        
        var effect = new ColorizeEffect(new Color.from_string("rgb(0%,80%,0%)"));
        actor_drag_handle.add_effect(effect);
        

        build_tree();
    }

    //Find element directly above el
    element? get_upper_element(element el){
        var parent = el.parent;
        int own_index = parent.children.index_of(el);
        if (own_index == 0){
            if (parent is root)
                return null;
            return parent;
        } else if (own_index > 0){
            element iter = parent.children[own_index - 1];
            while (iter.children.size != 0)
                iter = iter.children[iter.children.size - 1];
            return iter;
        }
        return null;
    }
    //Find element directly under el
    element? get_lower_element(element el){
        var parent = el.parent;
        int own_index = parent.children.index_of(el);
        
        if (el.children.size > 0){ //If existing, return first child
            return el.children[0];
        } else if (el.children.size == 0){ //Otherwise, iterate through parents for a parent with a following child

            element iter = el;

            while (iter.parent.children.index_of(iter) == iter.parent.children.size - 1){
                if (iter.parent is root)
                    return null;
                iter = iter.parent;
            }
            return iter.parent.children[iter.parent.children.index_of(iter) + 1];
        }
        return null;
    }
    
    //Poppler.Document preview_document;
    void update_preview(string file){
        //preview_document = new Poppler.Document.from_file (Filename.to_uri (""), "");//Filename.to_uri ("/home/linus/Dokumente/fahrplan.pdf"), "");
        //int x = Poppler.Backend.CAIRO;
        //Poppler.FontInfo a = new Poppler.FontInfo();
    }

    void on_btn_heading_clicked(){
        var actor_focus = stage.get_key_focus();
        element? el = get_element_of_actor(actor_focus);

        if (el is text){
            text tx = (text)el;
            var parent = el.parent;
            var children = el.children;

            var new_element = new heading(tx.text);
            parent.replace_child(el, new_element);
            actor_focus.destroy();
            new_element.children = children;

            build_tree();
        }
    }
    void on_btn_up_clicked(){
        var actor_focus = stage.get_key_focus();
        element? el = get_element_of_actor(actor_focus);

        var parent = el.parent;

        if (parent == null)
            return;

        int own_index = parent.children.index_of(el);
        if (own_index == 0)
            return;

        el.parent.children.remove(el);
        parent.add_child(el, own_index - 1);

        build_tree();
    }
    void on_btn_down_clicked(){
        var actor_focus = stage.get_key_focus();
        element? el = get_element_of_actor(actor_focus);

        var parent = el.parent;

        if (parent == null)
            return;

        int own_index = parent.children.index_of(el);
        if (own_index == parent.children.size - 1)
            return;

        el.parent.children.remove(el);
        parent.add_child(el, own_index + 1);

        build_tree();
    }
    void on_btn_back_clicked(){
        var actor_focus = stage.get_key_focus();
        element? el = get_element_of_actor(actor_focus);

        var parent = el.parent.parent;

        if (parent == null)
            return;

        int own_index = parent.children.index_of(el.parent);

        el.parent.children.remove(el);
        parent.add_child(el, own_index + 1);

        build_tree();
    }
    void on_btn_forward_clicked(){
        var actor_focus = stage.get_key_focus();
        element? el = get_element_of_actor(actor_focus);

        var parent = el.parent;

        if (parent == null)
            return;

        int own_index = parent.children.index_of(el);
        if (own_index < 1)
            return;

        if (!(parent.children[own_index - 1] is heading))
            return;

        parent.children.remove(el);

        parent.children[own_index - 1].add_child(el);

        build_tree();
    }
    void on_btn_paste_clicked(){
        var actor_focus = stage.get_key_focus();

        if (actor_focus is Text){
            var clipboard = Clipboard.get(Gdk.SELECTION_CLIPBOARD);
            string? clip_text = clipboard.wait_for_text();

            if (clip_text == null)
                return;

            var actor_text = (Text)actor_focus;
            int cursor_pos = actor_text.get_cursor_position();

            string new_string = actor_text.text.substring(0, cursor_pos) + clip_text;
            if (cursor_pos != -1)
                new_string += actor_text.text.substring(cursor_pos);

            actor_text.text = new_string;
        }
    }

    HashMap<element, Actor> map_element_actor = new HashMap<element, Actor>();
    void destroy_current_structure(){ //Destroys all actors in the current structure
        var iterator = map_element_actor.map_iterator();

        while (iterator.next() == true){
            stage.remove(iterator.get_value());
            iterator.get_value().destroy();
        }
        map_element_actor.clear();
    }
    element? get_element_of_actor(Actor actor){
        var iterator = map_element_actor.map_iterator();

        while (iterator.next() == true){
            if (iterator.get_value() == actor)
                return iterator.get_key();
        }
        return null;
    }
    element? get_element_above_y(float y, element ignore){
        var iterator = map_element_actor.map_iterator();

        element candidate = null;
        float candidate_y = 0;
        while (iterator.next() == true){
            Actor a = iterator.get_value();
            if (a.y < y && a.y >  candidate_y){
                if (ignore.has_descendant(iterator.get_key()))
                    continue;
                candidate = iterator.get_key();
                candidate_y = a.y;
            }
        }
        return candidate;
    }

    float tree_y;
    Actor actor_drag_handle = null;
    element last_actor_drag_target = null;
    int last_drag_depth = -1;
    void build_tree(){
        tree_y = 0;
        build_tree_iter(presentation, -1);
        stage.height = tree_y;
        embed.set_size_request(0, (int)stage.height);
    }
    void build_tree_iter(element iter, int depth){

        if (iter is heading){
            var hd = (heading)iter;

            if (!map_element_actor.has_key(iter)){
                var actor_text = new Text.full("DejaVu Sans Light 16", hd.text, new Clutter.Color.from_string("rgb(10%,10%,10%)"));
                actor_text.text_changed.connect(()=>{
                    hd.text = actor_text.text;
                });
                actor_text.key_press_event.connect((event)=>{
                    stdout.printf(event.keyval.to_string() + "\n");
                    if (event.keyval == 65288 && actor_text.text == ""){
                        remove_element(hd);
                        return true;
                    }
                    if (event.keyval == 65293){
                        iter.add_child(new text(""), 0);
                        build_tree();
                        return true;
                    }
                    return false;
                });
                add_drag_handle(actor_text, hd);

                stage.add(actor_text);
                map_element_actor.set(iter, actor_text);
                stage.key_focus = actor_text;
            }
            var actor_text = (Text)map_element_actor.get(iter);
            actor_text.text = hd.text;
            actor_text.animate(AnimationMode.EASE_IN_QUAD, animation_time,
                                x: depth * 20.0f + 20.0f,
                                y: tree_y + 5);
            actor_text.editable = true;
            actor_text.selectable = true;
            actor_text.activatable = true;
            actor_text.reactive = true;

            tree_y += actor_text.height + 2*5;
        }
        if (iter is text){
            var tx = (text)iter;

            if (!map_element_actor.has_key(iter)){
                var actor_text = new Text.full("DejaVu Sans Light 12", tx.text, new Clutter.Color.from_string("rgb(10%,10%,10%)"));

                actor_text.text_changed.connect(()=>{
                    tx.text = actor_text.text;
                });
                actor_text.key_press_event.connect((event)=>{
                    if (event.keyval == 65288 && actor_text.text == ""){
                        remove_element(tx);
                        return true;
                    }
                    if (event.keyval == 65293){
                        tx.parent.add_child(new text(""), tx.parent.children.index_of(tx) + 1);
                        build_tree();
                        return true;
                    }
                    return false;
                });
                add_drag_handle(actor_text, tx);
                actor_text.leave_event.connect(()=>{
                    //if (actor_drag_handle != null)
                    //    actor_drag_handle.destroy();
                    return false;
                });

                stage.add(actor_text);
                map_element_actor.set(iter, actor_text);
                stage.key_focus = actor_text;
            }
            var actor_text = (Text)map_element_actor.get(iter);
            actor_text.text = tx.text;
            //actor_text.margin_top = 3;
            //actor_text.margin_bottom = 3;
            actor_text.animate(AnimationMode.EASE_IN_QUAD, animation_time,
                                x: depth * 20.0f + 20.0f,
                                y: tree_y + 3);
            actor_text.width = stage.width - depth * 20.0f * 2;
            actor_text.editable = true;
            actor_text.selectable = true;
            actor_text.reactive = true;
            actor_text.single_line_mode = false;
            actor_text.line_wrap = true;
            tree_y += actor_text.height + 2*3;
        }

        foreach (element child in iter.children){
            build_tree_iter(child, depth + 1);
        }
    }

    void add_drag_handle(Actor actor, element el){
        actor.enter_event.connect(()=>{
            
            actor_drag_handle.height = actor.height;
            actor_drag_handle.reactive = true;
            actor_drag_handle.animate(AnimationMode.EASE_IN_QUAD, animation_time / 4,
                                      opacity: 150,
                                      x: actor.x - 15,
                                      y: actor.y,
                                      width: 15.0f,
                                      height: actor.height);
            
            actor_drag_handle.clear_actions();
            var drag_action = new DragAction();
            drag_action.drag_end.connect(()=>{
                actor_drag_handle.animate(AnimationMode.EASE_IN_QUAD, animation_time,
                                          x: actor.x - 15,
                                          y: actor.y);
                stage.key_focus = actor;
            });
            drag_action.drag_begin.connect(()=>{
                //stage.key_focus = actor;
            });
            drag_action.drag_motion.connect(()=>{
                int drag_depth = (int)Math.floor((actor_drag_handle.x - 20) / 20.0f);
                if (drag_depth < 0)
                    drag_depth = 0;


                element? target = get_element_above_y(actor_drag_handle.y + actor_drag_handle.height / 2, el);
                if (target == null)
                    return;
                
                //Avoid multiple iterations with same values
                if (last_actor_drag_target == target && drag_depth == last_drag_depth)
                    return;
                last_actor_drag_target = target;
                last_drag_depth = drag_depth;

                //Only allow to add a new indentation level if the target is a heading
                if (drag_depth > target.depth)
                    if (!(target is heading))
                        drag_depth = target.depth;
                
                //Determine minimum indentation level by
                element? lower = get_lower_element (el.get_lowest_child()); //And using its succeding element's depth
                if (lower != null)
                    if (lower.depth > drag_depth)
                        drag_depth = lower.depth;
                
                if (drag_depth > target.depth){
                    el.parent.children.remove(el);
                    target.add_child(el, 0);
                } else {
                    element iter = target;
                    for (int q = 0; q < target.depth - drag_depth; q++)
                        iter = iter.parent;
                    
                    el.parent.children.remove(el);
                    int target_index = iter.parent.children.index_of(iter);
                    iter.parent.add_child(el, target_index + 1);
                }
                build_tree();
            });
            actor_drag_handle.add_action(drag_action);
            return false;
        });
    }

    void remove_element(element el){
        var el_new_focus = get_upper_element(el);
        if (el_new_focus != null)
            stage.key_focus = map_element_actor[el_new_focus];

        map_element_actor[el].destroy();
        el.parent.children.remove(el);
        map_element_actor.remove(el);
        build_tree();
    }
    //int old_width;
    //int old_height;

    uint resize_timeout_id = 0;
    void on_embed_size_allocate(){
        //if (widget.height == old_height && widget.width == old_width)
        //    return;
        if (resize_timeout_id != 0)
            Source.remove(resize_timeout_id);
        resize_timeout_id = Timeout.add(250, ()=>{
            build_tree();
            return false;
        });
    }

    /*bool on_stage_embedded_clicked(){
        return false;
    }*/

    public Gtk.Widget widget;
    //public TextView txtCode = null;

}

// vim: set ai ts=4 sts=4 et sw=4