1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/dialog.cpp
3 // Author: Robert Roebling, Vaclav Slavik
4 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
8 // ============================================================================
10 // ============================================================================
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/dialog.h"
30 #include "wx/evtloop.h"
31 #include "wx/modalhook.h"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 BEGIN_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
)
47 m_windowDisabler
= NULL
;
49 m_isShowingModal
= false;
54 // if the dialog is modal, this will end its event loop
60 bool wxDialog::Create(wxWindow
*parent
,
61 wxWindowID id
, const wxString
&title
,
62 const wxPoint
&pos
, const wxSize
&size
,
63 long style
, const wxString
&name
)
65 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
67 // all dialogs should have tab traversal enabled
68 style
|= wxTAB_TRAVERSAL
;
70 return wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
);
73 void wxDialog::OnApply(wxCommandEvent
&WXUNUSED(event
))
76 TransferDataFromWindow();
79 void wxDialog::OnCancel(wxCommandEvent
&WXUNUSED(event
))
83 EndModal(wxID_CANCEL
);
87 SetReturnCode(wxID_CANCEL
);
92 void wxDialog::OnOK(wxCommandEvent
&WXUNUSED(event
))
94 if ( Validate() && TransferDataFromWindow() )
102 SetReturnCode(wxID_OK
);
108 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
110 // We'll send a Cancel message by default,
111 // which may close the dialog.
112 // Check for looping if the Cancel event handler calls Close().
114 // Note that if a cancel button and handler aren't present in the dialog,
115 // nothing will happen when you close the dialog via the window manager, or
117 // We wouldn't want to destroy the dialog by default, since the dialog may have been
118 // created on the stack.
119 // However, this does mean that calling dialog->Close() won't delete the dialog
120 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
121 // sure to destroy the dialog.
122 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
124 static wxList s_closing
;
126 if (s_closing
.Member(this))
129 s_closing
.Append(this);
131 wxCommandEvent
cancelEvent(wxEVT_BUTTON
, wxID_CANCEL
);
132 cancelEvent
.SetEventObject(this);
133 GetEventHandler()->ProcessEvent(cancelEvent
);
134 s_closing
.DeleteObject(this);
137 bool wxDialog::Show(bool show
)
141 // if we had disabled other app windows, reenable them back now because
142 // if they stay disabled Windows will activate another window (one
143 // which is enabled, anyhow) and we will lose activation
144 wxDELETE(m_windowDisabler
);
147 EndModal(wxID_CANCEL
);
150 if (show
&& CanDoLayoutAdaptation())
151 DoLayoutAdaptation();
153 bool ret
= wxDialogBase::Show(show
);
161 bool wxDialog::IsModal() const
163 return m_isShowingModal
;
166 int wxDialog::ShowModal()
168 WX_HOOK_MODAL_DIALOG();
172 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
173 return GetReturnCode();
176 // use the apps top level window as parent if none given unless explicitly
178 wxWindow
* const parent
= GetParentForModalDialog();
179 if ( parent
&& parent
!= this )
186 m_isShowingModal
= true;
188 wxASSERT_MSG( !m_windowDisabler
, wxT("disabling windows twice?") );
190 #if defined(__WXGTK__)
191 wxBusyCursorSuspender suspender
;
194 m_windowDisabler
= new wxWindowDisabler(this);
196 m_eventLoop
= new wxEventLoop
;
200 return GetReturnCode();
203 void wxDialog::EndModal(int retCode
)
205 wxASSERT_MSG( m_eventLoop
, wxT("wxDialog is not modal") );
207 SetReturnCode(retCode
);
211 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
215 m_isShowingModal
= false;