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