]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
make GetUsageString() public, this is useful at least for the tests
[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
57bd4c60 30 #include "wx/msw/wrapcdlg.h"
b6c588e1
VZ
31 #include "wx/utils.h"
32 #include "wx/frame.h"
33 #include "wx/app.h"
95e92d90 34 #include "wx/button.h"
b6c588e1
VZ
35 #include "wx/settings.h"
36 #include "wx/intl.h"
37 #include "wx/log.h"
4e3e485b 38 #include "wx/toolbar.h"
2bda0e17
KB
39#endif
40
41#include "wx/msw/private.h"
ac8d0c11 42#include "wx/evtloop.h"
8c7f5f03 43#include "wx/ptr_scpd.h"
2bda0e17 44
3180bc0e 45#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
ef6d716b 46 #include "wx/msw/wince/resources.h"
3180bc0e 47#endif // __SMARTPHONE__ && __WXWINCE__
ef6d716b 48
b6c588e1
VZ
49// ----------------------------------------------------------------------------
50// wxWin macros
51// ----------------------------------------------------------------------------
52
ffca575c
SC
53#if wxUSE_EXTENDED_RTTI
54WX_DEFINE_FLAGS( wxDialogStyle )
55
3ff066a4 56wxBEGIN_FLAGS( wxDialogStyle )
ffca575c
SC
57 // new style border flags, we put them first to
58 // use them for streaming out
3ff066a4
SC
59 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
60 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
61 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
62 wxFLAGS_MEMBER(wxBORDER_RAISED)
63 wxFLAGS_MEMBER(wxBORDER_STATIC)
64 wxFLAGS_MEMBER(wxBORDER_NONE)
044fe836 65
ffca575c 66 // old style border flags
3ff066a4
SC
67 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
68 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
69 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
70 wxFLAGS_MEMBER(wxRAISED_BORDER)
71 wxFLAGS_MEMBER(wxSTATIC_BORDER)
72 wxFLAGS_MEMBER(wxNO_BORDER)
ffca575c
SC
73
74 // standard window styles
3ff066a4
SC
75 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
76 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
ffca575c
SC
77
78 // dialog styles
3ff066a4
SC
79 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY)
80 wxFLAGS_MEMBER(wxSTAY_ON_TOP)
81 wxFLAGS_MEMBER(wxCAPTION)
1c067fe3 82#if WXWIN_COMPATIBILITY_2_6
3ff066a4 83 wxFLAGS_MEMBER(wxTHICK_FRAME)
1c067fe3 84#endif // WXWIN_COMPATIBILITY_2_6
3ff066a4
SC
85 wxFLAGS_MEMBER(wxSYSTEM_MENU)
86 wxFLAGS_MEMBER(wxRESIZE_BORDER)
1c067fe3 87#if WXWIN_COMPATIBILITY_2_6
3ff066a4 88 wxFLAGS_MEMBER(wxRESIZE_BOX)
1c067fe3 89#endif // WXWIN_COMPATIBILITY_2_6
3ff066a4
SC
90 wxFLAGS_MEMBER(wxCLOSE_BOX)
91 wxFLAGS_MEMBER(wxMAXIMIZE_BOX)
92 wxFLAGS_MEMBER(wxMINIMIZE_BOX)
93wxEND_FLAGS( wxDialogStyle )
ffca575c
SC
94
95IMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog, wxTopLevelWindow,"wx/dialog.h")
96
3ff066a4 97wxBEGIN_PROPERTIES_TABLE(wxDialog)
af498247
VZ
98 wxPROPERTY( Title, wxString, SetTitle, GetTitle, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
99 wxPROPERTY_FLAGS( WindowStyle , wxDialogStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 100wxEND_PROPERTIES_TABLE()
ffca575c 101
3ff066a4
SC
102wxBEGIN_HANDLERS_TABLE(wxDialog)
103wxEND_HANDLERS_TABLE()
ffca575c 104
044fe836 105wxCONSTRUCTOR_6( wxDialog , wxWindow* , Parent , wxWindowID , Id , wxString , Title , wxPoint , Position , wxSize , Size , long , WindowStyle)
ffca575c
SC
106
107#else
82c9f85c 108IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
ffca575c 109#endif
066f1b7a 110
6757b5e3
VZ
111// ----------------------------------------------------------------------------
112// wxDialogModalData
113// ----------------------------------------------------------------------------
114
8c7f5f03
VZ
115// this is simply a container for any data we need to implement modality which
116// allows us to avoid changing wxDialog each time the implementation changes
6757b5e3
VZ
117class wxDialogModalData
118{
119public:
8c7f5f03 120 wxDialogModalData(wxDialog *dialog) : m_evtLoop(dialog) { }
6757b5e3 121
8c7f5f03 122 void RunLoop()
6757b5e3 123 {
6757b5e3
VZ
124 m_evtLoop.Run();
125 }
126
127 void ExitLoop()
128 {
6757b5e3
VZ
129 m_evtLoop.Exit();
130 }
131
6757b5e3 132private:
8c7f5f03 133 wxModalEventLoop m_evtLoop;
6757b5e3
VZ
134};
135
259c43f6 136wxDEFINE_TIED_SCOPED_PTR_TYPE(wxDialogModalData)
8c7f5f03 137
b6c588e1
VZ
138// ============================================================================
139// implementation
140// ============================================================================
141
142// ----------------------------------------------------------------------------
143// wxDialog construction
144// ----------------------------------------------------------------------------
145
b0a6bb75 146void wxDialog::Init()
2bda0e17 147{
52a07708 148 m_oldFocus = (wxWindow *)NULL;
044fe836 149 m_isShown = false;
6757b5e3 150 m_modalData = NULL;
044fe836 151 m_endModalCalled = false;
ec5f0c24
JS
152#if wxUSE_TOOLBAR && defined(__POCKETPC__)
153 m_dialogToolBar = NULL;
154#endif
2c66581e 155 m_hGripper = 0;
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
2c66581e
VZ
187 if( HasFlag(wxRESIZE_BORDER) )
188 CreateGripper();
189
044fe836 190 return true;
2bda0e17
KB
191}
192
2bda0e17
KB
193wxDialog::~wxDialog()
194{
044fe836 195 m_isBeingDeleted = true;
2bda0e17 196
b0a6bb75 197 // this will also reenable all the other windows for a modal dialog
044fe836 198 Show(false);
2c66581e
VZ
199
200 DestroyGripper();
2bda0e17
KB
201}
202
b6c588e1
VZ
203// ----------------------------------------------------------------------------
204// showing the dialogs
205// ----------------------------------------------------------------------------
22cf5fec 206
a543e3ce
VZ
207wxWindow *wxDialog::FindSuitableParent() const
208{
209 // first try to use the currently active window
210 HWND hwndFg = ::GetForegroundWindow();
211 wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg)
212 : NULL;
213 if ( !parent )
214 {
215 // next try the main app window
216 parent = wxTheApp->GetTopWindow();
217 }
218
219 // finally, check if the parent we found is really suitable
220 if ( !parent || parent == (wxWindow *)this || !parent->IsShown() )
221 {
222 // don't use this one
223 parent = NULL;
224 }
225
226 return parent;
227}
228
b6c588e1 229bool wxDialog::Show(bool show)
2bda0e17 230{
044fe836
VZ
231 if ( show == IsShown() )
232 return false;
233
6757b5e3 234 if ( !show && m_modalData )
86ad564e 235 {
6757b5e3
VZ
236 // we need to do this before calling wxDialogBase version because if we
237 // had disabled other app windows, they must be reenabled right now as
b0a6bb75 238 // if they stay disabled Windows will activate another window (one
6757b5e3
VZ
239 // which is enabled, anyhow) when we're hidden in the base class Show()
240 // and we will lose activation
241 m_modalData->ExitLoop();
86ad564e
JS
242 }
243
044fe836 244 if ( show )
b6c588e1 245 {
3aa8e4ea
JS
246 if (CanDoLayoutAdaptation())
247 DoLayoutAdaptation();
248
044fe836
VZ
249 // this usually will result in TransferDataToWindow() being called
250 // which will change the controls values so do it before showing as
251 // otherwise we could have some flicker
252 InitDialog();
b6c588e1 253 }
2bda0e17 254
044fe836
VZ
255 wxDialogBase::Show(show);
256
b6c588e1
VZ
257 if ( show )
258 {
2b5f62a0
VZ
259 // dialogs don't get WM_SIZE message after creation unlike most (all?)
260 // other windows and so could start their life non laid out correctly
261 // if we didn't call Layout() from here
262 //
263 // NB: normally we should call it just the first time but doing it
264 // every time is simpler than keeping a flag
265 Layout();
b6c588e1 266 }
2bda0e17 267
044fe836 268 return true;
2bda0e17
KB
269}
270
313901f3
JS
271void wxDialog::Raise()
272{
273 ::SetForegroundWindow(GetHwnd());
274}
275
f46f4c86 276// show dialog modally
a23fd0e1 277int wxDialog::ShowModal()
2bda0e17 278{
f46f4c86
VZ
279 wxASSERT_MSG( !IsModal(), _T("wxDialog::ShowModal() reentered?") );
280
044fe836 281 m_endModalCalled = false;
f46f4c86
VZ
282
283 Show();
284
285 // EndModal may have been called from InitDialog handler (called from
286 // inside Show()), which would cause an infinite loop if we didn't take it
287 // into account
288 if ( !m_endModalCalled )
5e1febfa 289 {
f46f4c86
VZ
290 // modal dialog needs a parent window, so try to find one
291 wxWindow *parent = GetParent();
292 if ( !parent )
293 {
294 parent = FindSuitableParent();
295 }
5e1febfa 296
f46f4c86
VZ
297 // remember where the focus was
298 wxWindow *oldFocus = m_oldFocus;
299 if ( !oldFocus )
300 {
301 // VZ: do we really want to do this?
302 oldFocus = parent;
303 }
304
305 // We have to remember the HWND because we need to check
306 // the HWND still exists (oldFocus can be garbage when the dialog
307 // exits, if it has been destroyed)
308 HWND hwndOldFocus = oldFocus ? GetHwndOf(oldFocus) : NULL;
309
310
311 // enter and run the modal loop
312 {
313 wxDialogModalDataTiedPtr modalData(&m_modalData,
314 new wxDialogModalData(this));
315 modalData->RunLoop();
316 }
317
318
319 // and restore focus
320 // Note that this code MUST NOT access the dialog object's data
321 // in case the object has been deleted (which will be the case
322 // for a modal dialog that has been destroyed before calling EndModal).
323 if ( oldFocus && (oldFocus != this) && ::IsWindow(hwndOldFocus))
324 {
325 // This is likely to prove that the object still exists
326 if (wxFindWinFromHandle((WXHWND) hwndOldFocus) == oldFocus)
327 oldFocus->SetFocus();
328 }
329 }
5e1febfa 330
b6c588e1 331 return GetReturnCode();
2bda0e17
KB
332}
333
334void wxDialog::EndModal(int retCode)
335{
f46f4c86
VZ
336 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
337
044fe836 338 m_endModalCalled = true;
b6c588e1 339 SetReturnCode(retCode);
6a088435 340
12b58624
VZ
341 Hide();
342}
343
2c66581e
VZ
344// ----------------------------------------------------------------------------
345// wxDialog gripper handling
346// ----------------------------------------------------------------------------
347
348void wxDialog::SetWindowStyleFlag(long style)
349{
350 wxDialogBase::SetWindowStyleFlag(style);
351
352 if( HasFlag(wxRESIZE_BORDER) )
353 CreateGripper();
354 else
355 DestroyGripper();
356}
357
358void wxDialog::CreateGripper()
359{
360 if( !m_hGripper )
361 {
362 m_hGripper = (WXHWND)::CreateWindow
363 (
364 wxT("SCROLLBAR"),
365 wxT(""),
366 WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |
367 SBS_SIZEGRIP |
368 SBS_SIZEBOX |
369 SBS_SIZEBOXBOTTOMRIGHTALIGN,
370 0, 0, 0, 0,
371 GetHwnd(),
372 0,
373 wxGetInstance(),
374 NULL
375 );
376
377 // position the gripper correctly after creation
378 ResizeGripper();
379 }
380}
381
382void wxDialog::DestroyGripper()
383{
384 if ( m_hGripper )
385 {
386 ::DestroyWindow((HWND) m_hGripper);
387 m_hGripper = 0;
388 }
389}
390
391void wxDialog::ShowGripper(bool show)
392{
393 wxASSERT_MSG( m_hGripper, _T("shouldn't be called if we have no gripper") );
394
395 ::ShowWindow((HWND)m_hGripper, show ? SW_SHOW : SW_HIDE);
396}
397
398void wxDialog::ResizeGripper()
399{
400 wxASSERT_MSG( m_hGripper, _T("shouldn't be called if we have no gripper") );
401
402 HWND hwndGripper = (HWND)m_hGripper;
403
404 const wxRect rectGripper = wxRectFromRECT(wxGetWindowRect(hwndGripper));
405 const wxSize size = GetClientSize() - rectGripper.GetSize();
406
407 ::SetWindowPos(hwndGripper, HWND_BOTTOM,
408 size.x, size.y,
409 rectGripper.width, rectGripper.height,
410 SWP_NOACTIVATE);
411}
412
b6c588e1
VZ
413// ----------------------------------------------------------------------------
414// wxWin event handlers
415// ----------------------------------------------------------------------------
2bda0e17 416
9ceeecb9
JS
417#ifdef __POCKETPC__
418// Responds to the OK button in a PocketPC titlebar. This
419// can be overridden, or you can change the id used for
420// sending the event, by calling SetAffirmativeId.
421bool wxDialog::DoOK()
422{
f55fee08
VZ
423 const int idOk = GetAffirmativeId();
424 if ( EmulateButtonClickIfPresent(idOk) )
9ceeecb9 425 return true;
9ceeecb9 426
f55fee08
VZ
427 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
428 event.SetEventObject(this);
429
937013e0 430 return HandleWindowEvent(event);
9ceeecb9 431}
f55fee08 432#endif // __POCKETPC__
9ceeecb9 433
ec5f0c24
JS
434#if wxUSE_TOOLBAR && defined(__POCKETPC__)
435// create main toolbar by calling OnCreateToolBar()
436wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
437{
438 m_dialogToolBar = OnCreateToolBar(style, winid, name);
439
440 return m_dialogToolBar;
441}
442
443// return a new toolbar
444wxToolBar *wxDialog::OnCreateToolBar(long style,
445 wxWindowID winid,
446 const wxString& name)
447{
448 return new wxToolMenuBar(this, winid,
449 wxDefaultPosition, wxDefaultSize,
450 style, name);
660296aa 451}
ec5f0c24 452#endif
9ceeecb9 453
42e69d6b 454// ---------------------------------------------------------------------------
f55fee08 455// dialog Windows messages processing
42e69d6b
VZ
456// ---------------------------------------------------------------------------
457
c140b7e7 458WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
42e69d6b 459{
c140b7e7 460 WXLRESULT rc = 0;
044fe836 461 bool processed = false;
42e69d6b
VZ
462
463 switch ( message )
464 {
0fc58b86
RR
465#ifdef __WXWINCE__
466 // react to pressing the OK button in the title
467 case WM_COMMAND:
ef6d716b
WS
468 {
469 switch ( LOWORD(wParam) )
0fc58b86 470 {
9ceeecb9 471#ifdef __POCKETPC__
ef6d716b 472 case IDOK:
9ceeecb9
JS
473 processed = DoOK();
474 if (!processed)
b554cf63 475 processed = !Close();
9ceeecb9
JS
476#endif
477#ifdef __SMARTPHONE__
ef6d716b
WS
478 case IDM_LEFT:
479 case IDM_RIGHT:
480 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
0fc58b86 481 break;
ef6d716b 482#endif // __SMARTPHONE__
0fc58b86
RR
483 }
484 break;
ef6d716b 485 }
044fe836 486#endif
42e69d6b
VZ
487 case WM_CLOSE:
488 // if we can't close, tell the system that we processed the
489 // message - otherwise it would close us
490 processed = !Close();
491 break;
abceee76 492
f73f67be 493 case WM_SIZE:
2c66581e
VZ
494 if ( m_hGripper )
495 {
496 switch ( wParam )
497 {
498 case SIZE_MAXIMIZED:
499 ShowGripper(false);
500 break;
501
502 case SIZE_RESTORED:
503 ShowGripper(true);
504 // fall through
505
506 default:
507 ResizeGripper();
508 }
509 }
510
f73f67be
VZ
511 // the Windows dialogs unfortunately are not meant to be resizeable
512 // at all and their standard class doesn't include CS_[VH]REDRAW
513 // styles which means that the window is not refreshed properly
514 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
515 // help with it - so we have to refresh it manually which certainly
516 // creates flicker but at least doesn't show garbage on the screen
517 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
044fe836 518 processed = true;
e441e1f4 519 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
f73f67be 520 {
044fe836 521 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
f73f67be
VZ
522 }
523 break;
524
04ef50df 525#ifndef __WXMICROWIN__
abceee76
VZ
526 case WM_SETCURSOR:
527 // we want to override the busy cursor for modal dialogs:
528 // typically, wxBeginBusyCursor() is called and then a modal dialog
bfbd6dc1 529 // is shown, but the modal dialog shouldn't have hourglass cursor
f46f4c86 530 if ( IsModal() && wxIsBusy() )
abceee76 531 {
bfbd6dc1
VZ
532 // set our cursor for all windows (but see below)
533 wxCursor cursor = m_cursor;
534 if ( !cursor.Ok() )
535 cursor = wxCURSOR_ARROW;
abceee76 536
bfbd6dc1
VZ
537 ::SetCursor(GetHcursorOf(cursor));
538
539 // in any case, stop here and don't let wxWindow process this
540 // message (it would set the busy cursor)
044fe836 541 processed = true;
bfbd6dc1 542
044fe836 543 // but return false to tell the child window (if the event
bfbd6dc1
VZ
544 // comes from one of them and not from ourselves) that it can
545 // set its own cursor if it has one: thus, standard controls
546 // (e.g. text ctrl) still have correct cursors in a dialog
547 // invoked while wxIsBusy()
044fe836 548 rc = false;
abceee76 549 }
bfbd6dc1 550 break;
82c9f85c 551#endif // __WXMICROWIN__
42e69d6b
VZ
552 }
553
554 if ( !processed )
555 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
556
557 return rc;
558}