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