sync
[app.Builder.js] / src / Builder4 / Editor.vala
1 static Editor  _Editor;
2
3 public class Editor : Object 
4 {
5     public Gtk.VBox 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 string activeEditor;
23     public int pos_root_x;
24     public int pos_root_y;
25     public string ptype;
26     public string key;
27     public JsRender.JsRender file;
28     public bool pos;
29     public bool dirty;
30     public signal void save ();
31     public JsRender.Node node;
32
33     // ctor 
34     public Editor()
35     {
36         _this = this;
37         this.el = new Gtk.VBox( false, 0 );
38
39         // my vars (dec)
40         this.activeEditor = "";
41         this.ptype = "";
42         this.key = "";
43         this.file = null;
44         this.pos = false;
45         this.dirty = false;
46         this.node = null;
47
48         // set gobject values
49         var child_0 = new Xcls_HBox2( _this );
50         child_0.ref();
51         this.el.pack_start (  child_0.el , false,true );
52         var child_1 = new Xcls_RightEditor( _this );
53         child_1.ref();
54         this.el.add (  child_1.el  );
55     }
56
57     // user defined functions 
58     public   bool saveContents ()  {
59         
60         
61         
62         
63         
64         
65         
66        
67          
68          var str = _this.buffer.toString();
69          
70           
71          
72          
73          if (!_this.buffer.checkSyntax()) {
74              print("check syntax failed");
75              //this.get('/StandardErrorDialog').show("Fix errors in code and save.."); 
76              //return false;
77          }
78          
79          // LeftPanel.model.changed(  str , false);
80          _this.dirty = false;
81          _this.save_button.el.sensitive = false;
82          
83          
84             
85          
86         // find the text for the node..
87         if (ptype == "listener") {
88             this.node.listeners.set(key,str);
89         
90         } else {
91              this.node.props.set(key,str);
92         }
93     
94          
95         
96         // call the signal..
97         this.save();
98         
99         return true;
100     
101     }
102     public   void show (JsRender.JsRender file, JsRender.Node node, string ptype, string key)
103     {
104         this.ptype = ptype;
105         this.key  = key;
106         this.node = node;
107         this.file = file;
108         
109        string val = "";
110         // find the text for the node..
111         if (ptype == "listener") {
112             val = node.listeners.get(key);
113         
114         } else {
115             val = node.props.get(key);
116         }
117         this.view.load(val);
118         this.key_edit.el.text = key;    
119     
120     }
121     public class Xcls_HBox2 : Object 
122     {
123         public Gtk.HBox el;
124         private Editor  _this;
125
126
127             // my vars (def)
128
129         // ctor 
130         public Xcls_HBox2(Editor _owner )
131         {
132             _this = _owner;
133             this.el = new Gtk.HBox( false, 0 );
134
135             // my vars (dec)
136
137             // set gobject values
138             var child_0 = new Xcls_save_button( _this );
139             child_0.ref();
140             this.el.pack_start (  child_0.el , false,false );
141             var child_1 = new Xcls_key_edit( _this );
142             child_1.ref();
143             this.el.pack_end (  child_1.el , true,true );
144         }
145
146         // user defined functions 
147     }
148     public class Xcls_save_button : Object 
149     {
150         public Gtk.Button el;
151         private Editor  _this;
152
153
154             // my vars (def)
155
156         // ctor 
157         public Xcls_save_button(Editor _owner )
158         {
159             _this = _owner;
160             _this.save_button = this;
161             this.el = new Gtk.Button();
162
163             // my vars (dec)
164
165             // set gobject values
166             this.el.label = "Save";
167
168             // listeners 
169             this.el.clicked.connect( () => { 
170                 _this.saveContents();
171             });
172         }
173
174         // user defined functions 
175     }
176     public class Xcls_key_edit : Object 
177     {
178         public Gtk.Entry el;
179         private Editor  _this;
180
181
182             // my vars (def)
183
184         // ctor 
185         public Xcls_key_edit(Editor _owner )
186         {
187             _this = _owner;
188             _this.key_edit = this;
189             this.el = new Gtk.Entry();
190
191             // my vars (dec)
192
193             // set gobject values
194         }
195
196         // user defined functions 
197     }
198     public class Xcls_RightEditor : Object 
199     {
200         public Gtk.ScrolledWindow el;
201         private Editor  _this;
202
203
204             // my vars (def)
205
206         // ctor 
207         public Xcls_RightEditor(Editor _owner )
208         {
209             _this = _owner;
210             _this.RightEditor = this;
211             this.el = new Gtk.ScrolledWindow( null, null );
212
213             // my vars (dec)
214
215             // set gobject values
216             var child_0 = new Xcls_view( _this );
217             child_0.ref();
218             this.el.add (  child_0.el  );
219
220             // init method 
221
222             this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
223         }
224
225         // user defined functions 
226     }
227     public class Xcls_view : Object 
228     {
229         public Gtk.SourceView el;
230         private Editor  _this;
231
232
233             // my vars (def)
234
235         // ctor 
236         public Xcls_view(Editor _owner )
237         {
238             _this = _owner;
239             _this.view = this;
240             this.el = new Gtk.SourceView();
241
242             // my vars (dec)
243
244             // set gobject values
245             this.el.auto_indent = true;
246             this.el.indent_width = 4;
247             this.el.show_line_marks = true;
248             this.el.insert_spaces_instead_of_tabs = true;
249             this.el.show_line_numbers = true;
250             this.el.highlight_current_line = true;
251             var child_0 = new Xcls_buffer( _this );
252             child_0.ref();
253             this.el.set_buffer (  child_0.el  );
254
255             // init method 
256
257             var description =   Pango.FontDescription.from_string("monospace");
258                 description.set_size(8000);
259                 this.el.override_font(description);
260             
261                 var attrs = new Gtk.SourceMarkAttributes();
262                 var  pink = new Gdk.RGBA();
263                 pink.parse ( "pink");
264                 attrs.set_background ( pink);
265                 attrs.set_icon_name ( "process-stop");    
266                 attrs.query_tooltip_text.connect(( mark) => {
267                     print("tooltip query? %s\n", mark.name);
268                     return mark.name;
269                 });
270                 
271                 this.el.set_mark_attributes ("error", attrs, 1);
272
273             // listeners 
274             this.el.key_release_event.connect( (event) => {
275                 
276                 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
277                     print("SAVE: ctrl-S  pressed");
278                     _this.saveContents();
279                     return false;
280                 }
281                // print(event.key.keyval)
282                 
283                 return false;
284             
285             });
286         }
287
288         // user defined functions 
289         public   void load (string str) {
290         
291         // show the help page for the active node..
292            //this.get('/Help').show();
293         
294         
295           // this.get('/BottomPane').el.set_current_page(0);
296             this.el.get_buffer().set_text(str, str.length);
297             var lm = Gtk.SourceLanguageManager.get_default();
298             
299             var lang = _this.file.language;
300             //?? is javascript going to work as js?
301             
302             ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
303             var buf = this.el.get_buffer();
304             
305             /* -- what does all this do? */
306             /*
307             var cursor = buf.get_mark("insert");
308             Gtk.TextIter iter;
309             buf.get_iter_at_mark(out iter, cursor);
310             iter.set_line(1);
311             iter.set_line_offset(4);
312             buf.move_mark(cursor, iter);
313             
314             
315             cursor = buf.get_mark("selection_bound");
316             //iter= new Gtk.TextIter;
317             buf.get_iter_at_mark(out iter, cursor);
318             iter.set_line(1);
319             iter.set_line_offset(4);
320             buf.move_mark(cursor, iter);
321             
322             */
323             
324             _this.dirty = false;
325             this.el.grab_focus();
326             _this.save_button.el.sensitive = false;
327         }
328     }
329     public class Xcls_buffer : Object 
330     {
331         public Gtk.SourceBuffer el;
332         private Editor  _this;
333
334
335             // my vars (def)
336         public bool check_queued;
337         public int error_line;
338         public bool check_running;
339
340         // ctor 
341         public Xcls_buffer(Editor _owner )
342         {
343             _this = _owner;
344             _this.buffer = this;
345             this.el = new Gtk.SourceBuffer( null );
346
347             // my vars (dec)
348             this.check_queued = false;
349             this.error_line = -1;
350             this.check_running = false;
351
352             // set gobject values
353
354             // listeners 
355             this.el.changed.connect( () => {
356                 // check syntax??
357                 // ??needed..??
358                 _this.save_button.el.sensitive = true;
359                 print("EDITOR CHANGED");
360                 this.checkSyntax();
361                
362                 _this.dirty = true;
363             
364                 // this.get('/LeftPanel.model').changed(  str , false);
365                 return ;
366             });
367         }
368
369         // user defined functions 
370         public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
371                 this.error_line = validate_res.size;
372         
373                 if (this.error_line < 1) {
374                       return true;
375                 }
376                 var tlines = this.el.get_line_count ();
377                 Gtk.TextIter iter;
378                 var valiter = validate_res.map_iterator();
379                 while (valiter.next()) {
380                 
381             //        print("get inter\n");
382                     var eline = valiter.get_key();
383                     if (eline > tlines) {
384                         continue;
385                     }
386                     this.el.get_iter_at_line( out iter, eline);
387                     //print("mark line\n");
388                     this.el.create_source_mark(valiter.get_value(), "error", iter);
389                 }   
390                 return false;
391             }
392         public   string toString () {
393             
394             Gtk.TextIter s;
395             Gtk.TextIter e;
396             this.el.get_start_iter(out s);
397             this.el.get_end_iter(out e);
398             var ret = this.el.get_text(s,e,true);
399             //print("TO STRING? " + ret);
400             return ret;
401         }
402         public   bool checkSyntax () {
403          
404             if (this.check_running) {
405                 print("Check is running\n");
406                 if (this.check_queued) { 
407                     print("Check is already queued");
408                     return true;
409                 }
410                 this.check_queued = true;
411                 print("Adding queued Check ");
412                 GLib.Timeout.add_seconds(1, () => {
413                     this.check_queued = false;
414                     
415                     this.checkSyntax();
416                     return false;
417                 });
418             
419         
420                 return true;
421             }
422            
423            
424             var p = Palete.factory(_this.file.xtype);   
425             
426             var str = this.toString();
427             
428             string res = "";
429             
430             if (this.error_line > 0) {
431                  Gtk.TextIter start;
432                  Gtk.TextIter end;     
433                 this.el.get_bounds (out start, out end);
434         
435                 this.el.remove_source_marks (start, end, null);
436             }
437             
438             if (str.length < 1) {
439                 print("checkSyntax - empty string?\n");
440                 return true;
441             }
442             this.check_running = true;
443             
444             
445             if (_this.file.language == "js") {
446                 this.check_running = false;
447                 print("calling validate javascript\n"); 
448                 return this.highlightErrors(p.validateJavascript(
449                     str, 
450                      _this.key, 
451                     _this.ptype,
452                     _this.file,
453                     _this.node
454                 ));    
455                 
456             }
457                 
458             print("calling validate vala\n");    
459             p.validateVala.begin(
460                 str, 
461                  _this.key, 
462                 _this.ptype,
463                 _this.file,
464                 _this.node,
465                 (obj, res) => {
466                     this.check_running = false;
467                     var validate_res = p.validateVala.end(res);
468                     this.highlightErrors(validate_res);
469         
470                 }
471                 
472             );
473              
474             
475             
476             //print("done mark line\n");
477              
478             return true; // at present allow saving - even if it's invalid..
479         }
480     }
481 }