1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDialog class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/dialog.h"
16 #include "wx/settings.h"
18 #include "wx/mac/uma.h"
20 // Lists to keep track of windows, so we can disable/enable them
22 wxList wxModalDialogs
;
23 //wxList wxModelessWindows; // Frames and modeless dialogs
24 extern wxList wxPendingDelete
;
26 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
28 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
29 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
30 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
31 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
33 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
35 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
37 EVT_CLOSE(wxDialog::OnCloseWindow
)
42 m_isModalStyle
= false;
43 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
46 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
47 const wxString
& title
,
53 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
56 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
59 MacCreateRealWindow( title
, pos
, size
, MacRemoveBordersFromStyle(style
) & ~(wxYES
|wxOK
|wxNO
|wxCANCEL
) , name
) ;
61 m_macWindowBackgroundTheme
= kThemeBrushDialogBackgroundActive
;
62 SetThemeWindowBackground( (WindowRef
) m_macWindow
, m_macWindowBackgroundTheme
, false ) ;
67 void wxDialog::SetModal(bool flag
)
71 m_isModalStyle
= true;
73 wxModelessWindows
.DeleteObject(this);
75 SetWindowModality( (WindowRef
) MacGetWindowRef() , kWindowModalityAppModal
, NULL
) ;
80 m_isModalStyle
= false;
82 wxModelessWindows
.Append(this);
88 m_isBeingDeleted
= TRUE
;
92 // By default, pressing escape cancels the dialog , on mac command-stop does the same thing
93 void wxDialog::OnCharHook(wxKeyEvent
& event
)
95 if (( event
.m_keyCode
== WXK_ESCAPE
||
96 ( event
.m_keyCode
== '.' && event
.MetaDown() ) )
97 && FindWindow(wxID_CANCEL
) )
99 // Behaviour changed in 2.0: we'll send a Cancel message
100 // to the dialog instead of Close.
101 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
102 cancelEvent
.SetEventObject( this );
103 GetEventHandler()->ProcessEvent(cancelEvent
);
107 // We didn't process this event.
111 bool wxDialog::IsModal() const
113 return m_isModalStyle
;
117 bool wxDialog::IsModalShowing() const
119 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
122 bool wxDialog::Show(bool show
)
124 if ( !wxDialogBase::Show(show
) )
132 // usually will result in TransferDataToWindow() being called
142 else // end of modal dialog
144 // this will cause IsModalShowing() return FALSE and our local
145 // message loop will terminate
146 wxModalDialogs
.DeleteObject(this);
154 extern bool s_macIsInModalLoop
;
157 void wxDialog::DoShowModal()
159 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
161 wxModalDialogs
.Append(this);
164 BeginAppModalStateForWindow( (WindowRef
) MacGetWindowRef()) ;
166 // TODO : test whether parent gets disabled
167 bool formerModal
= s_macIsInModalLoop
;
168 s_macIsInModalLoop
= true ;
170 while ( IsModalShowing() )
172 wxTheApp
->MacDoOneEvent() ;
173 // calls process idle itself
177 EndAppModalStateForWindow( (WindowRef
) MacGetWindowRef() ) ;
179 // TODO probably reenable the parent window if any
180 s_macIsInModalLoop
= formerModal
;
185 // Replacement for Show(TRUE) for modal dialogs - returns return code
186 int wxDialog::ShowModal()
194 return GetReturnCode();
197 // NB: this function (surprizingly) may be called for both modal and modeless
198 // dialogs and should work for both of them
199 void wxDialog::EndModal(int retCode
)
201 SetReturnCode(retCode
);
206 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
208 if ( Validate() && TransferDataFromWindow() )
214 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
217 TransferDataFromWindow();
218 // TODO probably need to disable the Apply button until things change again
221 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
223 EndModal(wxID_CANCEL
);
226 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
228 // We'll send a Cancel message by default,
229 // which may close the dialog.
230 // Check for looping if the Cancel event handler calls Close().
232 // Note that if a cancel button and handler aren't present in the dialog,
233 // nothing will happen when you close the dialog via the window manager, or
235 // We wouldn't want to destroy the dialog by default, since the dialog may have been
236 // created on the stack.
237 // However, this does mean that calling dialog->Close() won't delete the dialog
238 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
239 // sure to destroy the dialog.
240 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
242 static wxList closing
;
244 if ( closing
.Member(this) )
247 closing
.Append(this);
249 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
250 cancelEvent
.SetEventObject( this );
251 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
253 closing
.DeleteObject(this);
256 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
258 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));