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.FileChooserButton;
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.Box;
32 private import gtk.Dialog;
33 private import gtk.FileChooserIF;
34 private import gtk.FileChooserT;
35 private import gtk.Widget;
36 private import gtk.c.functions;
37 public  import gtk.c.types;
38 public  import gtkc.gtktypes;
39 private import std.algorithm;
40 
41 
42 /**
43  * The #GtkFileChooserButton is a widget that lets the user select a
44  * file.  It implements the #GtkFileChooser interface.  Visually, it is a
45  * file name with a button to bring up a #GtkFileChooserDialog.
46  * The user can then use that dialog to change the file associated with
47  * that button.  This widget does not support setting the
48  * #GtkFileChooser:select-multiple property to %TRUE.
49  * 
50  * ## Create a button to let the user select a file in /etc
51  * 
52  * |[<!-- language="C" -->
53  * {
54  * GtkWidget *button;
55  * 
56  * button = gtk_file_chooser_button_new (_("Select a file"),
57  * GTK_FILE_CHOOSER_ACTION_OPEN);
58  * gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (button),
59  * "/etc");
60  * }
61  * ]|
62  * 
63  * The #GtkFileChooserButton supports the #GtkFileChooserActions
64  * %GTK_FILE_CHOOSER_ACTION_OPEN and %GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER.
65  * 
66  * > The #GtkFileChooserButton will ellipsize the label, and will thus
67  * > request little horizontal space.  To give the button more space,
68  * > you should call gtk_widget_get_preferred_size(),
69  * > gtk_file_chooser_button_set_width_chars(), or pack the button in
70  * > such a way that other interface elements give space to the
71  * > widget.
72  */
73 public class FileChooserButton : Box, FileChooserIF
74 {
75 	/** the main Gtk struct */
76 	protected GtkFileChooserButton* gtkFileChooserButton;
77 
78 	/** Get the main Gtk struct */
79 	public GtkFileChooserButton* getFileChooserButtonStruct(bool transferOwnership = false)
80 	{
81 		if (transferOwnership)
82 			ownedRef = false;
83 		return gtkFileChooserButton;
84 	}
85 
86 	/** the main Gtk struct as a void* */
87 	protected override void* getStruct()
88 	{
89 		return cast(void*)gtkFileChooserButton;
90 	}
91 
92 	protected override void setStruct(GObject* obj)
93 	{
94 		gtkFileChooserButton = cast(GtkFileChooserButton*)obj;
95 		super.setStruct(obj);
96 	}
97 
98 	/**
99 	 * Sets our main struct and passes it to the parent class.
100 	 */
101 	public this (GtkFileChooserButton* gtkFileChooserButton, bool ownedRef = false)
102 	{
103 		this.gtkFileChooserButton = gtkFileChooserButton;
104 		super(cast(GtkBox*)gtkFileChooserButton, ownedRef);
105 	}
106 
107 	// add the FileChooser capabilities
108 	mixin FileChooserT!(GtkFileChooserButton);
109 
110 
111 	/** */
112 	public static GType getType()
113 	{
114 		return gtk_file_chooser_button_get_type();
115 	}
116 
117 	/**
118 	 * Creates a new file-selecting button widget.
119 	 *
120 	 * Params:
121 	 *     title = the title of the browse dialog.
122 	 *     action = the open mode for the widget.
123 	 *
124 	 * Returns: a new button widget.
125 	 *
126 	 * Since: 2.6
127 	 *
128 	 * Throws: ConstructionException GTK+ fails to create the object.
129 	 */
130 	public this(string title, GtkFileChooserAction action)
131 	{
132 		auto p = gtk_file_chooser_button_new(Str.toStringz(title), action);
133 
134 		if(p is null)
135 		{
136 			throw new ConstructionException("null returned by new");
137 		}
138 
139 		this(cast(GtkFileChooserButton*) p);
140 	}
141 
142 	/**
143 	 * Creates a #GtkFileChooserButton widget which uses @dialog as its
144 	 * file-picking window.
145 	 *
146 	 * Note that @dialog must be a #GtkDialog (or subclass) which
147 	 * implements the #GtkFileChooser interface and must not have
148 	 * %GTK_DIALOG_DESTROY_WITH_PARENT set.
149 	 *
150 	 * Also note that the dialog needs to have its confirmative button
151 	 * added with response %GTK_RESPONSE_ACCEPT or %GTK_RESPONSE_OK in
152 	 * order for the button to take over the file selected in the dialog.
153 	 *
154 	 * Params:
155 	 *     dialog = the widget to use as dialog
156 	 *
157 	 * Returns: a new button widget.
158 	 *
159 	 * Since: 2.6
160 	 *
161 	 * Throws: ConstructionException GTK+ fails to create the object.
162 	 */
163 	public this(Dialog dialog)
164 	{
165 		auto p = gtk_file_chooser_button_new_with_dialog((dialog is null) ? null : cast(GtkWidget*)dialog.getDialogStruct());
166 
167 		if(p is null)
168 		{
169 			throw new ConstructionException("null returned by new_with_dialog");
170 		}
171 
172 		this(cast(GtkFileChooserButton*) p);
173 	}
174 
175 	/**
176 	 * Returns whether the button grabs focus when it is clicked with the mouse.
177 	 * See gtk_file_chooser_button_set_focus_on_click().
178 	 *
179 	 * Deprecated: Use gtk_widget_get_focus_on_click() instead
180 	 *
181 	 * Returns: %TRUE if the button grabs focus when it is clicked with
182 	 *     the mouse.
183 	 *
184 	 * Since: 2.10
185 	 */
186 	public override bool getFocusOnClick()
187 	{
188 		return gtk_file_chooser_button_get_focus_on_click(gtkFileChooserButton) != 0;
189 	}
190 
191 	/**
192 	 * Retrieves the title of the browse dialog used by @button. The returned value
193 	 * should not be modified or freed.
194 	 *
195 	 * Returns: a pointer to the browse dialog’s title.
196 	 *
197 	 * Since: 2.6
198 	 */
199 	public string getTitle()
200 	{
201 		return Str.toString(gtk_file_chooser_button_get_title(gtkFileChooserButton));
202 	}
203 
204 	/**
205 	 * Retrieves the width in characters of the @button widget’s entry and/or label.
206 	 *
207 	 * Returns: an integer width (in characters) that the button will use to size itself.
208 	 *
209 	 * Since: 2.6
210 	 */
211 	public int getWidthChars()
212 	{
213 		return gtk_file_chooser_button_get_width_chars(gtkFileChooserButton);
214 	}
215 
216 	/**
217 	 * Sets whether the button will grab focus when it is clicked with the mouse.
218 	 * Making mouse clicks not grab focus is useful in places like toolbars where
219 	 * you don’t want the keyboard focus removed from the main area of the
220 	 * application.
221 	 *
222 	 * Deprecated: Use gtk_widget_set_focus_on_click() instead
223 	 *
224 	 * Params:
225 	 *     focusOnClick = whether the button grabs focus when clicked with the mouse
226 	 *
227 	 * Since: 2.10
228 	 */
229 	public override void setFocusOnClick(bool focusOnClick)
230 	{
231 		gtk_file_chooser_button_set_focus_on_click(gtkFileChooserButton, focusOnClick);
232 	}
233 
234 	/**
235 	 * Modifies the @title of the browse dialog used by @button.
236 	 *
237 	 * Params:
238 	 *     title = the new browse dialog title.
239 	 *
240 	 * Since: 2.6
241 	 */
242 	public void setTitle(string title)
243 	{
244 		gtk_file_chooser_button_set_title(gtkFileChooserButton, Str.toStringz(title));
245 	}
246 
247 	/**
248 	 * Sets the width (in characters) that @button will use to @n_chars.
249 	 *
250 	 * Params:
251 	 *     nChars = the new width, in characters.
252 	 *
253 	 * Since: 2.6
254 	 */
255 	public void setWidthChars(int nChars)
256 	{
257 		gtk_file_chooser_button_set_width_chars(gtkFileChooserButton, nChars);
258 	}
259 
260 	protected class OnFileSetDelegateWrapper
261 	{
262 		void delegate(FileChooserButton) dlg;
263 		gulong handlerId;
264 
265 		this(void delegate(FileChooserButton) dlg)
266 		{
267 			this.dlg = dlg;
268 			onFileSetListeners ~= this;
269 		}
270 
271 		void remove(OnFileSetDelegateWrapper source)
272 		{
273 			foreach(index, wrapper; onFileSetListeners)
274 			{
275 				if (wrapper.handlerId == source.handlerId)
276 				{
277 					onFileSetListeners[index] = null;
278 					onFileSetListeners = std.algorithm.remove(onFileSetListeners, index);
279 					break;
280 				}
281 			}
282 		}
283 	}
284 	OnFileSetDelegateWrapper[] onFileSetListeners;
285 
286 	/**
287 	 * The ::file-set signal is emitted when the user selects a file.
288 	 *
289 	 * Note that this signal is only emitted when the user
290 	 * changes the file.
291 	 *
292 	 * Since: 2.12
293 	 */
294 	gulong addOnFileSet(void delegate(FileChooserButton) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
295 	{
296 		auto wrapper = new OnFileSetDelegateWrapper(dlg);
297 		wrapper.handlerId = Signals.connectData(
298 			this,
299 			"file-set",
300 			cast(GCallback)&callBackFileSet,
301 			cast(void*)wrapper,
302 			cast(GClosureNotify)&callBackFileSetDestroy,
303 			connectFlags);
304 		return wrapper.handlerId;
305 	}
306 
307 	extern(C) static void callBackFileSet(GtkFileChooserButton* filechooserbuttonStruct, OnFileSetDelegateWrapper wrapper)
308 	{
309 		wrapper.dlg(wrapper.outer);
310 	}
311 
312 	extern(C) static void callBackFileSetDestroy(OnFileSetDelegateWrapper wrapper, GClosure* closure)
313 	{
314 		wrapper.remove(wrapper);
315 	}
316 }