]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
Ensure that validators work even in presence of pushed event handlers.
[wxWidgets.git] / src / msw / dialog.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
b6c588e1 2// Name: src/msw/dialog.cpp
2bda0e17
KB
3// Purpose: wxDialog class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
b6c588e1
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
b6c588e1 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
fdf565fe
WS
27#include "wx/dialog.h"
28
2bda0e17 29#ifndef WX_PRECOMP
57bd4c60 30 #include "wx/msw/wrapcdlg.h"
b6c588e1
VZ
31 #include "wx/utils.h"
32 #include "wx/frame.h"
33 #include "wx/app.h"
95e92d90 34 #include "wx/button.h"
b6c588e1
VZ
35 #include "wx/settings.h"
36 #include "wx/intl.h"
37 #include "wx/log.h"
4e3e485b 38 #include "wx/toolbar.h"
2bda0e17
KB
39#endif
40
41#include "wx/msw/private.h"
ac8d0c11 42#include "wx/evtloop.h"
664e1314 43#include "wx/scopedptr.h"
2bda0e17 44
3180bc0e 45#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
ef6d716b 46 #include "wx/msw/wince/resources.h"
3180bc0e 47#endif // __SMARTPHONE__ && __WXWINCE__
ef6d716b 48
b6c588e1
VZ
49// ----------------------------------------------------------------------------
50// wxWin macros
51// ----------------------------------------------------------------------------
52
ffca575c
SC
53#if wxUSE_EXTENDED_RTTI
54WX_DEFINE_FLAGS( wxDialogStyle )
55
3ff066a4 56wxBEGIN_FLAGS( wxDialogStyle )
ffca575c
SC
57 // new style border flags, we put them first to
58 // use them for streaming out
3ff066a4
SC
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)
044fe836 65
ffca575c 66 // old style border flags
3ff066a4
SC
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)
ffca575c
SC
73
74 // standard window styles
3ff066a4
SC
75 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
76 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
ffca575c
SC
77
78 // dialog styles
3ff066a4
SC
79 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY)
80 wxFLAGS_MEMBER(wxSTAY_ON_TOP)
81 wxFLAGS_MEMBER(wxCAPTION)
1c067fe3 82#if WXWIN_COMPATIBILITY_2_6
3ff066a4 83 wxFLAGS_MEMBER(wxTHICK_FRAME)
1c067fe3 84#endif // WXWIN_COMPATIBILITY_2_6
3ff066a4
SC
85 wxFLAGS_MEMBER(wxSYSTEM_MENU)
86 wxFLAGS_MEMBER(wxRESIZE_BORDER)
1c067fe3 87#if WXWIN_COMPATIBILITY_2_6
3ff066a4 88 wxFLAGS_MEMBER(wxRESIZE_BOX)
1c067fe3 89#endif // WXWIN_COMPATIBILITY_2_6
3ff066a4
SC
90 wxFLAGS_MEMBER(wxCLOSE_BOX)
91 wxFLAGS_MEMBER(wxMAXIMIZE_BOX)
92 wxFLAGS_MEMBER(wxMINIMIZE_BOX)
93wxEND_FLAGS( wxDialogStyle )
ffca575c
SC
94
95IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog, wxTopLevelWindow,"wx/dialog.h")
96
3ff066a4 97wxBEGIN_PROPERTIES_TABLE(wxDialog)
af498247
VZ
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
3ff066a4 100wxEND_PROPERTIES_TABLE()
ffca575c 101
3ff066a4
SC
102wxBEGIN_HANDLERS_TABLE(wxDialog)
103wxEND_HANDLERS_TABLE()
ffca575c 104
044fe836 105wxCONSTRUCTOR_6( wxDialog , wxWindow* , Parent , wxWindowID , Id , wxString , Title , wxPoint , Position , wxSize , Size , long , WindowStyle)
ffca575c
SC
106
107#else
82c9f85c 108IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
ffca575c 109#endif
066f1b7a 110
6757b5e3
VZ
111// ----------------------------------------------------------------------------
112// wxDialogModalData
113// ----------------------------------------------------------------------------
114
8c7f5f03
VZ
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
6757b5e3
VZ
117class wxDialogModalData
118{
119public:
8c7f5f03 120 wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { }
6757b5e3 121
8c7f5f03 122 void RunLoop()
6757b5e3 123 {
6757b5e3
VZ
124 m_evtLoop.Run();
125 }
126
127 void ExitLoop()
128 {
6757b5e3
VZ
129 m_evtLoop.Exit();
130 }
131
6757b5e3 132private:
8c7f5f03 133 wxModalEventLoop m_evtLoop;
6757b5e3
VZ
134};
135
259c43f6 136wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData)
8c7f5f03 137
b6c588e1
VZ
138// ============================================================================
139// implementation
140// ============================================================================
141
142// ----------------------------------------------------------------------------
143// wxDialog construction
144// ----------------------------------------------------------------------------
145
b0a6bb75 146void wxDialog::Init()
2bda0e17 147{
044fe836 148 m_isShown = false;
6757b5e3 149 m_modalData = NULL;
ec5f0c24
JS
150#if wxUSE_TOOLBAR && defined(__POCKETPC__)
151 m_dialogToolBar = NULL;
152#endif
40636dcb 153#if wxUSE_DIALOG_SIZEGRIP
2c66581e 154 m_hGripper = 0;
40636dcb 155#endif // wxUSE_DIALOG_SIZEGRIP
2bda0e17
KB
156}
157
b3daa5a3
VZ
158bool wxDialog::Create(wxWindow *parent,
159 wxWindowID id,
462e2437
VZ
160 const wxString& title,
161 const wxPoint& pos,
162 const wxSize& size,
163 long style,
164 const wxString& name)
2bda0e17 165{
82c9f85c 166 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
bd9d76cb 167
706bb5f9 168 // All dialogs should really have this style
b225f659 169 style |= wxTAB_TRAVERSAL;
2bda0e17 170
b225f659 171 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
044fe836 172 return false;
00233716
VZ
173
174 if ( !m_hasFont )
6f951810 175 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
e1bdd507 176
3180bc0e 177#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
ef6d716b
WS
178 SetLeftMenu(wxID_OK, _("OK"));
179#endif
ec5f0c24
JS
180#if wxUSE_TOOLBAR && defined(__POCKETPC__)
181 CreateToolBar();
182#endif
ef6d716b 183
40636dcb 184#if wxUSE_DIALOG_SIZEGRIP
952f8d3c 185 if ( HasFlag(wxRESIZE_BORDER) )
eddc468e 186 {
2c66581e
VZ
187 CreateGripper();
188
eddc468e
VZ
189 Connect(wxEVT_CREATE,
190 wxWindowCreateEventHandler(wxDialog::OnWindowCreate));
191 }
40636dcb 192#endif // wxUSE_DIALOG_SIZEGRIP
eddc468e 193
044fe836 194 return true;
2bda0e17
KB
195}
196
2bda0e17
KB
197wxDialog::~wxDialog()
198{
b0a6bb75 199 // this will also reenable all the other windows for a modal dialog
044fe836 200 Show(false);
2c66581e 201
40636dcb 202#if wxUSE_DIALOG_SIZEGRIP
2c66581e 203 DestroyGripper();
40636dcb 204#endif // wxUSE_DIALOG_SIZEGRIP
2bda0e17
KB
205}
206
b6c588e1
VZ
207// ----------------------------------------------------------------------------
208// showing the dialogs
209// ----------------------------------------------------------------------------
22cf5fec 210
b6c588e1 211bool wxDialog::Show(bool show)
2bda0e17 212{
044fe836
VZ
213 if ( show == IsShown() )
214 return false;
215
6757b5e3 216 if ( !show && m_modalData )
86ad564e 217 {
6757b5e3
VZ
218 // we need to do this before calling wxDialogBase version because if we
219 // had disabled other app windows, they must be reenabled right now as
b0a6bb75 220 // if they stay disabled Windows will activate another window (one
6757b5e3
VZ
221 // which is enabled, anyhow) when we're hidden in the base class Show()
222 // and we will lose activation
223 m_modalData->ExitLoop();
86ad564e
JS
224 }
225
044fe836 226 if ( show )
b6c588e1 227 {
3aa8e4ea
JS
228 if (CanDoLayoutAdaptation())
229 DoLayoutAdaptation();
230
044fe836
VZ
231 // this usually will result in TransferDataToWindow() being called
232 // which will change the controls values so do it before showing as
233 // otherwise we could have some flicker
234 InitDialog();
b6c588e1 235 }
2bda0e17 236
044fe836
VZ
237 wxDialogBase::Show(show);
238
b6c588e1
VZ
239 if ( show )
240 {
2bc44d62
VZ
241 // dialogs don't get WM_SIZE message from ::ShowWindow() for some
242 // reason so generate it ourselves for consistency with frames and
243 // dialogs in other ports
2b5f62a0
VZ
244 //
245 // NB: normally we should call it just the first time but doing it
246 // every time is simpler than keeping a flag
2bc44d62
VZ
247 const wxSize size = GetClientSize();
248 ::SendMessage(GetHwnd(), WM_SIZE,
249 SIZE_RESTORED, MAKELPARAM(size.x, size.y));
b6c588e1 250 }
2bda0e17 251
044fe836 252 return true;
2bda0e17
KB
253}
254
313901f3
JS
255void wxDialog::Raise()
256{
257 ::SetForegroundWindow(GetHwnd());
258}
259
f46f4c86 260// show dialog modally
a23fd0e1 261int wxDialog::ShowModal()
2bda0e17 262{
9a83f860 263 wxASSERT_MSG( !IsModal(), wxT("ShowModal() can't be called twice") );
f46f4c86 264
f46f4c86
VZ
265 Show();
266
267 // EndModal may have been called from InitDialog handler (called from
89424d9b 268 // inside Show()) and hidden the dialog back again
4b5e178a 269 if ( IsShown() )
5e1febfa 270 {
f46f4c86 271 // enter and run the modal loop
88a67391
VZ
272 wxDialogModalDataTiedPtr modalData(&m_modalData,
273 new wxDialogModalData(this));
274 modalData->RunLoop();
f46f4c86 275 }
5e1febfa 276
b6c588e1 277 return GetReturnCode();
2bda0e17
KB
278}
279
280void wxDialog::EndModal(int retCode)
281{
9a83f860 282 wxASSERT_MSG( IsModal(), wxT("EndModal() called for non modal dialog") );
f46f4c86 283
b6c588e1 284 SetReturnCode(retCode);
6a088435 285
12b58624
VZ
286 Hide();
287}
288
2c66581e
VZ
289// ----------------------------------------------------------------------------
290// wxDialog gripper handling
291// ----------------------------------------------------------------------------
292
40636dcb
VZ
293#if wxUSE_DIALOG_SIZEGRIP
294
2c66581e
VZ
295void wxDialog::SetWindowStyleFlag(long style)
296{
297 wxDialogBase::SetWindowStyleFlag(style);
298
952f8d3c 299 if ( HasFlag(wxRESIZE_BORDER) )
2c66581e
VZ
300 CreateGripper();
301 else
302 DestroyGripper();
303}
304
305void wxDialog::CreateGripper()
306{
952f8d3c 307 if ( !m_hGripper )
2c66581e 308 {
952f8d3c 309 // just create it here, it will be positioned and shown later
2c66581e
VZ
310 m_hGripper = (WXHWND)::CreateWindow
311 (
312 wxT("SCROLLBAR"),
313 wxT(""),
952f8d3c
VZ
314 WS_CHILD |
315 WS_CLIPSIBLINGS |
2c66581e
VZ
316 SBS_SIZEGRIP |
317 SBS_SIZEBOX |
318 SBS_SIZEBOXBOTTOMRIGHTALIGN,
319 0, 0, 0, 0,
320 GetHwnd(),
321 0,
322 wxGetInstance(),
323 NULL
324 );
2c66581e
VZ
325 }
326}
327
328void wxDialog::DestroyGripper()
329{
330 if ( m_hGripper )
331 {
eddc468e
VZ
332 // we used to have trouble with gripper appearing on top (and hence
333 // overdrawing) the other, real, dialog children -- check that this
f78c70f3
VZ
334 // isn't the case automatically (but notice that this could be false if
335 // we're not shown at all as in this case ResizeGripper() might not
336 // have been called yet)
337 wxASSERT_MSG( !IsShown() ||
c3b46177 338 ::GetWindow((HWND)m_hGripper, GW_HWNDNEXT) == 0,
9a83f860 339 wxT("Bug in wxWidgets: gripper should be at the bottom of Z-order") );
2c66581e
VZ
340 ::DestroyWindow((HWND) m_hGripper);
341 m_hGripper = 0;
342 }
343}
344
345void wxDialog::ShowGripper(bool show)
346{
9a83f860 347 wxASSERT_MSG( m_hGripper, wxT("shouldn't be called if we have no gripper") );
2c66581e 348
952f8d3c
VZ
349 if ( show )
350 ResizeGripper();
351
2c66581e
VZ
352 ::ShowWindow((HWND)m_hGripper, show ? SW_SHOW : SW_HIDE);
353}
354
355void wxDialog::ResizeGripper()
356{
9a83f860 357 wxASSERT_MSG( m_hGripper, wxT("shouldn't be called if we have no gripper") );
2c66581e
VZ
358
359 HWND hwndGripper = (HWND)m_hGripper;
360
361 const wxRect rectGripper = wxRectFromRECT(wxGetWindowRect(hwndGripper));
362 const wxSize size = GetClientSize() - rectGripper.GetSize();
363
364 ::SetWindowPos(hwndGripper, HWND_BOTTOM,
365 size.x, size.y,
366 rectGripper.width, rectGripper.height,
367 SWP_NOACTIVATE);
368}
369
eddc468e
VZ
370void wxDialog::OnWindowCreate(wxWindowCreateEvent& event)
371{
372 if ( m_hGripper && IsShown() &&
373 event.GetWindow() && event.GetWindow()->GetParent() == this )
374 {
375 // Put gripper below the newly created child window
376 ::SetWindowPos((HWND)m_hGripper, HWND_BOTTOM, 0, 0, 0, 0,
377 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
378 }
379
380 event.Skip();
381}
382
40636dcb
VZ
383#endif // wxUSE_DIALOG_SIZEGRIP
384
b6c588e1
VZ
385// ----------------------------------------------------------------------------
386// wxWin event handlers
387// ----------------------------------------------------------------------------
2bda0e17 388
9ceeecb9
JS
389#ifdef __POCKETPC__
390// Responds to the OK button in a PocketPC titlebar. This
391// can be overridden, or you can change the id used for
392// sending the event, by calling SetAffirmativeId.
393bool wxDialog::DoOK()
394{
f55fee08
VZ
395 const int idOk = GetAffirmativeId();
396 if ( EmulateButtonClickIfPresent(idOk) )
9ceeecb9 397 return true;
9ceeecb9 398
f55fee08
VZ
399 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
400 event.SetEventObject(this);
401
937013e0 402 return HandleWindowEvent(event);
9ceeecb9 403}
f55fee08 404#endif // __POCKETPC__
9ceeecb9 405
ec5f0c24
JS
406#if wxUSE_TOOLBAR && defined(__POCKETPC__)
407// create main toolbar by calling OnCreateToolBar()
408wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
409{
410 m_dialogToolBar = OnCreateToolBar(style, winid, name);
411
412 return m_dialogToolBar;
413}
414
415// return a new toolbar
416wxToolBar *wxDialog::OnCreateToolBar(long style,
417 wxWindowID winid,
418 const wxString& name)
419{
420 return new wxToolMenuBar(this, winid,
421 wxDefaultPosition, wxDefaultSize,
422 style, name);
660296aa 423}
ec5f0c24 424#endif
9ceeecb9 425
42e69d6b 426// ---------------------------------------------------------------------------
f55fee08 427// dialog Windows messages processing
42e69d6b
VZ
428// ---------------------------------------------------------------------------
429
c140b7e7 430WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
42e69d6b 431{
c140b7e7 432 WXLRESULT rc = 0;
044fe836 433 bool processed = false;
42e69d6b
VZ
434
435 switch ( message )
436 {
0fc58b86
RR
437#ifdef __WXWINCE__
438 // react to pressing the OK button in the title
439 case WM_COMMAND:
ef6d716b
WS
440 {
441 switch ( LOWORD(wParam) )
0fc58b86 442 {
9ceeecb9 443#ifdef __POCKETPC__
ef6d716b 444 case IDOK:
9ceeecb9
JS
445 processed = DoOK();
446 if (!processed)
b554cf63 447 processed = !Close();
9ceeecb9
JS
448#endif
449#ifdef __SMARTPHONE__
ef6d716b
WS
450 case IDM_LEFT:
451 case IDM_RIGHT:
452 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
0fc58b86 453 break;
ef6d716b 454#endif // __SMARTPHONE__
0fc58b86
RR
455 }
456 break;
ef6d716b 457 }
044fe836 458#endif
42e69d6b
VZ
459 case WM_CLOSE:
460 // if we can't close, tell the system that we processed the
461 // message - otherwise it would close us
462 processed = !Close();
463 break;
abceee76 464
f73f67be 465 case WM_SIZE:
40636dcb 466#if wxUSE_DIALOG_SIZEGRIP
2c66581e
VZ
467 if ( m_hGripper )
468 {
469 switch ( wParam )
470 {
471 case SIZE_MAXIMIZED:
472 ShowGripper(false);
473 break;
474
475 case SIZE_RESTORED:
476 ShowGripper(true);
2c66581e
VZ
477 }
478 }
40636dcb 479#endif // wxUSE_DIALOG_SIZEGRIP
2c66581e 480
f73f67be
VZ
481 // the Windows dialogs unfortunately are not meant to be resizeable
482 // at all and their standard class doesn't include CS_[VH]REDRAW
483 // styles which means that the window is not refreshed properly
484 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
485 // help with it - so we have to refresh it manually which certainly
486 // creates flicker but at least doesn't show garbage on the screen
487 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
044fe836 488 processed = true;
e441e1f4 489 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
f73f67be 490 {
044fe836 491 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
f73f67be
VZ
492 }
493 break;
494
04ef50df 495#ifndef __WXMICROWIN__
abceee76
VZ
496 case WM_SETCURSOR:
497 // we want to override the busy cursor for modal dialogs:
498 // typically, wxBeginBusyCursor() is called and then a modal dialog
bfbd6dc1 499 // is shown, but the modal dialog shouldn't have hourglass cursor
f46f4c86 500 if ( IsModal() && wxIsBusy() )
abceee76 501 {
bfbd6dc1
VZ
502 // set our cursor for all windows (but see below)
503 wxCursor cursor = m_cursor;
504 if ( !cursor.Ok() )
505 cursor = wxCURSOR_ARROW;
abceee76 506
bfbd6dc1
VZ
507 ::SetCursor(GetHcursorOf(cursor));
508
509 // in any case, stop here and don't let wxWindow process this
510 // message (it would set the busy cursor)
044fe836 511 processed = true;
bfbd6dc1 512
044fe836 513 // but return false to tell the child window (if the event
bfbd6dc1
VZ
514 // comes from one of them and not from ourselves) that it can
515 // set its own cursor if it has one: thus, standard controls
516 // (e.g. text ctrl) still have correct cursors in a dialog
517 // invoked while wxIsBusy()
044fe836 518 rc = false;
abceee76 519 }
bfbd6dc1 520 break;
82c9f85c 521#endif // __WXMICROWIN__
42e69d6b
VZ
522 }
523
524 if ( !processed )
525 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
526
527 return rc;
528}