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/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 wxASSERT_MSG( !IsModal(), wxT("ShowModal() can't be called twice") );
204 // EndModal may have been called from InitDialog handler (called from
205 // inside Show()) and hidden the dialog back again
208 // enter and run the modal loop
209 wxDialogModalDataTiedPtr
modalData(&m_modalData
,
210 new wxDialogModalData(this));
211 modalData
->RunLoop();
214 return GetReturnCode();
217 void wxDialog::EndModal(int retCode
)
219 wxASSERT_MSG( IsModal(), wxT("EndModal() called for non modal dialog") );
221 SetReturnCode(retCode
);
226 // ----------------------------------------------------------------------------
227 // wxDialog gripper handling
228 // ----------------------------------------------------------------------------
230 #if wxUSE_DIALOG_SIZEGRIP
232 void wxDialog::SetWindowStyleFlag(long style
)
234 wxDialogBase::SetWindowStyleFlag(style
);
236 if ( HasFlag(wxRESIZE_BORDER
) )
242 void wxDialog::CreateGripper()
246 // just create it here, it will be positioned and shown later
247 m_hGripper
= (WXHWND
)::CreateWindow
255 SBS_SIZEBOXBOTTOMRIGHTALIGN
,
265 void wxDialog::DestroyGripper()
269 // we used to have trouble with gripper appearing on top (and hence
270 // overdrawing) the other, real, dialog children -- check that this
271 // isn't the case automatically (but notice that this could be false if
272 // we're not shown at all as in this case ResizeGripper() might not
273 // have been called yet)
274 wxASSERT_MSG( !IsShown() ||
275 ::GetWindow((HWND
)m_hGripper
, GW_HWNDNEXT
) == 0,
276 wxT("Bug in wxWidgets: gripper should be at the bottom of Z-order") );
277 ::DestroyWindow((HWND
) m_hGripper
);
282 void wxDialog::ShowGripper(bool show
)
284 wxASSERT_MSG( m_hGripper
, wxT("shouldn't be called if we have no gripper") );
289 ::ShowWindow((HWND
)m_hGripper
, show
? SW_SHOW
: SW_HIDE
);
292 void wxDialog::ResizeGripper()
294 wxASSERT_MSG( m_hGripper
, wxT("shouldn't be called if we have no gripper") );
296 HWND hwndGripper
= (HWND
)m_hGripper
;
298 const wxRect rectGripper
= wxRectFromRECT(wxGetWindowRect(hwndGripper
));
299 const wxSize size
= GetClientSize() - rectGripper
.GetSize();
301 ::SetWindowPos(hwndGripper
, HWND_BOTTOM
,
303 rectGripper
.width
, rectGripper
.height
,
307 void wxDialog::OnWindowCreate(wxWindowCreateEvent
& event
)
309 if ( m_hGripper
&& IsShown() &&
310 event
.GetWindow() && event
.GetWindow()->GetParent() == this )
312 // Put gripper below the newly created child window
313 ::SetWindowPos((HWND
)m_hGripper
, HWND_BOTTOM
, 0, 0, 0, 0,
314 SWP_NOSIZE
| SWP_NOMOVE
| SWP_NOACTIVATE
);
320 #endif // wxUSE_DIALOG_SIZEGRIP
322 // ----------------------------------------------------------------------------
323 // wxWin event handlers
324 // ----------------------------------------------------------------------------
327 // Responds to the OK button in a PocketPC titlebar. This
328 // can be overridden, or you can change the id used for
329 // sending the event, by calling SetAffirmativeId.
330 bool wxDialog::DoOK()
332 const int idOk
= GetAffirmativeId();
333 if ( EmulateButtonClickIfPresent(idOk
) )
336 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetAffirmativeId());
337 event
.SetEventObject(this);
339 return HandleWindowEvent(event
);
341 #endif // __POCKETPC__
343 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
344 // create main toolbar by calling OnCreateToolBar()
345 wxToolBar
* wxDialog::CreateToolBar(long style
, wxWindowID winid
, const wxString
& name
)
347 m_dialogToolBar
= OnCreateToolBar(style
, winid
, name
);
349 return m_dialogToolBar
;
352 // return a new toolbar
353 wxToolBar
*wxDialog::OnCreateToolBar(long style
,
355 const wxString
& name
)
357 return new wxToolMenuBar(this, winid
,
358 wxDefaultPosition
, wxDefaultSize
,
363 // ---------------------------------------------------------------------------
364 // dialog Windows messages processing
365 // ---------------------------------------------------------------------------
367 WXLRESULT
wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
370 bool processed
= false;
375 // react to pressing the OK button in the title
378 switch ( LOWORD(wParam
) )
384 processed
= !Close();
386 #ifdef __SMARTPHONE__
389 processed
= HandleCommand( LOWORD(wParam
) , 0 , NULL
);
391 #endif // __SMARTPHONE__
397 // if we can't close, tell the system that we processed the
398 // message - otherwise it would close us
399 processed
= !Close();
403 #if wxUSE_DIALOG_SIZEGRIP
416 #endif // wxUSE_DIALOG_SIZEGRIP
418 // the Windows dialogs unfortunately are not meant to be resizable
419 // at all and their standard class doesn't include CS_[VH]REDRAW
420 // styles which means that the window is not refreshed properly
421 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
422 // help with it - so we have to refresh it manually which certainly
423 // creates flicker but at least doesn't show garbage on the screen
424 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
426 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
428 ::InvalidateRect(GetHwnd(), NULL
, false /* erase bg */);
432 #ifndef __WXMICROWIN__
434 // we want to override the busy cursor for modal dialogs:
435 // typically, wxBeginBusyCursor() is called and then a modal dialog
436 // is shown, but the modal dialog shouldn't have hourglass cursor
437 if ( IsModal() && wxIsBusy() )
439 // set our cursor for all windows (but see below)
440 wxCursor cursor
= m_cursor
;
441 if ( !cursor
.IsOk() )
442 cursor
= wxCURSOR_ARROW
;
444 ::SetCursor(GetHcursorOf(cursor
));
446 // in any case, stop here and don't let wxWindow process this
447 // message (it would set the busy cursor)
450 // but return false to tell the child window (if the event
451 // comes from one of them and not from ourselves) that it can
452 // set its own cursor if it has one: thus, standard controls
453 // (e.g. text ctrl) still have correct cursors in a dialog
454 // invoked while wxIsBusy()
458 #endif // __WXMICROWIN__
462 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);