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.hide();
32             _this.activePath = false;
33             return true;
34         }
35         
36         */,
37         configure_event : function (self, object) {
38             this.pos = this.el.get_position();
39         
40         
41             return false;
42         }
43         /*--
44          (self, object) {
45             _this.pos = this.get_position();
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     height_request : 300,
58     id : "EditorWindow",
59     title : "Application Builder - Editor",
60     width_request : 500,
61     init : function() {
62         XObject.prototype.init.call(this);
63        // this.show_all();
64     },
65     save : function (self, event) {
66         if (!this.get('/Editor.RightEditor').save()) {
67             // no hiding with errors.
68             return true;
69         }
70         this.get('/Editor').activePath = false;
71         this.el.hide();
72         return true;
73     },
74     items : [
75         {
76             xtype: Gtk.VBox,
77             pack : "add",
78             items : [
79                 {
80                     xtype: Gtk.Toolbar,
81                     pack : "pack_start,false,true",
82                     items : [
83                         {
84                             xtype: Gtk.ToolButton,
85                             listeners : {
86                                 clicked : function (self) {
87                                 
88                                   this.get('/Editor.RightEditor').save();
89                                 }
90                             },
91                             id : "save_button",
92                             label : "Save"
93                         }
94                     ]
95                 },
96                 {
97                     xtype: Gtk.ScrolledWindow,
98                     id : "RightEditor",
99                     pack : "add",
100                     save : function() {
101                         // make sure we have an active path..
102                          if (!this.get('/Editor').activePath) {
103                             return true;
104                          }
105                          
106                          var str = this.get('/Editor.buffer').toString();
107                          if (!this.get('/Editor.buffer').checkSyntax()) {
108                              this.get('/StandardErrorDialog').show("Fix errors in code and save.."); 
109                              return false;
110                          }
111                          
112                          this.get('/LeftPanel.model').changed(  str , false);
113                          this.get('/Editor').dirty = false;
114                          this.get('/Editor.save_button').el.sensitive = false;
115                          return true;
116                     },
117                     items : [
118                         {
119                             xtype: GtkSource.View,
120                             listeners : {
121                                 key_release_event : function (self, event) {
122                                     
123                                     if (event.key.keyval == 115 && (event.key.state & Gdk.ModifierType.CONTROL_MASK ) ) {
124                                         print("SAVE: ctrl-S  pressed");
125                                         this.save();
126                                         return false;
127                                     }
128                                    // print(event.key.keyval)
129                                     
130                                     return false;
131                                 }
132                             },
133                             id : "view",
134                             indent_width : 4,
135                             pack : "add",
136                             auto_indent : true,
137                             init : function() {
138                                 XObject.prototype.init.call(this);
139                                  var description = Pango.Font.description_from_string("monospace")
140                                 description.set_size(8000);
141                                 this.el.modify_font(description);
142                             
143                             },
144                             insert_spaces_instead_of_tabs : true,
145                             load : function(str) {
146                             
147                             // show the help page for the active node..
148                                //this.get('/Help').show();
149                             
150                             
151                               // this.get('/BottomPane').el.set_current_page(0);
152                                 this.el.get_buffer().set_text(str, str.length);
153                                 var lm = GtkSource.LanguageManager.get_default();
154                                 
155                                 this.el.get_buffer().set_language(lm.get_language('js'));
156                                 var buf = this.el.get_buffer();
157                                 var cursor = buf.get_mark("insert");
158                                 var iter= new Gtk.TextIter;
159                                 buf.get_iter_at_mark(iter, cursor);
160                                 iter.set_line(1);
161                                 iter.set_line_offset(4);
162                                 buf.move_mark(cursor, iter);
163                                 
164                                 
165                                 cursor = buf.get_mark("selection_bound");
166                                 iter= new Gtk.TextIter;
167                                 buf.get_iter_at_mark(iter, cursor);
168                                 iter.set_line(1);
169                                 iter.set_line_offset(4);
170                                 buf.move_mark(cursor, iter);
171                                 this.get('/Editor').dirty = false;
172                                 this.el.grab_focus();
173                                  this.get('/Editor.save_button').el.sensitive = false;
174                             },
175                             save : function() {
176                                 
177                                 return this.get('/Editor.RightEditor').save();
178                             },
179                             show_line_numbers : true,
180                             items : [
181                                 {
182                                     xtype: GtkSource.Buffer,
183                                     listeners : {
184                                         changed : function (self) {
185                                         
186                                             if(this.checkSyntax()) {
187                                                 this.get('/Editor.save_button').el.sensitive = true;
188                                             }
189                                            // print("EDITOR CHANGED");
190                                             this.get('/Editor').dirty = true;
191                                         
192                                             // this.get('/LeftPanel.model').changed(  str , false);
193                                             return false;
194                                         }
195                                     },
196                                     id : "buffer",
197                                     pack : "set_buffer",
198                                     checkSyntax : function() {
199                                         var str = this.toString();
200                                         var res = '';
201                                         try {
202                                           //  print('var res = ' + str);
203                                             Seed.check_syntax('var res = ' + str);
204                                             
205                                            
206                                         } catch (e) {
207                                             
208                                             this.get('/RightEditor.view').el.modify_base(Gtk.StateType.NORMAL, new Gdk.Color({
209                                                 red: 0xFFFF, green: 0xCCCC , blue : 0xCCCC
210                                                }));
211                                             print("SYNTAX ERROR IN EDITOR");   
212                                             print(e);
213                                             // print(str);
214                                             //console.dump(e);
215                                             return false;
216                                         }
217                                          this.get('/RightEditor.view').el.modify_base(Gtk.StateType.NORMAL, new Gdk.Color({
218                                             red: 0xFFFF, green: 0xFFFF , blue : 0xFFFF
219                                            }));
220                                         
221                                         return true;
222                                     },
223                                     toString : function() {
224                                         
225                                         var s = new Gtk.TextIter();
226                                         var e = new Gtk.TextIter();
227                                         this.el.get_start_iter(s);
228                                         this.el.get_end_iter(e);
229                                         var ret = this.el.get_text(s,e,true);
230                                         //print("TO STRING? " + ret);
231                                         return ret;
232                                     }
233                                 }
234                             ]
235                         }
236                     ]
237                 }
238             ]
239         }
240     ]
241 });
242 Editor.init();
243 XObject.cache['/Editor'] = Editor;