]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
e5b50758 | 2 | // Name: src/msw/msgdlg.cpp |
2bda0e17 KB |
3 | // Purpose: wxMessageDialog |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
2bda0e17 KB |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
7520f3da | 16 | #pragma hdrstop |
2bda0e17 KB |
17 | #endif |
18 | ||
a8ff046b VZ |
19 | #if wxUSE_MSGDLG |
20 | ||
246c5004 WS |
21 | #include "wx/msgdlg.h" |
22 | ||
704c499e VZ |
23 | // there is no hook support under CE so we can't use the code for message box |
24 | // positioning there | |
25 | #ifndef __WXWINCE__ | |
26 | #define wxUSE_MSGBOX_HOOK 1 | |
27 | #else | |
28 | #define wxUSE_MSGBOX_HOOK 0 | |
29 | #endif | |
30 | ||
2bda0e17 | 31 | #ifndef WX_PRECOMP |
35bc781e | 32 | #include "wx/app.h" |
0d7ea902 VZ |
33 | #include "wx/utils.h" |
34 | #include "wx/dialog.h" | |
704c499e VZ |
35 | #if wxUSE_MSGBOX_HOOK |
36 | #include "wx/hashmap.h" | |
37 | #endif | |
2bda0e17 KB |
38 | #endif |
39 | ||
40 | #include "wx/msw/private.h" | |
23e00c55 VZ |
41 | #include "wx/msw/private/button.h" |
42 | #include "wx/msw/private/metrics.h" | |
43 | ||
44 | #if wxUSE_MSGBOX_HOOK | |
45 | #include "wx/fontutil.h" | |
46 | #endif | |
2bda0e17 | 47 | |
676d6550 JS |
48 | // For MB_TASKMODAL |
49 | #ifdef __WXWINCE__ | |
23e00c55 | 50 | #include "wx/msw/wince/missing.h" |
676d6550 JS |
51 | #endif |
52 | ||
2bda0e17 | 53 | IMPLEMENT_CLASS(wxMessageDialog, wxDialog) |
2bda0e17 | 54 | |
704c499e VZ |
55 | #if wxUSE_MSGBOX_HOOK |
56 | ||
1d89da8a VZ |
57 | // there can potentially be one message box per thread so we use a hash map |
58 | // with thread ids as keys and (currently shown) message boxes as values | |
59 | // | |
60 | // TODO: replace this with wxTLS once it's available | |
61 | WX_DECLARE_HASH_MAP(unsigned long, wxMessageDialog *, | |
62 | wxIntegerHash, wxIntegerEqual, | |
63 | wxMessageDialogMap); | |
64 | ||
65 | namespace | |
66 | { | |
67 | ||
68 | wxMessageDialogMap& HookMap() | |
69 | { | |
70 | static wxMessageDialogMap s_Map; | |
71 | ||
72 | return s_Map; | |
73 | } | |
74 | ||
75 | } // anonymous namespace | |
76 | ||
77 | /* static */ | |
78 | WXLRESULT wxCALLBACK | |
79 | wxMessageDialog::HookFunction(int code, WXWPARAM wParam, WXLPARAM lParam) | |
80 | { | |
81 | // Find the thread-local instance of wxMessageDialog | |
82 | const DWORD tid = ::GetCurrentThreadId(); | |
83 | wxMessageDialogMap::iterator node = HookMap().find(tid); | |
84 | wxCHECK_MSG( node != HookMap().end(), false, | |
85 | wxT("bogus thread id in wxMessageDialog::Hook") ); | |
86 | ||
87 | wxMessageDialog * const wnd = node->second; | |
88 | ||
89 | const HHOOK hhook = (HHOOK)wnd->m_hook; | |
90 | const LRESULT rc = ::CallNextHookEx(hhook, code, wParam, lParam); | |
91 | ||
23e00c55 VZ |
92 | if ( code == HCBT_ACTIVATE ) |
93 | { | |
94 | // we won't need this hook any longer | |
95 | ::UnhookWindowsHookEx(hhook); | |
96 | wnd->m_hook = NULL; | |
97 | HookMap().erase(tid); | |
98 | ||
99 | wnd->SetHWND((HWND)wParam); | |
100 | ||
101 | // centre the message box on its parent if requested | |
102 | if ( wnd->GetMessageDialogStyle() & wxCENTER ) | |
103 | wnd->Center(); // center on parent | |
104 | //else: default behaviour, center on screen | |
105 | ||
106 | // also update the labels if necessary | |
107 | if ( wnd->HasCustomLabels() ) | |
108 | wnd->AdjustButtonLabels(); | |
109 | ||
110 | // there seems to be no reason to leave it set | |
111 | wnd->SetHWND(NULL); | |
112 | } | |
113 | ||
114 | return rc; | |
115 | } | |
116 | ||
117 | namespace | |
118 | { | |
119 | ||
120 | // helper of AdjustButtonLabels(): set window position expressed in screen | |
121 | // coordinates, whether the window is child or top level | |
122 | void MoveWindowToScreenRect(HWND hwnd, RECT rc) | |
123 | { | |
124 | if ( const HWND hwndParent = ::GetAncestor(hwnd, GA_PARENT) ) | |
125 | { | |
126 | // map to parent window coordinates (notice that a RECT is laid out as | |
127 | // 2 consecutive POINTs) | |
128 | ::MapWindowPoints(HWND_DESKTOP, hwndParent, | |
129 | reinterpret_cast<POINT *>(&rc), 2); | |
130 | } | |
131 | ||
132 | ::MoveWindow(hwnd, | |
133 | rc.left, rc.top, | |
134 | rc.right - rc.left, rc.bottom - rc.top, | |
135 | FALSE); | |
136 | } | |
137 | ||
138 | // helper of AdjustButtonLabels(): move the given window by dx | |
139 | // | |
140 | // works for both child and top level windows | |
141 | void OffsetWindow(HWND hwnd, int dx) | |
142 | { | |
143 | RECT rc = wxGetWindowRect(hwnd); | |
144 | ||
145 | rc.left += dx; | |
146 | rc.right += dx; | |
147 | ||
148 | MoveWindowToScreenRect(hwnd, rc); | |
149 | } | |
150 | ||
151 | } // anonymous namespace | |
152 | ||
153 | void wxMessageDialog::AdjustButtonLabels() | |
154 | { | |
155 | // changing the button labels is the easy part but we also need to ensure | |
156 | // that the buttons are big enough for the label strings and increase their | |
157 | // size (and hence the size of the message box itself) if they are not | |
158 | ||
159 | // TODO-RTL: check whether this works correctly in RTL | |
160 | ||
161 | // the order in this array is the one in which buttons appear in the | |
162 | // message box | |
163 | const static struct ButtonAccessors | |
1d89da8a | 164 | { |
23e00c55 VZ |
165 | int id; |
166 | wxString (wxMessageDialog::*getter)() const; | |
167 | } | |
168 | buttons[] = | |
169 | { | |
170 | { IDYES, &wxMessageDialog::GetYesLabel }, | |
171 | { IDNO, &wxMessageDialog::GetNoLabel }, | |
172 | { IDOK, &wxMessageDialog::GetOKLabel }, | |
173 | { IDCANCEL, &wxMessageDialog::GetCancelLabel }, | |
174 | }; | |
175 | ||
176 | // this contains the amount by which we increased the message box width | |
177 | int dx = 0; | |
1d89da8a | 178 | |
23e00c55 VZ |
179 | const NONCLIENTMETRICS& ncm = wxMSWImpl::GetNonClientMetrics(); |
180 | const wxFont fontMsgBox(wxNativeFontInfo(ncm.lfMessageFont)); | |
181 | ||
182 | // we want to use this font in GetTextExtent() calls below but we don't | |
183 | // want to send WM_SETFONT to the message box, who knows how is it going to | |
184 | // react to it (right now it doesn't seem to do anything but what if this | |
185 | // changes) | |
186 | wxWindowBase::SetFont(fontMsgBox); | |
187 | ||
188 | for ( unsigned n = 0; n < WXSIZEOF(buttons); n++ ) | |
189 | { | |
190 | const HWND hwndBtn = ::GetDlgItem(GetHwnd(), buttons[n].id); | |
191 | if ( !hwndBtn ) | |
192 | continue; // it's ok, not all buttons are always present | |
193 | ||
194 | const wxString label = (this->*buttons[n].getter)(); | |
195 | const wxSize sizeLabel = wxWindowBase::GetTextExtent(label); | |
196 | ||
197 | // check if the button is big enough for this label | |
198 | RECT rc = wxGetWindowRect(hwndBtn); | |
199 | const int widthOld = rc.right - rc.left; | |
200 | const int widthNew = wxMSWButton::GetFittingSize(this, sizeLabel).x; | |
201 | const int dw = widthNew - widthOld; | |
202 | if ( dw > 0 ) | |
1d89da8a | 203 | { |
23e00c55 VZ |
204 | // we need to resize the button |
205 | rc.right += dw; | |
206 | MoveWindowToScreenRect(hwndBtn, rc); | |
1d89da8a | 207 | |
23e00c55 VZ |
208 | // and also move all the other buttons |
209 | for ( unsigned m = n + 1; m < WXSIZEOF(buttons); m++ ) | |
1d89da8a | 210 | { |
23e00c55 VZ |
211 | const HWND hwndBtnNext = ::GetDlgItem(GetHwnd(), buttons[m].id); |
212 | if ( hwndBtnNext ) | |
213 | OffsetWindow(hwndBtnNext, dw); | |
1d89da8a | 214 | } |
23e00c55 VZ |
215 | |
216 | dx += dw; | |
1d89da8a | 217 | } |
23e00c55 VZ |
218 | |
219 | ::SetWindowText(hwndBtn, label.wx_str()); | |
1d89da8a VZ |
220 | } |
221 | ||
23e00c55 VZ |
222 | |
223 | // resize the message box itself if needed | |
224 | if ( dx ) | |
225 | OffsetWindow(GetHwnd(), dx); | |
226 | ||
227 | // surprisingly, we don't need to resize the static text control, it seems | |
228 | // to adjust itself to the new size, at least under Windows 2003 | |
229 | // (TODO: test if this happens on older Windows versions) | |
1d89da8a VZ |
230 | } |
231 | ||
704c499e VZ |
232 | #endif // wxUSE_MSGBOX_HOOK |
233 | ||
234 | ||
0d7ea902 | 235 | int wxMessageDialog::ShowModal() |
2bda0e17 | 236 | { |
a543e3ce | 237 | if ( !wxTheApp->GetTopWindow() ) |
0d7ea902 VZ |
238 | { |
239 | // when the message box is shown from wxApp::OnInit() (i.e. before the | |
240 | // message loop is entered), this must be done or the next message box | |
241 | // will never be shown - just try putting 2 calls to wxMessageBox() in | |
242 | // OnInit() to see it | |
243 | while ( wxTheApp->Pending() ) | |
244 | wxTheApp->Dispatch(); | |
245 | } | |
93c95e18 | 246 | |
b8505921 | 247 | // use the top level window as parent if none specified |
a543e3ce VZ |
248 | if ( !m_parent ) |
249 | m_parent = FindSuitableParent(); | |
250 | HWND hWnd = m_parent ? GetHwndOf(m_parent) : NULL; | |
b8505921 VZ |
251 | |
252 | // translate wx style in MSW | |
0d7ea902 | 253 | unsigned int msStyle = MB_OK; |
e5b50758 WS |
254 | const long wxStyle = GetMessageDialogStyle(); |
255 | if (wxStyle & wxYES_NO) | |
0d7ea902 | 256 | { |
3180bc0e | 257 | #if !(defined(__SMARTPHONE__) && defined(__WXWINCE__)) |
e5b50758 | 258 | if (wxStyle & wxCANCEL) |
0d7ea902 VZ |
259 | msStyle = MB_YESNOCANCEL; |
260 | else | |
3180bc0e | 261 | #endif // !(__SMARTPHONE__ && __WXWINCE__) |
0d7ea902 | 262 | msStyle = MB_YESNO; |
93c95e18 | 263 | |
e5b50758 | 264 | if (wxStyle & wxNO_DEFAULT) |
0d7ea902 VZ |
265 | msStyle |= MB_DEFBUTTON2; |
266 | } | |
267 | ||
e5b50758 | 268 | if (wxStyle & wxOK) |
0d7ea902 | 269 | { |
e5b50758 | 270 | if (wxStyle & wxCANCEL) |
0d7ea902 VZ |
271 | msStyle = MB_OKCANCEL; |
272 | else | |
273 | msStyle = MB_OK; | |
274 | } | |
e5b50758 | 275 | if (wxStyle & wxICON_EXCLAMATION) |
0d7ea902 | 276 | msStyle |= MB_ICONEXCLAMATION; |
e5b50758 | 277 | else if (wxStyle & wxICON_HAND) |
0d7ea902 | 278 | msStyle |= MB_ICONHAND; |
e5b50758 | 279 | else if (wxStyle & wxICON_INFORMATION) |
0d7ea902 | 280 | msStyle |= MB_ICONINFORMATION; |
e5b50758 | 281 | else if (wxStyle & wxICON_QUESTION) |
0d7ea902 | 282 | msStyle |= MB_ICONQUESTION; |
2bda0e17 | 283 | |
e5b50758 | 284 | if ( wxStyle & wxSTAY_ON_TOP ) |
a7fd7c78 VZ |
285 | msStyle |= MB_TOPMOST; |
286 | ||
08a58133 | 287 | #ifndef __WXWINCE__ |
978af864 VZ |
288 | if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft ) |
289 | msStyle |= MB_RTLREADING | MB_RIGHT; | |
08a58133 | 290 | #endif |
978af864 | 291 | |
0d7ea902 VZ |
292 | if (hWnd) |
293 | msStyle |= MB_APPLMODAL; | |
294 | else | |
295 | msStyle |= MB_TASKMODAL; | |
93c95e18 | 296 | |
12e424d2 VZ |
297 | // per MSDN documentation for MessageBox() we can prefix the message with 2 |
298 | // right-to-left mark characters to tell the function to use RTL layout | |
299 | // (unfortunately this only works in Unicode builds) | |
2afb9e16 | 300 | wxString message = GetFullMessage(); |
12e424d2 VZ |
301 | #if wxUSE_UNICODE |
302 | if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft ) | |
303 | { | |
304 | // NB: not all compilers support \u escapes | |
305 | static const wchar_t wchRLM = 0x200f; | |
306 | message.Prepend(wxString(wchRLM, 2)); | |
307 | } | |
308 | #endif // wxUSE_UNICODE | |
309 | ||
704c499e | 310 | #if wxUSE_MSGBOX_HOOK |
1d89da8a | 311 | // install the hook if we need to position the dialog in a non-default way |
23e00c55 VZ |
312 | // or change the labels |
313 | if ( (wxStyle & wxCENTER) || HasCustomLabels() ) | |
1d89da8a VZ |
314 | { |
315 | const DWORD tid = ::GetCurrentThreadId(); | |
23e00c55 | 316 | m_hook = ::SetWindowsHookEx(WH_CBT, |
1d89da8a VZ |
317 | &wxMessageDialog::HookFunction, NULL, tid); |
318 | HookMap()[tid] = this; | |
319 | } | |
704c499e | 320 | #endif // wxUSE_MSGBOX_HOOK |
1d89da8a | 321 | |
b8505921 | 322 | // do show the dialog |
e0a050e3 | 323 | int msAns = MessageBox(hWnd, message.wx_str(), m_caption.wx_str(), msStyle); |
b8505921 | 324 | int ans; |
0d7ea902 VZ |
325 | switch (msAns) |
326 | { | |
b8505921 VZ |
327 | default: |
328 | wxFAIL_MSG(_T("unexpected ::MessageBox() return code")); | |
329 | // fall through | |
330 | ||
0d7ea902 VZ |
331 | case IDCANCEL: |
332 | ans = wxID_CANCEL; | |
333 | break; | |
334 | case IDOK: | |
335 | ans = wxID_OK; | |
336 | break; | |
337 | case IDYES: | |
338 | ans = wxID_YES; | |
339 | break; | |
340 | case IDNO: | |
341 | ans = wxID_NO; | |
342 | break; | |
343 | } | |
344 | return ans; | |
2bda0e17 | 345 | } |
a8ff046b VZ |
346 | |
347 | #endif // wxUSE_MSGDLG |