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 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
63 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
66 MacCreateRealWindow( title
, pos
, size
, MacRemoveBordersFromStyle(style
) , name
) ;
68 m_macWindowBackgroundTheme
= kThemeBrushDialogBackgroundActive
;
69 SetThemeWindowBackground( (WindowRef
) m_macWindow
, m_macWindowBackgroundTheme
, false ) ;
74 void wxDialog::SetModal(bool flag
)
78 m_windowStyle
|= wxDIALOG_MODAL
;
80 wxModelessWindows
.DeleteObject(this);
82 SetWindowModality( (WindowRef
) MacGetWindowRef() , kWindowModalityAppModal
, NULL
) ;
87 m_windowStyle
&= ~wxDIALOG_MODAL
;
89 wxModelessWindows
.Append(this);
95 m_isBeingDeleted
= TRUE
;
99 // By default, pressing escape cancels the dialog , on mac command-stop does the same thing
100 void wxDialog::OnCharHook(wxKeyEvent
& event
)
102 if (( event
.m_keyCode
== WXK_ESCAPE
||
103 ( event
.m_keyCode
== '.' && event
.MetaDown() ) )
104 && FindWindow(wxID_CANCEL
) )
106 // Behaviour changed in 2.0: we'll send a Cancel message
107 // to the dialog instead of Close.
108 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
109 cancelEvent
.SetEventObject( this );
110 GetEventHandler()->ProcessEvent(cancelEvent
);
114 // We didn't process this event.
118 bool wxDialog::IsModal() const
120 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
124 bool wxDialog::IsModalShowing() const
126 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
129 bool wxDialog::Show(bool show
)
131 if ( !wxDialogBase::Show(show
) )
139 // usually will result in TransferDataToWindow() being called
149 else // end of modal dialog
151 // this will cause IsModalShowing() return FALSE and our local
152 // message loop will terminate
153 wxModalDialogs
.DeleteObject(this);
161 extern bool s_macIsInModalLoop
;
164 void wxDialog::DoShowModal()
166 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
168 wxModalDialogs
.Append(this);
170 wxWindow
*parent
= GetParent();
172 // remember where the focus was
173 wxWindow
*winFocus
= FindFocus();
180 winFocus
= wxTheApp
->GetTopWindow();
183 BeginAppModalStateForWindow( (WindowRef
) MacGetWindowRef()) ;
185 // TODO : test whether parent gets disabled
186 bool formerModal
= s_macIsInModalLoop
;
187 s_macIsInModalLoop
= true ;
189 while ( IsModalShowing() )
191 wxTheApp
->MacDoOneEvent() ;
192 // calls process idle itself
196 EndAppModalStateForWindow( (WindowRef
) MacGetWindowRef() ) ;
198 // TODO probably reenable the parent window if any
199 s_macIsInModalLoop
= formerModal
;
206 winFocus
->SetFocus();
211 // Replacement for Show(TRUE) for modal dialogs - returns return code
212 int wxDialog::ShowModal()
220 return GetReturnCode();
223 // NB: this function (surprizingly) may be called for both modal and modeless
224 // dialogs and should work for both of them
225 void wxDialog::EndModal(int retCode
)
227 SetReturnCode(retCode
);
232 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
234 if ( Validate() && TransferDataFromWindow() )
240 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
243 TransferDataFromWindow();
244 // TODO probably need to disable the Apply button until things change again
247 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
249 EndModal(wxID_CANCEL
);
252 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
254 // We'll send a Cancel message by default,
255 // which may close the dialog.
256 // Check for looping if the Cancel event handler calls Close().
258 // Note that if a cancel button and handler aren't present in the dialog,
259 // nothing will happen when you close the dialog via the window manager, or
261 // We wouldn't want to destroy the dialog by default, since the dialog may have been
262 // created on the stack.
263 // However, this does mean that calling dialog->Close() won't delete the dialog
264 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
265 // sure to destroy the dialog.
266 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
268 static wxList closing
;
270 if ( closing
.Member(this) )
273 closing
.Append(this);
275 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
276 cancelEvent
.SetEventObject( this );
277 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
279 closing
.DeleteObject(this);
282 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
284 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));