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