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.FileChooserNative;
26 
27 private import glib.ConstructionException;
28 private import glib.Str;
29 private import glib.c.functions;
30 private import gobject.ObjectG;
31 private import gtk.FileChooserIF;
32 private import gtk.FileChooserT;
33 private import gtk.NativeDialog;
34 private import gtk.Window;
35 private import gtk.c.functions;
36 public  import gtk.c.types;
37 
38 
39 /**
40  * `GtkFileChooserNative` is an abstraction of a dialog suitable
41  * for use with “File Open” or “File Save as” commands.
42  * 
43  * By default, this just uses a `GtkFileChooserDialog` to implement
44  * the actual dialog. However, on some platforms, such as Windows and
45  * macOS, the native platform file chooser is used instead. When the
46  * application is running in a sandboxed environment without direct
47  * filesystem access (such as Flatpak), `GtkFileChooserNative` may call
48  * the proper APIs (portals) to let the user choose a file and make it
49  * available to the application.
50  * 
51  * While the API of `GtkFileChooserNative` closely mirrors `GtkFileChooserDialog`,
52  * the main difference is that there is no access to any `GtkWindow` or `GtkWidget`
53  * for the dialog. This is required, as there may not be one in the case of a
54  * platform native dialog.
55  * 
56  * Showing, hiding and running the dialog is handled by the
57  * [class@Gtk.NativeDialog] functions.
58  * 
59  * Note that unlike `GtkFileChooserDialog`, `GtkFileChooserNative` objects
60  * are not toplevel widgets, and GTK does not keep them alive. It is your
61  * responsibility to keep a reference until you are done with the
62  * object.
63  * 
64  * ## Typical usage
65  * 
66  * In the simplest of cases, you can the following code to use
67  * `GtkFileChooserNative` to select a file for opening:
68  * 
69  * ```c
70  * static void
71  * on_response (GtkNativeDialog *native,
72  * int              response)
73  * {
74  * if (response == GTK_RESPONSE_ACCEPT)
75  * {
76  * GtkFileChooser *chooser = GTK_FILE_CHOOSER (native);
77  * GFile *file = gtk_file_chooser_get_file (chooser);
78  * 
79  * open_file (file);
80  * 
81  * g_object_unref (file);
82  * }
83  * 
84  * g_object_unref (native);
85  * }
86  * 
87  * // ...
88  * GtkFileChooserNative *native;
89  * GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
90  * 
91  * native = gtk_file_chooser_native_new ("Open File",
92  * parent_window,
93  * action,
94  * "_Open",
95  * "_Cancel");
96  * 
97  * g_signal_connect (native, "response", G_CALLBACK (on_response), NULL);
98  * gtk_native_dialog_show (GTK_NATIVE_DIALOG (native));
99  * ```
100  * 
101  * To use a `GtkFileChooserNative` for saving, you can use this:
102  * 
103  * ```c
104  * static void
105  * on_response (GtkNativeDialog *native,
106  * int              response)
107  * {
108  * if (response == GTK_RESPONSE_ACCEPT)
109  * {
110  * GtkFileChooser *chooser = GTK_FILE_CHOOSER (native);
111  * GFile *file = gtk_file_chooser_get_file (chooser);
112  * 
113  * save_to_file (file);
114  * 
115  * g_object_unref (file);
116  * }
117  * 
118  * g_object_unref (native);
119  * }
120  * 
121  * // ...
122  * GtkFileChooserNative *native;
123  * GtkFileChooser *chooser;
124  * GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_SAVE;
125  * 
126  * native = gtk_file_chooser_native_new ("Save File",
127  * parent_window,
128  * action,
129  * "_Save",
130  * "_Cancel");
131  * chooser = GTK_FILE_CHOOSER (native);
132  * 
133  * if (user_edited_a_new_document)
134  * gtk_file_chooser_set_current_name (chooser, _("Untitled document"));
135  * else
136  * gtk_file_chooser_set_file (chooser, existing_file, NULL);
137  * 
138  * g_signal_connect (native, "response", G_CALLBACK (on_response), NULL);
139  * gtk_native_dialog_show (GTK_NATIVE_DIALOG (native));
140  * ```
141  * 
142  * For more information on how to best set up a file dialog,
143  * see the [class@Gtk.FileChooserDialog] documentation.
144  * 
145  * ## Response Codes
146  * 
147  * `GtkFileChooserNative` inherits from [class@Gtk.NativeDialog],
148  * which means it will return %GTK_RESPONSE_ACCEPT if the user accepted,
149  * and %GTK_RESPONSE_CANCEL if he pressed cancel. It can also return
150  * %GTK_RESPONSE_DELETE_EVENT if the window was unexpectedly closed.
151  * 
152  * ## Differences from #GtkFileChooserDialog
153  * 
154  * There are a few things in the [iface@Gtk.FileChooser] interface that
155  * are not possible to use with `GtkFileChooserNative`, as such use would
156  * prohibit the use of a native dialog.
157  * 
158  * No operations that change the dialog work while the dialog is visible.
159  * Set all the properties that are required before showing the dialog.
160  * 
161  * ## Win32 details
162  * 
163  * On windows the `IFileDialog` implementation (added in Windows Vista) is
164  * used. It supports many of the features that `GtkFileChooser` has, but
165  * there are some things it does not handle:
166  * 
167  * * Any [class@Gtk.FileFilter] added using a mimetype
168  * 
169  * If any of these features are used the regular `GtkFileChooserDialog`
170  * will be used in place of the native one.
171  * 
172  * ## Portal details
173  * 
174  * When the `org.freedesktop.portal.FileChooser` portal is available on
175  * the session bus, it is used to bring up an out-of-process file chooser.
176  * Depending on the kind of session the application is running in, this may
177  * or may not be a GTK file chooser.
178  * 
179  * ## macOS details
180  * 
181  * On macOS the `NSSavePanel` and `NSOpenPanel` classes are used to provide
182  * native file chooser dialogs. Some features provided by `GtkFileChooser`
183  * are not supported:
184  * 
185  * * Shortcut folders.
186  */
187 public class FileChooserNative : NativeDialog, FileChooserIF
188 {
189 	/** the main Gtk struct */
190 	protected GtkFileChooserNative* gtkFileChooserNative;
191 
192 	/** Get the main Gtk struct */
193 	public GtkFileChooserNative* getFileChooserNativeStruct(bool transferOwnership = false)
194 	{
195 		if (transferOwnership)
196 			ownedRef = false;
197 		return gtkFileChooserNative;
198 	}
199 
200 	/** the main Gtk struct as a void* */
201 	protected override void* getStruct()
202 	{
203 		return cast(void*)gtkFileChooserNative;
204 	}
205 
206 	/**
207 	 * Sets our main struct and passes it to the parent class.
208 	 */
209 	public this (GtkFileChooserNative* gtkFileChooserNative, bool ownedRef = false)
210 	{
211 		this.gtkFileChooserNative = gtkFileChooserNative;
212 		super(cast(GtkNativeDialog*)gtkFileChooserNative, ownedRef);
213 	}
214 
215 	// add the FileChooser capabilities
216 	mixin FileChooserT!(GtkFileChooserNative);
217 
218 
219 	/** */
220 	public static GType getType()
221 	{
222 		return gtk_file_chooser_native_get_type();
223 	}
224 
225 	/**
226 	 * Creates a new `GtkFileChooserNative`.
227 	 *
228 	 * Params:
229 	 *     title = Title of the native, or %NULL
230 	 *     parent = Transient parent of the native, or %NULL
231 	 *     action = Open or save mode for the dialog
232 	 *     acceptLabel = text to go in the accept button, or %NULL for the default
233 	 *     cancelLabel = text to go in the cancel button, or %NULL for the default
234 	 *
235 	 * Returns: a new `GtkFileChooserNative`
236 	 *
237 	 * Throws: ConstructionException GTK+ fails to create the object.
238 	 */
239 	public this(string title, Window parent, GtkFileChooserAction action, string acceptLabel, string cancelLabel)
240 	{
241 		auto __p = gtk_file_chooser_native_new(Str.toStringz(title), (parent is null) ? null : parent.getWindowStruct(), action, Str.toStringz(acceptLabel), Str.toStringz(cancelLabel));
242 
243 		if(__p is null)
244 		{
245 			throw new ConstructionException("null returned by new");
246 		}
247 
248 		this(cast(GtkFileChooserNative*) __p, true);
249 	}
250 
251 	/**
252 	 * Retrieves the custom label text for the accept button.
253 	 *
254 	 * Returns: The custom label, or %NULL for the default.
255 	 *     This string is owned by GTK and should not be modified or freed
256 	 */
257 	public string getAcceptLabel()
258 	{
259 		return Str.toString(gtk_file_chooser_native_get_accept_label(gtkFileChooserNative));
260 	}
261 
262 	/**
263 	 * Retrieves the custom label text for the cancel button.
264 	 *
265 	 * Returns: The custom label, or %NULL for the default.
266 	 *     This string is owned by GTK and should not be modified or freed
267 	 */
268 	public string getCancelLabel()
269 	{
270 		return Str.toString(gtk_file_chooser_native_get_cancel_label(gtkFileChooserNative));
271 	}
272 
273 	/**
274 	 * Sets the custom label text for the accept button.
275 	 *
276 	 * If characters in @label are preceded by an underscore, they are
277 	 * underlined. If you need a literal underscore character in a label,
278 	 * use “__” (two underscores). The first underlined character represents
279 	 * a keyboard accelerator called a mnemonic.
280 	 *
281 	 * Pressing Alt and that key should activate the button.
282 	 *
283 	 * Params:
284 	 *     acceptLabel = custom label or %NULL for the default
285 	 */
286 	public void setAcceptLabel(string acceptLabel)
287 	{
288 		gtk_file_chooser_native_set_accept_label(gtkFileChooserNative, Str.toStringz(acceptLabel));
289 	}
290 
291 	/**
292 	 * Sets the custom label text for the cancel button.
293 	 *
294 	 * If characters in @label are preceded by an underscore, they are
295 	 * underlined. If you need a literal underscore character in a label,
296 	 * use “__” (two underscores). The first underlined character represents
297 	 * a keyboard accelerator called a mnemonic.
298 	 *
299 	 * Pressing Alt and that key should activate the button.
300 	 *
301 	 * Params:
302 	 *     cancelLabel = custom label or %NULL for the default
303 	 */
304 	public void setCancelLabel(string cancelLabel)
305 	{
306 		gtk_file_chooser_native_set_cancel_label(gtkFileChooserNative, Str.toStringz(cancelLabel));
307 	}
308 }