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.AppChooserButton;
26 
27 private import gio.IconIF;
28 private import glib.ConstructionException;
29 private import glib.Str;
30 private import gobject.ObjectG;
31 private import gobject.Signals;
32 private import gtk.AppChooserIF;
33 private import gtk.AppChooserT;
34 private import gtk.ComboBox;
35 private import gtk.Widget;
36 public  import gtkc.gdktypes;
37 private import gtkc.gtk;
38 public  import gtkc.gtktypes;
39 private import std.algorithm;
40 
41 
42 /**
43  * The #GtkAppChooserButton is a widget that lets the user select
44  * an application. It implements the #GtkAppChooser interface.
45  * 
46  * Initially, a #GtkAppChooserButton selects the first application
47  * in its list, which will either be the most-recently used application
48  * or, if #GtkAppChooserButton:show-default-item is %TRUE, the
49  * default application.
50  * 
51  * The list of applications shown in a #GtkAppChooserButton includes
52  * the recommended applications for the given content type. When
53  * #GtkAppChooserButton:show-default-item is set, the default application
54  * is also included. To let the user chooser other applications,
55  * you can set the #GtkAppChooserButton:show-dialog-item property,
56  * which allows to open a full #GtkAppChooserDialog.
57  * 
58  * It is possible to add custom items to the list, using
59  * gtk_app_chooser_button_append_custom_item(). These items cause
60  * the #GtkAppChooserButton::custom-item-activated signal to be
61  * emitted when they are selected.
62  * 
63  * To track changes in the selected application, use the
64  * #GtkComboBox::changed signal.
65  */
66 public class AppChooserButton : ComboBox, AppChooserIF
67 {
68 	/** the main Gtk struct */
69 	protected GtkAppChooserButton* gtkAppChooserButton;
70 
71 	/** Get the main Gtk struct */
72 	public GtkAppChooserButton* getAppChooserButtonStruct()
73 	{
74 		return gtkAppChooserButton;
75 	}
76 
77 	/** the main Gtk struct as a void* */
78 	protected override void* getStruct()
79 	{
80 		return cast(void*)gtkAppChooserButton;
81 	}
82 
83 	protected override void setStruct(GObject* obj)
84 	{
85 		gtkAppChooserButton = cast(GtkAppChooserButton*)obj;
86 		super.setStruct(obj);
87 	}
88 
89 	/**
90 	 * Sets our main struct and passes it to the parent class.
91 	 */
92 	public this (GtkAppChooserButton* gtkAppChooserButton, bool ownedRef = false)
93 	{
94 		this.gtkAppChooserButton = gtkAppChooserButton;
95 		super(cast(GtkComboBox*)gtkAppChooserButton, ownedRef);
96 	}
97 
98 	// add the AppChooser capabilities
99 	mixin AppChooserT!(GtkAppChooserButton);
100 
101 
102 	/** */
103 	public static GType getType()
104 	{
105 		return gtk_app_chooser_button_get_type();
106 	}
107 
108 	/**
109 	 * Creates a new #GtkAppChooserButton for applications
110 	 * that can handle content of the given type.
111 	 *
112 	 * Params:
113 	 *     contentType = the content type to show applications for
114 	 *
115 	 * Return: a newly created #GtkAppChooserButton
116 	 *
117 	 * Since: 3.0
118 	 *
119 	 * Throws: ConstructionException GTK+ fails to create the object.
120 	 */
121 	public this(string contentType)
122 	{
123 		auto p = gtk_app_chooser_button_new(Str.toStringz(contentType));
124 		
125 		if(p is null)
126 		{
127 			throw new ConstructionException("null returned by new");
128 		}
129 		
130 		this(cast(GtkAppChooserButton*) p);
131 	}
132 
133 	/**
134 	 * Appends a custom item to the list of applications that is shown
135 	 * in the popup; the item name must be unique per-widget.
136 	 * Clients can use the provided name as a detail for the
137 	 * #GtkAppChooserButton::custom-item-activated signal, to add a
138 	 * callback for the activation of a particular custom item in the list.
139 	 * See also gtk_app_chooser_button_append_separator().
140 	 *
141 	 * Params:
142 	 *     name = the name of the custom item
143 	 *     label = the label for the custom item
144 	 *     icon = the icon for the custom item
145 	 *
146 	 * Since: 3.0
147 	 */
148 	public void appendCustomItem(string name, string label, IconIF icon)
149 	{
150 		gtk_app_chooser_button_append_custom_item(gtkAppChooserButton, Str.toStringz(name), Str.toStringz(label), (icon is null) ? null : icon.getIconStruct());
151 	}
152 
153 	/**
154 	 * Appends a separator to the list of applications that is shown
155 	 * in the popup.
156 	 *
157 	 * Since: 3.0
158 	 */
159 	public void appendSeparator()
160 	{
161 		gtk_app_chooser_button_append_separator(gtkAppChooserButton);
162 	}
163 
164 	/**
165 	 * Returns the text to display at the top of the dialog.
166 	 *
167 	 * Return: the text to display at the top of the dialog,
168 	 *     or %NULL, in which case a default text is displayed
169 	 */
170 	public string getHeading()
171 	{
172 		return Str.toString(gtk_app_chooser_button_get_heading(gtkAppChooserButton));
173 	}
174 
175 	/**
176 	 * Returns the current value of the #GtkAppChooserButton:show-default-item
177 	 * property.
178 	 *
179 	 * Return: the value of #GtkAppChooserButton:show-default-item
180 	 *
181 	 * Since: 3.2
182 	 */
183 	public bool getShowDefaultItem()
184 	{
185 		return gtk_app_chooser_button_get_show_default_item(gtkAppChooserButton) != 0;
186 	}
187 
188 	/**
189 	 * Returns the current value of the #GtkAppChooserButton:show-dialog-item
190 	 * property.
191 	 *
192 	 * Return: the value of #GtkAppChooserButton:show-dialog-item
193 	 *
194 	 * Since: 3.0
195 	 */
196 	public bool getShowDialogItem()
197 	{
198 		return gtk_app_chooser_button_get_show_dialog_item(gtkAppChooserButton) != 0;
199 	}
200 
201 	/**
202 	 * Selects a custom item previously added with
203 	 * gtk_app_chooser_button_append_custom_item().
204 	 *
205 	 * Use gtk_app_chooser_refresh() to bring the selection
206 	 * to its initial state.
207 	 *
208 	 * Params:
209 	 *     name = the name of the custom item
210 	 *
211 	 * Since: 3.0
212 	 */
213 	public void setActiveCustomItem(string name)
214 	{
215 		gtk_app_chooser_button_set_active_custom_item(gtkAppChooserButton, Str.toStringz(name));
216 	}
217 
218 	/**
219 	 * Sets the text to display at the top of the dialog.
220 	 * If the heading is not set, the dialog displays a default text.
221 	 *
222 	 * Params:
223 	 *     heading = a string containing Pango markup
224 	 */
225 	public void setHeading(string heading)
226 	{
227 		gtk_app_chooser_button_set_heading(gtkAppChooserButton, Str.toStringz(heading));
228 	}
229 
230 	/**
231 	 * Sets whether the dropdown menu of this button should show the
232 	 * default application for the given content type at top.
233 	 *
234 	 * Params:
235 	 *     setting = the new value for #GtkAppChooserButton:show-default-item
236 	 *
237 	 * Since: 3.2
238 	 */
239 	public void setShowDefaultItem(bool setting)
240 	{
241 		gtk_app_chooser_button_set_show_default_item(gtkAppChooserButton, setting);
242 	}
243 
244 	/**
245 	 * Sets whether the dropdown menu of this button should show an
246 	 * entry to trigger a #GtkAppChooserDialog.
247 	 *
248 	 * Params:
249 	 *     setting = the new value for #GtkAppChooserButton:show-dialog-item
250 	 *
251 	 * Since: 3.0
252 	 */
253 	public void setShowDialogItem(bool setting)
254 	{
255 		gtk_app_chooser_button_set_show_dialog_item(gtkAppChooserButton, setting);
256 	}
257 
258 	protected class OnCustomItemActivatedDelegateWrapper
259 	{
260 		void delegate(string, AppChooserButton) dlg;
261 		gulong handlerId;
262 		ConnectFlags flags;
263 		this(void delegate(string, AppChooserButton) dlg, gulong handlerId, ConnectFlags flags)
264 		{
265 			this.dlg = dlg;
266 			this.handlerId = handlerId;
267 			this.flags = flags;
268 		}
269 	}
270 	protected OnCustomItemActivatedDelegateWrapper[] onCustomItemActivatedListeners;
271 
272 	/**
273 	 * Emitted when a custom item, previously added with
274 	 * gtk_app_chooser_button_append_custom_item(), is activated from the
275 	 * dropdown menu.
276 	 *
277 	 * Params:
278 	 *     itemName = the name of the activated item
279 	 */
280 	gulong addOnCustomItemActivated(void delegate(string, AppChooserButton) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
281 	{
282 		onCustomItemActivatedListeners ~= new OnCustomItemActivatedDelegateWrapper(dlg, 0, connectFlags);
283 		onCustomItemActivatedListeners[onCustomItemActivatedListeners.length - 1].handlerId = Signals.connectData(
284 			this,
285 			"custom-item-activated",
286 			cast(GCallback)&callBackCustomItemActivated,
287 			cast(void*)onCustomItemActivatedListeners[onCustomItemActivatedListeners.length - 1],
288 			cast(GClosureNotify)&callBackCustomItemActivatedDestroy,
289 			connectFlags);
290 		return onCustomItemActivatedListeners[onCustomItemActivatedListeners.length - 1].handlerId;
291 	}
292 	
293 	extern(C) static void callBackCustomItemActivated(GtkAppChooserButton* appchooserbuttonStruct, char* itemName,OnCustomItemActivatedDelegateWrapper wrapper)
294 	{
295 		wrapper.dlg(Str.toString(itemName), wrapper.outer);
296 	}
297 	
298 	extern(C) static void callBackCustomItemActivatedDestroy(OnCustomItemActivatedDelegateWrapper wrapper, GClosure* closure)
299 	{
300 		wrapper.outer.internalRemoveOnCustomItemActivated(wrapper);
301 	}
302 
303 	protected void internalRemoveOnCustomItemActivated(OnCustomItemActivatedDelegateWrapper source)
304 	{
305 		foreach(index, wrapper; onCustomItemActivatedListeners)
306 		{
307 			if (wrapper.dlg == source.dlg && wrapper.flags == source.flags && wrapper.handlerId == source.handlerId)
308 			{
309 				onCustomItemActivatedListeners[index] = null;
310 				onCustomItemActivatedListeners = std.algorithm.remove(onCustomItemActivatedListeners, index);
311 				break;
312 			}
313 		}
314 	}
315 	
316 }