1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dialog.cpp
3 // Purpose: wxDialog class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
43 #include "wx/evtloop.h"
44 #include "wx/ptr_scpd.h"
46 #if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // default dialog pos and size
56 #define wxDIALOG_DEFAULT_X 300
57 #define wxDIALOG_DEFAULT_Y 300
59 #define wxDIALOG_DEFAULT_WIDTH 500
60 #define wxDIALOG_DEFAULT_HEIGHT 500
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 #if wxUSE_EXTENDED_RTTI
67 WX_DEFINE_FLAGS( wxDialogStyle
)
69 wxBEGIN_FLAGS( wxDialogStyle
)
70 // new style border flags, we put them first to
71 // use them for streaming out
72 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
73 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
74 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
75 wxFLAGS_MEMBER(wxBORDER_RAISED
)
76 wxFLAGS_MEMBER(wxBORDER_STATIC
)
77 wxFLAGS_MEMBER(wxBORDER_NONE
)
79 // old style border flags
80 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
81 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
82 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
83 wxFLAGS_MEMBER(wxRAISED_BORDER
)
84 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
85 wxFLAGS_MEMBER(wxNO_BORDER
)
87 // standard window styles
88 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
89 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
92 wxFLAGS_MEMBER(wxDIALOG_MODAL
)
93 wxFLAGS_MEMBER(wxDIALOG_MODELESS
)
94 wxFLAGS_MEMBER(wxNO_3D
)
95 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY
)
96 wxFLAGS_MEMBER(wxSTAY_ON_TOP
)
97 wxFLAGS_MEMBER(wxCAPTION
)
98 wxFLAGS_MEMBER(wxTHICK_FRAME
)
99 wxFLAGS_MEMBER(wxSYSTEM_MENU
)
100 wxFLAGS_MEMBER(wxRESIZE_BORDER
)
101 wxFLAGS_MEMBER(wxRESIZE_BOX
)
102 wxFLAGS_MEMBER(wxCLOSE_BOX
)
103 wxFLAGS_MEMBER(wxMAXIMIZE_BOX
)
104 wxFLAGS_MEMBER(wxMINIMIZE_BOX
)
105 wxEND_FLAGS( wxDialogStyle
)
107 IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog
, wxTopLevelWindow
,"wx/dialog.h")
109 wxBEGIN_PROPERTIES_TABLE(wxDialog
)
110 wxPROPERTY( Title
,wxString
, SetTitle
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
111 wxPROPERTY_FLAGS( WindowStyle
, wxDialogStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
112 wxEND_PROPERTIES_TABLE()
114 wxBEGIN_HANDLERS_TABLE(wxDialog
)
115 wxEND_HANDLERS_TABLE()
117 wxCONSTRUCTOR_6( wxDialog
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Title
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
120 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
123 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
124 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
125 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
126 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
128 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
130 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
132 EVT_CLOSE(wxDialog::OnCloseWindow
)
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 // this is simply a container for any data we need to implement modality which
140 // allows us to avoid changing wxDialog each time the implementation changes
141 class wxDialogModalData
144 wxDialogModalData(wxDialog
*dialog
) : m_evtLoop(dialog
) { }
157 wxModalEventLoop m_evtLoop
;
160 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData
);
162 // ============================================================================
164 // ============================================================================
166 // ----------------------------------------------------------------------------
167 // wxDialog construction
168 // ----------------------------------------------------------------------------
170 void wxDialog::Init()
172 m_oldFocus
= (wxWindow
*)NULL
;
175 m_endModalCalled
= FALSE
;
178 bool wxDialog::Create(wxWindow
*parent
,
180 const wxString
& title
,
184 const wxString
& name
)
186 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
188 // save focus before doing anything which can potentially change it
189 m_oldFocus
= FindFocus();
191 // All dialogs should really have this style
192 style
|= wxTAB_TRAVERSAL
;
194 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
198 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
200 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
205 void wxDialog::SetModal(bool flag
)
209 m_windowStyle
|= wxDIALOG_MODAL
;
211 wxModelessWindows
.DeleteObject(this);
215 m_windowStyle
&= ~wxDIALOG_MODAL
;
217 wxModelessWindows
.Append(this);
221 wxDialog::~wxDialog()
223 m_isBeingDeleted
= TRUE
;
225 // this will also reenable all the other windows for a modal dialog
229 // ----------------------------------------------------------------------------
231 // ----------------------------------------------------------------------------
233 // By default, pressing escape cancels the dialog
234 void wxDialog::OnCharHook(wxKeyEvent
& event
)
238 // "Esc" works as an accelerator for the "Cancel" button, but it
239 // shouldn't close the dialog which doesn't have any cancel button
240 if ( (event
.m_keyCode
== WXK_ESCAPE
) && FindWindow(wxID_CANCEL
) )
242 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
243 cancelEvent
.SetEventObject( this );
244 GetEventHandler()->ProcessEvent(cancelEvent
);
246 // ensure that there is another message for this window so the
247 // ShowModal loop will exit and won't get stuck in GetMessage().
248 ::PostMessage(GetHwnd(), WM_NULL
, 0, 0);
254 // We didn't process this event.
258 // ----------------------------------------------------------------------------
259 // showing the dialogs
260 // ----------------------------------------------------------------------------
262 bool wxDialog::IsModal() const
264 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
267 bool wxDialog::IsModalShowing() const
269 return m_modalData
!= NULL
;
272 wxWindow
*wxDialog::FindSuitableParent() const
274 // first try to use the currently active window
275 HWND hwndFg
= ::GetForegroundWindow();
276 wxWindow
*parent
= hwndFg
? wxFindWinFromHandle((WXHWND
)hwndFg
)
280 // next try the main app window
281 parent
= wxTheApp
->GetTopWindow();
284 // finally, check if the parent we found is really suitable
285 if ( !parent
|| parent
== (wxWindow
*)this || !parent
->IsShown() )
287 // don't use this one
294 void wxDialog::DoShowModal()
296 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
297 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
299 wxWindow
*parent
= GetParent();
301 wxWindow
* oldFocus
= m_oldFocus
;
303 // We have to remember the HWND because we need to check
304 // the HWND still exists (oldFocus can be garbage when the dialog
305 // exits, if it has been destroyed)
306 HWND hwndOldFocus
= 0;
308 hwndOldFocus
= (HWND
) oldFocus
->GetHWND();
310 // remember where the focus was
315 hwndOldFocus
= GetHwndOf(parent
);
318 // enter the modal loop
320 wxDialogModalDataTiedPtr
modalData(&m_modalData
,
321 new wxDialogModalData(this));
322 modalData
->RunLoop();
326 // Note that this code MUST NOT access the dialog object's data
327 // in case the object has been deleted (which will be the case
328 // for a modal dialog that has been destroyed before calling EndModal).
329 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
331 // This is likely to prove that the object still exists
332 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
333 oldFocus
->SetFocus();
337 bool wxDialog::Show(bool show
)
339 if ( !show
&& m_modalData
)
341 // we need to do this before calling wxDialogBase version because if we
342 // had disabled other app windows, they must be reenabled right now as
343 // if they stay disabled Windows will activate another window (one
344 // which is enabled, anyhow) when we're hidden in the base class Show()
345 // and we will lose activation
346 m_modalData
->ExitLoop();
349 // ShowModal() may be called for already shown dialog
350 if ( !wxDialogBase::Show(show
) && !(show
&& IsModal()) )
358 // dialogs don't get WM_SIZE message after creation unlike most (all?)
359 // other windows and so could start their life non laid out correctly
360 // if we didn't call Layout() from here
362 // NB: normally we should call it just the first time but doing it
363 // every time is simpler than keeping a flag
366 // this usually will result in TransferDataToWindow() being called
370 // EndModal may have been called from InitDialog handler,
371 // which would cause an infinite loop if we didn't take it
373 if ( show
&& IsModal() && !m_endModalCalled
)
375 // modal dialog needs a parent window, so try to find one
378 m_parent
= FindSuitableParent();
387 void wxDialog::Raise()
389 ::SetForegroundWindow(GetHwnd());
392 // a special version for Show(TRUE) for modal dialogs which returns return code
393 int wxDialog::ShowModal()
395 m_endModalCalled
= FALSE
;
403 return GetReturnCode();
406 // NB: this function (surprizingly) may be called for both modal and modeless
407 // dialogs and should work for both of them
408 void wxDialog::EndModal(int retCode
)
410 m_endModalCalled
= TRUE
;
411 SetReturnCode(retCode
);
416 // ----------------------------------------------------------------------------
417 // wxWin event handlers
418 // ----------------------------------------------------------------------------
421 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
423 if ( Validate() && TransferDataFromWindow() )
429 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
432 TransferDataFromWindow();
434 // TODO probably need to disable the Apply button until things change again
437 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
439 EndModal(wxID_CANCEL
);
442 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
444 // We'll send a Cancel message by default, which may close the dialog.
445 // Check for looping if the Cancel event handler calls Close().
447 // Note that if a cancel button and handler aren't present in the dialog,
448 // nothing will happen when you close the dialog via the window manager, or
449 // via Close(). We wouldn't want to destroy the dialog by default, since
450 // the dialog may have been created on the stack. However, this does mean
451 // that calling dialog->Close() won't delete the dialog unless the handler
452 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
453 // destroy the dialog. The default OnCancel (above) simply ends a modal
454 // dialog, and hides a modeless dialog.
456 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
457 // lists here? don't dare to change it now, but should be done later!
458 static wxList closing
;
460 if ( closing
.Member(this) )
463 closing
.Append(this);
465 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
466 cancelEvent
.SetEventObject( this );
467 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
469 closing
.DeleteObject(this);
472 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
477 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
482 // ---------------------------------------------------------------------------
483 // dialog window proc
484 // ---------------------------------------------------------------------------
486 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
489 bool processed
= FALSE
;
494 // if we can't close, tell the system that we processed the
495 // message - otherwise it would close us
496 processed
= !Close();
500 // the Windows dialogs unfortunately are not meant to be resizeable
501 // at all and their standard class doesn't include CS_[VH]REDRAW
502 // styles which means that the window is not refreshed properly
503 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
504 // help with it - so we have to refresh it manually which certainly
505 // creates flicker but at least doesn't show garbage on the screen
506 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
508 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
510 ::InvalidateRect(GetHwnd(), NULL
, FALSE
/* erase bg */);
514 #ifndef __WXMICROWIN__
516 // we want to override the busy cursor for modal dialogs:
517 // typically, wxBeginBusyCursor() is called and then a modal dialog
518 // is shown, but the modal dialog shouldn't have hourglass cursor
519 if ( IsModalShowing() && wxIsBusy() )
521 // set our cursor for all windows (but see below)
522 wxCursor cursor
= m_cursor
;
524 cursor
= wxCURSOR_ARROW
;
526 ::SetCursor(GetHcursorOf(cursor
));
528 // in any case, stop here and don't let wxWindow process this
529 // message (it would set the busy cursor)
532 // but return FALSE to tell the child window (if the event
533 // comes from one of them and not from ourselves) that it can
534 // set its own cursor if it has one: thus, standard controls
535 // (e.g. text ctrl) still have correct cursors in a dialog
536 // invoked while wxIsBusy()
540 #endif // __WXMICROWIN__
544 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
551 // Define for each class of dialog and control
552 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
553 WXHWND
WXUNUSED(pWnd
),
554 WXUINT
WXUNUSED(nCtlColor
),
559 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
562 #endif // wxUSE_CTL3D