4 * Copyright 1993 Alexandre Julliard
7 #define DEBUG_DIALOG /* */
9 static char Copyright
[] = "Copyright Alexandre Julliard, 1993";
20 /* Dialog base units */
21 static WORD xBaseUnit
= 0, yBaseUnit
= 0;
24 /***********************************************************************
27 * Initialisation of the dialog manager.
34 /* Calculate the dialog base units */
36 if (!(hdc
= GetDC(GetDesktopWindow()))) return FALSE
;
37 GetTextMetrics( hdc
, &tm
);
39 xBaseUnit
= tm
.tmAveCharWidth
;
40 yBaseUnit
= tm
.tmHeight
;
42 printf( "DIALOG_Init: base units = %d,%d\n", xBaseUnit
, yBaseUnit
);
48 /***********************************************************************
51 * Return the class and text of the control pointed to by ptr,
52 * and return a pointer to the next control.
54 static DLGCONTROLHEADER
* DIALOG_GetControl( DLGCONTROLHEADER
* ptr
,
55 char ** class, char ** text
)
57 unsigned char * p
= (unsigned char *)ptr
;
58 p
+= 14; /* size of control header */
63 case 0x80: *class = "BUTTON"; break;
64 case 0x81: *class = "EDIT"; break;
65 case 0x82: *class = "STATIC"; break;
66 case 0x83: *class = "LISTBOX"; break;
67 case 0x84: *class = "SCROLLBAR"; break;
68 case 0x85: *class = "COMBOBOX"; break;
69 default: *class = ""; break;
79 return (DLGCONTROLHEADER
*)p
;
83 /***********************************************************************
84 * DIALOG_ParseTemplate
86 * Fill a DLGTEMPLATE structure from the dialog template, and return
87 * a pointer to the first control.
89 static DLGCONTROLHEADER
* DIALOG_ParseTemplate( LPCSTR
template,
90 DLGTEMPLATE
* result
)
92 unsigned char * p
= (unsigned char *)template;
94 result
->header
= (DLGTEMPLATEHEADER
*)p
;
98 if (*p
== 0xff) p
+= 3;
99 else p
+= strlen(p
) + 1;
101 if (*p
) result
->className
= p
;
102 else result
->className
= DIALOG_CLASS_NAME
;
108 if (result
->header
->style
& DS_SETFONT
)
110 result
->pointSize
= *(WORD
*)p
; p
+= sizeof(WORD
);
111 result
->faceName
= p
; p
+= strlen(p
) + 1;
114 return (DLGCONTROLHEADER
*)p
;
118 /***********************************************************************
119 * DIALOG_DisplayTemplate
122 static void DIALOG_DisplayTemplate( DLGTEMPLATE
* result
)
124 printf( "DIALOG %d, %d, %d, %d\n", result
->header
->x
, result
->header
->y
,
125 result
->header
->cx
, result
->header
->cy
);
126 printf( " STYLE %08x\n", result
->header
->style
);
127 printf( " CAPTION '%s'\n", result
->caption
);
128 printf( " CLASS '%s'\n", result
->className
);
129 if (result
->menuName
[0] == 0xff)
130 printf( " MENU %d\n", result
->menuName
[1] + 256*result
->menuName
[2] );
131 else printf( " MENU '%s'\n", result
->menuName
);
132 if (result
->header
->style
& DS_SETFONT
)
133 printf( " FONT %d,'%s'\n", result
->pointSize
, result
->faceName
);
135 #endif /* DEBUG_DIALOG */
138 /***********************************************************************
139 * CreateDialog (USER.89)
141 HWND
CreateDialog( HINSTANCE hInst
, LPCSTR dlgTemplate
,
142 HWND owner
, FARPROC dlgProc
)
144 return CreateDialogParam( hInst
, dlgTemplate
, owner
, dlgProc
, 0 );
148 /***********************************************************************
149 * CreateDialogParam (USER.241)
151 HWND
CreateDialogParam( HINSTANCE hInst
, LPCSTR dlgTemplate
,
152 HWND owner
, FARPROC dlgProc
, LPARAM param
)
159 printf( "CreateDialogParam: %d,'%s',%d,%p,%d\n",
160 hInst
, dlgTemplate
, owner
, dlgProc
, param
);
163 /* FIXME: MAKEINTRESOURCE should be replaced by RT_DIALOG */
164 if (!(hres
= FindResource( hInst
, dlgTemplate
, MAKEINTRESOURCE(0x8005) )))
166 if (!(hmem
= LoadResource( hInst
, hres
))) return 0;
167 if (!(data
= LockResource( hmem
))) hwnd
= 0;
168 else hwnd
= CreateDialogIndirectParam(hInst
, data
, owner
, dlgProc
, param
);
169 FreeResource( hmem
);
174 /***********************************************************************
175 * CreateDialogIndirect (USER.219)
177 HWND
CreateDialogIndirect( HINSTANCE hInst
, LPCSTR dlgTemplate
,
178 HWND owner
, FARPROC dlgProc
)
180 return CreateDialogIndirectParam( hInst
, dlgTemplate
, owner
, dlgProc
, 0 );
184 /***********************************************************************
185 * CreateDialogIndirectParam (USER.242)
187 HWND
CreateDialogIndirectParam( HINSTANCE hInst
, LPCSTR dlgTemplate
,
188 HWND owner
, FARPROC dlgProc
, LPARAM param
)
196 DLGTEMPLATE
template;
197 DLGCONTROLHEADER
* header
;
198 DIALOGINFO
* dlgInfo
;
200 WORD xUnit
= xBaseUnit
;
201 WORD yUnit
= yBaseUnit
;
203 /* Parse dialog template */
205 if (!dlgTemplate
) return 0;
206 header
= DIALOG_ParseTemplate( dlgTemplate
, &template );
208 DIALOG_DisplayTemplate( &template );
213 switch (template.menuName
[0])
219 hMenu
= LoadMenu( hInst
, MAKEINTRESOURCE( template.menuName
[1] +
220 256*template.menuName
[2] ));
223 hMenu
= LoadMenu( hInst
, template.menuName
);
227 /* Create custom font if needed */
229 if (template.header
->style
& DS_SETFONT
)
231 hFont
= CreateFont( template.pointSize
, 0, 0, 0, FW_DONTCARE
,
232 FALSE
, FALSE
, FALSE
, DEFAULT_CHARSET
, 0, 0,
233 DEFAULT_QUALITY
, FF_DONTCARE
, template.faceName
);
241 oldFont
= SelectObject( hdc
, hFont
);
242 GetTextMetrics( hdc
, &tm
);
243 SelectObject( hdc
, oldFont
);
245 xUnit
= tm
.tmAveCharWidth
;
250 /* Create dialog main window */
252 rect
.left
= rect
.top
= 0;
253 rect
.right
= template.header
->cx
* xUnit
/ 4;
254 rect
.bottom
= template.header
->cy
* yUnit
/ 8;
255 if (template.header
->style
& DS_MODALFRAME
) exStyle
|= WS_EX_DLGMODALFRAME
;
256 AdjustWindowRectEx( &rect
, template.header
->style
, hMenu
, exStyle
);
258 hwnd
= CreateWindowEx( exStyle
, template.className
, template.caption
,
259 template.header
->style
,
260 rect
.left
+ template.header
->x
* xUnit
/ 4,
261 rect
.top
+ template.header
->y
* yUnit
/ 8,
262 rect
.right
- rect
.left
, rect
.bottom
- rect
.top
,
267 if (hFont
) DeleteObject( hFont
);
268 if (hMenu
) DestroyMenu( hMenu
);
272 ShowWindow(hwnd
, SW_SHOWNORMAL
);
275 /* Create control windows */
278 printf( " BEGIN\n" );
281 for (i
= 0; i
< template.header
->nbItems
; i
++)
283 DLGCONTROLHEADER
* next_header
;
285 next_header
= DIALOG_GetControl( header
, &class, &text
);
288 printf( " %s '%s' %d, %d, %d, %d, %d, %08x\n",
289 class, text
, header
->id
, header
->x
, header
->y
, header
->cx
,
290 header
->cy
, header
->style
);
292 if ((strcmp(class, "STATIC") == 0) & ((header
->style
& SS_ICON
) == SS_ICON
)) {
296 header
->style
|= WS_CHILD
;
297 CreateWindowEx( WS_EX_NOPARENTNOTIFY
,
298 class, text
, header
->style
,
299 header
->x
* xUnit
/ 4, header
->y
* yUnit
/ 8,
300 header
->cx
* xUnit
/ 4, header
->cy
* yUnit
/ 8,
301 hwnd
, header
->id
, hInst
, NULL
);
302 header
= next_header
;
309 /* Initialise dialog extra data */
311 wndPtr
= WIN_FindWndPtr( hwnd
);
312 dlgInfo
= (DIALOGINFO
*)wndPtr
->wExtra
;
313 dlgInfo
->dlgProc
= dlgProc
;
314 dlgInfo
->hUserFont
= hFont
;
315 dlgInfo
->hMenu
= hMenu
;
316 dlgInfo
->xBaseUnit
= xUnit
;
317 dlgInfo
->yBaseUnit
= yUnit
;
318 dlgInfo
->hwndFocus
= GetNextDlgTabItem( hwnd
,
319 GetWindow(wndPtr
->hwndChild
, GW_HWNDLAST
), FALSE
);
321 /* Send initialisation messages and set focus */
323 if (dlgInfo
->hUserFont
)
324 SendMessage( hwnd
, WM_SETFONT
, dlgInfo
->hUserFont
, 0 );
325 if (SendMessage( hwnd
, WM_INITDIALOG
, dlgInfo
->hwndFocus
, param
))
326 SetFocus( dlgInfo
->hwndFocus
);
332 /***********************************************************************
335 static int DIALOG_DoDialogBox( HWND hwnd
)
338 DIALOGINFO
* dlgInfo
;
343 if (!(wndPtr
= WIN_FindWndPtr( hwnd
))) return -1;
344 if (!(msgHandle
= USER_HEAP_ALLOC( GMEM_MOVEABLE
, sizeof(MSG
)))) return -1;
345 lpmsg
= (MSG
*) USER_HEAP_ADDR( msgHandle
);
346 dlgInfo
= (DIALOGINFO
*)wndPtr
->wExtra
;
347 ShowWindow( hwnd
, SW_SHOW
);
349 while (MSG_InternalGetMessage( lpmsg
, hwnd
, MSGF_DIALOGBOX
, TRUE
))
351 if (!IsDialogMessage( hwnd
, lpmsg
))
353 TranslateMessage( lpmsg
);
354 DispatchMessage( lpmsg
);
356 if (dlgInfo
->fEnd
) break;
358 retval
= dlgInfo
->msgResult
;
359 DestroyWindow( hwnd
);
360 USER_HEAP_FREE( msgHandle
);
365 /***********************************************************************
366 * DialogBox (USER.87)
368 int DialogBox( HINSTANCE hInst
, LPCSTR dlgTemplate
,
369 HWND owner
, FARPROC dlgProc
)
371 return DialogBoxParam( hInst
, dlgTemplate
, owner
, dlgProc
, 0 );
375 /***********************************************************************
376 * DialogBoxParam (USER.239)
378 int DialogBoxParam( HINSTANCE hInst
, LPCSTR dlgTemplate
,
379 HWND owner
, FARPROC dlgProc
, LPARAM param
)
384 printf( "DialogBoxParam: %d,'%s',%d,%p,%d\n",
385 hInst
, dlgTemplate
, owner
, dlgProc
, param
);
387 hwnd
= CreateDialogParam( hInst
, dlgTemplate
, owner
, dlgProc
, param
);
388 if (hwnd
) return DIALOG_DoDialogBox( hwnd
);
393 /***********************************************************************
394 * DialogBoxIndirect (USER.218)
396 int DialogBoxIndirect( HINSTANCE hInst
, HANDLE dlgTemplate
,
397 HWND owner
, FARPROC dlgProc
)
399 return DialogBoxIndirectParam( hInst
, dlgTemplate
, owner
, dlgProc
, 0 );
403 /***********************************************************************
404 * DialogBoxIndirectParam (USER.240)
406 int DialogBoxIndirectParam( HINSTANCE hInst
, HANDLE dlgTemplate
,
407 HWND owner
, FARPROC dlgProc
, LPARAM param
)
412 if (!(ptr
= GlobalLock( dlgTemplate
))) return -1;
413 hwnd
= CreateDialogIndirectParam( hInst
, ptr
, owner
, dlgProc
, param
);
414 GlobalUnlock( dlgTemplate
);
415 if (hwnd
) return DIALOG_DoDialogBox( hwnd
);
420 /***********************************************************************
421 * EndDialog (USER.88)
423 void EndDialog( HWND hwnd
, short retval
)
425 WND
* wndPtr
= WIN_FindWndPtr( hwnd
);
426 DIALOGINFO
* dlgInfo
= (DIALOGINFO
*)wndPtr
->wExtra
;
427 dlgInfo
->msgResult
= retval
;
428 dlgInfo
->fEnd
= TRUE
;
430 printf( "EndDialog: %d %d\n", hwnd
, retval
);
435 /***********************************************************************
436 * IsDialogMessage (USER.90)
438 BOOL
IsDialogMessage( HWND hwndDlg
, LPMSG msg
)
442 if (!(wndPtr
= WIN_FindWndPtr( hwndDlg
))) return FALSE
;
443 if ((hwndDlg
!= msg
->hwnd
) && !IsChild( hwndDlg
, msg
->hwnd
)) return FALSE
;
445 if (msg
->message
!= WM_KEYDOWN
)
447 SendMessage( msg
->hwnd
, msg
->message
, msg
->wParam
, msg
->lParam
);
451 int dlgCode
= SendMessage( msg
->hwnd
, WM_GETDLGCODE
, 0, 0 );
452 /* Process key message */
459 /****************************************************************
460 * GetDlgCtrlID (USER.277)
462 int GetDlgCtrlID( HWND hwnd
)
464 WND
*wndPtr
= WIN_FindWndPtr(hwnd
);
465 if (wndPtr
) return wndPtr
->wIDmenu
;
470 /***********************************************************************
471 * GetDlgItem (USER.91)
473 HWND
GetDlgItem( HWND hwndDlg
, WORD id
)
479 if (!(wndPtr
= WIN_FindWndPtr( hwndDlg
))) return 0;
480 curChild
= wndPtr
->hwndChild
;
483 childPtr
= WIN_FindWndPtr( curChild
);
484 if (childPtr
->wIDmenu
== id
) return curChild
;
485 curChild
= childPtr
->hwndNext
;
491 /*******************************************************************
492 * SendDlgItemMessage (USER.101)
494 LONG
SendDlgItemMessage(HWND hwnd
, WORD id
, UINT msg
, WORD wParam
, LONG lParam
)
496 HWND hwndCtrl
= GetDlgItem( hwnd
, id
);
497 if (hwndCtrl
) return SendMessage( hwndCtrl
, msg
, wParam
, lParam
);
502 /*******************************************************************
503 * SetDlgItemText (USER.92)
505 void SetDlgItemText( HWND hwnd
, WORD id
, LPSTR lpString
)
507 SendDlgItemMessage( hwnd
, id
, WM_SETTEXT
, 0, (DWORD
)lpString
);
511 /***********************************************************************
512 * GetDlgItemText (USER.93)
514 int GetDlgItemText( HWND hwnd
, WORD id
, LPSTR str
, WORD max
)
516 return (int)SendDlgItemMessage( hwnd
, id
, WM_GETTEXT
, max
, (DWORD
)str
);
520 /*******************************************************************
521 * SetDlgItemInt (USER.94)
523 void SetDlgItemInt( HWND hwnd
, WORD id
, WORD value
, BOOL fSigned
)
525 HANDLE hText
= USER_HEAP_ALLOC(0, 10 );
526 char * str
= (char *) USER_HEAP_ADDR( hText
);
528 if (fSigned
) sprintf( str
, "%d", value
);
529 else sprintf( str
, "%u", value
);
530 SendDlgItemMessage( hwnd
, id
, WM_SETTEXT
, 0, (DWORD
)str
);
531 USER_HEAP_FREE( hText
);
535 /***********************************************************************
536 * GetDlgItemInt (USER.95)
538 WORD
GetDlgItemInt( HWND hwnd
, WORD id
, BOOL
* translated
, BOOL fSigned
)
545 if (translated
) *translated
= FALSE
;
546 if (!(len
= SendDlgItemMessage( hwnd
, id
, WM_GETTEXTLENGTH
, 0, 0 )))
548 if (!(hText
= USER_HEAP_ALLOC(0, len
+1 )))
550 str
= (char *) USER_HEAP_ADDR( hText
);
551 if (SendDlgItemMessage( hwnd
, id
, WM_GETTEXT
, len
+1, (DWORD
)str
))
554 result
= strtol( str
, &endptr
, 10 );
555 if (endptr
&& (endptr
!= str
)) /* Conversion was successful */
559 if ((result
< -32767) || (result
> 32767)) result
= 0;
560 else if (translated
) *translated
= TRUE
;
564 if ((result
< 0) || (result
> 65535)) result
= 0;
565 else if (translated
) *translated
= TRUE
;
569 USER_HEAP_FREE( hText
);
574 /***********************************************************************
575 * CheckDlgButton (USER.97)
577 void CheckDlgButton( HWND hwnd
, WORD id
, WORD check
)
579 SendDlgItemMessage( hwnd
, id
, BM_SETCHECK
, check
, 0 );
583 /***********************************************************************
584 * IsDlgButtonChecked (USER.98)
586 WORD
IsDlgButtonChecked( HWND hwnd
, WORD id
)
588 return (WORD
)SendDlgItemMessage( hwnd
, id
, BM_GETCHECK
, 0, 0 );
592 /***********************************************************************
593 * CheckRadioButton (USER.96)
595 void CheckRadioButton( HWND hwndDlg
, WORD firstID
, WORD lastID
, WORD checkID
)
597 HWND button
= GetDlgItem( hwndDlg
, firstID
);
600 WND
* wndPtr
= WIN_FindWndPtr( button
);
602 SendMessage( button
, BM_SETCHECK
, (wndPtr
->wIDmenu
== checkID
), 0 );
603 if (wndPtr
->wIDmenu
== lastID
) break;
604 button
= wndPtr
->hwndNext
;
609 /***********************************************************************
610 * GetDialogBaseUnits (USER.243)
612 DWORD
GetDialogBaseUnits()
614 return MAKELONG( xBaseUnit
, yBaseUnit
);
618 /***********************************************************************
619 * MapDialogRect (USER.103)
621 void MapDialogRect( HWND hwnd
, LPRECT rect
)
623 DIALOGINFO
* dlgInfo
;
624 WND
* wndPtr
= WIN_FindWndPtr( hwnd
);
626 dlgInfo
= (DIALOGINFO
*)wndPtr
->wExtra
;
627 rect
->left
= (rect
->left
* dlgInfo
->xBaseUnit
) / 4;
628 rect
->right
= (rect
->right
* dlgInfo
->xBaseUnit
) / 4;
629 rect
->top
= (rect
->top
* dlgInfo
->yBaseUnit
) / 8;
630 rect
->bottom
= (rect
->bottom
* dlgInfo
->yBaseUnit
) / 8;
634 /***********************************************************************
635 * GetNextDlgGroupItem (USER.227)
637 HWND
GetNextDlgGroupItem( HWND hwndDlg
, HWND hwndCtrl
, BOOL fPrevious
)
640 WND
* dlgPtr
, * ctrlPtr
, * wndPtr
;
642 if (!(dlgPtr
= WIN_FindWndPtr( hwndDlg
))) return 0;
643 if (!(ctrlPtr
= WIN_FindWndPtr( hwndCtrl
))) return 0;
644 if (ctrlPtr
->hwndParent
!= hwndDlg
) return 0;
647 hwnd
= ctrlPtr
->hwndNext
;
650 if (!hwnd
) hwnd
= dlgPtr
->hwndChild
;
651 if (hwnd
== hwndCtrl
) break;
652 wndPtr
= WIN_FindWndPtr( hwnd
);
653 if (wndPtr
->dwStyle
& WS_TABSTOP
)
656 if (!fPrevious
) break;
658 hwnd
= wndPtr
->hwndNext
;
665 /***********************************************************************
666 * GetNextDlgTabItem (USER.228)
668 HWND
GetNextDlgTabItem( HWND hwndDlg
, HWND hwndCtrl
, BOOL fPrevious
)
671 WND
* dlgPtr
, * ctrlPtr
, * wndPtr
;
673 if (!(dlgPtr
= WIN_FindWndPtr( hwndDlg
))) return 0;
674 if (!(ctrlPtr
= WIN_FindWndPtr( hwndCtrl
))) return 0;
675 if (ctrlPtr
->hwndParent
!= hwndDlg
) return 0;
678 hwnd
= ctrlPtr
->hwndNext
;
681 if (!hwnd
) hwnd
= dlgPtr
->hwndChild
;
682 if (hwnd
== hwndCtrl
) break;
683 wndPtr
= WIN_FindWndPtr( hwnd
);
684 if (wndPtr
->dwStyle
& WS_TABSTOP
)
687 if (!fPrevious
) break;
689 hwnd
= wndPtr
->hwndNext
;