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