1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dialog.cpp
3 // Purpose: wxDialog class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dialog.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/dialog.h"
36 #include "wx/settings.h"
41 #include "wx/msw/private.h"
44 #if wxUSE_COMMON_DIALOGS
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // default dialog pos and size
54 #define wxDIALOG_DEFAULT_X 300
55 #define wxDIALOG_DEFAULT_Y 300
57 #define wxDIALOG_DEFAULT_WIDTH 500
58 #define wxDIALOG_DEFAULT_HEIGHT 500
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // all objects to be deleted during next idle processing - from window.cpp
65 extern wxList WXDLLEXPORT wxPendingDelete
;
67 // all frames and modeless dialogs - not static, used in frame.cpp, mdi.cpp &c
68 wxWindowList wxModelessWindows
;
70 // all modal dialogs currently shown
71 static wxWindowList wxModalDialogs
;
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxPanel
)
79 BEGIN_EVENT_TABLE(wxDialog
, wxPanel
)
80 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
81 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
82 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
84 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
86 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
88 EVT_CLOSE(wxDialog::OnCloseWindow
)
91 // ============================================================================
93 // ============================================================================
95 // ----------------------------------------------------------------------------
96 // wxDialog construction
97 // ----------------------------------------------------------------------------
101 m_oldFocus
= (wxWindow
*)NULL
;
105 m_windowDisabler
= (wxWindowDisabler
*)NULL
;
107 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
110 bool wxDialog::Create(wxWindow
*parent
,
112 const wxString
& title
,
116 const wxString
& name
)
120 m_oldFocus
= FindFocus();
124 wxTopLevelWindows
.Append(this);
127 parent
->AddChild(this);
130 m_windowId
= (int)NewControlId();
140 x
= wxDIALOG_DEFAULT_X
;
142 y
= wxDIALOG_DEFAULT_Y
;
144 m_windowStyle
= style
;
147 width
= wxDIALOG_DEFAULT_WIDTH
;
149 height
= wxDIALOG_DEFAULT_HEIGHT
;
151 // All dialogs should really have this style
152 m_windowStyle
|= wxTAB_TRAVERSAL
;
154 WXDWORD extendedStyle
= MakeExtendedStyle(m_windowStyle
);
155 if (m_windowStyle
& wxSTAY_ON_TOP
)
156 extendedStyle
|= WS_EX_TOPMOST
;
159 if (m_exStyle
& wxDIALOG_EX_CONTEXTHELP
)
160 extendedStyle
|= WS_EX_CONTEXTHELP
;
163 // Allows creation of dialogs with & without captions under MSWindows,
164 // resizeable or not (but a resizeable dialog always has caption -
165 // otherwise it would look too strange)
167 if ( style
& wxRESIZE_BORDER
)
168 dlg
= wxT("wxResizeableDialog");
169 else if ( style
& wxCAPTION
)
170 dlg
= wxT("wxCaptionDialog");
172 dlg
= wxT("wxNoCaptionDialog");
173 MSWCreate(m_windowId
, parent
, NULL
, this, NULL
,
175 0, // style is not used if we have dlg template
179 HWND hwnd
= (HWND
)GetHWND();
183 wxFAIL_MSG(_("Failed to create dialog. You probably forgot to include wx/msw/wx.rc in your resources."));
188 SubclassWin(GetHWND());
190 SetWindowText(hwnd
, title
);
195 bool wxDialog::EnableCloseButton(bool enable
)
197 // get system (a.k.a. window) menu
198 HMENU hmenu
= ::GetSystemMenu(GetHwnd(), FALSE
/* get it */);
201 wxLogLastError(_T("GetSystemMenu"));
206 // enabling/disabling the close item from it also automatically
207 // disables/enabling the close title bar button
208 if ( !::EnableMenuItem(hmenu
, SC_CLOSE
,
209 MF_BYCOMMAND
| (enable
? MF_ENABLED
: MF_GRAYED
)) )
211 wxLogLastError(_T("DeleteMenu(SC_CLOSE)"));
216 // update appearance immediately
217 if ( !::DrawMenuBar(GetHwnd()) )
219 wxLogLastError(_T("DrawMenuBar"));
225 void wxDialog::SetModal(bool flag
)
229 m_windowStyle
|= wxDIALOG_MODAL
;
231 wxModelessWindows
.DeleteObject(this);
235 m_windowStyle
&= ~wxDIALOG_MODAL
;
237 wxModelessWindows
.Append(this);
241 wxDialog::~wxDialog()
243 m_isBeingDeleted
= TRUE
;
245 wxTopLevelWindows
.DeleteObject(this);
247 // this will also reenable all the other windows for a modal dialog
251 wxModelessWindows
.DeleteObject(this);
253 // If this is the last top-level window, exit.
254 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
256 wxTheApp
->SetTopWindow(NULL
);
258 if ( wxTheApp
->GetExitOnFrameDelete() )
260 ::PostQuitMessage(0);
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
269 // By default, pressing escape cancels the dialog
270 void wxDialog::OnCharHook(wxKeyEvent
& event
)
274 // "Esc" works as an accelerator for the "Cancel" button, but it
275 // shouldn't close the dialog which doesn't have any cancel button
276 if ( (event
.m_keyCode
== WXK_ESCAPE
) && FindWindow(wxID_CANCEL
) )
278 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
279 cancelEvent
.SetEventObject( this );
280 GetEventHandler()->ProcessEvent(cancelEvent
);
282 // ensure that there is another message for this window so the
283 // ShowModal loop will exit and won't get stuck in GetMessage().
284 ::PostMessage(GetHwnd(), WM_NULL
, 0, 0);
290 // We didn't process this event.
294 // ----------------------------------------------------------------------------
295 // Windows dialog boxes can't be iconized
296 // ----------------------------------------------------------------------------
298 void wxDialog::Iconize(bool WXUNUSED(iconize
))
302 bool wxDialog::IsIconized() const
307 // ----------------------------------------------------------------------------
308 // size/position handling
309 // ----------------------------------------------------------------------------
311 void wxDialog::DoSetClientSize(int width
, int height
)
313 HWND hWnd
= (HWND
) GetHWND();
315 ::GetClientRect(hWnd
, &rect
);
318 GetWindowRect(hWnd
, &rect2
);
320 // Find the difference between the entire window (title bar and all)
321 // and the client area; add this to the new client size to move the
323 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
324 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
326 MoveWindow(hWnd
, rect2
.left
, rect2
.top
, actual_width
, actual_height
, TRUE
);
328 wxSizeEvent
event(wxSize(actual_width
, actual_height
), m_windowId
);
329 event
.SetEventObject( this );
330 GetEventHandler()->ProcessEvent(event
);
333 void wxDialog::DoGetPosition(int *x
, int *y
) const
336 GetWindowRect(GetHwnd(), &rect
);
344 // ----------------------------------------------------------------------------
345 // showing the dialogs
346 // ----------------------------------------------------------------------------
348 bool wxDialog::IsModal() const
350 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
353 bool wxDialog::IsModalShowing() const
355 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
358 void wxDialog::DoShowModal()
360 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
361 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
363 wxModalDialogs
.Append(this);
365 wxWindow
*parent
= GetParent();
367 wxWindow
* oldFocus
= m_oldFocus
;
369 // We have to remember the HWND because we need to check
370 // the HWND still exists (oldFocus can be garbage when the dialog
371 // exits, if it has been destroyed)
372 HWND hwndOldFocus
= 0;
374 hwndOldFocus
= (HWND
) oldFocus
->GetHWND();
376 // remember where the focus was
381 hwndOldFocus
= GetHwndOf(parent
);
384 // disable all other app windows
385 wxASSERT_MSG( !m_windowDisabler
, _T("disabling windows twice?") );
387 m_windowDisabler
= new wxWindowDisabler(this);
389 // enter the modal loop
390 while ( IsModalShowing() )
393 wxMutexGuiLeaveOrEnter();
394 #endif // wxUSE_THREADS
396 while ( !wxTheApp
->Pending() && wxTheApp
->ProcessIdle() )
399 // a message came or no more idle processing to do
400 wxTheApp
->DoMessage();
404 // Note that this code MUST NOT access the dialog object's data
405 // in case the object has been deleted (which will be the case
406 // for a modal dialog that has been destroyed before calling EndModal).
407 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
409 // This is likely to prove that the object still exists
410 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
411 oldFocus
->SetFocus();
415 bool wxDialog::Show(bool show
)
419 // if we had disabled other app windows, reenable them back now because
420 // if they stay disabled Windows will activate another window (one
421 // which is enabled, anyhow) and we will lose activation
422 if ( m_windowDisabler
)
424 delete m_windowDisabler
;
425 m_windowDisabler
= NULL
;
429 // ShowModal() may be called for already shown dialog
430 if ( !wxDialogBase::Show(show
) && !(show
&& IsModal()) )
438 // usually will result in TransferDataToWindow() being called
446 // modal dialog needs a parent window, so try to find one
449 wxWindow
*parent
= wxTheApp
->GetTopWindow();
450 if ( parent
&& parent
!= this && parent
->IsShown() )
455 // VZ: to make dialog behave properly we should reparent
456 // the dialog for Windows as well - unfortunately,
457 // following the docs for SetParent() results in this
458 // code which plainly doesn't work
460 long dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
461 dwStyle
&= ~WS_POPUP
;
463 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyle
);
464 ::SetParent(GetHwnd(), GetHwndOf(parent
));
471 else // end of modal dialog
473 // this will cause IsModalShowing() return FALSE and our local
474 // message loop will terminate
475 wxModalDialogs
.DeleteObject(this);
482 // a special version for Show(TRUE) for modal dialogs which returns return code
483 int wxDialog::ShowModal()
492 return GetReturnCode();
495 // NB: this function (surprizingly) may be called for both modal and modeless
496 // dialogs and should work for both of them
497 void wxDialog::EndModal(int retCode
)
499 SetReturnCode(retCode
);
504 // ----------------------------------------------------------------------------
505 // wxWin event handlers
506 // ----------------------------------------------------------------------------
509 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
511 if ( Validate() && TransferDataFromWindow() )
517 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
520 TransferDataFromWindow();
522 // TODO probably need to disable the Apply button until things change again
525 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
527 EndModal(wxID_CANCEL
);
530 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
532 // We'll send a Cancel message by default, which may close the dialog.
533 // Check for looping if the Cancel event handler calls Close().
535 // Note that if a cancel button and handler aren't present in the dialog,
536 // nothing will happen when you close the dialog via the window manager, or
537 // via Close(). We wouldn't want to destroy the dialog by default, since
538 // the dialog may have been created on the stack. However, this does mean
539 // that calling dialog->Close() won't delete the dialog unless the handler
540 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
541 // destroy the dialog. The default OnCancel (above) simply ends a modal
542 // dialog, and hides a modeless dialog.
544 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
545 // lists here? don't dare to change it now, but should be done later!
546 static wxList closing
;
548 if ( closing
.Member(this) )
551 closing
.Append(this);
553 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
554 cancelEvent
.SetEventObject( this );
555 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
557 closing
.DeleteObject(this);
560 // Destroy the window (delayed, if a managed window)
561 bool wxDialog::Destroy()
563 wxCHECK_MSG( !wxPendingDelete
.Member(this), FALSE
,
564 _T("wxDialog destroyed twice") );
566 wxPendingDelete
.Append(this);
571 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
576 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
581 // ---------------------------------------------------------------------------
582 // dialog window proc
583 // ---------------------------------------------------------------------------
585 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
588 bool processed
= FALSE
;
592 #if 0 // now that we got owner window right it doesn't seem to be needed
594 switch ( LOWORD(wParam
) )
598 if ( IsModalShowing() && GetParent() )
600 // bring the owner window to top as the standard dialog
604 GetHwndOf(GetParent()),
613 wxLogLastError(wxT("SetWindowPos(SWP_NOACTIVATE)"));
616 // fall through to process it normally as well
622 // if we can't close, tell the system that we processed the
623 // message - otherwise it would close us
624 processed
= !Close();
627 #ifndef __WXMICROWIN__
629 // we want to override the busy cursor for modal dialogs:
630 // typically, wxBeginBusyCursor() is called and then a modal dialog
631 // is shown, but the modal dialog shouldn't have hourglass cursor
632 if ( IsModalShowing() && wxIsBusy() )
634 // set our cursor for all windows (but see below)
635 wxCursor cursor
= m_cursor
;
637 cursor
= wxCURSOR_ARROW
;
639 ::SetCursor(GetHcursorOf(cursor
));
641 // in any case, stop here and don't let wxWindow process this
642 // message (it would set the busy cursor)
645 // but return FALSE to tell the child window (if the event
646 // comes from one of them and not from ourselves) that it can
647 // set its own cursor if it has one: thus, standard controls
648 // (e.g. text ctrl) still have correct cursors in a dialog
649 // invoked while wxIsBusy()
657 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
664 // Define for each class of dialog and control
665 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
666 WXHWND
WXUNUSED(pWnd
),
667 WXUINT
WXUNUSED(nCtlColor
),
672 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
675 #endif // wxUSE_CTL3D