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