Sample/Editor.bjs
[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         destroy_event : function (self, event) {
17             this.el.hide();
18             return true;
19         },
20         delete_event : function (self, event) {
21               this.el.hide();
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     init : function() {
41         XObject.prototype.init.call(this);
42        // this.show_all();
43     },
44     items : [
45         {
46             xtype: Gtk.VBox,
47             pack : "add",
48             items : [
49                 {
50                     xtype: Gtk.Toolbar,
51                     pack : "pack_start,false,true",
52                     items : [
53                         {
54                             xtype: Gtk.Button,
55                             label : "Save"
56                         }
57                     ]
58                 },
59                 {
60                     xtype: Gtk.ScrolledWindow,
61                     id : "RightEditor",
62                     pack : "add",
63                     save : function() {
64                          this.get('/LeftPanel.model').changed(  str , false);
65                          this.get('/Editor').dirty = false;
66                     },
67                     items : [
68                         {
69                             xtype: GtkSource.View,
70                             listeners : {
71                                 key_release_event : function (self, event) {
72                                     if (event.key.keyval != 115 || !(Gdk.ModifierType.CONTROL_MASK & 4) ) {
73                                         return;
74                                     }
75                                     print(event.key.keyval)
76                                     this.save();
77                                     return false;
78                                 }
79                             },
80                             id : "view",
81                             indent_width : 4,
82                             pack : "add",
83                             auto_indent : true,
84                             init : function() {
85                                 XObject.prototype.init.call(this);
86                                  var description = Pango.Font.description_from_string("monospace")
87                                 description.set_size(8000);
88                                 this.el.modify_font(description);
89                             
90                             },
91                             insert_spaces_instead_of_tabs : true,
92                             load : function(str) {
93                             
94                             // show the help page for the active node..
95                                //this.get('/Help').show();
96                             
97                             
98                               // this.get('/BottomPane').el.set_current_page(0);
99                                 this.el.get_buffer().set_text(str, str.length);
100                                 var lm = GtkSource.LanguageManager.get_default();
101                                 
102                                 this.el.get_buffer().set_language(lm.get_language('js'));
103                                 var buf = this.el.get_buffer();
104                                 var cursor = buf.get_mark("insert");
105                                 var iter= new Gtk.TextIter;
106                                 buf.get_iter_at_mark(iter, cursor);
107                                 iter.set_line(1);
108                                 iter.set_line_offset(4);
109                                 buf.move_mark(cursor, iter);
110                                 
111                                 
112                                 cursor = buf.get_mark("selection_bound");
113                                 iter= new Gtk.TextIter;
114                                 buf.get_iter_at_mark(iter, cursor);
115                                 iter.set_line(1);
116                                 iter.set_line_offset(4);
117                                 buf.move_mark(cursor, iter);
118                                 this.get('/Editor').dirty = false;
119                                 this.el.grab_focus();
120                             },
121                             save : function() {
122                                 var str = this.get('buffer').toString();
123                                 print("SAVE" + str);
124                                  this.get('/LeftPanel.model').changed(  str , false);
125                             },
126                             show_line_numbers : true,
127                             items : [
128                                 {
129                                     xtype: GtkSource.Buffer,
130                                     listeners : {
131                                         changed : function (self) {
132                                         
133                                             this.checkSyntax();
134                                            // print("EDITOR CHANGED");
135                                             this.get('/Editor').dirty = true;
136                                             // this.get('/LeftPanel.model').changed(  str , false);
137                                             return false;
138                                         }
139                                     },
140                                     id : "buffer",
141                                     pack : "set_buffer",
142                                     checkSyntax : function() {
143                                         var str = this.toString();
144                                         var res = '';
145                                         try {
146                                           //  print('var res = ' + str);
147                                             Seed.check_syntax('var res = ' + str);
148                                             
149                                             this.get('/RightEditor.view').el.modify_base(Gtk.StateType.NORMAL, new Gdk.Color({
150                                                 red: 0xFFFF, green: 0xFFFF , blue : 0xFFFF
151                                                }));
152                                             
153                                             return true;
154                                         } catch (e) {
155                                             
156                                             this.get('/RightEditor.view').el.modify_base(Gtk.StateType.NORMAL, new Gdk.Color({
157                                                 red: 0xFFFF, green: 0xCCCC , blue : 0xCCCC
158                                                }));
159                                             print("SYNTAX ERROR IN EDITOR");   
160                                             print(e);
161                                             // print(str);
162                                             //console.dump(e);
163                                             return false;
164                                         }
165                                     },
166                                     toString : function() {
167                                         
168                                         var s = new Gtk.TextIter();
169                                         var e = new Gtk.TextIter();
170                                         this.el.get_start_iter(s);
171                                         this.el.get_end_iter(e);
172                                         var ret = this.el.get_text(s,e,true);
173                                         //print("TO STRING? " + ret);
174                                         return ret;
175                                     }
176                                 }
177                             ]
178                         }
179                     ]
180                 }
181             ]
182         }
183     ]
184 });
185 Editor.init();
186 XObject.cache['/Editor'] = Editor;