1 /** 2 * Widget specific module. 3 * 4 * License: 5 * MIT. See LICENSE for full details. 6 */ 7 module tkd.widget.common.canvas.widgetspecific; 8 9 /** 10 * These are common commands that apply to all widgets that have them injected. 11 */ 12 mixin template WidgetSpecific() 13 { 14 import std.typecons : Nullable; 15 16 /** 17 * The widget to use. 18 */ 19 private Widget _widget; 20 21 /** 22 * The widget width. 23 */ 24 private Nullable!(int) _width; 25 26 /** 27 * The widget height. 28 */ 29 private Nullable!(int) _height; 30 31 /** 32 * Get the widget. 33 * 34 * Returns: 35 * The widget. 36 */ 37 public Widget getWidget() 38 { 39 return this._widget; 40 } 41 42 /** 43 * Specifies the widget to associate with this item. The widget specified 44 * must either be a child of the canvas widget or a child of some ancestor 45 * of the canvas widget. 46 * 47 * Params: 48 * widget = The widget. 49 * 50 * Returns: 51 * This item to aid method chaining. 52 */ 53 public auto setWidget(this T)(Widget widget) 54 { 55 this._widget = widget; 56 57 if (this._parent && this._widget) 58 { 59 this._tk.eval("%s itemconfigure %s -window %s", this._parent.id, this.id, this._widget.id); 60 } 61 62 return cast(T) this; 63 } 64 65 /** 66 * Get the widget width. 67 * 68 * Returns: 69 * The widget width. 70 */ 71 public int getWidth() 72 { 73 if (this._parent) 74 { 75 this._tk.eval("%s itemcget %s -width", this._parent.id, this.id); 76 this._width = this._tk.getResult!(int); 77 } 78 79 return this._width.isNull ? 0 : this._width.get; 80 } 81 82 /** 83 * Specifies the width to assign to the item's widget. If this option is 84 * not specified, or if it is specified as zero, then the widget is given 85 * whatever width it requests internally. 86 * 87 * Params: 88 * width = The widget width. 89 * 90 * Returns: 91 * This item to aid method chaining. 92 */ 93 public auto setWidth(this T, W)(W width) if (is(W == int) || is(W == Nullable!(int))) 94 { 95 static if (is(W == Nullable!(int))) 96 { 97 if (width.isNull) 98 { 99 return cast(T) this; 100 } 101 } 102 103 this._width = width; 104 105 if (this._parent) 106 { 107 this._tk.eval("%s itemconfigure %s -width %s", this._parent.id, this.id, this._width); 108 } 109 110 return cast(T) this; 111 } 112 113 /** 114 * Get the widget height. 115 * 116 * Returns: 117 * The widget height. 118 */ 119 public int getHeight() 120 { 121 if (this._parent) 122 { 123 this._tk.eval("%s itemcget %s -height", this._parent.id, this.id); 124 this._height = this._tk.getResult!(int); 125 } 126 127 return this._height.isNull ? 0 : this._height.get; 128 } 129 130 /** 131 * Specifies the height to assign to the item's widget. If this option is 132 * not specified, or if it is specified as zero, then the widget is given 133 * whatever height it requests internally. 134 * 135 * Params: 136 * height = The widget height. 137 * 138 * Returns: 139 * This item to aid method chaining. 140 */ 141 public auto setHeight(this T, H)(H height) if (is(H == int) || is(H == Nullable!(int))) 142 { 143 static if (is(H == Nullable!(int))) 144 { 145 if (height.isNull) 146 { 147 return cast(T) this; 148 } 149 } 150 151 this._height = height; 152 153 if (this._parent) 154 { 155 this._tk.eval("%s itemconfigure %s -height %s", this._parent.id, this.id, this._height); 156 } 157 158 return cast(T) this; 159 } 160 }