]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dialog.cpp
added (wxMSW-only) wxToolTip::SetMaxWidth() and improve its default behaviour (#2817)
[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{
044fe836 148 m_isShown = false;
6757b5e3 149 m_modalData = NULL;
ec5f0c24
JS
150#if wxUSE_TOOLBAR && defined(__POCKETPC__)
151 m_dialogToolBar = NULL;
152#endif
2c66581e 153 m_hGripper = 0;
2bda0e17
KB
154}
155
b3daa5a3
VZ
156bool wxDialog::Create(wxWindow *parent,
157 wxWindowID id,
462e2437
VZ
158 const wxString& title,
159 const wxPoint& pos,
160 const wxSize& size,
161 long style,
162 const wxString& name)
2bda0e17 163{
82c9f85c 164 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
bd9d76cb 165
706bb5f9 166 // All dialogs should really have this style
b225f659 167 style |= wxTAB_TRAVERSAL;
2bda0e17 168
b225f659 169 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
044fe836 170 return false;
00233716
VZ
171
172 if ( !m_hasFont )
6f951810 173 SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
e1bdd507 174
3180bc0e 175#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
ef6d716b
WS
176 SetLeftMenu(wxID_OK, _("OK"));
177#endif
ec5f0c24
JS
178#if wxUSE_TOOLBAR && defined(__POCKETPC__)
179 CreateToolBar();
180#endif
ef6d716b 181
952f8d3c 182 if ( HasFlag(wxRESIZE_BORDER) )
eddc468e 183 {
2c66581e
VZ
184 CreateGripper();
185
eddc468e
VZ
186 Connect(wxEVT_CREATE,
187 wxWindowCreateEventHandler(wxDialog::OnWindowCreate));
188 }
189
044fe836 190 return true;
2bda0e17
KB
191}
192
2bda0e17
KB
193wxDialog::~wxDialog()
194{
b0a6bb75 195 // this will also reenable all the other windows for a modal dialog
044fe836 196 Show(false);
2c66581e
VZ
197
198 DestroyGripper();
2bda0e17
KB
199}
200
b6c588e1
VZ
201// ----------------------------------------------------------------------------
202// showing the dialogs
203// ----------------------------------------------------------------------------
22cf5fec 204
a543e3ce
VZ
205wxWindow *wxDialog::FindSuitableParent() const
206{
207 // first try to use the currently active window
208 HWND hwndFg = ::GetForegroundWindow();
209 wxWindow *parent = hwndFg ? wxFindWinFromHandle((WXHWND)hwndFg)
210 : NULL;
211 if ( !parent )
212 {
213 // next try the main app window
214 parent = wxTheApp->GetTopWindow();
215 }
216
217 // finally, check if the parent we found is really suitable
218 if ( !parent || parent == (wxWindow *)this || !parent->IsShown() )
219 {
220 // don't use this one
221 parent = NULL;
222 }
223
224 return parent;
225}
226
b6c588e1 227bool wxDialog::Show(bool show)
2bda0e17 228{
044fe836
VZ
229 if ( show == IsShown() )
230 return false;
231
6757b5e3 232 if ( !show && m_modalData )
86ad564e 233 {
6757b5e3
VZ
234 // we need to do this before calling wxDialogBase version because if we
235 // had disabled other app windows, they must be reenabled right now as
b0a6bb75 236 // if they stay disabled Windows will activate another window (one
6757b5e3
VZ
237 // which is enabled, anyhow) when we're hidden in the base class Show()
238 // and we will lose activation
239 m_modalData->ExitLoop();
86ad564e
JS
240 }
241
044fe836 242 if ( show )
b6c588e1 243 {
3aa8e4ea
JS
244 if (CanDoLayoutAdaptation())
245 DoLayoutAdaptation();
246
044fe836
VZ
247 // this usually will result in TransferDataToWindow() being called
248 // which will change the controls values so do it before showing as
249 // otherwise we could have some flicker
250 InitDialog();
b6c588e1 251 }
2bda0e17 252
044fe836
VZ
253 wxDialogBase::Show(show);
254
b6c588e1
VZ
255 if ( show )
256 {
2bc44d62
VZ
257 // dialogs don't get WM_SIZE message from ::ShowWindow() for some
258 // reason so generate it ourselves for consistency with frames and
259 // dialogs in other ports
2b5f62a0
VZ
260 //
261 // NB: normally we should call it just the first time but doing it
262 // every time is simpler than keeping a flag
2bc44d62
VZ
263 const wxSize size = GetClientSize();
264 ::SendMessage(GetHwnd(), WM_SIZE,
265 SIZE_RESTORED, MAKELPARAM(size.x, size.y));
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{
dc28b856 279 wxASSERT_MSG( !IsModal(), _T("ShowModal() can't be called twice") );
f46f4c86 280
f46f4c86
VZ
281 Show();
282
283 // EndModal may have been called from InitDialog handler (called from
89424d9b 284 // inside Show()) and hidden the dialog back again
4b5e178a 285 if ( IsShown() )
5e1febfa 286 {
f46f4c86 287 // enter and run the modal loop
88a67391
VZ
288 wxDialogModalDataTiedPtr modalData(&m_modalData,
289 new wxDialogModalData(this));
290 modalData->RunLoop();
f46f4c86 291 }
5e1febfa 292
b6c588e1 293 return GetReturnCode();
2bda0e17
KB
294}
295
296void wxDialog::EndModal(int retCode)
297{
f46f4c86
VZ
298 wxASSERT_MSG( IsModal(), _T("EndModal() called for non modal dialog") );
299
b6c588e1 300 SetReturnCode(retCode);
6a088435 301
12b58624
VZ
302 Hide();
303}
304
2c66581e
VZ
305// ----------------------------------------------------------------------------
306// wxDialog gripper handling
307// ----------------------------------------------------------------------------
308
309void wxDialog::SetWindowStyleFlag(long style)
310{
311 wxDialogBase::SetWindowStyleFlag(style);
312
952f8d3c 313 if ( HasFlag(wxRESIZE_BORDER) )
2c66581e
VZ
314 CreateGripper();
315 else
316 DestroyGripper();
317}
318
319void wxDialog::CreateGripper()
320{
952f8d3c 321 if ( !m_hGripper )
2c66581e 322 {
952f8d3c 323 // just create it here, it will be positioned and shown later
2c66581e
VZ
324 m_hGripper = (WXHWND)::CreateWindow
325 (
326 wxT("SCROLLBAR"),
327 wxT(""),
952f8d3c
VZ
328 WS_CHILD |
329 WS_CLIPSIBLINGS |
2c66581e
VZ
330 SBS_SIZEGRIP |
331 SBS_SIZEBOX |
332 SBS_SIZEBOXBOTTOMRIGHTALIGN,
333 0, 0, 0, 0,
334 GetHwnd(),
335 0,
336 wxGetInstance(),
337 NULL
338 );
2c66581e
VZ
339 }
340}
341
342void wxDialog::DestroyGripper()
343{
344 if ( m_hGripper )
345 {
eddc468e
VZ
346 // we used to have trouble with gripper appearing on top (and hence
347 // overdrawing) the other, real, dialog children -- check that this
f78c70f3
VZ
348 // isn't the case automatically (but notice that this could be false if
349 // we're not shown at all as in this case ResizeGripper() might not
350 // have been called yet)
351 wxASSERT_MSG( !IsShown() ||
c3b46177 352 ::GetWindow((HWND)m_hGripper, GW_HWNDNEXT) == 0,
eddc468e 353 _T("Bug in wxWidgets: gripper should be at the bottom of Z-order") );
2c66581e
VZ
354 ::DestroyWindow((HWND) m_hGripper);
355 m_hGripper = 0;
356 }
357}
358
359void wxDialog::ShowGripper(bool show)
360{
361 wxASSERT_MSG( m_hGripper, _T("shouldn't be called if we have no gripper") );
362
952f8d3c
VZ
363 if ( show )
364 ResizeGripper();
365
2c66581e
VZ
366 ::ShowWindow((HWND)m_hGripper, show ? SW_SHOW : SW_HIDE);
367}
368
369void wxDialog::ResizeGripper()
370{
371 wxASSERT_MSG( m_hGripper, _T("shouldn't be called if we have no gripper") );
372
373 HWND hwndGripper = (HWND)m_hGripper;
374
375 const wxRect rectGripper = wxRectFromRECT(wxGetWindowRect(hwndGripper));
376 const wxSize size = GetClientSize() - rectGripper.GetSize();
377
378 ::SetWindowPos(hwndGripper, HWND_BOTTOM,
379 size.x, size.y,
380 rectGripper.width, rectGripper.height,
381 SWP_NOACTIVATE);
382}
383
eddc468e
VZ
384void wxDialog::OnWindowCreate(wxWindowCreateEvent& event)
385{
386 if ( m_hGripper && IsShown() &&
387 event.GetWindow() && event.GetWindow()->GetParent() == this )
388 {
389 // Put gripper below the newly created child window
390 ::SetWindowPos((HWND)m_hGripper, HWND_BOTTOM, 0, 0, 0, 0,
391 SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
392 }
393
394 event.Skip();
395}
396
b6c588e1
VZ
397// ----------------------------------------------------------------------------
398// wxWin event handlers
399// ----------------------------------------------------------------------------
2bda0e17 400
9ceeecb9
JS
401#ifdef __POCKETPC__
402// Responds to the OK button in a PocketPC titlebar. This
403// can be overridden, or you can change the id used for
404// sending the event, by calling SetAffirmativeId.
405bool wxDialog::DoOK()
406{
f55fee08
VZ
407 const int idOk = GetAffirmativeId();
408 if ( EmulateButtonClickIfPresent(idOk) )
9ceeecb9 409 return true;
9ceeecb9 410
f55fee08
VZ
411 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetAffirmativeId());
412 event.SetEventObject(this);
413
937013e0 414 return HandleWindowEvent(event);
9ceeecb9 415}
f55fee08 416#endif // __POCKETPC__
9ceeecb9 417
ec5f0c24
JS
418#if wxUSE_TOOLBAR && defined(__POCKETPC__)
419// create main toolbar by calling OnCreateToolBar()
420wxToolBar* wxDialog::CreateToolBar(long style, wxWindowID winid, const wxString& name)
421{
422 m_dialogToolBar = OnCreateToolBar(style, winid, name);
423
424 return m_dialogToolBar;
425}
426
427// return a new toolbar
428wxToolBar *wxDialog::OnCreateToolBar(long style,
429 wxWindowID winid,
430 const wxString& name)
431{
432 return new wxToolMenuBar(this, winid,
433 wxDefaultPosition, wxDefaultSize,
434 style, name);
660296aa 435}
ec5f0c24 436#endif
9ceeecb9 437
42e69d6b 438// ---------------------------------------------------------------------------
f55fee08 439// dialog Windows messages processing
42e69d6b
VZ
440// ---------------------------------------------------------------------------
441
c140b7e7 442WXLRESULT wxDialog::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
42e69d6b 443{
c140b7e7 444 WXLRESULT rc = 0;
044fe836 445 bool processed = false;
42e69d6b
VZ
446
447 switch ( message )
448 {
0fc58b86
RR
449#ifdef __WXWINCE__
450 // react to pressing the OK button in the title
451 case WM_COMMAND:
ef6d716b
WS
452 {
453 switch ( LOWORD(wParam) )
0fc58b86 454 {
9ceeecb9 455#ifdef __POCKETPC__
ef6d716b 456 case IDOK:
9ceeecb9
JS
457 processed = DoOK();
458 if (!processed)
b554cf63 459 processed = !Close();
9ceeecb9
JS
460#endif
461#ifdef __SMARTPHONE__
ef6d716b
WS
462 case IDM_LEFT:
463 case IDM_RIGHT:
464 processed = HandleCommand( LOWORD(wParam) , 0 , NULL );
0fc58b86 465 break;
ef6d716b 466#endif // __SMARTPHONE__
0fc58b86
RR
467 }
468 break;
ef6d716b 469 }
044fe836 470#endif
42e69d6b
VZ
471 case WM_CLOSE:
472 // if we can't close, tell the system that we processed the
473 // message - otherwise it would close us
474 processed = !Close();
475 break;
abceee76 476
f73f67be 477 case WM_SIZE:
2c66581e
VZ
478 if ( m_hGripper )
479 {
480 switch ( wParam )
481 {
482 case SIZE_MAXIMIZED:
483 ShowGripper(false);
484 break;
485
486 case SIZE_RESTORED:
487 ShowGripper(true);
2c66581e
VZ
488 }
489 }
490
f73f67be
VZ
491 // the Windows dialogs unfortunately are not meant to be resizeable
492 // at all and their standard class doesn't include CS_[VH]REDRAW
493 // styles which means that the window is not refreshed properly
494 // after the resize and no amount of WS_CLIPCHILDREN/SIBLINGS can
495 // help with it - so we have to refresh it manually which certainly
496 // creates flicker but at least doesn't show garbage on the screen
497 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
044fe836 498 processed = true;
e441e1f4 499 if ( HasFlag(wxFULL_REPAINT_ON_RESIZE) )
f73f67be 500 {
044fe836 501 ::InvalidateRect(GetHwnd(), NULL, false /* erase bg */);
f73f67be
VZ
502 }
503 break;
504
04ef50df 505#ifndef __WXMICROWIN__
abceee76
VZ
506 case WM_SETCURSOR:
507 // we want to override the busy cursor for modal dialogs:
508 // typically, wxBeginBusyCursor() is called and then a modal dialog
bfbd6dc1 509 // is shown, but the modal dialog shouldn't have hourglass cursor
f46f4c86 510 if ( IsModal() && wxIsBusy() )
abceee76 511 {
bfbd6dc1
VZ
512 // set our cursor for all windows (but see below)
513 wxCursor cursor = m_cursor;
514 if ( !cursor.Ok() )
515 cursor = wxCURSOR_ARROW;
abceee76 516
bfbd6dc1
VZ
517 ::SetCursor(GetHcursorOf(cursor));
518
519 // in any case, stop here and don't let wxWindow process this
520 // message (it would set the busy cursor)
044fe836 521 processed = true;
bfbd6dc1 522
044fe836 523 // but return false to tell the child window (if the event
bfbd6dc1
VZ
524 // comes from one of them and not from ourselves) that it can
525 // set its own cursor if it has one: thus, standard controls
526 // (e.g. text ctrl) still have correct cursors in a dialog
527 // invoked while wxIsBusy()
044fe836 528 rc = false;
abceee76 529 }
bfbd6dc1 530 break;
82c9f85c 531#endif // __WXMICROWIN__
42e69d6b
VZ
532 }
533
534 if ( !processed )
535 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
536
537 return rc;
538}