]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
Check for invalid items in generic wxDataViewCtrl::GetSelections().
[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
6757b5e3
VZ
53// ----------------------------------------------------------------------------
54// wxDialogModalData
55// ----------------------------------------------------------------------------
56
8c7f5f03
VZ
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
6757b5e3
VZ
59class wxDialogModalData
60{
61public:
8c7f5f03 62 wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { }
6757b5e3 63
8c7f5f03 64 void RunLoop()
6757b5e3 65 {
6757b5e3
VZ
66 m_evtLoop.Run();
67 }
68
69 void ExitLoop()
70 {
6757b5e3
VZ
71 m_evtLoop.Exit();
72 }
73
6757b5e3 74private:
8c7f5f03 75 wxModalEventLoop m_evtLoop;
6757b5e3
VZ
76};
77
259c43f6 78wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData)
8c7f5f03 79
b6c588e1
VZ
80// ============================================================================
81// implementation
82// ============================================================================
83
84// ----------------------------------------------------------------------------
85// wxDialog construction
86// ----------------------------------------------------------------------------
87
b0a6bb75 88void wxDialog::Init()
2bda0e17 89{
044fe836 90 m_isShown = false;
6757b5e3 91 m_modalData = NULL;
ec5f0c24
JS
92#if wxUSE_TOOLBAR && defined(__POCKETPC__)
93 m_dialogToolBar = NULL;
94#endif
40636dcb 95#if wxUSE_DIALOG_SIZEGRIP
2c66581e 96 m_hGripper = 0;
40636dcb 97#endif // wxUSE_DIALOG_SIZEGRIP
2bda0e17
KB
98}
99
b3daa5a3
VZ
100bool wxDialog::Create(wxWindow *parent,
101 wxWindowID id,
462e2437
VZ
102 const wxString& title,
103 const wxPoint& pos,
104 const wxSize& size,
105 long style,
106 const wxString& name)
2bda0e17 107{
82c9f85c 108 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
bd9d76cb 109
706bb5f9 110 // All dialogs should really have this style
b225f659 111 style |= wxTAB_TRAVERSAL;
2bda0e17 112
b225f659 113 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
044fe836 114 return false;
00233716
VZ
115
116 if ( !m_hasFont )
6f951810 117 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
e1bdd507 118
3180bc0e 119#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
ef6d716b
WS
120 SetLeftMenu(wxID_OK, _("OK"));
121#endif
ec5f0c24
JS
122#if wxUSE_TOOLBAR && defined(__POCKETPC__)
123 CreateToolBar();
124#endif
ef6d716b 125
40636dcb 126#if wxUSE_DIALOG_SIZEGRIP
952f8d3c 127 if ( HasFlag(wxRESIZE_BORDER) )
eddc468e 128 {
2c66581e
VZ
129 CreateGripper();
130
eddc468e
VZ
131 Connect(wxEVT_CREATE,
132 wxWindowCreateEventHandler(wxDialog::OnWindowCreate));
133 }
40636dcb 134#endif // wxUSE_DIALOG_SIZEGRIP
eddc468e 135
044fe836 136 return true;
2bda0e17
KB
137}
138
2bda0e17
KB
139wxDialog::~wxDialog()
140{
b0a6bb75 141 // this will also reenable all the other windows for a modal dialog
044fe836 142 Show(false);
2c66581e 143
40636dcb 144#if wxUSE_DIALOG_SIZEGRIP
2c66581e 145 DestroyGripper();
40636dcb 146#endif // wxUSE_DIALOG_SIZEGRIP
2bda0e17
KB
147}
148
b6c588e1
VZ
149// ----------------------------------------------------------------------------
150// showing the dialogs
151// ----------------------------------------------------------------------------
22cf5fec 152
b6c588e1 153bool wxDialog::Show(bool show)
2bda0e17 154{
044fe836
VZ
155 if ( show == IsShown() )
156 return false;
157
6757b5e3 158 if ( !show && m_modalData )
86ad564e 159 {
6757b5e3
VZ
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
b0a6bb75 162 // if they stay disabled Windows will activate another window (one
6757b5e3
VZ
163 // which is enabled, anyhow) when we're hidden in the base class Show()
164 // and we will lose activation
165 m_modalData->ExitLoop();
86ad564e
JS
166 }
167
044fe836 168 if ( show )
b6c588e1 169 {
3aa8e4ea
JS
170 if (CanDoLayoutAdaptation())
171 DoLayoutAdaptation();
172
044fe836
VZ
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
176 InitDialog();
b6c588e1 177 }
2bda0e17 178
044fe836
VZ
179 wxDialogBase::Show(show);
180
b6c588e1
VZ
181 if ( show )
182 {
2bc44d62
VZ
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
2b5f62a0
VZ
186 //
187 // NB: normally we should call it just the first time but doing it
188 // every time is simpler than keeping a flag
2bc44d62
VZ
189 const wxSize size = GetClientSize();
190 ::SendMessage(GetHwnd(), WM_SIZE,
191 SIZE_RESTORED, MAKELPARAM(size.x, size.y));
b6c588e1 192 }
2bda0e17 193
044fe836 194 return true;
2bda0e17
KB
195}
196
313901f3
JS
197void wxDialog::Raise()
198{
199 ::SetForegroundWindow(GetHwnd());
200}
201
f46f4c86 202// show dialog modally
a23fd0e1 203int wxDialog::ShowModal()
2bda0e17 204{
9a83f860 205 wxASSERT_MSG( !IsModal(), wxT("ShowModal() can't be called twice") );
f46f4c86 206
f46f4c86
VZ
207 Show();
208
209 // EndModal may have been called from InitDialog handler (called from
89424d9b 210 // inside Show()) and hidden the dialog back again
4b5e178a 211 if ( IsShown() )
5e1febfa 212 {
f46f4c86 213 // enter and run the modal loop
88a67391
VZ
214 wxDialogModalDataTiedPtr modalData(&m_modalData,
215 new wxDialogModalData(this));
216 modalData->RunLoop();
f46f4c86 217 }
5e1febfa 218
b6c588e1 219 return GetReturnCode();
2bda0e17
KB
220}
221
222void wxDialog::EndModal(int retCode)
223{
9a83f860 224 wxASSERT_MSG( IsModal(), wxT("EndModal() called for non modal dialog") );
f46f4c86 225
b6c588e1 226 SetReturnCode(retCode);
6a088435 227
12b58624
VZ
228 Hide();
229}
230
2c66581e
VZ
231// ----------------------------------------------------------------------------
232// wxDialog gripper handling
233// ----------------------------------------------------------------------------
234
40636dcb
VZ
235#if wxUSE_DIALOG_SIZEGRIP
236
2c66581e
VZ
237void wxDialog::SetWindowStyleFlag(long style)
238{
239 wxDialogBase::SetWindowStyleFlag(style);
240
952f8d3c 241 if ( HasFlag(wxRESIZE_BORDER) )
2c66581e
VZ
242 CreateGripper();
243 else
244 DestroyGripper();
245}
246
247void wxDialog::CreateGripper()
248{
952f8d3c 249 if ( !m_hGripper )
2c66581e 250 {
952f8d3c 251 // just create it here, it will be positioned and shown later
2c66581e
VZ
252 m_hGripper = (WXHWND)::CreateWindow
253 (
254 wxT("SCROLLBAR"),
255 wxT(""),
952f8d3c
VZ
256 WS_CHILD |
257 WS_CLIPSIBLINGS |
2c66581e
VZ
258 SBS_SIZEGRIP |
259 SBS_SIZEBOX |
260 SBS_SIZEBOXBOTTOMRIGHTALIGN,
261 0, 0, 0, 0,
262 GetHwnd(),
263 0,
264 wxGetInstance(),
265 NULL
266 );
2c66581e
VZ
267 }
268}
269
270void wxDialog::DestroyGripper()
271{
272 if ( m_hGripper )
273 {
eddc468e
VZ
274 // we used to have trouble with gripper appearing on top (and hence
275 // overdrawing) the other, real, dialog children -- check that this
f78c70f3
VZ
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() ||
c3b46177 280 ::GetWindow((HWND)m_hGripper, GW_HWNDNEXT) == 0,
9a83f860 281 wxT("Bug in wxWidgets: gripper should be at the bottom of Z-order") );
2c66581e
VZ
282 ::DestroyWindow((HWND) m_hGripper);
283 m_hGripper = 0;
284 }
285}
286
287void wxDialog::ShowGripper(bool show)
288{
9a83f860 289 wxASSERT_MSG( m_hGripper, wxT("shouldn't be called if we have no gripper") );
2c66581e 290
952f8d3c
VZ
291 if ( show )
292 ResizeGripper();
293
2c66581e
VZ
294 ::ShowWindow((HWND)m_hGripper, show ? SW_SHOW : SW_HIDE);
295}
296
297void wxDialog::ResizeGripper()
298{
9a83f860 299 wxASSERT_MSG( m_hGripper, wxT("shouldn't be called if we have no gripper") );
2c66581e
VZ
300
301 HWND hwndGripper = (HWND)m_hGripper;
302
303 const wxRect rectGripper = wxRectFromRECT(wxGetWindowRect(hwndGripper));
304 const wxSize size = GetClientSize() - rectGripper.GetSize();
305
306 ::SetWindowPos(hwndGripper, HWND_BOTTOM,
307 size.x, size.y,
308 rectGripper.width, rectGripper.height,
309 SWP_NOACTIVATE);
310}
311
eddc468e
VZ
312void wxDialog::OnWindowCreate(wxWindowCreateEvent& event)
313{
314 if ( m_hGripper && IsShown() &&
315 event.GetWindow() && event.GetWindow()->GetParent() == this )
316 {
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);
320 }
321
322 event.Skip();
323}
324
40636dcb
VZ
325#endif // wxUSE_DIALOG_SIZEGRIP
326
b6c588e1
VZ
327// ----------------------------------------------------------------------------
328// wxWin event handlers
329// ----------------------------------------------------------------------------
2bda0e17 330
9ceeecb9
JS
331#ifdef __POCKETPC__
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.
335bool wxDialog::DoOK()
336{
f55fee08
VZ
337 const int idOk = GetAffirmativeId();
338 if ( EmulateButtonClickIfPresent(idOk) )
9ceeecb9 339 return true;
9ceeecb9 340
f55fee08
VZ
341 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
342 event.SetEventObject(this);
343
937013e0 344 return HandleWindowEvent(event);
9ceeecb9 345}
f55fee08 346#endif // __POCKETPC__
9ceeecb9 347
ec5f0c24
JS
348#if wxUSE_TOOLBAR && defined(__POCKETPC__)
349// create main toolbar by calling OnCreateToolBar()
350wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
351{
352 m_dialogToolBar = OnCreateToolBar(style, winid, name);
353
354 return m_dialogToolBar;
355}
356
357// return a new toolbar
358wxToolBar *wxDialog::OnCreateToolBar(long style,
359 wxWindowID winid,
360 const wxString& name)
361{
362 return new wxToolMenuBar(this, winid,
363 wxDefaultPosition, wxDefaultSize,
364 style, name);
660296aa 365}
ec5f0c24 366#endif
9ceeecb9 367
42e69d6b 368// ---------------------------------------------------------------------------
f55fee08 369// dialog Windows messages processing
42e69d6b
VZ
370// ---------------------------------------------------------------------------
371
c140b7e7 372WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
42e69d6b 373{
c140b7e7 374 WXLRESULT rc = 0;
044fe836 375 bool processed = false;
42e69d6b
VZ
376
377 switch ( message )
378 {
0fc58b86
RR
379#ifdef __WXWINCE__
380 // react to pressing the OK button in the title
381 case WM_COMMAND:
ef6d716b
WS
382 {
383 switch ( LOWORD(wParam) )
0fc58b86 384 {
9ceeecb9 385#ifdef __POCKETPC__
ef6d716b 386 case IDOK:
9ceeecb9
JS
387 processed = DoOK();
388 if (!processed)
b554cf63 389 processed = !Close();
9ceeecb9
JS
390#endif
391#ifdef __SMARTPHONE__
ef6d716b
WS
392 case IDM_LEFT:
393 case IDM_RIGHT:
394 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
0fc58b86 395 break;
ef6d716b 396#endif // __SMARTPHONE__
0fc58b86
RR
397 }
398 break;
ef6d716b 399 }
044fe836 400#endif
42e69d6b
VZ
401 case WM_CLOSE:
402 // if we can't close, tell the system that we processed the
403 // message - otherwise it would close us
404 processed = !Close();
405 break;
abceee76 406
f73f67be 407 case WM_SIZE:
40636dcb 408#if wxUSE_DIALOG_SIZEGRIP
2c66581e
VZ
409 if ( m_hGripper )
410 {
411 switch ( wParam )
412 {
413 case SIZE_MAXIMIZED:
414 ShowGripper(false);
415 break;
416
417 case SIZE_RESTORED:
418 ShowGripper(true);
2c66581e
VZ
419 }
420 }
40636dcb 421#endif // wxUSE_DIALOG_SIZEGRIP
2c66581e 422
d13b34d3 423 // the Windows dialogs unfortunately are not meant to be resizable
f73f67be
VZ
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);
044fe836 430 processed = true;
e441e1f4 431 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
f73f67be 432 {
044fe836 433 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
f73f67be
VZ
434 }
435 break;
436
04ef50df 437#ifndef __WXMICROWIN__
abceee76
VZ
438 case WM_SETCURSOR:
439 // we want to override the busy cursor for modal dialogs:
440 // typically, wxBeginBusyCursor() is called and then a modal dialog
bfbd6dc1 441 // is shown, but the modal dialog shouldn't have hourglass cursor
f46f4c86 442 if ( IsModal() && wxIsBusy() )
abceee76 443 {
bfbd6dc1
VZ
444 // set our cursor for all windows (but see below)
445 wxCursor cursor = m_cursor;
a1b806b9 446 if ( !cursor.IsOk() )
bfbd6dc1 447 cursor = wxCURSOR_ARROW;
abceee76 448
bfbd6dc1
VZ
449 ::SetCursor(GetHcursorOf(cursor));
450
451 // in any case, stop here and don't let wxWindow process this
452 // message (it would set the busy cursor)
044fe836 453 processed = true;
bfbd6dc1 454
044fe836 455 // but return false to tell the child window (if the event
bfbd6dc1
VZ
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()
044fe836 460 rc = false;
abceee76 461 }
bfbd6dc1 462 break;
82c9f85c 463#endif // __WXMICROWIN__
42e69d6b
VZ
464 }
465
466 if ( !processed )
467 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
468
469 return rc;
470}