Fix #7247 - icons for object add
[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_key_edit key_edit;
17     public Xcls_RightEditor RightEditor;
18     public Xcls_view view;
19     public Xcls_buffer buffer;
20
21         // my vars (def)
22     public Xcls_MainWindow window;
23     public string activeEditor;
24     public int pos_root_x;
25     public JsRender.JsRender? file;
26     public int pos_root_y;
27     public string ptype;
28     public int last_search_end;
29     public string key;
30     public Gtk.SourceSearchContext searchcontext;
31     public bool pos;
32     public bool dirty;
33     public signal void save ();
34     public JsRender.Node node;
35
36     // ctor
37     public Editor()
38     {
39         _this = this;
40         this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
41
42         // my vars (dec)
43         this.window = null;
44         this.activeEditor = "";
45         this.file = null;
46         this.ptype = "";
47         this.last_search_end = 0;
48         this.key = "";
49         this.searchcontext = null;
50         this.pos = false;
51         this.dirty = false;
52         this.node = null;
53
54         // set gobject values
55         this.el.homogeneous = false;
56         this.el.hexpand = true;
57         this.el.vexpand = true;
58         var child_0 = new Xcls_Box2( _this );
59         child_0.ref();
60         this.el.pack_start (  child_0.el , false,true );
61         var child_1 = new Xcls_RightEditor( _this );
62         child_1.ref();
63         this.el.pack_end (  child_1.el , true,true );
64     }
65
66     // user defined functions
67     public   bool saveContents ()  {
68         
69         
70         if (_this.file == null) {
71             return true;
72         }
73         
74         
75        
76        
77          
78          var str = _this.buffer.toString();
79          
80          _this.buffer.checkSyntax();
81          
82          
83          
84          // LeftPanel.model.changed(  str , false);
85          _this.dirty = false;
86          _this.save_button.el.sensitive = false;
87          
88         // find the text for the node..
89         if (_this.file.xtype != "PlainFile") {
90             if (ptype == "listener") {
91                 this.node.listeners.set(key,str);
92             
93             } else {
94                  this.node.props.set(key,str);
95             }
96         } else {
97             _this.file.setSource(  str );
98          }
99         
100         // call the signal..
101         this.save();
102         
103         return true;
104     
105     }
106     public void scroll_to_line (int line) {
107     
108         GLib.Timeout.add(500, () => {
109        
110                 var buf = this.view.el.get_buffer();
111     
112                 var sbuf = (Gtk.SourceBuffer) buf;
113     
114     
115                 Gtk.TextIter iter;   
116                 sbuf.get_iter_at_line(out iter,  line);
117                 this.view.el.scroll_to_iter(iter,  0.1f, true, 0.0f, 0.5f);
118                 return false;
119         });   
120     }
121     public int search (string txt) {
122     
123         var s = new Gtk.SourceSearchSettings();
124         
125         this.searchcontext = new Gtk.SourceSearchContext(this.buffer.el,s);
126         this.searchcontext .set_highlight(true);
127         s.set_search_text(txt);
128         Gtk.TextIter beg, st,en;
129          
130         this.buffer.el.get_start_iter(out beg);
131         this.searchcontext.forward(beg, out st, out en);
132         this.last_search_end = 0;
133         
134         return this.searchcontext.get_occurrences_count();
135     
136      
137        
138     
139     }
140     public   void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
141     {
142         this.reset();
143         this.file = file;    
144         
145         if (file.xtype != "PlainFile") {
146         
147             this.ptype = ptype;
148             this.key  = key;
149             this.node = node;
150              string val = "";
151             // find the text for the node..
152             if (ptype == "listener") {
153                 val = node.listeners.get(key);
154             
155             } else {
156                 val = node.props.get(key);
157             }
158             this.view.load(val);
159             this.key_edit.el.show();
160             this.key_edit.el.text = key;  
161         
162         } else {
163             this.view.load(        file.toSource() );
164             this.key_edit.el.hide();
165         }
166      
167     }
168     public void forwardSearch (bool change_focus) {
169     
170         if (this.searchcontext == null) {
171                 return;
172         }
173         
174         Gtk.TextIter beg, st,en;
175          
176         this.buffer.el.get_iter_at_offset(out beg, this.last_search_end);
177         if (!this.searchcontext.forward(beg, out st, out en)) {
178         
179                 this.last_search_end = 0;
180         } else {
181                 this.last_search_end = en.get_offset();
182                 if (change_focus) {
183                         this.view.el.grab_focus();
184                 }
185                 this.buffer.el.place_cursor(st);
186                 this.view.el.scroll_to_iter(st,  0.1f, true, 0.0f, 0.5f);
187         }
188      
189     }
190     public void reset () {
191          this.file = null;    
192         this.ptype = "";
193         this.key  = "";
194         this.node = null;
195         this.searchcontext = null;
196       
197     }
198     public class Xcls_Box2 : Object
199     {
200         public Gtk.Box el;
201         private Editor  _this;
202
203
204             // my vars (def)
205
206         // ctor
207         public Xcls_Box2(Editor _owner )
208         {
209             _this = _owner;
210             this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
211
212             // my vars (dec)
213
214             // set gobject values
215             this.el.homogeneous = false;
216             var child_0 = new Xcls_save_button( _this );
217             child_0.ref();
218             this.el.pack_start (  child_0.el , false,false );
219             var child_1 = new Xcls_key_edit( _this );
220             child_1.ref();
221             this.el.pack_start (  child_1.el , true,true );
222             var child_2 = new Xcls_HScale5( _this );
223             child_2.ref();
224             this.el.pack_end (  child_2.el , true,true );
225         }
226
227         // user defined functions
228     }
229     public class Xcls_save_button : Object
230     {
231         public Gtk.Button el;
232         private Editor  _this;
233
234
235             // my vars (def)
236
237         // ctor
238         public Xcls_save_button(Editor _owner )
239         {
240             _this = _owner;
241             _this.save_button = this;
242             this.el = new Gtk.Button();
243
244             // my vars (dec)
245
246             // set gobject values
247             this.el.label = "Save";
248
249             //listeners
250             this.el.clicked.connect( () => { 
251                 _this.saveContents();
252             });
253         }
254
255         // user defined functions
256     }
257
258     public class Xcls_key_edit : Object
259     {
260         public Gtk.Entry el;
261         private Editor  _this;
262
263
264             // my vars (def)
265
266         // ctor
267         public Xcls_key_edit(Editor _owner )
268         {
269             _this = _owner;
270             _this.key_edit = this;
271             this.el = new Gtk.Entry();
272
273             // my vars (dec)
274
275             // set gobject values
276             this.el.width_request = 100;
277             this.el.editable = false;
278         }
279
280         // user defined functions
281     }
282
283     public class Xcls_HScale5 : Object
284     {
285         public Gtk.HScale el;
286         private Editor  _this;
287
288
289             // my vars (def)
290
291         // ctor
292         public Xcls_HScale5(Editor _owner )
293         {
294             _this = _owner;
295             this.el = new Gtk.HScale.with_range (6, 30, 1);
296
297             // my vars (dec)
298
299             // set gobject values
300             this.el.has_origin = true;
301             this.el.draw_value = true;
302             this.el.digits = 0;
303             this.el.sensitive = true;
304
305             // init method
306
307             {
308                 this.el.set_range(6,30);
309                 this.el.set_value(8);
310             }
311
312             //listeners
313             this.el.change_value.connect( (st, val ) => {
314                  
315                   var description =   Pango.FontDescription.from_string("monospace");
316                   print("resize to %d", (int)val*1000);
317                   description.set_size((int)val*1000);
318                   _this.view.el.override_font(description);
319                   return false;
320             });
321         }
322
323         // user defined functions
324     }
325
326
327     public class Xcls_RightEditor : Object
328     {
329         public Gtk.ScrolledWindow el;
330         private Editor  _this;
331
332
333             // my vars (def)
334
335         // ctor
336         public Xcls_RightEditor(Editor _owner )
337         {
338             _this = _owner;
339             _this.RightEditor = this;
340             this.el = new Gtk.ScrolledWindow( null, null );
341
342             // my vars (dec)
343
344             // set gobject values
345             var child_0 = new Xcls_view( _this );
346             child_0.ref();
347             this.el.add (  child_0.el  );
348
349             // init method
350
351             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
352         }
353
354         // user defined functions
355     }
356     public class Xcls_view : Object
357     {
358         public Gtk.SourceView el;
359         private Editor  _this;
360
361
362             // my vars (def)
363
364         // ctor
365         public Xcls_view(Editor _owner )
366         {
367             _this = _owner;
368             _this.view = this;
369             this.el = new Gtk.SourceView();
370
371             // my vars (dec)
372
373             // set gobject values
374             this.el.auto_indent = true;
375             this.el.indent_width = 4;
376             this.el.show_line_marks = true;
377             this.el.insert_spaces_instead_of_tabs = true;
378             this.el.show_line_numbers = true;
379             this.el.draw_spaces = Gtk.SourceDrawSpacesFlags.LEADING + Gtk.SourceDrawSpacesFlags.TRAILING + Gtk.SourceDrawSpacesFlags.TAB + Gtk.SourceDrawSpacesFlags.SPACE;
380             this.el.tab_width = 4;
381             this.el.highlight_current_line = true;
382             var child_0 = new Xcls_buffer( _this );
383             child_0.ref();
384             this.el.set_buffer (  child_0.el  );
385
386             // init method
387
388             var description =   Pango.FontDescription.from_string("monospace");
389                         description.set_size(8000);
390             
391                          this.el.override_font(description);
392             
393                 try {        
394                         this.el.completion.add_provider(new Palete.CompletionProvider(_this));
395                 } catch (GLib.Error  e) {}
396                 
397                 this.el.completion.unblock_interactive();
398                 this.el.completion.select_on_show                       = true; // select
399                 this.el.completion.show_headers                 = false;
400                 this.el.completion.remember_info_visibility             = true;
401                 
402               
403                 var attrs = new Gtk.SourceMarkAttributes();
404                 var  pink =   Gdk.RGBA();
405                 pink.parse ( "pink");
406                 attrs.set_background ( pink);
407                 attrs.set_icon_name ( "process-stop");    
408                 attrs.query_tooltip_text.connect(( mark) => {
409                     //print("tooltip query? %s\n", mark.name);
410                     return mark.name;
411                 });
412                 
413                 this.el.set_mark_attributes ("ERR", attrs, 1);
414                 
415                  var wattrs = new Gtk.SourceMarkAttributes();
416                 var  blue =   Gdk.RGBA();
417                 blue.parse ( "#ABF4EB");
418                 wattrs.set_background ( blue);
419                 wattrs.set_icon_name ( "process-stop");    
420                 wattrs.query_tooltip_text.connect(( mark) => {
421                     //print("tooltip query? %s\n", mark.name);
422                     return mark.name;
423                 });
424                 
425                 this.el.set_mark_attributes ("WARN", wattrs, 1);
426                 
427              
428                 
429                  var dattrs = new Gtk.SourceMarkAttributes();
430                 var  purple =   Gdk.RGBA();
431                 purple.parse ( "#EEA9FF");
432                 dattrs.set_background ( purple);
433                 dattrs.set_icon_name ( "process-stop");    
434                 dattrs.query_tooltip_text.connect(( mark) => {
435                     //print("tooltip query? %s\n", mark.name);
436                     return mark.name;
437                 });
438                 
439                 this.el.set_mark_attributes ("DEPR", dattrs, 1);
440
441             //listeners
442             this.el.key_release_event.connect( (event) => {
443                 
444                 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
445                     print("SAVE: ctrl-S  pressed");
446                     _this.saveContents();
447                     return false;
448                 }
449                // print(event.key.keyval)
450                 
451                 return false;
452             
453             });
454         }
455
456         // user defined functions
457         public   void load (string str) {
458         
459         // show the help page for the active node..
460            //this.get('/Help').show();
461         
462         
463           // this.get('/BottomPane').el.set_current_page(0);
464             var buf = (Gtk.SourceBuffer)this.el.get_buffer();
465             buf.set_text(str, str.length);
466             buf.set_undo_manager(null);
467             
468             var lm = Gtk.SourceLanguageManager.get_default();
469             var lang = "vala";
470             if (_this.file != null) {
471                  lang = _this.file.language;
472             }
473             print("lang=%s, content_type = %s\n", lang, _this.file.content_type);
474             var lg = _this.file.content_type.length > 0  ?
475                     lm.guess_language(_this.file.path, _this.file.content_type) :
476                     lm.get_language(lang);
477              
478            
479             ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lg); 
480         
481             this.el.insert_spaces_instead_of_tabs = true;
482             if (lg != null) {
483                         print("sourcelanguage  = %s\n", lg.name);
484                         if (lg.name == "Vala") {
485                             this.el.insert_spaces_instead_of_tabs = false;
486                         }
487              }
488             _this.dirty = false;
489             this.el.grab_focus();
490             _this.save_button.el.sensitive = false;
491         }
492     }
493     public class Xcls_buffer : Object
494     {
495         public Gtk.SourceBuffer el;
496         private Editor  _this;
497
498
499             // my vars (def)
500         public bool check_queued;
501         public int error_line;
502         public bool check_running;
503
504         // ctor
505         public Xcls_buffer(Editor _owner )
506         {
507             _this = _owner;
508             _this.buffer = this;
509             this.el = new Gtk.SourceBuffer( null );
510
511             // my vars (dec)
512             this.check_queued = false;
513             this.error_line = -1;
514             this.check_running = false;
515
516             // set gobject values
517
518             //listeners
519             this.el.changed.connect( () => {
520                 // check syntax??
521                 // ??needed..??
522                 _this.save_button.el.sensitive = true;
523                 print("EDITOR CHANGED");
524                 this.checkSyntax();
525                
526                 _this.dirty = true;
527             
528                 // this.get('/LeftPanel.model').changed(  str , false);
529                 return ;
530             });
531         }
532
533         // user defined functions
534         public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
535                  
536                 this.error_line = validate_res.size;
537         
538                 if (this.error_line < 1) {
539                       return true;
540                 }
541                 var tlines = this.el.get_line_count ();
542                 Gtk.TextIter iter;
543                 var valiter = validate_res.map_iterator();
544                 while (valiter.next()) {
545                 
546             //        print("get inter\n");
547                     var eline = valiter.get_key();
548                     if (eline > tlines) {
549                         continue;
550                     }
551                     this.el.get_iter_at_line( out iter, eline);
552                     //print("mark line\n");
553                     this.el.create_source_mark(valiter.get_value(), "ERR", iter);
554                 }   
555                 return false;
556             }
557         public   bool checkSyntax () {
558          
559             if (this.check_running) {
560                 print("Check is running\n");
561                 if (this.check_queued) { 
562                     print("Check is already queued");
563                     return true;
564                 }
565                 this.check_queued = true;
566                 print("Adding queued Check ");
567                 GLib.Timeout.add_seconds(1, () => {
568                     this.check_queued = false;
569                     
570                     this.checkSyntax();
571                     return false;
572                 });
573             
574         
575                 return true;
576             }
577             var str = this.toString();
578             
579             // needed???
580             if (this.error_line > 0) {
581                  Gtk.TextIter start;
582                  Gtk.TextIter end;     
583                 this.el.get_bounds (out start, out end);
584         
585                 this.el.remove_source_marks (start, end, null);
586             }
587             if (str.length < 1) {
588                 print("checkSyntax - empty string?\n");
589                 return true;
590             }
591             
592             if (_this.file.xtype == "PlainFile") {
593             
594                 // assume it's gtk...
595                    this.check_running = true;
596          
597                  if (!_this.window.windowstate.valasource.checkPlainFileSpawn(
598                    _this.file,
599                     str
600                  )) {
601                     this.check_running = false;
602                 }
603                 
604                 return true;
605             
606             }
607            if (_this.file == null) {
608                return true;
609            }
610             var p = _this.file.project.palete;
611             
612         
613              
614             this.check_running = true;
615             
616             
617             if (_this.file.language == "js") {
618                 this.check_running = false;
619                 print("calling validate javascript\n"); 
620                 Gee.HashMap<int,string> errors;
621                 p.javascriptHasErrors(
622                         _this.window.windowstate,
623                     str, 
624                      _this.key, 
625                     _this.ptype,
626                     _this.file,
627          
628                     out errors
629                 );
630                 return this.highlightErrors(errors);    
631                 
632             }
633                 
634                 
635             print("calling validate vala\n");    
636             // clear the buttons.
637          
638             
639            if (! _this.window.windowstate.valasource.checkFileWithNodePropChange(
640                 _this.file,
641                 _this.node,
642                  _this.key,        
643                  _this.ptype,
644                     str
645                 )) {
646                 this.check_running = false;
647             } 
648              
649             
650             
651             //print("done mark line\n");
652              
653             return true; // at present allow saving - even if it's invalid..
654         }
655         public   string toString () {
656             
657             Gtk.TextIter s;
658             Gtk.TextIter e;
659             this.el.get_start_iter(out s);
660             this.el.get_end_iter(out e);
661             var ret = this.el.get_text(s,e,true);
662             //print("TO STRING? " + ret);
663             return ret;
664         }
665         public bool highlightErrorsJson (string type, Json.Object obj) {
666               Gtk.TextIter start;
667              Gtk.TextIter end;     
668                 this.el.get_bounds (out start, out end);
669                 
670                 this.el.remove_source_marks (start, end, type);
671                          
672              
673              // we should highlight other types of errors..
674             
675             if (!obj.has_member(type)) {
676                 print("Return has no errors\n");
677                 return true;
678             }
679             
680             if (_this.window.windowstate.state != WindowState.State.CODEONLY 
681               
682                 ) {
683                 return true;
684             } 
685             
686             
687             var err = obj.get_object_member(type);
688             
689             
690             if (_this.file == null) {
691                 return true;
692             
693             }
694             var valafn = _this.file.path;
695          
696             if (_this.file.xtype != "PlainFile") {
697         
698         
699                 
700                 
701                  valafn = "";
702                   try {             
703                        var  regex = new Regex("\\.bjs$");
704                        // should not happen
705                       
706                      
707                         valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
708                      } catch (GLib.RegexError e) {
709                         return true;
710                     }   
711         
712         
713         
714               }
715                if (!err.has_member(valafn)) {
716                     print("File path has no errors\n");
717                     return  true;
718                 }
719         
720                 var lines = err.get_object_member(valafn);
721                 
722                 var offset = 1;
723                 if (obj.has_member("line_offset")) {
724                     offset = (int)obj.get_int_member("line_offset") + 1;
725                 }
726             
727         
728              
729             
730             var tlines = this.el.get_line_count () +1;
731             
732             lines.foreach_member((obj, line, node) => {
733                 
734                      Gtk.TextIter iter;
735             //        print("get inter\n");
736                     var eline = int.parse(line) - offset;
737                     print("GOT ERROR on line %s -- converted to %d\n", line,eline);
738                     
739                     
740                     if (eline > tlines || eline < 0) {
741                         return;
742                     }
743                     this.el.get_iter_at_line( out iter, eline);
744                     //print("mark line\n");
745                     var msg  = "Line: %d".printf(eline+1);
746                     var ar = lines.get_array_member(line);
747                     for (var i = 0 ; i < ar.get_length(); i++) {
748                             msg += (msg.length > 0) ? "\n" : "";
749                             msg += ar.get_string_element(i);
750                     }
751                     
752                     
753                     this.el.create_source_mark(msg, type, iter);
754                 } );
755                 return false;
756             
757         
758         
759         
760         
761         }
762     }
763
764
765
766 }