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