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