src/Builder4/About.vala.c
[roobuilder] / 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 Xcls_MainWindow win;
11  
12         public enum State {
13                 NONE,
14                 PREVIEW,
15                 OBJECT,
16                 PROP,
17                 LISTENER,
18                 CODE,    // code editor.
19                 CODEONLY,
20                 FILES //,
21                 // PROJECTCODEONLY // when editing code...              ??
22                 //PROJECT, // project settings..
23                 //FILEPROJECT, // project when in file mode
24
25         }
26
27         public State state = State.NONE;
28
29         public bool children_loaded = false;
30
31          
32         public Project.Project project;
33         public JsRender.JsRender file;
34          
35         
36         public Xcls_WindowLeftTree  left_tree;
37         public Xcls_WindowAddProp   add_props;
38         public Xcls_LeftProps       left_props;
39         public Xcls_RooProjectSettings roo_projectsettings_pop;
40         public Xcls_ValaProjectSettingsPopover  vala_projectsettings_pop;
41         public Xcls_RightPalete     rightpalete;
42         public Editor               code_editor;    
43         public Xcls_WindowRooView   window_rooview;
44         public Xcls_GtkView         window_gladeview;
45         
46         public Xcls_ClutterFiles     clutterfiles;
47
48         public Xcls_WindowLeftProjects left_projects; // can not see where this is initialized.. 
49         
50         public DialogTemplateSelect template_select; 
51         
52         public Xcls_PopoverFileDetails file_details;
53         
54         
55         public Xcls_ValaCompileResults compile_results;
56         
57         // dialogs??
58         public Xcls_DialogPluginWebkit webkit_plugin;
59         
60         
61         public Palete.ValaSource valasource; // the spawner that runs the vala compiler.
62         public Json.Object last_compile_result;
63         
64         // ctor 
65         public WindowState(Xcls_MainWindow win)
66         {
67                 this.win = win;
68                 // initialize
69
70                 // left elements..
71                 this.leftTreeInit();
72                 this.propsListInit();
73
74                 // on clutter space...
75                 this.projectEditInit();
76                 this.codeEditInit();
77                 this.projectListInit();
78                 this.fileViewInit();
79
80                 // adding stuff
81                 this.objectAddInit();
82                 this.propsAddInit();
83
84
85                 // previews...
86                 this.gtkViewInit();
87                 this.webkitViewInit();
88
89                 // dialogs
90
91                 this.fileDetailsInit();
92
93                 this.webkit_plugin = new Xcls_DialogPluginWebkit();
94                 this.template_select = new DialogTemplateSelect();
95                 this.children_loaded = true;
96                 
97                 
98                 this.valasource = new Palete.ValaSource();
99                 this.valasource.compiled.connect(this.showCompileResult);
100                 
101                 this.compile_results = new  Xcls_ValaCompileResults();
102                 this.compile_results.window = this.win;
103                 this.valasource.compile_output.connect(this.compile_results.addLine);
104                 
105                 this.win.statusbar_compilestatus_label.el.hide();
106                 this.win.statusbar_run.el.hide();
107                 this.win.search_results.el.hide();
108         }
109
110
111         // left tree
112
113         public void leftTreeInit()
114         {
115          
116                 this.left_tree = new Xcls_WindowLeftTree();
117                 this.left_tree.ref();
118                 this.left_tree.main_window = this.win;
119         
120                 this.win.tree.el.pack_start(this.left_tree.el,true, true,0);
121                 this.left_tree.el.show_all();
122                    
123                 this.left_tree.before_node_change.connect(() => {
124                         // if the node change is caused by the editor (code preview)
125                         if (this.left_tree.view.lastEventSource == "editor") {
126                                 return true;
127                         }
128                         return this.leftTreeBeforeChange();
129
130                 });
131                 // node selected -- only by clicking?
132                 this.left_tree.node_selected.connect((sel, source) => {
133                         if (source == "editor") {
134                                 return;
135                         }
136                         if (this.file.xtype == "Roo") { 
137                                 this.window_rooview.sourceview.nodeSelected(sel,true); // foce scroll.
138                         } else {
139                                 this.window_gladeview.sourceview.nodeSelected(sel);
140                         }
141                 });
142                 
143                 this.left_tree.node_selected.connect((sel, source) => {
144                         this.leftTreeNodeSelected(sel, source);
145                 });
146          
147                 this.left_tree.changed.connect(() => {
148                         print("LEFT TREE: Changed fired\n");
149                         this.file.save();
150                         if (this.left_tree.getActiveFile().xtype == "Roo" ) {
151                                    this.window_rooview.requestRedraw();
152                         } else {
153                                   this.window_gladeview.loadFile(this.left_tree.getActiveFile());
154                         }
155                          
156                 });
157                  
158         }
159
160         public bool leftTreeBeforeChange()
161         {
162                 if (this.state != State.CODE) {
163                         this.left_props.finish_editing();
164                         return true;
165                 }
166                 if (!this.code_editor.saveContents()) {
167                         return false;
168                 }
169                 return false;
170         }
171         
172         public void leftTreeNodeSelected(JsRender.Node? sel, string source)
173         {
174
175                 print("node_selected called %s\n", (sel == null) ? "NULL" : "a value");
176
177                 if (sel == null) {
178                         this.left_props.el.hide();
179                 } 
180                 this.left_props.el.show();
181                 this.left_props.load(this.left_tree.getActiveFile(), sel);
182                 switch (this.state) {
183                         
184                         case State.OBJECT: 
185                                   
186                                  if (sel == null) {
187                                         this.rightpalete.clear();
188                                         break;
189                                 }
190                                 this.rightpalete.load(this.left_tree.getActiveFile().palete(), sel.fqn());
191                                 break;
192                                  
193                 
194                    case State.PROP:
195                                 if (sel == null) {
196                                         this.add_props.clear();
197                                         break;
198                                 }
199                                 this.add_props.show(this.left_tree.getActiveFile().palete(), "props", sel.fqn());
200                                 break;
201
202                         case State.LISTENER:
203                            
204                                 if (sel == null) {
205                                         this.add_props.clear();
206                                         break;
207                                 }
208                                 this.add_props.show(this.left_tree.getActiveFile().palete(), "signals", sel.fqn());
209                                 break;
210                                 
211                         case State.CODE:
212                                  this.switchState(State.PREVIEW);
213                          
214                                 break;
215                            
216                                                         
217                 }
218                  
219
220         }
221
222
223
224
225         public void propsListInit()
226         {
227         
228                 this.left_props =new Xcls_LeftProps();
229                 this.left_props.ref();
230                 this.left_props.main_window = this.win;
231                 this.win.props.el.pack_start(this.left_props.el,true, true,0);
232                 this.left_props.el.show_all();
233         
234                 this.left_props.show_editor.connect( (file, node, type,  key) => {
235                         this.switchState(State.CODE);
236                         this.code_editor.show(
237                                 file,
238                                 node,
239                                 type,
240                                 key
241                         );
242                         
243                         
244                 });
245
246    
247                 this.left_props.stop_editor.connect( () => {
248                         if (this.state != State.CODE) {
249                                 return true;
250                         }
251         
252                         var ret =  this.code_editor.saveContents();
253                         if (!ret) {
254                                 return false;
255                         }
256                         this.switchState(State.PREVIEW);
257                         return ret;
258                 });
259         
260                 this.left_props.changed.connect(() => {
261                         if (this.left_tree.getActiveFile().xtype == "Roo" ) {
262                                    this.window_rooview.requestRedraw();
263                         } else {
264                                   this.window_gladeview.loadFile(this.left_tree.getActiveFile());
265                         }
266                         this.left_tree.model.updateSelected();
267                         this.file.save();
268                         if (this.file.xtype=="Gtk") {
269                                 this.valasource.checkFileSpawn(this.file);
270                         }
271                 });
272         
273
274
275         }
276
277         //-------------  projects edit
278
279         public void projectEditInit()
280         {
281                 this.roo_projectsettings_pop  =new Xcls_RooProjectSettings();
282                 this.roo_projectsettings_pop.ref();  /// really?
283         
284                 this.vala_projectsettings_pop  =new Xcls_ValaProjectSettingsPopover();
285                 this.vala_projectsettings_pop.ref();
286                 this.vala_projectsettings_pop.window = this.win;
287         
288                 //((Gtk.Container)(this.win.projecteditview.el.get_widget())).add(this.projectsettings.el);
289  
290  
291                 this.roo_projectsettings_pop.buttonPressed.connect((btn) => {
292                         // in theory active file can only be rooo...
293                          if (this.left_tree.getActiveFile().xtype == "Roo" ) {
294                                 if (btn == "save") {
295                                         this.window_rooview.view.renderJS(true);
296                                         this.roo_projectsettings_pop.el.hide();
297                                 }
298                                 if (btn == "apply") {
299                                         this.window_rooview.view.renderJS(true);
300                                         return;
301                                 }
302                         } else {
303                                 // do nothing for gtk..
304                         }
305                         if (btn == "save" || btn == "apply") {
306                                 this.win.project.save();
307                  
308                         }
309                         //this.switchState (State.PREVIEW); 
310                          
311                  });
312
313         }
314         public void projectPopoverShow(Gtk.Widget btn)
315         {
316                 if (this.left_tree.getActiveFile().xtype == "Roo" ) {
317                         this.roo_projectsettings_pop.show(btn,this.project);
318                         return;
319                 }
320                 // gtk..
321                 this.vala_projectsettings_pop.show(btn,(Project.Gtk)this.project);
322         
323         }
324         
325         
326         // ----------- object adding
327         public void objectAddInit()
328         {
329
330                 this.rightpalete  = new Xcls_RightPalete();
331                 this.rightpalete.ref();  /// really?
332                 ((Gtk.Container)(this.win.objectview.el.get_widget())).add(this.rightpalete.el);
333  
334
335                 var stage = this.win.objectview.el.get_stage();
336                 stage.set_background_color(  Clutter.Color.from_string("#000"));
337                    
338         }
339         
340         // -----------  properties adding list...
341         // listener uses the properties 
342         public void propsAddInit()
343         {
344         // Add properties
345                 this.add_props  = new Xcls_WindowAddProp();
346                 this.add_props.ref();  /// really?
347                 ((Gtk.Container)(this.win.addpropsview.el.get_widget())).add(this.add_props.el);
348  
349
350                 var  stage = this.win.addpropsview.el.get_stage();
351                 stage.set_background_color(  Clutter.Color.from_string("#000"));
352
353
354                 this.add_props.select.connect( (key,type,skel, etype) => {
355                         this.left_props.addProp(etype, key, skel, type);
356                 });
357
358         }
359         public void propsAddShow()
360         {
361
362         }
363         public void propsAddHide()
364         {
365         
366         }
367
368
369
370         
371         // ----------- Add / Edit listener
372         // listener uses the properties 
373         //public void listenerInit()     { }
374         public void listenerShow()
375         {
376
377         }
378         public void listenerHide()
379         {
380         
381         }
382
383         // -------------- codeEditor
384
385         public void codeEditInit()
386         {
387                 this.code_editor  = new  Editor();
388                 //this.code_editor.ref();  /// really?
389                 ((Gtk.Container)(this.win.codeeditview.el.get_widget())).add(this.code_editor.el);
390                 
391                 this.code_editor.window = this.win;
392  
393
394                 var stage = this.win.codeeditview.el.get_stage();
395                 stage.set_background_color(  Clutter.Color.from_string("#000"));
396                 // editor.save...
397
398                 this.code_editor.save.connect( () => {
399                         this.file.save();
400                         this.left_tree.model.updateSelected();
401                         if (this.left_tree.getActiveFile().xtype == "Roo" ) {
402                                    this.window_rooview.requestRedraw();
403                         } else {
404                                   this.window_gladeview.loadFile(this.left_tree.getActiveFile());
405                         }
406                          // we do not need to call spawn... - as it's already called by the editor?
407                          
408                 });
409                 
410         }
411
412         // ----------- list of projects on left
413         public void  projectListInit() 
414         {
415
416                 this.left_projects = new Xcls_WindowLeftProjects();
417                  this.left_projects.ref();
418                  this.win.leftpane.el.pack_start(this.left_projects.el,true, true,0);
419                  this.left_projects.el.show_all();
420                  this.left_projects.project_selected.connect((proj) => {
421                         this.buttonsShowHide();
422                         proj.scanDirs();
423                         this.clutterfiles.loadProject(proj);
424                 
425                  });
426
427         }
428         // ----------- file view
429
430         public void fileViewInit()
431         {
432                 var stage = this.win.rooview.el.get_stage(); // seems odd... 
433                 this.clutterfiles = new Xcls_ClutterFiles();
434                 this.clutterfiles.ref();
435                 stage.add_child(this.clutterfiles.el);
436                 this.clutterfiles.el.show();
437
438
439                 this.clutterfiles.open.connect((file) => { 
440                         this.fileViewOpen(file);
441                 });
442                 this.clutterfiles.el.transitions_completed.connect(() => {
443                         if (this.state == State.FILES) {
444                                 this.win.rooview.el.hide();
445                         } else {
446                                 this.clutterfiles.el.hide();
447                         }
448                         
449                         
450                 });
451
452         }
453  
454         public void fileDetailsInit()
455         {
456                 this.file_details = new Xcls_PopoverFileDetails();
457                 this.file_details.mainwindow = this.win;
458                 // force it modal to the main window..
459                 
460                 this.file_details.success.connect((project,file) =>
461                 {
462                         this.fileViewOpen(file);
463                 });
464
465         }
466         
467         public void fileViewOpen(JsRender.JsRender file, int line = -1)
468         {
469                 this.win.project = file.project;
470                 this.project = file.project;
471                 this.file = file;
472                 
473                 
474                 if (file.xtype == "PlainFile") {
475                         this.switchState (State.CODEONLY); 
476                         file.loadItems();
477                         this.code_editor.show(file, null, "", "");
478                         if (line> -1) {
479                                 this.code_editor.scroll_to_line(line);
480                         }
481                 } else {
482                 
483                         this.switchState (State.PREVIEW); 
484                         // this triggers loadItems..
485                         this.left_tree.model.loadFile(file);
486                         if (file.project.xtype == "Gtk" && line> -1 ) {
487                                 this.window_gladeview.scroll_to_line(line);
488                         }
489
490                 }
491         
492         
493                 var ctr= ((Gtk.Container)(this.win.rooview.el.get_widget()));
494  
495         
496                 if (file.project.xtype == "Roo" ) { 
497                         ctr.foreach( (w) => { ctr.remove(w); });
498  
499                         ctr.add(this.window_rooview.el);
500  
501                         if (file.xtype != "PlainFile") {       
502                                 this.window_rooview.loadFile(file);
503                                 this.window_rooview.el.show_all();
504                         }
505  
506                         
507
508                 } else {
509                         ctr.foreach( (w) => { ctr.remove(w); });
510
511                         ctr.add(this.window_gladeview.el);
512  
513                         if (file.xtype != "PlainFile") {    
514                                 this.window_gladeview.loadFile(file);
515                                 this.window_gladeview.el.show_all();
516                         }
517  
518                 }
519                 print("OPEN : " + file.name);
520                 if (file.xtype != "PlainFile") {    
521                         this.win.editpane.el.set_position(this.win.editpane.el.max_position);
522                 }
523                 this.win.setTitle(file.project.name + " : " + file.name);
524                          
525
526         }
527  
528  
529          /*
530         public void fileViewOpenPlain(string fname)
531         {
532                 
533                 this.switchState (State.CODEONLY); 
534                 this.code_editor.showPlainFile(fname);
535         }
536  */
537          
538         // ---------  webkit view
539         public void webkitViewInit()
540         {
541                 this.window_rooview  =new Xcls_WindowRooView();
542                 this.window_rooview.main_window = this.win;
543                 this.window_rooview.ref();
544                 ((Gtk.Container)(this.win.rooview.el.get_widget())).add(this.window_rooview.el);
545                 this.window_rooview.el.show_all();
546
547                 var stage = this.win.rooview.el.get_stage();
548                 stage.set_background_color(  Clutter.Color.from_string("#000"));
549         }
550
551         // ------ Gtk  - view
552
553         public void gtkViewInit()
554         {
555                 this.window_gladeview  =new Xcls_GtkView();
556                 this.window_gladeview.ref();
557                 this.window_gladeview.main_window = this.win;
558         }
559         
560         public void easingSaveAll()
561         {
562                 this.win.addpropsview.el.save_easing_state();
563                 this.win.codeeditview.el.save_easing_state();
564                 this.win.objectview.el.save_easing_state();
565                 this.win.rooview.el.save_easing_state();
566                 this.clutterfiles.el.save_easing_state();
567                  
568         }
569         public void easingRestoreAll()
570         {
571                 this.win.addpropsview.el.restore_easing_state();
572                 this.win.codeeditview.el.restore_easing_state();
573                 this.win.objectview.el.restore_easing_state();
574                 this.win.rooview.el.restore_easing_state();
575                 this.clutterfiles.el.restore_easing_state();
576                 
577         }
578         public void switchState(State new_state)
579         {
580                 
581                 // if the new state and the old state are the same..
582                 
583                 if (new_state == this.state) {
584                         return;
585                 }
586                 
587                 // stop werid stuff happening
588                 
589                 if (this.state == State.FILES 
590                         //&& new_state == State.FILEPROJECT 
591                         && this.left_projects.getSelectedProject() == null) {
592                         return;
593                 }
594                 // save the easing state of everything..
595                 this.easingSaveAll();
596                 
597                 switch (this.state) {
598
599                         case State.PREVIEW:
600                                 if (this.left_tree.getActiveFile() != null) {
601                                          if (this.left_tree.getActiveFile().xtype == "Roo" ) {
602                                                  this.window_rooview.createThumb();
603                                          } else {
604                                                   this.window_gladeview.createThumb();
605                                           }
606                                 }
607                                 // normally we are going from preview to another state.
608                                 // and different windows hide the preview in differnt ways..
609                                 
610                                 break;
611                                 
612                         
613                    case State.LISTENER:
614                    case State.PROP:
615                                 
616                                 this.win.addpropsview.el.set_scale(0.0f,0.0f);
617                                  break;
618                                 
619                         case State.CODE:
620                                 this.code_editor.saveContents();
621                           
622                                 this.win.codeeditview.el.set_scale(0.0f,0.0f);
623                                 break;
624                                 
625                         case State.CODEONLY:
626                                 // going from codeonly..
627                                 
628                                 // enable re-calc of canvas..
629
630                                 //this.code_editor.saveContents(); << not yet...
631
632                                 this.win.rooview.el.show(); 
633                                 this.win.leftpane.el.show();
634                                 this.win.codeeditview.el.set_scale(0.0f,0.0f);
635                         
636                                 /*this.win.codeeditview.el.set_easing_duration(1000);
637                                 this.win.codeeditview.el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, 0.0f);
638                                 //this.win.codeeditview.el.set_scale(1.0f,1.0f);
639                                 this.win.codeeditview.el.set_pivot_point(0.5f,0.5f);
640                                 this.win.codeeditview.el.set_opacity(0xff);
641                         */
642                         
643                             while (Gtk.events_pending()) { 
644                                         Gtk.main_iteration();
645                                 }
646                                 
647                                  // hides it completely...
648                                  
649                                 break;
650
651                          case State.OBJECT:
652                            
653                                 this.win.objectview.el.set_scale(0.0f,0.0f);
654                                  break;
655
656                         /*case State.FILEPROJECT:
657                         //case State.PROJECT:
658                         //case State.PROJECTCODEONLY:
659                                 if (this.win.project.xtype == "Gtk") {
660                                         this.vala_projectsettings.save();
661                                 } 
662                                 if (this.state == State.FILEPROJECT) {
663                                         this.clutterfiles.el.set_scale(1.0f,1.0f);
664                                 
665                                 }
666                                 this.win.projecteditview.el.set_scale(0.0f,0.0f);
667                                 break;
668                         */
669                   case State.FILES: // goes to preview or codeonly...
670                                 // hide files...
671                                 
672                                 
673                                 /*if (new_state == State.FILEPROJECT) {
674                                 
675                                         this.clutterfiles.el.set_easing_duration(1000);
676                                         this.clutterfiles.el.set_pivot_point(0.5f,1.0f);
677                                         this.clutterfiles.el.set_scale(0.5f,0.5f);
678                                         break;
679                                 }
680                                 */
681  
682                                 if (new_state == State.CODEONLY) {
683                                         this.win.rooview.el.hide();
684                                 } else {
685                                         this.win.rooview.el.show();
686                                 }
687                                 
688                                 this.win.rooview.el.set_easing_duration(1000);
689                                 this.win.rooview.el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, 0.0f);
690                                 this.win.rooview.el.set_scale(1.0f,1.0f);
691                                 this.win.rooview.el.set_pivot_point(0.5f,0.5f);
692                                 this.win.rooview.el.set_opacity(0xff);
693                                 
694                                 this.clutterfiles.el.set_easing_duration(1000);
695                                 this.clutterfiles.el.set_pivot_point(0.5f,0.5f);
696                                 this.clutterfiles.el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, -180.0f);
697                                 this.clutterfiles.el.set_opacity(0);
698  
699                            
700                                 //this.clutterfiles.el.hide();
701                                  
702
703                                 break;
704
705                                 
706                 }
707            
708                 var oldstate  =this.state;
709                 this.state = new_state;
710                 
711                 
712                                 
713                 this.buttonsShowHide();
714                 
715                 
716                 switch (this.state) {
717                         
718                         case State.PREVIEW:  // this is the default state when working...
719                                  this.win.editpane.el.show(); // holder for tree and properties..
720                                  
721                          
722                                  this.left_projects.el.hide(); 
723                                  if (oldstate != State.FILES) {
724                                         // it's handled above..
725                                         print ("changing state to preview from NOT files..");
726                                          
727  
728                                         this.win.rooview.el.set_scale(1.0f,1.0f);
729                                  }
730                            
731                                 break;
732
733
734                         case State.LISTENER:
735                 // same as prop?
736                         case State.PROP:
737                                 var ae =      this.left_tree.getActiveElement();
738                                 if (ae == null) {
739                                         this.state = oldstate;
740                                         this.buttonsShowHide();
741                                         this.resizeCanvasElements();
742                                         this.easingRestoreAll();
743                                         return;
744                                 }
745                                 this.add_props.el.show_all();
746                                 this.add_props.show(
747                                         Palete.factory(this.win.project.xtype), 
748                                         this.state == State.LISTENER ? "signals" : "props",
749                                         ae.fqn()
750                                 );
751  
752                                         
753  
754                                 
755                                 // -- FIXME? this needs to be State aware?
756                  
757                                 this.win.rooview.el.set_pivot_point(1.0f,0.5f);
758                                   
759                                 this.win.addpropsview.el.set_scale(1.0f,1.0f);
760                                 break;
761                    
762                         case State.OBJECT:
763                                  var n = this.left_tree.getActiveElement();
764
765                                 if (this.file == null) {
766                                         this.state =oldstate;
767                                         this.buttonsShowHide();
768                                         this.resizeCanvasElements();
769                                         this.easingRestoreAll();
770                                         return;
771                                 }
772                                 
773                                 if (n == null && this.file.tree != null) {
774                                         this.state = oldstate;
775                                         this.buttonsShowHide();
776                                         this.resizeCanvasElements();
777                                         this.easingRestoreAll();
778                                         return;
779                                 }
780
781                                 this.rightpalete.el.show_all();
782                                 this.rightpalete.load(this.left_tree.getActiveFile().palete(), n == null ? "*top" : n.fqn());
783
784                                 
785                           
786                                 this.win.rooview.el.set_pivot_point(1.0f,0.5f);
787                                 this.win.objectview.el.set_scale(1.0f,1.0f);
788                                  
789                                 break;
790                    
791                    
792                         case State.CODE:
793                                 this.win.codeeditview.el.show();
794                                 this.code_editor.el.show_all();
795                                 // caller needs to call editor - show....
796                                 this.win.codeeditview.el.set_scale(1.0f,1.0f);
797                                 this.win.rooview.el.set_pivot_point(1.0f,0.5f);
798
799                                 break;
800
801                         case State.CODEONLY:
802                                 // going to codeonly..
803                                 this.win.codeeditview.el.show();
804                                 // recalc canvas...
805                                 //while (Gtk.events_pending()) { 
806                                 //      Gtk.main_iteration();
807                                 //}
808                                 
809                                 this.win.leftpane.el.hide();
810                                 this.win.codeeditview.el.show();
811                                 //while (Gtk.events_pending()) { 
812                                 //      Gtk.main_iteration();
813                                 //}
814                                 
815                                 
816                                 this.code_editor.el.show_all();
817                             
818                                 this.win.codeeditview.el.set_scale(1.0f,1.0f);
819                                 this.win.rooview.el.set_pivot_point(1.0f,0.5f);
820                                 break;
821
822                         /*case State.PROJECTCODEONLY:
823                             // going to project edit (when in code only)
824
825                                 
826                             
827                                 if (this.win.project.xtype == "Roo") {
828                                         this.projectsettings.el.show_all();
829                                         this.projectsettings.show(this.win.project);
830                                 } else {
831                                         this.vala_projectsettings.el.show_all();
832                                         this.vala_projectsettings.show((Project.Gtk)this.win.project);
833                                 }
834  
835                                 this.win.projecteditview.el.set_scale(1.0f,1.0f);
836                                  
837                                 break;
838
839
840                         case State.PROJECT:
841                                 this.win.rooview.el.set_pivot_point(1.0f,1.0f); // bottom right..
842                                 
843                                 if (this.win.project.xtype == "Roo") {
844                                         this.projectsettings.el.show_all();
845                                         this.projectsettings.show(this.win.project);
846                                 } else {
847                                         this.vala_projectsettings.el.show_all();
848                                         this.vala_projectsettings.show((Project.Gtk)this.win.project);
849                                 }
850  
851                                 this.win.projecteditview.el.set_scale(1.0f,1.0f);
852                                  
853                                 break;
854                                 
855                         case State.FILEPROJECT:
856                                 var pr = this.left_projects.getSelectedProject();
857
858                                 this.win.project = pr;
859                                 
860                                 this.attachProjectSettings();
861                                 print("FIlE PROJECT -  show %s\n", pr.xtype);
862                                 
863                                 if (pr.xtype == "Roo") {
864                                         this.projectsettings.el.show_all();
865                                         this.projectsettings.show(this.win.project);
866                                 } else {
867                                         this.vala_projectsettings.el.show_all();
868                                         this.vala_projectsettings.show((Project.Gtk)this.win.project);
869                                 }
870  
871                                 this.win.projecteditview.el.set_scale(1.0f,1.0f);
872                                  
873                                 break;
874                         */
875                    case State.FILES:  // can only get here from PREVIEW (or code-only) state.. in theory..
876                                 
877    
878                                 this.win.editpane.el.hide(); // holder for tree and properties..
879                                 
880                                 this.left_projects.el.show(); 
881                                 
882                                 // rotate the preview to hidden...
883                                 this.win.rooview.el.set_easing_duration(1000);
884                                 this.win.rooview.el.set_pivot_point(0.5f,0.5f);
885                                 this.win.rooview.el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, 180.0f);
886                                 this.win.rooview.el.set_opacity(0);
887                          
888                                 
889                                 
890          
891                                 if (this.win.project != null) {
892                                         this.left_projects.selectProject(this.win.project);
893                                 }
894                          
895                                 
896                                 this.clutterfiles.el.show();
897                                  
898                                 this.clutterfiles.el.set_easing_duration(1000);
899                                 this.clutterfiles.el.set_pivot_point(0.5f,0.5f);
900                                 this.clutterfiles.el.set_rotation_angle(Clutter.RotateAxis.Y_AXIS, 0.0f);
901                                 this.clutterfiles.el.set_opacity(0xff);
902                                 
903                                  
904                                 
905                                 break;
906
907
908                 }
909                 this.resizeCanvasElements();
910                 this.easingRestoreAll();
911                 
912                 // run the animation.. - then load files...
913                 GLib.Timeout.add(500,  ()  =>{
914                          this.resizeCanvasElements();
915                          return false;
916                 });
917                         
918         }
919         
920         public int redraw_count = 0;
921         public void resizeCanvas() // called by window resize .. delays redraw
922         {
923                 var rc = this.redraw_count;        
924                 this.redraw_count = 2;
925                 if (rc == 0) {
926                         GLib.Timeout.add(100,  ()  =>{
927                                  return this.resizeCanvasQueue();
928                         });
929                 }
930         }
931         public bool  resizeCanvasQueue()
932         {
933                 //print("WindowState.resizeCanvasQueue %d\n", this.redraw_count);        
934
935                 if (this.redraw_count < 1) {
936                         return false; // should not really happen...
937                 }
938
939
940                 this.redraw_count--;
941
942                 if (this.redraw_count > 0) {
943                         return true; // do it again in 1 second...
944                 }
945                 // got down to 0 or -1....
946                 this.redraw_count = 0;
947                 this.resizeCanvasElements();
948                 return false;
949
950         }
951         public void resizeCanvasElements()
952         {
953                 Gtk.Allocation alloc;
954                 this.win.clutterembed.el.get_allocation(out alloc);
955
956            // print("WindowState.resizeCanvasElements\n");
957                 if (!this.children_loaded || this.win.clutterembed == null) {
958                         print("WindowState.resizeCanvasElements = ingnore not loaded or no clutterfiles\n");
959                         return; 
960                 }
961                 
962                 var avail = alloc.width < 50.0f ? 0 :  alloc.width - 50.0f;
963                 var palsize = avail < 300.0f ? avail : 300.0f;
964                    
965  
966                 // -------- code edit min 600
967                 
968                 var codesize = avail < 800.0f ? avail : 800.0f;
969                 
970                 
971                 //print("set code size %f\n", codesize);
972
973                         
974                 
975                 switch ( this.state) {
976                         case State.PREVIEW:
977                                  
978                                 this.win.rooview.el.set_size(alloc.width-50, alloc.height);
979                                 break;
980         
981                         case State.FILES: 
982                                 this.clutterfiles.set_size(alloc.width-50, alloc.height);
983                                 break;
984
985                         /*case State.PROJECT:
986                  
987                                 this.win.projecteditview.el.set_size(alloc.width-50, alloc.height / 2.0f);
988                 
989                            // this.win.rooview.el.save_easing_state();
990                                 //this.win.rooview.el.set_size(alloc.width / 2.0f, alloc.height / 2.0f);
991                                  
992                                 this.win.rooview.el.set_scale(0.5f, 0.5f);
993                                 //this.win.rooview.el.restore_easing_state();
994                                 break;
995                 */
996                         case State.CODE: 
997                                 this.win.codeeditview.el.set_size(codesize, alloc.height);
998                                 var scale = avail > 0.0f ? (avail - codesize -10 ) / avail : 0.0f;
999                                 //this.win.rooview.el.save_easing_state();
1000                                  
1001                                 this.win.rooview.el.set_scale(scale,scale);
1002                            // this.win.rooview.el.restore_easing_state();
1003                                 break;
1004                                 
1005                         case State.CODEONLY: 
1006                                 this.win.codeeditview.el.set_size(codesize, alloc.height);
1007                                 var scale = avail > 0.0f ? (avail - codesize -10 ) / avail : 0.0f;
1008                                 //this.win.rooview.el.save_easing_state();
1009                                 this.win.rooview.el.hide(); 
1010                                 this.win.rooview.el.set_scale(scale,scale);
1011                            // this.win.rooview.el.restore_easing_state();
1012                                 break;  
1013                         case State.PROP:
1014                         case State.LISTENER:
1015                                  this.win.addpropsview.el.set_size(palsize, alloc.height);
1016                                 var scale = avail > 0.0f ? (avail - palsize -10 ) / avail : 0.0f;
1017                                 this.win.rooview.el.set_scale(scale,scale);
1018                                 break;
1019                                 
1020                         case State.OBJECT:  
1021                                 this.win.objectview.el.set_size(palsize, alloc.height);    
1022                                 var scale = avail > 0.0f ? (avail - palsize -10 ) / avail : 0.0f;
1023                                 //this.win.rooview.el.save_easing_state();
1024                                 this.win.rooview.el.set_scale(scale,scale);
1025                            // this.win.rooview.el.restore_easing_state();
1026                                 break;
1027                 }
1028         }
1029
1030         // -- buttons show hide.....
1031
1032         public void buttonsShowHide()
1033         {
1034                 // basically hide everything, then show the relivant..
1035
1036                 // top bar btns
1037                 this.win.openbtn.el.hide();
1038                 this.win.openbackbtn.el.hide();
1039                 
1040                 this.win.backbutton.el.hide();
1041                 
1042
1043                 this.win.editfilebutton.el.hide();
1044                 this.win.projecteditbutton.el.hide();
1045                  
1046                 
1047                 this.win.objectshowbutton.el.hide(); // add objects
1048                 this.win.addpropbutton.el.hide();  
1049                 this.win.addlistenerbutton.el.hide(); 
1050
1051         
1052         
1053                 this.win.addprojectbutton.el.hide();
1054                 this.win.addfilebutton.el.hide();
1055                 this.win.delprojectbutton.el.hide();
1056                 
1057                 this.win.search_entry.el.hide();
1058                 this.win.search_results.el.hide();
1059                 switch (this.state) {
1060                         
1061                         case State.PREVIEW:  // this is the default state when working...
1062                            
1063                                 
1064                                 this.win.editfilebutton.el.show();
1065                                 this.win.projecteditbutton.el.show();
1066                                  
1067                                  
1068                                 
1069                                 this.win.objectshowbutton.el.show(); // add objects
1070                                 this.win.addpropbutton.el.show();  
1071                                 this.win.addlistenerbutton.el.show(); 
1072                                 this.win.search_entry.el.show();
1073                                 
1074                                 this.win.openbtn.el.show();
1075                                 
1076                                 break;
1077                         
1078                         case State.CODEONLY: 
1079                                 this.win.openbtn.el.show();
1080                                 this.win.projecteditbutton.el.show();
1081                                 this.win.search_entry.el.show();
1082                                 break;
1083                    
1084                         case State.CODE: 
1085                                 this.win.search_entry.el.show();
1086                                 this.win.backbutton.el.show();
1087                                 this.win.objectshowbutton.el.show(); // add objects ?? can you do this from here?
1088                                 this.win.addpropbutton.el.show();  
1089                                 this.win.addlistenerbutton.el.show(); 
1090                                 break;
1091                                 // continue thru..
1092                         case State.PROP:
1093                         case State.LISTENER:
1094                         case State.OBJECT:
1095                                 
1096                                 this.win.backbutton.el.show();
1097                                 this.win.objectshowbutton.el.show(); // add objects
1098                                 this.win.addpropbutton.el.show();  
1099                                 this.win.addlistenerbutton.el.show(); 
1100                                 break;
1101                         /*
1102                         case State.PROJECT: 
1103                         case State.FILEPROJECT:
1104                         case State.PROJECTCODEONLY:
1105                                 // anything else?
1106                                 this.win.backbutton.el.show();
1107                                 
1108                                 break;
1109                         */
1110         
1111                         case State.FILES:
1112                                 if (this.left_projects.getSelectedProject() != null ) {
1113                                         if (this.left_tree.getActiveFile() != null) {
1114                                          
1115                                                 this.win.openbackbtn.el.show();
1116                                         }
1117                                         this.win.addfilebutton.el.show();
1118                                         this.win.search_entry.el.show();
1119                                         this.win.projecteditbutton.el.show(); 
1120                                 } 
1121                                 
1122                                          
1123                                 this.win.addprojectbutton.el.show();
1124                                 this.win.delprojectbutton.el.show();
1125                                 
1126                                 
1127                                 
1128                                 
1129                                 break;
1130                 }
1131                 
1132                 
1133
1134         }
1135         
1136         
1137         public void showCompileResult(Json.Object obj)
1138                 {
1139                         // vala has finished compiling...
1140                         print("vala compiled");
1141                         var generator = new Json.Generator ();
1142                         var n  = new Json.Node(Json.NodeType.OBJECT);
1143                         n.init_object(obj);
1144                         generator.set_root (n);
1145                         print("result :%s", generator.to_data (null));
1146                         
1147                         
1148                         var buf = this.code_editor.buffer;
1149                         buf.check_running = false;
1150                         var has_errors = false;
1151                                       
1152                         if (obj.has_member("ERR-TOTAL")) {
1153                                 if (obj.get_int_member("ERR-TOTAL")> 0) {
1154                                         has_errors = true;
1155                                 }
1156                                  this.win.statusbar_errors.setNotices( obj.get_object_member("ERR") , (int) obj.get_int_member("ERR-TOTAL"));
1157                         } else {
1158                                  this.win.statusbar_errors.setNotices( new Json.Object() , 0);
1159                         }    
1160                         
1161                         if (obj.has_member("WARN-TOTAL")) {
1162
1163                                  this.win.statusbar_warnings.setNotices(obj.get_object_member("WARN"), (int) obj.get_int_member("WARN-TOTAL"));
1164                         } else {
1165                                  this.win.statusbar_warnings.setNotices( new Json.Object() , 0);
1166                                  
1167                         }
1168                         if (obj.has_member("DEPR-TOTAL")) {
1169                                 
1170                                  this.win.statusbar_depricated.setNotices( obj.get_object_member("DEPR"),  (int) obj.get_int_member("DEPR-TOTAL"));
1171                                  
1172                         } else {
1173                                 this.win.statusbar_depricated.setNotices( new Json.Object(),0);
1174                         }
1175                         //if (this.state == State.CODE || this.state == State.PROJECTCODEONLY) {
1176                         if (this.state == State.CODE) {
1177                                 buf.highlightErrorsJson("ERR", obj); 
1178                                 buf.highlightErrorsJson("WARN", obj);
1179                                 buf.highlightErrorsJson("DEPR", obj);
1180                         }
1181                         
1182                         this.win.statusbar_compilestatus_label.el.hide();
1183                         this.win.statusbar_run.el.hide();
1184                         if (!has_errors) { 
1185                                 this.win.statusbar_compilestatus_label.el.show();
1186                                 this.win.statusbar_run.el.show();
1187                         }
1188                         if (this.file.xtype == "Gtk") {
1189                                 // not sure how this is working ok? - as highlighting is happening on the vala files at present..
1190                                 var gbuf =   this.window_gladeview.sourceview;
1191                                 gbuf.highlightErrorsJson("ERR", obj);
1192                                 gbuf.highlightErrorsJson("WARN", obj);
1193                                 gbuf.highlightErrorsJson("DEPR", obj);                  
1194                                 
1195                                 if (!has_errors) {
1196                                         this.win.statusbar_run.el.show();
1197                                 }
1198                         
1199                    }
1200                    
1201                    if (this.file.xtype == "Gtk") {
1202                                 // not sure how this is working ok? - as highlighting is happening on the vala files at present..
1203                                 var gbuf =   this.window_rooview.sourceview;
1204                                 gbuf.highlightErrorsJson("ERR", obj);
1205                                 gbuf.highlightErrorsJson("WARN", obj);
1206                                 gbuf.highlightErrorsJson("DEPR", obj);                  
1207                         
1208                    }
1209                    
1210                         this.last_compile_result = obj;
1211                         
1212                         
1213                 }
1214         
1215 }
1216
1217