]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
added wxTreeCtrl::UnselectItem() and ToggleItemSelection() (includes patch 832281)
[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
dc1c4b62 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
b6c588e1
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
b6c588e1 21 #pragma implementation "dialog.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
b6c588e1 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
b6c588e1
VZ
32 #include "wx/dialog.h"
33 #include "wx/utils.h"
34 #include "wx/frame.h"
35 #include "wx/app.h"
36 #include "wx/settings.h"
37 #include "wx/intl.h"
38 #include "wx/log.h"
2bda0e17
KB
39#endif
40
41#include "wx/msw/private.h"
dbda9e86 42#include "wx/log.h"
ac8d0c11 43#include "wx/evtloop.h"
8c7f5f03 44#include "wx/ptr_scpd.h"
2bda0e17 45
c67d6888 46#if wxUSE_COMMON_DIALOGS && !defined(__WXMICROWIN__)
b6c588e1 47 #include <commdlg.h>
2bda0e17
KB
48#endif
49
b6c588e1
VZ
50// ----------------------------------------------------------------------------
51// wxWin macros
52// ----------------------------------------------------------------------------
53
ffca575c
SC
54#if wxUSE_EXTENDED_RTTI
55WX_DEFINE_FLAGS( wxDialogStyle )
56
3ff066a4 57wxBEGIN_FLAGS( wxDialogStyle )
ffca575c
SC
58 // new style border flags, we put them first to
59 // use them for streaming out
3ff066a4
SC
60 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
61 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
62 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
63 wxFLAGS_MEMBER(wxBORDER_RAISED)
64 wxFLAGS_MEMBER(wxBORDER_STATIC)
65 wxFLAGS_MEMBER(wxBORDER_NONE)
ffca575c
SC
66
67 // old style border flags
3ff066a4
SC
68 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
69 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
70 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
71 wxFLAGS_MEMBER(wxRAISED_BORDER)
72 wxFLAGS_MEMBER(wxSTATIC_BORDER)
73 wxFLAGS_MEMBER(wxNO_BORDER)
ffca575c
SC
74
75 // standard window styles
3ff066a4
SC
76 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
77 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
ffca575c
SC
78
79 // dialog styles
3ff066a4
SC
80 wxFLAGS_MEMBER(wxDIALOG_MODAL)
81 wxFLAGS_MEMBER(wxDIALOG_MODELESS)
82 wxFLAGS_MEMBER(wxNO_3D)
83 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY)
84 wxFLAGS_MEMBER(wxSTAY_ON_TOP)
85 wxFLAGS_MEMBER(wxCAPTION)
86 wxFLAGS_MEMBER(wxTHICK_FRAME)
87 wxFLAGS_MEMBER(wxSYSTEM_MENU)
88 wxFLAGS_MEMBER(wxRESIZE_BORDER)
89 wxFLAGS_MEMBER(wxRESIZE_BOX)
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
SC
97wxBEGIN_PROPERTIES_TABLE(wxDialog)
98 wxPROPERTY( Title,wxString, SetTitle, GetTitle, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
99 wxPROPERTY_FLAGS( WindowStyle , wxDialogStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
100wxEND_PROPERTIES_TABLE()
ffca575c 101
3ff066a4
SC
102wxBEGIN_HANDLERS_TABLE(wxDialog)
103wxEND_HANDLERS_TABLE()
ffca575c 104
3ff066a4 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
138e1502 111BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
1b6452df
VZ
112 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
113 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
114 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
b6c588e1 115
1b6452df 116 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
b6c588e1 117
1b6452df
VZ
118 EVT_CLOSE(wxDialog::OnCloseWindow)
119END_EVENT_TABLE()
2bda0e17 120
6757b5e3
VZ
121// ----------------------------------------------------------------------------
122// wxDialogModalData
123// ----------------------------------------------------------------------------
124
8c7f5f03
VZ
125// this is simply a container for any data we need to implement modality which
126// allows us to avoid changing wxDialog each time the implementation changes
6757b5e3
VZ
127class wxDialogModalData
128{
129public:
8c7f5f03 130 wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { }
6757b5e3 131
8c7f5f03 132 void RunLoop()
6757b5e3 133 {
6757b5e3
VZ
134 m_evtLoop.Run();
135 }
136
137 void ExitLoop()
138 {
6757b5e3
VZ
139 m_evtLoop.Exit();
140 }
141
6757b5e3 142private:
8c7f5f03 143 wxModalEventLoop m_evtLoop;
6757b5e3
VZ
144};
145
8c7f5f03
VZ
146wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData);
147
b6c588e1
VZ
148// ============================================================================
149// implementation
150// ============================================================================
151
152// ----------------------------------------------------------------------------
153// wxDialog construction
154// ----------------------------------------------------------------------------
155
b0a6bb75 156void wxDialog::Init()
2bda0e17 157{
52a07708 158 m_oldFocus = (wxWindow *)NULL;
b6c588e1 159 m_isShown = FALSE;
6757b5e3 160 m_modalData = NULL;
1a33ac8f 161 m_endModalCalled = FALSE;
2bda0e17
KB
162}
163
b3daa5a3
VZ
164bool wxDialog::Create(wxWindow *parent,
165 wxWindowID id,
462e2437
VZ
166 const wxString& title,
167 const wxPoint& pos,
168 const wxSize& size,
169 long style,
170 const wxString& name)
2bda0e17 171{
82c9f85c 172 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
bd9d76cb 173
b225f659 174 // save focus before doing anything which can potentially change it
82c9f85c 175 m_oldFocus = FindFocus();
462e2437 176
706bb5f9 177 // All dialogs should really have this style
b225f659 178 style |= wxTAB_TRAVERSAL;
2bda0e17 179
b225f659 180 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
462e2437 181 return FALSE;
00233716
VZ
182
183 if ( !m_hasFont )
6f951810 184 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
e1bdd507 185
00233716
VZ
186 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
187
462e2437 188 return TRUE;
2bda0e17
KB
189}
190
debe6624 191void wxDialog::SetModal(bool flag)
2bda0e17 192{
b6c588e1
VZ
193 if ( flag )
194 {
195 m_windowStyle |= wxDIALOG_MODAL;
196
197 wxModelessWindows.DeleteObject(this);
198 }
199 else
200 {
201 m_windowStyle &= ~wxDIALOG_MODAL;
202
203 wxModelessWindows.Append(this);
204 }
2bda0e17
KB
205}
206
207wxDialog::~wxDialog()
208{
b6c588e1 209 m_isBeingDeleted = TRUE;
2bda0e17 210
b0a6bb75 211 // this will also reenable all the other windows for a modal dialog
b6c588e1 212 Show(FALSE);
2bda0e17
KB
213}
214
b6c588e1
VZ
215// ----------------------------------------------------------------------------
216// showing the dialogs
217// ----------------------------------------------------------------------------
22cf5fec
VZ
218
219bool wxDialog::IsModal() const
220{
b6c588e1 221 return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0;
2bda0e17
KB
222}
223
b6c588e1 224bool wxDialog::IsModalShowing() const
2bda0e17 225{
6757b5e3 226 return m_modalData != NULL;
2bda0e17
KB
227}
228
a543e3ce
VZ
229wxWindow *wxDialog::FindSuitableParent() const
230{
231 // first try to use the currently active window
232 HWND hwndFg = ::GetForegroundWindow();
233 wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg)
234 : NULL;
235 if ( !parent )
236 {
237 // next try the main app window
238 parent = wxTheApp->GetTopWindow();
239 }
240
241 // finally, check if the parent we found is really suitable
242 if ( !parent || parent == (wxWindow *)this || !parent->IsShown() )
243 {
244 // don't use this one
245 parent = NULL;
246 }
247
248 return parent;
249}
250
b6c588e1 251void wxDialog::DoShowModal()
2bda0e17 252{
b6c588e1 253 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
f6bcfd97 254 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
b6c588e1 255
b6c588e1
VZ
256 wxWindow *parent = GetParent();
257
f6bcfd97 258 wxWindow* oldFocus = m_oldFocus;
b6c588e1 259
f6bcfd97
BP
260 // We have to remember the HWND because we need to check
261 // the HWND still exists (oldFocus can be garbage when the dialog
262 // exits, if it has been destroyed)
263 HWND hwndOldFocus = 0;
264 if (oldFocus)
265 hwndOldFocus = (HWND) oldFocus->GetHWND();
b6c588e1 266
b0a6bb75
VZ
267 // remember where the focus was
268 if ( !oldFocus )
b6c588e1 269 {
b0a6bb75
VZ
270 oldFocus = parent;
271 if ( parent )
272 hwndOldFocus = GetHwndOf(parent);
273 }
f6bcfd97 274
b0a6bb75 275 // enter the modal loop
8c7f5f03
VZ
276 {
277 wxDialogModalDataTiedPtr modalData(&m_modalData,
278 new wxDialogModalData(this));
279 modalData->RunLoop();
280 }
2b5f62a0 281
b6c588e1 282 // and restore focus
f6bcfd97
BP
283 // Note that this code MUST NOT access the dialog object's data
284 // in case the object has been deleted (which will be the case
285 // for a modal dialog that has been destroyed before calling EndModal).
286 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
b6c588e1 287 {
f6bcfd97
BP
288 // This is likely to prove that the object still exists
289 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
290 oldFocus->SetFocus();
b6c588e1 291 }
2bda0e17
KB
292}
293
b6c588e1 294bool wxDialog::Show(bool show)
2bda0e17 295{
6757b5e3 296 if ( !show && m_modalData )
86ad564e 297 {
6757b5e3
VZ
298 // we need to do this before calling wxDialogBase version because if we
299 // had disabled other app windows, they must be reenabled right now as
b0a6bb75 300 // if they stay disabled Windows will activate another window (one
6757b5e3
VZ
301 // which is enabled, anyhow) when we're hidden in the base class Show()
302 // and we will lose activation
303 m_modalData->ExitLoop();
86ad564e
JS
304 }
305
abceee76
VZ
306 // ShowModal() may be called for already shown dialog
307 if ( !wxDialogBase::Show(show) && !(show && IsModal()) )
b6c588e1
VZ
308 {
309 // nothing to do
310 return FALSE;
311 }
2bda0e17 312
b6c588e1
VZ
313 if ( show )
314 {
2b5f62a0
VZ
315 // dialogs don't get WM_SIZE message after creation unlike most (all?)
316 // other windows and so could start their life non laid out correctly
317 // if we didn't call Layout() from here
318 //
319 // NB: normally we should call it just the first time but doing it
320 // every time is simpler than keeping a flag
321 Layout();
322
6757b5e3 323 // this usually will result in TransferDataToWindow() being called
b6c588e1
VZ
324 InitDialog();
325 }
2bda0e17 326
1a33ac8f
JS
327 // EndModal may have been called from InitDialog handler,
328 // which would cause an infinite loop if we didn't take it
329 // into account
330 if ( show && IsModal() && !m_endModalCalled )
b6c588e1 331 {
6757b5e3
VZ
332 // modal dialog needs a parent window, so try to find one
333 if ( !GetParent() )
b6c588e1 334 {
6757b5e3 335 m_parent = FindSuitableParent();
b6c588e1 336 }
875c17fa 337
6757b5e3 338 DoShowModal();
b6c588e1 339 }
2bda0e17 340
b6c588e1 341 return TRUE;
2bda0e17
KB
342}
343
313901f3
JS
344void wxDialog::Raise()
345{
346 ::SetForegroundWindow(GetHwnd());
347}
348
f6bcfd97 349// a special version for Show(TRUE) for modal dialogs which returns return code
a23fd0e1 350int wxDialog::ShowModal()
2bda0e17 351{
1a33ac8f 352 m_endModalCalled = FALSE;
f6bcfd97 353 if ( !IsModal() )
5e1febfa 354 {
f6bcfd97 355 SetModal(TRUE);
5e1febfa
VZ
356 }
357
b6c588e1 358 Show(TRUE);
5e1febfa 359
b6c588e1 360 return GetReturnCode();
2bda0e17
KB
361}
362
b6c588e1
VZ
363// NB: this function (surprizingly) may be called for both modal and modeless
364// dialogs and should work for both of them
2bda0e17
KB
365void wxDialog::EndModal(int retCode)
366{
1a33ac8f 367 m_endModalCalled = TRUE;
b6c588e1 368 SetReturnCode(retCode);
6a088435 369
b6c588e1 370 Show(FALSE);
2bda0e17
KB
371}
372
b6c588e1
VZ
373// ----------------------------------------------------------------------------
374// wxWin event handlers
375// ----------------------------------------------------------------------------
2bda0e17
KB
376
377// Standard buttons
33ac7e6f 378void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
2bda0e17 379{
dc1c4b62
VZ
380 if ( Validate() && TransferDataFromWindow() )
381 {
b6c588e1 382 EndModal(wxID_OK);
dc1c4b62 383 }
2bda0e17
KB
384}
385
33ac7e6f 386void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
2bda0e17 387{
b6c588e1
VZ
388 if ( Validate() )
389 TransferDataFromWindow();
390
391 // TODO probably need to disable the Apply button until things change again
2bda0e17
KB
392}
393
33ac7e6f 394void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
2bda0e17 395{
b6c588e1 396 EndModal(wxID_CANCEL);
2bda0e17
KB
397}
398
33ac7e6f 399void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
2bda0e17 400{
b6c588e1 401 // We'll send a Cancel message by default, which may close the dialog.
e3065973
JS
402 // Check for looping if the Cancel event handler calls Close().
403
404 // Note that if a cancel button and handler aren't present in the dialog,
405 // nothing will happen when you close the dialog via the window manager, or
b6c588e1
VZ
406 // via Close(). We wouldn't want to destroy the dialog by default, since
407 // the dialog may have been created on the stack. However, this does mean
408 // that calling dialog->Close() won't delete the dialog unless the handler
409 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
410 // destroy the dialog. The default OnCancel (above) simply ends a modal
411 // dialog, and hides a modeless dialog.
412
413 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
414 // lists here? don't dare to change it now, but should be done later!
2bda0e17 415 static wxList closing;
bd9d76cb 416
2bda0e17 417 if ( closing.Member(this) )
e3065973 418 return;
bd9d76cb 419
2bda0e17 420 closing.Append(this);
bd9d76cb 421
387a3b02
JS
422 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
423 cancelEvent.SetEventObject( this );
e3065973 424 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
2bda0e17
KB
425
426 closing.DeleteObject(this);
e3065973 427}
2bda0e17 428
33ac7e6f 429void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
2bda0e17 430{
1f112209 431#if wxUSE_CTL3D
b6c588e1 432 Ctl3dColorChange();
2bda0e17 433#else
a756f210 434 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
b6c588e1 435 Refresh();
2bda0e17 436#endif
68ad65f8 437}
42e69d6b
VZ
438
439// ---------------------------------------------------------------------------
440// dialog window proc
441// ---------------------------------------------------------------------------
442
443long wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
444{
445 long rc = 0;
446 bool processed = FALSE;
447
448 switch ( message )
449 {
450 case WM_CLOSE:
451 // if we can't close, tell the system that we processed the
452 // message - otherwise it would close us
453 processed = !Close();
454 break;
abceee76 455
f73f67be
VZ
456 case WM_SIZE:
457 // the Windows dialogs unfortunately are not meant to be resizeable
458 // at all and their standard class doesn't include CS_[VH]REDRAW
459 // styles which means that the window is not refreshed properly
460 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
461 // help with it - so we have to refresh it manually which certainly
462 // creates flicker but at least doesn't show garbage on the screen
463 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
464 processed = TRUE;
e441e1f4 465 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
f73f67be 466 {
6b6ea919 467 ::InvalidateRect(GetHwnd(), NULL, FALSE /* erase bg */);
f73f67be
VZ
468 }
469 break;
470
04ef50df 471#ifndef __WXMICROWIN__
abceee76
VZ
472 case WM_SETCURSOR:
473 // we want to override the busy cursor for modal dialogs:
474 // typically, wxBeginBusyCursor() is called and then a modal dialog
bfbd6dc1 475 // is shown, but the modal dialog shouldn't have hourglass cursor
d1477745 476 if ( IsModalShowing() && wxIsBusy() )
abceee76 477 {
bfbd6dc1
VZ
478 // set our cursor for all windows (but see below)
479 wxCursor cursor = m_cursor;
480 if ( !cursor.Ok() )
481 cursor = wxCURSOR_ARROW;
abceee76 482
bfbd6dc1
VZ
483 ::SetCursor(GetHcursorOf(cursor));
484
485 // in any case, stop here and don't let wxWindow process this
486 // message (it would set the busy cursor)
abceee76 487 processed = TRUE;
bfbd6dc1
VZ
488
489 // but return FALSE to tell the child window (if the event
490 // comes from one of them and not from ourselves) that it can
491 // set its own cursor if it has one: thus, standard controls
492 // (e.g. text ctrl) still have correct cursors in a dialog
493 // invoked while wxIsBusy()
494 rc = FALSE;
abceee76 495 }
bfbd6dc1 496 break;
82c9f85c 497#endif // __WXMICROWIN__
42e69d6b
VZ
498 }
499
500 if ( !processed )
501 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
502
503 return rc;
504}
b6c588e1
VZ
505
506#if wxUSE_CTL3D
507
508// Define for each class of dialog and control
509WXHBRUSH wxDialog::OnCtlColor(WXHDC WXUNUSED(pDC),
510 WXHWND WXUNUSED(pWnd),
511 WXUINT WXUNUSED(nCtlColor),
512 WXUINT message,
513 WXWPARAM wParam,
514 WXLPARAM lParam)
515{
516 return (WXHBRUSH)Ctl3dCtlColorEx(message, wParam, lParam);
517}
518
519#endif // wxUSE_CTL3D
520