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