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