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