]> git.saurik.com Git - wxWidgets.git/blob - src/common/wincmn.cpp
fixed broken DoUpdateWindowUI: event.SetChecked doesn't have to be used together...
[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 wxDefaultCoord)
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::DoSetSizeHints(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 }
2047 #endif // wxUSE_CONTROLS
2048
2049 if ( event.GetSetChecked() )
2050 {
2051 #if wxUSE_CHECKBOX
2052 wxCheckBox *checkbox = wxDynamicCastThis(wxCheckBox);
2053 if ( checkbox )
2054 {
2055 checkbox->SetValue(event.GetChecked());
2056 }
2057 #endif // wxUSE_CHECKBOX
2058
2059 #if wxUSE_RADIOBTN
2060 wxRadioButton *radiobtn = wxDynamicCastThis(wxRadioButton);
2061 if ( radiobtn )
2062 {
2063 radiobtn->SetValue(event.GetChecked());
2064 }
2065 #endif // wxUSE_RADIOBTN
2066 }
2067 }
2068
2069 #if 0
2070 // call internal idle recursively
2071 // may be obsolete (wait until OnIdle scheme stabilises)
2072 void wxWindowBase::ProcessInternalIdle()
2073 {
2074 OnInternalIdle();
2075
2076 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2077 while (node)
2078 {
2079 wxWindow *child = node->GetData();
2080 child->ProcessInternalIdle();
2081 node = node->GetNext();
2082 }
2083 }
2084 #endif
2085
2086 // ----------------------------------------------------------------------------
2087 // dialog units translations
2088 // ----------------------------------------------------------------------------
2089
2090 wxPoint wxWindowBase::ConvertPixelsToDialog(const wxPoint& pt)
2091 {
2092 int charWidth = GetCharWidth();
2093 int charHeight = GetCharHeight();
2094 wxPoint pt2 = wxDefaultPosition;
2095 if (pt.x != wxDefaultCoord)
2096 pt2.x = (int) ((pt.x * 4) / charWidth);
2097 if (pt.y != wxDefaultCoord)
2098 pt2.y = (int) ((pt.y * 8) / charHeight);
2099
2100 return pt2;
2101 }
2102
2103 wxPoint wxWindowBase::ConvertDialogToPixels(const wxPoint& pt)
2104 {
2105 int charWidth = GetCharWidth();
2106 int charHeight = GetCharHeight();
2107 wxPoint pt2 = wxDefaultPosition;
2108 if (pt.x != wxDefaultCoord)
2109 pt2.x = (int) ((pt.x * charWidth) / 4);
2110 if (pt.y != wxDefaultCoord)
2111 pt2.y = (int) ((pt.y * charHeight) / 8);
2112
2113 return pt2;
2114 }
2115
2116 // ----------------------------------------------------------------------------
2117 // event handlers
2118 // ----------------------------------------------------------------------------
2119
2120 // propagate the colour change event to the subwindows
2121 void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& event)
2122 {
2123 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2124 while ( node )
2125 {
2126 // Only propagate to non-top-level windows
2127 wxWindow *win = node->GetData();
2128 if ( !win->IsTopLevel() )
2129 {
2130 wxSysColourChangedEvent event2;
2131 event.m_eventObject = win;
2132 win->GetEventHandler()->ProcessEvent(event2);
2133 }
2134
2135 node = node->GetNext();
2136 }
2137
2138 Refresh();
2139 }
2140
2141 // the default action is to populate dialog with data when it's created,
2142 // and nudge the UI into displaying itself correctly in case
2143 // we've turned the wxUpdateUIEvents frequency down low.
2144 void wxWindowBase::OnInitDialog( wxInitDialogEvent &WXUNUSED(event) )
2145 {
2146 TransferDataToWindow();
2147
2148 // Update the UI at this point
2149 UpdateWindowUI(wxUPDATE_UI_RECURSE);
2150 }
2151
2152 // process Ctrl-Alt-mclick
2153 void wxWindowBase::OnMiddleClick( wxMouseEvent& event )
2154 {
2155 #if wxUSE_MSGDLG
2156 if ( event.ControlDown() && event.AltDown() )
2157 {
2158 // don't translate these strings
2159 wxString port;
2160
2161 #ifdef __WXUNIVERSAL__
2162 port = _T("Univ/");
2163 #endif // __WXUNIVERSAL__
2164
2165 switch ( wxGetOsVersion() )
2166 {
2167 case wxMOTIF_X: port += _T("Motif"); break;
2168 case wxMAC:
2169 case wxMAC_DARWIN: port += _T("Mac"); break;
2170 case wxBEOS: port += _T("BeOS"); break;
2171 case wxGTK:
2172 case wxGTK_WIN32:
2173 case wxGTK_OS2:
2174 case wxGTK_BEOS: port += _T("GTK"); break;
2175 case wxWINDOWS:
2176 case wxPENWINDOWS:
2177 case wxWINDOWS_NT:
2178 case wxWIN32S:
2179 case wxWIN95:
2180 case wxWIN386: port += _T("MS Windows"); break;
2181 case wxMGL_UNIX:
2182 case wxMGL_X:
2183 case wxMGL_WIN32:
2184 case wxMGL_OS2: port += _T("MGL"); break;
2185 case wxWINDOWS_OS2:
2186 case wxOS2_PM: port += _T("OS/2"); break;
2187 default: port += _T("unknown"); break;
2188 }
2189
2190 wxMessageBox(wxString::Format(
2191 _T(
2192 " wxWidgets Library (%s port)\nVersion %u.%u.%u%s, compiled at %s %s\n Copyright (c) 1995-2002 wxWidgets team"
2193 ),
2194 port.c_str(),
2195 wxMAJOR_VERSION,
2196 wxMINOR_VERSION,
2197 wxRELEASE_NUMBER,
2198 #if wxUSE_UNICODE
2199 L" (Unicode)",
2200 #else
2201 "",
2202 #endif
2203 __TDATE__,
2204 __TTIME__
2205 ),
2206 _T("wxWidgets information"),
2207 wxICON_INFORMATION | wxOK,
2208 (wxWindow *)this);
2209 }
2210 else
2211 #endif // wxUSE_MSGDLG
2212 {
2213 event.Skip();
2214 }
2215 }
2216
2217 // ----------------------------------------------------------------------------
2218 // accessibility
2219 // ----------------------------------------------------------------------------
2220
2221 #if wxUSE_ACCESSIBILITY
2222 void wxWindowBase::SetAccessible(wxAccessible* accessible)
2223 {
2224 if (m_accessible && (accessible != m_accessible))
2225 delete m_accessible;
2226 m_accessible = accessible;
2227 if (m_accessible)
2228 m_accessible->SetWindow((wxWindow*) this);
2229 }
2230
2231 // Returns the accessible object, creating if necessary.
2232 wxAccessible* wxWindowBase::GetOrCreateAccessible()
2233 {
2234 if (!m_accessible)
2235 m_accessible = CreateAccessible();
2236 return m_accessible;
2237 }
2238
2239 // Override to create a specific accessible object.
2240 wxAccessible* wxWindowBase::CreateAccessible()
2241 {
2242 return new wxWindowAccessible((wxWindow*) this);
2243 }
2244
2245 #endif
2246
2247 #if !wxUSE_STL
2248 // ----------------------------------------------------------------------------
2249 // list classes implementation
2250 // ----------------------------------------------------------------------------
2251
2252 void wxWindowListNode::DeleteData()
2253 {
2254 delete (wxWindow *)GetData();
2255 }
2256 #endif
2257
2258 // ----------------------------------------------------------------------------
2259 // borders
2260 // ----------------------------------------------------------------------------
2261
2262 wxBorder wxWindowBase::GetBorder(long flags) const
2263 {
2264 wxBorder border = (wxBorder)(flags & wxBORDER_MASK);
2265 if ( border == wxBORDER_DEFAULT )
2266 {
2267 border = GetDefaultBorder();
2268 }
2269
2270 return border;
2271 }
2272
2273 wxBorder wxWindowBase::GetDefaultBorder() const
2274 {
2275 return wxBORDER_NONE;
2276 }
2277
2278 // ----------------------------------------------------------------------------
2279 // hit testing
2280 // ----------------------------------------------------------------------------
2281
2282 wxHitTest wxWindowBase::DoHitTest(wxCoord x, wxCoord y) const
2283 {
2284 // here we just check if the point is inside the window or not
2285
2286 // check the top and left border first
2287 bool outside = x < 0 || y < 0;
2288 if ( !outside )
2289 {
2290 // check the right and bottom borders too
2291 wxSize size = GetSize();
2292 outside = x >= size.x || y >= size.y;
2293 }
2294
2295 return outside ? wxHT_WINDOW_OUTSIDE : wxHT_WINDOW_INSIDE;
2296 }
2297
2298 // ----------------------------------------------------------------------------
2299 // mouse capture
2300 // ----------------------------------------------------------------------------
2301
2302 struct WXDLLEXPORT wxWindowNext
2303 {
2304 wxWindow *win;
2305 wxWindowNext *next;
2306 } *wxWindowBase::ms_winCaptureNext = NULL;
2307
2308 void wxWindowBase::CaptureMouse()
2309 {
2310 wxLogTrace(_T("mousecapture"), _T("CaptureMouse(%p)"), this);
2311
2312 wxWindow *winOld = GetCapture();
2313 if ( winOld )
2314 {
2315 ((wxWindowBase*) winOld)->DoReleaseMouse();
2316
2317 // save it on stack
2318 wxWindowNext *item = new wxWindowNext;
2319 item->win = winOld;
2320 item->next = ms_winCaptureNext;
2321 ms_winCaptureNext = item;
2322 }
2323 //else: no mouse capture to save
2324
2325 DoCaptureMouse();
2326 }
2327
2328 void wxWindowBase::ReleaseMouse()
2329 {
2330 wxLogTrace(_T("mousecapture"), _T("ReleaseMouse(%p)"), this);
2331
2332 wxASSERT_MSG( GetCapture() == this, wxT("attempt to release mouse, but this window hasn't captured it") );
2333
2334 DoReleaseMouse();
2335
2336 if ( ms_winCaptureNext )
2337 {
2338 ((wxWindowBase*)ms_winCaptureNext->win)->DoCaptureMouse();
2339
2340 wxWindowNext *item = ms_winCaptureNext;
2341 ms_winCaptureNext = item->next;
2342 delete item;
2343 }
2344 //else: stack is empty, no previous capture
2345
2346 wxLogTrace(_T("mousecapture"),
2347 (const wxChar *) _T("After ReleaseMouse() mouse is captured by %p"),
2348 GetCapture());
2349 }
2350
2351 #if wxUSE_HOTKEY
2352
2353 bool
2354 wxWindowBase::RegisterHotKey(int WXUNUSED(hotkeyId),
2355 int WXUNUSED(modifiers),
2356 int WXUNUSED(keycode))
2357 {
2358 // not implemented
2359 return false;
2360 }
2361
2362 bool wxWindowBase::UnregisterHotKey(int WXUNUSED(hotkeyId))
2363 {
2364 // not implemented
2365 return false;
2366 }
2367
2368 #endif // wxUSE_HOTKEY
2369
2370 void wxWindowBase::SendDestroyEvent()
2371 {
2372 wxWindowDestroyEvent event;
2373 event.SetEventObject(this);
2374 event.SetId(GetId());
2375 GetEventHandler()->ProcessEvent(event);
2376 }
2377
2378 // ----------------------------------------------------------------------------
2379 // event processing
2380 // ----------------------------------------------------------------------------
2381
2382 bool wxWindowBase::TryValidator(wxEvent& wxVALIDATOR_PARAM(event))
2383 {
2384 #if wxUSE_VALIDATORS
2385 // Can only use the validator of the window which
2386 // is receiving the event
2387 if ( event.GetEventObject() == this )
2388 {
2389 wxValidator *validator = GetValidator();
2390 if ( validator && validator->ProcessEvent(event) )
2391 {
2392 return true;
2393 }
2394 }
2395 #endif // wxUSE_VALIDATORS
2396
2397 return false;
2398 }
2399
2400 bool wxWindowBase::TryParent(wxEvent& event)
2401 {
2402 // carry on up the parent-child hierarchy if the propgation count hasn't
2403 // reached zero yet
2404 if ( event.ShouldPropagate() )
2405 {
2406 // honour the requests to stop propagation at this window: this is
2407 // used by the dialogs, for example, to prevent processing the events
2408 // from the dialog controls in the parent frame which rarely, if ever,
2409 // makes sense
2410 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
2411 {
2412 wxWindow *parent = GetParent();
2413 if ( parent && !parent->IsBeingDeleted() )
2414 {
2415 wxPropagateOnce propagateOnce(event);
2416
2417 return parent->GetEventHandler()->ProcessEvent(event);
2418 }
2419 }
2420 }
2421
2422 return wxEvtHandler::TryParent(event);
2423 }
2424
2425 // ----------------------------------------------------------------------------
2426 // keyboard navigation
2427 // ----------------------------------------------------------------------------
2428
2429 // Navigates in the specified direction.
2430 bool wxWindowBase::Navigate(int flags)
2431 {
2432 wxNavigationKeyEvent eventNav;
2433 eventNav.SetFlags(flags);
2434 eventNav.SetEventObject(this);
2435 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
2436 {
2437 return true;
2438 }
2439 return false;
2440 }
2441
2442 void wxWindowBase::DoMoveInTabOrder(wxWindow *win, MoveKind move)
2443 {
2444 // check that we're not a top level window
2445 wxCHECK_RET( GetParent(),
2446 _T("MoveBefore/AfterInTabOrder() don't work for TLWs!") );
2447
2448 // find the target window in the siblings list
2449 wxWindowList& siblings = GetParent()->GetChildren();
2450 wxWindowList::compatibility_iterator i = siblings.Find(win);
2451 wxCHECK_RET( i, _T("MoveBefore/AfterInTabOrder(): win is not a sibling") );
2452
2453 // unfortunately, when wxUSE_STL == 1 DetachNode() is not implemented so we
2454 // can't just move the node around
2455 wxWindow *self = (wxWindow *)this;
2456 siblings.DeleteObject(self);
2457 if ( move == MoveAfter )
2458 {
2459 i = i->GetNext();
2460 }
2461
2462 if ( i )
2463 {
2464 siblings.Insert(i, self);
2465 }
2466 else // MoveAfter and win was the last sibling
2467 {
2468 siblings.Append(self);
2469 }
2470 }
2471
2472 // ----------------------------------------------------------------------------
2473 // focus handling
2474 // ----------------------------------------------------------------------------
2475
2476 /*static*/ wxWindow* wxWindowBase::FindFocus()
2477 {
2478 wxWindowBase *win = DoFindFocus();
2479 return win ? win->GetMainWindowOfCompositeControl() : NULL;
2480 }
2481
2482 // ----------------------------------------------------------------------------
2483 // global functions
2484 // ----------------------------------------------------------------------------
2485
2486 wxWindow* wxGetTopLevelParent(wxWindow *win)
2487 {
2488 while ( win && !win->IsTopLevel() )
2489 win = win->GetParent();
2490
2491 return win;
2492 }
2493
2494 #if wxUSE_ACCESSIBILITY
2495 // ----------------------------------------------------------------------------
2496 // accessible object for windows
2497 // ----------------------------------------------------------------------------
2498
2499 // Can return either a child object, or an integer
2500 // representing the child element, starting from 1.
2501 wxAccStatus wxWindowAccessible::HitTest(const wxPoint& WXUNUSED(pt), int* WXUNUSED(childId), wxAccessible** WXUNUSED(childObject))
2502 {
2503 wxASSERT( GetWindow() != NULL );
2504 if (!GetWindow())
2505 return wxACC_FAIL;
2506
2507 return wxACC_NOT_IMPLEMENTED;
2508 }
2509
2510 // Returns the rectangle for this object (id = 0) or a child element (id > 0).
2511 wxAccStatus wxWindowAccessible::GetLocation(wxRect& rect, int elementId)
2512 {
2513 wxASSERT( GetWindow() != NULL );
2514 if (!GetWindow())
2515 return wxACC_FAIL;
2516
2517 wxWindow* win = NULL;
2518 if (elementId == 0)
2519 {
2520 win = GetWindow();
2521 }
2522 else
2523 {
2524 if (elementId <= (int) GetWindow()->GetChildren().GetCount())
2525 {
2526 win = GetWindow()->GetChildren().Item(elementId-1)->GetData();
2527 }
2528 else
2529 return wxACC_FAIL;
2530 }
2531 if (win)
2532 {
2533 rect = win->GetRect();
2534 if (win->GetParent() && !win->IsKindOf(CLASSINFO(wxTopLevelWindow)))
2535 rect.SetPosition(win->GetParent()->ClientToScreen(rect.GetPosition()));
2536 return wxACC_OK;
2537 }
2538
2539 return wxACC_NOT_IMPLEMENTED;
2540 }
2541
2542 // Navigates from fromId to toId/toObject.
2543 wxAccStatus wxWindowAccessible::Navigate(wxNavDir navDir, int fromId,
2544 int* WXUNUSED(toId), wxAccessible** toObject)
2545 {
2546 wxASSERT( GetWindow() != NULL );
2547 if (!GetWindow())
2548 return wxACC_FAIL;
2549
2550 switch (navDir)
2551 {
2552 case wxNAVDIR_FIRSTCHILD:
2553 {
2554 if (GetWindow()->GetChildren().GetCount() == 0)
2555 return wxACC_FALSE;
2556 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetFirst()->GetData();
2557 *toObject = childWindow->GetOrCreateAccessible();
2558
2559 return wxACC_OK;
2560 }
2561 case wxNAVDIR_LASTCHILD:
2562 {
2563 if (GetWindow()->GetChildren().GetCount() == 0)
2564 return wxACC_FALSE;
2565 wxWindow* childWindow = (wxWindow*) GetWindow()->GetChildren().GetLast()->GetData();
2566 *toObject = childWindow->GetOrCreateAccessible();
2567
2568 return wxACC_OK;
2569 }
2570 case wxNAVDIR_RIGHT:
2571 case wxNAVDIR_DOWN:
2572 case wxNAVDIR_NEXT:
2573 {
2574 wxWindowList::compatibility_iterator node =
2575 wxWindowList::compatibility_iterator();
2576 if (fromId == 0)
2577 {
2578 // Can't navigate to sibling of this window
2579 // if we're a top-level window.
2580 if (!GetWindow()->GetParent())
2581 return wxACC_NOT_IMPLEMENTED;
2582
2583 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
2584 }
2585 else
2586 {
2587 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
2588 node = GetWindow()->GetChildren().Item(fromId-1);
2589 }
2590
2591 if (node && node->GetNext())
2592 {
2593 wxWindow* nextWindow = node->GetNext()->GetData();
2594 *toObject = nextWindow->GetOrCreateAccessible();
2595 return wxACC_OK;
2596 }
2597 else
2598 return wxACC_FALSE;
2599 }
2600 case wxNAVDIR_LEFT:
2601 case wxNAVDIR_UP:
2602 case wxNAVDIR_PREVIOUS:
2603 {
2604 wxWindowList::compatibility_iterator node =
2605 wxWindowList::compatibility_iterator();
2606 if (fromId == 0)
2607 {
2608 // Can't navigate to sibling of this window
2609 // if we're a top-level window.
2610 if (!GetWindow()->GetParent())
2611 return wxACC_NOT_IMPLEMENTED;
2612
2613 node = GetWindow()->GetParent()->GetChildren().Find(GetWindow());
2614 }
2615 else
2616 {
2617 if (fromId <= (int) GetWindow()->GetChildren().GetCount())
2618 node = GetWindow()->GetChildren().Item(fromId-1);
2619 }
2620
2621 if (node && node->GetPrevious())
2622 {
2623 wxWindow* previousWindow = node->GetPrevious()->GetData();
2624 *toObject = previousWindow->GetOrCreateAccessible();
2625 return wxACC_OK;
2626 }
2627 else
2628 return wxACC_FALSE;
2629 }
2630 }
2631
2632 return wxACC_NOT_IMPLEMENTED;
2633 }
2634
2635 // Gets the name of the specified object.
2636 wxAccStatus wxWindowAccessible::GetName(int childId, wxString* name)
2637 {
2638 wxASSERT( GetWindow() != NULL );
2639 if (!GetWindow())
2640 return wxACC_FAIL;
2641
2642 wxString title;
2643
2644 // If a child, leave wxWidgets to call the function on the actual
2645 // child object.
2646 if (childId > 0)
2647 return wxACC_NOT_IMPLEMENTED;
2648
2649 // This will eventually be replaced by specialised
2650 // accessible classes, one for each kind of wxWidgets
2651 // control or window.
2652 #if wxUSE_BUTTON
2653 if (GetWindow()->IsKindOf(CLASSINFO(wxButton)))
2654 title = ((wxButton*) GetWindow())->GetLabel();
2655 else
2656 #endif
2657 title = GetWindow()->GetName();
2658
2659 if (!title.IsEmpty())
2660 {
2661 *name = title;
2662 return wxACC_OK;
2663 }
2664 else
2665 return wxACC_NOT_IMPLEMENTED;
2666 }
2667
2668 // Gets the number of children.
2669 wxAccStatus wxWindowAccessible::GetChildCount(int* childId)
2670 {
2671 wxASSERT( GetWindow() != NULL );
2672 if (!GetWindow())
2673 return wxACC_FAIL;
2674
2675 *childId = (int) GetWindow()->GetChildren().GetCount();
2676 return wxACC_OK;
2677 }
2678
2679 // Gets the specified child (starting from 1).
2680 // If *child is NULL and return value is wxACC_OK,
2681 // this means that the child is a simple element and
2682 // not an accessible object.
2683 wxAccStatus wxWindowAccessible::GetChild(int childId, wxAccessible** child)
2684 {
2685 wxASSERT( GetWindow() != NULL );
2686 if (!GetWindow())
2687 return wxACC_FAIL;
2688
2689 if (childId == 0)
2690 {
2691 *child = this;
2692 return wxACC_OK;
2693 }
2694
2695 if (childId > (int) GetWindow()->GetChildren().GetCount())
2696 return wxACC_FAIL;
2697
2698 wxWindow* childWindow = GetWindow()->GetChildren().Item(childId-1)->GetData();
2699 *child = childWindow->GetOrCreateAccessible();
2700 if (*child)
2701 return wxACC_OK;
2702 else
2703 return wxACC_FAIL;
2704 }
2705
2706 // Gets the parent, or NULL.
2707 wxAccStatus wxWindowAccessible::GetParent(wxAccessible** parent)
2708 {
2709 wxASSERT( GetWindow() != NULL );
2710 if (!GetWindow())
2711 return wxACC_FAIL;
2712
2713 wxWindow* parentWindow = GetWindow()->GetParent();
2714 if (!parentWindow)
2715 {
2716 *parent = NULL;
2717 return wxACC_OK;
2718 }
2719 else
2720 {
2721 *parent = parentWindow->GetOrCreateAccessible();
2722 if (*parent)
2723 return wxACC_OK;
2724 else
2725 return wxACC_FAIL;
2726 }
2727 }
2728
2729 // Performs the default action. childId is 0 (the action for this object)
2730 // or > 0 (the action for a child).
2731 // Return wxACC_NOT_SUPPORTED if there is no default action for this
2732 // window (e.g. an edit control).
2733 wxAccStatus wxWindowAccessible::DoDefaultAction(int WXUNUSED(childId))
2734 {
2735 wxASSERT( GetWindow() != NULL );
2736 if (!GetWindow())
2737 return wxACC_FAIL;
2738
2739 return wxACC_NOT_IMPLEMENTED;
2740 }
2741
2742 // Gets the default action for this object (0) or > 0 (the action for a child).
2743 // Return wxACC_OK even if there is no action. actionName is the action, or the empty
2744 // string if there is no action.
2745 // The retrieved string describes the action that is performed on an object,
2746 // not what the object does as a result. For example, a toolbar button that prints
2747 // a document has a default action of "Press" rather than "Prints the current document."
2748 wxAccStatus wxWindowAccessible::GetDefaultAction(int WXUNUSED(childId), wxString* WXUNUSED(actionName))
2749 {
2750 wxASSERT( GetWindow() != NULL );
2751 if (!GetWindow())
2752 return wxACC_FAIL;
2753
2754 return wxACC_NOT_IMPLEMENTED;
2755 }
2756
2757 // Returns the description for this object or a child.
2758 wxAccStatus wxWindowAccessible::GetDescription(int WXUNUSED(childId), wxString* description)
2759 {
2760 wxASSERT( GetWindow() != NULL );
2761 if (!GetWindow())
2762 return wxACC_FAIL;
2763
2764 wxString ht(GetWindow()->GetHelpText());
2765 if (!ht.IsEmpty())
2766 {
2767 *description = ht;
2768 return wxACC_OK;
2769 }
2770 return wxACC_NOT_IMPLEMENTED;
2771 }
2772
2773 // Returns help text for this object or a child, similar to tooltip text.
2774 wxAccStatus wxWindowAccessible::GetHelpText(int WXUNUSED(childId), wxString* helpText)
2775 {
2776 wxASSERT( GetWindow() != NULL );
2777 if (!GetWindow())
2778 return wxACC_FAIL;
2779
2780 wxString ht(GetWindow()->GetHelpText());
2781 if (!ht.IsEmpty())
2782 {
2783 *helpText = ht;
2784 return wxACC_OK;
2785 }
2786 return wxACC_NOT_IMPLEMENTED;
2787 }
2788
2789 // Returns the keyboard shortcut for this object or child.
2790 // Return e.g. ALT+K
2791 wxAccStatus wxWindowAccessible::GetKeyboardShortcut(int WXUNUSED(childId), wxString* WXUNUSED(shortcut))
2792 {
2793 wxASSERT( GetWindow() != NULL );
2794 if (!GetWindow())
2795 return wxACC_FAIL;
2796
2797 return wxACC_NOT_IMPLEMENTED;
2798 }
2799
2800 // Returns a role constant.
2801 wxAccStatus wxWindowAccessible::GetRole(int childId, wxAccRole* role)
2802 {
2803 wxASSERT( GetWindow() != NULL );
2804 if (!GetWindow())
2805 return wxACC_FAIL;
2806
2807 // If a child, leave wxWidgets to call the function on the actual
2808 // child object.
2809 if (childId > 0)
2810 return wxACC_NOT_IMPLEMENTED;
2811
2812 if (GetWindow()->IsKindOf(CLASSINFO(wxControl)))
2813 return wxACC_NOT_IMPLEMENTED;
2814 #if wxUSE_STATUSBAR
2815 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar)))
2816 return wxACC_NOT_IMPLEMENTED;
2817 #endif
2818 #if wxUSE_TOOLBAR
2819 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar)))
2820 return wxACC_NOT_IMPLEMENTED;
2821 #endif
2822
2823 //*role = wxROLE_SYSTEM_CLIENT;
2824 *role = wxROLE_SYSTEM_CLIENT;
2825 return wxACC_OK;
2826
2827 #if 0
2828 return wxACC_NOT_IMPLEMENTED;
2829 #endif
2830 }
2831
2832 // Returns a state constant.
2833 wxAccStatus wxWindowAccessible::GetState(int childId, long* state)
2834 {
2835 wxASSERT( GetWindow() != NULL );
2836 if (!GetWindow())
2837 return wxACC_FAIL;
2838
2839 // If a child, leave wxWidgets to call the function on the actual
2840 // child object.
2841 if (childId > 0)
2842 return wxACC_NOT_IMPLEMENTED;
2843
2844 if (GetWindow()->IsKindOf(CLASSINFO(wxControl)))
2845 return wxACC_NOT_IMPLEMENTED;
2846
2847 #if wxUSE_STATUSBAR
2848 if (GetWindow()->IsKindOf(CLASSINFO(wxStatusBar)))
2849 return wxACC_NOT_IMPLEMENTED;
2850 #endif
2851 #if wxUSE_TOOLBAR
2852 if (GetWindow()->IsKindOf(CLASSINFO(wxToolBar)))
2853 return wxACC_NOT_IMPLEMENTED;
2854 #endif
2855
2856 *state = 0;
2857 return wxACC_OK;
2858
2859 #if 0
2860 return wxACC_NOT_IMPLEMENTED;
2861 #endif
2862 }
2863
2864 // Returns a localized string representing the value for the object
2865 // or child.
2866 wxAccStatus wxWindowAccessible::GetValue(int WXUNUSED(childId), wxString* WXUNUSED(strValue))
2867 {
2868 wxASSERT( GetWindow() != NULL );
2869 if (!GetWindow())
2870 return wxACC_FAIL;
2871
2872 return wxACC_NOT_IMPLEMENTED;
2873 }
2874
2875 // Selects the object or child.
2876 wxAccStatus wxWindowAccessible::Select(int WXUNUSED(childId), wxAccSelectionFlags WXUNUSED(selectFlags))
2877 {
2878 wxASSERT( GetWindow() != NULL );
2879 if (!GetWindow())
2880 return wxACC_FAIL;
2881
2882 return wxACC_NOT_IMPLEMENTED;
2883 }
2884
2885 // Gets the window with the keyboard focus.
2886 // If childId is 0 and child is NULL, no object in
2887 // this subhierarchy has the focus.
2888 // If this object has the focus, child should be 'this'.
2889 wxAccStatus wxWindowAccessible::GetFocus(int* WXUNUSED(childId), wxAccessible** WXUNUSED(child))
2890 {
2891 wxASSERT( GetWindow() != NULL );
2892 if (!GetWindow())
2893 return wxACC_FAIL;
2894
2895 return wxACC_NOT_IMPLEMENTED;
2896 }
2897
2898 // Gets a variant representing the selected children
2899 // of this object.
2900 // Acceptable values:
2901 // - a null variant (IsNull() returns true)
2902 // - a list variant (GetType() == wxT("list")
2903 // - an integer representing the selected child element,
2904 // or 0 if this object is selected (GetType() == wxT("long")
2905 // - a "void*" pointer to a wxAccessible child object
2906 wxAccStatus wxWindowAccessible::GetSelections(wxVariant* WXUNUSED(selections))
2907 {
2908 wxASSERT( GetWindow() != NULL );
2909 if (!GetWindow())
2910 return wxACC_FAIL;
2911
2912 return wxACC_NOT_IMPLEMENTED;
2913 }
2914
2915 #endif // wxUSE_ACCESSIBILITY