6a3b20e9cadc5237a9f3a2715aa9115d8c5302df
[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                             BuilderApplication.showSpinner("appointment soon","document change pending");
865                         _this.file.getLanguageServer().document_change(_this.file);
866                 
867                         _this.file.setSource(oldcode);
868                         
869                                  
870                         return true;
871                     
872                     }
873                    if (_this.file == null) {
874                        return true;
875                    }
876                  
877                     
878                 
879                       
880                      
881                     GLib.debug("calling validate");    
882                     // clear the buttons.
883                         if (_this.prop.name == "xns" || _this.prop.name == "xtype") {
884                                 return true ;
885                         }
886                         var oldcode  = _this.prop.val;
887                         
888                         _this.prop.val = str;
889                         _this.node.updated_count++;
890                     _this.file.getLanguageServer().document_change(_this.file);
891                     _this.node.updated_count++;
892                     _this.prop.val = oldcode;
893                     
894                     
895                     //print("done mark line\n");
896                      
897                     return true; // at present allow saving - even if it's invalid..
898                 }
899                 public bool highlightErrorsJson (string type, Json.Object obj) {
900                         Gtk.TextIter start;
901                         Gtk.TextIter end;     
902                         this.el.get_bounds (out start, out end);
903                 
904                         this.el.remove_source_marks (start, end, type);
905                         GLib.debug("highlight errors");          
906                 
907                          // we should highlight other types of errors..
908                 
909                         if (!obj.has_member(type)) {
910                                 GLib.debug("Return has no errors\n");
911                                 return true;
912                         }
913                 
914                         if (_this.window.windowstate.state != WindowState.State.CODEONLY 
915                                 &&
916                                 _this.window.windowstate.state != WindowState.State.CODE
917                                 ) {
918                                 GLib.debug("windowstate != CODEONLY?");
919                                 
920                                 return true;
921                         } 
922                 
923                         //this.marks = new Gee.HashMap<int,string>();
924                         var err = obj.get_object_member(type);
925                  
926                         if (_this.file == null) {
927                                 GLib.debug("file is null?");
928                                 return true;
929                 
930                         }
931                         var valafn = _this.file.path;
932                 
933                         if (_this.file.xtype != "PlainFile") {
934                 
935                                 valafn = "";
936                                 try {             
937                                         var  regex = new Regex("\\.bjs$");
938                                         // should not happen
939                                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
940                                 } catch (GLib.RegexError e) {
941                                         return true;
942                                 }   
943                 
944                 
945                 
946                         }
947                         if (!err.has_member(valafn)) {
948                                 GLib.debug("File path has no errors");
949                                 return  true;
950                         }
951                 
952                         var lines = err.get_object_member(valafn);
953                         
954                         var offset = 1;
955                         if (obj.has_member("line_offset")) { // ?? why??
956                                 offset = (int)obj.get_int_member("line_offset") + 1;
957                         }
958                 
959                 
960                         var tlines = this.el.get_line_count () +1;
961                         
962                         if (_this.prop != null) {
963                         
964                                 tlines = _this.prop.end_line + 1;
965                                 offset = _this.prop.start_line + 1;
966                         
967                         }
968                         
969                 
970                 
971                         lines.foreach_member((obj, line, node) => {
972                                 
973                              Gtk.TextIter iter;
974                 //        print("get inter\n");
975                             var eline = int.parse(line) - offset;
976                             GLib.debug("GOT ERROR on line %s -- converted to %d  (offset = %d)\n", line,eline, offset);
977                             
978                             
979                             if (eline > tlines || eline < 0) {
980                                 return;
981                             }
982                            
983                             
984                             this.el.get_iter_at_line( out iter, eline);
985                             //print("mark line\n");
986                             var msg  = "";
987                             var ar = lines.get_array_member(line);
988                             for (var i = 0 ; i < ar.get_length(); i++) {
989                                 if (ar.get_string_element(i) == "Success") {
990                                         continue;
991                                 }
992                                         msg += (msg.length > 0) ? "\n" : "";
993                                         msg += ar.get_string_element(i);
994                                 }
995                                 if (msg == "") {
996                                         return;
997                                 }
998                                 msg = "Line: %d".printf(eline+1) +  " " + msg;
999                             this.el.create_source_mark(msg, type, iter);
1000                             GLib.debug("set line %d to %m", eline, msg);
1001                            // this.marks.set(eline, msg);
1002                         } );
1003                         return false;
1004                 
1005                 
1006                 
1007                 
1008                 
1009                         }
1010                 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
1011                          
1012                         this.error_line = validate_res.size;
1013                 
1014                         if (this.error_line < 1) {
1015                                 return true;
1016                         }
1017                         var tlines = this.el.get_line_count ();
1018                         Gtk.TextIter iter;
1019                         var valiter = validate_res.map_iterator();
1020                         while (valiter.next()) {
1021                 
1022                         //        print("get inter\n");
1023                                 var eline = valiter.get_key();
1024                                 if (eline > tlines) {
1025                                         continue;
1026                                 }
1027                                 this.el.get_iter_at_line( out iter, eline);
1028                                 //print("mark line\n");
1029                                 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
1030                         }   
1031                         return false;
1032                 }
1033                 public string toString () {
1034                     
1035                     Gtk.TextIter s;
1036                     Gtk.TextIter e;
1037                     this.el.get_start_iter(out s);
1038                     this.el.get_end_iter(out e);
1039                     var ret = this.el.get_text(s,e,true);
1040                     //print("TO STRING? " + ret);
1041                     return ret;
1042                 }
1043         }
1044
1045         public class Xcls_EventControllerKey11 : Object
1046         {
1047                 public Gtk.EventControllerKey el;
1048                 private Editor  _this;
1049
1050
1051                         // my vars (def)
1052
1053                 // ctor
1054                 public Xcls_EventControllerKey11(Editor _owner )
1055                 {
1056                         _this = _owner;
1057                         this.el = new Gtk.EventControllerKey();
1058
1059                         // my vars (dec)
1060
1061                         // set gobject values
1062
1063                         //listeners
1064                         this.el.key_released.connect( (keyval, keycode, state) => {
1065                         
1066                           
1067                             if (keyval == Gdk.Key.s && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1068                                 GLib.debug("SAVE: ctrl-S  pressed");
1069                                 _this.saveContents();
1070                                 return;
1071                             }
1072                             
1073                             if (keyval == Gdk.Key.g && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1074                                     GLib.debug("SAVE: ctrl-g  pressed");
1075                                         _this.forwardSearch(true);
1076                                     return;
1077                                 }
1078                                 if (keyval == Gdk.Key.f && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1079                                     GLib.debug("SAVE: ctrl-f  pressed");
1080                                         _this.search_entry.el.grab_focus();
1081                                         _this.search_entry.el.select_region(0,-1);
1082                                     return;
1083                                 }
1084                                 if (keyval == Gdk.Key.space && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1085                                         _this.view.el.show_completion();
1086                                 }
1087                                 
1088                                 Gtk.TextIter iter;
1089                                 _this.buffer.el.get_iter_at_offset( out iter, _this.buffer.el.cursor_position);  
1090                                 var line  = iter.get_line();
1091                                 var offset = iter.get_line_offset();
1092                                 GLib.debug("line  %d  off %d", line ,offset);
1093                                 if (_this.prop != null) {
1094                                         line += _this.prop.start_line + 1; // i think..
1095                                         offset += 12; // should probably be 8 without namespaced 
1096                                         GLib.debug("guess line  %d  off %d", line ,offset);
1097                                 } 
1098                             //_this.view.el.show_completion();
1099                            // print(event.key.keyval)
1100                            
1101                            
1102                            
1103                             
1104                             return;
1105                          
1106                          
1107                         });
1108                 }
1109
1110                 // user defined functions
1111         }
1112
1113
1114
1115         public class Xcls_Box12 : Object
1116         {
1117                 public Gtk.Box el;
1118                 private Editor  _this;
1119
1120
1121                         // my vars (def)
1122
1123                 // ctor
1124                 public Xcls_Box12(Editor _owner )
1125                 {
1126                         _this = _owner;
1127                         this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
1128
1129                         // my vars (dec)
1130
1131                         // set gobject values
1132                         this.el.homogeneous = false;
1133                         this.el.vexpand = false;
1134                         new Xcls_search_entry( _this );
1135                         this.el.append( _this.search_entry.el );
1136                         new Xcls_search_results( _this );
1137                         this.el.append( _this.search_results.el );
1138                         new Xcls_nextBtn( _this );
1139                         this.el.append( _this.nextBtn.el );
1140                         new Xcls_backBtn( _this );
1141                         this.el.append( _this.backBtn.el );
1142                         var child_5 = new Xcls_MenuButton18( _this );
1143                         child_5.ref();
1144                         this.el.append( child_5.el );
1145                 }
1146
1147                 // user defined functions
1148         }
1149         public class Xcls_search_entry : Object
1150         {
1151                 public Gtk.SearchEntry el;
1152                 private Editor  _this;
1153
1154
1155                         // my vars (def)
1156                 public Gtk.CssProvider css;
1157
1158                 // ctor
1159                 public Xcls_search_entry(Editor _owner )
1160                 {
1161                         _this = _owner;
1162                         _this.search_entry = this;
1163                         this.el = new Gtk.SearchEntry();
1164
1165                         // my vars (dec)
1166
1167                         // set gobject values
1168                         this.el.name = "editor-search-entry";
1169                         this.el.hexpand = true;
1170                         this.el.placeholder_text = "Press enter to search";
1171                         this.el.search_delay = 3;
1172                         var child_1 = new Xcls_EventControllerKey14( _this );
1173                         child_1.ref();
1174                         this.el.add_controller(  child_1.el );
1175
1176                         //listeners
1177                         this.el.search_changed.connect( ( ) => {
1178                         
1179                         _this.search(_this.search_entry.el.text);
1180                                  _this.search_results.updateResults();
1181                         
1182                                 GLib.Timeout.add_seconds(1,() => {
1183                                          _this.search_results.updateResults();
1184                                          return false;
1185                                  });
1186                         });
1187                 }
1188
1189                 // user defined functions
1190                 public void forwardSearch (bool change_focus) {
1191                 
1192                 
1193                         _this.forwardSearch(change_focus);
1194                 
1195                 /*
1196                 
1197                         switch(_this.windowstate.state) {
1198                                 case WindowState.State.CODEONLY:
1199                                 //case WindowState.State.CODE:
1200                                         // search the code being edited..
1201                                         _this.windowstate.code_editor_tab.forwardSearch(change_focus);
1202                                          
1203                                         break;
1204                                 case WindowState.State.PREVIEW:
1205                                         if (_this.windowstate.file.xtype == "Gtk") {
1206                                                 _this.windowstate.window_gladeview.forwardSearch(change_focus);
1207                                         } else { 
1208                                                  _this.windowstate.window_rooview.forwardSearch(change_focus);
1209                                         }
1210                                 
1211                                         break;
1212                         }
1213                         */
1214                         
1215                 }
1216         }
1217         public class Xcls_EventControllerKey14 : Object
1218         {
1219                 public Gtk.EventControllerKey el;
1220                 private Editor  _this;
1221
1222
1223                         // my vars (def)
1224
1225                 // ctor
1226                 public Xcls_EventControllerKey14(Editor _owner )
1227                 {
1228                         _this = _owner;
1229                         this.el = new Gtk.EventControllerKey();
1230
1231                         // my vars (dec)
1232
1233                         // set gobject values
1234
1235                         //listeners
1236                         this.el.key_pressed.connect( (keyval, keycode, state) => {
1237                         
1238                                 if (keyval == Gdk.Key.g && (state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
1239                                     GLib.debug("SAVE: ctrl-g  pressed");
1240                                         _this.forwardSearch(true);
1241                                     return true;
1242                                 }
1243                             
1244                           
1245                                 if (keyval == Gdk.Key.Return && _this.search_entry.el.text.length > 0) {
1246                                         _this.forwardSearch(true);
1247                                         
1248                                         
1249                                     return true;
1250                         
1251                                 }    
1252                            // print(event.key.keyval)
1253                            
1254                             return false;
1255                         });
1256                 }
1257
1258                 // user defined functions
1259         }
1260
1261
1262         public class Xcls_search_results : Object
1263         {
1264                 public Gtk.Label el;
1265                 private Editor  _this;
1266
1267
1268                         // my vars (def)
1269
1270                 // ctor
1271                 public Xcls_search_results(Editor _owner )
1272                 {
1273                         _this = _owner;
1274                         _this.search_results = this;
1275                         this.el = new Gtk.Label( "No Results" );
1276
1277                         // my vars (dec)
1278
1279                         // set gobject values
1280                         this.el.margin_end = 4;
1281                         this.el.margin_start = 4;
1282                 }
1283
1284                 // user defined functions
1285                 public void updateResults () {
1286                         this.el.visible = true;
1287                         
1288                         var res = _this.searchcontext.get_occurrences_count();
1289                         if (res < 0) {
1290                                 _this.search_results.el.label = "??? Matches";          
1291                                 return;
1292                         }
1293                 
1294                         _this.nextBtn.el.sensitive = false;
1295                         _this.backBtn.el.sensitive = false;     
1296                 
1297                         if (res > 0) {
1298                                 _this.search_results.el.label = "%d Matches".printf(res);
1299                                 _this.nextBtn.el.sensitive = true;
1300                                 _this.backBtn.el.sensitive = true;
1301                                 return;
1302                         } 
1303                         _this.search_results.el.label = "No Matches";
1304                         
1305                 }
1306         }
1307
1308         public class Xcls_nextBtn : Object
1309         {
1310                 public Gtk.Button el;
1311                 private Editor  _this;
1312
1313
1314                         // my vars (def)
1315                 public bool always_show_image;
1316
1317                 // ctor
1318                 public Xcls_nextBtn(Editor _owner )
1319                 {
1320                         _this = _owner;
1321                         _this.nextBtn = this;
1322                         this.el = new Gtk.Button();
1323
1324                         // my vars (dec)
1325                         this.always_show_image = true;
1326
1327                         // set gobject values
1328                         this.el.icon_name = "go-down";
1329                         this.el.sensitive = false;
1330
1331                         //listeners
1332                         this.el.clicked.connect( (event) => {
1333                         
1334                                 _this.forwardSearch(true);
1335                                 
1336                                  
1337                         });
1338                 }
1339
1340                 // user defined functions
1341         }
1342
1343         public class Xcls_backBtn : Object
1344         {
1345                 public Gtk.Button el;
1346                 private Editor  _this;
1347
1348
1349                         // my vars (def)
1350                 public bool always_show_image;
1351
1352                 // ctor
1353                 public Xcls_backBtn(Editor _owner )
1354                 {
1355                         _this = _owner;
1356                         _this.backBtn = this;
1357                         this.el = new Gtk.Button();
1358
1359                         // my vars (dec)
1360                         this.always_show_image = true;
1361
1362                         // set gobject values
1363                         this.el.icon_name = "go-up";
1364                         this.el.sensitive = false;
1365
1366                         //listeners
1367                         this.el.clicked.connect( (event) => {
1368                         
1369                                 _this.backSearch(true);
1370                                  
1371                         });
1372                 }
1373
1374                 // user defined functions
1375         }
1376
1377         public class Xcls_MenuButton18 : Object
1378         {
1379                 public Gtk.MenuButton el;
1380                 private Editor  _this;
1381
1382
1383                         // my vars (def)
1384                 public bool always_show_image;
1385
1386                 // ctor
1387                 public Xcls_MenuButton18(Editor _owner )
1388                 {
1389                         _this = _owner;
1390                         this.el = new Gtk.MenuButton();
1391
1392                         // my vars (dec)
1393                         this.always_show_image = true;
1394
1395                         // set gobject values
1396                         this.el.icon_name = "emblem-system";
1397                         this.el.always_show_arrow = true;
1398                         new Xcls_search_settings( _this );
1399                         this.el.popover = _this.search_settings.el;
1400                 }
1401
1402                 // user defined functions
1403         }
1404         public class Xcls_search_settings : Object
1405         {
1406                 public Gtk.Popover el;
1407                 private Editor  _this;
1408
1409
1410                         // my vars (def)
1411
1412                 // ctor
1413                 public Xcls_search_settings(Editor _owner )
1414                 {
1415                         _this = _owner;
1416                         _this.search_settings = this;
1417                         this.el = new Gtk.Popover();
1418
1419                         // my vars (dec)
1420
1421                         // set gobject values
1422                         var child_1 = new Xcls_Box20( _this );
1423                         this.el.child = child_1.el;
1424                 }
1425
1426                 // user defined functions
1427         }
1428         public class Xcls_Box20 : Object
1429         {
1430                 public Gtk.Box el;
1431                 private Editor  _this;
1432
1433
1434                         // my vars (def)
1435
1436                 // ctor
1437                 public Xcls_Box20(Editor _owner )
1438                 {
1439                         _this = _owner;
1440                         this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
1441
1442                         // my vars (dec)
1443
1444                         // set gobject values
1445                         new Xcls_case_sensitive( _this );
1446                         this.el.append( _this.case_sensitive.el );
1447                         new Xcls_regex( _this );
1448                         this.el.append( _this.regex.el );
1449                         new Xcls_multiline( _this );
1450                         this.el.append( _this.multiline.el );
1451                 }
1452
1453                 // user defined functions
1454         }
1455         public class Xcls_case_sensitive : Object
1456         {
1457                 public Gtk.CheckButton el;
1458                 private Editor  _this;
1459
1460
1461                         // my vars (def)
1462
1463                 // ctor
1464                 public Xcls_case_sensitive(Editor _owner )
1465                 {
1466                         _this = _owner;
1467                         _this.case_sensitive = this;
1468                         this.el = new Gtk.CheckButton();
1469
1470                         // my vars (dec)
1471
1472                         // set gobject values
1473                         this.el.label = "Case Sensitive";
1474
1475                         // init method
1476
1477                         {
1478                                 this.el.show();
1479                         }
1480                 }
1481
1482                 // user defined functions
1483         }
1484
1485         public class Xcls_regex : Object
1486         {
1487                 public Gtk.CheckButton el;
1488                 private Editor  _this;
1489
1490
1491                         // my vars (def)
1492
1493                 // ctor
1494                 public Xcls_regex(Editor _owner )
1495                 {
1496                         _this = _owner;
1497                         _this.regex = this;
1498                         this.el = new Gtk.CheckButton();
1499
1500                         // my vars (dec)
1501
1502                         // set gobject values
1503                         this.el.label = "Regex";
1504
1505                         // init method
1506
1507                         {
1508                                 this.el.show();
1509                         }
1510                 }
1511
1512                 // user defined functions
1513         }
1514
1515         public class Xcls_multiline : Object
1516         {
1517                 public Gtk.CheckButton el;
1518                 private Editor  _this;
1519
1520
1521                         // my vars (def)
1522
1523                 // ctor
1524                 public Xcls_multiline(Editor _owner )
1525                 {
1526                         _this = _owner;
1527                         _this.multiline = this;
1528                         this.el = new Gtk.CheckButton();
1529
1530                         // my vars (dec)
1531
1532                         // set gobject values
1533                         this.el.label = "Multi-line (add \\n)";
1534                 }
1535
1536                 // user defined functions
1537         }
1538
1539
1540
1541
1542
1543 }