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 32 33 public class DirectoryDialog extends Dialog { 34 String message = "", filterPath = ""; String directoryPath; 36 37 50 public DirectoryDialog (Shell parent) { 51 this (parent, SWT.PRIMARY_MODAL); 52 } 53 54 78 public DirectoryDialog (Shell parent, int style) { 79 super (parent, style); 80 checkSubclass (); 81 } 82 83 int BrowseCallbackProc (int hwnd, int uMsg, int lParam, int lpData) { 84 switch (uMsg) { 85 case OS.BFFM_INITIALIZED: 86 if (filterPath != null && filterPath.length () != 0) { 87 88 TCHAR buffer = new TCHAR (0, filterPath.replace ('/', '\\'), true); 89 OS.SendMessage (hwnd, OS.BFFM_SETSELECTION, 1, buffer); 90 } 91 if (title != null && title.length () != 0) { 92 93 TCHAR buffer = new TCHAR (0, title, true); 94 OS.SetWindowText (hwnd, buffer); 95 } 96 break; 97 case OS.BFFM_VALIDATEFAILEDA: 98 case OS.BFFM_VALIDATEFAILEDW: 99 100 int length = OS.IsUnicode ? OS.wcslen (lParam) : OS.strlen (lParam); 101 TCHAR buffer = new TCHAR (0, length); 102 int byteCount = buffer.length () * TCHAR.sizeof; 103 OS.MoveMemory (buffer, lParam, byteCount); 104 directoryPath = buffer.toString (0, length); 105 break; 106 } 107 return 0; 108 } 109 110 118 public String getFilterPath () { 119 return filterPath; 120 } 121 122 129 public String getMessage () { 130 return message; 131 } 132 133 145 public String open () { 146 if (OS.IsWinCE) SWT.error (SWT.ERROR_NOT_IMPLEMENTED); 147 148 int hHeap = OS.GetProcessHeap (); 149 150 151 int hwndOwner = 0; 152 if (parent != null) hwndOwner = parent.handle; 153 154 155 int lpszTitle = 0; 156 if (message.length () != 0) { 157 String string = message; 158 if (string.indexOf ('&') != -1) { 159 int length = string.length (); 160 char [] buffer = new char [length * 2]; 161 int index = 0; 162 for (int i=0; i<length; i++) { 163 char ch = string.charAt (i); 164 if (ch == '&') buffer [index++] = '&'; 165 buffer [index++] = ch; 166 } 167 string = new String (buffer, 0, index); 168 } 169 170 TCHAR buffer = new TCHAR (0, string, true); 171 int byteCount = buffer.length () * TCHAR.sizeof; 172 lpszTitle = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 173 OS.MoveMemory (lpszTitle, buffer, byteCount); 174 } 175 176 177 Callback callback = new Callback (this, "BrowseCallbackProc", 4); int lpfn = callback.getAddress (); 179 if (lpfn == 0) SWT.error (SWT.ERROR_NO_MORE_CALLBACKS); 180 181 182 Shell oldModal = null; 183 Display display = parent.getDisplay (); 184 if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) { 185 oldModal = display.getModalDialogShell (); 186 display.setModalDialogShell (parent); 187 } 188 189 directoryPath = null; 190 BROWSEINFO lpbi = new BROWSEINFO (); 191 lpbi.hwndOwner = hwndOwner; 192 lpbi.lpszTitle = lpszTitle; 193 lpbi.ulFlags = OS.BIF_NEWDIALOGSTYLE | OS.BIF_RETURNONLYFSDIRS | OS.BIF_EDITBOX | OS.BIF_VALIDATE; 194 lpbi.lpfn = lpfn; 195 212 int oldErrorMode = OS.SetErrorMode (OS.SEM_FAILCRITICALERRORS); 213 214 225 boolean oldRunMessages = display.runMessages; 226 if (OS.COMCTL32_MAJOR < 6) display.runMessages = false; 227 int lpItemIdList = OS.SHBrowseForFolder (lpbi); 228 if (OS.COMCTL32_MAJOR < 6) display.runMessages = oldRunMessages; 229 OS.SetErrorMode (oldErrorMode); 230 231 232 if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) { 233 display.setModalDialogShell (oldModal); 234 } 235 236 boolean success = lpItemIdList != 0; 237 if (success) { 238 239 TCHAR buffer = new TCHAR (0, OS.MAX_PATH); 240 if (OS.SHGetPathFromIDList (lpItemIdList, buffer)) { 241 directoryPath = buffer.toString (0, buffer.strlen ()); 242 filterPath = directoryPath; 243 } 244 } 245 246 247 callback.dispose (); 248 249 250 if (lpszTitle != 0) OS.HeapFree (hHeap, 0, lpszTitle); 251 252 253 int [] ppMalloc = new int [1]; 254 if (OS.SHGetMalloc (ppMalloc) == OS.S_OK) { 255 256 OS.VtblCall (5, ppMalloc [0], lpItemIdList); 257 } 258 259 265 267 268 if (!success) return null; 269 return directoryPath; 270 } 271 272 285 public void setFilterPath (String string) { 286 filterPath = string; 287 } 288 289 300 public void setMessage (String string) { 301 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 302 message = string; 303 } 304 305 } 306 | Popular Tags |