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