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.Box( Gtk.Orientation.VERTICAL, 0 );
42 this.activeEditor = "";
51 this.el.homogeneous = false;
52 var child_0 = new Xcls_Box2( _this );
54 this.el.pack_start ( child_0.el , false,true );
55 var child_1 = new Xcls_RightEditor( _this );
57 this.el.pack_end ( child_1.el , true,true );
60 // user defined functions
61 public bool saveContents () {
64 if (_this.file == null) {
73 var str = _this.buffer.toString();
75 _this.buffer.checkSyntax();
79 // LeftPanel.model.changed( str , false);
81 _this.save_button.el.sensitive = false;
83 // find the text for the node..
84 if (_this.file.xtype != "PlainFile") {
85 if (ptype == "listener") {
86 this.node.listeners.set(key,str);
89 this.node.props.set(key,str);
92 _this.file.setSource( str );
101 public void show (JsRender.JsRender file, JsRender.Node? node, string ptype, string key)
109 if (file.xtype != "PlainFile") {
115 // find the text for the node..
116 if (ptype == "listener") {
117 val = node.listeners.get(key);
120 val = node.props.get(key);
126 this.view.load( file.toSource() );
129 this.key_edit.el.text = key;
132 public class Xcls_Box2 : Object
135 private Editor _this;
141 public Xcls_Box2(Editor _owner )
144 this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
148 // set gobject values
149 this.el.homogeneous = false;
150 var child_0 = new Xcls_save_button( _this );
152 this.el.pack_start ( child_0.el , false,false );
153 var child_1 = new Xcls_key_edit( _this );
155 this.el.pack_end ( child_1.el , true,true );
158 // user defined functions
160 public class Xcls_save_button : Object
162 public Gtk.Button el;
163 private Editor _this;
169 public Xcls_save_button(Editor _owner )
172 _this.save_button = this;
173 this.el = new Gtk.Button();
177 // set gobject values
178 this.el.label = "Save";
181 this.el.clicked.connect( () => {
182 _this.saveContents();
186 // user defined functions
189 public class Xcls_key_edit : Object
192 private Editor _this;
198 public Xcls_key_edit(Editor _owner )
201 _this.key_edit = this;
202 this.el = new Gtk.Entry();
206 // set gobject values
209 // user defined functions
213 public class Xcls_RightEditor : Object
215 public Gtk.ScrolledWindow el;
216 private Editor _this;
222 public Xcls_RightEditor(Editor _owner )
225 _this.RightEditor = this;
226 this.el = new Gtk.ScrolledWindow( null, null );
230 // set gobject values
231 var child_0 = new Xcls_view( _this );
233 this.el.add ( child_0.el );
237 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
240 // user defined functions
242 public class Xcls_view : Object
244 public Gtk.SourceView el;
245 private Editor _this;
251 public Xcls_view(Editor _owner )
255 this.el = new Gtk.SourceView();
259 // set gobject values
260 this.el.auto_indent = true;
261 this.el.indent_width = 4;
262 this.el.show_line_marks = true;
263 this.el.insert_spaces_instead_of_tabs = true;
264 this.el.show_line_numbers = true;
265 this.el.highlight_current_line = true;
266 var child_0 = new Xcls_buffer( _this );
268 this.el.set_buffer ( child_0.el );
272 var description = Pango.FontDescription.from_string("monospace");
273 description.set_size(8000);
274 this.el.override_font(description);
278 var attrs = new Gtk.SourceMarkAttributes();
279 var pink = Gdk.RGBA();
280 pink.parse ( "pink");
281 attrs.set_background ( pink);
282 attrs.set_icon_name ( "process-stop");
283 attrs.query_tooltip_text.connect(( mark) => {
284 //print("tooltip query? %s\n", mark.name);
288 this.el.set_mark_attributes ("ERR", attrs, 1);
290 var wattrs = new Gtk.SourceMarkAttributes();
291 var blue = Gdk.RGBA();
292 blue.parse ( "#ABF4EB");
293 wattrs.set_background ( blue);
294 wattrs.set_icon_name ( "process-stop");
295 wattrs.query_tooltip_text.connect(( mark) => {
296 //print("tooltip query? %s\n", mark.name);
300 this.el.set_mark_attributes ("WARN", wattrs, 1);
304 var dattrs = new Gtk.SourceMarkAttributes();
305 var purple = Gdk.RGBA();
306 purple.parse ( "#EEA9FF");
307 dattrs.set_background ( purple);
308 dattrs.set_icon_name ( "process-stop");
309 dattrs.query_tooltip_text.connect(( mark) => {
310 //print("tooltip query? %s\n", mark.name);
314 this.el.set_mark_attributes ("DEPR", dattrs, 1);
317 this.el.key_release_event.connect( (event) => {
319 if (event.keyval == 115 && (event.state & Gdk.ModifierType.CONTROL_MASK ) > 0 ) {
320 print("SAVE: ctrl-S pressed");
321 _this.saveContents();
324 // print(event.key.keyval)
331 // user defined functions
332 public void load (string str) {
334 // show the help page for the active node..
335 //this.get('/Help').show();
338 // this.get('/BottomPane').el.set_current_page(0);
339 var buf = (Gtk.SourceBuffer)this.el.get_buffer();
340 buf.set_text(str, str.length);
341 buf.set_undo_manager(null);
343 var lm = Gtk.SourceLanguageManager.get_default();
345 if (_this.file != null) {
346 lang = _this.file.language;
348 //?? is javascript going to work as js?
350 ((Gtk.SourceBuffer)(this.el.get_buffer())) .set_language(lm.get_language(lang));
354 this.el.grab_focus();
355 _this.save_button.el.sensitive = false;
358 public class Xcls_buffer : Object
360 public Gtk.SourceBuffer el;
361 private Editor _this;
365 public bool check_queued;
366 public int error_line;
367 public bool check_running;
370 public Xcls_buffer(Editor _owner )
374 this.el = new Gtk.SourceBuffer( null );
377 this.check_queued = false;
378 this.error_line = -1;
379 this.check_running = false;
381 // set gobject values
384 this.el.changed.connect( () => {
387 _this.save_button.el.sensitive = true;
388 print("EDITOR CHANGED");
393 // this.get('/LeftPanel.model').changed( str , false);
398 // user defined functions
399 public bool highlightErrors ( Gee.HashMap<int,string> validate_res) {
401 this.error_line = validate_res.size;
403 if (this.error_line < 1) {
406 var tlines = this.el.get_line_count ();
408 var valiter = validate_res.map_iterator();
409 while (valiter.next()) {
411 // print("get inter\n");
412 var eline = valiter.get_key();
413 if (eline > tlines) {
416 this.el.get_iter_at_line( out iter, eline);
417 //print("mark line\n");
418 this.el.create_source_mark(valiter.get_value(), "ERR", iter);
422 public string toString () {
426 this.el.get_start_iter(out s);
427 this.el.get_end_iter(out e);
428 var ret = this.el.get_text(s,e,true);
429 //print("TO STRING? " + ret);
432 public bool checkSyntax () {
434 if (this.check_running) {
435 print("Check is running\n");
436 if (this.check_queued) {
437 print("Check is already queued");
440 this.check_queued = true;
441 print("Adding queued Check ");
442 GLib.Timeout.add_seconds(1, () => {
443 this.check_queued = false;
452 var str = this.toString();
455 if (this.error_line > 0) {
458 this.el.get_bounds (out start, out end);
460 this.el.remove_source_marks (start, end, null);
462 if (str.length < 1) {
463 print("checkSyntax - empty string?\n");
467 if (_this.file.xtype == "PlainFile") {
469 // assume it's gtk...
470 this.check_running = true;
472 _this.window.windowstate.valasource.checkPlainFileSpawn(
479 if (_this.file == null) {
482 var p = Palete.factory(_this.file.xtype);
486 this.check_running = true;
489 if (_this.file.language == "js") {
490 this.check_running = false;
491 print("calling validate javascript\n");
492 return this.highlightErrors(p.validateJavascript(
503 print("calling validate vala\n");
504 // clear the buttons.
508 _this.window.windowstate,
520 //print("done mark line\n");
522 return true; // at present allow saving - even if it's invalid..
524 public bool highlightErrorsJson (string type, Json.Object obj) {
527 this.el.get_bounds (out start, out end);
529 this.el.remove_source_marks (start, end, type);
532 // we should highlight other types of errors..
534 if (!obj.has_member(type)) {
535 print("Return has no errors\n");
539 if (_this.window.windowstate.state != WindowState.State.CODEONLY &&
540 _this.window.windowstate.state != WindowState.State.CODE
546 var err = obj.get_object_member(type);
549 if (_this.file == null) {
553 var valafn = _this.file.path;
555 if (_this.file.xtype != "PlainFile") {
562 var regex = new Regex("\\.bjs$");
566 valafn = regex.replace(_this.file.path,_this.file.path.length , 0 , ".vala");
567 } catch (GLib.RegexError e) {
574 if (!err.has_member(valafn)) {
575 print("File path has no errors\n");
579 var lines = err.get_object_member(valafn);
582 if (obj.has_member("line_offset")) {
583 offset = (int)obj.get_int_member("line_offset") + 1;
589 var tlines = this.el.get_line_count () +1;
591 lines.foreach_member((obj, line, node) => {
594 // print("get inter\n");
595 var eline = int.parse(line) - offset;
596 print("GOT ERROR on line %s -- converted to %d\n", line,eline);
599 if (eline > tlines || eline < 0) {
602 this.el.get_iter_at_line( out iter, eline);
603 //print("mark line\n");
604 var msg = "Line: %d".printf(eline+1);
605 var ar = lines.get_array_member(line);
606 for (var i = 0 ; i < ar.get_length(); i++) {
607 msg += (msg.length > 0) ? "\n" : "";
608 msg += ar.get_string_element(i);
612 this.el.create_source_mark(msg, type, iter);