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