filedlg95.c 82.2 KB
Newer Older
1 2 3
/*
 * COMMDLG - File Open Dialogs Win95 look and feel
 *
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * FIXME: The whole concept of handling unicode is badly broken.
 *	many hook-messages expecting a pointer to a
 *	OPENFILENAMEA or W structure. With the current architecture
 *	we would have to convert the beast at every call to a hook.
 *	we have to find a better solution but if would likely cause
 *	a complete rewrite with after we shouldhandle the
 *	OPENFILENAME structure without any converting (jsch).
 *
 * FIXME: any hook gets a OPENFILENAMEA structure
 *
 * FIXME: CDN_FILEOK is wrong implemented, other CDN_ messages likely too
 *
 * FIXME: old style hook messages are not implemented (except FILEOKSTRING)
 *
18
 * FIXME: lpstrCustomFilter not handled
19
 *
20
 * FIXME: if the size of lpstrFile (nMaxFile) is too small the first
21 22
 * two bytes of lpstrFile should contain the needed size
 *
23
 * FIXME: algorithm for selecting the initial directory is too simple
24 25 26 27 28
 *
 * FIXME: add to recent docs
 *
 * FIXME: flags not implemented: OFN_CREATEPROMPT, OFN_DONTADDTORECENT,
 * OFN_ENABLEINCLUDENOTIFY, OFN_ENABLESIZING, OFN_EXTENSIONDIFFERENT,
29
 * OFN_NOCHANGEDIR, OFN_NODEREFERENCELINKS, OFN_NOREADONLYRETURN,
30 31 32 33 34
 * OFN_NOTESTFILECREATE, OFN_OVERWRITEPROMPT, OFN_USEMONIKERS
 *
 * FIXME: lCustData for lpfnHook (WM_INITDIALOG)
 *
 *
35 36 37
 */
#include <ctype.h>
#include <stdlib.h>
38
#include <stdio.h>
39
#include <string.h>
Patrik Stridvall's avatar
Patrik Stridvall committed
40

41
#include "winbase.h"
42
#include "ntddk.h"
43
#include "winnls.h"
44 45 46
#include "heap.h"
#include "commdlg.h"
#include "dlgs.h"
47
#include "cdlg.h"
48 49 50
#include "debugtools.h"
#include "cderr.h"
#include "shellapi.h"
51
#include "shlguid.h"
52
#include "filedlgbrowser.h"
53
#include "shlwapi.h"
54 55
#include "wine/obj_contextmenu.h"

56
DEFAULT_DEBUG_CHANNEL(commdlg);
57

58 59 60 61 62 63 64 65
#define UNIMPLEMENTED_FLAGS \
(OFN_CREATEPROMPT | OFN_DONTADDTORECENT |\
OFN_ENABLEINCLUDENOTIFY | OFN_ENABLESIZING | OFN_EXTENSIONDIFFERENT |\
OFN_NOCHANGEDIR | OFN_NODEREFERENCELINKS | OFN_NOREADONLYRETURN |\
OFN_NOTESTFILECREATE | OFN_OVERWRITEPROMPT /*| OFN_USEMONIKERS*/)

#define IsHooked(fodInfos) \
	((fodInfos->ofnInfos->Flags & OFN_ENABLEHOOK) && fodInfos->ofnInfos->lpfnHook)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
/***********************************************************************
 * Data structure and global variables
 */
typedef struct SFolder
{
  int m_iImageIndex;    /* Index of picture in image list */
  HIMAGELIST hImgList;
  int m_iIndent;      /* Indentation index */
  LPITEMIDLIST pidlItem;  /* absolute pidl of the item */ 

} SFOLDER,*LPSFOLDER;

typedef struct tagLookInInfo
{
  int iMaxIndentation;
  UINT uSelectedItem;
} LookInInfos;


/***********************************************************************
 * Defines and global variables
 */

/* Draw item constant */
#define ICONWIDTH 18
#define XTEXTOFFSET 3

/* AddItem flags*/
#define LISTEND -1

/* SearchItem methods */
#define SEARCH_PIDL 1
#define SEARCH_EXP  2
#define ITEM_NOTFOUND -1

/* Undefined windows message sent by CreateViewObject*/
#define WM_GETISHELLBROWSER  WM_USER+7

/* NOTE
 * Those macros exist in windowsx.h. However, you can't really use them since
106
 * they rely on the UNICODE defines and can't be used inside Wine itself.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
 */

/* Combo box macros */
#define CBAddString(hwnd,str) \
  SendMessageA(hwnd,CB_ADDSTRING,0,(LPARAM)str);

#define CBInsertString(hwnd,str,pos) \
  SendMessageA(hwnd,CB_INSERTSTRING,(WPARAM)pos,(LPARAM)str);

#define CBDeleteString(hwnd,pos) \
  SendMessageA(hwnd,CB_DELETESTRING,(WPARAM)pos,0);

#define CBSetItemDataPtr(hwnd,iItemId,dataPtr) \
  SendMessageA(hwnd,CB_SETITEMDATA,(WPARAM)iItemId,(LPARAM)dataPtr);

#define CBGetItemDataPtr(hwnd,iItemId) \
  SendMessageA(hwnd,CB_GETITEMDATA,(WPARAM)iItemId,0)

#define CBGetLBText(hwnd,iItemId,str) \
  SendMessageA(hwnd,CB_GETLBTEXT,(WPARAM)iItemId,(LPARAM)str);

#define CBGetCurSel(hwnd) \
  SendMessageA(hwnd,CB_GETCURSEL,0,0);

#define CBSetCurSel(hwnd,pos) \
  SendMessageA(hwnd,CB_SETCURSEL,(WPARAM)pos,0);

#define CBGetCount(hwnd) \
    SendMessageA(hwnd,CB_GETCOUNT,0,0);
#define CBShowDropDown(hwnd,show) \
  SendMessageA(hwnd,CB_SHOWDROPDOWN,(WPARAM)show,0);
138 139
#define CBSetItemHeight(hwnd,index,height) \
  SendMessageA(hwnd,CB_SETITEMHEIGHT,(WPARAM)index,(LPARAM)height);
140 141 142 143 144 145 146 147 148 149 150 151


const char *FileOpenDlgInfosStr = "FileOpenDlgInfos"; /* windows property description string */
const char *LookInInfosStr = "LookInInfos"; /* LOOKIN combo box property */

static const char defaultFilter[] = "*.*";

/***********************************************************************
 * Prototypes
 */

/* Internal functions used by the dialog */
152
static LRESULT FILEDLG95_FillControls(HWND hwnd, WPARAM wParam, LPARAM lParam);
153 154 155
static LRESULT FILEDLG95_OnWMCommand(HWND hwnd, WPARAM wParam, LPARAM lParam);
static LRESULT FILEDLG95_OnWMGetIShellBrowser(HWND hwnd);
       BOOL    FILEDLG95_OnOpen(HWND hwnd);
156
static LRESULT FILEDLG95_InitControls(HWND hwnd);
157 158
static void    FILEDLG95_Clean(HWND hwnd);

Juergen Schmied's avatar
Juergen Schmied committed
159
/* Functions used by the shell navigation */
160 161 162 163
static LRESULT FILEDLG95_SHELL_Init(HWND hwnd);
static BOOL    FILEDLG95_SHELL_UpFolder(HWND hwnd);
static BOOL    FILEDLG95_SHELL_ExecuteCommand(HWND hwnd, LPCSTR lpVerb);
static void    FILEDLG95_SHELL_Clean(HWND hwnd);
Juergen Schmied's avatar
Juergen Schmied committed
164
static BOOL    FILEDLG95_SHELL_BrowseToDesktop(HWND hwnd);
165

166 167 168
/* Functions used by the filetype combo box */
static HRESULT FILEDLG95_FILETYPE_Init(HWND hwnd);
static BOOL    FILEDLG95_FILETYPE_OnCommand(HWND hwnd, WORD wNotifyCode);
169
static int     FILEDLG95_FILETYPE_SearchExt(HWND hwnd,LPCSTR lpstrExt);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
static void    FILEDLG95_FILETYPE_Clean(HWND hwnd);

/* Functions used by the Look In combo box */
static HRESULT FILEDLG95_LOOKIN_Init(HWND hwndCombo);
static LRESULT FILEDLG95_LOOKIN_DrawItem(LPDRAWITEMSTRUCT pDIStruct);
static BOOL    FILEDLG95_LOOKIN_OnCommand(HWND hwnd, WORD wNotifyCode);
static int     FILEDLG95_LOOKIN_AddItem(HWND hwnd,LPITEMIDLIST pidl, int iInsertId);
static int     FILEDLG95_LOOKIN_SearchItem(HWND hwnd,WPARAM searchArg,int iSearchMethod);
static int     FILEDLG95_LOOKIN_InsertItemAfterParent(HWND hwnd,LPITEMIDLIST pidl);
static int     FILEDLG95_LOOKIN_RemoveMostExpandedItem(HWND hwnd);
       int     FILEDLG95_LOOKIN_SelectItem(HWND hwnd,LPITEMIDLIST pidl);
static void    FILEDLG95_LOOKIN_Clean(HWND hwnd);

/* Miscellaneous tool functions */
HRESULT       GetName(LPSHELLFOLDER lpsf, LPITEMIDLIST pidl,DWORD dwFlags,LPSTR lpstrFileName);
HRESULT       GetFileName(HWND hwnd, LPITEMIDLIST pidl, LPSTR lpstrFileName);
IShellFolder* GetShellFolderFromPidl(LPITEMIDLIST pidlAbs);
LPITEMIDLIST  GetParentPidl(LPITEMIDLIST pidl);
LPITEMIDLIST  GetPidlFromName(IShellFolder *psf,LPCSTR lpcstrFileName);

/* Shell memory allocation */
191 192
static void *MemAlloc(UINT size);
static void MemFree(void *mem);
193

194
BOOL WINAPI GetFileName95(FileOpenDlgInfos *fodInfos);
195
HRESULT WINAPI FileOpenDlgProc95(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
196 197
HRESULT SendCustomDlgNotificationMessage(HWND hwndParentDlg, UINT uCode);
HRESULT FILEDLG95_HandleCustomDialogMessages(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
198
BOOL FILEDLG95_OnOpenMultipleFiles(HWND hwnd, LPSTR lpstrFileList, UINT nFileCount, UINT sizeUsed);
199
static BOOL BrowseSelectedFolder(HWND hwnd);
200 201

/***********************************************************************
202
 *      GetFileName95
203 204 205 206 207 208 209 210
 *
 * Creates an Open common dialog box that lets the user select 
 * the drive, directory, and the name of a file or set of files to open.
 *
 * IN  : The FileOpenDlgInfos structure associated with the dialog
 * OUT : TRUE on success
 *       FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
 */
211
BOOL WINAPI GetFileName95(FileOpenDlgInfos *fodInfos)
212 213 214 215 216 217 218
{

    LRESULT lRes;
    LPCVOID template;
    HRSRC hRes;
    HANDLE hDlgTmpl = 0;

219 220 221 222 223 224 225
    /* test for missing functionality */
    if (fodInfos->ofnInfos->Flags & UNIMPLEMENTED_FLAGS)
    {
      FIXME("Flags 0x%08lx not yet implemented\n",
         fodInfos->ofnInfos->Flags & UNIMPLEMENTED_FLAGS);
    }

226 227
    /* Create the dialog from a template */

228
    if(!(hRes = FindResourceA(COMMDLG_hInstance32,MAKEINTRESOURCEA(NEWFILEOPENORD),RT_DIALOGA)))
229 230 231 232 233 234 235 236 237 238
    {
        COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
        return FALSE;
    }
    if (!(hDlgTmpl = LoadResource(COMMDLG_hInstance32, hRes )) ||
        !(template = LockResource( hDlgTmpl )))
    {
        COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
        return FALSE;
    }
239 240 241 242 243 244 245 246 247 248
    
    /* old style hook messages */
    if (IsHooked(fodInfos))
    {
      fodInfos->HookMsg.fileokstring = RegisterWindowMessageA(FILEOKSTRING);
      fodInfos->HookMsg.lbselchstring = RegisterWindowMessageA(LBSELCHSTRING);
      fodInfos->HookMsg.helpmsgstring = RegisterWindowMessageA(HELPMSGSTRING);
      fodInfos->HookMsg.sharevistring = RegisterWindowMessageA(SHAREVISTRING);
    }
    
249 250
    lRes = DialogBoxIndirectParamA(COMMDLG_hInstance32,
                                  (LPDLGTEMPLATEA) template,
Alexandre Julliard's avatar
Alexandre Julliard committed
251
                                  fodInfos->ofnInfos->hwndOwner,
252 253 254
                                  (DLGPROC) FileOpenDlgProc95,
                                  (LPARAM) fodInfos);

255
    /* Unable to create the dialog */
256 257 258 259 260 261 262 263 264
    if( lRes == -1)
        return FALSE;
    
    return lRes;
}

/***********************************************************************
 *      GetFileDialog95A
 *
265
 * Call GetFileName95 with this structure and clean the memory.
266 267 268 269 270 271 272 273 274
 *
 * IN  : The OPENFILENAMEA initialisation structure passed to
 *       GetOpenFileNameA win api function (see filedlg.c)
 */
BOOL  WINAPI GetFileDialog95A(LPOPENFILENAMEA ofn,UINT iDlgType)
{

  BOOL ret;
  FileOpenDlgInfos *fodInfos;
Alexandre Julliard's avatar
Alexandre Julliard committed
275
  HINSTANCE hInstance;
276
  LPCSTR lpstrInitialDir = (LPCSTR)-1;
Alexandre Julliard's avatar
Alexandre Julliard committed
277
  DWORD dwFlags = 0;
278 279 280
  
  /* Initialise FileOpenDlgInfos structure*/  
  fodInfos = (FileOpenDlgInfos*)MemAlloc(sizeof(FileOpenDlgInfos));
281 282
  ZeroMemory(fodInfos, sizeof(FileOpenDlgInfos));
  
Alexandre Julliard's avatar
Alexandre Julliard committed
283 284 285 286 287 288 289 290
  /* Pass in the original ofn */
  fodInfos->ofnInfos = ofn;
  
  /* Save original hInstance value */
  hInstance = ofn->hInstance;
  fodInfos->ofnInfos->hInstance = MapHModuleLS(ofn->hInstance);

  dwFlags = ofn->Flags;
291
  ofn->Flags = ofn->Flags|OFN_WINE;
292

293 294
  /* Initialise the dialog property */
  fodInfos->DlgInfos.dwDlgProp = 0;
295
  fodInfos->DlgInfos.hwndCustomDlg = (HWND)NULL;
296
  
297 298
  switch(iDlgType)
  {
299
    case OPEN_DIALOG :
300
      ret = GetFileName95(fodInfos);
301
      break;
302
    case SAVE_DIALOG :
303
      fodInfos->DlgInfos.dwDlgProp |= FODPROP_SAVEDLG;
304
      ret = GetFileName95(fodInfos);
305
      break;
306
    default :
307 308 309
      ret = 0;
  }

310
  if (lpstrInitialDir != (LPCSTR)-1)
Alexandre Julliard's avatar
Alexandre Julliard committed
311
  {
312 313
    MemFree((LPVOID)(ofn->lpstrInitialDir));
    ofn->lpstrInitialDir = lpstrInitialDir;
Alexandre Julliard's avatar
Alexandre Julliard committed
314
  }
315

Alexandre Julliard's avatar
Alexandre Julliard committed
316 317
  ofn->Flags = dwFlags;
  ofn->hInstance = hInstance;
318 319 320 321 322 323 324 325
  MemFree((LPVOID)(fodInfos));
  return ret;
}

/***********************************************************************
 *      GetFileDialog95W
 *
 * Copy the OPENFILENAMEW structure in a FileOpenDlgInfos structure.
326
 * Call GetFileName95 with this structure and clean the memory.
327
 *
328
 * FIXME: lpstrCustomFilter has to converted back
Alexandre Julliard's avatar
Alexandre Julliard committed
329
 *
330
 */
331 332 333 334 335

/* converting IN arguments */
#define AllocInArgWtoA(arg, save) \
  if(arg) \
  { \
336
    DWORD _len = WideCharToMultiByte( CP_ACP, 0, arg, -1, NULL, 0, NULL, NULL ); \
337
    save = arg; \
338 339
    arg =  MemAlloc(_len); \
    WideCharToMultiByte( CP_ACP, 0, save, -1, (LPSTR)arg, _len, NULL, NULL ); \
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
  }

#define FreeInArg(arg, save) \
  if(arg) \
  { \
    MemFree((LPSTR)arg); \
    arg = save; \
  }

/* converting OUT arguments */
#define AllocOutArgWtoA(arg, save, len) \
  if(arg) \
  { \
    save = arg; \
    arg = MemAlloc(len); \
  }

#define FreeOutArg(arg, save, len) \
  if(arg) \
  { \
360
    MultiByteToWideChar( CP_ACP, 0, (LPCSTR)(arg), -1, (save), (len) ); \
361 362 363 364
    MemFree(arg); \
    arg = save; \
  }

365 366 367 368
BOOL  WINAPI GetFileDialog95W(LPOPENFILENAMEW ofn,UINT iDlgType)
{
  BOOL ret;
  FileOpenDlgInfos *fodInfos;
Alexandre Julliard's avatar
Alexandre Julliard committed
369
  HINSTANCE hInstance;
370 371
  
  /* out arguments */
372
  LPWSTR lpstrFile = NULL;
373 374 375 376 377 378 379 380 381 382 383
  LPWSTR lpstrFileTitle = NULL;

  /* in/out arguments */
  LPWSTR lpstrCustomFilter = NULL;

  /* input arguments */
  LPCWSTR lpstrFilter = NULL;
  LPCWSTR lpstrInitialDir = NULL;
  LPCWSTR lpstrTitle = NULL;
  LPCWSTR lpstrDefExt = NULL;
  LPCWSTR lpTemplateName = NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
384
  DWORD dwFlags;
385 386 387

  /* Initialise FileOpenDlgInfos structure*/
  fodInfos = (FileOpenDlgInfos*)MemAlloc(sizeof(FileOpenDlgInfos));
388
  ZeroMemory(fodInfos, sizeof(FileOpenDlgInfos));
Alexandre Julliard's avatar
Alexandre Julliard committed
389 390 391 392

  /*  Pass in the original ofn */
  fodInfos->ofnInfos = (LPOPENFILENAMEA) ofn;

393
  /* convert lpstrFilter */
394 395
  if (ofn->lpstrFilter)
  {
396
    LPCWSTR  s;
397 398
    LPSTR y;
    int n, len;
399

400
    lpstrFilter = ofn->lpstrFilter;
Alexandre Julliard's avatar
Alexandre Julliard committed
401

402
    /* filter is a list...  title\0ext\0......\0\0 */
403
    s = ofn->lpstrFilter;
404
    
405
    while (*s) s = s+strlenW(s)+1;
406 407
    s++;
    n = s - ofn->lpstrFilter; /* already divides by 2. ptr magic */
408 409 410
    len = WideCharToMultiByte( CP_ACP, 0, ofn->lpstrFilter, n, NULL, 0, NULL, NULL );
    y = (LPSTR)MemAlloc(len);
    WideCharToMultiByte( CP_ACP, 0, ofn->lpstrFilter, n, y, len, NULL, NULL );
411
    (LPSTR)ofn->lpstrFilter = y;
412
  }
413 414

  /* convert lpstrCustomFilter */
Alexandre Julliard's avatar
Alexandre Julliard committed
415 416
  if (ofn->lpstrCustomFilter)
  {
417
    LPWSTR  s;
418 419
    LPSTR y;
    int n, len;
420

421
    lpstrCustomFilter = ofn->lpstrCustomFilter;
422
    /* filter is a list...  title\0ext\0......\0\0 */
423
    s = ofn->lpstrCustomFilter;
424
    while (*s) s = s+strlenW(s)+1;
425 426
    s++;
    n = s - ofn->lpstrCustomFilter;
427 428 429
    len = WideCharToMultiByte( CP_ACP, 0, ofn->lpstrCustomFilter, n, NULL, 0, NULL, NULL );
    y = (LPSTR)MemAlloc(len);
    WideCharToMultiByte( CP_ACP, 0, ofn->lpstrCustomFilter, n, y, len, NULL, NULL );
430
    (LPSTR)ofn->lpstrCustomFilter = y;
431
  }
432

433 434 435 436 437 438 439 440 441 442 443 444
  /* convert string arguments, save others */
  AllocOutArgWtoA(ofn->lpstrFile, lpstrFile, ofn->nMaxFile);
  AllocOutArgWtoA(ofn->lpstrFileTitle, lpstrFileTitle, ofn->nMaxFileTitle);
  AllocInArgWtoA(ofn->lpstrInitialDir, lpstrInitialDir);
  AllocInArgWtoA(ofn->lpstrTitle, lpstrTitle);
  AllocInArgWtoA(ofn->lpstrDefExt, lpstrDefExt);
  AllocInArgWtoA(ofn->lpTemplateName, lpTemplateName);
  dwFlags = ofn->Flags;
  hInstance = ofn->hInstance;

  ofn->Flags = ofn->Flags|OFN_WINE|OFN_UNICODE;
  ofn->hInstance = MapHModuleLS(ofn->hInstance);
Alexandre Julliard's avatar
Alexandre Julliard committed
445

446 447 448
  switch(iDlgType)
  {
  case OPEN_DIALOG :
449
      ret = GetFileName95(fodInfos);
450 451
      break;
  case SAVE_DIALOG :
452
      fodInfos->DlgInfos.dwDlgProp |= FODPROP_SAVEDLG;
453
      ret = GetFileName95(fodInfos);
454 455 456 457 458
      break;
  default :
      ret = 0;
  }
      
459 460 461 462 463 464 465 466 467 468 469
  /* restore saved IN arguments and convert OUT arguments back */
  ofn->Flags = dwFlags;
  ofn->hInstance = hInstance;
  FreeInArg(ofn->lpstrFilter, lpstrFilter);
  FreeInArg(ofn->lpstrCustomFilter, lpstrCustomFilter);
  FreeOutArg(ofn->lpstrFile, lpstrFile, ofn->nMaxFile);
  FreeOutArg(ofn->lpstrFileTitle, lpstrFileTitle, ofn->nMaxFileTitle);
  FreeInArg(ofn->lpstrInitialDir, lpstrInitialDir);
  FreeInArg(ofn->lpstrTitle, lpstrTitle);
  FreeInArg(ofn->lpstrDefExt, lpstrDefExt);
  FreeInArg(ofn->lpTemplateName, lpTemplateName);
Alexandre Julliard's avatar
Alexandre Julliard committed
470

471 472 473 474
  MemFree((LPVOID)(fodInfos));
  return ret;
}

475 476
void ArrangeCtrlPositions( HWND hwndChildDlg, HWND hwndParentDlg)
{
477 478 479 480
    HWND hwndChild,hwndStc32;
    RECT rectParent, rectChild, rectCtrl, rectStc32, rectTemp;
    POINT ptMoveCtl;
    POINT ptParentClient;
481

482
    TRACE("\n");
483

484 485 486 487 488 489 490 491 492 493 494 495 496
    ptMoveCtl.x = ptMoveCtl.y = 0;
    hwndStc32=GetDlgItem(hwndChildDlg,stc32);
    GetClientRect(hwndParentDlg,&rectParent);
    GetClientRect(hwndChildDlg,&rectChild);

    if(hwndStc32)
    {
      GetWindowRect(hwndStc32,&rectStc32);
      MapWindowPoints(0, hwndChildDlg,(LPPOINT)&rectStc32,2);
      CopyRect(&rectTemp,&rectStc32);

      SetRect(&rectStc32,rectStc32.left,rectStc32.top,rectStc32.left + (rectParent.right-rectParent.left),rectStc32.top+(rectParent.bottom-rectParent.top));
      SetWindowPos(hwndStc32,0,rectStc32.left,rectStc32.top,rectStc32.right-rectStc32.left,rectStc32.bottom-rectStc32.top,SWP_NOMOVE|SWP_NOZORDER | SWP_NOACTIVATE);
497

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
      if(rectStc32.right < rectTemp.right)
      {
        ptParentClient.x = max((rectParent.right-rectParent.left),(rectChild.right-rectChild.left));
        ptMoveCtl.x = 0;
      }
      else
      {
        ptMoveCtl.x = (rectStc32.right - rectTemp.right);
        ptParentClient.x = max((rectParent.right-rectParent.left),((rectChild.right-rectChild.left)+rectStc32.right-rectTemp.right));
      }

      if(rectStc32.bottom < rectTemp.bottom)
      {
        ptParentClient.y = max((rectParent.bottom-rectParent.top),(rectChild.bottom-rectChild.top));
        ptMoveCtl.y = 0;
      }
      else
      {
        ptMoveCtl.y = (rectStc32.bottom - rectTemp.bottom);
        ptParentClient.y = max((rectParent.bottom-rectParent.top),((rectChild.bottom-rectChild.top)+rectStc32.bottom-rectTemp.bottom));
      }
    }
    else
    {
      if( (GetWindow(hwndChildDlg,GW_CHILD)) == (HWND) NULL) return;

      SetRectEmpty(&rectTemp);
      ptParentClient.x = max((rectParent.right-rectParent.left),(rectChild.right-rectChild.left));
      ptParentClient.y = (rectParent.bottom-rectParent.top) + (rectChild.bottom-rectChild.top);
      ptMoveCtl.y = rectParent.bottom-rectParent.top;
      ptMoveCtl.x=0;
    }
    SetRect(&rectParent,rectParent.left,rectParent.top,rectParent.left+ptParentClient.x,rectParent.top+ptParentClient.y);
    AdjustWindowRectEx( &rectParent,GetWindowLongA(hwndParentDlg,GWL_STYLE),FALSE,GetWindowLongA(hwndParentDlg,GWL_EXSTYLE));

    SetWindowPos(hwndChildDlg, 0, 0,0, ptParentClient.x,ptParentClient.y, SWP_NOZORDER );
    SetWindowPos(hwndParentDlg, 0, rectParent.left,rectParent.top, (rectParent.right- rectParent.left),
        (rectParent.bottom-rectParent.top),SWP_NOMOVE | SWP_NOZORDER);
536
	
537 538 539 540 541 542 543 544
    hwndChild = GetWindow(hwndChildDlg,GW_CHILD);
    if(hwndStc32)
    {
      GetWindowRect(hwndStc32,&rectStc32);
      MapWindowPoints( 0, hwndChildDlg,(LPPOINT)&rectStc32,2);
    }
    else
      SetRect(&rectStc32,0,0,0,0);
545

546 547 548 549 550 551 552
    if (hwndChild )
    {
      do
      {
        if(hwndChild != hwndStc32)
        {
          if (GetWindowLongA( hwndChild, GWL_STYLE ) & WS_MAXIMIZE)
553
				continue;
554 555
          GetWindowRect(hwndChild,&rectCtrl);
          MapWindowPoints( 0, hwndParentDlg,(LPPOINT)&rectCtrl,2);
556
                                  
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
          /*
            Check the initial position of the controls relative to the initial
            position and size of stc32 (before it is expanded).
          */
          if (rectCtrl.left >= rectTemp.right && rectCtrl.top >= rectTemp.bottom)
          {
            rectCtrl.left += ptMoveCtl.x;
            rectCtrl.top  += ptMoveCtl.y;
          }
          else if (rectCtrl.left >= rectTemp.right)
	  {
            rectCtrl.left += ptMoveCtl.x;
	  }
          else if (rectCtrl.top >= rectTemp.bottom)
          {
	    rectCtrl.top  += ptMoveCtl.y;
	  }
574
					
575
          SetWindowPos( hwndChild, 0, rectCtrl.left, rectCtrl.top, 
576 577
				rectCtrl.right-rectCtrl.left,rectCtrl.bottom-rectCtrl.top,
				SWP_NOSIZE | SWP_NOZORDER );
578 579 580 581
        }
      } while ((hwndChild=GetWindow( hwndChild, GW_HWNDNEXT )) != (HWND)NULL);
    }
    hwndChild = GetWindow(hwndParentDlg,GW_CHILD);
582
	
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
    if(hwndStc32)
    {
      GetWindowRect(hwndStc32,&rectStc32);
      MapWindowPoints( 0, hwndChildDlg,(LPPOINT)&rectStc32,2);
      ptMoveCtl.x = rectStc32.left - 0;
      ptMoveCtl.y = rectStc32.top - 0;
      if (hwndChild )
      {
        do
        {
          if(hwndChild != hwndChildDlg)
          {
            if (GetWindowLongA( hwndChild, GWL_STYLE ) & WS_MAXIMIZE)
              continue;
            GetWindowRect(hwndChild,&rectCtrl);
            MapWindowPoints( 0, hwndParentDlg,(LPPOINT)&rectCtrl,2);

            rectCtrl.left += ptMoveCtl.x;
            rectCtrl.top += ptMoveCtl.y;
602

603 604 605 606 607 608 609
            SetWindowPos( hwndChild, 0, rectCtrl.left, rectCtrl.top, 
                rectCtrl.right-rectCtrl.left,rectCtrl.bottom-rectCtrl.top,
                SWP_NOSIZE |SWP_NOZORDER );
          }
        } while ((hwndChild=GetWindow( hwndChild, GW_HWNDNEXT )) != (HWND)NULL);
      }		
    }
610 611 612 613 614
}


HRESULT WINAPI FileOpenDlgProcUserTemplate(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
615
    FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(GetParent(hwnd),FileOpenDlgInfosStr);
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

#if 0
    TRACE("0x%04x\n", uMsg);
#endif

    switch(uMsg)
    {
      case WM_INITDIALOG:
      {         
        fodInfos = (FileOpenDlgInfos *)lParam;
        lParam = (LPARAM) fodInfos->ofnInfos;
        ArrangeCtrlPositions(hwnd,GetParent(hwnd));

        if(fodInfos && IsHooked(fodInfos))
          return CallWindowProcA((WNDPROC)fodInfos->ofnInfos->lpfnHook,hwnd,uMsg,wParam,lParam);
        return 0;	
      } 
Alexandre Julliard's avatar
Alexandre Julliard committed
633
    }
634 635 636 637 638

    if(fodInfos && IsHooked(fodInfos))
      return CallWindowProcA((WNDPROC)fodInfos->ofnInfos->lpfnHook,hwnd,uMsg,wParam,lParam); 

    return DefWindowProcA(hwnd,uMsg,wParam,lParam); 
639 640
}

641
HWND CreateTemplateDialog(FileOpenDlgInfos *fodInfos, HWND hwnd)
642 643 644 645 646
{
    LPCVOID template;
    HRSRC hRes;
    HANDLE hDlgTmpl = 0;
    HWND hChildDlg = 0;
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673

    TRACE("\n");

    if (fodInfos->ofnInfos->Flags & OFN_ENABLETEMPLATE ||
        fodInfos->ofnInfos->Flags & OFN_ENABLETEMPLATEHANDLE)
    {
      if (fodInfos->ofnInfos->Flags  & OFN_ENABLETEMPLATEHANDLE)
      {
        if( !(template = LockResource( fodInfos->ofnInfos->hInstance)))
        {
          COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
          return (HWND)NULL;
        }
      }
      else
      {
        if (!(hRes = FindResourceA(MapHModuleSL(fodInfos->ofnInfos->hInstance),
             (fodInfos->ofnInfos->lpTemplateName), RT_DIALOGA)))
        {
          COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
          return (HWND)NULL;
        }
        if (!(hDlgTmpl = LoadResource( MapHModuleSL(fodInfos->ofnInfos->hInstance),
             hRes )) || !(template = LockResource( hDlgTmpl )))
        {
          COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
          return (HWND)NULL;
674
    	}
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
      }

      hChildDlg= CreateDialogIndirectParamA(fodInfos->ofnInfos->hInstance,template,hwnd,(DLGPROC)FileOpenDlgProcUserTemplate,(LPARAM)fodInfos);
      if(hChildDlg)
      {
        ShowWindow(hChildDlg,SW_SHOW); 
        return hChildDlg;
      }
    }
    else if( IsHooked(fodInfos))
    {
      RECT rectHwnd;
      DLGTEMPLATE tmplate;
      GetClientRect(hwnd,&rectHwnd);
      tmplate.style = WS_CHILD | WS_CLIPSIBLINGS;
      tmplate.dwExtendedStyle = 0;
      tmplate.cdit = 0;
      tmplate.x = 0;
      tmplate.y = 0;
      tmplate.cx = rectHwnd.right-rectHwnd.left;
      tmplate.cy = rectHwnd.bottom-rectHwnd.top;
696
       
697 698 699
      return CreateDialogIndirectParamA(fodInfos->ofnInfos->hInstance,&tmplate,hwnd,(DLGPROC)FileOpenDlgProcUserTemplate,(LPARAM)fodInfos);
    }
    return (HWND)NULL;
700
}
Alexandre Julliard's avatar
Alexandre Julliard committed
701
 
702 703 704 705 706 707 708 709 710
/***********************************************************************
*          SendCustomDlgNotificationMessage
*
* Send CustomDialogNotification (CDN_FIRST -- CDN_LAST) message to the custom template dialog
*/

HRESULT SendCustomDlgNotificationMessage(HWND hwndParentDlg, UINT uCode)
{
    FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwndParentDlg,FileOpenDlgInfosStr);
711 712 713 714 715 716 717 718

    TRACE("0x%04x 0x%04x\n",hwndParentDlg, uCode);

    if(!fodInfos) return 0;

    if(fodInfos->ofnInfos->Flags & OFN_UNICODE)
      FIXME("sending OPENFILENAMEA structure. Hook is expecting OPENFILENAMEW!");

719 720 721
    if(fodInfos->DlgInfos.hwndCustomDlg)
    {
        OFNOTIFYA ofnNotify;
722
	HRESULT ret;
723 724 725
        ofnNotify.hdr.hwndFrom=hwndParentDlg;
        ofnNotify.hdr.idFrom=0;
        ofnNotify.hdr.code = uCode;
Alexandre Julliard's avatar
Alexandre Julliard committed
726
        ofnNotify.lpOFN = fodInfos->ofnInfos;
727 728 729 730
	TRACE("CALL NOTIFY for %x\n", uCode);
	ret = SendMessageA(fodInfos->DlgInfos.hwndCustomDlg,WM_NOTIFY,0,(LPARAM)&ofnNotify);
	TRACE("RET NOTIFY\n");
	return ret;
731 732 733 734 735 736 737 738 739 740 741 742
    }
    return TRUE;
}

/***********************************************************************
*         FILEDLG95_HandleCustomDialogMessages
*
* Handle Custom Dialog Messages (CDM_FIRST -- CDM_LAST) messages
*/
HRESULT FILEDLG95_HandleCustomDialogMessages(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    LPSTR lpstrFileSpec;
743 744
    int reqSize;
    char lpstrPath[MAX_PATH];
745
    FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
Alexandre Julliard's avatar
Alexandre Julliard committed
746 747
    if(!fodInfos) return -1;

748 749 750
    switch(uMsg)
    {
        case CDM_GETFILEPATH:
751
            GetDlgItemTextA(hwnd,IDC_FILENAME,lpstrPath, sizeof(lpstrPath));
752
            lpstrFileSpec = (LPSTR)COMDLG32_PathFindFileNameA(lpstrPath);
Alexandre Julliard's avatar
Alexandre Julliard committed
753 754
            if (lpstrFileSpec==lpstrPath) 
	    {
755 756 757 758
                char lpstrCurrentDir[MAX_PATH];
                /* Prepend the current path */
                COMDLG32_SHGetPathFromIDListA(fodInfos->ShellInfos.pidlAbsCurrent,lpstrCurrentDir);
                if ((LPSTR)lParam!=NULL)
759
                    snprintf((LPSTR)lParam,(int)wParam,"%s\\%s",lpstrCurrentDir,lpstrPath);
760
                reqSize=strlen(lpstrCurrentDir)+1+strlen(lpstrPath)+1;
Alexandre Julliard's avatar
Alexandre Julliard committed
761 762 763
            } 
	    else 
	    {
764 765
                lstrcpynA((LPSTR)lParam,(LPSTR)lpstrPath,(int)wParam);
                reqSize=strlen(lpstrPath);
766
            }
767 768
            /* return the required buffer size */
            return reqSize;
Alexandre Julliard's avatar
Alexandre Julliard committed
769

770
        case CDM_GETFOLDERPATH:
Alexandre Julliard's avatar
Alexandre Julliard committed
771
	    COMDLG32_SHGetPathFromIDListA(fodInfos->ShellInfos.pidlAbsCurrent,lpstrPath);
772 773 774
            if ((LPSTR)lParam!=NULL)
                lstrcpynA((LPSTR)lParam,lpstrPath,(int)wParam);
            return strlen(lpstrPath);
Alexandre Julliard's avatar
Alexandre Julliard committed
775 776 777

        case CDM_GETSPEC:
	    reqSize=GetDlgItemTextA(hwnd,IDC_FILENAME,lpstrPath, sizeof(lpstrPath));
778
            lpstrFileSpec = (LPSTR)COMDLG32_PathFindFileNameA(lpstrPath);
779 780 781
            if ((LPSTR)lParam!=NULL)
                lstrcpynA((LPSTR)lParam, lpstrFileSpec, (int)wParam);
            return strlen(lpstrFileSpec);
Alexandre Julliard's avatar
Alexandre Julliard committed
782

783
        case CDM_SETCONTROLTEXT:
Alexandre Julliard's avatar
Alexandre Julliard committed
784 785 786 787 788
	    if ( 0 != lParam )
	        SetDlgItemTextA( hwnd, (UINT) wParam, (LPSTR) lParam );
	    return TRUE;

        case CDM_HIDECONTROL:
789
        case CDM_SETDEFEXT:
Alexandre Julliard's avatar
Alexandre Julliard committed
790 791
            FIXME("CDM_HIDECONTROL,CDM_SETCONTROLTEXT,CDM_SETDEFEXT not implemented\n");
            return -1;
792
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
793
    return TRUE;
794 795
}
 
796 797 798 799 800 801 802
/***********************************************************************
 *          FileOpenDlgProc95
 *
 * File open dialog procedure
 */
HRESULT WINAPI FileOpenDlgProc95(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
803 804 805 806
#if 0
  TRACE("0x%04x 0x%04x\n", hwnd, uMsg);
#endif
  
807 808
  switch(uMsg)
  {
809
    case WM_INITDIALOG:
810 811 812 813
      {
         FileOpenDlgInfos * fodInfos = (FileOpenDlgInfos *)lParam;

	 /* Adds the FileOpenDlgInfos in the property list of the dialog 
814
            so it will be easily accessible through a GetPropA(...) */
815 816 817 818
      	 SetPropA(hwnd, FileOpenDlgInfosStr, (HANDLE) fodInfos);

      	 fodInfos->DlgInfos.hwndCustomDlg = 
     	   CreateTemplateDialog((FileOpenDlgInfos *)lParam, hwnd);
819

820
         FILEDLG95_InitControls(hwnd);
821
         SendCustomDlgNotificationMessage(hwnd,CDN_INITDONE);
822
      	 FILEDLG95_FillControls(hwnd, wParam, lParam);
823
         SendCustomDlgNotificationMessage(hwnd,CDN_SELCHANGE);
824
         return 0;
825
       }
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
    case WM_COMMAND:
      return FILEDLG95_OnWMCommand(hwnd, wParam, lParam);
    case WM_DRAWITEM:
      {
        switch(((LPDRAWITEMSTRUCT)lParam)->CtlID)
        {
        case IDC_LOOKIN:
          FILEDLG95_LOOKIN_DrawItem((LPDRAWITEMSTRUCT) lParam);
          return TRUE;
        }
      }
      return FALSE;
          
    case WM_GETISHELLBROWSER:
      return FILEDLG95_OnWMGetIShellBrowser(hwnd);

842
    case WM_DESTROY:
843
      RemovePropA(hwnd, FileOpenDlgInfosStr);
844
      return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
845 846 847 848 849

    case WM_NOTIFY:
    {
	LPNMHDR lpnmh = (LPNMHDR)lParam;
	UINT stringId = -1;
850

Alexandre Julliard's avatar
Alexandre Julliard committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
	/* set up the button tooltips strings */
	if(TTN_GETDISPINFOA == lpnmh->code )
	{
	    LPNMTTDISPINFOA lpdi = (LPNMTTDISPINFOA)lParam; 
	    switch(lpnmh->idFrom )
	    {
		/* Up folder button */
		case FCIDM_TB_UPFOLDER:
		    stringId = IDS_UPFOLDER;
		    break;
		/* New folder button */
		case FCIDM_TB_NEWFOLDER:
		    stringId = IDS_NEWFOLDER;
		    break;
		/* List option button */
		case FCIDM_TB_SMALLICON:
		    stringId = IDS_LISTVIEW;
		    break;
		/* Details option button */
		case FCIDM_TB_REPORTVIEW:
		    stringId = IDS_REPORTVIEW;
		    break;
Juergen Schmied's avatar
Juergen Schmied committed
873 874 875 876 877 878
		/* Desktop button */
		case FCIDM_TB_DESKTOP:
		    stringId = IDS_TODESKTOP;
		    break;
		default:
		    stringId = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
879 880 881 882 883 884
	    }
	    lpdi->hinst = COMMDLG_hInstance32; 
	    lpdi->lpszText =  (LPSTR) stringId;
	}
        return FALSE;
    }
885
    default :
886 887
      if(uMsg >= CDM_FIRST && uMsg <= CDM_LAST)
        return FILEDLG95_HandleCustomDialogMessages(hwnd, uMsg, wParam, lParam);
888 889 890 891 892
      return FALSE;
  }
}

/***********************************************************************
893
 *      FILEDLG95_InitControls
894
 *
895
 * WM_INITDIALOG message handler (before hook notification)
896
 */
897
static LRESULT FILEDLG95_InitControls(HWND hwnd)
898
{
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
  TBBUTTON tbb[] =
  {
   {0,                 0,                   TBSTATE_ENABLED, TBSTYLE_SEP, {0, 0}, 0, 0 },
   {VIEW_PARENTFOLDER, FCIDM_TB_UPFOLDER,   TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
   {0,                 0,                   TBSTATE_ENABLED, TBSTYLE_SEP, {0, 0}, 0, 0 },
   {VIEW_NEWFOLDER+1,  FCIDM_TB_DESKTOP,    TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
   {0,                 0,                   TBSTATE_ENABLED, TBSTYLE_SEP, {0, 0}, 0, 0 },
   {VIEW_NEWFOLDER,    FCIDM_TB_NEWFOLDER,  TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
   {0,                 0,                   TBSTATE_ENABLED, TBSTYLE_SEP, {0, 0}, 0, 0 },
   {VIEW_LIST,         FCIDM_TB_SMALLICON,  TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
   {VIEW_DETAILS,      FCIDM_TB_REPORTVIEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0, 0}, 0, 0 },
  };
  TBADDBITMAP tba[] =
  {
   { HINST_COMMCTRL, IDB_VIEW_SMALL_COLOR },
   { COMDLG32_hInstance, 800 } 			// desktop icon
  };
  
  RECT rectTB;
  
  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);

  TRACE("%p\n", fodInfos);

  /* Get the hwnd of the controls */
  fodInfos->DlgInfos.hwndFileName = GetDlgItem(hwnd,IDC_FILENAME);
  fodInfos->DlgInfos.hwndFileTypeCB = GetDlgItem(hwnd,IDC_FILETYPE);
  fodInfos->DlgInfos.hwndLookInCB = GetDlgItem(hwnd,IDC_LOOKIN);

  /* construct the toolbar */
  GetWindowRect(GetDlgItem(hwnd,IDC_TOOLBARSTATIC),&rectTB);
  MapWindowPoints( 0, hwnd,(LPPOINT)&rectTB,2);

  fodInfos->DlgInfos.hwndTB = CreateWindowExA(0, TOOLBARCLASSNAMEA, (LPSTR) NULL, 
        WS_CHILD | WS_GROUP | TBSTYLE_TOOLTIPS | CCS_NODIVIDER | CCS_NORESIZE,
        0, 0, 150, 26, hwnd, (HMENU) IDC_TOOLBAR, COMMDLG_hInstance32, NULL); 
 
  SetWindowPos(fodInfos->DlgInfos.hwndTB, 0, 
  	rectTB.left,rectTB.top, rectTB.right-rectTB.left, rectTB.bottom-rectTB.top,
	SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_NOZORDER );

  SendMessageA(fodInfos->DlgInfos.hwndTB, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);

/* fixme: use TB_LOADIMAGES when implemented */
/*  SendMessageA(fodInfos->DlgInfos.hwndTB, TB_LOADIMAGES, (WPARAM) IDB_VIEW_SMALL_COLOR, HINST_COMMCTRL);*/
  SendMessageA(fodInfos->DlgInfos.hwndTB, TB_ADDBITMAP, (WPARAM) 12, (LPARAM) &tba[0]);
  SendMessageA(fodInfos->DlgInfos.hwndTB, TB_ADDBITMAP, (WPARAM) 1, (LPARAM) &tba[1]);

  SendMessageA(fodInfos->DlgInfos.hwndTB, TB_ADDBUTTONSA, (WPARAM) 9,(LPARAM) &tbb);
  SendMessageA(fodInfos->DlgInfos.hwndTB, TB_AUTOSIZE, 0, 0); 

  /* Set the window text with the text specified in the OPENFILENAME structure */
  if(fodInfos->ofnInfos->lpstrTitle)
  {
      SetWindowTextA(hwnd,fodInfos->ofnInfos->lpstrTitle);
  }
  else if (fodInfos->DlgInfos.dwDlgProp & FODPROP_SAVEDLG)
  {
      SetWindowTextA(hwnd,"Save");
  }

  /* Initialise the file name edit control */
  if(fodInfos->ofnInfos->lpstrFile)
  {
      LPSTR lpstrFile = COMDLG32_PathFindFileNameA(fodInfos->ofnInfos->lpstrFile);
      SetDlgItemTextA(hwnd, IDC_FILENAME, lpstrFile);
  }

  /* Must the open as read only check box be checked ?*/
  if(fodInfos->ofnInfos->Flags & OFN_READONLY)
  {
    SendDlgItemMessageA(hwnd,IDC_OPENREADONLY,BM_SETCHECK,(WPARAM)TRUE,0);
  }

  /* Must the open as read only check box be hid ?*/
  if(fodInfos->ofnInfos->Flags & OFN_HIDEREADONLY)
  {
    ShowWindow(GetDlgItem(hwnd,IDC_OPENREADONLY),SW_HIDE);
  }

  /* Must the help button be hid ?*/
  if (!(fodInfos->ofnInfos->Flags & OFN_SHOWHELP))
  {
    ShowWindow(GetDlgItem(hwnd, pshHelp), SW_HIDE);
  }

  /* Resize the height, if open as read only checkbox ad help button
     are hidden and we are not using a custom template */
  if ( (fodInfos->ofnInfos->Flags & OFN_HIDEREADONLY) &&
       (!(fodInfos->ofnInfos->Flags & 
         (OFN_SHOWHELP|OFN_ENABLETEMPLATE|OFN_ENABLETEMPLATEHANDLE))))
  {
    RECT rectDlg, rectHelp, rectCancel;
    GetWindowRect(hwnd, &rectDlg);
    GetWindowRect(GetDlgItem(hwnd, pshHelp), &rectHelp);
    GetWindowRect(GetDlgItem(hwnd, IDCANCEL), &rectCancel);
    /* subtract the height of the help button plus the space between
       the help button and the cancel button to the height of the dialog */
    SetWindowPos(hwnd, 0, 0, 0, rectDlg.right-rectDlg.left, 
                 (rectDlg.bottom-rectDlg.top) - (rectHelp.bottom - rectCancel.bottom), 
                 SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOZORDER);
  }

  /* change Open to Save FIXME: use resources */
  if (fodInfos->DlgInfos.dwDlgProp & FODPROP_SAVEDLG)
  {
      SetDlgItemTextA(hwnd,IDOK,"&Save");
      SetDlgItemTextA(hwnd,IDC_LOOKINSTATIC,"Save &in");
  }
  return 0;
}

/***********************************************************************
 *      FILEDLG95_FillControls
 *
 * WM_INITDIALOG message handler (after hook notification)
 */
static LRESULT FILEDLG95_FillControls(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
  LPITEMIDLIST pidlItemId = NULL;
  
1020 1021
  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) lParam;

Juergen Schmied's avatar
Juergen Schmied committed
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
  TRACE("dir=%s file=%s\n", 
  fodInfos->ofnInfos->lpstrInitialDir, fodInfos->ofnInfos->lpstrFile);

  /* Get the initial directory pidl */

  if(!(pidlItemId = GetPidlFromName(fodInfos->Shell.FOIShellFolder,fodInfos->ofnInfos->lpstrInitialDir)))
  {
    char path[MAX_PATH];

    GetCurrentDirectoryA(MAX_PATH,path);
    pidlItemId = GetPidlFromName(fodInfos->Shell.FOIShellFolder, path);
  }
1034 1035 1036 1037

  /* Initialise shell objects */
  FILEDLG95_SHELL_Init(hwnd);

1038
  /* Initialize the Look In combo box */
1039 1040 1041 1042 1043 1044
  FILEDLG95_LOOKIN_Init(fodInfos->DlgInfos.hwndLookInCB);

  /* Initialize the filter combo box */
  FILEDLG95_FILETYPE_Init(hwnd);

  /* Browse to the initial directory */
1045
  IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser,pidlItemId, SBSP_ABSOLUTE);
1046 1047

  /* Free pidlItem memory */
1048
  COMDLG32_SHFree(pidlItemId);
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069

  return TRUE;
}
/***********************************************************************
 *      FILEDLG95_Clean
 *
 * Regroups all the cleaning functions of the filedlg
 */
void FILEDLG95_Clean(HWND hwnd)
{
      FILEDLG95_FILETYPE_Clean(hwnd);
      FILEDLG95_LOOKIN_Clean(hwnd);
      FILEDLG95_SHELL_Clean(hwnd);
}
/***********************************************************************
 *      FILEDLG95_OnWMCommand
 *
 * WM_COMMAND message handler
 */
static LRESULT FILEDLG95_OnWMCommand(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
1070 1071
  WORD wNotifyCode = HIWORD(wParam); /* notification code */
  WORD wID = LOWORD(wParam);         /* item, control, or accelerator identifier */
1072
  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
1073 1074 1075 1076 1077

  switch(wID)
  {
    /* OK button */
  case IDOK:
1078 1079
      if(FILEDLG95_OnOpen(hwnd))
        SendCustomDlgNotificationMessage(hwnd,CDN_FILEOK);
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
    break;
    /* Cancel button */
  case IDCANCEL:
      FILEDLG95_Clean(hwnd);
      EndDialog(hwnd, FALSE);
    break;
    /* Filetype combo box */
  case IDC_FILETYPE:
    FILEDLG95_FILETYPE_OnCommand(hwnd,wNotifyCode);
    break;
    /* LookIn combo box */
  case IDC_LOOKIN:
    FILEDLG95_LOOKIN_OnCommand(hwnd,wNotifyCode);
    break;
1094 1095

  /* --- toolbar --- */
1096
    /* Up folder button */
1097
  case FCIDM_TB_UPFOLDER:
1098 1099
    FILEDLG95_SHELL_UpFolder(hwnd);
    break;
1100 1101
    /* New folder button */
  case FCIDM_TB_NEWFOLDER:
Alexandre Julliard's avatar
Alexandre Julliard committed
1102
    FILEDLG95_SHELL_ExecuteCommand(hwnd,CMDSTR_NEWFOLDER);
1103
    break;
1104
    /* List option button */
1105
  case FCIDM_TB_SMALLICON:
1106 1107 1108
    FILEDLG95_SHELL_ExecuteCommand(hwnd,CMDSTR_VIEWLIST);
    break;
    /* Details option button */
1109
  case FCIDM_TB_REPORTVIEW:
1110 1111
    FILEDLG95_SHELL_ExecuteCommand(hwnd,CMDSTR_VIEWDETAILS);
    break;
Juergen Schmied's avatar
Juergen Schmied committed
1112 1113 1114 1115
    /* Details option button */
  case FCIDM_TB_DESKTOP:
    FILEDLG95_SHELL_BrowseToDesktop(hwnd);
    break;
1116 1117 1118 1119 1120

  case IDC_FILENAME:
    break;

  }
1121 1122
  /* Do not use the listview selection anymore */
  fodInfos->DlgInfos.dwDlgProp &= ~FODPROP_USEVIEW;
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
  return 0; 
}

/***********************************************************************
 *      FILEDLG95_OnWMGetIShellBrowser
 *
 * WM_GETISHELLBROWSER message handler
 */
static LRESULT FILEDLG95_OnWMGetIShellBrowser(HWND hwnd)
{

  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);

  TRACE("\n");

  SetWindowLongA(hwnd,DWL_MSGRESULT,(LONG)fodInfos->Shell.FOIShellBrowser);

  return TRUE; 
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1144 1145 1146 1147
/***********************************************************************
 *      FILEDLG95_OnOpenMultipleFiles
 *      
 * Handles the opening of multiple files.
1148 1149 1150 1151
 *
 * FIXME
 *  check destination buffer size
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1152 1153 1154 1155
BOOL FILEDLG95_OnOpenMultipleFiles(HWND hwnd, LPSTR lpstrFileList, UINT nFileCount, UINT sizeUsed)
{
  CHAR   lpstrPathSpec[MAX_PATH] = "";
  LPSTR  lpstrFile;
1156
  UINT   nCount, nSizePath;
Alexandre Julliard's avatar
Alexandre Julliard committed
1157 1158 1159 1160 1161
  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);

  TRACE("\n");

  lpstrFile = fodInfos->ofnInfos->lpstrFile;
1162 1163
  lpstrFile[0] = '\0';
  
Alexandre Julliard's avatar
Alexandre Julliard committed
1164 1165
  COMDLG32_SHGetPathFromIDListA( fodInfos->ShellInfos.pidlAbsCurrent, lpstrPathSpec );

1166 1167 1168 1169
  if ( !(fodInfos->ofnInfos->Flags & OFN_NOVALIDATE) &&
      ( fodInfos->ofnInfos->Flags & OFN_FILEMUSTEXIST))
  {
    LPSTR lpstrTemp = lpstrFileList; 
Alexandre Julliard's avatar
Alexandre Julliard committed
1170

1171 1172 1173
    for ( nCount = 0; nCount < nFileCount; nCount++ )
    {
      LPITEMIDLIST pidl;
Alexandre Julliard's avatar
Alexandre Julliard committed
1174

1175 1176
      pidl = GetPidlFromName(fodInfos->Shell.FOIShellFolder, lpstrTemp);
      if (!pidl)
Alexandre Julliard's avatar
Alexandre Julliard committed
1177
      {
1178 1179 1180
        CHAR lpstrNotFound[100];
        CHAR lpstrMsg[100];
        CHAR tmp[400];
Alexandre Julliard's avatar
Alexandre Julliard committed
1181

1182 1183
        LoadStringA(COMMDLG_hInstance32, IDS_FILENOTFOUND, lpstrNotFound, 100);
        LoadStringA(COMMDLG_hInstance32, IDS_VERIFYFILE, lpstrMsg, 100);
Alexandre Julliard's avatar
Alexandre Julliard committed
1184

1185 1186 1187 1188 1189
        strcpy(tmp, lpstrTemp);
        strcat(tmp, "\n");
        strcat(tmp, lpstrNotFound);
        strcat(tmp, "\n");
        strcat(tmp, lpstrMsg);
Alexandre Julliard's avatar
Alexandre Julliard committed
1190

1191 1192
        MessageBoxA(hwnd, tmp, fodInfos->ofnInfos->lpstrTitle, MB_OK | MB_ICONEXCLAMATION);
        return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1193
      }
1194
  
1195 1196
      /* move to the next file in the list of files */
      lpstrTemp += strlen(lpstrTemp) + 1;
1197 1198
      COMDLG32_SHFree(pidl);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1199 1200
  }

1201 1202
  nSizePath = strlen(lpstrPathSpec);
  strcpy( lpstrFile, lpstrPathSpec);
1203
  memcpy( lpstrFile + nSizePath + 1, lpstrFileList, sizeUsed );
Alexandre Julliard's avatar
Alexandre Julliard committed
1204

1205 1206
  fodInfos->ofnInfos->nFileOffset = nSizePath + 1;
  fodInfos->ofnInfos->nFileExtension = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1207 1208 1209 1210 1211 1212

  /* clean and exit */
  FILEDLG95_Clean(hwnd);
  return EndDialog(hwnd,TRUE);
}

1213 1214 1215 1216 1217 1218 1219
/***********************************************************************
 *      FILEDLG95_OnOpen
 *
 * Ok button WM_COMMAND message handler
 * 
 * If the function succeeds, the return value is nonzero.
 */
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
#define ONOPEN_BROWSE 1
#define ONOPEN_OPEN   2
#define ONOPEN_SEARCH 3
static void FILEDLG95_OnOpenMessage(HWND hwnd, int idCaption, int idText)
{
  char strMsgTitle[MAX_PATH];
  char strMsgText [MAX_PATH];
  if (idCaption)
    LoadStringA(COMDLG32_hInstance, idCaption, strMsgTitle, sizeof(strMsgTitle));
  else
    strMsgTitle[0] = '\0';
  LoadStringA(COMDLG32_hInstance, idText, strMsgText, sizeof(strMsgText));
  MessageBoxA(hwnd,strMsgText, strMsgTitle, MB_OK | MB_ICONHAND);
}

1235 1236
BOOL FILEDLG95_OnOpen(HWND hwnd)
{
1237
  char * lpstrFileList;
Alexandre Julliard's avatar
Alexandre Julliard committed
1238 1239
  UINT nFileCount = 0;
  UINT sizeUsed = 0;
1240 1241 1242 1243 1244
  BOOL ret = TRUE;
  char lpstrPathAndFile[MAX_PATH];
  char lpstrTemp[MAX_PATH];
  LPSHELLFOLDER lpsf = NULL;
  int nOpenAction;
1245 1246
  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);

1247
  TRACE("hwnd=0x%04x\n", hwnd);
Alexandre Julliard's avatar
Alexandre Julliard committed
1248

1249 1250
  /* get the files from the edit control */
  nFileCount = FILEDLG95_FILENAME_GetFileNames(hwnd, &lpstrFileList, &sizeUsed);
Alexandre Julliard's avatar
Alexandre Julliard committed
1251

1252 1253
  /* try if the user selected a folder in the shellview */
  if(nFileCount == 0)
1254
  {
1255
      BrowseSelectedFolder(hwnd);
1256 1257 1258 1259 1260 1261 1262
      return FALSE;
  }
 
  if(nFileCount > 1)
  {
      ret = FILEDLG95_OnOpenMultipleFiles(hwnd, lpstrFileList, nFileCount, sizeUsed);
      goto ret;
1263 1264
  }

1265
  TRACE("count=%u len=%u file=%s\n", nFileCount, sizeUsed, lpstrFileList);
Alexandre Julliard's avatar
Alexandre Julliard committed
1266

1267 1268 1269 1270 1271 1272 1273 1274
/*
  Step 1:  Build a complete path name from the current folder and
  the filename or path in the edit box.
  Special cases:
  - the path in the edit box is a root path
    (with or without drive letter)
  - the edit box contains ".." (or a path with ".." in it)
*/
1275

1276 1277 1278 1279
  /* Get the current directory name */
  if (!COMDLG32_SHGetPathFromIDListA(fodInfos->ShellInfos.pidlAbsCurrent, lpstrPathAndFile))
  {
    /* we are in a special folder, default to desktop */
1280
    if(FAILED(COMDLG32_SHGetFolderPathA(hwnd, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, 0, 0, lpstrPathAndFile)))
1281 1282 1283 1284
    {
      /* last fallback */
      GetCurrentDirectoryA(MAX_PATH, lpstrPathAndFile);
    }
1285 1286
  }
  COMDLG32_PathAddBackslashA(lpstrPathAndFile);
1287

1288
  TRACE("current directory=%s\n", lpstrPathAndFile);
1289

1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
  /* if the user specifyed a fully qualified path use it */
  if(COMDLG32_PathIsRelativeA(lpstrFileList))
  {
    strcat(lpstrPathAndFile, lpstrFileList);
  }
  else
  {
    /* does the path have a drive letter? */
    if (COMDLG32_PathGetDriveNumberA(lpstrFileList) == -1)
      strcpy(lpstrPathAndFile+2, lpstrFileList);
    else
      strcpy(lpstrPathAndFile, lpstrFileList);
  }
1303

1304 1305 1306 1307
  /* resolve "." and ".." */
  COMDLG32_PathCanonicalizeA(lpstrTemp, lpstrPathAndFile );
  strcpy(lpstrPathAndFile, lpstrTemp);
  TRACE("canon=%s\n", lpstrPathAndFile);
1308

1309
  MemFree(lpstrFileList);
1310

1311 1312
/*
  Step 2: here we have a cleaned up path
1313

1314 1315 1316 1317 1318 1319 1320
  We have to parse the path step by step to see if we have to browse
  to a folder if the path points to a directory or the last
  valid element is a directory.
  
  valid variables:
    lpstrPathAndFile: cleaned up path
 */
1321

1322
  nOpenAction = ONOPEN_BROWSE;
1323

1324 1325 1326 1327 1328
  /* dont apply any checks with OFN_NOVALIDATE */
  if(!(fodInfos->ofnInfos->Flags & OFN_NOVALIDATE))
  {
    LPSTR lpszTemp, lpszTemp1;
    LPITEMIDLIST pidl = NULL;
1329

1330 1331 1332 1333 1334 1335
    /* check for invalid chars */
    if(strpbrk(lpstrPathAndFile+3, "/:<>|") != NULL)
    {
      FILEDLG95_OnOpenMessage(hwnd, IDS_INVALID_FILENAME_TITLE, IDS_INVALID_FILENAME);
      ret = FALSE;
      goto ret;
1336 1337
    }

1338 1339 1340 1341 1342 1343 1344 1345 1346
    if (FAILED (COMDLG32_SHGetDesktopFolder(&lpsf))) return FALSE;
  
    lpszTemp1 = lpszTemp = lpstrPathAndFile;
    while (lpszTemp1)
    {
      LPSHELLFOLDER lpsfChild;
      WCHAR lpwstrTemp[MAX_PATH];
      DWORD dwEaten, dwAttributes;

1347
      lpszTemp = COMDLG32_PathFindNextComponentA(lpszTemp);
1348 1349 1350 1351

      if (!lpszTemp) break; /* end of path */

      if(*lpszTemp)
1352 1353 1354 1355 1356
      {
          DWORD len = MultiByteToWideChar( CP_ACP, 0, lpszTemp1, lpszTemp - lpszTemp1,
                                           lpwstrTemp, MAX_PATH );
          lpwstrTemp[len] = 0;
      }
1357
      else
Alexandre Julliard's avatar
Alexandre Julliard committed
1358
      {
1359 1360
          MultiByteToWideChar( CP_ACP, 0, lpszTemp1, -1,
                               lpwstrTemp, sizeof(lpwstrTemp)/sizeof(WCHAR) );
1361 1362

	/* if the last element is a wildcard do a search */
1363 1364
        if(strpbrk(lpszTemp1, "*?") != NULL)
        {
1365
	  nOpenAction = ONOPEN_SEARCH;
1366
	  break;
1367
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
1368
      }
1369
      lpszTemp1 = lpszTemp;
Alexandre Julliard's avatar
Alexandre Julliard committed
1370

1371
      TRACE("parse now=%s next=%s sf=%p\n",debugstr_w(lpwstrTemp), debugstr_a(lpszTemp), lpsf);
1372

1373
      if(lstrlenW(lpwstrTemp)==2) COMDLG32_PathAddBackslashW(lpwstrTemp);
1374

1375
      dwAttributes = SFGAO_FOLDER;
1376
      if(SUCCEEDED(IShellFolder_ParseDisplayName(lpsf, hwnd, NULL, lpwstrTemp, &dwEaten, &pidl, &dwAttributes)))
1377
      {
1378
        /* the path component is valid, we have a pidl of the next path component */
1379 1380
        TRACE("parse OK attr=0x%08lx pidl=%p\n", dwAttributes, pidl);
        if(dwAttributes & SFGAO_FOLDER)
1381
        {
1382 1383 1384 1385 1386 1387 1388 1389
          if(FAILED(IShellFolder_BindToObject(lpsf, pidl, 0, &IID_IShellFolder, (LPVOID*)&lpsfChild)))
          {
            ERR("bind to failed\n"); /* should not fail */
            break;
          }
          IShellFolder_Release(lpsf);
          lpsf = lpsfChild;
          lpsfChild = NULL;
1390
        }
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
        else
        {
          TRACE("value\n");

	  /* end dialog, return value */
          nOpenAction = ONOPEN_OPEN;
	  break;
        }
	COMDLG32_SHFree(pidl);
	pidl = NULL;
1401
      }
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
      else
      {
	if(*lpszTemp)	/* points to trailing null for last path element */
        {
	  if(fodInfos->ofnInfos->Flags & OFN_PATHMUSTEXIST)
	  {
            FILEDLG95_OnOpenMessage(hwnd, 0, IDS_PATHNOTEXISTING);
	    break;
	  }
	}
        else
	{
          if(fodInfos->ofnInfos->Flags & OFN_FILEMUSTEXIST)
	  {
            FILEDLG95_OnOpenMessage(hwnd, 0, IDS_FILENOTEXISTING);
	    break;
	  }
	}
	/* change to the current folder */
        nOpenAction = ONOPEN_OPEN;
	break;
      }
1424
    }
1425 1426
    if(pidl) COMDLG32_SHFree(pidl);
  }
1427

1428 1429
/*
  Step 3: here we have a cleaned up and validated path
1430

1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
  valid variables:
   lpsf:             ShellFolder bound to the rightmost valid path component
   lpstrPathAndFile: cleaned up path
   nOpenAction:      action to do
*/
  TRACE("end validate sf=%p\n", lpsf);

  switch(nOpenAction)
  {
    case ONOPEN_SEARCH:   /* set the current filter to the file mask and refresh */
      TRACE("ONOPEN_SEARCH %s\n", lpstrPathAndFile);
1442
      {
1443 1444
        int iPos;
        LPSTR lpszTemp = COMDLG32_PathFindFileNameA(lpstrPathAndFile);
1445
        DWORD len;
1446 1447 1448 1449

        /* replace the current filter */
        if(fodInfos->ShellInfos.lpstrCurrentFilter)
	  MemFree((LPVOID)fodInfos->ShellInfos.lpstrCurrentFilter);
1450 1451 1452 1453
        len = MultiByteToWideChar( CP_ACP, 0, lpszTemp, -1, NULL, 0 );
        fodInfos->ShellInfos.lpstrCurrentFilter = MemAlloc(len * sizeof(WCHAR));
        MultiByteToWideChar( CP_ACP, 0, lpszTemp, -1,
                             fodInfos->ShellInfos.lpstrCurrentFilter, len );
1454 1455 1456 1457

        /* set the filter cb to the extension when possible */
        if(-1 < (iPos = FILEDLG95_FILETYPE_SearchExt(fodInfos->DlgInfos.hwndFileTypeCB, lpszTemp)))
        CBSetCurSel(fodInfos->DlgInfos.hwndFileTypeCB, iPos);
1458
      }
1459 1460 1461
      /* fall through */
    case ONOPEN_BROWSE:   /* browse to the highest folder we could bind to */
      TRACE("ONOPEN_BROWSE\n");
1462
      {
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
	IPersistFolder2 * ppf2;
        if(SUCCEEDED(IShellFolder_QueryInterface( lpsf, &IID_IPersistFolder2, (LPVOID*)&ppf2)))
        {
          LPITEMIDLIST pidlCurrent;
          IPersistFolder2_GetCurFolder(ppf2, &pidlCurrent);
          IPersistFolder2_Release(ppf2);
	  if( ! COMDLG32_PIDL_ILIsEqual(pidlCurrent, fodInfos->ShellInfos.pidlAbsCurrent))
	  {
	    IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser, pidlCurrent, SBSP_ABSOLUTE);
	  }
	  else if( nOpenAction == ONOPEN_SEARCH )
	  {
            IShellView_Refresh(fodInfos->Shell.FOIShellView);
	  }
          COMDLG32_SHFree(pidlCurrent);
        }
1479
      }
1480 1481 1482 1483 1484 1485 1486
      ret = FALSE;
      break;
    case ONOPEN_OPEN:   /* fill in the return struct and close the dialog */
      TRACE("ONOPEN_OPEN %s\n", lpstrPathAndFile);
      {
	/* add default extension */
	if (fodInfos->ofnInfos->lpstrDefExt)
Juergen Schmied's avatar
Juergen Schmied committed
1487 1488 1489 1490 1491 1492 1493
	{
	  if (! *COMDLG32_PathFindExtensionA(lpstrPathAndFile))
	  {
	    strcat(lpstrPathAndFile, ".");
	    strcat(lpstrPathAndFile, fodInfos->ofnInfos->lpstrDefExt);
	  }
	}
1494

1495 1496 1497 1498
        /* Check that the size of the file does not exceed buffer size.
             (Allow for extra \0 if OFN_MULTISELECT is set.) */
        if(strlen(lpstrPathAndFile) < fodInfos->ofnInfos->nMaxFile -
            ((fodInfos->ofnInfos->Flags & OFN_ALLOWMULTISELECT) ? 1 : 0))
1499 1500 1501 1502 1503
        {
          LPSTR lpszTemp;
	  
          /* fill destination buffer */
          strcpy(fodInfos->ofnInfos->lpstrFile, lpstrPathAndFile);
1504 1505 1506
          if (fodInfos->ofnInfos->Flags & OFN_ALLOWMULTISELECT)
            fodInfos->ofnInfos->lpstrFile[strlen(fodInfos->ofnInfos->lpstrFile)
                + 1] = '\0';
1507

1508 1509 1510 1511 1512 1513
          /* set filename offset */
          lpszTemp = COMDLG32_PathFindFileNameA(lpstrPathAndFile);
          fodInfos->ofnInfos->nFileOffset = lpszTemp - lpstrPathAndFile;
 
          /* set extension offset */
          lpszTemp = COMDLG32_PathFindExtensionA(lpstrPathAndFile);
Juergen Schmied's avatar
Juergen Schmied committed
1514
          fodInfos->ofnInfos->nFileExtension = (*lpszTemp) ? lpszTemp - lpstrPathAndFile + 1 : 0;
1515
    
1516 1517 1518 1519 1520 1521 1522
          /* set the lpstrFileTitle */
          if(fodInfos->ofnInfos->lpstrFileTitle)
	  {
            LPSTR lpstrFileTitle = COMDLG32_PathFindFileNameA(lpstrPathAndFile);
	    strncpy(fodInfos->ofnInfos->lpstrFileTitle, lpstrFileTitle, fodInfos->ofnInfos->nMaxFileTitle);
	  }

1523 1524 1525 1526 1527 1528
          /* ask the hook if we can close */
          if(IsHooked(fodInfos))
	  {
	    /* FIXME we are sending ASCII-structures. Does not work with NT */
	    /* first old style */
	    TRACE("---\n");
1529 1530 1531
            CallWindowProcA((WNDPROC)fodInfos->ofnInfos->lpfnHook,
                            fodInfos->DlgInfos.hwndCustomDlg,
                            fodInfos->HookMsg.fileokstring, 0, (LPARAM)fodInfos->ofnInfos);
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
	    if (GetWindowLongA(hwnd, DWL_MSGRESULT))
	    {
	      TRACE("cancled\n");
	      ret = FALSE;
	      goto ret;
	    }
	  }

          TRACE("close\n");
	  FILEDLG95_Clean(hwnd);
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
          ret = EndDialog(hwnd, TRUE);
	}
	else
        {
          /* FIXME set error FNERR_BUFFERTOSMALL */
          FILEDLG95_Clean(hwnd);
          ret = EndDialog(hwnd, FALSE);
        }
        goto ret;
      }
      break;
1553 1554
  }

1555 1556 1557
ret:
  if(lpsf) IShellFolder_Release(lpsf);
  return ret;
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
}

/***********************************************************************
 *      FILEDLG95_SHELL_Init
 *
 * Initialisation of the shell objects
 */
static HRESULT FILEDLG95_SHELL_Init(HWND hwnd)
{
  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);

  TRACE("\n");

  /*
   * Initialisation of the FileOpenDialogInfos structure 
   */

  /* Shell */

  /*ShellInfos */
  fodInfos->ShellInfos.hwndOwner = hwnd;

Alexandre Julliard's avatar
Alexandre Julliard committed
1580 1581 1582 1583 1584 1585
  /* Disable multi-select if flag not set */
  if (!(fodInfos->ofnInfos->Flags & OFN_ALLOWMULTISELECT))
  {
     fodInfos->ShellInfos.folderSettings.fFlags |= FWF_SINGLESEL; 
  }
  fodInfos->ShellInfos.folderSettings.fFlags |= FWF_AUTOARRANGE | FWF_ALIGNLEFT;
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
  fodInfos->ShellInfos.folderSettings.ViewMode = FVM_LIST;

  GetWindowRect(GetDlgItem(hwnd,IDC_SHELLSTATIC),&fodInfos->ShellInfos.rectView);
  ScreenToClient(hwnd,(LPPOINT)&fodInfos->ShellInfos.rectView.left);
  ScreenToClient(hwnd,(LPPOINT)&fodInfos->ShellInfos.rectView.right);

  /* Construct the IShellBrowser interface */
  fodInfos->Shell.FOIShellBrowser = IShellBrowserImpl_Construct(hwnd);  
    
  return NOERROR;
}

/***********************************************************************
 *      FILEDLG95_SHELL_ExecuteCommand
 *
 * Change the folder option and refresh the view
 * If the function succeeds, the return value is nonzero.
 */
static BOOL FILEDLG95_SHELL_ExecuteCommand(HWND hwnd, LPCSTR lpVerb)
{
  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);

  IContextMenu * pcm;
Alexandre Julliard's avatar
Alexandre Julliard committed
1609
  TRACE("(0x%08x,%p)\n", hwnd, lpVerb);
1610 1611 1612 1613 1614 1615

  if(SUCCEEDED(IShellView_GetItemObject(fodInfos->Shell.FOIShellView,
					SVGIO_BACKGROUND,
					&IID_IContextMenu,
					(LPVOID*)&pcm)))
  {
Alexandre Julliard's avatar
Alexandre Julliard committed
1616 1617
    CMINVOKECOMMANDINFO ci;
    ZeroMemory(&ci, sizeof(CMINVOKECOMMANDINFO));
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
    ci.cbSize = sizeof(CMINVOKECOMMANDINFO);
    ci.lpVerb = lpVerb;
    ci.hwnd = hwnd;

    IContextMenu_InvokeCommand(pcm, &ci);
    IContextMenu_Release(pcm);
  }

  return FALSE;
}

/***********************************************************************
 *      FILEDLG95_SHELL_UpFolder
 *
 * Browse to the specified object
 * If the function succeeds, the return value is nonzero.
 */
static BOOL FILEDLG95_SHELL_UpFolder(HWND hwnd)
{
  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);

  TRACE("\n");

  if(SUCCEEDED(IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser,
                                          NULL,
                                          SBSP_PARENT)))
  {
    return TRUE;
  }
  return FALSE;
}

Juergen Schmied's avatar
Juergen Schmied committed
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
/***********************************************************************
 *      FILEDLG95_SHELL_BrowseToDesktop
 *
 * Browse to the Desktop
 * If the function succeeds, the return value is nonzero.
 */
static BOOL FILEDLG95_SHELL_BrowseToDesktop(HWND hwnd)
{
  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
  LPITEMIDLIST pidl;
  HRESULT hres;
  
  TRACE("\n");

  COMDLG32_SHGetSpecialFolderLocation(0,CSIDL_DESKTOP,&pidl);
  hres = IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser, pidl, SBSP_ABSOLUTE);
  COMDLG32_SHFree(pidl);
  return SUCCEEDED(hres);
}
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
/***********************************************************************
 *      FILEDLG95_SHELL_Clean
 *
 * Cleans the memory used by shell objects
 */
static void FILEDLG95_SHELL_Clean(HWND hwnd)
{
    FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);

    TRACE("\n");

1680 1681
    COMDLG32_SHFree(fodInfos->ShellInfos.pidlAbsCurrent);

1682 1683 1684 1685 1686
    /* clean Shell interfaces */
    IShellView_DestroyViewWindow(fodInfos->Shell.FOIShellView);
    IShellView_Release(fodInfos->Shell.FOIShellView);
    IShellFolder_Release(fodInfos->Shell.FOIShellFolder);
    IShellBrowser_Release(fodInfos->Shell.FOIShellBrowser);
1687 1688
    if (fodInfos->Shell.FOIDataObject)
      IDataObject_Release(fodInfos->Shell.FOIDataObject);
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
}

/***********************************************************************
 *      FILEDLG95_FILETYPE_Init
 *
 * Initialisation of the file type combo box 
 */
static HRESULT FILEDLG95_FILETYPE_Init(HWND hwnd)
{
  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);

  TRACE("\n");

Alexandre Julliard's avatar
Alexandre Julliard committed
1702
  if(fodInfos->ofnInfos->lpstrFilter)
1703
  {
1704
    int nFilters = 0;	/* number of filters */
1705
    LPSTR lpstrFilter;
1706
    LPCSTR lpstrPos = fodInfos->ofnInfos->lpstrFilter;
1707 1708 1709

    for(;;)
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
1710 1711 1712 1713
      /* filter is a list...  title\0ext\0......\0\0 
       * Set the combo item text to the title and the item data
       *  to the ext
       */
1714 1715
      LPCSTR lpstrDisplay;
      LPSTR lpstrExt;
Alexandre Julliard's avatar
Alexandre Julliard committed
1716

1717
      /* Get the title */
1718 1719 1720 1721 1722 1723 1724 1725 1726
      if(! *lpstrPos) break;	/* end */
      lpstrDisplay = lpstrPos;
      lpstrPos += strlen(lpstrPos) + 1;

      /* Copy the extensions */
      if (! *lpstrPos) return E_FAIL;	/* malformed filter */
      if (!(lpstrExt = (LPSTR) MemAlloc(strlen(lpstrPos)+1))) return E_FAIL;
      strcpy(lpstrExt,lpstrPos);
      lpstrPos += strlen(lpstrPos) + 1;
1727 1728
            
      /* Add the item at the end of the combo */
1729 1730 1731
      CBAddString(fodInfos->DlgInfos.hwndFileTypeCB, lpstrDisplay);
      CBSetItemDataPtr(fodInfos->DlgInfos.hwndFileTypeCB, nFilters, lpstrExt);
      nFilters++;
1732
    }
1733 1734 1735 1736 1737
    /*
     * Set the current filter to the one specified
     * in the initialisation structure
     * FIXME: lpstrCustomFilter not handled at all
     */
Alexandre Julliard's avatar
Alexandre Julliard committed
1738
  
1739 1740 1741 1742 1743 1744 1745
    /* set default filter index */
    if(fodInfos->ofnInfos->nFilterIndex == 0 && fodInfos->ofnInfos->lpstrCustomFilter == NULL)
      fodInfos->ofnInfos->nFilterIndex = 1;

    /* First, check to make sure our index isn't out of bounds. */
    if ( fodInfos->ofnInfos->nFilterIndex > nFilters )
      fodInfos->ofnInfos->nFilterIndex = nFilters;
Alexandre Julliard's avatar
Alexandre Julliard committed
1746
 
1747 1748
    /* Set the current index selection. */
    CBSetCurSel(fodInfos->DlgInfos.hwndFileTypeCB, fodInfos->ofnInfos->nFilterIndex-1);
Alexandre Julliard's avatar
Alexandre Julliard committed
1749
  
1750 1751
    /* Get the corresponding text string from the combo box. */
    lpstrFilter = (LPSTR) CBGetItemDataPtr(fodInfos->DlgInfos.hwndFileTypeCB,
Alexandre Julliard's avatar
Alexandre Julliard committed
1752
                                             fodInfos->ofnInfos->nFilterIndex-1);
1753

1754 1755 1756
    if ((INT)lpstrFilter == CB_ERR)  /* control is empty */
      lpstrFilter = NULL;	

1757 1758
    if(lpstrFilter)
    {
1759
      DWORD len;
1760
      _strlwr(lpstrFilter);	/* lowercase */
1761 1762 1763 1764
      len = MultiByteToWideChar( CP_ACP, 0, lpstrFilter, -1, NULL, 0 );
      fodInfos->ShellInfos.lpstrCurrentFilter = MemAlloc( len * sizeof(WCHAR) );
      MultiByteToWideChar( CP_ACP, 0, lpstrFilter, -1,
                           fodInfos->ShellInfos.lpstrCurrentFilter, len );
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
    }
  }
  return NOERROR;
}

/***********************************************************************
 *      FILEDLG95_FILETYPE_OnCommand
 *
 * WM_COMMAND of the file type combo box
 * If the function succeeds, the return value is nonzero.
 */
static BOOL FILEDLG95_FILETYPE_OnCommand(HWND hwnd, WORD wNotifyCode)
{
  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);

  switch(wNotifyCode)
  {
Alexandre Julliard's avatar
Alexandre Julliard committed
1782
    case CBN_SELENDOK:
1783 1784 1785 1786 1787 1788
    {
      LPSTR lpstrFilter;

      /* Get the current item of the filetype combo box */
      int iItem = CBGetCurSel(fodInfos->DlgInfos.hwndFileTypeCB);

Alexandre Julliard's avatar
Alexandre Julliard committed
1789 1790 1791
      /* set the current filter index - indexed from 1 */
      fodInfos->ofnInfos->nFilterIndex = iItem + 1;

1792 1793 1794 1795 1796 1797
      /* Set the current filter with the current selection */
      if(fodInfos->ShellInfos.lpstrCurrentFilter)
         MemFree((LPVOID)fodInfos->ShellInfos.lpstrCurrentFilter);

      lpstrFilter = (LPSTR) CBGetItemDataPtr(fodInfos->DlgInfos.hwndFileTypeCB,
                                             iItem);
Alexandre Julliard's avatar
Alexandre Julliard committed
1798
      if((int)lpstrFilter != CB_ERR)
1799
      {
1800 1801 1802 1803 1804 1805 1806
          DWORD len;
          _strlwr(lpstrFilter); /* lowercase */
          len = MultiByteToWideChar( CP_ACP, 0, lpstrFilter, -1, NULL, 0 );
          fodInfos->ShellInfos.lpstrCurrentFilter = MemAlloc( len * sizeof(WCHAR) );
          MultiByteToWideChar( CP_ACP, 0, lpstrFilter, -1,
                               fodInfos->ShellInfos.lpstrCurrentFilter, len );
          SendCustomDlgNotificationMessage(hwnd,CDN_TYPECHANGE);
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
      }

      /* Refresh the actual view to display the included items*/
      IShellView_Refresh(fodInfos->Shell.FOIShellView);
    }
  }
  return FALSE;
}
/***********************************************************************
 *      FILEDLG95_FILETYPE_SearchExt
 *
1818
 * searches for a extension in the filetype box
1819
 */
1820
static int FILEDLG95_FILETYPE_SearchExt(HWND hwnd,LPCSTR lpstrExt)
1821
{
1822
  int i, iCount = CBGetCount(hwnd);
1823

1824
  TRACE("%s\n", lpstrExt);
1825

1826
  if(iCount != CB_ERR)
1827
  {
1828 1829 1830
    for(i=0;i<iCount;i++)
    {
      if(!strcasecmp(lpstrExt,(LPSTR)CBGetItemDataPtr(hwnd,i)))
Juergen Schmied's avatar
Juergen Schmied committed
1831
          return i;
1832
    }
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
  }
  return -1;
}

/***********************************************************************
 *      FILEDLG95_FILETYPE_Clean
 *
 * Clean the memory used by the filetype combo box
 */
static void FILEDLG95_FILETYPE_Clean(HWND hwnd)
{
  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
  int iPos;
  int iCount = CBGetCount(fodInfos->DlgInfos.hwndFileTypeCB);

  TRACE("\n");

  /* Delete each string of the combo and their associated data */
1851
  if(iCount != CB_ERR)
1852
  {
1853 1854 1855 1856 1857
    for(iPos = iCount-1;iPos>=0;iPos--)
    {
      MemFree((LPSTR) CBGetItemDataPtr(fodInfos->DlgInfos.hwndFileTypeCB,iPos));
      CBDeleteString(fodInfos->DlgInfos.hwndFileTypeCB,iPos);
    }
1858 1859 1860
  }
  /* Current filter */
  if(fodInfos->ShellInfos.lpstrCurrentFilter)
1861
     MemFree(fodInfos->ShellInfos.lpstrCurrentFilter);
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882

}
    
/***********************************************************************
 *      FILEDLG95_LOOKIN_Init
 *
 * Initialisation of the look in combo box 
 */
static HRESULT FILEDLG95_LOOKIN_Init(HWND hwndCombo)
{
  IShellFolder	*psfRoot, *psfDrives;
  IEnumIDList	*lpeRoot, *lpeDrives;
  LPITEMIDLIST	pidlDrives, pidlTmp, pidlTmp1, pidlAbsTmp;

  LookInInfos *liInfos = MemAlloc(sizeof(LookInInfos));

  TRACE("\n");

  liInfos->iMaxIndentation = 0;

  SetPropA(hwndCombo, LookInInfosStr, (HANDLE) liInfos);
1883 1884 1885

  /* set item height for both text field and listbox */
  CBSetItemHeight(hwndCombo,-1,GetSystemMetrics(SM_CYSMICON));
1886
  CBSetItemHeight(hwndCombo,0,GetSystemMetrics(SM_CYSMICON));
1887 1888

  /* Initialise data of Desktop folder */
1889
  COMDLG32_SHGetSpecialFolderLocation(0,CSIDL_DESKTOP,&pidlTmp);
1890
  FILEDLG95_LOOKIN_AddItem(hwndCombo, pidlTmp,LISTEND);
1891
  COMDLG32_SHFree(pidlTmp);
1892

1893
  COMDLG32_SHGetSpecialFolderLocation(0,CSIDL_DRIVES,&pidlDrives);
1894

1895
  COMDLG32_SHGetDesktopFolder(&psfRoot);
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906

  if (psfRoot)
  {
    /* enumerate the contents of the desktop */
    if(SUCCEEDED(IShellFolder_EnumObjects(psfRoot, hwndCombo, SHCONTF_FOLDERS, &lpeRoot)))
    {
      while (S_OK == IEnumIDList_Next(lpeRoot, 1, &pidlTmp, NULL))
      {
	FILEDLG95_LOOKIN_AddItem(hwndCombo, pidlTmp,LISTEND);

	/* special handling for CSIDL_DRIVES */
1907
	if (COMDLG32_PIDL_ILIsEqual(pidlTmp, pidlDrives))
1908 1909 1910 1911 1912 1913 1914 1915
	{
	  if(SUCCEEDED(IShellFolder_BindToObject(psfRoot, pidlTmp, NULL, &IID_IShellFolder, (LPVOID*)&psfDrives)))
	  {
	    /* enumerate the drives */
	    if(SUCCEEDED(IShellFolder_EnumObjects(psfDrives, hwndCombo,SHCONTF_FOLDERS, &lpeDrives)))
	    {
	      while (S_OK == IEnumIDList_Next(lpeDrives, 1, &pidlTmp1, NULL))
	      {
1916
	        pidlAbsTmp = COMDLG32_PIDL_ILCombine(pidlTmp, pidlTmp1);
1917
	        FILEDLG95_LOOKIN_AddItem(hwndCombo, pidlAbsTmp,LISTEND);
1918 1919
	        COMDLG32_SHFree(pidlAbsTmp);
	        COMDLG32_SHFree(pidlTmp1);
1920 1921 1922 1923 1924 1925
	      }
	      IEnumIDList_Release(lpeDrives);
	    }
	    IShellFolder_Release(psfDrives);
	  }
	}
1926
        COMDLG32_SHFree(pidlTmp);
1927 1928 1929 1930 1931 1932
      }
      IEnumIDList_Release(lpeRoot);
    }
  }

  IShellFolder_Release(psfRoot);
1933
  COMDLG32_SHFree(pidlDrives);
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
  return NOERROR;
}

/***********************************************************************
 *      FILEDLG95_LOOKIN_DrawItem
 *
 * WM_DRAWITEM message handler
 */
static LRESULT FILEDLG95_LOOKIN_DrawItem(LPDRAWITEMSTRUCT pDIStruct)
{
  COLORREF crWin = GetSysColor(COLOR_WINDOW);
  COLORREF crHighLight = GetSysColor(COLOR_HIGHLIGHT);
  COLORREF crText = GetSysColor(COLOR_WINDOWTEXT);
  RECT rectText;
  RECT rectIcon;
  SHFILEINFOA sfi;
  HIMAGELIST ilItemImage;
  int iIndentation;
1952
  TEXTMETRICA tm;
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
  LPSFOLDER tmpFolder;


  LookInInfos *liInfos = (LookInInfos *)GetPropA(pDIStruct->hwndItem,LookInInfosStr);

  TRACE("\n");

  if(pDIStruct->itemID == -1)
    return 0;

  if(!(tmpFolder = (LPSFOLDER) CBGetItemDataPtr(pDIStruct->hwndItem,
                            pDIStruct->itemID)))
    return 0;


  if(pDIStruct->itemID == liInfos->uSelectedItem)
  {
1970
    ilItemImage = (HIMAGELIST) COMDLG32_SHGetFileInfoA ((LPCSTR) tmpFolder->pidlItem,
1971 1972 1973 1974 1975 1976 1977 1978 1979
                                               0,    
                                               &sfi,    
                                               sizeof (SHFILEINFOA),   
                                               SHGFI_PIDL | SHGFI_SMALLICON |    
                                               SHGFI_OPENICON | SHGFI_SYSICONINDEX    | 
                                               SHGFI_DISPLAYNAME );   
  }
  else
  {
1980
    ilItemImage = (HIMAGELIST) COMDLG32_SHGetFileInfoA ((LPCSTR) tmpFolder->pidlItem,
1981 1982 1983 1984 1985 1986 1987 1988
                                                  0, 
                                                  &sfi, 
                                                  sizeof (SHFILEINFOA),
                                                  SHGFI_PIDL | SHGFI_SMALLICON | 
                                                  SHGFI_SYSICONINDEX | 
                                                  SHGFI_DISPLAYNAME);
  }

1989
  /* Is this item selected ? */
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
  if(pDIStruct->itemState & ODS_SELECTED)
  {
    SetTextColor(pDIStruct->hDC,(0x00FFFFFF & ~(crText)));
    SetBkColor(pDIStruct->hDC,crHighLight);
    FillRect(pDIStruct->hDC,&pDIStruct->rcItem,(HBRUSH)crHighLight);
  }
  else
  {
    SetTextColor(pDIStruct->hDC,crText);
    SetBkColor(pDIStruct->hDC,crWin);
    FillRect(pDIStruct->hDC,&pDIStruct->rcItem,(HBRUSH)crWin);
  }

2003
  /* Do not indent item if drawing in the edit of the combo */
2004 2005 2006
  if(pDIStruct->itemState & ODS_COMBOBOXEDIT)
  {
    iIndentation = 0;
2007
    ilItemImage = (HIMAGELIST) COMDLG32_SHGetFileInfoA ((LPCSTR) tmpFolder->pidlItem,
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
                                                0, 
                                                &sfi, 
                                                sizeof (SHFILEINFOA), 
                                                SHGFI_PIDL | SHGFI_SMALLICON | SHGFI_OPENICON 
                                                | SHGFI_SYSICONINDEX | SHGFI_DISPLAYNAME  );

  }
  else
  {
    iIndentation = tmpFolder->m_iIndent;
  }
  /* Draw text and icon */

  /* Initialise the icon display area */
  rectIcon.left = pDIStruct->rcItem.left + ICONWIDTH/2 * iIndentation;
  rectIcon.top = pDIStruct->rcItem.top;
  rectIcon.right = rectIcon.left + ICONWIDTH;
  rectIcon.bottom = pDIStruct->rcItem.bottom;

  /* Initialise the text display area */
2028
  GetTextMetricsA(pDIStruct->hDC, &tm);
2029
  rectText.left = rectIcon.right;
2030 2031
  rectText.top =
	  (pDIStruct->rcItem.top + pDIStruct->rcItem.bottom - tm.tmHeight) / 2;
2032
  rectText.right = pDIStruct->rcItem.right + XTEXTOFFSET;
2033 2034
  rectText.bottom =
	  (pDIStruct->rcItem.top + pDIStruct->rcItem.bottom + tm.tmHeight) / 2;
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061
 
  /* Draw the icon from the image list */
  COMDLG32_ImageList_Draw(ilItemImage,
                 sfi.iIcon,
                 pDIStruct->hDC,  
                 rectIcon.left,  
                 rectIcon.top,  
                 ILD_TRANSPARENT );  

  /* Draw the associated text */
  if(sfi.szDisplayName)
    TextOutA(pDIStruct->hDC,rectText.left,rectText.top,sfi.szDisplayName,strlen(sfi.szDisplayName));


  return NOERROR;
}

/***********************************************************************
 *      FILEDLG95_LOOKIN_OnCommand
 *
 * LookIn combo box WM_COMMAND message handler
 * If the function succeeds, the return value is nonzero.
 */
static BOOL FILEDLG95_LOOKIN_OnCommand(HWND hwnd, WORD wNotifyCode)
{
  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);

Alexandre Julliard's avatar
Alexandre Julliard committed
2062
  TRACE("%p\n", fodInfos);
2063 2064 2065

  switch(wNotifyCode)
  {
2066
    case CBN_SELENDOK:
2067 2068 2069 2070 2071 2072
    {
      LPSFOLDER tmpFolder;
      int iItem; 

      iItem = CBGetCurSel(fodInfos->DlgInfos.hwndLookInCB);

2073 2074 2075
      if(!(tmpFolder = (LPSFOLDER) CBGetItemDataPtr(fodInfos->DlgInfos.hwndLookInCB,
                                               iItem)))
	return FALSE;
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100


      if(SUCCEEDED(IShellBrowser_BrowseObject(fodInfos->Shell.FOIShellBrowser,
                                              tmpFolder->pidlItem,
                                              SBSP_ABSOLUTE)))
      {
        return TRUE;
      }
      break;
    }
      
  }
  return FALSE;
}

/***********************************************************************
 *      FILEDLG95_LOOKIN_AddItem
 *
 * Adds an absolute pidl item to the lookin combo box
 * returns the index of the inserted item
 */
static int FILEDLG95_LOOKIN_AddItem(HWND hwnd,LPITEMIDLIST pidl, int iInsertId)
{
  LPITEMIDLIST pidlNext;
  SHFILEINFOA sfi;
Alexandre Julliard's avatar
Alexandre Julliard committed
2101
  SFOLDER *tmpFolder;
2102 2103
  LookInInfos *liInfos;

2104
  TRACE("%08x\n", iInsertId);
2105

Alexandre Julliard's avatar
Alexandre Julliard committed
2106 2107 2108
  if(!pidl)
    return -1;

2109 2110 2111
  if(!(liInfos = (LookInInfos *)GetPropA(hwnd,LookInInfosStr)))
    return -1;
    
Alexandre Julliard's avatar
Alexandre Julliard committed
2112
  tmpFolder = MemAlloc(sizeof(SFOLDER));
2113 2114 2115 2116
  tmpFolder->m_iIndent = 0;

  /* Calculate the indentation of the item in the lookin*/
  pidlNext = pidl;
2117
  while( (pidlNext=COMDLG32_PIDL_ILGetNext(pidlNext)) )
2118 2119 2120 2121
  {
    tmpFolder->m_iIndent++;
  }

2122
  tmpFolder->pidlItem = COMDLG32_PIDL_ILClone(pidl); /* FIXME: memory leak*/
2123 2124 2125 2126

  if(tmpFolder->m_iIndent > liInfos->iMaxIndentation)
    liInfos->iMaxIndentation = tmpFolder->m_iIndent;
  
2127
  sfi.dwAttributes = SFGAO_FILESYSANCESTOR | SFGAO_FILESYSTEM;
2128
  COMDLG32_SHGetFileInfoA((LPSTR)pidl,
2129 2130 2131 2132
                  0,
                  &sfi,
                  sizeof(sfi),
                  SHGFI_DISPLAYNAME | SHGFI_SYSICONINDEX 
2133
                  | SHGFI_PIDL | SHGFI_SMALLICON | SHGFI_ATTRIBUTES | SHGFI_ATTR_SPECIFIED);
2134

2135
  TRACE("-- Add %s attr=%08lx\n", sfi.szDisplayName, sfi.dwAttributes);
2136 2137

  if((sfi.dwAttributes & SFGAO_FILESYSANCESTOR) || (sfi.dwAttributes & SFGAO_FILESYSTEM))
2138 2139 2140
  {
    int iItemID;
  
2141 2142
    TRACE("-- Add %s at %u\n", sfi.szDisplayName, tmpFolder->m_iIndent);

2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153
    /* Add the item at the end of the list */
    if(iInsertId < 0)
    {
      iItemID = CBAddString(hwnd,sfi.szDisplayName);
    }
    /* Insert the item at the iInsertId position*/
    else
    {
      iItemID = CBInsertString(hwnd,sfi.szDisplayName,iInsertId);
    }

2154 2155
    CBSetItemDataPtr(hwnd,iItemID,tmpFolder);
    return iItemID;
2156 2157
  }

Alexandre Julliard's avatar
Alexandre Julliard committed
2158
  MemFree( tmpFolder );
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183
  return -1;

}

/***********************************************************************
 *      FILEDLG95_LOOKIN_InsertItemAfterParent
 *
 * Insert an item below its parent 
 */
static int FILEDLG95_LOOKIN_InsertItemAfterParent(HWND hwnd,LPITEMIDLIST pidl)
{
  
  LPITEMIDLIST pidlParent = GetParentPidl(pidl);
  int iParentPos;

  TRACE("\n");

  iParentPos = FILEDLG95_LOOKIN_SearchItem(hwnd,(WPARAM)pidlParent,SEARCH_PIDL);

  if(iParentPos < 0)
  {
    iParentPos = FILEDLG95_LOOKIN_InsertItemAfterParent(hwnd,pidlParent);
  }

  /* Free pidlParent memory */
2184
  COMDLG32_SHFree((LPVOID)pidlParent);
2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250

  return FILEDLG95_LOOKIN_AddItem(hwnd,pidl,iParentPos + 1);
}

/***********************************************************************
 *      FILEDLG95_LOOKIN_SelectItem
 *
 * Adds an absolute pidl item to the lookin combo box
 * returns the index of the inserted item
 */
int FILEDLG95_LOOKIN_SelectItem(HWND hwnd,LPITEMIDLIST pidl)
{
  int iItemPos;
  LookInInfos *liInfos;

  TRACE("\n");

  iItemPos = FILEDLG95_LOOKIN_SearchItem(hwnd,(WPARAM)pidl,SEARCH_PIDL);

  liInfos = (LookInInfos *)GetPropA(hwnd,LookInInfosStr);

  if(iItemPos < 0)
  {
    while(FILEDLG95_LOOKIN_RemoveMostExpandedItem(hwnd) > -1);
    iItemPos = FILEDLG95_LOOKIN_InsertItemAfterParent(hwnd,pidl);
  }

  else
  {
    SFOLDER *tmpFolder = (LPSFOLDER) CBGetItemDataPtr(hwnd,iItemPos);
    while(liInfos->iMaxIndentation > tmpFolder->m_iIndent)
    {
      int iRemovedItem;

      if(-1 == (iRemovedItem = FILEDLG95_LOOKIN_RemoveMostExpandedItem(hwnd)))
        break;
      if(iRemovedItem < iItemPos)
        iItemPos--;
    }
  }
  
  CBSetCurSel(hwnd,iItemPos);
  liInfos->uSelectedItem = iItemPos;

  return 0;

}

/***********************************************************************
 *      FILEDLG95_LOOKIN_RemoveMostExpandedItem
 *
 * Remove the item with an expansion level over iExpansionLevel
 */
static int FILEDLG95_LOOKIN_RemoveMostExpandedItem(HWND hwnd)
{
  int iItemPos;

  LookInInfos *liInfos = (LookInInfos *)GetPropA(hwnd,LookInInfosStr);

  TRACE("\n");

  if(liInfos->iMaxIndentation <= 2)
    return -1;

  if((iItemPos = FILEDLG95_LOOKIN_SearchItem(hwnd,(WPARAM)liInfos->iMaxIndentation,SEARCH_EXP)) >=0)
  {
2251 2252 2253
    SFOLDER *tmpFolder = (LPSFOLDER) CBGetItemDataPtr(hwnd,iItemPos);
    COMDLG32_SHFree(tmpFolder->pidlItem);
    MemFree(tmpFolder);
2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
    CBDeleteString(hwnd,iItemPos);
    liInfos->iMaxIndentation--;

    return iItemPos;
  }

  return -1;
}

/***********************************************************************
 *      FILEDLG95_LOOKIN_SearchItem
 *
 * Search for pidl in the lookin combo box
 * returns the index of the found item
 */
static int FILEDLG95_LOOKIN_SearchItem(HWND hwnd,WPARAM searchArg,int iSearchMethod)
{
  int i = 0;
  int iCount = CBGetCount(hwnd);

2274
  TRACE("0x%08x 0x%x\n",searchArg, iSearchMethod);
2275

2276
  if (iCount != CB_ERR)
2277
  {
2278 2279 2280
    for(;i<iCount;i++)
    {
      LPSFOLDER tmpFolder = (LPSFOLDER) CBGetItemDataPtr(hwnd,i);
2281

2282 2283 2284 2285 2286
      if(iSearchMethod == SEARCH_PIDL && COMDLG32_PIDL_ILIsEqual((LPITEMIDLIST)searchArg,tmpFolder->pidlItem))
        return i;
      if(iSearchMethod == SEARCH_EXP && tmpFolder->m_iIndent == (int)searchArg)
        return i;
    }
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
  }

  return -1;
}

/***********************************************************************
 *      FILEDLG95_LOOKIN_Clean
 *
 * Clean the memory used by the lookin combo box
 */
static void FILEDLG95_LOOKIN_Clean(HWND hwnd)
{
    FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
    int iPos;
    int iCount = CBGetCount(fodInfos->DlgInfos.hwndLookInCB);

    TRACE("\n");

    /* Delete each string of the combo and their associated data */
2306
    if (iCount != CB_ERR)
2307
    {
2308 2309 2310 2311 2312
      for(iPos = iCount-1;iPos>=0;iPos--)
      {
        SFOLDER *tmpFolder = (LPSFOLDER) CBGetItemDataPtr(fodInfos->DlgInfos.hwndLookInCB,iPos);
        COMDLG32_SHFree(tmpFolder->pidlItem);
        MemFree(tmpFolder);
2313
        CBDeleteString(fodInfos->DlgInfos.hwndLookInCB,iPos);
2314
      }
2315
    }
2316

2317 2318 2319 2320
    /* LookInInfos structure */
    RemovePropA(fodInfos->DlgInfos.hwndLookInCB,LookInInfosStr);

}
2321 2322 2323 2324
/***********************************************************************
 * FILEDLG95_FILENAME_FillFromSelection
 *
 * fills the edit box from the cached DataObject
2325
 */
2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355
void FILEDLG95_FILENAME_FillFromSelection (HWND hwnd)
{
    FileOpenDlgInfos *fodInfos;
    LPITEMIDLIST      pidl;
    UINT              nFiles = 0, nFileToOpen, nFileSelected, nLength = 0;
    char              lpstrTemp[MAX_PATH];
    LPSTR             lpstrAllFile = NULL, lpstrCurrFile = NULL;

    TRACE("\n");
    fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);

    /* Count how many files we have */
    nFileSelected = GetNumSelected( fodInfos->Shell.FOIDataObject );

    /* calculate the string length, count files */
    if (nFileSelected >= 1)
    {
      nLength += 3;	/* first and last quotes, trailing \0 */
      for ( nFileToOpen = 0; nFileToOpen < nFileSelected; nFileToOpen++ )
      {
        pidl = GetPidlFromDataObject( fodInfos->Shell.FOIDataObject, nFileToOpen+1 );
    
        if (pidl)
	{
          /* get the total length of the selected file names*/
          lpstrTemp[0] = '\0';
          GetName( fodInfos->Shell.FOIShellFolder, pidl, SHGDN_INFOLDER, lpstrTemp );

          if ( ! IsPidlFolder(fodInfos->Shell.FOIShellFolder, pidl) ) /* Ignore folders */
	  {
2356
            nLength += strlen( lpstrTemp ) + 3;
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
            nFiles++;
	  }
          COMDLG32_SHFree( pidl );
	}
      }
    }

    /* allocate the buffer */
    if (nFiles <= 1) nLength = MAX_PATH;
    lpstrAllFile = (LPSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nLength);
    lpstrAllFile[0] = '\0';

    /* Generate the string for the edit control */
    if(nFiles >= 1)
    {
      lpstrCurrFile = lpstrAllFile;
      for ( nFileToOpen = 0; nFileToOpen < nFileSelected; nFileToOpen++ )
      {
        pidl = GetPidlFromDataObject( fodInfos->Shell.FOIDataObject, nFileToOpen+1 );

        if (pidl)
	{
	  /* get the file name */
          lpstrTemp[0] = '\0';
          GetName( fodInfos->Shell.FOIShellFolder, pidl, SHGDN_INFOLDER, lpstrTemp );

          if (! IsPidlFolder(fodInfos->Shell.FOIShellFolder, pidl)) /* Ignore folders */
	  {
            if ( nFiles > 1)
	    {
              *lpstrCurrFile++ =  '\"';
2388 2389 2390
              strcpy( lpstrCurrFile, lpstrTemp );
              lpstrCurrFile += strlen( lpstrTemp );
              strcpy( lpstrCurrFile, "\" " );
2391 2392 2393 2394
              lpstrCurrFile += 2;
	    }
	    else
	    {
2395
              strcpy( lpstrAllFile, lpstrTemp );
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
	    }
	  }
          COMDLG32_SHFree( (LPVOID) pidl );
	}
      }
    }

    SetWindowTextA( fodInfos->DlgInfos.hwndFileName, lpstrAllFile );
    HeapFree(GetProcessHeap(),0, lpstrAllFile );
}

2407

2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436
/* copied from shell32 to avoid linking to it */
static HRESULT COMDLG32_StrRetToStrNA (LPVOID dest, DWORD len, LPSTRRET src, LPITEMIDLIST pidl)
{
	switch (src->uType)
	{
	  case STRRET_WSTR:
	    WideCharToMultiByte(CP_ACP, 0, src->u.pOleStr, -1, (LPSTR)dest, len, NULL, NULL);
	    COMDLG32_SHFree(src->u.pOleStr);
	    break;

	  case STRRET_CSTRA:
	    lstrcpynA((LPSTR)dest, src->u.cStr, len);
	    break;

	  case STRRET_OFFSETA:
	    lstrcpynA((LPSTR)dest, ((LPCSTR)&pidl->mkid)+src->u.uOffset, len);
	    break;

	  default:
	    FIXME("unknown type!\n");
	    if (len)
	    {
	      *(LPSTR)dest = '\0';
	    }
	    return(FALSE);
	}
	return S_OK;
}

2437 2438 2439
/***********************************************************************
 * FILEDLG95_FILENAME_GetFileNames
 *
2440
 * copies the filenames to a 0-delimited string list (A\0B\0C\0\0)
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
 */
int FILEDLG95_FILENAME_GetFileNames (HWND hwnd, LPSTR * lpstrFileList, UINT * sizeUsed)
{
	FileOpenDlgInfos *fodInfos  = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);
	UINT nStrCharCount = 0;	/* index in src buffer */
	UINT nFileIndex = 0;	/* index in dest buffer */
	UINT nFileCount = 0;	/* number of files */
	UINT nStrLen = 0;	/* length of string in edit control */
	LPSTR lpstrEdit;	/* buffer for string from edit control */

	TRACE("\n");

	/* get the filenames from the edit control */
	nStrLen = SendMessageA(fodInfos->DlgInfos.hwndFileName, WM_GETTEXTLENGTH, 0, 0);
	lpstrEdit = MemAlloc(nStrLen+1);
	GetDlgItemTextA(hwnd, IDC_FILENAME, lpstrEdit, nStrLen+1);

	TRACE("nStrLen=%u str=%s\n", nStrLen, lpstrEdit);
	
2460 2461 2462
	/* we might get single filename without any '"',
	 * so we need nStrLen + terminating \0 + end-of-list \0 */
	*lpstrFileList = MemAlloc(nStrLen+2);
2463 2464
	*sizeUsed = 0;

2465
	/* build 0-delimited file list from filenames */
2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498
	while ( nStrCharCount <= nStrLen )
	{
	  if ( lpstrEdit[nStrCharCount]=='"' )
	  {
	    nStrCharCount++;
	    while ((lpstrEdit[nStrCharCount]!='"') && (nStrCharCount <= nStrLen))
	    {
	      (*lpstrFileList)[nFileIndex++] = lpstrEdit[nStrCharCount];
	      (*sizeUsed)++;
	      nStrCharCount++;
	    }
	    (*lpstrFileList)[nFileIndex++] = '\0';
	    (*sizeUsed)++;
	    nFileCount++;
	  } 
	  nStrCharCount++;
	}

	/* single, unquoted string */
	if ((nStrLen > 0) && (*sizeUsed == 0) )
	{
	  strcpy(*lpstrFileList, lpstrEdit);
	  nFileIndex = strlen(lpstrEdit) + 1;
	  (*sizeUsed) = nFileIndex;
	  nFileCount = 1;
	}

	/* trailing \0 */
	(*lpstrFileList)[nFileIndex] = '\0';
	(*sizeUsed)++;

	MemFree(lpstrEdit);
	return nFileCount;
2499
}
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597

#define SETDefFormatEtc(fe,cf,med) \
{ \
    (fe).cfFormat = cf;\
    (fe).dwAspect = DVASPECT_CONTENT; \
    (fe).ptd =NULL;\
    (fe).tymed = med;\
    (fe).lindex = -1;\
};

/*
 * DATAOBJECT Helper functions
 */

/***********************************************************************
 * COMCTL32_ReleaseStgMedium
 *
 * like ReleaseStgMedium from ole32
 */
static void COMCTL32_ReleaseStgMedium (STGMEDIUM medium)
{
      if(medium.pUnkForRelease)
      {
        IUnknown_Release(medium.pUnkForRelease);
      }
      else
      {
        GlobalUnlock(medium.u.hGlobal);
        GlobalFree(medium.u.hGlobal);
      }
}

/***********************************************************************
 *          GetPidlFromDataObject
 *
 * Return pidl(s) by number from the cached DataObject
 *
 * nPidlIndex=0 gets the fully qualified root path
 */
LPITEMIDLIST GetPidlFromDataObject ( IDataObject *doSelected, UINT nPidlIndex)
{
     
    STGMEDIUM medium;
    FORMATETC formatetc;
    LPITEMIDLIST pidl = NULL;
    
    TRACE("sv=%p index=%u\n", doSelected, nPidlIndex);
    
    /* Set the FORMATETC structure*/
    SETDefFormatEtc(formatetc, RegisterClipboardFormatA(CFSTR_SHELLIDLIST), TYMED_HGLOBAL);

    /* Get the pidls from IDataObject */
    if(SUCCEEDED(IDataObject_GetData(doSelected,&formatetc,&medium)))
    {
      LPIDA cida = GlobalLock(medium.u.hGlobal);
      if(nPidlIndex <= cida->cidl)
      {
        pidl = COMDLG32_PIDL_ILClone((LPITEMIDLIST)(&((LPBYTE)cida)[cida->aoffset[nPidlIndex]]));
      }
      COMCTL32_ReleaseStgMedium(medium);
    }
    return pidl;
}

/***********************************************************************
 *          GetNumSelected
 *
 * Return the number of selected items in the DataObject.
 *
*/
UINT GetNumSelected( IDataObject *doSelected )
{
    UINT retVal = 0;
    STGMEDIUM medium;
    FORMATETC formatetc;

    TRACE("sv=%p\n", doSelected);

    if (!doSelected) return 0;

    /* Set the FORMATETC structure*/
    SETDefFormatEtc(formatetc, RegisterClipboardFormatA(CFSTR_SHELLIDLIST), TYMED_HGLOBAL);

    /* Get the pidls from IDataObject */
    if(SUCCEEDED(IDataObject_GetData(doSelected,&formatetc,&medium)))
    {
      LPIDA cida = GlobalLock(medium.u.hGlobal);
      retVal = cida->cidl;
      COMCTL32_ReleaseStgMedium(medium);
      return retVal;
    }
    return 0;
}

/*
 * TOOLS
 */

2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612
/***********************************************************************
 *      GetName
 *
 * Get the pidl's display name (relative to folder) and 
 * put it in lpstrFileName.
 * 
 * Return NOERROR on success,
 * E_FAIL otherwise
 */

HRESULT GetName(LPSHELLFOLDER lpsf, LPITEMIDLIST pidl,DWORD dwFlags,LPSTR lpstrFileName)
{
  STRRET str;
  HRESULT hRes;

2613
  TRACE("sf=%p pidl=%p\n", lpsf, pidl);
2614 2615 2616 2617

  if(!lpsf)
  {
    HRESULT hRes;
2618
    COMDLG32_SHGetDesktopFolder(&lpsf);
2619 2620 2621 2622 2623 2624
    hRes = GetName(lpsf,pidl,dwFlags,lpstrFileName);
    IShellFolder_Release(lpsf);
    return hRes;
  }

  /* Get the display name of the pidl relative to the folder */
2625
  if (SUCCEEDED(hRes = IShellFolder_GetDisplayNameOf(lpsf, pidl, dwFlags, &str)))
2626
  {
2627
      return COMDLG32_StrRetToStrNA(lpstrFileName, MAX_PATH, &str, pidl);
2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643
  }
  return E_FAIL;
}

/***********************************************************************
 *      GetShellFolderFromPidl
 *
 * pidlRel is the item pidl relative 
 * Return the IShellFolder of the absolute pidl
 */
IShellFolder *GetShellFolderFromPidl(LPITEMIDLIST pidlAbs)
{
  IShellFolder *psf = NULL,*psfParent;

  TRACE("%p\n", pidlAbs);

2644
  if(SUCCEEDED(COMDLG32_SHGetDesktopFolder(&psfParent)))
2645 2646 2647 2648
  {
    psf = psfParent;
    if(pidlAbs && pidlAbs->mkid.cb)
    {
2649
      if(SUCCEEDED(IShellFolder_BindToObject(psfParent, pidlAbs, NULL, &IID_IShellFolder, (LPVOID*)&psf)))
2650
      {
2651 2652
	IShellFolder_Release(psfParent);
        return psf;
2653 2654
      }
    }
2655 2656
    /* return the desktop */
    return psfParent;
2657
  }
2658
  return NULL;
2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670
}

/***********************************************************************
 *      GetParentPidl
 *
 * Return the LPITEMIDLIST to the parent of the pidl in the list
 */
LPITEMIDLIST GetParentPidl(LPITEMIDLIST pidl)
{
  LPITEMIDLIST pidlParent;

  TRACE("%p\n", pidl);
2671
  
2672 2673
  pidlParent = COMDLG32_PIDL_ILClone(pidl);
  COMDLG32_PIDL_ILRemoveLastID(pidlParent);
2674
     
2675 2676 2677 2678 2679 2680 2681 2682 2683
  return pidlParent;
}

/***********************************************************************
 *      GetPidlFromName
 *
 * returns the pidl of the file name relative to folder 
 * NULL if an error occured
 */
2684
LPITEMIDLIST GetPidlFromName(IShellFolder *lpsf,LPCSTR lpcstrFileName)
2685 2686 2687
{
  LPITEMIDLIST pidl;
  ULONG ulEaten;
2688
  WCHAR lpwstrDirName[MAX_PATH];
2689

2690
  TRACE("sf=%p file=%s\n", lpsf, lpcstrFileName);
2691

2692
  if(!lpcstrFileName) return NULL;
2693
    
2694
  MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,lpcstrFileName,-1,(LPWSTR)lpwstrDirName,MAX_PATH);  
2695

2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
  if(!lpsf)
  {
    COMDLG32_SHGetDesktopFolder(&lpsf);
    pidl = GetPidlFromName(lpsf, lpcstrFileName);
    IShellFolder_Release(lpsf);
  }
  else
  {
    IShellFolder_ParseDisplayName(lpsf, 0, NULL, (LPWSTR)lpwstrDirName, &ulEaten, &pidl, NULL); 
  }
  return pidl;
}

/*
*/
BOOL IsPidlFolder (LPSHELLFOLDER psf, LPITEMIDLIST pidl)
{
	ULONG uAttr  = SFGAO_FOLDER | SFGAO_HASSUBFOLDER;
	HRESULT ret;
	
	TRACE("%p, %p\n", psf, pidl);
	
  	ret = IShellFolder_GetAttributesOf( psf, 1, &pidl, &uAttr );
	
	TRACE("-- 0x%08lx 0x%08lx\n", uAttr, ret);
	/* see documentation shell 4.1*/
        return uAttr & (SFGAO_FOLDER | SFGAO_HASSUBFOLDER);
2723 2724 2725
}

/***********************************************************************
2726
 *      BrowseSelectedFolder
2727
 */
2728
static BOOL BrowseSelectedFolder(HWND hwnd)
2729
{
2730 2731 2732 2733
  BOOL bBrowseSelFolder = FALSE;
  FileOpenDlgInfos *fodInfos = (FileOpenDlgInfos *) GetPropA(hwnd,FileOpenDlgInfosStr);

  TRACE("\n");
2734

2735
  if (GetNumSelected(fodInfos->Shell.FOIDataObject) == 1)
2736
  {
2737
      LPITEMIDLIST pidlSelection;
2738

2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752
      /* get the file selected */
      pidlSelection  = GetPidlFromDataObject( fodInfos->Shell.FOIDataObject, 1);
      if (IsPidlFolder (fodInfos->Shell.FOIShellFolder, pidlSelection))
      {
          if ( FAILED( IShellBrowser_BrowseObject( fodInfos->Shell.FOIShellBrowser,
                         pidlSelection, SBSP_RELATIVE ) ) )
          {
               MessageBoxA( hwnd, "Path does not exist", fodInfos->ofnInfos->lpstrTitle,
                      MB_OK | MB_ICONEXCLAMATION );
          }

         bBrowseSelFolder = TRUE;
      }
      COMDLG32_SHFree( pidlSelection );
2753
  }
2754 2755 2756

  return bBrowseSelFolder;
} 
2757 2758 2759

/*
 * Memory allocation methods */
2760
static void *MemAlloc(UINT size)
2761 2762 2763 2764
{
    return HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,size);
}

2765
static void MemFree(void *mem)
2766 2767 2768 2769 2770 2771
{
    if(mem)
    {
        HeapFree(GetProcessHeap(),0,mem);
    }
}
2772