]> git.saurik.com Git - wxWidgets.git/blame - src/msw/msgdlg.cpp
Ensure that validators work even in presence of pushed event handlers.
[wxWidgets.git] / src / msw / msgdlg.cpp
CommitLineData
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"
42c097b8 33 #include "wx/intl.h"
0d7ea902
VZ
34 #include "wx/utils.h"
35 #include "wx/dialog.h"
704c499e
VZ
36 #if wxUSE_MSGBOX_HOOK
37 #include "wx/hashmap.h"
38 #endif
2bda0e17
KB
39#endif
40
41#include "wx/msw/private.h"
23e00c55
VZ
42#include "wx/msw/private/button.h"
43#include "wx/msw/private/metrics.h"
44
45#if wxUSE_MSGBOX_HOOK
46 #include "wx/fontutil.h"
4b02d42e
VZ
47 #include "wx/textbuf.h"
48 #include "wx/display.h"
23e00c55 49#endif
2bda0e17 50
676d6550
JS
51// For MB_TASKMODAL
52#ifdef __WXWINCE__
23e00c55 53 #include "wx/msw/wince/missing.h"
676d6550
JS
54#endif
55
2bda0e17 56IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
2bda0e17 57
704c499e
VZ
58#if wxUSE_MSGBOX_HOOK
59
1d89da8a
VZ
60// there can potentially be one message box per thread so we use a hash map
61// with thread ids as keys and (currently shown) message boxes as values
62//
63// TODO: replace this with wxTLS once it's available
64WX_DECLARE_HASH_MAP(unsigned long, wxMessageDialog *,
65 wxIntegerHash, wxIntegerEqual,
66 wxMessageDialogMap);
67
4b02d42e
VZ
68// the order in this array is the one in which buttons appear in the
69// message box
70const wxMessageDialog::ButtonAccessors wxMessageDialog::ms_buttons[] =
71{
72 { IDYES, &wxMessageDialog::GetYesLabel },
73 { IDNO, &wxMessageDialog::GetNoLabel },
74 { IDOK, &wxMessageDialog::GetOKLabel },
75 { IDCANCEL, &wxMessageDialog::GetCancelLabel },
76};
77
1d89da8a
VZ
78namespace
79{
80
81wxMessageDialogMap& HookMap()
82{
83 static wxMessageDialogMap s_Map;
84
85 return s_Map;
86}
87
4b02d42e
VZ
88/*
89 All this code is used for adjusting the message box layout when we mess
90 with its contents. It's rather complicated because we try hard to avoid
91 assuming much about the standard layout details and so, instead of just
92 laying out everything ourselves (which would have been so much simpler!)
93 we try to only modify the existing controls positions by offsetting them
94 from their default ones in the hope that this will continue to work with
95 the future Windows versions.
96 */
97
98// convert the given RECT from screen to client coordinates in place
99void ScreenRectToClient(HWND hwnd, RECT& rc)
100{
101 // map from desktop (i.e. screen) coordinates to ones of this window
102 //
103 // notice that a RECT is laid out as 2 consecutive POINTs so the cast is
104 // valid
105 ::MapWindowPoints(HWND_DESKTOP, hwnd, reinterpret_cast<POINT *>(&rc), 2);
106}
107
108// set window position to the given rect
109inline void SetWindowRect(HWND hwnd, const RECT& rc)
110{
111 ::MoveWindow(hwnd,
112 rc.left, rc.top,
113 rc.right - rc.left, rc.bottom - rc.top,
114 FALSE);
115}
116
117// set window position expressed in screen coordinates, whether the window is
118// child or top level
119void MoveWindowToScreenRect(HWND hwnd, RECT rc)
120{
121 ScreenRectToClient(::GetParent(hwnd), rc);
122
123 SetWindowRect(hwnd, rc);
124}
125
126// helper of AdjustButtonLabels(): move the given window by dx
127//
128// works for both child and top level windows
129void OffsetWindow(HWND hwnd, int dx)
130{
131 RECT rc = wxGetWindowRect(hwnd);
132
133 rc.left += dx;
134 rc.right += dx;
135
136 MoveWindowToScreenRect(hwnd, rc);
137}
138
1d89da8a
VZ
139} // anonymous namespace
140
141/* static */
142WXLRESULT wxCALLBACK
143wxMessageDialog::HookFunction(int code, WXWPARAM wParam, WXLPARAM lParam)
144{
145 // Find the thread-local instance of wxMessageDialog
146 const DWORD tid = ::GetCurrentThreadId();
147 wxMessageDialogMap::iterator node = HookMap().find(tid);
148 wxCHECK_MSG( node != HookMap().end(), false,
149 wxT("bogus thread id in wxMessageDialog::Hook") );
150
151 wxMessageDialog * const wnd = node->second;
152
153 const HHOOK hhook = (HHOOK)wnd->m_hook;
154 const LRESULT rc = ::CallNextHookEx(hhook, code, wParam, lParam);
155
23e00c55
VZ
156 if ( code == HCBT_ACTIVATE )
157 {
158 // we won't need this hook any longer
159 ::UnhookWindowsHookEx(hhook);
160 wnd->m_hook = NULL;
161 HookMap().erase(tid);
162
163 wnd->SetHWND((HWND)wParam);
164
4b02d42e
VZ
165 // replace the static text with an edit control if the message box is
166 // too big to fit the display
167 wnd->ReplaceStaticWithEdit();
168
b6bcaaca
VZ
169 // update the labels if necessary: we need to do it before centering
170 // the dialog as this can change its size
171 if ( wnd->HasCustomLabels() )
172 wnd->AdjustButtonLabels();
173
23e00c55
VZ
174 // centre the message box on its parent if requested
175 if ( wnd->GetMessageDialogStyle() & wxCENTER )
176 wnd->Center(); // center on parent
177 //else: default behaviour, center on screen
178
23e00c55
VZ
179 // there seems to be no reason to leave it set
180 wnd->SetHWND(NULL);
181 }
182
183 return rc;
184}
185
4b02d42e 186void wxMessageDialog::ReplaceStaticWithEdit()
23e00c55 187{
4b02d42e
VZ
188 // check if the message box fits the display
189 int nDisplay = wxDisplay::GetFromWindow(this);
190 if ( nDisplay == wxNOT_FOUND )
191 nDisplay = 0;
192 const wxRect rectDisplay = wxDisplay(nDisplay).GetClientArea();
23e00c55 193
4b02d42e 194 if ( rectDisplay.Contains(GetRect()) )
23e00c55 195 {
4b02d42e
VZ
196 // nothing to do
197 return;
23e00c55
VZ
198 }
199
23e00c55 200
4b02d42e
VZ
201 // find the static control to replace: normally there are two of them, the
202 // icon and the text itself so search for all of them and ignore the icon
203 // ones
9a83f860 204 HWND hwndStatic = ::FindWindowEx(GetHwnd(), NULL, wxT("STATIC"), NULL);
4b02d42e 205 if ( ::GetWindowLong(hwndStatic, GWL_STYLE) & SS_ICON )
9a83f860 206 hwndStatic = ::FindWindowEx(GetHwnd(), hwndStatic, wxT("STATIC"), NULL);
23e00c55 207
4b02d42e
VZ
208 if ( !hwndStatic )
209 {
210 wxLogDebug("Failed to find the static text control in message box.");
211 return;
212 }
23e00c55 213
4b02d42e
VZ
214 // set the right font for GetCharHeight() call below
215 wxWindowBase::SetFont(GetMessageFont());
216
217 // put the new edit control at the same place
218 RECT rc = wxGetWindowRect(hwndStatic);
219 ScreenRectToClient(GetHwnd(), rc);
220
221 // but make it less tall so that the message box fits on the screen: we try
222 // to make the message box take no more than 7/8 of the screen to leave
223 // some space above and below it
224 const int hText = (7*rectDisplay.height)/8 -
225 (
226 2*::GetSystemMetrics(SM_CYFIXEDFRAME) +
227 ::GetSystemMetrics(SM_CYCAPTION) +
228 5*GetCharHeight() // buttons + margins
229 );
230 const int dh = (rc.bottom - rc.top) - hText; // vertical space we save
231 rc.bottom -= dh;
232
233 // and it also must be wider as it needs a vertical scrollbar (in order
234 // to preserve the word wrap, otherwise the number of lines would change
235 // and we want the control to look as similar as possible to the original)
236 //
237 // NB: you would have thought that 2*SM_CXEDGE would be enough but it
238 // isn't, somehow, and the text control breaks lines differently from
239 // the static one so fudge by adding some extra space
240 const int dw = ::GetSystemMetrics(SM_CXVSCROLL) +
241 4*::GetSystemMetrics(SM_CXEDGE);
242 rc.right += dw;
243
244
245 // chop of the trailing new line(s) from the message box text, they are
246 // ignored by the static control but result in extra lines and hence extra
247 // scrollbar position in the edit one
248 wxString text(wxGetWindowText(hwndStatic));
d3019e4d 249 for ( wxString::reverse_iterator i = text.rbegin(); i != text.rend(); ++i )
4b02d42e
VZ
250 {
251 if ( *i != '\n' )
252 {
d3019e4d
VZ
253 // found last non-newline char, remove everything after it and stop
254 text.erase(i.base() + 1, text.end());
4b02d42e
VZ
255 break;
256 }
257 }
23e00c55 258
4b02d42e
VZ
259 // do create the new control
260 HWND hwndEdit = ::CreateWindow
261 (
9a83f860 262 wxT("EDIT"),
cc5e20f8 263 wxTextBuffer::Translate(text).wx_str(),
4b02d42e
VZ
264 WS_CHILD | WS_VSCROLL | WS_VISIBLE |
265 ES_MULTILINE | ES_READONLY | ES_AUTOVSCROLL,
266 rc.left, rc.top,
267 rc.right - rc.left, rc.bottom - rc.top,
268 GetHwnd(),
269 NULL,
5431e4a6 270 wxGetInstance(),
4b02d42e
VZ
271 NULL
272 );
273
274 if ( !hwndEdit )
275 {
276 wxLogDebug("Creation of replacement edit control failed in message box");
277 return;
278 }
23e00c55 279
4b02d42e
VZ
280 // copy the font from the original control
281 LRESULT hfont = ::SendMessage(hwndStatic, WM_GETFONT, 0, 0);
282 ::SendMessage(hwndEdit, WM_SETFONT, hfont, 0);
283
284 // and get rid of it
285 ::DestroyWindow(hwndStatic);
23e00c55 286
23e00c55 287
4b02d42e
VZ
288 // shrink and centre the message box vertically and widen it box to account
289 // for the extra scrollbar
290 RECT rcBox = wxGetWindowRect(GetHwnd());
291 const int hMsgBox = rcBox.bottom - rcBox.top - dh;
292 rcBox.top = (rectDisplay.height - hMsgBox)/2;
293 rcBox.bottom = rcBox.top + hMsgBox + (rectDisplay.height - hMsgBox)%2;
294 rcBox.left -= dw/2;
295 rcBox.right += dw - dw/2;
296 SetWindowRect(GetHwnd(), rcBox);
297
298 // and adjust all the buttons positions
299 for ( unsigned n = 0; n < WXSIZEOF(ms_buttons); n++ )
1d89da8a 300 {
4b02d42e
VZ
301 const HWND hwndBtn = ::GetDlgItem(GetHwnd(), ms_buttons[n].id);
302 if ( !hwndBtn )
303 continue; // it's ok, not all buttons are always present
304
305 RECT rc = wxGetWindowRect(hwndBtn);
306 rc.top -= dh;
307 rc.bottom -= dh;
308 rc.left += dw/2;
309 rc.right += dw/2;
310 MoveWindowToScreenRect(hwndBtn, rc);
23e00c55 311 }
4b02d42e 312}
23e00c55 313
4b02d42e
VZ
314void wxMessageDialog::AdjustButtonLabels()
315{
316 // changing the button labels is the easy part but we also need to ensure
317 // that the buttons are big enough for the label strings and increase their
318 // size (and maybe the size of the message box itself) if they are not
1d89da8a 319
4b02d42e 320 // TODO-RTL: check whether this works correctly in RTL
23e00c55
VZ
321
322 // we want to use this font in GetTextExtent() calls below but we don't
323 // want to send WM_SETFONT to the message box, who knows how is it going to
324 // react to it (right now it doesn't seem to do anything but what if this
325 // changes)
4b02d42e
VZ
326 wxWindowBase::SetFont(GetMessageFont());
327
328 // first iteration: find the widest button and update the buttons labels
329 int wBtnOld = 0, // current buttons width
330 wBtnNew = 0; // required new buttons width
331 RECT rcBtn; // stores the button height and y positions
332 unsigned numButtons = 0; // total number of buttons in the message box
333 unsigned n;
334 for ( n = 0; n < WXSIZEOF(ms_buttons); n++ )
23e00c55 335 {
4b02d42e 336 const HWND hwndBtn = ::GetDlgItem(GetHwnd(), ms_buttons[n].id);
23e00c55
VZ
337 if ( !hwndBtn )
338 continue; // it's ok, not all buttons are always present
339
4b02d42e
VZ
340 numButtons++;
341
342 const wxString label = (this->*ms_buttons[n].getter)();
23e00c55
VZ
343 const wxSize sizeLabel = wxWindowBase::GetTextExtent(label);
344
345 // check if the button is big enough for this label
4b02d42e
VZ
346 const RECT rc = wxGetWindowRect(hwndBtn);
347 if ( !wBtnOld )
348 {
349 // initialize wBtnOld using the first button width, all the other
350 // ones should have the same one
351 wBtnOld = rc.right - rc.left;
352
353 rcBtn = rc; // remember for use below when we reposition the buttons
354 }
355 else
1d89da8a 356 {
4b02d42e
VZ
357 wxASSERT_MSG( wBtnOld == rc.right - rc.left,
358 "all buttons are supposed to be of same width" );
1d89da8a 359 }
23e00c55 360
4b02d42e
VZ
361 const int widthNeeded = wxMSWButton::GetFittingSize(this, sizeLabel).x;
362 if ( widthNeeded > wBtnNew )
363 wBtnNew = widthNeeded;
364
23e00c55 365 ::SetWindowText(hwndBtn, label.wx_str());
1d89da8a
VZ
366 }
367
4b02d42e
VZ
368 if ( wBtnNew <= wBtnOld )
369 {
370 // all buttons fit, nothing else to do
371 return;
372 }
373
374 // resize the message box to be wider if needed
375 const int wBoxOld = wxGetClientRect(GetHwnd()).right;
376
377 const int CHAR_WIDTH = GetCharWidth();
378 const int MARGIN_OUTER = 2*CHAR_WIDTH; // margin between box and buttons
379 const int MARGIN_INNER = CHAR_WIDTH; // margin between buttons
380
381 RECT rcBox = wxGetWindowRect(GetHwnd());
382
383 const int wAllButtons = numButtons*(wBtnNew + MARGIN_INNER) - MARGIN_INNER;
384 int wBoxNew = 2*MARGIN_OUTER + wAllButtons;
385 if ( wBoxNew > wBoxOld )
386 {
387 const int dw = wBoxNew - wBoxOld;
388 rcBox.left -= dw/2;
389 rcBox.right += dw - dw/2;
390
391 SetWindowRect(GetHwnd(), rcBox);
392
393 // surprisingly, we don't need to resize the static text control, it
394 // seems to adjust itself to the new size, at least under Windows 2003
395 // (TODO: test if this happens on older Windows versions)
396 }
397 else // the current width is big enough
398 {
399 wBoxNew = wBoxOld;
400 }
401
402
403 // finally position all buttons
23e00c55 404
4b02d42e
VZ
405 // notice that we have to take into account the difference between window
406 // and client width
407 rcBtn.left = (rcBox.left + rcBox.right - wxGetClientRect(GetHwnd()).right +
408 wBoxNew - wAllButtons) / 2;
409 rcBtn.right = rcBtn.left + wBtnNew;
23e00c55 410
4b02d42e
VZ
411 for ( n = 0; n < WXSIZEOF(ms_buttons); n++ )
412 {
413 const HWND hwndBtn = ::GetDlgItem(GetHwnd(), ms_buttons[n].id);
414 if ( !hwndBtn )
415 continue;
416
417 MoveWindowToScreenRect(hwndBtn, rcBtn);
418
419 rcBtn.left += wBtnNew + MARGIN_INNER;
420 rcBtn.right += wBtnNew + MARGIN_INNER;
421 }
1d89da8a
VZ
422}
423
704c499e
VZ
424#endif // wxUSE_MSGBOX_HOOK
425
4b02d42e
VZ
426/* static */
427wxFont wxMessageDialog::GetMessageFont()
428{
429 const NONCLIENTMETRICS& ncm = wxMSWImpl::GetNonClientMetrics();
430 return wxNativeFontInfo(ncm.lfMessageFont);
431}
704c499e 432
0d7ea902 433int wxMessageDialog::ShowModal()
2bda0e17 434{
a543e3ce 435 if ( !wxTheApp->GetTopWindow() )
0d7ea902
VZ
436 {
437 // when the message box is shown from wxApp::OnInit() (i.e. before the
438 // message loop is entered), this must be done or the next message box
439 // will never be shown - just try putting 2 calls to wxMessageBox() in
440 // OnInit() to see it
441 while ( wxTheApp->Pending() )
442 wxTheApp->Dispatch();
443 }
93c95e18 444
b8505921 445 // use the top level window as parent if none specified
cdc48273 446 m_parent = GetParentForModalDialog();
a543e3ce 447 HWND hWnd = m_parent ? GetHwndOf(m_parent) : NULL;
b8505921 448
42c097b8
VZ
449#if wxUSE_INTL
450 // native message box always uses the current user locale but the program
451 // may be using a different one and in this case we need to manually
452 // translate the button labels to avoid mismatch between the language of
453 // the message box text and its buttons
454 wxLocale * const loc = wxGetLocale();
455 if ( loc && loc->GetLanguage() != wxLocale::GetSystemLanguage() )
456 {
457 if ( m_dialogStyle & wxYES_NO )
458 {
459 // use the strings with mnemonics here as the native message box
460 // does
461 SetYesNoLabels(_("&Yes"), _("&No"));
462 }
463
464 // we may or not have the Ok/Cancel buttons but either we do have them
465 // or we already made the labels custom because we called
466 // SetYesNoLabels() above so doing this does no harm -- and is
467 // necessary in wxYES_NO | wxCANCEL case
468 //
469 // note that we don't use mnemonics here for consistency with the
470 // native message box (which probably doesn't use them because
471 // Enter/Esc keys can be already used to dismiss the message box
472 // using keyboard)
473 SetOKCancelLabels(_("OK"), _("Cancel"));
474 }
475#endif // wxUSE_INTL
476
b8505921 477 // translate wx style in MSW
f45d6ade 478 unsigned int msStyle;
e5b50758 479 const long wxStyle = GetMessageDialogStyle();
f45d6ade 480 if ( wxStyle & wxYES_NO )
0d7ea902 481 {
3180bc0e 482#if !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
e5b50758 483 if (wxStyle & wxCANCEL)
0d7ea902
VZ
484 msStyle = MB_YESNOCANCEL;
485 else
3180bc0e 486#endif // !(__SMARTPHONE__ && __WXWINCE__)
0d7ea902 487 msStyle = MB_YESNO;
93c95e18 488
f45d6ade 489 if ( wxStyle & wxNO_DEFAULT )
0d7ea902 490 msStyle |= MB_DEFBUTTON2;
f45d6ade
VZ
491 else if ( wxStyle & wxCANCEL_DEFAULT )
492 msStyle |= MB_DEFBUTTON3;
0d7ea902 493 }
f45d6ade 494 else // without Yes/No we're going to have an OK button
0d7ea902 495 {
f45d6ade
VZ
496 if ( wxStyle & wxCANCEL )
497 {
0d7ea902 498 msStyle = MB_OKCANCEL;
f45d6ade
VZ
499
500 if ( wxStyle & wxCANCEL_DEFAULT )
501 msStyle |= MB_DEFBUTTON2;
502 }
503 else // just "OK"
504 {
0d7ea902 505 msStyle = MB_OK;
f45d6ade 506 }
0d7ea902 507 }
f45d6ade 508
b76b3d28 509 // set the icon style
a4578b0c
VZ
510 switch ( GetEffectiveIcon() )
511 {
512 case wxICON_ERROR:
513 msStyle |= MB_ICONHAND;
514 break;
515
516 case wxICON_WARNING:
517 msStyle |= MB_ICONEXCLAMATION;
518 break;
519
520 case wxICON_QUESTION:
521 msStyle |= MB_ICONQUESTION;
522 break;
523
524 case wxICON_INFORMATION:
525 msStyle |= MB_ICONINFORMATION;
526 break;
527 }
2bda0e17 528
e5b50758 529 if ( wxStyle & wxSTAY_ON_TOP )
a7fd7c78
VZ
530 msStyle |= MB_TOPMOST;
531
08a58133 532#ifndef __WXWINCE__
978af864
VZ
533 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
534 msStyle |= MB_RTLREADING | MB_RIGHT;
08a58133 535#endif
978af864 536
0d7ea902
VZ
537 if (hWnd)
538 msStyle |= MB_APPLMODAL;
539 else
540 msStyle |= MB_TASKMODAL;
93c95e18 541
12e424d2
VZ
542 // per MSDN documentation for MessageBox() we can prefix the message with 2
543 // right-to-left mark characters to tell the function to use RTL layout
544 // (unfortunately this only works in Unicode builds)
2afb9e16 545 wxString message = GetFullMessage();
12e424d2
VZ
546#if wxUSE_UNICODE
547 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
548 {
549 // NB: not all compilers support \u escapes
550 static const wchar_t wchRLM = 0x200f;
551 message.Prepend(wxString(wchRLM, 2));
552 }
553#endif // wxUSE_UNICODE
554
704c499e 555#if wxUSE_MSGBOX_HOOK
4b02d42e
VZ
556 // install the hook in any case as we don't know in advance if the message
557 // box is not going to be too big (requiring the replacement of the static
558 // control with an edit one)
559 const DWORD tid = ::GetCurrentThreadId();
560 m_hook = ::SetWindowsHookEx(WH_CBT,
561 &wxMessageDialog::HookFunction, NULL, tid);
562 HookMap()[tid] = this;
704c499e 563#endif // wxUSE_MSGBOX_HOOK
1d89da8a 564
b8505921 565 // do show the dialog
e0a050e3 566 int msAns = MessageBox(hWnd, message.wx_str(), m_caption.wx_str(), msStyle);
b8505921 567 int ans;
0d7ea902
VZ
568 switch (msAns)
569 {
b8505921 570 default:
9a83f860 571 wxFAIL_MSG(wxT("unexpected ::MessageBox() return code"));
b8505921
VZ
572 // fall through
573
0d7ea902
VZ
574 case IDCANCEL:
575 ans = wxID_CANCEL;
576 break;
577 case IDOK:
578 ans = wxID_OK;
579 break;
580 case IDYES:
581 ans = wxID_YES;
582 break;
583 case IDNO:
584 ans = wxID_NO;
585 break;
586 }
587 return ans;
2bda0e17 588}
a8ff046b
VZ
589
590#endif // wxUSE_MSGDLG