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