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"
27 #include "wx/dialog.h"
33 #include "wx/button.h"
34 #include "wx/settings.h"
37 #include "wx/toolbar.h"
40 #include "wx/msw/private.h"
41 #include "wx/evtloop.h"
42 #include "wx/ptr_scpd.h"
44 #include "wx/msw/wrapcdlg.h"
46 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
47 #include "wx/msw/wince/resources.h"
48 #endif // __SMARTPHONE__ && __WXWINCE__
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(wxWS_EX_VALIDATE_RECURSIVELY
)
81 wxFLAGS_MEMBER(wxSTAY_ON_TOP
)
82 wxFLAGS_MEMBER(wxCAPTION
)
83 #if WXWIN_COMPATIBILITY_2_6
84 wxFLAGS_MEMBER(wxTHICK_FRAME
)
85 #endif // WXWIN_COMPATIBILITY_2_6
86 wxFLAGS_MEMBER(wxSYSTEM_MENU
)
87 wxFLAGS_MEMBER(wxRESIZE_BORDER
)
88 #if WXWIN_COMPATIBILITY_2_6
89 wxFLAGS_MEMBER(wxRESIZE_BOX
)
90 #endif // WXWIN_COMPATIBILITY_2_6
91 wxFLAGS_MEMBER(wxCLOSE_BOX
)
92 wxFLAGS_MEMBER(wxMAXIMIZE_BOX
)
93 wxFLAGS_MEMBER(wxMINIMIZE_BOX
)
94 wxEND_FLAGS( wxDialogStyle
)
96 IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog
, wxTopLevelWindow
,"wx/dialog.h")
98 wxBEGIN_PROPERTIES_TABLE(wxDialog
)
99 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
100 wxPROPERTY_FLAGS( WindowStyle
, wxDialogStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
101 wxEND_PROPERTIES_TABLE()
103 wxBEGIN_HANDLERS_TABLE(wxDialog
)
104 wxEND_HANDLERS_TABLE()
106 wxCONSTRUCTOR_6( wxDialog
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, Title
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
109 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 // this is simply a container for any data we need to implement modality which
117 // allows us to avoid changing wxDialog each time the implementation changes
118 class wxDialogModalData
121 wxDialogModalData(wxDialog
*dialog
) : m_evtLoop(dialog
) { }
134 wxModalEventLoop m_evtLoop
;
137 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData
)
139 // ============================================================================
141 // ============================================================================
143 // ----------------------------------------------------------------------------
144 // wxDialog construction
145 // ----------------------------------------------------------------------------
147 void wxDialog::Init()
149 m_oldFocus
= (wxWindow
*)NULL
;
152 m_endModalCalled
= false;
153 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
154 m_dialogToolBar
= NULL
;
158 bool wxDialog::Create(wxWindow
*parent
,
160 const wxString
& title
,
164 const wxString
& name
)
166 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
168 // save focus before doing anything which can potentially change it
169 m_oldFocus
= FindFocus();
171 // All dialogs should really have this style
172 style
|= wxTAB_TRAVERSAL
;
174 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
178 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
180 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
181 SetLeftMenu(wxID_OK
, _("OK"));
183 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
190 #if WXWIN_COMPATIBILITY_2_6
193 wxDialog::wxDialog(wxWindow
*parent
,
194 const wxString
& title
,
195 bool WXUNUSED(modal
),
201 const wxString
& name
)
205 Create(parent
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
), style
, name
);
208 void wxDialog::SetModal(bool WXUNUSED(flag
))
210 // nothing to do, obsolete method
213 #endif // WXWIN_COMPATIBILITY_2_6
215 wxDialog::~wxDialog()
217 m_isBeingDeleted
= true;
219 // this will also reenable all the other windows for a modal dialog
223 // ----------------------------------------------------------------------------
224 // showing the dialogs
225 // ----------------------------------------------------------------------------
227 #if WXWIN_COMPATIBILITY_2_6
229 bool wxDialog::IsModalShowing() const
234 #endif // WXWIN_COMPATIBILITY_2_6
236 wxWindow
*wxDialog::FindSuitableParent() const
238 // first try to use the currently active window
239 HWND hwndFg
= ::GetForegroundWindow();
240 wxWindow
*parent
= hwndFg
? wxFindWinFromHandle((WXHWND
)hwndFg
)
244 // next try the main app window
245 parent
= wxTheApp
->GetTopWindow();
248 // finally, check if the parent we found is really suitable
249 if ( !parent
|| parent
== (wxWindow
*)this || !parent
->IsShown() )
251 // don't use this one
258 bool wxDialog::Show(bool show
)
260 if ( show
== IsShown() )
263 if ( !show
&& m_modalData
)
265 // we need to do this before calling wxDialogBase version because if we
266 // had disabled other app windows, they must be reenabled right now as
267 // if they stay disabled Windows will activate another window (one
268 // which is enabled, anyhow) when we're hidden in the base class Show()
269 // and we will lose activation
270 m_modalData
->ExitLoop();
275 // this usually will result in TransferDataToWindow() being called
276 // which will change the controls values so do it before showing as
277 // otherwise we could have some flicker
281 wxDialogBase::Show(show
);
285 // dialogs don't get WM_SIZE message after creation unlike most (all?)
286 // other windows and so could start their life non laid out correctly
287 // if we didn't call Layout() from here
289 // NB: normally we should call it just the first time but doing it
290 // every time is simpler than keeping a flag
297 void wxDialog::Raise()
299 ::SetForegroundWindow(GetHwnd());
302 // show dialog modally
303 int wxDialog::ShowModal()
305 wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") );
307 m_endModalCalled
= false;
311 // EndModal may have been called from InitDialog handler (called from
312 // inside Show()), which would cause an infinite loop if we didn't take it
314 if ( !m_endModalCalled
)
316 // modal dialog needs a parent window, so try to find one
317 wxWindow
*parent
= GetParent();
320 parent
= FindSuitableParent();
323 // remember where the focus was
324 wxWindow
*oldFocus
= m_oldFocus
;
327 // VZ: do we really want to do this?
331 // We have to remember the HWND because we need to check
332 // the HWND still exists (oldFocus can be garbage when the dialog
333 // exits, if it has been destroyed)
334 HWND hwndOldFocus
= oldFocus
? GetHwndOf(oldFocus
) : NULL
;
337 // enter and run the modal loop
339 wxDialogModalDataTiedPtr
modalData(&m_modalData
,
340 new wxDialogModalData(this));
341 modalData
->RunLoop();
346 // Note that this code MUST NOT access the dialog object's data
347 // in case the object has been deleted (which will be the case
348 // for a modal dialog that has been destroyed before calling EndModal).
349 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
351 // This is likely to prove that the object still exists
352 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
353 oldFocus
->SetFocus();
357 return GetReturnCode();
360 void wxDialog::EndModal(int retCode
)
362 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
364 m_endModalCalled
= true;
365 SetReturnCode(retCode
);
370 // ----------------------------------------------------------------------------
371 // wxWin event handlers
372 // ----------------------------------------------------------------------------
375 // Responds to the OK button in a PocketPC titlebar. This
376 // can be overridden, or you can change the id used for
377 // sending the event, by calling SetAffirmativeId.
378 bool wxDialog::DoOK()
380 const int idOk
= GetAffirmativeId();
381 if ( EmulateButtonClickIfPresent(idOk
) )
384 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetAffirmativeId());
385 event
.SetEventObject(this);
387 return GetEventHandler()->ProcessEvent(event
);
389 #endif // __POCKETPC__
391 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
392 // create main toolbar by calling OnCreateToolBar()
393 wxToolBar
* wxDialog::CreateToolBar(long style
, wxWindowID winid
, const wxString
& name
)
395 m_dialogToolBar
= OnCreateToolBar(style
, winid
, name
);
397 return m_dialogToolBar
;
400 // return a new toolbar
401 wxToolBar
*wxDialog::OnCreateToolBar(long style
,
403 const wxString
& name
)
405 return new wxToolMenuBar(this, winid
,
406 wxDefaultPosition
, wxDefaultSize
,
411 // ---------------------------------------------------------------------------
412 // dialog Windows messages processing
413 // ---------------------------------------------------------------------------
415 WXLRESULT
wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
418 bool processed
= false;
423 // react to pressing the OK button in the title
426 switch ( LOWORD(wParam
) )
432 processed
= !Close();
434 #ifdef __SMARTPHONE__
437 processed
= HandleCommand( LOWORD(wParam
) , 0 , NULL
);
439 #endif // __SMARTPHONE__
445 // if we can't close, tell the system that we processed the
446 // message - otherwise it would close us
447 processed
= !Close();
451 // the Windows dialogs unfortunately are not meant to be resizeable
452 // at all and their standard class doesn't include CS_[VH]REDRAW
453 // styles which means that the window is not refreshed properly
454 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
455 // help with it - so we have to refresh it manually which certainly
456 // creates flicker but at least doesn't show garbage on the screen
457 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
459 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
461 ::InvalidateRect(GetHwnd(), NULL
, false /* erase bg */);
465 #ifndef __WXMICROWIN__
467 // we want to override the busy cursor for modal dialogs:
468 // typically, wxBeginBusyCursor() is called and then a modal dialog
469 // is shown, but the modal dialog shouldn't have hourglass cursor
470 if ( IsModal() && wxIsBusy() )
472 // set our cursor for all windows (but see below)
473 wxCursor cursor
= m_cursor
;
475 cursor
= wxCURSOR_ARROW
;
477 ::SetCursor(GetHcursorOf(cursor
));
479 // in any case, stop here and don't let wxWindow process this
480 // message (it would set the busy cursor)
483 // but return false to tell the child window (if the event
484 // comes from one of them and not from ourselves) that it can
485 // set its own cursor if it has one: thus, standard controls
486 // (e.g. text ctrl) still have correct cursors in a dialog
487 // invoked while wxIsBusy()
491 #endif // __WXMICROWIN__
495 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);