4 * Copyright 1993 Alexandre Julliard
7 static char Copyright
[] = "Copyright Alexandre Julliard, 1993";
18 /* Dialog base units */
19 static WORD xBaseUnit
= 0, yBaseUnit
= 0;
22 /***********************************************************************
25 * Initialisation of the dialog manager.
32 /* Calculate the dialog base units */
34 if (!(hdc
= GetDC(GetDesktopWindow()))) return FALSE
;
35 GetTextMetrics( hdc
, &tm
);
37 xBaseUnit
= tm
.tmAveCharWidth
;
38 yBaseUnit
= tm
.tmHeight
;
40 printf( "DIALOG_Init: base units = %d,%d\n", xBaseUnit
, yBaseUnit
);
46 /***********************************************************************
49 * Return the class and text of the control pointed to by ptr,
50 * and return a pointer to the next control.
52 static DLGCONTROLHEADER
* DIALOG_GetControl( DLGCONTROLHEADER
* ptr
,
53 char ** class, char ** text
)
55 unsigned char * p
= (unsigned char *)ptr
;
56 p
+= 14; /* size of control header */
61 case 0x80: *class = "BUTTON"; break;
62 case 0x81: *class = "EDIT"; break;
63 case 0x82: *class = "STATIC"; break;
64 case 0x83: *class = "LISTBOX"; break;
65 case 0x84: *class = "SCROLLBAR"; break;
66 case 0x85: *class = "COMBOBOX"; break;
67 default: *class = ""; break;
77 return (DLGCONTROLHEADER
*)p
;
81 /***********************************************************************
82 * DIALOG_ParseTemplate
84 * Fill a DLGTEMPLATE structure from the dialog template, and return
85 * a pointer to the first control.
87 static DLGCONTROLHEADER
* DIALOG_ParseTemplate( LPCSTR
template,
88 DLGTEMPLATE
* result
)
90 unsigned char * p
= (unsigned char *)template;
92 result
->header
= (DLGTEMPLATEHEADER
*)p
;
96 if (*p
== 0xff) p
+= 3;
97 else p
+= strlen(p
) + 1;
99 if (*p
) result
->className
= p
;
100 else result
->className
= DIALOG_CLASS_NAME
;
106 if (result
->header
->style
& DS_SETFONT
)
108 result
->pointSize
= *(WORD
*)p
; p
+= sizeof(WORD
);
109 result
->faceName
= p
; p
+= strlen(p
) + 1;
112 return (DLGCONTROLHEADER
*)p
;
116 /***********************************************************************
117 * DIALOG_DisplayTemplate
120 static void DIALOG_DisplayTemplate( DLGTEMPLATE
* result
)
122 printf( "DIALOG %d, %d, %d, %d\n", result
->header
->x
, result
->header
->y
,
123 result
->header
->cx
, result
->header
->cy
);
124 printf( " STYLE %08x\n", result
->header
->style
);
125 printf( " CAPTION '%s'\n", result
->caption
);
126 printf( " CLASS '%s'\n", result
->className
);
127 if (result
->menuName
[0] == 0xff)
128 printf( " MENU %d\n", result
->menuName
[1] + 256*result
->menuName
[2] );
129 else printf( " MENU '%s'\n", result
->menuName
);
130 if (result
->header
->style
& DS_SETFONT
)
131 printf( " FONT %d,'%s'\n", result
->pointSize
, result
->faceName
);
133 #endif /* DEBUG_DIALOG */
136 /***********************************************************************
137 * CreateDialog (USER.89)
139 HWND
CreateDialog( HINSTANCE hInst
, LPCSTR dlgTemplate
,
140 HWND owner
, FARPROC dlgProc
)
142 return CreateDialogParam( hInst
, dlgTemplate
, owner
, dlgProc
, 0 );
146 /***********************************************************************
147 * CreateDialogParam (USER.241)
149 HWND
CreateDialogParam( HINSTANCE hInst
, LPCSTR dlgTemplate
,
150 HWND owner
, FARPROC dlgProc
, LPARAM param
)
157 printf( "CreateDialogParam: %d,'%s',%d,%p,%d\n",
158 hInst
, dlgTemplate
, owner
, dlgProc
, param
);
161 /* FIXME: MAKEINTRESOURCE should be replaced by RT_DIALOG */
162 if (!(hres
= FindResource( hInst
, dlgTemplate
, MAKEINTRESOURCE(0x8005) )))
164 if (!(hmem
= LoadResource( hInst
, hres
))) return 0;
165 if (!(data
= LockResource( hmem
))) hwnd
= 0;
166 else hwnd
= CreateDialogIndirectParam(hInst
, data
, owner
, dlgProc
, param
);
167 FreeResource( hmem
);
172 /***********************************************************************
173 * CreateDialogIndirect (USER.219)
175 HWND
CreateDialogIndirect( HINSTANCE hInst
, LPCSTR dlgTemplate
,
176 HWND owner
, FARPROC dlgProc
)
178 return CreateDialogIndirectParam( hInst
, dlgTemplate
, owner
, dlgProc
, 0 );
182 /***********************************************************************
183 * CreateDialogIndirectParam (USER.242)
185 HWND
CreateDialogIndirectParam( HINSTANCE hInst
, LPCSTR dlgTemplate
,
186 HWND owner
, FARPROC dlgProc
, LPARAM param
)
194 DLGTEMPLATE
template;
195 DLGCONTROLHEADER
* header
;
196 DIALOGINFO
* dlgInfo
;
198 WORD xUnit
= xBaseUnit
;
199 WORD yUnit
= yBaseUnit
;
201 /* Parse dialog template */
203 if (!dlgTemplate
) return 0;
204 header
= DIALOG_ParseTemplate( dlgTemplate
, &template );
206 DIALOG_DisplayTemplate( &template );
211 switch (template.menuName
[0])
217 hMenu
= LoadMenu( hInst
, MAKEINTRESOURCE( template.menuName
[1] +
218 256*template.menuName
[2] ));
221 hMenu
= LoadMenu( hInst
, template.menuName
);
225 /* Create custom font if needed */
227 if (template.header
->style
& DS_SETFONT
)
229 hFont
= CreateFont( template.pointSize
, 0, 0, 0, FW_DONTCARE
,
230 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
, 0, 0,
231 DEFAULT_QUALITY
, FF_DONTCARE
, template.faceName
);
239 oldFont
= SelectObject( hdc
, hFont
);
240 GetTextMetrics( hdc
, &tm
);
241 SelectObject( hdc
, oldFont
);
243 xUnit
= tm
.tmAveCharWidth
;
248 /* Create dialog main window */
250 rect
.left
= rect
.top
= 0;
251 rect
.right
= template.header
->cx
* xUnit
/ 4;
252 rect
.bottom
= template.header
->cy
* yUnit
/ 8;
253 if (template.header
->style
& DS_MODALFRAME
) exStyle
|= WS_EX_DLGMODALFRAME
;
254 AdjustWindowRectEx( &rect
, template.header
->style
, hMenu
, exStyle
);
256 hwnd
= CreateWindowEx( exStyle
, template.className
, template.caption
,
257 template.header
->style
,
258 rect
.left
+ template.header
->x
* xUnit
/ 4,
259 rect
.top
+ template.header
->y
* yUnit
/ 8,
260 rect
.right
- rect
.left
, rect
.bottom
- rect
.top
,
265 if (hFont
) DeleteObject( hFont
);
266 if (hMenu
) DestroyMenu( hMenu
);
270 /* Create control windows */
273 printf( " BEGIN\n" );
276 for (i
= 0; i
< template.header
->nbItems
; i
++)
278 DLGCONTROLHEADER
* next_header
;
280 next_header
= DIALOG_GetControl( header
, &class, &text
);
283 printf( " %s '%s' %d, %d, %d, %d, %d, %08x\n",
284 class, text
, header
->id
, header
->x
, header
->y
, header
->cx
,
285 header
->cy
, header
->style
);
287 if ((strcmp(class, "STATIC") == 0) & ((header
->style
& SS_ICON
) == SS_ICON
)) {
291 header
->style
|= WS_CHILD
;
292 CreateWindowEx( WS_EX_NOPARENTNOTIFY
,
293 class, text
, header
->style
,
294 header
->x
* xUnit
/ 4, header
->y
* yUnit
/ 8,
295 header
->cx
* xUnit
/ 4, header
->cy
* yUnit
/ 8,
296 hwnd
, header
->id
, hInst
, NULL
);
297 header
= next_header
;
304 /* Initialise dialog extra data */
306 wndPtr
= WIN_FindWndPtr( hwnd
);
307 dlgInfo
= (DIALOGINFO
*)wndPtr
->wExtra
;
308 dlgInfo
->dlgProc
= dlgProc
;
309 dlgInfo
->hUserFont
= hFont
;
310 dlgInfo
->hMenu
= hMenu
;
311 dlgInfo
->xBaseUnit
= xUnit
;
312 dlgInfo
->yBaseUnit
= yUnit
;
313 dlgInfo
->hwndFocus
= GetNextDlgTabItem( hwnd
,
314 GetWindow(wndPtr
->hwndChild
, GW_HWNDLAST
), FALSE
);
316 /* Send initialisation messages and set focus */
318 if (dlgInfo
->hUserFont
)
319 SendMessage( hwnd
, WM_SETFONT
, dlgInfo
->hUserFont
, 0);
320 SendMessage( hwnd
, WM_INITDIALOG
, dlgInfo
->hwndFocus
, param
);
321 /* if (SendMessage( hwnd, WM_INITDIALOG, dlgInfo->hwndFocus, param ))
322 SetFocus( dlgInfo->hwndFocus ); */
328 /***********************************************************************
331 static int DIALOG_DoDialogBox( HWND hwnd
)
334 DIALOGINFO
* dlgInfo
;
338 if (!(wndPtr
= WIN_FindWndPtr( hwnd
))) return -1;
339 dlgInfo
= (DIALOGINFO
*)wndPtr
->wExtra
;
340 ShowWindow( hwnd
, SW_SHOW
);
342 while (GetMessage (&msg
, 0, 0, 0))
344 if (!IsDialogMessage( hwnd
, &msg
))
346 TranslateMessage(&msg
);
347 DispatchMessage(&msg
);
349 if (dlgInfo
->fEnd
) break;
351 retval
= dlgInfo
->msgResult
;
352 DestroyWindow( hwnd
);
357 /***********************************************************************
358 * DialogBox (USER.87)
360 int DialogBox( HINSTANCE hInst
, LPCSTR dlgTemplate
,
361 HWND owner
, FARPROC dlgProc
)
363 return DialogBoxParam( hInst
, dlgTemplate
, owner
, dlgProc
, 0 );
367 /***********************************************************************
368 * DialogBoxParam (USER.239)
370 int DialogBoxParam( HINSTANCE hInst
, LPCSTR dlgTemplate
,
371 HWND owner
, FARPROC dlgProc
, LPARAM param
)
376 printf( "DialogBoxParam: %d,'%s',%d,%p,%d\n",
377 hInst
, dlgTemplate
, owner
, dlgProc
, param
);
379 hwnd
= CreateDialogParam( hInst
, dlgTemplate
, owner
, dlgProc
, param
);
380 if (hwnd
) return DIALOG_DoDialogBox( hwnd
);
385 /***********************************************************************
386 * DialogBoxIndirect (USER.218)
388 int DialogBoxIndirect( HINSTANCE hInst
, HANDLE dlgTemplate
,
389 HWND owner
, FARPROC dlgProc
)
391 return DialogBoxIndirectParam( hInst
, dlgTemplate
, owner
, dlgProc
, 0 );
395 /***********************************************************************
396 * DialogBoxIndirectParam (USER.240)
398 int DialogBoxIndirectParam( HINSTANCE hInst
, HANDLE dlgTemplate
,
399 HWND owner
, FARPROC dlgProc
, LPARAM param
)
404 if (!(ptr
= GlobalLock( dlgTemplate
))) return -1;
405 hwnd
= CreateDialogIndirectParam( hInst
, ptr
, owner
, dlgProc
, param
);
406 GlobalUnlock( dlgTemplate
);
407 if (hwnd
) return DIALOG_DoDialogBox( hwnd
);
412 /***********************************************************************
413 * EndDialog (USER.88)
415 void EndDialog( HWND hwnd
, short retval
)
417 WND
* wndPtr
= WIN_FindWndPtr( hwnd
);
418 DIALOGINFO
* dlgInfo
= (DIALOGINFO
*)wndPtr
->wExtra
;
419 dlgInfo
->msgResult
= retval
;
420 dlgInfo
->fEnd
= TRUE
;
422 printf( "EndDialog: %d %d\n", hwnd
, retval
);
427 /***********************************************************************
428 * IsDialogMessage (USER.90)
430 BOOL
IsDialogMessage( HWND hwndDlg
, LPMSG msg
)
434 if (!(wndPtr
= WIN_FindWndPtr( hwndDlg
))) return FALSE
;
435 if ((hwndDlg
!= msg
->hwnd
) && !IsChild( hwndDlg
, msg
->hwnd
)) return FALSE
;
437 if (msg
->message
!= WM_KEYDOWN
)
439 SendMessage( msg
->hwnd
, msg
->message
, msg
->wParam
, msg
->lParam
);
443 int dlgCode
= SendMessage( msg
->hwnd
, WM_GETDLGCODE
, 0, 0 );
444 /* Process key message */
451 /****************************************************************
452 * GetDlgCtrlID (USER.277)
454 int GetDlgCtrlID( HWND hwnd
)
456 WND
*wndPtr
= WIN_FindWndPtr(hwnd
);
457 if (wndPtr
) return wndPtr
->wIDmenu
;
462 /***********************************************************************
463 * GetDlgItem (USER.91)
465 HWND
GetDlgItem( HWND hwndDlg
, WORD id
)
471 if (!(wndPtr
= WIN_FindWndPtr( hwndDlg
))) return 0;
472 curChild
= wndPtr
->hwndChild
;
475 childPtr
= WIN_FindWndPtr( curChild
);
476 if (childPtr
->wIDmenu
== id
) return curChild
;
477 curChild
= childPtr
->hwndNext
;
483 /*******************************************************************
484 * SendDlgItemMessage (USER.101)
486 LONG
SendDlgItemMessage(HWND hwnd
, WORD id
, UINT msg
, WORD wParam
, LONG lParam
)
488 HWND hwndCtrl
= GetDlgItem( hwnd
, id
);
489 if (hwndCtrl
) return SendMessage( hwndCtrl
, msg
, wParam
, lParam
);
494 /*******************************************************************
495 * SetDlgItemText (USER.92)
497 void SetDlgItemText( HWND hwnd
, WORD id
, LPSTR lpString
)
499 SendDlgItemMessage( hwnd
, id
, WM_SETTEXT
, 0, (DWORD
)lpString
);
503 /***********************************************************************
504 * GetDlgItemText (USER.93)
506 int GetDlgItemText( HWND hwnd
, WORD id
, LPSTR str
, WORD max
)
508 return (int)SendDlgItemMessage( hwnd
, id
, WM_GETTEXT
, max
, (DWORD
)str
);
512 /*******************************************************************
513 * SetDlgItemInt (USER.94)
515 void SetDlgItemInt( HWND hwnd
, WORD id
, WORD value
, BOOL fSigned
)
517 HANDLE hText
= USER_HEAP_ALLOC(0, 10 );
518 char * str
= (char *) USER_HEAP_ADDR( hText
);
520 if (fSigned
) sprintf( str
, "%d", value
);
521 else sprintf( str
, "%u", value
);
522 SendDlgItemMessage( hwnd
, id
, WM_SETTEXT
, 0, (DWORD
)str
);
523 USER_HEAP_FREE( hText
);
527 /***********************************************************************
528 * GetDlgItemInt (USER.95)
530 WORD
GetDlgItemInt( HWND hwnd
, WORD id
, BOOL
* translated
, BOOL fSigned
)
537 if (translated
) *translated
= FALSE
;
538 if (!(len
= SendDlgItemMessage( hwnd
, id
, WM_GETTEXTLENGTH
, 0, 0 )))
540 if (!(hText
= USER_HEAP_ALLOC(0, len
+1 )))
542 str
= (char *) USER_HEAP_ADDR( hText
);
543 if (SendDlgItemMessage( hwnd
, id
, WM_GETTEXT
, len
+1, (DWORD
)str
))
546 result
= strtol( str
, &endptr
, 10 );
547 if (endptr
&& (endptr
!= str
)) /* Conversion was successful */
551 if ((result
< -32767) || (result
> 32767)) result
= 0;
552 else if (translated
) *translated
= TRUE
;
556 if ((result
< 0) || (result
> 65535)) result
= 0;
557 else if (translated
) *translated
= TRUE
;
561 USER_HEAP_FREE( hText
);
566 /***********************************************************************
567 * CheckDlgButton (USER.97)
569 void CheckDlgButton( HWND hwnd
, WORD id
, WORD check
)
571 SendDlgItemMessage( hwnd
, id
, BM_SETCHECK
, check
, 0 );
575 /***********************************************************************
576 * IsDlgButtonChecked (USER.98)
578 WORD
IsDlgButtonChecked( HWND hwnd
, WORD id
)
580 return (WORD
)SendDlgItemMessage( hwnd
, id
, BM_GETCHECK
, 0, 0 );
584 /***********************************************************************
585 * CheckRadioButton (USER.96)
587 void CheckRadioButton( HWND hwndDlg
, WORD firstID
, WORD lastID
, WORD checkID
)
589 HWND button
= GetDlgItem( hwndDlg
, firstID
);
592 WND
* wndPtr
= WIN_FindWndPtr( button
);
594 SendMessage( button
, BM_SETCHECK
, (wndPtr
->wIDmenu
== checkID
), 0 );
595 if (wndPtr
->wIDmenu
== lastID
) break;
596 button
= wndPtr
->hwndNext
;
601 /***********************************************************************
602 * GetDialogBaseUnits (USER.243)
604 DWORD
GetDialogBaseUnits()
606 return MAKELONG( xBaseUnit
, yBaseUnit
);
610 /***********************************************************************
611 * MapDialogRect (USER.103)
613 void MapDialogRect( HWND hwnd
, LPRECT rect
)
615 DIALOGINFO
* dlgInfo
;
616 WND
* wndPtr
= WIN_FindWndPtr( hwnd
);
618 dlgInfo
= (DIALOGINFO
*)wndPtr
->wExtra
;
619 rect
->left
= (rect
->left
* dlgInfo
->xBaseUnit
) / 4;
620 rect
->right
= (rect
->right
* dlgInfo
->xBaseUnit
) / 4;
621 rect
->top
= (rect
->top
* dlgInfo
->yBaseUnit
) / 8;
622 rect
->bottom
= (rect
->bottom
* dlgInfo
->yBaseUnit
) / 8;
626 /***********************************************************************
627 * GetNextDlgGroupItem (USER.227)
629 HWND
GetNextDlgGroupItem( HWND hwndDlg
, HWND hwndCtrl
, BOOL fPrevious
)
632 WND
* dlgPtr
, * ctrlPtr
, * wndPtr
;
634 if (!(dlgPtr
= WIN_FindWndPtr( hwndDlg
))) return 0;
635 if (!(ctrlPtr
= WIN_FindWndPtr( hwndCtrl
))) return 0;
636 if (ctrlPtr
->hwndParent
!= hwndDlg
) return 0;
639 hwnd
= ctrlPtr
->hwndNext
;
642 if (!hwnd
) hwnd
= dlgPtr
->hwndChild
;
643 if (hwnd
== hwndCtrl
) break;
644 wndPtr
= WIN_FindWndPtr( hwnd
);
645 if (wndPtr
->dwStyle
& WS_TABSTOP
)
648 if (!fPrevious
) break;
650 hwnd
= wndPtr
->hwndNext
;
657 /***********************************************************************
658 * GetNextDlgTabItem (USER.228)
660 HWND
GetNextDlgTabItem( HWND hwndDlg
, HWND hwndCtrl
, BOOL fPrevious
)
663 WND
* dlgPtr
, * ctrlPtr
, * wndPtr
;
665 if (!(dlgPtr
= WIN_FindWndPtr( hwndDlg
))) return 0;
666 if (!(ctrlPtr
= WIN_FindWndPtr( hwndCtrl
))) return 0;
667 if (ctrlPtr
->hwndParent
!= hwndDlg
) return 0;
670 hwnd
= ctrlPtr
->hwndNext
;
673 if (!hwnd
) hwnd
= dlgPtr
->hwndChild
;
674 if (hwnd
== hwndCtrl
) break;
675 wndPtr
= WIN_FindWndPtr( hwnd
);
676 if (wndPtr
->dwStyle
& WS_TABSTOP
)
679 if (!fPrevious
) break;
681 hwnd
= wndPtr
->hwndNext
;