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