1 module declgtk.components.component; 2 3 import declgtk.queue; 4 import declui.components.component; 5 import gtk.Widget; 6 7 /** 8 An interface that defines some basic methods that can be used without requiring 9 instantiation the underlying template. 10 */ 11 interface IGtkWidgetComponent 12 { 13 Widget getWidget(); 14 } 15 16 abstract class GtkComponent(Type): IComponent 17 { 18 private Type _widget; 19 20 /// Initialises the component. 21 final void initialise() 22 { 23 _widget = createInstance(); 24 } 25 26 /// Returns the internal widget. 27 final Type getWidget() 28 { 29 if (_widget is null) 30 initialise(); 31 assert(_widget !is null, "Widget was not correctly created"); 32 return _widget; 33 } 34 35 /// Queues a setter to be executed. 36 protected void queue(void delegate(Type) callback) 37 { 38 queueOnGtk( 39 { 40 callback(getWidget); 41 }); 42 } 43 44 /// Creates and returns an new instance of this type. 45 protected abstract Type createInstance(); 46 47 override IComponent getInternal() 48 { 49 return this; 50 } 51 52 override bool visible() 53 { 54 return true; 55 } 56 57 override void visible(bool) 58 { 59 60 } 61 } 62 63 /** 64 A Component with a GTK backend. 65 */ 66 abstract class GtkWidgetComponent(Type) : IGtkWidgetComponent, IComponent 67 { 68 private Type _widget; 69 70 /// Initialises the component. 71 final void initialise() 72 { 73 _widget = createInstance(); 74 _visible = true; 75 _widget.show(); 76 } 77 78 /// Returns the internal widget. 79 override final Type getWidget() 80 { 81 if (_widget is null) 82 initialise(); 83 assert(_widget !is null, "Widget was not correctly created"); 84 return _widget; 85 } 86 87 /// Queues a setter to be executed. 88 protected void queue(void delegate(Type) callback) 89 { 90 queueOnGtk( 91 { 92 callback(getWidget); 93 }); 94 } 95 96 /// Creates and returns an new instance of this type. 97 protected abstract Type createInstance(); 98 99 override IComponent getInternal() 100 { 101 return this; 102 } 103 104 /* 105 Methods common for all Gtk Widgets 106 */ 107 private bool _visible = false; 108 override void visible(bool visible) 109 { 110 _visible = visible; 111 queue((widget) 112 { 113 if (_visible) 114 widget.show(); 115 else 116 widget.hide(); 117 }); 118 } 119 120 override bool visible() 121 { 122 return _visible; 123 } 124 }