Release 940405
[wine/gsoc-2012-control.git] / windows / dialog.c
blob35dc9f0db8b09238d549e11576adcac93f4047b6
1 /*
2 * Dialog functions
4 * Copyright 1993 Alexandre Julliard
5 */
7 static char Copyright[] = "Copyright Alexandre Julliard, 1993";
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include "windows.h"
13 #include "dialog.h"
14 #include "win.h"
15 #include "user.h"
18 /* Dialog base units */
19 static WORD xBaseUnit = 0, yBaseUnit = 0;
22 /***********************************************************************
23 * DIALOG_Init
25 * Initialisation of the dialog manager.
27 BOOL DIALOG_Init()
29 TEXTMETRIC tm;
30 HDC hdc;
32 /* Calculate the dialog base units */
34 if (!(hdc = GetDC(GetDesktopWindow()))) return FALSE;
35 GetTextMetrics( hdc, &tm );
36 ReleaseDC( 0, hdc );
37 xBaseUnit = tm.tmAveCharWidth;
38 yBaseUnit = tm.tmHeight;
39 #ifdef DEBUG_DIALOG
40 printf( "DIALOG_Init: base units = %d,%d\n", xBaseUnit, yBaseUnit );
41 #endif
42 return TRUE;
46 /***********************************************************************
47 * DIALOG_GetControl
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 */
57 if (*p & 0x80)
59 switch(*p++)
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;
70 else
72 *class = p;
73 p += strlen(p) + 1;
75 *text = p;
76 p += strlen(p) + 2;
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;
93 p += 13;
95 result->menuName = 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;
101 p += strlen(p) + 1;
103 result->caption = p;
104 p += strlen(p) + 1;
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
119 #ifdef DEBUG_DIALOG
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 )
152 HWND hwnd = 0;
153 HANDLE hres, hmem;
154 LPCSTR data;
156 #ifdef DEBUG_DIALOG
157 printf( "CreateDialogParam: %d,'%s',%d,%p,%d\n",
158 hInst, dlgTemplate, owner, dlgProc, param );
159 #endif
161 /* FIXME: MAKEINTRESOURCE should be replaced by RT_DIALOG */
162 if (!(hres = FindResource( hInst, dlgTemplate, MAKEINTRESOURCE(0x8005) )))
163 return 0;
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 );
168 return hwnd;
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 )
188 HMENU hMenu;
189 HFONT hFont = 0;
190 HWND hwnd;
191 RECT rect;
192 WND * wndPtr;
193 int i;
194 DLGTEMPLATE template;
195 DLGCONTROLHEADER * header;
196 DIALOGINFO * dlgInfo;
197 DWORD exStyle = 0;
198 WORD xUnit = xBaseUnit;
199 WORD yUnit = yBaseUnit;
201 /* Parse dialog template */
203 if (!dlgTemplate) return 0;
204 header = DIALOG_ParseTemplate( dlgTemplate, &template );
205 #ifdef DEBUG_DIALOG
206 DIALOG_DisplayTemplate( &template );
207 #endif
209 /* Load menu */
211 switch (template.menuName[0])
213 case 0x00:
214 hMenu = 0;
215 break;
216 case 0xff:
217 hMenu = LoadMenu( hInst, MAKEINTRESOURCE( template.menuName[1] +
218 256*template.menuName[2] ));
219 break;
220 default:
221 hMenu = LoadMenu( hInst, template.menuName );
222 break;
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 );
232 if (hFont)
234 TEXTMETRIC tm;
235 HFONT oldFont;
236 HDC hdc;
238 hdc = GetDC(0);
239 oldFont = SelectObject( hdc, hFont );
240 GetTextMetrics( hdc, &tm );
241 SelectObject( hdc, oldFont );
242 ReleaseDC( 0, hdc );
243 xUnit = tm.tmAveCharWidth;
244 yUnit = tm.tmHeight;
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,
261 owner, hMenu, hInst,
262 NULL );
263 if (!hwnd)
265 if (hFont) DeleteObject( hFont );
266 if (hMenu) DestroyMenu( hMenu );
267 return 0;
270 /* Create control windows */
272 #ifdef DEBUG_DIALOG
273 printf( " BEGIN\n" );
274 #endif
276 for (i = 0; i < template.header->nbItems; i++)
278 DLGCONTROLHEADER * next_header;
279 LPSTR class, text;
280 next_header = DIALOG_GetControl( header, &class, &text );
282 #ifdef DEBUG_DIALOG
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 );
286 #endif
287 if ((strcmp(class, "STATIC") == 0) & ((header->style & SS_ICON) == SS_ICON)) {
288 header->cx = 32;
289 header->cy = 32;
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;
300 #ifdef DEBUG_DIALOG
301 printf( " END\n" );
302 #endif
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 ); */
324 return hwnd;
328 /***********************************************************************
329 * DIALOG_DoDialogBox
331 static int DIALOG_DoDialogBox( HWND hwnd )
333 WND * wndPtr;
334 DIALOGINFO * dlgInfo;
335 MSG msg;
336 int retval;
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 );
353 return retval;
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 )
373 HWND hwnd;
375 #ifdef DEBUG_DIALOG
376 printf( "DialogBoxParam: %d,'%s',%d,%p,%d\n",
377 hInst, dlgTemplate, owner, dlgProc, param );
378 #endif
379 hwnd = CreateDialogParam( hInst, dlgTemplate, owner, dlgProc, param );
380 if (hwnd) return DIALOG_DoDialogBox( hwnd );
381 return -1;
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 )
401 HWND hwnd;
402 LPCSTR ptr;
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 );
408 return -1;
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;
421 #ifdef DEBUG_DIALOG
422 printf( "EndDialog: %d %d\n", hwnd, retval );
423 #endif
427 /***********************************************************************
428 * IsDialogMessage (USER.90)
430 BOOL IsDialogMessage( HWND hwndDlg, LPMSG msg )
432 WND * wndPtr;
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 );
441 else
443 int dlgCode = SendMessage( msg->hwnd, WM_GETDLGCODE, 0, 0 );
444 /* Process key message */
445 /* .... */
447 return TRUE;
451 /****************************************************************
452 * GetDlgCtrlID (USER.277)
454 int GetDlgCtrlID( HWND hwnd )
456 WND *wndPtr = WIN_FindWndPtr(hwnd);
457 if (wndPtr) return wndPtr->wIDmenu;
458 else return 0;
462 /***********************************************************************
463 * GetDlgItem (USER.91)
465 HWND GetDlgItem( HWND hwndDlg, WORD id )
467 HWND curChild;
468 WND * childPtr;
469 WND * wndPtr;
471 if (!(wndPtr = WIN_FindWndPtr( hwndDlg ))) return 0;
472 curChild = wndPtr->hwndChild;
473 while(curChild)
475 childPtr = WIN_FindWndPtr( curChild );
476 if (childPtr->wIDmenu == id) return curChild;
477 curChild = childPtr->hwndNext;
479 return 0;
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 );
490 else return 0;
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 )
532 int len;
533 HANDLE hText;
534 long result;
535 char * str;
537 if (translated) *translated = FALSE;
538 if (!(len = SendDlgItemMessage( hwnd, id, WM_GETTEXTLENGTH, 0, 0 )))
539 return 0;
540 if (!(hText = USER_HEAP_ALLOC(0, len+1 )))
541 return 0;
542 str = (char *) USER_HEAP_ADDR( hText );
543 if (SendDlgItemMessage( hwnd, id, WM_GETTEXT, len+1, (DWORD)str ))
545 char * endptr;
546 result = strtol( str, &endptr, 10 );
547 if (endptr && (endptr != str)) /* Conversion was successful */
549 if (fSigned)
551 if ((result < -32767) || (result > 32767)) result = 0;
552 else if (translated) *translated = TRUE;
554 else
556 if ((result < 0) || (result > 65535)) result = 0;
557 else if (translated) *translated = TRUE;
561 USER_HEAP_FREE( hText );
562 return (WORD)result;
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 );
590 while (button != 0)
592 WND * wndPtr = WIN_FindWndPtr( button );
593 if (!wndPtr) break;
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 );
617 if (!wndPtr) return;
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 )
631 HWND hwnd, hwndLast;
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;
638 hwndLast = hwndCtrl;
639 hwnd = ctrlPtr->hwndNext;
640 while (1)
642 if (!hwnd) hwnd = dlgPtr->hwndChild;
643 if (hwnd == hwndCtrl) break;
644 wndPtr = WIN_FindWndPtr( hwnd );
645 if (wndPtr->dwStyle & WS_TABSTOP)
647 hwndLast = hwnd;
648 if (!fPrevious) break;
650 hwnd = wndPtr->hwndNext;
652 return hwndLast;
653 return 0;
657 /***********************************************************************
658 * GetNextDlgTabItem (USER.228)
660 HWND GetNextDlgTabItem( HWND hwndDlg, HWND hwndCtrl, BOOL fPrevious )
662 HWND hwnd, hwndLast;
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;
669 hwndLast = hwndCtrl;
670 hwnd = ctrlPtr->hwndNext;
671 while (1)
673 if (!hwnd) hwnd = dlgPtr->hwndChild;
674 if (hwnd == hwndCtrl) break;
675 wndPtr = WIN_FindWndPtr( hwnd );
676 if (wndPtr->dwStyle & WS_TABSTOP)
678 hwndLast = hwnd;
679 if (!fPrevious) break;
681 hwnd = wndPtr->hwndNext;
683 return hwndLast;