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         this.state = new_state;
435
436         this.buttonShowHide();
437         
438         switch (this.state) {
439             
440             case State.PREVIEW:  // this is the default state when working...
441
442                 break
443            case State.FILES:  // can only get here from PREVIEW state.. in theory..
444                 
445    
446              this.win.editpane.el.hide(); // holder for tree and properties..
447              
448              this.left_projects.el.show(); 
449             
450             var el = _this.rooview.el;
451             el.save_easing_state();
452               el.set_easing_duration(1000);
453             // show project / file view..
454             //_this.mainpane.lastWidth = _this.leftpane.el.get_position();
455             //_this.mainpane.el.set_position(0);
456             // rotate y 180..
457             el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, 360.0f);
458             el.set_scale(0.0f,0.0f);
459            
460                 _this.state = "files";
461             if (_this.project != null) {
462                 _this.left_projects.selectProject(_this.project);
463                 }
464             //_this.clutterfiles.loadProject(_this.project);
465
466             el.restore_easing_state();
467                 
468             print("show view browsing");
469     }
470     
471     // -- buttons show hide.....
472
473     public void buttonsShowHide()
474     {
475         // basically hide everything, then show the relivant..
476
477          this.win.backbutton.el.hide();
478     
479         this.win.projectbutton.el.hide(); // show file nav...
480         this.win.editfilebutton.el.hide();
481         this.win.projecteditbutton.el.hide();
482          
483         
484         this.win.objectshowbutton.el.hide(); // add objects
485         this.win.addpropbutton.el.hide();  
486         this.win.addlistenerbutton.el.hide(); 
487
488     
489     
490         this.win.addprojectbutton.el.hide();
491         this.win.addfilebutton.el.hide();
492         this.win.delprojectbutton.el.hide();
493         this.win.new_window.el.hide();
494
495         
496         switch (this.state) {
497             
498             case State.PREVIEW:  // this is the default state when working...
499                
500                 this.win.projectbutton.el.show(); // show file nav...
501                 this.win.editfilebutton.el.show();
502                 this.win.projecteditbutton.el.show();
503                  
504                 
505                 this.win.objectshowbutton.el.show(); // add objects
506                 this.win.addpropbutton.el.show();  
507                 this.win.addlistenerbutton.el.show(); 
508                 break;
509             
510            
511             case State.CODE: 
512             case State.PROP:
513             case State.LISTENER:
514             case State.OBJECT
515                 
516                 this.win.backbutton.el.show();
517                 this.win.objectshowbutton.el.show(); // add objects
518                 this.win.addpropbutton.el.show();  
519                 this.win.addlistenerbutton.el.show(); 
520                  break;
521                 
522             case State.FILES:
523                 this.win.backbutton.el.show();
524                      
525                 this.win.addprojectbutton.el.show();
526                 this.win.addfilebutton.el.show();
527                 this.win.delprojectbutton.el.show();
528                 this.win.new_window.el.show();
529                 break;
530         
531
532     }
533
534
535     
536
537     
538 }
539
540