Builder4/Editor.bjs
[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 - 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     items : [
97         {
98             xtype: Gtk.VBox,
99             pack : "add",
100             items : [
101                 {
102                     xtype: Gtk.Toolbar,
103                     pack : "pack_start,false,true",
104                     items : [
105                         {
106                             xtype: Gtk.ToolButton,
107                             listeners : {
108                                 clicked : function (self) {
109                                 
110                                   this.get('/Editor.RightEditor').save();
111                                 }
112                                 /*--
113                                 () => { 
114                                     Editor.RightEditor.save();
115                                 }
116                                 
117                                 */
118                             },
119                             id : "save_button",
120                             label : "Save"
121                         }
122                     ]
123                 },
124                 {
125                     xtype: Gtk.ScrolledWindow,
126                     id : "RightEditor",
127                     pack : "add",
128                     save : function() {
129                         // make sure we have an active path..
130                          if (!this.get('/Editor').activePath) {
131                             return true;
132                          }
133                          
134                          var str = this.get('/Editor.buffer').toString();
135                          if (!this.get('/Editor.buffer').checkSyntax()) {
136                              this.get('/StandardErrorDialog').show("Fix errors in code and save.."); 
137                              return false;
138                          }
139                          
140                          this.get('/LeftPanel.model').changed(  str , false);
141                          this.get('/Editor').dirty = false;
142                          this.get('/Editor.save_button').el.sensitive = false;
143                          return true;
144                     }
145                     /*--
146                     bool () {
147                          print("editor.rightbutton.save");
148                          if (_this.active_path.length  < 1 ) {
149                               print("skip - no active path");
150                             return true;
151                          }
152                          
153                          var str = Editor.buffer.toString();
154                          
155                          if (!Editor.buffer.checkSyntax()) {
156                              print("check syntax failed");
157                              //this.get('/StandardErrorDialog').show("Fix errors in code and save.."); 
158                              return false;
159                          }
160                          
161                          // LeftPanel.model.changed(  str , false);
162                          _this.dirty = false;
163                          _this.save_button.el.sensitive = false;
164                          print("set save button grey");
165                          return true;
166                     }
167                     
168                     */,
169                     items : [
170                         {
171                             xtype: GtkSource.View,
172                             listeners : {
173                                 key_release_event : function (self, event) {
174                                     
175                                     if (event.key.keyval == 115 && (event.key.state & Gdk.ModifierType.CONTROL_MASK ) ) {
176                                         print("SAVE: ctrl-S  pressed");
177                                         this.save();
178                                         return false;
179                                     }
180                                    // print(event.key.keyval)
181                                     
182                                     return false;
183                                 }
184                                 /*--
185                                 (event) => {
186                                     
187                                     if (event.key.keyval == 115 && (event.key.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
188                                         print("SAVE: ctrl-S  pressed");
189                                         this.save();
190                                         return false;
191                                     }
192                                    // print(event.key.keyval)
193                                     
194                                     return false;
195                                 
196                                 }
197                                 */
198                             },
199                             id : "view",
200                             indent_width : 4,
201                             pack : "add",
202                             auto_indent : true,
203                             init : function() {
204                                 XObject.prototype.init.call(this);
205                                  var description = Pango.Font.description_from_string("monospace")
206                                 description.set_size(8000);
207                                 this.el.modify_font(description);
208                             
209                             }
210                             /*--
211                             
212                                 var description =   Pango.FontDescription.from_string("monospace");
213                                 description.set_size(8000);
214                                 this.override_font(description);
215                             
216                             
217                             */,
218                             insert_spaces_instead_of_tabs : true,
219                             load : function(str) {
220                             
221                             // show the help page for the active node..
222                                //this.get('/Help').show();
223                             
224                             
225                               // this.get('/BottomPane').el.set_current_page(0);
226                                 this.el.get_buffer().set_text(str, str.length);
227                                 var lm = GtkSource.LanguageManager.get_default();
228                                 
229                                 this.el.get_buffer().set_language(lm.get_language('js'));
230                                 var buf = this.el.get_buffer();
231                                 var cursor = buf.get_mark("insert");
232                                 var iter= new Gtk.TextIter;
233                                 buf.get_iter_at_mark(iter, cursor);
234                                 iter.set_line(1);
235                                 iter.set_line_offset(4);
236                                 buf.move_mark(cursor, iter);
237                                 
238                                 
239                                 cursor = buf.get_mark("selection_bound");
240                                 iter= new Gtk.TextIter;
241                                 buf.get_iter_at_mark(iter, cursor);
242                                 iter.set_line(1);
243                                 iter.set_line_offset(4);
244                                 buf.move_mark(cursor, iter);
245                                 this.get('/Editor').dirty = false;
246                                 this.el.grab_focus();
247                                  this.get('/Editor.save_button').el.sensitive = false;
248                             },
249                             save : function() {
250                                 
251                                 return this.get('/Editor.RightEditor').save();
252                             
253                             }
254                             /*--
255                             void () {
256                             
257                                 Editor.RightEditor.save();
258                             }
259                             */,
260                             show_line_numbers : true,
261                             items : [
262                                 {
263                                     xtype: GtkSource.Buffer,
264                                     listeners : {
265                                         changed : function (self) {
266                                         
267                                             if(this.checkSyntax()) {
268                                                 this.get('/Editor.save_button').el.sensitive = true;
269                                             }
270                                            // print("EDITOR CHANGED");
271                                             this.get('/Editor').dirty = true;
272                                         
273                                             // this.get('/LeftPanel.model').changed(  str , false);
274                                             return false;
275                                         }
276                                         
277                                         /*--
278                                         
279                                         () => {
280                                             // check syntax??
281                                                 if(this.checkSyntax()) {
282                                                 Editor.save_button.sensitive = true;
283                                             }
284                                            // print("EDITOR CHANGED");
285                                             Editor.dirty = true;
286                                         
287                                             // this.get('/LeftPanel.model').changed(  str , false);
288                                             return ;
289                                         }
290                                         
291                                         
292                                         */
293                                     },
294                                     id : "buffer",
295                                     pack : "set_buffer",
296                                     checkSyntax : function() {
297                                         var str = this.toString();
298                                         var res = '';
299                                         try {
300                                           //  print('var res = ' + str);
301                                             Seed.check_syntax('var res = ' + str);
302                                             
303                                            
304                                         } catch (e) {
305                                             
306                                             this.get('/RightEditor.view').el.modify_base(Gtk.StateType.NORMAL, new Gdk.Color({
307                                                 red: 0xFFFF, green: 0xCCCC , blue : 0xCCCC
308                                                }));
309                                             print("SYNTAX ERROR IN EDITOR");   
310                                             print(e);
311                                             // print(str);
312                                             //console.dump(e);
313                                             return false;
314                                         }
315                                          this.get('/RightEditor.view').el.modify_base(Gtk.StateType.NORMAL, new Gdk.Color({
316                                             red: 0xFFFF, green: 0xFFFF , blue : 0xFFFF
317                                            }));
318                                         
319                                         return true;
320                                     }
321                                     /*--
322                                     bool () { 
323                                         // we could try running valac... ?? but it's a bit confusing..
324                                         return true;
325                                     
326                                     }
327                                     
328                                     
329                                     */,
330                                     toString : function() {
331                                         
332                                         var s = new Gtk.TextIter();
333                                         var e = new Gtk.TextIter();
334                                         this.el.get_start_iter(s);
335                                         this.el.get_end_iter(e);
336                                         var ret = this.el.get_text(s,e,true);
337                                         //print("TO STRING? " + ret);
338                                         return ret;
339                                     }
340                                     /*--
341                                     string () {
342                                         
343                                         Gtk.TextIter s;
344                                         Gtk.TextIter e;
345                                         this.el.get_start_iter(out s);
346                                         this.el.get_end_iter(out e);
347                                         var ret = this.el.get_text(s,e,true);
348                                         //print("TO STRING? " + ret);
349                                         return ret;
350                                     }
351                                     
352                                     */
353                                 }
354                             ]
355                         }
356                     ]
357                 }
358             ]
359         }
360     ]
361 });
362 Editor.init();
363 XObject.cache['/Editor'] = Editor;