check that wxYES and wxNO are always used together as the native msg box can't use...
[wxWidgets.git] / src / msw / msgdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "msgdlg.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/app.h"
25 #include "wx/defs.h"
26 #include "wx/utils.h"
27 #include "wx/dialog.h"
28 #include "wx/msgdlg.h"
29 #endif
30
31 #include "wx/msw/private.h"
32
33 // For MB_TASKMODAL
34 #ifdef __WXWINCE__
35 #include "wx/msw/wince/missing.h"
36 #endif
37
38 IMPLEMENT_CLASS(wxMessageDialog, wxDialog)
39
40 wxMessageDialog::wxMessageDialog(wxWindow *parent,
41 const wxString& message,
42 const wxString& caption,
43 long style,
44 const wxPoint& WXUNUSED(pos))
45 {
46 #ifdef __WXDEBUG__
47 // check for common programming errors
48 if ( (style & wxID_OK) == wxID_OK )
49 {
50 // programmer probably confused wxID_OK with wxOK. Correct one is wxOK.
51 wxFAIL_MSG( _T("wxMessageBox: Did you mean wxOK (and not wxID_OK)?") );
52 }
53 #endif // __WXDEBUG__
54
55 m_caption = caption;
56 m_message = message;
57 m_dialogStyle = style;
58 m_parent = parent;
59 }
60
61 int wxMessageDialog::ShowModal()
62 {
63 if ( !wxTheApp->GetTopWindow() )
64 {
65 // when the message box is shown from wxApp::OnInit() (i.e. before the
66 // message loop is entered), this must be done or the next message box
67 // will never be shown - just try putting 2 calls to wxMessageBox() in
68 // OnInit() to see it
69 while ( wxTheApp->Pending() )
70 wxTheApp->Dispatch();
71 }
72
73 // use the top level window as parent if none specified
74 if ( !m_parent )
75 m_parent = FindSuitableParent();
76 HWND hWnd = m_parent ? GetHwndOf(m_parent) : NULL;
77
78 // translate wx style in MSW
79 unsigned int msStyle = MB_OK;
80 if (m_dialogStyle & wxYES_NO)
81 {
82 wxASSERT_MSG( (m_dialogStyle & wxYES_NO) == wxYES_NO,
83 _T("wxYES and wxNO may only be used together under MSW") );
84
85 #if !(defined(__SMARTPHONE__) && defined(__WXWINCE__))
86 if (m_dialogStyle & wxCANCEL)
87 msStyle = MB_YESNOCANCEL;
88 else
89 #endif // !(__SMARTPHONE__ && __WXWINCE__)
90 msStyle = MB_YESNO;
91
92 if (m_dialogStyle & wxNO_DEFAULT)
93 msStyle |= MB_DEFBUTTON2;
94 }
95
96 if (m_dialogStyle & wxOK)
97 {
98 if (m_dialogStyle & wxCANCEL)
99 msStyle = MB_OKCANCEL;
100 else
101 msStyle = MB_OK;
102 }
103 if (m_dialogStyle & wxICON_EXCLAMATION)
104 msStyle |= MB_ICONEXCLAMATION;
105 else if (m_dialogStyle & wxICON_HAND)
106 msStyle |= MB_ICONHAND;
107 else if (m_dialogStyle & wxICON_INFORMATION)
108 msStyle |= MB_ICONINFORMATION;
109 else if (m_dialogStyle & wxICON_QUESTION)
110 msStyle |= MB_ICONQUESTION;
111
112 if ( m_dialogStyle & wxSTAY_ON_TOP )
113 msStyle |= MB_TOPMOST;
114
115 if (hWnd)
116 msStyle |= MB_APPLMODAL;
117 else
118 msStyle |= MB_TASKMODAL;
119
120 // do show the dialog
121 int msAns = MessageBox(hWnd, m_message.c_str(), m_caption.c_str(), msStyle);
122 int ans;
123 switch (msAns)
124 {
125 default:
126 wxFAIL_MSG(_T("unexpected ::MessageBox() return code"));
127 // fall through
128
129 case IDCANCEL:
130 ans = wxID_CANCEL;
131 break;
132 case IDOK:
133 ans = wxID_OK;
134 break;
135 case IDYES:
136 ans = wxID_YES;
137 break;
138 case IDNO:
139 ans = wxID_NO;
140 break;
141 }
142 return ans;
143 }
144