3 public class Editor : Object
8 public static Editor singleton()
10 if (_Editor == null) {
11 _Editor= new Editor();
15 public Xcls_save_button save_button;
16 public Xcls_key_edit key_edit;
17 public Xcls_RightEditor RightEditor;
18 public Xcls_view view;
19 public Xcls_buffer buffer;
22 public Xcls_MainWindow window;
23 public string activeEditor;
24 public int pos_root_x;
25 public int pos_root_y;
28 public JsRender.JsRender file;
31 public signal void save ();
32 public JsRender.Node node;
38 this.el = new Gtk.VBox( false, 0 );
42 this.activeEditor = "";
51 var child_0 = new Xcls_HBox2( _this );
53 this.el.pack_start ( child_0.el , false,true );
54 var child_1 = new Xcls_RightEditor( _this );
56 this.el.add ( child_1.el );
59 // user defined functions
60 public bool saveContents () {
70 var str = _this.buffer.toString();
75 if (!_this.buffer.checkSyntax()) {
76 print("check syntax failed");
77 //this.get('/StandardErrorDialog').show("Fix errors in code and save..");
81 // LeftPanel.model.changed( str , false);
83 _this.save_button.el.sensitive = false;
88 // find the text for the node..
89 if (ptype == "listener") {
90 this.node.listeners.set(key,str);
93 this.node.props.set(key,str);
104 public void show (JsRender.JsRender file, JsRender.Node node, string ptype, string key)
112 // find the text for the node..
113 if (ptype == "listener") {
114 val = node.listeners.get(key);
117 val = node.props.get(key);
120 this.key_edit.el.text = key;
123 public class Xcls_HBox2 : Object
126 private Editor _this;
132 public Xcls_HBox2(Editor _owner )
135 this.el = new Gtk.HBox( false, 0 );
139 // set gobject values
140 var child_0 = new Xcls_save_button( _this );
142 this.el.pack_start ( child_0.el , false,false );
143 var child_1 = new Xcls_key_edit( _this );
145 this.el.pack_end ( child_1.el , true,true );
148 // user defined functions
150 public class Xcls_save_button : Object
152 public Gtk.Button el;
153 private Editor _this;
159 public Xcls_save_button(Editor _owner )
162 _this.save_button = this;
163 this.el = new Gtk.Button();
167 // set gobject values
168 this.el.label = "Save";
171 this.el.clicked.connect( () => {
172 _this.saveContents();
176 // user defined functions
178 public class Xcls_key_edit : Object
181 private Editor _this;
187 public Xcls_key_edit(Editor _owner )
190 _this.key_edit = this;
191 this.el = new Gtk.Entry();
195 // set gobject values
198 // user defined functions
200 public class Xcls_RightEditor : Object
202 public Gtk.ScrolledWindow el;
203 private Editor _this;
209 public Xcls_RightEditor(Editor _owner )
212 _this.RightEditor = this;
213 this.el = new Gtk.ScrolledWindow( null, null );
217 // set gobject values
218 var child_0 = new Xcls_view( _this );
220 this.el.add ( child_0.el );
224 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
227 // user defined functions
229 public class Xcls_view : Object
231 public Gtk.SourceView el;
232 private Editor _this;
238 public Xcls_view(Editor _owner )
242 this.el = new Gtk.SourceView();
246 // set gobject values
247 this.el.auto_indent = true;
248 this.el.indent_width = 4;
249 this.el.show_line_marks = true;
250 this.el.insert_spaces_instead_of_tabs = true;
251 this.el.show_line_numbers = true;
252 this.el.highlight_current_line = true;
253 var child_0 = new Xcls_buffer( _this );
255 this.el.set_buffer ( child_0.el );
259 var description = Pango.FontDescription.from_string("monospace");
260 description.set_size(8000);
261 this.el.override_font(description);
263 var attrs = new Gtk.SourceMarkAttributes();
264 var pink = new Gdk.RGBA();
265 pink.parse ( "pink");
266 attrs.set_background ( pink);
267 attrs.set_icon_name ( "process-stop");
268 attrs.query_tooltip_text.connect(( mark) => {
269 //print("tooltip query? %s\n", mark.name);
273 this.el.set_mark_attributes ("error", attrs, 1);
276 this.el.key_release_event.connect( (event) => {
278 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
279 print("SAVE: ctrl-S pressed");
280 _this.saveContents();
283 // print(event.key.keyval)
290 // user defined functions
291 public void load (string str) {
293 // show the help page for the active node..
294 //this.get('/Help').show();
297 // this.get('/BottomPane').el.set_current_page(0);
298 this.el.get_buffer().set_text(str, str.length);
299 var lm = Gtk.SourceLanguageManager.get_default();
301 var lang = _this.file.language;
302 //?? is javascript going to work as js?
304 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
305 var buf = this.el.get_buffer();
307 /* -- what does all this do? */
309 var cursor = buf.get_mark("insert");
311 buf.get_iter_at_mark(out iter, cursor);
313 iter.set_line_offset(4);
314 buf.move_mark(cursor, iter);
317 cursor = buf.get_mark("selection_bound");
318 //iter= new Gtk.TextIter;
319 buf.get_iter_at_mark(out iter, cursor);
321 iter.set_line_offset(4);
322 buf.move_mark(cursor, iter);
327 this.el.grab_focus();
328 _this.save_button.el.sensitive = false;
331 public class Xcls_buffer : Object
333 public Gtk.SourceBuffer el;
334 private Editor _this;
338 public bool check_queued;
339 public int error_line;
340 public bool check_running;
343 public Xcls_buffer(Editor _owner )
347 this.el = new Gtk.SourceBuffer( null );
350 this.check_queued = false;
351 this.error_line = -1;
352 this.check_running = false;
354 // set gobject values
357 this.el.changed.connect( () => {
360 _this.save_button.el.sensitive = true;
361 print("EDITOR CHANGED");
366 // this.get('/LeftPanel.model').changed( str , false);
371 // user defined functions
372 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
373 this.error_line = validate_res.size;
375 if (this.error_line < 1) {
378 var tlines = this.el.get_line_count ();
380 var valiter = validate_res.map_iterator();
381 while (valiter.next()) {
383 // print("get inter\n");
384 var eline = valiter.get_key();
385 if (eline > tlines) {
388 this.el.get_iter_at_line( out iter, eline);
389 //print("mark line\n");
390 this.el.create_source_mark(valiter.get_value(), "error", iter);
394 public string toString () {
398 this.el.get_start_iter(out s);
399 this.el.get_end_iter(out e);
400 var ret = this.el.get_text(s,e,true);
401 //print("TO STRING? " + ret);
404 public bool checkSyntax () {
406 if (this.check_running) {
407 print("Check is running\n");
408 if (this.check_queued) {
409 print("Check is already queued");
412 this.check_queued = true;
413 print("Adding queued Check ");
414 GLib.Timeout.add_seconds(1, () => {
415 this.check_queued = false;
426 var p = Palete.factory(_this.file.xtype);
428 var str = this.toString();
432 if (this.error_line > 0) {
435 this.el.get_bounds (out start, out end);
437 this.el.remove_source_marks (start, end, null);
440 if (str.length < 1) {
441 print("checkSyntax - empty string?\n");
444 this.check_running = true;
447 if (_this.file.language == "js") {
448 this.check_running = false;
449 print("calling validate javascript\n");
450 return this.highlightErrors(p.validateJavascript(
461 print("calling validate vala\n");
462 // clear the buttons.
463 _this.window.statusbar_errors.el.hide();
464 _this.window.statusbar_errors.el.hide();
465 _this.window.statusbar_errors.el.hide();
476 this.check_running = false;
477 this.highlightErrorsJson(obj);
479 //var validate_res = p.validateVala.end(res);
480 //this.highlightErrors(validate_res);
488 //print("done mark line\n");
490 return true; // at present allow saving - even if it's invalid..
492 public bool highlightErrorsJson (Json.Object obj) {
495 this.el.get_bounds (out start, out end);
497 this.el.remove_source_marks (start, end, null);
499 if (!obj.has_member("ERR")) {
500 print("Return has no errors\n");
503 var err = obj.get_object_member("ERR");
505 if (!err.has_member(_this.file.path)) {
506 print("File path has no errors\n");
510 var lines = err.get_object_member(_this.file.path);
513 if (obj.has_member("line_offset")) {
514 offset = (int)obj.get_int_member("line_offset");
518 var tlines = this.el.get_line_count ();
520 lines.foreach_member((obj, line, node) => {
523 // print("get inter\n");
524 var eline = int.parse(line) - offset;
525 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
528 if (eline > tlines || eline < 0) {
531 this.el.get_iter_at_line( out iter, eline);
532 //print("mark line\n");
533 var msg = "Line: %d".printf(eline+1);
534 var ar = lines.get_array_member(line);
535 for (var i = 0 ; i < ar.get_length(); i++) {
536 msg += (msg.length > 0) ? "\n" : "";
537 msg += ar.get_string_element(i);
541 this.el.create_source_mark(msg, "error", iter);