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"
28 #include "wx/testing.h"
31 #include "wx/msw/wrapcdlg.h"
35 #include "wx/button.h"
36 #include "wx/settings.h"
39 #include "wx/toolbar.h"
42 #include "wx/msw/private.h"
43 #include "wx/evtloop.h"
44 #include "wx/scopedptr.h"
46 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
47 #include "wx/msw/wince/resources.h"
48 #endif // __SMARTPHONE__ && __WXWINCE__
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 // this is simply a container for any data we need to implement modality which
59 // allows us to avoid changing wxDialog each time the implementation changes
60 class wxDialogModalData
63 wxDialogModalData(wxDialog
*dialog
) : m_evtLoop(dialog
) { }
76 wxModalEventLoop m_evtLoop
;
79 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData
)
81 // ============================================================================
83 // ============================================================================
85 // ----------------------------------------------------------------------------
86 // wxDialog construction
87 // ----------------------------------------------------------------------------
93 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
94 m_dialogToolBar
= NULL
;
96 #if wxUSE_DIALOG_SIZEGRIP
98 #endif // wxUSE_DIALOG_SIZEGRIP
101 bool wxDialog::Create(wxWindow
*parent
,
103 const wxString
& title
,
107 const wxString
& name
)
109 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
111 // All dialogs should really have this style
112 style
|= wxTAB_TRAVERSAL
;
114 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
118 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
120 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
121 SetLeftMenu(wxID_OK
, _("OK"));
123 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
127 #if wxUSE_DIALOG_SIZEGRIP
128 if ( HasFlag(wxRESIZE_BORDER
) )
132 Connect(wxEVT_CREATE
,
133 wxWindowCreateEventHandler(wxDialog::OnWindowCreate
));
135 #endif // wxUSE_DIALOG_SIZEGRIP
140 wxDialog::~wxDialog()
142 // this will also reenable all the other windows for a modal dialog
145 #if wxUSE_DIALOG_SIZEGRIP
147 #endif // wxUSE_DIALOG_SIZEGRIP
150 // ----------------------------------------------------------------------------
151 // showing the dialogs
152 // ----------------------------------------------------------------------------
154 bool wxDialog::Show(bool show
)
156 if ( show
== IsShown() )
159 if ( !show
&& m_modalData
)
161 // we need to do this before calling wxDialogBase version because if we
162 // had disabled other app windows, they must be reenabled right now as
163 // if they stay disabled Windows will activate another window (one
164 // which is enabled, anyhow) when we're hidden in the base class Show()
165 // and we will lose activation
166 m_modalData
->ExitLoop();
171 if (CanDoLayoutAdaptation())
172 DoLayoutAdaptation();
174 // this usually will result in TransferDataToWindow() being called
175 // which will change the controls values so do it before showing as
176 // otherwise we could have some flicker
180 wxDialogBase::Show(show
);
184 // dialogs don't get WM_SIZE message from ::ShowWindow() for some
185 // reason so generate it ourselves for consistency with frames and
186 // dialogs in other ports
188 // NB: normally we should call it just the first time but doing it
189 // every time is simpler than keeping a flag
190 const wxSize size
= GetClientSize();
191 ::SendMessage(GetHwnd(), WM_SIZE
,
192 SIZE_RESTORED
, MAKELPARAM(size
.x
, size
.y
));
198 // show dialog modally
199 int wxDialog::ShowModal()
201 WX_TESTING_SHOW_MODAL_HOOK();
203 wxASSERT_MSG( !IsModal(), wxT("ShowModal() can't be called twice") );
207 // EndModal may have been called from InitDialog handler (called from
208 // inside Show()) and hidden the dialog back again
211 // enter and run the modal loop
212 wxDialogModalDataTiedPtr
modalData(&m_modalData
,
213 new wxDialogModalData(this));
214 modalData
->RunLoop();
217 return GetReturnCode();
220 void wxDialog::EndModal(int retCode
)
222 wxASSERT_MSG( IsModal(), wxT("EndModal() called for non modal dialog") );
224 SetReturnCode(retCode
);
229 // ----------------------------------------------------------------------------
230 // wxDialog gripper handling
231 // ----------------------------------------------------------------------------
233 #if wxUSE_DIALOG_SIZEGRIP
235 void wxDialog::SetWindowStyleFlag(long style
)
237 wxDialogBase::SetWindowStyleFlag(style
);
239 if ( HasFlag(wxRESIZE_BORDER
) )
245 void wxDialog::CreateGripper()
249 // just create it here, it will be positioned and shown later
250 m_hGripper
= (WXHWND
)::CreateWindow
258 SBS_SIZEBOXBOTTOMRIGHTALIGN
,
268 void wxDialog::DestroyGripper()
272 // we used to have trouble with gripper appearing on top (and hence
273 // overdrawing) the other, real, dialog children -- check that this
274 // isn't the case automatically (but notice that this could be false if
275 // we're not shown at all as in this case ResizeGripper() might not
276 // have been called yet)
277 wxASSERT_MSG( !IsShown() ||
278 ::GetWindow((HWND
)m_hGripper
, GW_HWNDNEXT
) == 0,
279 wxT("Bug in wxWidgets: gripper should be at the bottom of Z-order") );
280 ::DestroyWindow((HWND
) m_hGripper
);
285 void wxDialog::ShowGripper(bool show
)
287 wxASSERT_MSG( m_hGripper
, wxT("shouldn't be called if we have no gripper") );
292 ::ShowWindow((HWND
)m_hGripper
, show
? SW_SHOW
: SW_HIDE
);
295 void wxDialog::ResizeGripper()
297 wxASSERT_MSG( m_hGripper
, wxT("shouldn't be called if we have no gripper") );
299 HWND hwndGripper
= (HWND
)m_hGripper
;
301 const wxRect rectGripper
= wxRectFromRECT(wxGetWindowRect(hwndGripper
));
302 const wxSize size
= GetClientSize() - rectGripper
.GetSize();
304 ::SetWindowPos(hwndGripper
, HWND_BOTTOM
,
306 rectGripper
.width
, rectGripper
.height
,
310 void wxDialog::OnWindowCreate(wxWindowCreateEvent
& event
)
312 if ( m_hGripper
&& IsShown() &&
313 event
.GetWindow() && event
.GetWindow()->GetParent() == this )
315 // Put gripper below the newly created child window
316 ::SetWindowPos((HWND
)m_hGripper
, HWND_BOTTOM
, 0, 0, 0, 0,
317 SWP_NOSIZE
| SWP_NOMOVE
| SWP_NOACTIVATE
);
323 #endif // wxUSE_DIALOG_SIZEGRIP
325 // ----------------------------------------------------------------------------
326 // wxWin event handlers
327 // ----------------------------------------------------------------------------
330 // Responds to the OK button in a PocketPC titlebar. This
331 // can be overridden, or you can change the id used for
332 // sending the event, by calling SetAffirmativeId.
333 bool wxDialog::DoOK()
335 const int idOk
= GetAffirmativeId();
336 if ( EmulateButtonClickIfPresent(idOk
) )
339 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetAffirmativeId());
340 event
.SetEventObject(this);
342 return HandleWindowEvent(event
);
344 #endif // __POCKETPC__
346 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
347 // create main toolbar by calling OnCreateToolBar()
348 wxToolBar
* wxDialog::CreateToolBar(long style
, wxWindowID winid
, const wxString
& name
)
350 m_dialogToolBar
= OnCreateToolBar(style
, winid
, name
);
352 return m_dialogToolBar
;
355 // return a new toolbar
356 wxToolBar
*wxDialog::OnCreateToolBar(long style
,
358 const wxString
& name
)
360 return new wxToolMenuBar(this, winid
,
361 wxDefaultPosition
, wxDefaultSize
,
366 // ---------------------------------------------------------------------------
367 // dialog Windows messages processing
368 // ---------------------------------------------------------------------------
370 WXLRESULT
wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
373 bool processed
= false;
378 // react to pressing the OK button in the title
381 switch ( LOWORD(wParam
) )
387 processed
= !Close();
389 #ifdef __SMARTPHONE__
392 processed
= HandleCommand( LOWORD(wParam
) , 0 , NULL
);
394 #endif // __SMARTPHONE__
400 // if we can't close, tell the system that we processed the
401 // message - otherwise it would close us
402 processed
= !Close();
406 #if wxUSE_DIALOG_SIZEGRIP
419 #endif // wxUSE_DIALOG_SIZEGRIP
421 // the Windows dialogs unfortunately are not meant to be resizable
422 // at all and their standard class doesn't include CS_[VH]REDRAW
423 // styles which means that the window is not refreshed properly
424 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
425 // help with it - so we have to refresh it manually which certainly
426 // creates flicker but at least doesn't show garbage on the screen
427 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
429 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
431 ::InvalidateRect(GetHwnd(), NULL
, false /* erase bg */);
435 #ifndef __WXMICROWIN__
437 // we want to override the busy cursor for modal dialogs:
438 // typically, wxBeginBusyCursor() is called and then a modal dialog
439 // is shown, but the modal dialog shouldn't have hourglass cursor
440 if ( IsModal() && wxIsBusy() )
442 // set our cursor for all windows (but see below)
443 wxCursor cursor
= m_cursor
;
444 if ( !cursor
.IsOk() )
445 cursor
= wxCURSOR_ARROW
;
447 ::SetCursor(GetHcursorOf(cursor
));
449 // in any case, stop here and don't let wxWindow process this
450 // message (it would set the busy cursor)
453 // but return false to tell the child window (if the event
454 // comes from one of them and not from ourselves) that it can
455 // set its own cursor if it has one: thus, standard controls
456 // (e.g. text ctrl) still have correct cursors in a dialog
457 // invoked while wxIsBusy()
461 #endif // __WXMICROWIN__
465 rc
= wxDialogBase::MSWWindowProc(message
, wParam
, lParam
);