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