added support for gcc precompiled headers
[wxWidgets.git] / src / univ / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/dialog.cpp
3 // Author: Robert Roebling, Vaclav Slavik
4 // Id: $Id$
5 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // ============================================================================
10 // declarations
11 // ============================================================================
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
18 #pragma implementation "univdialog.h"
19 #endif
20
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #ifndef WX_PRECOMP
29 #include "wx/dialog.h"
30 #include "wx/utils.h"
31 #include "wx/app.h"
32 #endif
33
34 #include "wx/evtloop.h"
35
36 //-----------------------------------------------------------------------------
37 // wxDialog
38 //-----------------------------------------------------------------------------
39
40 BEGIN_EVENT_TABLE(wxDialog,wxDialogBase)
41 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
42 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
43 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
44 EVT_CLOSE (wxDialog::OnCloseWindow)
45 END_EVENT_TABLE()
46
47 IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxTopLevelWindow)
48
49 void wxDialog::Init()
50 {
51 m_returnCode = 0;
52 m_windowDisabler = NULL;
53 m_eventLoop = NULL;
54 m_isShowingModal = FALSE;
55 }
56
57 wxDialog::~wxDialog()
58 {
59 delete m_eventLoop;
60 }
61
62 bool wxDialog::Create(wxWindow *parent,
63 wxWindowID id, const wxString &title,
64 const wxPoint &pos, const wxSize &size,
65 long style, const wxString &name)
66 {
67 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
68
69 // all dialogs should have tab traversal enabled
70 style |= wxTAB_TRAVERSAL;
71
72 return wxTopLevelWindow::Create(parent, id, title, pos, size, style, name);
73 }
74
75 void wxDialog::OnApply(wxCommandEvent &WXUNUSED(event))
76 {
77 if ( Validate() )
78 TransferDataFromWindow();
79 }
80
81 void wxDialog::OnCancel(wxCommandEvent &WXUNUSED(event))
82 {
83 if ( IsModal() )
84 {
85 EndModal(wxID_CANCEL);
86 }
87 else
88 {
89 SetReturnCode(wxID_CANCEL);
90 Show(FALSE);
91 }
92 }
93
94 void wxDialog::OnOK(wxCommandEvent &WXUNUSED(event))
95 {
96 if ( Validate() && TransferDataFromWindow() )
97 {
98 if ( IsModal() )
99 {
100 EndModal(wxID_OK);
101 }
102 else
103 {
104 SetReturnCode(wxID_OK);
105 Show(FALSE);
106 }
107 }
108 }
109
110 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
111 {
112 // We'll send a Cancel message by default,
113 // which may close the dialog.
114 // Check for looping if the Cancel event handler calls Close().
115
116 // Note that if a cancel button and handler aren't present in the dialog,
117 // nothing will happen when you close the dialog via the window manager, or
118 // via Close().
119 // We wouldn't want to destroy the dialog by default, since the dialog may have been
120 // created on the stack.
121 // However, this does mean that calling dialog->Close() won't delete the dialog
122 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
123 // sure to destroy the dialog.
124 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
125
126 static wxList s_closing;
127
128 if (s_closing.Member(this))
129 return; // no loops
130
131 s_closing.Append(this);
132
133 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
134 cancelEvent.SetEventObject(this);
135 GetEventHandler()->ProcessEvent(cancelEvent);
136 s_closing.DeleteObject(this);
137 }
138
139 bool wxDialog::Show(bool show)
140 {
141 if ( !show )
142 {
143 // if we had disabled other app windows, reenable them back now because
144 // if they stay disabled Windows will activate another window (one
145 // which is enabled, anyhow) and we will lose activation
146 if ( m_windowDisabler )
147 {
148 delete m_windowDisabler;
149 m_windowDisabler = NULL;
150 }
151
152 if ( IsModal() )
153 EndModal(wxID_CANCEL);
154 }
155
156 bool ret = wxDialogBase::Show(show);
157
158 if ( show )
159 InitDialog();
160
161 return ret;
162 }
163
164 bool wxDialog::IsModal() const
165 {
166 return m_isShowingModal;
167 }
168
169 int wxDialog::ShowModal()
170 {
171 if ( IsModal() )
172 {
173 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
174 return GetReturnCode();
175 }
176
177 // use the apps top level window as parent if none given unless explicitly
178 // forbidden
179 if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
180 {
181 wxWindow *parent = wxTheApp->GetTopWindow();
182 if ( parent && parent != this )
183 {
184 m_parent = parent;
185 }
186 }
187
188 Show(TRUE);
189
190 m_isShowingModal = TRUE;
191
192 wxASSERT_MSG( !m_windowDisabler, _T("disabling windows twice?") );
193
194 #if defined(__WXGTK__) || defined(__WXMGL__)
195 wxBusyCursorSuspender suspender;
196 // FIXME (FIXME_MGL) - make sure busy cursor disappears under MSW too
197 #endif
198
199 m_windowDisabler = new wxWindowDisabler(this);
200 if ( !m_eventLoop )
201 m_eventLoop = new wxEventLoop;
202
203 m_eventLoop->Run();
204
205 return GetReturnCode();
206 }
207
208 void wxDialog::EndModal(int retCode)
209 {
210 wxASSERT_MSG( m_eventLoop, _T("wxDialog is not modal") );
211
212 SetReturnCode(retCode);
213
214 if ( !IsModal() )
215 {
216 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
217 return;
218 }
219
220 m_isShowingModal = FALSE;
221
222 m_eventLoop->Exit();
223
224 Show(FALSE);
225 }