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 #if wxUSE_EXTENDED_RTTI
55 WX_DEFINE_FLAGS( wxDialogStyle
)
57 wxBEGIN_FLAGS( wxDialogStyle
)
58 // new style border flags, we put them first to
59 // use them for streaming out
60 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
61 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
62 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
63 wxFLAGS_MEMBER(wxBORDER_RAISED
)
64 wxFLAGS_MEMBER(wxBORDER_STATIC
)
65 wxFLAGS_MEMBER(wxBORDER_NONE
)
67 // old style border flags
68 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
69 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
70 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
71 wxFLAGS_MEMBER(wxRAISED_BORDER
)
72 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
73 wxFLAGS_MEMBER(wxNO_BORDER
)
75 // standard window styles
76 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
77 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
80 wxFLAGS_MEMBER(wxDIALOG_MODAL
)
81 wxFLAGS_MEMBER(wxDIALOG_MODELESS
)
82 wxFLAGS_MEMBER(wxNO_3D
)
83 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY
)
84 wxFLAGS_MEMBER(wxSTAY_ON_TOP
)
85 wxFLAGS_MEMBER(wxCAPTION
)
86 wxFLAGS_MEMBER(wxTHICK_FRAME
)
87 wxFLAGS_MEMBER(wxSYSTEM_MENU
)
88 wxFLAGS_MEMBER(wxRESIZE_BORDER
)
89 wxFLAGS_MEMBER(wxRESIZE_BOX
)
90 wxFLAGS_MEMBER(wxCLOSE_BOX
)
91 wxFLAGS_MEMBER(wxMAXIMIZE_BOX
)
92 wxFLAGS_MEMBER(wxMINIMIZE_BOX
)
93 wxEND_FLAGS( wxDialogStyle
)
95 IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog
, wxTopLevelWindow
,"wx/dialog.h")
97 wxBEGIN_PROPERTIES_TABLE(wxDialog
)
98 wxPROPERTY( Title
,wxString
, SetTitle
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
99 wxPROPERTY_FLAGS( WindowStyle
, wxDialogStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
100 wxEND_PROPERTIES_TABLE()
102 wxBEGIN_HANDLERS_TABLE(wxDialog
)
103 wxEND_HANDLERS_TABLE()
105 wxCONSTRUCTOR_6( wxDialog
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Title
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
108 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
111 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
112 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
113 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
114 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
116 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
118 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
120 EVT_CLOSE(wxDialog::OnCloseWindow
)
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 // this is simply a container for any data we need to implement modality which
128 // allows us to avoid changing wxDialog each time the implementation changes
129 class wxDialogModalData
132 wxDialogModalData(wxDialog
*dialog
) : m_evtLoop(dialog
) { }
145 wxModalEventLoop m_evtLoop
;
148 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData
);
150 // ============================================================================
152 // ============================================================================
154 // ----------------------------------------------------------------------------
155 // wxDialog construction
156 // ----------------------------------------------------------------------------
158 void wxDialog::Init()
160 m_oldFocus
= (wxWindow
*)NULL
;
163 m_endModalCalled
= FALSE
;
166 bool wxDialog::Create(wxWindow
*parent
,
168 const wxString
& title
,
172 const wxString
& name
)
174 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
176 // save focus before doing anything which can potentially change it
177 m_oldFocus
= FindFocus();
179 // All dialogs should really have this style
180 style
|= wxTAB_TRAVERSAL
;
182 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
186 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
188 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
193 void wxDialog::SetModal(bool flag
)
197 m_windowStyle
|= wxDIALOG_MODAL
;
199 wxModelessWindows
.DeleteObject(this);
203 m_windowStyle
&= ~wxDIALOG_MODAL
;
205 wxModelessWindows
.Append(this);
209 wxDialog::~wxDialog()
211 m_isBeingDeleted
= TRUE
;
213 // this will also reenable all the other windows for a modal dialog
217 // ----------------------------------------------------------------------------
219 // ----------------------------------------------------------------------------
221 // By default, pressing escape cancels the dialog
222 void wxDialog::OnCharHook(wxKeyEvent
& event
)
226 // "Esc" works as an accelerator for the "Cancel" button, but it
227 // shouldn't close the dialog which doesn't have any cancel button
228 if ( (event
.m_keyCode
== WXK_ESCAPE
) && FindWindow(wxID_CANCEL
) )
230 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
231 cancelEvent
.SetEventObject( this );
232 GetEventHandler()->ProcessEvent(cancelEvent
);
234 // ensure that there is another message for this window so the
235 // ShowModal loop will exit and won't get stuck in GetMessage().
236 ::PostMessage(GetHwnd(), WM_NULL
, 0, 0);
242 // We didn't process this event.
246 // ----------------------------------------------------------------------------
247 // showing the dialogs
248 // ----------------------------------------------------------------------------
250 bool wxDialog::IsModal() const
252 return (GetWindowStyleFlag() & wxDIALOG_MODAL
) != 0;
255 bool wxDialog::IsModalShowing() const
257 return m_modalData
!= NULL
;
260 wxWindow
*wxDialog::FindSuitableParent() const
262 // first try to use the currently active window
263 HWND hwndFg
= ::GetForegroundWindow();
264 wxWindow
*parent
= hwndFg
? wxFindWinFromHandle((WXHWND
)hwndFg
)
268 // next try the main app window
269 parent
= wxTheApp
->GetTopWindow();
272 // finally, check if the parent we found is really suitable
273 if ( !parent
|| parent
== (wxWindow
*)this || !parent
->IsShown() )
275 // don't use this one
282 void wxDialog::DoShowModal()
284 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
285 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
287 wxWindow
*parent
= GetParent();
289 wxWindow
* oldFocus
= m_oldFocus
;
291 // We have to remember the HWND because we need to check
292 // the HWND still exists (oldFocus can be garbage when the dialog
293 // exits, if it has been destroyed)
294 HWND hwndOldFocus
= 0;
296 hwndOldFocus
= (HWND
) oldFocus
->GetHWND();
298 // remember where the focus was
303 hwndOldFocus
= GetHwndOf(parent
);
306 // enter the modal loop
308 wxDialogModalDataTiedPtr
modalData(&m_modalData
,
309 new wxDialogModalData(this));
310 modalData
->RunLoop();
314 // Note that this code MUST NOT access the dialog object's data
315 // in case the object has been deleted (which will be the case
316 // for a modal dialog that has been destroyed before calling EndModal).
317 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
319 // This is likely to prove that the object still exists
320 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
321 oldFocus
->SetFocus();
325 bool wxDialog::Show(bool show
)
327 if ( !show
&& m_modalData
)
329 // we need to do this before calling wxDialogBase version because if we
330 // had disabled other app windows, they must be reenabled right now as
331 // if they stay disabled Windows will activate another window (one
332 // which is enabled, anyhow) when we're hidden in the base class Show()
333 // and we will lose activation
334 m_modalData
->ExitLoop();
337 // ShowModal() may be called for already shown dialog
338 if ( !wxDialogBase::Show(show
) && !(show
&& IsModal()) )
346 // dialogs don't get WM_SIZE message after creation unlike most (all?)
347 // other windows and so could start their life non laid out correctly
348 // if we didn't call Layout() from here
350 // NB: normally we should call it just the first time but doing it
351 // every time is simpler than keeping a flag
354 // this usually will result in TransferDataToWindow() being called
358 // EndModal may have been called from InitDialog handler,
359 // which would cause an infinite loop if we didn't take it
361 if ( show
&& IsModal() && !m_endModalCalled
)
363 // modal dialog needs a parent window, so try to find one
366 m_parent
= FindSuitableParent();
375 void wxDialog::Raise()
377 ::SetForegroundWindow(GetHwnd());
380 // a special version for Show(TRUE) for modal dialogs which returns return code
381 int wxDialog::ShowModal()
383 m_endModalCalled
= FALSE
;
391 return GetReturnCode();
394 // NB: this function (surprizingly) may be called for both modal and modeless
395 // dialogs and should work for both of them
396 void wxDialog::EndModal(int retCode
)
398 m_endModalCalled
= TRUE
;
399 SetReturnCode(retCode
);
404 // ----------------------------------------------------------------------------
405 // wxWin event handlers
406 // ----------------------------------------------------------------------------
409 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
411 if ( Validate() && TransferDataFromWindow() )
417 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
420 TransferDataFromWindow();
422 // TODO probably need to disable the Apply button until things change again
425 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
427 EndModal(wxID_CANCEL
);
430 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
432 // We'll send a Cancel message by default, which may close the dialog.
433 // Check for looping if the Cancel event handler calls Close().
435 // Note that if a cancel button and handler aren't present in the dialog,
436 // nothing will happen when you close the dialog via the window manager, or
437 // via Close(). We wouldn't want to destroy the dialog by default, since
438 // the dialog may have been created on the stack. However, this does mean
439 // that calling dialog->Close() won't delete the dialog unless the handler
440 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
441 // destroy the dialog. The default OnCancel (above) simply ends a modal
442 // dialog, and hides a modeless dialog.
444 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
445 // lists here? don't dare to change it now, but should be done later!
446 static wxList closing
;
448 if ( closing
.Member(this) )
451 closing
.Append(this);
453 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
454 cancelEvent
.SetEventObject( this );
455 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
457 closing
.DeleteObject(this);
460 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
465 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
470 // ---------------------------------------------------------------------------
471 // dialog window proc
472 // ---------------------------------------------------------------------------
474 long wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
477 bool processed
= FALSE
;
482 // if we can't close, tell the system that we processed the
483 // message - otherwise it would close us
484 processed
= !Close();
488 // the Windows dialogs unfortunately are not meant to be resizeable
489 // at all and their standard class doesn't include CS_[VH]REDRAW
490 // styles which means that the window is not refreshed properly
491 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
492 // help with it - so we have to refresh it manually which certainly
493 // creates flicker but at least doesn't show garbage on the screen
494 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
496 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
498 ::InvalidateRect(GetHwnd(), NULL
, FALSE
/* erase bg */);
502 #ifndef __WXMICROWIN__
504 // we want to override the busy cursor for modal dialogs:
505 // typically, wxBeginBusyCursor() is called and then a modal dialog
506 // is shown, but the modal dialog shouldn't have hourglass cursor
507 if ( IsModalShowing() && wxIsBusy() )
509 // set our cursor for all windows (but see below)
510 wxCursor cursor
= m_cursor
;
512 cursor
= wxCURSOR_ARROW
;
514 ::SetCursor(GetHcursorOf(cursor
));
516 // in any case, stop here and don't let wxWindow process this
517 // message (it would set the busy cursor)
520 // but return FALSE to tell the child window (if the event
521 // comes from one of them and not from ourselves) that it can
522 // set its own cursor if it has one: thus, standard controls
523 // (e.g. text ctrl) still have correct cursors in a dialog
524 // invoked while wxIsBusy()
528 #endif // __WXMICROWIN__
532 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
539 // Define for each class of dialog and control
540 WXHBRUSH
wxDialog::OnCtlColor(WXHDC
WXUNUSED(pDC
),
541 WXHWND
WXUNUSED(pWnd
),
542 WXUINT
WXUNUSED(nCtlColor
),
547 return (WXHBRUSH
)Ctl3dCtlColorEx(message
, wParam
, lParam
);
550 #endif // wxUSE_CTL3D