]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
macro naming changes, delegate api calls getting property info, object info exposed...
[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
ffca575c
SC
71#if wxUSE_EXTENDED_RTTI
72WX_DEFINE_FLAGS( wxDialogStyle )
73
74WX_BEGIN_FLAGS( wxDialogStyle )
75 // new style border flags, we put them first to
76 // use them for streaming out
77 WX_FLAGS_MEMBER(wxBORDER_SIMPLE)
78 WX_FLAGS_MEMBER(wxBORDER_SUNKEN)
79 WX_FLAGS_MEMBER(wxBORDER_DOUBLE)
80 WX_FLAGS_MEMBER(wxBORDER_RAISED)
81 WX_FLAGS_MEMBER(wxBORDER_STATIC)
82 WX_FLAGS_MEMBER(wxBORDER_NONE)
83
84 // old style border flags
85 WX_FLAGS_MEMBER(wxSIMPLE_BORDER)
86 WX_FLAGS_MEMBER(wxSUNKEN_BORDER)
87 WX_FLAGS_MEMBER(wxDOUBLE_BORDER)
88 WX_FLAGS_MEMBER(wxRAISED_BORDER)
89 WX_FLAGS_MEMBER(wxSTATIC_BORDER)
90 WX_FLAGS_MEMBER(wxNO_BORDER)
91
92 // standard window styles
93 WX_FLAGS_MEMBER(wxTAB_TRAVERSAL)
94 WX_FLAGS_MEMBER(wxCLIP_CHILDREN)
95
96 // dialog styles
97 WX_FLAGS_MEMBER(wxDIALOG_MODAL)
98 WX_FLAGS_MEMBER(wxDIALOG_MODELESS)
99 WX_FLAGS_MEMBER(wxNO_3D)
100 WX_FLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY)
101 WX_FLAGS_MEMBER(wxSTAY_ON_TOP)
102 WX_FLAGS_MEMBER(wxCAPTION)
103 WX_FLAGS_MEMBER(wxTHICK_FRAME)
104 WX_FLAGS_MEMBER(wxSYSTEM_MENU)
105 WX_FLAGS_MEMBER(wxRESIZE_BORDER)
106 WX_FLAGS_MEMBER(wxRESIZE_BOX)
107 WX_FLAGS_MEMBER(wxCLOSE_BOX)
108 WX_FLAGS_MEMBER(wxMAXIMIZE_BOX)
109 WX_FLAGS_MEMBER(wxMINIMIZE_BOX)
110WX_END_FLAGS( wxDialogStyle )
111
112IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog, wxTopLevelWindow,"wx/dialog.h")
113
114WX_BEGIN_PROPERTIES_TABLE(wxDialog)
115 WX_PROPERTY( Title,wxString, SetTitle, GetTitle, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
116 WX_PROPERTY_FLAGS( WindowStyle , wxDialogStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
117WX_END_PROPERTIES_TABLE()
118
119WX_BEGIN_HANDLERS_TABLE(wxDialog)
120WX_END_HANDLERS_TABLE()
121
122WX_CONSTRUCTOR_6( wxDialog , wxWindow* , Parent , wxWindowID , Id , wxString , Title , wxPoint , Position , wxSize , Size , long , WindowStyle)
123
124#else
82c9f85c 125IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
ffca575c 126#endif
066f1b7a 127
138e1502 128BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
1b6452df
VZ
129 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
130 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
131 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
b6c588e1 132
1b6452df 133 EVT_CHAR_HOOK(wxDialog::OnCharHook)
b6c588e1 134
1b6452df 135 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged)
b6c588e1 136
1b6452df
VZ
137 EVT_CLOSE(wxDialog::OnCloseWindow)
138END_EVENT_TABLE()
2bda0e17 139
b6c588e1
VZ
140// ============================================================================
141// implementation
142// ============================================================================
143
144// ----------------------------------------------------------------------------
145// wxDialog construction
146// ----------------------------------------------------------------------------
147
b0a6bb75 148void wxDialog::Init()
2bda0e17 149{
52a07708 150 m_oldFocus = (wxWindow *)NULL;
b0a6bb75 151
b6c588e1 152 m_isShown = FALSE;
2bda0e17 153
b0a6bb75
VZ
154 m_windowDisabler = (wxWindowDisabler *)NULL;
155
a756f210 156 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
2bda0e17
KB
157}
158
b3daa5a3
VZ
159bool wxDialog::Create(wxWindow *parent,
160 wxWindowID id,
462e2437
VZ
161 const wxString& title,
162 const wxPoint& pos,
163 const wxSize& size,
164 long style,
165 const wxString& name)
2bda0e17 166{
b0a6bb75
VZ
167 Init();
168
82c9f85c 169 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
bd9d76cb 170
b225f659 171 // save focus before doing anything which can potentially change it
82c9f85c 172 m_oldFocus = FindFocus();
462e2437 173
706bb5f9 174 // All dialogs should really have this style
b225f659 175 style |= wxTAB_TRAVERSAL;
2bda0e17 176
b225f659 177 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
462e2437 178 return FALSE;
2bda0e17 179
a756f210 180 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
e1bdd507 181
462e2437 182 return TRUE;
2bda0e17
KB
183}
184
debe6624 185void wxDialog::SetModal(bool flag)
2bda0e17 186{
b6c588e1
VZ
187 if ( flag )
188 {
189 m_windowStyle |= wxDIALOG_MODAL;
190
191 wxModelessWindows.DeleteObject(this);
192 }
193 else
194 {
195 m_windowStyle &= ~wxDIALOG_MODAL;
196
197 wxModelessWindows.Append(this);
198 }
2bda0e17
KB
199}
200
201wxDialog::~wxDialog()
202{
b6c588e1 203 m_isBeingDeleted = TRUE;
2bda0e17 204
b0a6bb75 205 // this will also reenable all the other windows for a modal dialog
b6c588e1 206 Show(FALSE);
2bda0e17
KB
207}
208
b6c588e1
VZ
209// ----------------------------------------------------------------------------
210// kbd handling
211// ----------------------------------------------------------------------------
212
2bda0e17
KB
213// By default, pressing escape cancels the dialog
214void wxDialog::OnCharHook(wxKeyEvent& event)
215{
cbc66a27 216 if (GetHWND())
2bda0e17 217 {
cbc66a27
VZ
218 // "Esc" works as an accelerator for the "Cancel" button, but it
219 // shouldn't close the dialog which doesn't have any cancel button
220 if ( (event.m_keyCode == WXK_ESCAPE) && FindWindow(wxID_CANCEL) )
221 {
222 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
223 cancelEvent.SetEventObject( this );
224 GetEventHandler()->ProcessEvent(cancelEvent);
225
226 // ensure that there is another message for this window so the
227 // ShowModal loop will exit and won't get stuck in GetMessage().
228 ::PostMessage(GetHwnd(), WM_NULL, 0, 0);
229
230 return;
231 }
2bda0e17 232 }
cbc66a27
VZ
233
234 // We didn't process this event.
235 event.Skip();
2bda0e17
KB
236}
237
b6c588e1
VZ
238// ----------------------------------------------------------------------------
239// showing the dialogs
240// ----------------------------------------------------------------------------
22cf5fec
VZ
241
242bool wxDialog::IsModal() const
243{
b6c588e1 244 return (GetWindowStyleFlag() & wxDIALOG_MODAL) != 0;
2bda0e17
KB
245}
246
b6c588e1 247bool wxDialog::IsModalShowing() const
2bda0e17 248{
dfa4a244 249 return wxModalDialogs.Find(wxConstCast(this, wxDialog)) != NULL;
2bda0e17
KB
250}
251
a543e3ce
VZ
252wxWindow *wxDialog::FindSuitableParent() const
253{
254 // first try to use the currently active window
255 HWND hwndFg = ::GetForegroundWindow();
256 wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg)
257 : NULL;
258 if ( !parent )
259 {
260 // next try the main app window
261 parent = wxTheApp->GetTopWindow();
262 }
263
264 // finally, check if the parent we found is really suitable
265 if ( !parent || parent == (wxWindow *)this || !parent->IsShown() )
266 {
267 // don't use this one
268 parent = NULL;
269 }
270
271 return parent;
272}
273
b6c588e1 274void wxDialog::DoShowModal()
2bda0e17 275{
b6c588e1 276 wxCHECK_RET( !IsModalShowing(), _T("DoShowModal() called twice") );
f6bcfd97 277 wxCHECK_RET( IsModal(), _T("can't DoShowModal() modeless dialog") );
b6c588e1
VZ
278
279 wxModalDialogs.Append(this);
280
281 wxWindow *parent = GetParent();
282
f6bcfd97 283 wxWindow* oldFocus = m_oldFocus;
b6c588e1 284
f6bcfd97
BP
285 // We have to remember the HWND because we need to check
286 // the HWND still exists (oldFocus can be garbage when the dialog
287 // exits, if it has been destroyed)
288 HWND hwndOldFocus = 0;
289 if (oldFocus)
290 hwndOldFocus = (HWND) oldFocus->GetHWND();
b6c588e1 291
b0a6bb75
VZ
292 // remember where the focus was
293 if ( !oldFocus )
b6c588e1 294 {
b0a6bb75
VZ
295 oldFocus = parent;
296 if ( parent )
297 hwndOldFocus = GetHwndOf(parent);
298 }
f6bcfd97 299
b0a6bb75
VZ
300 // disable all other app windows
301 wxASSERT_MSG( !m_windowDisabler, _T("disabling windows twice?") );
f6bcfd97 302
b0a6bb75
VZ
303 m_windowDisabler = new wxWindowDisabler(this);
304
2b5f62a0
VZ
305 // before entering the modal loop, reset the "is in OnIdle()" flag (see
306 // comment in app.cpp)
307 extern bool wxIsInOnIdleFlag;
308 bool wasInOnIdle = wxIsInOnIdleFlag;
309 wxIsInOnIdleFlag = FALSE;
310
b0a6bb75
VZ
311 // enter the modal loop
312 while ( IsModalShowing() )
313 {
b6c588e1 314#if wxUSE_THREADS
b0a6bb75 315 wxMutexGuiLeaveOrEnter();
b6c588e1
VZ
316#endif // wxUSE_THREADS
317
b0a6bb75
VZ
318 while ( !wxTheApp->Pending() && wxTheApp->ProcessIdle() )
319 ;
b6c588e1 320
b0a6bb75
VZ
321 // a message came or no more idle processing to do
322 wxTheApp->DoMessage();
b6c588e1
VZ
323 }
324
2b5f62a0
VZ
325 wxIsInOnIdleFlag = wasInOnIdle;
326
b6c588e1 327 // and restore focus
f6bcfd97
BP
328 // Note that this code MUST NOT access the dialog object's data
329 // in case the object has been deleted (which will be the case
330 // for a modal dialog that has been destroyed before calling EndModal).
331 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
b6c588e1 332 {
f6bcfd97
BP
333 // This is likely to prove that the object still exists
334 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
335 oldFocus->SetFocus();
b6c588e1 336 }
2bda0e17
KB
337}
338
b6c588e1 339bool wxDialog::Show(bool show)
2bda0e17 340{
abceee76 341 if ( !show )
86ad564e 342 {
b0a6bb75
VZ
343 // if we had disabled other app windows, reenable them back now because
344 // if they stay disabled Windows will activate another window (one
345 // which is enabled, anyhow) and we will lose activation
346 if ( m_windowDisabler )
86ad564e 347 {
b0a6bb75
VZ
348 delete m_windowDisabler;
349 m_windowDisabler = NULL;
86ad564e
JS
350 }
351 }
352
abceee76
VZ
353 // ShowModal() may be called for already shown dialog
354 if ( !wxDialogBase::Show(show) && !(show && IsModal()) )
b6c588e1
VZ
355 {
356 // nothing to do
357 return FALSE;
358 }
2bda0e17 359
b6c588e1
VZ
360 if ( show )
361 {
2b5f62a0
VZ
362 // dialogs don't get WM_SIZE message after creation unlike most (all?)
363 // other windows and so could start their life non laid out correctly
364 // if we didn't call Layout() from here
365 //
366 // NB: normally we should call it just the first time but doing it
367 // every time is simpler than keeping a flag
368 Layout();
369
b6c588e1
VZ
370 // usually will result in TransferDataToWindow() being called
371 InitDialog();
372 }
2bda0e17 373
b6c588e1
VZ
374 if ( IsModal() )
375 {
376 if ( show )
377 {
f6bcfd97
BP
378 // modal dialog needs a parent window, so try to find one
379 if ( !GetParent() )
380 {
a543e3ce 381 m_parent = FindSuitableParent();
f6bcfd97
BP
382 }
383
b6c588e1
VZ
384 DoShowModal();
385 }
386 else // end of modal dialog
387 {
388 // this will cause IsModalShowing() return FALSE and our local
389 // message loop will terminate
390 wxModalDialogs.DeleteObject(this);
875c17fa
VZ
391
392 // ensure that there is another message for this window so the
393 // ShowModal loop will exit and won't get stuck in GetMessage().
394 ::PostMessage(GetHwnd(), WM_NULL, 0, 0);
b6c588e1
VZ
395 }
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