| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: dialog.cpp |
| 3 | // Purpose: wxDialog class |
| 4 | // Author: AUTHOR |
| 5 | // Modified by: |
| 6 | // Created: ??/??/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) AUTHOR |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "dialog.h" |
| 14 | #endif |
| 15 | |
| 16 | #include "wx/dialog.h" |
| 17 | #include "wx/utils.h" |
| 18 | #include "wx/frame.h" |
| 19 | #include "wx/app.h" |
| 20 | #include "wx/settings.h" |
| 21 | |
| 22 | // Lists to keep track of windows, so we can disable/enable them |
| 23 | // for modal dialogs |
| 24 | wxList wxModalDialogs; |
| 25 | wxList wxModelessWindows; // Frames and modeless dialogs |
| 26 | extern wxList wxPendingDelete; |
| 27 | |
| 28 | IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxPanel) |
| 29 | |
| 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) |
| 37 | END_EVENT_TABLE() |
| 38 | |
| 39 | |
| 40 | wxDialog::wxDialog() |
| 41 | { |
| 42 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE)); |
| 43 | } |
| 44 | |
| 45 | bool wxDialog::Create(wxWindow *parent, wxWindowID id, |
| 46 | const wxString& title, |
| 47 | const wxPoint& pos, |
| 48 | const wxSize& size, |
| 49 | long style, |
| 50 | const wxString& name) |
| 51 | { |
| 52 | m_windowStyle = style; |
| 53 | |
| 54 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE)); |
| 55 | SetName(name); |
| 56 | |
| 57 | if (!parent) |
| 58 | wxTopLevelWindows.Append(this); |
| 59 | |
| 60 | if (parent) parent->AddChild(this); |
| 61 | |
| 62 | if ( id == -1 ) |
| 63 | m_windowId = (int)NewControlId(); |
| 64 | else |
| 65 | m_windowId = id; |
| 66 | |
| 67 | // TODO: create dialog |
| 68 | |
| 69 | return FALSE; |
| 70 | } |
| 71 | |
| 72 | void wxDialog::SetModal(bool flag) |
| 73 | { |
| 74 | if ( flag ) |
| 75 | m_windowStyle |= wxDIALOG_MODAL ; |
| 76 | else |
| 77 | if ( m_windowStyle & wxDIALOG_MODAL ) |
| 78 | m_windowStyle -= wxDIALOG_MODAL ; |
| 79 | |
| 80 | wxModelessWindows.DeleteObject(this); |
| 81 | if (!flag) |
| 82 | wxModelessWindows.Append(this); |
| 83 | } |
| 84 | |
| 85 | wxDialog::~wxDialog() |
| 86 | { |
| 87 | // TODO |
| 88 | wxTopLevelWindows.DeleteObject(this); |
| 89 | |
| 90 | if ( (GetWindowStyleFlag() & wxDIALOG_MODAL) != wxDIALOG_MODAL ) |
| 91 | wxModelessWindows.DeleteObject(this); |
| 92 | |
| 93 | // If this is the last top-level window, exit. |
| 94 | if (wxTheApp && (wxTopLevelWindows.Number() == 0)) |
| 95 | { |
| 96 | wxTheApp->SetTopWindow(NULL); |
| 97 | |
| 98 | if (wxTheApp->GetExitOnFrameDelete()) |
| 99 | { |
| 100 | // TODO: exit |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // By default, pressing escape cancels the dialog |
| 106 | void wxDialog::OnCharHook(wxKeyEvent& event) |
| 107 | { |
| 108 | if (event.m_keyCode == WXK_ESCAPE) |
| 109 | { |
| 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); |
| 115 | |
| 116 | return; |
| 117 | } |
| 118 | // We didn't process this event. |
| 119 | event.Skip(); |
| 120 | } |
| 121 | |
| 122 | void wxDialog::Iconize(bool WXUNUSED(iconize)) |
| 123 | { |
| 124 | // TODO |
| 125 | } |
| 126 | |
| 127 | bool wxDialog::IsIconized() const |
| 128 | { |
| 129 | // TODO |
| 130 | return FALSE; |
| 131 | } |
| 132 | |
| 133 | void wxDialog::SetClientSize(int width, int height) |
| 134 | { |
| 135 | // TODO |
| 136 | } |
| 137 | |
| 138 | void wxDialog::GetPosition(int *x, int *y) const |
| 139 | { |
| 140 | // TODO |
| 141 | } |
| 142 | |
| 143 | bool wxDialog::Show(bool show) |
| 144 | { |
| 145 | // TODO |
| 146 | return FALSE; |
| 147 | } |
| 148 | |
| 149 | void wxDialog::SetTitle(const wxString& title) |
| 150 | { |
| 151 | // TODO |
| 152 | } |
| 153 | |
| 154 | wxString wxDialog::GetTitle() const |
| 155 | { |
| 156 | // TODO |
| 157 | return wxString(""); |
| 158 | } |
| 159 | |
| 160 | void wxDialog::Centre(int direction) |
| 161 | { |
| 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) |
| 167 | { |
| 168 | parent->GetPosition(&x_offset,&y_offset) ; |
| 169 | parent->GetSize(&display_width,&display_height) ; |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | wxDisplaySize(&display_width, &display_height); |
| 174 | x_offset = 0 ; |
| 175 | y_offset = 0 ; |
| 176 | } |
| 177 | |
| 178 | GetSize(&width, &height); |
| 179 | GetPosition(&x, &y); |
| 180 | |
| 181 | if (direction & wxHORIZONTAL) |
| 182 | x = (int)((display_width - width)/2); |
| 183 | if (direction & wxVERTICAL) |
| 184 | y = (int)((display_height - height)/2); |
| 185 | |
| 186 | SetSize(x+x_offset, y+y_offset, width, height); |
| 187 | } |
| 188 | |
| 189 | // Replacement for Show(TRUE) for modal dialogs - returns return code |
| 190 | int wxDialog::ShowModal() |
| 191 | { |
| 192 | m_windowStyle |= wxDIALOG_MODAL; |
| 193 | // TODO: modal showing |
| 194 | Show(TRUE); |
| 195 | return GetReturnCode(); |
| 196 | } |
| 197 | |
| 198 | void wxDialog::EndModal(int retCode) |
| 199 | { |
| 200 | SetReturnCode(retCode); |
| 201 | // TODO modal un-showing |
| 202 | Show(FALSE); |
| 203 | } |
| 204 | |
| 205 | // Standard buttons |
| 206 | void wxDialog::OnOK(wxCommandEvent& event) |
| 207 | { |
| 208 | if ( Validate() && TransferDataFromWindow() ) |
| 209 | { |
| 210 | if ( IsModal() ) |
| 211 | EndModal(wxID_OK); |
| 212 | else |
| 213 | { |
| 214 | SetReturnCode(wxID_OK); |
| 215 | this->Show(FALSE); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void wxDialog::OnApply(wxCommandEvent& event) |
| 221 | { |
| 222 | if (Validate()) |
| 223 | TransferDataFromWindow(); |
| 224 | // TODO probably need to disable the Apply button until things change again |
| 225 | } |
| 226 | |
| 227 | void wxDialog::OnCancel(wxCommandEvent& event) |
| 228 | { |
| 229 | if ( IsModal() ) |
| 230 | EndModal(wxID_CANCEL); |
| 231 | else |
| 232 | { |
| 233 | SetReturnCode(wxID_CANCEL); |
| 234 | this->Show(FALSE); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void wxDialog::OnCloseWindow(wxCloseEvent& event) |
| 239 | { |
| 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(). |
| 243 | |
| 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 |
| 246 | // via Close(). |
| 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. |
| 253 | |
| 254 | static wxList closing; |
| 255 | |
| 256 | if ( closing.Member(this) ) |
| 257 | return; |
| 258 | |
| 259 | closing.Append(this); |
| 260 | |
| 261 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
| 262 | cancelEvent.SetEventObject( this ); |
| 263 | GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog |
| 264 | |
| 265 | closing.DeleteObject(this); |
| 266 | } |
| 267 | |
| 268 | // Destroy the window (delayed, if a managed window) |
| 269 | bool wxDialog::Destroy() |
| 270 | { |
| 271 | if (!wxPendingDelete.Member(this)) |
| 272 | wxPendingDelete.Append(this); |
| 273 | return TRUE; |
| 274 | } |
| 275 | |
| 276 | void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& event) |
| 277 | { |
| 278 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE)); |
| 279 | Refresh(); |
| 280 | } |
| 281 | |
| 282 | void wxDialog::Fit() |
| 283 | { |
| 284 | } |