]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/msgdlg.cpp
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"
44 #include "wx/msw/wince/missing.h"
47 IMPLEMENT_CLASS(wxMessageDialog
, wxDialog
)
51 // there can potentially be one message box per thread so we use a hash map
52 // with thread ids as keys and (currently shown) message boxes as values
54 // TODO: replace this with wxTLS once it's available
55 WX_DECLARE_HASH_MAP(unsigned long, wxMessageDialog
*,
56 wxIntegerHash
, wxIntegerEqual
,
62 wxMessageDialogMap
& HookMap()
64 static wxMessageDialogMap s_Map
;
69 } // anonymous namespace
73 wxMessageDialog::HookFunction(int code
, WXWPARAM wParam
, WXLPARAM lParam
)
75 // Find the thread-local instance of wxMessageDialog
76 const DWORD tid
= ::GetCurrentThreadId();
77 wxMessageDialogMap::iterator node
= HookMap().find(tid
);
78 wxCHECK_MSG( node
!= HookMap().end(), false,
79 wxT("bogus thread id in wxMessageDialog::Hook") );
81 wxMessageDialog
* const wnd
= node
->second
;
83 const HHOOK hhook
= (HHOOK
)wnd
->m_hook
;
84 const LRESULT rc
= ::CallNextHookEx(hhook
, code
, wParam
, lParam
);
86 if ( code
== HC_ACTION
&& lParam
)
88 const CWPRETSTRUCT
* const s
= (CWPRETSTRUCT
*)lParam
;
90 if ( s
->message
== HCBT_ACTIVATE
)
92 // we won't need this hook any longer
93 ::UnhookWindowsHookEx(hhook
);
97 if ( wnd
->GetMessageDialogStyle() & wxCENTER
)
99 wnd
->SetHWND(s
->hwnd
);
100 wnd
->Center(); // center on parent
103 //else: default behaviour, center on screen
110 #endif // wxUSE_MSGBOX_HOOK
113 int wxMessageDialog::ShowModal()
115 if ( !wxTheApp
->GetTopWindow() )
117 // when the message box is shown from wxApp::OnInit() (i.e. before the
118 // message loop is entered), this must be done or the next message box
119 // will never be shown - just try putting 2 calls to wxMessageBox() in
120 // OnInit() to see it
121 while ( wxTheApp
->Pending() )
122 wxTheApp
->Dispatch();
125 // use the top level window as parent if none specified
127 m_parent
= FindSuitableParent();
128 HWND hWnd
= m_parent
? GetHwndOf(m_parent
) : NULL
;
130 // translate wx style in MSW
131 unsigned int msStyle
= MB_OK
;
132 const long wxStyle
= GetMessageDialogStyle();
133 if (wxStyle
& wxYES_NO
)
135 #if !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
136 if (wxStyle
& wxCANCEL
)
137 msStyle
= MB_YESNOCANCEL
;
139 #endif // !(__SMARTPHONE__ && __WXWINCE__)
142 if (wxStyle
& wxNO_DEFAULT
)
143 msStyle
|= MB_DEFBUTTON2
;
148 if (wxStyle
& wxCANCEL
)
149 msStyle
= MB_OKCANCEL
;
153 if (wxStyle
& wxICON_EXCLAMATION
)
154 msStyle
|= MB_ICONEXCLAMATION
;
155 else if (wxStyle
& wxICON_HAND
)
156 msStyle
|= MB_ICONHAND
;
157 else if (wxStyle
& wxICON_INFORMATION
)
158 msStyle
|= MB_ICONINFORMATION
;
159 else if (wxStyle
& wxICON_QUESTION
)
160 msStyle
|= MB_ICONQUESTION
;
162 if ( wxStyle
& wxSTAY_ON_TOP
)
163 msStyle
|= MB_TOPMOST
;
166 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
167 msStyle
|= MB_RTLREADING
| MB_RIGHT
;
171 msStyle
|= MB_APPLMODAL
;
173 msStyle
|= MB_TASKMODAL
;
175 // per MSDN documentation for MessageBox() we can prefix the message with 2
176 // right-to-left mark characters to tell the function to use RTL layout
177 // (unfortunately this only works in Unicode builds)
178 wxString message
= GetFullMessage();
180 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
182 // NB: not all compilers support \u escapes
183 static const wchar_t wchRLM
= 0x200f;
184 message
.Prepend(wxString(wchRLM
, 2));
186 #endif // wxUSE_UNICODE
188 #if wxUSE_MSGBOX_HOOK
189 // install the hook if we need to position the dialog in a non-default way
190 if ( wxStyle
& wxCENTER
)
192 const DWORD tid
= ::GetCurrentThreadId();
193 m_hook
= ::SetWindowsHookEx(WH_CALLWNDPROCRET
,
194 &wxMessageDialog::HookFunction
, NULL
, tid
);
195 HookMap()[tid
] = this;
197 #endif // wxUSE_MSGBOX_HOOK
199 // do show the dialog
200 int msAns
= MessageBox(hWnd
, message
.wx_str(), m_caption
.wx_str(), msStyle
);
205 wxFAIL_MSG(_T("unexpected ::MessageBox() return code"));
224 #endif // wxUSE_MSGDLG