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