Mercurial > projects > dwt-linux
annotate dwt/widgets/DirectoryDialog.d @ 259:c0d810de7093
Update SWT 3.4M7 to 3.4
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 29 Jun 2008 14:33:38 +0200 |
parents | 5a30aa9820f3 |
children | 07d9ed8927b6 |
rev | line source |
---|---|
126
73b4c55700ec
Fix in DirectoryDialog string convertion
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
1 /******************************************************************************* |
259 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
78 | 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 | |
108 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
78 | 12 *******************************************************************************/ |
13 module dwt.widgets.DirectoryDialog; | |
14 | |
238 | 15 import dwt.dwthelper.utils; |
16 | |
78 | 17 |
18 | |
19 import dwt.DWT; | |
20 import dwt.DWTException; | |
21 import dwt.internal.gtk.OS; | |
22 import dwt.widgets.Dialog; | |
23 import dwt.widgets.Shell; | |
24 import dwt.widgets.Display; | |
25 | |
26 static import tango.io.FileConst; | |
27 static import tango.text.Util; | |
28 | |
29 /** | |
30 * Instances of this class allow the user to navigate | |
31 * the file system and select a directory. | |
32 * <dl> | |
33 * <dt><b>Styles:</b></dt> | |
34 * <dd>(none)</dd> | |
35 * <dt><b>Events:</b></dt> | |
36 * <dd>(none)</dd> | |
37 * </dl> | |
38 * <p> | |
39 * IMPORTANT: This class is intended to be subclassed <em>only</em> | |
40 * within the DWT implementation. | |
41 * </p> | |
259 | 42 * |
43 * @see <a href="http://www.eclipse.org/swt/snippets/#directorydialog">DirectoryDialog snippets</a> | |
44 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample, Dialog tab</a> | |
45 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
78 | 46 */ |
47 public class DirectoryDialog : Dialog { | |
238 | 48 String message = "", filterPath = ""; |
49 static const String SEPARATOR = tango.io.FileConst.FileConst.PathSeparatorString;; | |
78 | 50 |
51 /** | |
52 * Constructs a new instance of this class given only its parent. | |
53 * | |
54 * @param parent a shell which will be the parent of the new instance | |
55 * | |
56 * @exception IllegalArgumentException <ul> | |
57 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
58 * </ul> | |
59 * @exception DWTException <ul> | |
60 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
61 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
62 * </ul> | |
63 */ | |
64 public this (Shell parent) { | |
240 | 65 this (parent, DWT.APPLICATION_MODAL); |
78 | 66 } |
67 /** | |
68 * Constructs a new instance of this class given its parent | |
69 * and a style value describing its behavior and appearance. | |
70 * <p> | |
71 * The style value is either one of the style constants defined in | |
72 * class <code>DWT</code> which is applicable to instances of this | |
73 * class, or must be built by <em>bitwise OR</em>'ing together | |
74 * (that is, using the <code>int</code> "|" operator) two or more | |
75 * of those <code>DWT</code> style constants. The class description | |
76 * lists the style constants that are applicable to the class. | |
77 * Style bits are also inherited from superclasses. | |
78 * </p> | |
79 * | |
80 * @param parent a shell which will be the parent of the new instance | |
81 * @param style the style of dialog to construct | |
82 * | |
83 * @exception IllegalArgumentException <ul> | |
84 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
85 * </ul> | |
86 * @exception DWTException <ul> | |
87 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
88 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
89 * </ul> | |
90 */ | |
91 public this (Shell parent, int style) { | |
240 | 92 super (parent, checkStyle (parent, style)); |
78 | 93 checkSubclass (); |
94 } | |
95 /** | |
96 * Returns the path which the dialog will use to filter | |
97 * the directories it shows. | |
98 * | |
99 * @return the filter path | |
100 * | |
101 * @see #setFilterPath | |
102 */ | |
238 | 103 public String getFilterPath () { |
78 | 104 return filterPath; |
105 } | |
106 /** | |
107 * Returns the dialog's message, which is a description of | |
108 * the purpose for which it was opened. This message will be | |
109 * visible on the dialog while it is open. | |
110 * | |
111 * @return the message | |
112 */ | |
238 | 113 public String getMessage () { |
78 | 114 return message; |
115 } | |
116 /** | |
117 * Makes the dialog visible and brings it to the front | |
118 * of the display. | |
119 * | |
120 * @return a string describing the absolute path of the selected directory, | |
121 * or null if the dialog was cancelled or an error occurred | |
122 * | |
123 * @exception DWTException <ul> | |
124 * <li>ERROR_WIDGET_DISPOSED - if the dialog has been disposed</li> | |
125 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog</li> | |
126 * </ul> | |
127 */ | |
238 | 128 public String open () { |
78 | 129 bool useChooserDialog = OS.GTK_VERSION >= OS.buildVERSION (2, 4, 10); |
130 if (useChooserDialog) { | |
131 return openChooserDialog (); | |
132 } else { | |
133 return openClassicDialog (); | |
134 } | |
135 } | |
238 | 136 String openChooserDialog () { |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
137 char* titleBytes = toStringz(title); |
78 | 138 auto shellHandle = parent.topHandle (); |
139 auto handle = OS.gtk_file_chooser_dialog_new2 ( | |
140 titleBytes, | |
141 shellHandle, | |
142 OS.GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, | |
143 OS.GTK_STOCK_CANCEL (), OS.GTK_RESPONSE_CANCEL, | |
144 OS.GTK_STOCK_OK (), OS.GTK_RESPONSE_OK ); | |
145 auto pixbufs = OS.gtk_window_get_icon_list (shellHandle); | |
146 if (pixbufs !is null) { | |
147 OS.gtk_window_set_icon_list (handle, pixbufs); | |
148 OS.g_list_free (pixbufs); | |
149 } | |
150 if (filterPath !is null && filterPath.length > 0) { | |
238 | 151 String p; |
78 | 152 /* filename must be a full path */ |
153 if ( filterPath[ 0 .. SEPARATOR.length ] != SEPARATOR ) { | |
154 p ~= SEPARATOR; | |
155 p ~= filterPath; | |
156 } | |
157 else{ | |
158 p = filterPath; | |
159 } | |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
160 char* buffer = toStringz(p); |
240 | 161 /* |
162 * Bug in GTK. GtkFileChooser may crash on GTK versions 2.4.10 to 2.6 | |
163 * when setting a file name that is not a true canonical path. | |
164 * The fix is to use the canonical path. | |
165 */ | |
166 char* ptr = OS.realpath (buffer, null); | |
167 if (ptr !is null) { | |
168 OS.gtk_file_chooser_set_current_folder (handle, ptr); | |
169 OS.g_free (ptr); | |
170 } | |
78 | 171 } |
172 if (message.length > 0) { | |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
173 char* buffer = toStringz(message); |
78 | 174 auto box = OS.gtk_hbox_new (false, 0); |
175 if (box is null) error (DWT.ERROR_NO_HANDLES); | |
176 auto label = OS.gtk_label_new (buffer); | |
177 if (label is null) error (DWT.ERROR_NO_HANDLES); | |
178 OS.gtk_container_add (box, label); | |
179 OS.gtk_widget_show (label); | |
180 OS.gtk_label_set_line_wrap (label, true); | |
181 OS.gtk_label_set_justify (label, OS.GTK_JUSTIFY_CENTER); | |
182 OS.gtk_file_chooser_set_extra_widget (handle, box); | |
183 } | |
238 | 184 String answer = null; |
78 | 185 Display display = parent !is null ? parent.getDisplay (): Display.getCurrent (); |
186 display.addIdleProc (); | |
240 | 187 Dialog oldModal = null; |
188 if (OS.gtk_window_get_modal (handle)) { | |
189 oldModal = display.getModalDialog (); | |
190 display.setModalDialog (this); | |
191 } | |
192 int signalId = 0; | |
193 int /*long*/ hookId = 0; | |
194 CallbackData emissionData; | |
195 emissionData.display = display; | |
196 emissionData.data = handle; | |
197 if ((style & DWT.RIGHT_TO_LEFT) !is 0) { | |
198 signalId = OS.g_signal_lookup (OS.map.ptr, OS.GTK_TYPE_WIDGET()); | |
199 hookId = OS.g_signal_add_emission_hook (signalId, 0, &Display.emissionFunc, &emissionData, null); | |
200 } | |
78 | 201 int response = OS.gtk_dialog_run (handle); |
240 | 202 if ((style & DWT.RIGHT_TO_LEFT) !is 0) { |
203 OS.g_signal_remove_emission_hook (signalId, hookId); | |
204 } | |
205 if (OS.gtk_window_get_modal (handle)) { | |
206 display.setModalDialog (oldModal); | |
207 } | |
78 | 208 if (response is OS.GTK_RESPONSE_OK) { |
209 auto path = OS.gtk_file_chooser_get_filename (handle); | |
210 if (path !is null) { | |
126
73b4c55700ec
Fix in DirectoryDialog string convertion
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
211 uint items_written; |
73b4c55700ec
Fix in DirectoryDialog string convertion
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
212 auto utf8Ptr = OS.g_filename_to_utf8 (path, -1, null, &items_written, null); |
78 | 213 OS.g_free (path); |
214 if (utf8Ptr !is null) { | |
126
73b4c55700ec
Fix in DirectoryDialog string convertion
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
215 answer = utf8Ptr[ 0 .. items_written ].dup; |
240 | 216 filterPath = answer; |
78 | 217 OS.g_free (utf8Ptr); |
218 } | |
219 } | |
220 } | |
221 display.removeIdleProc (); | |
222 OS.gtk_widget_destroy (handle); | |
223 return answer; | |
224 } | |
238 | 225 String openClassicDialog () { |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
226 char* titleBytes = toStringz(title); |
78 | 227 auto handle = OS.gtk_file_selection_new (titleBytes); |
228 if (parent !is null) { | |
229 auto shellHandle = parent.topHandle (); | |
230 OS.gtk_window_set_transient_for (handle, shellHandle); | |
231 auto pixbufs = OS.gtk_window_get_icon_list (shellHandle); | |
232 if (pixbufs !is null) { | |
233 OS.gtk_window_set_icon_list (handle, pixbufs); | |
234 OS.g_list_free (pixbufs); | |
235 } | |
236 } | |
238 | 237 String answer = null; |
78 | 238 if (filterPath !is null) { |
238 | 239 String path = filterPath; |
78 | 240 if (path.length > 0 && path[ $-1 .. $ ] != SEPARATOR ) { |
241 path ~= SEPARATOR; | |
242 } | |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
243 char* fileNamePtr = OS.g_filename_from_utf8 (toStringz(path), -1, null, null, null); |
78 | 244 OS.gtk_file_selection_set_filename (handle, fileNamePtr); |
245 OS.g_free (fileNamePtr); | |
246 } | |
247 GtkFileSelection* selection = cast(GtkFileSelection*)handle; | |
248 OS.gtk_file_selection_hide_fileop_buttons (handle); | |
249 auto fileListParent = OS.gtk_widget_get_parent (selection.file_list); | |
250 OS.gtk_widget_hide (selection.file_list); | |
251 OS.gtk_widget_hide (fileListParent); | |
252 if (message.length > 0) { | |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
253 auto labelHandle = OS.gtk_label_new (toStringz(message)); |
78 | 254 OS.gtk_label_set_line_wrap (labelHandle, true); |
255 OS.gtk_misc_set_alignment (labelHandle, 0.0f, 0.0f); | |
256 OS.gtk_container_add (selection.main_vbox, labelHandle); | |
257 OS.gtk_box_set_child_packing ( | |
258 selection.main_vbox, labelHandle, false, false, 0, OS.GTK_PACK_START); | |
259 OS.gtk_widget_show (labelHandle); | |
260 } | |
261 Display display = parent !is null ? parent.getDisplay (): Display.getCurrent (); | |
262 display.addIdleProc (); | |
240 | 263 Dialog oldModal = null; |
264 if (OS.gtk_window_get_modal (handle)) { | |
265 oldModal = display.getModalDialog (); | |
266 display.setModalDialog (this); | |
267 } | |
268 int signalId = 0; | |
269 int /*long*/ hookId = 0; | |
270 CallbackData emissionData; | |
271 emissionData.display = display; | |
272 emissionData.data = handle; | |
273 if ((style & DWT.RIGHT_TO_LEFT) !is 0) { | |
274 signalId = OS.g_signal_lookup (OS.map.ptr, OS.GTK_TYPE_WIDGET()); | |
275 hookId = OS.g_signal_add_emission_hook (signalId, 0, &Display.emissionFunc, &emissionData, null); | |
276 } | |
78 | 277 int response = OS.gtk_dialog_run (handle); |
240 | 278 if ((style & DWT.RIGHT_TO_LEFT) !is 0) { |
279 OS.g_signal_remove_emission_hook (signalId, hookId); | |
280 } | |
281 if (OS.gtk_window_get_modal (handle)) { | |
282 display.setModalDialog (oldModal); | |
283 } | |
78 | 284 if (response is OS.GTK_RESPONSE_OK) { |
285 char* fileNamePtr = OS.gtk_file_selection_get_filename (handle); | |
126
73b4c55700ec
Fix in DirectoryDialog string convertion
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
286 uint items_written; |
73b4c55700ec
Fix in DirectoryDialog string convertion
Frank Benoit <benoit@tionex.de>
parents:
108
diff
changeset
|
287 char* utf8Ptr = OS.g_filename_to_utf8 (fileNamePtr, -1, null, &items_written, null); |
78 | 288 if (utf8Ptr !is null) { |
238 | 289 String osAnswer = utf8Ptr[ 0 .. items_written ]; |
78 | 290 if (osAnswer.length !is 0) { |
291 /* remove trailing separator, unless root directory */ | |
292 if ( osAnswer != SEPARATOR && osAnswer[ $-1 .. $ ] == SEPARATOR ) { | |
293 osAnswer = osAnswer[ 0 .. $ - 1 ]; | |
294 } | |
295 answer = filterPath = osAnswer.dup; | |
296 } | |
297 OS.g_free (utf8Ptr); | |
298 } | |
299 } | |
300 display.removeIdleProc (); | |
301 OS.gtk_widget_destroy (handle); | |
302 return answer; | |
303 } | |
304 /** | |
305 * Sets the path that the dialog will use to filter | |
306 * the directories it shows to the argument, which may | |
307 * be null. If the string is null, then the operating | |
308 * system's default filter path will be used. | |
309 * <p> | |
310 * Note that the path string is platform dependent. | |
311 * For convenience, either '/' or '\' can be used | |
312 * as a path separator. | |
313 * </p> | |
314 * | |
315 * @param string the filter path | |
316 */ | |
238 | 317 public void setFilterPath (String string) { |
78 | 318 filterPath = string.dup; |
319 } | |
320 /** | |
321 * Sets the dialog's message, which is a description of | |
322 * the purpose for which it was opened. This message will be | |
323 * visible on the dialog while it is open. | |
324 * | |
325 * @param string the message | |
326 * | |
327 */ | |
238 | 328 public void setMessage (String string) { |
255
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
329 // DWT extension: allow null for zero length string |
5a30aa9820f3
removed tango.stdc.stringz imports and allow null for arrays and string arguments.
Frank Benoit <benoit@tionex.de>
parents:
240
diff
changeset
|
330 //if (string is null) error (DWT.ERROR_NULL_ARGUMENT); |
78 | 331 message = string.dup; |
332 } | |
333 } |