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