allow centering wxMessageDialog on its parent window (patch 1836072)
[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 #ifndef WX_PRECOMP
24 #include "wx/app.h"
25 #include "wx/utils.h"
26 #include "wx/dialog.h"
27 #include "wx/hashmap.h"
28 #endif
29
30 #include "wx/msw/private.h"
31
32 // For MB_TASKMODAL
33 #ifdef __WXWINCE__
34 #include "wx/msw/wince/missing.h"
35 #endif
36
37 IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
38
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
41 //
42 // TODO: replace this with wxTLS once it's available
43 WX_DECLARE_HASH_MAP(unsigned long, wxMessageDialog *,
44 wxIntegerHash, wxIntegerEqual,
45 wxMessageDialogMap);
46
47 namespace
48 {
49
50 wxMessageDialogMap& HookMap()
51 {
52 static wxMessageDialogMap s_Map;
53
54 return s_Map;
55 }
56
57 } // anonymous namespace
58
59 /* static */
60 WXLRESULT wxCALLBACK
61 wxMessageDialog::HookFunction(int code, WXWPARAM wParam, WXLPARAM lParam)
62 {
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") );
68
69 wxMessageDialog * const wnd = node->second;
70
71 const HHOOK hhook = (HHOOK)wnd->m_hook;
72 const LRESULT rc = ::CallNextHookEx(hhook, code, wParam, lParam);
73
74 if ( code == HC_ACTION && lParam )
75 {
76 const CWPRETSTRUCT * const s = (CWPRETSTRUCT *)lParam;
77
78 if ( s->message == HCBT_ACTIVATE )
79 {
80 // we won't need this hook any longer
81 ::UnhookWindowsHookEx(hhook);
82 wnd->m_hook = NULL;
83 HookMap().erase(tid);
84
85 if ( wnd->GetMessageDialogStyle() & wxCENTER )
86 {
87 wnd->SetHWND(s->hwnd);
88 wnd->Center(); // center on parent
89 wnd->SetHWND(NULL);
90 }
91 //else: default behaviour, center on screen
92 }
93 }
94
95 return rc;
96 }
97
98 int wxMessageDialog::ShowModal()
99 {
100 if ( !wxTheApp->GetTopWindow() )
101 {
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();
108 }
109
110 // use the top level window as parent if none specified
111 if ( !m_parent )
112 m_parent = FindSuitableParent();
113 HWND hWnd = m_parent ? GetHwndOf(m_parent) : NULL;
114
115 // translate wx style in MSW
116 unsigned int msStyle = MB_OK;
117 const long wxStyle = GetMessageDialogStyle();
118 if (wxStyle & wxYES_NO)
119 {
120 #if !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
121 if (wxStyle & wxCANCEL)
122 msStyle = MB_YESNOCANCEL;
123 else
124 #endif // !(__SMARTPHONE__ && __WXWINCE__)
125 msStyle = MB_YESNO;
126
127 if (wxStyle & wxNO_DEFAULT)
128 msStyle |= MB_DEFBUTTON2;
129 }
130
131 if (wxStyle & wxOK)
132 {
133 if (wxStyle & wxCANCEL)
134 msStyle = MB_OKCANCEL;
135 else
136 msStyle = MB_OK;
137 }
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;
146
147 if ( wxStyle & wxSTAY_ON_TOP )
148 msStyle |= MB_TOPMOST;
149
150 #ifndef __WXWINCE__
151 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
152 msStyle |= MB_RTLREADING | MB_RIGHT;
153 #endif
154
155 if (hWnd)
156 msStyle |= MB_APPLMODAL;
157 else
158 msStyle |= MB_TASKMODAL;
159
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();
164 #if wxUSE_UNICODE
165 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
166 {
167 // NB: not all compilers support \u escapes
168 static const wchar_t wchRLM = 0x200f;
169 message.Prepend(wxString(wchRLM, 2));
170 }
171 #endif // wxUSE_UNICODE
172
173 // install the hook if we need to position the dialog in a non-default way
174 if ( wxStyle & wxCENTER )
175 {
176 const DWORD tid = ::GetCurrentThreadId();
177 m_hook = ::SetWindowsHookEx(WH_CALLWNDPROCRET,
178 &wxMessageDialog::HookFunction, NULL, tid);
179 HookMap()[tid] = this;
180 }
181
182 // do show the dialog
183 int msAns = MessageBox(hWnd, message.wx_str(), m_caption.wx_str(), msStyle);
184 int ans;
185 switch (msAns)
186 {
187 default:
188 wxFAIL_MSG(_T("unexpected ::MessageBox() return code"));
189 // fall through
190
191 case IDCANCEL:
192 ans = wxID_CANCEL;
193 break;
194 case IDOK:
195 ans = wxID_OK;
196 break;
197 case IDYES:
198 ans = wxID_YES;
199 break;
200 case IDNO:
201 ans = wxID_NO;
202 break;
203 }
204 return ans;
205 }
206
207 #endif // wxUSE_MSGDLG