Builder/Provider/RooUsage.txt
[app.Builder.js] / Builder3 / 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         configure_event : function (self, object) {
25             this.pos = this.el.get_position();
26         
27         
28             return false;
29         },
30         show : function (self) {
31             if (this.pos) {
32                 this.el.set_uposition(this.pos.root_x,this.pos.root_y);
33             }
34         }
35     },
36     height_request : 300,
37     id : "EditorWindow",
38     title : "Application Builder - Editor",
39     width_request : 500,
40     save : function (self, event) {
41         if (!this.get('/Editor.RightEditor').save()) {
42             // no hiding with errors.
43             return true;
44         }
45         this.get('/Editor').activePath = false;
46         this.el.hide();
47         return true;
48     },
49     init : function() {
50         XObject.prototype.init.call(this);
51        // this.show_all();
52     },
53     items : [
54         {
55             xtype: Gtk.VBox,
56             pack : "add",
57             items : [
58                 {
59                     xtype: Gtk.MenuBar,
60                     pack : "pack_start,false,true",
61                     items : [
62                         {
63                             xtype: Gtk.MenuItem,
64                              
65                             listeners : {
66                                 activate : 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 ret = {};
139                                 buf.get_iter_at_mark(ret, cursor);
140                                 ret.iter.set_line(1);
141                                 ret.iter.set_line_offset(4);
142                                 buf.move_mark(cursor, ret.iter);
143                                 
144                                 
145                                 cursor = buf.get_mark("selection_bound");
146                                 ret = {}; 
147                                 buf.get_iter_at_mark(ret, cursor);
148                                 ret.iter.set_line(1);
149                                 ret.iter.set_line_offset(4);
150                                 buf.move_mark(cursor, ret.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                                         var s = {};
205                                         var e = {};
206                                          
207                                         this.el.get_start_iter(s).value;
208                                         this.el.get_end_iter(e).value;
209                                         
210                                         var ret = this.el.get_text(s.iter,e.iter,true);
211                                         //print("TO STRING? " + ret);
212                                         return ret;
213                                     }
214                                 }
215                             ]
216                         }
217                     ]
218                 }
219             ]
220         }
221     ]
222 });
223 Editor.init();
224 XObject.cache['/Editor'] = Editor;