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 // Lists to keep track of windows, so we can disable/enable them
24 wxList wxModalDialogs
;
25 wxList wxModelessWindows
; // Frames and modeless dialogs
26 extern wxList wxPendingDelete
;
28 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
30 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
31 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
32 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
33 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
34 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
35 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
36 EVT_CLOSE(wxDialog::OnCloseWindow
)
42 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
45 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
46 const wxString
& title
,
52 m_windowStyle
= style
;
54 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
58 wxTopLevelWindows
.Append(this);
60 if (parent
) parent
->AddChild(this);
63 m_windowId
= (int)NewControlId();
67 // TODO: create dialog
72 void wxDialog::SetModal(bool flag
)
75 m_windowStyle
|= wxDIALOG_MODAL
;
77 if ( m_windowStyle
& wxDIALOG_MODAL
)
78 m_windowStyle
-= wxDIALOG_MODAL
;
80 wxModelessWindows
.DeleteObject(this);
82 wxModelessWindows
.Append(this);
88 wxTopLevelWindows
.DeleteObject(this);
90 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
91 wxModelessWindows
.DeleteObject(this);
93 // If this is the last top-level window, exit.
94 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
96 wxTheApp
->SetTopWindow(NULL
);
98 if (wxTheApp
->GetExitOnFrameDelete())
105 // By default, pressing escape cancels the dialog
106 void wxDialog::OnCharHook(wxKeyEvent
& event
)
108 if (event
.m_keyCode
== WXK_ESCAPE
)
110 // Behaviour changed in 2.0: we'll send a Cancel message
111 // to the dialog instead of Close.
112 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
113 cancelEvent
.SetEventObject( this );
114 GetEventHandler()->ProcessEvent(cancelEvent
);
118 // We didn't process this event.
122 void wxDialog::Iconize(bool WXUNUSED(iconize
))
127 bool wxDialog::IsIconized() const
133 void wxDialog::SetClientSize(int width
, int height
)
138 void wxDialog::GetPosition(int *x
, int *y
) const
143 bool wxDialog::Show(bool show
)
149 void wxDialog::SetTitle(const wxString
& title
)
154 wxString
wxDialog::GetTitle() const
160 void wxDialog::Centre(int direction
)
162 int x_offset
,y_offset
;
163 int display_width
, display_height
;
164 int width
, height
, x
, y
;
165 wxWindow
*parent
= GetParent();
166 if ((direction
& wxCENTER_FRAME
) && parent
)
168 parent
->GetPosition(&x_offset
,&y_offset
) ;
169 parent
->GetSize(&display_width
,&display_height
) ;
173 wxDisplaySize(&display_width
, &display_height
);
178 GetSize(&width
, &height
);
181 if (direction
& wxHORIZONTAL
)
182 x
= (int)((display_width
- width
)/2);
183 if (direction
& wxVERTICAL
)
184 y
= (int)((display_height
- height
)/2);
186 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
189 // Replacement for Show(TRUE) for modal dialogs - returns return code
190 int wxDialog::ShowModal()
192 m_windowStyle
|= wxDIALOG_MODAL
;
193 // TODO: modal showing
195 return GetReturnCode();
198 void wxDialog::EndModal(int retCode
)
200 SetReturnCode(retCode
);
201 // TODO modal un-showing
206 void wxDialog::OnOK(wxCommandEvent
& event
)
208 if ( Validate() && TransferDataFromWindow() )
214 SetReturnCode(wxID_OK
);
220 void wxDialog::OnApply(wxCommandEvent
& event
)
223 TransferDataFromWindow();
224 // TODO probably need to disable the Apply button until things change again
227 void wxDialog::OnCancel(wxCommandEvent
& event
)
230 EndModal(wxID_CANCEL
);
233 SetReturnCode(wxID_CANCEL
);
238 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
240 // We'll send a Cancel message by default,
241 // which may close the dialog.
242 // Check for looping if the Cancel event handler calls Close().
244 // Note that if a cancel button and handler aren't present in the dialog,
245 // nothing will happen when you close the dialog via the window manager, or
247 // We wouldn't want to destroy the dialog by default, since the dialog may have been
248 // created on the stack.
249 // However, this does mean that calling dialog->Close() won't delete the dialog
250 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
251 // sure to destroy the dialog.
252 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
254 static wxList closing
;
256 if ( closing
.Member(this) )
259 closing
.Append(this);
261 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
262 cancelEvent
.SetEventObject( this );
263 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
265 closing
.DeleteObject(this);
268 // Destroy the window (delayed, if a managed window)
269 bool wxDialog::Destroy()
271 if (!wxPendingDelete
.Member(this))
272 wxPendingDelete
.Append(this);
276 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
278 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));