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
)
50 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
53 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
54 const wxString
& title
,
60 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
62 // All dialogs should really have this style
63 style
|= wxTAB_TRAVERSAL
;
65 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
68 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
73 void wxDialog::SetModal(bool flag
)
77 m_windowStyle
|= wxDIALOG_MODAL
;
79 wxModelessWindows
.DeleteObject(this);
81 SetWindowModality( (WindowRef
) MacGetWindowRef() , kWindowModalityAppModal
, NULL
) ;
86 m_windowStyle
&= ~wxDIALOG_MODAL
;
88 wxModelessWindows
.Append(this);
94 m_isBeingDeleted
= TRUE
;
98 // By default, pressing escape cancels the dialog , on mac command-stop does the same thing
99 void wxDialog::OnCharHook(wxKeyEvent
& event
)
101 if (( event
.m_keyCode
== WXK_ESCAPE
||
102 ( event
.m_keyCode
== '.' && event
.MetaDown() ) )
103 && FindWindow(wxID_CANCEL
) )
105 // Behaviour changed in 2.0: we'll send a Cancel message
106 // to the dialog instead of Close.
107 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
108 cancelEvent
.SetEventObject( this );
109 GetEventHandler()->ProcessEvent(cancelEvent
);
113 // We didn't process this event.
117 bool wxDialog::IsModal() const
119 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
123 bool wxDialog::IsModalShowing() const
125 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
128 bool wxDialog::Show(bool show
)
130 if ( !wxDialogBase::Show(show
) )
138 // usually will result in TransferDataToWindow() being called
148 else // end of modal dialog
150 // this will cause IsModalShowing() return FALSE and our local
151 // message loop will terminate
152 wxModalDialogs
.DeleteObject(this);
160 extern bool s_macIsInModalLoop
;
163 void wxDialog::DoShowModal()
165 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
167 wxModalDialogs
.Append(this);
170 BeginAppModalStateForWindow( (WindowRef
) MacGetWindowRef()) ;
172 // TODO : test whether parent gets disabled
173 bool formerModal
= s_macIsInModalLoop
;
174 s_macIsInModalLoop
= true ;
176 while ( IsModalShowing() )
178 wxTheApp
->MacDoOneEvent() ;
179 // calls process idle itself
183 EndAppModalStateForWindow( (WindowRef
) MacGetWindowRef() ) ;
185 // TODO probably reenable the parent window if any
186 s_macIsInModalLoop
= formerModal
;
191 // Replacement for Show(TRUE) for modal dialogs - returns return code
192 int wxDialog::ShowModal()
200 return GetReturnCode();
203 // NB: this function (surprizingly) may be called for both modal and modeless
204 // dialogs and should work for both of them
205 void wxDialog::EndModal(int retCode
)
207 SetReturnCode(retCode
);
212 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
214 if ( Validate() && TransferDataFromWindow() )
220 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
223 TransferDataFromWindow();
224 // TODO probably need to disable the Apply button until things change again
227 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
229 EndModal(wxID_CANCEL
);
232 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
234 // We'll send a Cancel message by default,
235 // which may close the dialog.
236 // Check for looping if the Cancel event handler calls Close().
238 // Note that if a cancel button and handler aren't present in the dialog,
239 // nothing will happen when you close the dialog via the window manager, or
241 // We wouldn't want to destroy the dialog by default, since the dialog may have been
242 // created on the stack.
243 // However, this does mean that calling dialog->Close() won't delete the dialog
244 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
245 // sure to destroy the dialog.
246 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
248 static wxList closing
;
250 if ( closing
.Member(this) )
253 closing
.Append(this);
255 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
256 cancelEvent
.SetEventObject( this );
257 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
259 closing
.DeleteObject(this);
262 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
264 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));