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