filedlg16.c 16.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * COMMDLG - File Dialogs
 *
 * Copyright 1994 Martin Ayotte
 * Copyright 1996 Albrecht Kleine
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20
 */
21
#include <stdarg.h>
22 23 24
#include "windef.h"
#include "winbase.h"
#include "wine/winbase16.h"
25
#include "winuser.h"
26
#include "wine/winuser16.h"
27 28
#include "wine/debug.h"
#include "cderr.h"
29 30 31 32 33
#include "commdlg.h"

WINE_DEFAULT_DEBUG_CHANNEL(commdlg);

#include "cdlg.h"
34
#include "cdlg16.h"
35
#include "filedlg31.h"
36

37
typedef struct tagFD16_PRIVATE
38 39 40 41 42
{
    HANDLE16 hDlgTmpl16; /* handle for resource 16 */
    HANDLE16 hResource16; /* handle for allocated resource 16 */
    HANDLE16 hGlobal16; /* 16 bits mem block (resources) */
    OPENFILENAME16 *ofn16; /* original structure if 16 bits dialog */
43
} FD16_PRIVATE, *PFD16_PRIVATE;
44

45 46 47
/************************************************************************
 *                              FD16_MapOfnStruct16          [internal]
 *      map a 16 bits structure to an Unicode one
48
 */
49
static void FD16_MapOfnStruct16(const OPENFILENAME16 *ofn16, LPOPENFILENAMEW ofnW, BOOL open)
50
{
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    OPENFILENAMEA ofnA;
    /* first convert to linear pointers */
    memset(&ofnA, 0, sizeof(OPENFILENAMEA));
    ofnA.lStructSize = sizeof(OPENFILENAMEA);
    ofnA.hwndOwner = HWND_32(ofn16->hwndOwner);
    ofnA.hInstance = HINSTANCE_32(ofn16->hInstance);
    if (ofn16->lpstrFilter)
        ofnA.lpstrFilter = MapSL(ofn16->lpstrFilter);
    if (ofn16->lpstrCustomFilter)
        ofnA.lpstrCustomFilter = MapSL(ofn16->lpstrCustomFilter);
    ofnA.nMaxCustFilter = ofn16->nMaxCustFilter;
    ofnA.nFilterIndex = ofn16->nFilterIndex;
    ofnA.lpstrFile = MapSL(ofn16->lpstrFile);
    ofnA.nMaxFile = ofn16->nMaxFile;
    ofnA.lpstrFileTitle = MapSL(ofn16->lpstrFileTitle);
    ofnA.nMaxFileTitle = ofn16->nMaxFileTitle;
    ofnA.lpstrInitialDir = MapSL(ofn16->lpstrInitialDir);
    ofnA.lpstrTitle = MapSL(ofn16->lpstrTitle);
    ofnA.Flags = ofn16->Flags;
    ofnA.nFileOffset = ofn16->nFileOffset;
    ofnA.nFileExtension = ofn16->nFileExtension;
    ofnA.lpstrDefExt = MapSL(ofn16->lpstrDefExt);
    if (HIWORD(ofn16->lpTemplateName))
        ofnA.lpTemplateName = MapSL(ofn16->lpTemplateName);
    else
        ofnA.lpTemplateName = (LPSTR) ofn16->lpTemplateName; /* ressource number */
    /* now calls the 32 bits Ansi to Unicode version to complete the job */
    FD31_MapOfnStructA(&ofnA, ofnW, open);
79 80 81
}

/***********************************************************************
82
 *           FD16_GetTemplate                                [internal]
83 84 85 86 87
 *
 * Get a template (FALSE if failure) when 16 bits dialogs are used
 * by a 16 bits application
 *
 */
88
static BOOL FD16_GetTemplate(const FD31_DATA *lfs)
89
{
90 91
    PFD16_PRIVATE priv = (PFD16_PRIVATE) lfs->private1632;
    LPOPENFILENAME16 ofn16 = priv->ofn16;
92
    LPVOID template;
93 94 95
    HGLOBAL16 hGlobal16 = 0;

    if (ofn16->Flags & OFN_ENABLETEMPLATEHANDLE)
96
        priv->hDlgTmpl16 = ofn16->hInstance;
97 98 99 100 101 102 103 104 105 106
    else if (ofn16->Flags & OFN_ENABLETEMPLATE)
    {
	HANDLE16 hResInfo;
	if (!(hResInfo = FindResource16(ofn16->hInstance,
					MapSL(ofn16->lpTemplateName),
                                        (LPSTR)RT_DIALOG)))
	{
	    COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
	    return FALSE;
	}
107
	if (!(priv->hDlgTmpl16 = LoadResource16( ofn16->hInstance, hResInfo )))
108 109 110 111
	{
	    COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
	    return FALSE;
	}
112
        priv->hResource16 = priv->hDlgTmpl16;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    }
    else
    { /* get resource from (32 bits) own Wine resource; convert it to 16 */
	HRSRC hResInfo;
	HGLOBAL hDlgTmpl32;
        LPCVOID template32;
        DWORD size;

	if (!(hResInfo = FindResourceA(COMDLG32_hInstance,
               lfs->open ? "OPEN_FILE":"SAVE_FILE", (LPSTR)RT_DIALOG)))
	{
	    COMDLG32_SetCommDlgExtendedError(CDERR_FINDRESFAILURE);
	    return FALSE;
	}
	if (!(hDlgTmpl32 = LoadResource(COMDLG32_hInstance, hResInfo )) ||
	    !(template32 = LockResource( hDlgTmpl32 )))
	{
	    COMDLG32_SetCommDlgExtendedError(CDERR_LOADRESFAILURE);
	    return FALSE;
	}
133
        size = SizeofResource(COMDLG32_hInstance, hResInfo);
134 135 136 137
        hGlobal16 = GlobalAlloc16(0, size);
        if (!hGlobal16)
        {
            COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
138
            ERR("alloc failure for %d bytes\n", size);
139 140 141 142 143 144 145 146 147 148
            return FALSE;
        }
        template = GlobalLock16(hGlobal16);
        if (!template)
        {
            COMDLG32_SetCommDlgExtendedError(CDERR_MEMLOCKFAILURE);
            ERR("global lock failure for %x handle\n", hGlobal16);
            GlobalFree16(hGlobal16);
            return FALSE;
        }
149
        ConvertDialog32To16(template32, size, template);
150 151
        priv->hDlgTmpl16 = hGlobal16;
        priv->hGlobal16 = hGlobal16;
152 153 154 155
    }
    return TRUE;
}

156 157 158
/************************************************************************
 *                              FD16_Init          [internal]
 *      called from the common 16/32 code to initialize 16 bit data
159
 */
160
static BOOL CALLBACK FD16_Init(LPARAM lParam, PFD31_DATA lfs, DWORD data)
161
{
162
    PFD16_PRIVATE priv;
163

164 165 166
    priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(FD16_PRIVATE));
    lfs->private1632 = priv;
    if (NULL == lfs->private1632) return FALSE;
167

168 169 170 171
    priv->ofn16 = MapSL(lParam);
    if (priv->ofn16->Flags & OFN_ENABLEHOOK)
        if (priv->ofn16->lpfnHook)
            lfs->hook = TRUE;
172

173
    lfs->ofnW = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*lfs->ofnW));
174
    FD16_MapOfnStruct16(priv->ofn16, lfs->ofnW, lfs->open);
175

176
    if (! FD16_GetTemplate(lfs)) return FALSE;
177

178
    return TRUE;
179 180 181
}

/***********************************************************************
182 183 184
 *                              FD16_CallWindowProc          [internal]
 *
 *      called from the common 16/32 code to call the appropriate hook
185
 */
186
static BOOL CALLBACK FD16_CallWindowProc(const FD31_DATA *lfs, UINT wMsg, WPARAM wParam,
187
                                 LPARAM lParam)
188
{
189
    PFD16_PRIVATE priv = (PFD16_PRIVATE) lfs->private1632;
190

191
    if (priv->ofn16)
192
    {
193 194 195
        return (BOOL16) CallWindowProc16(
          (WNDPROC16)priv->ofn16->lpfnHook, HWND_16(lfs->hwnd),
          (UINT16)wMsg, (WPARAM16)wParam, lParam);
196 197 198 199
    }
    return FALSE;
}

200

201
/***********************************************************************
202 203
 *                              FD31_UpdateResult            [internal]
 *          update the real client structures
204
 */
205
static void CALLBACK FD16_UpdateResult(const FD31_DATA *lfs)
206
{
207
    PFD16_PRIVATE priv = (PFD16_PRIVATE) lfs->private1632;
208
    LPOPENFILENAMEW ofnW = lfs->ofnW;
209

210
    if (priv->ofn16)
211 212
    { /* we have to convert to short (8.3) path */
	char tmp[1024]; /* MAX_PATHNAME_LEN */
213
	LPOPENFILENAME16 ofn16 = priv->ofn16;
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        char *dest = MapSL(ofn16->lpstrFile);
        char *bs16;
        if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFile, -1,
                                  tmp, sizeof(tmp), NULL, NULL ))
            tmp[sizeof(tmp)-1] = 0;
	GetShortPathNameA(tmp, dest, ofn16->nMaxFile);

	/* the same procedure as every year... */
        if((bs16 = strrchr(dest, '\\')) != NULL)
            ofn16->nFileOffset = bs16 - dest +1;
        else
            ofn16->nFileOffset = 0;
        ofn16->nFileExtension = 0;
        while(dest[ofn16->nFileExtension] != '.' && dest[ofn16->nFileExtension] != '\0')
            ofn16->nFileExtension++;
        if (dest[ofn16->nFileExtension] == '\0')
            ofn16->nFileExtension = 0;
        else
            ofn16->nFileExtension++;
    }
}

236

237
/***********************************************************************
238 239
 *                              FD16_UpdateFileTitle         [internal]
 *          update the real client structures
240
 */
241
static void CALLBACK FD16_UpdateFileTitle(const FD31_DATA *lfs)
242
{
243
    PFD16_PRIVATE priv = (PFD16_PRIVATE) lfs->private1632;
244
    LPOPENFILENAMEW ofnW = lfs->ofnW;
245 246

    if (priv->ofn16)
247
    {
248
        char *dest = MapSL(priv->ofn16->lpstrFileTitle);
249 250 251 252 253 254 255 256
        if (!WideCharToMultiByte( CP_ACP, 0, ofnW->lpstrFileTitle, -1,
                                  dest, ofnW->nMaxFileTitle, NULL, NULL ))
            dest[ofnW->nMaxFileTitle-1] = 0;
    }
}


/***********************************************************************
257 258
 *                              FD16_SendLbGetCurSel         [internal]
 *          retrieve selected listbox item
259
 */
260
static LRESULT CALLBACK FD16_SendLbGetCurSel(const FD31_DATA *lfs)
261
{
262
    return SendDlgItemMessageW(lfs->hwnd, lst1, LB_GETCURSEL16, 0, 0);
263 264 265
}


266 267 268
/************************************************************************
 *                              FD16_Destroy          [internal]
 *      called from the common 16/32 code to cleanup 32 bit data
269
 */
270
static void CALLBACK FD16_Destroy(const FD31_DATA *lfs)
271
{
272
    PFD16_PRIVATE priv = (PFD16_PRIVATE) lfs->private1632;
273

274 275
    /* free resources for a 16 bits dialog */
    if (NULL != priv)
276
    {
277 278
        if (priv->hResource16) FreeResource16(priv->hResource16);
        if (priv->hGlobal16)
279
        {
280 281
            GlobalUnlock16(priv->hGlobal16);
            GlobalFree16(priv->hGlobal16);
282
        }
283
        FD31_FreeOfnW(lfs->ofnW);
284
        HeapFree(GetProcessHeap(), 0, lfs->ofnW);
285 286 287
    }
}

288
static void FD16_SetupCallbacks(PFD31_CALLBACKS callbacks)
289
{
290 291 292 293 294 295
    callbacks->Init = FD16_Init;
    callbacks->CWP = FD16_CallWindowProc;
    callbacks->UpdateResult = FD16_UpdateResult;
    callbacks->UpdateFileTitle = FD16_UpdateFileTitle;
    callbacks->SendLbGetCurSel = FD16_SendLbGetCurSel;
    callbacks->Destroy = FD16_Destroy;
296 297 298
}

/***********************************************************************
299
 *                              FD16_MapDrawItemStruct       [internal]
300 301
 *      map a 16 bits drawitem struct to 32
 */
302
static void FD16_MapDrawItemStruct(const DRAWITEMSTRUCT16 *lpdis16, LPDRAWITEMSTRUCT lpdis)
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
{
    lpdis->CtlType = lpdis16->CtlType;
    lpdis->CtlID = lpdis16->CtlID;
    lpdis->itemID = lpdis16->itemID;
    lpdis->itemAction = lpdis16->itemAction;
    lpdis->itemState = lpdis16->itemState;
    lpdis->hwndItem = HWND_32(lpdis16->hwndItem);
    lpdis->hDC = HDC_32(lpdis16->hDC);
    lpdis->rcItem.right = lpdis16->rcItem.right;
    lpdis->rcItem.left = lpdis16->rcItem.left;
    lpdis->rcItem.top = lpdis16->rcItem.top;
    lpdis->rcItem.bottom = lpdis16->rcItem.bottom;
    lpdis->itemData = lpdis16->itemData;
}

318

319
/***********************************************************************
320
 *                              FD16_WMMeasureItem16         [internal]
321
 */
322
static LONG FD16_WMMeasureItem(HWND16 hWnd, WPARAM16 wParam, LPARAM lParam)
323 324 325 326
{
    LPMEASUREITEMSTRUCT16 lpmeasure;

    lpmeasure = MapSL(lParam);
327
    lpmeasure->itemHeight = FD31_GetFldrHeight();
328 329 330 331 332
    return TRUE;
}

/* ------------------ Dialog procedures ---------------------- */

333
/***********************************************************************
334
 *           FileOpenDlgProc   (COMMDLG.6)
335
 */
336
BOOL16 CALLBACK FileOpenDlgProc16(HWND16 hWnd16, UINT16 wMsg, WPARAM16 wParam, LPARAM lParam)
337 338
{
    HWND hWnd = HWND_32(hWnd16);
339
    PFD31_DATA lfs = (PFD31_DATA)GetPropA(hWnd,FD31_OFN_PROP);
340 341 342 343 344
    DRAWITEMSTRUCT dis;

    TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
    if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
        {
345
            LRESULT lRet = (BOOL16)FD31_CallWindowProc(lfs, wMsg, wParam, lParam);
346 347 348 349 350 351
            if (lRet)
                return lRet;         /* else continue message processing */
        }
    switch (wMsg)
    {
    case WM_INITDIALOG:
352
        return FD31_WMInitDialog(hWnd, wParam, lParam);
353 354

    case WM_MEASUREITEM:
355
        return FD16_WMMeasureItem(hWnd16, wParam, lParam);
356 357

    case WM_DRAWITEM:
358 359
        FD16_MapDrawItemStruct(MapSL(lParam), &dis);
        return FD31_WMDrawItem(hWnd, wParam, lParam, FALSE, &dis);
360 361

    case WM_COMMAND:
362
        return FD31_WMCommand(hWnd, lParam, HIWORD(lParam),wParam, lfs);
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
#if 0
    case WM_CTLCOLOR:
         SetBkColor((HDC16)wParam, 0x00C0C0C0);
         switch (HIWORD(lParam))
         {
	 case CTLCOLOR_BTN:
	     SetTextColor((HDC16)wParam, 0x00000000);
             return hGRAYBrush;
	case CTLCOLOR_STATIC:
             SetTextColor((HDC16)wParam, 0x00000000);
             return hGRAYBrush;
	}
      break;
#endif
    }
    return FALSE;
}

/***********************************************************************
382
 *           FileSaveDlgProc   (COMMDLG.7)
383
 */
384
BOOL16 CALLBACK FileSaveDlgProc16(HWND16 hWnd16, UINT16 wMsg, WPARAM16 wParam, LPARAM lParam)
385 386
{
 HWND hWnd = HWND_32(hWnd16);
387
 PFD31_DATA lfs = (PFD31_DATA)GetPropA(hWnd,FD31_OFN_PROP);
388 389 390 391 392 393
 DRAWITEMSTRUCT dis;

 TRACE("msg=%x wparam=%x lParam=%lx\n", wMsg, wParam, lParam);
 if ((wMsg != WM_INITDIALOG) && lfs && lfs->hook)
  {
   LRESULT  lRet;
394
   lRet = (BOOL16)FD31_CallWindowProc(lfs, wMsg, wParam, lParam);
395 396 397 398 399
   if (lRet)
    return lRet;         /* else continue message processing */
  }
  switch (wMsg) {
   case WM_INITDIALOG:
400
      return FD31_WMInitDialog(hWnd, wParam, lParam);
401 402

   case WM_MEASUREITEM:
403
      return FD16_WMMeasureItem(hWnd16, wParam, lParam);
404 405

   case WM_DRAWITEM:
406 407
      FD16_MapDrawItemStruct(MapSL(lParam), &dis);
      return FD31_WMDrawItem(hWnd, wParam, lParam, TRUE, &dis);
408 409

   case WM_COMMAND:
410
      return FD31_WMCommand(hWnd, lParam, HIWORD(lParam), wParam, lfs);
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
  }

  /*
  case WM_CTLCOLOR:
   SetBkColor((HDC16)wParam, 0x00C0C0C0);
   switch (HIWORD(lParam))
   {
    case CTLCOLOR_BTN:
     SetTextColor((HDC16)wParam, 0x00000000);
     return hGRAYBrush;
    case CTLCOLOR_STATIC:
     SetTextColor((HDC16)wParam, 0x00000000);
     return hGRAYBrush;
   }
   return FALSE;

   */
  return FALSE;
}

/* ------------------ APIs ---------------------- */

/***********************************************************************
 *           GetOpenFileName   (COMMDLG.1)
 *
 * Creates a dialog box for the user to select a file to open.
 *
 * RETURNS
 *    TRUE on success: user selected a valid file
 *    FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
 *
 * BUGS
 *    unknown, there are some FIXME's left.
 */
BOOL16 WINAPI GetOpenFileName16(
				SEGPTR ofn /* [in/out] address of structure with data*/
				)
{
    HINSTANCE16 hInst;
    BOOL bRet = FALSE;
    LPOPENFILENAME16 lpofn = MapSL(ofn);
452
    PFD31_DATA lfs;
453
    FARPROC16 ptr;
454 455
    FD31_CALLBACKS callbacks;
    PFD16_PRIVATE priv;
456

457
    if (!lpofn || !FD31_Init()) return FALSE;
458

459 460
    FD16_SetupCallbacks(&callbacks);
    lfs = FD31_AllocPrivate((LPARAM) ofn, OPEN_DIALOG, &callbacks, 0);
461 462
    if (lfs)
    {
463
        priv = (PFD16_PRIVATE) lfs->private1632;
464
        hInst = GetWindowLongPtrA( HWND_32(lpofn->hwndOwner), GWLP_HINSTANCE );
465
        ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (LPCSTR) 6);
466
        bRet = DialogBoxIndirectParam16( hInst, priv->hDlgTmpl16, lpofn->hwndOwner,
467
                                         (DLGPROC16) ptr, (LPARAM) lfs);
468
        FD31_DestroyPrivate(lfs);
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    }

    TRACE("return lpstrFile='%s' !\n", (char *)MapSL(lpofn->lpstrFile));
    return bRet;
}

/***********************************************************************
 *           GetSaveFileName   (COMMDLG.2)
 *
 * Creates a dialog box for the user to select a file to save.
 *
 * RETURNS
 *    TRUE on success: user enters a valid file
 *    FALSE on cancel, error, close or filename-does-not-fit-in-buffer.
 *
 * BUGS
 *    unknown. There are some FIXME's left.
 */
BOOL16 WINAPI GetSaveFileName16(
				SEGPTR ofn /* [in/out] addess of structure with data*/
				)
{
    HINSTANCE16 hInst;
    BOOL bRet = FALSE;
    LPOPENFILENAME16 lpofn = MapSL(ofn);
494
    PFD31_DATA lfs;
495
    FARPROC16 ptr;
496 497
    FD31_CALLBACKS callbacks;
    PFD16_PRIVATE priv;
498

499
    if (!lpofn || !FD31_Init()) return FALSE;
500

501 502
    FD16_SetupCallbacks(&callbacks);
    lfs = FD31_AllocPrivate((LPARAM) ofn, SAVE_DIALOG, &callbacks, 0);
503 504
    if (lfs)
    {
505
        priv = (PFD16_PRIVATE) lfs->private1632;
506
        hInst = GetWindowLongPtrA( HWND_32(lpofn->hwndOwner), GWLP_HINSTANCE );
507
        ptr = GetProcAddress16(GetModuleHandle16("COMMDLG"), (LPCSTR) 7);
508
        bRet = DialogBoxIndirectParam16( hInst, priv->hDlgTmpl16, lpofn->hwndOwner,
509
                                         (DLGPROC16) ptr, (LPARAM) lfs);
510
        FD31_DestroyPrivate(lfs);
511 512 513 514 515
    }

    TRACE("return lpstrFile='%s' !\n", (char *)MapSL(lpofn->lpstrFile));
    return bRet;
}
516 517 518 519 520 521 522 523 524


/***********************************************************************
 *	GetFileTitle		(COMMDLG.27)
 */
short WINAPI GetFileTitle16(LPCSTR lpFile, LPSTR lpTitle, UINT16 cbBuf)
{
	return GetFileTitleA(lpFile, lpTitle, cbBuf);
}