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 string activeEditor;
23 public int pos_root_x;
24 public int pos_root_y;
27 public JsRender.JsRender file;
30 public signal void save ();
31 public JsRender.Node node;
37 this.el = new Gtk.VBox( false, 0 );
40 this.activeEditor = "";
49 var child_0 = new Xcls_HBox2( _this );
51 this.el.pack_start ( child_0.el , false,true );
52 var child_1 = new Xcls_RightEditor( _this );
54 this.el.add ( child_1.el );
57 // user defined functions
58 public bool saveContents () {
68 var str = _this.buffer.toString();
73 if (!_this.buffer.checkSyntax()) {
74 print("check syntax failed");
75 //this.get('/StandardErrorDialog').show("Fix errors in code and save..");
79 // LeftPanel.model.changed( str , false);
81 _this.save_button.el.sensitive = false;
86 // find the text for the node..
87 if (ptype == "listener") {
88 this.node.listeners.set(key,str);
91 this.node.props.set(key,str);
102 public void show (JsRender.JsRender file, JsRender.Node node, string ptype, string key)
110 // find the text for the node..
111 if (ptype == "listener") {
112 val = node.listeners.get(key);
115 val = node.props.get(key);
118 this.key_edit.el.text = key;
121 public class Xcls_HBox2 : Object
124 private Editor _this;
130 public Xcls_HBox2(Editor _owner )
133 this.el = new Gtk.HBox( false, 0 );
137 // set gobject values
138 var child_0 = new Xcls_save_button( _this );
140 this.el.pack_start ( child_0.el , false,false );
141 var child_1 = new Xcls_key_edit( _this );
143 this.el.pack_end ( child_1.el , true,true );
146 // user defined functions
148 public class Xcls_save_button : Object
150 public Gtk.Button el;
151 private Editor _this;
157 public Xcls_save_button(Editor _owner )
160 _this.save_button = this;
161 this.el = new Gtk.Button();
165 // set gobject values
166 this.el.label = "Save";
169 this.el.clicked.connect( () => {
170 _this.saveContents();
174 // user defined functions
176 public class Xcls_key_edit : Object
179 private Editor _this;
185 public Xcls_key_edit(Editor _owner )
188 _this.key_edit = this;
189 this.el = new Gtk.Entry();
193 // set gobject values
196 // user defined functions
198 public class Xcls_RightEditor : Object
200 public Gtk.ScrolledWindow el;
201 private Editor _this;
207 public Xcls_RightEditor(Editor _owner )
210 _this.RightEditor = this;
211 this.el = new Gtk.ScrolledWindow( null, null );
215 // set gobject values
216 var child_0 = new Xcls_view( _this );
218 this.el.add ( child_0.el );
222 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
225 // user defined functions
227 public class Xcls_view : Object
229 public Gtk.SourceView el;
230 private Editor _this;
236 public Xcls_view(Editor _owner )
240 this.el = new Gtk.SourceView();
244 // set gobject values
245 this.el.auto_indent = true;
246 this.el.indent_width = 4;
247 this.el.show_line_marks = true;
248 this.el.insert_spaces_instead_of_tabs = true;
249 this.el.show_line_numbers = true;
250 this.el.highlight_current_line = true;
251 var child_0 = new Xcls_buffer( _this );
253 this.el.set_buffer ( child_0.el );
257 var description = Pango.FontDescription.from_string("monospace");
258 description.set_size(8000);
259 this.el.override_font(description);
261 var attrs = new Gtk.SourceMarkAttributes();
262 var pink = new Gdk.RGBA();
263 pink.parse ( "pink");
264 attrs.set_background ( pink);
265 attrs.set_icon_name ( "process-stop");
266 attrs.query_tooltip_text.connect(( mark) => {
267 print("tooltip query? %s\n", mark.name);
271 this.el.set_mark_attributes ("error", attrs, 1);
274 this.el.key_release_event.connect( (event) => {
276 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
277 print("SAVE: ctrl-S pressed");
278 _this.saveContents();
281 // print(event.key.keyval)
288 // user defined functions
289 public void load (string str) {
291 // show the help page for the active node..
292 //this.get('/Help').show();
295 // this.get('/BottomPane').el.set_current_page(0);
296 this.el.get_buffer().set_text(str, str.length);
297 var lm = Gtk.SourceLanguageManager.get_default();
299 var lang = _this.file.language;
300 //?? is javascript going to work as js?
302 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
303 var buf = this.el.get_buffer();
305 /* -- what does all this do? */
307 var cursor = buf.get_mark("insert");
309 buf.get_iter_at_mark(out iter, cursor);
311 iter.set_line_offset(4);
312 buf.move_mark(cursor, iter);
315 cursor = buf.get_mark("selection_bound");
316 //iter= new Gtk.TextIter;
317 buf.get_iter_at_mark(out iter, cursor);
319 iter.set_line_offset(4);
320 buf.move_mark(cursor, iter);
325 this.el.grab_focus();
326 _this.save_button.el.sensitive = false;
329 public class Xcls_buffer : Object
331 public Gtk.SourceBuffer el;
332 private Editor _this;
336 public bool check_queued;
337 public int error_line;
338 public bool check_running;
341 public Xcls_buffer(Editor _owner )
345 this.el = new Gtk.SourceBuffer( null );
348 this.check_queued = false;
349 this.error_line = -1;
350 this.check_running = false;
352 // set gobject values
355 this.el.changed.connect( () => {
358 _this.save_button.el.sensitive = true;
359 print("EDITOR CHANGED");
364 // this.get('/LeftPanel.model').changed( str , false);
369 // user defined functions
370 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
371 this.error_line = validate_res.size;
373 if (this.error_line < 1) {
376 var tlines = this.el.get_line_count ();
378 var valiter = validate_res.map_iterator();
379 while (valiter.next()) {
381 // print("get inter\n");
382 var eline = valiter.get_key();
383 if (eline > tlines) {
386 this.el.get_iter_at_line( out iter, eline);
387 //print("mark line\n");
388 this.el.create_source_mark(valiter.get_value(), "error", iter);
392 public string toString () {
396 this.el.get_start_iter(out s);
397 this.el.get_end_iter(out e);
398 var ret = this.el.get_text(s,e,true);
399 //print("TO STRING? " + ret);
402 public bool checkSyntax () {
404 if (this.check_running) {
405 print("Check is running\n");
406 if (this.check_queued) {
407 print("Check is already queued");
410 this.check_queued = true;
411 print("Adding queued Check ");
412 GLib.Timeout.add_seconds(1, () => {
413 this.check_queued = false;
424 var p = Palete.factory(_this.file.xtype);
426 var str = this.toString();
430 if (this.error_line > 0) {
433 this.el.get_bounds (out start, out end);
435 this.el.remove_source_marks (start, end, null);
438 if (str.length < 1) {
439 print("checkSyntax - empty string?\n");
442 this.check_running = true;
445 if (_this.file.language == "js") {
446 this.check_running = false;
447 print("calling validate javascript\n");
448 return this.highlightErrors(p.validateJavascript(
458 print("calling validate vala\n");
466 this.check_running = false;
467 this.highlightErrorsJson(obj);
469 //var validate_res = p.validateVala.end(res);
470 //this.highlightErrors(validate_res);
478 //print("done mark line\n");
480 return true; // at present allow saving - even if it's invalid..
482 public bool highlightErrorsJson (Json.Object obj) {
484 if (!obj.has_member("ERR")) {
485 print("Return has no errors\n");
488 var err = obj.get_object_member("ERR");
490 if (!err.has_member(_this.file.path)) {
491 print("File path has no errors\n");
495 var lines = err.get_object_member(_this.file.path);
498 if (obj.has_member("line_offset")) {
499 offset = (int)obj.get_int_member("line_offset");
503 var tlines = this.el.get_line_count ();
505 lines.foreach_member((obj, line, node) => {
508 // print("get inter\n");
509 var eline = int.parse(line) - offset;
510 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
513 if (eline > tlines || eline < 0) {
516 this.el.get_iter_at_line( out iter, eline);
517 //print("mark line\n");
519 var ar = lines.get_array_member(line);
520 for (var i = 0 ; i < ar.get_length(); i++) {
521 msg += (msg.length > 0) ? "\n" : "";
522 msg += ar.get_string_element(i);
526 this.el.create_source_mark(msg, "error", iter);