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 void wxDialog::Raise()
199 ::SetForegroundWindow(GetHwnd());
202 // show dialog modally
203 int wxDialog::ShowModal()
205 wxASSERT_MSG( !IsModal(), wxT("ShowModal() can't be called twice") );
209 // EndModal may have been called from InitDialog handler (called from
210 // inside Show()) and hidden the dialog back again
213 // enter and run the modal loop
214 wxDialogModalDataTiedPtr
modalData(&m_modalData
,
215 new wxDialogModalData(this));
216 modalData
->RunLoop();
219 return GetReturnCode();
222 void wxDialog::EndModal(int retCode
)
224 wxASSERT_MSG( IsModal(), wxT("EndModal() called for non modal dialog") );
226 SetReturnCode(retCode
);
231 // ----------------------------------------------------------------------------
232 // wxDialog gripper handling
233 // ----------------------------------------------------------------------------
235 #if wxUSE_DIALOG_SIZEGRIP
237 void wxDialog::SetWindowStyleFlag(long style
)
239 wxDialogBase::SetWindowStyleFlag(style
);
241 if ( HasFlag(wxRESIZE_BORDER
) )
247 void wxDialog::CreateGripper()
251 // just create it here, it will be positioned and shown later
252 m_hGripper
= (WXHWND
)::CreateWindow
260 SBS_SIZEBOXBOTTOMRIGHTALIGN
,
270 void wxDialog::DestroyGripper()
274 // we used to have trouble with gripper appearing on top (and hence
275 // overdrawing) the other, real, dialog children -- check that this
276 // isn't the case automatically (but notice that this could be false if
277 // we're not shown at all as in this case ResizeGripper() might not
278 // have been called yet)
279 wxASSERT_MSG( !IsShown() ||
280 ::GetWindow((HWND
)m_hGripper
, GW_HWNDNEXT
) == 0,
281 wxT("Bug in wxWidgets: gripper should be at the bottom of Z-order") );
282 ::DestroyWindow((HWND
) m_hGripper
);
287 void wxDialog::ShowGripper(bool show
)
289 wxASSERT_MSG( m_hGripper
, wxT("shouldn't be called if we have no gripper") );
294 ::ShowWindow((HWND
)m_hGripper
, show
? SW_SHOW
: SW_HIDE
);
297 void wxDialog::ResizeGripper()
299 wxASSERT_MSG( m_hGripper
, wxT("shouldn't be called if we have no gripper") );
301 HWND hwndGripper
= (HWND
)m_hGripper
;
303 const wxRect rectGripper
= wxRectFromRECT(wxGetWindowRect(hwndGripper
));
304 const wxSize size
= GetClientSize() - rectGripper
.GetSize();
306 ::SetWindowPos(hwndGripper
, HWND_BOTTOM
,
308 rectGripper
.width
, rectGripper
.height
,
312 void wxDialog::OnWindowCreate(wxWindowCreateEvent
& event
)
314 if ( m_hGripper
&& IsShown() &&
315 event
.GetWindow() && event
.GetWindow()->GetParent() == this )
317 // Put gripper below the newly created child window
318 ::SetWindowPos((HWND
)m_hGripper
, HWND_BOTTOM
, 0, 0, 0, 0,
319 SWP_NOSIZE
| SWP_NOMOVE
| SWP_NOACTIVATE
);
325 #endif // wxUSE_DIALOG_SIZEGRIP
327 // ----------------------------------------------------------------------------
328 // wxWin event handlers
329 // ----------------------------------------------------------------------------
332 // Responds to the OK button in a PocketPC titlebar. This
333 // can be overridden, or you can change the id used for
334 // sending the event, by calling SetAffirmativeId.
335 bool wxDialog::DoOK()
337 const int idOk
= GetAffirmativeId();
338 if ( EmulateButtonClickIfPresent(idOk
) )
341 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetAffirmativeId());
342 event
.SetEventObject(this);
344 return HandleWindowEvent(event
);
346 #endif // __POCKETPC__
348 #if wxUSE_TOOLBAR && defined(__POCKETPC__)
349 // create main toolbar by calling OnCreateToolBar()
350 wxToolBar
* wxDialog::CreateToolBar(long style
, wxWindowID winid
, const wxString
& name
)
352 m_dialogToolBar
= OnCreateToolBar(style
, winid
, name
);
354 return m_dialogToolBar
;
357 // return a new toolbar
358 wxToolBar
*wxDialog::OnCreateToolBar(long style
,
360 const wxString
& name
)
362 return new wxToolMenuBar(this, winid
,
363 wxDefaultPosition
, wxDefaultSize
,
368 // ---------------------------------------------------------------------------
369 // dialog Windows messages processing
370 // ---------------------------------------------------------------------------
372 WXLRESULT
wxDialog::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
375 bool processed
= false;
380 // react to pressing the OK button in the title
383 switch ( LOWORD(wParam
) )
389 processed
= !Close();
391 #ifdef __SMARTPHONE__
394 processed
= HandleCommand( LOWORD(wParam
) , 0 , NULL
);
396 #endif // __SMARTPHONE__
402 // if we can't close, tell the system that we processed the
403 // message - otherwise it would close us
404 processed
= !Close();
408 #if wxUSE_DIALOG_SIZEGRIP
421 #endif // wxUSE_DIALOG_SIZEGRIP
423 // the Windows dialogs unfortunately are not meant to be resizable
424 // at all and their standard class doesn't include CS_[VH]REDRAW
425 // styles which means that the window is not refreshed properly
426 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
427 // help with it - so we have to refresh it manually which certainly
428 // creates flicker but at least doesn't show garbage on the screen
429 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);
431 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE
) )
433 ::InvalidateRect(GetHwnd(), NULL
, false /* erase bg */);
437 #ifndef __WXMICROWIN__
439 // we want to override the busy cursor for modal dialogs:
440 // typically, wxBeginBusyCursor() is called and then a modal dialog
441 // is shown, but the modal dialog shouldn't have hourglass cursor
442 if ( IsModal() && wxIsBusy() )
444 // set our cursor for all windows (but see below)
445 wxCursor cursor
= m_cursor
;
447 cursor
= wxCURSOR_ARROW
;
449 ::SetCursor(GetHcursorOf(cursor
));
451 // in any case, stop here and don't let wxWindow process this
452 // message (it would set the busy cursor)
455 // but return false to tell the child window (if the event
456 // comes from one of them and not from ourselves) that it can
457 // set its own cursor if it has one: thus, standard controls
458 // (e.g. text ctrl) still have correct cursors in a dialog
459 // invoked while wxIsBusy()
463 #endif // __WXMICROWIN__
467 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);