Attribute changed old-javascript
[app.Builder.js] / old-javascript / 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         // user defined functions 
222     }
223     public class Xcls_view : Object 
224     {
225         public Gtk.SourceView el;
226         private Editor  _this;
227
228
229             // my vars (def)
230
231         // ctor 
232         public Xcls_view(Editor _owner )
233         {
234             _this = _owner;
235             _this.view = this;
236             this.el = new Gtk.SourceView();
237
238             // my vars (dec)
239
240             // set gobject values
241             this.el.auto_indent = true;
242             this.el.indent_width = 4;
243             this.el.show_line_marks = true;
244             this.el.insert_spaces_instead_of_tabs = true;
245             this.el.show_line_numbers = true;
246             this.el.highlight_current_line = true;
247             var child_0 = new Xcls_buffer( _this );
248             child_0.ref();
249             this.el.set_buffer (  child_0.el  );
250
251             // init method 
252
253             var description =   Pango.FontDescription.from_string("monospace");
254                 description.set_size(8000);
255                 this.el.override_font(description);
256             
257                 var attrs = new Gtk.SourceMarkAttributes();
258                 var  pink = new Gdk.RGBA();
259                 pink.parse ( "pink");
260                 attrs.set_background ( pink);
261                 attrs.set_icon_name ( "process-stop");    
262                 attrs.query_tooltip_text.connect(( mark) => {
263                     print("tooltip query? %s\n", mark.name);
264                     return mark.name;
265                 });
266                 
267                 this.el.set_mark_attributes ("error", attrs, 1);
268             // listeners 
269             this.el.key_release_event.connect( (event) => {
270                 
271                 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
272                     print("SAVE: ctrl-S  pressed");
273                     _this.saveContents();
274                     return false;
275                 }
276                // print(event.key.keyval)
277                 
278                 return false;
279             
280             });
281         }
282
283         // user defined functions 
284         public   void load (string str) {
285         
286         // show the help page for the active node..
287            //this.get('/Help').show();
288         
289         
290           // this.get('/BottomPane').el.set_current_page(0);
291             this.el.get_buffer().set_text(str, str.length);
292             var lm = Gtk.SourceLanguageManager.get_default();
293             
294             var lang = _this.file.language;
295             //?? is javascript going to work as js?
296             
297             ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
298             var buf = this.el.get_buffer();
299             
300             /* -- what does all this do? */
301             /*
302             var cursor = buf.get_mark("insert");
303             Gtk.TextIter iter;
304             buf.get_iter_at_mark(out iter, cursor);
305             iter.set_line(1);
306             iter.set_line_offset(4);
307             buf.move_mark(cursor, iter);
308             
309             
310             cursor = buf.get_mark("selection_bound");
311             //iter= new Gtk.TextIter;
312             buf.get_iter_at_mark(out iter, cursor);
313             iter.set_line(1);
314             iter.set_line_offset(4);
315             buf.move_mark(cursor, iter);
316             
317             */
318             
319             _this.dirty = false;
320             this.el.grab_focus();
321             _this.save_button.el.sensitive = false;
322         }
323     }
324     public class Xcls_buffer : Object 
325     {
326         public Gtk.SourceBuffer el;
327         private Editor  _this;
328
329
330             // my vars (def)
331         public int error_line;
332
333         // ctor 
334         public Xcls_buffer(Editor _owner )
335         {
336             _this = _owner;
337             _this.buffer = this;
338             this.el = new Gtk.SourceBuffer( null );
339
340             // my vars (dec)
341             this.error_line = -1;
342
343             // set gobject values
344
345             // listeners 
346             this.el.changed.connect( () => {
347                 // check syntax??
348                     if(this.checkSyntax()) {
349                     _this.save_button.el.sensitive = true;
350                 }
351                // print("EDITOR CHANGED");
352                 _this.dirty = true;
353             
354                 // this.get('/LeftPanel.model').changed(  str , false);
355                 return ;
356             });
357         }
358
359         // user defined functions 
360         public   string toString () {
361             
362             Gtk.TextIter s;
363             Gtk.TextIter e;
364             this.el.get_start_iter(out s);
365             this.el.get_end_iter(out e);
366             var ret = this.el.get_text(s,e,true);
367             //print("TO STRING? " + ret);
368             return ret;
369         }
370         public   bool checkSyntax () {
371          
372             var p = Palete.factory(_this.file.xtype);   
373             
374             var str = this.toString();
375             
376             string res = "";
377             
378             if (this.error_line > 0) {
379                  Gtk.TextIter start;
380                  Gtk.TextIter end;     
381                 this.el.get_bounds (out start, out end);
382         
383                 this.el.remove_source_marks (start, end, null);
384             }
385             
386             
387             var validate_res =  p.validateCode(
388                 str, 
389                  _this.key, 
390                 _this.ptype,
391                 _this.file,
392                 _this.node
393                 
394             );
395              
396             this.error_line = validate_res.size;
397         
398             if (this.error_line < 1) {
399                   return true;
400             }
401             var tlines = this.el.get_line_count ();
402             Gtk.TextIter iter;
403             var valiter = validate_res.map_iterator();
404             while (valiter.next()) {
405             
406         //        print("get inter\n");
407                 var eline = valiter.get_key();
408                 if (eline > tlines) {
409                     continue;
410                 }
411                 this.el.get_iter_at_line( out iter, eline);
412                 //print("mark line\n");
413                 this.el.create_source_mark(valiter.get_value(), "error", iter);
414             }   
415             
416             //print("done mark line\n");
417              
418             return true; // at present allow saving - even if it's invalid..
419         }
420     }
421 }