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