Palete/Gtk.js
[app.Builder.js] / Builder4 / Editor.js
1 Gtk = imports.gi.Gtk;
2 Gdk = imports.gi.Gdk;
3 Pango = imports.gi.Pango;
4 GLib = imports.gi.GLib;
5 Gio = imports.gi.Gio;
6 GObject = imports.gi.GObject;
7 GtkSource = imports.gi.GtkSource;
8 WebKit = imports.gi.WebKit;
9 Vte = imports.gi.Vte;
10 console = imports.console;
11 XObject = imports.XObject.XObject;
12 Editor=new XObject({
13     xtype: Gtk.Window,
14     listeners : {
15         delete_event : function (self, event) {
16             if (!this.get('/Editor.RightEditor').save()) {
17                 // no hiding with errors.
18                 return true;
19             }
20             this.el.hide();
21             this.get('/Editor').activePath = false;
22             return true;
23         }
24         /*--
25         
26         (event) => {
27             if (!Editor.RightEditor.save()) {
28                 // no hiding with errors.
29                 return true;
30             }
31             _this.el.hide();
32             _this.active_path = "";
33             return true;
34         }
35         
36         */,
37         configure_event : function (self, object) {
38             this.pos = this.el.get_position();
39         
40             return false;
41         }
42         /*--
43          (object) => {
44             _this.pos = true;
45             this.el.get_position(out _this.pos_root_x, out _this.pos_root_y);
46         
47         
48             return false;
49         }
50         */,
51         show : function (self) {
52             if (this.pos) {
53                 this.el.set_uposition(this.pos.root_x,this.pos.root_y);
54             }
55         }
56         /*--
57          () => {
58             if (this.pos) {
59                 _this.el.move(this.pos_root_x,this.pos_root_y);
60             }
61         }
62         
63         */
64     },
65     height_request : 300,
66     id : "Editor",
67     title : "Application Builder -  Code  Editor",
68     width_request : 500,
69     init : function() {
70         XObject.prototype.init.call(this);
71        // this.show_all();
72     },
73     save : function (self, event) {
74         if (!this.get('/Editor.RightEditor').save()) {
75             // no hiding with errors.
76             return true;
77         }
78         this.get('/Editor').activePath = false;
79         this.el.hide();
80         return true;
81     }
82     /*--
83     
84     bool ()  {
85     
86         if (!Editor.RightEditor.save()) {
87             // no hiding with errors.
88             return true;
89         }
90         _this.active_path = "";
91         _this.el.hide();
92         return true;
93     
94     }
95     */,
96     show_all : function() {
97         
98     }
99     /*--
100     void () {
101         this.el.show_all();
102     
103     }
104     */,
105     items : [
106         {
107             xtype: Gtk.VBox,
108             homogeneous : true,
109             pack : "add",
110             items : [
111                 {
112                     xtype: Gtk.Toolbar,
113                     pack : "pack_start,false,true",
114                     items : [
115                         {
116                             xtype: Gtk.ToolButton,
117                             listeners : {
118                                 clicked : function (self) {
119                                 
120                                   this.get('/Editor.RightEditor').save();
121                                 }
122                                 /*--
123                                 () => { 
124                                     Editor.RightEditor.save();
125                                 }
126                                 
127                                 */
128                             },
129                             id : "save_button",
130                             label : "Save"
131                         }
132                     ]
133                 },
134                 {
135                     xtype: Gtk.ScrolledWindow,
136                     id : "RightEditor",
137                     pack : "add",
138                     save : function() {
139                         // make sure we have an active path..
140                          if (!this.get('/Editor').activePath) {
141                             return true;
142                          }
143                          
144                          var str = this.get('/Editor.buffer').toString();
145                          if (!this.get('/Editor.buffer').checkSyntax()) {
146                              this.get('/StandardErrorDialog').show("Fix errors in code and save.."); 
147                              return false;
148                          }
149                          
150                          this.get('/LeftPanel.model').changed(  str , false);
151                          this.get('/Editor').dirty = false;
152                          this.get('/Editor.save_button').el.sensitive = false;
153                          return true;
154                     }
155                     /*--
156                     bool () {
157                          print("editor.rightbutton.save");
158                          if (_this.active_path.length  < 1 ) {
159                               print("skip - no active path");
160                             return true;
161                          }
162                          
163                          var str = Editor.buffer.toString();
164                          
165                          if (!Editor.buffer.checkSyntax()) {
166                              print("check syntax failed");
167                              //this.get('/StandardErrorDialog').show("Fix errors in code and save.."); 
168                              return false;
169                          }
170                          
171                          // LeftPanel.model.changed(  str , false);
172                          _this.dirty = false;
173                          _this.save_button.el.sensitive = false;
174                          print("set save button grey");
175                          return true;
176                     }
177                     
178                     */,
179                     items : [
180                         {
181                             xtype: GtkSource.View,
182                             listeners : {
183                                 key_release_event : function (self, event) {
184                                     
185                                     if (event.key.keyval == 115 && (event.key.state & Gdk.ModifierType.CONTROL_MASK ) ) {
186                                         print("SAVE: ctrl-S  pressed");
187                                         this.save();
188                                         return false;
189                                     }
190                                    // print(event.key.keyval)
191                                     
192                                     return false;
193                                 }
194                                 /*--
195                                 (event) => {
196                                     
197                                     if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
198                                         print("SAVE: ctrl-S  pressed");
199                                         this.save();
200                                         return false;
201                                     }
202                                    // print(event.key.keyval)
203                                     
204                                     return false;
205                                 
206                                 }
207                                 */
208                             },
209                             id : "view",
210                             indent_width : 4,
211                             pack : "add",
212                             auto_indent : true,
213                             init : function() {
214                                 XObject.prototype.init.call(this);
215                                  var description = Pango.Font.description_from_string("monospace")
216                                 description.set_size(8000);
217                                 this.el.modify_font(description);
218                             
219                             }
220                             /*--
221                             
222                                 var description =   Pango.FontDescription.from_string("monospace");
223                                 description.set_size(8000);
224                                 this.el.override_font(description);
225                             
226                             
227                             */,
228                             insert_spaces_instead_of_tabs : true,
229                             load : function(str) {
230                             
231                             // show the help page for the active node..
232                                //this.get('/Help').show();
233                             
234                             
235                               // this.get('/BottomPane').el.set_current_page(0);
236                                 this.el.get_buffer().set_text(str, str.length);
237                                 var lm = GtkSource.LanguageManager.get_default();
238                                 
239                                 this.el.get_buffer().set_language(lm.get_language('js'));
240                                 var buf = this.el.get_buffer();
241                                 var cursor = buf.get_mark("insert");
242                                 var iter= new Gtk.TextIter;
243                                 buf.get_iter_at_mark(iter, cursor);
244                                 iter.set_line(1);
245                                 iter.set_line_offset(4);
246                                 buf.move_mark(cursor, iter);
247                                 
248                                 
249                                 cursor = buf.get_mark("selection_bound");
250                                 iter= new Gtk.TextIter;
251                                 buf.get_iter_at_mark(iter, cursor);
252                                 iter.set_line(1);
253                                 iter.set_line_offset(4);
254                                 buf.move_mark(cursor, iter);
255                                 this.get('/Editor').dirty = false;
256                                 this.el.grab_focus();
257                                  this.get('/Editor.save_button').el.sensitive = false;
258                             },
259                             save : function() {
260                                 
261                                 return this.get('/Editor.RightEditor').save();
262                             
263                             }
264                             /*--
265                             void () {
266                             
267                                 Editor.RightEditor.save();
268                             }
269                             */,
270                             show_line_numbers : true,
271                             items : [
272                                 {
273                                     xtype: GtkSource.Buffer,
274                                     listeners : {
275                                         changed : function (self) {
276                                         
277                                             if(this.checkSyntax()) {
278                                                 this.get('/Editor.save_button').el.sensitive = true;
279                                             }
280                                            // print("EDITOR CHANGED");
281                                             this.get('/Editor').dirty = true;
282                                         
283                                             // this.get('/LeftPanel.model').changed(  str , false);
284                                             return false;
285                                         }
286                                         
287                                         /*--
288                                         
289                                         () => {
290                                             // check syntax??
291                                                 if(this.checkSyntax()) {
292                                                 Editor.save_button.el.sensitive = true;
293                                             }
294                                            // print("EDITOR CHANGED");
295                                             Editor.dirty = true;
296                                         
297                                             // this.get('/LeftPanel.model').changed(  str , false);
298                                             return ;
299                                         }
300                                         
301                                         
302                                         */
303                                     },
304                                     id : "buffer",
305                                     pack : "set_buffer",
306                                     checkSyntax : function() {
307                                         var str = this.toString();
308                                         var res = '';
309                                         try {
310                                           //  print('var res = ' + str);
311                                             Seed.check_syntax('var res = ' + str);
312                                             
313                                            
314                                         } catch (e) {
315                                             
316                                             this.get('/RightEditor.view').el.modify_base(Gtk.StateType.NORMAL, new Gdk.Color({
317                                                 red: 0xFFFF, green: 0xCCCC , blue : 0xCCCC
318                                                }));
319                                             print("SYNTAX ERROR IN EDITOR");   
320                                             print(e);
321                                             // print(str);
322                                             //console.dump(e);
323                                             return false;
324                                         }
325                                          this.get('/RightEditor.view').el.modify_base(Gtk.StateType.NORMAL, new Gdk.Color({
326                                             red: 0xFFFF, green: 0xFFFF , blue : 0xFFFF
327                                            }));
328                                         
329                                         return true;
330                                     }
331                                     /*--
332                                     bool () { 
333                                         // we could try running valac... ?? but it's a bit confusing..
334                                         return true;
335                                     
336                                     }
337                                     
338                                     
339                                     */,
340                                     toString : function() {
341                                         
342                                         var s = new Gtk.TextIter();
343                                         var e = new Gtk.TextIter();
344                                         this.el.get_start_iter(s);
345                                         this.el.get_end_iter(e);
346                                         var ret = this.el.get_text(s,e,true);
347                                         //print("TO STRING? " + ret);
348                                         return ret;
349                                     }
350                                     /*--
351                                     string () {
352                                         
353                                         Gtk.TextIter s;
354                                         Gtk.TextIter e;
355                                         this.el.get_start_iter(out s);
356                                         this.el.get_end_iter(out e);
357                                         var ret = this.el.get_text(s,e,true);
358                                         //print("TO STRING? " + ret);
359                                         return ret;
360                                     }
361                                     
362                                     */
363                                 }
364                             ]
365                         }
366                     ]
367                 }
368             ]
369         }
370     ]
371 });
372 Editor.init();
373 XObject.cache['/Editor'] = Editor;