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
)
47 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
50 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
51 const wxString
& title
,
57 m_windowStyle
= style
;
59 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
63 wxTopLevelWindows
.Append(this);
65 if (parent
) parent
->AddChild(this);
68 m_windowId
= (int)NewControlId();
88 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
89 m_macWindowData
= new MacWindowData() ;
91 WindowClass wclass
= kMovableModalWindowClass
;
92 WindowAttributes attr
= kWindowNoAttributes
;
94 if ( ( m_windowStyle
& wxMINIMIZE_BOX
) || ( m_windowStyle
& wxMAXIMIZE_BOX
) )
96 attr
|= kWindowFullZoomAttribute
;
97 attr
|= kWindowResizableAttribute
;
100 UMACreateNewWindow( wclass
, attr
, &theBoundsRect
, &m_macWindowData
->m_macWindow
) ;
101 wxAssociateWinWithMacWindow( m_macWindowData
->m_macWindow
, this ) ;
103 if( wxApp::s_macDefaultEncodingIsPC
)
104 label
= wxMacMakeMacStringFromPC( title
) ;
107 UMASetWTitleC( m_macWindowData
->m_macWindow
, label
) ;
108 m_macWindowData
->m_macWindowBackgroundTheme
= kThemeBrushDialogBackgroundActive
;
109 UMACreateRootControl( m_macWindowData
->m_macWindow
, &m_macWindowData
->m_macRootControl
) ;
110 m_macWindowData
->m_macFocus
= NULL
;
114 void wxDialog::SetModal(bool flag
)
117 m_windowStyle
|= wxDIALOG_MODAL
;
119 if ( m_windowStyle
& wxDIALOG_MODAL
)
120 m_windowStyle
-= wxDIALOG_MODAL
;
122 wxModelessWindows
.DeleteObject(this);
124 wxModelessWindows
.Append(this);
127 wxDialog::~wxDialog()
129 m_isBeingDeleted
= TRUE
;
130 wxTopLevelWindows
.DeleteObject(this);
132 m_modalShowing
= FALSE
;
134 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
135 wxModelessWindows
.DeleteObject(this);
137 // If this is the last top-level window, exit.
138 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
140 wxTheApp
->SetTopWindow(NULL
);
142 if (wxTheApp
->GetExitOnFrameDelete())
144 wxTheApp
->ExitMainLoop() ;
149 // By default, pressing escape cancels the dialog
150 void wxDialog::OnCharHook(wxKeyEvent
& event
)
152 if (event
.m_keyCode
== WXK_ESCAPE
)
154 // Behaviour changed in 2.0: we'll send a Cancel message
155 // to the dialog instead of Close.
156 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
157 cancelEvent
.SetEventObject( this );
158 GetEventHandler()->ProcessEvent(cancelEvent
);
162 // We didn't process this event.
166 void wxDialog::Iconize(bool WXUNUSED(iconize
))
171 bool wxDialog::IsIconized() const
177 extern bool s_macIsInModalLoop
;
179 bool wxDialog::Show(bool show
)
186 bool modal
= ((GetWindowStyleFlag() & wxDIALOG_MODAL
) == wxDIALOG_MODAL
) ;
188 #if WXGARBAGE_COLLECTION_ON /* MATTHEW: GC */
193 if (!wxModelessWindows
.Find(this))
194 wxModelessWindows
.Append(this);
197 wxModelessWindows
.DeleteObject(this);
201 if (!wxTopLevelWindows
.Find(this))
202 wxTopLevelWindows
.Append(this);
205 wxTopLevelWindows
.DeleteObject(this);
210 s_macIsInModalLoop
= true ;
215 // BringWindowToTop((HWND) GetHWND());
219 m_modalShowing
= TRUE
;
220 // if we don't do it, some window might be deleted while we have pointers
221 // to them in our disabledWindows list and the program will crash when it
222 // will try to reenable them after the modal dialog end
223 wxTheApp
->DeletePendingObjects();
225 UMAShowWindow( m_macWindowData
->m_macWindow
) ;
226 UMASelectWindow( m_macWindowData
->m_macWindow
) ;
228 if (!wxModalDialogs
.Member(this))
229 wxModalDialogs
.Append(this);
231 while (wxModalDialogs
.Member(this) )
233 wxTheApp
->MacDoOneEvent() ;
238 wxModalDialogs
.DeleteObject(this);
239 UMAHideWindow( m_macWindowData
->m_macWindow
) ;
241 s_macIsInModalLoop
= false ;
247 UMAShowWindow( m_macWindowData
->m_macWindow
) ;
248 UMASelectWindow( m_macWindowData
->m_macWindow
) ;
252 UMAHideWindow( m_macWindowData
->m_macWindow
) ;
259 // Replacement for Show(TRUE) for modal dialogs - returns return code
260 int wxDialog::ShowModal()
262 m_windowStyle
|= wxDIALOG_MODAL
;
264 return GetReturnCode();
267 void wxDialog::EndModal(int retCode
)
269 SetReturnCode(retCode
);
270 // TODO modal un-showing
275 void wxDialog::OnOK(wxCommandEvent
& event
)
277 if ( Validate() && TransferDataFromWindow() )
283 SetReturnCode(wxID_OK
);
289 void wxDialog::OnApply(wxCommandEvent
& event
)
292 TransferDataFromWindow();
293 // TODO probably need to disable the Apply button until things change again
296 void wxDialog::OnCancel(wxCommandEvent
& event
)
299 EndModal(wxID_CANCEL
);
302 SetReturnCode(wxID_CANCEL
);
307 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
309 // We'll send a Cancel message by default,
310 // which may close the dialog.
311 // Check for looping if the Cancel event handler calls Close().
313 // Note that if a cancel button and handler aren't present in the dialog,
314 // nothing will happen when you close the dialog via the window manager, or
316 // We wouldn't want to destroy the dialog by default, since the dialog may have been
317 // created on the stack.
318 // However, this does mean that calling dialog->Close() won't delete the dialog
319 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
320 // sure to destroy the dialog.
321 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
323 static wxList closing
;
325 if ( closing
.Member(this) )
328 closing
.Append(this);
330 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
331 cancelEvent
.SetEventObject( this );
332 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
334 closing
.DeleteObject(this);
337 // Destroy the window (delayed, if a managed window)
338 bool wxDialog::Destroy()
340 if (!wxPendingDelete
.Member(this))
341 wxPendingDelete
.Append(this);
345 void wxDialog::OnSize(wxSizeEvent
& WXUNUSED(event
))
347 // if we're using constraints - do use them
348 #if wxUSE_CONSTRAINTS
349 if ( GetAutoLayout() ) {
355 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
357 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));