Builder4/Editor.bjs
[app.Builder.js] / Builder4 / Editor.vala
1 /* -- to compile
2 valac  --pkg gio-2.0  --pkg posix  --pkg gtk+-3.0 --pkg libnotify --pkg gtksourceview-3.0  --pkg  libwnck-3.0 \
3     /tmp/test.vala  -o /tmp/Editor
4 */
5
6
7 static int main (string[] args) {
8     Gtk.init (ref args);
9     new Xcls_Editor();
10     Editor.show_all();
11      Gtk.main ();
12     return 0;
13 }
14
15
16 public static Xcls_Editor  Editor;
17
18 private static Xcls_Editor  _this;
19
20 public class Xcls_Editor : Gtk.Window
21 {
22     public Xcls_save_button save_button;
23     public Xcls_RightEditor RightEditor;
24     public Xcls_view view;
25     public Xcls_buffer buffer;
26
27         // my vars
28     public string activeEditor;
29     public string active_path;
30     public bool dirty;
31     public bool pos;
32     public int pos_root_x;
33     public int pos_root_y;
34
35         // ctor 
36     public Xcls_Editor()
37     {
38         _this = this;
39         Editor = this;
40
41         // my vars
42         this.activeEditor = "";
43         this.active_path = "";
44         this.dirty = false;
45         this.pos = false;
46
47         // set gobject values
48         this.height_request = 300;
49         this.title = "Application Builder - Editor";
50         this.width_request = 500;
51         this.add (  new Xcls_VBox2() );
52
53         // listeners 
54         this.delete_event.connect( (event) => {
55             if (!Editor.RightEditor.save()) {
56                 // no hiding with errors.
57                 return true;
58             }
59             _this.hide();
60             _this.active_path = "";
61             return true;
62         } );
63         this.configure_event.connect(  (object) => {
64             _this.pos = true;
65             this.get_position(out _this.pos_root_x, out _this.pos_root_y);
66         
67         
68             return false;
69         } );
70         this.show.connect(  () => {
71             if (this.pos) {
72                 _this.move(this.pos_root_x,this.pos_root_y);
73             }
74         } );
75     }
76
77     // userdefined functions 
78     public bool save()  {
79         
80             if (!Editor.RightEditor.save()) {
81                 // no hiding with errors.
82                 return true;
83             }
84             _this.active_path = "";
85             _this.hide();
86             return true;
87         
88         }
89     public class Xcls_VBox2 : Gtk.VBox
90     {
91
92             // my vars
93
94             // ctor 
95         public Xcls_VBox2()
96         {
97
98             // my vars
99
100             // set gobject values
101             this.pack_start (  new Xcls_Toolbar3(), false,true );
102             this.add (  new Xcls_RightEditor() );
103         }
104
105         // userdefined functions 
106     }
107     public class Xcls_Toolbar3 : Gtk.Toolbar
108     {
109
110             // my vars
111
112             // ctor 
113         public Xcls_Toolbar3()
114         {
115
116             // my vars
117
118             // set gobject values
119             this.add (  new Xcls_save_button() );
120         }
121
122         // userdefined functions 
123     }
124     public class Xcls_save_button : Gtk.ToolButton
125     {
126
127             // my vars
128
129             // ctor 
130         public Xcls_save_button()
131         {
132             _this.save_button = this;
133
134             // my vars
135
136             // set gobject values
137             this.label = "Save";
138
139             // listeners 
140             this.clicked.connect( () => { 
141                 Editor.RightEditor.save();
142             } );
143         }
144
145         // userdefined functions 
146     }
147     public class Xcls_RightEditor : Gtk.ScrolledWindow
148     {
149
150             // my vars
151
152             // ctor 
153         public Xcls_RightEditor()
154         {
155             _this.RightEditor = this;
156
157             // my vars
158
159             // set gobject values
160             this.add (  new Xcls_view() );
161         }
162
163         // userdefined functions 
164         public bool save() {
165             
166                  if (_this.active_path.length  < 1 ) {
167                     return true;
168                  }
169                  
170                  var str = Editor.buffer.toString();
171                  
172                  if (!Editor.buffer.checkSyntax()) {
173                      print("check syntax failed");
174                      //this.get('/StandardErrorDialog').show("Fix errors in code and save.."); 
175                      return false;
176                  }
177                  
178                  // LeftPanel.model.changed(  str , false);
179                  _this.dirty = false;
180                  _this.save_button.sensitive = false;
181                  return true;
182             }
183     }
184     public class Xcls_view : Gtk.SourceView
185     {
186
187             // my vars
188
189             // ctor 
190         public Xcls_view()
191         {
192             _this.view = this;
193
194             // my vars
195
196             // set gobject values
197             this.auto_indent = true;
198             this.indent_width = 4;
199             this.insert_spaces_instead_of_tabs = true;
200             this.show_line_numbers = true;
201             this.set_buffer (  new Xcls_buffer() );
202
203             // init method 
204                 var description =   Pango.FontDescription.from_string("monospace");
205                 description.set_size(8000);
206                 this.override_font(description);
207
208             // listeners 
209             this.key_release_event.connect( (event) => {
210                 
211                 if (event.key.keyval == 115 && (event.key.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
212                     print("SAVE: ctrl-S  pressed");
213                     this.save();
214                     return false;
215                 }
216                // print(event.key.keyval)
217                 
218                 return false;
219             
220             } );
221         }
222
223         // userdefined functions 
224         public void save() {
225             
226                 Editor.RightEditor.save();
227             }
228     }
229     public class Xcls_buffer : Gtk.SourceBuffer
230     {
231
232             // my vars
233
234             // ctor 
235         public Xcls_buffer()
236         {
237             _this.buffer = this;
238
239             // my vars
240
241             // set gobject values
242
243             // listeners 
244             this.changed.connect( () => {
245                 // check syntax??
246                     if(this.checkSyntax()) {
247                     Editor.save_button.sensitive = true;
248                 }
249                // print("EDITOR CHANGED");
250                 Editor.dirty = true;
251             
252                 // this.get('/LeftPanel.model').changed(  str , false);
253                 return ;
254             } );
255         }
256
257         // userdefined functions 
258         public bool checkSyntax() { 
259                 // we could try running valac... ?? but it's a bit confusing..
260                 return true;
261             
262             }
263         public string toString() {
264                 
265                 Gtk.TextIter s;
266                 Gtk.TextIter e;
267                 this.get_start_iter(out s);
268                 this.get_end_iter(out e);
269                 var ret = this.get_text(s,e,true);
270                 //print("TO STRING? " + ret);
271                 return ret;
272             }
273     }
274 }