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