| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: dialog.cpp |
| 3 | // Purpose: wxDialog class |
| 4 | // Author: Stefan Csomor |
| 5 | // Modified by: |
| 6 | // Created: 1998-01-01 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Stefan Csomor |
| 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 | #include "wx/mac/uma.h" |
| 23 | |
| 24 | // Lists to keep track of windows, so we can disable/enable them |
| 25 | // for modal dialogs |
| 26 | wxList wxModalDialogs; |
| 27 | //wxList wxModelessWindows; // Frames and modeless dialogs |
| 28 | extern wxList wxPendingDelete; |
| 29 | |
| 30 | #if !USE_SHARED_LIBRARY |
| 31 | IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow) |
| 32 | |
| 33 | BEGIN_EVENT_TABLE(wxDialog, wxDialogBase) |
| 34 | EVT_BUTTON(wxID_OK, wxDialog::OnOK) |
| 35 | EVT_BUTTON(wxID_APPLY, wxDialog::OnApply) |
| 36 | EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel) |
| 37 | |
| 38 | EVT_CHAR_HOOK(wxDialog::OnCharHook) |
| 39 | |
| 40 | EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged) |
| 41 | |
| 42 | EVT_CLOSE(wxDialog::OnCloseWindow) |
| 43 | END_EVENT_TABLE() |
| 44 | |
| 45 | #endif |
| 46 | |
| 47 | void wxDialog::Init() |
| 48 | { |
| 49 | m_isModalStyle = false; |
| 50 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); |
| 51 | } |
| 52 | |
| 53 | bool wxDialog::Create(wxWindow *parent, wxWindowID id, |
| 54 | const wxString& title, |
| 55 | const wxPoint& pos, |
| 56 | const wxSize& size, |
| 57 | long style, |
| 58 | const wxString& name) |
| 59 | { |
| 60 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); |
| 61 | |
| 62 | |
| 63 | if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) ) |
| 64 | return FALSE; |
| 65 | |
| 66 | MacCreateRealWindow( title , pos , size , MacRemoveBordersFromStyle(style) & ~(wxYES|wxOK|wxNO|wxCANCEL) , name ) ; |
| 67 | |
| 68 | m_macWindowBackgroundTheme = kThemeBrushDialogBackgroundActive ; |
| 69 | SetThemeWindowBackground( (WindowRef) m_macWindow , m_macWindowBackgroundTheme , false ) ; |
| 70 | |
| 71 | return TRUE; |
| 72 | } |
| 73 | |
| 74 | void wxDialog::SetModal(bool flag) |
| 75 | { |
| 76 | if ( flag ) |
| 77 | { |
| 78 | m_isModalStyle = true; |
| 79 | |
| 80 | wxModelessWindows.DeleteObject(this); |
| 81 | #if TARGET_CARBON |
| 82 | SetWindowModality( (WindowRef) MacGetWindowRef() , kWindowModalityAppModal , NULL ) ; |
| 83 | #endif |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | m_isModalStyle = false; |
| 88 | |
| 89 | wxModelessWindows.Append(this); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | wxDialog::~wxDialog() |
| 94 | { |
| 95 | m_isBeingDeleted = TRUE; |
| 96 | Show(FALSE); |
| 97 | } |
| 98 | |
| 99 | // By default, pressing escape cancels the dialog , on mac command-stop does the same thing |
| 100 | void wxDialog::OnCharHook(wxKeyEvent& event) |
| 101 | { |
| 102 | if (( event.m_keyCode == WXK_ESCAPE || |
| 103 | ( event.m_keyCode == '.' && event.MetaDown() ) ) |
| 104 | && FindWindow(wxID_CANCEL) ) |
| 105 | { |
| 106 | // Behaviour changed in 2.0: we'll send a Cancel message |
| 107 | // to the dialog instead of Close. |
| 108 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
| 109 | cancelEvent.SetEventObject( this ); |
| 110 | GetEventHandler()->ProcessEvent(cancelEvent); |
| 111 | |
| 112 | return; |
| 113 | } |
| 114 | // We didn't process this event. |
| 115 | event.Skip(); |
| 116 | } |
| 117 | |
| 118 | bool wxDialog::IsModal() const |
| 119 | { |
| 120 | return m_isModalStyle; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | bool wxDialog::IsModalShowing() const |
| 125 | { |
| 126 | return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast |
| 127 | } |
| 128 | |
| 129 | bool wxDialog::Show(bool show) |
| 130 | { |
| 131 | if ( !wxDialogBase::Show(show) ) |
| 132 | { |
| 133 | // nothing to do |
| 134 | return FALSE; |
| 135 | } |
| 136 | |
| 137 | if ( show ) |
| 138 | { |
| 139 | // usually will result in TransferDataToWindow() being called |
| 140 | InitDialog(); |
| 141 | } |
| 142 | |
| 143 | if ( IsModal() ) |
| 144 | { |
| 145 | if ( show ) |
| 146 | { |
| 147 | DoShowModal(); |
| 148 | } |
| 149 | else // end of modal dialog |
| 150 | { |
| 151 | // this will cause IsModalShowing() return FALSE and our local |
| 152 | // message loop will terminate |
| 153 | wxModalDialogs.DeleteObject(this); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return TRUE; |
| 158 | } |
| 159 | |
| 160 | #if !TARGET_CARBON |
| 161 | extern bool s_macIsInModalLoop ; |
| 162 | #endif |
| 163 | |
| 164 | void wxDialog::DoShowModal() |
| 165 | { |
| 166 | wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") ); |
| 167 | |
| 168 | wxModalDialogs.Append(this); |
| 169 | |
| 170 | #if TARGET_CARBON |
| 171 | BeginAppModalStateForWindow( (WindowRef) MacGetWindowRef()) ; |
| 172 | #else |
| 173 | // TODO : test whether parent gets disabled |
| 174 | bool formerModal = s_macIsInModalLoop ; |
| 175 | s_macIsInModalLoop = true ; |
| 176 | #endif |
| 177 | while ( IsModalShowing() ) |
| 178 | { |
| 179 | wxTheApp->MacDoOneEvent() ; |
| 180 | // calls process idle itself |
| 181 | } |
| 182 | |
| 183 | #if TARGET_CARBON |
| 184 | EndAppModalStateForWindow( (WindowRef) MacGetWindowRef() ) ; |
| 185 | #else |
| 186 | // TODO probably reenable the parent window if any |
| 187 | s_macIsInModalLoop = formerModal ; |
| 188 | #endif |
| 189 | } |
| 190 | |
| 191 | |
| 192 | // Replacement for Show(TRUE) for modal dialogs - returns return code |
| 193 | int wxDialog::ShowModal() |
| 194 | { |
| 195 | if ( !IsModal() ) |
| 196 | { |
| 197 | SetModal(TRUE); |
| 198 | } |
| 199 | |
| 200 | Show(TRUE); |
| 201 | return GetReturnCode(); |
| 202 | } |
| 203 | |
| 204 | // NB: this function (surprizingly) may be called for both modal and modeless |
| 205 | // dialogs and should work for both of them |
| 206 | void wxDialog::EndModal(int retCode) |
| 207 | { |
| 208 | SetReturnCode(retCode); |
| 209 | Show(FALSE); |
| 210 | } |
| 211 | |
| 212 | // Standard buttons |
| 213 | void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event)) |
| 214 | { |
| 215 | if ( Validate() && TransferDataFromWindow() ) |
| 216 | { |
| 217 | EndModal(wxID_OK); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event)) |
| 222 | { |
| 223 | if (Validate()) |
| 224 | TransferDataFromWindow(); |
| 225 | // TODO probably need to disable the Apply button until things change again |
| 226 | } |
| 227 | |
| 228 | void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) |
| 229 | { |
| 230 | EndModal(wxID_CANCEL); |
| 231 | } |
| 232 | |
| 233 | void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
| 234 | { |
| 235 | // We'll send a Cancel message by default, |
| 236 | // which may close the dialog. |
| 237 | // Check for looping if the Cancel event handler calls Close(). |
| 238 | |
| 239 | // Note that if a cancel button and handler aren't present in the dialog, |
| 240 | // nothing will happen when you close the dialog via the window manager, or |
| 241 | // via Close(). |
| 242 | // We wouldn't want to destroy the dialog by default, since the dialog may have been |
| 243 | // created on the stack. |
| 244 | // However, this does mean that calling dialog->Close() won't delete the dialog |
| 245 | // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be |
| 246 | // sure to destroy the dialog. |
| 247 | // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog. |
| 248 | |
| 249 | static wxList closing; |
| 250 | |
| 251 | if ( closing.Member(this) ) |
| 252 | return; |
| 253 | |
| 254 | closing.Append(this); |
| 255 | |
| 256 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
| 257 | cancelEvent.SetEventObject( this ); |
| 258 | GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog |
| 259 | |
| 260 | closing.DeleteObject(this); |
| 261 | } |
| 262 | |
| 263 | void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) |
| 264 | { |
| 265 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); |
| 266 | Refresh(); |
| 267 | } |
| 268 | |