]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
Added intermediate state (m_showOnIdle) indicating that
[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
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
b6c588e1 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
fdf565fe
WS
27#include "wx/dialog.h"
28
2bda0e17 29#ifndef WX_PRECOMP
b6c588e1
VZ
30 #include "wx/utils.h"
31 #include "wx/frame.h"
32 #include "wx/app.h"
95e92d90 33 #include "wx/button.h"
b6c588e1
VZ
34 #include "wx/settings.h"
35 #include "wx/intl.h"
36 #include "wx/log.h"
4e3e485b 37 #include "wx/toolbar.h"
2bda0e17
KB
38#endif
39
40#include "wx/msw/private.h"
ac8d0c11 41#include "wx/evtloop.h"
8c7f5f03 42#include "wx/ptr_scpd.h"
2bda0e17 43
660296aa 44#include "wx/msw/wrapcdlg.h"
2bda0e17 45
3180bc0e 46#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
ef6d716b 47 #include "wx/msw/wince/resources.h"
3180bc0e 48#endif // __SMARTPHONE__ && __WXWINCE__
ef6d716b 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)
044fe836 66
ffca575c 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)
1c067fe3 83#if WXWIN_COMPATIBILITY_2_6
3ff066a4 84 wxFLAGS_MEMBER(wxTHICK_FRAME)
1c067fe3 85#endif // WXWIN_COMPATIBILITY_2_6
3ff066a4
SC
86 wxFLAGS_MEMBER(wxSYSTEM_MENU)
87 wxFLAGS_MEMBER(wxRESIZE_BORDER)
1c067fe3 88#if WXWIN_COMPATIBILITY_2_6
3ff066a4 89 wxFLAGS_MEMBER(wxRESIZE_BOX)
1c067fe3 90#endif // WXWIN_COMPATIBILITY_2_6
3ff066a4
SC
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
6757b5e3
VZ
112// ----------------------------------------------------------------------------
113// wxDialogModalData
114// ----------------------------------------------------------------------------
115
8c7f5f03
VZ
116// this is simply a container for any data we need to implement modality which
117// allows us to avoid changing wxDialog each time the implementation changes
6757b5e3
VZ
118class wxDialogModalData
119{
120public:
8c7f5f03 121 wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { }
6757b5e3 122
8c7f5f03 123 void RunLoop()
6757b5e3 124 {
6757b5e3
VZ
125 m_evtLoop.Run();
126 }
127
128 void ExitLoop()
129 {
6757b5e3
VZ
130 m_evtLoop.Exit();
131 }
132
6757b5e3 133private:
8c7f5f03 134 wxModalEventLoop m_evtLoop;
6757b5e3
VZ
135};
136
259c43f6 137wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData)
8c7f5f03 138
b6c588e1
VZ
139// ============================================================================
140// implementation
141// ============================================================================
142
143// ----------------------------------------------------------------------------
144// wxDialog construction
145// ----------------------------------------------------------------------------
146
b0a6bb75 147void wxDialog::Init()
2bda0e17 148{
52a07708 149 m_oldFocus = (wxWindow *)NULL;
044fe836 150 m_isShown = false;
6757b5e3 151 m_modalData = NULL;
044fe836 152 m_endModalCalled = false;
ec5f0c24
JS
153#if wxUSE_TOOLBAR && defined(__POCKETPC__)
154 m_dialogToolBar = NULL;
155#endif
2bda0e17
KB
156}
157
b3daa5a3
VZ
158bool wxDialog::Create(wxWindow *parent,
159 wxWindowID id,
462e2437
VZ
160 const wxString& title,
161 const wxPoint& pos,
162 const wxSize& size,
163 long style,
164 const wxString& name)
2bda0e17 165{
82c9f85c 166 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
bd9d76cb 167
b225f659 168 // save focus before doing anything which can potentially change it
82c9f85c 169 m_oldFocus = FindFocus();
462e2437 170
706bb5f9 171 // All dialogs should really have this style
b225f659 172 style |= wxTAB_TRAVERSAL;
2bda0e17 173
b225f659 174 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
044fe836 175 return false;
00233716
VZ
176
177 if ( !m_hasFont )
6f951810 178 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
e1bdd507 179
3180bc0e 180#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
ef6d716b
WS
181 SetLeftMenu(wxID_OK, _("OK"));
182#endif
ec5f0c24
JS
183#if wxUSE_TOOLBAR && defined(__POCKETPC__)
184 CreateToolBar();
185#endif
ef6d716b 186
044fe836 187 return true;
2bda0e17
KB
188}
189
40ff126a
WS
190#if WXWIN_COMPATIBILITY_2_6
191
f46f4c86
VZ
192// deprecated ctor
193wxDialog::wxDialog(wxWindow *parent,
194 const wxString& title,
195 bool WXUNUSED(modal),
196 int x,
197 int y,
198 int w,
199 int h,
200 long style,
201 const wxString& name)
2bda0e17 202{
f46f4c86
VZ
203 Init();
204
d71cc120 205 Create(parent, wxID_ANY, title, wxPoint(x, y), wxSize(w, h), style, name);
f46f4c86
VZ
206}
207
208void wxDialog::SetModal(bool WXUNUSED(flag))
209{
210 // nothing to do, obsolete method
2bda0e17
KB
211}
212
40ff126a
WS
213#endif // WXWIN_COMPATIBILITY_2_6
214
2bda0e17
KB
215wxDialog::~wxDialog()
216{
044fe836 217 m_isBeingDeleted = true;
2bda0e17 218
b0a6bb75 219 // this will also reenable all the other windows for a modal dialog
044fe836 220 Show(false);
2bda0e17
KB
221}
222
b6c588e1
VZ
223// ----------------------------------------------------------------------------
224// showing the dialogs
225// ----------------------------------------------------------------------------
22cf5fec 226
40ff126a
WS
227#if WXWIN_COMPATIBILITY_2_6
228
b6c588e1 229bool wxDialog::IsModalShowing() const
2bda0e17 230{
f46f4c86 231 return IsModal();
2bda0e17
KB
232}
233
40ff126a
WS
234#endif // WXWIN_COMPATIBILITY_2_6
235
a543e3ce
VZ
236wxWindow *wxDialog::FindSuitableParent() const
237{
238 // first try to use the currently active window
239 HWND hwndFg = ::GetForegroundWindow();
240 wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg)
241 : NULL;
242 if ( !parent )
243 {
244 // next try the main app window
245 parent = wxTheApp->GetTopWindow();
246 }
247
248 // finally, check if the parent we found is really suitable
249 if ( !parent || parent == (wxWindow *)this || !parent->IsShown() )
250 {
251 // don't use this one
252 parent = NULL;
253 }
254
255 return parent;
256}
257
b6c588e1 258bool wxDialog::Show(bool show)
2bda0e17 259{
044fe836
VZ
260 if ( show == IsShown() )
261 return false;
262
6757b5e3 263 if ( !show && m_modalData )
86ad564e 264 {
6757b5e3
VZ
265 // we need to do this before calling wxDialogBase version because if we
266 // had disabled other app windows, they must be reenabled right now as
b0a6bb75 267 // if they stay disabled Windows will activate another window (one
6757b5e3
VZ
268 // which is enabled, anyhow) when we're hidden in the base class Show()
269 // and we will lose activation
270 m_modalData->ExitLoop();
86ad564e
JS
271 }
272
044fe836 273 if ( show )
b6c588e1 274 {
044fe836
VZ
275 // this usually will result in TransferDataToWindow() being called
276 // which will change the controls values so do it before showing as
277 // otherwise we could have some flicker
278 InitDialog();
b6c588e1 279 }
2bda0e17 280
044fe836
VZ
281 wxDialogBase::Show(show);
282
b6c588e1
VZ
283 if ( show )
284 {
2b5f62a0
VZ
285 // dialogs don't get WM_SIZE message after creation unlike most (all?)
286 // other windows and so could start their life non laid out correctly
287 // if we didn't call Layout() from here
288 //
289 // NB: normally we should call it just the first time but doing it
290 // every time is simpler than keeping a flag
291 Layout();
b6c588e1 292 }
2bda0e17 293
044fe836 294 return true;
2bda0e17
KB
295}
296
313901f3
JS
297void wxDialog::Raise()
298{
299 ::SetForegroundWindow(GetHwnd());
300}
301
f46f4c86 302// show dialog modally
a23fd0e1 303int wxDialog::ShowModal()
2bda0e17 304{
f46f4c86
VZ
305 wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") );
306
044fe836 307 m_endModalCalled = false;
f46f4c86
VZ
308
309 Show();
310
311 // EndModal may have been called from InitDialog handler (called from
312 // inside Show()), which would cause an infinite loop if we didn't take it
313 // into account
314 if ( !m_endModalCalled )
5e1febfa 315 {
f46f4c86
VZ
316 // modal dialog needs a parent window, so try to find one
317 wxWindow *parent = GetParent();
318 if ( !parent )
319 {
320 parent = FindSuitableParent();
321 }
5e1febfa 322
f46f4c86
VZ
323 // remember where the focus was
324 wxWindow *oldFocus = m_oldFocus;
325 if ( !oldFocus )
326 {
327 // VZ: do we really want to do this?
328 oldFocus = parent;
329 }
330
331 // We have to remember the HWND because we need to check
332 // the HWND still exists (oldFocus can be garbage when the dialog
333 // exits, if it has been destroyed)
334 HWND hwndOldFocus = oldFocus ? GetHwndOf(oldFocus) : NULL;
335
336
337 // enter and run the modal loop
338 {
339 wxDialogModalDataTiedPtr modalData(&m_modalData,
340 new wxDialogModalData(this));
341 modalData->RunLoop();
342 }
343
344
345 // and restore focus
346 // Note that this code MUST NOT access the dialog object's data
347 // in case the object has been deleted (which will be the case
348 // for a modal dialog that has been destroyed before calling EndModal).
349 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
350 {
351 // This is likely to prove that the object still exists
352 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
353 oldFocus->SetFocus();
354 }
355 }
5e1febfa 356
b6c588e1 357 return GetReturnCode();
2bda0e17
KB
358}
359
360void wxDialog::EndModal(int retCode)
361{
f46f4c86
VZ
362 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
363
044fe836 364 m_endModalCalled = true;
b6c588e1 365 SetReturnCode(retCode);
6a088435 366
12b58624
VZ
367 Hide();
368}
369
b6c588e1
VZ
370// ----------------------------------------------------------------------------
371// wxWin event handlers
372// ----------------------------------------------------------------------------
2bda0e17 373
9ceeecb9
JS
374#ifdef __POCKETPC__
375// Responds to the OK button in a PocketPC titlebar. This
376// can be overridden, or you can change the id used for
377// sending the event, by calling SetAffirmativeId.
378bool wxDialog::DoOK()
379{
f55fee08
VZ
380 const int idOk = GetAffirmativeId();
381 if ( EmulateButtonClickIfPresent(idOk) )
9ceeecb9 382 return true;
9ceeecb9 383
f55fee08
VZ
384 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
385 event.SetEventObject(this);
386
387 return GetEventHandler()->ProcessEvent(event);
9ceeecb9 388}
f55fee08 389#endif // __POCKETPC__
9ceeecb9 390
ec5f0c24
JS
391#if wxUSE_TOOLBAR && defined(__POCKETPC__)
392// create main toolbar by calling OnCreateToolBar()
393wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
394{
395 m_dialogToolBar = OnCreateToolBar(style, winid, name);
396
397 return m_dialogToolBar;
398}
399
400// return a new toolbar
401wxToolBar *wxDialog::OnCreateToolBar(long style,
402 wxWindowID winid,
403 const wxString& name)
404{
405 return new wxToolMenuBar(this, winid,
406 wxDefaultPosition, wxDefaultSize,
407 style, name);
660296aa 408}
ec5f0c24 409#endif
9ceeecb9 410
42e69d6b 411// ---------------------------------------------------------------------------
f55fee08 412// dialog Windows messages processing
42e69d6b
VZ
413// ---------------------------------------------------------------------------
414
c140b7e7 415WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
42e69d6b 416{
c140b7e7 417 WXLRESULT rc = 0;
044fe836 418 bool processed = false;
42e69d6b
VZ
419
420 switch ( message )
421 {
0fc58b86
RR
422#ifdef __WXWINCE__
423 // react to pressing the OK button in the title
424 case WM_COMMAND:
ef6d716b
WS
425 {
426 switch ( LOWORD(wParam) )
0fc58b86 427 {
9ceeecb9 428#ifdef __POCKETPC__
ef6d716b 429 case IDOK:
9ceeecb9
JS
430 processed = DoOK();
431 if (!processed)
b554cf63 432 processed = !Close();
9ceeecb9
JS
433#endif
434#ifdef __SMARTPHONE__
ef6d716b
WS
435 case IDM_LEFT:
436 case IDM_RIGHT:
437 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
0fc58b86 438 break;
ef6d716b 439#endif // __SMARTPHONE__
0fc58b86
RR
440 }
441 break;
ef6d716b 442 }
044fe836 443#endif
42e69d6b
VZ
444 case WM_CLOSE:
445 // if we can't close, tell the system that we processed the
446 // message - otherwise it would close us
447 processed = !Close();
448 break;
abceee76 449
f73f67be
VZ
450 case WM_SIZE:
451 // the Windows dialogs unfortunately are not meant to be resizeable
452 // at all and their standard class doesn't include CS_[VH]REDRAW
453 // styles which means that the window is not refreshed properly
454 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
455 // help with it - so we have to refresh it manually which certainly
456 // creates flicker but at least doesn't show garbage on the screen
457 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
044fe836 458 processed = true;
e441e1f4 459 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
f73f67be 460 {
044fe836 461 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
f73f67be
VZ
462 }
463 break;
464
04ef50df 465#ifndef __WXMICROWIN__
abceee76
VZ
466 case WM_SETCURSOR:
467 // we want to override the busy cursor for modal dialogs:
468 // typically, wxBeginBusyCursor() is called and then a modal dialog
bfbd6dc1 469 // is shown, but the modal dialog shouldn't have hourglass cursor
f46f4c86 470 if ( IsModal() && wxIsBusy() )
abceee76 471 {
bfbd6dc1
VZ
472 // set our cursor for all windows (but see below)
473 wxCursor cursor = m_cursor;
474 if ( !cursor.Ok() )
475 cursor = wxCURSOR_ARROW;
abceee76 476
bfbd6dc1
VZ
477 ::SetCursor(GetHcursorOf(cursor));
478
479 // in any case, stop here and don't let wxWindow process this
480 // message (it would set the busy cursor)
044fe836 481 processed = true;
bfbd6dc1 482
044fe836 483 // but return false to tell the child window (if the event
bfbd6dc1
VZ
484 // comes from one of them and not from ourselves) that it can
485 // set its own cursor if it has one: thus, standard controls
486 // (e.g. text ctrl) still have correct cursors in a dialog
487 // invoked while wxIsBusy()
044fe836 488 rc = false;
abceee76 489 }
bfbd6dc1 490 break;
82c9f85c 491#endif // __WXMICROWIN__
42e69d6b
VZ
492 }
493
494 if ( !processed )
495 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
496
497 return rc;
498}