src/Builder4/WindowLeftTree.bjs
[app.Builder.js] / src / Builder4 / WindowLeftTree.vala
1 static Xcls_WindowLeftTree  _WindowLeftTree;
2
3 public class Xcls_WindowLeftTree : Object
4 {
5     public Gtk.ScrolledWindow el;
6     private Xcls_WindowLeftTree  _this;
7
8     public static Xcls_WindowLeftTree singleton()
9     {
10         if (_WindowLeftTree == null) {
11             _WindowLeftTree= new Xcls_WindowLeftTree();
12         }
13         return _WindowLeftTree;
14     }
15     public Xcls_view view;
16     public Xcls_model model;
17     public Xcls_renderer renderer;
18     public Xcls_LeftTreeMenu LeftTreeMenu;
19
20         // my vars (def)
21     public signal bool before_node_change (JsRender.Node? node);
22     public signal void changed ();
23     public signal void node_selected (JsRender.Node? node);
24     public Xcls_MainWindow main_window;
25
26     // ctor
27     public Xcls_WindowLeftTree()
28     {
29         _this = this;
30         this.el = new Gtk.ScrolledWindow( null, null );
31
32         // my vars (dec)
33         this.main_window = null;
34
35         // set gobject values
36         this.el.shadow_type = Gtk.ShadowType.IN;
37         var child_0 = new Xcls_view( _this );
38         child_0.ref();
39         this.el.add (  child_0.el  );
40         var child_1 = new Xcls_LeftTreeMenu( _this );
41         child_1.ref();
42
43         // init method
44
45         this.el.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
46     }
47
48     // user defined functions
49     public           JsRender.Node? getActiveElement () { // return path to actie node.
50     
51          var path = this.getActivePath();
52          if (path.length < 1) {
53             return null;
54          }
55          return _this.model.pathToNode(path);
56          
57     }
58     public           JsRender.JsRender getActiveFile () {
59         return this.main_window.windowstate.file;
60     }
61     public           string getActivePath () {
62         
63         var view = this.view.el;
64         if (view.get_selection().count_selected_rows() < 1) {
65             return "";
66         }
67         Gtk.TreeIter iter;
68         Gtk.TreeModel mod;
69         view.get_selection().get_selected(out mod, out iter);
70         return mod.get_path(iter).to_string();
71     }
72     public class Xcls_view : Object
73     {
74         public Gtk.TreeView el;
75         private Xcls_WindowLeftTree  _this;
76
77
78             // my vars (def)
79         public string dragData;
80         public int drag_x;
81         public string[] dropList;
82         public int drag_y;
83         public bool drag_in_motion;
84         public bool blockChanges;
85
86         // ctor
87         public Xcls_view(Xcls_WindowLeftTree _owner )
88         {
89             _this = _owner;
90             _this.view = this;
91             this.el = new Gtk.TreeView();
92
93             // my vars (dec)
94             this.blockChanges = false;
95
96             // set gobject values
97             this.el.expand = true;
98             this.el.tooltip_column = 1;
99             this.el.enable_tree_lines = true;
100             this.el.headers_visible = false;
101             var child_0 = new Xcls_model( _this );
102             child_0.ref();
103             this.el.set_model (  child_0.el  );
104             var child_1 = new Xcls_TreeViewColumn4( _this );
105             child_1.ref();
106             this.el.append_column (  child_1.el  );
107
108             // init method
109
110             {
111                 var description = new Pango.FontDescription();
112                 description.set_size(8000);
113                 this.el.override_font(description);
114             
115                 var selection = this.el.get_selection();
116                 selection.set_mode( Gtk.SelectionMode.SINGLE);
117             
118             
119                 // is this really needed??
120                 /*
121                 this.selection.signal['changed'].connect(function() {
122                     _this.get('/LeftTree.view').listeners.cursor_changed.apply(
123                         _this.get('/LeftTree.view'), [ _this.get('/LeftTree.view'), '']
124                     );
125                 });
126                 */
127                 Gtk.drag_source_set (
128                     this.el,            /* widget will be drag-able */
129                     Gdk.ModifierType.BUTTON1_MASK,       /* modifier that will start a drag */
130                     BuilderApplication.targetList,            /* lists of target to support */
131                     Gdk.DragAction.COPY   | Gdk.DragAction.MOVE    |  Gdk.DragAction.LINK           /* what to do with data after dropped */
132                 );
133             
134                 // ?? needed??
135                 //Gtk.drag_source_add_text_targets(this.el); 
136             
137                 Gtk.drag_dest_set
138                 (
139                     this.el,              /* widget that will accept a drop */
140                     Gtk.DestDefaults.MOTION  | Gtk.DestDefaults.HIGHLIGHT,
141                     BuilderApplication.targetList,            /* lists of target to support */
142                     Gdk.DragAction.COPY   | Gdk.DragAction.MOVE   | Gdk.DragAction.LINK     /* what to do with data after dropped */
143                 );
144             
145                 //Gtk.drag_dest_set_target_list(this.el, Builder.Application.targetList);
146                 //Gtk.drag_dest_add_text_targets(this.el);
147             }
148
149             //listeners
150             this.el.button_press_event.connect( ( ev) => {
151                 //console.log("button press?");
152                 if (! _this.before_node_change(null) ) {
153                    return true;
154                 }
155             
156                 
157                 if (ev.type != Gdk.EventType.BUTTON_PRESS  || ev.button != 3) {
158                     //print("click" + ev.type);
159                     return false;
160                 }
161                 Gtk.TreePath res;
162                 if (!_this.view.el.get_path_at_pos((int)ev.x,(int)ev.y, out res, null, null, null) ) {
163                     return true;
164                 }
165                  
166                 this.el.get_selection().select_path(res);
167                  
168                   //if (!this.get('/LeftTreeMenu').el)  { 
169                   //      this.get('/LeftTreeMenu').init(); 
170                   //  }
171                     
172                  _this.LeftTreeMenu.el.set_screen(Gdk.Screen.get_default());
173                  _this.LeftTreeMenu.el.show_all();
174                   _this.LeftTreeMenu.el.popup(null, null, null,  3, ev.time);
175                  //   print("click:" + res.path.to_string());
176                   return true;
177             });
178             this.el.drag_begin.connect( ( ctx)  => {
179                 //print('SOURCE: drag-begin');
180                     
181                     
182                     //this.targetData = "";
183                     
184                     // find what is selected in our tree...
185                     
186                     var s = _this.view.el.get_selection();
187                     if (s.count_selected_rows() < 1) {
188                         return;
189                     }
190                     Gtk.TreeIter iter;
191                     Gtk.TreeModel mod;
192                     s.get_selected(out mod, out iter);
193             
194                     
195             
196                     // set some properties of the tree for use by the dropped element.
197                     GLib.Value value;
198                     _this.model.el.get_value(iter, 2, out value);
199                     var tp = mod.get_path(iter).to_string();
200                     var data = (JsRender.Node)(value.dup_object());
201                     var xname = data.fqn();
202                     print ("XNAME  IS " + xname+ "\n");
203                     this.dragData = tp;
204                     this.dropList = _this.main_window.windowstate.file.palete().getDropList(xname);
205                     
206                     print ("DROP LIST IS " + string.joinv(", ", this.dropList) + "\n");
207                     
208             
209                     // make the drag icon a picture of the node that was selected
210                 
211                     
212                 // by default returns the path..
213                    var path = _this.model.el.get_path(iter);
214             
215                      
216                     var pix = this.el.create_row_drag_icon ( path);
217                     
218                     Gtk.drag_set_icon_surface (ctx, pix) ;
219                     
220                     return;
221             });
222             this.el.cursor_changed.connect( ( ) => {
223             
224             
225                  if (this.blockChanges) { // probably not needed.. 
226                    return  ;
227                  }
228                   if (!_this.before_node_change(null) ) {
229                      this.blockChanges = true;
230                      this.el.get_selection().unselect_all();
231                      this.blockChanges = false;
232                      return;
233                  }
234                  if (_this.main_window.windowstate.file == null) {
235                      return;
236                  } 
237                  
238                  //var render = this.get('/LeftTree').getRenderer();                
239                 print("LEFT TREE -> view -> selection changed called\n");
240                 
241                 
242                 // -- it appears that the selection is not updated.
243                   
244                 GLib.Timeout.add_full(GLib.Priority.DEFAULT,10 , () => {
245                      
246             
247                         if (this.el.get_selection().count_selected_rows() < 1) {
248             
249                             print("selected rows < 1\n");
250                             //??this.model.load( false);
251                             _this.node_selected(null);
252                             
253                             return false ;
254                         }
255                             
256                             //console.log('changed');
257                         var s = this.el.get_selection();
258                          Gtk.TreeIter iter;
259                          Gtk.TreeModel mod;
260                         s.get_selected(out mod, out iter);
261                         
262                         
263                         // var val = "";
264                         GLib.Value value;
265                         _this.model.el.get_value(iter, 2, out value);
266                         _this.model.activePath = mod.get_path(iter).to_string();
267                         
268                         var node = (JsRender.Node)value.dup_object();
269                         _this.node_selected(node);
270                         while (Gtk.events_pending()) {
271                             Gtk.main_iteration();
272                        }
273                         var cp = mod.get_path(iter);
274                         Gtk.TreePath sp, ep;
275                         this.el.get_visible_range(out sp, out ep);
276                         // if sp is before cp then retuns 1.
277                         // if cp is before ep then retuns 1.
278                         if (cp.compare(sp) >= 0 && ep.compare(cp) >=1) {
279                             return false;
280                         }
281                         
282                          
283                         
284                         this.el.scroll_to_cell(new Gtk.TreePath.from_string(_this.model.activePath), null, true, 0.1f,0.0f);
285                         
286                         return false;
287                   });  
288                 //_this.after_node_change(node);
289             
290             //        _this.model.file.changed(node, "tree");
291                
292                 //Seed.print( value.get_string());
293                 return  ;
294                             
295             });
296             this.el.drag_end.connect( (drag_context) => {
297                 //Seed.print('LEFT-TREE: drag-end');
298                     this.dragData = "";
299                     this.dropList = null;
300             //        this.targetData = "";
301                     this.highlightDropPath("",0);
302             //        return true;
303             });
304             this.el.drag_motion.connect( ( ctx, x, y, time)  => {
305                print("got drag motion\n");
306                 var src = Gtk.drag_get_source_widget(ctx);
307                this.drag_x = x;
308                this.drag_y = y;     
309             
310                if (src != this.el) {
311                
312              
313              
314                 // the point of this is to detect where an item could be dropped..
315                     print("requesting drag data\n");
316                    this.drag_in_motion = true;
317                    
318                         // request data that will be recieved by the recieve...              
319                     Gtk.drag_get_data
320                     (
321                             this.el,         // will receive 'drag-data-received' signal 
322                             ctx,        // represents the current state of the DnD 
323                             Gdk.Atom.intern("STRING",true),    // the target type we want 
324                             time            // time stamp 
325                     );
326                     return true;
327               }    
328             
329             
330               print("action: %d\n", ctx.get_actions());
331              //print("GETTING POS");
332                 var  targetData = "";
333             
334                 Gtk.TreePath path;
335                 Gtk.TreeViewDropPosition pos;
336                 var isOver = _this.view.el.get_dest_row_at_pos(this.drag_x,this.drag_y, out path, out pos);
337             
338                 // if there are not items in the tree.. the we have to set isOver to true for anything..
339                 var isEmpty = false;
340                 if (_this.model.el.iter_n_children(null) < 1) {
341                     print("got NO children?\n");
342                     isOver = true; //??? 
343                     isEmpty = true;
344                     pos = Gtk.TreeViewDropPosition.INTO_OR_AFTER;
345                 }
346             
347             
348                 // ------------- a drag from self..
349             
350             
351                 //var action = Gdk.DragAction.COPY;
352                     // unless we are copying!!! ctl button..
353                 
354                 var action = (ctx.get_actions() & Gdk.DragAction.MOVE) > 0 ?
355                              Gdk.DragAction.COPY  : Gdk.DragAction.MOVE ;
356                             // Gdk.DragAction.MOVE : Gdk.DragAction.COPY ;
357             
358             
359                 if (_this.model.el.iter_n_children(null) < 1) {
360                     // no children.. -- asume it's ok..
361                     
362                     targetData = "|%d|".printf((int)Gtk.TreeViewDropPosition.INTO_OR_AFTER);
363                        
364                     this.highlightDropPath("", (Gtk.TreeViewDropPosition)0);        
365                     Gdk.drag_status(ctx, action ,time);
366                     return true;
367                     
368                     // continue through to allow drop...
369             
370                 } 
371                     
372                     
373             
374                 
375                 
376                 //print("ISOVER? " + isOver);
377                 if (!isOver) {
378               
379                     Gdk.drag_status(ctx, 0 ,time);
380                      this.highlightDropPath("", (Gtk.TreeViewDropPosition)0);                    
381                      return false;
382             
383                 }
384                         
385                 // drag node is parent of child..
386                 //console.log("SRC TREEPATH: " + src.treepath);
387                 //console.log("TARGET TREEPATH: " + data.path.to_string());
388                 
389                 // nned to check a  few here..
390                 //Gtk.TreeViewDropPosition.INTO_OR_AFTER
391                 //Gtk.TreeViewDropPosition.INTO_OR_BEFORE
392                 //Gtk.TreeViewDropPosition.AFTER
393                 //Gtk.TreeViewDropPosition.BEFORE
394                 
395                 // locally dragged items to not really use the 
396                 var selection_text = this.dragData;
397                 
398                         
399                         
400                 if (selection_text == null || selection_text.length < 1) {
401                             //print("Error  - drag selection text returned NULL");
402                          Gdk.drag_status(ctx, 0 ,time);
403                         this.highlightDropPath("", (Gtk.TreeViewDropPosition)0);
404                          return false;
405                  }
406                                    
407                         
408                         // see if we are dragging into ourself?
409                 var target_path = path.to_string();            
410                 print ("Drag  %s onto %s--%d\n ", selection_text, target_path, pos);
411                 
412                 // pos : 3 = ontop - 0 = after, 1 = before
413                 //print("target_path="+target_path);
414             
415                 // 
416                 if (selection_text  == target_path) {
417                     print("self drag ?? == we should perhaps allow copy onto self..\n");
418                             
419                      Gdk.drag_status(ctx, 0 ,time);
420                       this.highlightDropPath("", (Gtk.TreeViewDropPosition)0);
421                       return false;
422             //                 -- fixme -- this is not really correct..
423             
424                 }
425                         
426                 // check that 
427                 //print("DUMPING DATA");
428                 //console.dump(data);
429                 // path, pos
430                 
431                 //print(data.path.to_string() +' => '+  data.pos);
432                 
433                 // dropList is a list of xtypes that this node could be dropped on.
434                 // it is set up when we start to drag..
435                 
436                 
437                 targetData = _this.model.findDropNodeByPath( path.to_string(), this.dropList, pos);
438                     
439                 print("targetDAta: " + targetData +"\n");
440                 
441                 if (targetData.length < 1) {
442                     //print("Can not find drop node path");
443                    
444                     Gdk.drag_status(ctx, 0, time);
445                     this.highlightDropPath("", (Gtk.TreeViewDropPosition)0);
446                     return false;
447                 }
448                 
449                 var td_ar = targetData.split("|");
450                   
451                 
452             
453                 Gdk.drag_status(ctx, action ,time);
454                 this.highlightDropPath(td_ar[0], (Gtk.TreeViewDropPosition)int.parse(td_ar[1]));
455                 return true;
456                    
457                    
458             });
459             this.el.drag_data_get.connect( ( drag_context, data, info, time) => {
460                         
461                         
462                              //print("drag-data-get");
463                              var s = this.el.get_selection();
464                              if (s.count_selected_rows() < 1) {
465                                     data.set_text("",0);     
466                                      print("return empty string - no selection..");
467                                     return;
468                                 }
469                              
470                              Gtk.TreeIter iter;
471                              Gtk.TreeModel mod;
472                              
473                              s.get_selected(out mod, out iter);
474                              
475                             
476                             
477                              GLib.Value value;
478                              _this.model.el.get_value(iter, 2, out value);
479                              var ndata = (JsRender.Node)(value.dup_object());
480                              
481                             
482                             
483                             var tp = mod.get_path(iter).to_string();
484                             // by default returns the path..
485                             
486                            if ( info != Gdk.Atom.intern("STRING",true) ) {
487                                 tp = ndata.toJsonString();
488                            }   
489                            
490                            //data.set_text(tp,tp.length);   
491                             
492                             data.set (data.get_target (), 8, (uchar[]) tp.to_utf8 ());
493                         
494                             
495                            //  print("return " + tp);
496                         });
497             this.el.drag_data_received.connect( (ctx, x, y, sel, info, time)  => {
498               
499                     // THIS CODE ONLY RELATES TO drag  or drop of "NEW" elements or "FROM another tree.."
500               
501               
502                     //  print("Tree: drag-data-received\n");
503                     var selection_text = (string)sel.get_data();
504                     //print("selection_text= %s\n",selection_text);
505             
506                     var is_drag = this.drag_in_motion;
507                 
508                     
509             
510                     print("Is Drag %s\n", is_drag ? "Y": "N");
511                     var  targetData = "";
512                     
513                     Gtk.TreePath path;
514                     Gtk.TreeViewDropPosition pos;
515                     var isOver = _this.view.el.get_dest_row_at_pos(this.drag_x,this.drag_y, out path, out pos);
516                     
517                     // if there are not items in the tree.. the we have to set isOver to true for anything..
518                     var isEmpty = false;
519                     if (_this.model.el.iter_n_children(null) < 1) {
520                         print("got NO children?\n");
521                         isOver = true; //??? 
522                         isEmpty = true;
523                         pos = Gtk.TreeViewDropPosition.INTO_OR_AFTER;
524                     }
525                     
526                  
527                     //console.log("LEFT-TREE: drag-motion");
528                     var src = Gtk.drag_get_source_widget(ctx);
529                     
530                     // a drag from self - this should be handled by drop and motion.
531                     if (src == this.el) {
532                         print("Source == this element should not happen.. ? \n");
533                         return;
534                     }
535                     //print("drag_data_recieved from another element");
536                     
537                      
538                     
539                     
540                     if (selection_text == null || selection_text.length < 1 || !isOver) {
541                         // nothing valid foudn to drop...
542                            print("empty sel text or not over");
543                         if (is_drag) {
544                             Gdk.drag_status(ctx, 0, time);
545                             this.highlightDropPath("", (Gtk.TreeViewDropPosition)0);
546                             return;
547                         }
548                         Gtk.drag_finish (ctx, false, false, time);        // drop failed..
549                         // no drop action...
550                         return;            
551                     
552                     }
553                     var dropNode = new JsRender.Node(); 
554                     
555                     var dropNodeType  = selection_text;
556                     var show_templates = true;
557                     // for drop
558                     if (dropNodeType[0] == '{') {
559                         var pa = new Json.Parser();
560                         try {
561                             pa.load_from_data(dropNodeType);
562                         } catch (Error e) {
563                             Gtk.drag_finish (ctx, false, false, time);        // drop failed..
564                             // no drop action...
565                             return;   
566                         }
567                          
568                         dropNode.loadFromJson( pa.get_root().get_object(), 2);
569                         dropNodeType = dropNode.fqn();
570                         show_templates = false;
571                         
572                         
573                     } else {
574             
575                         dropNode.setFqn(selection_text);
576                     }
577             
578                      
579                     // dropList --- need to gather this ... 
580                     print("get dropList for : %s\n",dropNodeType);            
581                     var dropList = _this.main_window.windowstate.file.palete().getDropList(dropNodeType);
582                     
583                     print("dropList: %s\n", string.joinv(" , ", dropList));
584                     
585                     // if drag action is link ... then we can drop it anywahere...
586                      if ((ctx.get_actions() & Gdk.DragAction.LINK) > 0) {
587                          // if path is null?? dragging into an empty tree?
588                          targetData = (path == null ? "" :  path.to_string()) + "|%d".printf((int)pos);
589                      } else {
590                     
591                     
592                         targetData = _this.model.findDropNodeByPath( isEmpty ? "" : path.to_string(), dropList, pos);
593                      }
594                     
595                     
596                         
597                     print("targetDAta: " + targetData +"\n");
598                     
599                     if (targetData.length < 1) {
600                      
601                         // invalid drop path..
602                         if (this.drag_in_motion) {
603                             Gdk.drag_status(ctx, 0, time);
604                             this.highlightDropPath("", (Gtk.TreeViewDropPosition)0);
605                             return;
606                         }
607                         Gtk.drag_finish (ctx, false, false, time);        // drop failed..
608                         // no drop action...
609                         return;
610                     }
611                     // valid drop path..
612                     
613                       var td_ar = targetData.split("|");
614                       
615                     
616                     if (this.drag_in_motion) { 
617                         Gdk.drag_status(ctx, Gdk.DragAction.COPY ,time);
618             
619                         this.highlightDropPath(  td_ar[0]  , (Gtk.TreeViewDropPosition)int.parse(td_ar[1]));
620                         return;
621                     }
622                     // continue on to allow drop..
623                 
624             
625                     // at this point, drag is not in motion... -- as checked above... - so it's a real drop event..
626                     
627             
628                     _this.model.dropNode(targetData, dropNode, show_templates);
629                     print("ADD new node!!!\n");
630                         
631                     ///Xcls_DialogTemplateSelect.singleton().show( _this.model.file.palete(), node);
632                     
633                     Gtk.drag_finish (ctx, false, false,time);
634                     
635                     
636                         
637                         
638                   
639             });
640             this.el.drag_drop.connect( (  ctx, x, y, time)  => {
641                   //Seed.print("TARGET: drag-drop");
642                
643                
644                 var src = Gtk.drag_get_source_widget(ctx);
645                  
646                if (src != this.el) {
647                
648                 
649                    
650                    this.drag_in_motion = false;   
651                         // request data that will be recieved by the recieve...              
652                     Gtk.drag_get_data
653                     (
654                             this.el,         // will receive 'drag-data-received' signal 
655                             ctx,        // represents the current state of the DnD 
656                             Gdk.Atom.intern("application/json",true),    // the target type we want 
657                             time            // time stamp 
658                     );
659             
660                      
661                     // No target offered by source => error
662                
663             
664                      return  false;
665                  }
666                  
667                  // handle drop around self..
668                  
669                               
670                         
671                 //print("GETTING POS");
672                 var  targetData = "";
673                 
674                 Gtk.TreePath path;
675                 Gtk.TreeViewDropPosition pos;
676                 var isOver = _this.view.el.get_dest_row_at_pos(this.drag_x,this.drag_y, out path, out pos);
677                 
678                 // if there are not items in the tree.. the we have to set isOver to true for anything..
679                 var isEmpty = false;
680                 if (_this.model.el.iter_n_children(null) < 1) {
681                     print("got NO children?\n");
682                     isOver = true; //??? 
683                     isEmpty = true;
684                     pos = Gtk.TreeViewDropPosition.INTO_OR_AFTER;
685                 }
686                 
687                  
688                  
689                 //var action = Gdk.DragAction.COPY;
690                     // unless we are copying!!! ctl button..
691                 
692                 var action = (ctx.get_actions() & Gdk.DragAction.MOVE) > 0 ?
693                              Gdk.DragAction.COPY  : Gdk.DragAction.MOVE ;
694                             // Gdk.DragAction.MOVE : Gdk.DragAction.COPY ;
695             
696                   
697                 if (_this.model.el.iter_n_children(null) < 1) {
698                     // no children.. -- asume it's ok..
699                     
700                     targetData = "|%d|".printf((int)Gtk.TreeViewDropPosition.INTO_OR_AFTER);
701                      
702                     // continue through to allow drop...
703             
704                 } else {
705                             
706                             
707                 
708                             
709                             
710                             //print("ISOVER? " + isOver);
711                     if (!isOver) {
712                         
713                         Gtk.drag_finish (ctx, false, false, time);        // drop failed..
714                         return true; // not over apoint!?! - no action on drop or motion..
715                     }
716                             
717                     // drag node is parent of child..
718                     //console.log("SRC TREEPATH: " + src.treepath);
719                     //console.log("TARGET TREEPATH: " + data.path.to_string());
720                     
721                     // nned to check a  few here..
722                     //Gtk.TreeViewDropPosition.INTO_OR_AFTER
723                     //Gtk.TreeViewDropPosition.INTO_OR_BEFORE
724                     //Gtk.TreeViewDropPosition.AFTER
725                     //Gtk.TreeViewDropPosition.BEFORE
726                     
727                     // locally dragged items to not really use the 
728                     var selection_text = this.dragData;
729                     
730                     
731                     
732                     if (selection_text == null || selection_text.length < 1) {
733                         //print("Error  - drag selection text returned NULL");
734                       
735                          Gtk.drag_finish (ctx, false, false, time);        // drop failed..
736                          return true; /// -- fixme -- this is not really correct..
737                     }                
738                             
739                             // see if we are dragging into ourself?
740                             print ("got selection text of  " + selection_text);
741                     
742                     var target_path = path.to_string();
743                     //print("target_path="+target_path);
744             
745                     // 
746                     if (selection_text  == target_path) {
747                         print("self drag ?? == we should perhaps allow copy onto self..\n");
748                         
749                          Gtk.drag_finish (ctx, false, false, time);        // drop failed..
750             
751                          return true; /// -- fixme -- this is not really correct..
752             
753                     }
754                             
755                     // check that 
756                     //print("DUMPING DATA");
757                     //console.dump(data);
758                     // path, pos
759                     
760                     //print(data.path.to_string() +' => '+  data.pos);
761                     
762                     // dropList is a list of xtypes that this node could be dropped on.
763                     // it is set up when we start to drag..
764                     
765                     
766                     targetData = _this.model.findDropNodeByPath( path.to_string(), this.dropList, pos);
767                         
768                     print("targetDAta: " + targetData +"\n");
769                     
770                     if (targetData.length < 1) {
771                         //print("Can not find drop node path");
772                          
773                         Gtk.drag_finish (ctx, false, false, time);        // drop failed..
774                         return true;
775                     }
776                                 
777                             
778                             
779                             // continue on to allow drop..
780               }
781                     // at this point, drag is not in motion... -- as checked above... - so it's a real drop event..
782             
783             
784                  var delete_selection_data = false;
785                     
786                 if (action == Gdk.DragAction.ASK)  {
787                     /* Ask the user to move or copy, then set the ctx action. */
788                 }
789             
790                 if (action == Gdk.DragAction.MOVE) {
791                     delete_selection_data = true;
792                 }
793                   
794                             // drag around.. - reorder..
795                 _this.model.moveNode(targetData, action);
796                     
797                    
798                     
799                     
800                     
801                     // we can send stuff to souce here...
802             
803             
804             // do we always say failure, so we handle the reall drop?
805                 Gtk.drag_finish (ctx, false, false,time); //delete_selection_data, time);
806             
807                 return true;
808              
809              
810              
811              
812              
813              
814             });
815         }
816
817         // user defined functions
818         public           void highlightDropPath ( string treepath, Gtk.TreeViewDropPosition pos) {
819         
820                 // highlighting for drag/drop
821                 if (treepath.length > 0) {
822                     this.el.set_drag_dest_row(  new  Gtk.TreePath.from_string( treepath ), pos);
823                   } else {
824                     this.el.set_drag_dest_row(null, Gtk.TreeViewDropPosition.INTO_OR_AFTER);
825                  }
826                      
827         }
828         public           void selectNode (string treepath_str) {
829             //this.selection.select_path(new  Gtk.TreePath.from_string( treepath_str));
830              var tp = new Gtk.TreePath.from_string(treepath_str);
831              
832              this.el.set_cursor(tp, null, false);  
833              this.el.scroll_to_cell(tp, null, false, 0,0);
834         }
835     }
836     public class Xcls_model : Object
837     {
838         public Gtk.TreeStore el;
839         private Xcls_WindowLeftTree  _this;
840
841
842             // my vars (def)
843         public DialogTemplateSelect template_select;
844         public string activePath;
845
846         // ctor
847         public Xcls_model(Xcls_WindowLeftTree _owner )
848         {
849             _this = _owner;
850             _this.model = this;
851             this.el = new Gtk.TreeStore( 3, typeof(string),typeof(string),typeof(Object) );
852
853             // my vars (dec)
854             this.template_select = null;
855             this.activePath = "";
856
857             // set gobject values
858
859             // init method
860
861             print("model initialized");
862         }
863
864         // user defined functions
865         public           string findDropNode (string treepath_str, string[] targets) {
866         
867             // this is used by the dragdrop code in the roo version AFAIR..
868         
869             //var path = treepath_str.replace(/^builder-/, '');
870             // treemap is depreciated... - should really check if model has any entries..
871         
872             if (this.el.iter_n_children(null) < 1) {
873                 //print("NO KEYS");
874                 return "|%d".printf((int)Gtk.TreeViewDropPosition.INTO_OR_AFTER);
875             }
876             //print("FIND treepath: " + path);
877             //console.dump(this.treemap);
878             
879             //if (!treepath_str.match(/^builder-/)) {
880             //    return []; // nothing!
881             //}
882             if (targets.length > 0 && targets[0] == "*") {
883                 return  treepath_str;
884             }
885             return this.findDropNodeByPath(treepath_str,targets, -1);
886         }
887         public           void loadFile (JsRender.JsRender f) {
888             //console.dump(f);
889             this.el.clear();
890             
891             // needed???
892             _this.main_window.windowstate.file = f;
893             
894            
895             if (f.tree == null) {
896                     try {
897                         f.loadItems( );
898                 } catch (Error e) {
899                         return;
900                 }
901             }
902             // if it's still null?
903             if (f.tree == null) {
904                 return;
905             }
906           
907             var o = new Gee.ArrayList<JsRender.Node>();
908             o.add(f.tree);
909             this.load(o,null);
910             
911             _this.view.el.expand_all();
912         
913             if (f.tree.items.size < 1) {
914                 // single item..
915                 
916                 //this.get('/Window.leftvpaned').el.set_position(80);
917                 // select first...
918                 _this.view.el.set_cursor( 
919                     new  Gtk.TreePath.from_string("0"), null, false);
920                 
921                 
922             } else {
923                   //this.get('/Window.leftvpaned').el.set_position(200);
924             }
925             
926             return;
927          
928                     
929         }
930         public    void updateSelected () {
931           
932            
933             var s = _this.view.el.get_selection();
934             
935              Gtk.TreeIter iter;
936             Gtk.TreeModel mod;
937             
938             
939             
940             if (!s.get_selected(out mod, out iter)) {
941                 return; // nothing seleted..
942             }
943           
944           GLib.Value value;
945             this.el.get_value(iter, 2, out value);
946             var node = (JsRender.Node)(value.get_object());
947             
948               this.el.set(iter, 0, node.nodeTitle(),
949                         1, node.nodeTip(), -1
950                 );
951         }
952         public           string findDropNodeByPath (string treepath_str, string[] targets, int in_pref = -1) {
953         
954             var path = treepath_str; // dupe it..
955             
956             
957             // pref : 3 = ontop - 0 = after, 1 = before
958             int pref = in_pref < 0  ?  Gtk.TreeViewDropPosition.INTO_OR_AFTER : in_pref;
959             
960             var last = "";
961             
962             //console.dump(this.treemap);
963             
964             print("findDropNodeByPath : got path length %d / %s\n", path.length, path);
965             
966             if (path.length == 0) {
967                 // top drop. // just return empty..
968                 return "|%d".printf((int)pref) ;
969                 
970             }
971             
972             
973             while (path.length > 0) {
974             
975                 if (path.length == treepath_str.length && pref != Gtk.TreeViewDropPosition.INTO_OR_AFTER) {
976                     if (path.last_index_of(":") < 0 ) {
977                         return "";
978                     }
979                     path = path.substring(0, path.last_index_of(":"));
980                     last = treepath_str;
981                     print("DROP  before or after : using %s\n",path);
982                     continue;
983                 }
984             
985                 //print("LOOKING FOR PATH: " + path);
986                 var node_data = this.pathToNode(path);
987                 
988                 if (node_data == null) {
989                     print("node not found");
990                     return "";
991                 }
992                 
993                 var xname = node_data.fqn();
994                 var match = "";
995                 var prop = "";
996                 
997                 for (var i =0; i < targets.length; i++)  {
998                     var tg = targets[i];
999                     if ((tg == xname)  ) {
1000                         match = tg;
1001                         break;
1002                     }
1003                     // if target is "xxxx:name"
1004                     if (tg.contains(xname +":")) {
1005                         match = tg;
1006                         var ar = tg.split(":");
1007                         prop = ar[1];
1008                         break;
1009                     }
1010                 }
1011                 
1012                 if (match.length > 0) {
1013                     if (last.length > 0) { // pref is after/before..
1014                         // then it's after last
1015                         //if (pref > 1) {
1016                         //    return "";
1017                         //}
1018                         return last + "|%d".printf((int)pref) + "|" + prop;
1019         
1020                         
1021                     }
1022                     // we need to add prop - as :store -> needs to bee added when dropping onto.
1023                     return path + "|%d".printf( (int) Gtk.TreeViewDropPosition.INTO_OR_AFTER)  + "|" + prop;
1024                 }
1025                 /*
1026                 last = "" + path;
1027                 var par = path.split(":");
1028                 string [] ppar = {};
1029                 for (var i = 0; i < par.length-1; i++) {
1030                     ppar += par[i];
1031                 }
1032                 
1033                 path = string.joinv(":", ppar);
1034                 */
1035                 break;
1036         
1037             }
1038             
1039             return "";
1040                     
1041         }
1042         public           void moveNode (string target_data, Gdk.DragAction action) 
1043         {
1044            
1045            /// target_data = "path|pos");
1046            
1047            
1048             //print("MOVE NODE");
1049             // console.dump(target_data);
1050             Gtk.TreeIter old_iter;
1051             Gtk.TreeModel mod;
1052             
1053             var s = _this.view.el.get_selection();
1054             s.get_selected(out mod , out old_iter);
1055             mod.get_path(old_iter);
1056             
1057             var node = this.pathToNode(mod.get_path(old_iter).to_string());
1058             //console.dump(node);
1059             if (node == null) {
1060                 print("moveNode: ERROR - node is null?");
1061             }
1062             
1063             
1064         
1065             // needs to drop first, otherwise the target_data 
1066             // treepath will be invalid.
1067         
1068             
1069             if ((action & Gdk.DragAction.MOVE) > 0) {
1070                     print("REMOVING OLD NODE : " + target_data + "\n");
1071                     node.remove();
1072                     this.dropNode(target_data, node, false);
1073                     this.el.remove(ref old_iter);
1074                     
1075                     
1076                                  
1077             } else {
1078                 print("DROPPING NODE // copy: " + target_data + "\n");
1079                 node = node.deepClone();
1080                 this.dropNode(target_data, node, false);
1081             }
1082             _this.changed();
1083             this.activePath= "";
1084             //this.updateNode(false,true);
1085         }
1086         public           void load (Gee.ArrayList<JsRender.Node> tr, Gtk.TreeIter? iter) 
1087         {
1088             Gtk.TreeIter citer;
1089             //this.insert(citer,iter,0);
1090             for(var i =0 ; i < tr.size; i++) {
1091                 if (iter != null) {
1092                     this.el.insert(out citer,iter,-1); // why not append?
1093                 } else {
1094                     this.el.append(out citer,null);
1095                 }
1096                 
1097                 this.el.set(citer, 0, tr.get(i).nodeTitle(),
1098                         1, tr.get(i).nodeTip(), -1
1099                 );
1100                 var o =   GLib.Value(typeof(Object));
1101                 o.set_object((Object)tr.get(i));
1102                 
1103                 this.el.set_value(citer, 2, o);
1104                 
1105                 if (tr.get(i).items.size > 0) {
1106                     this.load(tr.get(i).items, citer);
1107                 }
1108              
1109             }
1110         
1111             
1112         }
1113         public           void deleteSelected () {
1114             
1115             print("DELETE SELECTED?");
1116             //_this.view.blockChanges = true;
1117             print("GET SELECTION?");
1118         
1119             var s = _this.view.el.get_selection();
1120             
1121             print("GET  SELECTED?");
1122            Gtk.TreeIter iter;
1123             Gtk.TreeModel mod;
1124         
1125             
1126             if (!s.get_selected(out mod, out iter)) {
1127                 return; // nothing seleted..
1128             }
1129               
1130         
1131         
1132             this.activePath= "";      
1133             print("GET  vnode value?");
1134         
1135             GLib.Value value;
1136             this.el.get_value(iter, 2, out value);
1137             var data = (JsRender.Node)(value.get_object());
1138             print("removing node from Render\n");
1139             if (data.parent == null) {
1140                _this.main_window.windowstate.file.tree = null;
1141             } else {
1142                 data.remove();
1143             }
1144             print("removing node from Tree\n");    
1145             s.unselect_all();
1146             this.el.remove(ref iter);
1147         
1148             
1149             
1150             
1151             // 
1152             
1153             
1154         
1155         
1156             this.activePath= ""; // again!?!?      
1157             //this.changed(null,true);
1158             
1159             _this.changed();
1160             
1161             _this.view.blockChanges = false;
1162         }
1163         public           JsRender.Node pathToNode (string path) {
1164          
1165              
1166              Gtk.TreeIter   iter;
1167              _this.model.el.get_iter_from_string(out iter, path);
1168              
1169              GLib.Value value;
1170              _this.model.el.get_value(iter, 2, out value);
1171              
1172              return (JsRender.Node)value.dup_object();
1173         
1174         }
1175         public           void dropNode (string target_data_str, JsRender.Node node, bool show_templates) {
1176         //         print("drop Node");
1177              // console.dump(node);
1178           //    console.dump(target_data);
1179           
1180           
1181                 // 0 = before , 1=after 2/3 onto
1182           
1183           
1184                 var target_data= target_data_str.split("|");
1185           
1186                 var parent_str = target_data[0].length > 0 ? target_data[0] : "";
1187                 var pos = target_data.length > 1 ? int.parse(target_data[1]) : 2; // ontop..
1188           
1189           
1190                 Gtk.TreePath tree_path  =   parent_str.length > 0 ? new  Gtk.TreePath.from_string( parent_str ) : null;
1191                 
1192                 
1193                 
1194                 //print("add " + tp + "@" + target_data[1]  );
1195                 
1196                 JsRender.Node parentNode = null;
1197                 
1198                 Gtk.TreeIter iter_after;
1199                 Gtk.TreeIter iter_par ;
1200                 
1201                
1202                  if (target_data.length == 3 && target_data[2].length > 0) {
1203                     node.props.set("* prop", target_data[2]);
1204                 }
1205         
1206                 Gtk.TreePath expand_parent = null;
1207                 
1208                 // we only need to show the template if it's come from else where?
1209                  if (show_templates) {
1210                  
1211                      var ts = _this.main_window.windowstate.template_select;
1212                  
1213                      var new_node = ts.show(
1214                           _this.main_window, // (Gtk.Window) _this.el.get_toplevel (),
1215                          _this.main_window.windowstate.file.palete(),
1216                             node,
1217                             _this.main_window.windowstate.project);
1218                            
1219                      if (new_node == null) {
1220                          return; // do not add?
1221                      }
1222                      node = new_node;
1223                 }        
1224                 
1225                  //print("pos is %d  \n".printf(pos));
1226                 
1227                  Gtk.TreeIter n_iter; 
1228                  
1229                  if ( parent_str.length < 1) {
1230                       this.el.append(out n_iter, null); // drop at top level..
1231                       node.parent = null;
1232                       _this.main_window.windowstate.file.tree = node;
1233                       
1234                       
1235                 } else   if (pos  < 2) {
1236                     //print(target_data[1]  > 0 ? 'insert_after' : 'insert_before');
1237                     
1238                     this.el.get_iter(out iter_after, tree_path );            
1239                     this.el.iter_parent(out iter_par, iter_after);
1240                     expand_parent = this.el.get_path(iter_par);
1241                     
1242                     GLib.Value value;
1243                     this.el.get_value( iter_par, 2, out value);
1244                     parentNode =  (JsRender.Node)value.dup_object();
1245                     
1246                     
1247                     this.el.get_value( iter_after, 2, out value);
1248                     var relNode =  (JsRender.Node)value.dup_object();
1249                     
1250                     if ( pos  > 0 ) {
1251                      
1252                         this.el.insert_after(out n_iter,    iter_par  , iter_after);
1253                         var ix = parentNode.items.index_of(relNode);
1254                         parentNode.items.insert(ix+1, node);
1255                         
1256                     } else {
1257                         this.el.insert_before(out n_iter,  iter_par  , iter_after);
1258                         var ix = parentNode.items.index_of(relNode);
1259                         parentNode.items.insert(ix, node);
1260          
1261                     }
1262                     node.parent = parentNode;
1263                     
1264                     
1265                     
1266                 } else {
1267                    //  print("appending to  " + parent_str);
1268                     this.el.get_iter(out iter_par, tree_path);
1269                     this.el.append(out n_iter,   iter_par );
1270                     expand_parent = this.el.get_path(iter_par);
1271                     
1272                     GLib.Value value;
1273                     this.el.get_value( iter_par, 2, out value);
1274                     parentNode =  (JsRender.Node)value.dup_object();
1275                     node.parent = parentNode;
1276                     parentNode.items.add(node);
1277                 }
1278                 
1279                 // reparent node in tree...
1280                
1281                 
1282                 // why only on no parent???
1283                 
1284                 //if (node.parent = null) {
1285                      
1286                    
1287                     
1288                 //}
1289                 
1290                 
1291                 // work out what kind of packing to use.. -- should be in 
1292                 if (!node.has("pack")   && parent_str.length > 1) {
1293                     
1294                     _this.main_window.windowstate.file.palete().fillPack(node,parentNode);
1295                     
1296                     
1297                 }
1298                 
1299                 // add the node...
1300                 
1301                 this.el.set(n_iter, 0, node.nodeTitle(), 1, node.nodeTip(), -1  );
1302                 var o =   GLib.Value(typeof(Object));
1303                 o.set_object((Object)node);
1304                 
1305                 this.el.set_value(n_iter, 2, o);
1306                 
1307                 
1308                 
1309                 
1310         // load children - if it has any..
1311               
1312                 if (node.items.size > 0) {
1313                     this.load(node.items, n_iter);
1314                     _this.view.el.expand_row(this.el.get_path(n_iter), true);
1315                 } else if (expand_parent != null && !_this.view.el.is_row_expanded(expand_parent)) {
1316                    _this.view.el.expand_row(expand_parent,true);
1317                 }
1318         
1319                 //if (tp != null && (node.items.length() > 0 || pos > 1)) {
1320                 //    _this.view.el.expand_row(this.el.get_path(iter_par), true);
1321                // }
1322                 // wee need to get the empty proptypes from somewhere..
1323                 
1324                 //var olditer = this.activeIter;
1325                 this.activePath = this.el.get_path(n_iter).to_string();
1326         
1327         
1328                 
1329                 
1330                 _this.view.el.set_cursor(this.el.get_path(n_iter), null, false);
1331                 _this.changed();
1332              
1333                 
1334                     
1335         }
1336         public string treePathFromNode (JsRender.Node node) {
1337             // iterate through the tree and find the node
1338             var ret = "";
1339             
1340             this.el.foreach((mod, pth, iter) => {
1341                 // get the node..
1342               
1343              
1344                  GLib.Value value;
1345                  _this.model.el.get_value(iter, 2, out value);
1346                  
1347         
1348                  
1349                  var n = (JsRender.Node)value;
1350         
1351                  print("compare %s to %s\n", n.fqn(), node.fqn());
1352                 if (node == n) {
1353                     ret = pth.to_string();
1354                     return true;
1355                 }
1356                 return false;
1357             });
1358             return ret;
1359         
1360         }
1361     }
1362
1363     public class Xcls_TreeViewColumn4 : Object
1364     {
1365         public Gtk.TreeViewColumn el;
1366         private Xcls_WindowLeftTree  _this;
1367
1368
1369             // my vars (def)
1370
1371         // ctor
1372         public Xcls_TreeViewColumn4(Xcls_WindowLeftTree _owner )
1373         {
1374             _this = _owner;
1375             this.el = new Gtk.TreeViewColumn();
1376
1377             // my vars (dec)
1378
1379             // set gobject values
1380             this.el.title = "test";
1381             var child_0 = new Xcls_renderer( _this );
1382             child_0.ref();
1383             this.el.pack_start (  child_0.el , true );
1384
1385             // init method
1386
1387             this.el.add_attribute(_this.renderer.el , "markup", 0 );
1388         }
1389
1390         // user defined functions
1391     }
1392     public class Xcls_renderer : Object
1393     {
1394         public Gtk.CellRendererText el;
1395         private Xcls_WindowLeftTree  _this;
1396
1397
1398             // my vars (def)
1399
1400         // ctor
1401         public Xcls_renderer(Xcls_WindowLeftTree _owner )
1402         {
1403             _this = _owner;
1404             _this.renderer = this;
1405             this.el = new Gtk.CellRendererText();
1406
1407             // my vars (dec)
1408
1409             // set gobject values
1410         }
1411
1412         // user defined functions
1413     }
1414
1415
1416
1417     public class Xcls_LeftTreeMenu : Object
1418     {
1419         public Gtk.Menu el;
1420         private Xcls_WindowLeftTree  _this;
1421
1422
1423             // my vars (def)
1424
1425         // ctor
1426         public Xcls_LeftTreeMenu(Xcls_WindowLeftTree _owner )
1427         {
1428             _this = _owner;
1429             _this.LeftTreeMenu = this;
1430             this.el = new Gtk.Menu();
1431
1432             // my vars (dec)
1433
1434             // set gobject values
1435             var child_0 = new Xcls_MenuItem7( _this );
1436             child_0.ref();
1437             this.el.add (  child_0.el  );
1438             var child_1 = new Xcls_MenuItem8( _this );
1439             child_1.ref();
1440             this.el.add (  child_1.el  );
1441             var child_2 = new Xcls_MenuItem9( _this );
1442             child_2.ref();
1443             this.el.add (  child_2.el  );
1444         }
1445
1446         // user defined functions
1447     }
1448     public class Xcls_MenuItem7 : Object
1449     {
1450         public Gtk.MenuItem el;
1451         private Xcls_WindowLeftTree  _this;
1452
1453
1454             // my vars (def)
1455
1456         // ctor
1457         public Xcls_MenuItem7(Xcls_WindowLeftTree _owner )
1458         {
1459             _this = _owner;
1460             this.el = new Gtk.MenuItem();
1461
1462             // my vars (dec)
1463
1464             // set gobject values
1465             this.el.label = "Delete Element";
1466
1467             //listeners
1468             this.el.activate.connect( ( ) => {
1469                 
1470                 print("ACTIVATE?");
1471                 
1472               
1473                  _this.model.deleteSelected();
1474             });
1475         }
1476
1477         // user defined functions
1478     }
1479
1480     public class Xcls_MenuItem8 : Object
1481     {
1482         public Gtk.MenuItem el;
1483         private Xcls_WindowLeftTree  _this;
1484
1485
1486             // my vars (def)
1487
1488         // ctor
1489         public Xcls_MenuItem8(Xcls_WindowLeftTree _owner )
1490         {
1491             _this = _owner;
1492             this.el = new Gtk.MenuItem();
1493
1494             // my vars (dec)
1495
1496             // set gobject values
1497             this.el.label = "Save as Template";
1498
1499             //listeners
1500             this.el.activate.connect( () => {
1501             
1502                  DialogSaveTemplate.singleton().show(
1503                         (Gtk.Window) _this.el.get_toplevel (), 
1504                         _this.main_window.windowstate.file.palete(), 
1505                         _this.getActiveElement()
1506                 );
1507                  
1508                 
1509             });
1510         }
1511
1512         // user defined functions
1513     }
1514
1515     public class Xcls_MenuItem9 : Object
1516     {
1517         public Gtk.MenuItem el;
1518         private Xcls_WindowLeftTree  _this;
1519
1520
1521             // my vars (def)
1522
1523         // ctor
1524         public Xcls_MenuItem9(Xcls_WindowLeftTree _owner )
1525         {
1526             _this = _owner;
1527             this.el = new Gtk.MenuItem();
1528
1529             // my vars (dec)
1530
1531             // set gobject values
1532             this.el.label = "Save as Module";
1533
1534             //listeners
1535             this.el.activate.connect( () => {
1536                 var node = _this.getActiveElement();
1537                  var name = DialogSaveModule.singleton().show(
1538                         (Gtk.Window) _this.el.get_toplevel (), 
1539                         _this.main_window.windowstate.project, 
1540                         node
1541                  );
1542                  if (name.length < 1) {
1543                         return;
1544               
1545                  }
1546                  node.props.set("* xinclude", name);
1547                  node.items.clear();
1548             
1549             
1550                 var s = _this.view.el.get_selection();
1551                 
1552                 print("GET  SELECTED?");
1553                 Gtk.TreeIter iter;
1554                 Gtk.TreeModel mod;
1555             
1556                 
1557                 if (!s.get_selected(out mod, out iter)) {
1558                     return; // nothing seleted..
1559                 }
1560                 Gtk.TreeIter citer;
1561                 var n_cn = mod.iter_n_children(iter) -1;
1562                 for (var i = n_cn; i > -1; i--) {
1563                     mod.iter_nth_child(out citer, iter, i);
1564                     
1565             
1566                     print("removing node from Tree\n");    
1567                 
1568                     _this.model.el.remove(ref citer);
1569                 }
1570                 _this.changed();
1571                 _this.node_selected(node);
1572                  
1573                 
1574             });
1575         }
1576
1577         // user defined functions
1578     }
1579
1580
1581 }