]>
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"
26 #include "wx/dialog.h"
27 #include "wx/hashmap.h"
30 #include "wx/msw/private.h"
34 #include "wx/msw/wince/missing.h"
37 IMPLEMENT_CLASS(wxMessageDialog
, wxDialog
)
39 // there can potentially be one message box per thread so we use a hash map
40 // with thread ids as keys and (currently shown) message boxes as values
42 // TODO: replace this with wxTLS once it's available
43 WX_DECLARE_HASH_MAP(unsigned long, wxMessageDialog
*,
44 wxIntegerHash
, wxIntegerEqual
,
50 wxMessageDialogMap
& HookMap()
52 static wxMessageDialogMap s_Map
;
57 } // anonymous namespace
61 wxMessageDialog::HookFunction(int code
, WXWPARAM wParam
, WXLPARAM lParam
)
63 // Find the thread-local instance of wxMessageDialog
64 const DWORD tid
= ::GetCurrentThreadId();
65 wxMessageDialogMap::iterator node
= HookMap().find(tid
);
66 wxCHECK_MSG( node
!= HookMap().end(), false,
67 wxT("bogus thread id in wxMessageDialog::Hook") );
69 wxMessageDialog
* const wnd
= node
->second
;
71 const HHOOK hhook
= (HHOOK
)wnd
->m_hook
;
72 const LRESULT rc
= ::CallNextHookEx(hhook
, code
, wParam
, lParam
);
74 if ( code
== HC_ACTION
&& lParam
)
76 const CWPRETSTRUCT
* const s
= (CWPRETSTRUCT
*)lParam
;
78 if ( s
->message
== HCBT_ACTIVATE
)
80 // we won't need this hook any longer
81 ::UnhookWindowsHookEx(hhook
);
85 if ( wnd
->GetMessageDialogStyle() & wxCENTER
)
87 wnd
->SetHWND(s
->hwnd
);
88 wnd
->Center(); // center on parent
91 //else: default behaviour, center on screen
98 int wxMessageDialog::ShowModal()
100 if ( !wxTheApp
->GetTopWindow() )
102 // when the message box is shown from wxApp::OnInit() (i.e. before the
103 // message loop is entered), this must be done or the next message box
104 // will never be shown - just try putting 2 calls to wxMessageBox() in
105 // OnInit() to see it
106 while ( wxTheApp
->Pending() )
107 wxTheApp
->Dispatch();
110 // use the top level window as parent if none specified
112 m_parent
= FindSuitableParent();
113 HWND hWnd
= m_parent
? GetHwndOf(m_parent
) : NULL
;
115 // translate wx style in MSW
116 unsigned int msStyle
= MB_OK
;
117 const long wxStyle
= GetMessageDialogStyle();
118 if (wxStyle
& wxYES_NO
)
120 #if !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
121 if (wxStyle
& wxCANCEL
)
122 msStyle
= MB_YESNOCANCEL
;
124 #endif // !(__SMARTPHONE__ && __WXWINCE__)
127 if (wxStyle
& wxNO_DEFAULT
)
128 msStyle
|= MB_DEFBUTTON2
;
133 if (wxStyle
& wxCANCEL
)
134 msStyle
= MB_OKCANCEL
;
138 if (wxStyle
& wxICON_EXCLAMATION
)
139 msStyle
|= MB_ICONEXCLAMATION
;
140 else if (wxStyle
& wxICON_HAND
)
141 msStyle
|= MB_ICONHAND
;
142 else if (wxStyle
& wxICON_INFORMATION
)
143 msStyle
|= MB_ICONINFORMATION
;
144 else if (wxStyle
& wxICON_QUESTION
)
145 msStyle
|= MB_ICONQUESTION
;
147 if ( wxStyle
& wxSTAY_ON_TOP
)
148 msStyle
|= MB_TOPMOST
;
151 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
152 msStyle
|= MB_RTLREADING
| MB_RIGHT
;
156 msStyle
|= MB_APPLMODAL
;
158 msStyle
|= MB_TASKMODAL
;
160 // per MSDN documentation for MessageBox() we can prefix the message with 2
161 // right-to-left mark characters to tell the function to use RTL layout
162 // (unfortunately this only works in Unicode builds)
163 wxString message
= GetFullMessage();
165 if ( wxTheApp
->GetLayoutDirection() == wxLayout_RightToLeft
)
167 // NB: not all compilers support \u escapes
168 static const wchar_t wchRLM
= 0x200f;
169 message
.Prepend(wxString(wchRLM
, 2));
171 #endif // wxUSE_UNICODE
173 // install the hook if we need to position the dialog in a non-default way
174 if ( wxStyle
& wxCENTER
)
176 const DWORD tid
= ::GetCurrentThreadId();
177 m_hook
= ::SetWindowsHookEx(WH_CALLWNDPROCRET
,
178 &wxMessageDialog::HookFunction
, NULL
, tid
);
179 HookMap()[tid
] = this;
182 // do show the dialog
183 int msAns
= MessageBox(hWnd
, message
.wx_str(), m_caption
.wx_str(), msStyle
);
188 wxFAIL_MSG(_T("unexpected ::MessageBox() return code"));
207 #endif // wxUSE_MSGDLG