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