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