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