1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dialog.cpp
3 // Purpose: wxDialog class
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/dialog.h"
27 #include "wx/modalhook.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/scopedptr.h"
45 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
46 #include "wx/msw/wince/resources.h"
47 #endif // __SMARTPHONE__ && __WXWINCE__
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // this is simply a container for any data we need to implement modality which
58 // allows us to avoid changing wxDialog each time the implementation changes
59 class wxDialogModalData
62 wxDialogModalData(wxDialog
*dialog
) : m_evtLoop(dialog
) { }
75 wxModalEventLoop m_evtLoop
;
78 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData
)
80 // ============================================================================
82 // ============================================================================
84 // ----------------------------------------------------------------------------
85 // wxDialog construction
86 // ----------------------------------------------------------------------------
92 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
93 m_dialogToolBar
= NULL
;
95 #if wxUSE_DIALOG_SIZEGRIP
97 #endif // wxUSE_DIALOG_SIZEGRIP
100 bool wxDialog::Create(wxWindow
*parent
,
102 const wxString
& title
,
106 const wxString
& name
)
108 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
110 // All dialogs should really have this style
111 style
|= wxTAB_TRAVERSAL
;
113 if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) )
117 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
119 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
120 SetLeftMenu(wxID_OK
, _("OK"));
122 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
126 #if wxUSE_DIALOG_SIZEGRIP
127 if ( HasFlag(wxRESIZE_BORDER
) )
131 Connect(wxEVT_CREATE
,
132 wxWindowCreateEventHandler(wxDialog::OnWindowCreate
));
134 #endif // wxUSE_DIALOG_SIZEGRIP
139 wxDialog::~wxDialog()
141 // this will also reenable all the other windows for a modal dialog
144 #if wxUSE_DIALOG_SIZEGRIP
146 #endif // wxUSE_DIALOG_SIZEGRIP
149 // ----------------------------------------------------------------------------
150 // showing the dialogs
151 // ----------------------------------------------------------------------------
153 bool wxDialog::Show(bool show
)
155 if ( show
== IsShown() )
158 if ( !show
&& m_modalData
)
160 // we need to do this before calling wxDialogBase version because if we
161 // had disabled other app windows, they must be reenabled right now as
162 // if they stay disabled Windows will activate another window (one
163 // which is enabled, anyhow) when we're hidden in the base class Show()
164 // and we will lose activation
165 m_modalData
->ExitLoop();
170 if (CanDoLayoutAdaptation())
171 DoLayoutAdaptation();
173 // this usually will result in TransferDataToWindow() being called
174 // which will change the controls values so do it before showing as
175 // otherwise we could have some flicker
179 wxDialogBase::Show(show
);
183 // dialogs don't get WM_SIZE message from ::ShowWindow() for some
184 // reason so generate it ourselves for consistency with frames and
185 // dialogs in other ports
187 // NB: normally we should call it just the first time but doing it
188 // every time is simpler than keeping a flag
189 const wxSize size
= GetClientSize();
190 ::SendMessage(GetHwnd(), WM_SIZE
,
191 SIZE_RESTORED
, MAKELPARAM(size
.x
, size
.y
));
197 // show dialog modally
198 int wxDialog::ShowModal()
200 WX_HOOK_MODAL_DIALOG();
202 wxASSERT_MSG( !IsModal(), wxT("ShowModal() can't be called twice") );
206 // EndModal may have been called from InitDialog handler (called from
207 // inside Show()) and hidden the dialog back again
210 // enter and run the modal loop
211 wxDialogModalDataTiedPtr
modalData(&m_modalData
,
212 new wxDialogModalData(this));
213 modalData
->RunLoop();
216 return GetReturnCode();
219 void wxDialog::EndModal(int retCode
)
221 wxASSERT_MSG( IsModal(), wxT("EndModal() called for non modal dialog") );
223 SetReturnCode(retCode
);
228 // ----------------------------------------------------------------------------
229 // wxDialog gripper handling
230 // ----------------------------------------------------------------------------
232 #if wxUSE_DIALOG_SIZEGRIP
234 void wxDialog::SetWindowStyleFlag(long style
)
236 wxDialogBase::SetWindowStyleFlag(style
);
238 if ( HasFlag(wxRESIZE_BORDER
) )
244 void wxDialog::CreateGripper()
248 // just create it here, it will be positioned and shown later
249 m_hGripper
= (WXHWND
)::CreateWindow
257 SBS_SIZEBOXBOTTOMRIGHTALIGN
,
267 void wxDialog::DestroyGripper()
271 // we used to have trouble with gripper appearing on top (and hence
272 // overdrawing) the other, real, dialog children -- check that this
273 // isn't the case automatically (but notice that this could be false if
274 // we're not shown at all as in this case ResizeGripper() might not
275 // have been called yet)
276 wxASSERT_MSG( !IsShown() ||
277 ::GetWindow((HWND
)m_hGripper
, GW_HWNDNEXT
) == 0,
278 wxT("Bug in wxWidgets: gripper should be at the bottom of Z-order") );
279 ::DestroyWindow((HWND
) m_hGripper
);
284 void wxDialog::ShowGripper(bool show
)
286 wxASSERT_MSG( m_hGripper
, wxT("shouldn't be called if we have no gripper") );
291 ::ShowWindow((HWND
)m_hGripper
, show
? SW_SHOW
: SW_HIDE
);
294 void wxDialog::ResizeGripper()
296 wxASSERT_MSG( m_hGripper
, wxT("shouldn't be called if we have no gripper") );
298 HWND hwndGripper
= (HWND
)m_hGripper
;
300 const wxRect rectGripper
= wxRectFromRECT(wxGetWindowRect(hwndGripper
));
301 const wxSize size
= GetClientSize() - rectGripper
.GetSize();
303 ::SetWindowPos(hwndGripper
, HWND_BOTTOM
,
305 rectGripper
.width
, rectGripper
.height
,
309 void wxDialog::OnWindowCreate(wxWindowCreateEvent
& event
)
311 if ( m_hGripper
&& IsShown() &&
312 event
.GetWindow() && event
.GetWindow()->GetParent() == this )
314 // Put gripper below the newly created child window
315 ::SetWindowPos((HWND
)m_hGripper
, HWND_BOTTOM
, 0, 0, 0, 0,
316 SWP_NOSIZE
| SWP_NOMOVE
| SWP_NOACTIVATE
);
322 #endif // wxUSE_DIALOG_SIZEGRIP
324 // ----------------------------------------------------------------------------
325 // wxWin event handlers
326 // ----------------------------------------------------------------------------
329 // Responds to the OK button in a PocketPC titlebar. This
330 // can be overridden, or you can change the id used for
331 // sending the event, by calling SetAffirmativeId.
332 bool wxDialog::DoOK()
334 const int idOk
= GetAffirmativeId();
335 if ( EmulateButtonClickIfPresent(idOk
) )
338 wxCommandEvent
event(wxEVT_BUTTON
, GetAffirmativeId());
339 event
.SetEventObject(this);
341 return HandleWindowEvent(event
);
343 #endif // __POCKETPC__
345 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
346 // create main toolbar by calling OnCreateToolBar()
347 wxToolBar
* wxDialog::CreateToolBar(long style
, wxWindowID winid
, const wxString
& name
)
349 m_dialogToolBar
= OnCreateToolBar(style
, winid
, name
);
351 return m_dialogToolBar
;
354 // return a new toolbar
355 wxToolBar
*wxDialog::OnCreateToolBar(long style
,
357 const wxString
& name
)
359 return new wxToolMenuBar(this, winid
,
360 wxDefaultPosition
, wxDefaultSize
,
365 // ---------------------------------------------------------------------------
366 // dialog Windows messages processing
367 // ---------------------------------------------------------------------------
369 WXLRESULT
wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
372 bool processed
= false;
377 // react to pressing the OK button in the title
380 switch ( LOWORD(wParam
) )
386 processed
= !Close();
388 #ifdef __SMARTPHONE__
391 processed
= HandleCommand( LOWORD(wParam
) , 0 , NULL
);
393 #endif // __SMARTPHONE__
399 // if we can't close, tell the system that we processed the
400 // message - otherwise it would close us
401 processed
= !Close();
405 #if wxUSE_DIALOG_SIZEGRIP
418 #endif // wxUSE_DIALOG_SIZEGRIP
420 // the Windows dialogs unfortunately are not meant to be resizable
421 // at all and their standard class doesn't include CS_[VH]REDRAW
422 // styles which means that the window is not refreshed properly
423 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
424 // help with it - so we have to refresh it manually which certainly
425 // creates flicker but at least doesn't show garbage on the screen
426 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
428 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
430 ::InvalidateRect(GetHwnd(), NULL
, false /* erase bg */);
434 #ifndef __WXMICROWIN__
436 // we want to override the busy cursor for modal dialogs:
437 // typically, wxBeginBusyCursor() is called and then a modal dialog
438 // is shown, but the modal dialog shouldn't have hourglass cursor
439 if ( IsModal() && wxIsBusy() )
441 // set our cursor for all windows (but see below)
442 wxCursor cursor
= m_cursor
;
443 if ( !cursor
.IsOk() )
444 cursor
= wxCURSOR_ARROW
;
446 ::SetCursor(GetHcursorOf(cursor
));
448 // in any case, stop here and don't let wxWindow process this
449 // message (it would set the busy cursor)
452 // but return false to tell the child window (if the event
453 // comes from one of them and not from ourselves) that it can
454 // set its own cursor if it has one: thus, standard controls
455 // (e.g. text ctrl) still have correct cursors in a dialog
456 // invoked while wxIsBusy()
460 #endif // __WXMICROWIN__
464 rc
= wxDialogBase::MSWWindowProc(message
, wParam
, lParam
);