change property type dialog to use dropdown (has bug with popover autohide, but added...
[roobuilder] / src / Builder4 / PopoverProperty.vala
1 static Xcls_PopoverProperty  _PopoverProperty;
2
3 public class Xcls_PopoverProperty : Object
4 {
5         public Gtk.Popover el;
6         private Xcls_PopoverProperty  _this;
7
8         public static Xcls_PopoverProperty singleton()
9         {
10                 if (_PopoverProperty == null) {
11                     _PopoverProperty= new Xcls_PopoverProperty();
12                 }
13                 return _PopoverProperty;
14         }
15         public Xcls_header header;
16         public Xcls_cancelbtn cancelbtn;
17         public Xcls_headertitle headertitle;
18         public Xcls_savebtn savebtn;
19         public Xcls_ptype ptype;
20         public Xcls_ktype ktype;
21         public Xcls_kname kname;
22         public Xcls_error error;
23
24                 // my vars (def)
25         public bool is_new;
26         public Gtk.PositionType position;
27         public signal void success (Project.Project pr, JsRender.JsRender file);
28         public string key_type;
29         public Xcls_MainWindow mainwindow;
30         public JsRender.Node node;
31         public JsRender.NodeProp? original_prop;
32         public JsRender.NodeProp? prop;
33         public bool done;
34         public string old_keyname;
35
36         // ctor
37         public Xcls_PopoverProperty()
38         {
39                 _this = this;
40                 this.el = new Gtk.Popover();
41
42                 // my vars (dec)
43                 this.is_new = false;
44                 this.position = Gtk.PositionType.RIGHT;
45                 this.mainwindow = null;
46                 this.original_prop = null;
47                 this.prop = null;
48                 this.done = false;
49
50                 // set gobject values
51                 this.el.autohide = true;
52                 var child_1 = new Xcls_Box1( _this );
53                 child_1.ref();
54                 this.el.set_child ( child_1.el  );
55
56                 //listeners
57                 this.el.closed.connect( () => {
58                 
59                         GLib.debug("popover closed");
60                         if (_this.is_new) {
61                                 // dont allow hiding if we are creating a new one.
62                                 // on.hide will reshow it.
63                                 return;
64                         }
65                         if (_this.prop == null) {
66                                 // hide and dont update.
67                                 return;
68                         }
69                         if (this.kname.el.get_text().strip().length < 1) {
70                                 return;
71                         }
72                         
73                  
74                                 
75                          
76                         this.updateProp();
77                         
78                          
79                 
80                 
81                   
82                 });
83                 this.el.hide.connect( () => {
84                         GLib.debug("popover hidden");
85                         if (_this.is_new || this.kname.el.get_text().strip().length < 1) {
86                                 // dont allow hiding if we are creating a new one.
87                                 GLib.debug("prevent hiding as its new or text is empty"); 
88                                 this.el.show();
89                                 return;
90                 
91                         }
92                         if (this.original_prop != null && !this.prop.equals(this.original_prop)) {
93                                 // this is convoluted..
94                                 _this.mainwindow.windowstate.left_props.changed(); 
95                         }
96                         
97                         
98                 });
99         }
100
101         // user defined functions
102         public void updateProp () {
103                 GLib.debug("updateProp called");
104         
105                 
106                 
107                 _this.prop.name = this.kname.el.get_text().strip();
108                 _this.prop.ptype = this.ptype.getValue();
109                 _this.prop.rtype = this.ktype.el.get_text().strip();
110                 
111                   
112         }
113         public void show (
114                 Gtk.Widget btn, 
115                 JsRender.Node node, 
116                 JsRender.NodeProp prop, 
117                 int y,
118                 bool is_new = false
119                  ) 
120         {
121                 
122             this.original_prop = prop.dupe();
123                 this.is_new = is_new; 
124                 var pref = is_new ? "Add " : "Modify ";
125                 if (prop.ptype == JsRender.NodePropType.LISTENER) {
126                         this.headertitle.el.label = pref + "Event Listener"; // cant really happen yet?
127                 } else {
128                         this.headertitle.el.label = pref + "Property";
129                 }
130                 this.prop = prop;
131                 this.node = node;
132                 
133                 _this.kname.el.set_text(prop.name);
134                 _this.ktype.el.set_text(prop.rtype);
135                 
136                 _this.ptype.setValue(prop.ptype);
137                 // does node have this property...
138         
139         
140                 _this.node = node;
141                 //console.log('show all');
142                 
143                 GLib.debug("set parent = %s", btn.get_type().name());
144                 var par = btn.get_parent();
145                 
146                 if (par == null) {
147                         GLib.debug("parent of that is null - not showing");
148                         return;
149                 }
150                 if (this.el.parent == null) {
151                         this.el.set_parent(btn);
152                 }
153                 var  r = Gdk.Rectangle() {
154                                 x = btn.get_width(), // align left...
155                                 y = 0,
156                                 width = 1,
157                                 height = 1
158                         };
159                 //Gtk.Allocation rect;
160                 //btn.get_allocation(out rect);
161             this.el.set_pointing_to(r);
162             
163         
164                  
165                 if (y > -1) {
166                          
167                          r = Gdk.Rectangle() {
168                                 x = btn.get_width(), // align left...
169                                 y = y,
170                                 width = 1,
171                                 height = 1
172                         };
173                         this.el.set_pointing_to( r);
174                 }
175                 
176                 
177         
178                 //this.el.set_position(Gtk.PositionType.TOP);
179         
180                 // window + header?
181                  GLib.debug("SHOWALL - POPIP\n");
182                 
183                 this.kname.el.grab_focus();
184                 this.savebtn.el.set_label("Save");
185                 this.cancelbtn.el.visible = false;
186                 if (this.is_new) {
187                         this.savebtn.el.set_label("Add Property");
188                         this.cancelbtn.el.visible = true;
189                 }
190                 this.error.setError("");
191                 this.el.show();
192                 //this.success = c.success;
193          
194         }
195         public class Xcls_Box1 : Object
196         {
197                 public Gtk.Box el;
198                 private Xcls_PopoverProperty  _this;
199
200
201                         // my vars (def)
202
203                 // ctor
204                 public Xcls_Box1(Xcls_PopoverProperty _owner )
205                 {
206                         _this = _owner;
207                         this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
208
209                         // my vars (dec)
210
211                         // set gobject values
212                         this.el.homogeneous = false;
213                         new Xcls_header( _this );
214                         this.el.append( _this.header.el );
215                         var child_2 = new Xcls_Label4( _this );
216                         child_2.ref();
217                         this.el.append( child_2.el );
218                         new Xcls_ptype( _this );
219                         this.el.append( _this.ptype.el );
220                         var child_4 = new Xcls_Label7( _this );
221                         child_4.ref();
222                         this.el.append( child_4.el );
223                         new Xcls_ktype( _this );
224                         this.el.append( _this.ktype.el );
225                         var child_6 = new Xcls_Label9( _this );
226                         child_6.ref();
227                         this.el.append( child_6.el );
228                         new Xcls_kname( _this );
229                         this.el.append( _this.kname.el );
230                         new Xcls_error( _this );
231                         this.el.append( _this.error.el );
232                 }
233
234                 // user defined functions
235         }
236         public class Xcls_header : Object
237         {
238                 public Gtk.Box el;
239                 private Xcls_PopoverProperty  _this;
240
241
242                         // my vars (def)
243
244                 // ctor
245                 public Xcls_header(Xcls_PopoverProperty _owner )
246                 {
247                         _this = _owner;
248                         _this.header = this;
249                         this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
250
251                         // my vars (dec)
252
253                         // set gobject values
254                         new Xcls_cancelbtn( _this );
255                         this.el.append( _this.cancelbtn.el );
256                         new Xcls_headertitle( _this );
257                         this.el.append( _this.headertitle.el );
258                         new Xcls_savebtn( _this );
259                         this.el.append( _this.savebtn.el );
260                 }
261
262                 // user defined functions
263         }
264         public class Xcls_cancelbtn : Object
265         {
266                 public Gtk.Button el;
267                 private Xcls_PopoverProperty  _this;
268
269
270                         // my vars (def)
271                 public bool always_show_image;
272
273                 // ctor
274                 public Xcls_cancelbtn(Xcls_PopoverProperty _owner )
275                 {
276                         _this = _owner;
277                         _this.cancelbtn = this;
278                         this.el = new Gtk.Button();
279
280                         // my vars (dec)
281                         this.always_show_image = true;
282
283                         // set gobject values
284                         this.el.hexpand = true;
285                         this.el.label = "Cancel";
286
287                         //listeners
288                         this.el.clicked.connect( () => {
289                                 _this.prop = null;
290                                 _this.is_new = false;
291                                 _this.kname.el.set_text("Cancel");
292                                 _this.el.hide();
293                                 
294                         });
295                 }
296
297                 // user defined functions
298         }
299
300         public class Xcls_headertitle : Object
301         {
302                 public Gtk.Label el;
303                 private Xcls_PopoverProperty  _this;
304
305
306                         // my vars (def)
307
308                 // ctor
309                 public Xcls_headertitle(Xcls_PopoverProperty _owner )
310                 {
311                         _this = _owner;
312                         _this.headertitle = this;
313                         this.el = new Gtk.Label( "Add / Edit property" );
314
315                         // my vars (dec)
316
317                         // set gobject values
318                         this.el.hexpand = true;
319                 }
320
321                 // user defined functions
322         }
323
324         public class Xcls_savebtn : Object
325         {
326                 public Gtk.Button el;
327                 private Xcls_PopoverProperty  _this;
328
329
330                         // my vars (def)
331                 public bool always_show_image;
332
333                 // ctor
334                 public Xcls_savebtn(Xcls_PopoverProperty _owner )
335                 {
336                         _this = _owner;
337                         _this.savebtn = this;
338                         this.el = new Gtk.Button();
339
340                         // my vars (dec)
341                         this.always_show_image = true;
342
343                         // set gobject values
344                         this.el.hexpand = true;
345                         this.el.label = "Add Property";
346
347                         //listeners
348                         this.el.clicked.connect( () => {
349                                 if (!_this.is_new) {
350                                         _this.el.hide();
351                                 }
352                                 
353                                 // check if text is not empty..
354                                 if ( _this.kname.el.get_text().strip().length < 1) {
355                                 
356                                         // error should already be showing?
357                                         return;
358                                 }
359                                  
360                                 // since we can't add listeners?!?!?
361                                 // only check props.
362                                 // check if property already exists in node.    
363                         
364                         
365                                 var prop = new JsRender.NodeProp(
366                                         _this.kname.el.get_text().strip(),
367                                         _this.ptype.getValue(),
368                                         _this.ktype.el.get_text().strip(),
369                                         _this.prop.val
370                                 );
371                         
372                                 if (_this.node.props.has_key(prop.to_index_key())) {
373                                         _this.error.setError("Property already exists");
374                                         return; 
375                                 }
376                                 
377                                 
378                                 
379                                 _this.node.add_prop(prop);
380                                 // hide self
381                                 _this.prop = null; // skip checks..
382                                 _this.is_new = false;
383                                 _this.el.hide();
384                                 _this.mainwindow.windowstate.left_props.changed();
385                                 _this.mainwindow.windowstate.left_props.view.editProp(prop);
386                         
387                                 
388                                 
389                         });
390                 }
391
392                 // user defined functions
393         }
394
395
396         public class Xcls_Label4 : Object
397         {
398                 public Gtk.Label el;
399                 private Xcls_PopoverProperty  _this;
400
401
402                         // my vars (def)
403
404                 // ctor
405                 public Xcls_Label4(Xcls_PopoverProperty _owner )
406                 {
407                         _this = _owner;
408                         this.el = new Gtk.Label( "Property Type (eg. property or method)" );
409
410                         // my vars (dec)
411
412                         // set gobject values
413                         this.el.halign = Gtk.Align.START;
414                         this.el.justify = Gtk.Justification.LEFT;
415                         this.el.margin_top = 12;
416                         this.el.visible = true;
417                 }
418
419                 // user defined functions
420         }
421
422         public class Xcls_ptype : Object
423         {
424                 public Gtk.DropDown el;
425                 private Xcls_PopoverProperty  _this;
426
427
428                         // my vars (def)
429
430                 // ctor
431                 public Xcls_ptype(Xcls_PopoverProperty _owner )
432                 {
433                         _this = _owner;
434                         _this.ptype = this;
435                         var child_1 = new Xcls_StringList6( _this );
436                         child_1.ref();
437                         this.el = new Gtk.DropDown( child_1.el, null );
438
439                         // my vars (dec)
440
441                         // set gobject values
442                         this.el.show_arrow = true;
443
444                         //listeners
445                         this.el.notify["selected"].connect( () => {
446                         
447                                 _this.el.grab_focus(); // stop prevent autohide breaking.
448                          });
449                 }
450
451                 // user defined functions
452                 public JsRender.NodePropType getValue () {
453                         var sl = this.el.model as Gtk.StringList;
454                         var str = sl.get_string(this.el.selected);
455                         return JsRender.NodePropType.nameToType(str);
456                 }
457                 public void setValue (JsRender.NodePropType ty) {
458                         var str = ty.to_name();
459                         var sl = this.el.model as Gtk.StringList;
460                         for(var i = 0; i < sl.get_n_items(); i++) {
461                                 if(sl.get_string(i) == str) {
462                                         this.el.set_selected(i);
463                                         break;
464                                 }
465                         }
466                         
467                 }
468         }
469         public class Xcls_StringList6 : Object
470         {
471                 public Gtk.StringList el;
472                 private Xcls_PopoverProperty  _this;
473
474
475                         // my vars (def)
476
477                 // ctor
478                 public Xcls_StringList6(Xcls_PopoverProperty _owner )
479                 {
480                         _this = _owner;
481                         this.el = new Gtk.StringList( JsRender.NodePropType.get_pulldown_list() );
482
483                         // my vars (dec)
484
485                         // set gobject values
486                 }
487
488                 // user defined functions
489         }
490
491
492         public class Xcls_Label7 : Object
493         {
494                 public Gtk.Label el;
495                 private Xcls_PopoverProperty  _this;
496
497
498                         // my vars (def)
499
500                 // ctor
501                 public Xcls_Label7(Xcls_PopoverProperty _owner )
502                 {
503                         _this = _owner;
504                         this.el = new Gtk.Label( "Type or Return Type" );
505
506                         // my vars (dec)
507
508                         // set gobject values
509                         this.el.halign = Gtk.Align.START;
510                         this.el.justify = Gtk.Justification.LEFT;
511                         this.el.margin_top = 12;
512                         this.el.visible = true;
513                 }
514
515                 // user defined functions
516         }
517
518         public class Xcls_ktype : Object
519         {
520                 public Gtk.Entry el;
521                 private Xcls_PopoverProperty  _this;
522
523
524                         // my vars (def)
525
526                 // ctor
527                 public Xcls_ktype(Xcls_PopoverProperty _owner )
528                 {
529                         _this = _owner;
530                         _this.ktype = this;
531                         this.el = new Gtk.Entry();
532
533                         // my vars (dec)
534
535                         // set gobject values
536                         this.el.visible = true;
537                 }
538
539                 // user defined functions
540         }
541
542         public class Xcls_Label9 : Object
543         {
544                 public Gtk.Label el;
545                 private Xcls_PopoverProperty  _this;
546
547
548                         // my vars (def)
549
550                 // ctor
551                 public Xcls_Label9(Xcls_PopoverProperty _owner )
552                 {
553                         _this = _owner;
554                         this.el = new Gtk.Label( "Name" );
555
556                         // my vars (dec)
557
558                         // set gobject values
559                         this.el.halign = Gtk.Align.START;
560                         this.el.justify = Gtk.Justification.LEFT;
561                         this.el.tooltip_text = "center, north, south, east, west";
562                         this.el.margin_top = 12;
563                         this.el.visible = true;
564                 }
565
566                 // user defined functions
567         }
568
569         public class Xcls_kname : Object
570         {
571                 public Gtk.Entry el;
572                 private Xcls_PopoverProperty  _this;
573
574
575                         // my vars (def)
576
577                 // ctor
578                 public Xcls_kname(Xcls_PopoverProperty _owner )
579                 {
580                         _this = _owner;
581                         _this.kname = this;
582                         this.el = new Gtk.Entry();
583
584                         // my vars (dec)
585
586                         // set gobject values
587                         this.el.visible = true;
588                         var child_1 = new Xcls_EventControllerFocus11( _this );
589                         child_1.ref();
590                         this.el.add_controller(  child_1.el );
591                         var child_2 = new Xcls_EventControllerKey12( _this );
592                         child_2.ref();
593                         this.el.add_controller(  child_2.el );
594                 }
595
596                 // user defined functions
597         }
598         public class Xcls_EventControllerFocus11 : Object
599         {
600                 public Gtk.EventControllerFocus el;
601                 private Xcls_PopoverProperty  _this;
602
603
604                         // my vars (def)
605
606                 // ctor
607                 public Xcls_EventControllerFocus11(Xcls_PopoverProperty _owner )
608                 {
609                         _this = _owner;
610                         this.el = new Gtk.EventControllerFocus();
611
612                         // my vars (dec)
613
614                         // set gobject values
615
616                         //listeners
617                         this.el.leave.connect( ( ) => {
618                         
619                             _this.error.setError("");
620                                 var val = _this.kname.el.get_text().strip(); 
621                                 if (val.length < 1) {
622                                         _this.error.setError("Name can not be empty");
623                                 }
624                         
625                         });
626                 }
627
628                 // user defined functions
629         }
630
631         public class Xcls_EventControllerKey12 : Object
632         {
633                 public Gtk.EventControllerKey el;
634                 private Xcls_PopoverProperty  _this;
635
636
637                         // my vars (def)
638
639                 // ctor
640                 public Xcls_EventControllerKey12(Xcls_PopoverProperty _owner )
641                 {
642                         _this = _owner;
643                         this.el = new Gtk.EventControllerKey();
644
645                         // my vars (dec)
646
647                         // set gobject values
648
649                         //listeners
650                         this.el.key_released.connect( (keyval, keycode, state) => {
651                         
652                             _this.error.setError("");
653                                 var val = _this.kname.el.get_text().strip(); 
654                                 if (val.length < 1) {
655                                         _this.error.setError("Name can not be empty");
656                                 }
657                         
658                         });
659                 }
660
661                 // user defined functions
662         }
663
664
665         public class Xcls_error : Object
666         {
667                 public Gtk.Label el;
668                 private Xcls_PopoverProperty  _this;
669
670
671                         // my vars (def)
672
673                 // ctor
674                 public Xcls_error(Xcls_PopoverProperty _owner )
675                 {
676                         _this = _owner;
677                         _this.error = this;
678                         this.el = new Gtk.Label( "<span color=\"red\">Error Message</span>" );
679
680                         // my vars (dec)
681
682                         // set gobject values
683                         this.el.halign = Gtk.Align.START;
684                         this.el.justify = Gtk.Justification.LEFT;
685                         this.el.tooltip_text = "center, north, south, east, west";
686                         this.el.margin_top = 0;
687                         this.el.visible = true;
688                         this.el.use_markup = true;
689                 }
690
691                 // user defined functions
692                 public void setError (string err)   {
693                         if (err == "") {
694                                 this.el.label = "";
695                         } else {
696                 
697                                 
698                                 this.el.label = "<span color=\"red\">" + err + "</span>";
699                         }
700                 }
701         }
702
703
704 }