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