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