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