1 /**
2  * Widget module.
3  *
4  * License:
5  *     MIT. See LICENSE for full details.
6  */
7 module tkd.widget.button;
8 
9 /**
10  * Imports.
11  */
12 import tkd.element.uielement;
13 import tkd.image.image;
14 import tkd.image.imageposition;
15 import tkd.widget.common.command;
16 import tkd.widget.common.default_;
17 import tkd.widget.common.invoke;
18 import tkd.widget.textwidget;
19 
20 /**
21  * A button widget displays a textual label and/or image, and evaluates a 
22  * command when pressed.
23  *
24  * Example:
25  * ---
26  * auto button = new Button(new Png!("image.png"), "Text")
27  * 	.setCommand(delegate(CommandArgs arg){ ... })
28  * 	.pack();
29  * ---
30  *
31  * Common_Commands:
32  *     These are injected common commands that can also be used with this widget.
33  *     $(P
34  *         $(LINK2 ./common/command.html, Command) $(BR)
35  *         $(LINK2 ./common/default_.html, Default) $(BR)
36  *         $(LINK2 ./common/invoke.html, Invoke) $(BR)
37  *     )
38  *
39  * Additional_Events:
40  *     Additional events that can also be bound to using the $(LINK2 ../element/uielement.html#UiElement.bind, bind) method.
41  *     $(P
42  *         <<Invoke>>,
43  *         <<PrevWindow>>,
44  *         <Alt-Key>,
45  *         <B1-Enter>,
46  *         <B1-Leave>,
47  *         <Button-1>,
48  *         <ButtonRelease-1>,
49  *         <Enter>,
50  *         <Key-F10>,
51  *         <Key-Tab>,
52  *         <Key-space>,
53  *         <Leave>,
54  *     )
55  *
56  * Styles:
57  *     Button widgets support the Toolbutton style in all standard themes, 
58  *     which is useful for creating widgets for toolbars.
59  *
60  * See_Also:
61  *     $(LINK2 ./textwidget.html, tkd.widget.textwidget)
62  */
63 class Button : TextWidget
64 {
65 	/**
66 	 * Construct the widget.
67 	 *
68 	 * Params:
69 	 *     parent = The parent of this widget.
70 	 *     text = The text of the button.
71 	 *     image = The button image.
72 	 *     imagePosition = The position of the image in relation to the text.
73 	 *
74 	 * See_Also:
75 	 *     $(LINK2 ../element/uielement.html, tkd.element.uielement) $(BR)
76 	 *     $(LINK2 ../image/image.html, tkd.image.image) $(BR)
77 	 *     $(LINK2 ../image/png.html, tkd.image.png) $(BR)
78 	 *     $(LINK2 ../image/gif.html, tkd.image.gif) $(BR)
79 	 *     $(LINK2 ../image/imageposition.html, tkd.image.imageposition) $(BR)
80 	 */
81 	this(UiElement parent, string text, Image image = null, string imagePosition = ImagePosition.top)
82 	{
83 		super(parent);
84 		this._elementId = "button";
85 
86 		this._tk.eval("ttk::button %s -textvariable %s", this.id, this._textVariable);
87 
88 		this.setText(text);
89 
90 		if (image !is null)
91 		{
92 			this.setImage(image, imagePosition);
93 		}
94 	}
95 
96 	/**
97 	 * Construct the widget.
98 	 *
99 	 * Params:
100 	 *     parent = The parent of this widget.
101 	 *     image = The button image.
102 	 *     text = The text of the button.
103 	 *     imagePosition = The position of the image in relation to the text.
104 	 *
105 	 * See_Also:
106 	 *     $(LINK2 ../element/uielement.html, tkd.element.uielement) $(BR)
107 	 *     $(LINK2 ../image/image.html, tkd.image.image) $(BR)
108 	 *     $(LINK2 ../image/png.html, tkd.image.png) $(BR)
109 	 *     $(LINK2 ../image/gif.html, tkd.image.gif) $(BR)
110 	 *     $(LINK2 ../image/imageposition.html, tkd.image.imageposition) $(BR)
111 	 */
112 	this(UiElement parent, Image image, string text = null, string imagePosition = ImagePosition.image)
113 	{
114 		this(parent, text, image, imagePosition);
115 	}
116 
117 	/**
118 	 * Construct the widget.
119 	 *
120 	 * Params:
121 	 *     text = The text of the button.
122 	 *     image = The button image.
123 	 *     imagePosition = The position of the image in relation to the text.
124 	 *
125 	 * See_Also:
126 	 *     $(LINK2 ../image/image.html, tkd.image.image) $(BR)
127 	 *     $(LINK2 ../image/png.html, tkd.image.png) $(BR)
128 	 *     $(LINK2 ../image/gif.html, tkd.image.gif) $(BR)
129 	 *     $(LINK2 ../image/imageposition.html, tkd.image.imageposition) $(BR)
130 	 */
131 	this(string text, Image image = null, string imagePosition = ImagePosition.top)
132 	{
133 		this(null, text, image, imagePosition);
134 	}
135 
136 	/**
137 	 * Construct the widget.
138 	 *
139 	 * Params:
140 	 *     image = The button image.
141 	 *     text = The text of the button.
142 	 *     imagePosition = The position of the image in relation to the text.
143 	 *
144 	 * See_Also:
145 	 *     $(LINK2 ../image/image.html, tkd.image.image) $(BR)
146 	 *     $(LINK2 ../image/png.html, tkd.image.png) $(BR)
147 	 *     $(LINK2 ../image/gif.html, tkd.image.gif) $(BR)
148 	 *     $(LINK2 ../image/imageposition.html, tkd.image.imageposition) $(BR)
149 	 */
150 	this(Image image, string text = null, string imagePosition = ImagePosition.image)
151 	{
152 		this(null, text, image, imagePosition);
153 	}
154 
155 	/**
156 	 * Mixin common commands.
157 	 */
158 	mixin Command;
159 	mixin Default_;
160 	mixin Invoke;
161 }