]> git.saurik.com Git - wxWidgets.git/blob - src/common/wincmn.cpp
Spelling fixes [#1017001], source cleaning.
[wxWidgets.git] / src / common / wincmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "windowbase.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/string.h"
33 #include "wx/log.h"
34 #include "wx/intl.h"
35 #include "wx/frame.h"
36 #include "wx/defs.h"
37 #include "wx/window.h"
38 #include "wx/control.h"
39 #include "wx/checkbox.h"
40 #include "wx/radiobut.h"
41 #include "wx/statbox.h"
42 #include "wx/textctrl.h"
43 #include "wx/settings.h"
44 #include "wx/dialog.h"
45 #include "wx/msgdlg.h"
46 #include "wx/statusbr.h"
47 #include "wx/dcclient.h"
48 #endif //WX_PRECOMP
49
50 #if defined(__WXMAC__) && wxUSE_SCROLLBAR
51 #include "wx/scrolbar.h"
52 #endif
53
54 #if wxUSE_CONSTRAINTS
55 #include "wx/layout.h"
56 #endif // wxUSE_CONSTRAINTS
57
58 #include "wx/sizer.h"
59
60 #if wxUSE_DRAG_AND_DROP
61 #include "wx/dnd.h"
62 #endif // wxUSE_DRAG_AND_DROP
63
64 #if wxUSE_ACCESSIBILITY
65 #include "wx/access.h"
66 #endif
67
68 #if wxUSE_HELP
69 #include "wx/cshelp.h"
70 #endif // wxUSE_HELP
71
72 #if wxUSE_TOOLTIPS
73 #include "wx/tooltip.h"
74 #endif // wxUSE_TOOLTIPS
75
76 #if wxUSE_CARET
77 #include "wx/caret.h"
78 #endif // wxUSE_CARET
79
80 // ----------------------------------------------------------------------------
81 // static data
82 // ----------------------------------------------------------------------------
83
84 #if defined(__WXPM__)
85 int wxWindowBase::ms_lastControlId = 2000;
86 #else
87 int wxWindowBase::ms_lastControlId = -200;
88 #endif
89
90 IMPLEMENT_ABSTRACT_CLASS(wxWindowBase, wxEvtHandler)
91
92 // ----------------------------------------------------------------------------
93 // event table
94 // ----------------------------------------------------------------------------
95
96 BEGIN_EVENT_TABLE(wxWindowBase, wxEvtHandler)
97 EVT_SYS_COLOUR_CHANGED(wxWindowBase::OnSysColourChanged)
98 EVT_INIT_DIALOG(wxWindowBase::OnInitDialog)
99 EVT_MIDDLE_DOWN(wxWindowBase::OnMiddleClick)
100
101 #if wxUSE_HELP
102 EVT_HELP(wxID_ANY, wxWindowBase::OnHelp)
103 #endif // wxUSE_HELP
104
105 END_EVENT_TABLE()
106
107 // ============================================================================
108 // implementation of the common functionality of the wxWindow class
109 // ============================================================================
110
111 // ----------------------------------------------------------------------------
112 // initialization
113 // ----------------------------------------------------------------------------
114
115 // the default initialization
116 wxWindowBase::wxWindowBase()
117 {
118 // no window yet, no parent nor children
119 m_parent = (wxWindow *)NULL;
120 m_windowId = wxID_ANY;
121
122 // no constraints on the minimal window size
123 m_minWidth =
124 m_maxWidth = wxDefaultCoord;
125 m_minHeight =
126 m_maxHeight = wxDefaultCoord;
127
128 // invalidiated cache value
129 m_bestSizeCache = wxDefaultSize;
130
131 // window are created enabled and visible by default
132 m_isShown =
133 m_isEnabled = true;
134
135 // the default event handler is just this window
136 m_eventHandler = this;
137
138 #if wxUSE_VALIDATORS
139 // no validator
140 m_windowValidator = (wxValidator *) NULL;
141 #endif // wxUSE_VALIDATORS
142
143 // the colours/fonts are default for now, so leave m_font,
144 // m_backgroundColour and m_foregroundColour uninitialized and set those
145 m_hasBgCol =
146 m_hasFgCol =
147 m_hasFont = false;
148 m_inheritBgCol =
149 m_inheritFgCol =
150 m_inheritFont = false;
151
152 // no style bits
153 m_exStyle =
154 m_windowStyle = 0;
155
156 m_backgroundStyle = wxBG_STYLE_SYSTEM;
157
158 #if wxUSE_CONSTRAINTS
159 // no constraints whatsoever
160 m_constraints = (wxLayoutConstraints *) NULL;
161 m_constraintsInvolvedIn = (wxWindowList *) NULL;
162 #endif // wxUSE_CONSTRAINTS
163
164 m_windowSizer = (wxSizer *) NULL;
165 m_containingSizer = (wxSizer *) NULL;
166 m_autoLayout = false;
167
168 #if wxUSE_DRAG_AND_DROP
169 m_dropTarget = (wxDropTarget *)NULL;
170 #endif // wxUSE_DRAG_AND_DROP
171
172 #if wxUSE_TOOLTIPS
173 m_tooltip = (wxToolTip *)NULL;
174 #endif // wxUSE_TOOLTIPS
175
176 #if wxUSE_CARET
177 m_caret = (wxCaret *)NULL;
178 #endif // wxUSE_CARET
179
180 #if wxUSE_PALETTE
181 m_hasCustomPalette = false;
182 #endif // wxUSE_PALETTE
183
184 #if wxUSE_ACCESSIBILITY
185 m_accessible = NULL;
186 #endif
187
188 m_virtualSize = wxDefaultSize;
189
190 m_minVirtualWidth =
191 m_maxVirtualWidth = wxDefaultCoord;
192 m_minVirtualHeight =
193 m_maxVirtualHeight = wxDefaultCoord;
194
195 m_windowVariant = wxWINDOW_VARIANT_NORMAL;
196
197 // Whether we're using the current theme for this window (wxGTK only for now)
198 m_themeEnabled = false;
199
200 // VZ: this one shouldn't exist...
201 m_isBeingDeleted = false;
202 }
203
204 // common part of window creation process
205 bool wxWindowBase::CreateBase(wxWindowBase *parent,
206 wxWindowID id,
207 const wxPoint& WXUNUSED(pos),
208 const wxSize& WXUNUSED(size),
209 long style,
210 const wxValidator& wxVALIDATOR_PARAM(validator),
211 const wxString& name)
212 {
213 #if wxUSE_STATBOX
214 // wxGTK doesn't allow to create controls with static box as the parent so
215 // this will result in a crash when the program is ported to wxGTK so warn
216 // the user about it
217
218 // if you get this assert, the correct solution is to create the controls
219 // as siblings of the static box
220 wxASSERT_MSG( !parent || !wxDynamicCast(parent, wxStaticBox),
221 _T("wxStaticBox can't be used as a window parent!") );
222 #endif // wxUSE_STATBOX
223
224 // ids are limited to 16 bits under MSW so if you care about portability,
225 // it's not a good idea to use ids out of this range (and negative ids are
226 // reserved for wxWidgets own usage)
227 wxASSERT_MSG( id == wxID_ANY || (id >= 0 && id < 32767),
228 _T("invalid id value") );
229
230 // generate a new id if the user doesn't care about it
231 m_windowId = id == wxID_ANY ? NewControlId() : id;
232
233 SetName(name);
234 SetWindowStyleFlag(style);
235 SetParent(parent);
236
237 #if wxUSE_VALIDATORS
238 SetValidator(validator);
239 #endif // wxUSE_VALIDATORS
240
241 // if the parent window has wxWS_EX_VALIDATE_RECURSIVELY set, we want to
242 // have it too - like this it's possible to set it only in the top level
243 // dialog/frame and all children will inherit it by defult
244 if ( parent && (parent->GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) )
245 {
246 SetExtraStyle(GetExtraStyle() | wxWS_EX_VALIDATE_RECURSIVELY);
247 }
248
249 return true;
250 }
251
252 // ----------------------------------------------------------------------------
253 // destruction
254 // ----------------------------------------------------------------------------
255
256 // common clean up
257 wxWindowBase::~wxWindowBase()
258 {
259 wxASSERT_MSG( GetCapture() != this, wxT("attempt to destroy window with mouse capture") );
260
261 // FIXME if these 2 cases result from programming errors in the user code
262 // we should probably assert here instead of silently fixing them
263
264 // Just in case the window has been Closed, but we're then deleting
265 // immediately: don't leave dangling pointers.
266 wxPendingDelete.DeleteObject(this);
267
268 // Just in case we've loaded a top-level window via LoadNativeDialog but
269 // we weren't a dialog class
270 wxTopLevelWindows.DeleteObject((wxWindow*)this);
271
272 wxASSERT_MSG( GetChildren().GetCount() == 0, wxT("children not destroyed") );
273
274 // reset the dangling pointer our parent window may keep to us
275 if ( m_parent )
276 {
277 if ( m_parent->GetDefaultItem() == this )
278 {
279 m_parent->SetDefaultItem(NULL);
280 }
281
282 m_parent->RemoveChild(this);
283 }
284
285 #if wxUSE_CARET
286 delete m_caret;
287 #endif // wxUSE_CARET
288
289 #if wxUSE_VALIDATORS
290 delete m_windowValidator;
291 #endif // wxUSE_VALIDATORS
292
293 #if wxUSE_CONSTRAINTS
294 // Have to delete constraints/sizer FIRST otherwise sizers may try to look
295 // at deleted windows as they delete themselves.
296 DeleteRelatedConstraints();
297
298 if ( m_constraints )
299 {
300 // This removes any dangling pointers to this window in other windows'
301 // constraintsInvolvedIn lists.
302 UnsetConstraints(m_constraints);
303 delete m_constraints;
304 m_constraints = NULL;
305 }
306 #endif // wxUSE_CONSTRAINTS
307
308 if ( m_containingSizer )
309 m_containingSizer->Detach( (wxWindow*)this );
310
311 delete m_windowSizer;
312
313 #if wxUSE_DRAG_AND_DROP
314 delete m_dropTarget;
315 #endif // wxUSE_DRAG_AND_DROP
316
317 #if wxUSE_TOOLTIPS
318 delete m_tooltip;
319 #endif // wxUSE_TOOLTIPS
320
321 #if wxUSE_ACCESSIBILITY
322 delete m_accessible;
323 #endif
324 }
325
326 bool wxWindowBase::Destroy()
327 {
328 delete this;
329
330 return true;
331 }
332
333 bool wxWindowBase::Close(bool force)
334 {
335 wxCloseEvent event(wxEVT_CLOSE_WINDOW, m_windowId);
336 event.SetEventObject(this);
337 event.SetCanVeto(!force);
338
339 // return false if window wasn't closed because the application vetoed the
340 // close event
341 return GetEventHandler()->ProcessEvent(event) && !event.GetVeto();
342 }
343
344 bool wxWindowBase::DestroyChildren()
345 {
346 wxWindowList::compatibility_iterator node;
347 for ( ;; )
348 {
349 // we iterate until the list becomes empty
350 node = GetChildren().GetFirst();
351 if ( !node )
352 break;
353
354 wxWindow *child = node->GetData();
355
356 // note that we really want to call delete and not ->Destroy() here
357 // because we want to delete the child immediately, before we are
358 // deleted, and delayed deletion would result in problems as our (top
359 // level) child could outlive its parent
360 delete child;
361
362 wxASSERT_MSG( !GetChildren().Find(child),
363 wxT("child didn't remove itself using RemoveChild()") );
364 }
365
366 return true;
367 }
368
369 // ----------------------------------------------------------------------------
370 // size/position related methods
371 // ----------------------------------------------------------------------------
372
373 // centre the window with respect to its parent in either (or both) directions
374 void wxWindowBase::Centre(int direction)
375 {
376 // the position/size of the parent window or of the entire screen
377 wxPoint posParent;
378 int widthParent, heightParent;
379
380 wxWindow *parent = NULL;
381
382 if ( !(direction & wxCENTRE_ON_SCREEN) )
383 {
384 // find the parent to centre this window on: it should be the
385 // immediate parent for the controls but the top level parent for the
386 // top level windows (like dialogs)
387 parent = GetParent();
388 if ( IsTopLevel() )
389 {
390 while ( parent && !parent->IsTopLevel() )
391 {
392 parent = parent->GetParent();
393 }
394 }
395
396 // there is no wxTopLevelWindow under wxMotif yet
397 #ifndef __WXMOTIF__
398 // we shouldn't center the dialog on the iconized window: under
399 // Windows, for example, this places it completely off the screen
400 if ( parent )
401 {
402 wxTopLevelWindow *winTop = wxDynamicCast(parent, wxTopLevelWindow);
403 if ( winTop && winTop->IsIconized() )
404 {
405 parent = NULL;
406 }
407 }
408 #endif // __WXMOTIF__
409
410 // did we find the parent?
411 if ( !parent )
412 {
413 // no other choice
414 direction |= wxCENTRE_ON_SCREEN;
415 }
416 }
417
418 if ( direction & wxCENTRE_ON_SCREEN )
419 {
420 // centre with respect to the whole screen
421 wxDisplaySize(&widthParent, &heightParent);
422 }
423 else
424 {
425 if ( IsTopLevel() )
426 {
427 // centre on the parent
428 parent->GetSize(&widthParent, &heightParent);
429
430 // adjust to the parents position
431 posParent = parent->GetPosition();
432 }
433 else
434 {
435 // centre inside the parents client rectangle
436 parent->GetClientSize(&widthParent, &heightParent);
437 }
438 }
439
440 int width, height;
441 GetSize(&width, &height);
442
443 int xNew = wxDefaultCoord,
444 yNew = wxDefaultCoord;
445
446 if ( direction & wxHORIZONTAL )
447 xNew = (widthParent - width)/2;
448
449 if ( direction & wxVERTICAL )
450 yNew = (heightParent - height)/2;
451
452 xNew += posParent.x;
453 yNew += posParent.y;
454
455 // Base size of the visible dimensions of the display
456 // to take into account the taskbar. And the Mac menu bar at top.
457 wxRect clientrect = wxGetClientDisplayRect();
458
459 // NB: in wxMSW, negative position may not neccessary mean "out of screen",
460 // but it may mean that the window is placed on other than the main
461 // display. Therefore we only make sure centered window is on the main display
462 // if the parent is at least partially present here.
463 if (posParent.x + widthParent >= 0) // if parent is (partially) on the main display
464 {
465 if (xNew < clientrect.GetLeft())
466 xNew = clientrect.GetLeft();
467 else if (xNew + width > clientrect.GetRight())
468 xNew = clientrect.GetRight() - width;
469 }
470 if (posParent.y + heightParent >= 0) // if parent is (partially) on the main display
471 {
472 if (yNew + height > clientrect.GetBottom())
473 yNew = clientrect.GetBottom() - height;
474
475 // Make certain that the title bar is initially visible
476 // always, even if this would push the bottom of the
477 // dialog off the visible area of the display
478 if (yNew < clientrect.GetTop())
479 yNew = clientrect.GetTop();
480 }
481
482 // move the window to this position (keeping the old size but using
483 // SetSize() and not Move() to allow xNew and/or yNew to be -1)
484 SetSize(xNew, yNew, width, height, wxSIZE_ALLOW_MINUS_ONE);
485 }
486
487 // fits the window around the children
488 void wxWindowBase::Fit()
489 {
490 if ( GetChildren().GetCount() > 0 )
491 {
492 SetClientSize(GetBestSize());
493 }
494 //else: do nothing if we have no children
495 }
496
497 // fits virtual size (ie. scrolled area etc.) around children
498 void wxWindowBase::FitInside()
499 {
500 if ( GetChildren().GetCount() > 0 )
501 {
502 SetVirtualSize( GetBestVirtualSize() );
503 }
504 }
505
506 // On Mac, scrollbars are explicitly children.
507 #ifdef __WXMAC__
508 static bool wxHasRealChildren(const wxWindowBase* win)
509 {
510 int realChildCount = 0;
511
512 for ( wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
513 node;
514 node = node->GetNext() )
515 {
516 wxWindow *win = node->GetData();
517 if ( !win->IsTopLevel() && win->IsShown() && !win->IsKindOf(CLASSINFO(wxScrollBar)))
518 realChildCount ++;
519 }
520 return (realChildCount > 0);
521 }
522 #endif
523
524 // return the size best suited for the current window
525 wxSize wxWindowBase::DoGetBestSize() const
526 {
527 if ( m_windowSizer )
528 {
529 return m_windowSizer->GetMinSize();
530 }
531 #if wxUSE_CONSTRAINTS
532 else if ( m_constraints )
533 {
534 wxConstCast(this, wxWindowBase)->SatisfyConstraints();
535
536 // our minimal acceptable size is such that all our windows fit inside
537 int maxX = 0,
538 maxY = 0;
539
540 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
541 node;
542 node = node->GetNext() )
543 {
544 wxLayoutConstraints *c = node->GetData()->GetConstraints();
545 if ( !c )
546 {
547 // it's not normal that we have an unconstrained child, but
548 // what can we do about it?
549 continue;
550 }
551
552 int x = c->right.GetValue(),
553 y = c->bottom.GetValue();
554
555 if ( x > maxX )
556 maxX = x;
557
558 if ( y > maxY )
559 maxY = y;
560
561 // TODO: we must calculate the overlaps somehow, otherwise we
562 // will never return a size bigger than the current one :-(
563 }
564
565 return wxSize(maxX, maxY);
566 }
567 #endif // wxUSE_CONSTRAINTS
568 else if ( !GetChildren().empty()
569 #ifdef __WXMAC__
570 && wxHasRealChildren(this)
571 #endif
572 )
573 {
574 // our minimal acceptable size is such that all our visible child windows fit inside
575 int maxX = 0,
576 maxY = 0;
577
578 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
579 node;
580 node = node->GetNext() )
581 {
582 wxWindow *win = node->GetData();
583 if ( win->IsTopLevel() || ( ! win->IsShown() )
584 #if wxUSE_STATUSBAR
585 || wxDynamicCast(win, wxStatusBar)
586 #endif // wxUSE_STATUSBAR
587 )
588 {
589 // dialogs and frames lie in different top level windows -
590 // don't deal with them here; as for the status bars, they
591 // don't lie in the client area at all
592 continue;
593 }
594
595 int wx, wy, ww, wh;
596 win->GetPosition(&wx, &wy);
597
598 // if the window hadn't been positioned yet, assume that it is in
599 // the origin
600 if ( wx == wxDefaultCoord )
601 wx = 0;
602 if ( wy == wxDefaultCoord )
603 wy = 0;
604
605 win->GetSize(&ww, &wh);
606 if ( wx + ww > maxX )
607 maxX = wx + ww;
608 if ( wy + wh > maxY )
609 maxY = wy + wh;
610 }
611
612 // for compatibility with the old versions and because it really looks
613 // slightly more pretty like this, add a pad
614 maxX += 7;
615 maxY += 14;
616
617 return wxSize(maxX, maxY);
618 }
619 else // ! has children
620 {
621 // for a generic window there is no natural best size - just use either the
622 // minimum size if there is one, or the current size
623 if ( GetMinSize().IsFullySpecified() )
624 return GetMinSize();
625 else
626 return GetSize();
627 }
628 }
629
630
631 wxSize wxWindowBase::GetBestFittingSize() const
632 {
633 // merge the best size with the min size, giving priority to the min size
634 wxSize min = GetMinSize();
635 if (min.x == wxDefaultCoord || min.y == wxDefaultCoord)
636 {
637 wxSize best = GetBestSize();
638 if (min.x == wxDefaultCoord) min.x = best.x;
639 if (min.y == wxDefaultCoord) min.y = best.y;
640 }
641 return min;
642 }
643
644
645 void wxWindowBase::SetBestFittingSize(const wxSize& size)
646 {
647 // Set the min size to the size passed in. This will usually either be
648 // wxDefaultSize or the size passed to this window's ctor/Create function.
649 SetMinSize(size);
650
651 // Merge the size with the best size if needed
652 wxSize best = GetBestFittingSize();
653
654 // If the current size doesn't match then change it
655 if (GetSize() != best)
656 SetSize(best);
657 }
658
659
660 // by default the origin is not shifted
661 wxPoint wxWindowBase::GetClientAreaOrigin() const
662 {
663 return wxPoint(0, 0);
664 }
665
666 // set the min/max size of the window
667 void wxWindowBase::SetSizeHints(int minW, int minH,
668 int maxW, int maxH,
669 int WXUNUSED(incW), int WXUNUSED(incH))
670 {
671 // setting min width greater than max width leads to infinite loops under
672 // X11 and generally doesn't make any sense, so don't allow it
673 wxCHECK_RET( (minW == wxDefaultCoord || maxW == wxDefaultCoord || minW <= maxW) &&
674 (minH == wxDefaultCoord || maxH == wxDefaultCoord || minH <= maxH),
675 _T("min width/height must be less than max width/height!") );
676
677 m_minWidth = minW;
678 m_maxWidth = maxW;
679 m_minHeight = minH;
680 m_maxHeight = maxH;
681 }
682
683 void wxWindowBase::SetWindowVariant( wxWindowVariant variant )
684 {
685 if ( m_windowVariant != variant )
686 {
687 m_windowVariant = variant;
688
689 DoSetWindowVariant(variant);
690 }
691 }
692
693 void wxWindowBase::DoSetWindowVariant( wxWindowVariant variant )
694 {
695 // adjust the font height to correspond to our new variant (notice that
696 // we're only called if something really changed)
697 wxFont font = GetFont();
698 int size = font.GetPointSize();
699 switch ( variant )
700 {
701 case wxWINDOW_VARIANT_NORMAL:
702 break;
703
704 case wxWINDOW_VARIANT_SMALL:
705 size *= 3;
706 size /= 4;
707 break;
708
709 case wxWINDOW_VARIANT_MINI:
710 size *= 2;
711 size /= 3;
712 break;
713
714 case wxWINDOW_VARIANT_LARGE:
715 size *= 5;
716 size /= 4;
717 break;
718
719 default:
720 wxFAIL_MSG(_T("unexpected window variant"));
721 break;
722 }
723
724 font.SetPointSize(size);
725 SetFont(font);
726 }
727
728 void wxWindowBase::SetVirtualSizeHints( int minW, int minH,
729 int maxW, int maxH )
730 {
731 m_minVirtualWidth = minW;
732 m_maxVirtualWidth = maxW;
733 m_minVirtualHeight = minH;
734 m_maxVirtualHeight = maxH;
735 }
736
737 void wxWindowBase::DoSetVirtualSize( int x, int y )
738 {
739 if ( m_minVirtualWidth != wxDefaultCoord && m_minVirtualWidth > x )
740 x = m_minVirtualWidth;
741 if ( m_maxVirtualWidth != wxDefaultCoord && m_maxVirtualWidth < x )
742 x = m_maxVirtualWidth;
743 if ( m_minVirtualHeight != wxDefaultCoord && m_minVirtualHeight > y )
744 y = m_minVirtualHeight;
745 if ( m_maxVirtualHeight != wxDefaultCoord && m_maxVirtualHeight < y )
746 y = m_maxVirtualHeight;
747
748 m_virtualSize = wxSize(x, y);
749 }
750
751 wxSize wxWindowBase::DoGetVirtualSize() const
752 {
753 wxSize s( GetClientSize() );
754
755 return wxSize( wxMax( m_virtualSize.GetWidth(), s.GetWidth() ),
756 wxMax( m_virtualSize.GetHeight(), s.GetHeight() ) );
757 }
758
759 // ----------------------------------------------------------------------------
760 // show/hide/enable/disable the window
761 // ----------------------------------------------------------------------------
762
763 bool wxWindowBase::Show(bool show)
764 {
765 if ( show != m_isShown )
766 {
767 m_isShown = show;
768
769 return true;
770 }
771 else
772 {
773 return false;
774 }
775 }
776
777 bool wxWindowBase::Enable(bool enable)
778 {
779 if ( enable != m_isEnabled )
780 {
781 m_isEnabled = enable;
782
783 return true;
784 }
785 else
786 {
787 return false;
788 }
789 }
790 // ----------------------------------------------------------------------------
791 // RTTI
792 // ----------------------------------------------------------------------------
793
794 bool wxWindowBase::IsTopLevel() const
795 {
796 return false;
797 }
798
799 // ----------------------------------------------------------------------------
800 // reparenting the window
801 // ----------------------------------------------------------------------------
802
803 void wxWindowBase::AddChild(wxWindowBase *child)
804 {
805 wxCHECK_RET( child, wxT("can't add a NULL child") );
806
807 // this should never happen and it will lead to a crash later if it does
808 // because RemoveChild() will remove only one node from the children list
809 // and the other(s) one(s) will be left with dangling pointers in them
810 wxASSERT_MSG( !GetChildren().Find((wxWindow*)child), _T("AddChild() called twice") );
811
812 GetChildren().Append((wxWindow*)child);
813 child->SetParent(this);
814 }
815
816 void wxWindowBase::RemoveChild(wxWindowBase *child)
817 {
818 wxCHECK_RET( child, wxT("can't remove a NULL child") );
819
820 GetChildren().DeleteObject((wxWindow *)child);
821 child->SetParent(NULL);
822 }
823
824 bool wxWindowBase::Reparent(wxWindowBase *newParent)
825 {
826 wxWindow *oldParent = GetParent();
827 if ( newParent == oldParent )
828 {
829 // nothing done
830 return false;
831 }
832
833 // unlink this window from the existing parent.
834 if ( oldParent )
835 {
836 oldParent->RemoveChild(this);
837 }
838 else
839 {
840 wxTopLevelWindows.DeleteObject((wxWindow *)this);
841 }
842
843 // add it to the new one
844 if ( newParent )
845 {
846 newParent->AddChild(this);
847 }
848 else
849 {
850 wxTopLevelWindows.Append((wxWindow *)this);
851 }
852
853 return true;
854 }
855
856 // ----------------------------------------------------------------------------
857 // event handler stuff
858 // ----------------------------------------------------------------------------
859
860 void wxWindowBase::PushEventHandler(wxEvtHandler *handler)
861 {
862 wxEvtHandler *handlerOld = GetEventHandler();
863
864 handler->SetNextHandler(handlerOld);
865
866 if ( handlerOld )
867 GetEventHandler()->SetPreviousHandler(handler);
868
869 SetEventHandler(handler);
870 }
871
872 wxEvtHandler *wxWindowBase::PopEventHandler(bool deleteHandler)
873 {
874 wxEvtHandler *handlerA = GetEventHandler();
875 if ( handlerA )
876 {
877 wxEvtHandler *handlerB = handlerA->GetNextHandler();
878 handlerA->SetNextHandler((wxEvtHandler *)NULL);
879
880 if ( handlerB )
881 handlerB->SetPreviousHandler((wxEvtHandler *)NULL);
882 SetEventHandler(handlerB);
883
884 if ( deleteHandler )
885 {
886 delete handlerA;
887 handlerA = (wxEvtHandler *)NULL;
888 }
889 }
890
891 return handlerA;
892 }
893
894 bool wxWindowBase::RemoveEventHandler(wxEvtHandler *handler)
895 {
896 wxCHECK_MSG( handler, false, _T("RemoveEventHandler(NULL) called") );
897
898 wxEvtHandler *handlerPrev = NULL,
899 *handlerCur = GetEventHandler();
900 while ( handlerCur )
901 {
902 wxEvtHandler *handlerNext = handlerCur->GetNextHandler();
903
904 if ( handlerCur == handler )
905 {
906 if ( handlerPrev )
907 {
908 handlerPrev->SetNextHandler(handlerNext);
909 }
910 else
911 {
912 SetEventHandler(handlerNext);
913 }
914
915 if ( handlerNext )
916 {
917 handlerNext->SetPreviousHandler ( handlerPrev );
918 }
919
920 handler->SetNextHandler(NULL);
921 handler->SetPreviousHandler(NULL);
922
923 return true;
924 }
925
926 handlerPrev = handlerCur;
927 handlerCur = handlerNext;
928 }
929
930 wxFAIL_MSG( _T("where has the event handler gone?") );
931
932 return false;
933 }
934
935 // ----------------------------------------------------------------------------
936 // colours, fonts &c
937 // ----------------------------------------------------------------------------
938
939 void wxWindowBase::InheritAttributes()
940 {
941 const wxWindowBase * const parent = GetParent();
942 if ( !parent )
943 return;
944
945 // we only inherit attributes which had been explicitly set for the parent
946 // which ensures that this only happens if the user really wants it and
947 // not by default which wouldn't make any sense in modern GUIs where the
948 // controls don't all use the same fonts (nor colours)
949 if ( parent->m_inheritFont && !m_hasFont )
950 SetFont(parent->GetFont());
951
952 // in addition, there is a possibility to explicitly forbid inheriting
953 // colours at each class level by overriding ShouldInheritColours()
954 if ( ShouldInheritColours() )
955 {
956 if ( parent->m_inheritFgCol && !m_hasFgCol )
957 SetForegroundColour(parent->GetForegroundColour());
958
959 if ( parent->m_inheritBgCol && !m_hasBgCol )
960 SetBackgroundColour(parent->GetBackgroundColour());
961 }
962 }
963
964 /* static */ wxVisualAttributes
965 wxWindowBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
966 {
967 // it is important to return valid values for all attributes from here,
968 // GetXXX() below rely on this
969 wxVisualAttributes attrs;
970 attrs.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
971 attrs.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
972 attrs.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
973
974 return attrs;
975 }
976
977 wxColour wxWindowBase::GetBackgroundColour() const
978 {
979 if ( !m_backgroundColour.Ok() )
980 {
981 wxASSERT_MSG( !m_hasBgCol, _T("we have invalid explicit bg colour?") );
982
983 // get our default background colour
984 wxColour colBg = GetDefaultAttributes().colBg;
985
986 // we must return some valid colour to avoid redoing this every time
987 // and also to avoid surprizing the applications written for older
988 // wxWidgets versions where GetBackgroundColour() always returned
989 // something -- so give them something even if it doesn't make sense
990 // for this window (e.g. it has a themed background)
991 if ( !colBg.Ok() )
992 colBg = GetClassDefaultAttributes().colBg;
993
994 return colBg;
995 }
996 else
997 return m_backgroundColour;
998 }
999
1000 wxColour wxWindowBase::GetForegroundColour() const
1001 {
1002 // logic is the same as above
1003 if ( !m_hasFgCol && !m_foregroundColour.Ok() )
1004 {
1005 wxASSERT_MSG( !m_hasFgCol, _T("we have invalid explicit fg colour?") );
1006
1007 wxColour colFg = GetDefaultAttributes().colFg;
1008
1009 if ( !colFg.Ok() )
1010 colFg = GetClassDefaultAttributes().colFg;
1011
1012 return colFg;
1013 }
1014 else
1015 return m_foregroundColour;
1016 }
1017
1018 bool wxWindowBase::SetBackgroundColour( const wxColour &colour )
1019 {
1020 if ( colour == m_backgroundColour )
1021 return false;
1022
1023 m_hasBgCol = colour.Ok();
1024 m_inheritBgCol = m_hasBgCol;
1025 m_backgroundColour = colour;
1026 SetThemeEnabled( !m_hasBgCol && !m_foregroundColour.Ok() );
1027 return true;
1028 }
1029
1030 bool wxWindowBase::SetForegroundColour( const wxColour &colour )
1031 {
1032 if (colour == m_foregroundColour )
1033 return false;
1034
1035 m_hasFgCol = colour.Ok();
1036 m_inheritFgCol = m_hasFgCol;
1037 m_foregroundColour = colour;
1038 SetThemeEnabled( !m_hasFgCol && !m_backgroundColour.Ok() );
1039 return true;
1040 }
1041
1042 bool wxWindowBase::SetCursor(const wxCursor& cursor)
1043 {
1044 // setting an invalid cursor is ok, it means that we don't have any special
1045 // cursor
1046 if ( m_cursor == cursor )
1047 {
1048 // no change
1049 return false;
1050 }
1051
1052 m_cursor = cursor;
1053
1054 return true;
1055 }
1056
1057 wxFont wxWindowBase::GetFont() const
1058 {
1059 // logic is the same as in GetBackgroundColour()
1060 if ( !m_font.Ok() )
1061 {
1062 wxASSERT_MSG( !m_hasFont, _T("we have invalid explicit font?") );
1063
1064 wxFont font = GetDefaultAttributes().font;
1065 if ( !font.Ok() )
1066 font = GetClassDefaultAttributes().font;
1067
1068 return font;
1069 }
1070 else
1071 return m_font;
1072 }
1073
1074 bool wxWindowBase::SetFont(const wxFont& font)
1075 {
1076 if ( font == m_font )
1077 {
1078 // no change
1079 return false;
1080 }
1081
1082 m_font = font;
1083 m_hasFont = font.Ok();
1084 m_inheritFont = m_hasFont;
1085
1086 InvalidateBestSize();
1087
1088 return true;
1089 }
1090
1091 #if wxUSE_PALETTE
1092
1093 void wxWindowBase::SetPalette(const wxPalette& pal)
1094 {
1095 m_hasCustomPalette = true;
1096 m_palette = pal;
1097
1098 // VZ: can anyone explain me what do we do here?
1099 wxWindowDC d((wxWindow *) this);
1100 d.SetPalette(pal);
1101 }
1102
1103 wxWindow *wxWindowBase::GetAncestorWithCustomPalette() const
1104 {
1105 wxWindow *win = (wxWindow *)this;
1106 while ( win && !win->HasCustomPalette() )
1107 {
1108 win = win->GetParent();
1109 }
1110
1111 return win;
1112 }
1113
1114 #endif // wxUSE_PALETTE
1115
1116 #if wxUSE_CARET
1117 void wxWindowBase::SetCaret(wxCaret *caret)
1118 {
1119 if ( m_caret )
1120 {
1121 delete m_caret;
1122 }
1123
1124 m_caret = caret;
1125
1126 if ( m_caret )
1127 {
1128 wxASSERT_MSG( m_caret->GetWindow() == this,
1129 wxT("caret should be created associated to this window") );
1130 }
1131 }
1132 #endif // wxUSE_CARET
1133
1134 #if wxUSE_VALIDATORS
1135 // ----------------------------------------------------------------------------
1136 // validators
1137 // ----------------------------------------------------------------------------
1138
1139 void wxWindowBase::SetValidator(const wxValidator& validator)
1140 {
1141 if ( m_windowValidator )
1142 delete m_windowValidator;
1143
1144 m_windowValidator = (wxValidator *)validator.Clone();
1145
1146 if ( m_windowValidator )
1147 m_windowValidator->SetWindow(this);
1148 }
1149 #endif // wxUSE_VALIDATORS
1150
1151 // ----------------------------------------------------------------------------
1152 // update region stuff
1153 // ----------------------------------------------------------------------------
1154
1155 wxRect wxWindowBase::GetUpdateClientRect() const
1156 {
1157 wxRegion rgnUpdate = GetUpdateRegion();
1158 rgnUpdate.Intersect(GetClientRect());
1159 wxRect rectUpdate = rgnUpdate.GetBox();
1160 wxPoint ptOrigin = GetClientAreaOrigin();
1161 rectUpdate.x -= ptOrigin.x;
1162 rectUpdate.y -= ptOrigin.y;
1163
1164 return rectUpdate;
1165 }
1166
1167 bool wxWindowBase::IsExposed(int x, int y) const
1168 {
1169 return m_updateRegion.Contains(x, y) != wxOutRegion;
1170 }
1171
1172 bool wxWindowBase::IsExposed(int x, int y, int w, int h) const
1173 {
1174 return m_updateRegion.Contains(x, y, w, h) != wxOutRegion;
1175 }
1176
1177 void wxWindowBase::ClearBackground()
1178 {
1179 // wxGTK uses its own version, no need to add never used code
1180 #ifndef __WXGTK__
1181 wxClientDC dc((wxWindow *)this);
1182 wxBrush brush(GetBackgroundColour(), wxSOLID);
1183 dc.SetBackground(brush);
1184 dc.Clear();
1185 #endif // __WXGTK__
1186 }
1187
1188 // ----------------------------------------------------------------------------
1189 // find child window by id or name
1190 // ----------------------------------------------------------------------------
1191
1192 wxWindow *wxWindowBase::FindWindow( long id )
1193 {
1194 if ( id == m_windowId )
1195 return (wxWindow *)this;
1196
1197 wxWindowBase *res = (wxWindow *)NULL;
1198 wxWindowList::compatibility_iterator node;
1199 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
1200 {
1201 wxWindowBase *child = node->GetData();
1202 res = child->FindWindow( id );
1203 }
1204
1205 return (wxWindow *)res;
1206 }
1207
1208 wxWindow *wxWindowBase::FindWindow( const wxString& name )
1209 {
1210 if ( name == m_windowName )
1211 return (wxWindow *)this;
1212
1213 wxWindowBase *res = (wxWindow *)NULL;
1214 wxWindowList::compatibility_iterator node;
1215 for ( node = m_children.GetFirst(); node && !res; node = node->GetNext() )
1216 {
1217 wxWindow *child = node->GetData();
1218 res = child->FindWindow(name);
1219 }
1220
1221 return (wxWindow *)res;
1222 }
1223
1224
1225 // find any window by id or name or label: If parent is non-NULL, look through
1226 // children for a label or title matching the specified string. If NULL, look
1227 // through all top-level windows.
1228 //
1229 // to avoid duplicating code we reuse the same helper function but with
1230 // different comparators
1231
1232 typedef bool (*wxFindWindowCmp)(const wxWindow *win,
1233 const wxString& label, long id);
1234
1235 static
1236 bool wxFindWindowCmpLabels(const wxWindow *win, const wxString& label,
1237 long WXUNUSED(id))
1238 {
1239 return win->GetLabel() == label;
1240 }
1241
1242 static
1243 bool wxFindWindowCmpNames(const wxWindow *win, const wxString& label,
1244 long WXUNUSED(id))
1245 {
1246 return win->GetName() == label;
1247 }
1248
1249 static
1250 bool wxFindWindowCmpIds(const wxWindow *win, const wxString& WXUNUSED(label),
1251 long id)
1252 {
1253 return win->GetId() == id;
1254 }
1255
1256 // recursive helper for the FindWindowByXXX() functions
1257 static
1258 wxWindow *wxFindWindowRecursively(const wxWindow *parent,
1259 const wxString& label,
1260 long id,
1261 wxFindWindowCmp cmp)
1262 {
1263 if ( parent )
1264 {
1265 // see if this is the one we're looking for
1266 if ( (*cmp)(parent, label, id) )
1267 return (wxWindow *)parent;
1268
1269 // It wasn't, so check all its children
1270 for ( wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst();
1271 node;
1272 node = node->GetNext() )
1273 {
1274 // recursively check each child
1275 wxWindow *win = (wxWindow *)node->GetData();
1276 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1277 if (retwin)
1278 return retwin;
1279 }
1280 }
1281
1282 // Not found
1283 return NULL;
1284 }
1285
1286 // helper for FindWindowByXXX()
1287 static
1288 wxWindow *wxFindWindowHelper(const wxWindow *parent,
1289 const wxString& label,
1290 long id,
1291 wxFindWindowCmp cmp)
1292 {
1293 if ( parent )
1294 {
1295 // just check parent and all its children
1296 return wxFindWindowRecursively(parent, label, id, cmp);
1297 }
1298
1299 // start at very top of wx's windows
1300 for ( wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
1301 node;
1302 node = node->GetNext() )
1303 {
1304 // recursively check each window & its children
1305 wxWindow *win = node->GetData();
1306 wxWindow *retwin = wxFindWindowRecursively(win, label, id, cmp);
1307 if (retwin)
1308 return retwin;
1309 }
1310
1311 return NULL;
1312 }
1313
1314 /* static */
1315 wxWindow *
1316 wxWindowBase::FindWindowByLabel(const wxString& title, const wxWindow *parent)
1317 {
1318 return wxFindWindowHelper(parent, title, 0, wxFindWindowCmpLabels);
1319 }
1320
1321 /* static */
1322 wxWindow *
1323 wxWindowBase::FindWindowByName(const wxString& title, const wxWindow *parent)
1324 {
1325 wxWindow *win = wxFindWindowHelper(parent, title, 0, wxFindWindowCmpNames);
1326
1327 if ( !win )
1328 {
1329 // fall back to the label
1330 win = FindWindowByLabel(title, parent);
1331 }
1332
1333 return win;
1334 }
1335
1336 /* static */
1337 wxWindow *
1338 wxWindowBase::FindWindowById( long id, const wxWindow* parent )
1339 {
1340 return wxFindWindowHelper(parent, _T(""), id, wxFindWindowCmpIds);
1341 }
1342
1343 // ----------------------------------------------------------------------------
1344 // dialog oriented functions
1345 // ----------------------------------------------------------------------------
1346
1347 void wxWindowBase::MakeModal(bool modal)
1348 {
1349 // Disable all other windows
1350 if ( IsTopLevel() )
1351 {
1352 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
1353 while (node)
1354 {
1355 wxWindow *win = node->GetData();
1356 if (win != this)
1357 win->Enable(!modal);
1358
1359 node = node->GetNext();
1360 }
1361 }
1362 }
1363
1364 bool wxWindowBase::Validate()
1365 {
1366 #if wxUSE_VALIDATORS
1367 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1368
1369 wxWindowList::compatibility_iterator node;
1370 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1371 {
1372 wxWindowBase *child = node->GetData();
1373 wxValidator *validator = child->GetValidator();
1374 if ( validator && !validator->Validate((wxWindow *)this) )
1375 {
1376 return false;
1377 }
1378
1379 if ( recurse && !child->Validate() )
1380 {
1381 return false;
1382 }
1383 }
1384 #endif // wxUSE_VALIDATORS
1385
1386 return true;
1387 }
1388
1389 bool wxWindowBase::TransferDataToWindow()
1390 {
1391 #if wxUSE_VALIDATORS
1392 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1393
1394 wxWindowList::compatibility_iterator node;
1395 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1396 {
1397 wxWindowBase *child = node->GetData();
1398 wxValidator *validator = child->GetValidator();
1399 if ( validator && !validator->TransferToWindow() )
1400 {
1401 wxLogWarning(_("Could not transfer data to window"));
1402 #if wxUSE_LOG
1403 wxLog::FlushActive();
1404 #endif // wxUSE_LOG
1405
1406 return false;
1407 }
1408
1409 if ( recurse )
1410 {
1411 if ( !child->TransferDataToWindow() )
1412 {
1413 // warning already given
1414 return false;
1415 }
1416 }
1417 }
1418 #endif // wxUSE_VALIDATORS
1419
1420 return true;
1421 }
1422
1423 bool wxWindowBase::TransferDataFromWindow()
1424 {
1425 #if wxUSE_VALIDATORS
1426 bool recurse = (GetExtraStyle() & wxWS_EX_VALIDATE_RECURSIVELY) != 0;
1427
1428 wxWindowList::compatibility_iterator node;
1429 for ( node = m_children.GetFirst(); node; node = node->GetNext() )
1430 {
1431 wxWindow *child = node->GetData();
1432 wxValidator *validator = child->GetValidator();
1433 if ( validator && !validator->TransferFromWindow() )
1434 {
1435 // nop warning here because the application is supposed to give
1436 // one itself - we don't know here what might have gone wrongly
1437
1438 return false;
1439 }
1440
1441 if ( recurse )
1442 {
1443 if ( !child->TransferDataFromWindow() )
1444 {
1445 // warning already given
1446 return false;
1447 }
1448 }
1449 }
1450 #endif // wxUSE_VALIDATORS
1451
1452 return true;
1453 }
1454
1455 void wxWindowBase::InitDialog()
1456 {
1457 wxInitDialogEvent event(GetId());
1458 event.SetEventObject( this );
1459 GetEventHandler()->ProcessEvent(event);
1460 }
1461
1462 // ----------------------------------------------------------------------------
1463 // context-sensitive help support
1464 // ----------------------------------------------------------------------------
1465
1466 #if wxUSE_HELP
1467
1468 // associate this help text with this window
1469 void wxWindowBase::SetHelpText(const wxString& text)
1470 {
1471 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1472 if ( helpProvider )
1473 {
1474 helpProvider->AddHelp(this, text);
1475 }
1476 }
1477
1478 // associate this help text with all windows with the same id as this
1479 // one
1480 void wxWindowBase::SetHelpTextForId(const wxString& text)
1481 {
1482 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1483 if ( helpProvider )
1484 {
1485 helpProvider->AddHelp(GetId(), text);
1486 }
1487 }
1488
1489 // get the help string associated with this window (may be empty)
1490 wxString wxWindowBase::GetHelpText() const
1491 {
1492 wxString text;
1493 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1494 if ( helpProvider )
1495 {
1496 text = helpProvider->GetHelp(this);
1497 }
1498
1499 return text;
1500 }
1501
1502 // show help for this window
1503 void wxWindowBase::OnHelp(wxHelpEvent& event)
1504 {
1505 wxHelpProvider *helpProvider = wxHelpProvider::Get();
1506 if ( helpProvider )
1507 {
1508 if ( helpProvider->ShowHelp(this) )
1509 {
1510 // skip the event.Skip() below
1511 return;
1512 }
1513 }
1514
1515 event.Skip();
1516 }
1517
1518 #endif // wxUSE_HELP
1519
1520 // ----------------------------------------------------------------------------
1521 // tooltipsroot.Replace("\\", "/");
1522 // ----------------------------------------------------------------------------
1523
1524 #if wxUSE_TOOLTIPS
1525
1526 void wxWindowBase::SetToolTip( const wxString &tip )
1527 {
1528 // don't create the new tooltip if we already have one
1529 if ( m_tooltip )
1530 {
1531 m_tooltip->SetTip( tip );
1532 }
1533 else
1534 {
1535 SetToolTip( new wxToolTip( tip ) );
1536 }
1537
1538 // setting empty tooltip text does not remove the tooltip any more - use
1539 // SetToolTip((wxToolTip *)NULL) for this
1540 }
1541
1542 void wxWindowBase::DoSetToolTip(wxToolTip *tooltip)
1543 {
1544 if ( m_tooltip )
1545 delete m_tooltip;
1546
1547 m_tooltip = tooltip;
1548 }
1549
1550 #endif // wxUSE_TOOLTIPS
1551
1552 // ----------------------------------------------------------------------------
1553 // constraints and sizers
1554 // ----------------------------------------------------------------------------
1555
1556 #if wxUSE_CONSTRAINTS
1557
1558 void wxWindowBase::SetConstraints( wxLayoutConstraints *constraints )
1559 {
1560 if ( m_constraints )
1561 {
1562 UnsetConstraints(m_constraints);
1563 delete m_constraints;
1564 }
1565 m_constraints = constraints;
1566 if ( m_constraints )
1567 {
1568 // Make sure other windows know they're part of a 'meaningful relationship'
1569 if ( m_constraints->left.GetOtherWindow() && (m_constraints->left.GetOtherWindow() != this) )
1570 m_constraints->left.GetOtherWindow()->AddConstraintReference(this);
1571 if ( m_constraints->top.GetOtherWindow() && (m_constraints->top.GetOtherWindow() != this) )
1572 m_constraints->top.GetOtherWindow()->AddConstraintReference(this);
1573 if ( m_constraints->right.GetOtherWindow() && (m_constraints->right.GetOtherWindow() != this) )
1574 m_constraints->right.GetOtherWindow()->AddConstraintReference(this);
1575 if ( m_constraints->bottom.GetOtherWindow() && (m_constraints->bottom.GetOtherWindow() != this) )
1576 m_constraints->bottom.GetOtherWindow()->AddConstraintReference(this);
1577 if ( m_constraints->width.GetOtherWindow() && (m_constraints->width.GetOtherWindow() != this) )
1578 m_constraints->width.GetOtherWindow()->AddConstraintReference(this);
1579 if ( m_constraints->height.GetOtherWindow() && (m_constraints->height.GetOtherWindow() != this) )
1580 m_constraints->height.GetOtherWindow()->AddConstraintReference(this);
1581 if ( m_constraints->centreX.GetOtherWindow() && (m_constraints->centreX.GetOtherWindow() != this) )
1582 m_constraints->centreX.GetOtherWindow()->AddConstraintReference(this);
1583 if ( m_constraints->centreY.GetOtherWindow() && (m_constraints->centreY.GetOtherWindow() != this) )
1584 m_constraints->centreY.GetOtherWindow()->AddConstraintReference(this);
1585 }
1586 }
1587
1588 // This removes any dangling pointers to this window in other windows'
1589 // constraintsInvolvedIn lists.
1590 void wxWindowBase::UnsetConstraints(wxLayoutConstraints *c)
1591 {
1592 if ( c )
1593 {
1594 if ( c->left.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
1595 c->left.GetOtherWindow()->RemoveConstraintReference(this);
1596 if ( c->top.GetOtherWindow() && (c->top.GetOtherWindow() != this) )
1597 c->top.GetOtherWindow()->RemoveConstraintReference(this);
1598 if ( c->right.GetOtherWindow() && (c->right.GetOtherWindow() != this) )
1599 c->right.GetOtherWindow()->RemoveConstraintReference(this);
1600 if ( c->bottom.GetOtherWindow() && (c->bottom.GetOtherWindow() != this) )
1601 c->bottom.GetOtherWindow()->RemoveConstraintReference(this);
1602 if ( c->width.GetOtherWindow() && (c->width.GetOtherWindow() != this) )
1603 c->width.GetOtherWindow()->RemoveConstraintReference(this);
1604 if ( c->height.GetOtherWindow() && (c->height.GetOtherWindow() != this) )
1605 c->height.GetOtherWindow()->RemoveConstraintReference(this);
1606 if ( c->centreX.GetOtherWindow() && (c->centreX.GetOtherWindow() != this) )
1607 c->centreX.GetOtherWindow()->RemoveConstraintReference(this);
1608 if ( c->centreY.GetOtherWindow() && (c->centreY.GetOtherWindow() != this) )
1609 c->centreY.GetOtherWindow()->RemoveConstraintReference(this);
1610 }
1611 }
1612
1613 // Back-pointer to other windows we're involved with, so if we delete this
1614 // window, we must delete any constraints we're involved with.
1615 void wxWindowBase::AddConstraintReference(wxWindowBase *otherWin)
1616 {
1617 if ( !m_constraintsInvolvedIn )
1618 m_constraintsInvolvedIn = new wxWindowList;
1619 if ( !m_constraintsInvolvedIn->Find((wxWindow *)otherWin) )
1620 m_constraintsInvolvedIn->Append((wxWindow *)otherWin);
1621 }
1622
1623 // REMOVE back-pointer to other windows we're involved with.
1624 void wxWindowBase::RemoveConstraintReference(wxWindowBase *otherWin)
1625 {
1626 if ( m_constraintsInvolvedIn )
1627 m_constraintsInvolvedIn->DeleteObject((wxWindow *)otherWin);
1628 }
1629
1630 // Reset any constraints that mention this window
1631 void wxWindowBase::DeleteRelatedConstraints()
1632 {
1633 if ( m_constraintsInvolvedIn )
1634 {
1635 wxWindowList::compatibility_iterator node = m_constraintsInvolvedIn->GetFirst();
1636 while (node)
1637 {
1638 wxWindow *win = node->GetData();
1639 wxLayoutConstraints *constr = win->GetConstraints();
1640
1641 // Reset any constraints involving this window
1642 if ( constr )
1643 {
1644 constr->left.ResetIfWin(this);
1645 constr->top.ResetIfWin(this);
1646 constr->right.ResetIfWin(this);
1647 constr->bottom.ResetIfWin(this);
1648 constr->width.ResetIfWin(this);
1649 constr->height.ResetIfWin(this);
1650 constr->centreX.ResetIfWin(this);
1651 constr->centreY.ResetIfWin(this);
1652 }
1653
1654 wxWindowList::compatibility_iterator next = node->GetNext();
1655 m_constraintsInvolvedIn->Erase(node);
1656 node = next;
1657 }
1658
1659 delete m_constraintsInvolvedIn;
1660 m_constraintsInvolvedIn = (wxWindowList *) NULL;
1661 }
1662 }
1663
1664 #endif // wxUSE_CONSTRAINTS
1665
1666 void wxWindowBase::SetSizer(wxSizer *sizer, bool deleteOld)
1667 {
1668 if ( sizer == m_windowSizer)
1669 return;
1670
1671 if ( deleteOld )
1672 delete m_windowSizer;
1673
1674 m_windowSizer = sizer;
1675
1676 SetAutoLayout( sizer != NULL );
1677 }
1678
1679 void wxWindowBase::SetSizerAndFit(wxSizer *sizer, bool deleteOld)
1680 {
1681 SetSizer( sizer, deleteOld );
1682 sizer->SetSizeHints( (wxWindow*) this );
1683 }
1684
1685
1686 void wxWindowBase::SetContainingSizer(wxSizer* sizer)
1687 {
1688 // adding a window to a sizer twice is going to result in fatal and
1689 // hard to debug problems later because when deleting the second
1690 // associated wxSizerItem we're going to dereference a dangling
1691 // pointer; so try to detect this as early as possible
1692 wxASSERT_MSG( !sizer || m_containingSizer != sizer,
1693 _T("Adding a window to the same sizer twice?") );
1694
1695 m_containingSizer = sizer;
1696 }
1697
1698 #if wxUSE_CONSTRAINTS
1699
1700 void wxWindowBase::SatisfyConstraints()
1701 {
1702 wxLayoutConstraints *constr = GetConstraints();
1703 bool wasOk = constr && constr->AreSatisfied();
1704
1705 ResetConstraints(); // Mark all constraints as unevaluated
1706
1707 int noChanges = 1;
1708
1709 // if we're a top level panel (i.e. our parent is frame/dialog), our
1710 // own constraints will never be satisfied any more unless we do it
1711 // here
1712 if ( wasOk )
1713 {
1714 while ( noChanges > 0 )
1715 {
1716 LayoutPhase1(&noChanges);
1717 }
1718 }
1719
1720 LayoutPhase2(&noChanges);
1721 }
1722
1723 #endif // wxUSE_CONSTRAINTS
1724
1725 bool wxWindowBase::Layout()
1726 {
1727 // If there is a sizer, use it instead of the constraints
1728 if ( GetSizer() )
1729 {
1730 int w, h;
1731 GetVirtualSize(&w, &h);
1732 GetSizer()->SetDimension( 0, 0, w, h );
1733 }
1734 #if wxUSE_CONSTRAINTS
1735 else
1736 {
1737 SatisfyConstraints(); // Find the right constraints values
1738 SetConstraintSizes(); // Recursively set the real window sizes
1739 }
1740 #endif
1741
1742 return true;
1743 }
1744
1745 #if wxUSE_CONSTRAINTS
1746
1747 // first phase of the constraints evaluation: set our own constraints
1748 bool wxWindowBase::LayoutPhase1(int *noChanges)
1749 {
1750 wxLayoutConstraints *constr = GetConstraints();
1751
1752 return !constr || constr->SatisfyConstraints(this, noChanges);
1753 }
1754
1755 // second phase: set the constraints for our children
1756 bool wxWindowBase::LayoutPhase2(int *noChanges)
1757 {
1758 *noChanges = 0;
1759
1760 // Layout children
1761 DoPhase(1);
1762
1763 // Layout grand children
1764 DoPhase(2);
1765
1766 return true;
1767 }
1768
1769 // Do a phase of evaluating child constraints
1770 bool wxWindowBase::DoPhase(int phase)
1771 {
1772 // the list containing the children for which the constraints are already
1773 // set correctly
1774 wxWindowList succeeded;
1775
1776 // the max number of iterations we loop before concluding that we can't set
1777 // the constraints
1778 static const int maxIterations = 500;
1779
1780 for ( int noIterations = 0; noIterations < maxIterations; noIterations++ )
1781 {
1782 int noChanges = 0;
1783
1784 // loop over all children setting their constraints
1785 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1786 node;
1787 node = node->GetNext() )
1788 {
1789 wxWindow *child = node->GetData();
1790 if ( child->IsTopLevel() )
1791 {
1792 // top level children are not inside our client area
1793 continue;
1794 }
1795
1796 if ( !child->GetConstraints() || succeeded.Find(child) )
1797 {
1798 // this one is either already ok or nothing we can do about it
1799 continue;
1800 }
1801
1802 int tempNoChanges = 0;
1803 bool success = phase == 1 ? child->LayoutPhase1(&tempNoChanges)
1804 : child->LayoutPhase2(&tempNoChanges);
1805 noChanges += tempNoChanges;
1806
1807 if ( success )
1808 {
1809 succeeded.Append(child);
1810 }
1811 }
1812
1813 if ( !noChanges )
1814 {
1815 // constraints are set
1816 break;
1817 }
1818 }
1819
1820 return true;
1821 }
1822
1823 void wxWindowBase::ResetConstraints()
1824 {
1825 wxLayoutConstraints *constr = GetConstraints();
1826 if ( constr )
1827 {
1828 constr->left.SetDone(false);
1829 constr->top.SetDone(false);
1830 constr->right.SetDone(false);
1831 constr->bottom.SetDone(false);
1832 constr->width.SetDone(false);
1833 constr->height.SetDone(false);
1834 constr->centreX.SetDone(false);
1835 constr->centreY.SetDone(false);
1836 }
1837
1838 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1839 while (node)
1840 {
1841 wxWindow *win = node->GetData();
1842 if ( !win->IsTopLevel() )
1843 win->ResetConstraints();
1844 node = node->GetNext();
1845 }
1846 }
1847
1848 // Need to distinguish between setting the 'fake' size for windows and sizers,
1849 // and setting the real values.
1850 void wxWindowBase::SetConstraintSizes(bool recurse)
1851 {
1852 wxLayoutConstraints *constr = GetConstraints();
1853 if ( constr && constr->AreSatisfied() )
1854 {
1855 int x = constr->left.GetValue();
1856 int y = constr->top.GetValue();
1857 int w = constr->width.GetValue();
1858 int h = constr->height.GetValue();
1859
1860 if ( (constr->width.GetRelationship() != wxAsIs ) ||
1861 (constr->height.GetRelationship() != wxAsIs) )
1862 {
1863 SetSize(x, y, w, h);
1864 }
1865 else
1866 {
1867 // If we don't want to resize this window, just move it...
1868 Move(x, y);
1869 }
1870 }
1871 else if ( constr )
1872 {
1873 wxLogDebug(wxT("Constraints not satisfied for %s named '%s'."),
1874 GetClassInfo()->GetClassName(),
1875 GetName().c_str());
1876 }
1877
1878 if ( recurse )
1879 {
1880 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
1881 while (node)
1882 {
1883 wxWindow *win = node->GetData();
1884 if ( !win->IsTopLevel() && win->GetConstraints() )
1885 win->SetConstraintSizes();
1886 node = node->GetNext();
1887 }
1888 }
1889 }
1890
1891 // Only set the size/position of the constraint (if any)
1892 void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h)
1893 {
1894 wxLayoutConstraints *constr = GetConstraints();
1895 if ( constr )
1896 {
1897 if ( x != wxDefaultCoord )
1898 {
1899 constr->left.SetValue(x);
1900 constr->left.SetDone(true);
1901 }
1902 if ( y != wxDefaultCoord )
1903 {
1904 constr->top.SetValue(y);
1905 constr->top.SetDone(true);
1906 }
1907 if ( w != wxDefaultCoord )
1908 {
1909 constr->width.SetValue(w);
1910 constr->width.SetDone(true);
1911 }
1912 if ( h != wxDefaultCoord )
1913 {
1914 constr->height.SetValue(h);
1915 constr->height.SetDone(true);
1916 }
1917 }
1918 }
1919
1920 void wxWindowBase::MoveConstraint(int x, int y)
1921 {
1922 wxLayoutConstraints *constr = GetConstraints();
1923 if ( constr )
1924 {
1925 if ( x != wxDefaultCoord )
1926 {
1927 constr->left.SetValue(x);
1928 constr->left.SetDone(true);
1929 }
1930 if ( y != wxDefaultCoord )
1931 {
1932 constr->top.SetValue(y);
1933 constr->top.SetDone(true);
1934 }
1935 }
1936 }
1937
1938 void wxWindowBase::GetSizeConstraint(int *w, int *h) const
1939 {
1940 wxLayoutConstraints *constr = GetConstraints();
1941 if ( constr )
1942 {
1943 *w = constr->width.GetValue();
1944 *h = constr->height.GetValue();
1945 }
1946 else
1947 GetSize(w, h);
1948 }
1949
1950 void wxWindowBase::GetClientSizeConstraint(int *w, int *h) const
1951 {
1952 wxLayoutConstraints *constr = GetConstraints();
1953 if ( constr )
1954 {
1955 *w = constr->width.GetValue();
1956 *h = constr->height.GetValue();
1957 }
1958 else
1959 GetClientSize(w, h);
1960 }
1961
1962 void wxWindowBase::GetPositionConstraint(int *x, int *y) const
1963 {
1964 wxLayoutConstraints *constr = GetConstraints();
1965 if ( constr )
1966 {
1967 *x = constr->left.GetValue();
1968 *y = constr->top.GetValue();
1969 }
1970 else
1971 GetPosition(x, y);
1972 }
1973
1974 #endif // wxUSE_CONSTRAINTS
1975
1976 void wxWindowBase::AdjustForParentClientOrigin(int& x, int& y, int sizeFlags) const
1977 {
1978 // don't do it for the dialogs/frames - they float independently of their
1979 // parent
1980 if ( !IsTopLevel() )
1981 {
1982 wxWindow *parent = GetParent();
1983 if ( !(sizeFlags & wxSIZE_NO_ADJUSTMENTS) && parent )
1984 {
1985 wxPoint pt(parent->GetClientAreaOrigin());
1986 x += pt.x;
1987 y += pt.y;
1988 }
1989 }
1990 }
1991
1992 // ----------------------------------------------------------------------------
1993 // do Update UI processing for child controls
1994 // ----------------------------------------------------------------------------
1995
1996 void wxWindowBase::UpdateWindowUI(long flags)
1997 {
1998 wxUpdateUIEvent event(GetId());
1999 event.m_eventObject = this;
2000
2001 if ( GetEventHandler()->ProcessEvent(event) )
2002 {
2003 DoUpdateWindowUI(event);
2004 }
2005
2006 if (flags & wxUPDATE_UI_RECURSE)
2007 {
2008 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2009 while (node)
2010 {
2011 wxWindow* child = (wxWindow*) node->GetData();
2012 child->UpdateWindowUI(flags);
2013 node = node->GetNext();
2014 }
2015 }
2016 }
2017
2018 // do the window-specific processing after processing the update event
2019 // TODO: take specific knowledge out of this function and
2020 // put in each control's base class. Unfortunately we don't
2021 // yet have base implementation files for wxCheckBox and wxRadioButton.
2022 void wxWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event)
2023 {
2024 if ( event.GetSetEnabled() )
2025 Enable(event.GetEnabled());
2026
2027 #if wxUSE_CONTROLS
2028 if ( event.GetSetText() )
2029 {
2030 wxControl *control = wxDynamicCastThis(wxControl);
2031 if ( control )
2032 {
2033 if ( event.GetText() != control->GetLabel() )
2034 control->SetLabel(event.GetText());
2035 }
2036 #if wxUSE_CHECKBOX
2037 wxCheckBox *checkbox = wxDynamicCastThis(wxCheckBox);
2038 if ( checkbox )
2039 {
2040 if ( event.GetSetChecked() )
2041 checkbox->SetValue(event.GetChecked());
2042 }
2043 #endif // wxUSE_CHECKBOX
2044
2045 #if wxUSE_RADIOBTN
2046 wxRadioButton *radiobtn = wxDynamicCastThis(wxRadioButton);
2047 if ( radiobtn )
2048 {
2049 if ( event.GetSetChecked() )
2050 radiobtn->SetValue(event.GetChecked());
2051 }
2052 #endif // wxUSE_RADIOBTN
2053 }
2054 #endif
2055 }
2056
2057 #if 0
2058 // call internal idle recursively
2059 // may be obsolete (wait until OnIdle scheme stabilises)
2060 void wxWindowBase::ProcessInternalIdle()
2061 {
2062 OnInternalIdle();
2063
2064 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2065 while (node)
2066 {
2067 wxWindow *child = node->GetData();
2068 child->ProcessInternalIdle();
2069 node = node->GetNext();
2070 }
2071 }
2072 #endif
2073
2074 // ----------------------------------------------------------------------------
2075 // dialog units translations
2076 // ----------------------------------------------------------------------------
2077
2078 wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt)
2079 {
2080 int charWidth = GetCharWidth();
2081 int charHeight = GetCharHeight();
2082 wxPoint pt2(-1, -1);
2083 if (pt.x != -1)
2084 pt2.x = (int) ((pt.x * 4) / charWidth);
2085 if (pt.y != -1)
2086 pt2.y = (int) ((pt.y * 8) / charHeight);
2087
2088 return pt2;
2089 }
2090
2091 wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt)
2092 {
2093 int charWidth = GetCharWidth();
2094 int charHeight = GetCharHeight();
2095 wxPoint pt2(-1, -1);
2096 if (pt.x != -1)
2097 pt2.x = (int) ((pt.x * charWidth) / 4);
2098 if (pt.y != -1)
2099 pt2.y = (int) ((pt.y * charHeight) / 8);
2100
2101 return pt2;
2102 }
2103
2104 // ----------------------------------------------------------------------------
2105 // event handlers
2106 // ----------------------------------------------------------------------------
2107
2108 // propagate the colour change event to the subwindows
2109 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& event)
2110 {
2111 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2112 while ( node )
2113 {
2114 // Only propagate to non-top-level windows
2115 wxWindow *win = node->GetData();
2116 if ( !win->IsTopLevel() )
2117 {
2118 wxSysColourChangedEvent event2;
2119 event.m_eventObject = win;
2120 win->GetEventHandler()->ProcessEvent(event2);
2121 }
2122
2123 node = node->GetNext();
2124 }
2125
2126 Refresh();
2127 }
2128
2129 // the default action is to populate dialog with data when it's created,
2130 // and nudge the UI into displaying itself correctly in case
2131 // we've turned the wxUpdateUIEvents frequency down low.
2132 void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
2133 {
2134 TransferDataToWindow();
2135
2136 // Update the UI at this point
2137 UpdateWindowUI(wxUPDATE_UI_RECURSE);
2138 }
2139
2140 // process Ctrl-Alt-mclick
2141 void wxWindowBase::OnMiddleClick( wxMouseEvent& event )
2142 {
2143 #if wxUSE_MSGDLG
2144 if ( event.ControlDown() && event.AltDown() )
2145 {
2146 // don't translate these strings
2147 wxString port;
2148
2149 #ifdef __WXUNIVERSAL__
2150 port = _T("Univ/");
2151 #endif // __WXUNIVERSAL__
2152
2153 switch ( wxGetOsVersion() )
2154 {
2155 case wxMOTIF_X: port += _T("Motif"); break;
2156 case wxMAC:
2157 case wxMAC_DARWIN: port += _T("Mac"); break;
2158 case wxBEOS: port += _T("BeOS"); break;
2159 case wxGTK:
2160 case wxGTK_WIN32:
2161 case wxGTK_OS2:
2162 case wxGTK_BEOS: port += _T("GTK"); break;
2163 case wxWINDOWS:
2164 case wxPENWINDOWS:
2165 case wxWINDOWS_NT:
2166 case wxWIN32S:
2167 case wxWIN95:
2168 case wxWIN386: port += _T("MS Windows"); break;
2169 case wxMGL_UNIX:
2170 case wxMGL_X:
2171 case wxMGL_WIN32:
2172 case wxMGL_OS2: port += _T("MGL"); break;
2173 case wxWINDOWS_OS2:
2174 case wxOS2_PM: port += _T("OS/2"); break;
2175 default: port += _T("unknown"); break;
2176 }
2177
2178 wxMessageBox(wxString::Format(
2179 _T(
2180 " wxWidgets Library (%s port)\nVersion %u.%u.%u%s, compiled at %s %s\n Copyright (c) 1995-2002 wxWidgets team"
2181 ),
2182 port.c_str(),
2183 wxMAJOR_VERSION,
2184 wxMINOR_VERSION,
2185 wxRELEASE_NUMBER,
2186 #if wxUSE_UNICODE
2187 L" (Unicode)",
2188 #else
2189 "",
2190 #endif
2191 __TDATE__,
2192 __TTIME__
2193 ),
2194 _T("wxWidgets information"),
2195 wxICON_INFORMATION | wxOK,
2196 (wxWindow *)this);
2197 }
2198 else
2199 #endif // wxUSE_MSGDLG
2200 {
2201 event.Skip();
2202 }
2203 }
2204
2205 // ----------------------------------------------------------------------------
2206 // accessibility
2207 // ----------------------------------------------------------------------------
2208
2209 #if wxUSE_ACCESSIBILITY
2210 void wxWindowBase::SetAccessible(wxAccessible* accessible)
2211 {
2212 if (m_accessible && (accessible != m_accessible))
2213 delete m_accessible;
2214 m_accessible = accessible;
2215 if (m_accessible)
2216 m_accessible->SetWindow((wxWindow*) this);
2217 }
2218
2219 // Returns the accessible object, creating if necessary.
2220 wxAccessible* wxWindowBase::GetOrCreateAccessible()
2221 {
2222 if (!m_accessible)
2223 m_accessible = CreateAccessible();
2224 return m_accessible;
2225 }
2226
2227 // Override to create a specific accessible object.
2228 wxAccessible* wxWindowBase::CreateAccessible()
2229 {
2230 return new wxWindowAccessible((wxWindow*) this);
2231 }
2232
2233 #endif
2234
2235 #if !wxUSE_STL
2236 // ----------------------------------------------------------------------------
2237 // list classes implementation
2238 // ----------------------------------------------------------------------------
2239
2240 void wxWindowListNode::DeleteData()
2241 {
2242 delete (wxWindow *)GetData();
2243 }
2244 #endif
2245
2246 // ----------------------------------------------------------------------------
2247 // borders
2248 // ----------------------------------------------------------------------------
2249
2250 wxBorder wxWindowBase::GetBorder(long flags) const
2251 {
2252 wxBorder border = (wxBorder)(flags & wxBORDER_MASK);
2253 if ( border == wxBORDER_DEFAULT )
2254 {
2255 border = GetDefaultBorder();
2256 }
2257
2258 return border;
2259 }
2260
2261 wxBorder wxWindowBase::GetDefaultBorder() const
2262 {
2263 return wxBORDER_NONE;
2264 }
2265
2266 // ----------------------------------------------------------------------------
2267 // hit testing
2268 // ----------------------------------------------------------------------------
2269
2270 wxHitTest wxWindowBase::DoHitTest(wxCoord x, wxCoord y) const
2271 {
2272 // here we just check if the point is inside the window or not
2273
2274 // check the top and left border first
2275 bool outside = x < 0 || y < 0;
2276 if ( !outside )
2277 {
2278 // check the right and bottom borders too
2279 wxSize size = GetSize();
2280 outside = x >= size.x || y >= size.y;
2281 }
2282
2283 return outside ? wxHT_WINDOW_OUTSIDE : wxHT_WINDOW_INSIDE;
2284 }
2285
2286 // ----------------------------------------------------------------------------
2287 // mouse capture
2288 // ----------------------------------------------------------------------------
2289
2290 struct WXDLLEXPORT wxWindowNext
2291 {
2292 wxWindow *win;
2293 wxWindowNext *next;
2294 } *wxWindowBase::ms_winCaptureNext = NULL;
2295
2296 void wxWindowBase::CaptureMouse()
2297 {
2298 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(%p)"), this);
2299
2300 wxWindow *winOld = GetCapture();
2301 if ( winOld )
2302 {
2303 ((wxWindowBase*) winOld)->DoReleaseMouse();
2304
2305 // save it on stack
2306 wxWindowNext *item = new wxWindowNext;
2307 item->win = winOld;
2308 item->next = ms_winCaptureNext;
2309 ms_winCaptureNext = item;
2310 }
2311 //else: no mouse capture to save
2312
2313 DoCaptureMouse();
2314 }
2315
2316 void wxWindowBase::ReleaseMouse()
2317 {
2318 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(%p)"), this);
2319
2320 wxASSERT_MSG( GetCapture() == this, wxT("attempt to release mouse, but this window hasn't captured it") );
2321
2322 DoReleaseMouse();
2323
2324 if ( ms_winCaptureNext )
2325 {
2326 ((wxWindowBase*)ms_winCaptureNext->win)->DoCaptureMouse();
2327
2328 wxWindowNext *item = ms_winCaptureNext;
2329 ms_winCaptureNext = item->next;
2330 delete item;
2331 }
2332 //else: stack is empty, no previous capture
2333
2334 wxLogTrace(_T("mousecapture"),
2335 (const wxChar *) _T("After ReleaseMouse() mouse is captured by %p"),
2336 GetCapture());
2337 }
2338
2339 #if wxUSE_HOTKEY
2340
2341 bool
2342 wxWindowBase::RegisterHotKey(int WXUNUSED(hotkeyId),
2343 int WXUNUSED(modifiers),
2344 int WXUNUSED(keycode))
2345 {
2346 // not implemented
2347 return false;
2348 }
2349
2350 bool wxWindowBase::UnregisterHotKey(int WXUNUSED(hotkeyId))
2351 {
2352 // not implemented
2353 return false;
2354 }
2355
2356 #endif // wxUSE_HOTKEY
2357
2358 void wxWindowBase::SendDestroyEvent()
2359 {
2360 wxWindowDestroyEvent event;
2361 event.SetEventObject(this);
2362 event.SetId(GetId());
2363 GetEventHandler()->ProcessEvent(event);
2364 }
2365
2366 // ----------------------------------------------------------------------------
2367 // event processing
2368 // ----------------------------------------------------------------------------
2369
2370 bool wxWindowBase::TryValidator(wxEvent& wxVALIDATOR_PARAM(event))
2371 {
2372 #if wxUSE_VALIDATORS
2373 // Can only use the validator of the window which
2374 // is receiving the event
2375 if ( event.GetEventObject() == this )
2376 {
2377 wxValidator *validator = GetValidator();
2378 if ( validator && validator->ProcessEvent(event) )
2379 {
2380 return true;
2381 }
2382 }
2383 #endif // wxUSE_VALIDATORS
2384
2385 return false;
2386 }
2387
2388 bool wxWindowBase::TryParent(wxEvent& event)
2389 {
2390 // carry on up the parent-child hierarchy if the propgation count hasn't
2391 // reached zero yet
2392 if ( event.ShouldPropagate() )
2393 {
2394 // honour the requests to stop propagation at this window: this is
2395 // used by the dialogs, for example, to prevent processing the events
2396 // from the dialog controls in the parent frame which rarely, if ever,
2397 // makes sense
2398 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
2399 {
2400 wxWindow *parent = GetParent();
2401 if ( parent && !parent->IsBeingDeleted() )
2402 {
2403 wxPropagateOnce propagateOnce(event);
2404
2405 return parent->GetEventHandler()->ProcessEvent(event);
2406 }
2407 }
2408 }
2409
2410 return wxEvtHandler::TryParent(event);
2411 }
2412
2413 // ----------------------------------------------------------------------------
2414 // keyboard navigation
2415 // ----------------------------------------------------------------------------
2416
2417 // Navigates in the specified direction.
2418 bool wxWindowBase::Navigate(int flags)
2419 {
2420 wxNavigationKeyEvent eventNav;
2421 eventNav.SetFlags(flags);
2422 eventNav.SetEventObject(this);
2423 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
2424 {
2425 return true;
2426 }
2427 return false;
2428 }
2429
2430 void wxWindowBase::DoMoveInTabOrder(wxWindow *win, MoveKind move)
2431 {
2432 // check that we're not a top level window
2433 wxCHECK_RET( GetParent(),
2434 _T("MoveBefore/AfterInTabOrder() don't work for TLWs!") );
2435
2436 // find the target window in the siblings list
2437 wxWindowList& siblings = GetParent()->GetChildren();
2438 wxWindowList::compatibility_iterator i = siblings.Find(win);
2439 wxCHECK_RET( i, _T("MoveBefore/AfterInTabOrder(): win is not a sibling") );
2440
2441 // unfortunately, when wxUSE_STL == 1 DetachNode() is not implemented so we
2442 // can't just move the node around
2443 wxWindow *self = (wxWindow *)this;
2444 siblings.DeleteObject(self);
2445 if ( move == MoveAfter )
2446 {
2447 i = i->GetNext();
2448 }
2449
2450 if ( i )
2451 {
2452 siblings.Insert(i, self);
2453 }
2454 else // MoveAfter and win was the last sibling
2455 {
2456 siblings.Append(self);
2457 }
2458 }
2459
2460 // ----------------------------------------------------------------------------
2461 // global functions
2462 // ----------------------------------------------------------------------------
2463
2464 wxWindow* wxGetTopLevelParent(wxWindow *win)
2465 {
2466 while ( win && !win->IsTopLevel() )
2467 win = win->GetParent();
2468
2469 return win;
2470 }
2471
2472 #if wxUSE_ACCESSIBILITY
2473 // ----------------------------------------------------------------------------
2474 // accessible object for windows
2475 // ----------------------------------------------------------------------------
2476
2477 // Can return either a child object, or an integer
2478 // representing the child element, starting from 1.
2479 wxAccStatus wxWindowAccessible::HitTest(const wxPoint& WXUNUSED(pt), int* WXUNUSED(childId), wxAccessible** WXUNUSED(childObject))
2480 {
2481 wxASSERT( GetWindow() != NULL );
2482 if (!GetWindow())
2483 return wxACC_FAIL;
2484
2485 return wxACC_NOT_IMPLEMENTED;
2486 }
2487
2488 // Returns the rectangle for this object (id = 0) or a child element (id > 0).
2489 wxAccStatus wxWindowAccessible::GetLocation(wxRect& rect, int elementId)
2490 {
2491 wxASSERT( GetWindow() != NULL );
2492 if (!GetWindow())
2493 return wxACC_FAIL;
2494
2495 wxWindow* win = NULL;
2496 if (elementId == 0)
2497 {
2498 win = GetWindow();
2499 }
2500 else
2501 {
2502 if (elementId <= (int) GetWindow()->GetChildren().GetCount())
2503 {
2504 win = GetWindow()->GetChildren().Item(elementId-1)->GetData();
2505 }
2506 else
2507 return wxACC_FAIL;
2508 }
2509 if (win)
2510 {
2511 rect = win->GetRect();
2512 if (win->GetParent() && !win->IsKindOf(CLASSINFO(wxTopLevelWindow)))
2513 rect.SetPosition(win->GetParent()->ClientToScreen(rect.GetPosition()));
2514 return wxACC_OK;
2515 }
2516
2517 return wxACC_NOT_IMPLEMENTED;
2518 }
2519
2520 // Navigates from fromId to toId/toObject.
2521 wxAccStatus wxWindowAccessible::Navigate(wxNavDir navDir, int fromId,
2522 int* WXUNUSED(toId), wxAccessible** toObject)
2523 {
2524 wxASSERT( GetWindow() != NULL );
2525 if (!GetWindow())
2526 return wxACC_FAIL;
2527
2528 switch (navDir)
2529 {
2530 case wxNAVDIR_FIRSTCHILD:
2531 {
2532 if (GetWindow()->GetChildren().GetCount() == 0)
2533 return wxACC_FALSE;
2534 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetFirst()->GetData();
2535 *toObject = childWindow->GetOrCreateAccessible();
2536
2537 return wxACC_OK;
2538 }
2539 case wxNAVDIR_LASTCHILD:
2540 {
2541 if (GetWindow()->GetChildren().GetCount() == 0)
2542 return wxACC_FALSE;
2543 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetLast()->GetData();
2544 *toObject = childWindow->GetOrCreateAccessible();
2545
2546 return wxACC_OK;
2547 }
2548 case wxNAVDIR_RIGHT:
2549 case wxNAVDIR_DOWN:
2550 case wxNAVDIR_NEXT:
2551 {
2552 wxWindowList::compatibility_iterator node =
2553 wxWindowList::compatibility_iterator();
2554 if (fromId == 0)
2555 {
2556 // Can't navigate to sibling of this window
2557 // if we're a top-level window.
2558 if (!GetWindow()->GetParent())
2559 return wxACC_NOT_IMPLEMENTED;
2560
2561 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
2562 }
2563 else
2564 {
2565 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
2566 node = GetWindow()->GetChildren().Item(fromId-1);
2567 }
2568
2569 if (node && node->GetNext())
2570 {
2571 wxWindow* nextWindow = node->GetNext()->GetData();
2572 *toObject = nextWindow->GetOrCreateAccessible();
2573 return wxACC_OK;
2574 }
2575 else
2576 return wxACC_FALSE;
2577 }
2578 case wxNAVDIR_LEFT:
2579 case wxNAVDIR_UP:
2580 case wxNAVDIR_PREVIOUS:
2581 {
2582 wxWindowList::compatibility_iterator node =
2583 wxWindowList::compatibility_iterator();
2584 if (fromId == 0)
2585 {
2586 // Can't navigate to sibling of this window
2587 // if we're a top-level window.
2588 if (!GetWindow()->GetParent())
2589 return wxACC_NOT_IMPLEMENTED;
2590
2591 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
2592 }
2593 else
2594 {
2595 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
2596 node = GetWindow()->GetChildren().Item(fromId-1);
2597 }
2598
2599 if (node && node->GetPrevious())
2600 {
2601 wxWindow* previousWindow = node->GetPrevious()->GetData();
2602 *toObject = previousWindow->GetOrCreateAccessible();
2603 return wxACC_OK;
2604 }
2605 else
2606 return wxACC_FALSE;
2607 }
2608 }
2609
2610 return wxACC_NOT_IMPLEMENTED;
2611 }
2612
2613 // Gets the name of the specified object.
2614 wxAccStatus wxWindowAccessible::GetName(int childId, wxString* name)
2615 {
2616 wxASSERT( GetWindow() != NULL );
2617 if (!GetWindow())
2618 return wxACC_FAIL;
2619
2620 wxString title;
2621
2622 // If a child, leave wxWidgets to call the function on the actual
2623 // child object.
2624 if (childId > 0)
2625 return wxACC_NOT_IMPLEMENTED;
2626
2627 // This will eventually be replaced by specialised
2628 // accessible classes, one for each kind of wxWidgets
2629 // control or window.
2630 #if wxUSE_BUTTON
2631 if (GetWindow()->IsKindOf(CLASSINFO(wxButton)))
2632 title = ((wxButton*) GetWindow())->GetLabel();
2633 else
2634 #endif
2635 title = GetWindow()->GetName();
2636
2637 if (!title.IsEmpty())
2638 {
2639 *name = title;
2640 return wxACC_OK;
2641 }
2642 else
2643 return wxACC_NOT_IMPLEMENTED;
2644 }
2645
2646 // Gets the number of children.
2647 wxAccStatus wxWindowAccessible::GetChildCount(int* childId)
2648 {
2649 wxASSERT( GetWindow() != NULL );
2650 if (!GetWindow())
2651 return wxACC_FAIL;
2652
2653 *childId = (int) GetWindow()->GetChildren().GetCount();
2654 return wxACC_OK;
2655 }
2656
2657 // Gets the specified child (starting from 1).
2658 // If *child is NULL and return value is wxACC_OK,
2659 // this means that the child is a simple element and
2660 // not an accessible object.
2661 wxAccStatus wxWindowAccessible::GetChild(int childId, wxAccessible** child)
2662 {
2663 wxASSERT( GetWindow() != NULL );
2664 if (!GetWindow())
2665 return wxACC_FAIL;
2666
2667 if (childId == 0)
2668 {
2669 *child = this;
2670 return wxACC_OK;
2671 }
2672
2673 if (childId > (int) GetWindow()->GetChildren().GetCount())
2674 return wxACC_FAIL;
2675
2676 wxWindow* childWindow = GetWindow()->GetChildren().Item(childId-1)->GetData();
2677 *child = childWindow->GetOrCreateAccessible();
2678 if (*child)
2679 return wxACC_OK;
2680 else
2681 return wxACC_FAIL;
2682 }
2683
2684 // Gets the parent, or NULL.
2685 wxAccStatus wxWindowAccessible::GetParent(wxAccessible** parent)
2686 {
2687 wxASSERT( GetWindow() != NULL );
2688 if (!GetWindow())
2689 return wxACC_FAIL;
2690
2691 wxWindow* parentWindow = GetWindow()->GetParent();
2692 if (!parentWindow)
2693 {
2694 *parent = NULL;
2695 return wxACC_OK;
2696 }
2697 else
2698 {
2699 *parent = parentWindow->GetOrCreateAccessible();
2700 if (*parent)
2701 return wxACC_OK;
2702 else
2703 return wxACC_FAIL;
2704 }
2705 }
2706
2707 // Performs the default action. childId is 0 (the action for this object)
2708 // or > 0 (the action for a child).
2709 // Return wxACC_NOT_SUPPORTED if there is no default action for this
2710 // window (e.g. an edit control).
2711 wxAccStatus wxWindowAccessible::DoDefaultAction(int WXUNUSED(childId))
2712 {
2713 wxASSERT( GetWindow() != NULL );
2714 if (!GetWindow())
2715 return wxACC_FAIL;
2716
2717 return wxACC_NOT_IMPLEMENTED;
2718 }
2719
2720 // Gets the default action for this object (0) or > 0 (the action for a child).
2721 // Return wxACC_OK even if there is no action. actionName is the action, or the empty
2722 // string if there is no action.
2723 // The retrieved string describes the action that is performed on an object,
2724 // not what the object does as a result. For example, a toolbar button that prints
2725 // a document has a default action of "Press" rather than "Prints the current document."
2726 wxAccStatus wxWindowAccessible::GetDefaultAction(int WXUNUSED(childId), wxString* WXUNUSED(actionName))
2727 {
2728 wxASSERT( GetWindow() != NULL );
2729 if (!GetWindow())
2730 return wxACC_FAIL;
2731
2732 return wxACC_NOT_IMPLEMENTED;
2733 }
2734
2735 // Returns the description for this object or a child.
2736 wxAccStatus wxWindowAccessible::GetDescription(int WXUNUSED(childId), wxString* description)
2737 {
2738 wxASSERT( GetWindow() != NULL );
2739 if (!GetWindow())
2740 return wxACC_FAIL;
2741
2742 wxString ht(GetWindow()->GetHelpText());
2743 if (!ht.IsEmpty())
2744 {
2745 *description = ht;
2746 return wxACC_OK;
2747 }
2748 return wxACC_NOT_IMPLEMENTED;
2749 }
2750
2751 // Returns help text for this object or a child, similar to tooltip text.
2752 wxAccStatus wxWindowAccessible::GetHelpText(int WXUNUSED(childId), wxString* helpText)
2753 {
2754 wxASSERT( GetWindow() != NULL );
2755 if (!GetWindow())
2756 return wxACC_FAIL;
2757
2758 wxString ht(GetWindow()->GetHelpText());
2759 if (!ht.IsEmpty())
2760 {
2761 *helpText = ht;
2762 return wxACC_OK;
2763 }
2764 return wxACC_NOT_IMPLEMENTED;
2765 }
2766
2767 // Returns the keyboard shortcut for this object or child.
2768 // Return e.g. ALT+K
2769 wxAccStatus wxWindowAccessible::GetKeyboardShortcut(int WXUNUSED(childId), wxString* WXUNUSED(shortcut))
2770 {
2771 wxASSERT( GetWindow() != NULL );
2772 if (!GetWindow())
2773 return wxACC_FAIL;
2774
2775 return wxACC_NOT_IMPLEMENTED;
2776 }
2777
2778 // Returns a role constant.
2779 wxAccStatus wxWindowAccessible::GetRole(int childId, wxAccRole* role)
2780 {
2781 wxASSERT( GetWindow() != NULL );
2782 if (!GetWindow())
2783 return wxACC_FAIL;
2784
2785 // If a child, leave wxWidgets to call the function on the actual
2786 // child object.
2787 if (childId > 0)
2788 return wxACC_NOT_IMPLEMENTED;
2789
2790 if (GetWindow()->IsKindOf(CLASSINFO(wxControl)))
2791 return wxACC_NOT_IMPLEMENTED;
2792 #if wxUSE_STATUSBAR
2793 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar)))
2794 return wxACC_NOT_IMPLEMENTED;
2795 #endif
2796 #if wxUSE_TOOLBAR
2797 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar)))
2798 return wxACC_NOT_IMPLEMENTED;
2799 #endif
2800
2801 //*role = wxROLE_SYSTEM_CLIENT;
2802 *role = wxROLE_SYSTEM_CLIENT;
2803 return wxACC_OK;
2804
2805 #if 0
2806 return wxACC_NOT_IMPLEMENTED;
2807 #endif
2808 }
2809
2810 // Returns a state constant.
2811 wxAccStatus wxWindowAccessible::GetState(int childId, long* state)
2812 {
2813 wxASSERT( GetWindow() != NULL );
2814 if (!GetWindow())
2815 return wxACC_FAIL;
2816
2817 // If a child, leave wxWidgets to call the function on the actual
2818 // child object.
2819 if (childId > 0)
2820 return wxACC_NOT_IMPLEMENTED;
2821
2822 if (GetWindow()->IsKindOf(CLASSINFO(wxControl)))
2823 return wxACC_NOT_IMPLEMENTED;
2824
2825 #if wxUSE_STATUSBAR
2826 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar)))
2827 return wxACC_NOT_IMPLEMENTED;
2828 #endif
2829 #if wxUSE_TOOLBAR
2830 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar)))
2831 return wxACC_NOT_IMPLEMENTED;
2832 #endif
2833
2834 *state = 0;
2835 return wxACC_OK;
2836
2837 #if 0
2838 return wxACC_NOT_IMPLEMENTED;
2839 #endif
2840 }
2841
2842 // Returns a localized string representing the value for the object
2843 // or child.
2844 wxAccStatus wxWindowAccessible::GetValue(int WXUNUSED(childId), wxString* WXUNUSED(strValue))
2845 {
2846 wxASSERT( GetWindow() != NULL );
2847 if (!GetWindow())
2848 return wxACC_FAIL;
2849
2850 return wxACC_NOT_IMPLEMENTED;
2851 }
2852
2853 // Selects the object or child.
2854 wxAccStatus wxWindowAccessible::Select(int WXUNUSED(childId), wxAccSelectionFlags WXUNUSED(selectFlags))
2855 {
2856 wxASSERT( GetWindow() != NULL );
2857 if (!GetWindow())
2858 return wxACC_FAIL;
2859
2860 return wxACC_NOT_IMPLEMENTED;
2861 }
2862
2863 // Gets the window with the keyboard focus.
2864 // If childId is 0 and child is NULL, no object in
2865 // this subhierarchy has the focus.
2866 // If this object has the focus, child should be 'this'.
2867 wxAccStatus wxWindowAccessible::GetFocus(int* WXUNUSED(childId), wxAccessible** WXUNUSED(child))
2868 {
2869 wxASSERT( GetWindow() != NULL );
2870 if (!GetWindow())
2871 return wxACC_FAIL;
2872
2873 return wxACC_NOT_IMPLEMENTED;
2874 }
2875
2876 // Gets a variant representing the selected children
2877 // of this object.
2878 // Acceptable values:
2879 // - a null variant (IsNull() returns TRUE)
2880 // - a list variant (GetType() == wxT("list")
2881 // - an integer representing the selected child element,
2882 // or 0 if this object is selected (GetType() == wxT("long")
2883 // - a "void*" pointer to a wxAccessible child object
2884 wxAccStatus wxWindowAccessible::GetSelections(wxVariant* WXUNUSED(selections))
2885 {
2886 wxASSERT( GetWindow() != NULL );
2887 if (!GetWindow())
2888 return wxACC_FAIL;
2889
2890 return wxACC_NOT_IMPLEMENTED;
2891 }
2892
2893 #endif // wxUSE_ACCESSIBILITY