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");
174 #ifdef __WXMICROWIN__
175 extern const wxChar
*wxFrameClassName
;
177 int msflags
= WS_OVERLAPPED
;
178 if (style
& wxCAPTION
)
179 msflags
|= WS_CAPTION
;
180 if (style
& wxCLIP_CHILDREN
)
181 msflags
|= WS_CLIPCHILDREN
;
182 if ((style
& wxTHICK_FRAME
) == 0)
183 msflags
|= WS_BORDER
;
184 MSWCreate(m_windowId
, parent
, wxFrameClassName
, this, NULL
,
191 MSWCreate(m_windowId
, parent
, NULL
, this, NULL
,
193 0, // style is not used if we have dlg template
197 HWND hwnd
= (HWND
)GetHWND();
201 wxFAIL_MSG(_("Failed to create dialog. You probably forgot to include wx/msw/wx.rc in your resources."));
206 #ifndef __WXMICROWIN__
207 SubclassWin(GetHWND());
210 SetWindowText(hwnd
, title
);
215 bool wxDialog::EnableCloseButton(bool enable
)
217 #ifndef __WXMICROWIN__
218 // get system (a.k.a. window) menu
219 HMENU hmenu
= ::GetSystemMenu(GetHwnd(), FALSE
/* get it */);
222 wxLogLastError(_T("GetSystemMenu"));
227 // enabling/disabling the close item from it also automatically
228 // disables/enabling the close title bar button
229 if ( !::EnableMenuItem(hmenu
, SC_CLOSE
,
230 MF_BYCOMMAND
| (enable
? MF_ENABLED
: MF_GRAYED
)) )
232 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
237 // update appearance immediately
238 if ( !::DrawMenuBar(GetHwnd()) )
240 wxLogLastError(_T("DrawMenuBar"));
247 void wxDialog::SetModal(bool flag
)
251 m_windowStyle
|= wxDIALOG_MODAL
;
253 wxModelessWindows
.DeleteObject(this);
257 m_windowStyle
&= ~wxDIALOG_MODAL
;
259 wxModelessWindows
.Append(this);
263 wxDialog::~wxDialog()
265 m_isBeingDeleted
= TRUE
;
267 wxTopLevelWindows
.DeleteObject(this);
269 // this will also reenable all the other windows for a modal dialog
273 wxModelessWindows
.DeleteObject(this);
275 // If this is the last top-level window, exit.
276 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
278 wxTheApp
->SetTopWindow(NULL
);
280 if ( wxTheApp
->GetExitOnFrameDelete() )
282 ::PostQuitMessage(0);
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 // By default, pressing escape cancels the dialog
292 void wxDialog::OnCharHook(wxKeyEvent
& event
)
296 // "Esc" works as an accelerator for the "Cancel" button, but it
297 // shouldn't close the dialog which doesn't have any cancel button
298 if ( (event
.m_keyCode
== WXK_ESCAPE
) && FindWindow(wxID_CANCEL
) )
300 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
301 cancelEvent
.SetEventObject( this );
302 GetEventHandler()->ProcessEvent(cancelEvent
);
304 // ensure that there is another message for this window so the
305 // ShowModal loop will exit and won't get stuck in GetMessage().
306 ::PostMessage(GetHwnd(), WM_NULL
, 0, 0);
312 // We didn't process this event.
316 // ----------------------------------------------------------------------------
317 // Windows dialog boxes can't be iconized
318 // ----------------------------------------------------------------------------
320 void wxDialog::Iconize(bool WXUNUSED(iconize
))
324 bool wxDialog::IsIconized() const
329 // ----------------------------------------------------------------------------
330 // size/position handling
331 // ----------------------------------------------------------------------------
333 void wxDialog::DoSetClientSize(int width
, int height
)
335 HWND hWnd
= (HWND
) GetHWND();
337 ::GetClientRect(hWnd
, &rect
);
340 GetWindowRect(hWnd
, &rect2
);
342 // Find the difference between the entire window (title bar and all)
343 // and the client area; add this to the new client size to move the
345 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
346 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
348 MoveWindow(hWnd
, rect2
.left
, rect2
.top
, actual_width
, actual_height
, TRUE
);
350 wxSizeEvent
event(wxSize(actual_width
, actual_height
), m_windowId
);
351 event
.SetEventObject( this );
352 GetEventHandler()->ProcessEvent(event
);
355 void wxDialog::DoGetPosition(int *x
, int *y
) const
358 GetWindowRect(GetHwnd(), &rect
);
366 // ----------------------------------------------------------------------------
367 // showing the dialogs
368 // ----------------------------------------------------------------------------
370 bool wxDialog::IsModal() const
372 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
375 bool wxDialog::IsModalShowing() const
377 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
380 void wxDialog::DoShowModal()
382 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
383 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
385 wxModalDialogs
.Append(this);
387 wxWindow
*parent
= GetParent();
389 wxWindow
* oldFocus
= m_oldFocus
;
391 // We have to remember the HWND because we need to check
392 // the HWND still exists (oldFocus can be garbage when the dialog
393 // exits, if it has been destroyed)
394 HWND hwndOldFocus
= 0;
396 hwndOldFocus
= (HWND
) oldFocus
->GetHWND();
398 // remember where the focus was
403 hwndOldFocus
= GetHwndOf(parent
);
406 // disable all other app windows
407 wxASSERT_MSG( !m_windowDisabler
, _T("disabling windows twice?") );
409 m_windowDisabler
= new wxWindowDisabler(this);
411 // enter the modal loop
412 while ( IsModalShowing() )
415 wxMutexGuiLeaveOrEnter();
416 #endif // wxUSE_THREADS
418 while ( !wxTheApp
->Pending() && wxTheApp
->ProcessIdle() )
421 // a message came or no more idle processing to do
422 wxTheApp
->DoMessage();
426 // Note that this code MUST NOT access the dialog object's data
427 // in case the object has been deleted (which will be the case
428 // for a modal dialog that has been destroyed before calling EndModal).
429 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
431 // This is likely to prove that the object still exists
432 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
433 oldFocus
->SetFocus();
437 bool wxDialog::Show(bool show
)
441 // if we had disabled other app windows, reenable them back now because
442 // if they stay disabled Windows will activate another window (one
443 // which is enabled, anyhow) and we will lose activation
444 if ( m_windowDisabler
)
446 delete m_windowDisabler
;
447 m_windowDisabler
= NULL
;
451 // ShowModal() may be called for already shown dialog
452 if ( !wxDialogBase::Show(show
) && !(show
&& IsModal()) )
460 // usually will result in TransferDataToWindow() being called
468 // modal dialog needs a parent window, so try to find one
471 wxWindow
*parent
= wxTheApp
->GetTopWindow();
472 if ( parent
&& parent
!= this && parent
->IsShown() )
477 // VZ: to make dialog behave properly we should reparent
478 // the dialog for Windows as well - unfortunately,
479 // following the docs for SetParent() results in this
480 // code which plainly doesn't work
482 long dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
483 dwStyle
&= ~WS_POPUP
;
485 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyle
);
486 ::SetParent(GetHwnd(), GetHwndOf(parent
));
493 else // end of modal dialog
495 // this will cause IsModalShowing() return FALSE and our local
496 // message loop will terminate
497 wxModalDialogs
.DeleteObject(this);
504 // a special version for Show(TRUE) for modal dialogs which returns return code
505 int wxDialog::ShowModal()
514 return GetReturnCode();
517 // NB: this function (surprizingly) may be called for both modal and modeless
518 // dialogs and should work for both of them
519 void wxDialog::EndModal(int retCode
)
521 SetReturnCode(retCode
);
526 // ----------------------------------------------------------------------------
527 // wxWin event handlers
528 // ----------------------------------------------------------------------------
531 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
533 if ( Validate() && TransferDataFromWindow() )
539 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
542 TransferDataFromWindow();
544 // TODO probably need to disable the Apply button until things change again
547 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
549 EndModal(wxID_CANCEL
);
552 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
554 // We'll send a Cancel message by default, which may close the dialog.
555 // Check for looping if the Cancel event handler calls Close().
557 // Note that if a cancel button and handler aren't present in the dialog,
558 // nothing will happen when you close the dialog via the window manager, or
559 // via Close(). We wouldn't want to destroy the dialog by default, since
560 // the dialog may have been created on the stack. However, this does mean
561 // that calling dialog->Close() won't delete the dialog unless the handler
562 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
563 // destroy the dialog. The default OnCancel (above) simply ends a modal
564 // dialog, and hides a modeless dialog.
566 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
567 // lists here? don't dare to change it now, but should be done later!
568 static wxList closing
;
570 if ( closing
.Member(this) )
573 closing
.Append(this);
575 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
576 cancelEvent
.SetEventObject( this );
577 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
579 closing
.DeleteObject(this);
582 // Destroy the window (delayed, if a managed window)
583 bool wxDialog::Destroy()
585 wxCHECK_MSG( !wxPendingDelete
.Member(this), FALSE
,
586 _T("wxDialog destroyed twice") );
588 wxPendingDelete
.Append(this);
593 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
598 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
603 // ---------------------------------------------------------------------------
604 // dialog window proc
605 // ---------------------------------------------------------------------------
607 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
610 bool processed
= FALSE
;
614 #if 0 // now that we got owner window right it doesn't seem to be needed
616 switch ( LOWORD(wParam
) )
620 if ( IsModalShowing() && GetParent() )
622 // bring the owner window to top as the standard dialog
626 GetHwndOf(GetParent()),
635 wxLogLastError(wxT("SetWindowPos(SWP_NOACTIVATE)"));
638 // fall through to process it normally as well
644 // if we can't close, tell the system that we processed the
645 // message - otherwise it would close us
646 processed
= !Close();
649 #ifndef __WXMICROWIN__
651 // we want to override the busy cursor for modal dialogs:
652 // typically, wxBeginBusyCursor() is called and then a modal dialog
653 // is shown, but the modal dialog shouldn't have hourglass cursor
654 if ( IsModalShowing() && wxIsBusy() )
656 // set our cursor for all windows (but see below)
657 wxCursor cursor
= m_cursor
;
659 cursor
= wxCURSOR_ARROW
;
661 ::SetCursor(GetHcursorOf(cursor
));
663 // in any case, stop here and don't let wxWindow process this
664 // message (it would set the busy cursor)
667 // but return FALSE to tell the child window (if the event
668 // comes from one of them and not from ourselves) that it can
669 // set its own cursor if it has one: thus, standard controls
670 // (e.g. text ctrl) still have correct cursors in a dialog
671 // invoked while wxIsBusy()
679 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
686 // Define for each class of dialog and control
687 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
688 WXHWND
WXUNUSED(pWnd
),
689 WXUINT
WXUNUSED(nCtlColor
),
694 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
697 #endif // wxUSE_CTL3D