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