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