1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDialog class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dialog.h"
16 #include "wx/dialog.h"
20 #include "wx/settings.h"
22 #include "wx/mac/uma.h"
24 // Lists to keep track of windows, so we can disable/enable them
26 wxList wxModalDialogs
;
27 //wxList wxModelessWindows; // Frames and modeless dialogs
28 extern wxList wxPendingDelete
;
30 #if !USE_SHARED_LIBRARY
31 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
33 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
34 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
35 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
36 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
38 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
40 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
42 EVT_CLOSE(wxDialog::OnCloseWindow
)
49 m_isModalStyle
= false;
52 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
53 const wxString
& title
,
59 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
61 // All dialogs should really have this style
62 style
|= wxTAB_TRAVERSAL
;
64 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
& ~(wxYES
|wxOK
|wxNO
|wxCANCEL
) , name
) )
70 void wxDialog::SetModal(bool flag
)
74 m_isModalStyle
= true;
76 wxModelessWindows
.DeleteObject(this);
78 SetWindowModality( (WindowRef
) MacGetWindowRef() , kWindowModalityAppModal
, NULL
) ;
83 m_isModalStyle
= false;
85 wxModelessWindows
.Append(this);
91 m_isBeingDeleted
= TRUE
;
95 // By default, pressing escape cancels the dialog , on mac command-stop does the same thing
96 void wxDialog::OnCharHook(wxKeyEvent
& event
)
98 if (( event
.m_keyCode
== WXK_ESCAPE
||
99 ( event
.m_keyCode
== '.' && event
.MetaDown() ) )
100 && FindWindow(wxID_CANCEL
) )
102 // Behaviour changed in 2.0: we'll send a Cancel message
103 // to the dialog instead of Close.
104 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
105 cancelEvent
.SetEventObject( this );
106 GetEventHandler()->ProcessEvent(cancelEvent
);
110 // We didn't process this event.
114 bool wxDialog::IsModal() const
116 return m_isModalStyle
;
120 bool wxDialog::IsModalShowing() const
122 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
125 bool wxDialog::Show(bool show
)
127 if ( !wxDialogBase::Show(show
) )
135 // usually will result in TransferDataToWindow() being called
145 else // end of modal dialog
147 // this will cause IsModalShowing() return FALSE and our local
148 // message loop will terminate
149 wxModalDialogs
.DeleteObject(this);
157 extern bool s_macIsInModalLoop
;
160 void wxDialog::DoShowModal()
162 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
164 wxModalDialogs
.Append(this);
169 BeginAppModalStateForWindow( (WindowRef
) MacGetWindowRef()) ;
171 // TODO : test whether parent gets disabled
172 bool formerModal
= s_macIsInModalLoop
;
173 s_macIsInModalLoop
= true ;
175 while ( IsModalShowing() )
177 wxTheApp
->MacDoOneEvent() ;
178 // calls process idle itself
182 EndAppModalStateForWindow( (WindowRef
) MacGetWindowRef() ) ;
184 // TODO probably reenable the parent window if any
185 s_macIsInModalLoop
= formerModal
;
190 // Replacement for Show(TRUE) for modal dialogs - returns return code
191 int wxDialog::ShowModal()
199 return GetReturnCode();
202 // NB: this function (surprizingly) may be called for both modal and modeless
203 // dialogs and should work for both of them
204 void wxDialog::EndModal(int retCode
)
206 SetReturnCode(retCode
);
211 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
213 if ( Validate() && TransferDataFromWindow() )
219 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
222 TransferDataFromWindow();
223 // TODO probably need to disable the Apply button until things change again
226 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
228 EndModal(wxID_CANCEL
);
231 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
233 // We'll send a Cancel message by default,
234 // which may close the dialog.
235 // Check for looping if the Cancel event handler calls Close().
237 // Note that if a cancel button and handler aren't present in the dialog,
238 // nothing will happen when you close the dialog via the window manager, or
240 // We wouldn't want to destroy the dialog by default, since the dialog may have been
241 // created on the stack.
242 // However, this does mean that calling dialog->Close() won't delete the dialog
243 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
244 // sure to destroy the dialog.
245 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
247 static wxList closing
;
249 if ( closing
.Member(this) )
252 closing
.Append(this);
254 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
255 cancelEvent
.SetEventObject( this );
256 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
258 closing
.DeleteObject(this);
261 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
263 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));