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         OBJECT,
14         PROP,
15         LISTENER,
16         CODEEDIT
17     };
18
19     public State state;
20
21     
22     public Xcls_WindowLeftTree  left_tree;
23     public Xcls_WindowAddProp   add_props;
24     public Xcls_LeftProps       left_props;
25     public Xcls_ProjectSettings projectsettings;
26     public ValaProjectSettings  vala_projectsettings;
27     public Xcls_RightPalete     rightpalete;
28     public Editor               code_editor;    
29     public Xcls_WindowRooView   window_rooview;
30     public Xcls_GtkView         window_gladeview;
31      
32
33     // ctor 
34     public About(MainWindow win)
35     {
36         this.win = win;
37         // initialize
38
39         // left elements..
40         this.leftTreeInit();
41         this.propsListInit();
42
43         // on clutter space...
44         this.projectEditInit();
45         
46         this.codeEditInit();
47
48         // adding stuff
49         this.objectAddInit();
50         this.propsAddInit();
51         this.listenerInit();
52         
53         // previews...
54         this.gtkViewInit();
55         this.webkitViewInit();    
56     }
57
58
59     // left tree
60
61     public void leftTreeInit()
62     {
63          
64         this.left_tree = new Xcls_WindowLeftTree();
65         this.left_tree.ref();
66         this.left_tree.main_window = _this.win;
67         
68         this.win.tree.el.pack_start(this.left_tree.el,true, true,0);
69         this.left_tree.el.show_all();
70        
71         this.left_tree.before_node_change.connect(() => {
72             return this.leftTreeBeforeChange();
73
74         });
75
76         this.left_tree.node_selected.connect((sel) => {
77             this.leftTreeNodeSelected(sel);
78         });
79  
80         this.left_tree.changed.connect(() => {
81             this.window_rooview.requestRedraw();
82             this.left_tree.model.file.save();
83         });
84      
85     }
86
87     public bool leftTreeBeforeChange(JsRender.Node? sel)
88     {
89         if (this.state != "codeedit") {
90             this.left_props.finish_editing();
91             return true;
92         }
93         if (!this.code_editor.saveContents()) {
94             return false;
95         }
96         return false;
97     }
98     
99     public void leftTreeNodeSelected(JsRender.Node? sel)
100     {
101
102         print("node_selected called %s\n", (sel == null) ? "NULL" : "a value");
103
104         if (sel == null) {
105             this.left_props.el.hide();
106         } 
107         this.left_props.el.show();
108         this.left_props.load(this.left_tree.getActiveFile(), sel);
109         switch (this.state) {
110             
111             case State.OBJECT: 
112                   
113                  if (sel == null) {
114                     this.rightpalete.clear();
115                     break;
116                 }
117                 this.rightpalete.load(this.left_tree.getActiveFile().palete(), sel.fqn());
118                 break;
119                  
120                 
121            case State.PROP:
122                 if (sel == null) {
123                     this.add_props.clear();
124                     break;
125                 }
126                 this.add_props.show(this.left_tree.getActiveFile().palete(), "props", sel.fqn());
127                 break;
128
129             case State.LISTENER:
130            
131                 if (sel == null) {
132                     this.add_props.clear();
133                     break;
134                 }
135                 this.add_props.show(_this.left_tree.getActiveFile().palete(), "signals", sel.fqn());
136                 break;
137             case State.CODEEDIT:
138                 // SAVE FIRST???
139                 
140                 this.codeEditHide();
141                 break;
142                
143                                 
144         }
145          
146
147     }
148
149
150
151
152     public void propsListInit()
153     {
154         
155         this.left_props =new Xcls_LeftProps();
156         this.left_props.ref();
157         this.left_props.main_window = _this;
158         this.win.props.el.pack_start(this.left_props.el,true, true,0);
159         this.left_props.el.show_all();
160         
161         this.left_props.show_editor.connect( (file, node, type,  key) => {
162             this.codeEditShow(file, node, type,  key);
163         });
164
165         
166         this.left_props.stop_editor.connect( () => {
167             if (this.state != "codeedit") {
168                 return true;
169             }
170         
171             var ret =  this.code_editor.saveContents();
172             if (!ret) {
173                 return false;
174             }
175             this.codeEditHide();
176             return ret;
177         });
178         
179         this.left_props.changed.connect(() => {
180               if (this.left_tree.getActiveFile().xtype == "Roo" ) {
181                    this.window_rooview.requestRedraw();
182                    
183                } else {
184                   this.window_gladeview.loadFile(this.left_tree.getActiveFile());
185               }
186               this.left_tree.model.updateSelected();
187               this.left_tree.model.file.save();
188         });
189         
190
191
192     }
193
194     //-------------  projects edit
195
196     public void projectEditInit()
197     {
198         this.projectsettings  =new Xcls_ProjectSettings();
199         this.projectsettings.ref();  /// really?
200         
201         this.vala_projectsettings  =new ValaProjectSettings();
202         this.vala_projectsettings.ref();
203         this.vala_projectsettings.window = this;
204         
205         ((Gtk.Container)(this.win.projecteditview.el.get_widget())).add(this.projectsettings.el);
206         //this.projectsettings.el.show_all();
207
208         var stage = this.win.projecteditview.el.get_stage();
209         stage.set_background_color(  Clutter.Color.from_string("#000"));
210         
211         this.projectsettings.buttonPressed.connect((btn) => {
212              if (this.left_tree.getActiveFile().xtype == "Roo" ) {
213                 if (btn == "save") {
214                     this.window_rooview.view.renderJS(true);
215                 }
216                 if (btn == "apply") {
217                     this.window_rooview.view.renderJS(true);
218                     return;
219                 }
220             } else {
221                 // do nothing for gtk..
222             }
223             if (btn == "save" || btn == "apply") {
224                 this.win.project.save();
225      
226             }
227             
228             this.projectEditHide();
229              
230          });
231
232     }
233     // ----------- object adding
234     public void objectAddInit()
235     {
236
237         this.rightpalete  = new Xcls_RightPalete();
238         this.rightpalete.ref();  /// really?
239         ((Gtk.Container)(this.win.objectview.el.get_widget())).add(this.rightpalete.el);
240         //this.projectsettings.el.show_all();
241
242         stage = _this.win.objectview.el.get_stage();
243         stage.set_background_color(  Clutter.Color.from_string("#000"));
244        
245     }
246     
247     // -----------  properties adding list...
248     // listener uses the properties 
249     public void propsAddInit()
250     {
251         // Add properties
252         this.add_props  = new Xcls_WindowAddProp();
253         this.add_props.ref();  /// really?
254         ((Gtk.Container)(this.win.addpropsview.el.get_widget())).add(this.add_props.el);
255         //this.projectsettings.el.show_all();
256
257         var  stage = _this.win.addpropsview.el.get_stage();
258         stage.set_background_color(  Clutter.Color.from_string("#000"));
259
260         
261         this.add_props.select.connect( (key,type,skel, etype) => {
262             this.left_props.addProp(etype, key, skel, type);
263         });
264         
265     }
266     public void propsAddShow()
267     {
268
269     }
270     public void propsAddHide()
271     {
272         
273     }
274
275
276
277     
278     // ----------- Add / Edit listener
279     // listener uses the properties 
280     public void listenerInit()
281     {
282
283     }
284     public void listenerShow()
285     {
286
287     }
288     public void listenerHide()
289     {
290         
291     }
292
293     // -------------- codeEditor
294
295     public void codeEditInit()
296     {
297         this.code_editor  = new  Editor();
298         this.code_editor.ref();  /// really?
299         ((Gtk.Container)(this.win.codeeditview.el.get_widget())).add(this.code_editor.el);
300         //this.projectsettings.el.show_all();
301
302         stage = _this.win.codeeditview.el.get_stage();
303         stage.set_background_color(  Clutter.Color.from_string("#000"));
304         // editor.save...
305
306         this.code_editor.save.connect( () => {
307              this.left_tree.model.file.save();
308              this.left_tree.model.updateSelected();
309         });
310     
311     }
312
313     // ----------- file view
314
315     public void fileViewInit()
316     {
317
318         this.clutterfiles = new Xcls_ClutterFiles();
319         this.clutterfiles.ref();
320         stage.add_child(this.clutterfiles.el);
321         this.clutterfiles.el.show_all();
322
323
324         this.clutterfiles.open.connect((file) => { 
325             this.fileViewOpen(file);
326         });
327
328     }
329     public void fileViewOpen(JsRender.JsRender file)
330     {
331         this.project = file.project;
332         this.previewShow();
333         this.left_tree.model.loadFile(file);
334         
335         var ctr= ((Gtk.Container)(this.win.rooview.el.get_widget()));
336         var ctr_p= ((Gtk.Container)(this.win.projecteditview.el.get_widget()));
337         
338         if (file.xtype == "Roo" ) { 
339             ctr.foreach( (w) => { ctr.remove(w); });
340             ctr_p.foreach( (w) => { ctr_p.remove(w); });
341             ctr.add(this.window_rooview.el);
342             ctr_p.add(this.projectsettings.el);            
343             this.window_rooview.loadFile(file);
344             this.window_rooview.el.show_all();
345             this.projectsettings.el.show_all();            
346
347         } else {
348             ctr.foreach( (w) => { ctr.remove(w); });
349             ctr_p.foreach( (w) => { ctr_p.remove(w); });            
350             ctr.add(this.window_gladeview.el);
351             ctr_p.add(this.vala_projectsettings.el);
352             this.window_gladeview.loadFile(file);
353             this.window_gladeview.el.show_all();
354             this.vala_projectsettings.el.show_all();
355         }
356         print("OPEN : " + file.name);
357         this.editpane.el.set_position(_this.editpane.el.max_position);
358         this.win.setTitle(file.project.name + " : " +file.name);
359              
360
361     }
362
363     
364     // ---------  webkit view
365     public void webkitViewInit()
366     {
367         this.window_rooview  =new Xcls_WindowRooView();
368         this.window_rooview.ref();
369         ((Gtk.Container)(this.win.rooview.el.get_widget())).add(this.window_rooview.el);
370         this.window_rooview.el.show_all();
371
372         stage = this.win.rooview.el.get_stage();
373         stage.set_background_color(  Clutter.Color.from_string("#000"));
374     }
375
376     // ------ Gtk  - view
377
378     public void gtkViewInit()
379     {
380         this.window_gladeview  =new Xcls_GtkView();
381         this.window_gladeview.ref();
382     }
383
384
385
386     
387 }
388
389