Fix #8025 - move css to resources
[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                         this.el.child = child_1.el;
546
547                         //listeners
548                         this.el.clicked.connect( () => { 
549                             _this.saveContents();
550                             _this.window.windowstate.switchState(WindowState.State.PREVIEW);
551                         });
552                 }
553
554                 // user defined functions
555         }
556         public class Xcls_Image6 : Object
557         {
558                 public Gtk.Image el;
559                 private Editor  _this;
560
561
562                         // my vars (def)
563
564                 // ctor
565                 public Xcls_Image6(Editor _owner )
566                 {
567                         _this = _owner;
568                         this.el = new Gtk.Image();
569
570                         // my vars (dec)
571
572                         // set gobject values
573                         this.el.icon_name = "window-close";
574                         this.el.icon_size = Gtk.IconSize.NORMAL;
575                 }
576
577                 // user defined functions
578         }
579
580
581
582         public class Xcls_RightEditor : Object
583         {
584                 public Gtk.ScrolledWindow el;
585                 private Editor  _this;
586
587
588                         // my vars (def)
589
590                 // ctor
591                 public Xcls_RightEditor(Editor _owner )
592                 {
593                         _this = _owner;
594                         _this.RightEditor = this;
595                         this.el = new Gtk.ScrolledWindow();
596
597                         // my vars (dec)
598
599                         // set gobject values
600                         this.el.vscrollbar_policy = Gtk.PolicyType.AUTOMATIC;
601                         this.el.vexpand = true;
602                         this.el.overlay_scrolling = false;
603                         this.el.hscrollbar_policy = Gtk.PolicyType.AUTOMATIC;
604                         new Xcls_view( _this );
605                         this.el.child = _this.view.el;
606                 }
607
608                 // user defined functions
609         }
610         public class Xcls_view : Object
611         {
612                 public GtkSource.View el;
613                 private Editor  _this;
614
615
616                         // my vars (def)
617                 public Gtk.CssProvider css;
618
619                 // ctor
620                 public Xcls_view(Editor _owner )
621                 {
622                         _this = _owner;
623                         _this.view = this;
624                         this.el = new GtkSource.View();
625
626                         // my vars (dec)
627                         this.css = null;
628
629                         // set gobject values
630                         this.el.auto_indent = true;
631                         this.el.indent_width = 4;
632                         this.el.name = "editor-view";
633                         this.el.show_line_marks = true;
634                         this.el.insert_spaces_instead_of_tabs = true;
635                         this.el.show_line_numbers = true;
636                         this.el.hexpand = true;
637                         this.el.vexpand = true;
638                         this.el.has_tooltip = true;
639                         this.el.css_classes = { "code-editor" };
640                         this.el.tab_width = 4;
641                         this.el.highlight_current_line = true;
642                         new Xcls_buffer( _this );
643                         this.el.buffer = _this.buffer.el;
644                         var child_2 = new Xcls_EventControllerKey10( _this );
645                         child_2.ref();
646                         this.el.add_controller(  child_2.el );
647
648                         // init method
649
650                         this.el.completion.add_provider(
651                                 new Palete.CompletionProvider(_this)
652                         );
653                           
654                         //this.el.completion.unblock_interactive();
655                         this.el.completion.select_on_show = true; // select
656                         //this.el.completion.remember_info_visibility    = true;
657                         
658                         
659                         var attrs = new GtkSource.MarkAttributes();
660                         
661                         attrs.set_icon_name ( "process-stop");    
662                         attrs.query_tooltip_text.connect(( mark) => {
663                              GLib.debug("tooltip query? %s", mark.name);
664                             return strdup( mark.name);
665                         });
666                          attrs.query_tooltip_markup.connect(( mark) => {
667                              GLib.debug("tooltip query? %s", mark.name);
668                             return strdup( mark.name);
669                         });
670                         this.el.set_mark_attributes ("ERR", attrs, 1);
671                         attrs.ref();
672                         
673                         
674                         var wattrs = new GtkSource.MarkAttributes();
675                         wattrs.set_icon_name ( "process-stop");    
676                         wattrs.query_tooltip_text.connect(( mark) => {
677                              GLib.debug("tooltip query? %s", mark.name);
678                             return strdup(mark.name);
679                         });
680                         wattrs.query_tooltip_markup.connect(( mark) => {
681                              GLib.debug("tooltip query? %s", mark.name);
682                             return strdup(mark.name);
683                         });
684                         this.el.set_mark_attributes ("WARN", wattrs, 1);
685                         wattrs.ref();
686                         
687                          
688                         var dattrs = new GtkSource.MarkAttributes();
689                          
690                         dattrs.set_icon_name ( "process-stop"); 
691                         
692                         dattrs.query_tooltip_text.connect(( mark) => {
693                                 GLib.debug("tooltip query? %s", mark.name);
694                             return strdup(mark.name);
695                         });
696                         //dattrs.query_tooltip_markup.connect(( mark) => {
697                         //      GLib.debug("tooltip query? %s", mark.name);
698                          //   return strdup(mark.name);
699                         //});
700                         this.el.set_mark_attributes ("DEPR", dattrs, 1);
701                         dattrs.ref();    
702                         
703                          this.el.get_space_drawer().set_matrix(null);
704                          this.el.get_space_drawer().set_types_for_locations( 
705                                 GtkSource.SpaceLocationFlags.ALL,
706                                 GtkSource.SpaceTypeFlags.ALL
707                         );
708                         this.el.get_space_drawer().set_enable_matrix(true);
709
710                         //listeners
711                         this.el.query_tooltip.connect( (x, y, keyboard_tooltip, tooltip) => {
712                                 
713                                 //GLib.debug("query tooltip");
714                                 Gtk.TextIter iter;
715                                 int trailing;
716                                 
717                                 var yoff = (int) _this.RightEditor.el.vadjustment.value;
718                                 
719                                 // I think this is problematic - if it's compliing  / updating at same time as query.
720                                 
721                                 //if (_this.window.statusbar_compile_spinner.el.spinning) {
722                                 //      return false;
723                                 //}
724                                 
725                                 this.el.get_iter_at_position (out iter, out trailing,  x,  y + yoff);
726                                  
727                                 var l = iter.get_line();
728                         
729                                 
730                                  
731                                 // GLib.debug("query tooltip line %d", (int) l);
732                                 if (l < 0) {
733                         
734                                         return false;
735                                 }
736                                 /*
737                                 if (_this.buffer.marks != null && _this.buffer.marks.has_key(l)) {
738                                         GLib.debug("line %d setting tip to %s", l,  _this.buffer.marks.get(l));
739                                         tooltip.set_text(_this.buffer.marks.get(l).dup());
740                                         return true;
741                                 }
742                          
743                                 return false;
744                                 */
745                                 
746                                   
747                                 // this crashes?? - not sure why.
748                                 var marks = _this.buffer.el.get_source_marks_at_line(l, "ERR");
749                                 if (marks.is_empty()) {
750                                         marks = _this.buffer.el.get_source_marks_at_line(l, "WARN");
751                                 }
752                                 if (marks.is_empty()) {
753                                         marks = _this.buffer.el.get_source_marks_at_line(l, "DEPR");
754                                 }
755                                 
756                                 // GLib.debug("query tooltip line %d marks %d", (int)l, (int) marks.length());
757                                 var str = "";
758                                 marks.@foreach((m) => { 
759                                         //GLib.debug("got mark %s", m.name);
760                                         str += (str.length > 0 ? "\n" : "") + m.category + ": " + m.name;
761                                 });
762                                 // true if there is a mark..
763                                 if (str.length > 0 ) {
764                                         tooltip.set_text( str );
765                                 }
766                                 return str.length > 0 ? true : false;
767                                  
768                         });
769                 }
770
771                 // user defined functions
772                 public void load (string str) {
773                 
774                 // show the help page for the active node..
775                    //this.get('/Help').show();
776                  
777                   // this.get('/BottomPane').el.set_current_page(0);
778                         GLib.debug("load called - Reset undo buffer");
779                         
780                     var buf = (GtkSource.Buffer)this.el.get_buffer();
781                     buf.begin_irreversible_action();
782                     buf.set_text(str, str.length);
783                     buf.end_irreversible_action();
784                     
785                     var lm = GtkSource.LanguageManager.get_default();
786                     var lang = "vala";
787                     if (_this.file != null) {
788                          lang = _this.file.language;
789                     }
790                     print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
791                     var lg = _this.file.content_type.length > 0  ?
792                             lm.guess_language(_this.file.path, _this.file.content_type) :
793                             lm.get_language(lang);
794                      
795                    
796                     ((GtkSource.Buffer)(this.el.get_buffer())) .set_language(lg); 
797                 
798                     this.el.insert_spaces_instead_of_tabs = true;
799                     if (lg != null) {
800                                 print("sourcelanguage  = %s\n", lg.name);
801                                 if (lg.name == "Vala") {
802                                     this.el.insert_spaces_instead_of_tabs = false;
803                                 }
804                      }
805                     _this.dirty = false;
806                     this.el.grab_focus();
807                     _this.save_button.el.sensitive = false;
808                     _this.last_error_counter = -1;
809                 }
810         }
811         public class Xcls_buffer : Object
812         {
813                 public GtkSource.Buffer el;
814                 private Editor  _this;
815
816
817                         // my vars (def)
818                 public int error_line;
819                 public Gee.HashMap<int,string>? xmarks;
820                 public bool check_queued;
821
822                 // ctor
823                 public Xcls_buffer(Editor _owner )
824                 {
825                         _this = _owner;
826                         _this.buffer = this;
827                         this.el = new GtkSource.Buffer( null );
828
829                         // my vars (dec)
830                         this.error_line = -1;
831                         this.xmarks = null;
832                         this.check_queued = false;
833
834                         // set gobject values
835                         this.el.highlight_syntax = true;
836                         this.el.highlight_matching_brackets = true;
837                         this.el.enable_undo = true;
838
839                         // init method
840
841                         var buf = this.el;
842                         buf.create_tag ("bold", "weight", Pango.Weight.BOLD);
843                         buf.create_tag ("type", "weight", Pango.Weight.BOLD, "foreground", "#204a87");
844                         buf.create_tag ("keyword", "weight", Pango.Weight.BOLD, "foreground", "#a40000");
845                         buf.create_tag ("text", "weight", Pango.Weight.NORMAL, "foreground", "#729fcf");
846                         buf.create_tag ("number", "weight", Pango.Weight.BOLD, "foreground", "#ad7fa8");
847                         buf.create_tag ("method", "weight", Pango.Weight.BOLD, "foreground", "#729fcf");
848                         buf.create_tag ("property", "weight", Pango.Weight.BOLD, "foreground", "#BC1F51");
849                         buf.create_tag ("variable", "weight", Pango.Weight.BOLD, "foreground", "#A518B5");
850                         
851                         
852                         buf.create_tag ("ERR", "weight", Pango.Weight.BOLD, "background", "pink");
853                         buf.create_tag ("WARN", "weight", Pango.Weight.BOLD, "background", "#ABF4EB");
854                         buf.create_tag ("DEPR", "weight", Pango.Weight.BOLD, "background", "#EEA9FF");
855
856                         //listeners
857                         this.el.changed.connect( () => {
858                             // check syntax??
859                             // ??needed..??
860                             _this.save_button.el.sensitive = true;
861                             print("EDITOR CHANGED");
862                             this.checkSyntax();
863                            
864                             _this.dirty = true;
865                         
866                             // this.get('/LeftPanel.model').changed(  str , false);
867                             return ;
868                         });
869                 }
870
871                 // user defined functions
872                 public bool OLDhighlightErrorsJson (string type, Json.Object obj) {
873                         Gtk.TextIter start;
874                         Gtk.TextIter end;     
875                         this.el.get_bounds (out start, out end);
876                 
877                         this.el.remove_source_marks (start, end, type);
878                         GLib.debug("highlight errors");          
879                 
880                          // we should highlight other types of errors..
881                 
882                         if (!obj.has_member(type)) {
883                                 GLib.debug("Return has no errors\n");
884                                 return true;
885                         }
886                 
887                         if (_this.window.windowstate.state != WindowState.State.CODEONLY 
888                                 &&
889                                 _this.window.windowstate.state != WindowState.State.CODE
890                                 ) {
891                                 GLib.debug("windowstate != CODEONLY?");
892                                 
893                                 return true;
894                         } 
895                 
896                         //this.marks = new Gee.HashMap<int,string>();
897                         var err = obj.get_object_member(type);
898                  
899                         if (_this.file == null) {
900                                 GLib.debug("file is null?");
901                                 return true;
902                 
903                         }
904                         var valafn = _this.file.path;
905                 
906                         if (_this.file.xtype != "PlainFile") {
907                 
908                                 valafn = "";
909                                 try {             
910                                         var  regex = new Regex("\\.bjs$");
911                                         // should not happen
912                                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
913                                 } catch (GLib.RegexError e) {
914                                         return true;
915                                 }   
916                 
917                 
918                 
919                         }
920                         if (!err.has_member(valafn)) {
921                                 GLib.debug("File path has no errors");
922                                 return  true;
923                         }
924                 
925                         var lines = err.get_object_member(valafn);
926                         
927                         var offset = 1;
928                         if (obj.has_member("line_offset")) { // ?? why??
929                                 offset = (int)obj.get_int_member("line_offset") + 1;
930                         }
931                 
932                 
933                         var tlines = this.el.get_line_count () +1;
934                         
935                         if (_this.prop != null) {
936                         
937                                 tlines = _this.prop.end_line + 1;
938                                 offset = _this.prop.start_line + 1;
939                         
940                         }
941                         
942                 
943                 
944                         lines.foreach_member((obj, line, node) => {
945                                 
946                              Gtk.TextIter iter;
947                 //        print("get inter\n");
948                             var eline = int.parse(line) - offset;
949                             GLib.debug("GOT ERROR on line %s -- converted to %d  (offset = %d)\n", line,eline, offset);
950                             
951                             
952                             if (eline > tlines || eline < 0) {
953                                 return;
954                             }
955                            
956                             
957                             this.el.get_iter_at_line( out iter, eline);
958                             //print("mark line\n");
959                             var msg  = "";
960                             var ar = lines.get_array_member(line);
961                             for (var i = 0 ; i < ar.get_length(); i++) {
962                                 if (ar.get_string_element(i) == "Success") {
963                                         continue;
964                                 }
965                                         msg += (msg.length > 0) ? "\n" : "";
966                                         msg += ar.get_string_element(i);
967                                 }
968                                 if (msg == "") {
969                                         return;
970                                 }
971                                 msg = "Line: %d".printf(eline+1) +  " " + msg;
972                             this.el.create_source_mark(msg, type, iter);
973                             GLib.debug("set line %d to %m", eline, msg);
974                            // this.marks.set(eline, msg);
975                         } );
976                         return false;
977                 
978                 
979                 
980                 
981                 
982                         }
983                 public bool checkSyntax () {
984                  
985                     
986                     var str = this.toString();
987                     
988                     // needed???
989                     if (this.error_line > 0) {
990                          Gtk.TextIter start;
991                          Gtk.TextIter end;     
992                         this.el.get_bounds (out start, out end);
993                 
994                         this.el.remove_source_marks (start, end, null);
995                     }
996                     if (str.length < 1) {
997                         print("checkSyntax - empty string?\n");
998                         return true;
999                     }
1000                     
1001                     // bit presumptiona
1002                     if (_this.file.xtype == "PlainFile") {
1003                     
1004                         // assume it's gtk...
1005                          var  oldcode =_this.file.toSource();
1006                         _this.file.setSource(str);
1007                             BuilderApplication.showSpinner("appointment soon","document change pending");
1008                         _this.file.getLanguageServer().document_change(_this.file);
1009                 
1010                         _this.file.setSource(oldcode);
1011                         
1012                                  
1013                         return true;
1014                     
1015                     }
1016                    if (_this.file == null) {
1017                        return true;
1018                    }
1019                  
1020                     
1021                 
1022                       
1023                      
1024                     GLib.debug("calling validate");    
1025                     // clear the buttons.
1026                         if (_this.prop.name == "xns" || _this.prop.name == "xtype") {
1027                                 return true ;
1028                         }
1029                         var oldcode  = _this.prop.val;
1030                         
1031                         _this.prop.val = str;
1032                         _this.node.updated_count++;
1033                     _this.file.getLanguageServer().document_change(_this.file);
1034                     _this.node.updated_count++;
1035                     _this.prop.val = oldcode;
1036                     
1037                     
1038                     //print("done mark line\n");
1039                      
1040                     return true; // at present allow saving - even if it's invalid..
1041                 }
1042                 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
1043                          
1044                         this.error_line = validate_res.size;
1045                 
1046                         if (this.error_line < 1) {
1047                                 return true;
1048                         }
1049                         var tlines = this.el.get_line_count ();
1050                         Gtk.TextIter iter;
1051                         var valiter = validate_res.map_iterator();
1052                         while (valiter.next()) {
1053                 
1054                         //        print("get inter\n");
1055                                 var eline = valiter.get_key();
1056                                 if (eline > tlines) {
1057                                         continue;
1058                                 }
1059                                 this.el.get_iter_at_line( out iter, eline);
1060                                 //print("mark line\n");
1061                                 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
1062                         }   
1063                         return false;
1064                 }
1065                 public string toString () {
1066                     
1067                     Gtk.TextIter s;
1068                     Gtk.TextIter e;
1069                     this.el.get_start_iter(out s);
1070                     this.el.get_end_iter(out e);
1071                     var ret = this.el.get_text(s,e,true);
1072                     //print("TO STRING? " + ret);
1073                     return ret;
1074                 }
1075         }
1076
1077         public class Xcls_EventControllerKey10 : Object
1078         {
1079                 public Gtk.EventControllerKey el;
1080                 private Editor  _this;
1081
1082
1083                         // my vars (def)
1084
1085                 // ctor
1086                 public Xcls_EventControllerKey10(Editor _owner )
1087                 {
1088                         _this = _owner;
1089                         this.el = new Gtk.EventControllerKey();
1090
1091                         // my vars (dec)
1092
1093                         // set gobject values
1094
1095                         //listeners
1096                         this.el.key_released.connect( (keyval, keycode, state) => {
1097                         
1098                           
1099                             if (keyval == Gdk.Key.s && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1100                                 GLib.debug("SAVE: ctrl-S  pressed");
1101                                 _this.saveContents();
1102                                 return;
1103                             }
1104                             
1105                             if (keyval == Gdk.Key.g && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1106                                     GLib.debug("SAVE: ctrl-g  pressed");
1107                                         _this.forwardSearch(true);
1108                                     return;
1109                                 }
1110                                 if (keyval == Gdk.Key.f && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1111                                     GLib.debug("SAVE: ctrl-f  pressed");
1112                                         _this.search_entry.el.grab_focus();
1113                                         _this.search_entry.el.select_region(0,-1);
1114                                     return;
1115                                 }
1116                                 if (keyval == Gdk.Key.space && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1117                                         _this.view.el.show_completion();
1118                                 }
1119                                 
1120                                 Gtk.TextIter iter;
1121                                 _this.buffer.el.get_iter_at_offset( out iter, _this.buffer.el.cursor_position);  
1122                                 var line  = iter.get_line();
1123                                 var offset = iter.get_line_offset();
1124                                 GLib.debug("line  %d  off %d", line ,offset);
1125                                 if (_this.prop != null) {
1126                                         line += _this.prop.start_line + 1; // i think..
1127                                         offset += 12; // should probably be 8 without namespaced 
1128                                         GLib.debug("guess line  %d  off %d", line ,offset);
1129                                 } 
1130                             //_this.view.el.show_completion();
1131                            // print(event.key.keyval)
1132                            
1133                            
1134                            
1135                             
1136                             return;
1137                          
1138                          
1139                         });
1140                 }
1141
1142                 // user defined functions
1143         }
1144
1145
1146
1147         public class Xcls_Box11 : Object
1148         {
1149                 public Gtk.Box el;
1150                 private Editor  _this;
1151
1152
1153                         // my vars (def)
1154
1155                 // ctor
1156                 public Xcls_Box11(Editor _owner )
1157                 {
1158                         _this = _owner;
1159                         this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
1160
1161                         // my vars (dec)
1162
1163                         // set gobject values
1164                         this.el.homogeneous = false;
1165                         this.el.vexpand = false;
1166                         new Xcls_search_entry( _this );
1167                         this.el.append( _this.search_entry.el );
1168                         new Xcls_search_results( _this );
1169                         this.el.append( _this.search_results.el );
1170                         new Xcls_nextBtn( _this );
1171                         this.el.append( _this.nextBtn.el );
1172                         new Xcls_backBtn( _this );
1173                         this.el.append( _this.backBtn.el );
1174                         var child_5 = new Xcls_MenuButton17( _this );
1175                         child_5.ref();
1176                         this.el.append( child_5.el );
1177                 }
1178
1179                 // user defined functions
1180         }
1181         public class Xcls_search_entry : Object
1182         {
1183                 public Gtk.SearchEntry el;
1184                 private Editor  _this;
1185
1186
1187                         // my vars (def)
1188                 public Gtk.CssProvider css;
1189
1190                 // ctor
1191                 public Xcls_search_entry(Editor _owner )
1192                 {
1193                         _this = _owner;
1194                         _this.search_entry = this;
1195                         this.el = new Gtk.SearchEntry();
1196
1197                         // my vars (dec)
1198
1199                         // set gobject values
1200                         this.el.name = "editor-search-entry";
1201                         this.el.hexpand = true;
1202                         this.el.placeholder_text = "Press enter to search";
1203                         this.el.search_delay = 3;
1204                         var child_1 = new Xcls_EventControllerKey13( _this );
1205                         child_1.ref();
1206                         this.el.add_controller(  child_1.el );
1207
1208                         //listeners
1209                         this.el.search_changed.connect( ( ) => {
1210                         
1211                         _this.search(_this.search_entry.el.text);
1212                                  _this.search_results.updateResults();
1213                         
1214                                 GLib.Timeout.add_seconds(1,() => {
1215                                          _this.search_results.updateResults();
1216                                          return false;
1217                                  });
1218                         });
1219                 }
1220
1221                 // user defined functions
1222                 public void forwardSearch (bool change_focus) {
1223                 
1224                 
1225                         _this.forwardSearch(change_focus);
1226                 
1227                 /*
1228                 
1229                         switch(_this.windowstate.state) {
1230                                 case WindowState.State.CODEONLY:
1231                                 //case WindowState.State.CODE:
1232                                         // search the code being edited..
1233                                         _this.windowstate.code_editor_tab.forwardSearch(change_focus);
1234                                          
1235                                         break;
1236                                 case WindowState.State.PREVIEW:
1237                                         if (_this.windowstate.file.xtype == "Gtk") {
1238                                                 _this.windowstate.window_gladeview.forwardSearch(change_focus);
1239                                         } else { 
1240                                                  _this.windowstate.window_rooview.forwardSearch(change_focus);
1241                                         }
1242                                 
1243                                         break;
1244                         }
1245                         */
1246                         
1247                 }
1248         }
1249         public class Xcls_EventControllerKey13 : Object
1250         {
1251                 public Gtk.EventControllerKey el;
1252                 private Editor  _this;
1253
1254
1255                         // my vars (def)
1256
1257                 // ctor
1258                 public Xcls_EventControllerKey13(Editor _owner )
1259                 {
1260                         _this = _owner;
1261                         this.el = new Gtk.EventControllerKey();
1262
1263                         // my vars (dec)
1264
1265                         // set gobject values
1266
1267                         //listeners
1268                         this.el.key_pressed.connect( (keyval, keycode, state) => {
1269                         
1270                                 if (keyval == Gdk.Key.g && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1271                                     GLib.debug("SAVE: ctrl-g  pressed");
1272                                         _this.forwardSearch(true);
1273                                     return true;
1274                                 }
1275                             
1276                           
1277                                 if (keyval == Gdk.Key.Return && _this.search_entry.el.text.length > 0) {
1278                                         _this.forwardSearch(true);
1279                                         
1280                                         
1281                                     return true;
1282                         
1283                                 }    
1284                            // print(event.key.keyval)
1285                            
1286                             return false;
1287                         });
1288                 }
1289
1290                 // user defined functions
1291         }
1292
1293
1294         public class Xcls_search_results : Object
1295         {
1296                 public Gtk.Label el;
1297                 private Editor  _this;
1298
1299
1300                         // my vars (def)
1301
1302                 // ctor
1303                 public Xcls_search_results(Editor _owner )
1304                 {
1305                         _this = _owner;
1306                         _this.search_results = this;
1307                         this.el = new Gtk.Label( "No Results" );
1308
1309                         // my vars (dec)
1310
1311                         // set gobject values
1312                         this.el.margin_end = 4;
1313                         this.el.margin_start = 4;
1314                 }
1315
1316                 // user defined functions
1317                 public void updateResults () {
1318                         this.el.visible = true;
1319                         
1320                         var res = _this.searchcontext.get_occurrences_count();
1321                         if (res < 0) {
1322                                 _this.search_results.el.label = "??? Matches";          
1323                                 return;
1324                         }
1325                 
1326                         _this.nextBtn.el.sensitive = false;
1327                         _this.backBtn.el.sensitive = false;     
1328                 
1329                         if (res > 0) {
1330                                 _this.search_results.el.label = "%d Matches".printf(res);
1331                                 _this.nextBtn.el.sensitive = true;
1332                                 _this.backBtn.el.sensitive = true;
1333                                 return;
1334                         } 
1335                         _this.search_results.el.label = "No Matches";
1336                         
1337                 }
1338         }
1339
1340         public class Xcls_nextBtn : Object
1341         {
1342                 public Gtk.Button el;
1343                 private Editor  _this;
1344
1345
1346                         // my vars (def)
1347                 public bool always_show_image;
1348
1349                 // ctor
1350                 public Xcls_nextBtn(Editor _owner )
1351                 {
1352                         _this = _owner;
1353                         _this.nextBtn = this;
1354                         this.el = new Gtk.Button();
1355
1356                         // my vars (dec)
1357                         this.always_show_image = true;
1358
1359                         // set gobject values
1360                         this.el.icon_name = "go-down";
1361                         this.el.sensitive = false;
1362
1363                         //listeners
1364                         this.el.clicked.connect( (event) => {
1365                         
1366                                 _this.forwardSearch(true);
1367                                 
1368                                  
1369                         });
1370                 }
1371
1372                 // user defined functions
1373         }
1374
1375         public class Xcls_backBtn : Object
1376         {
1377                 public Gtk.Button el;
1378                 private Editor  _this;
1379
1380
1381                         // my vars (def)
1382                 public bool always_show_image;
1383
1384                 // ctor
1385                 public Xcls_backBtn(Editor _owner )
1386                 {
1387                         _this = _owner;
1388                         _this.backBtn = this;
1389                         this.el = new Gtk.Button();
1390
1391                         // my vars (dec)
1392                         this.always_show_image = true;
1393
1394                         // set gobject values
1395                         this.el.icon_name = "go-up";
1396                         this.el.sensitive = false;
1397
1398                         //listeners
1399                         this.el.clicked.connect( (event) => {
1400                         
1401                                 _this.backSearch(true);
1402                                  
1403                         });
1404                 }
1405
1406                 // user defined functions
1407         }
1408
1409         public class Xcls_MenuButton17 : Object
1410         {
1411                 public Gtk.MenuButton el;
1412                 private Editor  _this;
1413
1414
1415                         // my vars (def)
1416                 public bool always_show_image;
1417
1418                 // ctor
1419                 public Xcls_MenuButton17(Editor _owner )
1420                 {
1421                         _this = _owner;
1422                         this.el = new Gtk.MenuButton();
1423
1424                         // my vars (dec)
1425                         this.always_show_image = true;
1426
1427                         // set gobject values
1428                         this.el.icon_name = "emblem-system";
1429                         this.el.always_show_arrow = true;
1430                         new Xcls_search_settings( _this );
1431                         this.el.popover = _this.search_settings.el;
1432                 }
1433
1434                 // user defined functions
1435         }
1436         public class Xcls_search_settings : Object
1437         {
1438                 public Gtk.Popover el;
1439                 private Editor  _this;
1440
1441
1442                         // my vars (def)
1443
1444                 // ctor
1445                 public Xcls_search_settings(Editor _owner )
1446                 {
1447                         _this = _owner;
1448                         _this.search_settings = this;
1449                         this.el = new Gtk.Popover();
1450
1451                         // my vars (dec)
1452
1453                         // set gobject values
1454                         var child_1 = new Xcls_Box19( _this );
1455                         this.el.child = child_1.el;
1456                 }
1457
1458                 // user defined functions
1459         }
1460         public class Xcls_Box19 : Object
1461         {
1462                 public Gtk.Box el;
1463                 private Editor  _this;
1464
1465
1466                         // my vars (def)
1467
1468                 // ctor
1469                 public Xcls_Box19(Editor _owner )
1470                 {
1471                         _this = _owner;
1472                         this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
1473
1474                         // my vars (dec)
1475
1476                         // set gobject values
1477                         new Xcls_case_sensitive( _this );
1478                         this.el.append( _this.case_sensitive.el );
1479                         new Xcls_regex( _this );
1480                         this.el.append( _this.regex.el );
1481                         new Xcls_multiline( _this );
1482                         this.el.append( _this.multiline.el );
1483                 }
1484
1485                 // user defined functions
1486         }
1487         public class Xcls_case_sensitive : Object
1488         {
1489                 public Gtk.CheckButton el;
1490                 private Editor  _this;
1491
1492
1493                         // my vars (def)
1494
1495                 // ctor
1496                 public Xcls_case_sensitive(Editor _owner )
1497                 {
1498                         _this = _owner;
1499                         _this.case_sensitive = this;
1500                         this.el = new Gtk.CheckButton();
1501
1502                         // my vars (dec)
1503
1504                         // set gobject values
1505                         this.el.label = "Case Sensitive";
1506
1507                         // init method
1508
1509                         {
1510                                 this.el.show();
1511                         }
1512                 }
1513
1514                 // user defined functions
1515         }
1516
1517         public class Xcls_regex : Object
1518         {
1519                 public Gtk.CheckButton el;
1520                 private Editor  _this;
1521
1522
1523                         // my vars (def)
1524
1525                 // ctor
1526                 public Xcls_regex(Editor _owner )
1527                 {
1528                         _this = _owner;
1529                         _this.regex = this;
1530                         this.el = new Gtk.CheckButton();
1531
1532                         // my vars (dec)
1533
1534                         // set gobject values
1535                         this.el.label = "Regex";
1536
1537                         // init method
1538
1539                         {
1540                                 this.el.show();
1541                         }
1542                 }
1543
1544                 // user defined functions
1545         }
1546
1547         public class Xcls_multiline : Object
1548         {
1549                 public Gtk.CheckButton el;
1550                 private Editor  _this;
1551
1552
1553                         // my vars (def)
1554
1555                 // ctor
1556                 public Xcls_multiline(Editor _owner )
1557                 {
1558                         _this = _owner;
1559                         _this.multiline = this;
1560                         this.el = new Gtk.CheckButton();
1561
1562                         // my vars (dec)
1563
1564                         // set gobject values
1565                         this.el.label = "Multi-line (add \\n)";
1566                 }
1567
1568                 // user defined functions
1569         }
1570
1571
1572
1573
1574
1575 }