1 11 package org.eclipse.swt.widgets; 12 13 14 import org.eclipse.swt.internal.*; 15 import org.eclipse.swt.internal.win32.*; 16 import org.eclipse.swt.*; 17 18 34 public class FileDialog extends Dialog { 35 String [] filterNames = new String [0]; 36 String [] filterExtensions = new String [0]; 37 String [] fileNames = new String [0]; 38 String filterPath = "", fileName = ""; 39 static final String FILTER = "*.*"; 40 static int BUFFER_SIZE = 1024 * 32; 41 static boolean USE_HOOK; 42 43 56 public FileDialog (Shell parent) { 57 this (parent, SWT.PRIMARY_MODAL); 58 } 59 60 84 public FileDialog (Shell parent, int style) { 85 super (parent, style); 86 checkSubclass (); 87 } 88 89 96 public String getFileName () { 97 return fileName; 98 } 99 100 106 public String [] getFileNames () { 107 return fileNames; 108 } 109 110 116 public String [] getFilterExtensions () { 117 return filterExtensions; 118 } 119 120 126 public String [] getFilterNames () { 127 return filterNames; 128 } 129 130 139 public String getFilterPath () { 140 return filterPath; 141 } 142 143 int OFNHookProc (int hdlg, int uiMsg, int wParam, int lParam) { 144 switch (uiMsg) { 145 case OS.WM_NOTIFY: 146 OFNOTIFY ofn = new OFNOTIFY (); 147 OS.MoveMemory (ofn, lParam, OFNOTIFY.sizeof); 148 if (ofn.code == OS.CDN_SELCHANGE) { 149 int lResult = OS.SendMessage (ofn.hwndFrom, OS.CDM_GETSPEC, 0, 0); 150 if (lResult > 0) { 151 lResult += OS.MAX_PATH; 152 OPENFILENAME lpofn = new OPENFILENAME (); 153 OS.MoveMemory (lpofn, ofn.lpOFN, OPENFILENAME.sizeof); 154 if (lpofn.nMaxFile < lResult) { 155 int hHeap = OS.GetProcessHeap (); 156 int lpstrFile = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, lResult * TCHAR.sizeof); 157 if (lpstrFile != 0) { 158 if (lpofn.lpstrFile != 0) OS.HeapFree (hHeap, 0, lpofn.lpstrFile); 159 lpofn.lpstrFile = lpstrFile; 160 lpofn.nMaxFile = lResult; 161 OS.MoveMemory (ofn.lpOFN, lpofn, OPENFILENAME.sizeof); 162 } 163 } 164 } 165 } 166 break; 167 } 168 return 0; 169 } 170 171 183 public String open () { 184 int hHeap = OS.GetProcessHeap (); 185 186 187 int hwndOwner = 0; 188 if (parent != null) hwndOwner = parent.handle; 189 190 191 if (title == null) title = ""; 192 193 TCHAR buffer3 = new TCHAR (0, title, true); 194 int byteCount3 = buffer3.length () * TCHAR.sizeof; 195 int lpstrTitle = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount3); 196 OS.MoveMemory (lpstrTitle, buffer3, byteCount3); 197 198 199 String strFilter = ""; 200 if (filterNames == null) filterNames = new String [0]; 201 if (filterExtensions == null) filterExtensions = new String [0]; 202 for (int i=0; i<filterExtensions.length; i++) { 203 String filterName = filterExtensions [i]; 204 if (i < filterNames.length) filterName = filterNames [i]; 205 strFilter = strFilter + filterName + '\0' + filterExtensions [i] + '\0'; 206 } 207 if (filterExtensions.length == 0) { 208 strFilter = strFilter + FILTER + '\0' + FILTER + '\0'; 209 } 210 211 TCHAR buffer4 = new TCHAR (0, strFilter, true); 212 int byteCount4 = buffer4.length () * TCHAR.sizeof; 213 int lpstrFilter = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount4); 214 OS.MoveMemory (lpstrFilter, buffer4, byteCount4); 215 216 217 if (fileName == null) fileName = ""; 218 219 TCHAR name = new TCHAR (0, fileName, true); 220 221 225 int nMaxFile = OS.MAX_PATH; 226 if ((style & SWT.MULTI) != 0) nMaxFile = Math.max (nMaxFile, BUFFER_SIZE); 227 int byteCount = nMaxFile * TCHAR.sizeof; 228 int lpstrFile = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 229 int byteCountFile = Math.min (name.length () * TCHAR.sizeof, byteCount - TCHAR.sizeof); 230 OS.MoveMemory (lpstrFile, name, byteCountFile); 231 232 236 if (filterPath == null) filterPath = ""; 237 238 TCHAR path = new TCHAR (0, filterPath.replace ('/', '\\'), true); 239 int byteCount5 = OS.MAX_PATH * TCHAR.sizeof; 240 int lpstrInitialDir = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount5); 241 int byteCountDir = Math.min (path.length () * TCHAR.sizeof, byteCount5 - TCHAR.sizeof); 242 OS.MoveMemory (lpstrInitialDir, path, byteCountDir); 243 244 245 OPENFILENAME struct = new OPENFILENAME (); 246 struct.lStructSize = OPENFILENAME.sizeof; 247 struct.Flags = OS.OFN_HIDEREADONLY | OS.OFN_NOCHANGEDIR; 248 Callback callback = null; 249 if ((style & SWT.MULTI) != 0) { 250 struct.Flags |= OS.OFN_ALLOWMULTISELECT | OS.OFN_EXPLORER; 251 if (!OS.IsWinCE && USE_HOOK) { 252 callback = new Callback (this, "OFNHookProc", 4); int lpfnHook = callback.getAddress (); 254 if (lpfnHook == 0) SWT.error (SWT.ERROR_NO_MORE_CALLBACKS); 255 struct.lpfnHook = lpfnHook; 256 struct.Flags |= OS.OFN_ENABLEHOOK; 257 } 258 } 259 struct.hwndOwner = hwndOwner; 260 struct.lpstrTitle = lpstrTitle; 261 struct.lpstrFile = lpstrFile; 262 struct.nMaxFile = nMaxFile; 263 struct.lpstrInitialDir = lpstrInitialDir; 264 struct.lpstrFilter = lpstrFilter; 265 struct.nFilterIndex = 0; 266 267 273 int lpstrDefExt = 0; 274 boolean save = (style & SWT.SAVE) != 0; 275 if (save) { 276 lpstrDefExt = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, TCHAR.sizeof); 277 struct.lpstrDefExt = lpstrDefExt; 278 } 279 280 281 Shell oldModal = null; 282 Display display = parent.getDisplay (); 283 if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) { 284 oldModal = display.getModalDialogShell (); 285 display.setModalDialogShell (parent); 286 } 287 288 300 boolean oldRunMessagesInIdle = display.runMessagesInIdle; 301 display.runMessagesInIdle = !OS.IsWin95; 302 306 boolean success = (save) ? OS.GetSaveFileName (struct) : OS.GetOpenFileName (struct); 307 switch (OS.CommDlgExtendedError ()) { 308 case OS.FNERR_INVALIDFILENAME: 309 OS.MoveMemory (lpstrFile, new TCHAR (0, "", true), TCHAR.sizeof); 310 success = (save) ? OS.GetSaveFileName (struct) : OS.GetOpenFileName (struct); 311 break; 312 case OS.FNERR_BUFFERTOOSMALL: 313 USE_HOOK = true; 314 break; 315 } 316 display.runMessagesInIdle = oldRunMessagesInIdle; 317 318 319 if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) { 320 display.setModalDialogShell (oldModal); 321 } 322 323 324 if (callback != null) callback.dispose (); 325 lpstrFile = struct.lpstrFile; 326 327 328 fileNames = new String [0]; 329 String fullPath = null; 330 if (success) { 331 332 333 TCHAR buffer = new TCHAR (0, struct.nMaxFile); 334 int byteCount1 = buffer.length () * TCHAR.sizeof; 335 OS.MoveMemory (buffer, lpstrFile, byteCount1); 336 337 346 int nFileOffset = struct.nFileOffset; 347 if (OS.IsWinCE && nFileOffset == 0) { 348 int index = 0; 349 while (index < buffer.length ()) { 350 int ch = buffer.tcharAt (index); 351 if (ch == 0) break; 352 if (ch == '\\') nFileOffset = index + 1; 353 index++; 354 } 355 } 356 if (nFileOffset > 0) { 357 358 359 TCHAR prefix = new TCHAR (0, nFileOffset - 1); 360 int byteCount2 = prefix.length () * TCHAR.sizeof; 361 OS.MoveMemory (prefix, lpstrFile, byteCount2); 362 filterPath = prefix.toString (0, prefix.length ()); 363 364 368 int count = 0; 369 fileNames = new String [(style & SWT.MULTI) != 0 ? 4 : 1]; 370 int start = nFileOffset; 371 do { 372 int end = start; 373 while (end < buffer.length () && buffer.tcharAt (end) != 0) end++; 374 String string = buffer.toString (start, end - start); 375 start = end; 376 if (count == fileNames.length) { 377 String [] newFileNames = new String [fileNames.length + 4]; 378 System.arraycopy (fileNames, 0, newFileNames, 0, fileNames.length); 379 fileNames = newFileNames; 380 } 381 fileNames [count++] = string; 382 if ((style & SWT.MULTI) == 0) break; 383 start++; 384 } while (start < buffer.length () && buffer.tcharAt (start) != 0); 385 386 if (fileNames.length > 0) fileName = fileNames [0]; 387 String separator = ""; 388 int length = filterPath.length (); 389 if (length > 0 && filterPath.charAt (length - 1) != '\\') { 390 separator = "\\"; 391 } 392 fullPath = filterPath + separator + fileName; 393 if (count < fileNames.length) { 394 String [] newFileNames = new String [count]; 395 System.arraycopy (fileNames, 0, newFileNames, 0, count); 396 fileNames = newFileNames; 397 } 398 } 399 } 400 401 402 OS.HeapFree (hHeap, 0, lpstrFile); 403 OS.HeapFree (hHeap, 0, lpstrFilter); 404 OS.HeapFree (hHeap, 0, lpstrInitialDir); 405 OS.HeapFree (hHeap, 0, lpstrTitle); 406 if (lpstrDefExt != 0) OS.HeapFree (hHeap, 0, lpstrDefExt); 407 408 414 416 417 return fullPath; 418 } 419 420 428 public void setFileName (String string) { 429 fileName = string; 430 } 431 432 447 public void setFilterExtensions (String [] extensions) { 448 filterExtensions = extensions; 449 } 450 451 465 public void setFilterNames (String [] names) { 466 filterNames = names; 467 } 468 469 486 public void setFilterPath (String string) { 487 filterPath = string; 488 } 489 490 } 491 | Popular Tags |