]> git.saurik.com Git - wxWidgets.git/blob - src/univ/dialog.cpp
replaced by CHANGES.txt
[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 wxDialog::wxDialog()
50 {
51 Init();
52 }
53
54 wxDialog::wxDialog(wxWindow *parent,
55 wxWindowID id,
56 const wxString& title,
57 const wxPoint& pos,
58 const wxSize& size,
59 long style,
60 const wxString& name)
61 {
62 Init();
63 Create(parent, id, title, pos, size, style, name);
64 }
65
66 void wxDialog::Init()
67 {
68 m_returnCode = 0;
69 m_windowDisabler = NULL;
70 m_eventLoop = NULL;
71 m_isShowingModal = FALSE;
72 }
73
74 wxDialog::~wxDialog()
75 {
76 delete m_eventLoop;
77 }
78
79 bool wxDialog::Create(wxWindow *parent,
80 wxWindowID id, const wxString &title,
81 const wxPoint &pos, const wxSize &size,
82 long style, const wxString &name)
83 {
84 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
85
86 // all dialogs should have tab traversal enabled
87 style |= wxTAB_TRAVERSAL;
88
89 return wxTopLevelWindow::Create(parent, id, title, pos, size, style, name);
90 }
91
92 void wxDialog::OnApply(wxCommandEvent &WXUNUSED(event))
93 {
94 if ( Validate() )
95 TransferDataFromWindow();
96 }
97
98 void wxDialog::OnCancel(wxCommandEvent &WXUNUSED(event))
99 {
100 if ( IsModal() )
101 {
102 EndModal(wxID_CANCEL);
103 }
104 else
105 {
106 SetReturnCode(wxID_CANCEL);
107 Show(FALSE);
108 }
109 }
110
111 void wxDialog::OnOK(wxCommandEvent &WXUNUSED(event))
112 {
113 if ( Validate() && TransferDataFromWindow() )
114 {
115 if ( IsModal() )
116 {
117 EndModal(wxID_OK);
118 }
119 else
120 {
121 SetReturnCode(wxID_OK);
122 Show(FALSE);
123 }
124 }
125 }
126
127 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
128 {
129 // We'll send a Cancel message by default,
130 // which may close the dialog.
131 // Check for looping if the Cancel event handler calls Close().
132
133 // Note that if a cancel button and handler aren't present in the dialog,
134 // nothing will happen when you close the dialog via the window manager, or
135 // via Close().
136 // We wouldn't want to destroy the dialog by default, since the dialog may have been
137 // created on the stack.
138 // However, this does mean that calling dialog->Close() won't delete the dialog
139 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
140 // sure to destroy the dialog.
141 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
142
143 static wxList s_closing;
144
145 if (s_closing.Member(this))
146 return; // no loops
147
148 s_closing.Append(this);
149
150 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
151 cancelEvent.SetEventObject(this);
152 GetEventHandler()->ProcessEvent(cancelEvent);
153 s_closing.DeleteObject(this);
154 }
155
156 bool wxDialog::Show(bool show)
157 {
158 if ( !show )
159 {
160 // if we had disabled other app windows, reenable them back now because
161 // if they stay disabled Windows will activate another window (one
162 // which is enabled, anyhow) and we will lose activation
163 if ( m_windowDisabler )
164 {
165 delete m_windowDisabler;
166 m_windowDisabler = NULL;
167 }
168
169 if ( IsModal() )
170 EndModal(wxID_CANCEL);
171 }
172
173 bool ret = wxDialogBase::Show(show);
174
175 if ( show )
176 InitDialog();
177
178 return ret;
179 }
180
181 bool wxDialog::IsModal() const
182 {
183 return m_isShowingModal;
184 }
185
186 int wxDialog::ShowModal()
187 {
188 if ( IsModal() )
189 {
190 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
191 return GetReturnCode();
192 }
193
194 // use the apps top level window as parent if none given unless explicitly
195 // forbidden
196 if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
197 {
198 wxWindow *parent = wxTheApp->GetTopWindow();
199 if ( parent && parent != this )
200 {
201 m_parent = parent;
202 }
203 }
204
205 Show(TRUE);
206
207 m_isShowingModal = TRUE;
208
209 wxASSERT_MSG( !m_windowDisabler, _T("disabling windows twice?") );
210
211 #if defined(__WXGTK__) || defined(__WXMGL__)
212 wxBusyCursorSuspender suspender;
213 // FIXME (FIXME_MGL) - make sure busy cursor disappears under MSW too
214 #endif
215
216 m_windowDisabler = new wxWindowDisabler(this);
217 if ( !m_eventLoop )
218 m_eventLoop = new wxEventLoop;
219
220 m_eventLoop->Run();
221
222 return GetReturnCode();
223 }
224
225 void wxDialog::EndModal(int retCode)
226 {
227 wxASSERT_MSG( m_eventLoop, _T("wxDialog is not modal") );
228
229 SetReturnCode(retCode);
230
231 if ( !IsModal() )
232 {
233 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
234 return;
235 }
236
237 m_isShowingModal = FALSE;
238
239 m_eventLoop->Exit();
240
241 Show(FALSE);
242 }