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