src/Builder4/WindowState.vala
[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         FILES,
20         PROJECT // project settings..
21     }
22
23     public State state = State.NONE;
24
25     public bool children_loaded = false;
26
27     
28     public Xcls_WindowLeftTree  left_tree;
29     public Xcls_WindowAddProp   add_props;
30     public Xcls_LeftProps       left_props;
31     public Xcls_ProjectSettings projectsettings;
32     public ValaProjectSettings  vala_projectsettings;
33     public Xcls_RightPalete     rightpalete;
34     public Editor               code_editor;    
35     public Xcls_WindowRooView   window_rooview;
36     public Xcls_GtkView         window_gladeview;
37     public Xcls_DialogNewComponent new_file_dialog;
38     public Xcls_ClutterFiles     clutterfiles;
39
40     public Xcls_WindowLeftProjects left_projects; // can not see where this is initialized.. 
41     // ctor 
42     public WindowState(Xcls_MainWindow win)
43     {
44         this.win = win;
45         // initialize
46
47         // left elements..
48         this.leftTreeInit();
49         this.propsListInit();
50
51         // on clutter space...
52         this.projectEditInit();
53         this.codeEditInit();
54         this.projectListInit();
55         this.fileViewInit();
56         
57         // adding stuff
58         this.objectAddInit();
59         this.propsAddInit();
60      
61         
62         // previews...
63         this.gtkViewInit();
64         this.webkitViewInit();
65
66         // dialogs
67
68         this.fileNewInit();
69
70         this.children_loaded = true;
71     }
72
73
74     // left tree
75
76     public void leftTreeInit()
77     {
78      
79         this.left_tree = new Xcls_WindowLeftTree();
80         this.left_tree.ref();
81         this.left_tree.main_window = this.win;
82     
83         this.win.tree.el.pack_start(this.left_tree.el,true, true,0);
84         this.left_tree.el.show_all();
85            
86         this.left_tree.before_node_change.connect(() => {
87             return this.leftTreeBeforeChange();
88
89         });
90
91         this.left_tree.node_selected.connect((sel) => {
92             this.leftTreeNodeSelected(sel);
93         });
94      
95         this.left_tree.changed.connect(() => {
96             this.window_rooview.requestRedraw();
97             this.left_tree.model.file.save();
98         });
99          
100     }
101
102     public bool leftTreeBeforeChange()
103     {
104         if (this.state != State.CODE) {
105             this.left_props.finish_editing();
106             return true;
107         }
108         if (!this.code_editor.saveContents()) {
109             return false;
110         }
111         return false;
112     }
113     
114     public void leftTreeNodeSelected(JsRender.Node? sel)
115     {
116
117         print("node_selected called %s\n", (sel == null) ? "NULL" : "a value");
118
119         if (sel == null) {
120             this.left_props.el.hide();
121         } 
122         this.left_props.el.show();
123         this.left_props.load(this.left_tree.getActiveFile(), sel);
124         switch (this.state) {
125             
126             case State.OBJECT: 
127                   
128                  if (sel == null) {
129                     this.rightpalete.clear();
130                     break;
131                 }
132                 this.rightpalete.load(this.left_tree.getActiveFile().palete(), sel.fqn());
133                 break;
134                  
135         
136            case State.PROP:
137                 if (sel == null) {
138                     this.add_props.clear();
139                     break;
140                 }
141                 this.add_props.show(this.left_tree.getActiveFile().palete(), "props", sel.fqn());
142                 break;
143
144             case State.LISTENER:
145                
146                 if (sel == null) {
147                     this.add_props.clear();
148                     break;
149                 }
150                 this.add_props.show(this.left_tree.getActiveFile().palete(), "signals", sel.fqn());
151                 break;
152                 
153             case State.CODE:
154                  this.switchState(State.PREVIEW);
155              
156                 break;
157                
158                             
159         }
160          
161
162     }
163
164
165
166
167     public void propsListInit()
168     {
169     
170         this.left_props =new Xcls_LeftProps();
171         this.left_props.ref();
172         this.left_props.main_window = this.win;
173         this.win.props.el.pack_start(this.left_props.el,true, true,0);
174         this.left_props.el.show_all();
175     
176         this.left_props.show_editor.connect( (file, node, type,  key) => {
177             this.switchState(State.CODE);
178             this.code_editor.show(
179                 file,
180                 node,
181                 type,
182                 key
183             );
184             
185             
186         });
187
188    
189         this.left_props.stop_editor.connect( () => {
190             if (this.state != State.CODE) {
191                 return true;
192             }
193     
194             var ret =  this.code_editor.saveContents();
195             if (!ret) {
196                 return false;
197             }
198             this.switchState(State.PREVIEW);
199             return ret;
200         });
201     
202         this.left_props.changed.connect(() => {
203               if (this.left_tree.getActiveFile().xtype == "Roo" ) {
204                    this.win.window_rooview.requestRedraw();
205                    
206                } else {
207                   this.win.window_gladeview.loadFile(this.left_tree.getActiveFile());
208               }
209               this.left_tree.model.updateSelected();
210               this.left_tree.model.file.save();
211         });
212     
213
214
215     }
216
217     //-------------  projects edit
218
219     public void projectEditInit()
220     {
221         this.projectsettings  =new Xcls_ProjectSettings();
222         this.projectsettings.ref();  /// really?
223     
224         this.vala_projectsettings  =new ValaProjectSettings();
225         this.vala_projectsettings.ref();
226         this.vala_projectsettings.window = this.win;
227     
228         ((Gtk.Container)(this.win.projecteditview.el.get_widget())).add(this.projectsettings.el);
229         //this.projectsettings.el.show_all();
230
231         var stage = this.win.projecteditview.el.get_stage();
232         stage.set_background_color(  Clutter.Color.from_string("#000"));
233     
234         this.projectsettings.buttonPressed.connect((btn) => {
235              if (this.left_tree.getActiveFile().xtype == "Roo" ) {
236                 if (btn == "save") {
237                     this.window_rooview.view.renderJS(true);
238                 }
239                 if (btn == "apply") {
240                     this.window_rooview.view.renderJS(true);
241                     return;
242                 }
243             } else {
244                 // do nothing for gtk..
245             }
246             if (btn == "save" || btn == "apply") {
247                 this.win.project.save();
248          
249             }
250             this.switchState (State.PREVIEW); 
251              
252          });
253
254     }
255     // ----------- object adding
256     public void objectAddInit()
257     {
258
259         this.rightpalete  = new Xcls_RightPalete();
260         this.rightpalete.ref();  /// really?
261         ((Gtk.Container)(this.win.objectview.el.get_widget())).add(this.rightpalete.el);
262         //this.projectsettings.el.show_all();
263
264         var stage = this.win.objectview.el.get_stage();
265         stage.set_background_color(  Clutter.Color.from_string("#000"));
266            
267     }
268     
269     // -----------  properties adding list...
270     // listener uses the properties 
271     public void propsAddInit()
272     {
273     // Add properties
274         this.add_props  = new Xcls_WindowAddProp();
275         this.add_props.ref();  /// really?
276         ((Gtk.Container)(this.win.addpropsview.el.get_widget())).add(this.add_props.el);
277         //this.projectsettings.el.show_all();
278
279         var  stage = this.win.addpropsview.el.get_stage();
280         stage.set_background_color(  Clutter.Color.from_string("#000"));
281
282
283         this.add_props.select.connect( (key,type,skel, etype) => {
284             this.left_props.addProp(etype, key, skel, type);
285         });
286
287     }
288     public void propsAddShow()
289     {
290
291     }
292     public void propsAddHide()
293     {
294     
295     }
296
297
298
299     
300     // ----------- Add / Edit listener
301     // listener uses the properties 
302     //public void listenerInit()     { }
303     public void listenerShow()
304     {
305
306     }
307     public void listenerHide()
308     {
309     
310     }
311
312     // -------------- codeEditor
313
314     public void codeEditInit()
315     {
316         this.code_editor  = new  Editor();
317         this.code_editor.ref();  /// really?
318         ((Gtk.Container)(this.win.codeeditview.el.get_widget())).add(this.code_editor.el);
319         //this.projectsettings.el.show_all();
320
321         var stage = this.win.codeeditview.el.get_stage();
322         stage.set_background_color(  Clutter.Color.from_string("#000"));
323         // editor.save...
324
325         this.code_editor.save.connect( () => {
326              this.left_tree.model.file.save();
327              this.left_tree.model.updateSelected();
328         });
329         
330     }
331
332     // ----------- list of projects on left
333     public void  projectListInit() 
334     {
335
336         this.left_projects = new Xcls_WindowLeftProjects();
337          this.left_projects.ref();
338          this.win.leftpane.el.pack_start(this.left_projects.el,true, true,0);
339          this.left_projects.el.show_all();
340          this.left_projects.project_selected.connect((proj) => {
341             proj.scanDirs();
342             this.clutterfiles.loadProject(proj);
343         
344          });
345
346     }
347     // ----------- file view
348
349     public void fileViewInit()
350     {
351         var stage = this.win.rooview.el.get_stage(); // seems odd... 
352         this.clutterfiles = new Xcls_ClutterFiles();
353         this.clutterfiles.ref();
354         stage.add_child(this.clutterfiles.el);
355         this.clutterfiles.el.show_all();
356
357
358         this.clutterfiles.open.connect((file) => { 
359             this.fileViewOpen(file);
360         });
361
362     }
363     public void fileNewInit()
364     {
365         this.new_file_dialog = new Xcls_DialogNewComponent();
366         // force it modal to the main window..
367         this.new_file_dialog.el.set_transient_for(this.win.el);
368         this.new_file_dialog.el.set_modal(true);
369     
370         this.new_file_dialog.success.connect((project,file) =>
371         {
372             this.fileViewOpen(file);
373         });
374
375     }
376
377     
378     public void fileViewOpen(JsRender.JsRender file)
379     {
380         this.win.project = file.project;
381         this.switchState (State.PREVIEW); 
382         
383         this.left_tree.model.loadFile(file);
384     
385         var ctr= ((Gtk.Container)(this.win.rooview.el.get_widget()));
386         var ctr_p= ((Gtk.Container)(this.win.projecteditview.el.get_widget()));
387     
388         if (file.xtype == "Roo" ) { 
389             ctr.foreach( (w) => { ctr.remove(w); });
390             ctr_p.foreach( (w) => { ctr_p.remove(w); });
391             ctr.add(this.window_rooview.el);
392             ctr_p.add(this.projectsettings.el);            
393             this.window_rooview.loadFile(file);
394             this.window_rooview.el.show_all();
395             this.projectsettings.el.show_all();            
396
397         } else {
398             ctr.foreach( (w) => { ctr.remove(w); });
399             ctr_p.foreach( (w) => { ctr_p.remove(w); });            
400             ctr.add(this.window_gladeview.el);
401             ctr_p.add(this.vala_projectsettings.el);
402             this.window_gladeview.loadFile(file);
403             this.window_gladeview.el.show_all();
404             this.vala_projectsettings.el.show_all();
405         }
406         print("OPEN : " + file.name);
407         this.win.editpane.el.set_position(this.win.editpane.el.max_position);
408         this.win.setTitle(file.project.name + " : " +file.name);
409              
410
411         }
412
413     
414     // ---------  webkit view
415     public void webkitViewInit()
416     {
417         this.window_rooview  =new Xcls_WindowRooView();
418         this.window_rooview.ref();
419         ((Gtk.Container)(this.win.rooview.el.get_widget())).add(this.window_rooview.el);
420         this.window_rooview.el.show_all();
421
422         var stage = this.win.rooview.el.get_stage();
423         stage.set_background_color(  Clutter.Color.from_string("#000"));
424     }
425
426     // ------ Gtk  - view
427
428     public void gtkViewInit()
429     {
430         this.window_gladeview  =new Xcls_GtkView();
431         this.window_gladeview.ref();
432     }
433
434     public void switchState(State new_state)
435     {
436         
437
438         
439         switch (this.state) {
440
441             case State.PREVIEW:
442                 if (this.left_tree.getActiveFile() != null) {
443                      if (this.left_tree.getActiveFile().xtype == "Roo" ) {
444                          this.window_rooview.createThumb();
445                      } else {
446                           this.window_gladeview.createThumb();
447                       }
448                 }
449                 // normally we are going from preview to another state.
450                 // and different windows hide the preview in differnt ways..
451                 
452                 break;
453             
454            case State.LISTENER:
455            case State.PROP:
456                 this.win.addpropsview.el.save_easing_state();
457                 this.win.addpropsview.el.set_scale(0.0f,0.0f);
458                 this.win.addpropsview.el.restore_easing_state();   
459                 break;
460                 
461             case State.CODE:
462
463
464                 this.code_editor.saveContents();
465                 this.win.codeeditview.el.save_easing_state();
466                 this.win.codeeditview.el.set_scale(0.0f,0.0f);
467                 this.win.codeeditview.el.restore_easing_state();    
468                 break;
469
470
471              case State.OBJECT:
472                 this.win.objectview.el.save_easing_state();
473                 this.win.objectview.el.set_scale(0.0f,0.0f);
474                 this.win.objectview.el.restore_easing_state();    
475                 break;
476
477            case State.PROJECT:
478                 this.win.projecteditview.el.save_easing_state();
479                 this.win.projecteditview.el.set_scale(0.0f,0.0f);
480                 this.win.projecteditview.el.restore_easing_state();    
481                 break;
482
483           case State.FILES:       
484                 this.win.rooview.el.save_easing_state();
485                 this.win.rooview.el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, 0.0f);
486                 this.win.rooview.el.set_scale(1.0f,1.0f);
487                 this.win.rooview.el.restore_easing_state();  
488                 this.clutterfiles.el.hide();
489                  
490
491                 break;
492
493                 
494         }
495         
496         var oldstate  =this.state;
497         this.state = new_state;
498
499                 
500         this.buttonsShowHide();
501         
502         switch (this.state) {
503             
504             case State.PREVIEW:  // this is the default state when working...
505                  this.win.editpane.el.show(); // holder for tree and properties..
506              
507                  this.left_projects.el.hide(); 
508             
509
510                  this.win.rooview.el.save_easing_state();
511                  this.win.rooview.el.set_scale(1.0f,1.0f);
512                  this.win.rooview.el.restore_easing_state();
513                
514                 break;
515
516
517             case State.LISTENER:
518                 // same as prop?
519             case State.PROP:
520                 var ae =      this.left_tree.getActiveElement();
521                 if (ae == null) {
522                     this.state = oldstate;
523                     this.buttonsShowHide();
524                     return;
525                 }
526                 this.add_props.el.show_all();
527                 this.add_props.show(
528                     Palete.factory(this.win.project.xtype), 
529                     this.state == State.LISTENER ? "signals" : "props",
530                     ae.fqn()
531                 );
532  
533                     
534  
535                 this.win.rooview.el.save_easing_state();
536                 // -- FIXME? this needs to be State aware?
537                 this.resizeCanvasElementsA();
538                 this.win.rooview.el.restore_easing_state();
539                 
540               
541                 this.win.addpropsview.el.save_easing_state();
542                 this.win.addpropsview.el.set_scale(1.0f,1.0f);
543                 this.win.addpropsview.el.restore_easing_state();
544                 break;
545                 
546             case State.CODE:
547
548                 this.code_editor.el.show_all();
549                 
550                 // caller needs to call editor - show....
551                 this.win.rooview.el.save_easing_state();
552                 this.resizeCanvasElementsA();
553                 this.win.rooview.el.restore_easing_state();
554                 
555  
556                 this.win.codeeditview.el.save_easing_state();
557                 this.win.codeeditview.el.set_scale(1.0f,1.0f);
558                 this.win.codeeditview.el.restore_easing_state();    
559                 break;
560
561
562              case State.OBJECT:
563                  var n = this.left_tree.getActiveElement();
564
565                 if (this.left_tree.model.file == null) {
566                     this.state =oldstate;
567                     this.buttonsShowHide();
568                     return;
569                 }
570                 
571                 if (n == null && this.left_tree.model.file.tree != null) {
572                     this.state = oldstate;
573                     this.buttonsShowHide();
574                     return;
575                 }
576
577                 this.rightpalete.el.show_all();
578                 this.rightpalete.load(this.left_tree.getActiveFile().palete(), n == null ? "*top" : n.fqn());
579
580                 this.win.rooview.el.save_easing_state();
581                 this.resizeCanvasElementsA();
582                 this.win.rooview.el.restore_easing_state();
583
584                 
585                 this.win.objectview.el.save_easing_state();
586                 this.win.objectview.el.set_scale(1.0f,1.0f);
587                 this.win.objectview.el.restore_easing_state();    
588                 break;
589
590            case State.PROJECT:
591
592              if (this.win.project.xtype == "Roo") {
593                     this.projectsettings.el.show_all();
594                     this.projectsettings.show(this.win.project);
595                 } else {
596                     this.vala_projectsettings.el.show_all();
597                     this.vala_projectsettings.show((Project.Gtk)this.win.project);
598                 }
599
600                 this.win.rooview.el.save_easing_state();
601                 this.resizeCanvasElementsA();
602                 this.win.rooview.el.restore_easing_state();
603
604                 this.win.projecteditview.el.save_easing_state();
605                 this.win.projecteditview.el.set_scale(1.0f,1.0f);
606                 this.win.projecteditview.el.restore_easing_state();    
607                 break;
608                 
609            case State.FILES:  // can only get here from PREVIEW state.. in theory..
610                 
611    
612                 this.win.editpane.el.hide(); // holder for tree and properties..
613              
614                 this.left_projects.el.show(); 
615             
616                 var el = this.win.rooview.el;
617                 el.save_easing_state();
618                 el.set_easing_duration(1000);
619
620                 el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, 360.0f);
621                 el.set_scale(0.0f,0.0f);
622
623  
624                 if (this.win.project != null) {
625                     this.left_projects.selectProject(this.win.project);
626                 }
627              
628                 el.restore_easing_state();
629                 
630                 break;
631
632
633         }
634
635             
636     }
637     
638     // -- buttons show hide.....
639
640     public void buttonsShowHide()
641     {
642         // basically hide everything, then show the relivant..
643
644          this.win.backbutton.el.hide();
645     
646         this.win.projectbutton.el.hide(); // show file nav...
647         this.win.editfilebutton.el.hide();
648         this.win.projecteditbutton.el.hide();
649          
650         
651         this.win.objectshowbutton.el.hide(); // add objects
652         this.win.addpropbutton.el.hide();  
653         this.win.addlistenerbutton.el.hide(); 
654
655     
656     
657         this.win.addprojectbutton.el.hide();
658         this.win.addfilebutton.el.hide();
659         this.win.delprojectbutton.el.hide();
660         this.win.new_window.el.hide();
661
662         
663         switch (this.state) {
664             
665             case State.PREVIEW:  // this is the default state when working...
666                
667                 this.win.projectbutton.el.show(); // show file nav...
668                 this.win.editfilebutton.el.show();
669                 this.win.projecteditbutton.el.show();
670                  
671                 
672                 this.win.objectshowbutton.el.show(); // add objects
673                 this.win.addpropbutton.el.show();  
674                 this.win.addlistenerbutton.el.show(); 
675                 break;
676             
677            
678             case State.CODE: 
679             case State.PROP:
680             case State.LISTENER:
681             case State.OBJECT:
682                 
683                 this.win.backbutton.el.show();
684                 this.win.objectshowbutton.el.show(); // add objects
685                 this.win.addpropbutton.el.show();  
686                 this.win.addlistenerbutton.el.show(); 
687                 break;
688             
689             case State.PROJECT: 
690                 // anything else?
691                 this.win.backbutton.el.show();
692                 break;
693             
694     
695             case State.FILES:
696                 this.win.backbutton.el.show();
697                      
698                 this.win.addprojectbutton.el.show();
699                 this.win.addfilebutton.el.show();
700                 this.win.delprojectbutton.el.show();
701                 this.win.new_window.el.show();
702                 break;
703         }
704
705     }
706     public void resizeCanvasElementsA()
707     {
708         Gtk.Allocation alloc;
709         this.win.clutterembed.el.get_allocation(out alloc);
710         this.resizeCanvasElements(alloc);
711     }
712     public void resizeCanvasElements(Gtk.Allocation alloc)
713     {
714             //print("WindowState.resizeCanvasElements\n");
715             if (!this.children_loaded || this.win.clutterembed == null) {
716                 print("WindowState.resizeCanvasElements = ingnore not loaded or no clutterfiles\n");
717             return; 
718         }
719             //print("WindowState.resizeCanvasElements = running");
720
721         
722         // ------- project view appears at top...
723
724                
725         // ------- add property/object left - max 300px, min 50... (or disapear..)     
726         
727         var avail = alloc.width < 50.0f ? 0 :  alloc.width - 50.0f;
728         var palsize = avail < 300.0f ? avail : 300.0f;
729         //print("set palsize size %f\n", palsize);
730         // palate / props : fixed 300 pix
731                 
732  
733         // -------- code edit min 600
734         
735         var codesize = avail < 800.0f ? avail : 800.0f;
736         //print("set code size %f\n", codesize);
737
738
739         this.win.rooview.el.set_size(alloc.width-50, alloc.height);    
740
741
742         
743         switch ( this.state) {
744             case State.FILES: 
745                 this.clutterfiles.set_size(alloc.width-50, alloc.height);
746                 break;
747
748             case State.PROJECT: 
749                 this.win.projecteditview.el.set_size(alloc.width-50, alloc.height / 2.0f);
750                 break;
751
752             case State.CODE: 
753                 this.win.codeeditview.el.set_size(codesize, alloc.height);
754                 var scale = avail > 0.0f ? (avail - codesize -10 ) / avail : 0.0f;
755                     this.win.rooview.el.set_scale(scale,scale);
756                 break;
757                 
758             case State.PROP:
759             case State.LISTENER:        
760             case State.OBJECT:  
761                 this.win.objectview.el.set_size(palsize, alloc.height);    
762                 this.win.addpropsview.el.set_size(palsize, alloc.height);
763                     var scale = avail > 0.0f ? (avail - palsize -10 ) / avail : 0.0f;
764                 this.win.rooview.el.set_scale(scale,scale);
765                 break;
766         }
767     }
768
769     
770 }
771
772