Release 950522
[wine.git] / windows / msgbox.c
blob8e4aec41137c52d559d124b5ccf42d562137b14d
1 /*
2 * Message boxes
4 * Copyright 1995 Bernd Schmidt
6 */
8 #include "windows.h"
9 #include "dlgs.h"
10 #include "dialog.h"
11 #include "selectors.h"
12 #include "../rc/sysres.h"
13 #include "task.h"
15 typedef struct {
16 LPSTR title;
17 LPSTR text;
18 WORD type;
19 } MSGBOX, *LPMSGBOX;
21 LONG SystemMessageBoxProc(HWND hwnd, WORD message, WORD wParam, LONG lParam)
23 LPMSGBOX lpmb;
24 RECT rect, textrect;
25 HWND hItem;
26 HDC hdc;
27 LONG lRet;
28 int i, buttons, bwidth, bheight, theight, wwidth, bpos;
29 int borheight, iheight, tiheight;
31 switch(message) {
32 case WM_INITDIALOG:
33 lpmb = (LPMSGBOX)lParam;
34 if (lpmb->title != NULL) {
35 SetWindowText(hwnd, lpmb->title);
37 SetWindowText(GetDlgItem(hwnd, 100), lpmb->text);
38 /* Hide not selected buttons */
39 switch(lpmb->type & MB_TYPEMASK) {
40 case MB_OK:
41 ShowWindow(GetDlgItem(hwnd, 2), SW_HIDE);
42 /* fall through */
43 case MB_OKCANCEL:
44 ShowWindow(GetDlgItem(hwnd, 3), SW_HIDE);
45 ShowWindow(GetDlgItem(hwnd, 4), SW_HIDE);
46 ShowWindow(GetDlgItem(hwnd, 5), SW_HIDE);
47 ShowWindow(GetDlgItem(hwnd, 6), SW_HIDE);
48 ShowWindow(GetDlgItem(hwnd, 7), SW_HIDE);
49 break;
50 case MB_ABORTRETRYIGNORE:
51 ShowWindow(GetDlgItem(hwnd, 1), SW_HIDE);
52 ShowWindow(GetDlgItem(hwnd, 2), SW_HIDE);
53 ShowWindow(GetDlgItem(hwnd, 6), SW_HIDE);
54 ShowWindow(GetDlgItem(hwnd, 7), SW_HIDE);
55 break;
56 case MB_YESNO:
57 ShowWindow(GetDlgItem(hwnd, 2), SW_HIDE);
58 /* fall through */
59 case MB_YESNOCANCEL:
60 ShowWindow(GetDlgItem(hwnd, 1), SW_HIDE);
61 ShowWindow(GetDlgItem(hwnd, 3), SW_HIDE);
62 ShowWindow(GetDlgItem(hwnd, 4), SW_HIDE);
63 ShowWindow(GetDlgItem(hwnd, 5), SW_HIDE);
64 break;
66 /* Set the icon */
67 switch(lpmb->type & MB_ICONMASK) {
68 case MB_ICONEXCLAMATION:
69 SendDlgItemMessage(hwnd, stc1, STM_SETICON, LoadIcon(0, IDI_EXCLAMATION), 0);
70 break;
71 case MB_ICONQUESTION:
72 SendDlgItemMessage(hwnd, stc1, STM_SETICON, LoadIcon(0, IDI_QUESTION), 0);
73 break;
74 case MB_ICONASTERISK:
75 SendDlgItemMessage(hwnd, stc1, STM_SETICON, LoadIcon(0, IDI_ASTERISK), 0);
76 break;
77 case MB_ICONHAND:
78 SendDlgItemMessage(hwnd, stc1, STM_SETICON, LoadIcon(0, IDI_HAND), 0);
79 break;
82 /* Position everything */
83 GetWindowRect(hwnd, &rect);
84 borheight = rect.bottom - rect.top;
85 wwidth = rect.right - rect.left;
86 GetClientRect(hwnd, &rect);
87 borheight -= rect.bottom - rect.top;
89 /* Get the icon height */
90 GetWindowRect(GetDlgItem(hwnd, 1088), &rect);
91 iheight = rect.bottom - rect.top;
93 /* Get the number of visible buttons and their width */
94 GetWindowRect(GetDlgItem(hwnd, 2), &rect);
95 bheight = rect.bottom - rect.top;
96 bwidth = rect.left;
97 GetWindowRect(GetDlgItem(hwnd, 1), &rect);
98 bwidth -= rect.left;
99 for (buttons = 0, i = 1; i < 8; i++) {
100 hItem = GetDlgItem(hwnd, i);
101 if (GetWindowLong(hItem, GWL_STYLE) & WS_VISIBLE) {
102 buttons++;
106 /* Get the text size */
107 hItem = GetDlgItem(hwnd, 100);
108 GetWindowRect(hItem, &textrect);
109 MapWindowPoints(0, hwnd, (LPPOINT)&textrect, 2);
111 GetClientRect(hItem, &rect);
112 hdc = GetDC(hItem);
113 lRet = DrawText(hdc, lpmb->text, -1, &rect,
114 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
115 theight = rect.bottom - rect.top;
116 tiheight = 16 + max(iheight, theight);
117 ReleaseDC(hItem, hdc);
119 /* Position the text */
120 SetWindowPos(hItem, 0, textrect.left, (tiheight - theight) / 2,
121 rect.right - rect.left, theight,
122 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
124 /* Position the icon */
125 hItem = GetDlgItem(hwnd, 1088);
126 GetWindowRect(hItem, &rect);
127 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
128 SetWindowPos(hItem, 0, rect.left, (tiheight - iheight) / 2, 0, 0,
129 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
131 /* Resize the window */
132 SetWindowPos(hwnd, 0, 0, 0, wwidth, 8 + tiheight + bheight + borheight,
133 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
135 /* Position the buttons */
136 bpos = (wwidth - bwidth * buttons) / 2;
137 GetWindowRect(GetDlgItem(hwnd, 1), &rect);
138 for (buttons = i = 0; i < 7; i++) {
139 /* some arithmetic to get the right order for YesNoCancel windows */
140 hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
141 if (GetWindowLong(hItem, GWL_STYLE) & WS_VISIBLE) {
142 if (buttons++ == ((lpmb->type & MB_DEFMASK) >> 8)) {
143 SetFocus(hItem);
144 SendMessage(hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE);
146 SetWindowPos(hItem, 0, bpos, tiheight, 0, 0,
147 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
148 bpos += bwidth;
151 return 0;
152 break;
154 case WM_COMMAND:
155 switch (wParam) {
156 case IDOK:
157 case IDCANCEL:
158 case IDABORT:
159 case IDRETRY:
160 case IDIGNORE:
161 case IDYES:
162 case IDNO:
163 EndDialog(hwnd, wParam);
164 break;
166 break;
168 return 0;
171 /**************************************************************************
172 * MessageBox [USER.1]
175 int MessageBox(HWND hWnd, LPSTR text, LPSTR title, WORD type)
177 MSGBOX mbox;
179 mbox.title = title;
180 mbox.text = text;
181 mbox.type = type;
182 return DialogBoxIndirectParamPtr(GetWindowWord(hWnd, GWW_HINSTANCE),
183 sysres_DIALOG_MSGBOX, hWnd,
184 GetWndProcEntry16("SystemMessageBoxProc"),
185 (LONG)&mbox);
188 /**************************************************************************
189 * FatalAppExit [USER.137]
192 void FatalAppExit(WORD wAction, LPSTR str)
194 MessageBox(0, str, NULL, MB_SYSTEMMODAL | MB_OK);
195 TASK_KillCurrentTask(0);