egcs 'ambiguous else' warnings fixes.
[wine] / misc / commdlg.c
1 /*
2  * COMMDLG functions
3  *
4  * Copyright 1994 Martin Ayotte
5  * Copyright 1996 Albrecht Kleine
6  */
7
8 #include <ctype.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "win.h"
12 #include "heap.h"
13 #include "message.h"
14 #include "commdlg.h"
15 #include "dialog.h"
16 #include "dlgs.h"
17 #include "module.h"
18 #include "resource.h"
19 #include "drive.h"
20 #include "debug.h"
21 #include "font.h"
22 #include "winproc.h"
23
24 static  DWORD           CommDlgLastError = 0;
25
26 static HBITMAP16 hFolder = 0;
27 static HBITMAP16 hFolder2 = 0;
28 static HBITMAP16 hFloppy = 0;
29 static HBITMAP16 hHDisk = 0;
30 static HBITMAP16 hCDRom = 0;
31 static HBITMAP16 hBitmapTT = 0;
32 static const char defaultfilter[]=" \0\0";
33
34 /***********************************************************************
35  *                              FileDlg_Init                    [internal]
36  */
37 static BOOL32 FileDlg_Init()
38 {
39     static BOOL32 initialized = 0;
40     
41     if (!initialized) {
42         if (!hFolder) hFolder = LoadBitmap16(0, MAKEINTRESOURCE16(OBM_FOLDER));
43         if (!hFolder2) hFolder2 = LoadBitmap16(0, MAKEINTRESOURCE16(OBM_FOLDER2));
44         if (!hFloppy) hFloppy = LoadBitmap16(0, MAKEINTRESOURCE16(OBM_FLOPPY));
45         if (!hHDisk) hHDisk = LoadBitmap16(0, MAKEINTRESOURCE16(OBM_HDISK));
46         if (!hCDRom) hCDRom = LoadBitmap16(0, MAKEINTRESOURCE16(OBM_CDROM));
47         if (hFolder == 0 || hFolder2 == 0 || hFloppy == 0 || 
48             hHDisk == 0 || hCDRom == 0)
49         {       
50             WARN(commdlg, "Error loading bitmaps !\nprin");
51             return FALSE;
52         }
53         initialized = TRUE;
54     }
55     return TRUE;
56 }
57
58 /***********************************************************************
59  *           GetOpenFileName   (COMMDLG.1)
60  */
61 BOOL16 WINAPI GetOpenFileName16( SEGPTR ofn )
62 {
63     HINSTANCE32 hInst;
64     HANDLE32 hDlgTmpl = 0, hResInfo;
65     BOOL32 bRet = FALSE, win32Format = FALSE;
66     HWND32 hwndDialog;
67     LPOPENFILENAME16 lpofn = (LPOPENFILENAME16)PTR_SEG_TO_LIN(ofn);
68     LPCVOID template;
69     char defaultopen[]="Open File";
70     char *str=0,*str1=0;
71
72     if (!lpofn || !FileDlg_Init()) return FALSE;
73
74     if (lpofn->Flags & OFN_WINE32) {
75             if (lpofn->Flags & OFN_ENABLETEMPLATEHANDLE)
76             {
77                 if (!(template = LockResource32( MapHModuleSL(lpofn->hInstance ))))
78                 {
79                     CommDlgLastError = CDERR_LOADRESFAILURE;
80                     return FALSE;
81                 }
82             }
83             else if (lpofn->Flags & OFN_ENABLETEMPLATE)
84             {
85                 if (!(hResInfo = FindResource32A(MapHModuleSL(lpofn->hInstance),
86                                                 PTR_SEG_TO_LIN(lpofn->lpTemplateName), RT_DIALOG32A)))
87                 {
88                     CommDlgLastError = CDERR_FINDRESFAILURE;
89                     return FALSE;
90                 }
91                 if (!(hDlgTmpl = LoadResource32( MapHModuleSL(lpofn->hInstance),
92                                                  hResInfo )) ||
93                     !(template = LockResource32( hDlgTmpl )))
94                 {
95                     CommDlgLastError = CDERR_LOADRESFAILURE;
96                     return FALSE;
97                 }
98             } else {
99                 template = SYSRES_GetResPtr( SYSRES_DIALOG_OPEN_FILE );
100             }
101             win32Format = TRUE;
102     } else {
103             if (lpofn->Flags & OFN_ENABLETEMPLATEHANDLE)
104             {
105                 if (!(template = LockResource16( lpofn->hInstance )))
106                 {
107                     CommDlgLastError = CDERR_LOADRESFAILURE;
108                     return FALSE;
109                 }
110             }
111             else if (lpofn->Flags & OFN_ENABLETEMPLATE)
112             {
113                 if (!(hResInfo = FindResource16(lpofn->hInstance,
114                                                 lpofn->lpTemplateName,
115                                                 RT_DIALOG16)))
116                 {
117                     CommDlgLastError = CDERR_FINDRESFAILURE;
118                     return FALSE;
119                 }
120                 if (!(hDlgTmpl = LoadResource16( lpofn->hInstance, hResInfo )) ||
121                     !(template = LockResource16( hDlgTmpl )))
122                 {
123                     CommDlgLastError = CDERR_LOADRESFAILURE;
124                     return FALSE;
125                 }
126             } else {
127                 template = SYSRES_GetResPtr( SYSRES_DIALOG_OPEN_FILE );
128                 win32Format = TRUE;
129             }
130     }
131
132     hInst = WIN_GetWindowInstance( lpofn->hwndOwner );
133
134     if (!(lpofn->lpstrFilter))
135       {
136        str = SEGPTR_ALLOC(sizeof(defaultfilter));
137        TRACE(commdlg,"Alloc %p default for Filetype in GetOpenFileName\n",str);
138        memcpy(str,defaultfilter,sizeof(defaultfilter));
139        lpofn->lpstrFilter=SEGPTR_GET(str);
140       }
141
142     if (!(lpofn->lpstrTitle))
143       {
144        str1 = SEGPTR_ALLOC(strlen(defaultopen)+1);
145        TRACE(commdlg,"Alloc %p default for Title in GetOpenFileName\n",str1);
146        strcpy(str1,defaultopen);
147        lpofn->lpstrTitle=SEGPTR_GET(str1);
148       }
149
150     /* FIXME: doesn't handle win32 format correctly yet */
151     hwndDialog = DIALOG_CreateIndirect( hInst, template, win32Format,
152                                         lpofn->hwndOwner,
153                                         (DLGPROC16)MODULE_GetWndProcEntry16("FileOpenDlgProc"),
154                                         ofn, WIN_PROC_16 );
155     if (hwndDialog) bRet = DIALOG_DoDialogBox( hwndDialog, lpofn->hwndOwner );
156
157     if (str1)
158       {
159        TRACE(commdlg,"Freeing %p default for Title in GetOpenFileName\n",str1);
160         SEGPTR_FREE(str1);
161        lpofn->lpstrTitle=0;
162       }
163
164     if (str)
165       {
166        TRACE(commdlg,"Freeing %p default for Filetype in GetOpenFileName\n",str);
167         SEGPTR_FREE(str);
168        lpofn->lpstrFilter=0;
169       }
170
171     if (hDlgTmpl) {
172             if (lpofn->Flags & OFN_WINE32)
173                     FreeResource32( hDlgTmpl );
174             else
175                     FreeResource16( hDlgTmpl );
176     }
177
178     TRACE(commdlg,"return lpstrFile='%s' !\n", 
179            (LPSTR)PTR_SEG_TO_LIN(lpofn->lpstrFile));
180     return bRet;
181 }
182
183
184 /***********************************************************************
185  *           GetSaveFileName   (COMMDLG.2)
186  */
187 BOOL16 WINAPI GetSaveFileName16( SEGPTR ofn)
188 {
189     HINSTANCE32 hInst;
190     HANDLE32 hDlgTmpl = 0;
191     BOOL32 bRet = FALSE, win32Format = FALSE;
192     LPOPENFILENAME16 lpofn = (LPOPENFILENAME16)PTR_SEG_TO_LIN(ofn);
193     LPCVOID template;
194     HWND32 hwndDialog;
195     char defaultsave[]="Save as";
196     char *str =0,*str1=0;
197
198     if (!lpofn || !FileDlg_Init()) return FALSE;
199
200     if (lpofn->Flags & OFN_WINE32) {
201             if (lpofn->Flags & OFN_ENABLETEMPLATEHANDLE)
202             {
203                 if (!(template = LockResource32( MapHModuleSL(lpofn->hInstance ))))
204                 {
205                     CommDlgLastError = CDERR_LOADRESFAILURE;
206                     return FALSE;
207                 }
208             }
209             else if (lpofn->Flags & OFN_ENABLETEMPLATE)
210             {
211                 HANDLE32 hResInfo;
212                 if (!(hResInfo = FindResource32A(MapHModuleSL(lpofn->hInstance),
213                                                  PTR_SEG_TO_LIN(lpofn->lpTemplateName),
214                                                  RT_DIALOG32A)))
215                 {
216                     CommDlgLastError = CDERR_FINDRESFAILURE;
217                     return FALSE;
218                 }
219                 if (!(hDlgTmpl = LoadResource32(MapHModuleSL(lpofn->hInstance),
220                                                 hResInfo)) ||
221                     !(template = LockResource32(hDlgTmpl)))
222                 {
223                     CommDlgLastError = CDERR_LOADRESFAILURE;
224                     return FALSE;
225                 }
226                 win32Format= TRUE;
227             } else {
228                 template = SYSRES_GetResPtr( SYSRES_DIALOG_SAVE_FILE );
229                 win32Format = TRUE;
230             }
231     } else {
232             if (lpofn->Flags & OFN_ENABLETEMPLATEHANDLE)
233             {
234                 if (!(template = LockResource16( lpofn->hInstance )))
235                 {
236                     CommDlgLastError = CDERR_LOADRESFAILURE;
237                     return FALSE;
238                 }
239             }
240             else if (lpofn->Flags & OFN_ENABLETEMPLATE)
241             {
242                 HANDLE16 hResInfo;
243                 if (!(hResInfo = FindResource16(lpofn->hInstance,
244                                                 lpofn->lpTemplateName,
245                                                 RT_DIALOG16)))
246                 {
247                     CommDlgLastError = CDERR_FINDRESFAILURE;
248                     return FALSE;
249                 }
250                 if (!(hDlgTmpl = LoadResource16( lpofn->hInstance, hResInfo )) ||
251                     !(template = LockResource16( hDlgTmpl )))
252                 {
253                     CommDlgLastError = CDERR_LOADRESFAILURE;
254                     return FALSE;
255                 }
256         } else {
257                 template = SYSRES_GetResPtr( SYSRES_DIALOG_SAVE_FILE );
258                 win32Format = TRUE;
259         }
260     }
261
262     hInst = WIN_GetWindowInstance( lpofn->hwndOwner );
263
264     if (!(lpofn->lpstrFilter))
265       {
266        str = SEGPTR_ALLOC(sizeof(defaultfilter));
267        TRACE(commdlg,"Alloc default for Filetype in GetSaveFileName\n");
268        memcpy(str,defaultfilter,sizeof(defaultfilter));
269        lpofn->lpstrFilter=SEGPTR_GET(str);
270       }
271
272     if (!(lpofn->lpstrTitle))
273       {
274        str1 = SEGPTR_ALLOC(sizeof(defaultsave)+1);
275        TRACE(commdlg,"Alloc default for Title in GetSaveFileName\n");
276        strcpy(str1,defaultsave);
277        lpofn->lpstrTitle=SEGPTR_GET(str1);
278       }
279
280     hwndDialog = DIALOG_CreateIndirect( hInst, template, win32Format,
281                                         lpofn->hwndOwner,
282                                         (DLGPROC16)MODULE_GetWndProcEntry16("FileSaveDlgProc"),
283                                         ofn, WIN_PROC_16 );
284     if (hwndDialog) bRet = DIALOG_DoDialogBox( hwndDialog, lpofn->hwndOwner );
285
286     if (str1)
287       {
288        TRACE(commdlg,"Freeing %p default for Title in GetSaveFileName\n",str1);
289         SEGPTR_FREE(str1);
290        lpofn->lpstrTitle=0;
291       }
292  
293     if (str)
294       {
295        TRACE(commdlg,"Freeing %p default for Filetype in GetSaveFileName\n",str);
296         SEGPTR_FREE(str);
297        lpofn->lpstrFilter=0;
298       }
299     
300     if (hDlgTmpl) {
301             if (lpofn->Flags & OFN_WINE32)
302                     FreeResource32( hDlgTmpl );
303             else
304                     FreeResource16( hDlgTmpl );
305     }
306
307     TRACE(commdlg, "return lpstrFile='%s' !\n", 
308             (LPSTR)PTR_SEG_TO_LIN(lpofn->lpstrFile));
309     return bRet;
310 }
311
312 /***********************************************************************
313  *                              FILEDLG_StripEditControl        [internal]
314  * Strip pathnames off the contents of the edit control.
315  */
316 static void FILEDLG_StripEditControl(HWND16 hwnd)
317 {
318     char temp[512], *cp;
319
320     GetDlgItemText32A( hwnd, edt1, temp, sizeof(temp) );
321     cp = strrchr(temp, '\\');
322     if (cp != NULL) {
323         strcpy(temp, cp+1);
324     }
325     cp = strrchr(temp, ':');
326     if (cp != NULL) {
327         strcpy(temp, cp+1);
328     }
329     /* FIXME: shouldn't we do something with the result here? ;-) */
330 }
331
332 /***********************************************************************
333  *                              FILEDLG_ScanDir                 [internal]
334  */
335 static BOOL32 FILEDLG_ScanDir(HWND16 hWnd, LPSTR newPath)
336 {
337     char                buffer[512];
338          char*                  str = buffer;
339     int                         drive;
340     HWND32              hlb;
341
342     lstrcpyn32A(buffer, newPath, sizeof(buffer));
343
344     if (str[0] && (str[1] == ':')) {
345         drive = toupper(str[0]) - 'A';
346         str += 2;
347         if (!DRIVE_SetCurrentDrive(drive)) 
348                           return FALSE;
349     } else {
350                  drive = DRIVE_GetCurrentDrive();
351          }
352
353     if (str[0] && !DRIVE_Chdir(drive, str)) {
354                  return FALSE;
355     }
356
357     GetDlgItemText32A(hWnd, edt1, buffer, sizeof(buffer));
358     if ((hlb = GetDlgItem32(hWnd, lst1)) != 0) {
359                  char*  scptr; /* ptr on semi-colon */
360                  char*  filter = buffer;
361
362                  TRACE(commdlg, "Using filter %s\n", filter);
363                  SendMessage32A(hlb, LB_RESETCONTENT32, 0, 0);
364                  while (filter) {
365                          scptr = strchr(filter, ';');
366                          if (scptr)     *scptr = 0;
367                          TRACE(commdlg, "Using file spec %s\n", filter);
368                          if (SendMessage32A(hlb, LB_DIR32, 0, (LPARAM)filter) == LB_ERR)
369                                  return FALSE;
370                          if (scptr) *scptr = ';';
371                          filter = (scptr) ? (scptr + 1) : 0;
372                  }
373          }
374
375     strcpy(buffer, "*.*");
376     return DlgDirList32A(hWnd, buffer, lst2, stc1, 0x8010);
377 }
378
379 /***********************************************************************
380  *                              FILEDLG_GetFileType             [internal]
381  */
382
383 static LPSTR FILEDLG_GetFileType(LPSTR cfptr, LPSTR fptr, WORD index)
384 {
385   int n, i;
386   i = 0;
387   if (cfptr)
388     for ( ;(n = strlen(cfptr)) != 0; i++) 
389       {
390         cfptr += n + 1;
391         if (i == index)
392           return cfptr;
393         cfptr += strlen(cfptr) + 1;
394       }
395   if (fptr)
396     for ( ;(n = strlen(fptr)) != 0; i++) 
397       {
398         fptr += n + 1;
399         if (i == index)
400           return fptr;
401         fptr += strlen(fptr) + 1;
402     }
403   return "*.*"; /* FIXME */
404 }
405
406 /***********************************************************************
407  *                              FILEDLG_WMDrawItem              [internal]
408  */
409 static LONG FILEDLG_WMDrawItem(HWND16 hWnd, WPARAM16 wParam, LPARAM lParam,int savedlg)
410 {
411     LPDRAWITEMSTRUCT16 lpdis = (LPDRAWITEMSTRUCT16)PTR_SEG_TO_LIN(lParam);
412     char *str;
413     HBRUSH32 hBrush;
414     HBITMAP16 hBitmap, hPrevBitmap;
415     BITMAP16 bm;
416     HDC32 hMemDC;
417
418     if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst1)
419     {
420         if (!(str = SEGPTR_ALLOC(512))) return FALSE;
421         hBrush = SelectObject32(lpdis->hDC, GetStockObject32(LTGRAY_BRUSH));
422         SelectObject32(lpdis->hDC, hBrush);
423         FillRect16(lpdis->hDC, &lpdis->rcItem, hBrush);
424         SendMessage16(lpdis->hwndItem, LB_GETTEXT16, lpdis->itemID, 
425                       (LPARAM)SEGPTR_GET(str));
426
427         if (savedlg)       /* use _gray_ text in FileSaveDlg */
428         {
429           if (!lpdis->itemState)
430             SetTextColor32(lpdis->hDC,GetSysColor32(COLOR_GRAYTEXT) );
431           else  
432             SetTextColor32(lpdis->hDC,GetSysColor32(COLOR_WINDOWTEXT) );
433             /* inversion of gray would be bad readable */                 
434         }
435
436         TextOut16(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
437                   str, strlen(str));
438         if (lpdis->itemState != 0) {
439             InvertRect16(lpdis->hDC, &lpdis->rcItem);
440         }
441         SEGPTR_FREE(str);
442         return TRUE;
443     }
444     
445     if (lpdis->CtlType == ODT_LISTBOX && lpdis->CtlID == lst2)
446     {
447         if (!(str = SEGPTR_ALLOC(512))) return FALSE;
448         hBrush = SelectObject32(lpdis->hDC, GetStockObject32(LTGRAY_BRUSH));
449         SelectObject32(lpdis->hDC, hBrush);
450         FillRect16(lpdis->hDC, &lpdis->rcItem, hBrush);
451         SendMessage16(lpdis->hwndItem, LB_GETTEXT16, lpdis->itemID, 
452                       (LPARAM)SEGPTR_GET(str));
453
454         hBitmap = hFolder;
455         GetObject16( hBitmap, sizeof(bm), &bm );
456         TextOut16(lpdis->hDC, lpdis->rcItem.left + bm.bmWidth, 
457                   lpdis->rcItem.top, str, strlen(str));
458         hMemDC = CreateCompatibleDC32(lpdis->hDC);
459         hPrevBitmap = SelectObject32(hMemDC, hBitmap);
460         BitBlt32(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
461                  bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
462         SelectObject32(hMemDC, hPrevBitmap);
463         DeleteDC32(hMemDC);
464         if (lpdis->itemState != 0) InvertRect16(lpdis->hDC, &lpdis->rcItem);
465         SEGPTR_FREE(str);
466         return TRUE;
467     }
468     if (lpdis->CtlType == ODT_COMBOBOX && lpdis->CtlID == cmb2)
469     {
470         if (!(str = SEGPTR_ALLOC(512))) return FALSE;
471         hBrush = SelectObject32(lpdis->hDC, GetStockObject32(LTGRAY_BRUSH));
472         SelectObject32(lpdis->hDC, hBrush);
473         FillRect16(lpdis->hDC, &lpdis->rcItem, hBrush);
474         SendMessage16(lpdis->hwndItem, CB_GETLBTEXT16, lpdis->itemID, 
475                       (LPARAM)SEGPTR_GET(str));
476         switch(DRIVE_GetType( str[2] - 'a' ))
477         {
478         case TYPE_FLOPPY:  hBitmap = hFloppy; break;
479         case TYPE_CDROM:   hBitmap = hCDRom; break;
480         case TYPE_HD:
481         case TYPE_NETWORK:
482         default:           hBitmap = hHDisk; break;
483         }
484         GetObject16( hBitmap, sizeof(bm), &bm );
485         TextOut16(lpdis->hDC, lpdis->rcItem.left + bm.bmWidth, 
486                   lpdis->rcItem.top, str, strlen(str));
487         hMemDC = CreateCompatibleDC32(lpdis->hDC);
488         hPrevBitmap = SelectObject32(hMemDC, hBitmap);
489         BitBlt32( lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
490                   bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY );
491         SelectObject32(hMemDC, hPrevBitmap);
492         DeleteDC32(hMemDC);
493         if (lpdis->itemState != 0) InvertRect16(lpdis->hDC, &lpdis->rcItem);
494         SEGPTR_FREE(str);
495         return TRUE;
496     }
497     return FALSE;
498 }
499
500 /***********************************************************************
501  *                              FILEDLG_WMMeasureItem           [internal]
502  */
503 static LONG FILEDLG_WMMeasureItem(HWND16 hWnd, WPARAM16 wParam, LPARAM lParam) 
504 {
505     BITMAP16 bm;
506     LPMEASUREITEMSTRUCT16 lpmeasure;
507     
508     GetObject16( hFolder2, sizeof(bm), &bm );
509     lpmeasure = (LPMEASUREITEMSTRUCT16)PTR_SEG_TO_LIN(lParam);
510     lpmeasure->itemHeight = bm.bmHeight;
511     return TRUE;
512 }
513
514 /***********************************************************************
515  *                              FILEDLG_HookCallChk             [internal]
516  */
517 static int FILEDLG_HookCallChk(LPOPENFILENAME16 lpofn)
518 {
519  if (lpofn)
520   if (lpofn->Flags & OFN_ENABLEHOOK)
521    if (lpofn->lpfnHook)
522     return 1;
523  return 0;   
524
525
526 /***********************************************************************
527  *                              FILEDLG_CallWindowProc             [internal]
528  *
529  * Adapt the structures back for win32 calls so the callee can read lpCustData
530  */
531 static BOOL32 FILEDLG_CallWindowProc(LPOPENFILENAME16 lpofn,HWND32 hwnd,
532         UINT32 wMsg,WPARAM32 wParam,LPARAM lParam
533
534 ) {
535         BOOL32  needstruct;
536
537         needstruct = (PTR_SEG_TO_LIN(lParam) == lpofn);
538
539         if (!(lpofn->Flags & OFN_WINE32))
540                 return (BOOL32)CallWindowProc16(
541                         lpofn->lpfnHook,hwnd,(UINT16)wMsg,(WPARAM16)wParam,lParam
542                 );
543         /* |OFN_WINE32 */
544         if (lpofn->Flags & OFN_UNICODE) {
545                 if (needstruct) {
546                     OPENFILENAME32W ofnw;
547
548                     /* FIXME: probably needs more converted */
549                     ofnw.lCustData = lpofn->lCustData;
550                     return (BOOL32)CallWindowProc32W(
551                             (WNDPROC32)lpofn->lpfnHook,hwnd,wMsg,wParam,(LPARAM)&ofnw
552                     );
553                 } else
554                     return (BOOL32)CallWindowProc32W(
555                             (WNDPROC32)lpofn->lpfnHook,hwnd,wMsg,wParam,lParam
556                     );
557         }
558         /* ! |OFN_UNICODE */
559         if (needstruct) {
560                 OPENFILENAME32A ofna;
561
562                 /* FIXME: probably needs more converted */
563                 ofna.lCustData = lpofn->lCustData;
564                 return (BOOL32)CallWindowProc32A(
565                         (WNDPROC32)lpofn->lpfnHook,hwnd,wMsg,wParam,(LPARAM)&ofna
566                 );
567         } else
568                 return (BOOL32)CallWindowProc32A(
569                         (WNDPROC32)lpofn->lpfnHook,hwnd,wMsg,wParam,lParam
570                 );
571
572 }
573
574 /***********************************************************************
575  *                              FILEDLG_WMInitDialog            [internal]
576  */
577
578 static LONG FILEDLG_WMInitDialog(HWND16 hWnd, WPARAM16 wParam, LPARAM lParam) 
579 {
580   int i, n;
581   LPOPENFILENAME16 lpofn;
582   char tmpstr[512];
583   LPSTR pstr, old_pstr;
584   SetWindowLong32A(hWnd, DWL_USER, lParam);
585   lpofn = (LPOPENFILENAME16)PTR_SEG_TO_LIN(lParam);
586   if (lpofn->lpstrTitle) SetWindowText16( hWnd, lpofn->lpstrTitle );
587   /* read custom filter information */
588   if (lpofn->lpstrCustomFilter)
589     {
590       pstr = (LPSTR)PTR_SEG_TO_LIN(lpofn->lpstrCustomFilter);
591       n = 0;
592       TRACE(commdlg,"lpstrCustomFilter = %p\n", pstr);
593       while(*pstr)
594         {
595           old_pstr = pstr;
596           i = SendDlgItemMessage16(hWnd, cmb1, CB_ADDSTRING16, 0,
597                                    (LPARAM)lpofn->lpstrCustomFilter + n );
598           n += strlen(pstr) + 1;
599           pstr += strlen(pstr) + 1;
600           TRACE(commdlg,"add str='%s' "
601                           "associated to '%s'\n", old_pstr, pstr);
602           SendDlgItemMessage16(hWnd, cmb1, CB_SETITEMDATA16, i, (LPARAM)pstr);
603           n += strlen(pstr) + 1;
604           pstr += strlen(pstr) + 1;
605         }
606     }
607   /* read filter information */
608   if (lpofn->lpstrFilter) {
609         pstr = (LPSTR)PTR_SEG_TO_LIN(lpofn->lpstrFilter);
610         n = 0;
611         while(*pstr) {
612           old_pstr = pstr;
613           i = SendDlgItemMessage16(hWnd, cmb1, CB_ADDSTRING16, 0,
614                                        (LPARAM)lpofn->lpstrFilter + n );
615           n += strlen(pstr) + 1;
616           pstr += strlen(pstr) + 1;
617           TRACE(commdlg,"add str='%s' "
618                           "associated to '%s'\n", old_pstr, pstr);
619           SendDlgItemMessage16(hWnd, cmb1, CB_SETITEMDATA16, i, (LPARAM)pstr);
620           n += strlen(pstr) + 1;
621           pstr += strlen(pstr) + 1;
622         }
623   }
624   /* set default filter */
625   if (lpofn->nFilterIndex == 0 && lpofn->lpstrCustomFilter == (SEGPTR)NULL)
626         lpofn->nFilterIndex = 1;
627   SendDlgItemMessage16(hWnd, cmb1, CB_SETCURSEL16, lpofn->nFilterIndex - 1, 0);    
628   strncpy(tmpstr, FILEDLG_GetFileType(PTR_SEG_TO_LIN(lpofn->lpstrCustomFilter),
629              PTR_SEG_TO_LIN(lpofn->lpstrFilter), lpofn->nFilterIndex - 1),511);
630   tmpstr[511]=0;
631   TRACE(commdlg,"nFilterIndex = %ld, SetText of edt1 to '%s'\n", 
632                         lpofn->nFilterIndex, tmpstr);
633   SetDlgItemText32A( hWnd, edt1, tmpstr );
634   /* get drive list */
635   *tmpstr = 0;
636   DlgDirListComboBox32A(hWnd, tmpstr, cmb2, 0, 0xC000);
637   /* read initial directory */
638   if (PTR_SEG_TO_LIN(lpofn->lpstrInitialDir) != NULL) 
639     {
640       strncpy(tmpstr, PTR_SEG_TO_LIN(lpofn->lpstrInitialDir), 510);
641       tmpstr[510]=0;
642       if (strlen(tmpstr) > 0 && tmpstr[strlen(tmpstr)-1] != '\\' 
643           && tmpstr[strlen(tmpstr)-1] != ':')
644         strcat(tmpstr,"\\");
645     }
646   else
647     *tmpstr = 0;
648   if (!FILEDLG_ScanDir(hWnd, tmpstr)) {
649     *tmpstr = 0;
650     if (!FILEDLG_ScanDir(hWnd, tmpstr))
651       WARN(commdlg, "Couldn't read initial directory %s!\n",tmpstr);
652   }
653   /* select current drive in combo 2, omit missing drives */
654   for(i=0, n=-1; i<=DRIVE_GetCurrentDrive(); i++)
655     if (DRIVE_IsValid(i))                  n++;
656   SendDlgItemMessage16(hWnd, cmb2, CB_SETCURSEL16, n, 0);
657   if (!(lpofn->Flags & OFN_SHOWHELP))
658     ShowWindow32(GetDlgItem32(hWnd, pshHelp), SW_HIDE);
659   if (lpofn->Flags & OFN_HIDEREADONLY)
660     ShowWindow32(GetDlgItem32(hWnd, chx1), SW_HIDE); 
661   if (FILEDLG_HookCallChk(lpofn))
662      return (BOOL16)FILEDLG_CallWindowProc(lpofn,hWnd,WM_INITDIALOG,wParam,lParam );
663   else  
664      return TRUE;
665 }
666
667 /***********************************************************************
668  *                              FILEDLG_WMCommand               [internal]
669  */
670 BOOL32 in_update=FALSE;
671
672 static LRESULT FILEDLG_WMCommand(HWND16 hWnd, WPARAM16 wParam, LPARAM lParam) 
673 {
674   LONG lRet;
675   LPOPENFILENAME16 lpofn;
676   OPENFILENAME16 ofn2;
677   char tmpstr[512], tmpstr2[512];
678   LPSTR pstr, pstr2;
679   UINT16 control,notification;
680
681   /* Notifications are packaged differently in Win32 */
682   control = wParam;
683   notification = HIWORD(lParam);
684     
685   lpofn = (LPOPENFILENAME16)PTR_SEG_TO_LIN(GetWindowLong32A(hWnd, DWL_USER));
686   switch (control)
687     {
688     case lst1: /* file list */
689       FILEDLG_StripEditControl(hWnd);
690       if (notification == LBN_DBLCLK)
691         goto almost_ok;
692       lRet = SendDlgItemMessage16(hWnd, lst1, LB_GETCURSEL16, 0, 0);
693       if (lRet == LB_ERR) return TRUE;
694       if ((pstr = SEGPTR_ALLOC(512)))
695       {
696           SendDlgItemMessage16(hWnd, lst1, LB_GETTEXT16, lRet,
697                                (LPARAM)SEGPTR_GET(pstr));
698           SetDlgItemText32A( hWnd, edt1, pstr );
699           SEGPTR_FREE(pstr);
700       }
701       if (FILEDLG_HookCallChk(lpofn))
702        FILEDLG_CallWindowProc(lpofn,hWnd,
703                   RegisterWindowMessage32A( LBSELCHSTRING ),
704                   control, MAKELONG(lRet,CD_LBSELCHANGE));       
705       /* FIXME: for OFN_ALLOWMULTISELECT we need CD_LBSELSUB, CD_SELADD, CD_LBSELNOITEMS */                  
706       return TRUE;
707     case lst2: /* directory list */
708       FILEDLG_StripEditControl(hWnd);
709       if (notification == LBN_DBLCLK)
710         {
711           lRet = SendDlgItemMessage16(hWnd, lst2, LB_GETCURSEL16, 0, 0);
712           if (lRet == LB_ERR) return TRUE;
713           pstr = SEGPTR_ALLOC(512);
714           SendDlgItemMessage16(hWnd, lst2, LB_GETTEXT16, lRet,
715                              (LPARAM)SEGPTR_GET(pstr));
716           strcpy( tmpstr, pstr );
717           SEGPTR_FREE(pstr);
718           if (tmpstr[0] == '[')
719             {
720               tmpstr[strlen(tmpstr) - 1] = 0;
721               strcpy(tmpstr,tmpstr+1);
722             }
723           strcat(tmpstr, "\\");
724           goto reset_scan;
725         }
726       return TRUE;
727     case cmb1: /* file type drop list */
728       if (notification == CBN_SELCHANGE) 
729         {
730           *tmpstr = 0;
731           goto reset_scan;
732         }
733       return TRUE;
734     case chx1:
735       return TRUE;
736     case pshHelp:
737       return TRUE;
738     case cmb2: /* disk drop list */
739       FILEDLG_StripEditControl(hWnd);
740       lRet = SendDlgItemMessage16(hWnd, cmb2, CB_GETCURSEL16, 0, 0L);
741       if (lRet == LB_ERR) return 0;
742       pstr = SEGPTR_ALLOC(512);
743       SendDlgItemMessage16(hWnd, cmb2, CB_GETLBTEXT16, lRet,
744                            (LPARAM)SEGPTR_GET(pstr));
745       sprintf(tmpstr, "%c:", pstr[2]);
746       SEGPTR_FREE(pstr);
747     reset_scan:
748       lRet = SendDlgItemMessage16(hWnd, cmb1, CB_GETCURSEL16, 0, 0);
749       if (lRet == LB_ERR)
750         return TRUE;
751       pstr = (LPSTR)SendDlgItemMessage16(hWnd, cmb1, CB_GETITEMDATA16, lRet, 0);
752       TRACE(commdlg,"Selected filter : %s\n", pstr);
753       SetDlgItemText32A( hWnd, edt1, pstr );
754       FILEDLG_ScanDir(hWnd, tmpstr);
755       in_update=TRUE;
756     case IDOK:
757     almost_ok:
758       ofn2=*lpofn; /* for later restoring */
759       GetDlgItemText32A( hWnd, edt1, tmpstr, sizeof(tmpstr) );
760       pstr = strrchr(tmpstr, '\\');
761       if (pstr == NULL)
762         pstr = strrchr(tmpstr, ':');
763       if (strchr(tmpstr,'*') != NULL || strchr(tmpstr,'?') != NULL)
764         {
765           /* edit control contains wildcards */
766           if (pstr != NULL)
767             {
768               strncpy(tmpstr2, pstr+1, 511); tmpstr2[511]=0;
769               *(pstr+1) = 0;
770             }
771           else
772             {
773               strcpy(tmpstr2, tmpstr);
774               *tmpstr=0;
775             }
776           TRACE(commdlg,"tmpstr=%s, tmpstr2=%s\n", tmpstr, tmpstr2);
777           SetDlgItemText32A( hWnd, edt1, tmpstr2 );
778           FILEDLG_ScanDir(hWnd, tmpstr);
779           return TRUE;
780         }
781       /* no wildcards, we might have a directory or a filename */
782       /* try appending a wildcard and reading the directory */
783       pstr2 = tmpstr + strlen(tmpstr);
784       if (pstr == NULL || *(pstr+1) != 0)
785         strcat(tmpstr, "\\");
786       lRet = SendDlgItemMessage16(hWnd, cmb1, CB_GETCURSEL16, 0, 0);
787       if (lRet == LB_ERR) return TRUE;
788       lpofn->nFilterIndex = lRet + 1;
789       TRACE(commdlg,"lpofn->nFilterIndex=%ld\n", lpofn->nFilterIndex);
790       lstrcpyn32A(tmpstr2, 
791              FILEDLG_GetFileType(PTR_SEG_TO_LIN(lpofn->lpstrCustomFilter),
792                                  PTR_SEG_TO_LIN(lpofn->lpstrFilter),
793                                  lRet), sizeof(tmpstr2));
794       SetDlgItemText32A( hWnd, edt1, tmpstr2 );
795       if (!in_update)
796       /* if ScanDir succeeds, we have changed the directory */
797       if (FILEDLG_ScanDir(hWnd, tmpstr)) return TRUE;
798       /* if not, this must be a filename */
799       *pstr2 = 0;
800       if (pstr != NULL)
801         {
802           /* strip off the pathname */
803           *pstr = 0;
804           SetDlgItemText32A( hWnd, edt1, pstr + 1 );
805           lstrcpyn32A(tmpstr2, pstr+1, sizeof(tmpstr2) );
806           /* Should we MessageBox() if this fails? */
807           if (!FILEDLG_ScanDir(hWnd, tmpstr)) return TRUE;
808           strcpy(tmpstr, tmpstr2);
809         }
810       else SetDlgItemText32A( hWnd, edt1, tmpstr );
811 #if 0
812       ShowWindow16(hWnd, SW_HIDE);   /* this should not be necessary ?! (%%%) */
813 #endif
814       {
815         int drive = DRIVE_GetCurrentDrive();
816         tmpstr2[0] = 'A'+ drive;
817         tmpstr2[1] = ':';
818         tmpstr2[2] = '\\';
819         strncpy(tmpstr2 + 3, DRIVE_GetDosCwd(drive), 507); tmpstr2[510]=0;
820         if (strlen(tmpstr2) > 3)
821            strcat(tmpstr2, "\\");
822         strncat(tmpstr2, tmpstr, 511-strlen(tmpstr2)); tmpstr2[511]=0;
823         if (lpofn->lpstrFile)
824           lstrcpyn32A(PTR_SEG_TO_LIN(lpofn->lpstrFile), tmpstr2,lpofn->nMaxFile);
825       }
826       lpofn->nFileOffset = strrchr(tmpstr2,'\\') - tmpstr2 +1;
827       lpofn->nFileExtension = 0;
828       while(tmpstr2[lpofn->nFileExtension] != '.' && tmpstr2[lpofn->nFileExtension] != '\0')
829         lpofn->nFileExtension++;
830       if (tmpstr2[lpofn->nFileExtension] == '\0')
831         lpofn->nFileExtension = 0;
832       else
833         lpofn->nFileExtension++;
834
835       if(in_update)
836        {
837          if (FILEDLG_HookCallChk(lpofn))
838            FILEDLG_CallWindowProc(lpofn,hWnd,
839                                   RegisterWindowMessage32A( LBSELCHSTRING ),
840                                   control, MAKELONG(lRet,CD_LBSELCHANGE));
841
842          in_update = FALSE;
843          return TRUE;
844        }
845       if (PTR_SEG_TO_LIN(lpofn->lpstrFileTitle) != NULL) 
846         {
847           lRet = SendDlgItemMessage16(hWnd, lst1, LB_GETCURSEL16, 0, 0);
848           SendDlgItemMessage16(hWnd, lst1, LB_GETTEXT16, lRet,
849                                lpofn->lpstrFileTitle );
850         }
851       if (FILEDLG_HookCallChk(lpofn))
852       {
853        lRet= (BOOL16)FILEDLG_CallWindowProc(lpofn,
854                hWnd, RegisterWindowMessage32A( FILEOKSTRING ), 0, lParam );
855        if (lRet)       
856        {
857          *lpofn=ofn2; /* restore old state */
858 #if 0
859          ShowWindow16(hWnd, SW_SHOW);               /* only if above (%%%) SW_HIDE used */
860 #endif         
861          break;
862        }
863       }
864       EndDialog32(hWnd, TRUE);
865       return TRUE;
866     case IDCANCEL:
867       EndDialog32(hWnd, FALSE);
868       return TRUE;
869     }
870   return FALSE;
871 }
872
873
874 /***********************************************************************
875  *           FileOpenDlgProc   (COMMDLG.6)
876  */
877 LRESULT WINAPI FileOpenDlgProc(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
878                                LPARAM lParam)
879 {  
880  LPOPENFILENAME16 lpofn = (LPOPENFILENAME16)PTR_SEG_TO_LIN(GetWindowLong32A(hWnd, DWL_USER));
881  
882  if (wMsg!=WM_INITDIALOG)
883   if (FILEDLG_HookCallChk(lpofn))
884   {
885    LRESULT  lRet=(BOOL16)FILEDLG_CallWindowProc(lpofn,hWnd,wMsg,wParam,lParam);
886    if (lRet)   
887     return lRet;         /* else continue message processing */
888   }             
889   switch (wMsg)
890     {
891     case WM_INITDIALOG:
892       return FILEDLG_WMInitDialog(hWnd, wParam, lParam);
893     case WM_MEASUREITEM:
894       return FILEDLG_WMMeasureItem(hWnd, wParam, lParam);
895     case WM_DRAWITEM:
896       return FILEDLG_WMDrawItem(hWnd, wParam, lParam, FALSE);
897     case WM_COMMAND:
898       return FILEDLG_WMCommand(hWnd, wParam, lParam);
899 #if 0
900     case WM_CTLCOLOR:
901       SetBkColor((HDC16)wParam, 0x00C0C0C0);
902       switch (HIWORD(lParam))
903         {
904         case CTLCOLOR_BTN:
905           SetTextColor((HDC16)wParam, 0x00000000);
906           return hGRAYBrush;
907         case CTLCOLOR_STATIC:
908           SetTextColor((HDC16)wParam, 0x00000000);
909           return hGRAYBrush;
910         }
911       break;
912 #endif
913     }
914   return FALSE;
915 }
916
917
918 /***********************************************************************
919  *           FileSaveDlgProc   (COMMDLG.7)
920  */
921 LRESULT WINAPI FileSaveDlgProc(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
922                                LPARAM lParam)
923 {
924  LPOPENFILENAME16 lpofn = (LPOPENFILENAME16)PTR_SEG_TO_LIN(GetWindowLong32A(hWnd, DWL_USER));
925  
926  if (wMsg!=WM_INITDIALOG)
927   if (FILEDLG_HookCallChk(lpofn))
928   {
929    LRESULT  lRet=(BOOL16)FILEDLG_CallWindowProc(lpofn,hWnd,wMsg,wParam,lParam);
930    if (lRet)   
931     return lRet;         /* else continue message processing */
932   }             
933   switch (wMsg) {
934    case WM_INITDIALOG:
935       return FILEDLG_WMInitDialog(hWnd, wParam, lParam);
936       
937    case WM_MEASUREITEM:
938       return FILEDLG_WMMeasureItem(hWnd, wParam, lParam);
939     
940    case WM_DRAWITEM:
941       return FILEDLG_WMDrawItem(hWnd, wParam, lParam, TRUE);
942
943    case WM_COMMAND:
944       return FILEDLG_WMCommand(hWnd, wParam, lParam);
945   }
946   
947   /*
948   case WM_CTLCOLOR:
949    SetBkColor((HDC16)wParam, 0x00C0C0C0);
950    switch (HIWORD(lParam))
951    {
952     case CTLCOLOR_BTN:
953      SetTextColor((HDC16)wParam, 0x00000000);
954      return hGRAYBrush;
955     case CTLCOLOR_STATIC:
956      SetTextColor((HDC16)wParam, 0x00000000);
957      return hGRAYBrush;
958    }
959    return FALSE;
960    
961    */
962   return FALSE;
963 }
964
965
966 /***********************************************************************
967  *           FindText16   (COMMDLG.11)
968  */
969 HWND16 WINAPI FindText16( SEGPTR find )
970 {
971     HANDLE16 hInst;
972     LPCVOID ptr;
973     LPFINDREPLACE16 lpFind = (LPFINDREPLACE16)PTR_SEG_TO_LIN(find);
974
975     /*
976      * FIXME : Should respond to FR_ENABLETEMPLATE and FR_ENABLEHOOK here
977      * For now, only the standard dialog works.
978      */
979     if (lpFind->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE | 
980         FR_ENABLEHOOK)) FIXME(commdlg, ": unimplemented flag (ignored)\n");     
981     ptr = SYSRES_GetResPtr( SYSRES_DIALOG_FIND_TEXT );
982     hInst = WIN_GetWindowInstance( lpFind->hwndOwner );
983     return DIALOG_CreateIndirect( hInst, ptr, TRUE, lpFind->hwndOwner,
984                         (DLGPROC16)MODULE_GetWndProcEntry16("FindTextDlgProc"),
985                                   find, WIN_PROC_16 );
986 }
987
988 /***********************************************************************
989  *           FindText32A   (COMMDLG.6)
990  */
991 HWND32 WINAPI FindText32A( LPFINDREPLACE32A lpFind )
992 {
993     HANDLE16 hInst;
994     LPCVOID ptr;
995
996     /*
997      * FIXME : Should respond to FR_ENABLETEMPLATE and FR_ENABLEHOOK here
998      * For now, only the standard dialog works.
999      */
1000     if (lpFind->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE | 
1001         FR_ENABLEHOOK)) FIXME(commdlg, ": unimplemented flag (ignored)\n");     
1002     ptr = SYSRES_GetResPtr( SYSRES_DIALOG_FIND_TEXT );
1003     hInst = WIN_GetWindowInstance( lpFind->hwndOwner );
1004     return DIALOG_CreateIndirect( hInst, ptr, TRUE, lpFind->hwndOwner,
1005                 (DLGPROC16)FindTextDlgProc32A, (LPARAM)lpFind, WIN_PROC_32A );
1006 }
1007
1008 /***********************************************************************
1009  *           FindText32W   (COMMDLG.7)
1010  */
1011 HWND32 WINAPI FindText32W( LPFINDREPLACE32W lpFind )
1012 {
1013     HANDLE16 hInst;
1014     LPCVOID ptr;
1015
1016     /*
1017      * FIXME : Should respond to FR_ENABLETEMPLATE and FR_ENABLEHOOK here
1018      * For now, only the standard dialog works.
1019      */
1020     if (lpFind->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE | 
1021         FR_ENABLEHOOK)) FIXME(commdlg, ": unimplemented flag (ignored)\n");     
1022     ptr = SYSRES_GetResPtr( SYSRES_DIALOG_FIND_TEXT );
1023     hInst = WIN_GetWindowInstance( lpFind->hwndOwner );
1024     return DIALOG_CreateIndirect( hInst, ptr, TRUE, lpFind->hwndOwner,
1025                 (DLGPROC16)FindTextDlgProc32W, (LPARAM)lpFind, WIN_PROC_32W );
1026 }
1027
1028 /***********************************************************************
1029  *           ReplaceText16   (COMMDLG.12)
1030  */
1031 HWND16 WINAPI ReplaceText16( SEGPTR find )
1032 {
1033     HANDLE16 hInst;
1034     LPCVOID ptr;
1035     LPFINDREPLACE16 lpFind = (LPFINDREPLACE16)PTR_SEG_TO_LIN(find);
1036
1037     /*
1038      * FIXME : We should do error checking on the lpFind structure here
1039      * and make CommDlgExtendedError() return the error condition.
1040      */
1041     if (lpFind->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE | 
1042         FR_ENABLEHOOK)) FIXME(commdlg, ": unimplemented flag (ignored)\n");     
1043     ptr = SYSRES_GetResPtr( SYSRES_DIALOG_REPLACE_TEXT );
1044     hInst = WIN_GetWindowInstance( lpFind->hwndOwner );
1045     return DIALOG_CreateIndirect( hInst, ptr, TRUE, lpFind->hwndOwner,
1046                      (DLGPROC16)MODULE_GetWndProcEntry16("ReplaceTextDlgProc"),
1047                                   find, WIN_PROC_16 );
1048 }
1049
1050 /***********************************************************************
1051  *           ReplaceText32A   (COMDLG32.19)
1052  */
1053 HWND32 WINAPI ReplaceText32A( LPFINDREPLACE32A lpFind )
1054 {
1055     HANDLE16 hInst;
1056     LPCVOID ptr;
1057
1058     /*
1059      * FIXME : Should respond to FR_ENABLETEMPLATE and FR_ENABLEHOOK here
1060      * For now, only the standard dialog works.
1061      */
1062     if (lpFind->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE |
1063         FR_ENABLEHOOK)) FIXME(commdlg, ": unimplemented flag (ignored)\n");     
1064     ptr = SYSRES_GetResPtr( SYSRES_DIALOG_REPLACE_TEXT );
1065     hInst = WIN_GetWindowInstance( lpFind->hwndOwner );
1066     return DIALOG_CreateIndirect( hInst, ptr, TRUE, lpFind->hwndOwner,
1067                 (DLGPROC16)ReplaceTextDlgProc32A, (LPARAM)lpFind, WIN_PROC_32A );
1068 }
1069
1070 /***********************************************************************
1071  *           ReplaceText32W   (COMDLG32.20)
1072  */
1073 HWND32 WINAPI ReplaceText32W( LPFINDREPLACE32W lpFind )
1074 {
1075     HANDLE16 hInst;
1076     LPCVOID ptr;
1077
1078     /*
1079      * FIXME : We should do error checking on the lpFind structure here
1080      * and make CommDlgExtendedError() return the error condition.
1081      */
1082     if (lpFind->Flags & (FR_ENABLETEMPLATE | FR_ENABLETEMPLATEHANDLE | 
1083         FR_ENABLEHOOK)) FIXME(commdlg, ": unimplemented flag (ignored)\n");
1084     ptr = SYSRES_GetResPtr( SYSRES_DIALOG_REPLACE_TEXT );
1085     hInst = WIN_GetWindowInstance( lpFind->hwndOwner );
1086     return DIALOG_CreateIndirect( hInst, ptr, TRUE, lpFind->hwndOwner,
1087                 (DLGPROC16)ReplaceTextDlgProc32W, (LPARAM)lpFind, WIN_PROC_32W );
1088 }
1089
1090
1091 /***********************************************************************
1092  *                              FINDDLG_WMInitDialog            [internal]
1093  */
1094 static LRESULT FINDDLG_WMInitDialog(HWND32 hWnd, LPARAM lParam, LPDWORD lpFlags,
1095                                     LPSTR lpstrFindWhat, BOOL32 fUnicode)
1096 {
1097     SetWindowLong32A(hWnd, DWL_USER, lParam);
1098     *lpFlags &= ~(FR_FINDNEXT | FR_REPLACE | FR_REPLACEALL | FR_DIALOGTERM);
1099     /*
1100      * FIXME : If the initial FindWhat string is empty, we should disable the
1101      * FindNext (IDOK) button.  Only after typing some text, the button should be
1102      * enabled.
1103      */
1104     if (fUnicode) SetDlgItemText32W(hWnd, edt1, (LPWSTR)lpstrFindWhat);
1105         else SetDlgItemText32A(hWnd, edt1, lpstrFindWhat);
1106     CheckRadioButton32(hWnd, rad1, rad2, (*lpFlags & FR_DOWN) ? rad2 : rad1);
1107     if (*lpFlags & (FR_HIDEUPDOWN | FR_NOUPDOWN)) {
1108         EnableWindow32(GetDlgItem32(hWnd, rad1), FALSE);
1109         EnableWindow32(GetDlgItem32(hWnd, rad2), FALSE);
1110     }
1111     if (*lpFlags & FR_HIDEUPDOWN) {
1112         ShowWindow32(GetDlgItem32(hWnd, rad1), SW_HIDE);
1113         ShowWindow32(GetDlgItem32(hWnd, rad2), SW_HIDE);
1114         ShowWindow32(GetDlgItem32(hWnd, grp1), SW_HIDE);
1115     }
1116     CheckDlgButton32(hWnd, chx1, (*lpFlags & FR_WHOLEWORD) ? 1 : 0);
1117     if (*lpFlags & (FR_HIDEWHOLEWORD | FR_NOWHOLEWORD))
1118         EnableWindow32(GetDlgItem32(hWnd, chx1), FALSE);
1119     if (*lpFlags & FR_HIDEWHOLEWORD)
1120         ShowWindow32(GetDlgItem32(hWnd, chx1), SW_HIDE);
1121     CheckDlgButton32(hWnd, chx2, (*lpFlags & FR_MATCHCASE) ? 1 : 0);
1122     if (*lpFlags & (FR_HIDEMATCHCASE | FR_NOMATCHCASE))
1123         EnableWindow32(GetDlgItem32(hWnd, chx2), FALSE);
1124     if (*lpFlags & FR_HIDEMATCHCASE)
1125         ShowWindow32(GetDlgItem32(hWnd, chx2), SW_HIDE);
1126     if (!(*lpFlags & FR_SHOWHELP)) {
1127         EnableWindow32(GetDlgItem32(hWnd, pshHelp), FALSE);
1128         ShowWindow32(GetDlgItem32(hWnd, pshHelp), SW_HIDE);
1129     }
1130     ShowWindow32(hWnd, SW_SHOWNORMAL);
1131     return TRUE;
1132 }    
1133
1134
1135 /***********************************************************************
1136  *                              FINDDLG_WMCommand               [internal]
1137  */
1138 static LRESULT FINDDLG_WMCommand(HWND32 hWnd, WPARAM32 wParam, 
1139                         HWND32 hwndOwner, LPDWORD lpFlags,
1140                         LPSTR lpstrFindWhat, WORD wFindWhatLen, 
1141                         BOOL32 fUnicode)
1142 {
1143     int uFindReplaceMessage = RegisterWindowMessage32A( FINDMSGSTRING );
1144     int uHelpMessage = RegisterWindowMessage32A( HELPMSGSTRING );
1145
1146     switch (wParam) {
1147         case IDOK:
1148             if (fUnicode) 
1149               GetDlgItemText32W(hWnd, edt1, (LPWSTR)lpstrFindWhat, wFindWhatLen/2);
1150               else GetDlgItemText32A(hWnd, edt1, lpstrFindWhat, wFindWhatLen);
1151             if (IsDlgButtonChecked32(hWnd, rad2))
1152                 *lpFlags |= FR_DOWN;
1153                 else *lpFlags &= ~FR_DOWN;
1154             if (IsDlgButtonChecked32(hWnd, chx1))
1155                 *lpFlags |= FR_WHOLEWORD; 
1156                 else *lpFlags &= ~FR_WHOLEWORD;
1157             if (IsDlgButtonChecked32(hWnd, chx2))
1158                 *lpFlags |= FR_MATCHCASE; 
1159                 else *lpFlags &= ~FR_MATCHCASE;
1160             *lpFlags &= ~(FR_REPLACE | FR_REPLACEALL | FR_DIALOGTERM);
1161             *lpFlags |= FR_FINDNEXT;
1162             SendMessage32A(hwndOwner, uFindReplaceMessage, 0,
1163                           GetWindowLong32A(hWnd, DWL_USER) );
1164             return TRUE;
1165         case IDCANCEL:
1166             *lpFlags &= ~(FR_FINDNEXT | FR_REPLACE | FR_REPLACEALL);
1167             *lpFlags |= FR_DIALOGTERM;
1168             SendMessage32A(hwndOwner, uFindReplaceMessage, 0,
1169                           GetWindowLong32A(hWnd, DWL_USER) );
1170             DestroyWindow32(hWnd);
1171             return TRUE;
1172         case pshHelp:
1173             /* FIXME : should lpfr structure be passed as an argument ??? */
1174             SendMessage32A(hwndOwner, uHelpMessage, 0, 0);
1175             return TRUE;
1176     }
1177     return FALSE;
1178 }    
1179
1180
1181 /***********************************************************************
1182  *           FindTextDlgProc16   (COMMDLG.13)
1183  */
1184 LRESULT WINAPI FindTextDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
1185                                  LPARAM lParam)
1186 {
1187     LPFINDREPLACE16 lpfr;
1188     switch (wMsg) {
1189         case WM_INITDIALOG:
1190             lpfr=(LPFINDREPLACE16)PTR_SEG_TO_LIN(lParam);
1191             return FINDDLG_WMInitDialog(hWnd, lParam, &(lpfr->Flags),
1192                 PTR_SEG_TO_LIN(lpfr->lpstrFindWhat), FALSE);
1193         case WM_COMMAND:
1194             lpfr=(LPFINDREPLACE16)PTR_SEG_TO_LIN(GetWindowLong32A(hWnd, DWL_USER));
1195             return FINDDLG_WMCommand(hWnd, wParam, lpfr->hwndOwner,
1196                 &lpfr->Flags, PTR_SEG_TO_LIN(lpfr->lpstrFindWhat),
1197                 lpfr->wFindWhatLen, FALSE);
1198     }
1199     return FALSE;
1200 }
1201
1202 /***********************************************************************
1203  *           FindTextDlgProc32A
1204  */
1205 LRESULT WINAPI FindTextDlgProc32A(HWND32 hWnd, UINT32 wMsg, WPARAM32 wParam,
1206                                  LPARAM lParam)
1207 {
1208     LPFINDREPLACE32A lpfr;
1209     switch (wMsg) {
1210         case WM_INITDIALOG:
1211             lpfr=(LPFINDREPLACE32A)lParam;
1212             return FINDDLG_WMInitDialog(hWnd, lParam, &(lpfr->Flags),
1213               lpfr->lpstrFindWhat, FALSE);
1214         case WM_COMMAND:
1215             lpfr=(LPFINDREPLACE32A)GetWindowLong32A(hWnd, DWL_USER);
1216             return FINDDLG_WMCommand(hWnd, wParam, lpfr->hwndOwner,
1217                 &lpfr->Flags, lpfr->lpstrFindWhat, lpfr->wFindWhatLen,
1218                 FALSE);
1219     }
1220     return FALSE;
1221 }
1222
1223 /***********************************************************************
1224  *           FindTextDlgProc32W
1225  */
1226 LRESULT WINAPI FindTextDlgProc32W(HWND32 hWnd, UINT32 wMsg, WPARAM32 wParam,
1227                                  LPARAM lParam)
1228 {
1229     LPFINDREPLACE32W lpfr;
1230     switch (wMsg) {
1231         case WM_INITDIALOG:
1232             lpfr=(LPFINDREPLACE32W)lParam;
1233             return FINDDLG_WMInitDialog(hWnd, lParam, &(lpfr->Flags),
1234               (LPSTR)lpfr->lpstrFindWhat, TRUE);
1235         case WM_COMMAND:
1236             lpfr=(LPFINDREPLACE32W)GetWindowLong32A(hWnd, DWL_USER);
1237             return FINDDLG_WMCommand(hWnd, wParam, lpfr->hwndOwner,
1238                 &lpfr->Flags, (LPSTR)lpfr->lpstrFindWhat, lpfr->wFindWhatLen,
1239                 TRUE);
1240     }
1241     return FALSE;
1242 }
1243
1244
1245 /***********************************************************************
1246  *                              REPLACEDLG_WMInitDialog         [internal]
1247  */
1248 static LRESULT REPLACEDLG_WMInitDialog(HWND32 hWnd, LPARAM lParam,
1249                     LPDWORD lpFlags, LPSTR lpstrFindWhat, 
1250                     LPSTR lpstrReplaceWith, BOOL32 fUnicode)
1251 {
1252     SetWindowLong32A(hWnd, DWL_USER, lParam);
1253     *lpFlags &= ~(FR_FINDNEXT | FR_REPLACE | FR_REPLACEALL | FR_DIALOGTERM);
1254     /*
1255      * FIXME : If the initial FindWhat string is empty, we should disable the FinNext /
1256      * Replace / ReplaceAll buttons.  Only after typing some text, the buttons should be
1257      * enabled.
1258      */
1259     if (fUnicode)     
1260     {
1261         SetDlgItemText32W(hWnd, edt1, (LPWSTR)lpstrFindWhat);
1262         SetDlgItemText32W(hWnd, edt2, (LPWSTR)lpstrReplaceWith);
1263     } else
1264     {
1265         SetDlgItemText32A(hWnd, edt1, lpstrFindWhat);
1266         SetDlgItemText32A(hWnd, edt2, lpstrReplaceWith);
1267     }
1268     CheckDlgButton32(hWnd, chx1, (*lpFlags & FR_WHOLEWORD) ? 1 : 0);
1269     if (*lpFlags & (FR_HIDEWHOLEWORD | FR_NOWHOLEWORD))
1270         EnableWindow32(GetDlgItem32(hWnd, chx1), FALSE);
1271     if (*lpFlags & FR_HIDEWHOLEWORD)
1272         ShowWindow32(GetDlgItem32(hWnd, chx1), SW_HIDE);
1273     CheckDlgButton32(hWnd, chx2, (*lpFlags & FR_MATCHCASE) ? 1 : 0);
1274     if (*lpFlags & (FR_HIDEMATCHCASE | FR_NOMATCHCASE))
1275         EnableWindow32(GetDlgItem32(hWnd, chx2), FALSE);
1276     if (*lpFlags & FR_HIDEMATCHCASE)
1277         ShowWindow32(GetDlgItem32(hWnd, chx2), SW_HIDE);
1278     if (!(*lpFlags & FR_SHOWHELP)) {
1279         EnableWindow32(GetDlgItem32(hWnd, pshHelp), FALSE);
1280         ShowWindow32(GetDlgItem32(hWnd, pshHelp), SW_HIDE);
1281     }
1282     ShowWindow32(hWnd, SW_SHOWNORMAL);
1283     return TRUE;
1284 }    
1285
1286
1287 /***********************************************************************
1288  *                              REPLACEDLG_WMCommand            [internal]
1289  */
1290 static LRESULT REPLACEDLG_WMCommand(HWND32 hWnd, WPARAM16 wParam,
1291                     HWND32 hwndOwner, LPDWORD lpFlags,
1292                     LPSTR lpstrFindWhat, WORD wFindWhatLen,
1293                     LPSTR lpstrReplaceWith, WORD wReplaceWithLen,
1294                     BOOL32 fUnicode)
1295 {
1296     int uFindReplaceMessage = RegisterWindowMessage32A( FINDMSGSTRING );
1297     int uHelpMessage = RegisterWindowMessage32A( HELPMSGSTRING );
1298
1299     switch (wParam) {
1300         case IDOK:
1301             if (fUnicode)
1302             {
1303                 GetDlgItemText32W(hWnd, edt1, (LPWSTR)lpstrFindWhat, wFindWhatLen/2);
1304                 GetDlgItemText32W(hWnd, edt2, (LPWSTR)lpstrReplaceWith, wReplaceWithLen/2);
1305             }  else
1306             {
1307                 GetDlgItemText32A(hWnd, edt1, lpstrFindWhat, wFindWhatLen);
1308                 GetDlgItemText32A(hWnd, edt2, lpstrReplaceWith, wReplaceWithLen);
1309             }
1310             if (IsDlgButtonChecked32(hWnd, chx1))
1311                 *lpFlags |= FR_WHOLEWORD; 
1312                 else *lpFlags &= ~FR_WHOLEWORD;
1313             if (IsDlgButtonChecked32(hWnd, chx2))
1314                 *lpFlags |= FR_MATCHCASE; 
1315                 else *lpFlags &= ~FR_MATCHCASE;
1316             *lpFlags &= ~(FR_REPLACE | FR_REPLACEALL | FR_DIALOGTERM);
1317             *lpFlags |= FR_FINDNEXT;
1318             SendMessage32A(hwndOwner, uFindReplaceMessage, 0,
1319                           GetWindowLong32A(hWnd, DWL_USER) );
1320             return TRUE;
1321         case IDCANCEL:
1322             *lpFlags &= ~(FR_FINDNEXT | FR_REPLACE | FR_REPLACEALL);
1323             *lpFlags |= FR_DIALOGTERM;
1324             SendMessage32A(hwndOwner, uFindReplaceMessage, 0,
1325                           GetWindowLong32A(hWnd, DWL_USER) );
1326             DestroyWindow32(hWnd);
1327             return TRUE;
1328         case psh1:
1329             if (fUnicode)
1330             {
1331                 GetDlgItemText32W(hWnd, edt1, (LPWSTR)lpstrFindWhat, wFindWhatLen/2);
1332                 GetDlgItemText32W(hWnd, edt2, (LPWSTR)lpstrReplaceWith, wReplaceWithLen/2);
1333             }  else
1334             {   
1335                 GetDlgItemText32A(hWnd, edt1, lpstrFindWhat, wFindWhatLen);
1336                 GetDlgItemText32A(hWnd, edt2, lpstrReplaceWith, wReplaceWithLen);
1337             }
1338             if (IsDlgButtonChecked32(hWnd, chx1))
1339                 *lpFlags |= FR_WHOLEWORD; 
1340                 else *lpFlags &= ~FR_WHOLEWORD;
1341             if (IsDlgButtonChecked32(hWnd, chx2))
1342                 *lpFlags |= FR_MATCHCASE; 
1343                 else *lpFlags &= ~FR_MATCHCASE;
1344             *lpFlags &= ~(FR_FINDNEXT | FR_REPLACEALL | FR_DIALOGTERM);
1345             *lpFlags |= FR_REPLACE;
1346             SendMessage32A(hwndOwner, uFindReplaceMessage, 0,
1347                           GetWindowLong32A(hWnd, DWL_USER) );
1348             return TRUE;
1349         case psh2:
1350             if (fUnicode)
1351             {
1352                 GetDlgItemText32W(hWnd, edt1, (LPWSTR)lpstrFindWhat, wFindWhatLen/2);
1353                 GetDlgItemText32W(hWnd, edt2, (LPWSTR)lpstrReplaceWith, wReplaceWithLen/2);
1354             }  else
1355             {
1356                 GetDlgItemText32A(hWnd, edt1, lpstrFindWhat, wFindWhatLen);
1357                 GetDlgItemText32A(hWnd, edt2, lpstrReplaceWith, wReplaceWithLen);
1358             }
1359             if (IsDlgButtonChecked32(hWnd, chx1))
1360                 *lpFlags |= FR_WHOLEWORD; 
1361                 else *lpFlags &= ~FR_WHOLEWORD;
1362             if (IsDlgButtonChecked32(hWnd, chx2))
1363                 *lpFlags |= FR_MATCHCASE; 
1364                 else *lpFlags &= ~FR_MATCHCASE;
1365             *lpFlags &= ~(FR_FINDNEXT | FR_REPLACE | FR_DIALOGTERM);
1366             *lpFlags |= FR_REPLACEALL;
1367             SendMessage32A(hwndOwner, uFindReplaceMessage, 0,
1368                           GetWindowLong32A(hWnd, DWL_USER) );
1369             return TRUE;
1370         case pshHelp:
1371             /* FIXME : should lpfr structure be passed as an argument ??? */
1372             SendMessage32A(hwndOwner, uHelpMessage, 0, 0);
1373             return TRUE;
1374     }
1375     return FALSE;
1376 }    
1377
1378
1379 /***********************************************************************
1380  *           ReplaceTextDlgProc16   (COMMDLG.14)
1381  */
1382 LRESULT WINAPI ReplaceTextDlgProc16(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
1383                                     LPARAM lParam)
1384 {
1385     LPFINDREPLACE16 lpfr;
1386     switch (wMsg) {
1387         case WM_INITDIALOG:
1388             lpfr=(LPFINDREPLACE16)PTR_SEG_TO_LIN(lParam);
1389             return REPLACEDLG_WMInitDialog(hWnd, lParam, &lpfr->Flags,
1390                     PTR_SEG_TO_LIN(lpfr->lpstrFindWhat),
1391                     PTR_SEG_TO_LIN(lpfr->lpstrReplaceWith), FALSE);
1392         case WM_COMMAND:
1393             lpfr=(LPFINDREPLACE16)PTR_SEG_TO_LIN(GetWindowLong32A(hWnd, DWL_USER));
1394             return REPLACEDLG_WMCommand(hWnd, wParam, lpfr->hwndOwner, 
1395                     &lpfr->Flags, PTR_SEG_TO_LIN(lpfr->lpstrFindWhat),
1396                     lpfr->wFindWhatLen, PTR_SEG_TO_LIN(lpfr->lpstrReplaceWith),
1397                     lpfr->wReplaceWithLen, FALSE);
1398     }
1399     return FALSE;
1400 }
1401
1402 /***********************************************************************
1403  *           ReplaceTextDlgProc32A
1404  */ 
1405 LRESULT WINAPI ReplaceTextDlgProc32A(HWND32 hWnd, UINT32 wMsg, WPARAM32 wParam,
1406                                     LPARAM lParam)
1407 {
1408     LPFINDREPLACE32A lpfr;
1409     switch (wMsg) {
1410         case WM_INITDIALOG:
1411             lpfr=(LPFINDREPLACE32A)lParam;
1412             return REPLACEDLG_WMInitDialog(hWnd, lParam, &lpfr->Flags,
1413                     lpfr->lpstrFindWhat, lpfr->lpstrReplaceWith, FALSE);
1414         case WM_COMMAND:
1415             lpfr=(LPFINDREPLACE32A)GetWindowLong32A(hWnd, DWL_USER);
1416             return REPLACEDLG_WMCommand(hWnd, wParam, lpfr->hwndOwner, 
1417                     &lpfr->Flags, lpfr->lpstrFindWhat, lpfr->wFindWhatLen,
1418                     lpfr->lpstrReplaceWith, lpfr->wReplaceWithLen, FALSE);
1419     }
1420     return FALSE;
1421 }
1422
1423 /***********************************************************************
1424  *           ReplaceTextDlgProc32W
1425  */ 
1426 LRESULT WINAPI ReplaceTextDlgProc32W(HWND32 hWnd, UINT32 wMsg, WPARAM32 wParam,
1427                                     LPARAM lParam)
1428 {
1429     LPFINDREPLACE32W lpfr;
1430     switch (wMsg) {
1431         case WM_INITDIALOG:
1432             lpfr=(LPFINDREPLACE32W)lParam;
1433             return REPLACEDLG_WMInitDialog(hWnd, lParam, &lpfr->Flags,
1434                     (LPSTR)lpfr->lpstrFindWhat, (LPSTR)lpfr->lpstrReplaceWith,
1435                     TRUE);
1436         case WM_COMMAND:
1437             lpfr=(LPFINDREPLACE32W)GetWindowLong32A(hWnd, DWL_USER);
1438             return REPLACEDLG_WMCommand(hWnd, wParam, lpfr->hwndOwner, 
1439                     &lpfr->Flags, (LPSTR)lpfr->lpstrFindWhat, lpfr->wFindWhatLen,
1440                     (LPSTR)lpfr->lpstrReplaceWith, lpfr->wReplaceWithLen, TRUE);
1441     }
1442     return FALSE;
1443 }
1444
1445
1446 /***********************************************************************
1447  *           PrintDlg16   (COMMDLG.20)
1448  */
1449 BOOL16 WINAPI PrintDlg16( SEGPTR printdlg )
1450 {
1451     HANDLE16 hInst;
1452     BOOL16 bRet = FALSE;
1453     LPCVOID template;
1454     HWND32 hwndDialog;
1455     LPPRINTDLG16 lpPrint = (LPPRINTDLG16)PTR_SEG_TO_LIN(printdlg);
1456
1457     TRACE(commdlg,"(%p) -- Flags=%08lX\n", lpPrint, lpPrint->Flags );
1458
1459     if (lpPrint->Flags & PD_RETURNDEFAULT)
1460         /* FIXME: should fill lpPrint->hDevMode and lpPrint->hDevNames here */
1461         return TRUE;
1462
1463     if (lpPrint->Flags & PD_PRINTSETUP)
1464         template = SYSRES_GetResPtr( SYSRES_DIALOG_PRINT_SETUP );
1465     else
1466         template = SYSRES_GetResPtr( SYSRES_DIALOG_PRINT );
1467
1468     hInst = WIN_GetWindowInstance( lpPrint->hwndOwner );
1469     hwndDialog = DIALOG_CreateIndirect( hInst, template, TRUE,
1470                                         lpPrint->hwndOwner,
1471                                (DLGPROC16)((lpPrint->Flags & PD_PRINTSETUP) ?
1472                                 MODULE_GetWndProcEntry16("PrintSetupDlgProc") :
1473                                 MODULE_GetWndProcEntry16("PrintDlgProc")),
1474                                 printdlg, WIN_PROC_16 );
1475     if (hwndDialog) bRet = DIALOG_DoDialogBox( hwndDialog, lpPrint->hwndOwner);
1476     return bRet;
1477 }
1478
1479
1480 /***********************************************************************
1481  *           PrintDlg32A   (COMDLG32.17)
1482  */
1483 BOOL32 WINAPI PrintDlg32A( LPPRINTDLG32A printdlg )
1484 {
1485     FIXME(commdlg, "(%p): stub\n",printdlg);
1486     /* Altough returning FALSE is theoricaly the right thing
1487      * most programs check for a printer at startup, and if
1488      * none is found popup PrintDlg32A(), if it fails the program
1489      * terminates; by returning TRUE the programs can still run
1490      * as long as no printer related stuff is used
1491      */ 
1492     return TRUE;
1493 }
1494
1495
1496 /***********************************************************************
1497  *           PrintDlg32W   (COMDLG32.18)
1498  */
1499 BOOL32 WINAPI PrintDlg32W( LPPRINTDLG32W printdlg )
1500 {
1501     FIXME(commdlg, "empty stub\n" );
1502     return FALSE;
1503 }
1504
1505
1506 /***********************************************************************
1507  *           PrintDlgProc   (COMMDLG.21)
1508  */
1509 LRESULT WINAPI PrintDlgProc(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
1510                             LPARAM lParam)
1511 {
1512   switch (wMsg)
1513     {
1514     case WM_INITDIALOG:
1515       TRACE(commdlg,"WM_INITDIALOG lParam=%08lX\n", lParam);
1516       ShowWindow16(hWnd, SW_SHOWNORMAL);
1517       return (TRUE);
1518     case WM_COMMAND:
1519       switch (wParam)
1520         {
1521         case IDOK:
1522           EndDialog32(hWnd, TRUE);
1523           return(TRUE);
1524         case IDCANCEL:
1525           EndDialog32(hWnd, FALSE);
1526           return(TRUE);
1527         }
1528       return(FALSE);
1529     }
1530   return FALSE;
1531 }
1532
1533
1534 /***********************************************************************
1535  *           PrintSetupDlgProc   (COMMDLG.22)
1536  */
1537 LRESULT WINAPI PrintSetupDlgProc(HWND16 hWnd, UINT16 wMsg, WPARAM16 wParam,
1538                                  LPARAM lParam)
1539 {
1540   switch (wMsg)
1541     {
1542     case WM_INITDIALOG:
1543       TRACE(commdlg,"WM_INITDIALOG lParam=%08lX\n", lParam);
1544       ShowWindow16(hWnd, SW_SHOWNORMAL);
1545       return (TRUE);
1546     case WM_COMMAND:
1547       switch (wParam) {
1548       case IDOK:
1549         EndDialog32(hWnd, TRUE);
1550         return(TRUE);
1551       case IDCANCEL:
1552         EndDialog32(hWnd, FALSE);
1553         return(TRUE);
1554       }
1555       return(FALSE);
1556     }
1557   return FALSE;
1558 }
1559
1560
1561 /***********************************************************************
1562  *           CommDlgExtendedError   (COMMDLG.26)
1563  */
1564 DWORD WINAPI CommDlgExtendedError(void)
1565 {
1566   return CommDlgLastError;
1567 }
1568
1569 /***********************************************************************
1570  *           GetFileTitleA   (COMDLG32.8)
1571  */
1572 short WINAPI GetFileTitle32A(LPCSTR lpFile, LPSTR lpTitle, UINT32 cbBuf)
1573 {
1574     int i, len;
1575     TRACE(commdlg,"(%p %p %d); \n", lpFile, lpTitle, cbBuf);
1576     if (lpFile == NULL || lpTitle == NULL)
1577         return -1;
1578     len = strlen(lpFile);
1579     if (len == 0)
1580         return -1;
1581     if (strpbrk(lpFile, "*[]"))
1582         return -1;
1583     len--;
1584     if (lpFile[len] == '/' || lpFile[len] == '\\' || lpFile[len] == ':')
1585         return -1;
1586     for (i = len; i >= 0; i--)
1587         if (lpFile[i] == '/' ||  lpFile[i] == '\\' ||  lpFile[i] == ':')
1588     {
1589         i++;
1590         break;
1591     }
1592     TRACE(commdlg,"---> '%s' \n", &lpFile[i]);
1593     
1594     len = strlen(lpFile+i)+1;
1595     if (cbBuf < len)
1596         return len;
1597
1598     strncpy(lpTitle, &lpFile[i], len);
1599     return 0;
1600 }
1601
1602
1603 /***********************************************************************
1604  *           GetFileTitleA   (COMDLG32.8)
1605  */
1606 short WINAPI GetFileTitle32W(LPCWSTR lpFile, LPWSTR lpTitle, UINT32 cbBuf)
1607 {
1608         LPSTR file = HEAP_strdupWtoA(GetProcessHeap(),0,lpFile);
1609         LPSTR title = HeapAlloc(GetProcessHeap(),0,cbBuf);
1610         short   ret;
1611
1612         ret = GetFileTitle32A(file,title,cbBuf);
1613
1614         lstrcpynAtoW(lpTitle,title,cbBuf);
1615         HeapFree(GetProcessHeap(),0,file);
1616         HeapFree(GetProcessHeap(),0,title);
1617         return ret;
1618 }
1619 /***********************************************************************
1620  *           GetFileTitle   (COMMDLG.27)
1621  */
1622 short WINAPI GetFileTitle16(LPCSTR lpFile, LPSTR lpTitle, UINT16 cbBuf)
1623 {
1624     return GetFileTitle32A(lpFile,lpTitle,cbBuf);
1625 }
1626
1627
1628 /* ------------------------ Choose Color Dialog --------------------------- */
1629
1630 /***********************************************************************
1631  *           ChooseColor   (COMMDLG.5)
1632  */
1633 BOOL16 WINAPI ChooseColor16(LPCHOOSECOLOR16 lpChCol)
1634 {
1635     HINSTANCE16 hInst;
1636     HANDLE16 hDlgTmpl = 0;
1637     BOOL16 bRet = FALSE, win32Format = FALSE;
1638     LPCVOID template;
1639     HWND32 hwndDialog;
1640
1641     TRACE(commdlg,"ChooseColor\n");
1642     if (!lpChCol) return FALSE;    
1643
1644     if (lpChCol->Flags & CC_ENABLETEMPLATEHANDLE)
1645     {
1646         if (!(template = LockResource16( lpChCol->hInstance )))
1647         {
1648             CommDlgLastError = CDERR_LOADRESFAILURE;
1649             return FALSE;
1650         }
1651     }
1652     else if (lpChCol->Flags & CC_ENABLETEMPLATE)
1653     {
1654         HANDLE16 hResInfo;
1655         if (!(hResInfo = FindResource16(lpChCol->hInstance,
1656                                         lpChCol->lpTemplateName,
1657                                         RT_DIALOG16)))
1658         {
1659             CommDlgLastError = CDERR_FINDRESFAILURE;
1660             return FALSE;
1661         }
1662         if (!(hDlgTmpl = LoadResource16( lpChCol->hInstance, hResInfo )) ||
1663             !(template = LockResource16( hDlgTmpl )))
1664         {
1665             CommDlgLastError = CDERR_LOADRESFAILURE;
1666             return FALSE;
1667         }
1668     }
1669     else
1670     {
1671         template = SYSRES_GetResPtr( SYSRES_DIALOG_CHOOSE_COLOR );
1672         win32Format = TRUE;
1673     }
1674
1675     hInst = WIN_GetWindowInstance( lpChCol->hwndOwner );
1676     hwndDialog = DIALOG_CreateIndirect( hInst, template, win32Format,
1677                                         lpChCol->hwndOwner,
1678                            (DLGPROC16)MODULE_GetWndProcEntry16("ColorDlgProc"),
1679                                         (DWORD)lpChCol, WIN_PROC_16 );
1680     if (hwndDialog) bRet = DIALOG_DoDialogBox( hwndDialog, lpChCol->hwndOwner);
1681     if (hDlgTmpl) FreeResource16( hDlgTmpl );
1682     return bRet;
1683 }
1684
1685
1686 static const COLORREF predefcolors[6][8]=
1687 {
1688  { 0x008080FFL, 0x0080FFFFL, 0x0080FF80L, 0x0080FF00L,
1689    0x00FFFF80L, 0x00FF8000L, 0x00C080FFL, 0x00FF80FFL },
1690  { 0x000000FFL, 0x0000FFFFL, 0x0000FF80L, 0x0040FF00L,
1691    0x00FFFF00L, 0x00C08000L, 0x00C08080L, 0x00FF00FFL },
1692
1693  { 0x00404080L, 0x004080FFL, 0x0000FF00L, 0x00808000L,
1694    0x00804000L, 0x00FF8080L, 0x00400080L, 0x008000FFL },
1695  { 0x00000080L, 0x000080FFL, 0x00008000L, 0x00408000L,
1696    0x00FF0000L, 0x00A00000L, 0x00800080L, 0x00FF0080L },
1697
1698  { 0x00000040L, 0x00004080L, 0x00004000L, 0x00404000L,
1699    0x00800000L, 0x00400000L, 0x00400040L, 0x00800040L },
1700  { 0x00000000L, 0x00008080L, 0x00408080L, 0x00808080L,
1701    0x00808040L, 0x00C0C0C0L, 0x00400040L, 0x00FFFFFFL },
1702 };
1703
1704 struct CCPRIVATE
1705 {
1706  LPCHOOSECOLOR16 lpcc;  /* points to public known data structure */
1707  int nextuserdef;     /* next free place in user defined color array */
1708  HDC16 hdcMem;        /* color graph used for BitBlt() */
1709  HBITMAP16 hbmMem;    /* color graph bitmap */    
1710  RECT16 fullsize;     /* original dialog window size */
1711  UINT16 msetrgb;        /* # of SETRGBSTRING message (today not used)  */
1712  RECT16 old3angle;    /* last position of l-marker */
1713  RECT16 oldcross;     /* last position of color/satuation marker */
1714  BOOL32 updating;     /* to prevent recursive WM_COMMAND/EN_UPDATE procesing */
1715  int h;
1716  int s;
1717  int l;               /* for temporary storing of hue,sat,lum */
1718 };
1719
1720 /***********************************************************************
1721  *                             CC_HSLtoRGB                    [internal]
1722  */
1723 static int CC_HSLtoRGB(char c,int hue,int sat,int lum)
1724 {
1725  int res=0,maxrgb;
1726
1727  /* hue */
1728  switch(c)
1729  {
1730   case 'R':if (hue>80)  hue-=80;  else hue+=160; break;
1731   case 'G':if (hue>160) hue-=160; else hue+=80;  break;
1732   case 'B':break;
1733  }
1734
1735  /* l below 120 */
1736  maxrgb=(256*MIN(120,lum))/120;  /* 0 .. 256 */
1737  if (hue< 80)
1738   res=0;
1739  else
1740   if (hue< 120)
1741   {
1742    res=(hue-80)* maxrgb;           /* 0...10240 */
1743    res/=40;                        /* 0...256 */
1744   }
1745   else
1746    if (hue< 200)
1747     res=maxrgb;
1748    else
1749     {
1750      res=(240-hue)* maxrgb;
1751      res/=40;
1752     }
1753  res=res-maxrgb/2;                 /* -128...128 */
1754
1755  /* saturation */
1756  res=maxrgb/2 + (sat*res) /240;    /* 0..256 */
1757
1758  /* lum above 120 */
1759  if (lum>120 && res<256)
1760   res+=((lum-120) * (256-res))/120;
1761
1762  return MIN(res,255);
1763 }
1764
1765 /***********************************************************************
1766  *                             CC_RGBtoHSL                    [internal]
1767  */
1768 static int CC_RGBtoHSL(char c,int r,int g,int b)
1769 {
1770  WORD maxi,mini,mmsum,mmdif,result=0;
1771  int iresult=0;
1772
1773  maxi=MAX(r,b);
1774  maxi=MAX(maxi,g);
1775  mini=MIN(r,b);
1776  mini=MIN(mini,g);
1777
1778  mmsum=maxi+mini;
1779  mmdif=maxi-mini;
1780
1781  switch(c)
1782  {
1783   /* lum */
1784   case 'L':mmsum*=120;              /* 0...61200=(255+255)*120 */
1785            result=mmsum/255;        /* 0...240 */
1786            break;
1787   /* saturation */
1788   case 'S':if (!mmsum)
1789             result=0;
1790            else
1791             if (!mini || maxi==255)
1792              result=240;
1793            else
1794            {
1795             result=mmdif*240;       /* 0...61200=255*240 */
1796             result/= (mmsum>255 ? mmsum=510-mmsum : mmsum); /* 0..255 */
1797            }
1798            break;
1799   /* hue */
1800   case 'H':if (!mmdif)
1801             result=160;
1802            else
1803            {
1804             if (maxi==r)
1805             {
1806              iresult=40*(g-b);       /* -10200 ... 10200 */
1807              iresult/=(int)mmdif;    /* -40 .. 40 */
1808              if (iresult<0)
1809               iresult+=240;          /* 0..40 and 200..240 */
1810             }
1811             else
1812              if (maxi==g)
1813              {
1814               iresult=40*(b-r);
1815               iresult/=(int)mmdif;
1816               iresult+=80;           /* 40 .. 120 */
1817              }
1818              else
1819               if (maxi==b)
1820               {
1821                iresult=40*(r-g);
1822                iresult/=(int)mmdif;
1823                iresult+=160;         /* 120 .. 200 */
1824               }
1825             result=iresult;
1826            }
1827            break;
1828  }
1829  return result;    /* is this integer arithmetic precise enough ? */
1830 }
1831
1832 #define DISTANCE 4
1833
1834 /***********************************************************************
1835  *                CC_MouseCheckPredefColorArray               [internal]
1836  */
1837 static int CC_MouseCheckPredefColorArray(HWND16 hDlg,int dlgitem,int rows,int cols,
1838             LPARAM lParam,COLORREF *cr)
1839 {
1840  HWND16 hwnd;
1841  POINT16 point = MAKEPOINT16(lParam);
1842  RECT16 rect;
1843  int dx,dy,x,y;
1844
1845  ClientToScreen16(hDlg,&point);
1846  hwnd=GetDlgItem32(hDlg,dlgitem);
1847  GetWindowRect16(hwnd,&rect);
1848  if (PtInRect16(&rect,point))
1849  {
1850   dx=(rect.right-rect.left)/cols;
1851   dy=(rect.bottom-rect.top)/rows;
1852   ScreenToClient16(hwnd,&point);
1853
1854   if (point.x % dx < (dx-DISTANCE) && point.y % dy < (dy-DISTANCE))
1855   {
1856    x=point.x/dx;
1857    y=point.y/dy;
1858    *cr=predefcolors[y][x];
1859    /* FIXME: Draw_a_Focus_Rect() */
1860    return 1;
1861   }
1862  }
1863  return 0;
1864 }
1865
1866 /***********************************************************************
1867  *                  CC_MouseCheckUserColorArray               [internal]
1868  */
1869 static int CC_MouseCheckUserColorArray(HWND16 hDlg,int dlgitem,int rows,int cols,
1870             LPARAM lParam,COLORREF *cr,COLORREF*crarr)
1871 {
1872  HWND16 hwnd;
1873  POINT16 point = MAKEPOINT16(lParam);
1874  RECT16 rect;
1875  int dx,dy,x,y;
1876
1877  ClientToScreen16(hDlg,&point);
1878  hwnd=GetDlgItem32(hDlg,dlgitem);
1879  GetWindowRect16(hwnd,&rect);
1880  if (PtInRect16(&rect,point))
1881  {
1882   dx=(rect.right-rect.left)/cols;
1883   dy=(rect.bottom-rect.top)/rows;
1884   ScreenToClient16(hwnd,&point);
1885
1886   if (point.x % dx < (dx-DISTANCE) && point.y % dy < (dy-DISTANCE))
1887   {
1888    x=point.x/dx;
1889    y=point.y/dy;
1890    *cr=crarr[x+cols*y];
1891    /* FIXME: Draw_a_Focus_Rect() */
1892    return 1;
1893   }
1894  }
1895  return 0;
1896 }
1897
1898 #define MAXVERT  240
1899 #define MAXHORI  239
1900
1901 /*  240  ^......        ^^ 240
1902          |     .        ||
1903     SAT  |     .        || LUM
1904          |     .        ||
1905          +-----> 239   ----
1906            HUE
1907 */
1908 /***********************************************************************
1909  *                  CC_MouseCheckColorGraph                   [internal]
1910  */
1911 static int CC_MouseCheckColorGraph(HWND16 hDlg,int dlgitem,int *hori,int *vert,LPARAM lParam)
1912 {
1913  HWND32 hwnd;
1914  POINT16 point = MAKEPOINT16(lParam);
1915  RECT16 rect;
1916  long x,y;
1917
1918  ClientToScreen16(hDlg,&point);
1919  hwnd=GetDlgItem32(hDlg,dlgitem);
1920  GetWindowRect16(hwnd,&rect);
1921  if (PtInRect16(&rect,point))
1922  {
1923   GetClientRect16(hwnd,&rect);
1924   ScreenToClient16(hwnd,&point);
1925
1926   x=(long)point.x*MAXHORI;
1927   x/=rect.right;
1928   y=(long)(rect.bottom-point.y)*MAXVERT;
1929   y/=rect.bottom;
1930
1931   if (hori)
1932    *hori=x;
1933   if (vert)
1934    *vert=y;
1935   return 1;
1936  }
1937  else
1938   return 0;
1939 }
1940 /***********************************************************************
1941  *                  CC_MouseCheckResultWindow                 [internal]
1942  */
1943 static int CC_MouseCheckResultWindow(HWND16 hDlg,LPARAM lParam)
1944 {
1945  HWND16 hwnd;
1946  POINT16 point = MAKEPOINT16(lParam);
1947  RECT16 rect;
1948
1949  ClientToScreen16(hDlg,&point);
1950  hwnd=GetDlgItem32(hDlg,0x2c5);
1951  GetWindowRect16(hwnd,&rect);
1952  if (PtInRect16(&rect,point))
1953  {
1954   PostMessage16(hDlg,WM_COMMAND,0x2c9,0);
1955   return 1;
1956  }
1957  return 0;
1958 }
1959
1960 /***********************************************************************
1961  *                       CC_CheckDigitsInEdit                 [internal]
1962  */
1963 static int CC_CheckDigitsInEdit(HWND16 hwnd,int maxval)
1964 {
1965  int i,k,m,result,value;
1966  long editpos;
1967  char buffer[30];
1968  GetWindowText32A(hwnd,buffer,sizeof(buffer));
1969  m=strlen(buffer);
1970  result=0;
1971
1972  for (i=0;i<m;i++)
1973   if (buffer[i]<'0' || buffer[i]>'9')
1974   {
1975    for (k=i+1;k<=m;k++)   /* delete bad character */
1976    {
1977     buffer[i]=buffer[k];
1978     m--;
1979    }
1980    buffer[m]=0;
1981    result=1;
1982   }
1983
1984  value=atoi(buffer);
1985  if (value>maxval)       /* build a new string */
1986  {
1987   sprintf(buffer,"%d",maxval);
1988   result=2;
1989  }
1990  if (result)
1991  {
1992   editpos=SendMessage16(hwnd,EM_GETSEL16,0,0);
1993   SetWindowText32A(hwnd,buffer);
1994   SendMessage16(hwnd,EM_SETSEL16,0,editpos);
1995  }
1996  return value;
1997 }
1998
1999
2000
2001 /***********************************************************************
2002  *                    CC_PaintSelectedColor                   [internal]
2003  */
2004 static void CC_PaintSelectedColor(HWND16 hDlg,COLORREF cr)
2005 {
2006  RECT16 rect;
2007  HDC32  hdc;
2008  HBRUSH32 hBrush;
2009  HWND32 hwnd=GetDlgItem32(hDlg,0x2c5);
2010  if (IsWindowVisible32(GetDlgItem32(hDlg,0x2c6)))   /* if full size */
2011  {
2012   hdc=GetDC32(hwnd);
2013   GetClientRect16 (hwnd, &rect) ;
2014   hBrush = CreateSolidBrush32(cr);
2015   if (hBrush)
2016   {
2017    hBrush = SelectObject32 (hdc, hBrush) ;
2018    Rectangle32(hdc, rect.left,rect.top,rect.right/2,rect.bottom);
2019    DeleteObject32 (SelectObject32 (hdc,hBrush)) ;
2020    hBrush=CreateSolidBrush32(GetNearestColor32(hdc,cr));
2021    if (hBrush)
2022    {
2023     hBrush= SelectObject32 (hdc, hBrush) ;
2024     Rectangle32( hdc, rect.right/2-1,rect.top,rect.right,rect.bottom);
2025     DeleteObject32( SelectObject32 (hdc, hBrush)) ;
2026    }
2027   }
2028   ReleaseDC32(hwnd,hdc);
2029  }
2030 }
2031
2032 /***********************************************************************
2033  *                    CC_PaintTriangle                        [internal]
2034  */
2035 static void CC_PaintTriangle(HWND16 hDlg,int y)
2036 {
2037  HDC32 hDC;
2038  long temp;
2039  int w=GetDialogBaseUnits();
2040  POINT16 points[3];
2041  int height;
2042  int oben;
2043  RECT16 rect;
2044  HWND16 hwnd=GetDlgItem32(hDlg,0x2be);
2045  struct CCPRIVATE *lpp=(struct CCPRIVATE *)GetWindowLong32A(hDlg, DWL_USER); 
2046
2047  if (IsWindowVisible32(GetDlgItem32(hDlg,0x2c6)))   /* if full size */
2048  {
2049    GetClientRect16(hwnd,&rect);
2050    height=rect.bottom;
2051    hDC=GetDC32(hDlg);
2052
2053    points[0].y=rect.top;
2054    points[0].x=rect.right;           /*  |  /|  */
2055    ClientToScreen16(hwnd,points);    /*  | / |  */
2056    ScreenToClient16(hDlg,points);    /*  |<  |  */
2057    oben=points[0].y;                 /*  | \ |  */
2058                                      /*  |  \|  */
2059    temp=(long)height*(long)y;
2060    points[0].y=oben+height -temp/(long)MAXVERT;
2061    points[1].y=points[0].y+w;
2062    points[2].y=points[0].y-w;
2063    points[2].x=points[1].x=points[0].x + w;
2064
2065    if (lpp->old3angle.left)
2066     FillRect16(hDC,&lpp->old3angle,GetStockObject32(WHITE_BRUSH));
2067    lpp->old3angle.left  =points[0].x;
2068    lpp->old3angle.right =points[1].x+1;
2069    lpp->old3angle.top   =points[2].y-1;
2070    lpp->old3angle.bottom=points[1].y+1;
2071    Polygon16(hDC,points,3);
2072    ReleaseDC32(hDlg,hDC);
2073  }
2074 }
2075
2076
2077 /***********************************************************************
2078  *                    CC_PaintCross                           [internal]
2079  */
2080 static void CC_PaintCross(HWND16 hDlg,int x,int y)
2081 {
2082  HDC32 hDC;
2083  int w=GetDialogBaseUnits();
2084  HWND16 hwnd=GetDlgItem32(hDlg,0x2c6);
2085  struct CCPRIVATE * lpp=(struct CCPRIVATE *)GetWindowLong32A(hDlg, DWL_USER); 
2086  RECT16 rect;
2087  POINT16 point;
2088  HPEN32 hPen;
2089
2090  if (IsWindowVisible32(GetDlgItem32(hDlg,0x2c6)))   /* if full size */
2091  {
2092    GetClientRect16(hwnd,&rect);
2093    hDC=GetDC32(hwnd);
2094    SelectClipRgn32(hDC,CreateRectRgnIndirect16(&rect));
2095    hPen=CreatePen32(PS_SOLID,2,0);
2096    hPen=SelectObject32(hDC,hPen);
2097    point.x=((long)rect.right*(long)x)/(long)MAXHORI;
2098    point.y=rect.bottom-((long)rect.bottom*(long)y)/(long)MAXVERT;
2099    if (lpp->oldcross.left!=lpp->oldcross.right)
2100      BitBlt32(hDC,lpp->oldcross.left,lpp->oldcross.top,
2101               lpp->oldcross.right-lpp->oldcross.left,
2102               lpp->oldcross.bottom-lpp->oldcross.top,
2103               lpp->hdcMem,lpp->oldcross.left,lpp->oldcross.top,SRCCOPY);
2104    lpp->oldcross.left  =point.x-w-1;
2105    lpp->oldcross.right =point.x+w+1;
2106    lpp->oldcross.top   =point.y-w-1;
2107    lpp->oldcross.bottom=point.y+w+1; 
2108
2109    MoveTo(hDC,point.x-w,point.y); 
2110    LineTo32(hDC,point.x+w,point.y);
2111    MoveTo(hDC,point.x,point.y-w); 
2112    LineTo32(hDC,point.x,point.y+w);
2113    DeleteObject32(SelectObject32(hDC,hPen));
2114    ReleaseDC32(hwnd,hDC);
2115  }
2116 }
2117
2118
2119 #define XSTEPS 48
2120 #define YSTEPS 24
2121
2122
2123 /***********************************************************************
2124  *                    CC_PrepareColorGraph                    [internal]
2125  */
2126 static void CC_PrepareColorGraph(HWND16 hDlg)    
2127 {
2128  int sdif,hdif,xdif,ydif,r,g,b,hue,sat;
2129  HWND32 hwnd=GetDlgItem32(hDlg,0x2c6);
2130  struct CCPRIVATE * lpp=(struct CCPRIVATE *)GetWindowLong32A(hDlg, DWL_USER);  
2131  HBRUSH32 hbrush;
2132  HDC32 hdc ;
2133  RECT16 rect,client;
2134  HCURSOR16 hcursor=SetCursor16(LoadCursor16(0,IDC_WAIT16));
2135
2136  GetClientRect16(hwnd,&client);
2137  hdc=GetDC32(hwnd);
2138  lpp->hdcMem = CreateCompatibleDC32(hdc);
2139  lpp->hbmMem = CreateCompatibleBitmap32(hdc,client.right,client.bottom);
2140  SelectObject32(lpp->hdcMem,lpp->hbmMem);
2141
2142  xdif=client.right /XSTEPS;
2143  ydif=client.bottom/YSTEPS+1;
2144  hdif=239/XSTEPS;
2145  sdif=240/YSTEPS;
2146  for(rect.left=hue=0;hue<239+hdif;hue+=hdif)
2147  {
2148   rect.right=rect.left+xdif;
2149   rect.bottom=client.bottom;
2150   for(sat=0;sat<240+sdif;sat+=sdif)
2151   {
2152    rect.top=rect.bottom-ydif;
2153    r=CC_HSLtoRGB('R',hue,sat,120);
2154    g=CC_HSLtoRGB('G',hue,sat,120);
2155    b=CC_HSLtoRGB('B',hue,sat,120);
2156    hbrush=CreateSolidBrush32(RGB(r,g,b));
2157    FillRect16(lpp->hdcMem,&rect,hbrush);
2158    DeleteObject32(hbrush);
2159    rect.bottom=rect.top;
2160   }
2161   rect.left=rect.right;
2162  }
2163  ReleaseDC32(hwnd,hdc);
2164  SetCursor16(hcursor);
2165 }
2166
2167 /***********************************************************************
2168  *                          CC_PaintColorGraph                [internal]
2169  */
2170 static void CC_PaintColorGraph(HWND16 hDlg)
2171 {
2172  HWND32 hwnd=GetDlgItem32(hDlg,0x2c6);
2173  struct CCPRIVATE * lpp=(struct CCPRIVATE *)GetWindowLong32A(hDlg, DWL_USER); 
2174  HDC32  hDC;
2175  RECT16 rect;
2176  if (IsWindowVisible32(hwnd))   /* if full size */
2177  {
2178   if (!lpp->hdcMem)
2179    CC_PrepareColorGraph(hDlg);   /* should not be necessary */
2180
2181   hDC=GetDC32(hwnd);
2182   GetClientRect16(hwnd,&rect);
2183   if (lpp->hdcMem)
2184       BitBlt32(hDC,0,0,rect.right,rect.bottom,lpp->hdcMem,0,0,SRCCOPY);
2185   else
2186     WARN(commdlg,"choose color: hdcMem is not defined\n");
2187   ReleaseDC32(hwnd,hDC);
2188  }
2189 }
2190 /***********************************************************************
2191  *                           CC_PaintLumBar                   [internal]
2192  */
2193 static void CC_PaintLumBar(HWND16 hDlg,int hue,int sat)
2194 {
2195  HWND32 hwnd=GetDlgItem32(hDlg,0x2be);
2196  RECT16 rect,client;
2197  int lum,ldif,ydif,r,g,b;
2198  HBRUSH32 hbrush;
2199  HDC32 hDC;
2200
2201  if (IsWindowVisible32(hwnd))
2202  {
2203   hDC=GetDC32(hwnd);
2204   GetClientRect16(hwnd,&client);
2205   rect=client;
2206
2207   ldif=240/YSTEPS;
2208   ydif=client.bottom/YSTEPS+1;
2209   for(lum=0;lum<240+ldif;lum+=ldif)
2210   {
2211    rect.top=MAX(0,rect.bottom-ydif);
2212    r=CC_HSLtoRGB('R',hue,sat,lum);
2213    g=CC_HSLtoRGB('G',hue,sat,lum);
2214    b=CC_HSLtoRGB('B',hue,sat,lum);
2215    hbrush=CreateSolidBrush32(RGB(r,g,b));
2216    FillRect16(hDC,&rect,hbrush);
2217    DeleteObject32(hbrush);
2218    rect.bottom=rect.top;
2219   }
2220   GetClientRect16(hwnd,&rect);
2221   FrameRect16(hDC,&rect,GetStockObject32(BLACK_BRUSH));
2222   ReleaseDC32(hwnd,hDC);
2223  }
2224 }
2225
2226 /***********************************************************************
2227  *                             CC_EditSetRGB                  [internal]
2228  */
2229 static void CC_EditSetRGB(HWND16 hDlg,COLORREF cr)
2230 {
2231  char buffer[10];
2232  struct CCPRIVATE * lpp=(struct CCPRIVATE *)GetWindowLong32A(hDlg, DWL_USER); 
2233  int r=GetRValue(cr);
2234  int g=GetGValue(cr);
2235  int b=GetBValue(cr);
2236  if (IsWindowVisible32(GetDlgItem32(hDlg,0x2c6)))   /* if full size */
2237  {
2238    lpp->updating=TRUE;
2239    sprintf(buffer,"%d",r);
2240    SetWindowText32A(GetDlgItem32(hDlg,0x2c2),buffer);
2241    sprintf(buffer,"%d",g);
2242    SetWindowText32A(GetDlgItem32(hDlg,0x2c3),buffer);
2243    sprintf(buffer,"%d",b);
2244    SetWindowText32A(GetDlgItem32(hDlg,0x2c4),buffer);
2245    lpp->updating=FALSE;
2246  }
2247 }
2248
2249 /***********************************************************************
2250  *                             CC_EditSetHSL                  [internal]
2251  */
2252 static void CC_EditSetHSL(HWND16 hDlg,int h,int s,int l)
2253 {
2254  char buffer[10];
2255  struct CCPRIVATE * lpp=(struct CCPRIVATE *)GetWindowLong32A(hDlg, DWL_USER); 
2256  lpp->updating=TRUE;
2257  if (IsWindowVisible32(GetDlgItem32(hDlg,0x2c6)))   /* if full size */
2258  {
2259    lpp->updating=TRUE;
2260    sprintf(buffer,"%d",h);
2261    SetWindowText32A(GetDlgItem32(hDlg,0x2bf),buffer);
2262    sprintf(buffer,"%d",s);
2263    SetWindowText32A(GetDlgItem32(hDlg,0x2c0),buffer);
2264    sprintf(buffer,"%d",l);
2265    SetWindowText32A(GetDlgItem32(hDlg,0x2c1),buffer);
2266    lpp->updating=FALSE;
2267  }
2268  CC_PaintLumBar(hDlg,h,s);
2269 }
2270
2271 /***********************************************************************
2272  *                       CC_SwitchToFullSize                  [internal]
2273  */
2274 static void CC_SwitchToFullSize(HWND16 hDlg,COLORREF result,LPRECT16 lprect)
2275 {
2276  int i;
2277  struct CCPRIVATE * lpp=(struct CCPRIVATE *)GetWindowLong32A(hDlg, DWL_USER); 
2278  
2279  EnableWindow32(GetDlgItem32(hDlg,0x2cf),FALSE);
2280  CC_PrepareColorGraph(hDlg);
2281  for (i=0x2bf;i<0x2c5;i++)
2282    EnableWindow32(GetDlgItem32(hDlg,i),TRUE);
2283  for (i=0x2d3;i<0x2d9;i++)
2284    EnableWindow32(GetDlgItem32(hDlg,i),TRUE);
2285  EnableWindow32(GetDlgItem32(hDlg,0x2c9),TRUE);
2286  EnableWindow32(GetDlgItem32(hDlg,0x2c8),TRUE);
2287
2288  if (lprect)
2289   SetWindowPos32(hDlg,NULL,0,0,lprect->right-lprect->left,
2290    lprect->bottom-lprect->top, SWP_NOMOVE|SWP_NOZORDER);
2291
2292  ShowWindow32(GetDlgItem32(hDlg,0x2c6),SW_SHOW);
2293  ShowWindow32(GetDlgItem32(hDlg,0x2be),SW_SHOW);
2294  ShowWindow32(GetDlgItem32(hDlg,0x2c5),SW_SHOW);
2295
2296  CC_EditSetRGB(hDlg,result);
2297  CC_EditSetHSL(hDlg,lpp->h,lpp->s,lpp->l);
2298 }
2299
2300 /***********************************************************************
2301  *                           CC_PaintPredefColorArray         [internal]
2302  */
2303 static void CC_PaintPredefColorArray(HWND16 hDlg,int rows,int cols)
2304 {
2305  HWND32 hwnd=GetDlgItem32(hDlg,0x2d0);
2306  RECT16 rect;
2307  HDC32  hdc;
2308  HBRUSH32 hBrush;
2309  int dx,dy,i,j,k;
2310
2311  GetClientRect16(hwnd,&rect);
2312  dx=rect.right/cols;
2313  dy=rect.bottom/rows;
2314  k=rect.left;
2315
2316  hdc=GetDC32(hwnd);
2317  GetClientRect16 (hwnd, &rect) ;
2318
2319  for (j=0;j<rows;j++)
2320  {
2321   for (i=0;i<cols;i++)
2322   {
2323    hBrush = CreateSolidBrush32(predefcolors[j][i]);
2324    if (hBrush)
2325    {
2326     hBrush = SelectObject32 (hdc, hBrush) ;
2327     Rectangle32(hdc, rect.left, rect.top,
2328                 rect.left+dx-DISTANCE, rect.top+dy-DISTANCE);
2329     rect.left=rect.left+dx;
2330     DeleteObject32( SelectObject32 (hdc, hBrush)) ;
2331    }
2332   }
2333   rect.top=rect.top+dy;
2334   rect.left=k;
2335  }
2336  ReleaseDC32(hwnd,hdc);
2337  /* FIXME: draw_a_focus_rect */
2338 }
2339 /***********************************************************************
2340  *                             CC_PaintUserColorArray         [internal]
2341  */
2342 static void CC_PaintUserColorArray(HWND16 hDlg,int rows,int cols,COLORREF* lpcr)
2343 {
2344  HWND32 hwnd=GetDlgItem32(hDlg,0x2d1);
2345  RECT16 rect;
2346  HDC32  hdc;
2347  HBRUSH32 hBrush;
2348  int dx,dy,i,j,k;
2349
2350  GetClientRect16(hwnd,&rect);
2351
2352  dx=rect.right/cols;
2353  dy=rect.bottom/rows;
2354  k=rect.left;
2355
2356  hdc=GetDC32(hwnd);
2357  if (hdc)
2358  {
2359   for (j=0;j<rows;j++)
2360   {
2361    for (i=0;i<cols;i++)
2362    {
2363     hBrush = CreateSolidBrush32(lpcr[i+j*cols]);
2364     if (hBrush)
2365     {
2366      hBrush = SelectObject32 (hdc, hBrush) ;
2367      Rectangle32( hdc, rect.left, rect.top,
2368                   rect.left+dx-DISTANCE, rect.top+dy-DISTANCE);
2369      rect.left=rect.left+dx;
2370      DeleteObject32( SelectObject32 (hdc, hBrush)) ;
2371     }
2372    }
2373    rect.top=rect.top+dy;
2374    rect.left=k;
2375   }
2376   ReleaseDC32(hwnd,hdc);
2377  }
2378  /* FIXME: draw_a_focus_rect */
2379 }
2380
2381
2382
2383 /***********************************************************************
2384  *                             CC_HookCallChk                 [internal]
2385  */
2386 static BOOL32 CC_HookCallChk(LPCHOOSECOLOR16 lpcc)
2387 {
2388  if (lpcc)
2389   if(lpcc->Flags & CC_ENABLEHOOK)
2390    if (lpcc->lpfnHook)
2391     return TRUE;
2392  return FALSE;
2393 }
2394
2395 /***********************************************************************
2396  *                            CC_WMInitDialog                 [internal]
2397  */
2398 static LONG CC_WMInitDialog(HWND16 hDlg, WPARAM16 wParam, LPARAM lParam) 
2399 {
2400    int i,res;
2401    HWND16 hwnd;
2402    RECT16 rect;
2403    POINT16 point;
2404    struct CCPRIVATE * lpp; 
2405    
2406    TRACE(commdlg,"WM_INITDIALOG lParam=%08lX\n", lParam);
2407    lpp=calloc(1,sizeof(struct CCPRIVATE));
2408    lpp->lpcc=(LPCHOOSECOLOR16)lParam;
2409    if (lpp->lpcc->lStructSize != sizeof(CHOOSECOLOR16))
2410    {
2411       EndDialog32 (hDlg, 0) ;
2412       return FALSE;
2413    }
2414    SetWindowLong32A(hDlg, DWL_USER, (LONG)lpp); 
2415
2416    if (!(lpp->lpcc->Flags & CC_SHOWHELP))
2417       ShowWindow32(GetDlgItem32(hDlg,0x40e),SW_HIDE);
2418    lpp->msetrgb=RegisterWindowMessage32A( SETRGBSTRING );
2419 #if 0
2420    cpos=MAKELONG(5,7); /* init */
2421    if (lpp->lpcc->Flags & CC_RGBINIT)
2422    {
2423      for (i=0;i<6;i++)
2424        for (j=0;j<8;j++)
2425         if (predefcolors[i][j]==lpp->lpcc->rgbResult)
2426         {
2427           cpos=MAKELONG(i,j);
2428           goto found;
2429         }
2430    }
2431    found:
2432    /* FIXME: Draw_a_focus_rect & set_init_values */
2433 #endif
2434    GetWindowRect16(hDlg,&lpp->fullsize);
2435    if (lpp->lpcc->Flags & CC_FULLOPEN || lpp->lpcc->Flags & CC_PREVENTFULLOPEN)
2436    {
2437       hwnd=GetDlgItem32(hDlg,0x2cf);
2438       EnableWindow32(hwnd,FALSE);
2439    }
2440    if (!(lpp->lpcc->Flags & CC_FULLOPEN) || lpp->lpcc->Flags & CC_PREVENTFULLOPEN)
2441    {
2442       rect=lpp->fullsize;
2443       res=rect.bottom-rect.top;
2444       hwnd=GetDlgItem32(hDlg,0x2c6); /* cut at left border */
2445       point.x=point.y=0;
2446       ClientToScreen16(hwnd,&point);
2447       ScreenToClient16(hDlg,&point);
2448       GetClientRect16(hDlg,&rect);
2449       point.x+=GetSystemMetrics32(SM_CXDLGFRAME);
2450       SetWindowPos32(hDlg,NULL,0,0,point.x,res,SWP_NOMOVE|SWP_NOZORDER);
2451
2452       ShowWindow32(GetDlgItem32(hDlg,0x2c6),SW_HIDE);
2453       ShowWindow32(GetDlgItem32(hDlg,0x2c5),SW_HIDE);
2454    }
2455    else
2456       CC_SwitchToFullSize(hDlg,lpp->lpcc->rgbResult,NULL);
2457    res=TRUE;
2458    for (i=0x2bf;i<0x2c5;i++)
2459      SendMessage16(GetDlgItem32(hDlg,i),EM_LIMITTEXT16,3,0);      /* max 3 digits:  xyz  */
2460    if (CC_HookCallChk(lpp->lpcc))
2461       res=CallWindowProc16(lpp->lpcc->lpfnHook,hDlg,WM_INITDIALOG,wParam,lParam);
2462    return res;
2463 }
2464
2465 /***********************************************************************
2466  *                              CC_WMCommand                  [internal]
2467  */
2468 static LRESULT CC_WMCommand(HWND16 hDlg, WPARAM16 wParam, LPARAM lParam) 
2469 {
2470     int r,g,b,i,xx;
2471     UINT16 cokmsg;
2472     HDC32 hdc;
2473     COLORREF *cr;
2474     struct CCPRIVATE * lpp=(struct CCPRIVATE *)GetWindowLong32A(hDlg, DWL_USER); 
2475     TRACE(commdlg,"CC_WMCommand wParam=%x lParam=%lx\n",wParam,lParam);
2476     switch (wParam)
2477     {
2478           case 0x2c2:  /* edit notify RGB */
2479           case 0x2c3:
2480           case 0x2c4:
2481                if (HIWORD(lParam)==EN_UPDATE && !lpp->updating)
2482                          {
2483                            i=CC_CheckDigitsInEdit(LOWORD(lParam),255);
2484                            r=GetRValue(lpp->lpcc->rgbResult);
2485                            g=GetGValue(lpp->lpcc->rgbResult);
2486                            b=GetBValue(lpp->lpcc->rgbResult);
2487                            xx=0;
2488                            switch (wParam)
2489                            {
2490                             case 0x2c2:if ((xx=(i!=r))) r=i;break;
2491                             case 0x2c3:if ((xx=(i!=g))) g=i;break;
2492                             case 0x2c4:if ((xx=(i!=b))) b=i;break;
2493                            }
2494                            if (xx) /* something has changed */
2495                            {
2496                             lpp->lpcc->rgbResult=RGB(r,g,b);
2497                             CC_PaintSelectedColor(hDlg,lpp->lpcc->rgbResult);
2498                             lpp->h=CC_RGBtoHSL('H',r,g,b);
2499                             lpp->s=CC_RGBtoHSL('S',r,g,b);
2500                             lpp->l=CC_RGBtoHSL('L',r,g,b);
2501                             CC_EditSetHSL(hDlg,lpp->h,lpp->s,lpp->l);
2502                             CC_PaintCross(hDlg,lpp->h,lpp->s);
2503                             CC_PaintTriangle(hDlg,lpp->l);
2504                            }
2505                          }
2506                  break;
2507                  
2508           case 0x2bf:  /* edit notify HSL */
2509           case 0x2c0:
2510           case 0x2c1:
2511                if (HIWORD(lParam)==EN_UPDATE && !lpp->updating)
2512                          {
2513                            i=CC_CheckDigitsInEdit(LOWORD(lParam),wParam==0x2bf?239:240);
2514                            xx=0;
2515                            switch (wParam)
2516                            {
2517                             case 0x2bf:if ((xx=(i!=lpp->h))) lpp->h=i;break;
2518                             case 0x2c0:if ((xx=(i!=lpp->s))) lpp->s=i;break;
2519                             case 0x2c1:if ((xx=(i!=lpp->l))) lpp->l=i;break;
2520                            }
2521                            if (xx) /* something has changed */
2522                            {
2523                             r=CC_HSLtoRGB('R',lpp->h,lpp->s,lpp->l);
2524                             g=CC_HSLtoRGB('G',lpp->h,lpp->s,lpp->l);
2525                             b=CC_HSLtoRGB('B',lpp->h,lpp->s,lpp->l);
2526                             lpp->lpcc->rgbResult=RGB(r,g,b);
2527                             CC_PaintSelectedColor(hDlg,lpp->lpcc->rgbResult);
2528                             CC_EditSetRGB(hDlg,lpp->lpcc->rgbResult);
2529                             CC_PaintCross(hDlg,lpp->h,lpp->s);
2530                             CC_PaintTriangle(hDlg,lpp->l);
2531                            }
2532                          }
2533                break;
2534                
2535           case 0x2cf:
2536                CC_SwitchToFullSize(hDlg,lpp->lpcc->rgbResult,&lpp->fullsize);
2537                InvalidateRect32( hDlg, NULL, TRUE );
2538                SetFocus32(GetDlgItem32(hDlg,0x2bf));
2539                break;
2540
2541           case 0x2c8:    /* add colors ... column by column */
2542                cr=PTR_SEG_TO_LIN(lpp->lpcc->lpCustColors);
2543                cr[(lpp->nextuserdef%2)*8 + lpp->nextuserdef/2]=lpp->lpcc->rgbResult;
2544                if (++lpp->nextuserdef==16)
2545                    lpp->nextuserdef=0;
2546                CC_PaintUserColorArray(hDlg,2,8,PTR_SEG_TO_LIN(lpp->lpcc->lpCustColors));
2547                break;
2548
2549           case 0x2c9:              /* resulting color */
2550                hdc=GetDC32(hDlg);
2551                lpp->lpcc->rgbResult=GetNearestColor32(hdc,lpp->lpcc->rgbResult);
2552                ReleaseDC32(hDlg,hdc);
2553                CC_EditSetRGB(hDlg,lpp->lpcc->rgbResult);
2554                CC_PaintSelectedColor(hDlg,lpp->lpcc->rgbResult);
2555                r=GetRValue(lpp->lpcc->rgbResult);
2556                g=GetGValue(lpp->lpcc->rgbResult);
2557                b=GetBValue(lpp->lpcc->rgbResult);
2558                lpp->h=CC_RGBtoHSL('H',r,g,b);
2559                lpp->s=CC_RGBtoHSL('S',r,g,b);
2560                lpp->l=CC_RGBtoHSL('L',r,g,b);
2561                CC_EditSetHSL(hDlg,lpp->h,lpp->s,lpp->l);
2562                CC_PaintCross(hDlg,lpp->h,lpp->s);
2563                CC_PaintTriangle(hDlg,lpp->l);
2564                break;
2565
2566           case 0x40e:           /* Help! */ /* The Beatles, 1965  ;-) */
2567                i=RegisterWindowMessage32A( HELPMSGSTRING );
2568                if (lpp->lpcc->hwndOwner)
2569                    SendMessage16(lpp->lpcc->hwndOwner,i,0,(LPARAM)lpp->lpcc);
2570                if (CC_HookCallChk(lpp->lpcc))
2571                    CallWindowProc16(lpp->lpcc->lpfnHook,hDlg,
2572                       WM_COMMAND,psh15,(LPARAM)lpp->lpcc);
2573                break;
2574
2575           case IDOK :
2576                 cokmsg=RegisterWindowMessage32A( COLOROKSTRING );
2577                 if (lpp->lpcc->hwndOwner)
2578                         if (SendMessage16(lpp->lpcc->hwndOwner,cokmsg,0,(LPARAM)lpp->lpcc))
2579                            break;    /* do NOT close */
2580
2581                 EndDialog32 (hDlg, 1) ;
2582                 return TRUE ;
2583         
2584           case IDCANCEL :
2585                 EndDialog32 (hDlg, 0) ;
2586                 return TRUE ;
2587
2588        }
2589        return FALSE;
2590 }
2591
2592 /***********************************************************************
2593  *                              CC_WMPaint                    [internal]
2594  */
2595 static LRESULT CC_WMPaint(HWND16 hDlg, WPARAM16 wParam, LPARAM lParam) 
2596 {
2597     struct CCPRIVATE * lpp=(struct CCPRIVATE *)GetWindowLong32A(hDlg, DWL_USER); 
2598     /* we have to paint dialog children except text and buttons */
2599  
2600     CC_PaintPredefColorArray(hDlg,6,8);
2601     CC_PaintUserColorArray(hDlg,2,8,PTR_SEG_TO_LIN(lpp->lpcc->lpCustColors));
2602     CC_PaintColorGraph(hDlg);
2603     CC_PaintLumBar(hDlg,lpp->h,lpp->s);
2604     CC_PaintCross(hDlg,lpp->h,lpp->s);
2605     CC_PaintTriangle(hDlg,lpp->l);
2606     CC_PaintSelectedColor(hDlg,lpp->lpcc->rgbResult);
2607
2608     /* special necessary for Wine */
2609     ValidateRect32(GetDlgItem32(hDlg,0x2d0),NULL);
2610     ValidateRect32(GetDlgItem32(hDlg,0x2d1),NULL);
2611     ValidateRect32(GetDlgItem32(hDlg,0x2c6),NULL);
2612     ValidateRect32(GetDlgItem32(hDlg,0x2be),NULL);
2613     ValidateRect32(GetDlgItem32(hDlg,0x2c5),NULL);
2614     /* hope we can remove it later -->FIXME */
2615  return 0;
2616 }
2617
2618
2619 /***********************************************************************
2620  *                              CC_WMLButtonDown              [internal]
2621  */
2622 static LRESULT CC_WMLButtonDown(HWND16 hDlg, WPARAM16 wParam, LPARAM lParam) 
2623 {
2624    struct CCPRIVATE * lpp=(struct CCPRIVATE *)GetWindowLong32A(hDlg, DWL_USER); 
2625    int r,g,b,i;
2626    i=0;
2627    if (CC_MouseCheckPredefColorArray(hDlg,0x2d0,6,8,lParam,&lpp->lpcc->rgbResult))
2628       i=1;
2629    else
2630       if (CC_MouseCheckUserColorArray(hDlg,0x2d1,2,8,lParam,&lpp->lpcc->rgbResult,
2631               PTR_SEG_TO_LIN(lpp->lpcc->lpCustColors)))
2632          i=1;
2633       else
2634          if (CC_MouseCheckColorGraph(hDlg,0x2c6,&lpp->h,&lpp->s,lParam))
2635             i=2;
2636          else
2637             if (CC_MouseCheckColorGraph(hDlg,0x2be,NULL,&lpp->l,lParam))
2638                i=2;
2639    if (i==2)
2640    {
2641       r=CC_HSLtoRGB('R',lpp->h,lpp->s,lpp->l);
2642       g=CC_HSLtoRGB('G',lpp->h,lpp->s,lpp->l);
2643       b=CC_HSLtoRGB('B',lpp->h,lpp->s,lpp->l);
2644       lpp->lpcc->rgbResult=RGB(r,g,b);
2645    }
2646    if (i==1)
2647    {
2648       r=GetRValue(lpp->lpcc->rgbResult);
2649       g=GetGValue(lpp->lpcc->rgbResult);
2650       b=GetBValue(lpp->lpcc->rgbResult);
2651       lpp->h=CC_RGBtoHSL('H',r,g,b);
2652       lpp->s=CC_RGBtoHSL('S',r,g,b);
2653       lpp->l=CC_RGBtoHSL('L',r,g,b);
2654    }
2655    if (i)
2656    {
2657       CC_EditSetRGB(hDlg,lpp->lpcc->rgbResult);
2658       CC_EditSetHSL(hDlg,lpp->h,lpp->s,lpp->l);
2659       CC_PaintCross(hDlg,lpp->h,lpp->s);
2660       CC_PaintTriangle(hDlg,lpp->l);
2661       CC_PaintSelectedColor(hDlg,lpp->lpcc->rgbResult);
2662       return TRUE;
2663    }
2664    return FALSE;
2665 }
2666
2667 /***********************************************************************
2668  *           ColorDlgProc   (COMMDLG.8)
2669  */
2670 LRESULT WINAPI ColorDlgProc(HWND16 hDlg, UINT16 message,
2671                             WPARAM16 wParam, LONG lParam)
2672 {
2673  int res;
2674  struct CCPRIVATE * lpp=(struct CCPRIVATE *)GetWindowLong32A(hDlg, DWL_USER); 
2675  if (message!=WM_INITDIALOG)
2676  {
2677   if (!lpp)
2678      return FALSE;
2679   res=0;
2680   if (CC_HookCallChk(lpp->lpcc))
2681      res=CallWindowProc16(lpp->lpcc->lpfnHook,hDlg,message,wParam,lParam);
2682   if (res)
2683      return res;
2684  }
2685
2686  /* FIXME: SetRGB message
2687  if (message && message==msetrgb)
2688     return HandleSetRGB(hDlg,lParam);
2689  */
2690
2691  switch (message)
2692         {
2693           case WM_INITDIALOG:
2694                         return CC_WMInitDialog(hDlg,wParam,lParam);
2695           case WM_NCDESTROY:
2696                         DeleteDC32(lpp->hdcMem); 
2697                         DeleteObject32(lpp->hbmMem); 
2698                         free(lpp);
2699                         SetWindowLong32A(hDlg, DWL_USER, 0L); /* we don't need it anymore */
2700                         break;
2701           case WM_COMMAND:
2702                         if (CC_WMCommand(hDlg, wParam, lParam))
2703                            return TRUE;
2704                         break;     
2705           case WM_PAINT:
2706                         CC_WMPaint(hDlg, wParam, lParam);
2707                         break;
2708           case WM_LBUTTONDBLCLK:
2709                         if (CC_MouseCheckResultWindow(hDlg,lParam))
2710                           return TRUE;
2711                         break;
2712           case WM_MOUSEMOVE:  /* FIXME: calculate new hue,sat,lum (if in color graph) */
2713                         break;
2714           case WM_LBUTTONUP:  /* FIXME: ClipCursor off (if in color graph)*/
2715                         break;
2716           case WM_LBUTTONDOWN:/* FIXME: ClipCursor on  (if in color graph)*/
2717                         if (CC_WMLButtonDown(hDlg, wParam, lParam))
2718                            return TRUE;
2719                         break;     
2720         }
2721      return FALSE ;
2722 }
2723
2724 static void CFn_CHOOSEFONT16to32A(LPCHOOSEFONT16 chf16, LPCHOOSEFONT32A chf32a)
2725 {
2726   chf32a->lStructSize=sizeof(CHOOSEFONT32A);
2727   chf32a->hwndOwner=chf16->hwndOwner;
2728   chf32a->hDC=chf16->hDC;
2729   chf32a->iPointSize=chf16->iPointSize;
2730   chf32a->Flags=chf16->Flags;
2731   chf32a->rgbColors=chf16->rgbColors;
2732   chf32a->lCustData=chf16->lCustData;
2733   chf32a->lpfnHook=NULL;
2734   chf32a->lpTemplateName=PTR_SEG_TO_LIN(chf16->lpTemplateName);
2735   chf32a->hInstance=chf16->hInstance;
2736   chf32a->lpszStyle=PTR_SEG_TO_LIN(chf16->lpszStyle);
2737   chf32a->nFontType=chf16->nFontType;
2738   chf32a->nSizeMax=chf16->nSizeMax;
2739   chf32a->nSizeMin=chf16->nSizeMin;
2740   FONT_LogFont16To32A(PTR_SEG_TO_LIN(chf16->lpLogFont), chf32a->lpLogFont);
2741 }
2742
2743
2744 /***********************************************************************
2745  *                        ChooseFont16   (COMMDLG.15)     
2746  */
2747 BOOL16 WINAPI ChooseFont16(LPCHOOSEFONT16 lpChFont)
2748 {
2749     HINSTANCE16 hInst;
2750     HANDLE16 hDlgTmpl = 0;
2751     BOOL16 bRet = FALSE, win32Format = FALSE;
2752     LPCVOID template;
2753     HWND32 hwndDialog;
2754     CHOOSEFONT32A cf32a;
2755     LOGFONT32A lf32a;
2756     SEGPTR lpTemplateName;
2757     
2758     cf32a.lpLogFont=&lf32a;
2759     CFn_CHOOSEFONT16to32A(lpChFont, &cf32a);
2760
2761     TRACE(commdlg,"ChooseFont\n");
2762     if (!lpChFont) return FALSE;    
2763
2764     if (lpChFont->Flags & CF_ENABLETEMPLATEHANDLE)
2765     {
2766         if (!(template = LockResource16( lpChFont->hInstance )))
2767         {
2768             CommDlgLastError = CDERR_LOADRESFAILURE;
2769             return FALSE;
2770         }
2771     }
2772     else if (lpChFont->Flags & CF_ENABLETEMPLATE)
2773     {
2774         HANDLE16 hResInfo;
2775         if (!(hResInfo = FindResource16( lpChFont->hInstance,
2776                                          lpChFont->lpTemplateName,
2777                                          RT_DIALOG16)))
2778         {
2779             CommDlgLastError = CDERR_FINDRESFAILURE;
2780             return FALSE;
2781         }
2782         if (!(hDlgTmpl = LoadResource16( lpChFont->hInstance, hResInfo )) ||
2783             !(template = LockResource16( hDlgTmpl )))
2784         {
2785             CommDlgLastError = CDERR_LOADRESFAILURE;
2786             return FALSE;
2787         }
2788     }
2789     else
2790     {
2791         template = SYSRES_GetResPtr( SYSRES_DIALOG_CHOOSE_FONT );
2792         win32Format = TRUE;
2793     }
2794
2795     hInst = WIN_GetWindowInstance( lpChFont->hwndOwner );
2796     
2797     /* lpTemplateName is not used in the dialog */
2798     lpTemplateName=lpChFont->lpTemplateName;
2799     lpChFont->lpTemplateName=(SEGPTR)&cf32a;
2800     
2801     hwndDialog = DIALOG_CreateIndirect( hInst, template, win32Format,
2802                                         lpChFont->hwndOwner,
2803                       (DLGPROC16)MODULE_GetWndProcEntry16("FormatCharDlgProc"),
2804                                         (DWORD)lpChFont, WIN_PROC_16 );
2805     if (hwndDialog) bRet = DIALOG_DoDialogBox(hwndDialog, lpChFont->hwndOwner);
2806     if (hDlgTmpl) FreeResource16( hDlgTmpl );
2807     lpChFont->lpTemplateName=lpTemplateName;
2808     FONT_LogFont32ATo16(cf32a.lpLogFont, 
2809         (LPLOGFONT16)(PTR_SEG_TO_LIN(lpChFont->lpLogFont)));
2810     return bRet;
2811 }
2812
2813
2814 /***********************************************************************
2815  *           ChooseFont32A   (COMDLG32.3)
2816  */
2817 BOOL32 WINAPI ChooseFont32A(LPCHOOSEFONT32A lpChFont)
2818 {
2819   BOOL32 bRet=FALSE;
2820   HWND32 hwndDialog;
2821   HINSTANCE32 hInst=WIN_GetWindowInstance( lpChFont->hwndOwner );
2822   LPCVOID template = SYSRES_GetResPtr( SYSRES_DIALOG_CHOOSE_FONT );
2823   if (lpChFont->Flags & (CF_SELECTSCRIPT | CF_NOVERTFONTS | CF_ENABLETEMPLATE |
2824     CF_ENABLETEMPLATEHANDLE)) FIXME(commdlg, ": unimplemented flag (ignored)\n");
2825   hwndDialog = DIALOG_CreateIndirect(hInst, template, TRUE, lpChFont->hwndOwner,
2826             (DLGPROC16)FormatCharDlgProc32A, (LPARAM)lpChFont, WIN_PROC_32A );
2827   if (hwndDialog) bRet = DIALOG_DoDialogBox(hwndDialog, lpChFont->hwndOwner);  
2828   return bRet;
2829 }
2830
2831 /***********************************************************************
2832  *           ChooseFont32W   (COMDLG32.4)
2833  */
2834 BOOL32 WINAPI ChooseFont32W(LPCHOOSEFONT32W lpChFont)
2835 {
2836   BOOL32 bRet=FALSE;
2837   HWND32 hwndDialog;
2838   HINSTANCE32 hInst=WIN_GetWindowInstance( lpChFont->hwndOwner );
2839   CHOOSEFONT32A cf32a;
2840   LOGFONT32A lf32a;
2841   LPCVOID template = SYSRES_GetResPtr( SYSRES_DIALOG_CHOOSE_FONT );
2842   if (lpChFont->Flags & (CF_SELECTSCRIPT | CF_NOVERTFONTS | CF_ENABLETEMPLATE |
2843     CF_ENABLETEMPLATEHANDLE)) FIXME(commdlg, ": unimplemented flag (ignored)\n");
2844   memcpy(&cf32a, lpChFont, sizeof(cf32a));
2845   memcpy(&lf32a, lpChFont->lpLogFont, sizeof(LOGFONT32A));
2846   lstrcpynWtoA(lf32a.lfFaceName, lpChFont->lpLogFont->lfFaceName, LF_FACESIZE);
2847   cf32a.lpLogFont=&lf32a;
2848   cf32a.lpszStyle=HEAP_strdupWtoA(GetProcessHeap(), 0, lpChFont->lpszStyle);
2849   lpChFont->lpTemplateName=(LPWSTR)&cf32a;
2850   hwndDialog=DIALOG_CreateIndirect(hInst, template, TRUE, lpChFont->hwndOwner,
2851             (DLGPROC16)FormatCharDlgProc32W, (LPARAM)lpChFont, WIN_PROC_32W );
2852   if (hwndDialog)bRet=DIALOG_DoDialogBox(hwndDialog, lpChFont->hwndOwner);  
2853   HeapFree(GetProcessHeap(), 0, cf32a.lpszStyle);
2854   lpChFont->lpTemplateName=(LPWSTR)cf32a.lpTemplateName;
2855   memcpy(lpChFont->lpLogFont, &lf32a, sizeof(CHOOSEFONT32A));
2856   lstrcpynAtoW(lpChFont->lpLogFont->lfFaceName, lf32a.lfFaceName, LF_FACESIZE);
2857   return bRet;
2858 }
2859
2860
2861 #define TEXT_EXTRAS 4
2862 #define TEXT_COLORS 16
2863
2864 static const COLORREF textcolors[TEXT_COLORS]=
2865 {
2866  0x00000000L,0x00000080L,0x00008000L,0x00008080L,
2867  0x00800000L,0x00800080L,0x00808000L,0x00808080L,
2868  0x00c0c0c0L,0x000000ffL,0x0000ff00L,0x0000ffffL,
2869  0x00ff0000L,0x00ff00ffL,0x00ffff00L,0x00FFFFFFL
2870 };
2871
2872 /***********************************************************************
2873  *                          CFn_HookCallChk                 [internal]
2874  */
2875 static BOOL32 CFn_HookCallChk(LPCHOOSEFONT16 lpcf)
2876 {
2877  if (lpcf)
2878   if(lpcf->Flags & CF_ENABLEHOOK)
2879    if (lpcf->lpfnHook)
2880     return TRUE;
2881  return FALSE;
2882 }
2883
2884 /***********************************************************************
2885  *                          CFn_HookCallChk32                 [internal]
2886  */
2887 static BOOL32 CFn_HookCallChk32(LPCHOOSEFONT32A lpcf)
2888 {
2889  if (lpcf)
2890   if(lpcf->Flags & CF_ENABLEHOOK)
2891    if (lpcf->lpfnHook)
2892     return TRUE;
2893  return FALSE;
2894 }
2895
2896
2897 /*************************************************************************
2898  *              AddFontFamily                               [internal]
2899  */
2900 static INT32 AddFontFamily(LPLOGFONT32A lplf, UINT32 nFontType, 
2901                            LPCHOOSEFONT32A lpcf, HWND32 hwnd)
2902 {
2903   int i;
2904   WORD w;
2905
2906   TRACE(commdlg,"font=%s (nFontType=%d)\n", lplf->lfFaceName,nFontType);
2907
2908   if (lpcf->Flags & CF_FIXEDPITCHONLY)
2909    if (!(lplf->lfPitchAndFamily & FIXED_PITCH))
2910      return 1;
2911   if (lpcf->Flags & CF_ANSIONLY)
2912    if (lplf->lfCharSet != ANSI_CHARSET)
2913      return 1;
2914   if (lpcf->Flags & CF_TTONLY)
2915    if (!(nFontType & TRUETYPE_FONTTYPE))
2916      return 1;   
2917
2918   i=SendMessage32A(hwnd, CB_ADDSTRING32, 0, (LPARAM)lplf->lfFaceName);
2919   if (i!=CB_ERR)
2920   {
2921     w=(lplf->lfCharSet << 8) | lplf->lfPitchAndFamily;
2922     SendMessage32A(hwnd, CB_SETITEMDATA32, i, MAKELONG(nFontType,w));
2923     return 1 ;        /* store some important font information */
2924   }
2925   else
2926     return 0;
2927 }
2928
2929 typedef struct
2930 {
2931   HWND32 hWnd1;
2932   HWND32 hWnd2;
2933   LPCHOOSEFONT32A lpcf32a;
2934 } CFn_ENUMSTRUCT, *LPCFn_ENUMSTRUCT;
2935
2936 /*************************************************************************
2937  *              FontFamilyEnumProc32                           [internal]
2938  */
2939 INT32 WINAPI FontFamilyEnumProc32(LPENUMLOGFONT32A lpEnumLogFont, 
2940           LPNEWTEXTMETRIC32A metrics, UINT32 nFontType, LPARAM lParam)
2941 {
2942   LPCFn_ENUMSTRUCT e;
2943   e=(LPCFn_ENUMSTRUCT)lParam;
2944   return AddFontFamily(&lpEnumLogFont->elfLogFont, nFontType, e->lpcf32a, e->hWnd1);
2945 }
2946
2947 /***********************************************************************
2948  *                FontFamilyEnumProc16                     (COMMDLG.19)
2949  */
2950 INT16 WINAPI FontFamilyEnumProc16( SEGPTR logfont, SEGPTR metrics,
2951                                    UINT16 nFontType, LPARAM lParam )
2952 {
2953   HWND16 hwnd=LOWORD(lParam);
2954   HWND16 hDlg=GetParent16(hwnd);
2955   LPCHOOSEFONT16 lpcf=(LPCHOOSEFONT16)GetWindowLong32A(hDlg, DWL_USER); 
2956   LOGFONT16 *lplf = (LOGFONT16 *)PTR_SEG_TO_LIN( logfont );
2957   LOGFONT32A lf32a;
2958   FONT_LogFont16To32A(lplf, &lf32a);
2959   return AddFontFamily(&lf32a, nFontType, (LPCHOOSEFONT32A)lpcf->lpTemplateName,
2960                        hwnd);
2961 }
2962
2963 /*************************************************************************
2964  *              SetFontStylesToCombo2                           [internal]
2965  *
2966  * Fill font style information into combobox  (without using font.c directly)
2967  */
2968 static int SetFontStylesToCombo2(HWND32 hwnd, HDC32 hdc, LPLOGFONT32A lplf)
2969 {
2970    #define FSTYLES 4
2971    struct FONTSTYLE
2972           { int italic; 
2973             int weight;
2974             char stname[20]; };
2975    static struct FONTSTYLE fontstyles[FSTYLES]={ 
2976           { 0,FW_NORMAL,"Regular"},{0,FW_BOLD,"Bold"},
2977           { 1,FW_NORMAL,"Italic"}, {1,FW_BOLD,"Bold Italic"}};
2978    HFONT16 hf;
2979    TEXTMETRIC16 tm;
2980    int i,j;
2981
2982    for (i=0;i<FSTYLES;i++)
2983    {
2984      lplf->lfItalic=fontstyles[i].italic;
2985      lplf->lfWeight=fontstyles[i].weight;
2986      hf=CreateFontIndirect32A(lplf);
2987      hf=SelectObject32(hdc,hf);
2988      GetTextMetrics16(hdc,&tm);
2989      hf=SelectObject32(hdc,hf);
2990      DeleteObject32(hf);
2991
2992      if (tm.tmWeight==fontstyles[i].weight &&
2993          tm.tmItalic==fontstyles[i].italic)    /* font successful created ? */
2994      {
2995        char *str = SEGPTR_STRDUP(fontstyles[i].stname);
2996        j=SendMessage16(hwnd,CB_ADDSTRING16,0,(LPARAM)SEGPTR_GET(str) );
2997        SEGPTR_FREE(str);
2998        if (j==CB_ERR) return 1;
2999        j=SendMessage16(hwnd, CB_SETITEMDATA16, j, 
3000                                  MAKELONG(fontstyles[i].weight,fontstyles[i].italic));
3001        if (j==CB_ERR) return 1;                                 
3002      }
3003    }  
3004   return 0;
3005 }
3006
3007 /*************************************************************************
3008  *              AddFontSizeToCombo3                           [internal]
3009  */
3010 static int AddFontSizeToCombo3(HWND32 hwnd, UINT32 h, LPCHOOSEFONT32A lpcf)
3011 {
3012     int j;
3013     char buffer[20];
3014
3015     if (  (!(lpcf->Flags & CF_LIMITSIZE))  ||
3016         ((lpcf->Flags & CF_LIMITSIZE) && (h >= lpcf->nSizeMin) && (h <= lpcf->nSizeMax)))
3017     {
3018         sprintf(buffer, "%2d", h);
3019         j=SendMessage32A(hwnd, CB_FINDSTRINGEXACT32, -1, (LPARAM)buffer);
3020         if (j==CB_ERR)
3021         {
3022             j=SendMessage32A(hwnd, CB_ADDSTRING32, 0, (LPARAM)buffer);  
3023             if (j!=CB_ERR) j = SendMessage32A(hwnd, CB_SETITEMDATA32, j, h); 
3024             if (j==CB_ERR) return 1;
3025         }
3026     }
3027     return 0;
3028
3029  
3030 /*************************************************************************
3031  *              SetFontSizesToCombo3                           [internal]
3032  */
3033 static int SetFontSizesToCombo3(HWND32 hwnd, LPCHOOSEFONT32A lpcf)
3034 {
3035   static const int sizes[]={8,9,10,11,12,14,16,18,20,22,24,26,28,36,48,72,0};
3036   int i;
3037
3038   for (i=0; sizes[i]; i++)
3039     if (AddFontSizeToCombo3(hwnd, sizes[i], lpcf)) return 1;
3040   return 0;
3041 }
3042
3043 /***********************************************************************
3044  *                 AddFontStyle                          [internal]
3045  */
3046 INT32 AddFontStyle(LPLOGFONT32A lplf, UINT32 nFontType, 
3047     LPCHOOSEFONT32A lpcf, HWND32 hcmb2, HWND32 hcmb3, HWND32 hDlg)
3048 {
3049   int i;
3050   
3051   TRACE(commdlg,"(nFontType=%d)\n",nFontType);
3052   TRACE(commdlg,"  %s h=%d w=%d e=%d o=%d wg=%d i=%d u=%d s=%d"
3053                " ch=%d op=%d cp=%d q=%d pf=%xh\n",
3054                lplf->lfFaceName,lplf->lfHeight,lplf->lfWidth,
3055                lplf->lfEscapement,lplf->lfOrientation,
3056                lplf->lfWeight,lplf->lfItalic,lplf->lfUnderline,
3057                lplf->lfStrikeOut,lplf->lfCharSet, lplf->lfOutPrecision,
3058                lplf->lfClipPrecision,lplf->lfQuality, lplf->lfPitchAndFamily);
3059   if (nFontType & RASTER_FONTTYPE)
3060   {
3061     if (AddFontSizeToCombo3(hcmb3, lplf->lfHeight, lpcf)) return 0;
3062   } else if (SetFontSizesToCombo3(hcmb3, lpcf)) return 0;
3063
3064   if (!SendMessage32A(hcmb2, CB_GETCOUNT32, 0, 0))
3065   {
3066        HDC32 hdc= (lpcf->Flags & CF_PRINTERFONTS && lpcf->hDC) ? lpcf->hDC : GetDC32(hDlg);
3067        i=SetFontStylesToCombo2(hcmb2,hdc,lplf);
3068        if (!(lpcf->Flags & CF_PRINTERFONTS && lpcf->hDC))
3069          ReleaseDC32(hDlg,hdc);
3070        if (i)
3071         return 0;
3072   }
3073   return 1 ;
3074
3075 }    
3076
3077 /***********************************************************************
3078  *                 FontStyleEnumProc16                     (COMMDLG.18)
3079  */
3080 INT16 WINAPI FontStyleEnumProc16( SEGPTR logfont, SEGPTR metrics,
3081                                   UINT16 nFontType, LPARAM lParam )
3082 {
3083   HWND16 hcmb2=LOWORD(lParam);
3084   HWND16 hcmb3=HIWORD(lParam);
3085   HWND16 hDlg=GetParent16(hcmb3);
3086   LPCHOOSEFONT16 lpcf=(LPCHOOSEFONT16)GetWindowLong32A(hDlg, DWL_USER); 
3087   LOGFONT16 *lplf = (LOGFONT16 *)PTR_SEG_TO_LIN(logfont);
3088   LOGFONT32A lf32a;
3089   FONT_LogFont16To32A(lplf, &lf32a);
3090   return AddFontStyle(&lf32a, nFontType, (LPCHOOSEFONT32A)lpcf->lpTemplateName,
3091                       hcmb2, hcmb3, hDlg);
3092 }
3093
3094 /***********************************************************************
3095  *                 FontStyleEnumProc32                     [internal]
3096  */
3097 INT32 WINAPI FontStyleEnumProc32( LPENUMLOGFONT32A lpFont, 
3098           LPNEWTEXTMETRIC32A metrics, UINT32 nFontType, LPARAM lParam )
3099 {
3100   LPCFn_ENUMSTRUCT s=(LPCFn_ENUMSTRUCT)lParam;
3101   HWND32 hcmb2=s->hWnd1;
3102   HWND32 hcmb3=s->hWnd2;
3103   HWND32 hDlg=GetParent32(hcmb3);
3104   return AddFontStyle(&lpFont->elfLogFont, nFontType, s->lpcf32a, hcmb2,
3105                       hcmb3, hDlg);
3106 }
3107
3108 /***********************************************************************
3109  *           CFn_WMInitDialog                            [internal]
3110  */
3111 LRESULT CFn_WMInitDialog(HWND32 hDlg, WPARAM32 wParam, LPARAM lParam,
3112                          LPCHOOSEFONT32A lpcf)
3113 {
3114   HDC32 hdc;
3115   int i,j,res,init=0;
3116   long l;
3117   LPLOGFONT32A lpxx;
3118   HCURSOR32 hcursor=SetCursor32(LoadCursor32A(0,IDC_WAIT32A));
3119
3120   SetWindowLong32A(hDlg, DWL_USER, lParam); 
3121   lpxx=lpcf->lpLogFont;
3122   TRACE(commdlg,"WM_INITDIALOG lParam=%08lX\n", lParam);
3123
3124   if (lpcf->lStructSize != sizeof(CHOOSEFONT32A))
3125   {
3126     ERR(commdlg,"structure size failure !!!\n");
3127     EndDialog32 (hDlg, 0); 
3128     return FALSE;
3129   }
3130   if (!hBitmapTT)
3131     hBitmapTT = LoadBitmap32A(0, MAKEINTRESOURCE32A(OBM_TRTYPE));
3132
3133   /* This font will be deleted by WM_COMMAND */
3134   SendDlgItemMessage32A(hDlg,stc6,WM_SETFONT,
3135      CreateFont32A(0, 0, 1, 1, 400, 0, 0, 0, 0, 0, 0, 0, 0, NULL),FALSE);
3136                          
3137   if (!(lpcf->Flags & CF_SHOWHELP) || !IsWindow32(lpcf->hwndOwner))
3138     ShowWindow32(GetDlgItem32(hDlg,pshHelp),SW_HIDE);
3139   if (!(lpcf->Flags & CF_APPLY))
3140     ShowWindow32(GetDlgItem32(hDlg,psh3),SW_HIDE);
3141   if (lpcf->Flags & CF_EFFECTS)
3142   {
3143     for (res=1,i=0;res && i<TEXT_COLORS;i++)
3144     {
3145       /* FIXME: load color name from resource:  res=LoadString(...,i+....,buffer,.....); */
3146       char name[20];
3147       strcpy( name, "[color name]" );
3148       j=SendDlgItemMessage32A(hDlg, cmb4, CB_ADDSTRING32, 0, (LPARAM)name);
3149       SendDlgItemMessage32A(hDlg, cmb4, CB_SETITEMDATA16, j, textcolors[j]);
3150       /* look for a fitting value in color combobox */
3151       if (textcolors[j]==lpcf->rgbColors)
3152         SendDlgItemMessage32A(hDlg,cmb4, CB_SETCURSEL32,j,0);
3153     }
3154   }
3155   else
3156   {
3157     ShowWindow32(GetDlgItem32(hDlg,cmb4),SW_HIDE);
3158     ShowWindow32(GetDlgItem32(hDlg,chx1),SW_HIDE);
3159     ShowWindow32(GetDlgItem32(hDlg,chx2),SW_HIDE);
3160     ShowWindow32(GetDlgItem32(hDlg,grp1),SW_HIDE);
3161     ShowWindow32(GetDlgItem32(hDlg,stc4),SW_HIDE);
3162   }
3163   hdc= (lpcf->Flags & CF_PRINTERFONTS && lpcf->hDC) ? lpcf->hDC : GetDC32(hDlg);
3164   if (hdc)
3165   {
3166     CFn_ENUMSTRUCT s;
3167     s.hWnd1=GetDlgItem32(hDlg,cmb1);
3168     s.lpcf32a=lpcf;
3169     if (!EnumFontFamilies32A(hdc, NULL, FontFamilyEnumProc32, (LPARAM)&s))
3170       TRACE(commdlg,"EnumFontFamilies returns 0\n");
3171     if (lpcf->Flags & CF_INITTOLOGFONTSTRUCT)
3172     {
3173       /* look for fitting font name in combobox1 */
3174       j=SendDlgItemMessage32A(hDlg,cmb1,CB_FINDSTRING32,-1,(LONG)lpxx->lfFaceName);
3175       if (j!=CB_ERR)
3176       {
3177         SendDlgItemMessage32A(hDlg, cmb1, CB_SETCURSEL32, j, 0);
3178         SendMessage32A(hDlg, WM_COMMAND, MAKEWPARAM(cmb1, CBN_SELCHANGE),
3179                        GetDlgItem32(hDlg,cmb1));
3180         init=1;
3181         /* look for fitting font style in combobox2 */
3182         l=MAKELONG(lpxx->lfWeight > FW_MEDIUM ? FW_BOLD:FW_NORMAL,lpxx->lfItalic !=0);
3183         for (i=0;i<TEXT_EXTRAS;i++)
3184         {
3185           if (l==SendDlgItemMessage32A(hDlg, cmb2, CB_GETITEMDATA32, i, 0))
3186             SendDlgItemMessage32A(hDlg, cmb2, CB_SETCURSEL32, i, 0);
3187         }
3188       
3189         /* look for fitting font size in combobox3 */
3190         j=SendDlgItemMessage32A(hDlg, cmb3, CB_GETCOUNT32, 0, 0);
3191         for (i=0;i<j;i++)
3192         {
3193           if (lpxx->lfHeight==(int)SendDlgItemMessage32A(hDlg,cmb3, CB_GETITEMDATA32,i,0))
3194             SendDlgItemMessage32A(hDlg,cmb3,CB_SETCURSEL32,i,0);
3195         }
3196       }
3197     }
3198     if (!init)
3199     {
3200       SendDlgItemMessage32A(hDlg,cmb1,CB_SETCURSEL32,0,0);
3201       SendMessage32A(hDlg, WM_COMMAND, MAKEWPARAM(cmb1, CBN_SELCHANGE),
3202                        GetDlgItem32(hDlg,cmb1));
3203     }    
3204     if (lpcf->Flags & CF_USESTYLE && lpcf->lpszStyle)
3205     {
3206       j=SendDlgItemMessage32A(hDlg,cmb2,CB_FINDSTRING32,-1,(LONG)lpcf->lpszStyle);
3207       if (j!=CB_ERR)
3208       {
3209         j=SendDlgItemMessage32A(hDlg,cmb2,CB_SETCURSEL32,j,0);
3210         SendMessage32A(hDlg,WM_COMMAND,cmb2,
3211                        MAKELONG(GetDlgItem32(hDlg,cmb2),CBN_SELCHANGE));
3212       }
3213     }
3214   }
3215   else
3216   {
3217     WARN(commdlg,"HDC failure !!!\n");
3218     EndDialog32 (hDlg, 0); 
3219     return FALSE;
3220   }
3221
3222   if (!(lpcf->Flags & CF_PRINTERFONTS && lpcf->hDC))
3223     ReleaseDC32(hDlg,hdc);
3224   SetCursor32(hcursor);   
3225   return TRUE;
3226 }
3227
3228
3229 /***********************************************************************
3230  *           CFn_WMMeasureItem                           [internal]
3231  */
3232 LRESULT CFn_WMMeasureItem(HWND32 hDlg, WPARAM32 wParam, LPARAM lParam)
3233 {
3234   BITMAP32 bm;
3235   LPMEASUREITEMSTRUCT32 lpmi=(LPMEASUREITEMSTRUCT32)lParam;
3236   if (!hBitmapTT)
3237     hBitmapTT = LoadBitmap32A(0, MAKEINTRESOURCE32A(OBM_TRTYPE));
3238   GetObject32A( hBitmapTT, sizeof(bm), &bm );
3239   lpmi->itemHeight=bm.bmHeight;
3240   /* FIXME: use MAX of bm.bmHeight and tm.tmHeight .*/
3241   return 0;
3242 }
3243
3244
3245 /***********************************************************************
3246  *           CFn_WMDrawItem                              [internal]
3247  */
3248 LRESULT CFn_WMDrawItem(HWND32 hDlg, WPARAM32 wParam, LPARAM lParam)
3249 {
3250   HBRUSH32 hBrush;
3251   char buffer[40];
3252   BITMAP32 bm;
3253   COLORREF cr, oldText=0, oldBk=0;
3254   RECT32 rect;
3255 #if 0  
3256   HDC32 hMemDC;
3257   int nFontType;
3258   HBITMAP32 hBitmap; /* for later TT usage */
3259 #endif  
3260   LPDRAWITEMSTRUCT32 lpdi = (LPDRAWITEMSTRUCT32)lParam;
3261
3262   if (lpdi->itemID == 0xFFFF)                   /* got no items */
3263     DrawFocusRect32(lpdi->hDC, &lpdi->rcItem);
3264   else
3265   {
3266    if (lpdi->CtlType == ODT_COMBOBOX)
3267    {
3268      if (lpdi->itemState ==ODS_SELECTED)
3269      {
3270        hBrush=GetSysColorBrush32(COLOR_HIGHLIGHT);
3271        oldText=SetTextColor32(lpdi->hDC, GetSysColor32(COLOR_HIGHLIGHTTEXT));
3272        oldBk=SetBkColor32(lpdi->hDC, GetSysColor32(COLOR_HIGHLIGHT));
3273      }  else
3274      {
3275        hBrush = SelectObject32(lpdi->hDC, GetStockObject32(LTGRAY_BRUSH));
3276        SelectObject32(lpdi->hDC, hBrush);
3277      }
3278      FillRect32(lpdi->hDC, &lpdi->rcItem, hBrush);
3279    }
3280    else
3281      return TRUE;       /* this should never happen */
3282
3283    rect=lpdi->rcItem;
3284    switch (lpdi->CtlID)
3285    {
3286     case cmb1:  /* TRACE(commdlg,"WM_Drawitem cmb1\n"); */
3287                 SendMessage32A(lpdi->hwndItem, CB_GETLBTEXT32, lpdi->itemID,
3288                                (LPARAM)buffer);           
3289                 GetObject32A( hBitmapTT, sizeof(bm), &bm );
3290                 TextOut32A(lpdi->hDC, lpdi->rcItem.left + bm.bmWidth + 10,
3291                            lpdi->rcItem.top, buffer, lstrlen32A(buffer));
3292 #if 0
3293                 nFontType = SendMessage32A(lpdi->hwndItem, CB_GETITEMDATA32, lpdi->itemID,0L);
3294                   /* FIXME: draw bitmap if truetype usage */
3295                 if (nFontType&TRUETYPE_FONTTYPE)
3296                 {
3297                   hMemDC = CreateCompatibleDC32(lpdi->hDC);
3298                   hBitmap = SelectObject32(hMemDC, hBitmapTT);
3299                   BitBlt32(lpdi->hDC, lpdi->rcItem.left, lpdi->rcItem.top,
3300                            bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
3301                   SelectObject32(hMemDC, hBitmap);
3302                   DeleteDC32(hMemDC);
3303                 }
3304 #endif
3305                 break;
3306     case cmb2:
3307     case cmb3:  /* TRACE(commdlg,"WM_DRAWITEN cmb2,cmb3\n"); */
3308                 SendMessage32A(lpdi->hwndItem, CB_GETLBTEXT32, lpdi->itemID,
3309                                (LPARAM)buffer);
3310                 TextOut32A(lpdi->hDC, lpdi->rcItem.left,
3311                            lpdi->rcItem.top, buffer, lstrlen32A(buffer));
3312                 break;
3313
3314     case cmb4:  /* TRACE(commdlg,"WM_DRAWITEM cmb4 (=COLOR)\n"); */
3315                 SendMessage32A(lpdi->hwndItem, CB_GETLBTEXT32, lpdi->itemID,
3316                                (LPARAM)buffer);
3317                 TextOut32A(lpdi->hDC, lpdi->rcItem.left +  25+5,
3318                            lpdi->rcItem.top, buffer, lstrlen32A(buffer));
3319                 cr = SendMessage32A(lpdi->hwndItem, CB_GETITEMDATA32, lpdi->itemID,0L);
3320                 hBrush = CreateSolidBrush32(cr);
3321                 if (hBrush)
3322                 {
3323                   hBrush = SelectObject32 (lpdi->hDC, hBrush) ;
3324                   rect.right=rect.left+25;
3325                   rect.top++;
3326                   rect.left+=5;
3327                   rect.bottom--;
3328                   Rectangle32( lpdi->hDC, rect.left, rect.top,
3329                                rect.right, rect.bottom );
3330                   DeleteObject32( SelectObject32 (lpdi->hDC, hBrush)) ;
3331                 }
3332                 rect=lpdi->rcItem;
3333                 rect.left+=25+5;
3334                 break;
3335
3336     default:    return TRUE;    /* this should never happen */
3337    }
3338    if (lpdi->itemState == ODS_SELECTED)
3339    {
3340      SetTextColor32(lpdi->hDC, oldText);
3341      SetBkColor32(lpdi->hDC, oldBk);
3342    }
3343  }
3344  return TRUE;
3345 }
3346
3347 /***********************************************************************
3348  *           CFn_WMCtlColor                              [internal]
3349  */
3350 LRESULT CFn_WMCtlColorStatic(HWND32 hDlg, WPARAM32 wParam, LPARAM lParam,
3351                              LPCHOOSEFONT32A lpcf)
3352 {
3353   if (lpcf->Flags & CF_EFFECTS)
3354    if (GetDlgCtrlID32(lParam)==stc6)
3355    {
3356      SetTextColor32((HDC32)wParam, lpcf->rgbColors);
3357      return GetStockObject32(WHITE_BRUSH);
3358    }
3359   return 0;
3360 }
3361
3362 /***********************************************************************
3363  *           CFn_WMCommand                               [internal]
3364  */
3365 LRESULT CFn_WMCommand(HWND32 hDlg, WPARAM32 wParam, LPARAM lParam,
3366                       LPCHOOSEFONT32A lpcf)
3367 {
3368   HFONT32 hFont;
3369   int i,j;
3370   long l;
3371   HDC32 hdc;
3372   LPLOGFONT32A lpxx=lpcf->lpLogFont;
3373   
3374   TRACE(commdlg,"WM_COMMAND wParam=%08lX lParam=%08lX\n", (LONG)wParam, lParam);
3375   switch (LOWORD(wParam))
3376   {
3377         case cmb1:if (HIWORD(wParam)==CBN_SELCHANGE)
3378                   {
3379                     hdc=(lpcf->Flags & CF_PRINTERFONTS && lpcf->hDC) ? lpcf->hDC : GetDC32(hDlg);
3380                     if (hdc)
3381                     {
3382                       SendDlgItemMessage32A(hDlg, cmb2, CB_RESETCONTENT16, 0, 0); 
3383                       SendDlgItemMessage32A(hDlg, cmb3, CB_RESETCONTENT16, 0, 0);
3384                       i=SendDlgItemMessage32A(hDlg, cmb1, CB_GETCURSEL16, 0, 0);
3385                       if (i!=CB_ERR)
3386                       {
3387                         HCURSOR32 hcursor=SetCursor32(LoadCursor32A(0,IDC_WAIT32A));
3388                         CFn_ENUMSTRUCT s;
3389                         char str[256];
3390                         SendDlgItemMessage32A(hDlg, cmb1, CB_GETLBTEXT32, i,
3391                                               (LPARAM)str);
3392                         TRACE(commdlg,"WM_COMMAND/cmb1 =>%s\n",str);
3393                         s.hWnd1=GetDlgItem32(hDlg, cmb2);
3394                         s.hWnd2=GetDlgItem32(hDlg, cmb3);
3395                         s.lpcf32a=lpcf;
3396                         EnumFontFamilies32A(hdc, str, FontStyleEnumProc32, (LPARAM)&s);
3397                         SetCursor32(hcursor);
3398                       }
3399                       if (!(lpcf->Flags & CF_PRINTERFONTS && lpcf->hDC))
3400                         ReleaseDC32(hDlg,hdc);
3401                     }
3402                     else
3403                     {
3404                       WARN(commdlg,"HDC failure !!!\n");
3405                       EndDialog32 (hDlg, 0); 
3406                       return TRUE;
3407                     }
3408                   }
3409         case chx1:
3410         case chx2:
3411         case cmb2:
3412         case cmb3:if (HIWORD(wParam)==CBN_SELCHANGE || HIWORD(wParam)== BN_CLICKED )
3413                   {
3414                     char str[256];
3415                     TRACE(commdlg,"WM_COMMAND/cmb2,3 =%08lX\n", lParam);
3416                     i=SendDlgItemMessage32A(hDlg,cmb1,CB_GETCURSEL32,0,0);
3417                     if (i==CB_ERR)
3418                       i=GetDlgItemText32A( hDlg, cmb1, str, 256 );
3419                     else
3420                     {
3421                       SendDlgItemMessage32A(hDlg,cmb1,CB_GETLBTEXT32,i,
3422                                             (LPARAM)str);
3423                       l=SendDlgItemMessage32A(hDlg,cmb1,CB_GETITEMDATA32,i,0);
3424                       j=HIWORD(l);
3425                       lpcf->nFontType = LOWORD(l);
3426                       /* FIXME:   lpcf->nFontType |= ....  SIMULATED_FONTTYPE and so */
3427                       /* same value reported to the EnumFonts
3428                        call back with the extra FONTTYPE_...  bits added */
3429                       lpxx->lfPitchAndFamily=j&0xff;
3430                       lpxx->lfCharSet=j>>8;
3431                     }
3432                     strcpy(lpxx->lfFaceName,str);
3433                     i=SendDlgItemMessage32A(hDlg, cmb2, CB_GETCURSEL32, 0, 0);
3434                     if (i!=CB_ERR)
3435                     {
3436                       l=SendDlgItemMessage32A(hDlg, cmb2, CB_GETITEMDATA32, i, 0);
3437                       if (0!=(lpxx->lfItalic=HIWORD(l)))
3438                         lpcf->nFontType |= ITALIC_FONTTYPE;
3439                       if ((lpxx->lfWeight=LOWORD(l)) > FW_MEDIUM)
3440                         lpcf->nFontType |= BOLD_FONTTYPE;
3441                     }
3442                     i=SendDlgItemMessage32A(hDlg, cmb3, CB_GETCURSEL32, 0, 0);
3443                     if (i!=CB_ERR)
3444                       lpxx->lfHeight=-LOWORD(SendDlgItemMessage32A(hDlg, cmb3, CB_GETITEMDATA32, i, 0));
3445                     else
3446                       lpxx->lfHeight=0;
3447                     lpxx->lfStrikeOut=IsDlgButtonChecked32(hDlg,chx1);
3448                     lpxx->lfUnderline=IsDlgButtonChecked32(hDlg,chx2);
3449                     lpxx->lfWidth=lpxx->lfOrientation=lpxx->lfEscapement=0;
3450                     lpxx->lfOutPrecision=OUT_DEFAULT_PRECIS;
3451                     lpxx->lfClipPrecision=CLIP_DEFAULT_PRECIS;
3452                     lpxx->lfQuality=DEFAULT_QUALITY;
3453                     lpcf->iPointSize= -10*lpxx->lfHeight;
3454
3455                     hFont=CreateFontIndirect32A(lpxx);
3456                     if (hFont)
3457                     {
3458                       HFONT32 oldFont=SendDlgItemMessage32A(hDlg, stc6, 
3459                           WM_GETFONT, 0, 0);
3460                       SendDlgItemMessage32A(hDlg,stc6,WM_SETFONT,hFont,TRUE);
3461                       DeleteObject32(oldFont);
3462                     }
3463                   }
3464                   break;
3465
3466         case cmb4:i=SendDlgItemMessage32A(hDlg, cmb4, CB_GETCURSEL32, 0, 0);
3467                   if (i!=CB_ERR)
3468                   {
3469                    lpcf->rgbColors=textcolors[i];
3470                    InvalidateRect32( GetDlgItem32(hDlg,stc6), NULL, 0 );
3471                   }
3472                   break;
3473         
3474         case psh15:i=RegisterWindowMessage32A( HELPMSGSTRING );
3475                   if (lpcf->hwndOwner)
3476                     SendMessage32A(lpcf->hwndOwner, i, 0, (LPARAM)GetWindowLong32A(hDlg, DWL_USER));
3477 /*                if (CFn_HookCallChk(lpcf))
3478                     CallWindowProc16(lpcf->lpfnHook,hDlg,WM_COMMAND,psh15,(LPARAM)lpcf);*/
3479                   break;
3480
3481         case IDOK:if (  (!(lpcf->Flags & CF_LIMITSIZE))  ||
3482                      ( (lpcf->Flags & CF_LIMITSIZE) && 
3483                       (-lpxx->lfHeight >= lpcf->nSizeMin) && 
3484                       (-lpxx->lfHeight <= lpcf->nSizeMax)))
3485                      EndDialog32(hDlg, TRUE);
3486                   else
3487                   {
3488                    char buffer[80];
3489                    sprintf(buffer,"Select a font size between %d and %d points.",
3490                            lpcf->nSizeMin,lpcf->nSizeMax);
3491                    MessageBox32A(hDlg, buffer, NULL, MB_OK);
3492                   } 
3493                   return(TRUE);
3494         case IDCANCEL:EndDialog32(hDlg, FALSE);
3495                   return(TRUE);
3496         }
3497       return(FALSE);
3498 }
3499
3500 static LRESULT CFn_WMDestroy(HWND32 hwnd, WPARAM32 wParam, LPARAM lParam)
3501 {
3502   DeleteObject32(SendDlgItemMessage32A(hwnd, stc6, WM_GETFONT, 0, 0));
3503   return TRUE;
3504 }
3505
3506
3507 /***********************************************************************
3508  *           FormatCharDlgProc16   (COMMDLG.16)
3509              FIXME: 1. some strings are "hardcoded", but it's better load from sysres
3510                     2. some CF_.. flags are not supported
3511                     3. some TType extensions
3512  */
3513 LRESULT WINAPI FormatCharDlgProc16(HWND16 hDlg, UINT16 message, WPARAM16 wParam,
3514                                    LPARAM lParam)
3515 {
3516   LPCHOOSEFONT16 lpcf;
3517   LPCHOOSEFONT32A lpcf32a;
3518   UINT32 uMsg32;
3519   WPARAM32 wParam32;
3520   LRESULT res=0;  
3521   if (message!=WM_INITDIALOG)
3522   {
3523    lpcf=(LPCHOOSEFONT16)GetWindowLong32A(hDlg, DWL_USER);   
3524    if (!lpcf)
3525       return FALSE;
3526    if (CFn_HookCallChk(lpcf))
3527      res=CallWindowProc16(lpcf->lpfnHook,hDlg,message,wParam,lParam);
3528    if (res)
3529     return res;
3530   }
3531   else
3532   {
3533     lpcf=(LPCHOOSEFONT16)lParam;
3534     lpcf32a=(LPCHOOSEFONT32A)lpcf->lpTemplateName;
3535     if (!CFn_WMInitDialog(hDlg, wParam, lParam, lpcf32a)) 
3536     {
3537       TRACE(commdlg, "CFn_WMInitDialog returned FALSE\n");
3538       return FALSE;
3539     }  
3540     if (CFn_HookCallChk(lpcf))
3541       return CallWindowProc16(lpcf->lpfnHook,hDlg,WM_INITDIALOG,wParam,lParam);
3542   }
3543   WINPROC_MapMsg16To32A(message, wParam, &uMsg32, &wParam32, &lParam);
3544   lpcf32a=(LPCHOOSEFONT32A)lpcf->lpTemplateName;
3545   switch (uMsg32)
3546     {
3547       case WM_MEASUREITEM:
3548                         res=CFn_WMMeasureItem(hDlg, wParam32, lParam);
3549                         break;
3550       case WM_DRAWITEM:
3551                         res=CFn_WMDrawItem(hDlg, wParam32, lParam);
3552                         break;
3553       case WM_CTLCOLORSTATIC:
3554                         res=CFn_WMCtlColorStatic(hDlg, wParam32, lParam, lpcf32a);
3555                         break;
3556       case WM_COMMAND:
3557                         res=CFn_WMCommand(hDlg, wParam32, lParam, lpcf32a);
3558                         break;
3559       case WM_DESTROY:
3560                         res=CFn_WMDestroy(hDlg, wParam32, lParam);
3561                         break;
3562       case WM_CHOOSEFONT_GETLOGFONT: 
3563                          TRACE(commdlg,"WM_CHOOSEFONT_GETLOGFONT lParam=%08lX\n",
3564                                       lParam);
3565                          FIXME(commdlg, "current logfont back to caller\n");
3566                         break;
3567     }
3568   WINPROC_UnmapMsg16To32A(uMsg32, wParam32, lParam, res);    
3569   return res;
3570 }
3571
3572 /***********************************************************************
3573  *           FormatCharDlgProc32A   [internal]
3574  */
3575 LRESULT WINAPI FormatCharDlgProc32A(HWND32 hDlg, UINT32 uMsg, WPARAM32 wParam,
3576                                     LPARAM lParam)
3577 {
3578   LPCHOOSEFONT32A lpcf;
3579   LRESULT res=FALSE;
3580   if (uMsg!=WM_INITDIALOG)
3581   {
3582    lpcf=(LPCHOOSEFONT32A)GetWindowLong32A(hDlg, DWL_USER);   
3583    if (!lpcf)
3584      return FALSE;
3585    if (CFn_HookCallChk32(lpcf))
3586      res=CallWindowProc32A(lpcf->lpfnHook, hDlg, uMsg, wParam, lParam);
3587    if (res)
3588      return res;
3589   }
3590   else
3591   {
3592     lpcf=(LPCHOOSEFONT32A)lParam;
3593     if (!CFn_WMInitDialog(hDlg, wParam, lParam, lpcf)) 
3594     {
3595       TRACE(commdlg, "CFn_WMInitDialog returned FALSE\n");
3596       return FALSE;
3597     }  
3598     if (CFn_HookCallChk32(lpcf))
3599       return CallWindowProc32A(lpcf->lpfnHook,hDlg,WM_INITDIALOG,wParam,lParam);
3600   }
3601   switch (uMsg)
3602     {
3603       case WM_MEASUREITEM:
3604                         return CFn_WMMeasureItem(hDlg, wParam, lParam);
3605       case WM_DRAWITEM:
3606                         return CFn_WMDrawItem(hDlg, wParam, lParam);
3607       case WM_CTLCOLORSTATIC:
3608                         return CFn_WMCtlColorStatic(hDlg, wParam, lParam, lpcf);
3609       case WM_COMMAND:
3610                         return CFn_WMCommand(hDlg, wParam, lParam, lpcf);
3611       case WM_DESTROY:
3612                         return CFn_WMDestroy(hDlg, wParam, lParam);
3613       case WM_CHOOSEFONT_GETLOGFONT:
3614                          TRACE(commdlg,"WM_CHOOSEFONT_GETLOGFONT lParam=%08lX\n",
3615                                       lParam);
3616                          FIXME(commdlg, "current logfont back to caller\n");
3617                         break;
3618     }
3619   return res;
3620 }
3621
3622 /***********************************************************************
3623  *           FormatCharDlgProc32W   [internal]
3624  */
3625 LRESULT WINAPI FormatCharDlgProc32W(HWND32 hDlg, UINT32 uMsg, WPARAM32 wParam,
3626                                     LPARAM lParam)
3627 {
3628   LPCHOOSEFONT32W lpcf32w;
3629   LPCHOOSEFONT32A lpcf32a;
3630   LRESULT res=FALSE;
3631   if (uMsg!=WM_INITDIALOG)
3632   {
3633    lpcf32w=(LPCHOOSEFONT32W)GetWindowLong32A(hDlg, DWL_USER);   
3634    if (!lpcf32w)
3635      return FALSE;
3636    if (CFn_HookCallChk32((LPCHOOSEFONT32A)lpcf32w))
3637      res=CallWindowProc32W(lpcf32w->lpfnHook, hDlg, uMsg, wParam, lParam);
3638    if (res)
3639      return res;
3640   }
3641   else
3642   {
3643     lpcf32w=(LPCHOOSEFONT32W)lParam;
3644     lpcf32a=(LPCHOOSEFONT32A)lpcf32w->lpTemplateName;
3645     if (!CFn_WMInitDialog(hDlg, wParam, lParam, lpcf32a)) 
3646     {
3647       TRACE(commdlg, "CFn_WMInitDialog returned FALSE\n");
3648       return FALSE;
3649     }  
3650     if (CFn_HookCallChk32((LPCHOOSEFONT32A)lpcf32w))
3651       return CallWindowProc32W(lpcf32w->lpfnHook,hDlg,WM_INITDIALOG,wParam,lParam);
3652   }
3653   lpcf32a=(LPCHOOSEFONT32A)lpcf32w->lpTemplateName;
3654   switch (uMsg)
3655     {
3656       case WM_MEASUREITEM:
3657                         return CFn_WMMeasureItem(hDlg, wParam, lParam);
3658       case WM_DRAWITEM:
3659                         return CFn_WMDrawItem(hDlg, wParam, lParam);
3660       case WM_CTLCOLORSTATIC:
3661                         return CFn_WMCtlColorStatic(hDlg, wParam, lParam, lpcf32a);
3662       case WM_COMMAND:
3663                         return CFn_WMCommand(hDlg, wParam, lParam, lpcf32a);
3664       case WM_DESTROY:
3665                         return CFn_WMDestroy(hDlg, wParam, lParam);
3666       case WM_CHOOSEFONT_GETLOGFONT: 
3667                          TRACE(commdlg,"WM_CHOOSEFONT_GETLOGFONT lParam=%08lX\n",
3668                                       lParam);
3669                          FIXME(commdlg, "current logfont back to caller\n");
3670                         break;
3671     }
3672   return res;
3673 }
3674
3675
3676 static BOOL32 Commdlg_GetFileName32A( BOOL16 (CALLBACK *dofunction)(SEGPTR x),
3677                                       LPOPENFILENAME32A ofn )
3678 {
3679         BOOL16 ret;
3680         LPOPENFILENAME16 ofn16 = SEGPTR_ALLOC(sizeof(OPENFILENAME16));
3681
3682         memset(ofn16,'\0',sizeof(*ofn16));
3683         ofn16->lStructSize = sizeof(*ofn16);
3684         ofn16->hwndOwner = ofn->hwndOwner;
3685         ofn16->hInstance = MapHModuleLS(ofn->hInstance);
3686         if (ofn->lpstrFilter) {
3687                 LPSTR   s,x;
3688
3689                 /* filter is a list...  title\0ext\0......\0\0 */
3690                 s = (LPSTR)ofn->lpstrFilter;
3691                 while (*s)
3692                         s = s+strlen(s)+1;
3693                 s++;
3694                 x = (LPSTR)SEGPTR_ALLOC(s-ofn->lpstrFilter);
3695                 memcpy(x,ofn->lpstrFilter,s-ofn->lpstrFilter);
3696                 ofn16->lpstrFilter = SEGPTR_GET(x);
3697         }
3698         if (ofn->lpstrCustomFilter) {
3699                 LPSTR   s,x;
3700
3701                 /* filter is a list...  title\0ext\0......\0\0 */
3702                 s = (LPSTR)ofn->lpstrCustomFilter;
3703                 while (*s)
3704                         s = s+strlen(s)+1;
3705                 s++;
3706                 x = SEGPTR_ALLOC(s-ofn->lpstrCustomFilter);
3707                 memcpy(x,ofn->lpstrCustomFilter,s-ofn->lpstrCustomFilter);
3708                 ofn16->lpstrCustomFilter = SEGPTR_GET(x);
3709         }
3710         ofn16->nMaxCustFilter = ofn->nMaxCustFilter;
3711         ofn16->nFilterIndex = ofn->nFilterIndex;
3712         if (ofn->nMaxFile)
3713             ofn16->lpstrFile = SEGPTR_GET(SEGPTR_ALLOC(ofn->nMaxFile));
3714         ofn16->nMaxFile = ofn->nMaxFile;
3715         ofn16->nMaxFileTitle = ofn->nMaxFileTitle;
3716         if (ofn16->nMaxFileTitle)
3717             ofn16->lpstrFileTitle = SEGPTR_GET(SEGPTR_ALLOC(ofn->nMaxFileTitle));
3718         if (ofn->lpstrInitialDir)
3719             ofn16->lpstrInitialDir = SEGPTR_GET(SEGPTR_STRDUP(ofn->lpstrInitialDir));
3720         if (ofn->lpstrTitle)
3721             ofn16->lpstrTitle = SEGPTR_GET(SEGPTR_STRDUP(ofn->lpstrTitle));
3722         ofn16->Flags = ofn->Flags|OFN_WINE32;
3723         ofn16->nFileOffset = ofn->nFileOffset;
3724         ofn16->nFileExtension = ofn->nFileExtension;
3725         if (ofn->lpstrDefExt)
3726             ofn16->lpstrDefExt = SEGPTR_GET(SEGPTR_STRDUP(ofn->lpstrDefExt));
3727         ofn16->lCustData = ofn->lCustData;
3728         ofn16->lpfnHook = (WNDPROC16)ofn->lpfnHook;
3729
3730         if (ofn->lpTemplateName)
3731             ofn16->lpTemplateName = SEGPTR_GET(SEGPTR_STRDUP(ofn->lpTemplateName));
3732
3733         ret = dofunction(SEGPTR_GET(ofn16));
3734
3735         ofn->nFileOffset = ofn16->nFileOffset;
3736         ofn->nFileExtension = ofn16->nFileExtension;
3737         if (ofn16->lpstrFilter)
3738             SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpstrFilter));
3739         if (ofn16->lpTemplateName)
3740             SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpTemplateName));
3741         if (ofn16->lpstrDefExt)
3742             SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpstrDefExt));
3743         if (ofn16->lpstrTitle)
3744             SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpstrTitle));
3745         if (ofn16->lpstrInitialDir)
3746             SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpstrInitialDir));
3747         if (ofn16->lpstrCustomFilter)
3748             SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpstrCustomFilter));
3749
3750         if (ofn16->lpstrFile) 
3751           {
3752             strcpy(ofn->lpstrFile,PTR_SEG_TO_LIN(ofn16->lpstrFile));
3753             SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpstrFile));
3754           }
3755
3756         if (ofn16->lpstrFileTitle) 
3757           {
3758             strcpy(ofn->lpstrFileTitle,PTR_SEG_TO_LIN(ofn16->lpstrFileTitle));
3759             SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpstrFileTitle));
3760           }
3761         SEGPTR_FREE(ofn16);
3762         return ret;
3763 }
3764
3765 static BOOL32 Commdlg_GetFileName32W( BOOL16 (CALLBACK *dofunction)(SEGPTR x), 
3766                                       LPOPENFILENAME32W ofn )
3767 {
3768         BOOL16 ret;
3769         LPOPENFILENAME16 ofn16 = SEGPTR_ALLOC(sizeof(OPENFILENAME16));
3770
3771         memset(ofn16,'\0',sizeof(*ofn16));
3772         ofn16->lStructSize = sizeof(*ofn16);
3773         ofn16->hwndOwner = ofn->hwndOwner;
3774         ofn16->hInstance = MapHModuleLS(ofn->hInstance);
3775         if (ofn->lpstrFilter) {
3776                 LPWSTR  s;
3777                 LPSTR   x,y;
3778                 int     n;
3779
3780                 /* filter is a list...  title\0ext\0......\0\0 */
3781                 s = (LPWSTR)ofn->lpstrFilter;
3782                 while (*s)
3783                         s = s+lstrlen32W(s)+1;
3784                 s++;
3785                 n = s - ofn->lpstrFilter; /* already divides by 2. ptr magic */
3786                 x = y = (LPSTR)SEGPTR_ALLOC(n);
3787                 s = (LPWSTR)ofn->lpstrFilter;
3788                 while (*s) {
3789                         lstrcpyWtoA(x,s);
3790                         x+=lstrlen32A(x)+1;
3791                         s+=lstrlen32W(s)+1;
3792                 }
3793                 *x=0;
3794                 ofn16->lpstrFilter = SEGPTR_GET(y);
3795 }
3796         if (ofn->lpstrCustomFilter) {
3797                 LPWSTR  s;
3798                 LPSTR   x,y;
3799                 int     n;
3800
3801                 /* filter is a list...  title\0ext\0......\0\0 */
3802                 s = (LPWSTR)ofn->lpstrCustomFilter;
3803                 while (*s)
3804                         s = s+lstrlen32W(s)+1;
3805                 s++;
3806                 n = s - ofn->lpstrCustomFilter;
3807                 x = y = (LPSTR)SEGPTR_ALLOC(n);
3808                 s = (LPWSTR)ofn->lpstrCustomFilter;
3809                 while (*s) {
3810                         lstrcpyWtoA(x,s);
3811                         x+=lstrlen32A(x)+1;
3812                         s+=lstrlen32W(s)+1;
3813                 }
3814                 *x=0;
3815                 ofn16->lpstrCustomFilter = SEGPTR_GET(y);
3816         }
3817         ofn16->nMaxCustFilter = ofn->nMaxCustFilter;
3818         ofn16->nFilterIndex = ofn->nFilterIndex;
3819         if (ofn->nMaxFile) 
3820            ofn16->lpstrFile = SEGPTR_GET(SEGPTR_ALLOC(ofn->nMaxFile));
3821         ofn16->nMaxFile = ofn->nMaxFile;
3822         ofn16->nMaxFileTitle = ofn->nMaxFileTitle;
3823         if (ofn->nMaxFileTitle)
3824                 ofn16->lpstrFileTitle = SEGPTR_GET(SEGPTR_ALLOC(ofn->nMaxFileTitle));
3825         if (ofn->lpstrInitialDir)
3826                 ofn16->lpstrInitialDir = SEGPTR_GET(SEGPTR_STRDUP_WtoA(ofn->lpstrInitialDir));
3827         if (ofn->lpstrTitle)
3828                 ofn16->lpstrTitle = SEGPTR_GET(SEGPTR_STRDUP_WtoA(ofn->lpstrTitle));
3829         ofn16->Flags = ofn->Flags|OFN_WINE32|OFN_UNICODE;
3830         ofn16->nFileOffset = ofn->nFileOffset;
3831         ofn16->nFileExtension = ofn->nFileExtension;
3832         if (ofn->lpstrDefExt)
3833                 ofn16->lpstrDefExt = SEGPTR_GET(SEGPTR_STRDUP_WtoA(ofn->lpstrDefExt));
3834         ofn16->lCustData = ofn->lCustData;
3835         ofn16->lpfnHook = (WNDPROC16)ofn->lpfnHook;
3836         if (ofn->lpTemplateName) 
3837                 ofn16->lpTemplateName = SEGPTR_GET(SEGPTR_STRDUP_WtoA(ofn->lpTemplateName));
3838         ret = dofunction(SEGPTR_GET(ofn16));
3839
3840         ofn->nFileOffset = ofn16->nFileOffset;
3841         ofn->nFileExtension = ofn16->nFileExtension;
3842         if (ofn16->lpstrFilter)
3843                 SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpstrFilter));
3844         if (ofn16->lpTemplateName)
3845                 SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpTemplateName));
3846         if (ofn16->lpstrDefExt)
3847                 SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpstrDefExt));
3848         if (ofn16->lpstrTitle)
3849                 SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpstrTitle));
3850         if (ofn16->lpstrInitialDir)
3851                 SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpstrInitialDir));
3852         if (ofn16->lpstrCustomFilter)
3853                 SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpstrCustomFilter));
3854
3855         if (ofn16->lpstrFile) {
3856         lstrcpyAtoW(ofn->lpstrFile,PTR_SEG_TO_LIN(ofn16->lpstrFile));
3857         SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpstrFile));
3858         }
3859
3860         if (ofn16->lpstrFileTitle) {
3861                 lstrcpyAtoW(ofn->lpstrFileTitle,PTR_SEG_TO_LIN(ofn16->lpstrFileTitle));
3862                 SEGPTR_FREE(PTR_SEG_TO_LIN(ofn16->lpstrFileTitle));
3863         }
3864         SEGPTR_FREE(ofn16);
3865         return ret;
3866 }
3867
3868 /***********************************************************************
3869  *            GetOpenFileName32A  (COMDLG32.10)
3870  */
3871 BOOL32 WINAPI GetOpenFileName32A( LPOPENFILENAME32A ofn )
3872 {
3873    BOOL16 (CALLBACK * dofunction)(SEGPTR ofn16) = GetOpenFileName16;
3874    return Commdlg_GetFileName32A(dofunction,ofn);
3875 }
3876
3877 /***********************************************************************
3878  *            GetOpenFileName32W (COMDLG32.11)
3879  */
3880 BOOL32 WINAPI GetOpenFileName32W( LPOPENFILENAME32W ofn )
3881 {
3882    BOOL16 (CALLBACK * dofunction)(SEGPTR ofn16) = GetOpenFileName16;
3883    return Commdlg_GetFileName32W(dofunction,ofn);
3884 }
3885
3886 /***********************************************************************
3887  *            GetSaveFileName32A  (COMDLG32.12)
3888  */
3889 BOOL32 WINAPI GetSaveFileName32A( LPOPENFILENAME32A ofn )
3890 {
3891    BOOL16 (CALLBACK * dofunction)(SEGPTR ofn16) = GetSaveFileName16;
3892    return Commdlg_GetFileName32A(dofunction,ofn);
3893 }
3894
3895 /***********************************************************************
3896  *            GetSaveFileName32W  (COMDLG32.13)
3897  */
3898 BOOL32 WINAPI GetSaveFileName32W( LPOPENFILENAME32W ofn )
3899 {
3900    BOOL16 (CALLBACK * dofunction)(SEGPTR ofn16) = GetSaveFileName16;
3901    return Commdlg_GetFileName32W(dofunction,ofn);
3902 }
3903
3904 /***********************************************************************
3905  *            ChooseColorA  (COMDLG32.1)
3906  */
3907 BOOL32 WINAPI ChooseColor32A(LPCHOOSECOLOR32A lpChCol )
3908
3909 {
3910   BOOL16 ret;
3911   char *str = NULL;
3912   COLORREF* ccref=SEGPTR_ALLOC(64);
3913   LPCHOOSECOLOR16 lpcc16=SEGPTR_ALLOC(sizeof(CHOOSECOLOR16));
3914
3915   memset(lpcc16,'\0',sizeof(*lpcc16));
3916   lpcc16->lStructSize=sizeof(*lpcc16);
3917   lpcc16->hwndOwner=lpChCol->hwndOwner;
3918   lpcc16->hInstance=MapHModuleLS(lpChCol->hInstance);
3919   lpcc16->rgbResult=lpChCol->rgbResult;
3920   memcpy(ccref,lpChCol->lpCustColors,64);
3921   lpcc16->lpCustColors=(COLORREF*)SEGPTR_GET(ccref);
3922   lpcc16->Flags=lpChCol->Flags;
3923   lpcc16->lCustData=lpChCol->lCustData;
3924   lpcc16->lpfnHook=(WNDPROC16)lpChCol->lpfnHook;
3925   if (lpChCol->lpTemplateName)
3926     str = SEGPTR_STRDUP(lpChCol->lpTemplateName );
3927   lpcc16->lpTemplateName=SEGPTR_GET(str);
3928   
3929   ret = ChooseColor16(lpcc16);
3930   if(str)
3931     SEGPTR_FREE(str);
3932   memcpy(lpChCol->lpCustColors,ccref,64);
3933   SEGPTR_FREE(ccref);
3934   SEGPTR_FREE(lpcc16);
3935   return (BOOL32)ret;
3936 }
3937
3938 /***********************************************************************
3939  *            ChooseColorW  (COMDLG32.2)
3940  */
3941 BOOL32 WINAPI ChooseColor32W(LPCHOOSECOLOR32W lpChCol )
3942
3943 {
3944   BOOL16 ret;
3945   char *str = NULL;
3946   COLORREF* ccref=SEGPTR_ALLOC(64);
3947   LPCHOOSECOLOR16 lpcc16=SEGPTR_ALLOC(sizeof(CHOOSECOLOR16));
3948
3949   memset(lpcc16,'\0',sizeof(*lpcc16));
3950   lpcc16->lStructSize=sizeof(*lpcc16);
3951   lpcc16->hwndOwner=lpChCol->hwndOwner;
3952   lpcc16->hInstance=MapHModuleLS(lpChCol->hInstance);
3953   lpcc16->rgbResult=lpChCol->rgbResult;
3954   memcpy(ccref,lpChCol->lpCustColors,64);
3955   lpcc16->lpCustColors=(COLORREF*)SEGPTR_GET(ccref);
3956   lpcc16->Flags=lpChCol->Flags;
3957   lpcc16->lCustData=lpChCol->lCustData;
3958   lpcc16->lpfnHook=(WNDPROC16)lpChCol->lpfnHook;
3959   if (lpChCol->lpTemplateName)
3960     str = SEGPTR_STRDUP_WtoA(lpChCol->lpTemplateName );
3961   lpcc16->lpTemplateName=SEGPTR_GET(str);
3962   
3963   ret = ChooseColor16(lpcc16);
3964   if(str)
3965     SEGPTR_FREE(str);
3966   memcpy(lpChCol->lpCustColors,ccref,64);
3967   SEGPTR_FREE(ccref);
3968   SEGPTR_FREE(lpcc16);
3969   return (BOOL32)ret;
3970 }
3971
3972 /***********************************************************************
3973  *            PageSetupDlgA  (COMDLG32.15)
3974  */
3975 BOOL32 WINAPI PageSetupDlg32A(LPPAGESETUPDLG32A setupdlg) {
3976         FIXME(commdlg,"(%p), stub!\n",setupdlg);
3977         return FALSE;
3978 }