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