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