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 #if !USE_SHARED_LIBRARY
29 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
31 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
32 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
33 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
34 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
35 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
36 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
37 EVT_CLOSE(wxDialog::OnCloseWindow
)
44 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
47 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
48 const wxString
& title
,
54 m_windowStyle
= style
;
56 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
60 wxTopLevelWindows
.Append(this);
62 if (parent
) parent
->AddChild(this);
65 m_windowId
= (int)NewControlId();
69 // TODO: create dialog
74 void wxDialog::SetModal(bool flag
)
77 m_windowStyle
|= wxDIALOG_MODAL
;
79 if ( m_windowStyle
& wxDIALOG_MODAL
)
80 m_windowStyle
-= wxDIALOG_MODAL
;
82 wxModelessWindows
.DeleteObject(this);
84 wxModelessWindows
.Append(this);
90 wxTopLevelWindows
.DeleteObject(this);
92 if ( (GetWindowStyleFlag() & wxDIALOG_MODAL
) != wxDIALOG_MODAL
)
93 wxModelessWindows
.DeleteObject(this);
95 // If this is the last top-level window, exit.
96 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
98 wxTheApp
->SetTopWindow(NULL
);
100 if (wxTheApp
->GetExitOnFrameDelete())
107 // By default, pressing escape cancels the dialog
108 void wxDialog::OnCharHook(wxKeyEvent
& event
)
112 if (event
.m_keyCode
== WXK_ESCAPE
)
114 // Behaviour changed in 2.0: we'll send a Cancel message
115 // to the dialog instead of Close.
116 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
117 cancelEvent
.SetEventObject( this );
118 GetEventHandler()->ProcessEvent(cancelEvent
);
123 // We didn't process this event.
127 void wxDialog::Iconize(bool WXUNUSED(iconize
))
132 bool wxDialog::IsIconized() const
138 void wxDialog::SetClientSize(int width
, int height
)
143 void wxDialog::GetPosition(int *x
, int *y
) const
148 bool wxDialog::Show(bool show
)
154 void wxDialog::SetTitle(const wxString
& title
)
159 wxString
wxDialog::GetTitle() const
165 void wxDialog::Centre(int direction
)
167 int x_offset
,y_offset
;
168 int display_width
, display_height
;
169 int width
, height
, x
, y
;
171 if (direction
& wxCENTER_FRAME
)
173 frame
= (wxFrame
*)GetParent() ;
176 frame
->GetPosition(&x_offset
,&y_offset
) ;
177 frame
->GetSize(&display_width
,&display_height
) ;
185 wxDisplaySize(&display_width
, &display_height
);
190 GetSize(&width
, &height
);
193 if (direction
& wxHORIZONTAL
)
194 x
= (int)((display_width
- width
)/2);
195 if (direction
& wxVERTICAL
)
196 y
= (int)((display_height
- height
)/2);
198 SetSize(x
+x_offset
, y
+y_offset
, width
, height
);
201 // Replacement for Show(TRUE) for modal dialogs - returns return code
202 int wxDialog::ShowModal()
204 m_windowStyle
|= wxDIALOG_MODAL
;
205 // TODO: modal showing
207 return GetReturnCode();
210 void wxDialog::EndModal(int retCode
)
212 SetReturnCode(retCode
);
213 // TODO modal un-showing
218 void wxDialog::OnOK(wxCommandEvent
& event
)
220 if ( Validate() && TransferDataFromWindow() )
226 SetReturnCode(wxID_OK
);
232 void wxDialog::OnApply(wxCommandEvent
& event
)
235 TransferDataFromWindow();
236 // TODO probably need to disable the Apply button until things change again
239 void wxDialog::OnCancel(wxCommandEvent
& event
)
242 EndModal(wxID_CANCEL
);
245 SetReturnCode(wxID_CANCEL
);
250 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
252 // We'll send a Cancel message by default,
253 // which may close the dialog.
254 // Check for looping if the Cancel event handler calls Close().
256 // Note that if a cancel button and handler aren't present in the dialog,
257 // nothing will happen when you close the dialog via the window manager, or
259 // We wouldn't want to destroy the dialog by default, since the dialog may have been
260 // created on the stack.
261 // However, this does mean that calling dialog->Close() won't delete the dialog
262 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
263 // sure to destroy the dialog.
264 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
266 static wxList closing
;
268 if ( closing
.Member(this) )
271 closing
.Append(this);
273 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
274 cancelEvent
.SetEventObject( this );
275 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
277 closing
.DeleteObject(this);
280 // Destroy the window (delayed, if a managed window)
281 bool wxDialog::Destroy()
283 if (!wxPendingDelete
.Member(this))
284 wxPendingDelete
.Append(this);
288 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
290 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));