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
;
104 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
107 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
108 const wxString
& title
,
112 const wxString
& name
)
114 m_oldFocus
= FindFocus();
116 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
119 wxTopLevelWindows
.Append(this);
121 // windowFont = wxTheFontList->FindOrCreateFont(11, wxSWISS, wxNORMAL, wxNORMAL);
123 if (parent
) parent
->AddChild(this);
126 m_windowId
= (int)NewControlId();
136 x
= wxDIALOG_DEFAULT_X
;
138 y
= wxDIALOG_DEFAULT_Y
;
140 m_windowStyle
= style
;
145 width
= wxDIALOG_DEFAULT_WIDTH
;
147 height
= wxDIALOG_DEFAULT_HEIGHT
;
149 // All dialogs should really have this style
150 m_windowStyle
|= wxTAB_TRAVERSAL
;
152 WXDWORD extendedStyle
= MakeExtendedStyle(m_windowStyle
);
153 if (m_windowStyle
& wxSTAY_ON_TOP
)
154 extendedStyle
|= WS_EX_TOPMOST
;
155 if (m_exStyle
& wxFRAME_EX_CONTEXTHELP
)
156 extendedStyle
|= WS_EX_CONTEXTHELP
;
158 // Allows creation of dialogs with & without captions under MSWindows,
159 // resizeable or not (but a resizeable dialog always has caption -
160 // otherwise it would look too strange)
162 if ( style
& wxRESIZE_BORDER
)
163 dlg
= wxT("wxResizeableDialog");
164 else if ( style
& wxCAPTION
)
165 dlg
= wxT("wxCaptionDialog");
167 dlg
= wxT("wxNoCaptionDialog");
168 MSWCreate(m_windowId
, parent
, NULL
, this, NULL
,
170 0, // style is not used if we have dlg template
174 HWND hwnd
= (HWND
)GetHWND();
178 wxFAIL_MSG(_("Failed to create dialog. You probably forgot to include wx/msw/wx.rc in your resources."));
183 SubclassWin(GetHWND());
185 SetWindowText(hwnd
, title
);
186 SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
191 void wxDialog::SetModal(bool flag
)
195 m_windowStyle
|= wxDIALOG_MODAL
;
197 wxModelessWindows
.DeleteObject(this);
201 m_windowStyle
&= ~wxDIALOG_MODAL
;
203 wxModelessWindows
.Append(this);
207 wxDialog::~wxDialog()
209 m_isBeingDeleted
= TRUE
;
211 wxTopLevelWindows
.DeleteObject(this);
213 // this will call BringWindowToTop() if necessary to bring back our parent
218 wxModelessWindows
.DeleteObject(this);
220 // If this is the last top-level window, exit.
221 if ( wxTheApp
&& (wxTopLevelWindows
.Number() == 0) )
223 wxTheApp
->SetTopWindow(NULL
);
225 if ( wxTheApp
->GetExitOnFrameDelete() )
227 ::PostQuitMessage(0);
232 // ----------------------------------------------------------------------------
234 // ----------------------------------------------------------------------------
236 // By default, pressing escape cancels the dialog
237 void wxDialog::OnCharHook(wxKeyEvent
& event
)
241 // "Esc" works as an accelerator for the "Cancel" button, but it
242 // shouldn't close the dialog which doesn't have any cancel button
243 if ( (event
.m_keyCode
== WXK_ESCAPE
) && FindWindow(wxID_CANCEL
) )
245 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
246 cancelEvent
.SetEventObject( this );
247 GetEventHandler()->ProcessEvent(cancelEvent
);
249 // ensure that there is another message for this window so the
250 // ShowModal loop will exit and won't get stuck in GetMessage().
251 ::PostMessage(GetHwnd(), WM_NULL
, 0, 0);
257 // We didn't process this event.
261 // ----------------------------------------------------------------------------
262 // Windows dialog boxes can't be iconized
263 // ----------------------------------------------------------------------------
265 void wxDialog::Iconize(bool WXUNUSED(iconize
))
269 bool wxDialog::IsIconized() const
274 // ----------------------------------------------------------------------------
275 // size/position handling
276 // ----------------------------------------------------------------------------
278 void wxDialog::DoSetClientSize(int width
, int height
)
280 HWND hWnd
= (HWND
) GetHWND();
282 ::GetClientRect(hWnd
, &rect
);
285 GetWindowRect(hWnd
, &rect2
);
287 // Find the difference between the entire window (title bar and all)
288 // and the client area; add this to the new client size to move the
290 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
291 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
293 MoveWindow(hWnd
, rect2
.left
, rect2
.top
, actual_width
, actual_height
, TRUE
);
295 wxSizeEvent
event(wxSize(actual_width
, actual_height
), m_windowId
);
296 event
.SetEventObject( this );
297 GetEventHandler()->ProcessEvent(event
);
300 void wxDialog::DoGetPosition(int *x
, int *y
) const
303 GetWindowRect(GetHwnd(), &rect
);
311 // ----------------------------------------------------------------------------
312 // showing the dialogs
313 // ----------------------------------------------------------------------------
315 bool wxDialog::IsModal() const
317 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
320 bool wxDialog::IsModalShowing() const
322 return wxModalDialogs
.Find((wxDialog
*)this) != NULL
; // const_cast
325 void wxDialog::DoShowModal()
327 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
328 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
330 wxModalDialogs
.Append(this);
332 wxWindow
*parent
= GetParent();
334 wxWindow
* oldFocus
= m_oldFocus
;
336 // We have to remember the HWND because we need to check
337 // the HWND still exists (oldFocus can be garbage when the dialog
338 // exits, if it has been destroyed)
339 HWND hwndOldFocus
= 0;
341 hwndOldFocus
= (HWND
) oldFocus
->GetHWND();
343 // inside this block, all app windows are disabled
345 wxWindowDisabler
wd(this);
347 // remember where the focus was
352 hwndOldFocus
= (HWND
) parent
->GetHWND();
355 // enter the modal loop
356 while ( IsModalShowing() )
359 wxMutexGuiLeaveOrEnter();
360 #endif // wxUSE_THREADS
362 while ( !wxTheApp
->Pending() && wxTheApp
->ProcessIdle() )
365 // a message came or no more idle processing to do
366 wxTheApp
->DoMessage();
372 ::SetActiveWindow(GetHwndOf(parent
));
376 // Note that this code MUST NOT access the dialog object's data
377 // in case the object has been deleted (which will be the case
378 // for a modal dialog that has been destroyed before calling EndModal).
379 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
381 // This is likely to prove that the object still exists
382 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
383 oldFocus
->SetFocus();
387 bool wxDialog::Show(bool show
)
389 // The following is required when the parent has been disabled, (modal
390 // dialogs, or modeless dialogs with disabling such as wxProgressDialog).
391 // Otherwise the parent disappears behind other windows when the dialog is
395 wxWindow
*parent
= GetParent();
398 ::BringWindowToTop(GetHwndOf(parent
));
402 // ShowModal() may be called for already shown dialog
403 if ( !wxDialogBase::Show(show
) && !(show
&& IsModal()) )
411 // usually will result in TransferDataToWindow() being called
419 // modal dialog needs a parent window, so try to find one
422 wxWindow
*parent
= wxTheApp
->GetTopWindow();
423 if ( parent
&& parent
!= this && parent
->IsShown() )
432 else // end of modal dialog
434 // this will cause IsModalShowing() return FALSE and our local
435 // message loop will terminate
436 wxModalDialogs
.DeleteObject(this);
443 // a special version for Show(TRUE) for modal dialogs which returns return code
444 int wxDialog::ShowModal()
453 return GetReturnCode();
456 // NB: this function (surprizingly) may be called for both modal and modeless
457 // dialogs and should work for both of them
458 void wxDialog::EndModal(int retCode
)
460 SetReturnCode(retCode
);
465 // ----------------------------------------------------------------------------
466 // wxWin event handlers
467 // ----------------------------------------------------------------------------
470 void wxDialog::OnOK(wxCommandEvent
& event
)
472 if ( Validate() && TransferDataFromWindow() )
478 void wxDialog::OnApply(wxCommandEvent
& event
)
481 TransferDataFromWindow();
483 // TODO probably need to disable the Apply button until things change again
486 void wxDialog::OnCancel(wxCommandEvent
& event
)
488 EndModal(wxID_CANCEL
);
491 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
493 // We'll send a Cancel message by default, which may close the dialog.
494 // Check for looping if the Cancel event handler calls Close().
496 // Note that if a cancel button and handler aren't present in the dialog,
497 // nothing will happen when you close the dialog via the window manager, or
498 // via Close(). We wouldn't want to destroy the dialog by default, since
499 // the dialog may have been created on the stack. However, this does mean
500 // that calling dialog->Close() won't delete the dialog unless the handler
501 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
502 // destroy the dialog. The default OnCancel (above) simply ends a modal
503 // dialog, and hides a modeless dialog.
505 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
506 // lists here? don't dare to change it now, but should be done later!
507 static wxList closing
;
509 if ( closing
.Member(this) )
512 closing
.Append(this);
514 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
515 cancelEvent
.SetEventObject( this );
516 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
518 closing
.DeleteObject(this);
521 // Destroy the window (delayed, if a managed window)
522 bool wxDialog::Destroy()
524 wxCHECK_MSG( !wxPendingDelete
.Member(this), FALSE
,
525 _T("wxDialog destroyed twice") );
527 wxPendingDelete
.Append(this);
532 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& event
)
537 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
));
542 // ---------------------------------------------------------------------------
543 // dialog window proc
544 // ---------------------------------------------------------------------------
546 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
549 bool processed
= FALSE
;
554 switch ( LOWORD(wParam
) )
558 if ( IsModalShowing() && GetParent() )
560 // bring the owner window to top as the standard dialog
564 GetHwndOf(GetParent()),
573 wxLogLastError(wxT("SetWindowPos(SWP_NOACTIVATE)"));
576 // fall through to process it normally as well
581 // if we can't close, tell the system that we processed the
582 // message - otherwise it would close us
583 processed
= !Close();
587 // we want to override the busy cursor for modal dialogs:
588 // typically, wxBeginBusyCursor() is called and then a modal dialog
589 // is shown, but the modal dialog shouldn't have hourglass cursor
590 if ( IsModalShowing() && wxIsBusy() )
592 // set our cursor for all windows (but see below)
593 wxCursor cursor
= m_cursor
;
595 cursor
= wxCURSOR_ARROW
;
597 ::SetCursor(GetHcursorOf(cursor
));
599 // in any case, stop here and don't let wxWindow process this
600 // message (it would set the busy cursor)
603 // but return FALSE to tell the child window (if the event
604 // comes from one of them and not from ourselves) that it can
605 // set its own cursor if it has one: thus, standard controls
606 // (e.g. text ctrl) still have correct cursors in a dialog
607 // invoked while wxIsBusy()
614 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
621 // Define for each class of dialog and control
622 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
623 WXHWND
WXUNUSED(pWnd
),
624 WXUINT
WXUNUSED(nCtlColor
),
629 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
632 #endif // wxUSE_CTL3D