]> git.saurik.com Git - wxWidgets.git/blob - src/msw/msgdlg.cpp
Better fix
[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
42 // For MB_TASKMODAL
43 #ifdef __WXWINCE__
44 #include "wx/msw/wince/missing.h"
45 #endif
46
47 IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
48
49 #if wxUSE_MSGBOX_HOOK
50
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
53 //
54 // TODO: replace this with wxTLS once it's available
55 WX_DECLARE_HASH_MAP(unsigned long, wxMessageDialog *,
56 wxIntegerHash, wxIntegerEqual,
57 wxMessageDialogMap);
58
59 namespace
60 {
61
62 wxMessageDialogMap& HookMap()
63 {
64 static wxMessageDialogMap s_Map;
65
66 return s_Map;
67 }
68
69 } // anonymous namespace
70
71 /* static */
72 WXLRESULT wxCALLBACK
73 wxMessageDialog::HookFunction(int code, WXWPARAM wParam, WXLPARAM lParam)
74 {
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") );
80
81 wxMessageDialog * const wnd = node->second;
82
83 const HHOOK hhook = (HHOOK)wnd->m_hook;
84 const LRESULT rc = ::CallNextHookEx(hhook, code, wParam, lParam);
85
86 if ( code == HC_ACTION && lParam )
87 {
88 const CWPRETSTRUCT * const s = (CWPRETSTRUCT *)lParam;
89
90 if ( s->message == HCBT_ACTIVATE )
91 {
92 // we won't need this hook any longer
93 ::UnhookWindowsHookEx(hhook);
94 wnd->m_hook = NULL;
95 HookMap().erase(tid);
96
97 if ( wnd->GetMessageDialogStyle() & wxCENTER )
98 {
99 wnd->SetHWND(s->hwnd);
100 wnd->Center(); // center on parent
101 wnd->SetHWND(NULL);
102 }
103 //else: default behaviour, center on screen
104 }
105 }
106
107 return rc;
108 }
109
110 #endif // wxUSE_MSGBOX_HOOK
111
112
113 int wxMessageDialog::ShowModal()
114 {
115 if ( !wxTheApp->GetTopWindow() )
116 {
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();
123 }
124
125 // use the top level window as parent if none specified
126 if ( !m_parent )
127 m_parent = FindSuitableParent();
128 HWND hWnd = m_parent ? GetHwndOf(m_parent) : NULL;
129
130 // translate wx style in MSW
131 unsigned int msStyle = MB_OK;
132 const long wxStyle = GetMessageDialogStyle();
133 if (wxStyle & wxYES_NO)
134 {
135 #if !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
136 if (wxStyle & wxCANCEL)
137 msStyle = MB_YESNOCANCEL;
138 else
139 #endif // !(__SMARTPHONE__ && __WXWINCE__)
140 msStyle = MB_YESNO;
141
142 if (wxStyle & wxNO_DEFAULT)
143 msStyle |= MB_DEFBUTTON2;
144 }
145
146 if (wxStyle & wxOK)
147 {
148 if (wxStyle & wxCANCEL)
149 msStyle = MB_OKCANCEL;
150 else
151 msStyle = MB_OK;
152 }
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;
161
162 if ( wxStyle & wxSTAY_ON_TOP )
163 msStyle |= MB_TOPMOST;
164
165 #ifndef __WXWINCE__
166 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
167 msStyle |= MB_RTLREADING | MB_RIGHT;
168 #endif
169
170 if (hWnd)
171 msStyle |= MB_APPLMODAL;
172 else
173 msStyle |= MB_TASKMODAL;
174
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();
179 #if wxUSE_UNICODE
180 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
181 {
182 // NB: not all compilers support \u escapes
183 static const wchar_t wchRLM = 0x200f;
184 message.Prepend(wxString(wchRLM, 2));
185 }
186 #endif // wxUSE_UNICODE
187
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 )
191 {
192 const DWORD tid = ::GetCurrentThreadId();
193 m_hook = ::SetWindowsHookEx(WH_CALLWNDPROCRET,
194 &wxMessageDialog::HookFunction, NULL, tid);
195 HookMap()[tid] = this;
196 }
197 #endif // wxUSE_MSGBOX_HOOK
198
199 // do show the dialog
200 int msAns = MessageBox(hWnd, message.wx_str(), m_caption.wx_str(), msStyle);
201 int ans;
202 switch (msAns)
203 {
204 default:
205 wxFAIL_MSG(_T("unexpected ::MessageBox() return code"));
206 // fall through
207
208 case IDCANCEL:
209 ans = wxID_CANCEL;
210 break;
211 case IDOK:
212 ans = wxID_OK;
213 break;
214 case IDYES:
215 ans = wxID_YES;
216 break;
217 case IDNO:
218 ans = wxID_NO;
219 break;
220 }
221 return ans;
222 }
223
224 #endif // wxUSE_MSGDLG