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"
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
)
44 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxTopLevelWindow
)
49 m_windowDisabler
= NULL
;
51 m_isShowingModal
= false;
56 m_isBeingDeleted
= true;
58 // if the dialog is modal, this will end its event loop
64 bool wxDialog::Create(wxWindow
*parent
,
65 wxWindowID id
, const wxString
&title
,
66 const wxPoint
&pos
, const wxSize
&size
,
67 long style
, const wxString
&name
)
69 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
71 // all dialogs should have tab traversal enabled
72 style
|= wxTAB_TRAVERSAL
;
74 return wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
);
77 void wxDialog::OnApply(wxCommandEvent
&WXUNUSED(event
))
80 TransferDataFromWindow();
83 void wxDialog::OnCancel(wxCommandEvent
&WXUNUSED(event
))
87 EndModal(wxID_CANCEL
);
91 SetReturnCode(wxID_CANCEL
);
96 void wxDialog::OnOK(wxCommandEvent
&WXUNUSED(event
))
98 if ( Validate() && TransferDataFromWindow() )
106 SetReturnCode(wxID_OK
);
112 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
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().
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
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.
128 static wxList s_closing
;
130 if (s_closing
.Member(this))
133 s_closing
.Append(this);
135 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
136 cancelEvent
.SetEventObject(this);
137 GetEventHandler()->ProcessEvent(cancelEvent
);
138 s_closing
.DeleteObject(this);
141 bool wxDialog::Show(bool show
)
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
)
150 delete m_windowDisabler
;
151 m_windowDisabler
= NULL
;
155 EndModal(wxID_CANCEL
);
158 if (show
&& CanDoLayoutAdaptation())
159 DoLayoutAdaptation();
161 bool ret
= wxDialogBase::Show(show
);
169 bool wxDialog::IsModal() const
171 return m_isShowingModal
;
174 int wxDialog::ShowModal()
178 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
179 return GetReturnCode();
182 // use the apps top level window as parent if none given unless explicitly
184 if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
186 wxWindow
* const parent
= GetParentForModalDialog();
187 if ( parent
&& parent
!= this )
195 m_isShowingModal
= true;
197 wxASSERT_MSG( !m_windowDisabler
, _T("disabling windows twice?") );
199 #if defined(__WXGTK__) || defined(__WXMGL__)
200 wxBusyCursorSuspender suspender
;
201 // FIXME (FIXME_MGL) - make sure busy cursor disappears under MSW too
204 m_windowDisabler
= new wxWindowDisabler(this);
206 m_eventLoop
= new wxEventLoop
;
210 return GetReturnCode();
213 void wxDialog::EndModal(int retCode
)
215 wxASSERT_MSG( m_eventLoop
, _T("wxDialog is not modal") );
217 SetReturnCode(retCode
);
221 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
225 m_isShowingModal
= false;