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