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.NativeDialog;
26 
27 private import glib.Str;
28 private import gobject.ObjectG;
29 private import gobject.Signals;
30 private import gtk.Window;
31 private import gtk.c.functions;
32 public  import gtk.c.types;
33 public  import gtkc.gtktypes;
34 private import std.algorithm;
35 
36 
37 /**
38  * Native dialogs are platform dialogs that don't use #GtkDialog or
39  * #GtkWindow. They are used in order to integrate better with a
40  * platform, by looking the same as other native applications and
41  * supporting platform specific features.
42  * 
43  * The #GtkDialog functions cannot be used on such objects, but we
44  * need a similar API in order to drive them. The #GtkNativeDialog
45  * object is an API that allows you to do this. It allows you to set
46  * various common properties on the dialog, as well as show and hide
47  * it and get a #GtkNativeDialog::response signal when the user finished
48  * with the dialog.
49  * 
50  * There is also a gtk_native_dialog_run() helper that makes it easy
51  * to run any native dialog in a modal way with a recursive mainloop,
52  * similar to gtk_dialog_run().
53  */
54 public class NativeDialog : ObjectG
55 {
56 	/** the main Gtk struct */
57 	protected GtkNativeDialog* gtkNativeDialog;
58 
59 	/** Get the main Gtk struct */
60 	public GtkNativeDialog* getNativeDialogStruct(bool transferOwnership = false)
61 	{
62 		if (transferOwnership)
63 			ownedRef = false;
64 		return gtkNativeDialog;
65 	}
66 
67 	/** the main Gtk struct as a void* */
68 	protected override void* getStruct()
69 	{
70 		return cast(void*)gtkNativeDialog;
71 	}
72 
73 	protected override void setStruct(GObject* obj)
74 	{
75 		gtkNativeDialog = cast(GtkNativeDialog*)obj;
76 		super.setStruct(obj);
77 	}
78 
79 	/**
80 	 * Sets our main struct and passes it to the parent class.
81 	 */
82 	public this (GtkNativeDialog* gtkNativeDialog, bool ownedRef = false)
83 	{
84 		this.gtkNativeDialog = gtkNativeDialog;
85 		super(cast(GObject*)gtkNativeDialog, ownedRef);
86 	}
87 
88 
89 	/** */
90 	public static GType getType()
91 	{
92 		return gtk_native_dialog_get_type();
93 	}
94 
95 	/**
96 	 * Destroys a dialog.
97 	 *
98 	 * When a dialog is destroyed, it will break any references it holds
99 	 * to other objects. If it is visible it will be hidden and any underlying
100 	 * window system resources will be destroyed.
101 	 *
102 	 * Note that this does not release any reference to the object (as opposed to
103 	 * destroying a GtkWindow) because there is no reference from the windowing
104 	 * system to the #GtkNativeDialog.
105 	 *
106 	 * Since: 3.20
107 	 */
108 	public void destroy()
109 	{
110 		gtk_native_dialog_destroy(gtkNativeDialog);
111 	}
112 
113 	/**
114 	 * Returns whether the dialog is modal. See gtk_native_dialog_set_modal().
115 	 *
116 	 * Returns: %TRUE if the dialog is set to be modal
117 	 *
118 	 * Since: 3.20
119 	 */
120 	public bool getModal()
121 	{
122 		return gtk_native_dialog_get_modal(gtkNativeDialog) != 0;
123 	}
124 
125 	/**
126 	 * Gets the title of the #GtkNativeDialog.
127 	 *
128 	 * Returns: the title of the dialog, or %NULL if none has
129 	 *     been set explicitly. The returned string is owned by the widget
130 	 *     and must not be modified or freed.
131 	 *
132 	 * Since: 3.20
133 	 */
134 	public string getTitle()
135 	{
136 		return Str.toString(gtk_native_dialog_get_title(gtkNativeDialog));
137 	}
138 
139 	/**
140 	 * Fetches the transient parent for this window. See
141 	 * gtk_native_dialog_set_transient_for().
142 	 *
143 	 * Returns: the transient parent for this window,
144 	 *     or %NULL if no transient parent has been set.
145 	 *
146 	 * Since: 3.20
147 	 */
148 	public Window getTransientFor()
149 	{
150 		auto p = gtk_native_dialog_get_transient_for(gtkNativeDialog);
151 
152 		if(p is null)
153 		{
154 			return null;
155 		}
156 
157 		return ObjectG.getDObject!(Window)(cast(GtkWindow*) p);
158 	}
159 
160 	/**
161 	 * Determines whether the dialog is visible.
162 	 *
163 	 * Returns: %TRUE if the dialog is visible
164 	 *
165 	 * Since: 3.20
166 	 */
167 	public bool getVisible()
168 	{
169 		return gtk_native_dialog_get_visible(gtkNativeDialog) != 0;
170 	}
171 
172 	/**
173 	 * Hides the dialog if it is visilbe, aborting any interaction. Once this
174 	 * is called the  #GtkNativeDialog::response signal will not be emitted
175 	 * until after the next call to gtk_native_dialog_show().
176 	 *
177 	 * If the dialog is not visible this does nothing.
178 	 *
179 	 * Since: 3.20
180 	 */
181 	public void hide()
182 	{
183 		gtk_native_dialog_hide(gtkNativeDialog);
184 	}
185 
186 	/**
187 	 * Blocks in a recursive main loop until @self emits the
188 	 * #GtkNativeDialog::response signal. It then returns the response ID
189 	 * from the ::response signal emission.
190 	 *
191 	 * Before entering the recursive main loop, gtk_native_dialog_run()
192 	 * calls gtk_native_dialog_show() on the dialog for you.
193 	 *
194 	 * After gtk_native_dialog_run() returns, then dialog will be hidden.
195 	 *
196 	 * Typical usage of this function might be:
197 	 * |[<!-- language="C" -->
198 	 * gint result = gtk_native_dialog_run (GTK_NATIVE_DIALOG (dialog));
199 	 * switch (result)
200 	 * {
201 	 * case GTK_RESPONSE_ACCEPT:
202 	 * do_application_specific_something ();
203 	 * break;
204 	 * default:
205 	 * do_nothing_since_dialog_was_cancelled ();
206 	 * break;
207 	 * }
208 	 * g_object_unref (dialog);
209 	 * ]|
210 	 *
211 	 * Note that even though the recursive main loop gives the effect of a
212 	 * modal dialog (it prevents the user from interacting with other
213 	 * windows in the same window group while the dialog is run), callbacks
214 	 * such as timeouts, IO channel watches, DND drops, etc, will
215 	 * be triggered during a gtk_nautilus_dialog_run() call.
216 	 *
217 	 * Returns: response ID
218 	 *
219 	 * Since: 3.20
220 	 */
221 	public int run()
222 	{
223 		return gtk_native_dialog_run(gtkNativeDialog);
224 	}
225 
226 	/**
227 	 * Sets a dialog modal or non-modal. Modal dialogs prevent interaction
228 	 * with other windows in the same application. To keep modal dialogs
229 	 * on top of main application windows, use
230 	 * gtk_native_dialog_set_transient_for() to make the dialog transient for the
231 	 * parent; most [window managers][gtk-X11-arch]
232 	 * will then disallow lowering the dialog below the parent.
233 	 *
234 	 * Params:
235 	 *     modal = whether the window is modal
236 	 *
237 	 * Since: 3.20
238 	 */
239 	public void setModal(bool modal)
240 	{
241 		gtk_native_dialog_set_modal(gtkNativeDialog, modal);
242 	}
243 
244 	/**
245 	 * Sets the title of the #GtkNativeDialog.
246 	 *
247 	 * Params:
248 	 *     title = title of the dialog
249 	 *
250 	 * Since: 3.20
251 	 */
252 	public void setTitle(string title)
253 	{
254 		gtk_native_dialog_set_title(gtkNativeDialog, Str.toStringz(title));
255 	}
256 
257 	/**
258 	 * Dialog windows should be set transient for the main application
259 	 * window they were spawned from. This allows
260 	 * [window managers][gtk-X11-arch] to e.g. keep the
261 	 * dialog on top of the main window, or center the dialog over the
262 	 * main window.
263 	 *
264 	 * Passing %NULL for @parent unsets the current transient window.
265 	 *
266 	 * Params:
267 	 *     parent = parent window, or %NULL
268 	 *
269 	 * Since: 3.20
270 	 */
271 	public void setTransientFor(Window parent)
272 	{
273 		gtk_native_dialog_set_transient_for(gtkNativeDialog, (parent is null) ? null : parent.getWindowStruct());
274 	}
275 
276 	/**
277 	 * Shows the dialog on the display, allowing the user to interact with
278 	 * it. When the user accepts the state of the dialog the dialog will
279 	 * be automatically hidden and the #GtkNativeDialog::response signal
280 	 * will be emitted.
281 	 *
282 	 * Multiple calls while the dialog is visible will be ignored.
283 	 *
284 	 * Since: 3.20
285 	 */
286 	public void show()
287 	{
288 		gtk_native_dialog_show(gtkNativeDialog);
289 	}
290 
291 	protected class OnResponseDelegateWrapper
292 	{
293 		void delegate(int, NativeDialog) dlg;
294 		gulong handlerId;
295 
296 		this(void delegate(int, NativeDialog) dlg)
297 		{
298 			this.dlg = dlg;
299 			onResponseListeners ~= this;
300 		}
301 
302 		void remove(OnResponseDelegateWrapper source)
303 		{
304 			foreach(index, wrapper; onResponseListeners)
305 			{
306 				if (wrapper.handlerId == source.handlerId)
307 				{
308 					onResponseListeners[index] = null;
309 					onResponseListeners = std.algorithm.remove(onResponseListeners, index);
310 					break;
311 				}
312 			}
313 		}
314 	}
315 	OnResponseDelegateWrapper[] onResponseListeners;
316 
317 	/**
318 	 * Emitted when the user responds to the dialog.
319 	 *
320 	 * When this is called the dialog has been hidden.
321 	 *
322 	 * If you call gtk_native_dialog_hide() before the user responds to
323 	 * the dialog this signal will not be emitted.
324 	 *
325 	 * Params:
326 	 *     responseId = the response ID
327 	 *
328 	 * Since: 3.20
329 	 */
330 	gulong addOnResponse(void delegate(int, NativeDialog) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
331 	{
332 		auto wrapper = new OnResponseDelegateWrapper(dlg);
333 		wrapper.handlerId = Signals.connectData(
334 			this,
335 			"response",
336 			cast(GCallback)&callBackResponse,
337 			cast(void*)wrapper,
338 			cast(GClosureNotify)&callBackResponseDestroy,
339 			connectFlags);
340 		return wrapper.handlerId;
341 	}
342 
343 	extern(C) static void callBackResponse(GtkNativeDialog* nativedialogStruct, int responseId, OnResponseDelegateWrapper wrapper)
344 	{
345 		wrapper.dlg(responseId, wrapper.outer);
346 	}
347 
348 	extern(C) static void callBackResponseDestroy(OnResponseDelegateWrapper wrapper, GClosure* closure)
349 	{
350 		wrapper.remove(wrapper);
351 	}
352 }