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 MainWindow win;
11
12     public enum State {
13         PREVIEW,
14         OBJECT,
15         PROP,
16         LISTENER,
17         CODE,
18         FILES
19     };
20
21     public State state;
22
23     
24     public Xcls_WindowLeftTree  left_tree;
25     public Xcls_WindowAddProp   add_props;
26     public Xcls_LeftProps       left_props;
27     public Xcls_ProjectSettings projectsettings;
28     public ValaProjectSettings  vala_projectsettings;
29     public Xcls_RightPalete     rightpalete;
30     public Editor               code_editor;    
31     public Xcls_WindowRooView   window_rooview;
32     public Xcls_GtkView         window_gladeview;
33     public Xcls_DialogNewComponent new_file_dialog;     
34
35     public Xcls_WindowLeftProjects left_projects; // can not see where this is initialized.. 
36     // ctor 
37     public WindowState(MainWindow win)
38     {
39         this.win = win;
40         // initialize
41
42         // left elements..
43         this.leftTreeInit();
44         this.propsListInit();
45
46         // on clutter space...
47         this.projectEditInit();
48         this.codeEditInit();
49         this.projectListInit();)
50         this.fileViewInit();
51         
52         // adding stuff
53         this.objectAddInit();
54         this.propsAddInit();
55      
56         
57         // previews...
58         this.gtkViewInit();
59         this.webkitViewInit();
60
61         // dialogs
62
63         this.fileNewInit();
64     }
65
66
67     // left tree
68
69     public void leftTreeInit()
70     {
71      
72         this.left_tree = new Xcls_WindowLeftTree();
73         this.left_tree.ref();
74         this.left_tree.main_window = _this.win;
75     
76         this.win.tree.el.pack_start(this.left_tree.el,true, true,0);
77         this.left_tree.el.show_all();
78            
79         this.left_tree.before_node_change.connect(() => {
80             return this.leftTreeBeforeChange();
81
82         });
83
84         this.left_tree.node_selected.connect((sel) => {
85             this.leftTreeNodeSelected(sel);
86         });
87      
88         this.left_tree.changed.connect(() => {
89             this.window_rooview.requestRedraw();
90             this.left_tree.model.file.save();
91         });
92          
93     }
94
95     public bool leftTreeBeforeChange(JsRender.Node? sel)
96     {
97         if (this.state != "codeedit") {
98             this.left_props.finish_editing();
99             return true;
100         }
101         if (!this.code_editor.saveContents()) {
102             return false;
103         }
104         return false;
105     }
106     
107     public void leftTreeNodeSelected(JsRender.Node? sel)
108     {
109
110         print("node_selected called %s\n", (sel == null) ? "NULL" : "a value");
111
112         if (sel == null) {
113             this.left_props.el.hide();
114         } 
115         this.left_props.el.show();
116         this.left_props.load(this.left_tree.getActiveFile(), sel);
117         switch (this.state) {
118             
119             case State.OBJECT: 
120                   
121                  if (sel == null) {
122                     this.rightpalete.clear();
123                     break;
124                 }
125                 this.rightpalete.load(this.left_tree.getActiveFile().palete(), sel.fqn());
126                 break;
127                  
128         
129            case State.PROP:
130                 if (sel == null) {
131                     this.add_props.clear();
132                     break;
133                 }
134                 this.add_props.show(this.left_tree.getActiveFile().palete(), "props", sel.fqn());
135                 break;
136
137             case State.LISTENER:
138                
139                 if (sel == null) {
140                     this.add_props.clear();
141                     break;
142                 }
143                 this.add_props.show(_this.left_tree.getActiveFile().palete(), "signals", sel.fqn());
144                 break;
145                 
146             case State.CODEEDIT:
147             // SAVE FIRST???
148         
149             this.codeEditHide();
150             break;
151                
152                             
153         }
154          
155
156     }
157
158
159
160
161     public void propsListInit()
162     {
163     
164         this.left_props =new Xcls_LeftProps();
165         this.left_props.ref();
166         this.left_props.main_window = _this;
167         this.win.props.el.pack_start(this.left_props.el,true, true,0);
168         this.left_props.el.show_all();
169     
170         this.left_props.show_editor.connect( (file, node, type,  key) => {
171             this.codeEditShow(file, node, type,  key);
172         });
173
174     
175         this.left_props.stop_editor.connect( () => {
176             if (this.state != "codeedit") {
177                 return true;
178             }
179     
180             var ret =  this.code_editor.saveContents();
181             if (!ret) {
182                 return false;
183             }
184             this.codeEditHide();
185             return ret;
186         });
187     
188         this.left_props.changed.connect(() => {
189               if (this.left_tree.getActiveFile().xtype == "Roo" ) {
190                    this.window_rooview.requestRedraw();
191                    
192                } else {
193                   this.window_gladeview.loadFile(this.left_tree.getActiveFile());
194               }
195               this.left_tree.model.updateSelected();
196               this.left_tree.model.file.save();
197         });
198     
199
200
201     }
202
203     //-------------  projects edit
204
205     public void projectEditInit()
206     {
207         this.projectsettings  =new Xcls_ProjectSettings();
208         this.projectsettings.ref();  /// really?
209     
210         this.vala_projectsettings  =new ValaProjectSettings();
211         this.vala_projectsettings.ref();
212         this.vala_projectsettings.window = this;
213     
214         ((Gtk.Container)(this.win.projecteditview.el.get_widget())).add(this.projectsettings.el);
215         //this.projectsettings.el.show_all();
216
217         var stage = this.win.projecteditview.el.get_stage();
218         stage.set_background_color(  Clutter.Color.from_string("#000"));
219     
220         this.projectsettings.buttonPressed.connect((btn) => {
221              if (this.left_tree.getActiveFile().xtype == "Roo" ) {
222                 if (btn == "save") {
223                     this.window_rooview.view.renderJS(true);
224                 }
225                 if (btn == "apply") {
226                     this.window_rooview.view.renderJS(true);
227                     return;
228                 }
229             } else {
230                 // do nothing for gtk..
231             }
232             if (btn == "save" || btn == "apply") {
233                 this.win.project.save();
234          
235             }
236             
237             this.projectEditHide();
238              
239          });
240
241     }
242     // ----------- object adding
243     public void objectAddInit()
244     {
245
246         this.rightpalete  = new Xcls_RightPalete();
247         this.rightpalete.ref();  /// really?
248         ((Gtk.Container)(this.win.objectview.el.get_widget())).add(this.rightpalete.el);
249         //this.projectsettings.el.show_all();
250
251         stage = _this.win.objectview.el.get_stage();
252         stage.set_background_color(  Clutter.Color.from_string("#000"));
253            
254     }
255     
256     // -----------  properties adding list...
257     // listener uses the properties 
258     public void propsAddInit()
259     {
260     // Add properties
261         this.add_props  = new Xcls_WindowAddProp();
262         this.add_props.ref();  /// really?
263         ((Gtk.Container)(this.win.addpropsview.el.get_widget())).add(this.add_props.el);
264         //this.projectsettings.el.show_all();
265
266         var  stage = _this.win.addpropsview.el.get_stage();
267         stage.set_background_color(  Clutter.Color.from_string("#000"));
268
269
270         this.add_props.select.connect( (key,type,skel, etype) => {
271             this.left_props.addProp(etype, key, skel, type);
272         });
273
274     }
275     public void propsAddShow()
276     {
277
278     }
279     public void propsAddHide()
280     {
281     
282     }
283
284
285
286     
287     // ----------- Add / Edit listener
288     // listener uses the properties 
289     //public void listenerInit()     { }
290     public void listenerShow()
291     {
292
293     }
294     public void listenerHide()
295     {
296     
297     }
298
299     // -------------- codeEditor
300
301     public void codeEditInit()
302     {
303         this.code_editor  = new  Editor();
304         this.code_editor.ref();  /// really?
305         ((Gtk.Container)(this.win.codeeditview.el.get_widget())).add(this.code_editor.el);
306         //this.projectsettings.el.show_all();
307
308         stage = _this.win.codeeditview.el.get_stage();
309         stage.set_background_color(  Clutter.Color.from_string("#000"));
310         // editor.save...
311
312         this.code_editor.save.connect( () => {
313              this.left_tree.model.file.save();
314              this.left_tree.model.updateSelected();
315         });
316         
317     }
318
319     // ----------- list of projects on left
320     public void  projectListInit() 
321     {
322
323         this.left_projects = new Xcls_WindowLeftProjects();
324          this.left_projects.ref();
325          this.win.leftpane.el.pack_start(this.left_projects.el,true, true,0);
326          this.left_projects.el.show_all();
327          this.left_projects.project_selected.connect((proj) => {
328             proj.scanDirs();
329             this.clutterfiles.loadProject(proj);
330         
331          });
332
333     }
334     // ----------- file view
335
336     public void fileViewInit()
337     {
338         stage = _this.rooview.el.get_stage(); \\ seems odd... 
339         this.clutterfiles = new Xcls_ClutterFiles();
340         this.clutterfiles.ref();
341         stage.add_child(this.clutterfiles.el);
342         this.clutterfiles.el.show_all();
343
344
345         this.clutterfiles.open.connect((file) => { 
346             this.fileViewOpen(file);
347         });
348
349     }
350     public void fileNewInit()
351     {
352         this.new_file_dialog = new Xcls_DialogNewComponent();
353         // force it modal to the main window..
354         this.new_file_dialog.el.set_transient_for(this.el);
355         this.new_file_dialog.el.set_modal(true);
356     
357         this.new_file_dialog.success.connect((project,file) =>
358         {
359             this.fileViewOpen(file);
360         });
361
362     }
363
364     
365     public void fileViewOpen(JsRender.JsRender file)
366     {
367         this.win.project = file.project;
368         this.previewShow();
369             this.left_tree.model.loadFile(file);
370     
371         var ctr= ((Gtk.Container)(this.win.rooview.el.get_widget()));
372         var ctr_p= ((Gtk.Container)(this.win.projecteditview.el.get_widget()));
373     
374         if (file.xtype == "Roo" ) { 
375             ctr.foreach( (w) => { ctr.remove(w); });
376             ctr_p.foreach( (w) => { ctr_p.remove(w); });
377             ctr.add(this.window_rooview.el);
378             ctr_p.add(this.projectsettings.el);            
379             this.window_rooview.loadFile(file);
380             this.window_rooview.el.show_all();
381             this.projectsettings.el.show_all();            
382
383         } else {
384             ctr.foreach( (w) => { ctr.remove(w); });
385             ctr_p.foreach( (w) => { ctr_p.remove(w); });            
386             ctr.add(this.window_gladeview.el);
387             ctr_p.add(this.vala_projectsettings.el);
388             this.window_gladeview.loadFile(file);
389             this.window_gladeview.el.show_all();
390             this.vala_projectsettings.el.show_all();
391         }
392         print("OPEN : " + file.name);
393         this.editpane.el.set_position(_this.editpane.el.max_position);
394         this.win.setTitle(file.project.name + " : " +file.name);
395              
396
397         }
398
399     
400     // ---------  webkit view
401     public void webkitViewInit()
402     {
403         this.window_rooview  =new Xcls_WindowRooView();
404         this.window_rooview.ref();
405         ((Gtk.Container)(this.win.rooview.el.get_widget())).add(this.window_rooview.el);
406         this.window_rooview.el.show_all();
407
408         stage = this.win.rooview.el.get_stage();
409         stage.set_background_color(  Clutter.Color.from_string("#000"));
410     }
411
412     // ------ Gtk  - view
413
414     public void gtkViewInit()
415     {
416         this.window_gladeview  =new Xcls_GtkView();
417         this.window_gladeview.ref();
418     }
419
420     public void switchState(State new_state)
421     {
422         if (this.state == State.PREVIEW) {
423             // try and do a snapshot..
424             
425             if (this.left_tree.getActiveFile() != null) {
426                  if (this.left_tree.getActiveFile().xtype == "Roo" ) {
427                      this.window_rooview.createThumb();
428                  } else {
429                       this.window_gladeview.createThumb();
430                   }
431               }
432         }
433
434         switch (this.state) {
435            case State.LISTENER:
436            case State.PROP:
437                 this.win.rooview.el.save_easing_state();
438                 this.addpropsview.el.save_easing_state();
439                 
440                 this.win.rooview.el.set_scale(1.0f,1.0f);
441                 this.addpropsview.el.set_scale(0.0f,0.0f);
442                 
443  
444                 this.win.rooview.el.restore_easing_state();
445                 this.win.addpropsview.el.restore_easing_state();   
446         
447                 break;
448
449
450          
451         
452         this.state = new_state;
453
454         this.buttonShowHide();
455         
456         switch (this.state) {
457             
458             case State.PREVIEW:  // this is the default state when working...
459
460                 break
461            case State.FILES:  // can only get here from PREVIEW state.. in theory..
462                 
463    
464                 this.win.editpane.el.hide(); // holder for tree and properties..
465              
466                 this.left_projects.el.show(); 
467             
468                 var el = this.win.rooview.el;
469                 el.save_easing_state();
470                 el.set_easing_duration(1000);
471
472                 el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, 360.0f);
473                 el.set_scale(0.0f,0.0f);
474
475  
476                 if (this.win.project != null) {
477                     this.left_projects.selectProject(_this.project);
478                 }
479              
480                 el.restore_easing_state();
481                 
482                 break;
483
484
485
486
487             
488     }
489     
490     // -- buttons show hide.....
491
492     public void buttonsShowHide()
493     {
494         // basically hide everything, then show the relivant..
495
496          this.win.backbutton.el.hide();
497     
498         this.win.projectbutton.el.hide(); // show file nav...
499         this.win.editfilebutton.el.hide();
500         this.win.projecteditbutton.el.hide();
501          
502         
503         this.win.objectshowbutton.el.hide(); // add objects
504         this.win.addpropbutton.el.hide();  
505         this.win.addlistenerbutton.el.hide(); 
506
507     
508     
509         this.win.addprojectbutton.el.hide();
510         this.win.addfilebutton.el.hide();
511         this.win.delprojectbutton.el.hide();
512         this.win.new_window.el.hide();
513
514         
515         switch (this.state) {
516             
517             case State.PREVIEW:  // this is the default state when working...
518                
519                 this.win.projectbutton.el.show(); // show file nav...
520                 this.win.editfilebutton.el.show();
521                 this.win.projecteditbutton.el.show();
522                  
523                 
524                 this.win.objectshowbutton.el.show(); // add objects
525                 this.win.addpropbutton.el.show();  
526                 this.win.addlistenerbutton.el.show(); 
527                 break;
528             
529            
530             case State.CODE: 
531             case State.PROP:
532             case State.LISTENER:
533             case State.OBJECT
534                 
535                 this.win.backbutton.el.show();
536                 this.win.objectshowbutton.el.show(); // add objects
537                 this.win.addpropbutton.el.show();  
538                 this.win.addlistenerbutton.el.show(); 
539                  break;
540                 
541             case State.FILES:
542                 this.win.backbutton.el.show();
543                      
544                 this.win.addprojectbutton.el.show();
545                 this.win.addfilebutton.el.show();
546                 this.win.delprojectbutton.el.show();
547                 this.win.new_window.el.show();
548                 break;
549         
550
551     }
552
553
554     
555
556     
557 }
558
559