Fix #8062 - fix new file node selection issue
[roobuilder] / src / Builder4 / PopoverAddObject.bjs
1 {
2  "build_module" : "",
3  "gen_extended" : false,
4  "items" : [
5   {
6    "$ xns" : "Gtk",
7    "@ void after_node_change" : "(JsRender.Node? node)",
8    "@ void before_node_change" : "(JsRender.Node? node)",
9    "Gtk.PositionType position" : "Gtk.PositionType.RIGHT",
10    "Xcls_MainWindow mainwindow" : "",
11    "bool active" : false,
12    "bool hexpand" : false,
13    "bool modal" : true,
14    "id" : "PopoverAddObject",
15    "int height_request" : 800,
16    "int width_request" : 900,
17    "items" : [
18     {
19      "$ shadow_type" : "Gtk.ShadowType.IN",
20      "$ xns" : "Gtk",
21      "* init" : [
22       "  this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);",
23       "   this.el.set_size_request(-1,200);",
24       " ",
25       ""
26      ],
27      "* pack" : "set_child",
28      "id" : "viewwin",
29      "items" : [
30       {
31        "$ xns" : "Gtk",
32        "* pack" : "set_child",
33        "bool hexpand" : true,
34        "bool vexpand" : true,
35        "id" : "view",
36        "items" : [
37         {
38          "$ Gdk.DragAction[] actions" : "Gdk.DragAction.COPY   | Gdk.DragAction.MOVE   ",
39          "$ xns" : "Gtk",
40          "listeners" : {
41           "drag_begin" : [
42            "( drag )  => {",
43            "\tGLib.debug(\"SOURCE: drag-begin\");",
44            "\t ",
45            "    // find what is selected in our tree...",
46            "   var data = _this.selmodel.getSelectedNode();",
47            "\tif (data == null) {",
48            "\t\treturn  ;",
49            "\t}",
50            "\t ",
51            "    var xname = data.fqn();",
52            "    GLib.debug (\"XNAME  IS %s\", xname);",
53            "",
54            " \tvar widget = _this.view.getWidgetAtRow(_this.selmodel.el.selected);",
55            " \t",
56            " \t",
57            "    var paintable = new Gtk.WidgetPaintable(widget);",
58            "    this.el.set_icon(paintable, 0,0);",
59            "            ",
60            " ",
61            "}"
62           ],
63           "drag_end" : [
64            "(drag, delete_data) => {",
65            "\t_this.hide();",
66            "",
67            "}",
68            ""
69           ],
70           "prepare" : [
71            "(x, y) => {",
72            "",
73            "\t",
74            "\t",
75            "///\t( drag_context, data, info, time) => {",
76            "            ",
77            "",
78            "\t//print(\"drag-data-get\");",
79            " \tvar ndata = _this.selmodel.getSelectedNode();",
80            "\tif (ndata == null) {",
81            "\t \tGLib.debug(\"return empty string - no selection..\");",
82            "\t\treturn null;",
83            "\t ",
84            "\t}",
85            "",
86            "  ",
87            "\t//data.set_text(tp,tp.length);   ",
88            "",
89            "\tvar \tstr = ndata.toJsonString();",
90            "\tGLib.debug(\"prepare  store: %s\", str);",
91            "\tGLib.Value ov = GLib.Value(typeof(string));",
92            "\tov.set_string(str);",
93            " \tvar cont = new Gdk.ContentProvider.for_value(ov);",
94            "    ",
95            "\t//GLib.Value v = GLib.Value(typeof(string));",
96            "\t//var str = drop.read_text( [ \"text/plain\" ] 0);",
97            "\t ",
98            "\t//cont.get_value(ref v);",
99            "\t//GLib.debug(\"set %s\", v.get_string());",
100            "        ",
101            " \treturn cont;",
102            "\t  ",
103            "}",
104            ""
105           ]
106          },
107          "xtype" : "DragSource"
108         },
109         {
110          "$ xns" : "Gtk",
111          "* prop" : "model",
112          "id" : "selmodel",
113          "items" : [
114           {
115            "$ xns" : "Gtk",
116            "* ctor" : [
117             "new Gtk.TreeListModel(",
118             "    new GLib.ListStore(typeof(JsRender.Node)), //..... << that's our store..",
119             "    false, // passthru",
120             "    true, // autexpand",
121             "    (item) => {",
122             "    \treturn ((JsRender.Node)item).childstore;",
123             "    ",
124             "    }",
125             "    ",
126             "    ",
127             ")"
128            ],
129            "* prop" : "model",
130            "id" : "model",
131            "xtype" : "TreeListModel"
132           }
133          ],
134          "xtype" : "SingleSelection",
135          "| JsRender.Node getNodeAt" : [
136           "(uint row) {",
137           "",
138           "   var tr = (Gtk.TreeListRow)this.el.get_item(row);",
139           "   ",
140           "   var a = tr.get_item();;   ",
141           "   GLib.debug(\"get_item (2) = %s\", a.get_type().name());",
142           "    ",
143           "   return (JsRender.Node)tr.get_item();",
144           "\t ",
145           "}"
146          ],
147          "| JsRender.Node? getSelectedNode" : [
148           "() {",
149           "  if (this.el.selected_item == null) {",
150           "\t\treturn null;",
151           "  }\t\t\t        ",
152           "   var tr = (Gtk.TreeListRow)this.el.selected_item;",
153           "   return (JsRender.Node)tr.get_item();",
154           "\t ",
155           "}"
156          ]
157         },
158         {
159          "$ xns" : "Gtk",
160          "* pack" : "append_column",
161          "bool expand" : true,
162          "id" : "maincol",
163          "items" : [
164           {
165            "$ xns" : "Gtk",
166            "* prop" : "factory",
167            "listeners" : {
168             "bind" : [
169              "(listitem) => {",
170              "\t //GLib.debug(\"listitme is is %s\", ((Gtk.ListItem)listitem).get_type().name());",
171              "\t",
172              "\t//var expand = (Gtk.TreeExpander) ((Gtk.ListItem)listitem).get_child();",
173              " ",
174              "\t ",
175              " \tvar hbox = (Gtk.Box)  ((Gtk.ListItem)listitem).get_child();",
176              " ",
177              " ",
178              "\t",
179              "\tvar img = (Gtk.Image) hbox.get_first_child();",
180              "\tvar lbl = (Gtk.Label) img.get_next_sibling();",
181              "\t",
182              "\tvar lr = (Gtk.TreeListRow)((Gtk.ListItem)listitem).get_item();",
183              "\t",
184              "\t",
185              "\t",
186              "\tvar node = (JsRender.Node) lr.get_item();",
187              "\t",
188              "   GLib.debug(\"node is %s\", node.get_type().name());",
189              "   GLib.debug(\"lbl is %s\", lbl.get_type().name());",
190              "   GLib.debug(\"node fqn %s\", node.fqn());",
191              "// was item (1) in old layout",
192              "",
193              "\t ",
194              " \timg.resource = node.iconResourceName;",
195              " \tlbl.label =  node.fqn();",
196              "// \tlbl.tooltip_markup = node.nodeTip();",
197              " ",
198              "  ",
199              " \t// bind image...",
200              " \t",
201              "}",
202              ""
203             ],
204             "setup" : [
205              "(listitem) => {",
206              "\t",
207              " ",
208              "\tvar hbox = new Gtk.Box(Gtk.Orientation.HORIZONTAL,0);",
209              "\tvar icon = new Gtk.Image();",
210              "\tvar lbl = new Gtk.Label(\"\");",
211              "\tlbl.use_markup = true;",
212              "\t",
213              "\t",
214              " \tlbl.justify = Gtk.Justification.LEFT;",
215              " \tlbl.xalign = 0;",
216              "\tlbl.margin_start = 4;",
217              "//\tlistitem.activatable = true; ??",
218              "\t",
219              "\thbox.append(icon);",
220              "\thbox.append(lbl);",
221              "\t ",
222              "\t((Gtk.ListItem)listitem).set_child(hbox);",
223              "\t ",
224              "}",
225              ""
226             ]
227            },
228            "xtype" : "SignalListItemFactory"
229           }
230          ],
231          "title" : "Drag to add Object",
232          "xtype" : "ColumnViewColumn"
233         },
234         {
235          "$ xns" : "Gtk",
236          "listeners" : {
237           "pressed" : [
238            "(n_press, x, y) => {",
239            "\tif (n_press < 2) { /// doubleclick?",
240            "\t\treturn;",
241            "\t}",
242            "\t//var pos = \"\";",
243            "\t// find left tree selected node",
244            "\tvar ws =_this.mainwindow.windowstate;",
245            "\tvar lt = ws.left_tree;",
246            "",
247            "\tvar add = _this.selmodel.getSelectedNode().deepClone();",
248            "\tif (lt.model.el.n_items < 1) {",
249            "\t\tws.file.tree = add;  ",
250            "    \tadd.updated_count++;",
251            "    \tvar m = (GLib.ListStore) lt.model.el.model;",
252            "\t\tm.append(add);",
253            "\t\tlt.model.selectNode(add); \t",
254            "\t\tlt.changed();",
255            "\t\tlt.node_selected(add);",
256            "\t\treturn;",
257            "\t}",
258            "\tvar addto = _this.mainwindow.windowstate.left_tree.selmodel.getSelectedNode();\t",
259            "\t//var row = _this.view.getRowAt(x,y, out pos);",
260            "\t",
261            "",
262            "\taddto.appendChild(add);",
263            "\t_this.mainwindow.windowstate.left_props.changed();",
264            "\t_this.mainwindow.windowstate.left_tree.model.selectNode(add);",
265            "}",
266            ""
267           ]
268          },
269          "xtype" : "GestureClick"
270         }
271        ],
272        "xtype" : "ColumnView",
273        "| Gtk.Widget? getWidgetAt" : [
274         "(double x,  double in_y) {",
275         "/*",
276         "    \t",
277         "from    \thttps://discourse.gnome.org/t/gtk4-finding-a-row-data-on-gtkcolumnview/8465",
278         "    \tvar colview = gesture.widget;",
279         "    \tvar line_no = check_list_widget(colview, x,y);",
280         "         if (line_no > -1) {",
281         "    \t\tvar item = colview.model.get_item(line_no);",
282         "    \t\t ",
283         "    \t}",
284         "    \t*/",
285         "    \tvar y = in_y + _this.viewwin.el.vadjustment.value; ",
286         "        var  child = this.el.get_first_child(); ",
287         "    \t//Gtk.Allocation alloc = { 0, 0, 0, 0 };",
288         "    \tvar line_no = -1; ",
289         "    \tvar reading_header = true;",
290         "    \tvar curr_y = 0;",
291         "    \tvar header_height  = 0;",
292         "    \tvar h = 0;",
293         "    \twhile (child != null) {",
294         "\t\t\t//GLib.debug(\"Got %s\", child.get_type().name());",
295         "\t        if (reading_header) {",
296         "\t\t\t\t",
297         "",
298         "\t\t\t\tif (child.get_type().name() != \"GtkColumnListView\") {",
299         "\t\t\t        h += child.get_height();",
300         "\t\t\t\t\tchild = child.get_next_sibling();",
301         "\t\t\t\t\tcontinue;",
302         "\t\t\t\t}",
303         "\t\t\t\t// should be columnlistview",
304         "\t\t\t\tchild = child.get_first_child(); ",
305         "\t\t\t    GLib.debug(\"header height=%d\", h);",
306         "\t\t\t\theader_height =  h;",
307         "\t\t\t\t",
308         "\t\t\t\treading_header = false;",
309         "\t\t\t\t",
310         "\t        }",
311         "\t\t    line_no++;",
312         "",
313         "\t\t\tif (y < header_height) {",
314         "\t\t    \treturn null;",
315         "\t    \t}",
316         "",
317         "\t\t\tvar hh = child.get_height();",
318         "\t\t\t//GLib.debug(\"got cell xy = %d,%d  w,h= %d,%d\", alloc.x, alloc.y, alloc.width, alloc.height);",
319         "",
320         "\t\t    if (y > curr_y && y <= header_height + hh + curr_y ) {",
321         "\t\t\t    return (Gtk.Widget)child;",
322         "\t\t    }",
323         "\t\t    curr_y +=  hh ;",
324         "",
325         "\t\t    if (curr_y > y) {",
326         "\t\t        return null;",
327         "\t        }",
328         "\t        child = child.get_next_sibling(); ",
329         "    \t}",
330         "        return null;",
331         "",
332         " }"
333        ],
334        "| Gtk.Widget? getWidgetAtRow" : [
335         "(uint row) {",
336         "/*",
337         "// ?? could be done with model?",
338         "    \t",
339         "from    \thttps://discourse.gnome.org/t/gtk4-finding-a-row-data-on-gtkcolumnview/8465",
340         "    \tvar colview = gesture.widget;",
341         "    \tvar line_no = check_list_widget(colview, x,y);",
342         "         if (line_no > -1) {",
343         "    \t\tvar item = colview.model.get_item(line_no);",
344         "    \t\t ",
345         "    \t}",
346         "    \t*/",
347         "\t\tGLib.debug(\"Get Widget At Row %d\", (int)row);",
348         "        var  child = this.el.get_first_child(); ",
349         "    \tvar line_no = -1; ",
350         "    \tvar reading_header = true;",
351         "",
352         "    \twhile (child != null) {",
353         "\t\t\tGLib.debug(\"Got %s\", child.get_type().name());",
354         "    \t    if (reading_header) {",
355         "\t\t\t ",
356         "\t\t\t   ",
357         "\t\t\t\tif (child.get_type().name() != \"GtkColumnListView\") {",
358         "\t\t\t\t\tchild = child.get_next_sibling();",
359         "\t\t\t\t\tcontinue;",
360         "\t\t\t\t}",
361         "\t\t\t\tchild = child.get_first_child(); ",
362         "\t\t\t\treading_header = false;",
363         "\t        }",
364         "\t\t    if (child.get_type().name() != \"GtkColumnViewRowWidget\") {",
365         "    \t\t    child = child.get_next_sibling();",
366         "    \t\t    continue;",
367         "\t\t    }",
368         "\t\t    line_no++;",
369         "\t\t\tif (line_no == row) {",
370         "\t\t\t\tGLib.debug(\"Returning widget %s\", child.get_type().name());",
371         "\t\t\t    return (Gtk.Widget)child;",
372         "\t\t    }",
373         "\t        child = child.get_next_sibling(); ",
374         "    \t}",
375         "\t\tGLib.debug(\"Rturning null\");",
376         "        return null;",
377         "",
378         " }"
379        ],
380        "| int getRowAt" : [
381         "(double x,  double in_y, out string pos) {",
382         "",
383         "",
384         "\t ",
385         "",
386         "/*",
387         "    \t",
388         "from    \thttps://discourse.gnome.org/t/gtk4-finding-a-row-data-on-gtkcolumnview/8465",
389         "    \tvar colview = gesture.widget;",
390         "    \tvar line_no = check_list_widget(colview, x,y);",
391         "         if (line_no > -1) {",
392         "    \t\tvar item = colview.model.get_item(line_no);",
393         "    \t\t ",
394         "    \t}",
395         "    \t*/",
396         " \t\t ",
397         " \t\t",
398         " \t\t//GLib.debug(\"offset = %d  y = %d\", (int) voff, (int) in_y);",
399         "    \tvar y = in_y + _this.viewwin.el.vadjustment.value; ",
400         "        var  child = this.el.get_first_child(); ",
401         "    \t//Gtk.Allocation alloc = { 0, 0, 0, 0 };",
402         "    \tvar line_no = -1; ",
403         "    \tvar reading_header = true;",
404         "    \tvar real_y = 0;",
405         "    \tvar header_height  = 0;",
406         "    \tpos = \"none\";",
407         "    \tvar h = 0;",
408         "    \twhile (child != null) {",
409         "\t\t\t//GLib.debug(\"Got %s\", child.get_type().name());",
410         "    \t    if (reading_header) {",
411         "\t\t\t\t",
412         "",
413         "\t\t\t\tif (child.get_type().name() != \"GtkColumnListView\") {",
414         "\t\t\t        h += child.get_height();",
415         "\t\t\t\t\tchild = child.get_next_sibling();",
416         "\t\t\t\t\tcontinue;",
417         "\t\t\t\t}",
418         "\t\t\t\t// should be columnlistview",
419         "\t\t\t\tchild = child.get_first_child(); ",
420         "\t\t\t    GLib.debug(\"header height=%d\", h);",
421         "\t\t\t\theader_height =  h;",
422         "\t\t\t\t",
423         "\t\t\t\treading_header = false;",
424         "\t\t\t\t",
425         "\t        }",
426         "\t        ",
427         "\t\t    if (child.get_type().name() != \"GtkColumnViewRowWidget\") {",
428         "    \t\t    child = child.get_next_sibling();",
429         "    \t\t    continue;",
430         "\t\t    }",
431         "\t\t    ",
432         "\t\t \tif (y < header_height) {",
433         "\t\t    \treturn -1;",
434         "\t    \t}",
435         "\t\t    ",
436         "\t\t    line_no++;",
437         "\t\t\tvar hh = child.get_height();",
438         "\t\t\t//child.get_allocation(out alloc);",
439         "\t\t\t//GLib.debug(\"got cell xy = %d,%d  w,h= %d,%d\", alloc.x, alloc.y, alloc.width, alloc.height);",
440         "\t\t\t//GLib.debug(\"row %d y= %d %s\", line_no, (int) (header_height + alloc.y),",
441         "\t\t\t",
442         "\t\t\t//\tchild.visible ? \"VIS\" : \"hidden\");",
443         "",
444         "\t\t    if (y >  (header_height + real_y) && y <= (header_height +  real_y + hh) ) {",
445         "\t\t    \tif (y > ( header_height + real_y + (hh * 0.8))) {",
446         "\t\t    \t\tpos = \"below\";",
447         "\t    \t\t} else if (y > ( header_height + real_y + (hh * 0.2))) {",
448         "\t    \t\t\tpos = \"over\";",
449         "    \t\t\t} else {",
450         "    \t\t\t\tpos = \"above\";",
451         "\t\t\t\t}",
452         "\t\t    \t GLib.debug(\"getRowAt return : %d, %s\", line_no, pos);",
453         "\t\t\t    return line_no;",
454         "\t\t    }",
455         " ",
456         "",
457         "\t\t    if (real_y + hh > y) {",
458         "\t\t        return -1;",
459         "\t        }",
460         "\t        real_y += hh;",
461         "\t        child = child.get_next_sibling(); ",
462         "    \t}",
463         "        return -1;",
464         "",
465         " }"
466        ]
467       }
468      ],
469      "xtype" : "ScrolledWindow"
470     }
471    ],
472    "xtype" : "Popover",
473    "| void a_clear" : [
474     "() {",
475     "    var m = (GLib.ListStore) _this.model.el.model;",
476     "\tm.remove_all();",
477     "",
478     "\t",
479     "",
480     "}",
481     ""
482    ],
483    "| void hide" : [
484     "() {",
485     " ",
486     "\tthis.el.hide();",
487     "}",
488     ""
489    ],
490    "| void show" : [
491     "(Palete.Palete pal, string cls,  Gtk.Widget onbtn) {",
492     "",
493     "     ",
494     "",
495     "    var tr = pal.getChildList(cls, false);",
496     "    var m = (GLib.ListStore) _this.model.el.model;",
497     "\tm.remove_all();",
498     "",
499     "\t",
500     "\t// new version will not support properties here..",
501     "\t// they will be part of the properties, clicking will add a node..",
502     "\t// will change the return list above eventually?",
503     "\t",
504     " ",
505     "\tforeach (var dname in tr) {",
506     "\t\t ",
507     "",
508     "\t\tGLib.debug(\"add to model: %s\", dname);\t\t",
509     "\t\tm.append(pal.fqnToNode(dname));",
510     "\t}",
511     "\t m.sort( (a, b) => {",
512     "",
513     "\t\t\treturn Posix.strcmp( ((JsRender.Node)a).fqn(),  ((JsRender.Node)b).fqn());",
514     "\t\t\t",
515     "\t\t});",
516     "\t ",
517     "    ",
518     "    var win = this.mainwindow.el;",
519     "    //var  w = win.get_width();",
520     "    var h = win.get_height();",
521     "",
522     "    ",
523     "    // left tree = 250, editor area = 500?",
524     "    ",
525     "    // min 450?",
526     "\t// max hieght ...",
527     "    this.el.set_size_request( 350, h); // full height?",
528     "",
529     "      this.el.set_parent(onbtn);",
530     "",
531     "    //if (this.el.relative_to == null) {",
532     "    \t//Gtk.Allocation rect;",
533     "    \t//onbtn.get_allocation(out rect);",
534     "      //  this.el.set_pointing_to(rect);",
535     "    //}",
536     "    this.selmodel.el.set_selected(Gtk.INVALID_LIST_POSITION);",
537     "    this.el.show();",
538     "   ",
539     "}",
540     ""
541    ]
542   }
543  ],
544  "name" : "PopoverAddObject"
545 }