Fix #7996 - handle namespaces in glade, adw overlay for window selection and open
[roobuilder] / src / Builder4 / Editor.vala
1 static Editor  _Editor;
2
3 public class Editor : Object
4 {
5         public Gtk.Box el;
6         private Editor  _this;
7
8         public static Editor singleton()
9         {
10                 if (_Editor == null) {
11                     _Editor= new Editor();
12                 }
13                 return _Editor;
14         }
15         public Xcls_save_button save_button;
16         public Xcls_close_btn close_btn;
17         public Xcls_RightEditor RightEditor;
18         public Xcls_view view;
19         public Xcls_buffer buffer;
20         public Xcls_search_entry search_entry;
21         public Xcls_search_results search_results;
22         public Xcls_nextBtn nextBtn;
23         public Xcls_backBtn backBtn;
24         public Xcls_search_settings search_settings;
25         public Xcls_case_sensitive case_sensitive;
26         public Xcls_regex regex;
27         public Xcls_multiline multiline;
28
29                 // my vars (def)
30         public int pos_root_x;
31         public Xcls_MainWindow window;
32         public bool dirty;
33         public int pos_root_y;
34         public bool pos;
35         public int last_error_counter;
36         public GtkSource.SearchContext searchcontext;
37         public int last_search_end;
38         public signal void save ();
39         public JsRender.JsRender? file;
40         public JsRender.Node node;
41         public JsRender.NodeProp? prop;
42         public string activeEditor;
43
44         // ctor
45         public Editor()
46         {
47                 _this = this;
48                 this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
49
50                 // my vars (dec)
51                 this.window = null;
52                 this.dirty = false;
53                 this.pos = false;
54                 this.last_error_counter = 0;
55                 this.searchcontext = null;
56                 this.last_search_end = 0;
57                 this.file = null;
58                 this.node = null;
59                 this.prop = null;
60                 this.activeEditor = "";
61
62                 // set gobject values
63                 this.el.homogeneous = false;
64                 this.el.hexpand = true;
65                 this.el.vexpand = true;
66                 var child_1 = new Xcls_Box2( _this );
67                 child_1.ref();
68                 this.el.append( child_1.el );
69                 new Xcls_RightEditor( _this );
70                 this.el.append( _this.RightEditor.el );
71                 var child_3 = new Xcls_Box12( _this );
72                 child_3.ref();
73                 this.el.append ( child_3.el  );
74         }
75
76         // user defined functions
77         public bool saveContents ()  {
78             
79             
80             if (_this.file == null) {
81                 return true;
82             }
83             
84              
85              
86              var str = _this.buffer.toString();
87              
88              _this.buffer.checkSyntax();
89              
90              
91              
92              // LeftPanel.model.changed(  str , false);
93              _this.dirty = false;
94              _this.save_button.el.sensitive = false;
95              
96             // find the text for the node..
97             if (_this.file.xtype != "PlainFile") {
98                // in theory these properties have to exist!?!
99                 this.prop.val = str;
100                 //this.window.windowstate.left_props.reload();
101             } else {
102                 _this.file.setSource(  str );
103              }
104             
105             // call the signal..
106             this.save();
107             
108             return true;
109         
110         }
111         public void forwardSearch (bool change_focus) {
112         
113                 if (this.searchcontext == null) {
114                         return;
115                 } 
116         
117                 Gtk.TextIter beg, st,en;
118                  bool has_wrapped_around;
119                 this.buffer.el.get_iter_at_offset(out beg, this.last_search_end);
120                 if (!this.searchcontext.forward(beg, out st, out en, out has_wrapped_around)) {
121                 
122                         this.last_search_end = 0; // not sure if this should happen
123                 } else {
124                         if (has_wrapped_around) {
125                                 return;
126                         }
127                 
128                         this.last_search_end = en.get_offset();
129                         if (change_focus) {
130                                 this.view.el.grab_focus();
131                         }
132                         this.buffer.el.place_cursor(st);
133                         this.view.el.scroll_to_iter(st,  0.1f, true, 0.0f, 0.5f);
134                 }
135          
136         }
137         public void show (JsRender.JsRender file, JsRender.Node? node, JsRender.NodeProp? prop)
138         {
139             this.reset();
140             this.file = file;    
141             
142             if (file.xtype != "PlainFile") {
143                 this.prop = prop;
144                 this.node = node;
145         
146                 // find the text for the node..
147                 this.view.load( prop.val );
148                 this.updateErrorMarks();
149                 
150                 this.close_btn.el.show();       
151             
152             } else {
153                 this.view.load(        file.toSource() );
154                  this.updateErrorMarks();
155                 this.close_btn.el.hide();
156                 
157             }
158          
159         }
160         public void backSearch (bool change_focus) {
161         
162                 if (this.searchcontext == null) {
163                         return;
164                 } 
165                 
166                 Gtk.TextIter beg, st,en;
167                 bool has_wrapped_around;
168                 this.buffer.el.get_iter_at_offset(out beg, this.last_search_end -1 );
169                 
170                 if (!this.searchcontext.backward(beg, out st, out en, out has_wrapped_around)) {
171                         this.last_search_end = 0;
172                 } else {
173                         this.last_search_end = en.get_offset();
174                         if (change_focus) {
175                                 this.view.el.grab_focus();
176                         }
177                         this.buffer.el.place_cursor(st);
178                         this.view.el.scroll_to_iter(st,  0.1f, true, 0.0f, 0.5f);
179                 }
180         
181         }
182         public string tempFileContents () {
183            
184            
185            if (_this.file == null) {
186                return "";
187            }
188                 var str= this.buffer.toString();
189                 if (_this.file.xtype == "PlainFile") {
190             
191                 return str;
192             
193             }
194           
195               
196              
197             GLib.debug("calling validate");    
198             // clear the buttons.
199                 if (_this.prop.name == "xns" || _this.prop.name == "xtype") {
200                         return this.file.toSource(); ;
201                 }
202                 
203                 var oldcode  = _this.prop.val;
204                 _this.prop.val = str;
205             var ret = _this.file.toSource();
206             _this.prop.val = oldcode;
207             return ret;
208             
209         }
210         public void reset () {
211                  this.file = null;    
212              
213             this.node = null;
214             this.prop = null;
215                 this.searchcontext = null;
216           
217         }
218         public int search (string in_txt) {
219         
220                 var s = new GtkSource.SearchSettings();
221                 s.case_sensitive = _this.case_sensitive.el.active;
222                 s.regex_enabled = _this.regex.el.active;        
223                 s.wrap_around = false;
224                 
225                 this.searchcontext = new GtkSource.SearchContext(this.buffer.el,s);
226                 this.searchcontext.set_highlight(true);
227                 var txt = in_txt;
228                 
229                 if (_this.multiline.el.active) {
230                         txt = in_txt.replace("\\n", "\n");
231                 }
232                 
233                 s.set_search_text(txt);
234                 Gtk.TextIter beg, st,en;
235                  
236                 this.buffer.el.get_start_iter(out beg);
237                 bool has_wrapped_around;
238                 this.searchcontext.forward(beg, out st, out en, out has_wrapped_around);
239                 this.last_search_end = 0;
240                 
241                 return this.searchcontext.get_occurrences_count();
242         
243          
244            
245         
246         }
247         public void updateErrorMarks () {
248                 
249          
250         
251                 var buf = _this.buffer.el;
252                 Gtk.TextIter start;
253                 Gtk.TextIter end;     
254                 buf.get_bounds (out start, out end);
255         
256                 
257         
258          
259                 //GLib.debug("highlight errors");                
260         
261                  // we should highlight other types of errors..
262         
263                 if (_this.window.windowstate.state != WindowState.State.CODEONLY 
264                         &&
265                         _this.window.windowstate.state != WindowState.State.CODE
266                         ) {
267                         //GLib.debug("windowstate != CODEONLY?");
268                         
269                         return;
270                 } 
271         
272                  
273                 if (_this.file == null) {
274                         GLib.debug("file is null?");
275                         return;
276         
277                 }
278                 var ar = this.file.getErrors();
279                 if (ar.size < 1) {
280                         buf.remove_source_marks (start, end, null);
281                         buf.remove_tag_by_name ("ERR", start, end);
282                         buf.remove_tag_by_name ("WARN", start, end);
283                         buf.remove_tag_by_name ("DEPR", start, end);
284                         this.last_error_counter = file.error_counter ;
285                         //GLib.debug("highlight %s :  %s has no errors", this.file.relpath, category);
286                         return;
287                 }
288                 
289         
290          // basicaly check if there is no change, then we do not do any update..
291          // we can do this by just using an error counter?
292          // if that's changed then we will do an update, otherwise dont bother.
293                   
294                 
295                 var offset = 0;
296                 var hoffset = 0;
297         
298                 var tlines = buf.get_line_count () +1;
299                 
300                 if (_this.prop != null) {
301                         // this still seems flaky...
302         
303                         tlines = _this.prop.end_line;
304                         offset = _this.prop.start_line;
305                         hoffset = _this.node.node_pad.length + 2; //shift it left  by 2 ? ..
306                         
307                          
308                 } else {
309                         // no update...
310                         if (this.last_error_counter == file.error_counter) {
311                         
312                                 return;
313                         }
314                 
315                 }
316                 buf.remove_source_marks (start, end, null);
317                 buf.remove_tag_by_name ("ERR", start, end);
318                 buf.remove_tag_by_name ("WARN", start, end);
319                 buf.remove_tag_by_name ("DEPR", start, end);
320                 
321                 foreach(var diag in ar) { 
322                      Gtk.TextIter iter;
323         //        print("get inter\n");
324                     var eline = (int)diag.range.start.line - offset;
325                     var eline_to = (int)diag.range.end.line - offset;
326                     //var eline =  diag.range.end_line - offset;
327                     //GLib.debug("GOT ERROR on line %d -- converted to %d  (offset = %d)",
328                     //  err.line ,eline, offset);
329                     
330                     
331                     if (eline > tlines || eline < 0) {
332                         continue;
333                     }
334                     
335                     buf.get_iter_at_line( out iter, eline);
336                         var msg = "Line: %d %s : %s".printf(eline+1, diag.category, diag.message);
337                     buf.create_source_mark( msg, diag.category, iter);
338                     
339                     var spos = (int)diag.range.start.character - hoffset;
340                     if (spos < 0) { spos =0 ; }
341                     if (spos > iter.get_chars_in_line()) {
342                         spos = iter.get_chars_in_line();
343                 }
344                         buf.get_iter_at_line( out iter, eline_to);
345                         var epos = (int)diag.range.end.character - hoffset;
346                     if (epos < 0) { epos =0 ; }
347                     if (epos > iter.get_chars_in_line()) {
348                         epos = iter.get_chars_in_line();
349                 }
350                      
351                     
352                     buf.get_iter_at_line_offset( out start, eline, spos); 
353                    
354                     buf.get_iter_at_line_offset( out end, eline_to,epos); 
355                         
356                     buf.apply_tag_by_name(diag.category, start, end);
357                     
358                    // GLib.debug("set line %d to %s", eline, msg);
359                     //this.marks.set(eline, msg);
360                 }
361                 this.last_error_counter = file.error_counter ;
362         
363         
364         
365          
366         
367         }
368         public void scroll_to_line (int line) {
369         
370                 GLib.Timeout.add(500, () => {
371            
372                         var buf = this.view.el.get_buffer();
373         
374                         var sbuf = (GtkSource.Buffer) buf;
375         
376         
377                         Gtk.TextIter iter;   
378                         sbuf.get_iter_at_line(out iter,  line);
379                         this.view.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);
380                         return false;
381                 });   
382         }
383         public class Xcls_Box2 : Object
384         {
385                 public Gtk.Box el;
386                 private Editor  _this;
387
388
389                         // my vars (def)
390
391                 // ctor
392                 public Xcls_Box2(Editor _owner )
393                 {
394                         _this = _owner;
395                         this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
396
397                         // my vars (dec)
398
399                         // set gobject values
400                         this.el.homogeneous = false;
401                         this.el.hexpand = true;
402                         new Xcls_save_button( _this );
403                         this.el.append( _this.save_button.el );
404                         var child_2 = new Xcls_Label4( _this );
405                         child_2.ref();
406                         this.el.append( child_2.el );
407                         var child_3 = new Xcls_Scale5( _this );
408                         child_3.ref();
409                         this.el.append( child_3.el );
410                         new Xcls_close_btn( _this );
411                         this.el.append( _this.close_btn.el );
412                 }
413
414                 // user defined functions
415         }
416         public class Xcls_save_button : Object
417         {
418                 public Gtk.Button el;
419                 private Editor  _this;
420
421
422                         // my vars (def)
423
424                 // ctor
425                 public Xcls_save_button(Editor _owner )
426                 {
427                         _this = _owner;
428                         _this.save_button = this;
429                         this.el = new Gtk.Button();
430
431                         // my vars (dec)
432
433                         // set gobject values
434                         this.el.label = "Save";
435
436                         //listeners
437                         this.el.clicked.connect( () => { 
438                             _this.saveContents();
439                         });
440                 }
441
442                 // user defined functions
443         }
444
445         public class Xcls_Label4 : Object
446         {
447                 public Gtk.Label el;
448                 private Editor  _this;
449
450
451                         // my vars (def)
452
453                 // ctor
454                 public Xcls_Label4(Editor _owner )
455                 {
456                         _this = _owner;
457                         this.el = new Gtk.Label( null );
458
459                         // my vars (dec)
460
461                         // set gobject values
462                         this.el.hexpand = true;
463                 }
464
465                 // user defined functions
466         }
467
468         public class Xcls_Scale5 : Object
469         {
470                 public Gtk.Scale el;
471                 private Editor  _this;
472
473
474                         // my vars (def)
475
476                 // ctor
477                 public Xcls_Scale5(Editor _owner )
478                 {
479                         _this = _owner;
480                         this.el = new Gtk.Scale.with_range (Gtk.Orientation.HORIZONTAL,6, 30, 1);
481
482                         // my vars (dec)
483
484                         // set gobject values
485                         this.el.width_request = 200;
486                         this.el.has_origin = true;
487                         this.el.draw_value = false;
488                         this.el.digits = 0;
489                         this.el.sensitive = true;
490
491                         // init method
492
493                         {
494                                 this.el.set_range(6,30);
495                                 this.el.set_value(8);
496                         }
497
498                         //listeners
499                         this.el.change_value.connect( (st, val ) => {
500                                  
501                                    
502                                   _this.view.css.load_from_string(
503                                                 "#editor-view { font: %dpx monospace; }".printf((int)val)
504                                    );
505                              
506                                 return false;
507                         });
508                 }
509
510                 // user defined functions
511         }
512
513         public class Xcls_close_btn : Object
514         {
515                 public Gtk.Button el;
516                 private Editor  _this;
517
518
519                         // my vars (def)
520
521                 // ctor
522                 public Xcls_close_btn(Editor _owner )
523                 {
524                         _this = _owner;
525                         _this.close_btn = this;
526                         this.el = new Gtk.Button();
527
528                         // my vars (dec)
529
530                         // set gobject values
531                         this.el.icon_name = "window-close";
532                         var child_1 = new Xcls_Image7( _this );
533                         this.el.child = child_1.el;
534
535                         //listeners
536                         this.el.clicked.connect( () => { 
537                             _this.saveContents();
538                             _this.window.windowstate.switchState(WindowState.State.PREVIEW);
539                         });
540                 }
541
542                 // user defined functions
543         }
544         public class Xcls_Image7 : Object
545         {
546                 public Gtk.Image el;
547                 private Editor  _this;
548
549
550                         // my vars (def)
551
552                 // ctor
553                 public Xcls_Image7(Editor _owner )
554                 {
555                         _this = _owner;
556                         this.el = new Gtk.Image();
557
558                         // my vars (dec)
559
560                         // set gobject values
561                         this.el.icon_name = "window-close";
562                         this.el.icon_size = Gtk.IconSize.NORMAL;
563                 }
564
565                 // user defined functions
566         }
567
568
569
570         public class Xcls_RightEditor : Object
571         {
572                 public Gtk.ScrolledWindow el;
573                 private Editor  _this;
574
575
576                         // my vars (def)
577
578                 // ctor
579                 public Xcls_RightEditor(Editor _owner )
580                 {
581                         _this = _owner;
582                         _this.RightEditor = this;
583                         this.el = new Gtk.ScrolledWindow();
584
585                         // my vars (dec)
586
587                         // set gobject values
588                         this.el.vscrollbar_policy = Gtk.PolicyType.AUTOMATIC;
589                         this.el.vexpand = true;
590                         this.el.overlay_scrolling = false;
591                         this.el.hscrollbar_policy = Gtk.PolicyType.AUTOMATIC;
592                         new Xcls_view( _this );
593                         this.el.child = _this.view.el;
594                 }
595
596                 // user defined functions
597         }
598         public class Xcls_view : Object
599         {
600                 public GtkSource.View el;
601                 private Editor  _this;
602
603
604                         // my vars (def)
605                 public Gtk.CssProvider css;
606
607                 // ctor
608                 public Xcls_view(Editor _owner )
609                 {
610                         _this = _owner;
611                         _this.view = this;
612                         this.el = new GtkSource.View();
613
614                         // my vars (dec)
615                         this.css = null;
616
617                         // set gobject values
618                         this.el.auto_indent = true;
619                         this.el.indent_width = 4;
620                         this.el.name = "editor-view";
621                         this.el.show_line_marks = true;
622                         this.el.insert_spaces_instead_of_tabs = true;
623                         this.el.show_line_numbers = true;
624                         this.el.hexpand = true;
625                         this.el.vexpand = true;
626                         this.el.has_tooltip = true;
627                         this.el.tab_width = 4;
628                         this.el.highlight_current_line = true;
629                         new Xcls_buffer( _this );
630                         this.el.buffer = _this.buffer.el;
631                         var child_2 = new Xcls_EventControllerKey11( _this );
632                         child_2.ref();
633                         this.el.add_controller(  child_2.el );
634
635                         // init method
636
637                         this.css = new Gtk.CssProvider();
638                         
639                         this.css.load_from_string(
640                                 "#editor-view { font:  12px monospace;}"
641                         );
642                          
643                         Gtk.StyleContext.add_provider_for_display(
644                                 this.el.get_display(),
645                                 this.css,
646                                 Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
647                         );
648                                 
649                          
650                          
651                         this.el.completion.add_provider(
652                                 new Palete.CompletionProvider(_this)
653                         );
654                           
655                         //this.el.completion.unblock_interactive();
656                         this.el.completion.select_on_show = true; // select
657                         //this.el.completion.remember_info_visibility    = true;
658                         
659                         
660                         var attrs = new GtkSource.MarkAttributes();
661                         
662                         attrs.set_icon_name ( "process-stop");    
663                         attrs.query_tooltip_text.connect(( mark) => {
664                              GLib.debug("tooltip query? %s", mark.name);
665                             return strdup( mark.name);
666                         });
667                          attrs.query_tooltip_markup.connect(( mark) => {
668                              GLib.debug("tooltip query? %s", mark.name);
669                             return strdup( mark.name);
670                         });
671                         this.el.set_mark_attributes ("ERR", attrs, 1);
672                         attrs.ref();
673                         
674                         
675                         var wattrs = new GtkSource.MarkAttributes();
676                         wattrs.set_icon_name ( "process-stop");    
677                         wattrs.query_tooltip_text.connect(( mark) => {
678                              GLib.debug("tooltip query? %s", mark.name);
679                             return strdup(mark.name);
680                         });
681                         wattrs.query_tooltip_markup.connect(( mark) => {
682                              GLib.debug("tooltip query? %s", mark.name);
683                             return strdup(mark.name);
684                         });
685                         this.el.set_mark_attributes ("WARN", wattrs, 1);
686                         wattrs.ref();
687                         
688                          
689                         var dattrs = new GtkSource.MarkAttributes();
690                          
691                         dattrs.set_icon_name ( "process-stop"); 
692                         
693                         dattrs.query_tooltip_text.connect(( mark) => {
694                                 GLib.debug("tooltip query? %s", mark.name);
695                             return strdup(mark.name);
696                         });
697                         //dattrs.query_tooltip_markup.connect(( mark) => {
698                         //      GLib.debug("tooltip query? %s", mark.name);
699                          //   return strdup(mark.name);
700                         //});
701                         this.el.set_mark_attributes ("DEPR", dattrs, 1);
702                         dattrs.ref();    
703                         
704                          this.el.get_space_drawer().set_matrix(null);
705                          this.el.get_space_drawer().set_types_for_locations( 
706                                 GtkSource.SpaceLocationFlags.ALL,
707                                 GtkSource.SpaceTypeFlags.ALL
708                         );
709                         this.el.get_space_drawer().set_enable_matrix(true);
710
711                         //listeners
712                         this.el.query_tooltip.connect( (x, y, keyboard_tooltip, tooltip) => {
713                                 
714                                 //GLib.debug("query tooltip");
715                                 Gtk.TextIter iter;
716                                 int trailing;
717                                 
718                                 var yoff = (int) _this.RightEditor.el.vadjustment.value;
719                                 
720                                 // I think this is problematic - if it's compliing  / updating at same time as query.
721                                 
722                                 //if (_this.window.statusbar_compile_spinner.el.spinning) {
723                                 //      return false;
724                                 //}
725                                 
726                                 this.el.get_iter_at_position (out iter, out trailing,  x,  y + yoff);
727                                  
728                                 var l = iter.get_line();
729                         
730                                 
731                                  
732                                 // GLib.debug("query tooltip line %d", (int) l);
733                                 if (l < 0) {
734                         
735                                         return false;
736                                 }
737                                 /*
738                                 if (_this.buffer.marks != null && _this.buffer.marks.has_key(l)) {
739                                         GLib.debug("line %d setting tip to %s", l,  _this.buffer.marks.get(l));
740                                         tooltip.set_text(_this.buffer.marks.get(l).dup());
741                                         return true;
742                                 }
743                          
744                                 return false;
745                                 */
746                                 
747                                   
748                                 // this crashes?? - not sure why.
749                                 var marks = _this.buffer.el.get_source_marks_at_line(l, "ERR");
750                                 if (marks.is_empty()) {
751                                         marks = _this.buffer.el.get_source_marks_at_line(l, "WARN");
752                                 }
753                                 if (marks.is_empty()) {
754                                         marks = _this.buffer.el.get_source_marks_at_line(l, "DEPR");
755                                 }
756                                 
757                                 // GLib.debug("query tooltip line %d marks %d", (int)l, (int) marks.length());
758                                 var str = "";
759                                 marks.@foreach((m) => { 
760                                         //GLib.debug("got mark %s", m.name);
761                                         str += (str.length > 0 ? "\n" : "") + m.category + ": " + m.name;
762                                 });
763                                 // true if there is a mark..
764                                 if (str.length > 0 ) {
765                                         tooltip.set_text( str );
766                                 }
767                                 return str.length > 0 ? true : false;
768                                  
769                         });
770                 }
771
772                 // user defined functions
773                 public void load (string str) {
774                 
775                 // show the help page for the active node..
776                    //this.get('/Help').show();
777                  
778                   // this.get('/BottomPane').el.set_current_page(0);
779                         GLib.debug("load called - Reset undo buffer");
780                         
781                     var buf = (GtkSource.Buffer)this.el.get_buffer();
782                     buf.begin_irreversible_action();
783                     buf.set_text(str, str.length);
784                     buf.end_irreversible_action();
785                     
786                     var lm = GtkSource.LanguageManager.get_default();
787                     var lang = "vala";
788                     if (_this.file != null) {
789                          lang = _this.file.language;
790                     }
791                     print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
792                     var lg = _this.file.content_type.length > 0  ?
793                             lm.guess_language(_this.file.path, _this.file.content_type) :
794                             lm.get_language(lang);
795                      
796                    
797                     ((GtkSource.Buffer)(this.el.get_buffer())) .set_language(lg); 
798                 
799                     this.el.insert_spaces_instead_of_tabs = true;
800                     if (lg != null) {
801                                 print("sourcelanguage  = %s\n", lg.name);
802                                 if (lg.name == "Vala") {
803                                     this.el.insert_spaces_instead_of_tabs = false;
804                                 }
805                      }
806                     _this.dirty = false;
807                     this.el.grab_focus();
808                     _this.save_button.el.sensitive = false;
809                     _this.last_error_counter = -1;
810                 }
811         }
812         public class Xcls_buffer : Object
813         {
814                 public GtkSource.Buffer el;
815                 private Editor  _this;
816
817
818                         // my vars (def)
819                 public int error_line;
820                 public Gee.HashMap<int,string>? xmarks;
821                 public bool check_queued;
822
823                 // ctor
824                 public Xcls_buffer(Editor _owner )
825                 {
826                         _this = _owner;
827                         _this.buffer = this;
828                         this.el = new GtkSource.Buffer( null );
829
830                         // my vars (dec)
831                         this.error_line = -1;
832                         this.xmarks = null;
833                         this.check_queued = false;
834
835                         // set gobject values
836                         this.el.highlight_syntax = true;
837                         this.el.highlight_matching_brackets = true;
838                         this.el.enable_undo = true;
839
840                         // init method
841
842                         var buf = this.el;
843                         buf.create_tag ("bold", "weight", Pango.Weight.BOLD);
844                         buf.create_tag ("type", "weight", Pango.Weight.BOLD, "foreground", "#204a87");
845                         buf.create_tag ("keyword", "weight", Pango.Weight.BOLD, "foreground", "#a40000");
846                         buf.create_tag ("text", "weight", Pango.Weight.NORMAL, "foreground", "#729fcf");
847                         buf.create_tag ("number", "weight", Pango.Weight.BOLD, "foreground", "#ad7fa8");
848                         buf.create_tag ("method", "weight", Pango.Weight.BOLD, "foreground", "#729fcf");
849                         buf.create_tag ("property", "weight", Pango.Weight.BOLD, "foreground", "#BC1F51");
850                         buf.create_tag ("variable", "weight", Pango.Weight.BOLD, "foreground", "#A518B5");
851                         
852                         
853                         buf.create_tag ("ERR", "weight", Pango.Weight.BOLD, "background", "pink");
854                         buf.create_tag ("WARN", "weight", Pango.Weight.BOLD, "background", "#ABF4EB");
855                         buf.create_tag ("DEPR", "weight", Pango.Weight.BOLD, "background", "#EEA9FF");
856
857                         //listeners
858                         this.el.changed.connect( () => {
859                             // check syntax??
860                             // ??needed..??
861                             _this.save_button.el.sensitive = true;
862                             print("EDITOR CHANGED");
863                             this.checkSyntax();
864                            
865                             _this.dirty = true;
866                         
867                             // this.get('/LeftPanel.model').changed(  str , false);
868                             return ;
869                         });
870                 }
871
872                 // user defined functions
873                 public bool OLDhighlightErrorsJson (string type, Json.Object obj) {
874                         Gtk.TextIter start;
875                         Gtk.TextIter end;     
876                         this.el.get_bounds (out start, out end);
877                 
878                         this.el.remove_source_marks (start, end, type);
879                         GLib.debug("highlight errors");          
880                 
881                          // we should highlight other types of errors..
882                 
883                         if (!obj.has_member(type)) {
884                                 GLib.debug("Return has no errors\n");
885                                 return true;
886                         }
887                 
888                         if (_this.window.windowstate.state != WindowState.State.CODEONLY 
889                                 &&
890                                 _this.window.windowstate.state != WindowState.State.CODE
891                                 ) {
892                                 GLib.debug("windowstate != CODEONLY?");
893                                 
894                                 return true;
895                         } 
896                 
897                         //this.marks = new Gee.HashMap<int,string>();
898                         var err = obj.get_object_member(type);
899                  
900                         if (_this.file == null) {
901                                 GLib.debug("file is null?");
902                                 return true;
903                 
904                         }
905                         var valafn = _this.file.path;
906                 
907                         if (_this.file.xtype != "PlainFile") {
908                 
909                                 valafn = "";
910                                 try {             
911                                         var  regex = new Regex("\\.bjs$");
912                                         // should not happen
913                                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
914                                 } catch (GLib.RegexError e) {
915                                         return true;
916                                 }   
917                 
918                 
919                 
920                         }
921                         if (!err.has_member(valafn)) {
922                                 GLib.debug("File path has no errors");
923                                 return  true;
924                         }
925                 
926                         var lines = err.get_object_member(valafn);
927                         
928                         var offset = 1;
929                         if (obj.has_member("line_offset")) { // ?? why??
930                                 offset = (int)obj.get_int_member("line_offset") + 1;
931                         }
932                 
933                 
934                         var tlines = this.el.get_line_count () +1;
935                         
936                         if (_this.prop != null) {
937                         
938                                 tlines = _this.prop.end_line + 1;
939                                 offset = _this.prop.start_line + 1;
940                         
941                         }
942                         
943                 
944                 
945                         lines.foreach_member((obj, line, node) => {
946                                 
947                              Gtk.TextIter iter;
948                 //        print("get inter\n");
949                             var eline = int.parse(line) - offset;
950                             GLib.debug("GOT ERROR on line %s -- converted to %d  (offset = %d)\n", line,eline, offset);
951                             
952                             
953                             if (eline > tlines || eline < 0) {
954                                 return;
955                             }
956                            
957                             
958                             this.el.get_iter_at_line( out iter, eline);
959                             //print("mark line\n");
960                             var msg  = "";
961                             var ar = lines.get_array_member(line);
962                             for (var i = 0 ; i < ar.get_length(); i++) {
963                                 if (ar.get_string_element(i) == "Success") {
964                                         continue;
965                                 }
966                                         msg += (msg.length > 0) ? "\n" : "";
967                                         msg += ar.get_string_element(i);
968                                 }
969                                 if (msg == "") {
970                                         return;
971                                 }
972                                 msg = "Line: %d".printf(eline+1) +  " " + msg;
973                             this.el.create_source_mark(msg, type, iter);
974                             GLib.debug("set line %d to %m", eline, msg);
975                            // this.marks.set(eline, msg);
976                         } );
977                         return false;
978                 
979                 
980                 
981                 
982                 
983                         }
984                 public bool checkSyntax () {
985                  
986                     
987                     var str = this.toString();
988                     
989                     // needed???
990                     if (this.error_line > 0) {
991                          Gtk.TextIter start;
992                          Gtk.TextIter end;     
993                         this.el.get_bounds (out start, out end);
994                 
995                         this.el.remove_source_marks (start, end, null);
996                     }
997                     if (str.length < 1) {
998                         print("checkSyntax - empty string?\n");
999                         return true;
1000                     }
1001                     
1002                     // bit presumptiona
1003                     if (_this.file.xtype == "PlainFile") {
1004                     
1005                         // assume it's gtk...
1006                          var  oldcode =_this.file.toSource();
1007                         _this.file.setSource(str);
1008                             BuilderApplication.showSpinner("appointment soon","document change pending");
1009                         _this.file.getLanguageServer().document_change(_this.file);
1010                 
1011                         _this.file.setSource(oldcode);
1012                         
1013                                  
1014                         return true;
1015                     
1016                     }
1017                    if (_this.file == null) {
1018                        return true;
1019                    }
1020                  
1021                     
1022                 
1023                       
1024                      
1025                     GLib.debug("calling validate");    
1026                     // clear the buttons.
1027                         if (_this.prop.name == "xns" || _this.prop.name == "xtype") {
1028                                 return true ;
1029                         }
1030                         var oldcode  = _this.prop.val;
1031                         
1032                         _this.prop.val = str;
1033                         _this.node.updated_count++;
1034                     _this.file.getLanguageServer().document_change(_this.file);
1035                     _this.node.updated_count++;
1036                     _this.prop.val = oldcode;
1037                     
1038                     
1039                     //print("done mark line\n");
1040                      
1041                     return true; // at present allow saving - even if it's invalid..
1042                 }
1043                 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
1044                          
1045                         this.error_line = validate_res.size;
1046                 
1047                         if (this.error_line < 1) {
1048                                 return true;
1049                         }
1050                         var tlines = this.el.get_line_count ();
1051                         Gtk.TextIter iter;
1052                         var valiter = validate_res.map_iterator();
1053                         while (valiter.next()) {
1054                 
1055                         //        print("get inter\n");
1056                                 var eline = valiter.get_key();
1057                                 if (eline > tlines) {
1058                                         continue;
1059                                 }
1060                                 this.el.get_iter_at_line( out iter, eline);
1061                                 //print("mark line\n");
1062                                 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
1063                         }   
1064                         return false;
1065                 }
1066                 public string toString () {
1067                     
1068                     Gtk.TextIter s;
1069                     Gtk.TextIter e;
1070                     this.el.get_start_iter(out s);
1071                     this.el.get_end_iter(out e);
1072                     var ret = this.el.get_text(s,e,true);
1073                     //print("TO STRING? " + ret);
1074                     return ret;
1075                 }
1076         }
1077
1078         public class Xcls_EventControllerKey11 : Object
1079         {
1080                 public Gtk.EventControllerKey el;
1081                 private Editor  _this;
1082
1083
1084                         // my vars (def)
1085
1086                 // ctor
1087                 public Xcls_EventControllerKey11(Editor _owner )
1088                 {
1089                         _this = _owner;
1090                         this.el = new Gtk.EventControllerKey();
1091
1092                         // my vars (dec)
1093
1094                         // set gobject values
1095
1096                         //listeners
1097                         this.el.key_released.connect( (keyval, keycode, state) => {
1098                         
1099                           
1100                             if (keyval == Gdk.Key.s && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1101                                 GLib.debug("SAVE: ctrl-S  pressed");
1102                                 _this.saveContents();
1103                                 return;
1104                             }
1105                             
1106                             if (keyval == Gdk.Key.g && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1107                                     GLib.debug("SAVE: ctrl-g  pressed");
1108                                         _this.forwardSearch(true);
1109                                     return;
1110                                 }
1111                                 if (keyval == Gdk.Key.f && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1112                                     GLib.debug("SAVE: ctrl-f  pressed");
1113                                         _this.search_entry.el.grab_focus();
1114                                         _this.search_entry.el.select_region(0,-1);
1115                                     return;
1116                                 }
1117                                 if (keyval == Gdk.Key.space && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1118                                         _this.view.el.show_completion();
1119                                 }
1120                                 
1121                                 Gtk.TextIter iter;
1122                                 _this.buffer.el.get_iter_at_offset( out iter, _this.buffer.el.cursor_position);  
1123                                 var line  = iter.get_line();
1124                                 var offset = iter.get_line_offset();
1125                                 GLib.debug("line  %d  off %d", line ,offset);
1126                                 if (_this.prop != null) {
1127                                         line += _this.prop.start_line + 1; // i think..
1128                                         offset += 12; // should probably be 8 without namespaced 
1129                                         GLib.debug("guess line  %d  off %d", line ,offset);
1130                                 } 
1131                             //_this.view.el.show_completion();
1132                            // print(event.key.keyval)
1133                            
1134                            
1135                            
1136                             
1137                             return;
1138                          
1139                          
1140                         });
1141                 }
1142
1143                 // user defined functions
1144         }
1145
1146
1147
1148         public class Xcls_Box12 : Object
1149         {
1150                 public Gtk.Box el;
1151                 private Editor  _this;
1152
1153
1154                         // my vars (def)
1155
1156                 // ctor
1157                 public Xcls_Box12(Editor _owner )
1158                 {
1159                         _this = _owner;
1160                         this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
1161
1162                         // my vars (dec)
1163
1164                         // set gobject values
1165                         this.el.homogeneous = false;
1166                         this.el.vexpand = false;
1167                         new Xcls_search_entry( _this );
1168                         this.el.append( _this.search_entry.el );
1169                         new Xcls_search_results( _this );
1170                         this.el.append( _this.search_results.el );
1171                         new Xcls_nextBtn( _this );
1172                         this.el.append( _this.nextBtn.el );
1173                         new Xcls_backBtn( _this );
1174                         this.el.append( _this.backBtn.el );
1175                         var child_5 = new Xcls_MenuButton18( _this );
1176                         child_5.ref();
1177                         this.el.append( child_5.el );
1178                 }
1179
1180                 // user defined functions
1181         }
1182         public class Xcls_search_entry : Object
1183         {
1184                 public Gtk.SearchEntry el;
1185                 private Editor  _this;
1186
1187
1188                         // my vars (def)
1189                 public Gtk.CssProvider css;
1190
1191                 // ctor
1192                 public Xcls_search_entry(Editor _owner )
1193                 {
1194                         _this = _owner;
1195                         _this.search_entry = this;
1196                         this.el = new Gtk.SearchEntry();
1197
1198                         // my vars (dec)
1199
1200                         // set gobject values
1201                         this.el.name = "editor-search-entry";
1202                         this.el.hexpand = true;
1203                         this.el.placeholder_text = "Press enter to search";
1204                         this.el.search_delay = 3;
1205                         var child_1 = new Xcls_EventControllerKey14( _this );
1206                         child_1.ref();
1207                         this.el.add_controller(  child_1.el );
1208
1209                         //listeners
1210                         this.el.search_changed.connect( ( ) => {
1211                         
1212                         _this.search(_this.search_entry.el.text);
1213                                  _this.search_results.updateResults();
1214                         
1215                                 GLib.Timeout.add_seconds(1,() => {
1216                                          _this.search_results.updateResults();
1217                                          return false;
1218                                  });
1219                         });
1220                 }
1221
1222                 // user defined functions
1223                 public void forwardSearch (bool change_focus) {
1224                 
1225                 
1226                         _this.forwardSearch(change_focus);
1227                 
1228                 /*
1229                 
1230                         switch(_this.windowstate.state) {
1231                                 case WindowState.State.CODEONLY:
1232                                 //case WindowState.State.CODE:
1233                                         // search the code being edited..
1234                                         _this.windowstate.code_editor_tab.forwardSearch(change_focus);
1235                                          
1236                                         break;
1237                                 case WindowState.State.PREVIEW:
1238                                         if (_this.windowstate.file.xtype == "Gtk") {
1239                                                 _this.windowstate.window_gladeview.forwardSearch(change_focus);
1240                                         } else { 
1241                                                  _this.windowstate.window_rooview.forwardSearch(change_focus);
1242                                         }
1243                                 
1244                                         break;
1245                         }
1246                         */
1247                         
1248                 }
1249         }
1250         public class Xcls_EventControllerKey14 : Object
1251         {
1252                 public Gtk.EventControllerKey el;
1253                 private Editor  _this;
1254
1255
1256                         // my vars (def)
1257
1258                 // ctor
1259                 public Xcls_EventControllerKey14(Editor _owner )
1260                 {
1261                         _this = _owner;
1262                         this.el = new Gtk.EventControllerKey();
1263
1264                         // my vars (dec)
1265
1266                         // set gobject values
1267
1268                         //listeners
1269                         this.el.key_pressed.connect( (keyval, keycode, state) => {
1270                         
1271                                 if (keyval == Gdk.Key.g && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1272                                     GLib.debug("SAVE: ctrl-g  pressed");
1273                                         _this.forwardSearch(true);
1274                                     return true;
1275                                 }
1276                             
1277                           
1278                                 if (keyval == Gdk.Key.Return && _this.search_entry.el.text.length > 0) {
1279                                         _this.forwardSearch(true);
1280                                         
1281                                         
1282                                     return true;
1283                         
1284                                 }    
1285                            // print(event.key.keyval)
1286                            
1287                             return false;
1288                         });
1289                 }
1290
1291                 // user defined functions
1292         }
1293
1294
1295         public class Xcls_search_results : Object
1296         {
1297                 public Gtk.Label el;
1298                 private Editor  _this;
1299
1300
1301                         // my vars (def)
1302
1303                 // ctor
1304                 public Xcls_search_results(Editor _owner )
1305                 {
1306                         _this = _owner;
1307                         _this.search_results = this;
1308                         this.el = new Gtk.Label( "No Results" );
1309
1310                         // my vars (dec)
1311
1312                         // set gobject values
1313                         this.el.margin_end = 4;
1314                         this.el.margin_start = 4;
1315                 }
1316
1317                 // user defined functions
1318                 public void updateResults () {
1319                         this.el.visible = true;
1320                         
1321                         var res = _this.searchcontext.get_occurrences_count();
1322                         if (res < 0) {
1323                                 _this.search_results.el.label = "??? Matches";          
1324                                 return;
1325                         }
1326                 
1327                         _this.nextBtn.el.sensitive = false;
1328                         _this.backBtn.el.sensitive = false;     
1329                 
1330                         if (res > 0) {
1331                                 _this.search_results.el.label = "%d Matches".printf(res);
1332                                 _this.nextBtn.el.sensitive = true;
1333                                 _this.backBtn.el.sensitive = true;
1334                                 return;
1335                         } 
1336                         _this.search_results.el.label = "No Matches";
1337                         
1338                 }
1339         }
1340
1341         public class Xcls_nextBtn : Object
1342         {
1343                 public Gtk.Button el;
1344                 private Editor  _this;
1345
1346
1347                         // my vars (def)
1348                 public bool always_show_image;
1349
1350                 // ctor
1351                 public Xcls_nextBtn(Editor _owner )
1352                 {
1353                         _this = _owner;
1354                         _this.nextBtn = this;
1355                         this.el = new Gtk.Button();
1356
1357                         // my vars (dec)
1358                         this.always_show_image = true;
1359
1360                         // set gobject values
1361                         this.el.icon_name = "go-down";
1362                         this.el.sensitive = false;
1363
1364                         //listeners
1365                         this.el.clicked.connect( (event) => {
1366                         
1367                                 _this.forwardSearch(true);
1368                                 
1369                                  
1370                         });
1371                 }
1372
1373                 // user defined functions
1374         }
1375
1376         public class Xcls_backBtn : Object
1377         {
1378                 public Gtk.Button el;
1379                 private Editor  _this;
1380
1381
1382                         // my vars (def)
1383                 public bool always_show_image;
1384
1385                 // ctor
1386                 public Xcls_backBtn(Editor _owner )
1387                 {
1388                         _this = _owner;
1389                         _this.backBtn = this;
1390                         this.el = new Gtk.Button();
1391
1392                         // my vars (dec)
1393                         this.always_show_image = true;
1394
1395                         // set gobject values
1396                         this.el.icon_name = "go-up";
1397                         this.el.sensitive = false;
1398
1399                         //listeners
1400                         this.el.clicked.connect( (event) => {
1401                         
1402                                 _this.backSearch(true);
1403                                  
1404                         });
1405                 }
1406
1407                 // user defined functions
1408         }
1409
1410         public class Xcls_MenuButton18 : Object
1411         {
1412                 public Gtk.MenuButton el;
1413                 private Editor  _this;
1414
1415
1416                         // my vars (def)
1417                 public bool always_show_image;
1418
1419                 // ctor
1420                 public Xcls_MenuButton18(Editor _owner )
1421                 {
1422                         _this = _owner;
1423                         this.el = new Gtk.MenuButton();
1424
1425                         // my vars (dec)
1426                         this.always_show_image = true;
1427
1428                         // set gobject values
1429                         this.el.icon_name = "emblem-system";
1430                         this.el.always_show_arrow = true;
1431                         new Xcls_search_settings( _this );
1432                         this.el.popover = _this.search_settings.el;
1433                 }
1434
1435                 // user defined functions
1436         }
1437         public class Xcls_search_settings : Object
1438         {
1439                 public Gtk.Popover el;
1440                 private Editor  _this;
1441
1442
1443                         // my vars (def)
1444
1445                 // ctor
1446                 public Xcls_search_settings(Editor _owner )
1447                 {
1448                         _this = _owner;
1449                         _this.search_settings = this;
1450                         this.el = new Gtk.Popover();
1451
1452                         // my vars (dec)
1453
1454                         // set gobject values
1455                         var child_1 = new Xcls_Box20( _this );
1456                         this.el.child = child_1.el;
1457                 }
1458
1459                 // user defined functions
1460         }
1461         public class Xcls_Box20 : Object
1462         {
1463                 public Gtk.Box el;
1464                 private Editor  _this;
1465
1466
1467                         // my vars (def)
1468
1469                 // ctor
1470                 public Xcls_Box20(Editor _owner )
1471                 {
1472                         _this = _owner;
1473                         this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
1474
1475                         // my vars (dec)
1476
1477                         // set gobject values
1478                         new Xcls_case_sensitive( _this );
1479                         this.el.append( _this.case_sensitive.el );
1480                         new Xcls_regex( _this );
1481                         this.el.append( _this.regex.el );
1482                         new Xcls_multiline( _this );
1483                         this.el.append( _this.multiline.el );
1484                 }
1485
1486                 // user defined functions
1487         }
1488         public class Xcls_case_sensitive : Object
1489         {
1490                 public Gtk.CheckButton el;
1491                 private Editor  _this;
1492
1493
1494                         // my vars (def)
1495
1496                 // ctor
1497                 public Xcls_case_sensitive(Editor _owner )
1498                 {
1499                         _this = _owner;
1500                         _this.case_sensitive = this;
1501                         this.el = new Gtk.CheckButton();
1502
1503                         // my vars (dec)
1504
1505                         // set gobject values
1506                         this.el.label = "Case Sensitive";
1507
1508                         // init method
1509
1510                         {
1511                                 this.el.show();
1512                         }
1513                 }
1514
1515                 // user defined functions
1516         }
1517
1518         public class Xcls_regex : Object
1519         {
1520                 public Gtk.CheckButton el;
1521                 private Editor  _this;
1522
1523
1524                         // my vars (def)
1525
1526                 // ctor
1527                 public Xcls_regex(Editor _owner )
1528                 {
1529                         _this = _owner;
1530                         _this.regex = this;
1531                         this.el = new Gtk.CheckButton();
1532
1533                         // my vars (dec)
1534
1535                         // set gobject values
1536                         this.el.label = "Regex";
1537
1538                         // init method
1539
1540                         {
1541                                 this.el.show();
1542                         }
1543                 }
1544
1545                 // user defined functions
1546         }
1547
1548         public class Xcls_multiline : Object
1549         {
1550                 public Gtk.CheckButton el;
1551                 private Editor  _this;
1552
1553
1554                         // my vars (def)
1555
1556                 // ctor
1557                 public Xcls_multiline(Editor _owner )
1558                 {
1559                         _this = _owner;
1560                         _this.multiline = this;
1561                         this.el = new Gtk.CheckButton();
1562
1563                         // my vars (dec)
1564
1565                         // set gobject values
1566                         this.el.label = "Multi-line (add \\n)";
1567                 }
1568
1569                 // user defined functions
1570         }
1571
1572
1573
1574
1575
1576 }