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