]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/msgdlg.cpp
be6f508df69614b859973e069b9adb841233f0cb
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/msgdlg.cpp
3 // Purpose: wxMessageDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/msgdlg.h"
23 // there is no hook support under CE so we can't use the code for message box
26 #define wxUSE_MSGBOX_HOOK 1
28 #define wxUSE_MSGBOX_HOOK 0
34 #include "wx/dialog.h"
36 #include "wx/hashmap.h"
40 #include "wx/msw/private.h"
41 #include "wx/msw/private/button.h"
42 #include "wx/msw/private/metrics.h"
45 #include "wx/fontutil.h"
50 #include "wx/msw/wince/missing.h"
53 IMPLEMENT_CLASS(wxMessageDialog
, wxDialog
)
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
60 // TODO: replace this with wxTLS once it's available
61 WX_DECLARE_HASH_MAP(unsigned long, wxMessageDialog
*,
62 wxIntegerHash
, wxIntegerEqual
,
68 wxMessageDialogMap
& HookMap()
70 static wxMessageDialogMap s_Map
;
75 } // anonymous namespace
79 wxMessageDialog::HookFunction(int code
, WXWPARAM wParam
, WXLPARAM lParam
)
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") );
87 wxMessageDialog
* const wnd
= node
->second
;
89 const HHOOK hhook
= (HHOOK
)wnd
->m_hook
;
90 const LRESULT rc
= ::CallNextHookEx(hhook
, code
, wParam
, lParam
);
92 if ( code
== HCBT_ACTIVATE
)
94 // we won't need this hook any longer
95 ::UnhookWindowsHookEx(hhook
);
99 wnd
->SetHWND((HWND
)wParam
);
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
106 // also update the labels if necessary
107 if ( wnd
->HasCustomLabels() )
108 wnd
->AdjustButtonLabels();
110 // there seems to be no reason to leave it set
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
)
124 if ( const HWND hwndParent
= ::GetAncestor(hwnd
, GA_PARENT
) )
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);
134 rc
.right
- rc
.left
, rc
.bottom
- rc
.top
,
138 // helper of AdjustButtonLabels(): move the given window by dx
140 // works for both child and top level windows
141 void OffsetWindow(HWND hwnd
, int dx
)
143 RECT rc
= wxGetWindowRect(hwnd
);
148 MoveWindowToScreenRect(hwnd
, rc
);
151 } // anonymous namespace
153 void wxMessageDialog::AdjustButtonLabels()
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
159 // TODO-RTL: check whether this works correctly in RTL
161 // the order in this array is the one in which buttons appear in the
163 const static struct ButtonAccessors
166 wxString (wxMessageDialog::*getter
)() const;
170 { IDYES
, &wxMessageDialog::GetYesLabel
},
171 { IDNO
, &wxMessageDialog::GetNoLabel
},
172 { IDOK
, &wxMessageDialog::GetOKLabel
},
173 { IDCANCEL
, &wxMessageDialog::GetCancelLabel
},
176 // this contains the amount by which we increased the message box width
179 const NONCLIENTMETRICS
& ncm
= wxMSWImpl::GetNonClientMetrics();
180 const wxFont
fontMsgBox(wxNativeFontInfo(ncm
.lfMessageFont
));
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
186 wxWindowBase::SetFont(fontMsgBox
);
188 for ( unsigned n
= 0; n
< WXSIZEOF(buttons
); n
++ )
190 const HWND hwndBtn
= ::GetDlgItem(GetHwnd(), buttons
[n
].id
);
192 continue; // it's ok, not all buttons are always present
194 const wxString label
= (this->*buttons
[n
].getter
)();
195 const wxSize sizeLabel
= wxWindowBase::GetTextExtent(label
);
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
;
204 // we need to resize the button
206 MoveWindowToScreenRect(hwndBtn
, rc
);
208 // and also move all the other buttons
209 for ( unsigned m
= n
+ 1; m
< WXSIZEOF(buttons
); m
++ )
211 const HWND hwndBtnNext
= ::GetDlgItem(GetHwnd(), buttons
[m
].id
);
213 OffsetWindow(hwndBtnNext
, dw
);
219 ::SetWindowText(hwndBtn
, label
.wx_str());
223 // resize the message box itself if needed
225 OffsetWindow(GetHwnd(), dx
);
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)
232 #endif // wxUSE_MSGBOX_HOOK
235 int wxMessageDialog::ShowModal()
237 if ( !wxTheApp
->GetTopWindow() )
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();
247 // use the top level window as parent if none specified
249 m_parent
= FindSuitableParent();
250 HWND hWnd
= m_parent
? GetHwndOf(m_parent
) : NULL
;
252 // translate wx style in MSW
253 unsigned int msStyle
= MB_OK
;
254 const long wxStyle
= GetMessageDialogStyle();
255 if (wxStyle
& wxYES_NO
)
257 #if !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
258 if (wxStyle
& wxCANCEL
)
259 msStyle
= MB_YESNOCANCEL
;
261 #endif // !(__SMARTPHONE__ && __WXWINCE__)
264 if (wxStyle
& wxNO_DEFAULT
)
265 msStyle
|= MB_DEFBUTTON2
;
270 if (wxStyle
& wxCANCEL
)
271 msStyle
= MB_OKCANCEL
;
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
;
284 if ( wxStyle
& wxSTAY_ON_TOP
)
285 msStyle
|= MB_TOPMOST
;
288 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
289 msStyle
|= MB_RTLREADING
| MB_RIGHT
;
293 msStyle
|= MB_APPLMODAL
;
295 msStyle
|= MB_TASKMODAL
;
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();
302 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
304 // NB: not all compilers support \u escapes
305 static const wchar_t wchRLM
= 0x200f;
306 message
.Prepend(wxString(wchRLM
, 2));
308 #endif // wxUSE_UNICODE
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() )
315 const DWORD tid
= ::GetCurrentThreadId();
316 m_hook
= ::SetWindowsHookEx(WH_CBT
,
317 &wxMessageDialog::HookFunction
, NULL
, tid
);
318 HookMap()[tid
] = this;
320 #endif // wxUSE_MSGBOX_HOOK
322 // do show the dialog
323 int msAns
= MessageBox(hWnd
, message
.wx_str(), m_caption
.wx_str(), msStyle
);
328 wxFAIL_MSG(_T("unexpected ::MessageBox() return code"));
347 #endif // wxUSE_MSGDLG