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"
30 #include "wx/msw/wrapcdlg.h"
34 #include "wx/button.h"
35 #include "wx/settings.h"
38 #include "wx/toolbar.h"
41 #include "wx/msw/private.h"
42 #include "wx/evtloop.h"
43 #include "wx/ptr_scpd.h"
45 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
46 #include "wx/msw/wince/resources.h"
47 #endif // __SMARTPHONE__ && __WXWINCE__
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #if wxUSE_EXTENDED_RTTI
54 WX_DEFINE_FLAGS( wxDialogStyle
)
56 wxBEGIN_FLAGS( wxDialogStyle
)
57 // new style border flags, we put them first to
58 // use them for streaming out
59 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
60 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
61 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
62 wxFLAGS_MEMBER(wxBORDER_RAISED
)
63 wxFLAGS_MEMBER(wxBORDER_STATIC
)
64 wxFLAGS_MEMBER(wxBORDER_NONE
)
66 // old style border flags
67 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
68 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
69 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
70 wxFLAGS_MEMBER(wxRAISED_BORDER
)
71 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
72 wxFLAGS_MEMBER(wxNO_BORDER
)
74 // standard window styles
75 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
76 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
79 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY
)
80 wxFLAGS_MEMBER(wxSTAY_ON_TOP
)
81 wxFLAGS_MEMBER(wxCAPTION
)
82 #if WXWIN_COMPATIBILITY_2_6
83 wxFLAGS_MEMBER(wxTHICK_FRAME
)
84 #endif // WXWIN_COMPATIBILITY_2_6
85 wxFLAGS_MEMBER(wxSYSTEM_MENU
)
86 wxFLAGS_MEMBER(wxRESIZE_BORDER
)
87 #if WXWIN_COMPATIBILITY_2_6
88 wxFLAGS_MEMBER(wxRESIZE_BOX
)
89 #endif // WXWIN_COMPATIBILITY_2_6
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
, EMPTY_MACROVALUE
, 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 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 // this is simply a container for any data we need to implement modality which
116 // allows us to avoid changing wxDialog each time the implementation changes
117 class wxDialogModalData
120 wxDialogModalData(wxDialog
*dialog
) : m_evtLoop(dialog
) { }
133 wxModalEventLoop m_evtLoop
;
136 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData
)
138 // ============================================================================
140 // ============================================================================
142 // ----------------------------------------------------------------------------
143 // wxDialog construction
144 // ----------------------------------------------------------------------------
146 void wxDialog::Init()
148 m_oldFocus
= (wxWindow
*)NULL
;
151 m_endModalCalled
= false;
152 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
153 m_dialogToolBar
= NULL
;
157 bool wxDialog::Create(wxWindow
*parent
,
159 const wxString
& title
,
163 const wxString
& name
)
165 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
167 // save focus before doing anything which can potentially change it
168 m_oldFocus
= FindFocus();
170 // All dialogs should really have this style
171 style
|= wxTAB_TRAVERSAL
;
173 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
177 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
179 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
180 SetLeftMenu(wxID_OK
, _("OK"));
182 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
189 #if WXWIN_COMPATIBILITY_2_6
192 wxDialog::wxDialog(wxWindow
*parent
,
193 const wxString
& title
,
194 bool WXUNUSED(modal
),
200 const wxString
& name
)
204 Create(parent
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
), style
, name
);
207 void wxDialog::SetModal(bool WXUNUSED(flag
))
209 // nothing to do, obsolete method
212 #endif // WXWIN_COMPATIBILITY_2_6
214 wxDialog::~wxDialog()
216 m_isBeingDeleted
= true;
218 // this will also reenable all the other windows for a modal dialog
222 // ----------------------------------------------------------------------------
223 // showing the dialogs
224 // ----------------------------------------------------------------------------
226 #if WXWIN_COMPATIBILITY_2_6
228 bool wxDialog::IsModalShowing() const
233 #endif // WXWIN_COMPATIBILITY_2_6
235 wxWindow
*wxDialog::FindSuitableParent() const
237 // first try to use the currently active window
238 HWND hwndFg
= ::GetForegroundWindow();
239 wxWindow
*parent
= hwndFg
? wxFindWinFromHandle((WXHWND
)hwndFg
)
243 // next try the main app window
244 parent
= wxTheApp
->GetTopWindow();
247 // finally, check if the parent we found is really suitable
248 if ( !parent
|| parent
== (wxWindow
*)this || !parent
->IsShown() )
250 // don't use this one
257 bool wxDialog::Show(bool show
)
259 if ( show
== IsShown() )
262 if ( !show
&& m_modalData
)
264 // we need to do this before calling wxDialogBase version because if we
265 // had disabled other app windows, they must be reenabled right now as
266 // if they stay disabled Windows will activate another window (one
267 // which is enabled, anyhow) when we're hidden in the base class Show()
268 // and we will lose activation
269 m_modalData
->ExitLoop();
274 // this usually will result in TransferDataToWindow() being called
275 // which will change the controls values so do it before showing as
276 // otherwise we could have some flicker
280 wxDialogBase::Show(show
);
284 // dialogs don't get WM_SIZE message after creation unlike most (all?)
285 // other windows and so could start their life non laid out correctly
286 // if we didn't call Layout() from here
288 // NB: normally we should call it just the first time but doing it
289 // every time is simpler than keeping a flag
296 void wxDialog::Raise()
298 ::SetForegroundWindow(GetHwnd());
301 // show dialog modally
302 int wxDialog::ShowModal()
304 wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") );
306 m_endModalCalled
= false;
310 // EndModal may have been called from InitDialog handler (called from
311 // inside Show()), which would cause an infinite loop if we didn't take it
313 if ( !m_endModalCalled
)
315 // modal dialog needs a parent window, so try to find one
316 wxWindow
*parent
= GetParent();
319 parent
= FindSuitableParent();
322 // remember where the focus was
323 wxWindow
*oldFocus
= m_oldFocus
;
326 // VZ: do we really want to do this?
330 // We have to remember the HWND because we need to check
331 // the HWND still exists (oldFocus can be garbage when the dialog
332 // exits, if it has been destroyed)
333 HWND hwndOldFocus
= oldFocus
? GetHwndOf(oldFocus
) : NULL
;
336 // enter and run the modal loop
338 wxDialogModalDataTiedPtr
modalData(&m_modalData
,
339 new wxDialogModalData(this));
340 modalData
->RunLoop();
345 // Note that this code MUST NOT access the dialog object's data
346 // in case the object has been deleted (which will be the case
347 // for a modal dialog that has been destroyed before calling EndModal).
348 if ( oldFocus
&& (oldFocus
!= this) && ::IsWindow(hwndOldFocus
))
350 // This is likely to prove that the object still exists
351 if (wxFindWinFromHandle((WXHWND
) hwndOldFocus
) == oldFocus
)
352 oldFocus
->SetFocus();
356 return GetReturnCode();
359 void wxDialog::EndModal(int retCode
)
361 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
363 m_endModalCalled
= true;
364 SetReturnCode(retCode
);
369 // ----------------------------------------------------------------------------
370 // wxWin event handlers
371 // ----------------------------------------------------------------------------
374 // Responds to the OK button in a PocketPC titlebar. This
375 // can be overridden, or you can change the id used for
376 // sending the event, by calling SetAffirmativeId.
377 bool wxDialog::DoOK()
379 const int idOk
= GetAffirmativeId();
380 if ( EmulateButtonClickIfPresent(idOk
) )
383 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetAffirmativeId());
384 event
.SetEventObject(this);
386 return GetEventHandler()->ProcessEvent(event
);
388 #endif // __POCKETPC__
390 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
391 // create main toolbar by calling OnCreateToolBar()
392 wxToolBar
* wxDialog::CreateToolBar(long style
, wxWindowID winid
, const wxString
& name
)
394 m_dialogToolBar
= OnCreateToolBar(style
, winid
, name
);
396 return m_dialogToolBar
;
399 // return a new toolbar
400 wxToolBar
*wxDialog::OnCreateToolBar(long style
,
402 const wxString
& name
)
404 return new wxToolMenuBar(this, winid
,
405 wxDefaultPosition
, wxDefaultSize
,
410 // ---------------------------------------------------------------------------
411 // dialog Windows messages processing
412 // ---------------------------------------------------------------------------
414 WXLRESULT
wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
417 bool processed
= false;
422 // react to pressing the OK button in the title
425 switch ( LOWORD(wParam
) )
431 processed
= !Close();
433 #ifdef __SMARTPHONE__
436 processed
= HandleCommand( LOWORD(wParam
) , 0 , NULL
);
438 #endif // __SMARTPHONE__
444 // if we can't close, tell the system that we processed the
445 // message - otherwise it would close us
446 processed
= !Close();
450 // the Windows dialogs unfortunately are not meant to be resizeable
451 // at all and their standard class doesn't include CS_[VH]REDRAW
452 // styles which means that the window is not refreshed properly
453 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
454 // help with it - so we have to refresh it manually which certainly
455 // creates flicker but at least doesn't show garbage on the screen
456 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
458 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
460 ::InvalidateRect(GetHwnd(), NULL
, false /* erase bg */);
464 #ifndef __WXMICROWIN__
466 // we want to override the busy cursor for modal dialogs:
467 // typically, wxBeginBusyCursor() is called and then a modal dialog
468 // is shown, but the modal dialog shouldn't have hourglass cursor
469 if ( IsModal() && wxIsBusy() )
471 // set our cursor for all windows (but see below)
472 wxCursor cursor
= m_cursor
;
474 cursor
= wxCURSOR_ARROW
;
476 ::SetCursor(GetHcursorOf(cursor
));
478 // in any case, stop here and don't let wxWindow process this
479 // message (it would set the busy cursor)
482 // but return false to tell the child window (if the event
483 // comes from one of them and not from ourselves) that it can
484 // set its own cursor if it has one: thus, standard controls
485 // (e.g. text ctrl) still have correct cursors in a dialog
486 // invoked while wxIsBusy()
490 #endif // __WXMICROWIN__
494 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);