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