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