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;
59 bool wxDialog::Create(wxWindow
*parent
,
60 wxWindowID id
, const wxString
&title
,
61 const wxPoint
&pos
, const wxSize
&size
,
62 long style
, const wxString
&name
)
64 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
66 // all dialogs should have tab traversal enabled
67 style
|= wxTAB_TRAVERSAL
;
69 return wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
);
72 void wxDialog::OnApply(wxCommandEvent
&WXUNUSED(event
))
75 TransferDataFromWindow();
78 void wxDialog::OnCancel(wxCommandEvent
&WXUNUSED(event
))
82 EndModal(wxID_CANCEL
);
86 SetReturnCode(wxID_CANCEL
);
91 void wxDialog::OnOK(wxCommandEvent
&WXUNUSED(event
))
93 if ( Validate() && TransferDataFromWindow() )
101 SetReturnCode(wxID_OK
);
107 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
109 // We'll send a Cancel message by default,
110 // which may close the dialog.
111 // Check for looping if the Cancel event handler calls Close().
113 // Note that if a cancel button and handler aren't present in the dialog,
114 // nothing will happen when you close the dialog via the window manager, or
116 // We wouldn't want to destroy the dialog by default, since the dialog may have been
117 // created on the stack.
118 // However, this does mean that calling dialog->Close() won't delete the dialog
119 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
120 // sure to destroy the dialog.
121 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
123 static wxList s_closing
;
125 if (s_closing
.Member(this))
128 s_closing
.Append(this);
130 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
131 cancelEvent
.SetEventObject(this);
132 GetEventHandler()->ProcessEvent(cancelEvent
);
133 s_closing
.DeleteObject(this);
136 bool wxDialog::Show(bool show
)
140 // if we had disabled other app windows, reenable them back now because
141 // if they stay disabled Windows will activate another window (one
142 // which is enabled, anyhow) and we will lose activation
143 if ( m_windowDisabler
)
145 delete m_windowDisabler
;
146 m_windowDisabler
= NULL
;
150 EndModal(wxID_CANCEL
);
153 bool ret
= wxDialogBase::Show(show
);
161 bool wxDialog::IsModal() const
163 return m_isShowingModal
;
166 int wxDialog::ShowModal()
170 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
171 return GetReturnCode();
174 // use the apps top level window as parent if none given unless explicitly
176 if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
178 wxWindow
*parent
= wxTheApp
->GetTopWindow();
179 if ( parent
&& parent
!= this )
187 m_isShowingModal
= true;
189 wxASSERT_MSG( !m_windowDisabler
, _T("disabling windows twice?") );
191 #if defined(__WXGTK__) || defined(__WXMGL__)
192 wxBusyCursorSuspender suspender
;
193 // FIXME (FIXME_MGL) - make sure busy cursor disappears under MSW too
196 m_windowDisabler
= new wxWindowDisabler(this);
198 m_eventLoop
= new wxEventLoop
;
202 return GetReturnCode();
205 void wxDialog::EndModal(int retCode
)
207 wxASSERT_MSG( m_eventLoop
, _T("wxDialog is not modal") );
209 SetReturnCode(retCode
);
213 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
217 m_isShowingModal
= false;