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