Fix #7214 - move edit code into popover
[roobuilder] / src / Builder4 / WindowState.vala
1 /**
2  * as state management is a bit too complicated inside the builder
3  * it's better to seperate this into this class
4  * 
5  * This class has references to all the Class instances that make up the window..
6  * 
7  */
8 public class WindowState : Object 
9 {
10         public Xcls_MainWindow win;
11  
12         public enum State {
13                 NONE,
14                 PREVIEW,
15                 //OBJECT,
16                 //PROP,
17                 //LISTENER,
18                 //CODE,    // code editor.
19                 CODEONLY,
20                 FILES //,
21                  
22
23         }
24
25         public State state = State.NONE;
26
27         public bool children_loaded = false;
28  
29         public Project.Project project;
30         public JsRender.JsRender file;
31           
32         public Xcls_WindowLeftTree  left_tree;
33         public Xcls_PopoverAddProp   add_props;
34         public Xcls_LeftProps       left_props;
35         public Xcls_RooProjectSettings roo_projectsettings_pop;
36         public Xcls_ValaProjectSettingsPopover  vala_projectsettings_pop;
37         public Xcls_PopoverAddObject     rightpalete;
38         public Xcls_PopoverEditor               code_editor_popover;
39         public Editor                                    code_editor_tab; 
40         public Xcls_WindowRooView   window_rooview;
41         public Xcls_GtkView         window_gladeview;
42         
43         public Xcls_ClutterFiles     clutterfiles;
44
45         public Xcls_WindowLeftProjects left_projects; // can not see where this is initialized.. 
46         
47         public DialogTemplateSelect template_select; 
48         
49         public Xcls_PopoverFileDetails file_details;
50         
51         
52         public Xcls_ValaCompileResults compile_results;
53         
54         // dialogs??
55         public Xcls_DialogPluginWebkit webkit_plugin;
56         
57         
58         public Palete.ValaSource valasource; // the spawner that runs the vala compiler.
59         public Json.Object last_compile_result;
60         
61         // ctor 
62         public WindowState(Xcls_MainWindow win)
63         {
64                 this.win = win;
65                 // initialize
66
67                 // left elements..
68                 this.leftTreeInit();
69                 this.propsListInit();
70
71                 // on clutter space...
72                 this.projectEditInit();
73                 this.codeEditInit();
74                 this.codePopoverEditInit();
75                 this.projectListInit();
76                 this.fileViewInit();
77
78                 // adding stuff
79                 this.objectAddInit();
80                 this.propsAddInit();
81
82
83                 // previews...
84                 this.gtkViewInit();
85                 this.webkitViewInit();
86
87                 // dialogs
88
89                 this.fileDetailsInit();
90
91                 this.webkit_plugin = new Xcls_DialogPluginWebkit();
92                 this.template_select = new DialogTemplateSelect();
93                 this.children_loaded = true;
94                 
95                 
96                 this.valasource = new Palete.ValaSource(this.win);
97
98                 this.valasource.compiled.connect(this.showCompileResult);
99                 
100                 this.compile_results = new  Xcls_ValaCompileResults();
101                 this.compile_results.window = this.win;
102                 this.valasource.compile_output.connect(this.compile_results.addLine);
103                 
104                 this.win.statusbar_compilestatus_label.el.hide();
105                 this.win.statusbar_run.el.hide();
106                 this.win.search_results.el.hide();
107         }
108
109
110         // left tree
111
112         public void leftTreeInit()
113         {
114          
115                 this.left_tree = new Xcls_WindowLeftTree();
116                 this.left_tree.ref();
117                 this.left_tree.main_window = this.win;
118         
119                 this.win.tree.el.pack_start(this.left_tree.el,true, true,0);
120                 this.left_tree.el.show_all();
121                    
122                 this.left_tree.before_node_change.connect(() => {
123                         // if the node change is caused by the editor (code preview)
124                         if (this.left_tree.view.lastEventSource == "editor") {
125                                 return true;
126                         }
127                         return this.leftTreeBeforeChange();
128
129                 });
130                 // node selected -- only by clicking?
131                 this.left_tree.node_selected.connect((sel, source) => {
132                         if (source == "editor") {
133                                 return;
134                         }
135                         if (this.file.xtype == "Roo") { 
136                                 this.window_rooview.sourceview.nodeSelected(sel,true); // foce scroll.
137                         } else {
138                                 this.window_gladeview.sourceview.nodeSelected(sel);
139                         }
140                 });
141                 
142                 this.left_tree.node_selected.connect((sel, source) => {
143                         this.leftTreeNodeSelected(sel, source);
144                 });
145          
146                 this.left_tree.changed.connect(() => {
147                         print("LEFT TREE: Changed fired\n");
148                         this.file.save();
149                         if (this.left_tree.getActiveFile().xtype == "Roo" ) {
150                                    this.window_rooview.requestRedraw();
151                         } else {
152                                   this.window_gladeview.loadFile(this.left_tree.getActiveFile());
153                         }
154                          
155                 });
156                  
157         }
158
159         public bool leftTreeBeforeChange()
160         {
161                 // in theory code editor has to hide before tree change occurs.
162                 //if (this.state != State.CODE) {
163                         this.left_props.finish_editing();
164                         return true;
165                 //}
166
167                 //if (!this.code_editor.saveContents()) {
168                 //      return false;
169                 //}
170                 return false;
171         }
172         
173         public void leftTreeNodeSelected(JsRender.Node? sel, string source)
174         {
175                 
176                 // do we really want to flip paletes if differnt nodes are selected
177                 // showing palete should be deliberate thing..
178                  
179          
180                 print("node_selected called %s\n", (sel == null) ? "NULL" : "a value");
181
182                 if (sel == null) {
183                         this.left_props.el.hide();
184                 } 
185                 this.left_props.el.show();
186                 this.left_props.load(this.left_tree.getActiveFile(), sel);
187                 
188                 
189                 // if either of these are active.. then we should update them??
190                 
191                 this.add_props.hide(); // always hide add node/add listener if we change node.
192                 this.rightpalete.hide(); 
193                 
194                 
195                 /*
196                 switch (this.state) {
197                  
198                         case State.CODE:
199                                  this.switchState(State.PREVIEW);
200                          
201                                 break;
202                            
203                                                         
204                 }
205                 */
206  
207                  
208
209         }
210
211
212
213
214         public void propsListInit()
215         {
216         
217                 this.left_props =new Xcls_LeftProps();
218                 this.left_props.ref();
219                 this.left_props.main_window = this.win;
220                 this.win.props.el.pack_start(this.left_props.el,true, true,0);
221                 this.left_props.el.show_all();
222         
223                 this.left_props.show_editor.connect( (file, node, type,  key) => {
224                         //this.switchState(State.CODE);
225                         this.code_editor_popover.show(
226                                 this.left_props.el,
227                                 file,
228                                 node,
229                                 type,
230                                 key
231                         );
232                         
233                         
234                 });
235
236                 // not sure if this is needed - as closing the popvoer should save it.
237                 this.left_props.stop_editor.connect( () => {
238                         //if (this.state != State.CODE) {
239                         //      return true;
240                         //}
241         
242                         var ret =  this.code_editor_popover.editor.saveContents();
243                         if (!ret) {
244                                 return false;
245                         }
246                         //this.switchState(State.PREVIEW);
247                         return ret;
248                 });
249         
250                 this.left_props.changed.connect(() => {
251                         if (this.left_tree.getActiveFile().xtype == "Roo" ) {
252                                    this.window_rooview.requestRedraw();
253                         } else {
254                                   this.window_gladeview.loadFile(this.left_tree.getActiveFile());
255                         }
256                         this.left_tree.model.updateSelected();
257                         this.file.save();
258                         if (this.file.xtype=="Gtk") {
259                                 this.valasource.checkFileSpawn(this.file);
260                         }
261                 });
262         
263
264
265         }
266
267         //-------------  projects edit
268
269         public void projectEditInit()
270         {
271                 this.roo_projectsettings_pop  =new Xcls_RooProjectSettings();
272                 this.roo_projectsettings_pop.ref();  /// really?
273         
274                 this.vala_projectsettings_pop  =new Xcls_ValaProjectSettingsPopover();
275                 this.vala_projectsettings_pop.ref();
276                 this.vala_projectsettings_pop.window = this.win;
277         
278                 //((Gtk.Container)(this.win.projecteditview.el.get_widget())).add(this.projectsettings.el);
279  
280  
281                 this.roo_projectsettings_pop.buttonPressed.connect((btn) => {
282                         // in theory active file can only be rooo...
283                          if (this.left_tree.getActiveFile().xtype == "Roo" ) {
284                                 if (btn == "save") {
285                                         this.window_rooview.view.renderJS(true);
286                                         this.roo_projectsettings_pop.el.hide();
287                                 }
288                                 if (btn == "apply") {
289                                         this.window_rooview.view.renderJS(true);
290                                         return;
291                                 }
292                         } else {
293                                 // do nothing for gtk..
294                         }
295                         if (btn == "save" || btn == "apply") {
296                                 this.win.project.save();
297                  
298                         }
299                         //this.switchState (State.PREVIEW); 
300                          
301                  });
302
303         }
304         public void projectPopoverShow(Gtk.Widget btn)
305         {
306                 var xtype = "";
307         var  pr = this.project;
308         var active_file = this.left_tree.getActiveFile() ;
309         if (active_file != null) {
310             xtype = active_file.xtype;
311         } else {
312             // we might be on the file brower..
313             pr = this.left_projects.getSelectedProject();        
314             if (pr != null) {
315                 xtype = pr.xtype;
316             }
317         }   
318         if (xtype == "") {
319             return;
320         }
321         if (xtype == "Roo" ) {
322                         this.roo_projectsettings_pop.show(btn,pr);
323                         return;
324                 }
325
326                 // gtk..
327                 this.vala_projectsettings_pop.show(btn,(Project.Gtk)pr);
328         
329         }
330         
331         
332         // ----------- object adding
333         public void objectAddInit()
334         {
335
336                 this.rightpalete  = new Xcls_PopoverAddObject();
337                 this.rightpalete.mainwindow = this.win;
338                 this.rightpalete.ref();  /// really?
339                 /*((Gtk.Container)(this.win.objectview.el.get_widget())).add(this.rightpalete.el);
340  
341
342                 var stage = this.win.objectview.el.get_stage();
343                 stage.set_background_color(  Clutter.Color.from_string("#000"));
344                  */
345         }
346         
347         // -----------  properties adding list...
348         // listener uses the properties 
349         public void propsAddInit()
350         {
351         // Add properties
352                 this.add_props  = new Xcls_PopoverAddProp();
353                 this.add_props.mainwindow = this.win;
354                 this.add_props.ref();  /// really?
355                 // don't need to add it..
356                 //((Gtk.Container)(this.win.addpropsview.el.get_widget())).add(this.add_props.el);
357  
358
359                 //var  stage = this.win.addpropsview.el.get_stage();
360                 //stage.set_background_color(  Clutter.Color.from_string("#000"));
361
362
363                 this.add_props.select.connect( (key,type,skel, etype) => {
364                          
365                         this.left_props.addProp(etype, key, skel, type);
366                 });
367
368         }
369         public void propsAddShow()
370         {
371
372         }
373         public void propsAddHide()
374         {
375         
376         }
377
378  
379         // ----------- Add / Edit listener
380         // listener uses the properties 
381         //public void listenerInit()     { }
382         public void listenerShow()
383         {
384
385         }
386         public void listenerHide()
387         {
388         
389         }
390
391         // -------------- codeEditor
392
393         public void codeEditInit()
394         {
395                 this.code_editor_tab  = new  Editor();
396                 //this.code_editor.ref();  /// really?
397                 ((Gtk.Container)(this.win.codeeditview.el.get_widget())).add(this.code_editor_tab.el);
398                 
399                 this.code_editor_tab.window = this.win;
400  
401
402                 var stage = this.win.codeeditview.el.get_stage();
403                 stage.set_background_color(  Clutter.Color.from_string("#000"));
404                 // editor.save...
405
406                 this.code_editor_tab.save.connect( () => {
407                         this.file.save();
408                         this.left_tree.model.updateSelected();
409                         if (this.left_tree.getActiveFile().xtype == "Roo" ) {
410                                    this.window_rooview.requestRedraw();
411                         } else {
412                                   this.window_gladeview.loadFile(this.left_tree.getActiveFile());
413                         }
414                          // we do not need to call spawn... - as it's already called by the editor?
415                          
416                 });
417                 
418         }
419         public void codePopoverEditInit()
420         {
421                 this.code_editor_popover  = new  Xcls_PopoverEditor();
422                 //this.code_editor.ref();  /// really?
423                  
424                 this.code_editor_popover.setMainWindow( this.win);
425   
426                 this.code_editor_popover.editor.save.connect( () => {
427                         this.file.save();
428                         this.left_tree.model.updateSelected();
429                         if (this.left_tree.getActiveFile().xtype == "Roo" ) {
430                                    this.window_rooview.requestRedraw();
431                         } else {
432                                   this.window_gladeview.loadFile(this.left_tree.getActiveFile());
433                         }
434                          // we do not need to call spawn... - as it's already called by the editor?
435                          
436                 });
437                 
438         }
439         // ----------- list of projects on left
440         public void  projectListInit() 
441         {
442
443                 this.left_projects = new Xcls_WindowLeftProjects();
444                  this.left_projects.ref();
445                  this.win.leftpane.el.pack_start(this.left_projects.el,true, true,0);
446                  this.left_projects.el.show_all();
447                  this.left_projects.project_selected.connect((proj) => {
448                         this.buttonsShowHide();
449                         proj.scanDirs();
450                         this.clutterfiles.loadProject(proj);
451                 
452                  });
453
454         }
455         // ----------- file view
456
457         public void fileViewInit()
458         {
459                 var stage = this.win.rooview.el.get_stage(); // seems odd... 
460                 this.clutterfiles = new Xcls_ClutterFiles();
461                 this.clutterfiles.ref();
462                 stage.add_child(this.clutterfiles.el);
463                 this.clutterfiles.el.show();
464
465
466                 this.clutterfiles.open.connect((file) => { 
467                         this.fileViewOpen(file);
468                 });
469                 this.clutterfiles.el.transitions_completed.connect(() => {
470                         if (this.state == State.FILES) {
471                                 this.win.rooview.el.hide();
472                         } else {
473                                 this.clutterfiles.el.hide();
474                         }
475                         
476                         
477                 });
478
479         }
480  
481         public void fileDetailsInit()
482         {
483                 this.file_details = new Xcls_PopoverFileDetails();
484                 this.file_details.mainwindow = this.win;
485                 // force it modal to the main window..
486                 
487                 this.file_details.success.connect((project,file) =>
488                 {
489                         this.fileViewOpen(file);
490                 });
491
492         }
493         
494         public void fileViewOpen(JsRender.JsRender file, int line = -1)
495         {
496                 this.win.project = file.project;
497                 this.project = file.project;
498                 this.file = file;
499                 
500                 
501                 if (file.xtype == "PlainFile") {
502                         this.switchState (State.CODEONLY); 
503                         file.loadItems();
504                         this.code_editor_tab.show(file, null, "", "");
505                         if (line> -1) {
506                                 this.code_editor_tab.scroll_to_line(line);
507                         }
508                 } else {
509                 
510                         this.switchState (State.PREVIEW); 
511                         // this triggers loadItems..
512                         this.left_tree.model.loadFile(file);
513                         if (file.project.xtype == "Gtk" && line> -1 ) {
514                                 this.window_gladeview.scroll_to_line(line);
515                         }
516
517                 }
518         
519         
520                 var ctr= ((Gtk.Container)(this.win.rooview.el.get_widget()));
521  
522         
523                 if (file.project.xtype == "Roo" ) { 
524                         ctr.foreach( (w) => { ctr.remove(w); });
525  
526                         ctr.add(this.window_rooview.el);
527  
528                         if (file.xtype != "PlainFile") {       
529                                 this.window_rooview.loadFile(file);
530                                 this.window_rooview.el.show_all();
531                         }
532  
533                         
534
535                 } else {
536                         ctr.foreach( (w) => { ctr.remove(w); });
537
538                         ctr.add(this.window_gladeview.el);
539  
540                         if (file.xtype != "PlainFile") {    
541                                 this.window_gladeview.loadFile(file);
542                                 this.window_gladeview.el.show_all();
543                         }
544  
545                 }
546                 print("OPEN : " + file.name);
547                 if (file.xtype != "PlainFile") {    
548                         this.win.editpane.el.set_position(this.win.editpane.el.max_position);
549                 }
550                 this.win.setTitle(file.project.name + " : " + file.name);
551                          
552
553         }
554  
555  
556          /*
557         public void fileViewOpenPlain(string fname)
558         {
559                 
560                 this.switchState (State.CODEONLY); 
561                 this.code_editor.showPlainFile(fname);
562         }
563  */
564          
565         // ---------  webkit view
566         public void webkitViewInit()
567         {
568                 this.window_rooview  =new Xcls_WindowRooView();
569                 this.window_rooview.main_window = this.win;
570                 this.window_rooview.ref();
571                 ((Gtk.Container)(this.win.rooview.el.get_widget())).add(this.window_rooview.el);
572                 this.window_rooview.el.show_all();
573
574                 var stage = this.win.rooview.el.get_stage();
575                 stage.set_background_color(  Clutter.Color.from_string("#000"));
576         }
577
578         // ------ Gtk  - view
579
580         public void gtkViewInit()
581         {
582                 this.window_gladeview  =new Xcls_GtkView();
583                 this.window_gladeview.ref();
584                 this.window_gladeview.main_window = this.win;
585         }
586         
587         public void easingSaveAll()
588         {
589                 this.win.addpropsview.el.save_easing_state();
590                 this.win.codeeditview.el.save_easing_state();
591                 this.win.objectview.el.save_easing_state();
592                 this.win.rooview.el.save_easing_state();
593                 this.clutterfiles.el.save_easing_state();
594                  
595         }
596         public void easingRestoreAll()
597         {
598                 this.win.addpropsview.el.restore_easing_state();
599                 this.win.codeeditview.el.restore_easing_state();
600                 this.win.objectview.el.restore_easing_state();
601                 this.win.rooview.el.restore_easing_state();
602                 this.clutterfiles.el.restore_easing_state();
603                 
604         }
605         
606         
607         public void showProps(Gtk.Widget btn, string sig_or_listen)
608         {
609                 var ae =  this.left_tree.getActiveElement();
610                 if (ae == null) {
611                                 return;
612                 }
613                 this.rightpalete.hide(); 
614                 
615                 this.add_props.el.show_all(); 
616                 this.add_props.show(
617                         this.win.project.palete, //Palete.factory(this.win.project.xtype), 
618                          sig_or_listen, //this.state == State.LISTENER ? "signals" : "props",
619                         ae.fqn(),
620                         btn
621                         
622                 );
623         }
624         
625         public void showAddObject(Gtk.Widget btn)
626         {
627          
628                  var n = this.left_tree.getActiveElement();
629                 this.add_props.hide();
630                 this.rightpalete.el.show_all();
631                 this.rightpalete.show(
632                         this.left_tree.getActiveFile().palete(), 
633                         n == null ? "*top" : n.fqn(),
634                         btn
635                 );
636         }
637
638                                 
639         
640         
641         public void switchState(State new_state)
642         {
643                 
644                 // if the new state and the old state are the same..
645                 
646                 if (new_state == this.state) {
647                         return;
648                 }
649                 
650                 // stop werid stuff happening
651                 
652                 if (this.state == State.FILES 
653                         //&& new_state == State.FILEPROJECT 
654                         && this.left_projects.getSelectedProject() == null) {
655                         return;
656                 }
657                 // save the easing state of everything..
658                 this.easingSaveAll();
659                 
660                 switch (this.state) {
661
662                         case State.PREVIEW:
663                                 if (this.left_tree.getActiveFile() != null) {
664                                          if (this.left_tree.getActiveFile().xtype == "Roo" ) {
665                                                  this.window_rooview.createThumb();
666                                          } else {
667                                                   this.window_gladeview.createThumb();
668                                           }
669                                 }
670                                 // normally we are going from preview to another state.
671                                 // and different windows hide the preview in differnt ways..
672                                 
673                                 break;
674                                 
675          
676                          
677                                 
678                         case State.CODEONLY:
679                                 // going from codeonly..
680                                 
681                                 // enable re-calc of canvas..
682
683                                 //this.code_editor.saveContents(); << not yet...
684
685                                 this.win.rooview.el.show(); 
686                                 this.win.leftpane.el.show();
687                                 this.win.codeeditview.el.set_scale(0.0f,0.0f);
688                         
689                          
690                         
691                             while (Gtk.events_pending()) { 
692                                         Gtk.main_iteration();
693                                 }
694                                 
695                                  // hides it completely...
696                                  
697                                 break;
698
699                  
700                   case State.FILES: // goes to preview or codeonly...
701                                 // hide files...
702                                 
703                          
704  
705                                 if (new_state == State.CODEONLY) {
706                                         this.win.rooview.el.hide();
707                                 } else {
708                                         this.win.rooview.el.show();
709                                 }
710                                 
711                                 this.win.rooview.el.set_easing_duration(1000);
712                                 this.win.rooview.el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, 0.0f);
713                                 this.win.rooview.el.set_scale(1.0f,1.0f);
714                                 this.win.rooview.el.set_pivot_point(0.5f,0.5f);
715                                 this.win.rooview.el.set_opacity(0xff);
716                                 
717                                 this.clutterfiles.el.set_easing_duration(1000);
718                                 this.clutterfiles.el.set_pivot_point(0.5f,0.5f);
719                                 this.clutterfiles.el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, -180.0f);
720                                 this.clutterfiles.el.set_opacity(0);
721  
722                            
723                                 //this.clutterfiles.el.hide();
724                                  
725
726                                 break;
727
728                                 
729                 }
730            
731                 var oldstate  =this.state;
732                 this.state = new_state;
733                 
734                 
735                                 
736                 this.buttonsShowHide();
737                 
738                 
739                 switch (this.state) {
740                         
741                         case State.PREVIEW:  // this is the default state when working...
742                                  this.win.editpane.el.show(); // holder for tree and properties..
743                                  
744                          
745                                  this.left_projects.el.hide(); 
746                                  if (oldstate != State.FILES) {
747                                         // it's handled above..
748                                         print ("changing state to preview from NOT files..");
749                                          
750  
751                                         this.win.rooview.el.set_scale(1.0f,1.0f);
752                                  }
753                            
754                                 break;
755  
756                  
757                   
758
759                         case State.CODEONLY:
760                                 // going to codeonly..
761                                 this.win.codeeditview.el.show();
762                                 // recalc canvas...
763                                 //while (Gtk.events_pending()) { 
764                                 //      Gtk.main_iteration();
765                                 //}
766                                 
767                                 this.win.leftpane.el.hide();
768                                 this.win.codeeditview.el.show();
769                                 //while (Gtk.events_pending()) { 
770                                 //      Gtk.main_iteration();
771                                 //}
772                                 
773                                 
774                                 this.code_editor_tab.el.show_all();
775                             
776                                 this.win.codeeditview.el.set_scale(1.0f,1.0f);
777                                 this.win.rooview.el.set_pivot_point(1.0f,0.5f);
778                                 break;
779
780                          
781                    case State.FILES:  // can only get here from PREVIEW (or code-only) state.. in theory..
782                                 
783    
784                                 this.win.editpane.el.hide(); // holder for tree and properties..
785                                 
786                                 this.left_projects.el.show(); 
787                                 
788                                 // rotate the preview to hidden...
789                                 this.win.rooview.el.set_easing_duration(1000);
790                                 this.win.rooview.el.set_pivot_point(0.5f,0.5f);
791                                 this.win.rooview.el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, 180.0f);
792                                 this.win.rooview.el.set_opacity(0);
793                          
794                                 
795                                 
796          
797                                 if (this.win.project != null) {
798                                         this.left_projects.selectProject(this.win.project);
799                                 }
800                          
801                                 
802                                 this.clutterfiles.el.show();
803                                  
804                                 this.clutterfiles.el.set_easing_duration(1000);
805                                 this.clutterfiles.el.set_pivot_point(0.5f,0.5f);
806                                 this.clutterfiles.el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, 0.0f);
807                                 this.clutterfiles.el.set_opacity(0xff);
808                                 
809                                  
810                                 
811                                 break;
812
813
814                 }
815                 this.resizeCanvasElements();
816                 this.easingRestoreAll();
817                 
818                 // run the animation.. - then load files...
819                 GLib.Timeout.add(500,  ()  =>{
820                          this.resizeCanvasElements();
821                          return false;
822                 });
823                         
824         }
825         
826         public int redraw_count = 0;
827         public void resizeCanvas() // called by window resize .. delays redraw
828         {
829                 var rc = this.redraw_count;        
830                 this.redraw_count = 2;
831                 if (rc == 0) {
832                         GLib.Timeout.add(100,  ()  =>{
833                                  return this.resizeCanvasQueue();
834                         });
835                 }
836         }
837         public bool  resizeCanvasQueue()
838         {
839                 //print("WindowState.resizeCanvasQueue %d\n", this.redraw_count);        
840
841                 if (this.redraw_count < 1) {
842                         return false; // should not really happen...
843                 }
844
845
846                 this.redraw_count--;
847
848                 if (this.redraw_count > 0) {
849                         return true; // do it again in 1 second...
850                 }
851                 // got down to 0 or -1....
852                 this.redraw_count = 0;
853                 this.resizeCanvasElements();
854                 return false;
855
856         }
857         public void resizeCanvasElements()
858         {
859                 Gtk.Allocation alloc;
860                 this.win.clutterembed.el.get_allocation(out alloc);
861
862            // print("WindowState.resizeCanvasElements\n");
863                 if (!this.children_loaded || this.win.clutterembed == null) {
864                         print("WindowState.resizeCanvasElements = ingnore not loaded or no clutterfiles\n");
865                         return; 
866                 }
867                 
868                 var avail = alloc.width < 50.0f ? 0 :  alloc.width - 50.0f;
869                 var palsize = avail < 300.0f ? avail : 300.0f;
870                    
871  
872                 // -------- code edit min 600
873                 
874                 var codesize = avail < 800.0f ? avail : 800.0f;
875                 
876                 
877                 //print("set code size %f\n", codesize);
878
879                         
880                 
881                 switch ( this.state) {
882                         case State.PREVIEW:
883                                  
884                                 this.win.rooview.el.set_size(alloc.width-50, alloc.height);
885                                 break;
886         
887                         case State.FILES: 
888                                 this.clutterfiles.set_size(alloc.width-50, alloc.height);
889                                 break;
890
891                   
892                                 
893                         case State.CODEONLY: 
894                                 this.win.codeeditview.el.set_size(codesize, alloc.height);
895                                 var scale = avail > 0.0f ? (avail - codesize -10 ) / avail : 0.0f;
896                                 //this.win.rooview.el.save_easing_state();
897                                 this.win.rooview.el.hide(); 
898                                 this.win.rooview.el.set_scale(scale,scale);
899                            // this.win.rooview.el.restore_easing_state();
900                                 break;  
901                          
902                 }
903         }
904
905         // -- buttons show hide.....
906
907         public void buttonsShowHide()
908         {
909                 // basically hide everything, then show the relivant..
910
911                 // top bar btns
912                 this.win.openbtn.el.hide();
913                 this.win.openbackbtn.el.hide();
914                 
915                 this.win.backbutton.el.hide();
916                 
917
918                 this.win.editfilebutton.el.hide();
919                 this.win.projecteditbutton.el.hide();
920                  
921                 
922                 this.win.objectshowbutton.el.hide(); // add objects
923                 this.win.addpropbutton.el.hide();  
924                 this.win.addlistenerbutton.el.hide(); 
925
926         
927         
928                 this.win.addprojectbutton.el.hide();
929                 this.win.addfilebutton.el.hide();
930                 this.win.delprojectbutton.el.hide();
931                 
932                 this.win.search_entry.el.hide();
933                 this.win.search_results.el.hide();
934                 switch (this.state) {
935                         
936                         case State.PREVIEW:  // this is the default state when working...
937                            
938                                 
939                                 this.win.editfilebutton.el.show();
940                                 this.win.projecteditbutton.el.show();
941                                  
942                                  
943                                 
944                                 this.win.objectshowbutton.el.show(); // add objects
945                                 this.win.addpropbutton.el.show();  
946                                 this.win.addlistenerbutton.el.show(); 
947                                 this.win.search_entry.el.show();
948                                 
949                                 this.win.openbtn.el.show();
950                                 
951                                 break;
952                         
953                         case State.CODEONLY: 
954                                 this.win.openbtn.el.show();
955                                 this.win.projecteditbutton.el.show();
956                                 this.win.search_entry.el.show();
957                                 break;
958                  
959                          
960                  
961                         case State.FILES:
962                                 if (this.left_projects.getSelectedProject() != null ) {
963                                         if (this.left_tree.getActiveFile() != null) {
964                                          
965                                                 this.win.openbackbtn.el.show();
966                                         }
967                                         this.win.addfilebutton.el.show();
968                                         this.win.search_entry.el.show();
969                                         this.win.projecteditbutton.el.show(); 
970                                 } 
971                                 
972                                          
973                                 this.win.addprojectbutton.el.show();
974                                 this.win.delprojectbutton.el.show();
975                                 
976                                 
977                                 
978                                 
979                                 break;
980                 }
981                 
982                 
983
984         }
985         
986         
987         public void showCompileResult(Json.Object obj)
988                 {
989                         // vala has finished compiling...
990                         print("vala compiled");
991                         // stop the spinner...
992  
993                         
994                         var generator = new Json.Generator ();
995                         var n  = new Json.Node(Json.NodeType.OBJECT);
996                         n.init_object(obj);
997                         generator.set_root (n);
998                         print("result :%s", generator.to_data (null));
999                         
1000                         
1001                         var buf = this.code_editor_tab.buffer;
1002                         buf.check_running = false;
1003                         var has_errors = false;
1004                                       
1005                         if (obj.has_member("ERR-TOTAL")) {
1006                                 if (obj.get_int_member("ERR-TOTAL")> 0) {
1007                                         has_errors = true;
1008                                 }
1009                                  this.win.statusbar_errors.setNotices( obj.get_object_member("ERR") , (int) obj.get_int_member("ERR-TOTAL"));
1010                         } else {
1011                                  this.win.statusbar_errors.setNotices( new Json.Object() , 0);
1012                         }    
1013                         
1014                         if (obj.has_member("WARN-TOTAL")) {
1015
1016                                  this.win.statusbar_warnings.setNotices(obj.get_object_member("WARN"), (int) obj.get_int_member("WARN-TOTAL"));
1017                         } else {
1018                                  this.win.statusbar_warnings.setNotices( new Json.Object() , 0);
1019                                  
1020                         }
1021                         if (obj.has_member("DEPR-TOTAL")) {
1022                                 
1023                                  this.win.statusbar_depricated.setNotices( obj.get_object_member("DEPR"),  (int) obj.get_int_member("DEPR-TOTAL"));
1024                                  
1025                         } else {
1026                                 this.win.statusbar_depricated.setNotices( new Json.Object(),0);
1027                         }
1028                         //if (this.state == State.CODE || this.state == State.PROJECTCODEONLY) {
1029                         if ( this.state == State.CODEONLY) {
1030                                 buf.highlightErrorsJson("ERR", obj); 
1031                                 buf.highlightErrorsJson("WARN", obj);
1032                                 buf.highlightErrorsJson("DEPR", obj);
1033                         }
1034                         
1035                         this.win.statusbar_compilestatus_label.el.hide();
1036                         this.win.statusbar_run.el.hide();
1037                         if (!has_errors) { 
1038                                 this.win.statusbar_compilestatus_label.el.show();
1039                                 this.win.statusbar_run.el.show();
1040                         }
1041                         if (this.file.xtype == "Gtk") {
1042                                 // not sure how this is working ok? - as highlighting is happening on the vala files at present..
1043                                 var gbuf =   this.window_gladeview.sourceview;
1044                                 gbuf.highlightErrorsJson("ERR", obj);
1045                                 gbuf.highlightErrorsJson("WARN", obj);
1046                                 gbuf.highlightErrorsJson("DEPR", obj);                  
1047                                 
1048                                 if (!has_errors) {
1049                                         this.win.statusbar_run.el.show();
1050                                 }
1051                         
1052                    }
1053                    
1054                    if (this.file.xtype == "Roo") {
1055                                 // not sure how this is working ok? - as highlighting is happening on the vala files at present..
1056                                 var gbuf =   this.window_rooview.sourceview;
1057                                 gbuf.highlightErrorsJson("ERR", obj);
1058                                 gbuf.highlightErrorsJson("WARN", obj);
1059                                 gbuf.highlightErrorsJson("DEPR", obj);                  
1060                         
1061                    }
1062                     
1063                         this.last_compile_result = obj;
1064                         
1065                         
1066                 }
1067         
1068 }
1069
1070