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