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 projectsettings
320     public void  projectListInit() {
321
322      this.left_projects = new Xcls_WindowLeftProjects();
323          this.left_projects.ref();
324          this.leftpane.el.pack_start(this.left_projects.el,true, true,0);
325          this.left_projects.el.show_all();
326          this.left_projects.project_selected.connect((proj) => {
327             proj.scanDirs();
328             this.clutterfiles.loadProject(proj);
329         
330          });
331         
332
333     // ----------- file view
334
335     public void fileViewInit()
336     {
337         stage = _this.rooview.el.get_stage(); \\ seems odd... 
338         this.clutterfiles = new Xcls_ClutterFiles();
339         this.clutterfiles.ref();
340         stage.add_child(this.clutterfiles.el);
341         this.clutterfiles.el.show_all();
342
343
344         this.clutterfiles.open.connect((file) => { 
345             this.fileViewOpen(file);
346         });
347
348     }
349     public void fileNewInit()
350     {
351         this.new_file_dialog = new Xcls_DialogNewComponent();
352         // force it modal to the main window..
353         this.new_file_dialog.el.set_transient_for(this.el);
354         this.new_file_dialog.el.set_modal(true);
355     
356         this.new_file_dialog.success.connect((project,file) =>
357         {
358             this.fileViewOpen(file);
359         });
360
361     }
362
363     
364     public void fileViewOpen(JsRender.JsRender file)
365     {
366         this.win.project = file.project;
367         this.previewShow();
368             this.left_tree.model.loadFile(file);
369     
370         var ctr= ((Gtk.Container)(this.win.rooview.el.get_widget()));
371         var ctr_p= ((Gtk.Container)(this.win.projecteditview.el.get_widget()));
372     
373         if (file.xtype == "Roo" ) { 
374             ctr.foreach( (w) => { ctr.remove(w); });
375             ctr_p.foreach( (w) => { ctr_p.remove(w); });
376             ctr.add(this.window_rooview.el);
377             ctr_p.add(this.projectsettings.el);            
378             this.window_rooview.loadFile(file);
379             this.window_rooview.el.show_all();
380             this.projectsettings.el.show_all();            
381
382         } else {
383             ctr.foreach( (w) => { ctr.remove(w); });
384             ctr_p.foreach( (w) => { ctr_p.remove(w); });            
385             ctr.add(this.window_gladeview.el);
386             ctr_p.add(this.vala_projectsettings.el);
387             this.window_gladeview.loadFile(file);
388             this.window_gladeview.el.show_all();
389             this.vala_projectsettings.el.show_all();
390         }
391         print("OPEN : " + file.name);
392         this.editpane.el.set_position(_this.editpane.el.max_position);
393         this.win.setTitle(file.project.name + " : " +file.name);
394              
395
396         }
397
398     
399     // ---------  webkit view
400     public void webkitViewInit()
401     {
402         this.window_rooview  =new Xcls_WindowRooView();
403         this.window_rooview.ref();
404         ((Gtk.Container)(this.win.rooview.el.get_widget())).add(this.window_rooview.el);
405         this.window_rooview.el.show_all();
406
407         stage = this.win.rooview.el.get_stage();
408         stage.set_background_color(  Clutter.Color.from_string("#000"));
409     }
410
411     // ------ Gtk  - view
412
413     public void gtkViewInit()
414     {
415         this.window_gladeview  =new Xcls_GtkView();
416         this.window_gladeview.ref();
417     }
418
419     public void switchState(State new_state)
420     {
421         if (this.state == State.PREVIEW) {
422             // try and do a snapshot..
423             
424             if (this.left_tree.getActiveFile() != null) {
425                  if (this.left_tree.getActiveFile().xtype == "Roo" ) {
426                      this.window_rooview.createThumb();
427                  } else {
428                       this.window_gladeview.createThumb();
429                   }
430               }
431         }
432         
433         this.state = new_state;
434
435         this.buttonShowHide();
436         
437         switch (this.state) {
438             
439             case State.PREVIEW:  // this is the default state when working...
440
441                 break
442            case State.FILES:  // can only get here from PREVIEW state.. in theory..
443                 
444    
445              this.win.editpane.el.hide(); // holder for tree and properties..
446              
447              this.left_projects.el.show(); 
448             
449             var el = _this.rooview.el;
450             el.save_easing_state();
451               el.set_easing_duration(1000);
452             // show project / file view..
453             //_this.mainpane.lastWidth = _this.leftpane.el.get_position();
454             //_this.mainpane.el.set_position(0);
455             // rotate y 180..
456             el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, 360.0f);
457             el.set_scale(0.0f,0.0f);
458            
459                 _this.state = "files";
460             if (_this.project != null) {
461                 _this.left_projects.selectProject(_this.project);
462                 }
463             //_this.clutterfiles.loadProject(_this.project);
464
465             el.restore_easing_state();
466                 
467             print("show view browsing");
468     }
469     
470     // -- buttons show hide.....
471
472     public void buttonsShowHide()
473     {
474         // basically hide everything, then show the relivant..
475
476          this.win.backbutton.el.hide();
477     
478         this.win.projectbutton.el.hide(); // show file nav...
479         this.win.editfilebutton.el.hide();
480         this.win.projecteditbutton.el.hide();
481          
482         
483         this.win.objectshowbutton.el.hide(); // add objects
484         this.win.addpropbutton.el.hide();  
485         this.win.addlistenerbutton.el.hide(); 
486
487     
488     
489         this.win.addprojectbutton.el.hide();
490         this.win.addfilebutton.el.hide();
491         this.win.delprojectbutton.el.hide();
492         this.win.new_window.el.hide();
493
494         
495         switch (this.state) {
496             
497             case State.PREVIEW:  // this is the default state when working...
498                
499                 this.win.projectbutton.el.show(); // show file nav...
500                 this.win.editfilebutton.el.show();
501                 this.win.projecteditbutton.el.show();
502                  
503                 
504                 this.win.objectshowbutton.el.show(); // add objects
505                 this.win.addpropbutton.el.show();  
506                 this.win.addlistenerbutton.el.show(); 
507                 break;
508             
509            
510             case State.CODE: 
511             case State.PROP:
512             case State.LISTENER:
513             case State.OBJECT
514                 
515                 this.win.backbutton.el.show();
516                 this.win.objectshowbutton.el.show(); // add objects
517                 this.win.addpropbutton.el.show();  
518                 this.win.addlistenerbutton.el.show(); 
519                  break;
520                 
521             case State.FILES:
522                 this.win.backbutton.el.show();
523                      
524                 this.win.addprojectbutton.el.show();
525                 this.win.addfilebutton.el.show();
526                 this.win.delprojectbutton.el.show();
527                 this.win.new_window.el.show();
528                 break;
529         
530
531     }
532
533
534     
535
536     
537 }
538
539