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