implement support for custom button labels in wxMessageBox under MSW; refactor the...
[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 // 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
164 {
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;
178
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 )
203 {
204 // we need to resize the button
205 rc.right += dw;
206 MoveWindowToScreenRect(hwndBtn, rc);
207
208 // and also move all the other buttons
209 for ( unsigned m = n + 1; m < WXSIZEOF(buttons); m++ )
210 {
211 const HWND hwndBtnNext = ::GetDlgItem(GetHwnd(), buttons[m].id);
212 if ( hwndBtnNext )
213 OffsetWindow(hwndBtnNext, dw);
214 }
215
216 dx += dw;
217 }
218
219 ::SetWindowText(hwndBtn, label.wx_str());
220 }
221
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)
230 }
231
232 #endif // wxUSE_MSGBOX_HOOK
233
234
235 int wxMessageDialog::ShowModal()
236 {
237 if ( !wxTheApp->GetTopWindow() )
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 }
246
247 // use the top level window as parent if none specified
248 if ( !m_parent )
249 m_parent = FindSuitableParent();
250 HWND hWnd = m_parent ? GetHwndOf(m_parent) : NULL;
251
252 // translate wx style in MSW
253 unsigned int msStyle = MB_OK;
254 const long wxStyle = GetMessageDialogStyle();
255 if (wxStyle & wxYES_NO)
256 {
257 #if !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
258 if (wxStyle & wxCANCEL)
259 msStyle = MB_YESNOCANCEL;
260 else
261 #endif // !(__SMARTPHONE__ && __WXWINCE__)
262 msStyle = MB_YESNO;
263
264 if (wxStyle & wxNO_DEFAULT)
265 msStyle |= MB_DEFBUTTON2;
266 }
267
268 if (wxStyle & wxOK)
269 {
270 if (wxStyle & wxCANCEL)
271 msStyle = MB_OKCANCEL;
272 else
273 msStyle = MB_OK;
274 }
275 if (wxStyle & wxICON_EXCLAMATION)
276 msStyle |= MB_ICONEXCLAMATION;
277 else if (wxStyle & wxICON_HAND)
278 msStyle |= MB_ICONHAND;
279 else if (wxStyle & wxICON_INFORMATION)
280 msStyle |= MB_ICONINFORMATION;
281 else if (wxStyle & wxICON_QUESTION)
282 msStyle |= MB_ICONQUESTION;
283
284 if ( wxStyle & wxSTAY_ON_TOP )
285 msStyle |= MB_TOPMOST;
286
287 #ifndef __WXWINCE__
288 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
289 msStyle |= MB_RTLREADING | MB_RIGHT;
290 #endif
291
292 if (hWnd)
293 msStyle |= MB_APPLMODAL;
294 else
295 msStyle |= MB_TASKMODAL;
296
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)
300 wxString message = GetFullMessage();
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
310 #if wxUSE_MSGBOX_HOOK
311 // install the hook if we need to position the dialog in a non-default way
312 // or change the labels
313 if ( (wxStyle & wxCENTER) || HasCustomLabels() )
314 {
315 const DWORD tid = ::GetCurrentThreadId();
316 m_hook = ::SetWindowsHookEx(WH_CBT,
317 &wxMessageDialog::HookFunction, NULL, tid);
318 HookMap()[tid] = this;
319 }
320 #endif // wxUSE_MSGBOX_HOOK
321
322 // do show the dialog
323 int msAns = MessageBox(hWnd, message.wx_str(), m_caption.wx_str(), msStyle);
324 int ans;
325 switch (msAns)
326 {
327 default:
328 wxFAIL_MSG(_T("unexpected ::MessageBox() return code"));
329 // fall through
330
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;
345 }
346
347 #endif // wxUSE_MSGDLG