]> git.saurik.com Git - wxWidgets.git/blame - src/msw/msgdlg.cpp
include wx/crt.h needed for wxFprintf() (closes #9509)
[wxWidgets.git] / src / msw / msgdlg.cpp
CommitLineData
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
704c499e
VZ
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
2bda0e17 31#ifndef WX_PRECOMP
35bc781e 32 #include "wx/app.h"
0d7ea902
VZ
33 #include "wx/utils.h"
34 #include "wx/dialog.h"
704c499e
VZ
35 #if wxUSE_MSGBOX_HOOK
36 #include "wx/hashmap.h"
37 #endif
2bda0e17
KB
38#endif
39
40#include "wx/msw/private.h"
41
676d6550
JS
42// For MB_TASKMODAL
43#ifdef __WXWINCE__
44#include "wx/msw/wince/missing.h"
45#endif
46
2bda0e17 47IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
2bda0e17 48
704c499e
VZ
49#if wxUSE_MSGBOX_HOOK
50
1d89da8a
VZ
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
55WX_DECLARE_HASH_MAP(unsigned long, wxMessageDialog *,
56 wxIntegerHash, wxIntegerEqual,
57 wxMessageDialogMap);
58
59namespace
60{
61
62wxMessageDialogMap& HookMap()
63{
64 static wxMessageDialogMap s_Map;
65
66 return s_Map;
67}
68
69} // anonymous namespace
70
71/* static */
72WXLRESULT wxCALLBACK
73wxMessageDialog::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
704c499e
VZ
110#endif // wxUSE_MSGBOX_HOOK
111
112
0d7ea902 113int wxMessageDialog::ShowModal()
2bda0e17 114{
a543e3ce 115 if ( !wxTheApp->GetTopWindow() )
0d7ea902
VZ
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 }
93c95e18 124
b8505921 125 // use the top level window as parent if none specified
a543e3ce
VZ
126 if ( !m_parent )
127 m_parent = FindSuitableParent();
128 HWND hWnd = m_parent ? GetHwndOf(m_parent) : NULL;
b8505921
VZ
129
130 // translate wx style in MSW
0d7ea902 131 unsigned int msStyle = MB_OK;
e5b50758
WS
132 const long wxStyle = GetMessageDialogStyle();
133 if (wxStyle & wxYES_NO)
0d7ea902 134 {
3180bc0e 135#if !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
e5b50758 136 if (wxStyle & wxCANCEL)
0d7ea902
VZ
137 msStyle = MB_YESNOCANCEL;
138 else
3180bc0e 139#endif // !(__SMARTPHONE__ && __WXWINCE__)
0d7ea902 140 msStyle = MB_YESNO;
93c95e18 141
e5b50758 142 if (wxStyle & wxNO_DEFAULT)
0d7ea902
VZ
143 msStyle |= MB_DEFBUTTON2;
144 }
145
e5b50758 146 if (wxStyle & wxOK)
0d7ea902 147 {
e5b50758 148 if (wxStyle & wxCANCEL)
0d7ea902
VZ
149 msStyle = MB_OKCANCEL;
150 else
151 msStyle = MB_OK;
152 }
e5b50758 153 if (wxStyle & wxICON_EXCLAMATION)
0d7ea902 154 msStyle |= MB_ICONEXCLAMATION;
e5b50758 155 else if (wxStyle & wxICON_HAND)
0d7ea902 156 msStyle |= MB_ICONHAND;
e5b50758 157 else if (wxStyle & wxICON_INFORMATION)
0d7ea902 158 msStyle |= MB_ICONINFORMATION;
e5b50758 159 else if (wxStyle & wxICON_QUESTION)
0d7ea902 160 msStyle |= MB_ICONQUESTION;
2bda0e17 161
e5b50758 162 if ( wxStyle & wxSTAY_ON_TOP )
a7fd7c78
VZ
163 msStyle |= MB_TOPMOST;
164
08a58133 165#ifndef __WXWINCE__
978af864
VZ
166 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
167 msStyle |= MB_RTLREADING | MB_RIGHT;
08a58133 168#endif
978af864 169
0d7ea902
VZ
170 if (hWnd)
171 msStyle |= MB_APPLMODAL;
172 else
173 msStyle |= MB_TASKMODAL;
93c95e18 174
12e424d2
VZ
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)
2afb9e16 178 wxString message = GetFullMessage();
12e424d2
VZ
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
704c499e 188#if wxUSE_MSGBOX_HOOK
1d89da8a
VZ
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 }
704c499e 197#endif // wxUSE_MSGBOX_HOOK
1d89da8a 198
b8505921 199 // do show the dialog
e0a050e3 200 int msAns = MessageBox(hWnd, message.wx_str(), m_caption.wx_str(), msStyle);
b8505921 201 int ans;
0d7ea902
VZ
202 switch (msAns)
203 {
b8505921
VZ
204 default:
205 wxFAIL_MSG(_T("unexpected ::MessageBox() return code"));
206 // fall through
207
0d7ea902
VZ
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;
2bda0e17 222}
a8ff046b
VZ
223
224#endif // wxUSE_MSGDLG