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