1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/dialog.cpp
3 // Author: Robert Roebling, Vaclav Slavik
5 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // ============================================================================
11 // ============================================================================
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
24 #include "wx/dialog.h"
31 #include "wx/evtloop.h"
32 #include "wx/modalhook.h"
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 BEGIN_EVENT_TABLE(wxDialog
,wxDialogBase
)
39 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
40 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
41 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
42 EVT_CLOSE (wxDialog::OnCloseWindow
)
48 m_windowDisabler
= NULL
;
50 m_isShowingModal
= false;
55 // if the dialog is modal, this will end its event loop
61 bool wxDialog::Create(wxWindow
*parent
,
62 wxWindowID id
, const wxString
&title
,
63 const wxPoint
&pos
, const wxSize
&size
,
64 long style
, const wxString
&name
)
66 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
68 // all dialogs should have tab traversal enabled
69 style
|= wxTAB_TRAVERSAL
;
71 return wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
);
74 void wxDialog::OnApply(wxCommandEvent
&WXUNUSED(event
))
77 TransferDataFromWindow();
80 void wxDialog::OnCancel(wxCommandEvent
&WXUNUSED(event
))
84 EndModal(wxID_CANCEL
);
88 SetReturnCode(wxID_CANCEL
);
93 void wxDialog::OnOK(wxCommandEvent
&WXUNUSED(event
))
95 if ( Validate() && TransferDataFromWindow() )
103 SetReturnCode(wxID_OK
);
109 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
111 // We'll send a Cancel message by default,
112 // which may close the dialog.
113 // Check for looping if the Cancel event handler calls Close().
115 // Note that if a cancel button and handler aren't present in the dialog,
116 // nothing will happen when you close the dialog via the window manager, or
118 // We wouldn't want to destroy the dialog by default, since the dialog may have been
119 // created on the stack.
120 // However, this does mean that calling dialog->Close() won't delete the dialog
121 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
122 // sure to destroy the dialog.
123 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
125 static wxList s_closing
;
127 if (s_closing
.Member(this))
130 s_closing
.Append(this);
132 wxCommandEvent
cancelEvent(wxEVT_BUTTON
, wxID_CANCEL
);
133 cancelEvent
.SetEventObject(this);
134 GetEventHandler()->ProcessEvent(cancelEvent
);
135 s_closing
.DeleteObject(this);
138 bool wxDialog::Show(bool show
)
142 // if we had disabled other app windows, reenable them back now because
143 // if they stay disabled Windows will activate another window (one
144 // which is enabled, anyhow) and we will lose activation
145 wxDELETE(m_windowDisabler
);
148 EndModal(wxID_CANCEL
);
151 if (show
&& CanDoLayoutAdaptation())
152 DoLayoutAdaptation();
154 bool ret
= wxDialogBase::Show(show
);
162 bool wxDialog::IsModal() const
164 return m_isShowingModal
;
167 int wxDialog::ShowModal()
169 WX_HOOK_MODAL_DIALOG();
173 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
174 return GetReturnCode();
177 // use the apps top level window as parent if none given unless explicitly
179 wxWindow
* const parent
= GetParentForModalDialog();
180 if ( parent
&& parent
!= this )
187 m_isShowingModal
= true;
189 wxASSERT_MSG( !m_windowDisabler
, wxT("disabling windows twice?") );
191 #if defined(__WXGTK__)
192 wxBusyCursorSuspender suspender
;
195 m_windowDisabler
= new wxWindowDisabler(this);
197 m_eventLoop
= new wxEventLoop
;
201 return GetReturnCode();
204 void wxDialog::EndModal(int retCode
)
206 wxASSERT_MSG( m_eventLoop
, wxT("wxDialog is not modal") );
208 SetReturnCode(retCode
);
212 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
216 m_isShowingModal
= false;