Release 950918
[wine/hacks.git] / windows / msgbox.c
blob05bc09c6f636845d40e73ecd63cdbbcfea6e49bb
1 /*
2 * Message boxes
4 * Copyright 1995 Bernd Schmidt
6 */
8 #include "windows.h"
9 #include "dlgs.h"
10 #include "global.h"
11 #include "selectors.h"
12 #include "alias.h"
13 #include "relay32.h"
14 #include "../rc/sysres.h"
15 #include "task.h"
17 typedef struct {
18 LPSTR title;
19 LPSTR text;
20 WORD type;
21 } MSGBOX, *LPMSGBOX;
23 LONG SystemMessageBoxProc(HWND hwnd, WORD message, WORD wParam, LONG lParam)
25 LPMSGBOX lpmb;
26 RECT rect, textrect;
27 HWND hItem;
28 HDC hdc;
29 LONG lRet;
30 int i, buttons, bwidth, bheight, theight, wwidth, bpos;
31 int borheight, iheight, tiheight;
33 switch(message) {
34 case WM_INITDIALOG:
35 lpmb = (LPMSGBOX)lParam;
36 if (lpmb->title != NULL) {
37 SetWindowText(hwnd, lpmb->title);
39 SetWindowText(GetDlgItem(hwnd, 100), lpmb->text);
40 /* Hide not selected buttons */
41 switch(lpmb->type & MB_TYPEMASK) {
42 case MB_OK:
43 ShowWindow(GetDlgItem(hwnd, 2), SW_HIDE);
44 /* fall through */
45 case MB_OKCANCEL:
46 ShowWindow(GetDlgItem(hwnd, 3), SW_HIDE);
47 ShowWindow(GetDlgItem(hwnd, 4), SW_HIDE);
48 ShowWindow(GetDlgItem(hwnd, 5), SW_HIDE);
49 ShowWindow(GetDlgItem(hwnd, 6), SW_HIDE);
50 ShowWindow(GetDlgItem(hwnd, 7), SW_HIDE);
51 break;
52 case MB_ABORTRETRYIGNORE:
53 ShowWindow(GetDlgItem(hwnd, 1), SW_HIDE);
54 ShowWindow(GetDlgItem(hwnd, 2), SW_HIDE);
55 ShowWindow(GetDlgItem(hwnd, 6), SW_HIDE);
56 ShowWindow(GetDlgItem(hwnd, 7), SW_HIDE);
57 break;
58 case MB_YESNO:
59 ShowWindow(GetDlgItem(hwnd, 2), SW_HIDE);
60 /* fall through */
61 case MB_YESNOCANCEL:
62 ShowWindow(GetDlgItem(hwnd, 1), SW_HIDE);
63 ShowWindow(GetDlgItem(hwnd, 3), SW_HIDE);
64 ShowWindow(GetDlgItem(hwnd, 4), SW_HIDE);
65 ShowWindow(GetDlgItem(hwnd, 5), SW_HIDE);
66 break;
68 /* Set the icon */
69 switch(lpmb->type & MB_ICONMASK) {
70 case MB_ICONEXCLAMATION:
71 SendDlgItemMessage(hwnd, stc1, STM_SETICON, LoadIcon(0, IDI_EXCLAMATION), 0);
72 break;
73 case MB_ICONQUESTION:
74 SendDlgItemMessage(hwnd, stc1, STM_SETICON, LoadIcon(0, IDI_QUESTION), 0);
75 break;
76 case MB_ICONASTERISK:
77 SendDlgItemMessage(hwnd, stc1, STM_SETICON, LoadIcon(0, IDI_ASTERISK), 0);
78 break;
79 case MB_ICONHAND:
80 default:
81 SendDlgItemMessage(hwnd, stc1, STM_SETICON, LoadIcon(0, IDI_HAND), 0);
82 break;
85 /* Position everything */
86 GetWindowRect(hwnd, &rect);
87 borheight = rect.bottom - rect.top;
88 wwidth = rect.right - rect.left;
89 GetClientRect(hwnd, &rect);
90 borheight -= rect.bottom - rect.top;
92 /* Get the icon height */
93 GetWindowRect(GetDlgItem(hwnd, 1088), &rect);
94 iheight = rect.bottom - rect.top;
96 /* Get the number of visible buttons and their width */
97 GetWindowRect(GetDlgItem(hwnd, 2), &rect);
98 bheight = rect.bottom - rect.top;
99 bwidth = rect.left;
100 GetWindowRect(GetDlgItem(hwnd, 1), &rect);
101 bwidth -= rect.left;
102 for (buttons = 0, i = 1; i < 8; i++) {
103 hItem = GetDlgItem(hwnd, i);
104 if (GetWindowLong(hItem, GWL_STYLE) & WS_VISIBLE) {
105 buttons++;
109 /* Get the text size */
110 hItem = GetDlgItem(hwnd, 100);
111 GetWindowRect(hItem, &textrect);
112 MapWindowPoints(0, hwnd, (LPPOINT)&textrect, 2);
114 GetClientRect(hItem, &rect);
115 hdc = GetDC(hItem);
116 lRet = DrawText(hdc, lpmb->text, -1, &rect,
117 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
118 theight = rect.bottom - rect.top;
119 tiheight = 16 + max(iheight, theight);
120 ReleaseDC(hItem, hdc);
122 /* Position the text */
123 SetWindowPos(hItem, 0, textrect.left, (tiheight - theight) / 2,
124 rect.right - rect.left, theight,
125 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
127 /* Position the icon */
128 hItem = GetDlgItem(hwnd, 1088);
129 GetWindowRect(hItem, &rect);
130 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
131 SetWindowPos(hItem, 0, rect.left, (tiheight - iheight) / 2, 0, 0,
132 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
134 /* Resize the window */
135 SetWindowPos(hwnd, 0, 0, 0, wwidth, 8 + tiheight + bheight + borheight,
136 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
138 /* Position the buttons */
139 bpos = (wwidth - bwidth * buttons) / 2;
140 GetWindowRect(GetDlgItem(hwnd, 1), &rect);
141 for (buttons = i = 0; i < 7; i++) {
142 /* some arithmetic to get the right order for YesNoCancel windows */
143 hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
144 if (GetWindowLong(hItem, GWL_STYLE) & WS_VISIBLE) {
145 if (buttons++ == ((lpmb->type & MB_DEFMASK) >> 8)) {
146 SetFocus(hItem);
147 SendMessage(hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
149 SetWindowPos(hItem, 0, bpos, tiheight, 0, 0,
150 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
151 bpos += bwidth;
154 return 0;
155 break;
157 case WM_COMMAND:
158 switch (wParam) {
159 case IDOK:
160 case IDCANCEL:
161 case IDABORT:
162 case IDRETRY:
163 case IDIGNORE:
164 case IDYES:
165 case IDNO:
166 EndDialog(hwnd, wParam);
167 break;
169 break;
171 return 0;
174 /**************************************************************************
175 * MessageBox [USER.1]
178 int MessageBox(HWND hWnd, LPSTR text, LPSTR title, WORD type)
180 HANDLE handle;
181 MSGBOX mbox;
182 int ret;
183 DWORD WineProc,Win16Proc,Win32Proc;
184 static int initialized = 0;
186 mbox.title = title;
187 mbox.text = text;
188 mbox.type = type;
190 if (!initialized)
192 WineProc=(DWORD)SystemMessageBoxProc;
193 Win16Proc=(DWORD)GetWndProcEntry16("SystemMessageBoxProc");
194 Win32Proc=(DWORD)RELAY32_GetEntryPoint("WINPROCS32","SystemMessageBoxProc",0);
195 ALIAS_RegisterAlias(WineProc,Win16Proc,Win32Proc);
196 initialized=1;
199 handle = GLOBAL_CreateBlock( GMEM_FIXED, sysres_DIALOG_MSGBOX.bytes,
200 sysres_DIALOG_MSGBOX.size, GetCurrentPDB(),
201 FALSE, FALSE, TRUE, NULL );
202 if (!handle) return 0;
203 ret = DialogBoxIndirectParam( GetWindowWord(hWnd, GWW_HINSTANCE),
204 handle, hWnd,
205 GetWndProcEntry16("SystemMessageBoxProc"),
206 (LONG)&mbox );
207 GLOBAL_FreeBlock( handle );
208 return ret;
211 /**************************************************************************
212 * FatalAppExit [USER.137]
215 void FatalAppExit(WORD wAction, LPSTR str)
217 MessageBox(0, str, NULL, MB_SYSTEMMODAL | MB_OK);
218 TASK_KillCurrentTask(0);