6d7d33440c0642bd04c63f0a9463b54f0fc5fba8
[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_Box1( _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_Box11( _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, "ERR");
281                         buf.remove_source_marks (start, end, "WARN");
282                         buf.remove_source_marks (start, end, "DEPR");
283                         buf.remove_tag_by_name ("ERR", start, end);
284                         buf.remove_tag_by_name ("WARN", start, end);
285                         buf.remove_tag_by_name ("DEPR", start, end);
286                         this.last_error_counter = file.error_counter ;
287                         //GLib.debug("highlight %s :  %s has no errors", this.file.relpath, category);
288                         return;
289                 }
290                 
291         
292          // basicaly check if there is no change, then we do not do any update..
293          // we can do this by just using an error counter?
294          // if that's changed then we will do an update, otherwise dont bother.
295                   
296                 
297                 var offset = 0;
298                 var hoffset = 0;
299         
300                 var tlines = buf.get_line_count () +1;
301                 
302                 if (_this.prop != null) {
303                         // this still seems flaky...
304         
305                         tlines = _this.prop.end_line;
306                         offset = _this.prop.start_line;
307                         hoffset = _this.node.node_pad.length + 2; //shift it left  by 2 ? ..
308                         
309                          
310                 } else {
311                         // no update...
312                         if (this.last_error_counter == file.error_counter) {
313                         
314                                 return;
315                         }
316                 
317                 }
318                 buf.remove_source_marks (start, end, "ERR");
319                 buf.remove_source_marks (start, end, "WARN");
320                 buf.remove_source_marks (start, end, "DEPR");
321                 buf.remove_tag_by_name ("ERR", start, end);
322                 buf.remove_tag_by_name ("WARN", start, end);
323                 buf.remove_tag_by_name ("DEPR", start, end);
324                 
325                 foreach(var diag in ar) { 
326                      Gtk.TextIter iter;
327         //        print("get inter\n");
328                     var eline = (int)diag.range.start.line - offset;
329                     var eline_to = (int)diag.range.end.line - offset;
330                     //var eline =  diag.range.end_line - offset;
331                     //GLib.debug("GOT ERROR on line %d -- converted to %d  (offset = %d)",
332                     //  err.line ,eline, offset);
333                     
334                     
335                     if (eline > tlines || eline < 0) {
336                         continue;
337                     }
338                     
339                     buf.get_iter_at_line( out iter, eline);
340                         var msg = "Line: %d %s : %s".printf(eline+1, diag.category, diag.message);
341                     buf.create_source_mark( msg, diag.category, iter);
342                     
343                     var spos = (int)diag.range.start.character - hoffset;
344                     if (spos < 0) { spos =0 ; }
345                     if (spos > iter.get_chars_in_line()) {
346                         spos = iter.get_chars_in_line();
347                 }
348                         buf.get_iter_at_line( out iter, eline_to);
349                         var epos = (int)diag.range.end.character - hoffset;
350                     if (epos < 0) { epos =0 ; }
351                     if (epos > iter.get_chars_in_line()) {
352                         epos = iter.get_chars_in_line();
353                 }
354                      
355                     
356                     buf.get_iter_at_line_offset( out start, eline, spos); 
357                    
358                     buf.get_iter_at_line_offset( out end, eline_to,epos); 
359                         
360                     buf.apply_tag_by_name(diag.category, start, end);
361                     
362                    // GLib.debug("set line %d to %s", eline, msg);
363                     //this.marks.set(eline, msg);
364                 }
365                 this.last_error_counter = file.error_counter ;
366         
367         
368         
369          
370         
371         }
372         public void scroll_to_line (int line) {
373         
374                 GLib.Timeout.add(500, () => {
375            
376                         var buf = this.view.el.get_buffer();
377         
378                         var sbuf = (GtkSource.Buffer) buf;
379         
380         
381                         Gtk.TextIter iter;   
382                         sbuf.get_iter_at_line(out iter,  line);
383                         this.view.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);
384                         return false;
385                 });   
386         }
387         public class Xcls_Box1 : Object
388         {
389                 public Gtk.Box el;
390                 private Editor  _this;
391
392
393                         // my vars (def)
394
395                 // ctor
396                 public Xcls_Box1(Editor _owner )
397                 {
398                         _this = _owner;
399                         this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
400
401                         // my vars (dec)
402
403                         // set gobject values
404                         this.el.homogeneous = false;
405                         this.el.hexpand = true;
406                         new Xcls_save_button( _this );
407                         this.el.append( _this.save_button.el );
408                         var child_2 = new Xcls_Label3( _this );
409                         child_2.ref();
410                         this.el.append( child_2.el );
411                         var child_3 = new Xcls_Scale4( _this );
412                         child_3.ref();
413                         this.el.append( child_3.el );
414                         new Xcls_close_btn( _this );
415                         this.el.append( _this.close_btn.el );
416                 }
417
418                 // user defined functions
419         }
420         public class Xcls_save_button : Object
421         {
422                 public Gtk.Button el;
423                 private Editor  _this;
424
425
426                         // my vars (def)
427
428                 // ctor
429                 public Xcls_save_button(Editor _owner )
430                 {
431                         _this = _owner;
432                         _this.save_button = this;
433                         this.el = new Gtk.Button();
434
435                         // my vars (dec)
436
437                         // set gobject values
438                         this.el.label = "Save";
439
440                         //listeners
441                         this.el.clicked.connect( () => { 
442                             _this.saveContents();
443                         });
444                 }
445
446                 // user defined functions
447         }
448
449         public class Xcls_Label3 : Object
450         {
451                 public Gtk.Label el;
452                 private Editor  _this;
453
454
455                         // my vars (def)
456
457                 // ctor
458                 public Xcls_Label3(Editor _owner )
459                 {
460                         _this = _owner;
461                         this.el = new Gtk.Label( null );
462
463                         // my vars (dec)
464
465                         // set gobject values
466                         this.el.hexpand = true;
467                 }
468
469                 // user defined functions
470         }
471
472         public class Xcls_Scale4 : Object
473         {
474                 public Gtk.Scale el;
475                 private Editor  _this;
476
477
478                         // my vars (def)
479
480                 // ctor
481                 public Xcls_Scale4(Editor _owner )
482                 {
483                         _this = _owner;
484                         this.el = new Gtk.Scale.with_range (Gtk.Orientation.HORIZONTAL,6, 30, 1);
485
486                         // my vars (dec)
487
488                         // set gobject values
489                         this.el.width_request = 200;
490                         this.el.has_origin = true;
491                         this.el.draw_value = false;
492                         this.el.digits = 0;
493                         this.el.sensitive = true;
494
495                         // init method
496
497                         {
498                                 //this.el.set_range(6,30);
499                                 this.el.set_value ( BuilderApplication.settings.editor_font_size);
500                                 BuilderApplication.settings.editor_font_size_updated.connect(
501                                         () => {
502                                                 BuilderApplication.settings.editor_font_size_inchange = true;
503                                                 GLib.debug("update range");
504                                                 this.el.set_value (BuilderApplication.settings.editor_font_size);
505                                                 BuilderApplication.settings.editor_font_size_inchange = false;
506                                         }
507                                 );
508                                 
509                          
510                         }
511
512                         //listeners
513                         this.el.change_value.connect( (st, val ) => {
514                                 if (BuilderApplication.settings.editor_font_size_inchange) {
515                                         return false;
516                                 }
517                                 BuilderApplication.settings.editor_font_size = val;
518                                 return false;
519                         });
520                 }
521
522                 // user defined functions
523         }
524
525         public class Xcls_close_btn : Object
526         {
527                 public Gtk.Button el;
528                 private Editor  _this;
529
530
531                         // my vars (def)
532
533                 // ctor
534                 public Xcls_close_btn(Editor _owner )
535                 {
536                         _this = _owner;
537                         _this.close_btn = this;
538                         this.el = new Gtk.Button();
539
540                         // my vars (dec)
541
542                         // set gobject values
543                         this.el.icon_name = "window-close";
544                         var child_1 = new Xcls_Image6( _this );
545                         child_1.ref();
546                         this.el.child = child_1.el;
547
548                         //listeners
549                         this.el.clicked.connect( () => { 
550                             _this.saveContents();
551                             _this.window.windowstate.switchState(WindowState.State.PREVIEW);
552                         });
553                 }
554
555                 // user defined functions
556         }
557         public class Xcls_Image6 : Object
558         {
559                 public Gtk.Image el;
560                 private Editor  _this;
561
562
563                         // my vars (def)
564
565                 // ctor
566                 public Xcls_Image6(Editor _owner )
567                 {
568                         _this = _owner;
569                         this.el = new Gtk.Image();
570
571                         // my vars (dec)
572
573                         // set gobject values
574                         this.el.icon_name = "window-close";
575                         this.el.icon_size = Gtk.IconSize.NORMAL;
576                 }
577
578                 // user defined functions
579         }
580
581
582
583         public class Xcls_RightEditor : Object
584         {
585                 public Gtk.ScrolledWindow el;
586                 private Editor  _this;
587
588
589                         // my vars (def)
590
591                 // ctor
592                 public Xcls_RightEditor(Editor _owner )
593                 {
594                         _this = _owner;
595                         _this.RightEditor = this;
596                         this.el = new Gtk.ScrolledWindow();
597
598                         // my vars (dec)
599
600                         // set gobject values
601                         this.el.vscrollbar_policy = Gtk.PolicyType.AUTOMATIC;
602                         this.el.vexpand = true;
603                         this.el.overlay_scrolling = false;
604                         this.el.hscrollbar_policy = Gtk.PolicyType.AUTOMATIC;
605                         new Xcls_view( _this );
606                         this.el.child = _this.view.el;
607                 }
608
609                 // user defined functions
610         }
611         public class Xcls_view : Object
612         {
613                 public GtkSource.View el;
614                 private Editor  _this;
615
616
617                         // my vars (def)
618                 public Gtk.CssProvider css;
619
620                 // ctor
621                 public Xcls_view(Editor _owner )
622                 {
623                         _this = _owner;
624                         _this.view = this;
625                         this.el = new GtkSource.View();
626
627                         // my vars (dec)
628                         this.css = null;
629
630                         // set gobject values
631                         this.el.auto_indent = true;
632                         this.el.indent_width = 4;
633                         this.el.name = "editor-view";
634                         this.el.show_line_marks = true;
635                         this.el.insert_spaces_instead_of_tabs = true;
636                         this.el.show_line_numbers = true;
637                         this.el.hexpand = true;
638                         this.el.vexpand = true;
639                         this.el.has_tooltip = true;
640                         this.el.css_classes = { "code-editor" };
641                         this.el.tab_width = 4;
642                         this.el.highlight_current_line = true;
643                         new Xcls_buffer( _this );
644                         this.el.buffer = _this.buffer.el;
645                         var child_2 = new Xcls_EventControllerKey10( _this );
646                         child_2.ref();
647                         this.el.add_controller(  child_2.el );
648
649                         // init method
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_EventControllerKey10 : Object
1079         {
1080                 public Gtk.EventControllerKey el;
1081                 private Editor  _this;
1082
1083
1084                         // my vars (def)
1085
1086                 // ctor
1087                 public Xcls_EventControllerKey10(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_Box11 : Object
1149         {
1150                 public Gtk.Box el;
1151                 private Editor  _this;
1152
1153
1154                         // my vars (def)
1155
1156                 // ctor
1157                 public Xcls_Box11(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_MenuButton17( _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_EventControllerKey13( _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_EventControllerKey13 : Object
1251         {
1252                 public Gtk.EventControllerKey el;
1253                 private Editor  _this;
1254
1255
1256                         // my vars (def)
1257
1258                 // ctor
1259                 public Xcls_EventControllerKey13(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_MenuButton17 : 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_MenuButton17(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_Box19( _this );
1456                         child_1.ref();
1457                         this.el.child = child_1.el;
1458                 }
1459
1460                 // user defined functions
1461         }
1462         public class Xcls_Box19 : Object
1463         {
1464                 public Gtk.Box el;
1465                 private Editor  _this;
1466
1467
1468                         // my vars (def)
1469
1470                 // ctor
1471                 public Xcls_Box19(Editor _owner )
1472                 {
1473                         _this = _owner;
1474                         this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
1475
1476                         // my vars (dec)
1477
1478                         // set gobject values
1479                         new Xcls_case_sensitive( _this );
1480                         this.el.append( _this.case_sensitive.el );
1481                         new Xcls_regex( _this );
1482                         this.el.append( _this.regex.el );
1483                         new Xcls_multiline( _this );
1484                         this.el.append( _this.multiline.el );
1485                 }
1486
1487                 // user defined functions
1488         }
1489         public class Xcls_case_sensitive : Object
1490         {
1491                 public Gtk.CheckButton el;
1492                 private Editor  _this;
1493
1494
1495                         // my vars (def)
1496
1497                 // ctor
1498                 public Xcls_case_sensitive(Editor _owner )
1499                 {
1500                         _this = _owner;
1501                         _this.case_sensitive = this;
1502                         this.el = new Gtk.CheckButton();
1503
1504                         // my vars (dec)
1505
1506                         // set gobject values
1507                         this.el.label = "Case Sensitive";
1508
1509                         // init method
1510
1511                         {
1512                                 this.el.show();
1513                         }
1514                 }
1515
1516                 // user defined functions
1517         }
1518
1519         public class Xcls_regex : Object
1520         {
1521                 public Gtk.CheckButton el;
1522                 private Editor  _this;
1523
1524
1525                         // my vars (def)
1526
1527                 // ctor
1528                 public Xcls_regex(Editor _owner )
1529                 {
1530                         _this = _owner;
1531                         _this.regex = this;
1532                         this.el = new Gtk.CheckButton();
1533
1534                         // my vars (dec)
1535
1536                         // set gobject values
1537                         this.el.label = "Regex";
1538
1539                         // init method
1540
1541                         {
1542                                 this.el.show();
1543                         }
1544                 }
1545
1546                 // user defined functions
1547         }
1548
1549         public class Xcls_multiline : Object
1550         {
1551                 public Gtk.CheckButton el;
1552                 private Editor  _this;
1553
1554
1555                         // my vars (def)
1556
1557                 // ctor
1558                 public Xcls_multiline(Editor _owner )
1559                 {
1560                         _this = _owner;
1561                         _this.multiline = this;
1562                         this.el = new Gtk.CheckButton();
1563
1564                         // my vars (dec)
1565
1566                         // set gobject values
1567                         this.el.label = "Multi-line (add \\n)";
1568                 }
1569
1570                 // user defined functions
1571         }
1572
1573
1574
1575
1576
1577 }