Fix #7238 - remove clutter and change layout of interface
[roobuilder] / src / Builder4 / GtkView.bjs
1 {
2  "build_module" : "builder",
3  "items" : [
4   {
5    "# Gtk.Widget lastObj" : "null",
6    "# JsRender.JsRender file" : "null",
7    "# Xcls_MainWindow main_window" : "",
8    "$ xns" : "Gtk",
9    "Gtk.Orientation orientation" : "Gtk.Orientation.VERTICAL",
10    "Gtk.SourceSearchContext searchcontext" : "",
11    "bool hexpand" : true,
12    "bool vexpand" : true,
13    "id" : "GtkView",
14    "int height" : 0,
15    "int last_search_end" : 0,
16    "int width" : 0,
17    "items" : [
18     {
19      "$ xns" : "Gtk",
20      "* pack" : "pack_start,true,true,0",
21      "id" : "notebook",
22      "items" : [
23       {
24        "$ xns" : "Gtk",
25        "* pack" : false,
26        "id" : "label_preview",
27        "utf8 label" : "Preview",
28        "xtype" : "Label"
29       },
30       {
31        "$ xns" : "Gtk",
32        "* pack" : false,
33        "id" : "label_code",
34        "utf8 label" : "Preview Generated Code",
35        "xtype" : "Label"
36       },
37       {
38        "$ xns" : "Gtk",
39        "* pack" : "append_page,_this.label_preview.el",
40        "items" : [
41         {
42          "$ xns" : "Gtk",
43          "* pack" : "add",
44          "id" : "view_layout",
45          "items" : [
46           {
47            "$ xns" : "Gtk",
48            "* pack" : "put,10,10",
49            "Gtk.Orientation orientation" : "Gtk.Orientation.HORIZONTAL",
50            "id" : "container",
51            "xtype" : "Box"
52           }
53          ],
54          "xtype" : "Layout"
55         }
56        ],
57        "xtype" : "ScrolledWindow"
58       },
59       {
60        "$ xns" : "Gtk",
61        "* pack" : "append_page,_this.label_code.el",
62        "items" : [
63         {
64          "$ xns" : "Gtk",
65          "* init" : [
66           "{",
67           "   ",
68           "    var description =   Pango.FontDescription.from_string(\"monospace\");",
69           "    description.set_size(8000);",
70           "    this.el.override_font(description);",
71           "",
72           "    this.loading = true;",
73           "    var buf = this.el.get_buffer();",
74           "    buf.notify.connect((ps) => {",
75           "        if (this.loading) {",
76           "            return;",
77           "        }",
78           "        if (ps.name != \"cursor-position\") {",
79           "            return;",
80           "        }",
81           "        print(\"cursor changed : %d\\n\", buf.cursor_position);",
82           "        Gtk.TextIter cpos;",
83           "        buf.get_iter_at_offset(out cpos, buf.cursor_position);",
84           "        ",
85           "        var ln = cpos.get_line();",
86           " ",
87           "        var node = _this.file.lineToNode(ln);",
88           " ",
89           "        if (node == null) {",
90           "            print(\"can not find node\\n\");",
91           "            return;",
92           "        }",
93           "        var ltree = _this.main_window.windowstate.left_tree;",
94           "        var tp = ltree.model.treePathFromNode(node);",
95           "        print(\"got tree path %s\\n\", tp);",
96           "        if (tp != \"\") {",
97           "\t       this.allow_node_scroll = false;        ",
98           "\t       print(\"changing cursor on tree..\\n\");",
99           "            ltree.view.el.set_cursor(new Gtk.TreePath.from_string(tp), null, false);",
100           "            // scrolling is disabled... as node selection calls scroll 10ms after it changes.",
101           "            GLib.Timeout.add_full(GLib.Priority.DEFAULT,100 , () => {",
102           "\t            this.allow_node_scroll = true;",
103           "\t            return false;",
104           "            });",
105           "        }",
106           "        ",
107           "        // highlight the node..",
108           "        ",
109           "    });",
110           "  ",
111           "  ",
112           "  ",
113           "    var attrs = new Gtk.SourceMarkAttributes();",
114           "    var  pink =   Gdk.RGBA();",
115           "    pink.parse ( \"pink\");",
116           "    attrs.set_background ( pink);",
117           "    attrs.set_icon_name ( \"process-stop\");    ",
118           "    attrs.query_tooltip_text.connect(( mark) => {",
119           "        //print(\"tooltip query? %s\\n\", mark.name);",
120           "        return mark.name;",
121           "    });",
122           "    ",
123           "    this.el.set_mark_attributes (\"ERR\", attrs, 1);",
124           "    ",
125           "     var wattrs = new Gtk.SourceMarkAttributes();",
126           "    var  blue =   Gdk.RGBA();",
127           "    blue.parse ( \"#ABF4EB\");",
128           "    wattrs.set_background ( blue);",
129           "    wattrs.set_icon_name ( \"process-stop\");    ",
130           "    wattrs.query_tooltip_text.connect(( mark) => {",
131           "        //print(\"tooltip query? %s\\n\", mark.name);",
132           "        return mark.name;",
133           "    });",
134           "    ",
135           "    this.el.set_mark_attributes (\"WARN\", wattrs, 1);",
136           "    ",
137           " ",
138           "    ",
139           "     var dattrs = new Gtk.SourceMarkAttributes();",
140           "    var  purple =   Gdk.RGBA();",
141           "    purple.parse ( \"#EEA9FF\");",
142           "    dattrs.set_background ( purple);",
143           "    dattrs.set_icon_name ( \"process-stop\");    ",
144           "    dattrs.query_tooltip_text.connect(( mark) => {",
145           "        //print(\"tooltip query? %s\\n\", mark.name);",
146           "        return mark.name;",
147           "    });",
148           "    ",
149           "    this.el.set_mark_attributes (\"DEPR\", dattrs, 1);",
150           "    ",
151           "    ",
152           "    var gattrs = new Gtk.SourceMarkAttributes();",
153           "    var  grey =   Gdk.RGBA();",
154           "    grey.parse ( \"#ccc\");",
155           "    gattrs.set_background ( grey);",
156           " ",
157           "    ",
158           "    this.el.set_mark_attributes (\"grey\", gattrs, 1);",
159           "    ",
160           "    ",
161           "    ",
162           "    ",
163           "    ",
164           "    ",
165           "}",
166           " "
167          ],
168          "* pack" : "add",
169          "bool allow_node_scroll" : true,
170          "bool loading" : true,
171          "gboolean editable" : false,
172          "gboolean show_line_marks" : true,
173          "gboolean show_line_numbers" : true,
174          "id" : "sourceview",
175          "xtype" : "SourceView",
176          "| string toString" : [
177           "() {",
178           "   Gtk.TextIter s;",
179           "    Gtk.TextIter e;",
180           "    this.el.get_buffer().get_start_iter(out s);",
181           "    this.el.get_buffer().get_end_iter(out e);",
182           "    var ret = this.el.get_buffer().get_text(s,e,true);",
183           "    //print(\"TO STRING? \" + ret);",
184           "    return ret;",
185           "}",
186           ""
187          ],
188          "| void highlightErrorsJson" : [
189           " (string type, Json.Object obj) {",
190           "      Gtk.TextIter start;",
191           "     Gtk.TextIter end;   ",
192           "     ",
193           "     var buf =  this.el.get_buffer();",
194           "       var sbuf = (Gtk.SourceBuffer)buf;",
195           "        buf.get_bounds (out start, out end);",
196           "        ",
197           "        sbuf.remove_source_marks (start, end, type);",
198           "                 ",
199           "     ",
200           "     // we should highlight other types of errors..",
201           "    ",
202           "    if (!obj.has_member(type)) {",
203           "        print(\"Return has no errors\\n\");",
204           "        return  ;",
205           "    }",
206           "    var err = obj.get_object_member(type);",
207           "    ",
208           "    if (_this.file == null) { ",
209           "        return; // just in case the file has not loaded yet?",
210           "    }",
211           " ",
212           "",
213           "    var valafn = \"\";",
214           "      try {             ",
215           "           var  regex = new Regex(\"\\\\.bjs$\");",
216           "        ",
217           "         ",
218           "            valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , \".vala\");",
219           "         } catch (GLib.RegexError e) {",
220           "            return;",
221           "        }   ",
222           "",
223           "   if (!err.has_member(valafn)) {",
224           "        print(\"File path has no errors\\n\");",
225           "        return  ;",
226           "    }",
227           "    var lines = err.get_object_member(valafn);",
228           "    ",
229           "   ",
230           "    ",
231           "    var tlines = buf.get_line_count () +1;",
232           "    ",
233           "    lines.foreach_member((obj, line, node) => {",
234           "        ",
235           "             Gtk.TextIter iter;",
236           "    //        print(\"get inter\\n\");",
237           "            var eline = int.parse(line) -1  ;",
238           "            print(\"GOT ERROR on line %s -- converted to %d\\n\", line,eline);",
239           "            ",
240           "            ",
241           "            if (eline > tlines || eline < 0) {",
242           "                return;",
243           "            }",
244           "            sbuf.get_iter_at_line( out iter, eline);",
245           "            //print(\"mark line\\n\");",
246           "            var msg  = type + \" on line: %d - %s\".printf(eline+1, valafn);",
247           "            var ar = lines.get_array_member(line);",
248           "            for (var i = 0 ; i < ar.get_length(); i++) {",
249           "\t\t    msg += (msg.length > 0) ? \"\\n\" : \"\";",
250           "\t\t    msg += ar.get_string_element(i);",
251           "\t    }",
252           "            ",
253           "            ",
254           "            sbuf.create_source_mark(msg, type, iter);",
255           "        } );",
256           "        return  ;",
257           "    ",
258           " ",
259           "",
260           "",
261           "}"
262          ],
263          "| void loadFile" : [
264           "( ) {",
265           "    this.loading = true;",
266           "    var buf = this.el.get_buffer();",
267           "    buf.set_text(\"\",0);",
268           "    var sbuf = (Gtk.SourceBuffer) buf;",
269           "",
270           "    ",
271           "",
272           "    if (_this.file == null || _this.file.xtype != \"Gtk\") {",
273           "        print(\"xtype != Gtk\");",
274           "        this.loading = false;",
275           "        return;",
276           "    }",
277           "    ",
278           "    var valafn = \"\";",
279           "      try {             ",
280           "           var  regex = new Regex(\"\\\\.bjs$\");",
281           "        ",
282           "         ",
283           "            valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , \".vala\");",
284           "         } catch (GLib.RegexError e) {",
285           "             this.loading = false;",
286           "            return;",
287           "        }   ",
288           "    ",
289           "",
290           "   if (!FileUtils.test(valafn,FileTest.IS_REGULAR) ) {",
291           "        print(\"File path has no errors\\n\");",
292           "        this.loading = false;",
293           "        return  ;",
294           "    }",
295           "    ",
296           "    string str;",
297           "    try {",
298           "    ",
299           "        GLib.FileUtils.get_contents (valafn, out str);",
300           "    } catch (Error e) {",
301           "        this.loading = false;",
302           "        return  ;",
303           "    }",
304           "",
305           "//    print(\"setting str %d\\n\", str.length);",
306           "    buf.set_text(str, str.length);",
307           "    var lm = Gtk.SourceLanguageManager.get_default();",
308           "     ",
309           "    //?? is javascript going to work as js?",
310           "    ",
311           "    ((Gtk.SourceBuffer)(buf)) .set_language(lm.get_language(_this.file.language));",
312           "  ",
313           "    ",
314           "    Gtk.TextIter start;",
315           "    Gtk.TextIter end;     ",
316           "        ",
317           "    sbuf.get_bounds (out start, out end);",
318           "    sbuf.remove_source_marks (start, end, null); // remove all marks..",
319           "    ",
320           "    ",
321           "    if (_this.main_window.windowstate.last_compile_result != null) {",
322           "        var obj = _this.main_window.windowstate.last_compile_result;",
323           "        this.highlightErrorsJson(\"ERR\", obj);",
324           "        this.highlightErrorsJson(\"WARN\", obj);",
325           "        this.highlightErrorsJson(\"DEPR\", obj);\t\t\t",
326           "    }",
327           "    //while (Gtk.events_pending()) {",
328           "     //   Gtk.main_iteration();",
329           "   // }",
330           "    ",
331           "    this.loading = false; ",
332           "}",
333           ""
334          ],
335          "| void nodeSelected" : [
336           "(JsRender.Node? sel) {",
337           "  ",
338           "    ",
339           "  ",
340           "    // this is connected in widnowstate",
341           "    print(\"Roo-view - node selected\\n\");",
342           "    var buf = this.el.get_buffer();",
343           " ",
344           "    var sbuf = (Gtk.SourceBuffer) buf;",
345           "",
346           "   ",
347           "    while(Gtk.events_pending()) {",
348           "        Gtk.main_iteration();",
349           "    }",
350           "    ",
351           "   ",
352           "    // clear all the marks..",
353           "     Gtk.TextIter start;",
354           "    Gtk.TextIter end;     ",
355           "        ",
356           "    sbuf.get_bounds (out start, out end);",
357           "    sbuf.remove_source_marks (start, end, \"grey\");",
358           "    ",
359           "    ",
360           "     if (sel == null) {",
361           "        // no highlighting..",
362           "        return;",
363           "    }",
364           "    Gtk.TextIter iter;   ",
365           "    sbuf.get_iter_at_line(out iter,  sel.line_start);",
366           "    ",
367           "    ",
368           "    Gtk.TextIter cur_iter;",
369           "    sbuf.get_iter_at_offset(out cur_iter, sbuf.cursor_position);",
370           "    ",
371           "    //var cur_line = cur_iter.get_line();",
372           "    //if (cur_line > sel.line_start && cur_line < sel.line_end) {",
373           "    ",
374           "    //} else {",
375           "    if (this.allow_node_scroll) {",
376           "\t\t ",
377           "    \tthis.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);",
378           "\t}",
379           "    ",
380           "     ",
381           "    ",
382           "    for (var i = 0; i < buf.get_line_count();i++) {",
383           "        if (i < sel.line_start || i > sel.line_end) {",
384           "           ",
385           "            sbuf.get_iter_at_line(out iter, i);",
386           "            sbuf.create_source_mark(null, \"grey\", iter);",
387           "            ",
388           "        }",
389           "    ",
390           "    }",
391           "    ",
392           "",
393           "}",
394           ""
395          ]
396         }
397        ],
398        "xtype" : "ScrolledWindow"
399       }
400      ],
401      "xtype" : "Notebook"
402     }
403    ],
404    "listeners" : {
405     "size_allocate" : [
406      "(aloc) => {",
407      " ",
408      "    this.width = aloc.width;",
409      "    this.height =aloc.height;",
410      "}",
411      " "
412     ]
413    },
414    "xtype" : "Box",
415    "| int search" : [
416     "(string txt) {",
417     "\tthis.notebook.el.page = 1;",
418     " \tvar s = new Gtk.SourceSearchSettings();",
419     "\tvar buf = (Gtk.SourceBuffer) this.sourceview.el.get_buffer();",
420     "\tthis.searchcontext = new Gtk.SourceSearchContext(buf,s);",
421     "\tthis.searchcontext.set_highlight(true);",
422     "\ts.set_search_text(txt);",
423     "\t",
424     "\tGtk.TextIter beg, st,en;",
425     "\t ",
426     "\tbuf.get_start_iter(out beg);",
427     "\tthis.searchcontext.forward(beg, out st, out en);",
428     "\tthis.last_search_end  = 0;",
429     "\treturn this.searchcontext.get_occurrences_count();",
430     "",
431     "   ",
432     "}",
433     ""
434    ],
435    "| void createThumb" : [
436     "() {",
437     "    ",
438     "    ",
439     "    if (this.file == null) {",
440     "        return;",
441     "    }",
442     "    // only screenshot the gtk preview..",
443     "    if (this.notebook.el.page > 0 ) {",
444     "        return;",
445     "    }",
446     "    ",
447     "    ",
448     "    var filename = this.file.getIconFileName(false);",
449     "    ",
450     "    var  win = this.el.get_parent_window();",
451     "    var width = win.get_width();",
452     "    var height = win.get_height();",
453     "    try {",
454     "         Gdk.Pixbuf screenshot = Gdk.pixbuf_get_from_window(win, 0, 0, width, height); // this.el.position?",
455     "         screenshot.save(filename,\"png\");",
456     "    } catch (Error e) {",
457     "        ",
458     "    }",
459     "",
460     "   ",
461     "    return;",
462     "    ",
463     "    ",
464     "     ",
465     "     ",
466     "    ",
467     "    // should we hold until it's printed...",
468     "    ",
469     "      ",
470     "",
471     "    ",
472     "    ",
473     "",
474     "",
475     "    ",
476     "     ",
477     "}",
478     ""
479    ],
480    "| void forwardSearch" : [
481     "(bool change_focus) {",
482     "",
483     "\tif (this.searchcontext == null) {",
484     "\t\treturn;",
485     "\t}",
486     "\tthis.notebook.el.page = 1;",
487     "\tGtk.TextIter beg, st,en, stl;",
488     "\t",
489     "\tvar buf = this.sourceview.el.get_buffer();",
490     "\tbuf.get_iter_at_offset(out beg, this.last_search_end);",
491     "\tif (!this.searchcontext.forward(beg, out st, out en)) {",
492     "\t\tthis.last_search_end = 0;",
493     "\t} else { ",
494     "\t\tthis.last_search_end = en.get_offset();",
495     "\t\tif (change_focus) {",
496     "\t\t\tthis.sourceview.el.grab_focus();\t",
497     "\t\t}",
498     "\t\tbuf.place_cursor(st);",
499     "\t\tvar ln = st.get_line();",
500     "\t\tbuf.get_iter_at_line(out stl,ln);",
501     "\t\t ",
502     "\t\tthis.sourceview.el.scroll_to_iter(stl,  0.0f, true, 0.0f, 0.5f);",
503     "\t}",
504     "",
505     "}",
506     ""
507    ],
508    "| void loadFile" : [
509     "(JsRender.JsRender file) ",
510     "{",
511     "        this.file = null;",
512     "        ",
513     "        if (file.tree == null) {",
514     "            return;",
515     "        }",
516     "        this.notebook.el.page = 0;// gtk preview ",
517     "   ",
518     "  ",
519     "        ",
520     "       this.file = file;     ",
521     "        this.sourceview.loadFile();",
522     "        this.searchcontext = null;",
523     "        ",
524     "",
525     "        if (this.lastObj != null) {",
526     "            this.container.el.remove(this.lastObj);",
527     "        }",
528     "        ",
529     "        // hide the compile view at present..",
530     "          ",
531     "        ",
532     "        var w = this.width;",
533     "        var h = this.height;",
534     "        ",
535     "        print(\"ALLOC SET SIZES %d, %d\\n\", w,h); ",
536     "        ",
537     "        // set the container size min to 500/500 or 20 px less than max..",
538     "        w = int.max (w-20, 500);",
539     "        h = int.max (h-20, 500); ",
540     "        ",
541     "        print(\"SET SIZES %d, %d\\n\", w,h);       ",
542     "        _this.container.el.set_size_request(w,h);",
543     "        ",
544     "        _this.view_layout.el.set_size(w,h); // should be baded on calc.. -- see update_scrolled.",
545     "        var rgba = Gdk.RGBA ();",
546     "        rgba.parse (\"#ccc\");",
547     "        _this.view_layout.el.override_background_color(Gtk.StateFlags.NORMAL, rgba);",
548     "        ",
549     "        ",
550     "\tvar x = new JsRender.NodeToGtk((Project.Gtk) file.project, file.tree);",
551     "        var obj = x.munge() as Gtk.Widget;",
552     "        this.lastObj = null;",
553     "\tif (obj == null) {",
554     "        \treturn;",
555     "\t}",
556     "\tthis.lastObj = obj;",
557     "        ",
558     "        this.container.el.add(obj);",
559     "        obj.show_all();",
560     "        ",
561     "         ",
562     "        ",
563     "}",
564     " "
565    ],
566    "| void scroll_to_line" : [
567     "(int line) {",
568     "   this.notebook.el.page = 1;// code preview...",
569     "   ",
570     "   GLib.Timeout.add(500, () => {",
571     "   ",
572     "   ",
573     "\t   ",
574     "\t   ",
575     "\t\t  var buf = this.sourceview.el.get_buffer();",
576     "\t ",
577     "\t\tvar sbuf = (Gtk.SourceBuffer) buf;",
578     "",
579     "",
580     "\t\tGtk.TextIter iter;   ",
581     "\t\tsbuf.get_iter_at_line(out iter,  line);",
582     "\t\tthis.sourceview.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);",
583     "\t\treturn false;",
584     "\t});   ",
585     "",
586     "   ",
587     "}",
588     ""
589    ]
590   }
591  ],
592  "modOrder" : "",
593  "name" : "GtkView",
594  "parent" : "",
595  "path" : "/home/alan/gitlive/roobuilder/src/Builder4/GtkView.bjs",
596  "permname" : "",
597  "title" : ""
598 }