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
, wxWindowID id
,
111 const wxString
& title
,
115 const wxString
& name
)
119 m_oldFocus
= FindFocus();
123 wxTopLevelWindows
.Append(this);
125 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
127 if (parent
) 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
;
162 // Allows creation of dialogs with & without captions under MSWindows,
163 // resizeable or not (but a resizeable dialog always has caption -
164 // otherwise it would look too strange)
166 if ( style
& wxRESIZE_BORDER
)
167 dlg
= wxT("wxResizeableDialog");
168 else if ( style
& wxCAPTION
)
169 dlg
= wxT("wxCaptionDialog");
171 dlg
= wxT("wxNoCaptionDialog");
172 MSWCreate(m_windowId
, parent
, NULL
, this, NULL
,
174 0, // style is not used if we have dlg template
178 HWND hwnd
= (HWND
)GetHWND();
182 wxFAIL_MSG(_("Failed to create dialog. You probably forgot to include wx/msw/wx.rc in your resources."));
187 SubclassWin(GetHWND());
189 SetWindowText(hwnd
, title
);
190 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
195 void wxDialog::SetModal(bool flag
)
199 m_windowStyle
|= wxDIALOG_MODAL
;
201 wxModelessWindows
.DeleteObject(this);
205 m_windowStyle
&= ~wxDIALOG_MODAL
;
207 wxModelessWindows
.Append(this);
211 wxDialog::~wxDialog()
213 m_isBeingDeleted
= TRUE
;
215 wxTopLevelWindows
.DeleteObject(this);
217 // this will also reenable all the other windows for a modal dialog
221 wxModelessWindows
.DeleteObject(this);
223 // If this is the last top-level window, exit.
224 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
226 wxTheApp
->SetTopWindow(NULL
);
228 if ( wxTheApp
->GetExitOnFrameDelete() )
230 ::PostQuitMessage(0);
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 // By default, pressing escape cancels the dialog
240 void wxDialog::OnCharHook(wxKeyEvent
& event
)
244 // "Esc" works as an accelerator for the "Cancel" button, but it
245 // shouldn't close the dialog which doesn't have any cancel button
246 if ( (event
.m_keyCode
== WXK_ESCAPE
) && FindWindow(wxID_CANCEL
) )
248 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
249 cancelEvent
.SetEventObject( this );
250 GetEventHandler()->ProcessEvent(cancelEvent
);
252 // ensure that there is another message for this window so the
253 // ShowModal loop will exit and won't get stuck in GetMessage().
254 ::PostMessage(GetHwnd(), WM_NULL
, 0, 0);
260 // We didn't process this event.
264 // ----------------------------------------------------------------------------
265 // Windows dialog boxes can't be iconized
266 // ----------------------------------------------------------------------------
268 void wxDialog::Iconize(bool WXUNUSED(iconize
))
272 bool wxDialog::IsIconized() const
277 // ----------------------------------------------------------------------------
278 // size/position handling
279 // ----------------------------------------------------------------------------
281 void wxDialog::DoSetClientSize(int width
, int height
)
283 HWND hWnd
= (HWND
) GetHWND();
285 ::GetClientRect(hWnd
, &rect
);
288 GetWindowRect(hWnd
, &rect2
);
290 // Find the difference between the entire window (title bar and all)
291 // and the client area; add this to the new client size to move the
293 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
294 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
296 MoveWindow(hWnd
, rect2
.left
, rect2
.top
, actual_width
, actual_height
, TRUE
);
298 wxSizeEvent
event(wxSize(actual_width
, actual_height
), m_windowId
);
299 event
.SetEventObject( this );
300 GetEventHandler()->ProcessEvent(event
);
303 void wxDialog::DoGetPosition(int *x
, int *y
) const
306 GetWindowRect(GetHwnd(), &rect
);
314 // ----------------------------------------------------------------------------
315 // showing the dialogs
316 // ----------------------------------------------------------------------------
318 bool wxDialog::IsModal() const
320 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
323 bool wxDialog::IsModalShowing() const
325 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
328 void wxDialog::DoShowModal()
330 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
331 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
333 wxModalDialogs
.Append(this);
335 wxWindow
*parent
= GetParent();
337 wxWindow
* oldFocus
= m_oldFocus
;
339 // We have to remember the HWND because we need to check
340 // the HWND still exists (oldFocus can be garbage when the dialog
341 // exits, if it has been destroyed)
342 HWND hwndOldFocus
= 0;
344 hwndOldFocus
= (HWND
) oldFocus
->GetHWND();
346 // remember where the focus was
351 hwndOldFocus
= GetHwndOf(parent
);
354 // disable all other app windows
355 wxASSERT_MSG( !m_windowDisabler
, _T("disabling windows twice?") );
357 m_windowDisabler
= new wxWindowDisabler(this);
359 // enter the modal loop
360 while ( IsModalShowing() )
363 wxMutexGuiLeaveOrEnter();
364 #endif // wxUSE_THREADS
366 while ( !wxTheApp
->Pending() && wxTheApp
->ProcessIdle() )
369 // a message came or no more idle processing to do
370 wxTheApp
->DoMessage();
374 // Note that this code MUST NOT access the dialog object's data
375 // in case the object has been deleted (which will be the case
376 // for a modal dialog that has been destroyed before calling EndModal).
377 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
379 // This is likely to prove that the object still exists
380 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
381 oldFocus
->SetFocus();
385 bool wxDialog::Show(bool show
)
389 // if we had disabled other app windows, reenable them back now because
390 // if they stay disabled Windows will activate another window (one
391 // which is enabled, anyhow) and we will lose activation
392 if ( m_windowDisabler
)
394 delete m_windowDisabler
;
395 m_windowDisabler
= NULL
;
399 // ShowModal() may be called for already shown dialog
400 if ( !wxDialogBase::Show(show
) && !(show
&& IsModal()) )
408 // usually will result in TransferDataToWindow() being called
416 // modal dialog needs a parent window, so try to find one
419 wxWindow
*parent
= wxTheApp
->GetTopWindow();
420 if ( parent
&& parent
!= this && parent
->IsShown() )
425 // VZ: to make dialog behave properly we should reparent
426 // the dialog for Windows as well - unfortunately,
427 // following the docs for SetParent() results in this
428 // code which plainly doesn't work
430 long dwStyle
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
431 dwStyle
&= ~WS_POPUP
;
433 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyle
);
434 ::SetParent(GetHwnd(), GetHwndOf(parent
));
441 else // end of modal dialog
443 // this will cause IsModalShowing() return FALSE and our local
444 // message loop will terminate
445 wxModalDialogs
.DeleteObject(this);
452 // a special version for Show(TRUE) for modal dialogs which returns return code
453 int wxDialog::ShowModal()
462 return GetReturnCode();
465 // NB: this function (surprizingly) may be called for both modal and modeless
466 // dialogs and should work for both of them
467 void wxDialog::EndModal(int retCode
)
469 SetReturnCode(retCode
);
474 // ----------------------------------------------------------------------------
475 // wxWin event handlers
476 // ----------------------------------------------------------------------------
479 void wxDialog::OnOK(wxCommandEvent
& event
)
481 if ( Validate() && TransferDataFromWindow() )
487 void wxDialog::OnApply(wxCommandEvent
& event
)
490 TransferDataFromWindow();
492 // TODO probably need to disable the Apply button until things change again
495 void wxDialog::OnCancel(wxCommandEvent
& event
)
497 EndModal(wxID_CANCEL
);
500 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
502 // We'll send a Cancel message by default, which may close the dialog.
503 // Check for looping if the Cancel event handler calls Close().
505 // Note that if a cancel button and handler aren't present in the dialog,
506 // nothing will happen when you close the dialog via the window manager, or
507 // via Close(). We wouldn't want to destroy the dialog by default, since
508 // the dialog may have been created on the stack. However, this does mean
509 // that calling dialog->Close() won't delete the dialog unless the handler
510 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
511 // destroy the dialog. The default OnCancel (above) simply ends a modal
512 // dialog, and hides a modeless dialog.
514 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
515 // lists here? don't dare to change it now, but should be done later!
516 static wxList closing
;
518 if ( closing
.Member(this) )
521 closing
.Append(this);
523 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
524 cancelEvent
.SetEventObject( this );
525 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
527 closing
.DeleteObject(this);
530 // Destroy the window (delayed, if a managed window)
531 bool wxDialog::Destroy()
533 wxCHECK_MSG( !wxPendingDelete
.Member(this), FALSE
,
534 _T("wxDialog destroyed twice") );
536 wxPendingDelete
.Append(this);
541 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
546 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
551 // ---------------------------------------------------------------------------
552 // dialog window proc
553 // ---------------------------------------------------------------------------
555 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
558 bool processed
= FALSE
;
562 #if 0 // now that we got owner window right it doesn't seem to be needed
564 switch ( LOWORD(wParam
) )
568 if ( IsModalShowing() && GetParent() )
570 // bring the owner window to top as the standard dialog
574 GetHwndOf(GetParent()),
583 wxLogLastError(wxT("SetWindowPos(SWP_NOACTIVATE)"));
586 // fall through to process it normally as well
592 // if we can't close, tell the system that we processed the
593 // message - otherwise it would close us
594 processed
= !Close();
598 // we want to override the busy cursor for modal dialogs:
599 // typically, wxBeginBusyCursor() is called and then a modal dialog
600 // is shown, but the modal dialog shouldn't have hourglass cursor
601 if ( IsModalShowing() && wxIsBusy() )
603 // set our cursor for all windows (but see below)
604 wxCursor cursor
= m_cursor
;
606 cursor
= wxCURSOR_ARROW
;
608 ::SetCursor(GetHcursorOf(cursor
));
610 // in any case, stop here and don't let wxWindow process this
611 // message (it would set the busy cursor)
614 // but return FALSE to tell the child window (if the event
615 // comes from one of them and not from ourselves) that it can
616 // set its own cursor if it has one: thus, standard controls
617 // (e.g. text ctrl) still have correct cursors in a dialog
618 // invoked while wxIsBusy()
625 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
632 // Define for each class of dialog and control
633 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
634 WXHWND
WXUNUSED(pWnd
),
635 WXUINT
WXUNUSED(nCtlColor
),
640 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
643 #endif // wxUSE_CTL3D