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"
25 #include "wx/dialog.h"
30 #include "wx/evtloop.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 BEGIN_EVENT_TABLE(wxDialog
,wxDialogBase
)
37 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
38 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
39 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
40 EVT_CLOSE (wxDialog::OnCloseWindow
)
43 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxTopLevelWindow
)
48 m_windowDisabler
= NULL
;
50 m_isShowingModal
= false;
58 bool wxDialog::Create(wxWindow
*parent
,
59 wxWindowID id
, const wxString
&title
,
60 const wxPoint
&pos
, const wxSize
&size
,
61 long style
, const wxString
&name
)
63 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
65 // all dialogs should have tab traversal enabled
66 style
|= wxTAB_TRAVERSAL
;
68 return wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
);
71 void wxDialog::OnApply(wxCommandEvent
&WXUNUSED(event
))
74 TransferDataFromWindow();
77 void wxDialog::OnCancel(wxCommandEvent
&WXUNUSED(event
))
81 EndModal(wxID_CANCEL
);
85 SetReturnCode(wxID_CANCEL
);
90 void wxDialog::OnOK(wxCommandEvent
&WXUNUSED(event
))
92 if ( Validate() && TransferDataFromWindow() )
100 SetReturnCode(wxID_OK
);
106 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
108 // We'll send a Cancel message by default,
109 // which may close the dialog.
110 // Check for looping if the Cancel event handler calls Close().
112 // Note that if a cancel button and handler aren't present in the dialog,
113 // nothing will happen when you close the dialog via the window manager, or
115 // We wouldn't want to destroy the dialog by default, since the dialog may have been
116 // created on the stack.
117 // However, this does mean that calling dialog->Close() won't delete the dialog
118 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
119 // sure to destroy the dialog.
120 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
122 static wxList s_closing
;
124 if (s_closing
.Member(this))
127 s_closing
.Append(this);
129 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
130 cancelEvent
.SetEventObject(this);
131 GetEventHandler()->ProcessEvent(cancelEvent
);
132 s_closing
.DeleteObject(this);
135 bool wxDialog::Show(bool show
)
139 // if we had disabled other app windows, reenable them back now because
140 // if they stay disabled Windows will activate another window (one
141 // which is enabled, anyhow) and we will lose activation
142 if ( m_windowDisabler
)
144 delete m_windowDisabler
;
145 m_windowDisabler
= NULL
;
149 EndModal(wxID_CANCEL
);
152 bool ret
= wxDialogBase::Show(show
);
160 bool wxDialog::IsModal() const
162 return m_isShowingModal
;
165 int wxDialog::ShowModal()
169 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
170 return GetReturnCode();
173 // use the apps top level window as parent if none given unless explicitly
175 if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
) )
177 wxWindow
*parent
= wxTheApp
->GetTopWindow();
178 if ( parent
&& parent
!= this )
186 m_isShowingModal
= true;
188 wxASSERT_MSG( !m_windowDisabler
, _T("disabling windows twice?") );
190 #if defined(__WXGTK__) || defined(__WXMGL__)
191 wxBusyCursorSuspender suspender
;
192 // FIXME (FIXME_MGL) - make sure busy cursor disappears under MSW too
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
, _T("wxDialog is not modal") );
208 SetReturnCode(retCode
);
212 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
216 m_isShowingModal
= false;