1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.ToolButton;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import gobject.ObjectG;
30 private import gobject.Signals;
31 private import gtk.ActionableIF;
32 private import gtk.ActionableT;
33 private import gtk.ToolItem;
34 private import gtk.Widget;
35 public  import gtkc.gdktypes;
36 private import gtkc.gtk;
37 public  import gtkc.gtktypes;
38 private import std.algorithm;
39 
40 
41 /**
42  * #GtkToolButtons are #GtkToolItems containing buttons.
43  * 
44  * Use gtk_tool_button_new() to create a new #GtkToolButton.
45  * 
46  * The label of a #GtkToolButton is determined by the properties
47  * #GtkToolButton:label-widget, #GtkToolButton:label, and
48  * #GtkToolButton:stock-id. If #GtkToolButton:label-widget is
49  * non-%NULL, then that widget is used as the label. Otherwise, if
50  * #GtkToolButton:label is non-%NULL, that string is used as the label.
51  * Otherwise, if #GtkToolButton:stock-id is non-%NULL, the label is
52  * determined by the stock item. Otherwise, the button does not have a label.
53  * 
54  * The icon of a #GtkToolButton is determined by the properties
55  * #GtkToolButton:icon-widget and #GtkToolButton:stock-id. If
56  * #GtkToolButton:icon-widget is non-%NULL, then
57  * that widget is used as the icon. Otherwise, if #GtkToolButton:stock-id is
58  * non-%NULL, the icon is determined by the stock item. Otherwise,
59  * the button does not have a icon.
60  * 
61  * # CSS nodes
62  * 
63  * GtkToolButton has a single CSS node with name toolbutton.
64  */
65 public class ToolButton : ToolItem, ActionableIF
66 {
67 	/** the main Gtk struct */
68 	protected GtkToolButton* gtkToolButton;
69 
70 	/** Get the main Gtk struct */
71 	public GtkToolButton* getToolButtonStruct()
72 	{
73 		return gtkToolButton;
74 	}
75 
76 	/** the main Gtk struct as a void* */
77 	protected override void* getStruct()
78 	{
79 		return cast(void*)gtkToolButton;
80 	}
81 
82 	protected override void setStruct(GObject* obj)
83 	{
84 		gtkToolButton = cast(GtkToolButton*)obj;
85 		super.setStruct(obj);
86 	}
87 
88 	/**
89 	 * Sets our main struct and passes it to the parent class.
90 	 */
91 	public this (GtkToolButton* gtkToolButton, bool ownedRef = false)
92 	{
93 		this.gtkToolButton = gtkToolButton;
94 		super(cast(GtkToolItem*)gtkToolButton, ownedRef);
95 	}
96 
97 	// add the Actionable capabilities
98 	mixin ActionableT!(GtkToolButton);
99 
100 	/** */
101 	public this (StockID stockID)
102 	{
103 		this(cast(string)stockID);
104 	}
105 
106 	/**
107 	 */
108 
109 	/** */
110 	public static GType getType()
111 	{
112 		return gtk_tool_button_get_type();
113 	}
114 
115 	/**
116 	 * Creates a new #GtkToolButton using @icon_widget as contents and @label as
117 	 * label.
118 	 *
119 	 * Params:
120 	 *     iconWidget = a widget that will be used as the button contents, or %NULL
121 	 *     label = a string that will be used as label, or %NULL
122 	 *
123 	 * Return: A new #GtkToolButton
124 	 *
125 	 * Since: 2.4
126 	 *
127 	 * Throws: ConstructionException GTK+ fails to create the object.
128 	 */
129 	public this(Widget iconWidget, string label)
130 	{
131 		auto p = gtk_tool_button_new((iconWidget is null) ? null : iconWidget.getWidgetStruct(), Str.toStringz(label));
132 		
133 		if(p is null)
134 		{
135 			throw new ConstructionException("null returned by new");
136 		}
137 		
138 		this(cast(GtkToolButton*) p);
139 	}
140 
141 	/**
142 	 * Creates a new #GtkToolButton containing the image and text from a
143 	 * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
144 	 * and #GTK_STOCK_APPLY.
145 	 *
146 	 * It is an error if @stock_id is not a name of a stock item.
147 	 *
148 	 * Deprecated: Use gtk_tool_button_new() together with
149 	 * gtk_image_new_from_icon_name() instead.
150 	 *
151 	 * Params:
152 	 *     stockId = the name of the stock item
153 	 *
154 	 * Return: A new #GtkToolButton
155 	 *
156 	 * Since: 2.4
157 	 *
158 	 * Throws: ConstructionException GTK+ fails to create the object.
159 	 */
160 	public this(string stockId)
161 	{
162 		auto p = gtk_tool_button_new_from_stock(Str.toStringz(stockId));
163 		
164 		if(p is null)
165 		{
166 			throw new ConstructionException("null returned by new_from_stock");
167 		}
168 		
169 		this(cast(GtkToolButton*) p);
170 	}
171 
172 	/**
173 	 * Returns the name of the themed icon for the tool button,
174 	 * see gtk_tool_button_set_icon_name().
175 	 *
176 	 * Return: the icon name or %NULL if the tool button has
177 	 *     no themed icon
178 	 *
179 	 * Since: 2.8
180 	 */
181 	public string getIconName()
182 	{
183 		return Str.toString(gtk_tool_button_get_icon_name(gtkToolButton));
184 	}
185 
186 	/**
187 	 * Return the widget used as icon widget on @button.
188 	 * See gtk_tool_button_set_icon_widget().
189 	 *
190 	 * Return: The widget used as icon
191 	 *     on @button, or %NULL.
192 	 *
193 	 * Since: 2.4
194 	 */
195 	public Widget getIconWidget()
196 	{
197 		auto p = gtk_tool_button_get_icon_widget(gtkToolButton);
198 		
199 		if(p is null)
200 		{
201 			return null;
202 		}
203 		
204 		return ObjectG.getDObject!(Widget)(cast(GtkWidget*) p);
205 	}
206 
207 	/**
208 	 * Returns the label used by the tool button, or %NULL if the tool button
209 	 * doesn’t have a label. or uses a the label from a stock item. The returned
210 	 * string is owned by GTK+, and must not be modified or freed.
211 	 *
212 	 * Return: The label, or %NULL
213 	 *
214 	 * Since: 2.4
215 	 */
216 	public string getLabel()
217 	{
218 		return Str.toString(gtk_tool_button_get_label(gtkToolButton));
219 	}
220 
221 	/**
222 	 * Returns the widget used as label on @button.
223 	 * See gtk_tool_button_set_label_widget().
224 	 *
225 	 * Return: The widget used as label
226 	 *     on @button, or %NULL.
227 	 *
228 	 * Since: 2.4
229 	 */
230 	public Widget getLabelWidget()
231 	{
232 		auto p = gtk_tool_button_get_label_widget(gtkToolButton);
233 		
234 		if(p is null)
235 		{
236 			return null;
237 		}
238 		
239 		return ObjectG.getDObject!(Widget)(cast(GtkWidget*) p);
240 	}
241 
242 	/**
243 	 * Returns the name of the stock item. See gtk_tool_button_set_stock_id().
244 	 * The returned string is owned by GTK+ and must not be freed or modifed.
245 	 *
246 	 * Deprecated: Use gtk_tool_button_get_icon_name() instead.
247 	 *
248 	 * Return: the name of the stock item for @button.
249 	 *
250 	 * Since: 2.4
251 	 */
252 	public string getStockId()
253 	{
254 		return Str.toString(gtk_tool_button_get_stock_id(gtkToolButton));
255 	}
256 
257 	/**
258 	 * Returns whether underscores in the label property are used as mnemonics
259 	 * on menu items on the overflow menu. See gtk_tool_button_set_use_underline().
260 	 *
261 	 * Return: %TRUE if underscores in the label property are used as
262 	 *     mnemonics on menu items on the overflow menu.
263 	 *
264 	 * Since: 2.4
265 	 */
266 	public bool getUseUnderline()
267 	{
268 		return gtk_tool_button_get_use_underline(gtkToolButton) != 0;
269 	}
270 
271 	/**
272 	 * Sets the icon for the tool button from a named themed icon.
273 	 * See the docs for #GtkIconTheme for more details.
274 	 * The #GtkToolButton:icon-name property only has an effect if not
275 	 * overridden by non-%NULL #GtkToolButton:label-widget,
276 	 * #GtkToolButton:icon-widget and #GtkToolButton:stock-id properties.
277 	 *
278 	 * Params:
279 	 *     iconName = the name of the themed icon
280 	 *
281 	 * Since: 2.8
282 	 */
283 	public void setIconName(string iconName)
284 	{
285 		gtk_tool_button_set_icon_name(gtkToolButton, Str.toStringz(iconName));
286 	}
287 
288 	/**
289 	 * Sets @icon as the widget used as icon on @button. If @icon_widget is
290 	 * %NULL the icon is determined by the #GtkToolButton:stock-id property. If the
291 	 * #GtkToolButton:stock-id property is also %NULL, @button will not have an icon.
292 	 *
293 	 * Params:
294 	 *     iconWidget = the widget used as icon, or %NULL
295 	 *
296 	 * Since: 2.4
297 	 */
298 	public void setIconWidget(Widget iconWidget)
299 	{
300 		gtk_tool_button_set_icon_widget(gtkToolButton, (iconWidget is null) ? null : iconWidget.getWidgetStruct());
301 	}
302 
303 	/**
304 	 * Sets @label as the label used for the tool button. The #GtkToolButton:label
305 	 * property only has an effect if not overridden by a non-%NULL
306 	 * #GtkToolButton:label-widget property. If both the #GtkToolButton:label-widget
307 	 * and #GtkToolButton:label properties are %NULL, the label is determined by the
308 	 * #GtkToolButton:stock-id property. If the #GtkToolButton:stock-id property is
309 	 * also %NULL, @button will not have a label.
310 	 *
311 	 * Params:
312 	 *     label = a string that will be used as label, or %NULL.
313 	 *
314 	 * Since: 2.4
315 	 */
316 	public void setLabel(string label)
317 	{
318 		gtk_tool_button_set_label(gtkToolButton, Str.toStringz(label));
319 	}
320 
321 	/**
322 	 * Sets @label_widget as the widget that will be used as the label
323 	 * for @button. If @label_widget is %NULL the #GtkToolButton:label property is used
324 	 * as label. If #GtkToolButton:label is also %NULL, the label in the stock item
325 	 * determined by the #GtkToolButton:stock-id property is used as label. If
326 	 * #GtkToolButton:stock-id is also %NULL, @button does not have a label.
327 	 *
328 	 * Params:
329 	 *     labelWidget = the widget used as label, or %NULL
330 	 *
331 	 * Since: 2.4
332 	 */
333 	public void setLabelWidget(Widget labelWidget)
334 	{
335 		gtk_tool_button_set_label_widget(gtkToolButton, (labelWidget is null) ? null : labelWidget.getWidgetStruct());
336 	}
337 
338 	/**
339 	 * Sets the name of the stock item. See gtk_tool_button_new_from_stock().
340 	 * The stock_id property only has an effect if not overridden by non-%NULL
341 	 * #GtkToolButton:label-widget and #GtkToolButton:icon-widget properties.
342 	 *
343 	 * Deprecated: Use gtk_tool_button_set_icon_name() instead.
344 	 *
345 	 * Params:
346 	 *     stockId = a name of a stock item, or %NULL
347 	 *
348 	 * Since: 2.4
349 	 */
350 	public void setStockId(string stockId)
351 	{
352 		gtk_tool_button_set_stock_id(gtkToolButton, Str.toStringz(stockId));
353 	}
354 
355 	/**
356 	 * If set, an underline in the label property indicates that the next character
357 	 * should be used for the mnemonic accelerator key in the overflow menu. For
358 	 * example, if the label property is “_Open” and @use_underline is %TRUE,
359 	 * the label on the tool button will be “Open” and the item on the overflow
360 	 * menu will have an underlined “O”.
361 	 *
362 	 * Labels shown on tool buttons never have mnemonics on them; this property
363 	 * only affects the menu item on the overflow menu.
364 	 *
365 	 * Params:
366 	 *     useUnderline = whether the button label has the form “_Open”
367 	 *
368 	 * Since: 2.4
369 	 */
370 	public void setUseUnderline(bool useUnderline)
371 	{
372 		gtk_tool_button_set_use_underline(gtkToolButton, useUnderline);
373 	}
374 
375 	protected class OnClickedDelegateWrapper
376 	{
377 		void delegate(ToolButton) dlg;
378 		gulong handlerId;
379 		ConnectFlags flags;
380 		this(void delegate(ToolButton) dlg, gulong handlerId, ConnectFlags flags)
381 		{
382 			this.dlg = dlg;
383 			this.handlerId = handlerId;
384 			this.flags = flags;
385 		}
386 	}
387 	protected OnClickedDelegateWrapper[] onClickedListeners;
388 
389 	/**
390 	 * This signal is emitted when the tool button is clicked with the mouse
391 	 * or activated with the keyboard.
392 	 */
393 	gulong addOnClicked(void delegate(ToolButton) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
394 	{
395 		onClickedListeners ~= new OnClickedDelegateWrapper(dlg, 0, connectFlags);
396 		onClickedListeners[onClickedListeners.length - 1].handlerId = Signals.connectData(
397 			this,
398 			"clicked",
399 			cast(GCallback)&callBackClicked,
400 			cast(void*)onClickedListeners[onClickedListeners.length - 1],
401 			cast(GClosureNotify)&callBackClickedDestroy,
402 			connectFlags);
403 		return onClickedListeners[onClickedListeners.length - 1].handlerId;
404 	}
405 	
406 	extern(C) static void callBackClicked(GtkToolButton* toolbuttonStruct,OnClickedDelegateWrapper wrapper)
407 	{
408 		wrapper.dlg(wrapper.outer);
409 	}
410 	
411 	extern(C) static void callBackClickedDestroy(OnClickedDelegateWrapper wrapper, GClosure* closure)
412 	{
413 		wrapper.outer.internalRemoveOnClicked(wrapper);
414 	}
415 
416 	protected void internalRemoveOnClicked(OnClickedDelegateWrapper source)
417 	{
418 		foreach(index, wrapper; onClickedListeners)
419 		{
420 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
421 			{
422 				onClickedListeners[index] = null;
423 				onClickedListeners = std.algorithm.remove(onClickedListeners, index);
424 				break;
425 			}
426 		}
427 	}
428 	
429 }