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