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