]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
e5b50758 | 2 | // Name: src/msw/msgdlg.cpp |
2bda0e17 KB |
3 | // Purpose: wxMessageDialog |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
2bda0e17 KB |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
7520f3da | 16 | #pragma hdrstop |
2bda0e17 KB |
17 | #endif |
18 | ||
a8ff046b VZ |
19 | #if wxUSE_MSGDLG |
20 | ||
246c5004 WS |
21 | #include "wx/msgdlg.h" |
22 | ||
2bda0e17 | 23 | #ifndef WX_PRECOMP |
35bc781e | 24 | #include "wx/app.h" |
0d7ea902 VZ |
25 | #include "wx/utils.h" |
26 | #include "wx/dialog.h" | |
1d89da8a | 27 | #include "wx/hashmap.h" |
2bda0e17 KB |
28 | #endif |
29 | ||
30 | #include "wx/msw/private.h" | |
31 | ||
676d6550 JS |
32 | // For MB_TASKMODAL |
33 | #ifdef __WXWINCE__ | |
34 | #include "wx/msw/wince/missing.h" | |
35 | #endif | |
36 | ||
2bda0e17 | 37 | IMPLEMENT_CLASS(wxMessageDialog, wxDialog) |
2bda0e17 | 38 | |
1d89da8a VZ |
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 | ||
0d7ea902 | 98 | int wxMessageDialog::ShowModal() |
2bda0e17 | 99 | { |
a543e3ce | 100 | if ( !wxTheApp->GetTopWindow() ) |
0d7ea902 VZ |
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 | } | |
93c95e18 | 109 | |
b8505921 | 110 | // use the top level window as parent if none specified |
a543e3ce VZ |
111 | if ( !m_parent ) |
112 | m_parent = FindSuitableParent(); | |
113 | HWND hWnd = m_parent ? GetHwndOf(m_parent) : NULL; | |
b8505921 VZ |
114 | |
115 | // translate wx style in MSW | |
0d7ea902 | 116 | unsigned int msStyle = MB_OK; |
e5b50758 WS |
117 | const long wxStyle = GetMessageDialogStyle(); |
118 | if (wxStyle & wxYES_NO) | |
0d7ea902 | 119 | { |
3180bc0e | 120 | #if !(defined(__SMARTPHONE__) && defined(__WXWINCE__)) |
e5b50758 | 121 | if (wxStyle & wxCANCEL) |
0d7ea902 VZ |
122 | msStyle = MB_YESNOCANCEL; |
123 | else | |
3180bc0e | 124 | #endif // !(__SMARTPHONE__ && __WXWINCE__) |
0d7ea902 | 125 | msStyle = MB_YESNO; |
93c95e18 | 126 | |
e5b50758 | 127 | if (wxStyle & wxNO_DEFAULT) |
0d7ea902 VZ |
128 | msStyle |= MB_DEFBUTTON2; |
129 | } | |
130 | ||
e5b50758 | 131 | if (wxStyle & wxOK) |
0d7ea902 | 132 | { |
e5b50758 | 133 | if (wxStyle & wxCANCEL) |
0d7ea902 VZ |
134 | msStyle = MB_OKCANCEL; |
135 | else | |
136 | msStyle = MB_OK; | |
137 | } | |
e5b50758 | 138 | if (wxStyle & wxICON_EXCLAMATION) |
0d7ea902 | 139 | msStyle |= MB_ICONEXCLAMATION; |
e5b50758 | 140 | else if (wxStyle & wxICON_HAND) |
0d7ea902 | 141 | msStyle |= MB_ICONHAND; |
e5b50758 | 142 | else if (wxStyle & wxICON_INFORMATION) |
0d7ea902 | 143 | msStyle |= MB_ICONINFORMATION; |
e5b50758 | 144 | else if (wxStyle & wxICON_QUESTION) |
0d7ea902 | 145 | msStyle |= MB_ICONQUESTION; |
2bda0e17 | 146 | |
e5b50758 | 147 | if ( wxStyle & wxSTAY_ON_TOP ) |
a7fd7c78 VZ |
148 | msStyle |= MB_TOPMOST; |
149 | ||
08a58133 | 150 | #ifndef __WXWINCE__ |
978af864 VZ |
151 | if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft ) |
152 | msStyle |= MB_RTLREADING | MB_RIGHT; | |
08a58133 | 153 | #endif |
978af864 | 154 | |
0d7ea902 VZ |
155 | if (hWnd) |
156 | msStyle |= MB_APPLMODAL; | |
157 | else | |
158 | msStyle |= MB_TASKMODAL; | |
93c95e18 | 159 | |
12e424d2 VZ |
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) | |
2afb9e16 | 163 | wxString message = GetFullMessage(); |
12e424d2 VZ |
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 | ||
1d89da8a VZ |
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 | ||
b8505921 | 182 | // do show the dialog |
e0a050e3 | 183 | int msAns = MessageBox(hWnd, message.wx_str(), m_caption.wx_str(), msStyle); |
b8505921 | 184 | int ans; |
0d7ea902 VZ |
185 | switch (msAns) |
186 | { | |
b8505921 VZ |
187 | default: |
188 | wxFAIL_MSG(_T("unexpected ::MessageBox() return code")); | |
189 | // fall through | |
190 | ||
0d7ea902 VZ |
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; | |
2bda0e17 | 205 | } |
a8ff046b VZ |
206 | |
207 | #endif // wxUSE_MSGDLG |