]> git.saurik.com Git - wxWidgets.git/blame - src/common/wincmn.cpp
Avoid resetting size if paper id not found
[wxWidgets.git] / src / common / wincmn.cpp
CommitLineData
7ec1983b 1/////////////////////////////////////////////////////////////////////////////
7ec69821 2// Name: src/common/window.cpp
7ec1983b
VZ
3// Purpose: common (to all ports) wxWindow functions
4// Author: Julian Smart, Vadim Zeitlin
5// Modified by:
6// Created: 13/07/98
7// RCS-ID: $Id$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
7ec1983b
VZ
10/////////////////////////////////////////////////////////////////////////////
11
f03fc89f
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
f701d7ab 19
341287bf
JS
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
f701d7ab 23#ifdef __BORLANDC__
f03fc89f 24 #pragma hdrstop
f701d7ab
JS
25#endif
26
f03fc89f
VZ
27#ifndef WX_PRECOMP
28 #include "wx/string.h"
29 #include "wx/log.h"
30 #include "wx/intl.h"
31 #include "wx/frame.h"
f03fc89f 32 #include "wx/window.h"
1e6feb95 33 #include "wx/control.h"
f03fc89f
VZ
34 #include "wx/checkbox.h"
35 #include "wx/radiobut.h"
106844da 36 #include "wx/statbox.h"
26bf1ce0 37 #include "wx/textctrl.h"
f03fc89f
VZ
38 #include "wx/settings.h"
39 #include "wx/dialog.h"
a02dc3e3 40 #include "wx/msgdlg.h"
1b942d5f 41 #include "wx/msgout.h"
a37a5a73 42 #include "wx/statusbr.h"
e0cf3745 43 #include "wx/toolbar.h"
ed39ff57 44 #include "wx/dcclient.h"
7d59475e 45 #include "wx/scrolbar.h"
f03fc89f 46 #include "wx/layout.h"
ed2fbeb8 47 #include "wx/sizer.h"
8515098c 48 #include "wx/menu.h"
ed2fbeb8 49#endif //WX_PRECOMP
461e93f9 50
f03fc89f
VZ
51#if wxUSE_DRAG_AND_DROP
52 #include "wx/dnd.h"
53#endif // wxUSE_DRAG_AND_DROP
54
ed5317e5 55#if wxUSE_ACCESSIBILITY
7de59551 56 #include "wx/access.h"
ed5317e5
JS
57#endif
58
bd83cb56
VZ
59#if wxUSE_HELP
60 #include "wx/cshelp.h"
61#endif // wxUSE_HELP
62
f03fc89f
VZ
63#if wxUSE_TOOLTIPS
64 #include "wx/tooltip.h"
65#endif // wxUSE_TOOLTIPS
66
789295bf
VZ
67#if wxUSE_CARET
68 #include "wx/caret.h"
69#endif // wxUSE_CARET
70
ff9f7a12 71#if wxUSE_SYSTEM_OPTIONS
cab1a605 72 #include "wx/sysopt.h"
ff9f7a12
SC
73#endif
74
8bb6b2c0
VZ
75#include "wx/platinfo.h"
76
e7445ff8
PC
77// Windows List
78WXDLLIMPEXP_DATA_CORE(wxWindowList) wxTopLevelWindows;
79
eeb0bee1 80// globals
8bb08801 81#if wxUSE_MENUS
eeb0bee1 82wxMenu *wxCurrentPopupMenu = NULL;
8bb08801 83#endif // wxUSE_MENUS
eeb0bee1 84
f03fc89f
VZ
85// ----------------------------------------------------------------------------
86// static data
87// ----------------------------------------------------------------------------
88
f03fc89f
VZ
89
90IMPLEMENT_ABSTRACT_CLASS(wxWindowBase, wxEvtHandler)
91
92// ----------------------------------------------------------------------------
93// event table
94// ----------------------------------------------------------------------------
95
96BEGIN_EVENT_TABLE(wxWindowBase, wxEvtHandler)
97 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged)
98 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog)
a02dc3e3 99 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick)
bd83cb56
VZ
100
101#if wxUSE_HELP
e11f4636 102 EVT_HELP(wxID_ANY, wxWindowBase::OnHelp)
bd83cb56
VZ
103#endif // wxUSE_HELP
104
f03fc89f
VZ
105END_EVENT_TABLE()
106
107// ============================================================================
108// implementation of the common functionality of the wxWindow class
109// ============================================================================
110
111// ----------------------------------------------------------------------------
112// initialization
113// ----------------------------------------------------------------------------
114
115// the default initialization
72795335 116wxWindowBase::wxWindowBase()
f03fc89f
VZ
117{
118 // no window yet, no parent nor children
d3b9f782 119 m_parent = NULL;
e11f4636 120 m_windowId = wxID_ANY;
f03fc89f
VZ
121
122 // no constraints on the minimal window size
123 m_minWidth =
422d0ff0 124 m_maxWidth = wxDefaultCoord;
f03fc89f 125 m_minHeight =
422d0ff0 126 m_maxHeight = wxDefaultCoord;
f03fc89f 127
9f884528
RD
128 // invalidiated cache value
129 m_bestSizeCache = wxDefaultSize;
130
eefe6d4b
VZ
131 // window are created enabled and visible by default
132 m_isShown =
e11f4636 133 m_isEnabled = true;
f03fc89f 134
f03fc89f
VZ
135 // the default event handler is just this window
136 m_eventHandler = this;
137
88ac883a 138#if wxUSE_VALIDATORS
f03fc89f 139 // no validator
d3b9f782 140 m_windowValidator = NULL;
88ac883a 141#endif // wxUSE_VALIDATORS
f03fc89f 142
1b69c815
VZ
143 // the colours/fonts are default for now, so leave m_font,
144 // m_backgroundColour and m_foregroundColour uninitialized and set those
08df3e44
VZ
145 m_hasBgCol =
146 m_hasFgCol =
e11f4636 147 m_hasFont = false;
f8ff87ed
VS
148 m_inheritBgCol =
149 m_inheritFgCol =
150 m_inheritFont = false;
e11f4636 151
f03fc89f 152 // no style bits
d80cd92a 153 m_exStyle =
f03fc89f 154 m_windowStyle = 0;
cab1a605 155
50c53860 156 m_backgroundStyle = wxBG_STYLE_SYSTEM;
f03fc89f 157
f03fc89f
VZ
158#if wxUSE_CONSTRAINTS
159 // no constraints whatsoever
d3b9f782
VZ
160 m_constraints = NULL;
161 m_constraintsInvolvedIn = NULL;
461e93f9
JS
162#endif // wxUSE_CONSTRAINTS
163
d3b9f782
VZ
164 m_windowSizer = NULL;
165 m_containingSizer = NULL;
e11f4636 166 m_autoLayout = false;
f03fc89f
VZ
167
168#if wxUSE_DRAG_AND_DROP
d3b9f782 169 m_dropTarget = NULL;
f03fc89f
VZ
170#endif // wxUSE_DRAG_AND_DROP
171
172#if wxUSE_TOOLTIPS
d3b9f782 173 m_tooltip = NULL;
f03fc89f 174#endif // wxUSE_TOOLTIPS
789295bf
VZ
175
176#if wxUSE_CARET
d3b9f782 177 m_caret = NULL;
789295bf 178#endif // wxUSE_CARET
a2d93e73 179
574c939e 180#if wxUSE_PALETTE
e11f4636 181 m_hasCustomPalette = false;
574c939e
KB
182#endif // wxUSE_PALETTE
183
ed5317e5
JS
184#if wxUSE_ACCESSIBILITY
185 m_accessible = NULL;
186#endif
187
566d84a7 188 m_virtualSize = wxDefaultSize;
1b69c815 189
d3b9f782 190 m_scrollHelper = NULL;
f4fe2f20 191
1b69c815 192 m_windowVariant = wxWINDOW_VARIANT_NORMAL;
ff9f7a12
SC
193#if wxUSE_SYSTEM_OPTIONS
194 if ( wxSystemOptions::HasOption(wxWINDOW_DEFAULT_VARIANT) )
195 {
196 m_windowVariant = (wxWindowVariant) wxSystemOptions::GetOptionInt( wxWINDOW_DEFAULT_VARIANT ) ;
197 }
198#endif
69d90995 199
a2d93e73 200 // Whether we're using the current theme for this window (wxGTK only for now)
e11f4636 201 m_themeEnabled = false;
1b69c815 202
c6212a0c
VZ
203 // This is set to true by SendDestroyEvent() which should be called by the
204 // most derived class to ensure that the destruction event is sent as soon
205 // as possible to allow its handlers to still see the undestroyed window
1b69c815 206 m_isBeingDeleted = false;
17808a75
VZ
207
208 m_freezeCount = 0;
f03fc89f
VZ
209}
210
211// common part of window creation process
212bool wxWindowBase::CreateBase(wxWindowBase *parent,
213 wxWindowID id,
74e3313b 214 const wxPoint& WXUNUSED(pos),
400a9e41 215 const wxSize& WXUNUSED(size),
f03fc89f 216 long style,
ac8d0c11 217 const wxValidator& wxVALIDATOR_PARAM(validator),
f03fc89f
VZ
218 const wxString& name)
219{
925f154d
VZ
220 // ids are limited to 16 bits under MSW so if you care about portability,
221 // it's not a good idea to use ids out of this range (and negative ids are
77ffb593 222 // reserved for wxWidgets own usage)
f35fdf7e
VZ
223 wxASSERT_MSG( id == wxID_ANY || (id >= 0 && id < 32767) ||
224 (id >= wxID_AUTO_LOWEST && id <= wxID_AUTO_HIGHEST),
925f154d
VZ
225 _T("invalid id value") );
226
f03fc89f 227 // generate a new id if the user doesn't care about it
f35fdf7e
VZ
228 if ( id == wxID_ANY )
229 {
230 m_windowId = NewControlId();
f35fdf7e
VZ
231 }
232 else // valid id specified
233 {
234 m_windowId = id;
235 }
f03fc89f 236
e96248c4
VZ
237 // don't use SetWindowStyleFlag() here, this function should only be called
238 // to change the flag after creation as it tries to reflect the changes in
239 // flags by updating the window dynamically and we don't need this here
240 m_windowStyle = style;
241
f03fc89f 242 SetName(name);
f03fc89f 243 SetParent(parent);
674ac8b9
VZ
244
245#if wxUSE_VALIDATORS
8d99be5f 246 SetValidator(validator);
674ac8b9 247#endif // wxUSE_VALIDATORS
f03fc89f 248
8ae6ce07
VZ
249 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
250 // have it too - like this it's possible to set it only in the top level
251 // dialog/frame and all children will inherit it by defult
252 if ( parent && (parent->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) )
253 {
9cb98d89 254 SetExtraStyle(GetExtraStyle() | wxWS_EX_VALIDATE_RECURSIVELY);
8ae6ce07
VZ
255 }
256
e11f4636 257 return true;
f03fc89f
VZ
258}
259
d63312a9
VZ
260bool wxWindowBase::ToggleWindowStyle(int flag)
261{
262 wxASSERT_MSG( flag, _T("flags with 0 value can't be toggled") );
263
264 bool rc;
265 long style = GetWindowStyleFlag();
266 if ( style & flag )
267 {
268 style &= ~flag;
269 rc = false;
270 }
271 else // currently off
272 {
273 style |= flag;
274 rc = true;
275 }
276
277 SetWindowStyleFlag(style);
278
279 return rc;
280}
281
f03fc89f
VZ
282// ----------------------------------------------------------------------------
283// destruction
284// ----------------------------------------------------------------------------
285
286// common clean up
287wxWindowBase::~wxWindowBase()
288{
349d1942
VS
289 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
290
f03fc89f
VZ
291 // FIXME if these 2 cases result from programming errors in the user code
292 // we should probably assert here instead of silently fixing them
293
294 // Just in case the window has been Closed, but we're then deleting
295 // immediately: don't leave dangling pointers.
296 wxPendingDelete.DeleteObject(this);
297
298 // Just in case we've loaded a top-level window via LoadNativeDialog but
299 // we weren't a dialog class
222ed1d6 300 wxTopLevelWindows.DeleteObject((wxWindow*)this);
a23fd0e1 301
3bc9aac0 302#if wxUSE_MENUS
eeb0bee1
VZ
303 // The associated popup menu can still be alive, disassociate from it in
304 // this case
305 if ( wxCurrentPopupMenu && wxCurrentPopupMenu->GetInvokingWindow() == this )
306 wxCurrentPopupMenu->SetInvokingWindow(NULL);
3bc9aac0 307#endif // wxUSE_MENUS
eeb0bee1 308
223d09f6 309 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
f03fc89f 310
f8ab85ae 311 // notify the parent about this window destruction
f09df8b2 312 if ( m_parent )
8e35ab96 313 m_parent->RemoveChild(this);
8e35ab96 314
789295bf 315#if wxUSE_CARET
a2b436fb 316 delete m_caret;
789295bf
VZ
317#endif // wxUSE_CARET
318
88ac883a 319#if wxUSE_VALIDATORS
a2b436fb 320 delete m_windowValidator;
88ac883a 321#endif // wxUSE_VALIDATORS
f03fc89f 322
f03fc89f
VZ
323#if wxUSE_CONSTRAINTS
324 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
325 // at deleted windows as they delete themselves.
326 DeleteRelatedConstraints();
327
328 if ( m_constraints )
329 {
330 // This removes any dangling pointers to this window in other windows'
331 // constraintsInvolvedIn lists.
332 UnsetConstraints(m_constraints);
333 delete m_constraints;
334 m_constraints = NULL;
335 }
461e93f9
JS
336#endif // wxUSE_CONSTRAINTS
337
be90c029 338 if ( m_containingSizer )
12a3f227 339 m_containingSizer->Detach( (wxWindow*)this );
be90c029 340
a2b436fb 341 delete m_windowSizer;
f03fc89f 342
f03fc89f 343#if wxUSE_DRAG_AND_DROP
a2b436fb 344 delete m_dropTarget;
f03fc89f
VZ
345#endif // wxUSE_DRAG_AND_DROP
346
347#if wxUSE_TOOLTIPS
a2b436fb 348 delete m_tooltip;
f03fc89f 349#endif // wxUSE_TOOLTIPS
b0ee47ff 350
ed5317e5 351#if wxUSE_ACCESSIBILITY
a2b436fb 352 delete m_accessible;
ed5317e5 353#endif
d18d8bda
VS
354
355#if wxUSE_HELP
356 // NB: this has to be called unconditionally, because we don't know
357 // whether this window has associated help text or not
358 wxHelpProvider *helpProvider = wxHelpProvider::Get();
359 if ( helpProvider )
360 helpProvider->RemoveHelp(this);
361#endif
f03fc89f
VZ
362}
363
a3ac93e3
VZ
364bool wxWindowBase::IsBeingDeleted() const
365{
366 return m_isBeingDeleted ||
367 (!IsTopLevel() && m_parent && m_parent->IsBeingDeleted());
368}
369
602a2e02
VZ
370void wxWindowBase::SendDestroyEvent()
371{
c6212a0c
VZ
372 if ( m_isBeingDeleted )
373 {
374 // we could have been already called from a more derived class dtor,
375 // e.g. ~wxTLW calls us and so does ~wxWindow and the latter call
376 // should be simply ignored
377 return;
378 }
379
380 m_isBeingDeleted = true;
381
602a2e02
VZ
382 wxWindowDestroyEvent event;
383 event.SetEventObject(this);
384 event.SetId(GetId());
385 GetEventHandler()->ProcessEvent(event);
386}
387
f03fc89f
VZ
388bool wxWindowBase::Destroy()
389{
a79a6671
VZ
390 SendDestroyEvent();
391
f03fc89f
VZ
392 delete this;
393
e11f4636 394 return true;
f03fc89f
VZ
395}
396
397bool wxWindowBase::Close(bool force)
398{
399 wxCloseEvent event(wxEVT_CLOSE_WINDOW, m_windowId);
400 event.SetEventObject(this);
f03fc89f
VZ
401 event.SetCanVeto(!force);
402
e11f4636 403 // return false if window wasn't closed because the application vetoed the
f03fc89f 404 // close event
a50c5fcc 405 return HandleWindowEvent(event) && !event.GetVeto();
f03fc89f
VZ
406}
407
408bool wxWindowBase::DestroyChildren()
409{
222ed1d6 410 wxWindowList::compatibility_iterator node;
a23fd0e1 411 for ( ;; )
f03fc89f 412 {
a23fd0e1
VZ
413 // we iterate until the list becomes empty
414 node = GetChildren().GetFirst();
415 if ( !node )
416 break;
417
f03fc89f 418 wxWindow *child = node->GetData();
a23fd0e1 419
a79a6671
VZ
420 // note that we really want to delete it immediately so don't call the
421 // possible overridden Destroy() version which might not delete the
422 // child immediately resulting in problems with our (top level) child
423 // outliving its parent
424 child->wxWindowBase::Destroy();
a23fd0e1
VZ
425
426 wxASSERT_MSG( !GetChildren().Find(child),
223d09f6 427 wxT("child didn't remove itself using RemoveChild()") );
f03fc89f
VZ
428 }
429
e11f4636 430 return true;
f03fc89f
VZ
431}
432
433// ----------------------------------------------------------------------------
f68586e5 434// size/position related methods
f03fc89f
VZ
435// ----------------------------------------------------------------------------
436
437// centre the window with respect to its parent in either (or both) directions
1f464296 438void wxWindowBase::DoCentre(int dir)
f03fc89f 439{
1f464296
VZ
440 wxCHECK_RET( !(dir & wxCENTRE_ON_SCREEN) && GetParent(),
441 _T("this method only implements centering child windows") );
ef397583 442
1f464296 443 SetSize(GetRect().CentreIn(GetParent()->GetClientSize(), dir));
7631a292
RD
444}
445
f03fc89f
VZ
446// fits the window around the children
447void wxWindowBase::Fit()
448{
74205969 449 if ( !GetChildren().empty() )
f68586e5 450 {
1736f021 451 SetSize(GetBestSize());
f68586e5
VZ
452 }
453 //else: do nothing if we have no children
454}
f03fc89f 455
2b5f62a0
VZ
456// fits virtual size (ie. scrolled area etc.) around children
457void wxWindowBase::FitInside()
458{
459 if ( GetChildren().GetCount() > 0 )
460 {
461 SetVirtualSize( GetBestVirtualSize() );
462 }
463}
464
7d59475e 465// On Mac, scrollbars are explicitly children.
d7928233 466#if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
7d59475e
JS
467static bool wxHasRealChildren(const wxWindowBase* win)
468{
469 int realChildCount = 0;
cab1a605 470
7d59475e
JS
471 for ( wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
472 node;
473 node = node->GetNext() )
474 {
475 wxWindow *win = node->GetData();
476 if ( !win->IsTopLevel() && win->IsShown() && !win->IsKindOf(CLASSINFO(wxScrollBar)))
477 realChildCount ++;
478 }
479 return (realChildCount > 0);
480}
481#endif
ba889513 482
992b2ec4
VS
483void wxWindowBase::InvalidateBestSize()
484{
485 m_bestSizeCache = wxDefaultSize;
486
487 // parent's best size calculation may depend on its children's
5b7df27e
WS
488 // as long as child window we are in is not top level window itself
489 // (because the TLW size is never resized automatically)
490 // so let's invalidate it as well to be safe:
5ff84587 491 if (m_parent && !IsTopLevel())
992b2ec4
VS
492 m_parent->InvalidateBestSize();
493}
7d59475e 494
f68586e5
VZ
495// return the size best suited for the current window
496wxSize wxWindowBase::DoGetBestSize() const
497{
08a19f64 498 wxSize best;
2997ca30 499
ec5bb70d
VZ
500 if ( m_windowSizer )
501 {
7e0f7539 502 best = m_windowSizer->GetMinSize();
ec5bb70d
VZ
503 }
504#if wxUSE_CONSTRAINTS
505 else if ( m_constraints )
506 {
507 wxConstCast(this, wxWindowBase)->SatisfyConstraints();
508
509 // our minimal acceptable size is such that all our windows fit inside
510 int maxX = 0,
511 maxY = 0;
512
222ed1d6 513 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
ec5bb70d
VZ
514 node;
515 node = node->GetNext() )
516 {
517 wxLayoutConstraints *c = node->GetData()->GetConstraints();
518 if ( !c )
519 {
520 // it's not normal that we have an unconstrained child, but
521 // what can we do about it?
522 continue;
523 }
524
525 int x = c->right.GetValue(),
526 y = c->bottom.GetValue();
527
528 if ( x > maxX )
529 maxX = x;
530
531 if ( y > maxY )
532 maxY = y;
533
534 // TODO: we must calculate the overlaps somehow, otherwise we
535 // will never return a size bigger than the current one :-(
536 }
537
08a19f64 538 best = wxSize(maxX, maxY);
ec5bb70d
VZ
539 }
540#endif // wxUSE_CONSTRAINTS
7d59475e 541 else if ( !GetChildren().empty()
d7928233 542#if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
7d59475e
JS
543 && wxHasRealChildren(this)
544#endif
545 )
f03fc89f 546 {
54af2461
VZ
547 // our minimal acceptable size is such that all our visible child
548 // windows fit inside
f68586e5
VZ
549 int maxX = 0,
550 maxY = 0;
551
222ed1d6 552 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
f68586e5
VZ
553 node;
554 node = node->GetNext() )
42e69d6b 555 {
f68586e5 556 wxWindow *win = node->GetData();
54af2461
VZ
557 if ( win->IsTopLevel()
558 || !win->IsShown()
1e6feb95 559#if wxUSE_STATUSBAR
54af2461 560 || wxDynamicCast(win, wxStatusBar)
1e6feb95
VZ
561#endif // wxUSE_STATUSBAR
562 )
f68586e5
VZ
563 {
564 // dialogs and frames lie in different top level windows -
7d9fd004
VZ
565 // don't deal with them here; as for the status bars, they
566 // don't lie in the client area at all
f68586e5
VZ
567 continue;
568 }
569
570 int wx, wy, ww, wh;
571 win->GetPosition(&wx, &wy);
3f5513f5
VZ
572
573 // if the window hadn't been positioned yet, assume that it is in
574 // the origin
422d0ff0 575 if ( wx == wxDefaultCoord )
3f5513f5 576 wx = 0;
422d0ff0 577 if ( wy == wxDefaultCoord )
3f5513f5
VZ
578 wy = 0;
579
f68586e5
VZ
580 win->GetSize(&ww, &wh);
581 if ( wx + ww > maxX )
582 maxX = wx + ww;
583 if ( wy + wh > maxY )
584 maxY = wy + wh;
42e69d6b
VZ
585 }
586
08a19f64 587 best = wxSize(maxX, maxY);
f68586e5 588 }
997c7280 589 else // ! has children
f68586e5 590 {
c0bb586f
VZ
591 // for a generic window there is no natural best size so, if the
592 // minimal size is not set, use the current size but take care to
593 // remember it as minimal size for the next time because our best size
594 // should be constant: otherwise we could get into a situation when the
595 // window is initially at some size, then expanded to a larger size and
596 // then, when the containing window is shrunk back (because our initial
597 // best size had been used for computing the parent min size), we can't
598 // be shrunk back any more because our best size is now bigger
9240613a
VZ
599 wxSize size = GetMinSize();
600 if ( !size.IsFullySpecified() )
601 {
602 size.SetDefaults(GetSize());
603 wxConstCast(this, wxWindowBase)->SetMinSize(size);
604 }
c0bb586f
VZ
605
606 // return as-is, unadjusted by the client size difference.
9240613a 607 return size;
f03fc89f 608 }
08a19f64
RD
609
610 // Add any difference between size and client size
611 wxSize diff = GetSize() - GetClientSize();
612 best.x += wxMax(0, diff.x);
613 best.y += wxMax(0, diff.y);
614
615 return best;
f03fc89f
VZ
616}
617
333d7052
VZ
618// helper of GetWindowBorderSize(): as many ports don't implement support for
619// wxSYS_BORDER/EDGE_X/Y metrics in their wxSystemSettings, use hard coded
620// fallbacks in this case
d45a01e0 621static int wxGetMetricOrDefault(wxSystemMetric what, const wxWindowBase* win)
333d7052 622{
d45a01e0
PC
623 int rc = wxSystemSettings::GetMetric(
624 what, static_cast<wxWindow*>(const_cast<wxWindowBase*>(win)));
333d7052
VZ
625 if ( rc == -1 )
626 {
627 switch ( what )
628 {
629 case wxSYS_BORDER_X:
630 case wxSYS_BORDER_Y:
631 // 2D border is by default 1 pixel wide
632 rc = 1;
633 break;
634
635 case wxSYS_EDGE_X:
636 case wxSYS_EDGE_Y:
637 // 3D borders are by default 2 pixels
638 rc = 2;
639 break;
640
641 default:
642 wxFAIL_MSG( _T("unexpected wxGetMetricOrDefault() argument") );
643 rc = 0;
644 }
645 }
646
647 return rc;
648}
649
650wxSize wxWindowBase::GetWindowBorderSize() const
651{
652 wxSize size;
653
654 switch ( GetBorder() )
655 {
656 case wxBORDER_NONE:
657 // nothing to do, size is already (0, 0)
658 break;
659
660 case wxBORDER_SIMPLE:
661 case wxBORDER_STATIC:
d45a01e0
PC
662 size.x = wxGetMetricOrDefault(wxSYS_BORDER_X, this);
663 size.y = wxGetMetricOrDefault(wxSYS_BORDER_Y, this);
333d7052
VZ
664 break;
665
666 case wxBORDER_SUNKEN:
667 case wxBORDER_RAISED:
d45a01e0
PC
668 size.x = wxMax(wxGetMetricOrDefault(wxSYS_EDGE_X, this),
669 wxGetMetricOrDefault(wxSYS_BORDER_X, this));
670 size.y = wxMax(wxGetMetricOrDefault(wxSYS_EDGE_Y, this),
671 wxGetMetricOrDefault(wxSYS_BORDER_Y, this));
333d7052
VZ
672 break;
673
674 case wxBORDER_DOUBLE:
d45a01e0
PC
675 size.x = wxGetMetricOrDefault(wxSYS_EDGE_X, this) +
676 wxGetMetricOrDefault(wxSYS_BORDER_X, this);
677 size.y = wxGetMetricOrDefault(wxSYS_EDGE_Y, this) +
678 wxGetMetricOrDefault(wxSYS_BORDER_Y, this);
333d7052
VZ
679 break;
680
681 default:
682 wxFAIL_MSG(_T("Unknown border style."));
683 break;
684 }
685
686 // we have borders on both sides
687 return size*2;
688}
9f884528 689
170acdc9 690wxSize wxWindowBase::GetEffectiveMinSize() const
400a9e41 691{
9f884528
RD
692 // merge the best size with the min size, giving priority to the min size
693 wxSize min = GetMinSize();
1b942d5f 694
9f884528 695 if (min.x == wxDefaultCoord || min.y == wxDefaultCoord)
400a9e41 696 {
9f884528
RD
697 wxSize best = GetBestSize();
698 if (min.x == wxDefaultCoord) min.x = best.x;
699 if (min.y == wxDefaultCoord) min.y = best.y;
17c48da8 700 }
1b942d5f 701
9f884528
RD
702 return min;
703}
17c48da8 704
26f7724c
RR
705wxSize wxWindowBase::GetBestSize() const
706{
707 if ((!m_windowSizer) && (m_bestSizeCache.IsFullySpecified()))
708 return m_bestSizeCache;
1b942d5f 709
26f7724c
RR
710 return DoGetBestSize();
711}
42cfa184 712
e81c7155 713void wxWindowBase::SetMinSize(const wxSize& minSize)
1b942d5f
FM
714{
715 m_minWidth = minSize.x;
716 m_minHeight = minSize.y;
e81c7155
RR
717}
718
719void wxWindowBase::SetMaxSize(const wxSize& maxSize)
1b942d5f
FM
720{
721 m_maxWidth = maxSize.x;
e81c7155
RR
722 m_maxHeight = maxSize.y;
723}
724
170acdc9 725void wxWindowBase::SetInitialSize(const wxSize& size)
9f884528
RD
726{
727 // Set the min size to the size passed in. This will usually either be
728 // wxDefaultSize or the size passed to this window's ctor/Create function.
729 SetMinSize(size);
730
731 // Merge the size with the best size if needed
170acdc9 732 wxSize best = GetEffectiveMinSize();
cab1a605 733
9f884528
RD
734 // If the current size doesn't match then change it
735 if (GetSize() != best)
736 SetSize(best);
400a9e41
VZ
737}
738
9f884528 739
1e6feb95
VZ
740// by default the origin is not shifted
741wxPoint wxWindowBase::GetClientAreaOrigin() const
742{
c47addef 743 return wxPoint(0,0);
1e6feb95
VZ
744}
745
301d7a0d
VS
746wxSize wxWindowBase::ClientToWindowSize(const wxSize& size) const
747{
748 const wxSize diff(GetSize() - GetClientSize());
749
750 return wxSize(size.x == -1 ? -1 : size.x + diff.x,
751 size.y == -1 ? -1 : size.y + diff.y);
752}
753
754wxSize wxWindowBase::WindowToClientSize(const wxSize& size) const
755{
756 const wxSize diff(GetSize() - GetClientSize());
757
758 return wxSize(size.x == -1 ? -1 : size.x - diff.x,
759 size.y == -1 ? -1 : size.y - diff.y);
760}
761
69d90995
SC
762void wxWindowBase::SetWindowVariant( wxWindowVariant variant )
763{
1b69c815
VZ
764 if ( m_windowVariant != variant )
765 {
766 m_windowVariant = variant;
69d90995 767
1b69c815
VZ
768 DoSetWindowVariant(variant);
769 }
69d90995
SC
770}
771
772void wxWindowBase::DoSetWindowVariant( wxWindowVariant variant )
773{
1b69c815
VZ
774 // adjust the font height to correspond to our new variant (notice that
775 // we're only called if something really changed)
776 wxFont font = GetFont();
777 int size = font.GetPointSize();
69d90995
SC
778 switch ( variant )
779 {
1b69c815
VZ
780 case wxWINDOW_VARIANT_NORMAL:
781 break;
782
783 case wxWINDOW_VARIANT_SMALL:
784 size *= 3;
785 size /= 4;
786 break;
787
788 case wxWINDOW_VARIANT_MINI:
789 size *= 2;
790 size /= 3;
791 break;
792
793 case wxWINDOW_VARIANT_LARGE:
794 size *= 5;
795 size /= 4;
796 break;
797
69d90995
SC
798 default:
799 wxFAIL_MSG(_T("unexpected window variant"));
1b69c815 800 break;
69d90995 801 }
1b69c815
VZ
802
803 font.SetPointSize(size);
804 SetFont(font);
69d90995
SC
805}
806
b21f4960
RR
807void wxWindowBase::DoSetSizeHints( int minW, int minH,
808 int maxW, int maxH,
f36978ba 809 int WXUNUSED(incW), int WXUNUSED(incH) )
b21f4960
RR
810{
811 wxCHECK_RET( (minW == wxDefaultCoord || maxW == wxDefaultCoord || minW <= maxW) &&
812 (minH == wxDefaultCoord || maxH == wxDefaultCoord || minH <= maxH),
813 _T("min width/height must be less than max width/height!") );
814
815 m_minWidth = minW;
816 m_maxWidth = maxW;
817 m_minHeight = minH;
818 m_maxHeight = maxH;
819}
820
821
f944aec0
VS
822#if WXWIN_COMPATIBILITY_2_8
823void wxWindowBase::SetVirtualSizeHints(int WXUNUSED(minW), int WXUNUSED(minH),
824 int WXUNUSED(maxW), int WXUNUSED(maxH))
566d84a7 825{
566d84a7
RL
826}
827
f944aec0
VS
828void wxWindowBase::SetVirtualSizeHints(const wxSize& WXUNUSED(minsize),
829 const wxSize& WXUNUSED(maxsize))
566d84a7 830{
f944aec0
VS
831}
832#endif // WXWIN_COMPATIBILITY_2_8
1e2a653b 833
f944aec0
VS
834void wxWindowBase::DoSetVirtualSize( int x, int y )
835{
1e2a653b 836 m_virtualSize = wxSize(x, y);
566d84a7
RL
837}
838
839wxSize wxWindowBase::DoGetVirtualSize() const
840{
143e2719
VZ
841 // we should use the entire client area so if it is greater than our
842 // virtual size, expand it to fit (otherwise if the window is big enough we
843 // wouldn't be using parts of it)
c20ab85b 844 wxSize size = GetClientSize();
143e2719 845 if ( m_virtualSize.x > size.x )
c20ab85b
VZ
846 size.x = m_virtualSize.x;
847
143e2719 848 if ( m_virtualSize.y >= size.y )
c20ab85b
VZ
849 size.y = m_virtualSize.y;
850
851 return size;
566d84a7
RL
852}
853
3c81c9aa
VZ
854void wxWindowBase::DoGetScreenPosition(int *x, int *y) const
855{
856 // screen position is the same as (0, 0) in client coords for non TLWs (and
857 // TLWs override this method)
858 if ( x )
859 *x = 0;
860 if ( y )
861 *y = 0;
862
aa738eb8 863 ClientToScreen(x, y);
3c81c9aa
VZ
864}
865
ecdc1183 866void wxWindowBase::SendSizeEvent(int flags)
0dba08dd
VZ
867{
868 wxSizeEvent event(GetSize(), GetId());
869 event.SetEventObject(this);
ecdc1183
VZ
870 if ( flags & wxSEND_EVENT_POST )
871 wxPostEvent(this, event);
872 else
873 HandleWindowEvent(event);
0dba08dd
VZ
874}
875
ecdc1183 876void wxWindowBase::SendSizeEventToParent(int flags)
0dba08dd
VZ
877{
878 wxWindow * const parent = GetParent();
879 if ( parent && !parent->IsBeingDeleted() )
ecdc1183 880 parent->SendSizeEvent(flags);
0dba08dd
VZ
881}
882
f03fc89f
VZ
883// ----------------------------------------------------------------------------
884// show/hide/enable/disable the window
885// ----------------------------------------------------------------------------
886
887bool wxWindowBase::Show(bool show)
888{
889 if ( show != m_isShown )
890 {
891 m_isShown = show;
892
e11f4636 893 return true;
f03fc89f
VZ
894 }
895 else
896 {
e11f4636 897 return false;
f03fc89f
VZ
898 }
899}
900
47a8a4d5 901bool wxWindowBase::IsEnabled() const
f03fc89f 902{
47a8a4d5
VZ
903 return IsThisEnabled() && (IsTopLevel() || !GetParent() || GetParent()->IsEnabled());
904}
f03fc89f 905
47a8a4d5
VZ
906void wxWindowBase::NotifyWindowOnEnableChange(bool enabled)
907{
908#ifndef wxHAS_NATIVE_ENABLED_MANAGEMENT
909 DoEnable(enabled);
910#endif // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
911
912 OnEnabled(enabled);
913
914 // If we are top-level then the logic doesn't apply - otherwise
915 // showing a modal dialog would result in total greying out (and ungreying
916 // out later) of everything which would be really ugly
917 if ( IsTopLevel() )
918 return;
919
920 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
921 node;
922 node = node->GetNext() )
f03fc89f 923 {
47a8a4d5
VZ
924 wxWindowBase * const child = node->GetData();
925 if ( !child->IsTopLevel() && child->IsThisEnabled() )
926 child->NotifyWindowOnEnableChange(enabled);
927 }
928}
929
930bool wxWindowBase::Enable(bool enable)
931{
932 if ( enable == IsThisEnabled() )
e11f4636 933 return false;
47a8a4d5
VZ
934
935 m_isEnabled = enable;
936
937#ifdef wxHAS_NATIVE_ENABLED_MANAGEMENT
938 DoEnable(enable);
939#else // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
940 wxWindowBase * const parent = GetParent();
941 if( !IsTopLevel() && parent && !parent->IsEnabled() )
942 {
943 return true;
f03fc89f 944 }
47a8a4d5
VZ
945#endif // !defined(wxHAS_NATIVE_ENABLED_MANAGEMENT)
946
947 NotifyWindowOnEnableChange(enable);
948
949 return true;
f03fc89f 950}
9c72cf76 951
865a74c7 952bool wxWindowBase::IsShownOnScreen() const
9c72cf76 953{
8a6a997f
VS
954 // A window is shown on screen if it itself is shown and so are all its
955 // parents. But if a window is toplevel one, then its always visible on
956 // screen if IsShown() returns true, even if it has a hidden parent.
865a74c7 957 return IsShown() &&
8a6a997f 958 (IsTopLevel() || GetParent() == NULL || GetParent()->IsShownOnScreen());
9c72cf76
VS
959}
960
34636400
VZ
961// ----------------------------------------------------------------------------
962// RTTI
963// ----------------------------------------------------------------------------
964
965bool wxWindowBase::IsTopLevel() const
966{
e11f4636 967 return false;
34636400 968}
f03fc89f 969
1c8e5c51
VS
970// ----------------------------------------------------------------------------
971// Freeze/Thaw
972// ----------------------------------------------------------------------------
973
974void wxWindowBase::Freeze()
975{
976 if ( !m_freezeCount++ )
977 {
978 // physically freeze this window:
979 DoFreeze();
980
981 // and recursively freeze all children:
982 for ( wxWindowList::iterator i = GetChildren().begin();
983 i != GetChildren().end(); ++i )
984 {
985 wxWindow *child = *i;
986 if ( child->IsTopLevel() )
987 continue;
988
989 child->Freeze();
990 }
991 }
992}
993
994void wxWindowBase::Thaw()
995{
996 wxASSERT_MSG( m_freezeCount, "Thaw() without matching Freeze()" );
997
998 if ( !--m_freezeCount )
999 {
1000 // recursively thaw all children:
1001 for ( wxWindowList::iterator i = GetChildren().begin();
1002 i != GetChildren().end(); ++i )
1003 {
1004 wxWindow *child = *i;
1005 if ( child->IsTopLevel() )
1006 continue;
1007
1008 child->Thaw();
1009 }
1010
1011 // physically thaw this window:
1012 DoThaw();
1013 }
1014}
1015
f03fc89f
VZ
1016// ----------------------------------------------------------------------------
1017// reparenting the window
1018// ----------------------------------------------------------------------------
1019
1020void wxWindowBase::AddChild(wxWindowBase *child)
1021{
223d09f6 1022 wxCHECK_RET( child, wxT("can't add a NULL child") );
f03fc89f 1023
f6bcfd97
BP
1024 // this should never happen and it will lead to a crash later if it does
1025 // because RemoveChild() will remove only one node from the children list
1026 // and the other(s) one(s) will be left with dangling pointers in them
222ed1d6 1027 wxASSERT_MSG( !GetChildren().Find((wxWindow*)child), _T("AddChild() called twice") );
f6bcfd97 1028
222ed1d6 1029 GetChildren().Append((wxWindow*)child);
f03fc89f 1030 child->SetParent(this);
1c8e5c51 1031
76be36e4 1032 // adding a child while frozen will assert when thawed, so freeze it as if
1c8e5c51
VS
1033 // it had been already present when we were frozen
1034 if ( IsFrozen() && !child->IsTopLevel() )
1035 child->Freeze();
f03fc89f
VZ
1036}
1037
1038void wxWindowBase::RemoveChild(wxWindowBase *child)
1039{
223d09f6 1040 wxCHECK_RET( child, wxT("can't remove a NULL child") );
f03fc89f 1041
1c8e5c51
VS
1042 // removing a child while frozen may result in permanently frozen window
1043 // if used e.g. from Reparent(), so thaw it
76be36e4
VZ
1044 //
1045 // NB: IsTopLevel() doesn't return true any more when a TLW child is being
1046 // removed from its ~wxWindowBase, so check for IsBeingDeleted() too
1047 if ( IsFrozen() && !child->IsBeingDeleted() && !child->IsTopLevel() )
1c8e5c51
VS
1048 child->Thaw();
1049
222ed1d6
MB
1050 GetChildren().DeleteObject((wxWindow *)child);
1051 child->SetParent(NULL);
f03fc89f 1052}
439b3bf1 1053
f03fc89f
VZ
1054bool wxWindowBase::Reparent(wxWindowBase *newParent)
1055{
1056 wxWindow *oldParent = GetParent();
1057 if ( newParent == oldParent )
1058 {
1059 // nothing done
e11f4636 1060 return false;
f03fc89f
VZ
1061 }
1062
47a8a4d5
VZ
1063 const bool oldEnabledState = IsEnabled();
1064
f03fc89f
VZ
1065 // unlink this window from the existing parent.
1066 if ( oldParent )
1067 {
1068 oldParent->RemoveChild(this);
1069 }
1070 else
1071 {
222ed1d6 1072 wxTopLevelWindows.DeleteObject((wxWindow *)this);
f03fc89f
VZ
1073 }
1074
1075 // add it to the new one
1076 if ( newParent )
1077 {
1078 newParent->AddChild(this);
1079 }
1080 else
1081 {
222ed1d6 1082 wxTopLevelWindows.Append((wxWindow *)this);
f03fc89f
VZ
1083 }
1084
47a8a4d5
VZ
1085 // We need to notify window (and its subwindows) if by changing the parent
1086 // we also change our enabled/disabled status.
1087 const bool newEnabledState = IsEnabled();
1088 if ( newEnabledState != oldEnabledState )
1089 {
1090 NotifyWindowOnEnableChange(newEnabledState);
1091 }
1092
e11f4636 1093 return true;
f03fc89f
VZ
1094}
1095
1096// ----------------------------------------------------------------------------
1097// event handler stuff
1098// ----------------------------------------------------------------------------
1099
7f853dd0 1100void wxWindowBase::SetEventHandler(wxEvtHandler *handler)
f03fc89f 1101{
7f853dd0
FM
1102 wxCHECK_RET(handler != NULL, "SetEventHandler(NULL) called");
1103
1104 m_eventHandler = handler;
1105}
1106
1107void wxWindowBase::SetNextHandler(wxEvtHandler *WXUNUSED(handler))
1108{
1109 // disable wxEvtHandler chain mechanism for wxWindows:
1110 // wxWindow uses its own stack mechanism which doesn't mix well with wxEvtHandler's one
1111
1112 wxFAIL_MSG("wxWindow cannot be part of a wxEvtHandler chain");
1113}
1114void wxWindowBase::SetPreviousHandler(wxEvtHandler *WXUNUSED(handler))
1115{
1116 // we can't simply wxFAIL here as in SetNextHandler: in fact the last
1117 // handler of our stack when is destroyed will be Unlink()ed and thus
1118 // will call this function to update the pointer of this window...
1119
1120 //wxFAIL_MSG("wxWindow cannot be part of a wxEvtHandler chain");
1121}
1122
1123void wxWindowBase::PushEventHandler(wxEvtHandler *handlerToPush)
1124{
1125 wxCHECK_RET( handlerToPush != NULL, "PushEventHandler(NULL) called" );
1126
1127 // the new handler is going to be part of the wxWindow stack of event handlers:
1128 // it can't be part also of an event handler double-linked chain:
1129 wxASSERT_MSG(handlerToPush->IsUnlinked(),
1130 "The handler being pushed in the wxWindow stack shouldn't be part of "
1131 "a wxEvtHandler chain; call Unlink() on it first");
1132
3a074ba8 1133 wxEvtHandler *handlerOld = GetEventHandler();
7f853dd0
FM
1134 wxCHECK_RET( handlerOld, "an old event handler is NULL?" );
1135
1136 // now use wxEvtHandler double-linked list to implement a stack:
1137 handlerToPush->SetNextHandler(handlerOld);
3a074ba8 1138
7f853dd0
FM
1139 if (handlerOld != this)
1140 handlerOld->SetPreviousHandler(handlerToPush);
3a074ba8 1141
7f853dd0 1142 SetEventHandler(handlerToPush);
3a074ba8 1143
4b6a582b 1144#if wxDEBUG_LEVEL
7f853dd0
FM
1145 // final checks of the operations done above:
1146 wxASSERT_MSG( handlerToPush->GetPreviousHandler() == NULL,
4b6a582b
VZ
1147 "the first handler of the wxWindow stack should "
1148 "have no previous handlers set" );
7f853dd0 1149 wxASSERT_MSG( handlerToPush->GetNextHandler() != NULL,
4b6a582b
VZ
1150 "the first handler of the wxWindow stack should "
1151 "have non-NULL next handler" );
7f853dd0
FM
1152
1153 wxEvtHandler* pLast = handlerToPush;
4b6a582b 1154 while ( pLast && pLast != this )
7f853dd0
FM
1155 pLast = pLast->GetNextHandler();
1156 wxASSERT_MSG( pLast->GetNextHandler() == NULL,
4b6a582b
VZ
1157 "the last handler of the wxWindow stack should "
1158 "have this window as next handler" );
1159#endif // wxDEBUG_LEVEL
f03fc89f
VZ
1160}
1161
1162wxEvtHandler *wxWindowBase::PopEventHandler(bool deleteHandler)
1163{
7f853dd0 1164 // we need to pop the wxWindow stack, i.e. we need to remove the first handler
3a074ba8 1165
7f853dd0
FM
1166 wxEvtHandler *firstHandler = GetEventHandler();
1167 wxCHECK_MSG( firstHandler != NULL, NULL, "wxWindow cannot have a NULL event handler" );
1168 wxCHECK_MSG( firstHandler != this, NULL, "cannot pop the wxWindow itself" );
1169 wxCHECK_MSG( firstHandler->GetPreviousHandler() == NULL, NULL,
1170 "the first handler of the wxWindow stack should have no previous handlers set" );
3a074ba8 1171
7f853dd0
FM
1172 wxEvtHandler *secondHandler = firstHandler->GetNextHandler();
1173 wxCHECK_MSG( secondHandler != NULL, NULL,
1174 "the first handler of the wxWindow stack should have non-NULL next handler" );
1175
1176 firstHandler->SetNextHandler(NULL);
1177 secondHandler->SetPreviousHandler(NULL);
1178
1179 // now firstHandler is completely unlinked; set secondHandler as the new window event handler
1180 SetEventHandler(secondHandler);
1181
1182 if ( deleteHandler )
1183 {
1184 delete firstHandler;
1185 firstHandler = NULL;
f03fc89f
VZ
1186 }
1187
7f853dd0 1188 return firstHandler;
f03fc89f
VZ
1189}
1190
7f853dd0 1191bool wxWindowBase::RemoveEventHandler(wxEvtHandler *handlerToRemove)
2e36d5cf 1192{
7f853dd0
FM
1193 wxCHECK_MSG( handlerToRemove != NULL, false, "RemoveEventHandler(NULL) called" );
1194 wxCHECK_MSG( handlerToRemove != this, false, "Cannot remove the window itself" );
1195
1196 if (handlerToRemove == GetEventHandler())
1197 {
1198 // removing the first event handler is equivalent to "popping" the stack
1199 PopEventHandler(false);
1200 return true;
1201 }
2e36d5cf 1202
7f853dd0
FM
1203 // NOTE: the wxWindow event handler list is always terminated with "this" handler
1204 wxEvtHandler *handlerCur = GetEventHandler()->GetNextHandler();
1205 while ( handlerCur != this )
2e36d5cf
VZ
1206 {
1207 wxEvtHandler *handlerNext = handlerCur->GetNextHandler();
1208
7f853dd0 1209 if ( handlerCur == handlerToRemove )
2e36d5cf 1210 {
7f853dd0 1211 handlerCur->Unlink();
2e36d5cf 1212
7f853dd0
FM
1213 wxASSERT_MSG( handlerCur != GetEventHandler(),
1214 "the case Remove == Pop should was already handled" );
e11f4636 1215 return true;
2e36d5cf
VZ
1216 }
1217
2e36d5cf
VZ
1218 handlerCur = handlerNext;
1219 }
1220
1221 wxFAIL_MSG( _T("where has the event handler gone?") );
1222
e11f4636 1223 return false;
2e36d5cf
VZ
1224}
1225
937013e0
VS
1226bool wxWindowBase::HandleWindowEvent(wxEvent& event) const
1227{
7f853dd0 1228 // SafelyProcessEvent() will handle exceptions nicely
937013e0
VS
1229 return GetEventHandler()->SafelyProcessEvent(event);
1230}
1231
f03fc89f 1232// ----------------------------------------------------------------------------
1b69c815 1233// colours, fonts &c
f03fc89f
VZ
1234// ----------------------------------------------------------------------------
1235
b8e7b673
VZ
1236void wxWindowBase::InheritAttributes()
1237{
8afef133 1238 const wxWindowBase * const parent = GetParent();
b8e7b673
VZ
1239 if ( !parent )
1240 return;
1241
1242 // we only inherit attributes which had been explicitly set for the parent
1243 // which ensures that this only happens if the user really wants it and
1244 // not by default which wouldn't make any sense in modern GUIs where the
1245 // controls don't all use the same fonts (nor colours)
f8ff87ed 1246 if ( parent->m_inheritFont && !m_hasFont )
b8e7b673
VZ
1247 SetFont(parent->GetFont());
1248
1249 // in addition, there is a possibility to explicitly forbid inheriting
1250 // colours at each class level by overriding ShouldInheritColours()
1251 if ( ShouldInheritColours() )
1252 {
f8ff87ed 1253 if ( parent->m_inheritFgCol && !m_hasFgCol )
b8e7b673
VZ
1254 SetForegroundColour(parent->GetForegroundColour());
1255
eb96212b
VZ
1256 // inheriting (solid) background colour is wrong as it totally breaks
1257 // any kind of themed backgrounds
1258 //
1259 // instead, the controls should use the same background as their parent
1260 // (ideally by not drawing it at all)
1261#if 0
f8ff87ed 1262 if ( parent->m_inheritBgCol && !m_hasBgCol )
b8e7b673 1263 SetBackgroundColour(parent->GetBackgroundColour());
eb96212b 1264#endif // 0
b8e7b673
VZ
1265 }
1266}
1267
1b69c815
VZ
1268/* static */ wxVisualAttributes
1269wxWindowBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1270{
1271 // it is important to return valid values for all attributes from here,
1272 // GetXXX() below rely on this
1273 wxVisualAttributes attrs;
1274 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
1275 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
1b69c815 1276
0477a857
JS
1277 // On Smartphone/PocketPC, wxSYS_COLOUR_WINDOW is a better reflection of
1278 // the usual background colour than wxSYS_COLOUR_BTNFACE.
1279 // It's a pity that wxSYS_COLOUR_WINDOW isn't always a suitable background
1280 // colour on other platforms.
1281
1282#if defined(__WXWINCE__) && (defined(__SMARTPHONE__) || defined(__POCKETPC__))
1283 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1284#else
1285 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1286#endif
1b69c815
VZ
1287 return attrs;
1288}
1289
1290wxColour wxWindowBase::GetBackgroundColour() const
1291{
1117d56f 1292 if ( !m_backgroundColour.IsOk() )
1b69c815
VZ
1293 {
1294 wxASSERT_MSG( !m_hasBgCol, _T("we have invalid explicit bg colour?") );
1295
1296 // get our default background colour
1297 wxColour colBg = GetDefaultAttributes().colBg;
1298
1299 // we must return some valid colour to avoid redoing this every time
1300 // and also to avoid surprizing the applications written for older
77ffb593 1301 // wxWidgets versions where GetBackgroundColour() always returned
1b69c815
VZ
1302 // something -- so give them something even if it doesn't make sense
1303 // for this window (e.g. it has a themed background)
1304 if ( !colBg.Ok() )
1305 colBg = GetClassDefaultAttributes().colBg;
1306
f604c658 1307 return colBg;
1b69c815 1308 }
f604c658
VS
1309 else
1310 return m_backgroundColour;
1b69c815
VZ
1311}
1312
1313wxColour wxWindowBase::GetForegroundColour() const
1314{
1315 // logic is the same as above
1316 if ( !m_hasFgCol && !m_foregroundColour.Ok() )
1317 {
1b69c815
VZ
1318 wxColour colFg = GetDefaultAttributes().colFg;
1319
1117d56f 1320 if ( !colFg.IsOk() )
1b69c815
VZ
1321 colFg = GetClassDefaultAttributes().colFg;
1322
f604c658 1323 return colFg;
1b69c815 1324 }
f604c658
VS
1325 else
1326 return m_foregroundColour;
1b69c815
VZ
1327}
1328
f03fc89f
VZ
1329bool wxWindowBase::SetBackgroundColour( const wxColour &colour )
1330{
cab1a605 1331 if ( colour == m_backgroundColour )
e11f4636 1332 return false;
f03fc89f 1333
1117d56f 1334 m_hasBgCol = colour.IsOk();
17bcd9a6
VZ
1335 if ( m_backgroundStyle != wxBG_STYLE_CUSTOM )
1336 m_backgroundStyle = m_hasBgCol ? wxBG_STYLE_COLOUR : wxBG_STYLE_SYSTEM;
1337
f8ff87ed 1338 m_inheritBgCol = m_hasBgCol;
f03fc89f 1339 m_backgroundColour = colour;
44dfb5ce 1340 SetThemeEnabled( !m_hasBgCol && !m_foregroundColour.Ok() );
e11f4636 1341 return true;
f03fc89f
VZ
1342}
1343
1344bool wxWindowBase::SetForegroundColour( const wxColour &colour )
1345{
44dfb5ce 1346 if (colour == m_foregroundColour )
e11f4636 1347 return false;
f03fc89f 1348
1117d56f 1349 m_hasFgCol = colour.IsOk();
f8ff87ed 1350 m_inheritFgCol = m_hasFgCol;
f03fc89f 1351 m_foregroundColour = colour;
44dfb5ce 1352 SetThemeEnabled( !m_hasFgCol && !m_backgroundColour.Ok() );
e11f4636 1353 return true;
f03fc89f
VZ
1354}
1355
1356bool wxWindowBase::SetCursor(const wxCursor& cursor)
1357{
8a9c2246
VZ
1358 // setting an invalid cursor is ok, it means that we don't have any special
1359 // cursor
a3ab1c18 1360 if ( m_cursor.IsSameAs(cursor) )
f03fc89f
VZ
1361 {
1362 // no change
e11f4636 1363 return false;
f03fc89f
VZ
1364 }
1365
8a9c2246 1366 m_cursor = cursor;
f03fc89f 1367
e11f4636 1368 return true;
f03fc89f
VZ
1369}
1370
f604c658 1371wxFont wxWindowBase::GetFont() const
1b69c815
VZ
1372{
1373 // logic is the same as in GetBackgroundColour()
1117d56f 1374 if ( !m_font.IsOk() )
1b69c815
VZ
1375 {
1376 wxASSERT_MSG( !m_hasFont, _T("we have invalid explicit font?") );
1377
1378 wxFont font = GetDefaultAttributes().font;
1117d56f 1379 if ( !font.IsOk() )
1b69c815
VZ
1380 font = GetClassDefaultAttributes().font;
1381
f604c658 1382 return font;
1b69c815 1383 }
f604c658
VS
1384 else
1385 return m_font;
1b69c815
VZ
1386}
1387
f03fc89f
VZ
1388bool wxWindowBase::SetFont(const wxFont& font)
1389{
1b69c815 1390 if ( font == m_font )
f03fc89f
VZ
1391 {
1392 // no change
e11f4636 1393 return false;
f03fc89f
VZ
1394 }
1395
1b69c815 1396 m_font = font;
1117d56f 1397 m_hasFont = font.IsOk();
f8ff87ed 1398 m_inheritFont = m_hasFont;
23895080 1399
583d7e55
VS
1400 InvalidateBestSize();
1401
e11f4636 1402 return true;
f03fc89f
VZ
1403}
1404
b95edd47
VZ
1405#if wxUSE_PALETTE
1406
1407void wxWindowBase::SetPalette(const wxPalette& pal)
1408{
e11f4636 1409 m_hasCustomPalette = true;
b95edd47
VZ
1410 m_palette = pal;
1411
1412 // VZ: can anyone explain me what do we do here?
1413 wxWindowDC d((wxWindow *) this);
1414 d.SetPalette(pal);
1415}
1416
1417wxWindow *wxWindowBase::GetAncestorWithCustomPalette() const
1418{
1419 wxWindow *win = (wxWindow *)this;
1420 while ( win && !win->HasCustomPalette() )
1421 {
1422 win = win->GetParent();
1423 }
1424
1425 return win;
1426}
1427
1428#endif // wxUSE_PALETTE
1429
789295bf
VZ
1430#if wxUSE_CARET
1431void wxWindowBase::SetCaret(wxCaret *caret)
1432{
1433 if ( m_caret )
1434 {
1435 delete m_caret;
1436 }
1437
1438 m_caret = caret;
1439
1440 if ( m_caret )
1441 {
1442 wxASSERT_MSG( m_caret->GetWindow() == this,
223d09f6 1443 wxT("caret should be created associated to this window") );
789295bf
VZ
1444 }
1445}
1446#endif // wxUSE_CARET
1447
88ac883a 1448#if wxUSE_VALIDATORS
f03fc89f
VZ
1449// ----------------------------------------------------------------------------
1450// validators
1451// ----------------------------------------------------------------------------
1452
1453void wxWindowBase::SetValidator(const wxValidator& validator)
1454{
1455 if ( m_windowValidator )
1456 delete m_windowValidator;
1457
1458 m_windowValidator = (wxValidator *)validator.Clone();
1459
1460 if ( m_windowValidator )
1b69c815 1461 m_windowValidator->SetWindow(this);
f03fc89f 1462}
88ac883a 1463#endif // wxUSE_VALIDATORS
f03fc89f
VZ
1464
1465// ----------------------------------------------------------------------------
1e6feb95 1466// update region stuff
f03fc89f
VZ
1467// ----------------------------------------------------------------------------
1468
1e6feb95
VZ
1469wxRect wxWindowBase::GetUpdateClientRect() const
1470{
1471 wxRegion rgnUpdate = GetUpdateRegion();
1472 rgnUpdate.Intersect(GetClientRect());
1473 wxRect rectUpdate = rgnUpdate.GetBox();
1474 wxPoint ptOrigin = GetClientAreaOrigin();
1475 rectUpdate.x -= ptOrigin.x;
1476 rectUpdate.y -= ptOrigin.y;
1477
1478 return rectUpdate;
1479}
1480
657b4fd4 1481bool wxWindowBase::DoIsExposed(int x, int y) const
f03fc89f
VZ
1482{
1483 return m_updateRegion.Contains(x, y) != wxOutRegion;
1484}
1485
657b4fd4 1486bool wxWindowBase::DoIsExposed(int x, int y, int w, int h) const
f03fc89f
VZ
1487{
1488 return m_updateRegion.Contains(x, y, w, h) != wxOutRegion;
1489}
1490
5da0803c
VZ
1491void wxWindowBase::ClearBackground()
1492{
1493 // wxGTK uses its own version, no need to add never used code
1494#ifndef __WXGTK__
1495 wxClientDC dc((wxWindow *)this);
cb129171 1496 wxBrush brush(GetBackgroundColour(), wxBRUSHSTYLE_SOLID);
5da0803c
VZ
1497 dc.SetBackground(brush);
1498 dc.Clear();
1499#endif // __WXGTK__
1500}
1501
f03fc89f 1502// ----------------------------------------------------------------------------
146ba0fe 1503// find child window by id or name
f03fc89f
VZ
1504// ----------------------------------------------------------------------------
1505
6b73af79 1506wxWindow *wxWindowBase::FindWindow(long id) const
f03fc89f
VZ
1507{
1508 if ( id == m_windowId )
1509 return (wxWindow *)this;
1510
d3b9f782 1511 wxWindowBase *res = NULL;
222ed1d6 1512 wxWindowList::compatibility_iterator node;
f03fc89f
VZ
1513 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
1514 {
1515 wxWindowBase *child = node->GetData();
1516 res = child->FindWindow( id );
1517 }
1518
1519 return (wxWindow *)res;
1520}
1521
6b73af79 1522wxWindow *wxWindowBase::FindWindow(const wxString& name) const
f03fc89f
VZ
1523{
1524 if ( name == m_windowName )
1525 return (wxWindow *)this;
1526
d3b9f782 1527 wxWindowBase *res = NULL;
222ed1d6 1528 wxWindowList::compatibility_iterator node;
f03fc89f
VZ
1529 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
1530 {
1531 wxWindow *child = node->GetData();
1532 res = child->FindWindow(name);
1533 }
1534
1535 return (wxWindow *)res;
1536}
1537
146ba0fe
VZ
1538
1539// find any window by id or name or label: If parent is non-NULL, look through
1540// children for a label or title matching the specified string. If NULL, look
1541// through all top-level windows.
1542//
1543// to avoid duplicating code we reuse the same helper function but with
1544// different comparators
1545
1546typedef bool (*wxFindWindowCmp)(const wxWindow *win,
1547 const wxString& label, long id);
1548
1549static
1550bool wxFindWindowCmpLabels(const wxWindow *win, const wxString& label,
1551 long WXUNUSED(id))
1552{
1553 return win->GetLabel() == label;
1554}
1555
1556static
1557bool wxFindWindowCmpNames(const wxWindow *win, const wxString& label,
1558 long WXUNUSED(id))
1559{
1560 return win->GetName() == label;
1561}
1562
1563static
1564bool wxFindWindowCmpIds(const wxWindow *win, const wxString& WXUNUSED(label),
1565 long id)
1566{
1567 return win->GetId() == id;
1568}
1569
1570// recursive helper for the FindWindowByXXX() functions
1571static
1572wxWindow *wxFindWindowRecursively(const wxWindow *parent,
1573 const wxString& label,
1574 long id,
1575 wxFindWindowCmp cmp)
1576{
1577 if ( parent )
1578 {
1579 // see if this is the one we're looking for
1580 if ( (*cmp)(parent, label, id) )
1581 return (wxWindow *)parent;
1582
1583 // It wasn't, so check all its children
222ed1d6 1584 for ( wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst();
146ba0fe
VZ
1585 node;
1586 node = node->GetNext() )
1587 {
1588 // recursively check each child
1589 wxWindow *win = (wxWindow *)node->GetData();
1590 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1591 if (retwin)
1592 return retwin;
1593 }
1594 }
1595
1596 // Not found
1597 return NULL;
1598}
1599
1600// helper for FindWindowByXXX()
1601static
1602wxWindow *wxFindWindowHelper(const wxWindow *parent,
1603 const wxString& label,
1604 long id,
1605 wxFindWindowCmp cmp)
1606{
1607 if ( parent )
1608 {
1609 // just check parent and all its children
1610 return wxFindWindowRecursively(parent, label, id, cmp);
1611 }
1612
1613 // start at very top of wx's windows
222ed1d6 1614 for ( wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
146ba0fe
VZ
1615 node;
1616 node = node->GetNext() )
1617 {
1618 // recursively check each window & its children
1619 wxWindow *win = node->GetData();
1620 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1621 if (retwin)
1622 return retwin;
1623 }
1624
1625 return NULL;
1626}
1627
1628/* static */
1629wxWindow *
1630wxWindowBase::FindWindowByLabel(const wxString& title, const wxWindow *parent)
1631{
1632 return wxFindWindowHelper(parent, title, 0, wxFindWindowCmpLabels);
1633}
1634
1635/* static */
1636wxWindow *
1637wxWindowBase::FindWindowByName(const wxString& title, const wxWindow *parent)
1638{
1639 wxWindow *win = wxFindWindowHelper(parent, title, 0, wxFindWindowCmpNames);
1640
1641 if ( !win )
1642 {
1643 // fall back to the label
1644 win = FindWindowByLabel(title, parent);
1645 }
1646
1647 return win;
1648}
1649
1650/* static */
1651wxWindow *
1652wxWindowBase::FindWindowById( long id, const wxWindow* parent )
1653{
525d8583 1654 return wxFindWindowHelper(parent, wxEmptyString, id, wxFindWindowCmpIds);
146ba0fe
VZ
1655}
1656
f03fc89f
VZ
1657// ----------------------------------------------------------------------------
1658// dialog oriented functions
1659// ----------------------------------------------------------------------------
1660
34636400 1661void wxWindowBase::MakeModal(bool modal)
f03fc89f 1662{
34636400
VZ
1663 // Disable all other windows
1664 if ( IsTopLevel() )
1665 {
222ed1d6 1666 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
34636400
VZ
1667 while (node)
1668 {
1669 wxWindow *win = node->GetData();
1670 if (win != this)
1671 win->Enable(!modal);
1672
1673 node = node->GetNext();
1674 }
1675 }
f03fc89f
VZ
1676}
1677
1678bool wxWindowBase::Validate()
1679{
88ac883a 1680#if wxUSE_VALIDATORS
d80cd92a
VZ
1681 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1682
222ed1d6 1683 wxWindowList::compatibility_iterator node;
f03fc89f
VZ
1684 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1685 {
1686 wxWindowBase *child = node->GetData();
1687 wxValidator *validator = child->GetValidator();
dcd6b914 1688 if ( validator && !validator->Validate((wxWindow *)this) )
f03fc89f 1689 {
e11f4636 1690 return false;
f03fc89f 1691 }
d80cd92a
VZ
1692
1693 if ( recurse && !child->Validate() )
1694 {
e11f4636 1695 return false;
d80cd92a 1696 }
f03fc89f 1697 }
88ac883a 1698#endif // wxUSE_VALIDATORS
f03fc89f 1699
e11f4636 1700 return true;
f03fc89f
VZ
1701}
1702
1703bool wxWindowBase::TransferDataToWindow()
1704{
88ac883a 1705#if wxUSE_VALIDATORS
d80cd92a
VZ
1706 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1707
222ed1d6 1708 wxWindowList::compatibility_iterator node;
f03fc89f
VZ
1709 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1710 {
1711 wxWindowBase *child = node->GetData();
1712 wxValidator *validator = child->GetValidator();
1713 if ( validator && !validator->TransferToWindow() )
1714 {
d80cd92a 1715 wxLogWarning(_("Could not transfer data to window"));
e30285ab 1716#if wxUSE_LOG
d80cd92a 1717 wxLog::FlushActive();
e30285ab 1718#endif // wxUSE_LOG
f03fc89f 1719
e11f4636 1720 return false;
f03fc89f 1721 }
d80cd92a
VZ
1722
1723 if ( recurse )
1724 {
a58a12e9 1725 if ( !child->TransferDataToWindow() )
d80cd92a
VZ
1726 {
1727 // warning already given
e11f4636 1728 return false;
d80cd92a
VZ
1729 }
1730 }
f03fc89f 1731 }
88ac883a 1732#endif // wxUSE_VALIDATORS
f03fc89f 1733
e11f4636 1734 return true;
f03fc89f
VZ
1735}
1736
1737bool wxWindowBase::TransferDataFromWindow()
1738{
88ac883a 1739#if wxUSE_VALIDATORS
d80cd92a
VZ
1740 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1741
222ed1d6 1742 wxWindowList::compatibility_iterator node;
f03fc89f
VZ
1743 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1744 {
1745 wxWindow *child = node->GetData();
d80cd92a
VZ
1746 wxValidator *validator = child->GetValidator();
1747 if ( validator && !validator->TransferFromWindow() )
f03fc89f 1748 {
d80cd92a
VZ
1749 // nop warning here because the application is supposed to give
1750 // one itself - we don't know here what might have gone wrongly
1751
e11f4636 1752 return false;
f03fc89f 1753 }
d80cd92a
VZ
1754
1755 if ( recurse )
1756 {
a58a12e9 1757 if ( !child->TransferDataFromWindow() )
d80cd92a
VZ
1758 {
1759 // warning already given
e11f4636 1760 return false;
d80cd92a
VZ
1761 }
1762 }
f03fc89f 1763 }
88ac883a 1764#endif // wxUSE_VALIDATORS
f03fc89f 1765
e11f4636 1766 return true;
f03fc89f
VZ
1767}
1768
1769void wxWindowBase::InitDialog()
1770{
1771 wxInitDialogEvent event(GetId());
1772 event.SetEventObject( this );
1773 GetEventHandler()->ProcessEvent(event);
1774}
1775
1776// ----------------------------------------------------------------------------
bd83cb56
VZ
1777// context-sensitive help support
1778// ----------------------------------------------------------------------------
1779
1780#if wxUSE_HELP
1781
1782// associate this help text with this window
1783void wxWindowBase::SetHelpText(const wxString& text)
1784{
1785 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1786 if ( helpProvider )
1787 {
1788 helpProvider->AddHelp(this, text);
1789 }
1790}
1791
520746ac 1792#if WXWIN_COMPATIBILITY_2_8
bd83cb56
VZ
1793// associate this help text with all windows with the same id as this
1794// one
1795void wxWindowBase::SetHelpTextForId(const wxString& text)
1796{
1797 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1798 if ( helpProvider )
1799 {
1800 helpProvider->AddHelp(GetId(), text);
1801 }
1802}
520746ac 1803#endif // WXWIN_COMPATIBILITY_2_8
bd83cb56
VZ
1804
1805// get the help string associated with this window (may be empty)
dc6588e7
VZ
1806// default implementation forwards calls to the help provider
1807wxString
1808wxWindowBase::GetHelpTextAtPoint(const wxPoint & WXUNUSED(pt),
1809 wxHelpEvent::Origin WXUNUSED(origin)) const
bd83cb56
VZ
1810{
1811 wxString text;
1812 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1813 if ( helpProvider )
1814 {
1815 text = helpProvider->GetHelp(this);
1816 }
1817
1818 return text;
1819}
1820
1821// show help for this window
1822void wxWindowBase::OnHelp(wxHelpEvent& event)
1823{
1824 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1825 if ( helpProvider )
1826 {
95d3881f
VZ
1827 wxPoint pos = event.GetPosition();
1828 const wxHelpEvent::Origin origin = event.GetOrigin();
1829 if ( origin == wxHelpEvent::Origin_Keyboard )
1830 {
1831 // if the help event was generated from keyboard it shouldn't
1832 // appear at the mouse position (which is still the only position
1833 // associated with help event) if the mouse is far away, although
1834 // we still do use the mouse position if it's over the window
1835 // because we suppose the user looks approximately at the mouse
1836 // already and so it would be more convenient than showing tooltip
1837 // at some arbitrary position which can be quite far from it
1838 const wxRect rectClient = GetClientRect();
1839 if ( !rectClient.Contains(ScreenToClient(pos)) )
1840 {
1841 // position help slightly under and to the right of this window
1842 pos = ClientToScreen(wxPoint(
1843 2*GetCharWidth(),
1844 rectClient.height + GetCharHeight()
1845 ));
1846 }
1847 }
1848
1849 if ( helpProvider->ShowHelpAtPoint(this, pos, origin) )
bd83cb56
VZ
1850 {
1851 // skip the event.Skip() below
1852 return;
1853 }
1854 }
1855
1856 event.Skip();
1857}
1858
1859#endif // wxUSE_HELP
1860
1861// ----------------------------------------------------------------------------
602a2e02 1862// tooltips
f03fc89f
VZ
1863// ----------------------------------------------------------------------------
1864
1865#if wxUSE_TOOLTIPS
1866
1867void wxWindowBase::SetToolTip( const wxString &tip )
1868{
1869 // don't create the new tooltip if we already have one
1870 if ( m_tooltip )
1871 {
1872 m_tooltip->SetTip( tip );
1873 }
1874 else
1875 {
1876 SetToolTip( new wxToolTip( tip ) );
1877 }
1878
1879 // setting empty tooltip text does not remove the tooltip any more - use
d3b9f782 1880 // SetToolTip(NULL) for this
f03fc89f
VZ
1881}
1882
1883void wxWindowBase::DoSetToolTip(wxToolTip *tooltip)
1884{
0cb57187
VZ
1885 if ( m_tooltip != tooltip )
1886 {
1887 if ( m_tooltip )
1888 delete m_tooltip;
f03fc89f 1889
0cb57187
VZ
1890 m_tooltip = tooltip;
1891 }
f03fc89f
VZ
1892}
1893
1894#endif // wxUSE_TOOLTIPS
1895
1896// ----------------------------------------------------------------------------
1897// constraints and sizers
1898// ----------------------------------------------------------------------------
1899
1900#if wxUSE_CONSTRAINTS
1901
1902void wxWindowBase::SetConstraints( wxLayoutConstraints *constraints )
1903{
1904 if ( m_constraints )
1905 {
1906 UnsetConstraints(m_constraints);
1907 delete m_constraints;
1908 }
1909 m_constraints = constraints;
1910 if ( m_constraints )
1911 {
1912 // Make sure other windows know they're part of a 'meaningful relationship'
1913 if ( m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this) )
1914 m_constraints->left.GetOtherWindow()->AddConstraintReference(this);
1915 if ( m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this) )
1916 m_constraints->top.GetOtherWindow()->AddConstraintReference(this);
1917 if ( m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this) )
1918 m_constraints->right.GetOtherWindow()->AddConstraintReference(this);
1919 if ( m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this) )
1920 m_constraints->bottom.GetOtherWindow()->AddConstraintReference(this);
1921 if ( m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this) )
1922 m_constraints->width.GetOtherWindow()->AddConstraintReference(this);
1923 if ( m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this) )
1924 m_constraints->height.GetOtherWindow()->AddConstraintReference(this);
1925 if ( m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this) )
1926 m_constraints->centreX.GetOtherWindow()->AddConstraintReference(this);
1927 if ( m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this) )
1928 m_constraints->centreY.GetOtherWindow()->AddConstraintReference(this);
1929 }
1930}
1931
1932// This removes any dangling pointers to this window in other windows'
1933// constraintsInvolvedIn lists.
1934void wxWindowBase::UnsetConstraints(wxLayoutConstraints *c)
1935{
1936 if ( c )
1937 {
1938 if ( c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
1939 c->left.GetOtherWindow()->RemoveConstraintReference(this);
1940 if ( c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
1941 c->top.GetOtherWindow()->RemoveConstraintReference(this);
1942 if ( c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this) )
1943 c->right.GetOtherWindow()->RemoveConstraintReference(this);
1944 if ( c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this) )
1945 c->bottom.GetOtherWindow()->RemoveConstraintReference(this);
1946 if ( c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this) )
1947 c->width.GetOtherWindow()->RemoveConstraintReference(this);
1948 if ( c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this) )
1949 c->height.GetOtherWindow()->RemoveConstraintReference(this);
1950 if ( c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this) )
1951 c->centreX.GetOtherWindow()->RemoveConstraintReference(this);
1952 if ( c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this) )
1953 c->centreY.GetOtherWindow()->RemoveConstraintReference(this);
1954 }
1955}
1956
1957// Back-pointer to other windows we're involved with, so if we delete this
1958// window, we must delete any constraints we're involved with.
1959void wxWindowBase::AddConstraintReference(wxWindowBase *otherWin)
1960{
1961 if ( !m_constraintsInvolvedIn )
1962 m_constraintsInvolvedIn = new wxWindowList;
222ed1d6
MB
1963 if ( !m_constraintsInvolvedIn->Find((wxWindow *)otherWin) )
1964 m_constraintsInvolvedIn->Append((wxWindow *)otherWin);
f03fc89f
VZ
1965}
1966
1967// REMOVE back-pointer to other windows we're involved with.
1968void wxWindowBase::RemoveConstraintReference(wxWindowBase *otherWin)
1969{
1970 if ( m_constraintsInvolvedIn )
222ed1d6 1971 m_constraintsInvolvedIn->DeleteObject((wxWindow *)otherWin);
f03fc89f
VZ
1972}
1973
1974// Reset any constraints that mention this window
1975void wxWindowBase::DeleteRelatedConstraints()
1976{
1977 if ( m_constraintsInvolvedIn )
1978 {
222ed1d6 1979 wxWindowList::compatibility_iterator node = m_constraintsInvolvedIn->GetFirst();
f03fc89f
VZ
1980 while (node)
1981 {
1982 wxWindow *win = node->GetData();
1983 wxLayoutConstraints *constr = win->GetConstraints();
1984
1985 // Reset any constraints involving this window
1986 if ( constr )
1987 {
1988 constr->left.ResetIfWin(this);
1989 constr->top.ResetIfWin(this);
1990 constr->right.ResetIfWin(this);
1991 constr->bottom.ResetIfWin(this);
1992 constr->width.ResetIfWin(this);
1993 constr->height.ResetIfWin(this);
1994 constr->centreX.ResetIfWin(this);
1995 constr->centreY.ResetIfWin(this);
1996 }
1997
222ed1d6
MB
1998 wxWindowList::compatibility_iterator next = node->GetNext();
1999 m_constraintsInvolvedIn->Erase(node);
f03fc89f
VZ
2000 node = next;
2001 }
2002
2003 delete m_constraintsInvolvedIn;
d3b9f782 2004 m_constraintsInvolvedIn = NULL;
f03fc89f
VZ
2005 }
2006}
ec5bb70d
VZ
2007
2008#endif // wxUSE_CONSTRAINTS
f03fc89f 2009
3aa5d532 2010void wxWindowBase::SetSizer(wxSizer *sizer, bool deleteOld)
f03fc89f 2011{
fb89cfc5
RD
2012 if ( sizer == m_windowSizer)
2013 return;
1b69c815 2014
e8cfff87
VZ
2015 if ( m_windowSizer )
2016 {
2017 m_windowSizer->SetContainingWindow(NULL);
2018
2019 if ( deleteOld )
2020 delete m_windowSizer;
2021 }
3417c2cd 2022
f03fc89f 2023 m_windowSizer = sizer;
e8cfff87
VZ
2024 if ( m_windowSizer )
2025 {
2026 m_windowSizer->SetContainingWindow((wxWindow *)this);
2027 }
566d84a7 2028
e8cfff87 2029 SetAutoLayout(m_windowSizer != NULL);
566d84a7
RL
2030}
2031
2032void wxWindowBase::SetSizerAndFit(wxSizer *sizer, bool deleteOld)
2033{
2034 SetSizer( sizer, deleteOld );
2035 sizer->SetSizeHints( (wxWindow*) this );
f03fc89f
VZ
2036}
2037
400a9e41 2038
1ebfafde
RD
2039void wxWindowBase::SetContainingSizer(wxSizer* sizer)
2040{
2041 // adding a window to a sizer twice is going to result in fatal and
2042 // hard to debug problems later because when deleting the second
2043 // associated wxSizerItem we're going to dereference a dangling
2044 // pointer; so try to detect this as early as possible
2045 wxASSERT_MSG( !sizer || m_containingSizer != sizer,
2046 _T("Adding a window to the same sizer twice?") );
1ebfafde 2047
400a9e41 2048 m_containingSizer = sizer;
1ebfafde 2049}
400a9e41 2050
ec5bb70d
VZ
2051#if wxUSE_CONSTRAINTS
2052
2053void wxWindowBase::SatisfyConstraints()
2054{
2055 wxLayoutConstraints *constr = GetConstraints();
2056 bool wasOk = constr && constr->AreSatisfied();
2057
2058 ResetConstraints(); // Mark all constraints as unevaluated
2059
2060 int noChanges = 1;
2061
2062 // if we're a top level panel (i.e. our parent is frame/dialog), our
2063 // own constraints will never be satisfied any more unless we do it
2064 // here
2065 if ( wasOk )
2066 {
2067 while ( noChanges > 0 )
2068 {
2069 LayoutPhase1(&noChanges);
2070 }
2071 }
2072
2073 LayoutPhase2(&noChanges);
2074}
2075
2076#endif // wxUSE_CONSTRAINTS
2077
f03fc89f
VZ
2078bool wxWindowBase::Layout()
2079{
3417c2cd 2080 // If there is a sizer, use it instead of the constraints
f03fc89f
VZ
2081 if ( GetSizer() )
2082 {
8d7eaf91 2083 int w = 0, h = 0;
566d84a7 2084 GetVirtualSize(&w, &h);
3417c2cd 2085 GetSizer()->SetDimension( 0, 0, w, h );
f03fc89f 2086 }
461e93f9 2087#if wxUSE_CONSTRAINTS
f1df0927 2088 else
f03fc89f 2089 {
ec5bb70d 2090 SatisfyConstraints(); // Find the right constraints values
f1df0927 2091 SetConstraintSizes(); // Recursively set the real window sizes
f03fc89f 2092 }
461e93f9 2093#endif
5d4b632b 2094
e11f4636 2095 return true;
f03fc89f
VZ
2096}
2097
461e93f9 2098#if wxUSE_CONSTRAINTS
ec5bb70d
VZ
2099
2100// first phase of the constraints evaluation: set our own constraints
f03fc89f
VZ
2101bool wxWindowBase::LayoutPhase1(int *noChanges)
2102{
2103 wxLayoutConstraints *constr = GetConstraints();
ec5bb70d
VZ
2104
2105 return !constr || constr->SatisfyConstraints(this, noChanges);
f03fc89f
VZ
2106}
2107
ec5bb70d 2108// second phase: set the constraints for our children
f03fc89f
VZ
2109bool wxWindowBase::LayoutPhase2(int *noChanges)
2110{
2111 *noChanges = 0;
2112
2113 // Layout children
2114 DoPhase(1);
ec5bb70d
VZ
2115
2116 // Layout grand children
f03fc89f 2117 DoPhase(2);
ec5bb70d 2118
e11f4636 2119 return true;
f03fc89f
VZ
2120}
2121
2122// Do a phase of evaluating child constraints
2123bool wxWindowBase::DoPhase(int phase)
2124{
ec5bb70d
VZ
2125 // the list containing the children for which the constraints are already
2126 // set correctly
f03fc89f 2127 wxWindowList succeeded;
ec5bb70d
VZ
2128
2129 // the max number of iterations we loop before concluding that we can't set
2130 // the constraints
2131 static const int maxIterations = 500;
2132
2133 for ( int noIterations = 0; noIterations < maxIterations; noIterations++ )
f03fc89f 2134 {
ec5bb70d
VZ
2135 int noChanges = 0;
2136
2137 // loop over all children setting their constraints
222ed1d6 2138 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
ec5bb70d
VZ
2139 node;
2140 node = node->GetNext() )
f03fc89f
VZ
2141 {
2142 wxWindow *child = node->GetData();
ec5bb70d 2143 if ( child->IsTopLevel() )
f03fc89f 2144 {
ec5bb70d
VZ
2145 // top level children are not inside our client area
2146 continue;
2147 }
2148
2149 if ( !child->GetConstraints() || succeeded.Find(child) )
2150 {
2151 // this one is either already ok or nothing we can do about it
2152 continue;
2153 }
2154
2155 int tempNoChanges = 0;
2156 bool success = phase == 1 ? child->LayoutPhase1(&tempNoChanges)
2157 : child->LayoutPhase2(&tempNoChanges);
2158 noChanges += tempNoChanges;
2159
2160 if ( success )
2161 {
2162 succeeded.Append(child);
f03fc89f 2163 }
f03fc89f
VZ
2164 }
2165
ec5bb70d
VZ
2166 if ( !noChanges )
2167 {
2168 // constraints are set
2169 break;
2170 }
f03fc89f
VZ
2171 }
2172
e11f4636 2173 return true;
f03fc89f
VZ
2174}
2175
2176void wxWindowBase::ResetConstraints()
2177{
2178 wxLayoutConstraints *constr = GetConstraints();
2179 if ( constr )
2180 {
e11f4636
DS
2181 constr->left.SetDone(false);
2182 constr->top.SetDone(false);
2183 constr->right.SetDone(false);
2184 constr->bottom.SetDone(false);
2185 constr->width.SetDone(false);
2186 constr->height.SetDone(false);
2187 constr->centreX.SetDone(false);
2188 constr->centreY.SetDone(false);
f03fc89f 2189 }
f1df0927 2190
222ed1d6 2191 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
f03fc89f
VZ
2192 while (node)
2193 {
2194 wxWindow *win = node->GetData();
34636400 2195 if ( !win->IsTopLevel() )
f03fc89f
VZ
2196 win->ResetConstraints();
2197 node = node->GetNext();
2198 }
2199}
2200
2201// Need to distinguish between setting the 'fake' size for windows and sizers,
2202// and setting the real values.
2203void wxWindowBase::SetConstraintSizes(bool recurse)
2204{
2205 wxLayoutConstraints *constr = GetConstraints();
4b7f2165 2206 if ( constr && constr->AreSatisfied() )
f03fc89f
VZ
2207 {
2208 int x = constr->left.GetValue();
2209 int y = constr->top.GetValue();
2210 int w = constr->width.GetValue();
2211 int h = constr->height.GetValue();
2212
f03fc89f 2213 if ( (constr->width.GetRelationship() != wxAsIs ) ||
3417c2cd 2214 (constr->height.GetRelationship() != wxAsIs) )
f03fc89f 2215 {
3417c2cd 2216 SetSize(x, y, w, h);
f03fc89f
VZ
2217 }
2218 else
2219 {
3417c2cd
RR
2220 // If we don't want to resize this window, just move it...
2221 Move(x, y);
f03fc89f
VZ
2222 }
2223 }
2224 else if ( constr )
2225 {
4b7f2165 2226 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
f1df0927 2227 GetClassInfo()->GetClassName(),
4b7f2165 2228 GetName().c_str());
f03fc89f
VZ
2229 }
2230
2231 if ( recurse )
2232 {
222ed1d6 2233 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
f03fc89f
VZ
2234 while (node)
2235 {
2236 wxWindow *win = node->GetData();
e2f9212c 2237 if ( !win->IsTopLevel() && win->GetConstraints() )
f03fc89f
VZ
2238 win->SetConstraintSizes();
2239 node = node->GetNext();
2240 }
2241 }
2242}
2243
f03fc89f
VZ
2244// Only set the size/position of the constraint (if any)
2245void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h)
2246{
2247 wxLayoutConstraints *constr = GetConstraints();
2248 if ( constr )
2249 {
422d0ff0 2250 if ( x != wxDefaultCoord )
f03fc89f
VZ
2251 {
2252 constr->left.SetValue(x);
e11f4636 2253 constr->left.SetDone(true);
f03fc89f 2254 }
422d0ff0 2255 if ( y != wxDefaultCoord )
f03fc89f
VZ
2256 {
2257 constr->top.SetValue(y);
e11f4636 2258 constr->top.SetDone(true);
f03fc89f 2259 }
422d0ff0 2260 if ( w != wxDefaultCoord )
f03fc89f
VZ
2261 {
2262 constr->width.SetValue(w);
e11f4636 2263 constr->width.SetDone(true);
f03fc89f 2264 }
422d0ff0 2265 if ( h != wxDefaultCoord )
f03fc89f
VZ
2266 {
2267 constr->height.SetValue(h);
e11f4636 2268 constr->height.SetDone(true);
f03fc89f
VZ
2269 }
2270 }
2271}
2272
2273void wxWindowBase::MoveConstraint(int x, int y)
2274{
2275 wxLayoutConstraints *constr = GetConstraints();
2276 if ( constr )
2277 {
422d0ff0 2278 if ( x != wxDefaultCoord )
f03fc89f
VZ
2279 {
2280 constr->left.SetValue(x);
e11f4636 2281 constr->left.SetDone(true);
f03fc89f 2282 }
422d0ff0 2283 if ( y != wxDefaultCoord )
f03fc89f
VZ
2284 {
2285 constr->top.SetValue(y);
e11f4636 2286 constr->top.SetDone(true);
f03fc89f
VZ
2287 }
2288 }
2289}
2290
2291void wxWindowBase::GetSizeConstraint(int *w, int *h) const
2292{
2293 wxLayoutConstraints *constr = GetConstraints();
2294 if ( constr )
2295 {
2296 *w = constr->width.GetValue();
2297 *h = constr->height.GetValue();
2298 }
2299 else
2300 GetSize(w, h);
2301}
2302
2303void wxWindowBase::GetClientSizeConstraint(int *w, int *h) const
2304{
2305 wxLayoutConstraints *constr = GetConstraints();
2306 if ( constr )
2307 {
2308 *w = constr->width.GetValue();
2309 *h = constr->height.GetValue();
2310 }
2311 else
2312 GetClientSize(w, h);
2313}
2314
461e93f9
JS
2315void wxWindowBase::GetPositionConstraint(int *x, int *y) const
2316{
2317 wxLayoutConstraints *constr = GetConstraints();
2318 if ( constr )
2319 {
2320 *x = constr->left.GetValue();
2321 *y = constr->top.GetValue();
2322 }
2323 else
2324 GetPosition(x, y);
2325}
2326
2327#endif // wxUSE_CONSTRAINTS
2328
20a1eea1 2329void wxWindowBase::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags) const
a200c35e
VS
2330{
2331 // don't do it for the dialogs/frames - they float independently of their
2332 // parent
2333 if ( !IsTopLevel() )
2334 {
2335 wxWindow *parent = GetParent();
2336 if ( !(sizeFlags & wxSIZE_NO_ADJUSTMENTS) && parent )
2337 {
2338 wxPoint pt(parent->GetClientAreaOrigin());
2339 x += pt.x;
2340 y += pt.y;
2341 }
2342 }
2343}
2344
f03fc89f 2345// ----------------------------------------------------------------------------
602a2e02 2346// Update UI processing
f03fc89f 2347// ----------------------------------------------------------------------------
7ec1983b 2348
e39af974 2349void wxWindowBase::UpdateWindowUI(long flags)
7ec1983b 2350{
26bf1ce0 2351 wxUpdateUIEvent event(GetId());
687706f5 2352 event.SetEventObject(this);
26bf1ce0
VZ
2353
2354 if ( GetEventHandler()->ProcessEvent(event) )
7ec1983b 2355 {
e39af974
JS
2356 DoUpdateWindowUI(event);
2357 }
7ec1983b 2358
e39af974
JS
2359 if (flags & wxUPDATE_UI_RECURSE)
2360 {
222ed1d6 2361 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
e39af974 2362 while (node)
f03fc89f 2363 {
e39af974
JS
2364 wxWindow* child = (wxWindow*) node->GetData();
2365 child->UpdateWindowUI(flags);
2366 node = node->GetNext();
26bf1ce0 2367 }
e39af974
JS
2368 }
2369}
f03fc89f 2370
e39af974 2371// do the window-specific processing after processing the update event
e39af974
JS
2372void wxWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
2373{
2374 if ( event.GetSetEnabled() )
2375 Enable(event.GetEnabled());
e11f4636 2376
a3a4105d
VZ
2377 if ( event.GetSetShown() )
2378 Show(event.GetShown());
e39af974
JS
2379}
2380
f03fc89f
VZ
2381// ----------------------------------------------------------------------------
2382// dialog units translations
2383// ----------------------------------------------------------------------------
2384
2385wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt)
fd71308f
JS
2386{
2387 int charWidth = GetCharWidth();
2388 int charHeight = GetCharHeight();
cab1a605
WS
2389 wxPoint pt2 = wxDefaultPosition;
2390 if (pt.x != wxDefaultCoord)
1b69c815 2391 pt2.x = (int) ((pt.x * 4) / charWidth);
cab1a605 2392 if (pt.y != wxDefaultCoord)
1b69c815 2393 pt2.y = (int) ((pt.y * 8) / charHeight);
fd71308f
JS
2394
2395 return pt2;
2396}
2397
f03fc89f 2398wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt)
fd71308f
JS
2399{
2400 int charWidth = GetCharWidth();
2401 int charHeight = GetCharHeight();
cab1a605
WS
2402 wxPoint pt2 = wxDefaultPosition;
2403 if (pt.x != wxDefaultCoord)
1b69c815 2404 pt2.x = (int) ((pt.x * charWidth) / 4);
cab1a605 2405 if (pt.y != wxDefaultCoord)
1b69c815 2406 pt2.y = (int) ((pt.y * charHeight) / 8);
fd71308f
JS
2407
2408 return pt2;
2409}
2410
f03fc89f
VZ
2411// ----------------------------------------------------------------------------
2412// event handlers
2413// ----------------------------------------------------------------------------
2414
2415// propagate the colour change event to the subwindows
2416void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& event)
2417{
222ed1d6 2418 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
f03fc89f
VZ
2419 while ( node )
2420 {
2421 // Only propagate to non-top-level windows
2422 wxWindow *win = node->GetData();
2423 if ( !win->IsTopLevel() )
2424 {
2425 wxSysColourChangedEvent event2;
687706f5 2426 event.SetEventObject(win);
f03fc89f
VZ
2427 win->GetEventHandler()->ProcessEvent(event2);
2428 }
2429
2430 node = node->GetNext();
2431 }
9a8477e1
VS
2432
2433 Refresh();
f03fc89f
VZ
2434}
2435
e39af974
JS
2436// the default action is to populate dialog with data when it's created,
2437// and nudge the UI into displaying itself correctly in case
2438// we've turned the wxUpdateUIEvents frequency down low.
f03fc89f
VZ
2439void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
2440{
2441 TransferDataToWindow();
e11f4636 2442
e39af974
JS
2443 // Update the UI at this point
2444 UpdateWindowUI(wxUPDATE_UI_RECURSE);
f03fc89f
VZ
2445}
2446
00a77b7c
VZ
2447// ----------------------------------------------------------------------------
2448// menu-related functions
2449// ----------------------------------------------------------------------------
2450
2451#if wxUSE_MENUS
2452
eeb0bee1
VZ
2453bool wxWindowBase::PopupMenu(wxMenu *menu, int x, int y)
2454{
2455 wxCHECK_MSG( menu, false, "can't popup NULL menu" );
2456
2457 wxCurrentPopupMenu = menu;
2458 const bool rc = DoPopupMenu(menu, x, y);
2459 wxCurrentPopupMenu = NULL;
2460
2461 return rc;
2462}
2463
00a77b7c
VZ
2464// this is used to pass the id of the selected item from the menu event handler
2465// to the main function itself
2466//
2467// it's ok to use a global here as there can be at most one popup menu shown at
2468// any time
2469static int gs_popupMenuSelection = wxID_NONE;
2470
2471void wxWindowBase::InternalOnPopupMenu(wxCommandEvent& event)
2472{
2473 // store the id in a global variable where we'll retrieve it from later
2474 gs_popupMenuSelection = event.GetId();
2475}
2476
565382de
VZ
2477void wxWindowBase::InternalOnPopupMenuUpdate(wxUpdateUIEvent& WXUNUSED(event))
2478{
2479 // nothing to do but do not skip it
2480}
2481
00a77b7c
VZ
2482int
2483wxWindowBase::DoGetPopupMenuSelectionFromUser(wxMenu& menu, int x, int y)
2484{
2485 gs_popupMenuSelection = wxID_NONE;
2486
2487 Connect(wxEVT_COMMAND_MENU_SELECTED,
2488 wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu),
2489 NULL,
2490 this);
2491
565382de
VZ
2492 // it is common to construct the menu passed to this function dynamically
2493 // using some fixed range of ids which could clash with the ids used
2494 // elsewhere in the program, which could result in some menu items being
2495 // unintentionally disabled or otherwise modified by update UI handlers
2496 // elsewhere in the program code and this is difficult to avoid in the
2497 // program itself, so instead we just temporarily suspend UI updating while
2498 // this menu is shown
2499 Connect(wxEVT_UPDATE_UI,
2500 wxUpdateUIEventHandler(wxWindowBase::InternalOnPopupMenuUpdate),
2501 NULL,
2502 this);
2503
00a77b7c
VZ
2504 PopupMenu(&menu, x, y);
2505
565382de
VZ
2506 Disconnect(wxEVT_UPDATE_UI,
2507 wxUpdateUIEventHandler(wxWindowBase::InternalOnPopupMenuUpdate),
2508 NULL,
2509 this);
00a77b7c
VZ
2510 Disconnect(wxEVT_COMMAND_MENU_SELECTED,
2511 wxCommandEventHandler(wxWindowBase::InternalOnPopupMenu),
2512 NULL,
2513 this);
2514
2515 return gs_popupMenuSelection;
2516}
2517
2518#endif // wxUSE_MENUS
2519
dddd1f21
VZ
2520// methods for drawing the sizers in a visible way
2521#ifdef __WXDEBUG__
2522
2523static void DrawSizers(wxWindowBase *win);
2524
a721fd82 2525static void DrawBorder(wxWindowBase *win, const wxRect& rect, bool fill, const wxPen* pen)
dddd1f21
VZ
2526{
2527 wxClientDC dc((wxWindow *)win);
a721fd82 2528 dc.SetPen(*pen);
1b942d5f 2529 dc.SetBrush(fill ? wxBrush(pen->GetColour(), wxBRUSHSTYLE_CROSSDIAG_HATCH) :
a721fd82 2530 *wxTRANSPARENT_BRUSH);
dddd1f21
VZ
2531 dc.DrawRectangle(rect.Deflate(1, 1));
2532}
2533
2534static void DrawSizer(wxWindowBase *win, wxSizer *sizer)
2535{
2536 const wxSizerItemList& items = sizer->GetChildren();
2537 for ( wxSizerItemList::const_iterator i = items.begin(),
2538 end = items.end();
2539 i != end;
2540 ++i )
2541 {
2542 wxSizerItem *item = *i;
2543 if ( item->IsSizer() )
2544 {
a721fd82 2545 DrawBorder(win, item->GetRect().Deflate(2), false, wxRED_PEN);
dddd1f21
VZ
2546 DrawSizer(win, item->GetSizer());
2547 }
2548 else if ( item->IsSpacer() )
2549 {
a721fd82 2550 DrawBorder(win, item->GetRect().Deflate(2), true, wxBLUE_PEN);
dddd1f21
VZ
2551 }
2552 else if ( item->IsWindow() )
2553 {
2554 DrawSizers(item->GetWindow());
2555 }
1b942d5f
FM
2556 else
2557 wxFAIL_MSG("inconsistent wxSizerItem status!");
dddd1f21
VZ
2558 }
2559}
2560
2561static void DrawSizers(wxWindowBase *win)
2562{
a721fd82
FM
2563 DrawBorder(win, win->GetClientSize(), false, wxGREEN_PEN);
2564
dddd1f21
VZ
2565 wxSizer *sizer = win->GetSizer();
2566 if ( sizer )
2567 {
dddd1f21
VZ
2568 DrawSizer(win, sizer);
2569 }
2570 else // no sizer, still recurse into the children
2571 {
2572 const wxWindowList& children = win->GetChildren();
2573 for ( wxWindowList::const_iterator i = children.begin(),
2574 end = children.end();
2575 i != end;
2576 ++i )
2577 {
2578 DrawSizers(*i);
2579 }
1b942d5f
FM
2580
2581 // show all kind of sizes of this window; see the "window sizing" topic
2582 // overview for more info about the various differences:
2583 wxSize fullSz = win->GetSize();
2584 wxSize clientSz = win->GetClientSize();
2585 wxSize bestSz = win->GetBestSize();
2586 wxSize minSz = win->GetMinSize();
2587 wxSize maxSz = win->GetMaxSize();
2588 wxSize virtualSz = win->GetVirtualSize();
2589
2590 wxMessageOutputDebug dbgout;
2591 dbgout.Printf(
2592 "%-10s => fullsz=%4d;%-4d clientsz=%4d;%-4d bestsz=%4d;%-4d minsz=%4d;%-4d maxsz=%4d;%-4d virtualsz=%4d;%-4d\n",
79cb759d 2593 win->GetName(),
1b942d5f
FM
2594 fullSz.x, fullSz.y,
2595 clientSz.x, clientSz.y,
2596 bestSz.x, bestSz.y,
2597 minSz.x, minSz.y,
2598 maxSz.x, maxSz.y,
2599 virtualSz.x, virtualSz.y);
dddd1f21
VZ
2600 }
2601}
2602
2603#endif // __WXDEBUG__
2604
2605// process special middle clicks
a02dc3e3
VZ
2606void wxWindowBase::OnMiddleClick( wxMouseEvent& event )
2607{
2608 if ( event.ControlDown() && event.AltDown() )
2609 {
dddd1f21
VZ
2610#ifdef __WXDEBUG__
2611 // Ctrl-Alt-Shift-mclick makes the sizers visible in debug builds
2612 if ( event.ShiftDown() )
2613 {
2614 DrawSizers(this);
2615 return;
2616 }
2617#endif // __WXDEBUG__
8cf304f8 2618 ::wxInfoMessageBox((wxWindow*)this);
a02dc3e3
VZ
2619 }
2620 else
2621 {
2622 event.Skip();
2623 }
2624}
2625
ed5317e5
JS
2626// ----------------------------------------------------------------------------
2627// accessibility
2628// ----------------------------------------------------------------------------
2629
2630#if wxUSE_ACCESSIBILITY
2631void wxWindowBase::SetAccessible(wxAccessible* accessible)
2632{
2aefc528 2633 if (m_accessible && (accessible != m_accessible))
ed5317e5
JS
2634 delete m_accessible;
2635 m_accessible = accessible;
2636 if (m_accessible)
2637 m_accessible->SetWindow((wxWindow*) this);
2638}
2639
2640// Returns the accessible object, creating if necessary.
2641wxAccessible* wxWindowBase::GetOrCreateAccessible()
2642{
2643 if (!m_accessible)
2644 m_accessible = CreateAccessible();
2645 return m_accessible;
2646}
2647
2648// Override to create a specific accessible object.
2649wxAccessible* wxWindowBase::CreateAccessible()
2650{
2651 return new wxWindowAccessible((wxWindow*) this);
2652}
2653
2654#endif
2655
f03fc89f
VZ
2656// ----------------------------------------------------------------------------
2657// list classes implementation
2658// ----------------------------------------------------------------------------
2659
ed1288c1
VZ
2660#if wxUSE_STL
2661
7ec69821 2662#include "wx/listimpl.cpp"
412e0d47 2663WX_DEFINE_LIST(wxWindowList)
ed1288c1 2664
602a2e02 2665#else // !wxUSE_STL
ed1288c1 2666
f03fc89f
VZ
2667void wxWindowListNode::DeleteData()
2668{
2669 delete (wxWindow *)GetData();
2670}
ed1288c1 2671
602a2e02 2672#endif // wxUSE_STL/!wxUSE_STL
f03fc89f 2673
1e6feb95
VZ
2674// ----------------------------------------------------------------------------
2675// borders
2676// ----------------------------------------------------------------------------
2677
aede4ebb 2678wxBorder wxWindowBase::GetBorder(long flags) const
1e6feb95 2679{
aede4ebb 2680 wxBorder border = (wxBorder)(flags & wxBORDER_MASK);
1e6feb95
VZ
2681 if ( border == wxBORDER_DEFAULT )
2682 {
2683 border = GetDefaultBorder();
2684 }
78cd9c69
JS
2685 else if ( border == wxBORDER_THEME )
2686 {
2687 border = GetDefaultBorderForControl();
2688 }
1e6feb95
VZ
2689
2690 return border;
2691}
2692
2693wxBorder wxWindowBase::GetDefaultBorder() const
2694{
2695 return wxBORDER_NONE;
2696}
2697
2698// ----------------------------------------------------------------------------
2699// hit testing
2700// ----------------------------------------------------------------------------
2701
2702wxHitTest wxWindowBase::DoHitTest(wxCoord x, wxCoord y) const
2703{
2704 // here we just check if the point is inside the window or not
2705
2706 // check the top and left border first
2707 bool outside = x < 0 || y < 0;
2708 if ( !outside )
2709 {
2710 // check the right and bottom borders too
2711 wxSize size = GetSize();
2712 outside = x >= size.x || y >= size.y;
2713 }
2714
2715 return outside ? wxHT_WINDOW_OUTSIDE : wxHT_WINDOW_INSIDE;
2716}
2717
94633ad9
VZ
2718// ----------------------------------------------------------------------------
2719// mouse capture
2720// ----------------------------------------------------------------------------
2721
2722struct WXDLLEXPORT wxWindowNext
2723{
2724 wxWindow *win;
2725 wxWindowNext *next;
786646f3 2726} *wxWindowBase::ms_winCaptureNext = NULL;
63e819f2
VS
2727wxWindow *wxWindowBase::ms_winCaptureCurrent = NULL;
2728bool wxWindowBase::ms_winCaptureChanging = false;
94633ad9 2729
a83e1475 2730void wxWindowBase::CaptureMouse()
94633ad9 2731{
5c33522f 2732 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(%p)"), static_cast<void*>(this));
45e0dc94 2733
63e819f2
VS
2734 wxASSERT_MSG( !ms_winCaptureChanging, _T("recursive CaptureMouse call?") );
2735
2736 ms_winCaptureChanging = true;
2737
94633ad9
VZ
2738 wxWindow *winOld = GetCapture();
2739 if ( winOld )
2740 {
df2f507b 2741 ((wxWindowBase*) winOld)->DoReleaseMouse();
edd802c6 2742
94633ad9
VZ
2743 // save it on stack
2744 wxWindowNext *item = new wxWindowNext;
2745 item->win = winOld;
2746 item->next = ms_winCaptureNext;
2747 ms_winCaptureNext = item;
2748 }
2749 //else: no mouse capture to save
2750
2751 DoCaptureMouse();
63e819f2
VS
2752 ms_winCaptureCurrent = (wxWindow*)this;
2753
2754 ms_winCaptureChanging = false;
94633ad9
VZ
2755}
2756
a83e1475 2757void wxWindowBase::ReleaseMouse()
94633ad9 2758{
5c33522f 2759 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(%p)"), static_cast<void*>(this));
349d1942 2760
63e819f2
VS
2761 wxASSERT_MSG( !ms_winCaptureChanging, _T("recursive ReleaseMouse call?") );
2762
31b9d29f
VS
2763 wxASSERT_MSG( GetCapture() == this,
2764 "attempt to release mouse, but this window hasn't captured it" );
2765 wxASSERT_MSG( ms_winCaptureCurrent == this,
2766 "attempt to release mouse, but this window hasn't captured it" );
a7b09001 2767
63e819f2
VS
2768 ms_winCaptureChanging = true;
2769
94633ad9 2770 DoReleaseMouse();
63e819f2 2771 ms_winCaptureCurrent = NULL;
94633ad9
VZ
2772
2773 if ( ms_winCaptureNext )
2774 {
44f8caa7 2775 ((wxWindowBase*)ms_winCaptureNext->win)->DoCaptureMouse();
63e819f2 2776 ms_winCaptureCurrent = ms_winCaptureNext->win;
edd802c6 2777
94633ad9
VZ
2778 wxWindowNext *item = ms_winCaptureNext;
2779 ms_winCaptureNext = item->next;
2780 delete item;
2781 }
2782 //else: stack is empty, no previous capture
2783
63e819f2
VS
2784 ms_winCaptureChanging = false;
2785
94633ad9 2786 wxLogTrace(_T("mousecapture"),
e11f4636 2787 (const wxChar *) _T("After ReleaseMouse() mouse is captured by %p"),
5c33522f 2788 static_cast<void*>(GetCapture()));
94633ad9
VZ
2789}
2790
63e819f2
VS
2791static void DoNotifyWindowAboutCaptureLost(wxWindow *win)
2792{
2793 wxMouseCaptureLostEvent event(win->GetId());
2794 event.SetEventObject(win);
40bdc5d6
VZ
2795 if ( !win->GetEventHandler()->ProcessEvent(event) )
2796 {
44038f9a
VS
2797 // windows must handle this event, otherwise the app wouldn't behave
2798 // correctly if it loses capture unexpectedly; see the discussion here:
2799 // http://sourceforge.net/tracker/index.php?func=detail&aid=1153662&group_id=9863&atid=109863
2800 // http://article.gmane.org/gmane.comp.lib.wxwidgets.devel/82376
40bdc5d6
VZ
2801 wxFAIL_MSG( _T("window that captured the mouse didn't process wxEVT_MOUSE_CAPTURE_LOST") );
2802 }
63e819f2
VS
2803}
2804
2805/* static */
72f8c792 2806void wxWindowBase::NotifyCaptureLost()
63e819f2
VS
2807{
2808 // don't do anything if capture lost was expected, i.e. resulted from
2809 // a wx call to ReleaseMouse or CaptureMouse:
2810 if ( ms_winCaptureChanging )
2811 return;
2812
2813 // if the capture was lost unexpectedly, notify every window that has
2814 // capture (on stack or current) about it and clear the stack:
2815
2816 if ( ms_winCaptureCurrent )
2817 {
2818 DoNotifyWindowAboutCaptureLost(ms_winCaptureCurrent);
2819 ms_winCaptureCurrent = NULL;
2820 }
2821
2822 while ( ms_winCaptureNext )
2823 {
2824 wxWindowNext *item = ms_winCaptureNext;
2825 ms_winCaptureNext = item->next;
2826
2827 DoNotifyWindowAboutCaptureLost(item->win);
2828
2829 delete item;
2830 }
2831}
2832
5048c832 2833#if wxUSE_HOTKEY
540b6b09
VZ
2834
2835bool
2836wxWindowBase::RegisterHotKey(int WXUNUSED(hotkeyId),
2837 int WXUNUSED(modifiers),
2838 int WXUNUSED(keycode))
5048c832
JS
2839{
2840 // not implemented
2841 return false;
2842}
2843
540b6b09 2844bool wxWindowBase::UnregisterHotKey(int WXUNUSED(hotkeyId))
5048c832
JS
2845{
2846 // not implemented
2847 return false;
2848}
540b6b09
VZ
2849
2850#endif // wxUSE_HOTKEY
7de59551 2851
4caf847c
VZ
2852// ----------------------------------------------------------------------------
2853// event processing
2854// ----------------------------------------------------------------------------
2855
8cc208e3 2856bool wxWindowBase::TryBefore(wxEvent& event)
4caf847c 2857{
6eabf58c 2858#if wxUSE_VALIDATORS
4caf847c
VZ
2859 // Can only use the validator of the window which
2860 // is receiving the event
2861 if ( event.GetEventObject() == this )
2862 {
92089d00
VZ
2863 wxValidator * const validator = GetValidator();
2864 if ( validator && validator->ProcessEventHere(event) )
4caf847c 2865 {
6eabf58c 2866 return true;
4caf847c
VZ
2867 }
2868 }
6eabf58c 2869#endif // wxUSE_VALIDATORS
4caf847c 2870
8cc208e3 2871 return wxEvtHandler::TryBefore(event);
4caf847c
VZ
2872}
2873
8cc208e3 2874bool wxWindowBase::TryAfter(wxEvent& event)
4caf847c 2875{
90e572f1 2876 // carry on up the parent-child hierarchy if the propagation count hasn't
040e234d
VZ
2877 // reached zero yet
2878 if ( event.ShouldPropagate() )
4caf847c
VZ
2879 {
2880 // honour the requests to stop propagation at this window: this is
2881 // used by the dialogs, for example, to prevent processing the events
2882 // from the dialog controls in the parent frame which rarely, if ever,
2883 // makes sense
2884 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
2885 {
2886 wxWindow *parent = GetParent();
2887 if ( parent && !parent->IsBeingDeleted() )
040e234d
VZ
2888 {
2889 wxPropagateOnce propagateOnce(event);
2890
4caf847c 2891 return parent->GetEventHandler()->ProcessEvent(event);
040e234d 2892 }
4caf847c
VZ
2893 }
2894 }
2895
8cc208e3 2896 return wxEvtHandler::TryAfter(event);
4caf847c
VZ
2897}
2898
c944775f
VZ
2899// ----------------------------------------------------------------------------
2900// window relationships
2901// ----------------------------------------------------------------------------
2902
2903wxWindow *wxWindowBase::DoGetSibling(WindowOrder order) const
2904{
2905 wxCHECK_MSG( GetParent(), NULL,
2906 _T("GetPrev/NextSibling() don't work for TLWs!") );
2907
2908 wxWindowList& siblings = GetParent()->GetChildren();
c52b9635 2909 wxWindowList::compatibility_iterator i = siblings.Find((wxWindow *)this);
c944775f
VZ
2910 wxCHECK_MSG( i, NULL, _T("window not a child of its parent?") );
2911
2912 if ( order == OrderBefore )
2913 i = i->GetPrevious();
2914 else // OrderAfter
2915 i = i->GetNext();
2916
2917 return i ? i->GetData() : NULL;
2918}
2919
5f6cfda7 2920// ----------------------------------------------------------------------------
a24de76b 2921// keyboard navigation
5f6cfda7
JS
2922// ----------------------------------------------------------------------------
2923
5644933f 2924// Navigates in the specified direction inside this window
553d2d7d 2925bool wxWindowBase::DoNavigateIn(int flags)
5f6cfda7 2926{
5644933f
VZ
2927#ifdef wxHAS_NATIVE_TAB_TRAVERSAL
2928 // native code doesn't process our wxNavigationKeyEvents anyhow
553d2d7d 2929 wxUnusedVar(flags);
5644933f
VZ
2930 return false;
2931#else // !wxHAS_NATIVE_TAB_TRAVERSAL
5f6cfda7 2932 wxNavigationKeyEvent eventNav;
f029f1d1
VS
2933 wxWindow *focused = FindFocus();
2934 eventNav.SetCurrentFocus(focused);
2935 eventNav.SetEventObject(focused);
eedc82f4 2936 eventNav.SetFlags(flags);
5644933f
VZ
2937 return GetEventHandler()->ProcessEvent(eventNav);
2938#endif // wxHAS_NATIVE_TAB_TRAVERSAL/!wxHAS_NATIVE_TAB_TRAVERSAL
5f6cfda7
JS
2939}
2940
f029f1d1
VS
2941bool wxWindowBase::HandleAsNavigationKey(const wxKeyEvent& event)
2942{
2943 if ( event.GetKeyCode() != WXK_TAB )
2944 return false;
2945
2946 int flags = wxNavigationKeyEvent::FromTab;
2947
2948 if ( event.ShiftDown() )
2949 flags |= wxNavigationKeyEvent::IsBackward;
2950 else
2951 flags |= wxNavigationKeyEvent::IsForward;
2952
2953 if ( event.ControlDown() )
2954 flags |= wxNavigationKeyEvent::WinChange;
2955
2956 Navigate(flags);
2957 return true;
2958}
2959
c944775f 2960void wxWindowBase::DoMoveInTabOrder(wxWindow *win, WindowOrder move)
a24de76b
VZ
2961{
2962 // check that we're not a top level window
2963 wxCHECK_RET( GetParent(),
2964 _T("MoveBefore/AfterInTabOrder() don't work for TLWs!") );
2965
ef6816ef
VZ
2966 // detect the special case when we have nothing to do anyhow and when the
2967 // code below wouldn't work
2968 if ( win == this )
2969 return;
2970
a24de76b
VZ
2971 // find the target window in the siblings list
2972 wxWindowList& siblings = GetParent()->GetChildren();
2973 wxWindowList::compatibility_iterator i = siblings.Find(win);
2974 wxCHECK_RET( i, _T("MoveBefore/AfterInTabOrder(): win is not a sibling") );
2975
2976 // unfortunately, when wxUSE_STL == 1 DetachNode() is not implemented so we
2977 // can't just move the node around
feef1ecf
VZ
2978 wxWindow *self = (wxWindow *)this;
2979 siblings.DeleteObject(self);
c944775f 2980 if ( move == OrderAfter )
9e9300c4
VZ
2981 {
2982 i = i->GetNext();
2983 }
2984
2985 if ( i )
a24de76b 2986 {
feef1ecf 2987 siblings.Insert(i, self);
a24de76b 2988 }
c944775f 2989 else // OrderAfter and win was the last sibling
a24de76b 2990 {
feef1ecf 2991 siblings.Append(self);
a24de76b
VZ
2992 }
2993}
2994
0fe02759
VS
2995// ----------------------------------------------------------------------------
2996// focus handling
2997// ----------------------------------------------------------------------------
2998
2999/*static*/ wxWindow* wxWindowBase::FindFocus()
3000{
9c52a429 3001 wxWindowBase *win = DoFindFocus();
0fe02759
VS
3002 return win ? win->GetMainWindowOfCompositeControl() : NULL;
3003}
3004
3358af0f
VS
3005bool wxWindowBase::HasFocus() const
3006{
3007 wxWindowBase *win = DoFindFocus();
3008 return win == this ||
3009 win == wxConstCast(this, wxWindowBase)->GetMainWindowOfCompositeControl();
3010}
3011
4f24cbbd
VS
3012// ----------------------------------------------------------------------------
3013// drag and drop
3014// ----------------------------------------------------------------------------
3015
3016#if wxUSE_DRAG_AND_DROP && !defined(__WXMSW__)
3017
3018namespace
3019{
3020
3021class DragAcceptFilesTarget : public wxFileDropTarget
3022{
3023public:
3024 DragAcceptFilesTarget(wxWindowBase *win) : m_win(win) {}
3025
3026 virtual bool OnDropFiles(wxCoord x, wxCoord y,
3027 const wxArrayString& filenames)
3028 {
3029 wxDropFilesEvent event(wxEVT_DROP_FILES,
3030 filenames.size(),
3031 wxCArrayString(filenames).Release());
3032 event.SetEventObject(m_win);
3033 event.m_pos.x = x;
3034 event.m_pos.y = y;
3035
3036 return m_win->HandleWindowEvent(event);
3037 }
3038
3039private:
3040 wxWindowBase * const m_win;
3041
c0c133e1 3042 wxDECLARE_NO_COPY_CLASS(DragAcceptFilesTarget);
4f24cbbd
VS
3043};
3044
3045
3046} // anonymous namespace
3047
3048// Generic version of DragAcceptFiles(). It works by installing a simple
3049// wxFileDropTarget-to-EVT_DROP_FILES adaptor and therefore cannot be used
3050// together with explicit SetDropTarget() calls.
3051void wxWindowBase::DragAcceptFiles(bool accept)
3052{
3053 if ( accept )
3054 {
3055 wxASSERT_MSG( !GetDropTarget(),
3056 "cannot use DragAcceptFiles() and SetDropTarget() together" );
3057 SetDropTarget(new DragAcceptFilesTarget(this));
3058 }
3059 else
3060 {
3061 SetDropTarget(NULL);
3062 }
3063}
3064
3065#endif // wxUSE_DRAG_AND_DROP && !defined(__WXMSW__)
3066
33b494d6
VZ
3067// ----------------------------------------------------------------------------
3068// global functions
3069// ----------------------------------------------------------------------------
3070
3071wxWindow* wxGetTopLevelParent(wxWindow *win)
3072{
3073 while ( win && !win->IsTopLevel() )
3074 win = win->GetParent();
3075
3076 return win;
3077}
3078
ed5317e5
JS
3079#if wxUSE_ACCESSIBILITY
3080// ----------------------------------------------------------------------------
3081// accessible object for windows
3082// ----------------------------------------------------------------------------
3083
3084// Can return either a child object, or an integer
3085// representing the child element, starting from 1.
66b9ec3d 3086wxAccStatus wxWindowAccessible::HitTest(const wxPoint& WXUNUSED(pt), int* WXUNUSED(childId), wxAccessible** WXUNUSED(childObject))
ed5317e5
JS
3087{
3088 wxASSERT( GetWindow() != NULL );
3089 if (!GetWindow())
3090 return wxACC_FAIL;
3091
3092 return wxACC_NOT_IMPLEMENTED;
3093}
3094
3095// Returns the rectangle for this object (id = 0) or a child element (id > 0).
3096wxAccStatus wxWindowAccessible::GetLocation(wxRect& rect, int elementId)
3097{
3098 wxASSERT( GetWindow() != NULL );
3099 if (!GetWindow())
3100 return wxACC_FAIL;
3101
c6e2af45
JS
3102 wxWindow* win = NULL;
3103 if (elementId == 0)
3104 {
3105 win = GetWindow();
3106 }
3107 else
3108 {
3109 if (elementId <= (int) GetWindow()->GetChildren().GetCount())
3110 {
ee6eb8d8 3111 win = GetWindow()->GetChildren().Item(elementId-1)->GetData();
c6e2af45
JS
3112 }
3113 else
3114 return wxACC_FAIL;
3115 }
3116 if (win)
3117 {
3118 rect = win->GetRect();
3119 if (win->GetParent() && !win->IsKindOf(CLASSINFO(wxTopLevelWindow)))
3120 rect.SetPosition(win->GetParent()->ClientToScreen(rect.GetPosition()));
3121 return wxACC_OK;
3122 }
3123
ed5317e5
JS
3124 return wxACC_NOT_IMPLEMENTED;
3125}
3126
3127// Navigates from fromId to toId/toObject.
3128wxAccStatus wxWindowAccessible::Navigate(wxNavDir navDir, int fromId,
66b9ec3d 3129 int* WXUNUSED(toId), wxAccessible** toObject)
ed5317e5
JS
3130{
3131 wxASSERT( GetWindow() != NULL );
3132 if (!GetWindow())
3133 return wxACC_FAIL;
3134
c6e2af45
JS
3135 switch (navDir)
3136 {
3137 case wxNAVDIR_FIRSTCHILD:
3138 {
3139 if (GetWindow()->GetChildren().GetCount() == 0)
3140 return wxACC_FALSE;
3141 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetFirst()->GetData();
3142 *toObject = childWindow->GetOrCreateAccessible();
3143
3144 return wxACC_OK;
3145 }
3146 case wxNAVDIR_LASTCHILD:
3147 {
3148 if (GetWindow()->GetChildren().GetCount() == 0)
3149 return wxACC_FALSE;
3150 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetLast()->GetData();
3151 *toObject = childWindow->GetOrCreateAccessible();
3152
3153 return wxACC_OK;
3154 }
3155 case wxNAVDIR_RIGHT:
3156 case wxNAVDIR_DOWN:
3157 case wxNAVDIR_NEXT:
3158 {
ee6eb8d8
MB
3159 wxWindowList::compatibility_iterator node =
3160 wxWindowList::compatibility_iterator();
c6e2af45
JS
3161 if (fromId == 0)
3162 {
3163 // Can't navigate to sibling of this window
3164 // if we're a top-level window.
3165 if (!GetWindow()->GetParent())
3166 return wxACC_NOT_IMPLEMENTED;
3167
3168 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
3169 }
3170 else
3171 {
3172 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
33b3f7c3 3173 node = GetWindow()->GetChildren().Item(fromId-1);
c6e2af45
JS
3174 }
3175
3176 if (node && node->GetNext())
3177 {
ee6eb8d8 3178 wxWindow* nextWindow = node->GetNext()->GetData();
c6e2af45
JS
3179 *toObject = nextWindow->GetOrCreateAccessible();
3180 return wxACC_OK;
3181 }
3182 else
3183 return wxACC_FALSE;
3184 }
3185 case wxNAVDIR_LEFT:
3186 case wxNAVDIR_UP:
3187 case wxNAVDIR_PREVIOUS:
3188 {
ee6eb8d8
MB
3189 wxWindowList::compatibility_iterator node =
3190 wxWindowList::compatibility_iterator();
c6e2af45
JS
3191 if (fromId == 0)
3192 {
3193 // Can't navigate to sibling of this window
3194 // if we're a top-level window.
3195 if (!GetWindow()->GetParent())
3196 return wxACC_NOT_IMPLEMENTED;
3197
3198 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
3199 }
3200 else
3201 {
3202 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
33b3f7c3 3203 node = GetWindow()->GetChildren().Item(fromId-1);
c6e2af45
JS
3204 }
3205
3206 if (node && node->GetPrevious())
3207 {
ee6eb8d8 3208 wxWindow* previousWindow = node->GetPrevious()->GetData();
c6e2af45
JS
3209 *toObject = previousWindow->GetOrCreateAccessible();
3210 return wxACC_OK;
3211 }
3212 else
3213 return wxACC_FALSE;
3214 }
3215 }
3216
ed5317e5
JS
3217 return wxACC_NOT_IMPLEMENTED;
3218}
3219
3220// Gets the name of the specified object.
3221wxAccStatus wxWindowAccessible::GetName(int childId, wxString* name)
3222{
3223 wxASSERT( GetWindow() != NULL );
3224 if (!GetWindow())
3225 return wxACC_FAIL;
3226
2aefc528
JS
3227 wxString title;
3228
77ffb593 3229 // If a child, leave wxWidgets to call the function on the actual
2aefc528
JS
3230 // child object.
3231 if (childId > 0)
3232 return wxACC_NOT_IMPLEMENTED;
3233
3234 // This will eventually be replaced by specialised
77ffb593 3235 // accessible classes, one for each kind of wxWidgets
2aefc528 3236 // control or window.
0b4f47a3 3237#if wxUSE_BUTTON
2aefc528
JS
3238 if (GetWindow()->IsKindOf(CLASSINFO(wxButton)))
3239 title = ((wxButton*) GetWindow())->GetLabel();
3240 else
0b4f47a3 3241#endif
2aefc528 3242 title = GetWindow()->GetName();
e11f4636 3243
ba889513 3244 if (!title.empty())
ed5317e5
JS
3245 {
3246 *name = title;
3247 return wxACC_OK;
3248 }
3249 else
3250 return wxACC_NOT_IMPLEMENTED;
3251}
3252
3253// Gets the number of children.
3254wxAccStatus wxWindowAccessible::GetChildCount(int* childId)
3255{
3256 wxASSERT( GetWindow() != NULL );
3257 if (!GetWindow())
3258 return wxACC_FAIL;
3259
3260 *childId = (int) GetWindow()->GetChildren().GetCount();
3261 return wxACC_OK;
3262}
3263
3264// Gets the specified child (starting from 1).
3265// If *child is NULL and return value is wxACC_OK,
3266// this means that the child is a simple element and
3267// not an accessible object.
3268wxAccStatus wxWindowAccessible::GetChild(int childId, wxAccessible** child)
3269{
3270 wxASSERT( GetWindow() != NULL );
3271 if (!GetWindow())
3272 return wxACC_FAIL;
3273
3274 if (childId == 0)
3275 {
3276 *child = this;
3277 return wxACC_OK;
3278 }
3279
3280 if (childId > (int) GetWindow()->GetChildren().GetCount())
3281 return wxACC_FAIL;
3282
ee6eb8d8 3283 wxWindow* childWindow = GetWindow()->GetChildren().Item(childId-1)->GetData();
ed5317e5
JS
3284 *child = childWindow->GetOrCreateAccessible();
3285 if (*child)
3286 return wxACC_OK;
3287 else
3288 return wxACC_FAIL;
3289}
3290
3291// Gets the parent, or NULL.
3292wxAccStatus wxWindowAccessible::GetParent(wxAccessible** parent)
3293{
3294 wxASSERT( GetWindow() != NULL );
3295 if (!GetWindow())
3296 return wxACC_FAIL;
3297
3298 wxWindow* parentWindow = GetWindow()->GetParent();
c6e2af45 3299 if (!parentWindow)
ed5317e5
JS
3300 {
3301 *parent = NULL;
3302 return wxACC_OK;
3303 }
3304 else
3305 {
3306 *parent = parentWindow->GetOrCreateAccessible();
3307 if (*parent)
3308 return wxACC_OK;
3309 else
3310 return wxACC_FAIL;
3311 }
3312}
3313
3314// Performs the default action. childId is 0 (the action for this object)
3315// or > 0 (the action for a child).
3316// Return wxACC_NOT_SUPPORTED if there is no default action for this
3317// window (e.g. an edit control).
66b9ec3d 3318wxAccStatus wxWindowAccessible::DoDefaultAction(int WXUNUSED(childId))
ed5317e5
JS
3319{
3320 wxASSERT( GetWindow() != NULL );
3321 if (!GetWindow())
3322 return wxACC_FAIL;
3323
3324 return wxACC_NOT_IMPLEMENTED;
3325}
3326
3327// Gets the default action for this object (0) or > 0 (the action for a child).
3328// Return wxACC_OK even if there is no action. actionName is the action, or the empty
3329// string if there is no action.
3330// The retrieved string describes the action that is performed on an object,
3331// not what the object does as a result. For example, a toolbar button that prints
3332// a document has a default action of "Press" rather than "Prints the current document."
66b9ec3d 3333wxAccStatus wxWindowAccessible::GetDefaultAction(int WXUNUSED(childId), wxString* WXUNUSED(actionName))
ed5317e5
JS
3334{
3335 wxASSERT( GetWindow() != NULL );
3336 if (!GetWindow())
3337 return wxACC_FAIL;
3338
3339 return wxACC_NOT_IMPLEMENTED;
3340}
3341
3342// Returns the description for this object or a child.
66b9ec3d 3343wxAccStatus wxWindowAccessible::GetDescription(int WXUNUSED(childId), wxString* description)
ed5317e5
JS
3344{
3345 wxASSERT( GetWindow() != NULL );
3346 if (!GetWindow())
3347 return wxACC_FAIL;
3348
dc6588e7 3349 wxString ht(GetWindow()->GetHelpTextAtPoint(wxDefaultPosition, wxHelpEvent::Origin_Keyboard));
ba889513 3350 if (!ht.empty())
2aefc528
JS
3351 {
3352 *description = ht;
3353 return wxACC_OK;
3354 }
ed5317e5
JS
3355 return wxACC_NOT_IMPLEMENTED;
3356}
3357
3358// Returns help text for this object or a child, similar to tooltip text.
66b9ec3d 3359wxAccStatus wxWindowAccessible::GetHelpText(int WXUNUSED(childId), wxString* helpText)
ed5317e5
JS
3360{
3361 wxASSERT( GetWindow() != NULL );
3362 if (!GetWindow())
3363 return wxACC_FAIL;
3364
dc6588e7 3365 wxString ht(GetWindow()->GetHelpTextAtPoint(wxDefaultPosition, wxHelpEvent::Origin_Keyboard));
ba889513 3366 if (!ht.empty())
ed5317e5
JS
3367 {
3368 *helpText = ht;
3369 return wxACC_OK;
3370 }
3371 return wxACC_NOT_IMPLEMENTED;
3372}
3373
3374// Returns the keyboard shortcut for this object or child.
3375// Return e.g. ALT+K
66b9ec3d 3376wxAccStatus wxWindowAccessible::GetKeyboardShortcut(int WXUNUSED(childId), wxString* WXUNUSED(shortcut))
ed5317e5
JS
3377{
3378 wxASSERT( GetWindow() != NULL );
3379 if (!GetWindow())
3380 return wxACC_FAIL;
3381
3382 return wxACC_NOT_IMPLEMENTED;
3383}
3384
3385// Returns a role constant.
3386wxAccStatus wxWindowAccessible::GetRole(int childId, wxAccRole* role)
3387{
3388 wxASSERT( GetWindow() != NULL );
3389 if (!GetWindow())
3390 return wxACC_FAIL;
3391
77ffb593 3392 // If a child, leave wxWidgets to call the function on the actual
2aefc528
JS
3393 // child object.
3394 if (childId > 0)
3395 return wxACC_NOT_IMPLEMENTED;
3396
3397 if (GetWindow()->IsKindOf(CLASSINFO(wxControl)))
3398 return wxACC_NOT_IMPLEMENTED;
3399#if wxUSE_STATUSBAR
3400 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar)))
3401 return wxACC_NOT_IMPLEMENTED;
3402#endif
3403#if wxUSE_TOOLBAR
3404 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar)))
3405 return wxACC_NOT_IMPLEMENTED;
3406#endif
3407
3408 //*role = wxROLE_SYSTEM_CLIENT;
3409 *role = wxROLE_SYSTEM_CLIENT;
3410 return wxACC_OK;
3411
66b9ec3d 3412 #if 0
ed5317e5 3413 return wxACC_NOT_IMPLEMENTED;
66b9ec3d 3414 #endif
ed5317e5
JS
3415}
3416
3417// Returns a state constant.
3418wxAccStatus wxWindowAccessible::GetState(int childId, long* state)
3419{
3420 wxASSERT( GetWindow() != NULL );
3421 if (!GetWindow())
3422 return wxACC_FAIL;
3423
77ffb593 3424 // If a child, leave wxWidgets to call the function on the actual
2aefc528
JS
3425 // child object.
3426 if (childId > 0)
3427 return wxACC_NOT_IMPLEMENTED;
3428
3429 if (GetWindow()->IsKindOf(CLASSINFO(wxControl)))
3430 return wxACC_NOT_IMPLEMENTED;
3431
3432#if wxUSE_STATUSBAR
3433 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar)))
3434 return wxACC_NOT_IMPLEMENTED;
3435#endif
3436#if wxUSE_TOOLBAR
3437 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar)))
3438 return wxACC_NOT_IMPLEMENTED;
3439#endif
3440
3441 *state = 0;
3442 return wxACC_OK;
3443
66b9ec3d 3444 #if 0
ed5317e5 3445 return wxACC_NOT_IMPLEMENTED;
66b9ec3d 3446 #endif
ed5317e5
JS
3447}
3448
3449// Returns a localized string representing the value for the object
3450// or child.
66b9ec3d 3451wxAccStatus wxWindowAccessible::GetValue(int WXUNUSED(childId), wxString* WXUNUSED(strValue))
ed5317e5
JS
3452{
3453 wxASSERT( GetWindow() != NULL );
3454 if (!GetWindow())
3455 return wxACC_FAIL;
3456
3457 return wxACC_NOT_IMPLEMENTED;
3458}
3459
3460// Selects the object or child.
66b9ec3d 3461wxAccStatus wxWindowAccessible::Select(int WXUNUSED(childId), wxAccSelectionFlags WXUNUSED(selectFlags))
ed5317e5
JS
3462{
3463 wxASSERT( GetWindow() != NULL );
3464 if (!GetWindow())
3465 return wxACC_FAIL;
3466
3467 return wxACC_NOT_IMPLEMENTED;
3468}
3469
3470// Gets the window with the keyboard focus.
3471// If childId is 0 and child is NULL, no object in
3472// this subhierarchy has the focus.
3473// If this object has the focus, child should be 'this'.
66b9ec3d 3474wxAccStatus wxWindowAccessible::GetFocus(int* WXUNUSED(childId), wxAccessible** WXUNUSED(child))
ed5317e5
JS
3475{
3476 wxASSERT( GetWindow() != NULL );
3477 if (!GetWindow())
3478 return wxACC_FAIL;
3479
3480 return wxACC_NOT_IMPLEMENTED;
3481}
3482
ca5c6ac3 3483#if wxUSE_VARIANT
ed5317e5
JS
3484// Gets a variant representing the selected children
3485// of this object.
3486// Acceptable values:
cab1a605 3487// - a null variant (IsNull() returns true)
ed5317e5
JS
3488// - a list variant (GetType() == wxT("list")
3489// - an integer representing the selected child element,
3490// or 0 if this object is selected (GetType() == wxT("long")
3491// - a "void*" pointer to a wxAccessible child object
66b9ec3d 3492wxAccStatus wxWindowAccessible::GetSelections(wxVariant* WXUNUSED(selections))
ed5317e5
JS
3493{
3494 wxASSERT( GetWindow() != NULL );
3495 if (!GetWindow())
3496 return wxACC_FAIL;
3497
3498 return wxACC_NOT_IMPLEMENTED;
3499}
ca5c6ac3 3500#endif // wxUSE_VARIANT
ed5317e5
JS
3501
3502#endif // wxUSE_ACCESSIBILITY
978af864
VZ
3503
3504// ----------------------------------------------------------------------------
3505// RTL support
3506// ----------------------------------------------------------------------------
3507
3508wxCoord
3509wxWindowBase::AdjustForLayoutDirection(wxCoord x,
3510 wxCoord width,
3511 wxCoord widthTotal) const
3512{
3513 if ( GetLayoutDirection() == wxLayout_RightToLeft )
3514 {
3515 x = widthTotal - x - width;
3516 }
3517
3518 return x;
3519}
3520
f35fdf7e 3521