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