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