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