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