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         this.codeEditInit();
46         this.fileViewInit();
47         
48         // adding stuff
49         this.objectAddInit();
50         this.propsAddInit();
51         this.listenerInit();
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             case State.CODEEDIT:
142                 // SAVE FIRST???
143                 
144                 this.codeEditHide();
145                 break;
146                
147                                 
148         }
149          
150
151     }
152
153
154
155
156     public void propsListInit()
157     {
158         
159         this.left_props =new Xcls_LeftProps();
160         this.left_props.ref();
161         this.left_props.main_window = _this;
162         this.win.props.el.pack_start(this.left_props.el,true, true,0);
163         this.left_props.el.show_all();
164         
165         this.left_props.show_editor.connect( (file, node, type,  key) => {
166             this.codeEditShow(file, node, type,  key);
167         });
168
169         
170         this.left_props.stop_editor.connect( () => {
171             if (this.state != "codeedit") {
172                 return true;
173             }
174         
175             var ret =  this.code_editor.saveContents();
176             if (!ret) {
177                 return false;
178             }
179             this.codeEditHide();
180             return ret;
181         });
182         
183         this.left_props.changed.connect(() => {
184               if (this.left_tree.getActiveFile().xtype == "Roo" ) {
185                    this.window_rooview.requestRedraw();
186                    
187                } else {
188                   this.window_gladeview.loadFile(this.left_tree.getActiveFile());
189               }
190               this.left_tree.model.updateSelected();
191               this.left_tree.model.file.save();
192         });
193         
194
195
196     }
197
198     //-------------  projects edit
199
200     public void projectEditInit()
201     {
202         this.projectsettings  =new Xcls_ProjectSettings();
203         this.projectsettings.ref();  /// really?
204         
205         this.vala_projectsettings  =new ValaProjectSettings();
206         this.vala_projectsettings.ref();
207         this.vala_projectsettings.window = this;
208         
209         ((Gtk.Container)(this.win.projecteditview.el.get_widget())).add(this.projectsettings.el);
210         //this.projectsettings.el.show_all();
211
212         var stage = this.win.projecteditview.el.get_stage();
213         stage.set_background_color(  Clutter.Color.from_string("#000"));
214         
215         this.projectsettings.buttonPressed.connect((btn) => {
216              if (this.left_tree.getActiveFile().xtype == "Roo" ) {
217                 if (btn == "save") {
218                     this.window_rooview.view.renderJS(true);
219                 }
220                 if (btn == "apply") {
221                     this.window_rooview.view.renderJS(true);
222                     return;
223                 }
224             } else {
225                 // do nothing for gtk..
226             }
227             if (btn == "save" || btn == "apply") {
228                 this.win.project.save();
229      
230             }
231             
232             this.projectEditHide();
233              
234          });
235
236     }
237     // ----------- object adding
238     public void objectAddInit()
239     {
240
241         this.rightpalete  = new Xcls_RightPalete();
242         this.rightpalete.ref();  /// really?
243         ((Gtk.Container)(this.win.objectview.el.get_widget())).add(this.rightpalete.el);
244         //this.projectsettings.el.show_all();
245
246         stage = _this.win.objectview.el.get_stage();
247         stage.set_background_color(  Clutter.Color.from_string("#000"));
248        
249     }
250     
251     // -----------  properties adding list...
252     // listener uses the properties 
253     public void propsAddInit()
254     {
255         // Add properties
256         this.add_props  = new Xcls_WindowAddProp();
257         this.add_props.ref();  /// really?
258         ((Gtk.Container)(this.win.addpropsview.el.get_widget())).add(this.add_props.el);
259         //this.projectsettings.el.show_all();
260
261         var  stage = _this.win.addpropsview.el.get_stage();
262         stage.set_background_color(  Clutter.Color.from_string("#000"));
263
264         
265         this.add_props.select.connect( (key,type,skel, etype) => {
266             this.left_props.addProp(etype, key, skel, type);
267         });
268         
269     }
270     public void propsAddShow()
271     {
272
273     }
274     public void propsAddHide()
275     {
276         
277     }
278
279
280
281     
282     // ----------- Add / Edit listener
283     // listener uses the properties 
284     public void listenerInit()
285     {
286
287     }
288     public void listenerShow()
289     {
290
291     }
292     public void listenerHide()
293     {
294         
295     }
296
297     // -------------- codeEditor
298
299     public void codeEditInit()
300     {
301         this.code_editor  = new  Editor();
302         this.code_editor.ref();  /// really?
303         ((Gtk.Container)(this.win.codeeditview.el.get_widget())).add(this.code_editor.el);
304         //this.projectsettings.el.show_all();
305
306         stage = _this.win.codeeditview.el.get_stage();
307         stage.set_background_color(  Clutter.Color.from_string("#000"));
308         // editor.save...
309
310         this.code_editor.save.connect( () => {
311              this.left_tree.model.file.save();
312              this.left_tree.model.updateSelected();
313         });
314     
315     }
316
317     // ----------- file view
318
319     public void fileViewInit()
320     {
321
322         this.clutterfiles = new Xcls_ClutterFiles();
323         this.clutterfiles.ref();
324         stage.add_child(this.clutterfiles.el);
325         this.clutterfiles.el.show_all();
326
327
328         this.clutterfiles.open.connect((file) => { 
329             this.fileViewOpen(file);
330         });
331
332     }
333     public void fileNewInit()
334     {
335         this.new_file_dialog = new Xcls_DialogNewComponent();
336         // force it modal to the main window..
337         this.new_file_dialog.el.set_transient_for(this.el);
338         this.new_file_dialog.el.set_modal(true);
339         
340         this.new_file_dialog.success.connect((project,file) =>
341         {
342             this.fileViewOpen(file);
343         });
344
345     }
346
347     
348     public void fileViewOpen(JsRender.JsRender file)
349     {
350         this.win.project = file.project;
351         this.previewShow();
352         this.left_tree.model.loadFile(file);
353         
354         var ctr= ((Gtk.Container)(this.win.rooview.el.get_widget()));
355         var ctr_p= ((Gtk.Container)(this.win.projecteditview.el.get_widget()));
356         
357         if (file.xtype == "Roo" ) { 
358             ctr.foreach( (w) => { ctr.remove(w); });
359             ctr_p.foreach( (w) => { ctr_p.remove(w); });
360             ctr.add(this.window_rooview.el);
361             ctr_p.add(this.projectsettings.el);            
362             this.window_rooview.loadFile(file);
363             this.window_rooview.el.show_all();
364             this.projectsettings.el.show_all();            
365
366         } else {
367             ctr.foreach( (w) => { ctr.remove(w); });
368             ctr_p.foreach( (w) => { ctr_p.remove(w); });            
369             ctr.add(this.window_gladeview.el);
370             ctr_p.add(this.vala_projectsettings.el);
371             this.window_gladeview.loadFile(file);
372             this.window_gladeview.el.show_all();
373             this.vala_projectsettings.el.show_all();
374         }
375         print("OPEN : " + file.name);
376         this.editpane.el.set_position(_this.editpane.el.max_position);
377         this.win.setTitle(file.project.name + " : " +file.name);
378              
379
380     }
381
382     
383     // ---------  webkit view
384     public void webkitViewInit()
385     {
386         this.window_rooview  =new Xcls_WindowRooView();
387         this.window_rooview.ref();
388         ((Gtk.Container)(this.win.rooview.el.get_widget())).add(this.window_rooview.el);
389         this.window_rooview.el.show_all();
390
391         stage = this.win.rooview.el.get_stage();
392         stage.set_background_color(  Clutter.Color.from_string("#000"));
393     }
394
395     // ------ Gtk  - view
396
397     public void gtkViewInit()
398     {
399         this.window_gladeview  =new Xcls_GtkView();
400         this.window_gladeview.ref();
401     }
402
403
404
405     
406 }
407
408