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