Release 941030
[wine/multimedia.git] / misc / message.c
blob50637369077fc763fe8845aa580be5ee78e45eba
1 /*
2 * 'Wine' MessageBox function handling
4 * Copyright 1993 Martin Ayotte
5 */
7 static char Copyright[] = "Copyright Martin Ayotte, 1993";
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <windows.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <ctype.h>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include "prototypes.h"
20 #include "heap.h"
21 #include "win.h"
22 #include "texts.h"
23 #include "stddebug.h"
24 /* #define DEBUG_MSGBOX */
25 /* #undef DEBUG_MSGBOX */
26 #include "debug.h"
30 * Defaults for button-texts
33 ButtonTexts ButtonText = {
34 "&Yes", 'Y',
35 "&No", 'N',
36 "&Ok", 'O',
37 "&Cancel", 'C',
38 "&Abort", 'A',
39 "&Retry", 'R',
40 "&Ignore", 'I'
43 extern HINSTANCE hSysRes;
44 extern HBITMAP hUpArrow;
46 typedef struct tagMSGBOX {
47 LPSTR Title;
48 LPSTR Str;
49 WORD wType;
50 WORD wRetVal;
51 BOOL ActiveFlg;
52 HWND hWndYes;
53 HWND hWndNo;
54 HWND hWndCancel;
55 HICON hIcon;
56 RECT rectIcon;
57 RECT rectStr;
58 } MSGBOX;
59 typedef MSGBOX FAR* LPMSGBOX;
61 LONG SystemMessageBoxProc(HWND hwnd, WORD message, WORD wParam, LONG lParam);
63 /**************************************************************************
64 * MessageBox [USER.1]
67 int MessageBox(HWND hWnd, LPSTR str, LPSTR title, WORD type)
69 HWND hDlg, hWndOld;
70 WND *wndPtr;
71 WNDCLASS wndClass;
72 MSG msg;
73 LPMSGBOX lpmb;
74 DWORD dwStyle;
75 HINSTANCE hInst;
76 int nRet;
78 if (title == NULL)
79 title = "Error";
80 wndPtr = WIN_FindWndPtr(hWnd);
81 if (wndPtr == NULL) {
82 hInst = hSysRes;
83 dprintf_msgbox(stddeb,"MessageBox(NULL, %08X='%s', %08X='%s', %04X)\n",
84 str, str, title, title, type);
86 else {
87 hInst = wndPtr->hInstance;
88 dprintf_msgbox(stddeb,"MessageBox(%04X, %08X='%s', %08X='%s', %04X)\n",
89 hWnd, str, str, title, title, type);
91 lpmb = (LPMSGBOX) malloc(sizeof(MSGBOX));
92 memset(lpmb, 0, sizeof(MSGBOX));
93 /* lpmb->Title = title;*/
94 lpmb->Title = (LPSTR) malloc(strlen(title) + 1);
95 strcpy(lpmb->Title, title);
96 /* lpmb->Str = str;*/
97 if (str && *str)
99 lpmb->Str = (LPSTR) malloc(strlen(str) + 1);
100 strcpy(lpmb->Str, str);
102 else
104 lpmb->Str = (LPSTR) malloc(8);
105 strcpy(lpmb->Str, "Message");
107 lpmb->wType = type;
108 lpmb->ActiveFlg = TRUE;
109 wndClass.style = CS_HREDRAW | CS_VREDRAW ;
110 wndClass.lpfnWndProc = (WNDPROC)SystemMessageBoxProc;
111 wndClass.cbClsExtra = 0;
112 wndClass.cbWndExtra = 0;
113 wndClass.hInstance = hInst;
114 wndClass.hIcon = (HICON)NULL;
115 wndClass.hCursor = LoadCursor((HANDLE)NULL, IDC_ARROW);
116 wndClass.hbrBackground = GetStockObject(WHITE_BRUSH);
117 wndClass.lpszMenuName = NULL;
118 wndClass.lpszClassName = "MESSAGEBOX";
119 dprintf_msgbox(stddeb, "MessageBox // before RegisterClass, '%s' '%s' !\n", str, title);
120 if (!RegisterClass(&wndClass)) {
121 printf("Unable to Register class 'MESSAGEBOX' !\n");
122 if (lpmb != NULL) free(lpmb);
123 return 0;
125 dwStyle = WS_POPUP | WS_DLGFRAME | WS_VISIBLE;
126 if ((type & (MB_SYSTEMMODAL | MB_TASKMODAL)) == 0) dwStyle |= WS_CAPTION;
127 hWndOld = GetFocus();
128 hDlg = CreateWindow("MESSAGEBOX", lpmb->Title, dwStyle, 100, 150, 400, 160,
129 (HWND)NULL, (HMENU)NULL, hInst, (LPSTR)lpmb);
130 if (hDlg == 0) {
131 printf("Unable to create 'MESSAGEBOX' window !\n");
132 if (lpmb != NULL) free(lpmb);
133 return 0;
135 dprintf_msgbox(stddeb, "MessageBox // before Msg Loop !\n");
136 while(TRUE) {
137 if (!lpmb->ActiveFlg) break;
138 if (!GetMessage(&msg, (HWND)NULL, 0, 0)) break;
139 TranslateMessage(&msg);
140 if ((type & (MB_SYSTEMMODAL | MB_TASKMODAL)) != 0 &&
141 msg.hwnd != hDlg) {
142 switch(msg.message) {
143 case WM_KEYDOWN:
144 case WM_LBUTTONDOWN:
145 case WM_MBUTTONDOWN:
146 case WM_RBUTTONDOWN:
147 MessageBeep(0);
148 break;
151 DispatchMessage(&msg);
153 SetFocus(hWndOld);
154 nRet = lpmb->wRetVal;
155 if (lpmb != NULL) free(lpmb);
156 if (!UnregisterClass("MESSAGEBOX", hInst)) return 0;
157 dprintf_msgbox(stddeb, "MessageBox return %04X !\n", nRet);
158 return(nRet);
162 LPMSGBOX MsgBoxGetStorageHeader(HWND hwnd)
164 WND *wndPtr;
165 LPMSGBOX lpmb;
166 wndPtr = WIN_FindWndPtr(hwnd);
167 if (wndPtr == 0) {
168 printf("Bad Window handle on MessageBox !\n");
169 return 0;
171 lpmb = *((LPMSGBOX *)&wndPtr->wExtra[1]);
172 return lpmb;
178 LONG SystemMessageBoxProc(HWND hWnd, WORD message, WORD wParam, LONG lParam)
180 WND *wndPtr;
181 CREATESTRUCT *createStruct;
182 PAINTSTRUCT ps;
183 HDC hDC;
184 DWORD OldTextColor;
185 RECT rect;
186 LPMSGBOX lpmb;
188 switch(message) {
189 case WM_CREATE:
190 dprintf_msgbox(stddeb, "MessageBox WM_CREATE hWnd=%04X !\n", hWnd);
191 wndPtr = WIN_FindWndPtr(hWnd);
192 createStruct = (CREATESTRUCT *)lParam;
193 lpmb = (LPMSGBOX)createStruct->lpCreateParams;
194 if (lpmb == NULL) break;
195 *((LPMSGBOX *)&wndPtr->wExtra[1]) = lpmb;
196 dprintf_msgbox(stddeb, "MessageBox WM_CREATE title='%s' str='%s' !\n",
197 lpmb->Title, lpmb->Str);
198 GetClientRect(hWnd, &rect);
199 CopyRect(&lpmb->rectStr, &rect);
200 lpmb->rectStr.bottom -= 32;
201 switch(lpmb->wType & MB_TYPEMASK) {
202 case MB_OK :
203 lpmb->hWndYes = CreateWindow("BUTTON", ButtonText.Ok.Label,
204 WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | BS_PUSHBUTTON,
205 rect.right / 2 - 30, rect.bottom - 25,
206 60, 18, hWnd, IDOK, wndPtr->hInstance, 0L);
207 break;
208 case MB_OKCANCEL :
209 lpmb->hWndYes = CreateWindow("BUTTON", ButtonText.Ok.Label,
210 WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | BS_PUSHBUTTON,
211 rect.right / 2 - 65, rect.bottom - 25,
212 60, 18, hWnd, IDOK, wndPtr->hInstance, 0L);
213 lpmb->hWndCancel = CreateWindow("BUTTON", ButtonText.Cancel.Label,
214 WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | BS_PUSHBUTTON,
215 rect.right / 2 + 5, rect.bottom - 25,
216 60, 18, hWnd, IDCANCEL, wndPtr->hInstance, 0L);
217 break;
218 case MB_ABORTRETRYIGNORE :
219 lpmb->hWndYes = CreateWindow("BUTTON", ButtonText.Retry.Label,
220 WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | BS_PUSHBUTTON,
221 rect.right / 2 - 100, rect.bottom - 25,
222 60, 18, hWnd, IDRETRY, wndPtr->hInstance, 0L);
223 lpmb->hWndNo = CreateWindow("BUTTON", ButtonText.Ignore.Label,
224 WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | BS_PUSHBUTTON,
225 rect.right / 2 - 30, rect.bottom - 25,
226 60, 18, hWnd, IDIGNORE, wndPtr->hInstance, 0L);
227 lpmb->hWndCancel = CreateWindow("BUTTON", ButtonText.Abort.Label,
228 WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | BS_PUSHBUTTON,
229 rect.right / 2 + 40, rect.bottom - 25,
230 60, 18, hWnd, IDABORT, wndPtr->hInstance, 0L);
231 break;
232 case MB_YESNO :
233 lpmb->hWndYes = CreateWindow("BUTTON", ButtonText.Yes.Label,
234 WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | BS_PUSHBUTTON,
235 rect.right / 2 - 65, rect.bottom - 25,
236 60, 18, hWnd, IDYES, wndPtr->hInstance, 0L);
237 lpmb->hWndNo = CreateWindow("BUTTON", ButtonText.No.Label,
238 WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | BS_PUSHBUTTON,
239 rect.right / 2 + 5, rect.bottom - 25,
240 60, 18, hWnd, IDNO, wndPtr->hInstance, 0L);
241 break;
242 case MB_YESNOCANCEL :
243 lpmb->hWndYes = CreateWindow("BUTTON", ButtonText.Yes.Label,
244 WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | BS_PUSHBUTTON,
245 rect.right / 2 - 100, rect.bottom - 25,
246 60, 18, hWnd, IDYES, wndPtr->hInstance, 0L);
247 lpmb->hWndNo = CreateWindow("BUTTON", ButtonText.No.Label,
248 WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | BS_PUSHBUTTON,
249 rect.right / 2 - 30, rect.bottom - 25,
250 60, 18, hWnd, IDNO, wndPtr->hInstance, 0L);
251 lpmb->hWndCancel = CreateWindow("BUTTON", ButtonText.Cancel.Label,
252 WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE | BS_PUSHBUTTON,
253 rect.right / 2 + 40, rect.bottom - 25,
254 60, 18, hWnd, IDCANCEL, wndPtr->hInstance, 0L);
255 break;
257 switch(lpmb->wType & MB_ICONMASK) {
258 case MB_ICONEXCLAMATION:
259 printf("MsgBox LoadIcon Exclamation !\n");
260 lpmb->hIcon = LoadIcon((HINSTANCE)NULL, IDI_EXCLAMATION);
261 break;
262 case MB_ICONQUESTION:
263 printf("MsgBox LoadIcon Question !\n");
264 lpmb->hIcon = LoadIcon((HINSTANCE)NULL, IDI_QUESTION);
265 break;
266 case MB_ICONASTERISK:
267 printf("MsgBox LoadIcon Asterisk !\n");
268 lpmb->hIcon = LoadIcon((HINSTANCE)NULL, IDI_ASTERISK);
269 break;
270 case MB_ICONHAND:
271 printf("MsgBox LoadIcon Hand !\n");
272 lpmb->hIcon = LoadIcon((HINSTANCE)NULL, IDI_HAND);
273 break;
275 if (lpmb->hIcon != (HICON)NULL) {
276 SetRect(&lpmb->rectIcon, 16,
277 lpmb->rectStr.bottom / 2 - 16, 48,
278 lpmb->rectStr.bottom / 2 + 16);
279 lpmb->rectStr.left += 64;
281 break;
282 case WM_SHOWWINDOW:
283 dprintf_msgbox(stddeb, "MessageBox WM_SHOWWINDOW hWnd=%04X !\n", hWnd);
284 if (!(wParam == 0 && lParam == 0L)) {
285 InvalidateRect(hWnd, NULL, TRUE);
287 break;
288 case WM_PAINT:
289 dprintf_msgbox(stddeb, "MessageBox WM_PAINT hWnd=%04X !\n", hWnd);
290 lpmb = MsgBoxGetStorageHeader(hWnd);
291 if (lpmb == NULL) break;
292 if (!lpmb->ActiveFlg) break;
293 hDC = BeginPaint(hWnd, &ps);
294 if (hDC == 0) {
295 printf("MessageBox WM_PAINT // BeginPaint returned BAD hDC !\n");
296 break;
298 GetClientRect(hWnd, &rect);
299 FillRect(hDC, &rect, GetStockObject(WHITE_BRUSH));
300 CopyRect(&rect, &lpmb->rectStr);
301 OldTextColor = SetTextColor(hDC, 0x00000000);
302 if (lpmb->hIcon)
303 DrawIcon(hDC, lpmb->rectIcon.left,
304 lpmb->rectIcon.top, lpmb->hIcon);
305 DrawText(hDC, lpmb->Str, -1, &rect,
306 DT_CALCRECT | DT_CENTER | DT_WORDBREAK);
307 rect.top = lpmb->rectStr.bottom / 2 - rect.bottom / 2;
308 rect.bottom = lpmb->rectStr.bottom / 2 + rect.bottom / 2;
309 DrawText(hDC, lpmb->Str, -1, &rect, DT_CENTER | DT_WORDBREAK);
310 SetTextColor(hDC, OldTextColor);
311 EndPaint(hWnd, &ps);
312 dprintf_msgbox(stddeb, "MessageBox End of WM_PAINT !\n");
313 break;
314 case WM_DESTROY:
315 dprintf_msgbox(stddeb, "MessageBox WM_DESTROY !\n");
316 ReleaseCapture();
317 lpmb = MsgBoxGetStorageHeader(hWnd);
318 if (lpmb == NULL) break;
319 if (lpmb->hIcon) DestroyIcon(lpmb->hIcon);
320 if (lpmb->hWndYes) DestroyWindow(lpmb->hWndYes);
321 if (lpmb->hWndNo) DestroyWindow(lpmb->hWndNo);
322 if (lpmb->hWndCancel) DestroyWindow(lpmb->hWndCancel);
323 dprintf_msgbox(stddeb, "MessageBox WM_DESTROY end !\n");
324 lpmb->ActiveFlg = FALSE;
325 break;
326 case WM_COMMAND:
327 lpmb = MsgBoxGetStorageHeader(hWnd);
328 if (lpmb == NULL) break;
329 if (wParam < IDOK || wParam > IDNO) return(0);
330 lpmb->wRetVal = wParam;
331 dprintf_msgbox(stddeb, "MessageBox sending WM_CLOSE !\n");
332 PostMessage(hWnd, WM_CLOSE, 0, 0L);
333 break;
334 case WM_CHAR:
335 lpmb = MsgBoxGetStorageHeader(hWnd);
336 /* if (wParam >= 'a' || wParam <= 'z') wParam -= 'a' - 'A'; */
337 wParam = toupper(wParam);
338 if (wParam == ButtonText.Yes.Hotkey)
339 lpmb->wRetVal = IDYES;
340 else if (wParam == ButtonText.Ok.Hotkey)
341 lpmb->wRetVal = IDOK;
342 else if (wParam == ButtonText.Retry.Hotkey)
343 lpmb->wRetVal = IDRETRY;
344 else if (wParam == ButtonText.Abort.Hotkey)
345 lpmb->wRetVal = IDABORT;
346 else if (wParam == ButtonText.No.Hotkey)
347 lpmb->wRetVal = IDNO;
348 else if (wParam == ButtonText.Ignore.Hotkey)
349 lpmb->wRetVal = IDIGNORE;
350 else if ((wParam == ButtonText.Ok.Hotkey) || (wParam == VK_ESCAPE))
351 lpmb->wRetVal = IDCANCEL;
352 else
353 return 0;
354 if (lpmb == NULL) break;
355 ShowWindow(hWnd, SW_HIDE);
356 PostMessage(hWnd, WM_CLOSE, 0, 0L);
357 break;
358 default:
359 return DefWindowProc(hWnd, message, wParam, lParam );
361 return(0);
366 /*************************************************************************
367 * "About Wine..." Dialog Box
369 BOOL FAR PASCAL AboutWine_Proc(HWND hDlg, WORD msg, WORD wParam, LONG lParam)
371 HDC hDC;
372 HDC hMemDC;
373 PAINTSTRUCT ps;
374 RECT rect;
375 BITMAP bm;
376 OFSTRUCT ofstruct;
377 HBITMAP hbmpOld;
378 static LPSTR ptr;
379 static char str[256];
380 static HBITMAP hBitMap = 0;
381 static BOOL CreditMode;
382 static HANDLE hFile = 0;
383 switch (msg) {
384 case WM_INITDIALOG:
385 CreditMode = FALSE;
386 strcpy(str, "WINELOGO");
387 hBitMap = LoadBitmap((HINSTANCE)NULL, (LPSTR)str);
389 strcpy(str, "LICENSE");
390 printf("str = '%s'\n", str);
391 hFile = OpenFile((LPSTR)str, &ofstruct, OF_READ);
392 ptr = (LPSTR)malloc(2048);
393 lseek(hFile, 0L, SEEK_SET);
394 _lread(hFile, ptr, 2000L);
395 close(hFile);
396 return TRUE;
397 case WM_PAINT:
398 hDC = BeginPaint(hDlg, &ps);
399 GetClientRect(hDlg, &rect);
400 if (CreditMode) {
401 FillRect(hDC, &rect, GetStockObject(WHITE_BRUSH));
402 InflateRect(&rect, -8, -8);
403 DrawText(hDC, ptr, -1, &rect, DT_LEFT | DT_WORDBREAK);
404 EndPaint(hDlg, &ps);
405 return TRUE;
407 FillRect(hDC, &rect, GetStockObject(GRAY_BRUSH));
408 InflateRect(&rect, -3, -3);
409 FrameRect(hDC, &rect, GetStockObject(BLACK_BRUSH));
410 InflateRect(&rect, -10, -10);
411 hMemDC = CreateCompatibleDC(hDC);
412 hbmpOld = SelectObject(hMemDC, hBitMap);
413 GetObject(hBitMap, sizeof(BITMAP), (LPSTR)&bm);
414 BitBlt(hDC, rect.left, rect.top, bm.bmWidth, bm.bmHeight,
415 hMemDC, 0, 0, SRCCOPY);
416 SelectObject( hMemDC, hbmpOld );
417 DeleteDC(hMemDC);
418 EndPaint(hDlg, &ps);
419 return TRUE;
420 case WM_COMMAND:
421 switch (wParam)
423 case IDYES:
424 if (!CreditMode) {
425 SetWindowPos(hDlg, (HWND)NULL, 0, 0, 640, 480,
426 SWP_NOMOVE | SWP_NOZORDER);
428 else {
429 SetWindowPos(hDlg, (HWND)NULL, 0, 0, 320, 250,
430 SWP_NOMOVE | SWP_NOZORDER);
432 CreditMode = !CreditMode;
433 ShowWindow(GetDlgItem(hDlg, IDYES), CreditMode ? SW_HIDE : SW_SHOW);
434 ShowWindow(GetDlgItem(hDlg, IDOK), CreditMode ? SW_HIDE : SW_SHOW);
435 InvalidateRect(hDlg, (LPRECT)NULL, TRUE);
436 UpdateWindow(hDlg);
437 return TRUE;
438 case IDCANCEL:
439 case IDOK:
440 if (hBitMap != 0 ) DeleteObject(hBitMap);
441 if (ptr != NULL) free(ptr);
442 EndDialog(hDlg, TRUE);
443 return TRUE;
444 default:
445 return TRUE;
448 return FALSE;
452 /**************************************************************************
453 * FatalAppExit [USER.137]
456 void FatalAppExit(WORD wAction, LPSTR str)
458 MessageBox((HWND)NULL, str, NULL, MB_SYSTEMMODAL | MB_OK);
459 exit(1);