1 /** 2 * Widget module. 3 * 4 * License: 5 * MIT. See LICENSE for full details. 6 */ 7 module tkd.widget.textwidget; 8 9 /** 10 * Imports. 11 */ 12 import std..string; 13 import tkd.element.uielement; 14 import tkd.image.image; 15 import tkd.image.imageposition; 16 import tkd.widget.common.underline; 17 import tkd.widget.widget; 18 19 /** 20 * The text widget base class. 21 * 22 * Common_Commands: 23 * These are injected common commands that can also be used with this widget. 24 * $(P 25 * $(LINK2 ./common/underline.html, Underline) $(BR) 26 * ) 27 * 28 * See_Also: 29 * $(LINK2 ./widget.html, tkd.widget.widget) 30 */ 31 abstract class TextWidget : Widget 32 { 33 /* 34 * The name of the text variable that contains the widget's text. 35 */ 36 protected string _textVariable; 37 38 /* 39 * The image of this widget. 40 */ 41 protected Image _image; 42 43 /** 44 * Construct the widget. 45 * 46 * Params: 47 * parent = An optional parent of this widget. 48 * 49 * See_Also: 50 * $(LINK2 ../element/uielement.html, tkd.element.uielement) 51 */ 52 public this(UiElement parent = null) 53 { 54 super(parent); 55 56 this._elementId = "textwidget"; 57 this._textVariable = format("variable-%s", this.generateHash(this.id)); 58 } 59 60 /** 61 * Set the widget text. 62 * 63 * Params: 64 * text = The widget text. 65 * 66 * Returns: 67 * This widget to aid method chaining. 68 */ 69 public auto setText(this T)(string text) 70 { 71 this._tk.setVariable(this._textVariable, text); 72 73 return cast(T) this; 74 } 75 76 /** 77 * Get the widget text. 78 * 79 * Returns: 80 * A string containing the widget text. 81 */ 82 public string getText() 83 { 84 return this._tk.getVariable(this._textVariable); 85 } 86 87 /** 88 * Set the image for this widget. 89 * 90 * Params: 91 * image = The image to set on the widget. 92 * imagePosition = The position of the image relative to the text. 93 * 94 * Returns: 95 * This widget to aid method chaining. 96 * 97 * See_Also: 98 * $(LINK2 ../image/image.html, tkd.image.image) $(BR) 99 * $(LINK2 ../image/png.html, tkd.image.png) $(BR) 100 * $(LINK2 ../image/gif.html, tkd.image.gif) $(BR) 101 * $(LINK2 ../image/imageposition.html, tkd.image.imageposition) $(BR) 102 */ 103 public auto setImage(this T)(Image image, string imagePosition = ImagePosition.image) 104 { 105 this._image = image; 106 107 this._tk.eval("%s configure -image %s", this.id, this._image.id); 108 this.setImagePosition(imagePosition); 109 110 return cast(T) this; 111 } 112 113 /** 114 * Change the position of the image in relation to the text. 115 * 116 * Params: 117 * imagePosition = The position of the image relative to the text. 118 * 119 * Returns: 120 * This widget to aid method chaining. 121 * 122 * See_Also: 123 * $(LINK2 ../image/imageposition.html, tkd.image.imageposition) 124 */ 125 public auto setImagePosition(this T)(string imagePosition) 126 { 127 this._tk.eval("%s configure -compound %s", this.id, imagePosition); 128 129 return cast(T) this; 130 } 131 132 /** 133 * Set the text character width. 134 * 135 * Params: 136 * characterWidth = The width of characters to set the label to. 137 * 138 * Returns: 139 * This widget to aid method chaining. 140 */ 141 public auto setTextCharacterWidth(this T)(int characterWidth) 142 { 143 this._tk.eval("%s configure -width %s", this.id, characterWidth); 144 145 return cast(T) this; 146 } 147 148 /** 149 * Destroy this widget. 150 * 151 * Caveats: 152 * Once a widget is destroyed it can no longer be referenced in your 153 * code or a segmentation fault will occur and potentially crash your 154 * program. 155 */ 156 override public void destroy() 157 { 158 this._tk.deleteVariable(this._textVariable); 159 super.destroy(); 160 } 161 162 /** 163 * Mixin common commands. 164 */ 165 mixin Underline; 166 }