| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/msw/dialog.cpp |
| 3 | // Purpose: wxDialog class |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 01/02/97 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart and Markus Holzem |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #ifdef __GNUG__ |
| 21 | #pragma implementation "dialog.h" |
| 22 | #endif |
| 23 | |
| 24 | // For compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | #ifdef __BORLANDC__ |
| 28 | #pragma hdrstop |
| 29 | #endif |
| 30 | |
| 31 | #ifndef WX_PRECOMP |
| 32 | #include "wx/dialog.h" |
| 33 | #include "wx/utils.h" |
| 34 | #include "wx/frame.h" |
| 35 | #include "wx/app.h" |
| 36 | #include "wx/settings.h" |
| 37 | #include "wx/intl.h" |
| 38 | #include "wx/log.h" |
| 39 | #endif |
| 40 | |
| 41 | #include "wx/msw/private.h" |
| 42 | #include "wx/log.h" |
| 43 | |
| 44 | #if wxUSE_COMMON_DIALOGS |
| 45 | #include <commdlg.h> |
| 46 | #endif |
| 47 | |
| 48 | // ---------------------------------------------------------------------------- |
| 49 | // constants |
| 50 | // ---------------------------------------------------------------------------- |
| 51 | |
| 52 | // default dialog pos and size |
| 53 | |
| 54 | #define wxDIALOG_DEFAULT_X 300 |
| 55 | #define wxDIALOG_DEFAULT_Y 300 |
| 56 | |
| 57 | #define wxDIALOG_DEFAULT_WIDTH 500 |
| 58 | #define wxDIALOG_DEFAULT_HEIGHT 500 |
| 59 | |
| 60 | // ---------------------------------------------------------------------------- |
| 61 | // globals |
| 62 | // ---------------------------------------------------------------------------- |
| 63 | |
| 64 | // all modal dialogs currently shown |
| 65 | static wxWindowList wxModalDialogs; |
| 66 | |
| 67 | // ---------------------------------------------------------------------------- |
| 68 | // wxWin macros |
| 69 | // ---------------------------------------------------------------------------- |
| 70 | |
| 71 | IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow) |
| 72 | |
| 73 | BEGIN_EVENT_TABLE(wxDialog, wxTopLevelWindow) |
| 74 | EVT_BUTTON(wxID_OK, wxDialog::OnOK) |
| 75 | EVT_BUTTON(wxID_APPLY, wxDialog::OnApply) |
| 76 | EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel) |
| 77 | |
| 78 | EVT_CHAR_HOOK(wxDialog::OnCharHook) |
| 79 | |
| 80 | EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged) |
| 81 | |
| 82 | EVT_CLOSE(wxDialog::OnCloseWindow) |
| 83 | END_EVENT_TABLE() |
| 84 | |
| 85 | // ============================================================================ |
| 86 | // implementation |
| 87 | // ============================================================================ |
| 88 | |
| 89 | // ---------------------------------------------------------------------------- |
| 90 | // wxDialog construction |
| 91 | // ---------------------------------------------------------------------------- |
| 92 | |
| 93 | void wxDialog::Init() |
| 94 | { |
| 95 | m_oldFocus = (wxWindow *)NULL; |
| 96 | |
| 97 | m_isShown = FALSE; |
| 98 | |
| 99 | m_windowDisabler = (wxWindowDisabler *)NULL; |
| 100 | |
| 101 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE)); |
| 102 | } |
| 103 | |
| 104 | bool wxDialog::Create(wxWindow *parent, |
| 105 | wxWindowID id, |
| 106 | const wxString& title, |
| 107 | const wxPoint& pos, |
| 108 | const wxSize& size, |
| 109 | long style, |
| 110 | const wxString& name) |
| 111 | { |
| 112 | Init(); |
| 113 | |
| 114 | SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG); |
| 115 | |
| 116 | // save focus before doing anything which can potentially change it |
| 117 | m_oldFocus = FindFocus(); |
| 118 | |
| 119 | // All dialogs should really have this style |
| 120 | style |= wxTAB_TRAVERSAL; |
| 121 | |
| 122 | if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) ) |
| 123 | return FALSE; |
| 124 | |
| 125 | SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT)); |
| 126 | |
| 127 | return TRUE; |
| 128 | } |
| 129 | |
| 130 | bool wxDialog::EnableCloseButton(bool enable) |
| 131 | { |
| 132 | #ifndef __WXMICROWIN__ |
| 133 | // get system (a.k.a. window) menu |
| 134 | HMENU hmenu = ::GetSystemMenu(GetHwnd(), FALSE /* get it */); |
| 135 | if ( !hmenu ) |
| 136 | { |
| 137 | wxLogLastError(_T("GetSystemMenu")); |
| 138 | |
| 139 | return FALSE; |
| 140 | } |
| 141 | |
| 142 | // enabling/disabling the close item from it also automatically |
| 143 | // disables/enabling the close title bar button |
| 144 | if ( !::EnableMenuItem(hmenu, SC_CLOSE, |
| 145 | MF_BYCOMMAND | (enable ? MF_ENABLED : MF_GRAYED)) ) |
| 146 | { |
| 147 | wxLogLastError(_T("EnableMenuItem(SC_CLOSE)")); |
| 148 | |
| 149 | return FALSE; |
| 150 | } |
| 151 | |
| 152 | // update appearance immediately |
| 153 | if ( !::DrawMenuBar(GetHwnd()) ) |
| 154 | { |
| 155 | wxLogLastError(_T("DrawMenuBar")); |
| 156 | } |
| 157 | #endif |
| 158 | |
| 159 | return TRUE; |
| 160 | } |
| 161 | |
| 162 | void wxDialog::SetModal(bool flag) |
| 163 | { |
| 164 | if ( flag ) |
| 165 | { |
| 166 | m_windowStyle |= wxDIALOG_MODAL; |
| 167 | |
| 168 | wxModelessWindows.DeleteObject(this); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | m_windowStyle &= ~wxDIALOG_MODAL; |
| 173 | |
| 174 | wxModelessWindows.Append(this); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | wxDialog::~wxDialog() |
| 179 | { |
| 180 | m_isBeingDeleted = TRUE; |
| 181 | |
| 182 | // this will also reenable all the other windows for a modal dialog |
| 183 | Show(FALSE); |
| 184 | } |
| 185 | |
| 186 | // ---------------------------------------------------------------------------- |
| 187 | // kbd handling |
| 188 | // ---------------------------------------------------------------------------- |
| 189 | |
| 190 | // By default, pressing escape cancels the dialog |
| 191 | void wxDialog::OnCharHook(wxKeyEvent& event) |
| 192 | { |
| 193 | if (GetHWND()) |
| 194 | { |
| 195 | // "Esc" works as an accelerator for the "Cancel" button, but it |
| 196 | // shouldn't close the dialog which doesn't have any cancel button |
| 197 | if ( (event.m_keyCode == WXK_ESCAPE) && FindWindow(wxID_CANCEL) ) |
| 198 | { |
| 199 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
| 200 | cancelEvent.SetEventObject( this ); |
| 201 | GetEventHandler()->ProcessEvent(cancelEvent); |
| 202 | |
| 203 | // ensure that there is another message for this window so the |
| 204 | // ShowModal loop will exit and won't get stuck in GetMessage(). |
| 205 | ::PostMessage(GetHwnd(), WM_NULL, 0, 0); |
| 206 | |
| 207 | return; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // We didn't process this event. |
| 212 | event.Skip(); |
| 213 | } |
| 214 | |
| 215 | // ---------------------------------------------------------------------------- |
| 216 | // showing the dialogs |
| 217 | // ---------------------------------------------------------------------------- |
| 218 | |
| 219 | bool wxDialog::IsModal() const |
| 220 | { |
| 221 | return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0; |
| 222 | } |
| 223 | |
| 224 | bool wxDialog::IsModalShowing() const |
| 225 | { |
| 226 | return wxModalDialogs.Find((wxDialog *)this) != NULL; // const_cast |
| 227 | } |
| 228 | |
| 229 | void wxDialog::DoShowModal() |
| 230 | { |
| 231 | wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") ); |
| 232 | wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") ); |
| 233 | |
| 234 | wxModalDialogs.Append(this); |
| 235 | |
| 236 | wxWindow *parent = GetParent(); |
| 237 | |
| 238 | wxWindow* oldFocus = m_oldFocus; |
| 239 | |
| 240 | // We have to remember the HWND because we need to check |
| 241 | // the HWND still exists (oldFocus can be garbage when the dialog |
| 242 | // exits, if it has been destroyed) |
| 243 | HWND hwndOldFocus = 0; |
| 244 | if (oldFocus) |
| 245 | hwndOldFocus = (HWND) oldFocus->GetHWND(); |
| 246 | |
| 247 | // remember where the focus was |
| 248 | if ( !oldFocus ) |
| 249 | { |
| 250 | oldFocus = parent; |
| 251 | if ( parent ) |
| 252 | hwndOldFocus = GetHwndOf(parent); |
| 253 | } |
| 254 | |
| 255 | // disable all other app windows |
| 256 | wxASSERT_MSG( !m_windowDisabler, _T("disabling windows twice?") ); |
| 257 | |
| 258 | m_windowDisabler = new wxWindowDisabler(this); |
| 259 | |
| 260 | // enter the modal loop |
| 261 | while ( IsModalShowing() ) |
| 262 | { |
| 263 | #if wxUSE_THREADS |
| 264 | wxMutexGuiLeaveOrEnter(); |
| 265 | #endif // wxUSE_THREADS |
| 266 | |
| 267 | while ( !wxTheApp->Pending() && wxTheApp->ProcessIdle() ) |
| 268 | ; |
| 269 | |
| 270 | // a message came or no more idle processing to do |
| 271 | wxTheApp->DoMessage(); |
| 272 | } |
| 273 | |
| 274 | // and restore focus |
| 275 | // Note that this code MUST NOT access the dialog object's data |
| 276 | // in case the object has been deleted (which will be the case |
| 277 | // for a modal dialog that has been destroyed before calling EndModal). |
| 278 | if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus)) |
| 279 | { |
| 280 | // This is likely to prove that the object still exists |
| 281 | if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus) |
| 282 | oldFocus->SetFocus(); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | bool wxDialog::Show(bool show) |
| 287 | { |
| 288 | if ( !show ) |
| 289 | { |
| 290 | // if we had disabled other app windows, reenable them back now because |
| 291 | // if they stay disabled Windows will activate another window (one |
| 292 | // which is enabled, anyhow) and we will lose activation |
| 293 | if ( m_windowDisabler ) |
| 294 | { |
| 295 | delete m_windowDisabler; |
| 296 | m_windowDisabler = NULL; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // ShowModal() may be called for already shown dialog |
| 301 | if ( !wxDialogBase::Show(show) && !(show && IsModal()) ) |
| 302 | { |
| 303 | // nothing to do |
| 304 | return FALSE; |
| 305 | } |
| 306 | |
| 307 | if ( show ) |
| 308 | { |
| 309 | // usually will result in TransferDataToWindow() being called |
| 310 | InitDialog(); |
| 311 | } |
| 312 | |
| 313 | if ( IsModal() ) |
| 314 | { |
| 315 | if ( show ) |
| 316 | { |
| 317 | // modal dialog needs a parent window, so try to find one |
| 318 | if ( !GetParent() ) |
| 319 | { |
| 320 | wxWindow *parent = wxTheApp->GetTopWindow(); |
| 321 | if ( parent && parent != this && parent->IsShown() ) |
| 322 | { |
| 323 | // use it |
| 324 | m_parent = parent; |
| 325 | |
| 326 | // VZ: to make dialog behave properly we should reparent |
| 327 | // the dialog for Windows as well - unfortunately, |
| 328 | // following the docs for SetParent() results in this |
| 329 | // code which plainly doesn't work |
| 330 | #if 0 |
| 331 | long dwStyle = ::GetWindowLong(GetHwnd(), GWL_STYLE); |
| 332 | dwStyle &= ~WS_POPUP; |
| 333 | dwStyle |= WS_CHILD; |
| 334 | ::SetWindowLong(GetHwnd(), GWL_STYLE, dwStyle); |
| 335 | ::SetParent(GetHwnd(), GetHwndOf(parent)); |
| 336 | #endif // 0 |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | DoShowModal(); |
| 341 | } |
| 342 | else // end of modal dialog |
| 343 | { |
| 344 | // this will cause IsModalShowing() return FALSE and our local |
| 345 | // message loop will terminate |
| 346 | wxModalDialogs.DeleteObject(this); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | return TRUE; |
| 351 | } |
| 352 | |
| 353 | // a special version for Show(TRUE) for modal dialogs which returns return code |
| 354 | int wxDialog::ShowModal() |
| 355 | { |
| 356 | if ( !IsModal() ) |
| 357 | { |
| 358 | SetModal(TRUE); |
| 359 | } |
| 360 | |
| 361 | Show(TRUE); |
| 362 | |
| 363 | return GetReturnCode(); |
| 364 | } |
| 365 | |
| 366 | // NB: this function (surprizingly) may be called for both modal and modeless |
| 367 | // dialogs and should work for both of them |
| 368 | void wxDialog::EndModal(int retCode) |
| 369 | { |
| 370 | SetReturnCode(retCode); |
| 371 | |
| 372 | Show(FALSE); |
| 373 | } |
| 374 | |
| 375 | // ---------------------------------------------------------------------------- |
| 376 | // wxWin event handlers |
| 377 | // ---------------------------------------------------------------------------- |
| 378 | |
| 379 | // Standard buttons |
| 380 | void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event)) |
| 381 | { |
| 382 | if ( Validate() && TransferDataFromWindow() ) |
| 383 | { |
| 384 | EndModal(wxID_OK); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event)) |
| 389 | { |
| 390 | if ( Validate() ) |
| 391 | TransferDataFromWindow(); |
| 392 | |
| 393 | // TODO probably need to disable the Apply button until things change again |
| 394 | } |
| 395 | |
| 396 | void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) |
| 397 | { |
| 398 | EndModal(wxID_CANCEL); |
| 399 | } |
| 400 | |
| 401 | void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
| 402 | { |
| 403 | // We'll send a Cancel message by default, which may close the dialog. |
| 404 | // Check for looping if the Cancel event handler calls Close(). |
| 405 | |
| 406 | // Note that if a cancel button and handler aren't present in the dialog, |
| 407 | // nothing will happen when you close the dialog via the window manager, or |
| 408 | // via Close(). We wouldn't want to destroy the dialog by default, since |
| 409 | // the dialog may have been created on the stack. However, this does mean |
| 410 | // that calling dialog->Close() won't delete the dialog unless the handler |
| 411 | // for wxID_CANCEL does so. So use Destroy() if you want to be sure to |
| 412 | // destroy the dialog. The default OnCancel (above) simply ends a modal |
| 413 | // dialog, and hides a modeless dialog. |
| 414 | |
| 415 | // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global |
| 416 | // lists here? don't dare to change it now, but should be done later! |
| 417 | static wxList closing; |
| 418 | |
| 419 | if ( closing.Member(this) ) |
| 420 | return; |
| 421 | |
| 422 | closing.Append(this); |
| 423 | |
| 424 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
| 425 | cancelEvent.SetEventObject( this ); |
| 426 | GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog |
| 427 | |
| 428 | closing.DeleteObject(this); |
| 429 | } |
| 430 | |
| 431 | void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) |
| 432 | { |
| 433 | #if wxUSE_CTL3D |
| 434 | Ctl3dColorChange(); |
| 435 | #else |
| 436 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE)); |
| 437 | Refresh(); |
| 438 | #endif |
| 439 | } |
| 440 | |
| 441 | // --------------------------------------------------------------------------- |
| 442 | // dialog window proc |
| 443 | // --------------------------------------------------------------------------- |
| 444 | |
| 445 | long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam) |
| 446 | { |
| 447 | long rc = 0; |
| 448 | bool processed = FALSE; |
| 449 | |
| 450 | switch ( message ) |
| 451 | { |
| 452 | #if 0 // now that we got owner window right it doesn't seem to be needed |
| 453 | case WM_ACTIVATE: |
| 454 | switch ( LOWORD(wParam) ) |
| 455 | { |
| 456 | case WA_ACTIVE: |
| 457 | case WA_CLICKACTIVE: |
| 458 | if ( IsModalShowing() && GetParent() ) |
| 459 | { |
| 460 | // bring the owner window to top as the standard dialog |
| 461 | // boxes do |
| 462 | if ( !::SetWindowPos |
| 463 | ( |
| 464 | GetHwndOf(GetParent()), |
| 465 | GetHwnd(), |
| 466 | 0, 0, |
| 467 | 0, 0, |
| 468 | SWP_NOACTIVATE | |
| 469 | SWP_NOMOVE | |
| 470 | SWP_NOSIZE |
| 471 | ) ) |
| 472 | { |
| 473 | wxLogLastError(wxT("SetWindowPos(SWP_NOACTIVATE)")); |
| 474 | } |
| 475 | } |
| 476 | // fall through to process it normally as well |
| 477 | } |
| 478 | break; |
| 479 | #endif // 0 |
| 480 | |
| 481 | case WM_CLOSE: |
| 482 | // if we can't close, tell the system that we processed the |
| 483 | // message - otherwise it would close us |
| 484 | processed = !Close(); |
| 485 | break; |
| 486 | |
| 487 | #ifndef __WXMICROWIN__ |
| 488 | case WM_SETCURSOR: |
| 489 | // we want to override the busy cursor for modal dialogs: |
| 490 | // typically, wxBeginBusyCursor() is called and then a modal dialog |
| 491 | // is shown, but the modal dialog shouldn't have hourglass cursor |
| 492 | if ( IsModalShowing() && wxIsBusy() ) |
| 493 | { |
| 494 | // set our cursor for all windows (but see below) |
| 495 | wxCursor cursor = m_cursor; |
| 496 | if ( !cursor.Ok() ) |
| 497 | cursor = wxCURSOR_ARROW; |
| 498 | |
| 499 | ::SetCursor(GetHcursorOf(cursor)); |
| 500 | |
| 501 | // in any case, stop here and don't let wxWindow process this |
| 502 | // message (it would set the busy cursor) |
| 503 | processed = TRUE; |
| 504 | |
| 505 | // but return FALSE to tell the child window (if the event |
| 506 | // comes from one of them and not from ourselves) that it can |
| 507 | // set its own cursor if it has one: thus, standard controls |
| 508 | // (e.g. text ctrl) still have correct cursors in a dialog |
| 509 | // invoked while wxIsBusy() |
| 510 | rc = FALSE; |
| 511 | } |
| 512 | break; |
| 513 | #endif // __WXMICROWIN__ |
| 514 | } |
| 515 | |
| 516 | if ( !processed ) |
| 517 | rc = wxWindow::MSWWindowProc(message, wParam, lParam); |
| 518 | |
| 519 | return rc; |
| 520 | } |
| 521 | |
| 522 | #if wxUSE_CTL3D |
| 523 | |
| 524 | // Define for each class of dialog and control |
| 525 | WXHBRUSH wxDialog::OnCtlColor(WXHDC WXUNUSED(pDC), |
| 526 | WXHWND WXUNUSED(pWnd), |
| 527 | WXUINT WXUNUSED(nCtlColor), |
| 528 | WXUINT message, |
| 529 | WXWPARAM wParam, |
| 530 | WXLPARAM lParam) |
| 531 | { |
| 532 | return (WXHBRUSH)Ctl3dCtlColorEx(message, wParam, lParam); |
| 533 | } |
| 534 | |
| 535 | #endif // wxUSE_CTL3D |
| 536 | |