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