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