1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDialog class
8 // Copyright: (c) AUTHOR
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
, wxPanel
)
33 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
34 EVT_SIZE(wxDialog::OnSize
)
35 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
36 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
37 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
38 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
39 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
40 EVT_CLOSE(wxDialog::OnCloseWindow
)
48 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
51 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
52 const wxString
& title
,
59 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
62 wxTopLevelWindows
.Append(this);
64 if (parent
) parent
->AddChild(this);
67 m_windowId
= (int)NewControlId();
71 MacCreateRealWindow( title
, pos
, size
, MacRemoveBordersFromStyle(style
) , name
) ;
73 m_macWindowData
->m_macWindowBackgroundTheme
= kThemeBrushDialogBackgroundActive
;
77 void wxDialog::SetModal(bool flag
)
81 m_windowStyle
|= wxDIALOG_MODAL
;
83 wxModelessWindows
.DeleteObject(this);
87 m_windowStyle
&= ~wxDIALOG_MODAL
;
89 wxModelessWindows
.Append(this);
95 m_isBeingDeleted
= TRUE
;
96 wxTopLevelWindows
.DeleteObject(this);
101 wxModelessWindows
.DeleteObject(this);
103 // If this is the last top-level window, exit.
104 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
106 wxTheApp
->SetTopWindow(NULL
);
108 if (wxTheApp
->GetExitOnFrameDelete())
110 wxTheApp
->ExitMainLoop() ;
115 // By default, pressing escape cancels the dialog
116 void wxDialog::OnCharHook(wxKeyEvent
& event
)
118 if (event
.m_keyCode
== WXK_ESCAPE
)
120 // Behaviour changed in 2.0: we'll send a Cancel message
121 // to the dialog instead of Close.
122 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
123 cancelEvent
.SetEventObject( this );
124 GetEventHandler()->ProcessEvent(cancelEvent
);
128 // We didn't process this event.
132 void wxDialog::Iconize(bool WXUNUSED(iconize
))
134 // mac dialogs cannot be iconized
137 bool wxDialog::IsIconized() const
139 // mac dialogs cannot be iconized
143 void wxDialog::DoSetClientSize(int width
, int height
)
145 wxWindow::DoSetClientSize( width
, height
) ;
148 void wxDialog::DoGetPosition(int *x
, int *y
) const
150 wxWindow::DoGetPosition( x
, y
) ;
153 bool wxDialog::IsModal() const
155 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
159 bool wxDialog::IsModalShowing() const
161 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
165 extern bool s_macIsInModalLoop
;
167 bool wxDialog::Show(bool show
)
169 if ( !wxDialogBase::Show(show
) )
177 // usually will result in TransferDataToWindow() being called
187 else // end of modal dialog
189 // this will cause IsModalShowing() return FALSE and our local
190 // message loop will terminate
191 wxModalDialogs
.DeleteObject(this);
198 void wxDialog::DoShowModal()
200 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
202 wxModalDialogs
.Append(this);
204 wxWindow
*parent
= GetParent();
206 // remember where the focus was
207 wxWindow
*winFocus
= FindFocus();
214 winFocus
= wxTheApp
->GetTopWindow();
216 // TODO : test whether parent gets disabled
218 bool formerModal
= s_macIsInModalLoop
;
219 s_macIsInModalLoop
= true ;
221 while ( IsModalShowing() )
223 while ( !wxTheApp
->Pending() && wxTheApp
->ProcessIdle() )
226 wxTheApp
->MacDoOneEvent() ;
229 s_macIsInModalLoop
= formerModal
;
231 // TODO probably reenable the parent window if any
236 winFocus
->SetFocus();
241 // Replacement for Show(TRUE) for modal dialogs - returns return code
242 int wxDialog::ShowModal()
244 m_windowStyle
|= wxDIALOG_MODAL
;
246 return GetReturnCode();
249 // NB: this function (surprizingly) may be called for both modal and modeless
250 // dialogs and should work for both of them
251 void wxDialog::EndModal(int retCode
)
253 SetReturnCode(retCode
);
258 void wxDialog::OnOK(wxCommandEvent
& event
)
260 if ( Validate() && TransferDataFromWindow() )
266 void wxDialog::OnApply(wxCommandEvent
& event
)
269 TransferDataFromWindow();
270 // TODO probably need to disable the Apply button until things change again
273 void wxDialog::OnCancel(wxCommandEvent
& event
)
275 EndModal(wxID_CANCEL
);
278 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
280 // We'll send a Cancel message by default,
281 // which may close the dialog.
282 // Check for looping if the Cancel event handler calls Close().
284 // Note that if a cancel button and handler aren't present in the dialog,
285 // nothing will happen when you close the dialog via the window manager, or
287 // We wouldn't want to destroy the dialog by default, since the dialog may have been
288 // created on the stack.
289 // However, this does mean that calling dialog->Close() won't delete the dialog
290 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
291 // sure to destroy the dialog.
292 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
294 static wxList closing
;
296 if ( closing
.Member(this) )
299 closing
.Append(this);
301 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
302 cancelEvent
.SetEventObject( this );
303 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
305 closing
.DeleteObject(this);
308 // Destroy the window (delayed, if a managed window)
309 bool wxDialog::Destroy()
311 wxCHECK_MSG( !wxPendingDelete
.Member(this), FALSE
,
312 _T("wxDialog destroyed twice") );
314 wxPendingDelete
.Append(this);
318 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
320 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));