25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.widgets.FileDialog;
|
|
14
|
|
15
|
|
16
|
|
17 import org.eclipse.swt.SWT;
|
|
18 import org.eclipse.swt.SWTException;
|
|
19 import org.eclipse.swt.internal.gtk.OS;
|
|
20 import org.eclipse.swt.widgets.Dialog;
|
|
21 import org.eclipse.swt.widgets.Shell;
|
|
22 import org.eclipse.swt.widgets.Display;
|
|
23 import java.lang.all;
|
|
24
|
48
|
25 version(Tango){
|
25
|
26 static import tango.io.model.IFile;
|
|
27 static import tango.text.Util;
|
48
|
28 } else { // Phobos
|
|
29 }
|
25
|
30
|
|
31 /**
|
|
32 * Instances of this class allow the user to navigate
|
|
33 * the file system and select or enter a file name.
|
|
34 * <dl>
|
|
35 * <dt><b>Styles:</b></dt>
|
|
36 * <dd>SAVE, OPEN, MULTI</dd>
|
|
37 * <dt><b>Events:</b></dt>
|
|
38 * <dd>(none)</dd>
|
|
39 * </dl>
|
|
40 * <p>
|
|
41 * Note: Only one of the styles SAVE and OPEN may be specified.
|
|
42 * </p><p>
|
|
43 * IMPORTANT: This class is intended to be subclassed <em>only</em>
|
|
44 * within the SWT implementation.
|
|
45 * </p>
|
|
46 *
|
|
47 * @see <a href="http://www.eclipse.org/swt/snippets/#filedialog">FileDialog snippets</a>
|
|
48 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample, Dialog tab</a>
|
|
49 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
|
|
50 */
|
|
51 public class FileDialog : Dialog {
|
|
52 String [] filterNames;
|
|
53 String [] filterExtensions;
|
|
54 String filterPath = "";
|
|
55 String fileName = "";
|
|
56 String[] fileNames;
|
|
57 String fullPath = "";
|
|
58 int filterIndex = -1;
|
|
59 bool overwrite = false;
|
|
60 GtkWidget* handle;
|
|
61 static final char SEPARATOR = tango.io.model.IFile.FileConst.PathSeparatorChar;
|
|
62 static final char EXTENSION_SEPARATOR = ';';
|
|
63
|
|
64 /**
|
|
65 * Constructs a new instance of this class given only its parent.
|
|
66 *
|
|
67 * @param parent a shell which will be the parent of the new instance
|
|
68 *
|
|
69 * @exception IllegalArgumentException <ul>
|
|
70 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
|
|
71 * </ul>
|
|
72 * @exception SWTException <ul>
|
|
73 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
|
|
74 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
|
|
75 * </ul>
|
|
76 */
|
|
77 public this (Shell parent) {
|
|
78 this (parent, SWT.APPLICATION_MODAL);
|
|
79 }
|
|
80 /**
|
|
81 * Constructs a new instance of this class given its parent
|
|
82 * and a style value describing its behavior and appearance.
|
|
83 * <p>
|
|
84 * The style value is either one of the style constants defined in
|
|
85 * class <code>SWT</code> which is applicable to instances of this
|
|
86 * class, or must be built by <em>bitwise OR</em>'ing together
|
|
87 * (that is, using the <code>int</code> "|" operator) two or more
|
|
88 * of those <code>SWT</code> style constants. The class description
|
|
89 * lists the style constants that are applicable to the class.
|
|
90 * Style bits are also inherited from superclasses.
|
|
91 * </p>
|
|
92 *
|
|
93 * @param parent a shell which will be the parent of the new instance
|
|
94 * @param style the style of dialog to construct
|
|
95 *
|
|
96 * @exception IllegalArgumentException <ul>
|
|
97 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
|
|
98 * </ul>
|
|
99 * @exception SWTException <ul>
|
|
100 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
|
|
101 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
|
|
102 * </ul>
|
|
103 */
|
|
104 public this (Shell parent, int style) {
|
|
105 super (parent, checkStyle (parent, style));
|
|
106 checkSubclass ();
|
|
107 }
|
|
108 String computeResultChooserDialog () {
|
|
109 /* MULTI is only valid if the native dialog's action is Open */
|
|
110 fullPath = null;
|
|
111 if ((style & (SWT.SAVE | SWT.MULTI)) is SWT.MULTI) {
|
|
112 auto list = OS.gtk_file_chooser_get_filenames (handle);
|
|
113 int listLength = OS.g_slist_length (list);
|
|
114 fileNames = new String [listLength];
|
|
115 auto current = list;
|
|
116 int writePos = 0;
|
|
117 for (int i = 0; i < listLength; i++) {
|
|
118 auto name = cast(char*)OS.g_slist_data (current);
|
|
119 uint items_written;
|
|
120 char* utf8Ptr = OS.g_filename_to_utf8 (name, -1, null, &items_written, null);
|
|
121 OS.g_free (name);
|
|
122 if (utf8Ptr !is null) {
|
|
123 fullPath = utf8Ptr[ 0 .. items_written ].dup;
|
|
124 int start = tango.text.Util.locatePrior( fullPath, SEPARATOR);
|
|
125 if( start is fullPath.length ) start = -1;
|
|
126 fileNames [writePos++] = fullPath[ start + 1 .. $ ].dup;
|
|
127 OS.g_free (utf8Ptr);
|
|
128 }
|
|
129 current = OS.g_slist_next (current);
|
|
130 }
|
|
131 if (writePos !is 0 && writePos !is listLength) {
|
|
132 String [] validFileNames = new String [writePos];
|
|
133 System.arraycopy (fileNames, 0, validFileNames, 0, writePos);
|
|
134 fileNames = validFileNames;
|
|
135 }
|
|
136 OS.g_slist_free (list);
|
|
137 } else {
|
|
138 auto path = OS.gtk_file_chooser_get_filename (handle);
|
|
139 if (path !is null) {
|
|
140 uint items_written;
|
|
141 auto utf8Ptr = OS.g_filename_to_utf8 (path, -1, null, &items_written, null);
|
|
142 OS.g_free (path);
|
|
143 if (utf8Ptr !is null) {
|
|
144 fullPath = utf8Ptr[ 0 .. items_written ].dup;
|
|
145 fileNames = new String [1];
|
|
146 int start = tango.text.Util.locatePrior( fullPath, SEPARATOR);
|
|
147 if( start == fullPath.length ) start = -1;
|
|
148 fileNames[0] = fullPath[ start + 1 .. $ ];
|
|
149 OS.g_free (utf8Ptr);
|
|
150 }
|
|
151 }
|
|
152 }
|
|
153 filterIndex = -1;
|
|
154 auto filter = OS.gtk_file_chooser_get_filter (handle);
|
|
155 if (filter !is null) {
|
|
156 auto filterNamePtr = OS.gtk_file_filter_get_name (filter);
|
|
157 if (filterNamePtr !is null) {
|
|
158 String filterName = fromStringz(filterNamePtr).dup;
|
|
159 //OS.g_free (filterNamePtr); //GTK owns this pointer - do not free
|
|
160 for (int i = 0; i < filterExtensions.length; i++) {
|
|
161 if (filterNames.length > 0) {
|
|
162 if (filterNames[i].equals(filterName)) {
|
|
163 filterIndex = i;
|
|
164 break;
|
|
165 }
|
|
166 } else {
|
|
167 if (filterExtensions[i].equals(filterName)) {
|
|
168 filterIndex = i;
|
|
169 break;
|
|
170 }
|
|
171 }
|
|
172 }
|
|
173 }
|
|
174 }
|
|
175 if (fullPath !is null) {
|
|
176 int separatorIndex = tango.text.Util.locatePrior( fullPath, SEPARATOR);
|
|
177 if( separatorIndex is fullPath.length ) separatorIndex = -1;
|
|
178 fileName = fullPath[separatorIndex + 1 .. $ ];
|
|
179 filterPath = fullPath[0 .. separatorIndex ];
|
|
180 }
|
|
181 return fullPath;
|
|
182 }
|
|
183 String computeResultClassicDialog () {
|
|
184 filterIndex = -1;
|
|
185 GtkFileSelection* selection = cast(GtkFileSelection*)handle;
|
|
186 auto entry = selection.selection_entry;
|
|
187 auto entryText = OS.gtk_entry_get_text (entry);
|
|
188 String txt = fromStringz( entryText );
|
|
189 if (txt.length is 0) {
|
|
190 auto fileList = selection.file_list;
|
|
191 auto listSelection = OS.gtk_tree_view_get_selection (fileList);
|
|
192 void* model;
|
|
193 auto selectedList = OS.gtk_tree_selection_get_selected_rows (listSelection, &model);
|
|
194 if (selectedList is null) return null;
|
|
195 int listLength = OS.g_list_length (selectedList);
|
|
196 if (listLength is 0) {
|
|
197 OS.g_list_free (selectedList);
|
|
198 return null;
|
|
199 }
|
|
200 auto path = OS.g_list_nth_data (selectedList, 0);
|
|
201 char* ptr;
|
|
202 GtkTreeIter iter;
|
|
203 if (OS.gtk_tree_model_get_iter (&model, &iter, path)) {
|
|
204 OS.gtk_tree_model_get1 (&model, &iter, 0, cast(void**)&ptr);
|
|
205 }
|
|
206 for (int i = 0; i < listLength; i++) {
|
|
207 OS.gtk_tree_path_free (OS.g_list_nth_data (selectedList, i));
|
|
208 }
|
|
209 OS.g_list_free (selectedList);
|
|
210 if (ptr is null) return null;
|
|
211 OS.gtk_entry_set_text (entry, ptr);
|
|
212 OS.g_free (ptr);
|
|
213 }
|
|
214
|
|
215 auto fileNamePtr = OS.gtk_file_selection_get_filename (handle);
|
|
216 uint items_written;
|
|
217 auto utf8Ptr = OS.g_filename_to_utf8 (fileNamePtr, -1, null, &items_written, null);
|
|
218 String osAnswer = utf8Ptr[ 0 .. items_written ].dup;
|
|
219 OS.g_free (utf8Ptr);
|
|
220
|
|
221 if (osAnswer.length is 0) return null;
|
|
222 int separatorIndex = tango.text.Util.locatePrior( osAnswer, SEPARATOR);
|
|
223 if (separatorIndex is osAnswer.length ) separatorIndex = -1;
|
|
224 if (separatorIndex+1 is osAnswer.length ) return null;
|
|
225
|
|
226 String answer = fullPath = osAnswer;
|
|
227 fileName = fullPath[ separatorIndex+1 .. $ ];
|
|
228 filterPath = fullPath[ 0 .. separatorIndex ];
|
|
229 if ((style & SWT.MULTI) is 0) {
|
|
230 fileNames = [ fileName ];
|
|
231 } else {
|
|
232 auto namesPtr = OS.gtk_file_selection_get_selections (handle);
|
|
233 auto namesPtr1 = namesPtr;
|
|
234 char* namePtr = namesPtr1[0];
|
|
235 int length_ = 0;
|
|
236 while (namePtr !is null) {
|
|
237 length_++;
|
|
238 namePtr = namesPtr1[length_];
|
|
239 }
|
|
240 fileNames = new String[](length_);
|
|
241 for (int i = 0; i < length_; i++) {
|
|
242 utf8Ptr = OS.g_filename_to_utf8 (namesPtr [i], -1, null, &items_written, null);
|
|
243 String name = utf8Ptr[ 0 .. items_written ].dup;
|
|
244 int start = tango.text.Util.locatePrior( name, SEPARATOR);
|
|
245 if( start == name.length ) start = -1;
|
|
246 fileNames [i] = name[ start + 1 .. $ ].dup;
|
|
247 OS.g_free (utf8Ptr);
|
|
248 }
|
|
249 OS.g_strfreev (namesPtr);
|
|
250 }
|
|
251 return answer;
|
|
252 }
|
|
253 /**
|
|
254 * Returns the path of the first file that was
|
|
255 * selected in the dialog relative to the filter path, or an
|
|
256 * empty string if no such file has been selected.
|
|
257 *
|
|
258 * @return the relative path of the file
|
|
259 */
|
|
260 public String getFileName () {
|
|
261 return fileName;
|
|
262 }
|
|
263 /**
|
|
264 * Returns a (possibly empty) array with the paths of all files
|
|
265 * that were selected in the dialog relative to the filter path.
|
|
266 *
|
|
267 * @return the relative paths of the files
|
|
268 */
|
|
269 public String [] getFileNames () {
|
|
270 return fileNames;
|
|
271 }
|
|
272 /**
|
|
273 * Returns the file extensions which the dialog will
|
|
274 * use to filter the files it shows.
|
|
275 *
|
|
276 * @return the file extensions filter
|
|
277 */
|
|
278 public String [] getFilterExtensions () {
|
|
279 return filterExtensions;
|
|
280 }
|
|
281 /**
|
|
282 * Get the 0-based index of the file extension filter
|
|
283 * which was selected by the user, or -1 if no filter
|
|
284 * was selected.
|
|
285 * <p>
|
|
286 * This is an index into the FilterExtensions array and
|
|
287 * the FilterNames array.
|
|
288 * </p>
|
|
289 *
|
|
290 * @return index the file extension filter index
|
|
291 *
|
|
292 * @see #getFilterExtensions
|
|
293 * @see #getFilterNames
|
|
294 *
|
|
295 * @since 3.4
|
|
296 */
|
|
297 public int getFilterIndex () {
|
|
298 return filterIndex;
|
|
299 }
|
|
300 /**
|
|
301 * Returns the names that describe the filter extensions
|
|
302 * which the dialog will use to filter the files it shows.
|
|
303 *
|
|
304 * @return the list of filter names
|
|
305 */
|
|
306 public String [] getFilterNames () {
|
|
307 return filterNames;
|
|
308 }
|
|
309 /**
|
|
310 * Returns the directory path that the dialog will use, or an empty
|
|
311 * string if this is not set. File names in this path will appear
|
|
312 * in the dialog, filtered according to the filter extensions.
|
|
313 *
|
|
314 * @return the directory path string
|
|
315 *
|
|
316 * @see #setFilterExtensions
|
|
317 */
|
|
318 public String getFilterPath () {
|
|
319 return filterPath;
|
|
320 }
|
|
321 /**
|
|
322 * Returns the flag that the dialog will use to
|
|
323 * determine whether to prompt the user for file
|
|
324 * overwrite if the selected file already exists.
|
|
325 *
|
|
326 * @return true if the dialog will prompt for file overwrite, false otherwise
|
|
327 *
|
|
328 * @since 3.4
|
|
329 */
|
|
330 public bool getOverwrite () {
|
|
331 return overwrite;
|
|
332 }
|
|
333 /**
|
|
334 * Makes the dialog visible and brings it to the front
|
|
335 * of the display.
|
|
336 *
|
|
337 * @return a string describing the absolute path of the first selected file,
|
|
338 * or null if the dialog was cancelled or an error occurred
|
|
339 *
|
|
340 * @exception SWTException <ul>
|
|
341 * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li>
|
|
342 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li>
|
|
343 * </ul>
|
|
344 */
|
|
345 public String open () {
|
|
346 bool useChooserDialog = OS.GTK_VERSION >= OS.buildVERSION (2, 4, 10);
|
|
347 if (useChooserDialog) {
|
|
348 return openChooserDialog ();
|
|
349 } else {
|
|
350 return openClassicDialog ();
|
|
351 }
|
|
352 }
|
|
353 String openChooserDialog () {
|
|
354 char* titleBytes = toStringz( title );
|
|
355 int action = (style & SWT.SAVE) !is 0 ?
|
|
356 OS.GTK_FILE_CHOOSER_ACTION_SAVE :
|
|
357 OS.GTK_FILE_CHOOSER_ACTION_OPEN;
|
|
358 auto shellHandle = parent.topHandle ();
|
|
359 handle = OS.gtk_file_chooser_dialog_new2 (
|
|
360 titleBytes,
|
|
361 shellHandle,
|
|
362 action,
|
|
363 OS.GTK_STOCK_CANCEL (), OS.GTK_RESPONSE_CANCEL,
|
|
364 OS.GTK_STOCK_OK (), OS.GTK_RESPONSE_OK);
|
|
365 auto pixbufs = OS.gtk_window_get_icon_list (shellHandle);
|
|
366 if (pixbufs !is null) {
|
|
367 OS.gtk_window_set_icon_list (handle, pixbufs);
|
|
368 OS.g_list_free (pixbufs);
|
|
369 }
|
|
370 presetChooserDialog ();
|
|
371 Display display = parent !is null ? parent.getDisplay (): Display.getCurrent ();
|
|
372 display.addIdleProc ();
|
|
373 String answer = null;
|
|
374 Dialog oldModal = null;
|
|
375 if (OS.gtk_window_get_modal (handle)) {
|
|
376 oldModal = display.getModalDialog ();
|
|
377 display.setModalDialog (this);
|
|
378 }
|
|
379 uint signalId = 0;
|
|
380 uint /*long*/ hookId = 0;
|
|
381 CallbackData emissionData;
|
|
382 emissionData.display = display;
|
|
383 if ((style & SWT.RIGHT_TO_LEFT) !is 0) {
|
|
384 signalId = OS.g_signal_lookup (OS.map.ptr, OS.GTK_TYPE_WIDGET());
|
|
385 emissionData.data = handle;
|
|
386 hookId = OS.g_signal_add_emission_hook (signalId, 0, &Display.emissionFunc, &emissionData, null);
|
|
387 }
|
|
388 int response = OS.gtk_dialog_run (handle);
|
|
389 if ((style & SWT.RIGHT_TO_LEFT) !is 0) {
|
|
390 OS.g_signal_remove_emission_hook (signalId, hookId);
|
|
391 }
|
|
392 if (OS.gtk_window_get_modal (handle)) {
|
|
393 display.setModalDialog (oldModal);
|
|
394 }
|
|
395 if (response is OS.GTK_RESPONSE_OK) {
|
|
396 answer = computeResultChooserDialog ();
|
|
397 }
|
|
398 display.removeIdleProc ();
|
|
399 OS.gtk_widget_destroy (handle);
|
|
400 return answer;
|
|
401 }
|
|
402 String openClassicDialog () {
|
|
403 char* titleBytes = toStringz( title );
|
|
404 handle = OS.gtk_file_selection_new (titleBytes);
|
|
405 if (parent !is null) {
|
|
406 auto shellHandle = parent.topHandle ();
|
|
407 OS.gtk_window_set_transient_for (handle, shellHandle);
|
|
408 auto pixbufs = OS.gtk_window_get_icon_list (shellHandle);
|
|
409 if (pixbufs !is null) {
|
|
410 OS.gtk_window_set_icon_list (handle, pixbufs);
|
|
411 OS.g_list_free (pixbufs);
|
|
412 }
|
|
413 }
|
|
414 presetClassicDialog ();
|
|
415 Display display = parent !is null ? parent.getDisplay (): Display.getCurrent ();
|
|
416 display.addIdleProc ();
|
|
417 String answer = null;
|
|
418 Dialog oldModal = null;
|
|
419 if (OS.gtk_window_get_modal (handle)) {
|
|
420 oldModal = display.getModalDialog ();
|
|
421 display.setModalDialog (this);
|
|
422 }
|
|
423 int signalId = 0;
|
|
424 int /*long*/ hookId = 0;
|
|
425 CallbackData emissionData;
|
|
426 emissionData.display = display;
|
|
427 if ((style & SWT.RIGHT_TO_LEFT) !is 0) {
|
|
428 signalId = OS.g_signal_lookup (OS.map.ptr, OS.GTK_TYPE_WIDGET());
|
|
429 emissionData.data = handle;
|
|
430 hookId = OS.g_signal_add_emission_hook (signalId, 0, &Display.emissionFunc, &emissionData, null);
|
|
431 }
|
|
432 int response = OS.gtk_dialog_run (handle);
|
|
433 if ((style & SWT.RIGHT_TO_LEFT) !is 0) {
|
|
434 OS.g_signal_remove_emission_hook (signalId, hookId);
|
|
435 }
|
|
436 if (OS.gtk_window_get_modal (handle)) {
|
|
437 display.setModalDialog (oldModal);
|
|
438 }
|
|
439 if (response is OS.GTK_RESPONSE_OK) {
|
|
440 answer = computeResultClassicDialog ();
|
|
441 }
|
|
442 display.removeIdleProc ();
|
|
443 OS.gtk_widget_destroy (handle);
|
|
444 return answer;
|
|
445 }
|
|
446 void presetChooserDialog () {
|
|
447 /* MULTI is only valid if the native dialog's action is Open */
|
|
448 if ((style & (SWT.SAVE | SWT.MULTI)) is SWT.MULTI) {
|
|
449 OS.gtk_file_chooser_set_select_multiple (handle, true);
|
|
450 }
|
|
451 if (filterPath is null) filterPath = "";
|
|
452 if (fileName is null) fileName = "";
|
|
453 if (filterPath.length > 0) {
|
|
454 StringBuffer stringBuffer = new StringBuffer();
|
|
455 /* filename must be a full path */
|
|
456 if (filterPath[0] !is SEPARATOR) {
|
|
457 stringBuffer.append (SEPARATOR);
|
|
458 }
|
|
459 stringBuffer.append (filterPath);
|
|
460 if (fileName.length > 0 && (style & SWT.SAVE) is 0) {
|
|
461 if (filterPath[filterPath.length - 1 ] !is SEPARATOR) {
|
|
462 stringBuffer.append (SEPARATOR);
|
|
463 }
|
|
464 stringBuffer.append (fileName);
|
|
465 char* buffer = toStringz( stringBuffer.toString ());
|
|
466 /*
|
|
467 * Bug in GTK. GtkFileChooser may crash on GTK versions 2.4.10 to 2.6
|
|
468 * when setting a file name that is not a true canonical path.
|
|
469 * The fix is to use the canonical path.
|
|
470 */
|
|
471 auto ptr = OS.realpath (buffer, null);
|
|
472 if (ptr !is null) {
|
|
473 OS.gtk_file_chooser_set_filename (handle, ptr);
|
|
474 OS.g_free (ptr);
|
|
475 }
|
|
476 } else {
|
|
477 char* buffer = toStringz( stringBuffer.toString () );
|
|
478 /*
|
|
479 * Bug in GTK. GtkFileChooser may crash on GTK versions 2.4.10 to 2.6
|
|
480 * when setting a file name that is not a true canonical path.
|
|
481 * The fix is to use the canonical path.
|
|
482 */
|
|
483 auto ptr = OS.realpath (buffer, null);
|
|
484 if (ptr !is null) {
|
|
485 OS.gtk_file_chooser_set_current_folder (handle, ptr);
|
|
486 OS.g_free (ptr);
|
|
487 }
|
|
488 }
|
|
489 } else {
|
|
490 if (fileName.length > 0) {
|
|
491 if (fileName[0] is SEPARATOR) {
|
|
492 char* buffer = toStringz(fileName);
|
|
493
|
|
494 /*
|
|
495 * Bug in GTK. GtkFileChooser may crash on GTK versions 2.4.10 to 2.6
|
|
496 * when setting a file name that is not a true canonical path.
|
|
497 * The fix is to use the canonical path.
|
|
498 */
|
|
499 auto ptr = OS.realpath (buffer, null);
|
|
500 if (ptr !is null) {
|
|
501 OS.gtk_file_chooser_set_filename (handle, ptr);
|
|
502 OS.g_free (ptr);
|
|
503 }
|
|
504 }
|
|
505 }
|
|
506 }
|
|
507 if ((style & SWT.SAVE) !is 0 && fileName.length > 0) {
|
|
508 char* buffer = toStringz(fileName);
|
|
509 OS.gtk_file_chooser_set_current_name (handle, buffer);
|
|
510 }
|
|
511 if ((style & SWT.SAVE) !is 0) {
|
|
512 if (OS.GTK_VERSION >= OS.buildVERSION (2, 8, 0)) {
|
|
513 OS.gtk_file_chooser_set_do_overwrite_confirmation (handle, overwrite);
|
|
514 }
|
|
515 }
|
|
516
|
|
517 /* Set the extension filters */
|
|
518 if (filterNames is null) filterNames = null;
|
|
519 if (filterExtensions is null) filterExtensions = null;
|
|
520 GtkFileFilter* initialFilter = null;
|
|
521 for (int i = 0; i < filterExtensions.length; i++) {
|
|
522 if (filterExtensions [i] !is null) {
|
|
523 auto filter = OS.gtk_file_filter_new ();
|
|
524 if (filterNames.length > i && filterNames [i] !is null) {
|
|
525 char* name = toStringz(filterNames [i]);
|
|
526 OS.gtk_file_filter_set_name (filter, name);
|
|
527 } else {
|
|
528 char* name = toStringz(filterExtensions [i]);
|
|
529 OS.gtk_file_filter_set_name (filter, name);
|
|
530 }
|
|
531 int start = 0;
|
|
532 int index = tango.text.Util.locate( filterExtensions [i], EXTENSION_SEPARATOR );
|
|
533 while (index !is filterExtensions [i].length ) {
|
|
534 String current = filterExtensions [i][ start .. index ];
|
|
535 char* filterString = toStringz(current);
|
|
536 OS.gtk_file_filter_add_pattern (filter, filterString);
|
|
537 start = index + 1;
|
|
538 index = tango.text.Util.locate( filterExtensions [i], EXTENSION_SEPARATOR, start);
|
|
539 }
|
|
540 String current = filterExtensions [i][ start .. $ ];
|
|
541 char* filterString = toStringz(current);
|
|
542 OS.gtk_file_filter_add_pattern (filter, filterString);
|
|
543 OS.gtk_file_chooser_add_filter (handle, filter);
|
|
544 if (i is filterIndex) {
|
|
545 initialFilter = filter;
|
|
546 }
|
|
547 }
|
|
548 }
|
|
549 if (initialFilter !is null) {
|
|
550 OS.gtk_file_chooser_set_filter(handle, initialFilter);
|
|
551 }
|
|
552 fullPath = null;
|
|
553 fileNames = null;
|
|
554 }
|
|
555 void presetClassicDialog () {
|
|
556 OS.gtk_file_selection_set_select_multiple(handle, (style & SWT.MULTI) !is 0);
|
|
557
|
|
558 /* Calculate the fully-specified file name and convert to bytes */
|
|
559 StringBuffer stringBuffer = new StringBuffer();
|
|
560 if (filterPath is null) {
|
|
561 filterPath = "";
|
|
562 } else {
|
|
563 if (filterPath.length > 0) {
|
|
564 stringBuffer.append (filterPath);
|
|
565 if (filterPath[filterPath.length - 1] !is SEPARATOR) {
|
|
566 stringBuffer.append (SEPARATOR);
|
|
567 }
|
|
568 }
|
|
569 }
|
|
570 if (fileName is null) {
|
|
571 fileName = "";
|
|
572 } else {
|
|
573 stringBuffer.append (fileName);
|
|
574 }
|
|
575 fullPath = stringBuffer.toString ();
|
|
576 auto fileNamePtr = OS.g_filename_from_utf8 (toStringz( fullPath ), -1, null, null, null);
|
|
577 OS.gtk_file_selection_set_filename (handle, fileNamePtr);
|
|
578 OS.g_free (fileNamePtr);
|
|
579
|
|
580 if (filterNames is null) filterNames = null;
|
|
581 if (filterExtensions is null) filterExtensions = null;
|
|
582 fullPath = null;
|
|
583 fileNames = null;
|
|
584 }
|
|
585 /**
|
|
586 * Set the initial filename which the dialog will
|
|
587 * select by default when opened to the argument,
|
|
588 * which may be null. The name will be prefixed with
|
|
589 * the filter path when one is supplied.
|
|
590 *
|
|
591 * @param string the file name
|
|
592 */
|
|
593 public void setFileName (String string) {
|
|
594 fileName = string;
|
|
595 }
|
|
596 /**
|
|
597 * Set the file extensions which the dialog will
|
|
598 * use to filter the files it shows to the argument,
|
|
599 * which may be null.
|
|
600 * <p>
|
|
601 * The strings are platform specific. For example, on
|
|
602 * some platforms, an extension filter string is typically
|
|
603 * of the form "*.extension", where "*.*" matches all files.
|
|
604 * For filters with multiple extensions, use semicolon as
|
|
605 * a separator, e.g. "*.jpg;*.png".
|
|
606 * </p>
|
|
607 *
|
|
608 * @param extensions the file extension filter
|
|
609 *
|
|
610 * @see #setFilterNames to specify the user-friendly
|
|
611 * names corresponding to the extensions
|
|
612 */
|
|
613 public void setFilterExtensions (String [] extensions) {
|
|
614 filterExtensions = extensions;
|
|
615 }
|
|
616 /**
|
|
617 * Set the 0-based index of the file extension filter
|
|
618 * which the dialog will use initially to filter the files
|
|
619 * it shows to the argument.
|
|
620 * <p>
|
|
621 * This is an index into the FilterExtensions array and
|
|
622 * the FilterNames array.
|
|
623 * </p>
|
|
624 *
|
|
625 * @param index the file extension filter index
|
|
626 *
|
|
627 * @see #setFilterExtensions
|
|
628 * @see #setFilterNames
|
|
629 *
|
|
630 * @since 3.4
|
|
631 */
|
|
632 public void setFilterIndex (int index) {
|
|
633 filterIndex = index;
|
|
634 }
|
|
635 /**
|
|
636 * Sets the names that describe the filter extensions
|
|
637 * which the dialog will use to filter the files it shows
|
|
638 * to the argument, which may be null.
|
|
639 * <p>
|
|
640 * Each name is a user-friendly short description shown for
|
|
641 * its corresponding filter. The <code>names</code> array must
|
|
642 * be the same length as the <code>extensions</code> array.
|
|
643 * </p>
|
|
644 *
|
|
645 * @param names the list of filter names, or null for no filter names
|
|
646 *
|
|
647 * @see #setFilterExtensions
|
|
648 */
|
|
649 public void setFilterNames (String [] names) {
|
|
650 filterNames = names;
|
|
651 }
|
|
652 /**
|
|
653 * Sets the directory path that the dialog will use
|
|
654 * to the argument, which may be null. File names in this
|
|
655 * path will appear in the dialog, filtered according
|
|
656 * to the filter extensions. If the string is null,
|
|
657 * then the operating system's default filter path
|
|
658 * will be used.
|
|
659 * <p>
|
|
660 * Note that the path string is platform dependent.
|
|
661 * For convenience, either '/' or '\' can be used
|
|
662 * as a path separator.
|
|
663 * </p>
|
|
664 *
|
|
665 * @param string the directory path
|
|
666 *
|
|
667 * @see #setFilterExtensions
|
|
668 */
|
|
669 public void setFilterPath (String string) {
|
|
670 filterPath = string;
|
|
671 }
|
|
672
|
|
673 /**
|
|
674 * Sets the flag that the dialog will use to
|
|
675 * determine whether to prompt the user for file
|
|
676 * overwrite if the selected file already exists.
|
|
677 *
|
|
678 * @param overwrite true if the dialog will prompt for file overwrite, false otherwise
|
|
679 *
|
|
680 * @since 3.4
|
|
681 */
|
|
682 public void setOverwrite (bool overwrite) {
|
|
683 this.overwrite = overwrite;
|
|
684 }
|
|
685 }
|