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