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