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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/dialog.h"
32 #include "wx/button.h"
33 #include "wx/settings.h"
38 #include "wx/msw/private.h"
39 #include "wx/evtloop.h"
40 #include "wx/ptr_scpd.h"
42 #include "wx/msw/wrapcdlg.h"
44 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
45 #include "wx/msw/wince/resources.h"
46 #endif // __SMARTPHONE__ && __WXWINCE__
48 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
49 #include "wx/toolbar.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 #if wxUSE_EXTENDED_RTTI
57 WX_DEFINE_FLAGS( wxDialogStyle
)
59 wxBEGIN_FLAGS( wxDialogStyle
)
60 // new style border flags, we put them first to
61 // use them for streaming out
62 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
63 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
64 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
65 wxFLAGS_MEMBER(wxBORDER_RAISED
)
66 wxFLAGS_MEMBER(wxBORDER_STATIC
)
67 wxFLAGS_MEMBER(wxBORDER_NONE
)
69 // old style border flags
70 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
71 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
72 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
73 wxFLAGS_MEMBER(wxRAISED_BORDER
)
74 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
75 wxFLAGS_MEMBER(wxNO_BORDER
)
77 // standard window styles
78 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
79 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
82 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY
)
83 wxFLAGS_MEMBER(wxSTAY_ON_TOP
)
84 wxFLAGS_MEMBER(wxCAPTION
)
85 #if WXWIN_COMPATIBILITY_2_6
86 wxFLAGS_MEMBER(wxTHICK_FRAME
)
87 #endif // WXWIN_COMPATIBILITY_2_6
88 wxFLAGS_MEMBER(wxSYSTEM_MENU
)
89 wxFLAGS_MEMBER(wxRESIZE_BORDER
)
90 #if WXWIN_COMPATIBILITY_2_6
91 wxFLAGS_MEMBER(wxRESIZE_BOX
)
92 #endif // WXWIN_COMPATIBILITY_2_6
93 wxFLAGS_MEMBER(wxCLOSE_BOX
)
94 wxFLAGS_MEMBER(wxMAXIMIZE_BOX
)
95 wxFLAGS_MEMBER(wxMINIMIZE_BOX
)
96 wxEND_FLAGS( wxDialogStyle
)
98 IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog
, wxTopLevelWindow
,"wx/dialog.h")
100 wxBEGIN_PROPERTIES_TABLE(wxDialog
)
101 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
102 wxPROPERTY_FLAGS( WindowStyle
, wxDialogStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
103 wxEND_PROPERTIES_TABLE()
105 wxBEGIN_HANDLERS_TABLE(wxDialog
)
106 wxEND_HANDLERS_TABLE()
108 wxCONSTRUCTOR_6( wxDialog
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Title
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
111 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
114 BEGIN_EVENT_TABLE(wxDialog
, wxDialogBase
)
115 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
116 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
117 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
119 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
121 EVT_CLOSE(wxDialog::OnCloseWindow
)
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 // this is simply a container for any data we need to implement modality which
129 // allows us to avoid changing wxDialog each time the implementation changes
130 class wxDialogModalData
133 wxDialogModalData(wxDialog
*dialog
) : m_evtLoop(dialog
) { }
146 wxModalEventLoop m_evtLoop
;
149 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData
)
151 // ============================================================================
153 // ============================================================================
155 // ----------------------------------------------------------------------------
156 // wxDialog construction
157 // ----------------------------------------------------------------------------
159 void wxDialog::Init()
161 m_oldFocus
= (wxWindow
*)NULL
;
164 m_endModalCalled
= false;
165 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
166 m_dialogToolBar
= NULL
;
170 bool wxDialog::Create(wxWindow
*parent
,
172 const wxString
& title
,
176 const wxString
& name
)
178 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
180 // save focus before doing anything which can potentially change it
181 m_oldFocus
= FindFocus();
183 // All dialogs should really have this style
184 style
|= wxTAB_TRAVERSAL
;
186 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
190 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
192 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
193 SetLeftMenu(wxID_OK
, _("OK"));
195 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
202 #if WXWIN_COMPATIBILITY_2_6
205 wxDialog::wxDialog(wxWindow
*parent
,
206 const wxString
& title
,
207 bool WXUNUSED(modal
),
213 const wxString
& name
)
217 Create(parent
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
), style
, name
);
220 void wxDialog::SetModal(bool WXUNUSED(flag
))
222 // nothing to do, obsolete method
225 #endif // WXWIN_COMPATIBILITY_2_6
227 wxDialog::~wxDialog()
229 m_isBeingDeleted
= true;
231 // this will also reenable all the other windows for a modal dialog
235 // ----------------------------------------------------------------------------
236 // showing the dialogs
237 // ----------------------------------------------------------------------------
239 #if WXWIN_COMPATIBILITY_2_6
241 bool wxDialog::IsModalShowing() const
246 #endif // WXWIN_COMPATIBILITY_2_6
248 wxWindow
*wxDialog::FindSuitableParent() const
250 // first try to use the currently active window
251 HWND hwndFg
= ::GetForegroundWindow();
252 wxWindow
*parent
= hwndFg
? wxFindWinFromHandle((WXHWND
)hwndFg
)
256 // next try the main app window
257 parent
= wxTheApp
->GetTopWindow();
260 // finally, check if the parent we found is really suitable
261 if ( !parent
|| parent
== (wxWindow
*)this || !parent
->IsShown() )
263 // don't use this one
270 bool wxDialog::Show(bool show
)
272 if ( show
== IsShown() )
275 if ( !show
&& m_modalData
)
277 // we need to do this before calling wxDialogBase version because if we
278 // had disabled other app windows, they must be reenabled right now as
279 // if they stay disabled Windows will activate another window (one
280 // which is enabled, anyhow) when we're hidden in the base class Show()
281 // and we will lose activation
282 m_modalData
->ExitLoop();
287 // this usually will result in TransferDataToWindow() being called
288 // which will change the controls values so do it before showing as
289 // otherwise we could have some flicker
293 wxDialogBase::Show(show
);
297 // dialogs don't get WM_SIZE message after creation unlike most (all?)
298 // other windows and so could start their life non laid out correctly
299 // if we didn't call Layout() from here
301 // NB: normally we should call it just the first time but doing it
302 // every time is simpler than keeping a flag
309 void wxDialog::Raise()
311 ::SetForegroundWindow(GetHwnd());
314 // show dialog modally
315 int wxDialog::ShowModal()
317 wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") );
319 m_endModalCalled
= false;
323 // EndModal may have been called from InitDialog handler (called from
324 // inside Show()), which would cause an infinite loop if we didn't take it
326 if ( !m_endModalCalled
)
328 // modal dialog needs a parent window, so try to find one
329 wxWindow
*parent
= GetParent();
332 parent
= FindSuitableParent();
335 // remember where the focus was
336 wxWindow
*oldFocus
= m_oldFocus
;
339 // VZ: do we really want to do this?
343 // We have to remember the HWND because we need to check
344 // the HWND still exists (oldFocus can be garbage when the dialog
345 // exits, if it has been destroyed)
346 HWND hwndOldFocus
= oldFocus
? GetHwndOf(oldFocus
) : NULL
;
349 // enter and run the modal loop
351 wxDialogModalDataTiedPtr
modalData(&m_modalData
,
352 new wxDialogModalData(this));
353 modalData
->RunLoop();
358 // Note that this code MUST NOT access the dialog object's data
359 // in case the object has been deleted (which will be the case
360 // for a modal dialog that has been destroyed before calling EndModal).
361 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
363 // This is likely to prove that the object still exists
364 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
365 oldFocus
->SetFocus();
369 return GetReturnCode();
372 void wxDialog::EndModal(int retCode
)
374 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
376 m_endModalCalled
= true;
377 SetReturnCode(retCode
);
382 void wxDialog::EndDialog(int rc
)
390 // ----------------------------------------------------------------------------
391 // wxWin event handlers
392 // ----------------------------------------------------------------------------
394 bool wxDialog::EmulateButtonClickIfPresent(int id
)
396 wxButton
*btn
= wxDynamicCast(FindWindow(id
), wxButton
);
398 if ( !btn
|| !btn
->IsEnabled() || !btn
->IsShown() )
401 btn
->MSWCommand(BN_CLICKED
, 0 /* unused */);
406 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
408 if ( Validate() && TransferDataFromWindow() )
414 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
417 TransferDataFromWindow();
419 // TODO probably need to disable the Apply button until things change again
422 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
424 EndDialog(wxID_CANCEL
);
427 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
429 // We'll send a Cancel message by default, which may close the dialog.
430 // Check for looping if the Cancel event handler calls Close().
432 // Note that if a cancel button and handler aren't present in the dialog,
433 // nothing will happen when you close the dialog via the window manager, or
434 // via Close(). We wouldn't want to destroy the dialog by default, since
435 // the dialog may have been created on the stack. However, this does mean
436 // that calling dialog->Close() won't delete the dialog unless the handler
437 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
438 // destroy the dialog. The default OnCancel (above) simply ends a modal
439 // dialog, and hides a modeless dialog.
441 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
442 // lists here? don't dare to change it now, but should be done later!
443 static wxList closing
;
445 if ( closing
.Member(this) )
448 closing
.Append(this);
450 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
451 cancelEvent
.SetEventObject( this );
452 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
454 closing
.DeleteObject(this);
457 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
459 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
464 // Responds to the OK button in a PocketPC titlebar. This
465 // can be overridden, or you can change the id used for
466 // sending the event, by calling SetAffirmativeId.
467 bool wxDialog::DoOK()
469 const int idOk
= GetAffirmativeId();
470 if ( EmulateButtonClickIfPresent(idOk
) )
473 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetAffirmativeId());
474 event
.SetEventObject(this);
476 return GetEventHandler()->ProcessEvent(event
);
478 #endif // __POCKETPC__
480 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
481 // create main toolbar by calling OnCreateToolBar()
482 wxToolBar
* wxDialog::CreateToolBar(long style
, wxWindowID winid
, const wxString
& name
)
484 m_dialogToolBar
= OnCreateToolBar(style
, winid
, name
);
486 return m_dialogToolBar
;
489 // return a new toolbar
490 wxToolBar
*wxDialog::OnCreateToolBar(long style
,
492 const wxString
& name
)
494 return new wxToolMenuBar(this, winid
,
495 wxDefaultPosition
, wxDefaultSize
,
500 // ---------------------------------------------------------------------------
501 // dialog Windows messages processing
502 // ---------------------------------------------------------------------------
504 bool wxDialog::MSWProcessMessage(WXMSG
* pMsg
)
506 const MSG
* const msg
= wx_reinterpret_cast(MSG
*, pMsg
);
507 if ( msg
->message
== WM_KEYDOWN
&& msg
->wParam
== VK_ESCAPE
)
509 int idCancel
= GetEscapeId();
513 // don't handle Esc specially at all
517 // this value is special: it means translate Esc to wxID_CANCEL
518 // but if there is no such button, then fall back to wxID_OK
519 if ( EmulateButtonClickIfPresent(wxID_CANCEL
) )
525 // translate Esc to button press for the button with given id
526 if ( EmulateButtonClickIfPresent(idCancel
) )
531 return wxDialogBase::MSWProcessMessage(pMsg
);
534 WXLRESULT
wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
537 bool processed
= false;
542 // react to pressing the OK button in the title
545 switch ( LOWORD(wParam
) )
551 processed
= !Close();
553 #ifdef __SMARTPHONE__
556 processed
= HandleCommand( LOWORD(wParam
) , 0 , NULL
);
558 #endif // __SMARTPHONE__
564 // if we can't close, tell the system that we processed the
565 // message - otherwise it would close us
566 processed
= !Close();
570 // the Windows dialogs unfortunately are not meant to be resizeable
571 // at all and their standard class doesn't include CS_[VH]REDRAW
572 // styles which means that the window is not refreshed properly
573 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
574 // help with it - so we have to refresh it manually which certainly
575 // creates flicker but at least doesn't show garbage on the screen
576 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
578 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
580 ::InvalidateRect(GetHwnd(), NULL
, false /* erase bg */);
584 #ifndef __WXMICROWIN__
586 // we want to override the busy cursor for modal dialogs:
587 // typically, wxBeginBusyCursor() is called and then a modal dialog
588 // is shown, but the modal dialog shouldn't have hourglass cursor
589 if ( IsModal() && wxIsBusy() )
591 // set our cursor for all windows (but see below)
592 wxCursor cursor
= m_cursor
;
594 cursor
= wxCURSOR_ARROW
;
596 ::SetCursor(GetHcursorOf(cursor
));
598 // in any case, stop here and don't let wxWindow process this
599 // message (it would set the busy cursor)
602 // but return false to tell the child window (if the event
603 // comes from one of them and not from ourselves) that it can
604 // set its own cursor if it has one: thus, standard controls
605 // (e.g. text ctrl) still have correct cursors in a dialog
606 // invoked while wxIsBusy()
610 #endif // __WXMICROWIN__
614 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);