Release 980413
[wine] / windows / msgbox.c
1 /*
2  * Message boxes
3  *
4  * Copyright 1995 Bernd Schmidt
5  */
6
7 #include <stdio.h>
8 #include "windows.h"
9 #include "dlgs.h"
10 #include "heap.h"
11 #include "module.h"
12 #include "win.h"
13 #include "resource.h"
14 #include "task.h"
15 #include "debug.h"
16 #include "debugstr.h"
17
18 /**************************************************************************
19  *           MSGBOX_DlgProc
20  *
21  * Dialog procedure for message boxes.
22  */
23 static LRESULT CALLBACK MSGBOX_DlgProc( HWND32 hwnd, UINT32 message,
24                                         WPARAM32 wParam, LPARAM lParam )
25 {
26   LPMSGBOXPARAMS32A lpmb;
27
28   RECT32 rect, textrect;
29   HWND32 hItem;
30   HDC32 hdc;
31   LRESULT lRet;
32   int i, buttons, bwidth, bheight, theight, wwidth, bpos;
33   int borheight, iheight, tiheight;
34   
35   switch(message) {
36    case WM_INITDIALOG:
37     lpmb = (LPMSGBOXPARAMS32A)lParam;
38     if (lpmb->lpszCaption) SetWindowText32A(hwnd, lpmb->lpszCaption);
39     SetWindowText32A(GetDlgItem32(hwnd, 100), lpmb->lpszText);
40     /* Hide not selected buttons */
41     switch(lpmb->dwStyle & MB_TYPEMASK) {
42      case MB_OK:
43       ShowWindow32(GetDlgItem32(hwnd, 2), SW_HIDE);
44       /* fall through */
45      case MB_OKCANCEL:
46       ShowWindow32(GetDlgItem32(hwnd, 3), SW_HIDE);
47       ShowWindow32(GetDlgItem32(hwnd, 4), SW_HIDE);
48       ShowWindow32(GetDlgItem32(hwnd, 5), SW_HIDE);
49       ShowWindow32(GetDlgItem32(hwnd, 6), SW_HIDE);
50       ShowWindow32(GetDlgItem32(hwnd, 7), SW_HIDE);
51       break;
52      case MB_ABORTRETRYIGNORE:
53       ShowWindow32(GetDlgItem32(hwnd, 1), SW_HIDE);
54       ShowWindow32(GetDlgItem32(hwnd, 2), SW_HIDE);
55       ShowWindow32(GetDlgItem32(hwnd, 6), SW_HIDE);
56       ShowWindow32(GetDlgItem32(hwnd, 7), SW_HIDE);
57       break;
58      case MB_YESNO:
59       ShowWindow32(GetDlgItem32(hwnd, 2), SW_HIDE);
60       /* fall through */
61      case MB_YESNOCANCEL:
62       ShowWindow32(GetDlgItem32(hwnd, 1), SW_HIDE);
63       ShowWindow32(GetDlgItem32(hwnd, 3), SW_HIDE);
64       ShowWindow32(GetDlgItem32(hwnd, 4), SW_HIDE);
65       ShowWindow32(GetDlgItem32(hwnd, 5), SW_HIDE);
66       break;
67     }
68     /* Set the icon */
69     switch(lpmb->dwStyle & MB_ICONMASK) {
70      case MB_ICONEXCLAMATION:
71       SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
72                            (WPARAM16)LoadIcon16(0, IDI_EXCLAMATION16), 0);
73       break;
74      case MB_ICONQUESTION:
75       SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
76                            (WPARAM16)LoadIcon16(0, IDI_QUESTION16), 0);
77       break;
78      case MB_ICONASTERISK:
79       SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
80                            (WPARAM16)LoadIcon16(0, IDI_ASTERISK16), 0);
81       break;
82      case MB_ICONHAND:
83      default:
84       SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
85                            (WPARAM16)LoadIcon16(0, IDI_HAND16), 0);
86       break;
87     }
88     
89     /* Position everything */
90     GetWindowRect32(hwnd, &rect);
91     borheight = rect.bottom - rect.top;
92     wwidth = rect.right - rect.left;
93     GetClientRect32(hwnd, &rect);
94     borheight -= rect.bottom - rect.top;
95
96     /* Get the icon height */
97     GetWindowRect32(GetDlgItem32(hwnd, 1088), &rect);
98     iheight = rect.bottom - rect.top;
99     
100     /* Get the number of visible buttons and their width */
101     GetWindowRect32(GetDlgItem32(hwnd, 2), &rect);
102     bheight = rect.bottom - rect.top;
103     bwidth = rect.left;
104     GetWindowRect32(GetDlgItem32(hwnd, 1), &rect);
105     bwidth -= rect.left;
106     for (buttons = 0, i = 1; i < 8; i++)
107     {
108       hItem = GetDlgItem32(hwnd, i);
109       if (GetWindowLong32A(hItem, GWL_STYLE) & WS_VISIBLE) buttons++;
110     }
111     
112     /* Get the text size */
113     hItem = GetDlgItem32(hwnd, 100);
114     GetWindowRect32(hItem, &textrect);
115     MapWindowPoints32(0, hwnd, (LPPOINT32)&textrect, 2);
116     
117     GetClientRect32(hItem, &rect);
118     hdc = GetDC32(hItem);
119     lRet = DrawText32A( hdc, lpmb->lpszText, -1, &rect,
120                         DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
121     theight = rect.bottom  - rect.top;
122     tiheight = 16 + MAX(iheight, theight);
123     ReleaseDC32(hItem, hdc);
124     
125     /* Position the text */
126     SetWindowPos32(hItem, 0, textrect.left, (tiheight - theight) / 2, 
127                    rect.right - rect.left, theight,
128                    SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
129     
130     /* Position the icon */
131     hItem = GetDlgItem32(hwnd, 1088);
132     GetWindowRect32(hItem, &rect);
133     MapWindowPoints32(0, hwnd, (LPPOINT32)&rect, 2);
134     SetWindowPos32(hItem, 0, rect.left, (tiheight - iheight) / 2, 0, 0,
135                    SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
136     
137     /* Resize the window */
138     SetWindowPos32(hwnd, 0, 0, 0, wwidth, 8 + tiheight + bheight + borheight,
139                    SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
140     
141     /* Position the buttons */
142     bpos = (wwidth - bwidth * buttons) / 2;
143     GetWindowRect32(GetDlgItem32(hwnd, 1), &rect);
144     for (buttons = i = 0; i < 7; i++) {
145       /* some arithmetic to get the right order for YesNoCancel windows */
146       hItem = GetDlgItem32(hwnd, (i + 5) % 7 + 1);
147       if (GetWindowLong32A(hItem, GWL_STYLE) & WS_VISIBLE) {
148         if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
149           SetFocus32(hItem);
150           SendMessage32A( hItem, BM_SETSTYLE32, BS_DEFPUSHBUTTON, TRUE );
151         }
152         SetWindowPos32(hItem, 0, bpos, tiheight, 0, 0,
153                        SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
154         bpos += bwidth;
155       }
156     }
157     return 0;
158     break;
159     
160    case WM_COMMAND:
161     switch (wParam)
162     {
163      case IDOK:
164      case IDCANCEL:
165      case IDABORT:
166      case IDRETRY:
167      case IDIGNORE:
168      case IDYES:
169      case IDNO:
170       EndDialog32(hwnd, wParam);
171       break;
172     }
173    default:
174      /* Ok. Ignore all the other messages */
175      TRACE (dialog, "Message number %i is being ignored.\n", message);
176     break;
177   }
178   return 0;
179 }
180
181
182 /**************************************************************************
183  *           MessageBox16   (USER.1)
184  */
185 INT16 WINAPI MessageBox16( HWND16 hwnd, LPCSTR text, LPCSTR title, UINT16 type)
186 {
187     return MessageBox32A( hwnd, text, title, type );
188 }
189
190
191 /**************************************************************************
192  *           MessageBox32A   (USER32.391)
193  */
194 INT32 WINAPI MessageBox32A(HWND32 hWnd, LPCSTR text, LPCSTR title, UINT32 type)
195 {
196     MSGBOXPARAMS32A mbox;
197
198     if (!text) text="<WINE-NULL>";
199     if (!title)
200       title="Error";
201     mbox.lpszCaption = title;
202     mbox.lpszText  = text;
203     mbox.dwStyle  = type;
204     return DialogBoxIndirectParam32A( WIN_GetWindowInstance(hWnd),
205                                       SYSRES_GetResPtr( SYSRES_DIALOG_MSGBOX ),
206                                       hWnd, MSGBOX_DlgProc, (LPARAM)&mbox );
207 }
208
209
210 /**************************************************************************
211  *           MessageBox32W   (USER32.396)
212  */
213 INT32 WINAPI MessageBox32W( HWND32 hwnd, LPCWSTR text, LPCWSTR title,
214                             UINT32 type )
215 {
216     LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
217     LPSTR textA  = HEAP_strdupWtoA( GetProcessHeap(), 0, text );
218     INT32 ret = MessageBox32A( hwnd, textA, titleA, type );
219     HeapFree( GetProcessHeap(), 0, titleA );
220     HeapFree( GetProcessHeap(), 0, textA );
221     return ret;
222 }
223
224
225 /**************************************************************************
226  *           MessageBoxEx32A   (USER32.392)
227  */
228 INT32 WINAPI MessageBoxEx32A( HWND32 hWnd, LPCSTR text, LPCSTR title,
229                               UINT32 type, WORD langid )
230 {
231     /* ignore language id for now */
232     return MessageBox32A(hWnd,text,title,type);
233 }
234
235 /**************************************************************************
236  *           MessageBoxEx32W   (USER32.393)
237  */
238 INT32 WINAPI MessageBoxEx32W( HWND32 hWnd, LPCWSTR text, LPCWSTR title,
239                               UINT32 type, WORD langid )
240 {
241     /* ignore language id for now */
242     return MessageBox32W(hWnd,text,title,type);
243 }
244
245 /**************************************************************************
246  *           MessageBoxIndirect16   (USER.827)
247  */
248 INT16 WINAPI MessageBoxIndirect16( LPMSGBOXPARAMS16 msgbox )
249 {
250     MSGBOXPARAMS32A msgbox32;
251     
252     msgbox32.cbSize             = msgbox->cbSize;
253     msgbox32.hwndOwner          = msgbox->hwndOwner;
254     msgbox32.hInstance          = msgbox->hInstance;
255     msgbox32.lpszText           = PTR_SEG_TO_LIN(msgbox->lpszText);
256     msgbox32.lpszCaption        = PTR_SEG_TO_LIN(msgbox->lpszCaption);
257     msgbox32.dwStyle            = msgbox->dwStyle;
258     msgbox32.lpszIcon           = PTR_SEG_TO_LIN(msgbox->lpszIcon);
259     msgbox32.dwContextHelpId    = msgbox->dwContextHelpId;
260     msgbox32.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
261     msgbox32.dwLanguageId       = msgbox->dwLanguageId;
262
263     return DialogBoxIndirectParam32A( msgbox32.hInstance,
264                                       SYSRES_GetResPtr( SYSRES_DIALOG_MSGBOX ),
265                                       msgbox32.hwndOwner, MSGBOX_DlgProc,
266                                       (LPARAM)&msgbox32 );
267 }
268
269 /**************************************************************************
270  *           MessageBoxIndirect32A   (USER32.394)
271  */
272 INT32 WINAPI MessageBoxIndirect32A( LPMSGBOXPARAMS32A msgbox )
273 {
274     return DialogBoxIndirectParam32A( msgbox->hInstance,
275                                       SYSRES_GetResPtr( SYSRES_DIALOG_MSGBOX ),
276                                       msgbox->hwndOwner, MSGBOX_DlgProc,
277                                       (LPARAM)msgbox );
278 }
279
280 /**************************************************************************
281  *           MessageBoxIndirect32W   (USER32.395)
282  */
283 INT32 WINAPI MessageBoxIndirect32W( LPMSGBOXPARAMS32W msgbox )
284 {
285     MSGBOXPARAMS32A     msgboxa;
286
287     memcpy(&msgboxa,msgbox,sizeof(msgboxa));
288     if (msgbox->lpszCaption)    lstrcpyWtoA(msgboxa.lpszCaption,msgbox->lpszCaption);
289     if (msgbox->lpszText)       lstrcpyWtoA(msgboxa.lpszText,msgbox->lpszText);
290
291     return MessageBoxIndirect32A(&msgboxa);
292 }
293
294
295 /**************************************************************************
296  *           FatalAppExit16   (KERNEL.137)
297  */
298 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
299 {
300     FatalAppExit32A( action, str );
301 }
302
303
304 /**************************************************************************
305  *           FatalAppExit32A   (KERNEL32.108)
306  */
307 void WINAPI FatalAppExit32A( UINT32 action, LPCSTR str )
308 {
309     MessageBox32A( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
310     TASK_KillCurrentTask(0);
311 }
312
313
314 /**************************************************************************
315  *           FatalAppExit32W   (KERNEL32.109)
316  */
317 void WINAPI FatalAppExit32W( UINT32 action, LPCWSTR str )
318 {
319     MessageBox32W( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
320     TASK_KillCurrentTask(0);
321 }
322
323