- fixed reported bugs
[wine.git] / dlls / commdlg / filedlg95.c
blobdf3763c07786891da7b0f05f14fa0376bd4838f8
1 /*
2 * COMMDLG - File Open Dialogs Win95 look and feel
4 * FIXME: lpstrCustomFilter not handeled
6 * FIXME: if the size of lpstrFile (nMaxFile) is to small the first
7 * two bytes of lpstrFile should contain the needed size
9 * FIXME: algorithm for selecting the initial directory is to simple
11 * FIXME: add to recent docs
13 * FIXME: flags not implemented: OFN_CREATEPROMPT, OFN_DONTADDTORECENT,
14 * OFN_ENABLEINCLUDENOTIFY, OFN_ENABLESIZING, OFN_EXTENSIONDIFFERENT,
15 * OFN_NOCHANGEDIR, OFN_NODEREFERENCELINKS, OFN_READONLYRETURN,
16 * OFN_NOTESTFILECREATE, OFN_OVERWRITEPROMPT, OFN_USEMONIKERS
18 * FIXME: lCustData for lpfnHook (WM_INITDIALOG)
22 #include <ctype.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
27 #include "winbase.h"
28 #include "ldt.h"
29 #include "heap.h"
30 #include "commdlg.h"
31 #include "dlgs.h"
32 #include "cdlg.h"
33 #include "debugtools.h"
34 #include "cderr.h"
35 #include "tweak.h"
36 #include "winnls.h"
37 #include "shellapi.h"
38 #include "filedlgbrowser.h"
39 #include "shlwapi.h"
40 #include "wine/obj_contextmenu.h"
42 DEFAULT_DEBUG_CHANNEL(commdlg);
44 /***********************************************************************
45 * Data structure and global variables
47 typedef struct SFolder
49 int m_iImageIndex; /* Index of picture in image list */
50 HIMAGELIST hImgList;
51 int m_iIndent; /* Indentation index */
52 LPITEMIDLIST pidlItem; /* absolute pidl of the item */
54 } SFOLDER,*LPSFOLDER;
56 typedef struct tagLookInInfo
58 int iMaxIndentation;
59 UINT uSelectedItem;
60 } LookInInfos;
63 /***********************************************************************
64 * Defines and global variables
67 /* Draw item constant */
68 #define ICONWIDTH 18
69 #define YTEXTOFFSET 2
70 #define XTEXTOFFSET 3
72 /* AddItem flags*/
73 #define LISTEND -1
75 /* SearchItem methods */
76 #define SEARCH_PIDL 1
77 #define SEARCH_EXP 2
78 #define ITEM_NOTFOUND -1
80 /* Undefined windows message sent by CreateViewObject*/
81 #define WM_GETISHELLBROWSER WM_USER+7
83 /* NOTE
84 * Those macros exist in windowsx.h. However, you can't really use them since
85 * they rely on the UNICODE defines and can't be use inside Wine itself.
88 /* Combo box macros */
89 #define CBAddString(hwnd,str) \
90 SendMessageA(hwnd,CB_ADDSTRING,0,(LPARAM)str);
92 #define CBInsertString(hwnd,str,pos) \
93 SendMessageA(hwnd,CB_INSERTSTRING,(WPARAM)pos,(LPARAM)str);
95 #define CBDeleteString(hwnd,pos) \
96 SendMessageA(hwnd,CB_DELETESTRING,(WPARAM)pos,0);
98 #define CBSetItemDataPtr(hwnd,iItemId,dataPtr) \
99 SendMessageA(hwnd,CB_SETITEMDATA,(WPARAM)iItemId,(LPARAM)dataPtr);
101 #define CBGetItemDataPtr(hwnd,iItemId) \
102 SendMessageA(hwnd,CB_GETITEMDATA,(WPARAM)iItemId,0)
104 #define CBGetLBText(hwnd,iItemId,str) \
105 SendMessageA(hwnd,CB_GETLBTEXT,(WPARAM)iItemId,(LPARAM)str);
107 #define CBGetCurSel(hwnd) \
108 SendMessageA(hwnd,CB_GETCURSEL,0,0);
110 #define CBSetCurSel(hwnd,pos) \
111 SendMessageA(hwnd,CB_SETCURSEL,(WPARAM)pos,0);
113 #define CBGetCount(hwnd) \
114 SendMessageA(hwnd,CB_GETCOUNT,0,0);
115 #define CBShowDropDown(hwnd,show) \
116 SendMessageA(hwnd,CB_SHOWDROPDOWN,(WPARAM)show,0);
117 #define CBSetItemHeight(hwnd,index,height) \
118 SendMessageA(hwnd,CB_SETITEMHEIGHT,(WPARAM)index,(LPARAM)height);
121 const char *FileOpenDlgInfosStr = "FileOpenDlgInfos"; /* windows property description string */
122 const char *LookInInfosStr = "LookInInfos"; /* LOOKIN combo box property */
124 static const char defaultFilter[] = "*.*";
126 /***********************************************************************
127 * Prototypes
130 /* Internal functions used by the dialog */
131 static LRESULT FILEDLG95_OnWMInitDialog(HWND hwnd, WPARAM wParam, LPARAM lParam);
132 static LRESULT FILEDLG95_OnWMCommand(HWND hwnd, WPARAM wParam, LPARAM lParam);
133 static LRESULT FILEDLG95_OnWMGetIShellBrowser(HWND hwnd);
134 BOOL FILEDLG95_OnOpen(HWND hwnd);
135 static LRESULT FILEDLG95_InitUI(HWND hwnd);
136 static void FILEDLG95_Clean(HWND hwnd);
138 /* Functions used by the shell navigation */
139 static LRESULT FILEDLG95_SHELL_Init(HWND hwnd);
140 static BOOL FILEDLG95_SHELL_UpFolder(HWND hwnd);
141 static BOOL FILEDLG95_SHELL_ExecuteCommand(HWND hwnd, LPCSTR lpVerb);
142 static void FILEDLG95_SHELL_Clean(HWND hwnd);
143 static BOOL FILEDLG95_SHELL_BrowseToDesktop(HWND hwnd);
145 /* Functions used by the filetype combo box */
146 static HRESULT FILEDLG95_FILETYPE_Init(HWND hwnd);
147 static BOOL FILEDLG95_FILETYPE_OnCommand(HWND hwnd, WORD wNotifyCode);
148 static int FILEDLG95_FILETYPE_SearchExt(HWND hwnd,LPCSTR lpstrExt);
149 static void FILEDLG95_FILETYPE_Clean(HWND hwnd);
151 /* Functions used by the Look In combo box */
152 static HRESULT FILEDLG95_LOOKIN_Init(HWND hwndCombo);
153 static LRESULT FILEDLG95_LOOKIN_DrawItem(LPDRAWITEMSTRUCT pDIStruct);
154 static BOOL FILEDLG95_LOOKIN_OnCommand(HWND hwnd, WORD wNotifyCode);
155 static int FILEDLG95_LOOKIN_AddItem(HWND hwnd,LPITEMIDLIST pidl, int iInsertId);
156 static int FILEDLG95_LOOKIN_SearchItem(HWND hwnd,WPARAM searchArg,int iSearchMethod);
157 static int FILEDLG95_LOOKIN_InsertItemAfterParent(HWND hwnd,LPITEMIDLIST pidl);
158 static int FILEDLG95_LOOKIN_RemoveMostExpandedItem(HWND hwnd);
159 int FILEDLG95_LOOKIN_SelectItem(HWND hwnd,LPITEMIDLIST pidl);
160 static void FILEDLG95_LOOKIN_Clean(HWND hwnd);
162 /* Miscellaneous tool functions */
163 HRESULT GetName(LPSHELLFOLDER lpsf, LPITEMIDLIST pidl,DWORD dwFlags,LPSTR lpstrFileName);
164 HRESULT GetFileName(HWND hwnd, LPITEMIDLIST pidl, LPSTR lpstrFileName);
165 IShellFolder* GetShellFolderFromPidl(LPITEMIDLIST pidlAbs);
166 LPITEMIDLIST GetParentPidl(LPITEMIDLIST pidl);
167 LPITEMIDLIST GetPidlFromName(IShellFolder *psf,LPCSTR lpcstrFileName);
169 /* Shell memory allocation */
170 static void *MemAlloc(UINT size);
171 static void MemFree(void *mem);
173 BOOL WINAPI GetFileName95(FileOpenDlgInfos *fodInfos);
174 HRESULT WINAPI FileOpenDlgProc95(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
175 HRESULT SendCustomDlgNotificationMessage(HWND hwndParentDlg, UINT uCode);
176 HRESULT FILEDLG95_HandleCustomDialogMessages(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
177 BOOL FILEDLG95_OnOpenMultipleFiles(HWND hwnd, LPSTR lpstrFileList, UINT nFileCount, UINT sizeUsed);
178 static BOOL BrowseSelectedFolder(HWND hwnd);
180 extern LPSTR _strlwr( LPSTR str );
182 /***********************************************************************
183 * GetFileName95
185 * Creates an Open common dialog box that lets the user select
186 * the drive, directory, and the name of a file or set of files to open.
188 * IN : The FileOpenDlgInfos structure associated with the dialog
189 * OUT : TRUE on success
190 * FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
192 BOOL WINAPI GetFileName95(FileOpenDlgInfos *fodInfos)
195 LRESULT lRes;
196 LPCVOID template;
197 HRSRC hRes;
198 HANDLE hDlgTmpl = 0;
200 /* Create the dialog from a template */
202 if(!(hRes = FindResourceA(COMMDLG_hInstance32,MAKEINTRESOURCEA(NEWFILEOPENORD),RT_DIALOGA)))
204 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
205 return FALSE;
207 if (!(hDlgTmpl = LoadResource(COMMDLG_hInstance32, hRes )) ||
208 !(template = LockResource( hDlgTmpl )))
210 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
211 return FALSE;
213 lRes = DialogBoxIndirectParamA(COMMDLG_hInstance32,
214 (LPDLGTEMPLATEA) template,
215 fodInfos->ofnInfos->hwndOwner,
216 (DLGPROC) FileOpenDlgProc95,
217 (LPARAM) fodInfos);
219 /* Unable to create the dialog*/
220 if( lRes == -1)
221 return FALSE;
223 return lRes;
226 /***********************************************************************
227 * GetFileDialog95A
229 * Call GetFileName95 with this structure and clean the memory.
231 * IN : The OPENFILENAMEA initialisation structure passed to
232 * GetOpenFileNameA win api function (see filedlg.c)
234 BOOL WINAPI GetFileDialog95A(LPOPENFILENAMEA ofn,UINT iDlgType)
237 BOOL ret;
238 FileOpenDlgInfos *fodInfos;
239 HINSTANCE hInstance;
240 LPCSTR lpstrInitialDir = NULL;
241 DWORD dwFlags = 0;
243 /* Initialise FileOpenDlgInfos structure*/
244 fodInfos = (FileOpenDlgInfos*)MemAlloc(sizeof(FileOpenDlgInfos));
245 ZeroMemory(fodInfos, sizeof(FileOpenDlgInfos));
247 /* Pass in the original ofn */
248 fodInfos->ofnInfos = ofn;
250 /* Save original hInstance value */
251 hInstance = ofn->hInstance;
252 fodInfos->ofnInfos->hInstance = MapHModuleLS(ofn->hInstance);
254 dwFlags = ofn->Flags;
255 ofn->Flags = ofn->Flags|OFN_WINE;
257 /* Replace the NULL lpstrInitialDir by the current folder */
258 if(!ofn->lpstrInitialDir)
260 lpstrInitialDir = ofn->lpstrInitialDir;
261 ofn->lpstrInitialDir = MemAlloc(MAX_PATH);
262 GetCurrentDirectoryA(MAX_PATH,(LPSTR)ofn->lpstrInitialDir);
265 /* Initialise the dialog property */
266 fodInfos->DlgInfos.dwDlgProp = 0;
267 fodInfos->DlgInfos.hwndCustomDlg = (HWND)NULL;
269 switch(iDlgType)
271 case OPEN_DIALOG :
272 ret = GetFileName95(fodInfos);
273 break;
274 case SAVE_DIALOG :
275 fodInfos->DlgInfos.dwDlgProp |= FODPROP_SAVEDLG;
276 ret = GetFileName95(fodInfos);
277 break;
278 default :
279 ret = 0;
282 if (lpstrInitialDir)
284 MemFree((LPVOID)(ofn->lpstrInitialDir));
285 ofn->lpstrInitialDir = lpstrInitialDir;
288 ofn->Flags = dwFlags;
289 ofn->hInstance = hInstance;
290 MemFree((LPVOID)(fodInfos));
291 return ret;
294 /***********************************************************************
295 * GetFileDialog95W
297 * Copy the OPENFILENAMEW structure in a FileOpenDlgInfos structure.
298 * Call GetFileName95 with this structure and clean the memory.
300 * IN : The OPENFILENAMEW initialisation structure passed to
301 * GetOpenFileNameW win api function (see filedlg.c)
303 * FIXME:
304 * some more strings are needing to be convertet AtoW
306 BOOL WINAPI GetFileDialog95W(LPOPENFILENAMEW ofn,UINT iDlgType)
308 BOOL ret;
309 FileOpenDlgInfos *fodInfos;
310 HINSTANCE hInstance;
311 LPCSTR lpstrFilter = NULL;
312 LPSTR lpstrCustomFilter = NULL;
313 LPWSTR lpstrFile = NULL;
314 DWORD dwFlags;
316 /* Initialise FileOpenDlgInfos structure*/
317 fodInfos = (FileOpenDlgInfos*)MemAlloc(sizeof(FileOpenDlgInfos));
318 ZeroMemory(fodInfos, sizeof(FileOpenDlgInfos));
320 /* Pass in the original ofn */
321 fodInfos->ofnInfos = (LPOPENFILENAMEA) ofn;
323 /* Save hInstance */
324 hInstance = fodInfos->ofnInfos->hInstance;
325 fodInfos->ofnInfos->hInstance = MapHModuleLS(ofn->hInstance);
327 /* Save lpstrFilter */
328 if (ofn->lpstrFilter)
330 LPWSTR s;
331 LPSTR x,y;
332 int n;
334 lpstrFilter = fodInfos->ofnInfos->lpstrFilter;
336 /* filter is a list... title\0ext\0......\0\0 */
337 s = (LPWSTR)ofn->lpstrFilter;
339 while (*s)
340 s = s+lstrlenW(s)+1;
341 s++;
342 n = s - ofn->lpstrFilter; /* already divides by 2. ptr magic */
343 x = y = (LPSTR)MemAlloc(n);
344 s = (LPWSTR)ofn->lpstrFilter;
345 while (*s) {
346 lstrcpyWtoA(x,s);
347 x+=lstrlenA(x)+1;
348 s+=lstrlenW(s)+1;
350 *x=0;
351 fodInfos->ofnInfos->lpstrFilter = (LPSTR)y;
353 /* Save lpstrCustomFilter */
354 if (ofn->lpstrCustomFilter)
356 LPWSTR s;
357 LPSTR x,y;
358 int n;
360 lpstrCustomFilter = fodInfos->ofnInfos->lpstrCustomFilter;
361 /* filter is a list... title\0ext\0......\0\0 */
362 s = (LPWSTR)ofn->lpstrCustomFilter;
363 while (*s)
364 s = s+lstrlenW(s)+1;
365 s++;
366 n = s - ofn->lpstrCustomFilter;
367 x = y = (LPSTR)MemAlloc(n);
368 s = (LPWSTR)ofn->lpstrCustomFilter;
369 while (*s) {
370 lstrcpyWtoA(x,s);
371 x+=lstrlenA(x)+1;
372 s+=lstrlenW(s)+1;
374 *x=0;
375 fodInfos->ofnInfos->lpstrCustomFilter = (LPSTR)y;
378 /* Save Flags */
379 dwFlags = fodInfos->ofnInfos->Flags;
380 fodInfos->ofnInfos->Flags = ofn->Flags|OFN_WINE|OFN_UNICODE;
382 /* Initialise the dialog property */
383 fodInfos->DlgInfos.dwDlgProp = 0;
385 /* allocate ansi filename buffer */
386 lpstrFile = ofn->lpstrFile;
387 ofn->lpstrFile = MemAlloc(ofn->nMaxFile);
389 switch(iDlgType)
391 case OPEN_DIALOG :
392 ret = GetFileName95(fodInfos);
393 break;
394 case SAVE_DIALOG :
395 fodInfos->DlgInfos.dwDlgProp |= FODPROP_SAVEDLG;
396 ret = GetFileName95(fodInfos);
397 break;
398 default :
399 ret = 0;
402 /* Cleaning */
403 /* Restore Flags */
404 fodInfos->ofnInfos->Flags = dwFlags;
406 /* Restore lpstrFilter */
407 if (fodInfos->ofnInfos->lpstrFilter)
409 MemFree((LPVOID)(fodInfos->ofnInfos->lpstrFilter));
410 fodInfos->ofnInfos->lpstrFilter = lpstrFilter;
412 if (fodInfos->ofnInfos->lpstrCustomFilter)
414 MemFree((LPVOID)(fodInfos->ofnInfos->lpstrCustomFilter));
415 fodInfos->ofnInfos->lpstrCustomFilter = lpstrCustomFilter;
418 /* Restore hInstance */
419 fodInfos->ofnInfos->hInstance = hInstance;
420 MemFree((LPVOID)(fodInfos));
422 /* filename */
423 lstrcpynAtoW(lpstrFile, (LPCSTR)ofn->lpstrFile, ofn->nMaxFile);
424 MemFree(ofn->lpstrFile);
425 ofn->lpstrFile = lpstrFile;
427 return ret;
430 void ArrangeCtrlPositions( HWND hwndChildDlg, HWND hwndParentDlg)
433 HWND hwndChild,hwndStc32;
434 RECT rectParent, rectChild, rectCtrl, rectStc32, rectTemp;
435 POINT ptMoveCtl;
436 POINT ptParentClient;
438 ptMoveCtl.x = ptMoveCtl.y = 0;
439 hwndStc32=GetDlgItem(hwndChildDlg,stc32);
440 GetClientRect(hwndParentDlg,&rectParent);
441 GetClientRect(hwndChildDlg,&rectChild);
442 if(hwndStc32)
444 GetWindowRect(hwndStc32,&rectStc32);
445 MapWindowPoints(0, hwndChildDlg,(LPPOINT)&rectStc32,2);
446 CopyRect(&rectTemp,&rectStc32);
448 SetRect(&rectStc32,rectStc32.left,rectStc32.top,rectStc32.left + (rectParent.right-rectParent.left),rectStc32.top+(rectParent.bottom-rectParent.top));
449 SetWindowPos(hwndStc32,0,rectStc32.left,rectStc32.top,rectStc32.right-rectStc32.left,rectStc32.bottom-rectStc32.top,SWP_NOMOVE|SWP_NOZORDER | SWP_NOACTIVATE);
451 if(rectStc32.right < rectTemp.right)
453 ptParentClient.x = max((rectParent.right-rectParent.left),(rectChild.right-rectChild.left));
454 ptMoveCtl.x = 0;
456 else
458 ptMoveCtl.x = (rectStc32.right - rectTemp.right);
459 ptParentClient.x = max((rectParent.right-rectParent.left),((rectChild.right-rectChild.left)+rectStc32.right-rectTemp.right));
461 if(rectStc32.bottom < rectTemp.bottom)
463 ptParentClient.y = max((rectParent.bottom-rectParent.top),(rectChild.bottom-rectChild.top));
464 ptMoveCtl.y = 0;
466 else
468 ptMoveCtl.y = (rectStc32.bottom - rectTemp.bottom);
469 ptParentClient.y = max((rectParent.bottom-rectParent.top),((rectChild.bottom-rectChild.top)+rectStc32.bottom-rectTemp.bottom));
472 else
474 if( (GetWindow(hwndChildDlg,GW_CHILD)) == (HWND) NULL)
475 return;
476 SetRectEmpty(&rectTemp);
477 ptParentClient.x = max((rectParent.right-rectParent.left),(rectChild.right-rectChild.left));
478 ptParentClient.y = (rectParent.bottom-rectParent.top) + (rectChild.bottom-rectChild.top);
479 ptMoveCtl.y = rectParent.bottom-rectParent.top;
480 ptMoveCtl.x=0;
482 SetRect(&rectParent,rectParent.left,rectParent.top,rectParent.left+ptParentClient.x,rectParent.top+ptParentClient.y);
483 AdjustWindowRectEx( &rectParent,GetWindowLongA(hwndParentDlg,GWL_STYLE),FALSE,GetWindowLongA(hwndParentDlg,GWL_EXSTYLE));
485 SetWindowPos(hwndChildDlg, 0, 0,0, ptParentClient.x,ptParentClient.y,
486 SWP_NOZORDER );
487 SetWindowPos(hwndParentDlg, 0, rectParent.left,rectParent.top, (rectParent.right- rectParent.left),
488 (rectParent.bottom-rectParent.top),SWP_NOMOVE | SWP_NOZORDER);
490 hwndChild = GetWindow(hwndChildDlg,GW_CHILD);
491 if(hwndStc32)
493 GetWindowRect(hwndStc32,&rectStc32);
494 MapWindowPoints( 0, hwndChildDlg,(LPPOINT)&rectStc32,2);
496 else
497 SetRect(&rectStc32,0,0,0,0);
499 if (hwndChild )
503 if(hwndChild != hwndStc32)
505 if (GetWindowLongA( hwndChild, GWL_STYLE ) & WS_MAXIMIZE)
506 continue;
507 GetWindowRect(hwndChild,&rectCtrl);
508 MapWindowPoints( 0, hwndParentDlg,(LPPOINT)&rectCtrl,2);
511 Check the initial position of the controls relative to the initial
512 position and size of stc32 (before it is expanded).
514 if (rectCtrl.left >= rectTemp.right && rectCtrl.top >= rectTemp.bottom)
516 rectCtrl.left += ptMoveCtl.x;
517 rectCtrl.top += ptMoveCtl.y;
519 else if (rectCtrl.left >= rectTemp.right)
520 rectCtrl.left += ptMoveCtl.x;
521 else if (rectCtrl.top >= rectTemp.bottom)
522 rectCtrl.top += ptMoveCtl.y;
524 SetWindowPos( hwndChild, 0, rectCtrl.left, rectCtrl.top,
525 rectCtrl.right-rectCtrl.left,rectCtrl.bottom-rectCtrl.top,
526 SWP_NOSIZE | SWP_NOZORDER );
529 while ((hwndChild=GetWindow( hwndChild, GW_HWNDNEXT )) != (HWND)NULL);
531 hwndChild = GetWindow(hwndParentDlg,GW_CHILD);
533 if(hwndStc32)
535 GetWindowRect(hwndStc32,&rectStc32);
536 MapWindowPoints( 0, hwndChildDlg,(LPPOINT)&rectStc32,2);
537 ptMoveCtl.x = rectStc32.left - 0;
538 ptMoveCtl.y = rectStc32.top - 0;
539 if (hwndChild )
543 if(hwndChild != hwndChildDlg)
546 if (GetWindowLongA( hwndChild, GWL_STYLE ) & WS_MAXIMIZE)
547 continue;
548 GetWindowRect(hwndChild,&rectCtrl);
549 MapWindowPoints( 0, hwndParentDlg,(LPPOINT)&rectCtrl,2);
551 rectCtrl.left += ptMoveCtl.x;
552 rectCtrl.top += ptMoveCtl.y;
554 SetWindowPos( hwndChild, 0, rectCtrl.left, rectCtrl.top,
555 rectCtrl.right-rectCtrl.left,rectCtrl.bottom-rectCtrl.top,
556 SWP_NOSIZE |SWP_NOZORDER );
559 while ((hwndChild=GetWindow( hwndChild, GW_HWNDNEXT )) != (HWND)NULL);
566 HRESULT WINAPI FileOpenDlgProcUserTemplate(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
568 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(GetParent(hwnd),FileOpenDlgInfosStr);
569 switch(uMsg)
571 case WM_INITDIALOG:
573 fodInfos = (FileOpenDlgInfos *)lParam;
574 lParam = (LPARAM) fodInfos->ofnInfos;
575 ArrangeCtrlPositions(hwnd,GetParent(hwnd));
576 if(fodInfos && (fodInfos->ofnInfos->Flags & OFN_ENABLEHOOK) && fodInfos->ofnInfos->lpfnHook)
577 return CallWindowProcA((WNDPROC)fodInfos->ofnInfos->lpfnHook,hwnd,uMsg,wParam,lParam);
578 return 0;
581 if(fodInfos && (fodInfos->ofnInfos->Flags & OFN_ENABLEHOOK) && fodInfos->ofnInfos->lpfnHook )
582 return CallWindowProcA((WNDPROC)fodInfos->ofnInfos->lpfnHook,hwnd,uMsg,wParam,lParam);
583 return DefWindowProcA(hwnd,uMsg,wParam,lParam);
586 HWND CreateTemplateDialog(FileOpenDlgInfos *fodInfos,HWND hwnd)
588 LPCVOID template;
589 HRSRC hRes;
590 HANDLE hDlgTmpl = 0;
591 HWND hChildDlg = 0;
592 if (fodInfos->ofnInfos->Flags & OFN_ENABLETEMPLATE || fodInfos->ofnInfos->Flags & OFN_ENABLETEMPLATEHANDLE)
594 if (fodInfos->ofnInfos->Flags & OFN_ENABLETEMPLATEHANDLE)
596 if( !(template = LockResource( fodInfos->ofnInfos->hInstance)))
598 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
599 return (HWND)NULL;
603 else
605 if (!(hRes = FindResourceA(MapHModuleSL(fodInfos->ofnInfos->hInstance),
606 (fodInfos->ofnInfos->lpTemplateName), RT_DIALOGA)))
608 COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
609 return (HWND)NULL;
611 if (!(hDlgTmpl = LoadResource( MapHModuleSL(fodInfos->ofnInfos->hInstance),
612 hRes )) ||
613 !(template = LockResource( hDlgTmpl )))
615 COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
616 return (HWND)NULL;
620 hChildDlg= CreateDialogIndirectParamA(fodInfos->ofnInfos->hInstance,template,hwnd,(DLGPROC)FileOpenDlgProcUserTemplate,(LPARAM)fodInfos);
621 if(hChildDlg)
623 ShowWindow(hChildDlg,SW_SHOW);
624 return hChildDlg;
627 else if(fodInfos->ofnInfos->Flags & OFN_ENABLEHOOK && fodInfos->ofnInfos->lpfnHook)
629 RECT rectHwnd;
630 DLGTEMPLATE tmplate;
631 GetClientRect(hwnd,&rectHwnd);
632 tmplate.style = WS_CHILD | WS_CLIPSIBLINGS;
633 tmplate.dwExtendedStyle = 0;
634 tmplate.cdit = 0;
635 tmplate.x = 0;
636 tmplate.y = 0;
637 tmplate.cx = rectHwnd.right-rectHwnd.left;
638 tmplate.cy = rectHwnd.bottom-rectHwnd.top;
640 return CreateDialogIndirectParamA(fodInfos->ofnInfos->hInstance,&tmplate,hwnd,(DLGPROC)FileOpenDlgProcUserTemplate,(LPARAM)fodInfos);
642 return (HWND)NULL;
645 /***********************************************************************
646 * SendCustomDlgNotificationMessage
648 * Send CustomDialogNotification (CDN_FIRST -- CDN_LAST) message to the custom template dialog
651 HRESULT SendCustomDlgNotificationMessage(HWND hwndParentDlg, UINT uCode)
653 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwndParentDlg,FileOpenDlgInfosStr);
654 if(!fodInfos)
655 return 0;
656 if(fodInfos->DlgInfos.hwndCustomDlg)
658 OFNOTIFYA ofnNotify;
659 ofnNotify.hdr.hwndFrom=hwndParentDlg;
660 ofnNotify.hdr.idFrom=0;
661 ofnNotify.hdr.code = uCode;
662 ofnNotify.lpOFN = fodInfos->ofnInfos;
663 return SendMessageA(fodInfos->DlgInfos.hwndCustomDlg,WM_NOTIFY,0,(LPARAM)&ofnNotify);
665 return TRUE;
668 /***********************************************************************
669 * FILEDLG95_HandleCustomDialogMessages
671 * Handle Custom Dialog Messages (CDM_FIRST -- CDM_LAST) messages
673 HRESULT FILEDLG95_HandleCustomDialogMessages(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
675 LPSTR lpstrFileSpec;
676 int reqSize;
677 char lpstrPath[MAX_PATH];
678 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
679 if(!fodInfos) return -1;
681 switch(uMsg)
683 case CDM_GETFILEPATH:
684 GetDlgItemTextA(hwnd,IDC_FILENAME,lpstrPath, sizeof(lpstrPath));
685 lpstrFileSpec = (LPSTR)COMDLG32_PathFindFileNameA(lpstrPath);
686 if (lpstrFileSpec==lpstrPath)
688 char lpstrCurrentDir[MAX_PATH];
689 /* Prepend the current path */
690 COMDLG32_SHGetPathFromIDListA(fodInfos->ShellInfos.pidlAbsCurrent,lpstrCurrentDir);
691 if ((LPSTR)lParam!=NULL)
692 wsnprintfA((LPSTR)lParam,(int)wParam,"%s\\%s",lpstrCurrentDir,lpstrPath);
693 reqSize=strlen(lpstrCurrentDir)+1+strlen(lpstrPath)+1;
695 else
697 lstrcpynA((LPSTR)lParam,(LPSTR)lpstrPath,(int)wParam);
698 reqSize=strlen(lpstrPath);
700 /* return the required buffer size */
701 return reqSize;
703 case CDM_GETFOLDERPATH:
704 COMDLG32_SHGetPathFromIDListA(fodInfos->ShellInfos.pidlAbsCurrent,lpstrPath);
705 if ((LPSTR)lParam!=NULL)
706 lstrcpynA((LPSTR)lParam,lpstrPath,(int)wParam);
707 return strlen(lpstrPath);
709 case CDM_GETSPEC:
710 reqSize=GetDlgItemTextA(hwnd,IDC_FILENAME,lpstrPath, sizeof(lpstrPath));
711 lpstrFileSpec = (LPSTR)COMDLG32_PathFindFileNameA(lpstrPath);
712 if ((LPSTR)lParam!=NULL)
713 lstrcpynA((LPSTR)lParam, lpstrFileSpec, (int)wParam);
714 return strlen(lpstrFileSpec);
716 case CDM_SETCONTROLTEXT:
717 if ( 0 != lParam )
718 SetDlgItemTextA( hwnd, (UINT) wParam, (LPSTR) lParam );
719 return TRUE;
721 case CDM_HIDECONTROL:
722 case CDM_SETDEFEXT:
723 FIXME("CDM_HIDECONTROL,CDM_SETCONTROLTEXT,CDM_SETDEFEXT not implemented\n");
724 return -1;
726 return TRUE;
729 /***********************************************************************
730 * FileOpenDlgProc95
732 * File open dialog procedure
734 HRESULT WINAPI FileOpenDlgProc95(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
736 switch(uMsg)
738 case WM_INITDIALOG :
739 /* Adds the FileOpenDlgInfos in the property list of the dialog
740 so it will be easily accessible through a GetPropA(...) */
741 SetPropA(hwnd, FileOpenDlgInfosStr, (HANDLE) lParam);
743 FILEDLG95_OnWMInitDialog(hwnd, wParam, lParam);
744 ((FileOpenDlgInfos *)lParam)->DlgInfos.hwndCustomDlg =
745 CreateTemplateDialog((FileOpenDlgInfos *)lParam,hwnd);
746 SendCustomDlgNotificationMessage(hwnd,CDN_INITDONE);
747 return 0;
748 case WM_COMMAND:
749 return FILEDLG95_OnWMCommand(hwnd, wParam, lParam);
750 case WM_DRAWITEM:
752 switch(((LPDRAWITEMSTRUCT)lParam)->CtlID)
754 case IDC_LOOKIN:
755 FILEDLG95_LOOKIN_DrawItem((LPDRAWITEMSTRUCT) lParam);
756 return TRUE;
759 return FALSE;
761 case WM_GETISHELLBROWSER:
762 return FILEDLG95_OnWMGetIShellBrowser(hwnd);
764 case WM_DESTROY:
765 RemovePropA(hwnd, FileOpenDlgInfosStr);
766 return FALSE;
768 case WM_NOTIFY:
770 LPNMHDR lpnmh = (LPNMHDR)lParam;
771 UINT stringId = -1;
773 /* set up the button tooltips strings */
774 if(TTN_GETDISPINFOA == lpnmh->code )
776 LPNMTTDISPINFOA lpdi = (LPNMTTDISPINFOA)lParam;
777 switch(lpnmh->idFrom )
779 /* Up folder button */
780 case FCIDM_TB_UPFOLDER:
781 stringId = IDS_UPFOLDER;
782 break;
783 /* New folder button */
784 case FCIDM_TB_NEWFOLDER:
785 stringId = IDS_NEWFOLDER;
786 break;
787 /* List option button */
788 case FCIDM_TB_SMALLICON:
789 stringId = IDS_LISTVIEW;
790 break;
791 /* Details option button */
792 case FCIDM_TB_REPORTVIEW:
793 stringId = IDS_REPORTVIEW;
794 break;
795 /* Desktop button */
796 case FCIDM_TB_DESKTOP:
797 stringId = IDS_TODESKTOP;
798 break;
799 default:
800 stringId = 0;
802 lpdi->hinst = COMMDLG_hInstance32;
803 lpdi->lpszText = (LPSTR) stringId;
805 return FALSE;
807 default :
808 if(uMsg >= CDM_FIRST && uMsg <= CDM_LAST)
809 return FILEDLG95_HandleCustomDialogMessages(hwnd, uMsg, wParam, lParam);
810 return FALSE;
814 /***********************************************************************
815 * FILEDLG95_OnWMInitDialog
817 * WM_INITDIALOG message handler
819 static LRESULT FILEDLG95_OnWMInitDialog(HWND hwnd, WPARAM wParam, LPARAM lParam)
821 LPITEMIDLIST pidlItemId;
822 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) lParam;
824 TRACE("dir=%s file=%s\n",
825 fodInfos->ofnInfos->lpstrInitialDir, fodInfos->ofnInfos->lpstrFile);
827 /* Get the initial directory pidl */
829 if(!(pidlItemId = GetPidlFromName(fodInfos->Shell.FOIShellFolder,fodInfos->ofnInfos->lpstrInitialDir)))
831 char path[MAX_PATH];
833 GetCurrentDirectoryA(MAX_PATH,path);
834 pidlItemId = GetPidlFromName(fodInfos->Shell.FOIShellFolder, path);
838 /* Initialise shell objects */
839 FILEDLG95_SHELL_Init(hwnd);
841 /* Initialise dialog UI */
842 FILEDLG95_InitUI(hwnd);
844 /* Initialize the Look In combo box*/
845 FILEDLG95_LOOKIN_Init(fodInfos->DlgInfos.hwndLookInCB);
847 /* Initialize the filter combo box */
848 FILEDLG95_FILETYPE_Init(hwnd);
850 /* Browse to the initial directory */
851 IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser,pidlItemId, SBSP_ABSOLUTE);
853 /* Free pidlItem memory */
854 COMDLG32_SHFree(pidlItemId);
856 return TRUE;
858 /***********************************************************************
859 * FILEDLG95_Clean
861 * Regroups all the cleaning functions of the filedlg
863 void FILEDLG95_Clean(HWND hwnd)
865 FILEDLG95_FILETYPE_Clean(hwnd);
866 FILEDLG95_LOOKIN_Clean(hwnd);
867 FILEDLG95_SHELL_Clean(hwnd);
869 /***********************************************************************
870 * FILEDLG95_OnWMCommand
872 * WM_COMMAND message handler
874 static LRESULT FILEDLG95_OnWMCommand(HWND hwnd, WPARAM wParam, LPARAM lParam)
876 WORD wNotifyCode = HIWORD(wParam); /* notification code */
877 WORD wID = LOWORD(wParam); /* item, control, or accelerator identifier */
878 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
880 switch(wID)
882 /* OK button */
883 case IDOK:
884 if(FILEDLG95_OnOpen(hwnd))
885 SendCustomDlgNotificationMessage(hwnd,CDN_FILEOK);
886 break;
887 /* Cancel button */
888 case IDCANCEL:
889 FILEDLG95_Clean(hwnd);
890 EndDialog(hwnd, FALSE);
891 break;
892 /* Filetype combo box */
893 case IDC_FILETYPE:
894 FILEDLG95_FILETYPE_OnCommand(hwnd,wNotifyCode);
895 break;
896 /* LookIn combo box */
897 case IDC_LOOKIN:
898 FILEDLG95_LOOKIN_OnCommand(hwnd,wNotifyCode);
899 break;
901 /* --- toolbar --- */
902 /* Up folder button */
903 case FCIDM_TB_UPFOLDER:
904 FILEDLG95_SHELL_UpFolder(hwnd);
905 break;
906 /* New folder button */
907 case FCIDM_TB_NEWFOLDER:
908 FILEDLG95_SHELL_ExecuteCommand(hwnd,CMDSTR_NEWFOLDER);
909 break;
910 /* List option button */
911 case FCIDM_TB_SMALLICON:
912 FILEDLG95_SHELL_ExecuteCommand(hwnd,CMDSTR_VIEWLIST);
913 break;
914 /* Details option button */
915 case FCIDM_TB_REPORTVIEW:
916 FILEDLG95_SHELL_ExecuteCommand(hwnd,CMDSTR_VIEWDETAILS);
917 break;
918 /* Details option button */
919 case FCIDM_TB_DESKTOP:
920 FILEDLG95_SHELL_BrowseToDesktop(hwnd);
921 break;
923 case IDC_FILENAME:
924 break;
927 /* Do not use the listview selection anymore */
928 fodInfos->DlgInfos.dwDlgProp &= ~FODPROP_USEVIEW;
929 return 0;
932 /***********************************************************************
933 * FILEDLG95_OnWMGetIShellBrowser
935 * WM_GETISHELLBROWSER message handler
937 static LRESULT FILEDLG95_OnWMGetIShellBrowser(HWND hwnd)
940 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
942 TRACE("\n");
944 SetWindowLongA(hwnd,DWL_MSGRESULT,(LONG)fodInfos->Shell.FOIShellBrowser);
946 return TRUE;
950 /***********************************************************************
951 * FILEDLG95_InitUI
954 static LRESULT FILEDLG95_InitUI(HWND hwnd)
956 TBBUTTON tbb[] =
958 {0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, {0, 0}, 0, 0 },
959 {VIEW_PARENTFOLDER, FCIDM_TB_UPFOLDER, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
960 {0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, {0, 0}, 0, 0 },
961 {VIEW_NEWFOLDER+1, FCIDM_TB_DESKTOP, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
962 {0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, {0, 0}, 0, 0 },
963 {VIEW_NEWFOLDER, FCIDM_TB_NEWFOLDER, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
964 {0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, {0, 0}, 0, 0 },
965 {VIEW_LIST, FCIDM_TB_SMALLICON, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
966 {VIEW_DETAILS, FCIDM_TB_REPORTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
968 TBADDBITMAP tba[] =
970 { HINST_COMMCTRL, IDB_VIEW_SMALL_COLOR },
971 { COMDLG32_hInstance, 800 } // desktop icon
974 RECT rectTB;
976 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
978 TRACE("%p\n", fodInfos);
980 /* Get the hwnd of the controls */
981 fodInfos->DlgInfos.hwndFileName = GetDlgItem(hwnd,IDC_FILENAME);
982 fodInfos->DlgInfos.hwndFileTypeCB = GetDlgItem(hwnd,IDC_FILETYPE);
983 fodInfos->DlgInfos.hwndLookInCB = GetDlgItem(hwnd,IDC_LOOKIN);
985 /* construct the toolbar */
986 GetWindowRect(GetDlgItem(hwnd,IDC_TOOLBARSTATIC),&rectTB);
987 MapWindowPoints( 0, hwnd,(LPPOINT)&rectTB,2);
989 fodInfos->DlgInfos.hwndTB = CreateWindowExA(0, TOOLBARCLASSNAMEA, (LPSTR) NULL,
990 WS_CHILD | WS_GROUP | TBSTYLE_TOOLTIPS | CCS_NODIVIDER | CCS_NORESIZE,
991 0, 0, 150, 26,
992 hwnd, (HMENU) IDC_TOOLBAR, COMMDLG_hInstance32, NULL);
994 SetWindowPos(fodInfos->DlgInfos.hwndTB, 0,
995 rectTB.left,rectTB.top, rectTB.right-rectTB.left, rectTB.bottom-rectTB.top,
996 SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_NOZORDER );
998 SendMessageA(fodInfos->DlgInfos.hwndTB, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);
1000 /* fixme: use TB_LOADIMAGES when implemented */
1001 /* SendMessageA(fodInfos->DlgInfos.hwndTB, TB_LOADIMAGES, (WPARAM) IDB_VIEW_SMALL_COLOR, HINST_COMMCTRL);*/
1002 SendMessageA(fodInfos->DlgInfos.hwndTB, TB_ADDBITMAP, (WPARAM) 12, (LPARAM) &tba[0]);
1003 SendMessageA(fodInfos->DlgInfos.hwndTB, TB_ADDBITMAP, (WPARAM) 1, (LPARAM) &tba[1]);
1005 SendMessageA(fodInfos->DlgInfos.hwndTB, TB_ADDBUTTONSA, (WPARAM) 9,(LPARAM) &tbb);
1006 SendMessageA(fodInfos->DlgInfos.hwndTB, TB_AUTOSIZE, 0, 0);
1008 /* Set the window text with the text specified in the OPENFILENAME structure */
1009 if(fodInfos->ofnInfos->lpstrTitle)
1011 SetWindowTextA(hwnd,fodInfos->ofnInfos->lpstrTitle);
1013 else if (fodInfos->DlgInfos.dwDlgProp & FODPROP_SAVEDLG)
1015 SetWindowTextA(hwnd,"Save");
1018 /* Initialise the file name edit control */
1019 if(fodInfos->ofnInfos->lpstrFile)
1021 LPSTR lpstrFile = COMDLG32_PathFindFileNameA(fodInfos->ofnInfos->lpstrFile);
1022 SetDlgItemTextA(hwnd, IDC_FILENAME, lpstrFile);
1025 /* Must the open as read only check box be checked ?*/
1026 if(fodInfos->ofnInfos->Flags & OFN_READONLY)
1028 SendDlgItemMessageA(hwnd,IDC_OPENREADONLY,BM_SETCHECK,(WPARAM)TRUE,0);
1031 /* Must the open as read only check box be hid ?*/
1032 if(fodInfos->ofnInfos->Flags & OFN_HIDEREADONLY)
1034 ShowWindow(GetDlgItem(hwnd,IDC_OPENREADONLY),SW_HIDE);
1037 /* Must the help button be hid ?*/
1038 if (!(fodInfos->ofnInfos->Flags & OFN_SHOWHELP))
1040 ShowWindow(GetDlgItem(hwnd, pshHelp), SW_HIDE);
1043 /* Resize the height, if open as read only checkbox ad help button
1044 are hidden */
1045 if ( (fodInfos->ofnInfos->Flags & OFN_HIDEREADONLY) &&
1046 (!(fodInfos->ofnInfos->Flags & OFN_SHOWHELP)) )
1048 RECT rectDlg, rectHelp, rectCancel;
1049 GetWindowRect(hwnd, &rectDlg);
1050 GetWindowRect(GetDlgItem(hwnd, pshHelp), &rectHelp);
1051 GetWindowRect(GetDlgItem(hwnd, IDCANCEL), &rectCancel);
1052 /* subtract the height of the help button plus the space between
1053 the help button and the cancel button to the height of the dialog */
1054 SetWindowPos(hwnd, 0, 0, 0, rectDlg.right-rectDlg.left,
1055 (rectDlg.bottom-rectDlg.top) - (rectHelp.bottom - rectCancel.bottom),
1056 SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOZORDER);
1059 /* change Open to Save FIXME: use resources */
1060 if (fodInfos->DlgInfos.dwDlgProp & FODPROP_SAVEDLG)
1062 SetDlgItemTextA(hwnd,IDOK,"&Save");
1063 SetDlgItemTextA(hwnd,IDC_LOOKINSTATIC,"Save &in");
1065 return 0;
1068 /***********************************************************************
1069 * FILEDLG95_OnOpenMultipleFiles
1071 * Handles the opening of multiple files.
1073 * FIXME
1074 * check destination buffer size
1076 BOOL FILEDLG95_OnOpenMultipleFiles(HWND hwnd, LPSTR lpstrFileList, UINT nFileCount, UINT sizeUsed)
1078 CHAR lpstrPathSpec[MAX_PATH] = "";
1079 LPSTR lpstrFile;
1080 UINT nCount, nSizePath;
1081 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1083 TRACE("\n");
1085 lpstrFile = fodInfos->ofnInfos->lpstrFile;
1086 lpstrFile[0] = '\0';
1088 COMDLG32_SHGetPathFromIDListA( fodInfos->ShellInfos.pidlAbsCurrent, lpstrPathSpec );
1090 if ( !(fodInfos->ofnInfos->Flags & OFN_NOVALIDATE) &&
1091 ( fodInfos->ofnInfos->Flags & OFN_FILEMUSTEXIST))
1093 LPSTR lpstrTemp = lpstrFileList;
1095 for ( nCount = 0; nCount < nFileCount; nCount++ )
1097 LPITEMIDLIST pidl;
1099 pidl = GetPidlFromName(fodInfos->Shell.FOIShellFolder, lpstrTemp);
1100 if (!pidl)
1102 CHAR lpstrNotFound[100];
1103 CHAR lpstrMsg[100];
1104 CHAR tmp[400];
1106 LoadStringA(COMMDLG_hInstance32, IDS_FILENOTFOUND, lpstrNotFound, 100);
1107 LoadStringA(COMMDLG_hInstance32, IDS_VERIFYFILE, lpstrMsg, 100);
1109 strcpy(tmp, lpstrTemp);
1110 strcat(tmp, "\n");
1111 strcat(tmp, lpstrNotFound);
1112 strcat(tmp, "\n");
1113 strcat(tmp, lpstrMsg);
1115 MessageBoxA(hwnd, tmp, fodInfos->ofnInfos->lpstrTitle, MB_OK | MB_ICONEXCLAMATION);
1116 return FALSE;
1119 lpstrTemp += strlen(lpstrFileList) + 1;
1120 COMDLG32_SHFree(pidl);
1124 nSizePath = lstrlenA(lpstrPathSpec);
1125 lstrcpyA( lpstrFile, lpstrPathSpec);
1126 memcpy( lpstrFile + nSizePath + 1, lpstrFileList, sizeUsed );
1128 fodInfos->ofnInfos->nFileOffset = nSizePath + 1;
1129 fodInfos->ofnInfos->nFileExtension = 0;
1131 /* clean and exit */
1132 FILEDLG95_Clean(hwnd);
1133 return EndDialog(hwnd,TRUE);
1136 /***********************************************************************
1137 * FILEDLG95_OnOpen
1139 * Ok button WM_COMMAND message handler
1141 * If the function succeeds, the return value is nonzero.
1143 #define ONOPEN_BROWSE 1
1144 #define ONOPEN_OPEN 2
1145 #define ONOPEN_SEARCH 3
1146 static void FILEDLG95_OnOpenMessage(HWND hwnd, int idCaption, int idText)
1148 char strMsgTitle[MAX_PATH];
1149 char strMsgText [MAX_PATH];
1150 if (idCaption)
1151 LoadStringA(COMDLG32_hInstance, idCaption, strMsgTitle, sizeof(strMsgTitle));
1152 else
1153 strMsgTitle[0] = '\0';
1154 LoadStringA(COMDLG32_hInstance, idText, strMsgText, sizeof(strMsgText));
1155 MessageBoxA(hwnd,strMsgText, strMsgTitle, MB_OK | MB_ICONHAND);
1158 BOOL FILEDLG95_OnOpen(HWND hwnd)
1160 char * lpstrFileList;
1161 UINT nFileCount = 0;
1162 UINT sizeUsed = 0;
1163 BOOL ret = TRUE;
1164 char lpstrPathAndFile[MAX_PATH];
1165 char lpstrTemp[MAX_PATH];
1166 LPSHELLFOLDER lpsf = NULL;
1167 int nOpenAction;
1168 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1171 TRACE("hwnd=0x%04x\n", hwnd);
1173 /* get the files from the edit control */
1174 nFileCount = FILEDLG95_FILENAME_GetFileNames(hwnd, &lpstrFileList, &sizeUsed);
1176 /* try if the user selected a folder in the shellview */
1177 if(nFileCount == 0)
1179 if (BrowseSelectedFolder(hwnd))
1180 return FALSE;
1183 if(nFileCount > 1)
1185 ret = FILEDLG95_OnOpenMultipleFiles(hwnd, lpstrFileList, nFileCount, sizeUsed);
1186 goto ret;
1189 TRACE("count=%u len=%u file=%s\n", nFileCount, sizeUsed, lpstrFileList);
1192 Step 1: Build a complete path name from the current folder and
1193 the filename or path in the edit box.
1194 Special cases:
1195 - the path in the edit box is a root path
1196 (with or without drive letter)
1197 - the edit box contains ".." (or a path with ".." in it)
1200 /* Get the current directory name */
1201 if (!COMDLG32_SHGetPathFromIDListA(fodInfos->ShellInfos.pidlAbsCurrent, lpstrPathAndFile))
1203 /* we are in a special folder, default to desktop */
1204 if(FAILED(COMDLG32_SHGetFolderPathA(hwnd, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL, 0, lpstrPathAndFile)))
1206 /* last fallback */
1207 GetCurrentDirectoryA(MAX_PATH, lpstrPathAndFile);
1210 COMDLG32_PathAddBackslashA(lpstrPathAndFile);
1212 TRACE("current directory=%s\n", lpstrPathAndFile);
1214 /* if the user specifyed a fully qualified path use it */
1215 if(COMDLG32_PathIsRelativeA(lpstrFileList))
1217 strcat(lpstrPathAndFile, lpstrFileList);
1219 else
1221 /* does the path have a drive letter? */
1222 if (COMDLG32_PathGetDriveNumberA(lpstrFileList) == -1)
1223 strcpy(lpstrPathAndFile+2, lpstrFileList);
1224 else
1225 strcpy(lpstrPathAndFile, lpstrFileList);
1228 /* resolve "." and ".." */
1229 COMDLG32_PathCanonicalizeA(lpstrTemp, lpstrPathAndFile );
1230 strcpy(lpstrPathAndFile, lpstrTemp);
1231 TRACE("canon=%s\n", lpstrPathAndFile);
1233 MemFree(lpstrFileList);
1236 Step 2: here we have a cleaned up path
1238 We have to parse the path step by step to see if we have to browse
1239 to a folder if the path points to a directory or the last
1240 valid element is a directory.
1242 valid variables:
1243 lpstrPathAndFile: cleaned up path
1246 nOpenAction = ONOPEN_BROWSE;
1248 /* dont apply any checks with OFN_NOVALIDATE */
1249 if(!(fodInfos->ofnInfos->Flags & OFN_NOVALIDATE))
1251 LPSTR lpszTemp, lpszTemp1;
1252 LPITEMIDLIST pidl = NULL;
1254 /* check for invalid chars */
1255 if(strpbrk(lpstrPathAndFile+3, "/:<>|") != NULL)
1257 FILEDLG95_OnOpenMessage(hwnd, IDS_INVALID_FILENAME_TITLE, IDS_INVALID_FILENAME);
1258 ret = FALSE;
1259 goto ret;
1262 if (FAILED (COMDLG32_SHGetDesktopFolder(&lpsf))) return FALSE;
1264 lpszTemp1 = lpszTemp = lpstrPathAndFile;
1265 while (lpszTemp1)
1267 LPSHELLFOLDER lpsfChild;
1268 WCHAR lpwstrTemp[MAX_PATH];
1269 DWORD dwEaten, dwAttributes;
1271 lpszTemp = COMDLG32_PathFindNextComponentA(lpszTemp);
1272 if (*lpszTemp)
1273 lstrcpynAtoW(lpwstrTemp, lpszTemp1, lpszTemp - lpszTemp1);
1274 else
1276 lstrcpyAtoW(lpwstrTemp, lpszTemp1); /* last element */
1277 if(strpbrk(lpszTemp1, "*?") != NULL)
1279 nOpenAction = ONOPEN_SEARCH;
1280 break;
1283 lpszTemp1 = lpszTemp;
1285 TRACE("parse now=%s next=%s sf=%p\n",debugstr_w(lpwstrTemp), debugstr_a(lpszTemp), lpsf);
1287 if(lstrlenW(lpwstrTemp)==2) COMDLG32_PathAddBackslashW(lpwstrTemp);
1289 dwAttributes = SFGAO_FOLDER;
1290 if(FAILED(IShellFolder_ParseDisplayName(lpsf, hwnd, NULL, lpwstrTemp, &dwEaten, &pidl, &dwAttributes)))
1292 if(*lpszTemp) /* points to trailing null for last path element */
1294 if(fodInfos->ofnInfos->Flags & OFN_PATHMUSTEXIST)
1296 FILEDLG95_OnOpenMessage(hwnd, 0, IDS_PATHNOTEXISTING);
1297 break;
1300 else
1302 if(fodInfos->ofnInfos->Flags & OFN_FILEMUSTEXIST)
1304 FILEDLG95_OnOpenMessage(hwnd, 0, IDS_FILENOTEXISTING);
1305 break;
1308 /* change to the current folder */
1309 nOpenAction = ONOPEN_OPEN;
1310 break;
1312 else
1314 /* the path component is valid */
1315 TRACE("parse OK attr=0x%08lx pidl=%p\n", dwAttributes, pidl);
1316 if(dwAttributes & SFGAO_FOLDER)
1318 if(FAILED(IShellFolder_BindToObject(lpsf, pidl, 0, &IID_IShellFolder, (LPVOID*)&lpsfChild)))
1320 ERR("bind to failed\n"); /* should not fail */
1321 break;
1323 IShellFolder_Release(lpsf);
1324 lpsf = lpsfChild;
1325 lpsfChild = NULL;
1327 else
1329 TRACE("value\n");
1331 /* end dialog, return value */
1332 nOpenAction = ONOPEN_OPEN;
1333 break;
1335 COMDLG32_SHFree(pidl);
1336 pidl = NULL;
1339 if(pidl) COMDLG32_SHFree(pidl);
1342 /* path is valid, clean the edit box */
1343 SetDlgItemTextA(hwnd,IDC_FILENAME,"");
1346 Step 3: here we have a cleaned up and validated path
1348 valid variables:
1349 lpsf: ShellFolder bound to the rightmost valid path component
1350 lpstrPathAndFile: cleaned up path
1351 nOpenAction: action to do
1353 TRACE("end validate sf=%p\n", lpsf);
1355 switch(nOpenAction)
1357 case ONOPEN_SEARCH: /* set the current filter to the file mask and refresh */
1358 TRACE("ONOPEN_SEARCH %s\n", lpstrPathAndFile);
1360 int iPos;
1361 LPSTR lpszTemp = COMDLG32_PathFindFileNameA(lpstrPathAndFile);
1363 /* replace the current filter */
1364 if(fodInfos->ShellInfos.lpstrCurrentFilter)
1365 MemFree((LPVOID)fodInfos->ShellInfos.lpstrCurrentFilter);
1366 fodInfos->ShellInfos.lpstrCurrentFilter = MemAlloc((strlen(lpszTemp)+1)*sizeof(WCHAR));
1367 lstrcpyAtoW(fodInfos->ShellInfos.lpstrCurrentFilter, lpszTemp);
1369 /* set the filter cb to the extension when possible */
1370 if(-1 < (iPos = FILEDLG95_FILETYPE_SearchExt(fodInfos->DlgInfos.hwndFileTypeCB, lpszTemp)))
1371 CBSetCurSel(fodInfos->DlgInfos.hwndFileTypeCB, iPos);
1373 /* fall through */
1374 case ONOPEN_BROWSE: /* browse to the highest folder we could bind to */
1375 TRACE("ONOPEN_BROWSE\n");
1377 IPersistFolder2 * ppf2;
1378 if(SUCCEEDED(IShellFolder_QueryInterface( lpsf, &IID_IPersistFolder2, (LPVOID*)&ppf2)))
1380 LPITEMIDLIST pidlCurrent;
1381 IPersistFolder2_GetCurFolder(ppf2, &pidlCurrent);
1382 IPersistFolder2_Release(ppf2);
1383 if( ! COMDLG32_PIDL_ILIsEqual(pidlCurrent, fodInfos->ShellInfos.pidlAbsCurrent))
1385 IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser, pidlCurrent, SBSP_ABSOLUTE);
1387 else if( nOpenAction == ONOPEN_SEARCH )
1389 IShellView_Refresh(fodInfos->Shell.FOIShellView);
1391 COMDLG32_SHFree(pidlCurrent);
1394 ret = FALSE;
1395 break;
1396 case ONOPEN_OPEN: /* fill in the return struct and close the dialog */
1397 TRACE("ONOPEN_OPEN %s\n", lpstrPathAndFile);
1399 /* add default extension */
1400 if (fodInfos->ofnInfos->lpstrDefExt)
1402 if (! *COMDLG32_PathFindExtensionA(lpstrPathAndFile))
1404 strcat(lpstrPathAndFile, ".");
1405 strcat(lpstrPathAndFile, fodInfos->ofnInfos->lpstrDefExt);
1409 /* Check that size size of the file does not exceed buffer size */
1410 if(strlen(lpstrPathAndFile) < fodInfos->ofnInfos->nMaxFile)
1412 LPSTR lpszTemp;
1414 /* fill destination buffer */
1415 strcpy(fodInfos->ofnInfos->lpstrFile, lpstrPathAndFile);
1417 /* set filename offset */
1418 lpszTemp = COMDLG32_PathFindFileNameA(lpstrPathAndFile);
1419 fodInfos->ofnInfos->nFileOffset = lpszTemp - lpstrPathAndFile;
1421 /* set extension offset */
1422 lpszTemp = COMDLG32_PathFindExtensionA(lpstrPathAndFile);
1423 fodInfos->ofnInfos->nFileExtension = (*lpszTemp) ? lpszTemp - lpstrPathAndFile + 1 : 0;
1425 /* set the lpstrFileTitle */
1426 if(fodInfos->ofnInfos->lpstrFileTitle)
1428 LPSTR lpstrFileTitle = COMDLG32_PathFindFileNameA(lpstrPathAndFile);
1429 strncpy(fodInfos->ofnInfos->lpstrFileTitle, lpstrFileTitle, fodInfos->ofnInfos->nMaxFileTitle);
1432 FILEDLG95_Clean(hwnd);
1433 ret = EndDialog(hwnd, TRUE);
1435 else
1437 /* FIXME set error FNERR_BUFFERTOSMALL */
1438 FILEDLG95_Clean(hwnd);
1439 ret = EndDialog(hwnd, FALSE);
1441 goto ret;
1443 break;
1446 ret:
1447 if(lpsf) IShellFolder_Release(lpsf);
1448 return ret;
1451 /***********************************************************************
1452 * FILEDLG95_SHELL_Init
1454 * Initialisation of the shell objects
1456 static HRESULT FILEDLG95_SHELL_Init(HWND hwnd)
1458 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1460 TRACE("\n");
1463 * Initialisation of the FileOpenDialogInfos structure
1466 /* Shell */
1468 /*ShellInfos */
1469 fodInfos->ShellInfos.hwndOwner = hwnd;
1471 /* Disable multi-select if flag not set */
1472 if (!(fodInfos->ofnInfos->Flags & OFN_ALLOWMULTISELECT))
1474 fodInfos->ShellInfos.folderSettings.fFlags |= FWF_SINGLESEL;
1476 fodInfos->ShellInfos.folderSettings.fFlags |= FWF_AUTOARRANGE | FWF_ALIGNLEFT;
1477 fodInfos->ShellInfos.folderSettings.ViewMode = FVM_LIST;
1479 GetWindowRect(GetDlgItem(hwnd,IDC_SHELLSTATIC),&fodInfos->ShellInfos.rectView);
1480 ScreenToClient(hwnd,(LPPOINT)&fodInfos->ShellInfos.rectView.left);
1481 ScreenToClient(hwnd,(LPPOINT)&fodInfos->ShellInfos.rectView.right);
1483 /* Construct the IShellBrowser interface */
1484 fodInfos->Shell.FOIShellBrowser = IShellBrowserImpl_Construct(hwnd);
1486 return NOERROR;
1489 /***********************************************************************
1490 * FILEDLG95_SHELL_ExecuteCommand
1492 * Change the folder option and refresh the view
1493 * If the function succeeds, the return value is nonzero.
1495 static BOOL FILEDLG95_SHELL_ExecuteCommand(HWND hwnd, LPCSTR lpVerb)
1497 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1499 IContextMenu * pcm;
1500 TRACE("(0x%08x,%p)\n", hwnd, lpVerb);
1502 if(SUCCEEDED(IShellView_GetItemObject(fodInfos->Shell.FOIShellView,
1503 SVGIO_BACKGROUND,
1504 &IID_IContextMenu,
1505 (LPVOID*)&pcm)))
1507 CMINVOKECOMMANDINFO ci;
1508 ZeroMemory(&ci, sizeof(CMINVOKECOMMANDINFO));
1509 ci.cbSize = sizeof(CMINVOKECOMMANDINFO);
1510 ci.lpVerb = lpVerb;
1511 ci.hwnd = hwnd;
1513 IContextMenu_InvokeCommand(pcm, &ci);
1514 IContextMenu_Release(pcm);
1517 return FALSE;
1520 /***********************************************************************
1521 * FILEDLG95_SHELL_UpFolder
1523 * Browse to the specified object
1524 * If the function succeeds, the return value is nonzero.
1526 static BOOL FILEDLG95_SHELL_UpFolder(HWND hwnd)
1528 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1530 TRACE("\n");
1532 if(SUCCEEDED(IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser,
1533 NULL,
1534 SBSP_PARENT)))
1536 return TRUE;
1538 return FALSE;
1541 /***********************************************************************
1542 * FILEDLG95_SHELL_BrowseToDesktop
1544 * Browse to the Desktop
1545 * If the function succeeds, the return value is nonzero.
1547 static BOOL FILEDLG95_SHELL_BrowseToDesktop(HWND hwnd)
1549 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1550 LPITEMIDLIST pidl;
1551 HRESULT hres;
1553 TRACE("\n");
1555 COMDLG32_SHGetSpecialFolderLocation(0,CSIDL_DESKTOP,&pidl);
1556 hres = IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser, pidl, SBSP_ABSOLUTE);
1557 COMDLG32_SHFree(pidl);
1558 return SUCCEEDED(hres);
1560 /***********************************************************************
1561 * FILEDLG95_SHELL_Clean
1563 * Cleans the memory used by shell objects
1565 static void FILEDLG95_SHELL_Clean(HWND hwnd)
1567 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1569 TRACE("\n");
1571 COMDLG32_SHFree(fodInfos->ShellInfos.pidlAbsCurrent);
1573 /* clean Shell interfaces */
1574 IShellView_DestroyViewWindow(fodInfos->Shell.FOIShellView);
1575 IShellView_Release(fodInfos->Shell.FOIShellView);
1576 IShellFolder_Release(fodInfos->Shell.FOIShellFolder);
1577 IShellBrowser_Release(fodInfos->Shell.FOIShellBrowser);
1578 if (fodInfos->Shell.FOIDataObject)
1579 IDataObject_Release(fodInfos->Shell.FOIDataObject);
1582 /***********************************************************************
1583 * FILEDLG95_FILETYPE_Init
1585 * Initialisation of the file type combo box
1587 static HRESULT FILEDLG95_FILETYPE_Init(HWND hwnd)
1589 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1591 TRACE("\n");
1593 if(fodInfos->ofnInfos->lpstrFilter)
1595 int nFilters = 0; /* number of filters */
1596 LPSTR lpstrFilter;
1597 LPCSTR lpstrPos = fodInfos->ofnInfos->lpstrFilter;
1599 for(;;)
1601 /* filter is a list... title\0ext\0......\0\0
1602 * Set the combo item text to the title and the item data
1603 * to the ext
1605 LPCSTR lpstrDisplay;
1606 LPSTR lpstrExt;
1608 /* Get the title */
1609 if(! *lpstrPos) break; /* end */
1610 lpstrDisplay = lpstrPos;
1611 lpstrPos += strlen(lpstrPos) + 1;
1613 /* Copy the extensions */
1614 if (! *lpstrPos) return E_FAIL; /* malformed filter */
1615 if (!(lpstrExt = (LPSTR) MemAlloc(strlen(lpstrPos)+1))) return E_FAIL;
1616 strcpy(lpstrExt,lpstrPos);
1617 lpstrPos += strlen(lpstrPos) + 1;
1619 /* Add the item at the end of the combo */
1620 CBAddString(fodInfos->DlgInfos.hwndFileTypeCB, lpstrDisplay);
1621 CBSetItemDataPtr(fodInfos->DlgInfos.hwndFileTypeCB, nFilters, lpstrExt);
1622 nFilters++;
1625 * Set the current filter to the one specified
1626 * in the initialisation structure
1627 * FIXME: lpstrCustomFilter not handled at all
1630 /* set default filter index */
1631 if(fodInfos->ofnInfos->nFilterIndex == 0 && fodInfos->ofnInfos->lpstrCustomFilter == NULL)
1632 fodInfos->ofnInfos->nFilterIndex = 1;
1634 /* First, check to make sure our index isn't out of bounds. */
1635 if ( fodInfos->ofnInfos->nFilterIndex > nFilters )
1636 fodInfos->ofnInfos->nFilterIndex = nFilters;
1638 /* Set the current index selection. */
1639 CBSetCurSel(fodInfos->DlgInfos.hwndFileTypeCB, fodInfos->ofnInfos->nFilterIndex-1);
1641 /* Get the corresponding text string from the combo box. */
1642 lpstrFilter = (LPSTR) CBGetItemDataPtr(fodInfos->DlgInfos.hwndFileTypeCB,
1643 fodInfos->ofnInfos->nFilterIndex-1);
1645 if ((INT)lpstrFilter == CB_ERR) /* control is empty */
1646 lpstrFilter = NULL;
1648 if(lpstrFilter)
1650 _strlwr(lpstrFilter); /* lowercase */
1651 fodInfos->ShellInfos.lpstrCurrentFilter = MemAlloc((strlen(lpstrFilter)+1)*2);
1652 lstrcpyAtoW(fodInfos->ShellInfos.lpstrCurrentFilter, lpstrFilter);
1655 return NOERROR;
1658 /***********************************************************************
1659 * FILEDLG95_FILETYPE_OnCommand
1661 * WM_COMMAND of the file type combo box
1662 * If the function succeeds, the return value is nonzero.
1664 static BOOL FILEDLG95_FILETYPE_OnCommand(HWND hwnd, WORD wNotifyCode)
1666 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1668 switch(wNotifyCode)
1670 case CBN_SELENDOK:
1672 LPSTR lpstrFilter;
1674 /* Get the current item of the filetype combo box */
1675 int iItem = CBGetCurSel(fodInfos->DlgInfos.hwndFileTypeCB);
1677 /* set the current filter index - indexed from 1 */
1678 fodInfos->ofnInfos->nFilterIndex = iItem + 1;
1680 /* Set the current filter with the current selection */
1681 if(fodInfos->ShellInfos.lpstrCurrentFilter)
1682 MemFree((LPVOID)fodInfos->ShellInfos.lpstrCurrentFilter);
1684 lpstrFilter = (LPSTR) CBGetItemDataPtr(fodInfos->DlgInfos.hwndFileTypeCB,
1685 iItem);
1686 if((int)lpstrFilter != CB_ERR)
1688 fodInfos->ShellInfos.lpstrCurrentFilter = MemAlloc((strlen(lpstrFilter)+1)*2);
1689 lstrcpyAtoW(fodInfos->ShellInfos.lpstrCurrentFilter,_strlwr(lpstrFilter));
1690 SendCustomDlgNotificationMessage(hwnd,CDN_TYPECHANGE);
1693 /* Refresh the actual view to display the included items*/
1694 IShellView_Refresh(fodInfos->Shell.FOIShellView);
1697 return FALSE;
1699 /***********************************************************************
1700 * FILEDLG95_FILETYPE_SearchExt
1702 * searches for a extension in the filetype box
1704 static int FILEDLG95_FILETYPE_SearchExt(HWND hwnd,LPCSTR lpstrExt)
1706 int i, iCount = CBGetCount(hwnd);
1708 TRACE("%s\n", lpstrExt);
1710 if(iCount != CB_ERR)
1712 for(i=0;i<iCount;i++)
1714 if(!strcasecmp(lpstrExt,(LPSTR)CBGetItemDataPtr(hwnd,i)))
1715 return i;
1718 return -1;
1721 /***********************************************************************
1722 * FILEDLG95_FILETYPE_Clean
1724 * Clean the memory used by the filetype combo box
1726 static void FILEDLG95_FILETYPE_Clean(HWND hwnd)
1728 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1729 int iPos;
1730 int iCount = CBGetCount(fodInfos->DlgInfos.hwndFileTypeCB);
1732 TRACE("\n");
1734 /* Delete each string of the combo and their associated data */
1735 if(iCount != CB_ERR)
1737 for(iPos = iCount-1;iPos>=0;iPos--)
1739 MemFree((LPSTR) CBGetItemDataPtr(fodInfos->DlgInfos.hwndFileTypeCB,iPos));
1740 CBDeleteString(fodInfos->DlgInfos.hwndFileTypeCB,iPos);
1743 /* Current filter */
1744 if(fodInfos->ShellInfos.lpstrCurrentFilter)
1745 MemFree(fodInfos->ShellInfos.lpstrCurrentFilter);
1749 /***********************************************************************
1750 * FILEDLG95_LOOKIN_Init
1752 * Initialisation of the look in combo box
1754 static HRESULT FILEDLG95_LOOKIN_Init(HWND hwndCombo)
1756 IShellFolder *psfRoot, *psfDrives;
1757 IEnumIDList *lpeRoot, *lpeDrives;
1758 LPITEMIDLIST pidlDrives, pidlTmp, pidlTmp1, pidlAbsTmp;
1760 LookInInfos *liInfos = MemAlloc(sizeof(LookInInfos));
1762 TRACE("\n");
1764 liInfos->iMaxIndentation = 0;
1766 SetPropA(hwndCombo, LookInInfosStr, (HANDLE) liInfos);
1767 CBSetItemHeight(hwndCombo,0,GetSystemMetrics(SM_CYSMICON));
1769 /* Initialise data of Desktop folder */
1770 COMDLG32_SHGetSpecialFolderLocation(0,CSIDL_DESKTOP,&pidlTmp);
1771 FILEDLG95_LOOKIN_AddItem(hwndCombo, pidlTmp,LISTEND);
1772 COMDLG32_SHFree(pidlTmp);
1774 COMDLG32_SHGetSpecialFolderLocation(0,CSIDL_DRIVES,&pidlDrives);
1776 COMDLG32_SHGetDesktopFolder(&psfRoot);
1778 if (psfRoot)
1780 /* enumerate the contents of the desktop */
1781 if(SUCCEEDED(IShellFolder_EnumObjects(psfRoot, hwndCombo, SHCONTF_FOLDERS, &lpeRoot)))
1783 while (S_OK == IEnumIDList_Next(lpeRoot, 1, &pidlTmp, NULL))
1785 FILEDLG95_LOOKIN_AddItem(hwndCombo, pidlTmp,LISTEND);
1787 /* special handling for CSIDL_DRIVES */
1788 if (COMDLG32_PIDL_ILIsEqual(pidlTmp, pidlDrives))
1790 if(SUCCEEDED(IShellFolder_BindToObject(psfRoot, pidlTmp, NULL, &IID_IShellFolder, (LPVOID*)&psfDrives)))
1792 /* enumerate the drives */
1793 if(SUCCEEDED(IShellFolder_EnumObjects(psfDrives, hwndCombo,SHCONTF_FOLDERS, &lpeDrives)))
1795 while (S_OK == IEnumIDList_Next(lpeDrives, 1, &pidlTmp1, NULL))
1797 pidlAbsTmp = COMDLG32_PIDL_ILCombine(pidlTmp, pidlTmp1);
1798 FILEDLG95_LOOKIN_AddItem(hwndCombo, pidlAbsTmp,LISTEND);
1799 COMDLG32_SHFree(pidlAbsTmp);
1800 COMDLG32_SHFree(pidlTmp1);
1802 IEnumIDList_Release(lpeDrives);
1804 IShellFolder_Release(psfDrives);
1807 COMDLG32_SHFree(pidlTmp);
1809 IEnumIDList_Release(lpeRoot);
1813 IShellFolder_Release(psfRoot);
1814 COMDLG32_SHFree(pidlDrives);
1815 return NOERROR;
1818 /***********************************************************************
1819 * FILEDLG95_LOOKIN_DrawItem
1821 * WM_DRAWITEM message handler
1823 static LRESULT FILEDLG95_LOOKIN_DrawItem(LPDRAWITEMSTRUCT pDIStruct)
1825 COLORREF crWin = GetSysColor(COLOR_WINDOW);
1826 COLORREF crHighLight = GetSysColor(COLOR_HIGHLIGHT);
1827 COLORREF crText = GetSysColor(COLOR_WINDOWTEXT);
1828 RECT rectText;
1829 RECT rectIcon;
1830 SHFILEINFOA sfi;
1831 HIMAGELIST ilItemImage;
1832 int iIndentation;
1833 LPSFOLDER tmpFolder;
1836 LookInInfos *liInfos = (LookInInfos *)GetPropA(pDIStruct->hwndItem,LookInInfosStr);
1838 TRACE("\n");
1840 if(pDIStruct->itemID == -1)
1841 return 0;
1843 if(!(tmpFolder = (LPSFOLDER) CBGetItemDataPtr(pDIStruct->hwndItem,
1844 pDIStruct->itemID)))
1845 return 0;
1848 if(pDIStruct->itemID == liInfos->uSelectedItem)
1850 ilItemImage = (HIMAGELIST) COMDLG32_SHGetFileInfoA ((LPCSTR) tmpFolder->pidlItem,
1852 &sfi,
1853 sizeof (SHFILEINFOA),
1854 SHGFI_PIDL | SHGFI_SMALLICON |
1855 SHGFI_OPENICON | SHGFI_SYSICONINDEX |
1856 SHGFI_DISPLAYNAME );
1858 else
1860 ilItemImage = (HIMAGELIST) COMDLG32_SHGetFileInfoA ((LPCSTR) tmpFolder->pidlItem,
1862 &sfi,
1863 sizeof (SHFILEINFOA),
1864 SHGFI_PIDL | SHGFI_SMALLICON |
1865 SHGFI_SYSICONINDEX |
1866 SHGFI_DISPLAYNAME);
1869 /* Is this item selected ?*/
1870 if(pDIStruct->itemState & ODS_SELECTED)
1872 SetTextColor(pDIStruct->hDC,(0x00FFFFFF & ~(crText)));
1873 SetBkColor(pDIStruct->hDC,crHighLight);
1874 FillRect(pDIStruct->hDC,&pDIStruct->rcItem,(HBRUSH)crHighLight);
1876 else
1878 SetTextColor(pDIStruct->hDC,crText);
1879 SetBkColor(pDIStruct->hDC,crWin);
1880 FillRect(pDIStruct->hDC,&pDIStruct->rcItem,(HBRUSH)crWin);
1883 /* Do not indent item if drawing in the edit of the combo*/
1884 if(pDIStruct->itemState & ODS_COMBOBOXEDIT)
1886 iIndentation = 0;
1887 ilItemImage = (HIMAGELIST) COMDLG32_SHGetFileInfoA ((LPCSTR) tmpFolder->pidlItem,
1889 &sfi,
1890 sizeof (SHFILEINFOA),
1891 SHGFI_PIDL | SHGFI_SMALLICON | SHGFI_OPENICON
1892 | SHGFI_SYSICONINDEX | SHGFI_DISPLAYNAME );
1895 else
1897 iIndentation = tmpFolder->m_iIndent;
1899 /* Draw text and icon */
1901 /* Initialise the icon display area */
1902 rectIcon.left = pDIStruct->rcItem.left + ICONWIDTH/2 * iIndentation;
1903 rectIcon.top = pDIStruct->rcItem.top;
1904 rectIcon.right = rectIcon.left + ICONWIDTH;
1905 rectIcon.bottom = pDIStruct->rcItem.bottom;
1907 /* Initialise the text display area */
1908 rectText.left = rectIcon.right;
1909 rectText.top = pDIStruct->rcItem.top + YTEXTOFFSET;
1910 rectText.right = pDIStruct->rcItem.right + XTEXTOFFSET;
1911 rectText.bottom = pDIStruct->rcItem.bottom;
1914 /* Draw the icon from the image list */
1915 COMDLG32_ImageList_Draw(ilItemImage,
1916 sfi.iIcon,
1917 pDIStruct->hDC,
1918 rectIcon.left,
1919 rectIcon.top,
1920 ILD_TRANSPARENT );
1922 /* Draw the associated text */
1923 if(sfi.szDisplayName)
1924 TextOutA(pDIStruct->hDC,rectText.left,rectText.top,sfi.szDisplayName,strlen(sfi.szDisplayName));
1927 return NOERROR;
1930 /***********************************************************************
1931 * FILEDLG95_LOOKIN_OnCommand
1933 * LookIn combo box WM_COMMAND message handler
1934 * If the function succeeds, the return value is nonzero.
1936 static BOOL FILEDLG95_LOOKIN_OnCommand(HWND hwnd, WORD wNotifyCode)
1938 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1940 TRACE("%p\n", fodInfos);
1942 switch(wNotifyCode)
1944 case CBN_SELENDOK:
1946 LPSFOLDER tmpFolder;
1947 int iItem;
1949 iItem = CBGetCurSel(fodInfos->DlgInfos.hwndLookInCB);
1951 if(!(tmpFolder = (LPSFOLDER) CBGetItemDataPtr(fodInfos->DlgInfos.hwndLookInCB,
1952 iItem)))
1953 return FALSE;
1956 if(SUCCEEDED(IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser,
1957 tmpFolder->pidlItem,
1958 SBSP_ABSOLUTE)))
1960 return TRUE;
1962 break;
1966 return FALSE;
1969 /***********************************************************************
1970 * FILEDLG95_LOOKIN_AddItem
1972 * Adds an absolute pidl item to the lookin combo box
1973 * returns the index of the inserted item
1975 static int FILEDLG95_LOOKIN_AddItem(HWND hwnd,LPITEMIDLIST pidl, int iInsertId)
1977 LPITEMIDLIST pidlNext;
1978 SHFILEINFOA sfi;
1979 SFOLDER *tmpFolder;
1980 LookInInfos *liInfos;
1982 TRACE("\n");
1984 if(!pidl)
1985 return -1;
1987 if(!(liInfos = (LookInInfos *)GetPropA(hwnd,LookInInfosStr)))
1988 return -1;
1990 tmpFolder = MemAlloc(sizeof(SFOLDER));
1991 tmpFolder->m_iIndent = 0;
1993 /* Calculate the indentation of the item in the lookin*/
1994 pidlNext = pidl;
1995 while( (pidlNext=COMDLG32_PIDL_ILGetNext(pidlNext)) )
1997 tmpFolder->m_iIndent++;
2000 tmpFolder->pidlItem = COMDLG32_PIDL_ILClone(pidl); /* FIXME: memory leak*/
2002 if(tmpFolder->m_iIndent > liInfos->iMaxIndentation)
2003 liInfos->iMaxIndentation = tmpFolder->m_iIndent;
2005 sfi.dwAttributes = SFGAO_FILESYSANCESTOR | SFGAO_FILESYSTEM;
2006 COMDLG32_SHGetFileInfoA((LPSTR)pidl,
2008 &sfi,
2009 sizeof(sfi),
2010 SHGFI_DISPLAYNAME | SHGFI_SYSICONINDEX
2011 | SHGFI_PIDL | SHGFI_SMALLICON | SHGFI_ATTRIBUTES | SHGFI_ATTR_SPECIFIED);
2014 if((sfi.dwAttributes & SFGAO_FILESYSANCESTOR) || (sfi.dwAttributes & SFGAO_FILESYSTEM))
2016 int iItemID;
2018 /* Add the item at the end of the list */
2019 if(iInsertId < 0)
2021 iItemID = CBAddString(hwnd,sfi.szDisplayName);
2023 /* Insert the item at the iInsertId position*/
2024 else
2026 iItemID = CBInsertString(hwnd,sfi.szDisplayName,iInsertId);
2029 CBSetItemDataPtr(hwnd,iItemID,tmpFolder);
2030 return iItemID;
2033 MemFree( tmpFolder );
2034 return -1;
2038 /***********************************************************************
2039 * FILEDLG95_LOOKIN_InsertItemAfterParent
2041 * Insert an item below its parent
2043 static int FILEDLG95_LOOKIN_InsertItemAfterParent(HWND hwnd,LPITEMIDLIST pidl)
2046 LPITEMIDLIST pidlParent = GetParentPidl(pidl);
2047 int iParentPos;
2049 TRACE("\n");
2051 iParentPos = FILEDLG95_LOOKIN_SearchItem(hwnd,(WPARAM)pidlParent,SEARCH_PIDL);
2053 if(iParentPos < 0)
2055 iParentPos = FILEDLG95_LOOKIN_InsertItemAfterParent(hwnd,pidlParent);
2058 /* Free pidlParent memory */
2059 COMDLG32_SHFree((LPVOID)pidlParent);
2061 return FILEDLG95_LOOKIN_AddItem(hwnd,pidl,iParentPos + 1);
2064 /***********************************************************************
2065 * FILEDLG95_LOOKIN_SelectItem
2067 * Adds an absolute pidl item to the lookin combo box
2068 * returns the index of the inserted item
2070 int FILEDLG95_LOOKIN_SelectItem(HWND hwnd,LPITEMIDLIST pidl)
2072 int iItemPos;
2073 LookInInfos *liInfos;
2075 TRACE("\n");
2077 iItemPos = FILEDLG95_LOOKIN_SearchItem(hwnd,(WPARAM)pidl,SEARCH_PIDL);
2079 liInfos = (LookInInfos *)GetPropA(hwnd,LookInInfosStr);
2081 if(iItemPos < 0)
2083 while(FILEDLG95_LOOKIN_RemoveMostExpandedItem(hwnd) > -1);
2084 iItemPos = FILEDLG95_LOOKIN_InsertItemAfterParent(hwnd,pidl);
2087 else
2089 SFOLDER *tmpFolder = (LPSFOLDER) CBGetItemDataPtr(hwnd,iItemPos);
2090 while(liInfos->iMaxIndentation > tmpFolder->m_iIndent)
2092 int iRemovedItem;
2094 if(-1 == (iRemovedItem = FILEDLG95_LOOKIN_RemoveMostExpandedItem(hwnd)))
2095 break;
2096 if(iRemovedItem < iItemPos)
2097 iItemPos--;
2101 CBSetCurSel(hwnd,iItemPos);
2102 liInfos->uSelectedItem = iItemPos;
2104 return 0;
2108 /***********************************************************************
2109 * FILEDLG95_LOOKIN_RemoveMostExpandedItem
2111 * Remove the item with an expansion level over iExpansionLevel
2113 static int FILEDLG95_LOOKIN_RemoveMostExpandedItem(HWND hwnd)
2115 int iItemPos;
2117 LookInInfos *liInfos = (LookInInfos *)GetPropA(hwnd,LookInInfosStr);
2119 TRACE("\n");
2121 if(liInfos->iMaxIndentation <= 2)
2122 return -1;
2124 if((iItemPos = FILEDLG95_LOOKIN_SearchItem(hwnd,(WPARAM)liInfos->iMaxIndentation,SEARCH_EXP)) >=0)
2126 SFOLDER *tmpFolder = (LPSFOLDER) CBGetItemDataPtr(hwnd,iItemPos);
2127 COMDLG32_SHFree(tmpFolder->pidlItem);
2128 MemFree(tmpFolder);
2129 CBDeleteString(hwnd,iItemPos);
2130 liInfos->iMaxIndentation--;
2132 return iItemPos;
2135 return -1;
2138 /***********************************************************************
2139 * FILEDLG95_LOOKIN_SearchItem
2141 * Search for pidl in the lookin combo box
2142 * returns the index of the found item
2144 static int FILEDLG95_LOOKIN_SearchItem(HWND hwnd,WPARAM searchArg,int iSearchMethod)
2146 int i = 0;
2147 int iCount = CBGetCount(hwnd);
2149 TRACE("\n");
2151 if (iCount != CB_ERR)
2153 for(;i<iCount;i++)
2155 LPSFOLDER tmpFolder = (LPSFOLDER) CBGetItemDataPtr(hwnd,i);
2157 if(iSearchMethod == SEARCH_PIDL && COMDLG32_PIDL_ILIsEqual((LPITEMIDLIST)searchArg,tmpFolder->pidlItem))
2158 return i;
2159 if(iSearchMethod == SEARCH_EXP && tmpFolder->m_iIndent == (int)searchArg)
2160 return i;
2164 return -1;
2167 /***********************************************************************
2168 * FILEDLG95_LOOKIN_Clean
2170 * Clean the memory used by the lookin combo box
2172 static void FILEDLG95_LOOKIN_Clean(HWND hwnd)
2174 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
2175 int iPos;
2176 int iCount = CBGetCount(fodInfos->DlgInfos.hwndLookInCB);
2178 TRACE("\n");
2180 /* Delete each string of the combo and their associated data */
2181 if (iCount != CB_ERR)
2183 for(iPos = iCount-1;iPos>=0;iPos--)
2185 SFOLDER *tmpFolder = (LPSFOLDER) CBGetItemDataPtr(fodInfos->DlgInfos.hwndLookInCB,iPos);
2186 COMDLG32_SHFree(tmpFolder->pidlItem);
2187 MemFree(tmpFolder);
2188 CBDeleteString(fodInfos->DlgInfos.hwndLookInCB,iPos);
2192 /* LookInInfos structure */
2193 RemovePropA(fodInfos->DlgInfos.hwndLookInCB,LookInInfosStr);
2196 /***********************************************************************
2197 * FILEDLG95_FILENAME_FillFromSelection
2199 * fills the edit box from the cached DataObject
2201 void FILEDLG95_FILENAME_FillFromSelection (HWND hwnd)
2203 FileOpenDlgInfos *fodInfos;
2204 LPITEMIDLIST pidl;
2205 UINT nFiles = 0, nFileToOpen, nFileSelected, nLength = 0;
2206 char lpstrTemp[MAX_PATH];
2207 LPSTR lpstrAllFile = NULL, lpstrCurrFile = NULL;
2209 TRACE("\n");
2210 fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
2212 /* Count how many files we have */
2213 nFileSelected = GetNumSelected( fodInfos->Shell.FOIDataObject );
2215 /* calculate the string length, count files */
2216 if (nFileSelected >= 1)
2218 nLength += 3; /* first and last quotes, trailing \0 */
2219 for ( nFileToOpen = 0; nFileToOpen < nFileSelected; nFileToOpen++ )
2221 pidl = GetPidlFromDataObject( fodInfos->Shell.FOIDataObject, nFileToOpen+1 );
2223 if (pidl)
2225 /* get the total length of the selected file names*/
2226 lpstrTemp[0] = '\0';
2227 GetName( fodInfos->Shell.FOIShellFolder, pidl, SHGDN_INFOLDER, lpstrTemp );
2229 if ( ! IsPidlFolder(fodInfos->Shell.FOIShellFolder, pidl) ) /* Ignore folders */
2231 nLength += lstrlenA( lpstrTemp ) + 3;
2232 nFiles++;
2234 COMDLG32_SHFree( pidl );
2239 /* allocate the buffer */
2240 if (nFiles <= 1) nLength = MAX_PATH;
2241 lpstrAllFile = (LPSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nLength);
2242 lpstrAllFile[0] = '\0';
2244 /* Generate the string for the edit control */
2245 if(nFiles >= 1)
2247 lpstrCurrFile = lpstrAllFile;
2248 for ( nFileToOpen = 0; nFileToOpen < nFileSelected; nFileToOpen++ )
2250 pidl = GetPidlFromDataObject( fodInfos->Shell.FOIDataObject, nFileToOpen+1 );
2252 if (pidl)
2254 /* get the file name */
2255 lpstrTemp[0] = '\0';
2256 GetName( fodInfos->Shell.FOIShellFolder, pidl, SHGDN_INFOLDER, lpstrTemp );
2258 if (! IsPidlFolder(fodInfos->Shell.FOIShellFolder, pidl)) /* Ignore folders */
2260 if ( nFiles > 1)
2262 *lpstrCurrFile++ = '\"';
2263 lstrcpyA( lpstrCurrFile, lpstrTemp );
2264 lpstrCurrFile += lstrlenA( lpstrTemp );
2265 lstrcpyA( lpstrCurrFile, "\" " );
2266 lpstrCurrFile += 2;
2268 else
2270 lstrcpyA( lpstrAllFile, lpstrTemp );
2273 COMDLG32_SHFree( (LPVOID) pidl );
2278 SetWindowTextA( fodInfos->DlgInfos.hwndFileName, lpstrAllFile );
2279 HeapFree(GetProcessHeap(),0, lpstrAllFile );
2283 /* copied from shell32 to avoid linking to it */
2284 static HRESULT COMDLG32_StrRetToStrNA (LPVOID dest, DWORD len, LPSTRRET src, LPITEMIDLIST pidl)
2286 switch (src->uType)
2288 case STRRET_WSTR:
2289 WideCharToMultiByte(CP_ACP, 0, src->u.pOleStr, -1, (LPSTR)dest, len, NULL, NULL);
2290 COMDLG32_SHFree(src->u.pOleStr);
2291 break;
2293 case STRRET_CSTRA:
2294 lstrcpynA((LPSTR)dest, src->u.cStr, len);
2295 break;
2297 case STRRET_OFFSETA:
2298 lstrcpynA((LPSTR)dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
2299 break;
2301 default:
2302 FIXME("unknown type!\n");
2303 if (len)
2305 *(LPSTR)dest = '\0';
2307 return(FALSE);
2309 return S_OK;
2312 /***********************************************************************
2313 * FILEDLG95_FILENAME_GetFileNames
2315 * copys the filenames to a 0-delimited string
2317 int FILEDLG95_FILENAME_GetFileNames (HWND hwnd, LPSTR * lpstrFileList, UINT * sizeUsed)
2319 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
2320 UINT nStrCharCount = 0; /* index in src buffer */
2321 UINT nFileIndex = 0; /* index in dest buffer */
2322 UINT nFileCount = 0; /* number of files */
2323 UINT nStrLen = 0; /* length of string in edit control */
2324 LPSTR lpstrEdit; /* buffer for string from edit control */
2326 TRACE("\n");
2328 /* get the filenames from the edit control */
2329 nStrLen = SendMessageA(fodInfos->DlgInfos.hwndFileName, WM_GETTEXTLENGTH, 0, 0);
2330 lpstrEdit = MemAlloc(nStrLen+1);
2331 GetDlgItemTextA(hwnd, IDC_FILENAME, lpstrEdit, nStrLen+1);
2333 TRACE("nStrLen=%u str=%s\n", nStrLen, lpstrEdit);
2335 *lpstrFileList = MemAlloc(nStrLen);
2336 *sizeUsed = 0;
2338 /* build 0-delimited file list from filenames*/
2339 while ( nStrCharCount <= nStrLen )
2341 if ( lpstrEdit[nStrCharCount]=='"' )
2343 nStrCharCount++;
2344 while ((lpstrEdit[nStrCharCount]!='"') && (nStrCharCount <= nStrLen))
2346 (*lpstrFileList)[nFileIndex++] = lpstrEdit[nStrCharCount];
2347 (*sizeUsed)++;
2348 nStrCharCount++;
2350 (*lpstrFileList)[nFileIndex++] = '\0';
2351 (*sizeUsed)++;
2352 nFileCount++;
2354 nStrCharCount++;
2357 /* single, unquoted string */
2358 if ((nStrLen > 0) && (*sizeUsed == 0) )
2360 strcpy(*lpstrFileList, lpstrEdit);
2361 nFileIndex = strlen(lpstrEdit) + 1;
2362 (*sizeUsed) = nFileIndex;
2363 nFileCount = 1;
2366 /* trailing \0 */
2367 (*lpstrFileList)[nFileIndex] = '\0';
2368 (*sizeUsed)++;
2370 MemFree(lpstrEdit);
2371 return nFileCount;
2374 #define SETDefFormatEtc(fe,cf,med) \
2376 (fe).cfFormat = cf;\
2377 (fe).dwAspect = DVASPECT_CONTENT; \
2378 (fe).ptd =NULL;\
2379 (fe).tymed = med;\
2380 (fe).lindex = -1;\
2384 * DATAOBJECT Helper functions
2387 /***********************************************************************
2388 * COMCTL32_ReleaseStgMedium
2390 * like ReleaseStgMedium from ole32
2392 static void COMCTL32_ReleaseStgMedium (STGMEDIUM medium)
2394 if(medium.pUnkForRelease)
2396 IUnknown_Release(medium.pUnkForRelease);
2398 else
2400 GlobalUnlock(medium.u.hGlobal);
2401 GlobalFree(medium.u.hGlobal);
2405 /***********************************************************************
2406 * GetPidlFromDataObject
2408 * Return pidl(s) by number from the cached DataObject
2410 * nPidlIndex=0 gets the fully qualified root path
2412 LPITEMIDLIST GetPidlFromDataObject ( IDataObject *doSelected, UINT nPidlIndex)
2415 STGMEDIUM medium;
2416 FORMATETC formatetc;
2417 LPITEMIDLIST pidl = NULL;
2419 TRACE("sv=%p index=%u\n", doSelected, nPidlIndex);
2421 /* Set the FORMATETC structure*/
2422 SETDefFormatEtc(formatetc, RegisterClipboardFormatA(CFSTR_SHELLIDLIST), TYMED_HGLOBAL);
2424 /* Get the pidls from IDataObject */
2425 if(SUCCEEDED(IDataObject_GetData(doSelected,&formatetc,&medium)))
2427 LPIDA cida = GlobalLock(medium.u.hGlobal);
2428 if(nPidlIndex <= cida->cidl)
2430 pidl = COMDLG32_PIDL_ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[nPidlIndex]]));
2432 COMCTL32_ReleaseStgMedium(medium);
2434 return pidl;
2437 /***********************************************************************
2438 * GetNumSelected
2440 * Return the number of selected items in the DataObject.
2443 UINT GetNumSelected( IDataObject *doSelected )
2445 UINT retVal = 0;
2446 STGMEDIUM medium;
2447 FORMATETC formatetc;
2449 TRACE("sv=%p\n", doSelected);
2451 if (!doSelected) return 0;
2453 /* Set the FORMATETC structure*/
2454 SETDefFormatEtc(formatetc, RegisterClipboardFormatA(CFSTR_SHELLIDLIST), TYMED_HGLOBAL);
2456 /* Get the pidls from IDataObject */
2457 if(SUCCEEDED(IDataObject_GetData(doSelected,&formatetc,&medium)))
2459 LPIDA cida = GlobalLock(medium.u.hGlobal);
2460 retVal = cida->cidl;
2461 COMCTL32_ReleaseStgMedium(medium);
2462 return retVal;
2464 return 0;
2468 * TOOLS
2471 /***********************************************************************
2472 * GetName
2474 * Get the pidl's display name (relative to folder) and
2475 * put it in lpstrFileName.
2477 * Return NOERROR on success,
2478 * E_FAIL otherwise
2481 HRESULT GetName(LPSHELLFOLDER lpsf, LPITEMIDLIST pidl,DWORD dwFlags,LPSTR lpstrFileName)
2483 STRRET str;
2484 HRESULT hRes;
2486 TRACE("sf=%p pidl=%p\n", lpsf, pidl);
2488 if(!lpsf)
2490 HRESULT hRes;
2491 COMDLG32_SHGetDesktopFolder(&lpsf);
2492 hRes = GetName(lpsf,pidl,dwFlags,lpstrFileName);
2493 IShellFolder_Release(lpsf);
2494 return hRes;
2497 /* Get the display name of the pidl relative to the folder */
2498 if (SUCCEEDED(hRes = IShellFolder_GetDisplayNameOf(lpsf, pidl, dwFlags, &str)))
2500 return COMDLG32_StrRetToStrNA(lpstrFileName, MAX_PATH, &str, pidl);
2502 return E_FAIL;
2505 /***********************************************************************
2506 * GetShellFolderFromPidl
2508 * pidlRel is the item pidl relative
2509 * Return the IShellFolder of the absolute pidl
2511 IShellFolder *GetShellFolderFromPidl(LPITEMIDLIST pidlAbs)
2513 IShellFolder *psf = NULL,*psfParent;
2515 TRACE("%p\n", pidlAbs);
2517 if(SUCCEEDED(COMDLG32_SHGetDesktopFolder(&psfParent)))
2519 psf = psfParent;
2520 if(pidlAbs && pidlAbs->mkid.cb)
2522 if(SUCCEEDED(IShellFolder_BindToObject(psfParent, pidlAbs, NULL, &IID_IShellFolder, (LPVOID*)&psf)))
2524 IShellFolder_Release(psfParent);
2525 return psf;
2528 /* return the desktop */
2529 return psfParent;
2531 return NULL;
2534 /***********************************************************************
2535 * GetParentPidl
2537 * Return the LPITEMIDLIST to the parent of the pidl in the list
2539 LPITEMIDLIST GetParentPidl(LPITEMIDLIST pidl)
2541 LPITEMIDLIST pidlParent;
2543 TRACE("%p\n", pidl);
2545 pidlParent = COMDLG32_PIDL_ILClone(pidl);
2546 COMDLG32_PIDL_ILRemoveLastID(pidlParent);
2548 return pidlParent;
2551 /***********************************************************************
2552 * GetPidlFromName
2554 * returns the pidl of the file name relative to folder
2555 * NULL if an error occured
2557 LPITEMIDLIST GetPidlFromName(IShellFolder *lpsf,LPCSTR lpcstrFileName)
2559 LPITEMIDLIST pidl;
2560 ULONG ulEaten;
2561 WCHAR lpwstrDirName[MAX_PATH];
2563 TRACE("sf=%p file=%s\n", lpsf, lpcstrFileName);
2565 if(!lpcstrFileName) return NULL;
2567 MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,lpcstrFileName,-1,(LPWSTR)lpwstrDirName,MAX_PATH);
2569 if(!lpsf)
2571 COMDLG32_SHGetDesktopFolder(&lpsf);
2572 pidl = GetPidlFromName(lpsf, lpcstrFileName);
2573 IShellFolder_Release(lpsf);
2575 else
2577 IShellFolder_ParseDisplayName(lpsf, 0, NULL, (LPWSTR)lpwstrDirName, &ulEaten, &pidl, NULL);
2579 return pidl;
2584 BOOL IsPidlFolder (LPSHELLFOLDER psf, LPITEMIDLIST pidl)
2586 ULONG uAttr = SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
2587 HRESULT ret;
2589 TRACE("%p, %p\n", psf, pidl);
2591 ret = IShellFolder_GetAttributesOf( psf, 1, &pidl, &uAttr );
2593 TRACE("-- 0x%08lx 0x%08lx\n", uAttr, ret);
2594 /* see documentation shell 4.1*/
2595 return uAttr & (SFGAO_FOLDER | SFGAO_HASSUBFOLDER);
2598 /***********************************************************************
2599 * BrowseSelectedFolder
2601 static BOOL BrowseSelectedFolder(HWND hwnd)
2603 BOOL bBrowseSelFolder = FALSE;
2604 FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
2606 TRACE("\n");
2608 if (GetNumSelected(fodInfos->Shell.FOIDataObject) == 1)
2610 LPITEMIDLIST pidlSelection;
2612 /* get the file selected */
2613 pidlSelection = GetPidlFromDataObject( fodInfos->Shell.FOIDataObject, 1);
2614 if (IsPidlFolder (fodInfos->Shell.FOIShellFolder, pidlSelection))
2616 if ( FAILED( IShellBrowser_BrowseObject( fodInfos->Shell.FOIShellBrowser,
2617 pidlSelection, SBSP_RELATIVE ) ) )
2619 MessageBoxA( hwnd, "Path does not exist", fodInfos->ofnInfos->lpstrTitle,
2620 MB_OK | MB_ICONEXCLAMATION );
2623 bBrowseSelFolder = TRUE;
2625 COMDLG32_SHFree( pidlSelection );
2628 return bBrowseSelFolder;
2632 * Memory allocation methods */
2633 static void *MemAlloc(UINT size)
2635 return HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,size);
2638 static void MemFree(void *mem)
2640 if(mem)
2642 HeapFree(GetProcessHeap(),0,mem);