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