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