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.MenuToolButton;
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.Menu;
32 private import gtk.ToolButton;
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  * A #GtkMenuToolButton is a #GtkToolItem that contains a button and
43  * a small additional button with an arrow. When clicked, the arrow
44  * button pops up a dropdown menu.
45  * 
46  * Use gtk_menu_tool_button_new() to create a new
47  * #GtkMenuToolButton.
48  * 
49  * # GtkMenuToolButton as GtkBuildable
50  * 
51  * The GtkMenuToolButton implementation of the GtkBuildable interface
52  * supports adding a menu by specifying “menu” as the “type” attribute
53  * of a <child> element.
54  * 
55  * An example for a UI definition fragment with menus:
56  * |[
57  * <object class="GtkMenuToolButton">
58  * <child type="menu">
59  * <object class="GtkMenu"/>
60  * </child>
61  * </object>
62  * ]|
63  */
64 public class MenuToolButton : ToolButton
65 {
66 	/** the main Gtk struct */
67 	protected GtkMenuToolButton* gtkMenuToolButton;
68 
69 	/** Get the main Gtk struct */
70 	public GtkMenuToolButton* getMenuToolButtonStruct()
71 	{
72 		return gtkMenuToolButton;
73 	}
74 
75 	/** the main Gtk struct as a void* */
76 	protected override void* getStruct()
77 	{
78 		return cast(void*)gtkMenuToolButton;
79 	}
80 
81 	protected override void setStruct(GObject* obj)
82 	{
83 		gtkMenuToolButton = cast(GtkMenuToolButton*)obj;
84 		super.setStruct(obj);
85 	}
86 
87 	/**
88 	 * Sets our main struct and passes it to the parent class.
89 	 */
90 	public this (GtkMenuToolButton* gtkMenuToolButton, bool ownedRef = false)
91 	{
92 		this.gtkMenuToolButton = gtkMenuToolButton;
93 		super(cast(GtkToolButton*)gtkMenuToolButton, ownedRef);
94 	}
95 
96 	/**
97 	 * Creates a new GtkMenuToolButton.
98 	 * The new GtkMenuToolButton will contain an icon and label from
99 	 * the stock item indicated by stockID.
100 	 * Since: 2.6
101 	 * Params:
102 	 *     stockID = the name of a stock item
103 	 * Throws: ConstructionException GTK+ fails to create the object.
104 	 */
105 	public this(StockID stockId)
106 	{
107 		this(cast(string)stockId);
108 	}
109 	
110 	/**
111 	 * Gets the GtkMenu associated with GtkMenuToolButton.
112 	 * Since: 2.6
113 	 * Params:
114 	 *  button = a GtkMenuToolButton
115 	 * Returns:
116 	 *  the GtkMenu associated with GtkMenuToolButton
117 	 */
118 	public Menu getMenu()
119 	{
120 		auto p =  gtk_menu_tool_button_get_menu(gtkMenuToolButton);
121 		if(p is null)
122 		{
123 			return null;
124 		}
125 		return new Menu(cast(GtkMenu*)p);
126 	}
127 
128 	/**
129 	 */
130 
131 	/** */
132 	public static GType getType()
133 	{
134 		return gtk_menu_tool_button_get_type();
135 	}
136 
137 	/**
138 	 * Creates a new #GtkMenuToolButton using @icon_widget as icon and
139 	 * @label as label.
140 	 *
141 	 * Params:
142 	 *     iconWidget = a widget that will be used as icon widget, or %NULL
143 	 *     label = a string that will be used as label, or %NULL
144 	 *
145 	 * Return: the new #GtkMenuToolButton
146 	 *
147 	 * Since: 2.6
148 	 *
149 	 * Throws: ConstructionException GTK+ fails to create the object.
150 	 */
151 	public this(Widget iconWidget, string label)
152 	{
153 		auto p = gtk_menu_tool_button_new((iconWidget is null) ? null : iconWidget.getWidgetStruct(), Str.toStringz(label));
154 		
155 		if(p is null)
156 		{
157 			throw new ConstructionException("null returned by new");
158 		}
159 		
160 		this(cast(GtkMenuToolButton*) p);
161 	}
162 
163 	/**
164 	 * Creates a new #GtkMenuToolButton.
165 	 * The new #GtkMenuToolButton will contain an icon and label from
166 	 * the stock item indicated by @stock_id.
167 	 *
168 	 * Deprecated: Use gtk_menu_tool_button_new() instead.
169 	 *
170 	 * Params:
171 	 *     stockId = the name of a stock item
172 	 *
173 	 * Return: the new #GtkMenuToolButton
174 	 *
175 	 * Since: 2.6
176 	 *
177 	 * Throws: ConstructionException GTK+ fails to create the object.
178 	 */
179 	public this(string stockId)
180 	{
181 		auto p = gtk_menu_tool_button_new_from_stock(Str.toStringz(stockId));
182 		
183 		if(p is null)
184 		{
185 			throw new ConstructionException("null returned by new_from_stock");
186 		}
187 		
188 		this(cast(GtkMenuToolButton*) p);
189 	}
190 
191 	/**
192 	 * Sets the tooltip markup text to be used as tooltip for the arrow button
193 	 * which pops up the menu.  See gtk_tool_item_set_tooltip_text() for setting
194 	 * a tooltip on the whole #GtkMenuToolButton.
195 	 *
196 	 * Params:
197 	 *     markup = markup text to be used as tooltip text for button’s arrow button
198 	 *
199 	 * Since: 2.12
200 	 */
201 	public void setArrowTooltipMarkup(string markup)
202 	{
203 		gtk_menu_tool_button_set_arrow_tooltip_markup(gtkMenuToolButton, Str.toStringz(markup));
204 	}
205 
206 	/**
207 	 * Sets the tooltip text to be used as tooltip for the arrow button which
208 	 * pops up the menu.  See gtk_tool_item_set_tooltip_text() for setting a tooltip
209 	 * on the whole #GtkMenuToolButton.
210 	 *
211 	 * Params:
212 	 *     text = text to be used as tooltip text for button’s arrow button
213 	 *
214 	 * Since: 2.12
215 	 */
216 	public void setArrowTooltipText(string text)
217 	{
218 		gtk_menu_tool_button_set_arrow_tooltip_text(gtkMenuToolButton, Str.toStringz(text));
219 	}
220 
221 	/**
222 	 * Sets the #GtkMenu that is popped up when the user clicks on the arrow.
223 	 * If @menu is NULL, the arrow button becomes insensitive.
224 	 *
225 	 * Params:
226 	 *     menu = the #GtkMenu associated with #GtkMenuToolButton
227 	 *
228 	 * Since: 2.6
229 	 */
230 	public void setMenu(Widget menu)
231 	{
232 		gtk_menu_tool_button_set_menu(gtkMenuToolButton, (menu is null) ? null : menu.getWidgetStruct());
233 	}
234 
235 	protected class OnShowMenuDelegateWrapper
236 	{
237 		void delegate(MenuToolButton) dlg;
238 		gulong handlerId;
239 		ConnectFlags flags;
240 		this(void delegate(MenuToolButton) dlg, gulong handlerId, ConnectFlags flags)
241 		{
242 			this.dlg = dlg;
243 			this.handlerId = handlerId;
244 			this.flags = flags;
245 		}
246 	}
247 	protected OnShowMenuDelegateWrapper[] onShowMenuListeners;
248 
249 	/**
250 	 * The ::show-menu signal is emitted before the menu is shown.
251 	 *
252 	 * It can be used to populate the menu on demand, using
253 	 * gtk_menu_tool_button_set_menu().
254 	 *
255 	 * Note that even if you populate the menu dynamically in this way,
256 	 * you must set an empty menu on the #GtkMenuToolButton beforehand,
257 	 * since the arrow is made insensitive if the menu is not set.
258 	 */
259 	gulong addOnShowMenu(void delegate(MenuToolButton) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
260 	{
261 		onShowMenuListeners ~= new OnShowMenuDelegateWrapper(dlg, 0, connectFlags);
262 		onShowMenuListeners[onShowMenuListeners.length - 1].handlerId = Signals.connectData(
263 			this,
264 			"show-menu",
265 			cast(GCallback)&callBackShowMenu,
266 			cast(void*)onShowMenuListeners[onShowMenuListeners.length - 1],
267 			cast(GClosureNotify)&callBackShowMenuDestroy,
268 			connectFlags);
269 		return onShowMenuListeners[onShowMenuListeners.length - 1].handlerId;
270 	}
271 	
272 	extern(C) static void callBackShowMenu(GtkMenuToolButton* menutoolbuttonStruct,OnShowMenuDelegateWrapper wrapper)
273 	{
274 		wrapper.dlg(wrapper.outer);
275 	}
276 	
277 	extern(C) static void callBackShowMenuDestroy(OnShowMenuDelegateWrapper wrapper, GClosure* closure)
278 	{
279 		wrapper.outer.internalRemoveOnShowMenu(wrapper);
280 	}
281 
282 	protected void internalRemoveOnShowMenu(OnShowMenuDelegateWrapper source)
283 	{
284 		foreach(index, wrapper; onShowMenuListeners)
285 		{
286 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
287 			{
288 				onShowMenuListeners[index] = null;
289 				onShowMenuListeners = std.algorithm.remove(onShowMenuListeners, index);
290 				break;
291 			}
292 		}
293 	}
294 	
295 }