X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1ebfafde515e58d4a6f9fda5290a5620ebc1637f..a4de7e8ccf4bf69d150c29c9a3d53e5754c8b8a9:/src/common/wincmn.cpp diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index 0c419c0476..8f7143f57a 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -5,7 +5,7 @@ // Modified by: // Created: 13/07/98 // RCS-ID: $Id$ -// Copyright: (c) wxWindows team +// Copyright: (c) wxWidgets team // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -47,6 +47,10 @@ #include "wx/dcclient.h" #endif //WX_PRECOMP +#if defined(__WXMAC__) && wxUSE_SCROLLBAR + #include "wx/scrolbar.h" +#endif + #if wxUSE_CONSTRAINTS #include "wx/layout.h" #endif // wxUSE_CONSTRAINTS @@ -115,13 +119,14 @@ wxWindowBase::wxWindowBase() m_parent = (wxWindow *)NULL; m_windowId = wxID_ANY; - m_initialSize = wxDefaultSize; - // no constraints on the minimal window size m_minWidth = + m_maxWidth = wxDefaultSize.x; m_minHeight = - m_maxWidth = - m_maxHeight = -1; + m_maxHeight = wxDefaultSize.y; + + // invalidiated cache value + m_bestSizeCache = wxDefaultSize; // window are created enabled and visible by default m_isShown = @@ -140,6 +145,9 @@ wxWindowBase::wxWindowBase() m_hasBgCol = m_hasFgCol = m_hasFont = false; + m_inheritBgCol = + m_inheritFgCol = + m_inheritFont = false; // no style bits m_exStyle = @@ -178,9 +186,9 @@ wxWindowBase::wxWindowBase() m_virtualSize = wxDefaultSize; m_minVirtualWidth = + m_maxVirtualWidth = wxDefaultSize.x; m_minVirtualHeight = - m_maxVirtualWidth = - m_maxVirtualHeight = -1; + m_maxVirtualHeight = wxDefaultSize.y; m_windowVariant = wxWINDOW_VARIANT_NORMAL; @@ -195,7 +203,7 @@ wxWindowBase::wxWindowBase() bool wxWindowBase::CreateBase(wxWindowBase *parent, wxWindowID id, const wxPoint& WXUNUSED(pos), - const wxSize& size, + const wxSize& WXUNUSED(size), long style, const wxValidator& wxVALIDATOR_PARAM(validator), const wxString& name) @@ -213,7 +221,7 @@ bool wxWindowBase::CreateBase(wxWindowBase *parent, // ids are limited to 16 bits under MSW so if you care about portability, // it's not a good idea to use ids out of this range (and negative ids are - // reserved for wxWindows own usage) + // reserved for wxWidgets own usage) wxASSERT_MSG( id == wxID_ANY || (id >= 0 && id < 32767), _T("invalid id value") ); @@ -224,10 +232,6 @@ bool wxWindowBase::CreateBase(wxWindowBase *parent, SetWindowStyleFlag(style); SetParent(parent); - // Save the size passed to the ctor (if any.) This will be used later as - // the minimal size if the window is added to a sizer. - m_initialSize = size; - #if wxUSE_VALIDATORS SetValidator(validator); #endif // wxUSE_VALIDATORS @@ -434,8 +438,8 @@ void wxWindowBase::Centre(int direction) int width, height; GetSize(&width, &height); - int xNew = -1, - yNew = -1; + int xNew = wxDefaultPosition.x, + yNew = wxDefaultPosition.y; if ( direction & wxHORIZONTAL ) xNew = (widthParent - width)/2; @@ -484,7 +488,7 @@ void wxWindowBase::Fit() { if ( GetChildren().GetCount() > 0 ) { - SetClientSize(DoGetBestSize()); + SetClientSize(GetBestSize()); } //else: do nothing if we have no children } @@ -498,6 +502,24 @@ void wxWindowBase::FitInside() } } +// On Mac, scrollbars are explicitly children. +#ifdef __WXMAC__ +static bool wxHasRealChildren(const wxWindowBase* win) +{ + int realChildCount = 0; + + for ( wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst(); + node; + node = node->GetNext() ) + { + wxWindow *win = node->GetData(); + if ( !win->IsTopLevel() && win->IsShown() && !win->IsKindOf(CLASSINFO(wxScrollBar))) + realChildCount ++; + } + return (realChildCount > 0); +} +#endif + // return the size best suited for the current window wxSize wxWindowBase::DoGetBestSize() const { @@ -542,7 +564,11 @@ wxSize wxWindowBase::DoGetBestSize() const return wxSize(maxX, maxY); } #endif // wxUSE_CONSTRAINTS - else if ( !GetChildren().empty() ) + else if ( !GetChildren().empty() +#ifdef __WXMAC__ + && wxHasRealChildren(this) +#endif + ) { // our minimal acceptable size is such that all our visible child windows fit inside int maxX = 0, @@ -570,9 +596,9 @@ wxSize wxWindowBase::DoGetBestSize() const // if the window hadn't been positioned yet, assume that it is in // the origin - if ( wx == -1 ) + if ( wx == wxDefaultPosition.x ) wx = 0; - if ( wy == -1 ) + if ( wy == wxDefaultPosition.y ) wy = 0; win->GetSize(&ww, &wh); @@ -589,14 +615,47 @@ wxSize wxWindowBase::DoGetBestSize() const return wxSize(maxX, maxY); } - else // has children + else // ! has children + { + // for a generic window there is no natural best size - just use either the + // minimum size if there is one, or the current size + if ( GetMinSize().IsFullySpecified() ) + return GetMinSize(); + else + return GetSize(); + } +} + + +wxSize wxWindowBase::GetBestFittingSize() const +{ + // merge the best size with the min size, giving priority to the min size + wxSize min = GetMinSize(); + if (min.x == wxDefaultCoord || min.y == wxDefaultCoord) { - // for a generic window there is no natural best size - just use the - // current size - return GetSize(); + wxSize best = GetBestSize(); + if (min.x == wxDefaultCoord) min.x = best.x; + if (min.y == wxDefaultCoord) min.y = best.y; } + return min; +} + + +void wxWindowBase::SetBestFittingSize(const wxSize& size) +{ + // Set the min size to the size passed in. This will usually either be + // wxDefaultSize or the size passed to this window's ctor/Create function. + SetMinSize(size); + + // Merge the size with the best size if needed + wxSize best = GetBestFittingSize(); + + // If the current size doesn't match then change it + if (GetSize() != best) + SetSize(best); } + // by default the origin is not shifted wxPoint wxWindowBase::GetClientAreaOrigin() const { @@ -610,8 +669,8 @@ void wxWindowBase::SetSizeHints(int minW, int minH, { // setting min width greater than max width leads to infinite loops under // X11 and generally doesn't make any sense, so don't allow it - wxCHECK_RET( (minW == -1 || maxW == -1 || minW <= maxW) && - (minH == -1 || maxH == -1 || minH <= maxH), + wxCHECK_RET( (minW == wxDefaultSize.x || maxW == wxDefaultSize.x || minW <= maxW) && + (minH == wxDefaultSize.y || maxH == wxDefaultSize.y || minH <= maxH), _T("min width/height must be less than max width/height!") ); m_minWidth = minW; @@ -676,13 +735,13 @@ void wxWindowBase::SetVirtualSizeHints( int minW, int minH, void wxWindowBase::DoSetVirtualSize( int x, int y ) { - if ( m_minVirtualWidth != -1 && m_minVirtualWidth > x ) + if ( m_minVirtualWidth != wxDefaultSize.x && m_minVirtualWidth > x ) x = m_minVirtualWidth; - if ( m_maxVirtualWidth != -1 && m_maxVirtualWidth < x ) + if ( m_maxVirtualWidth != wxDefaultSize.x && m_maxVirtualWidth < x ) x = m_maxVirtualWidth; - if ( m_minVirtualHeight != -1 && m_minVirtualHeight > y ) + if ( m_minVirtualHeight != wxDefaultSize.y && m_minVirtualHeight > y ) y = m_minVirtualHeight; - if ( m_maxVirtualHeight != -1 && m_maxVirtualHeight < y ) + if ( m_maxVirtualHeight != wxDefaultSize.y && m_maxVirtualHeight < y ) y = m_maxVirtualHeight; m_virtualSize = wxSize(x, y); @@ -886,17 +945,17 @@ void wxWindowBase::InheritAttributes() // which ensures that this only happens if the user really wants it and // not by default which wouldn't make any sense in modern GUIs where the // controls don't all use the same fonts (nor colours) - if ( parent->m_hasFont && !m_hasFont ) + if ( parent->m_inheritFont && !m_hasFont ) SetFont(parent->GetFont()); // in addition, there is a possibility to explicitly forbid inheriting // colours at each class level by overriding ShouldInheritColours() if ( ShouldInheritColours() ) { - if ( parent->m_hasFgCol && !m_hasFgCol ) + if ( parent->m_inheritFgCol && !m_hasFgCol ) SetForegroundColour(parent->GetForegroundColour()); - if ( parent->m_hasBgCol && !m_hasBgCol ) + if ( parent->m_inheritBgCol && !m_hasBgCol ) SetBackgroundColour(parent->GetBackgroundColour()); } } @@ -925,17 +984,16 @@ wxColour wxWindowBase::GetBackgroundColour() const // we must return some valid colour to avoid redoing this every time // and also to avoid surprizing the applications written for older - // wxWindows versions where GetBackgroundColour() always returned + // wxWidgets versions where GetBackgroundColour() always returned // something -- so give them something even if it doesn't make sense // for this window (e.g. it has a themed background) if ( !colBg.Ok() ) colBg = GetClassDefaultAttributes().colBg; - // cache it for the next call - wxConstCast(this, wxWindowBase)->m_backgroundColour = colBg; + return colBg; } - - return m_backgroundColour; + else + return m_backgroundColour; } wxColour wxWindowBase::GetForegroundColour() const @@ -950,33 +1008,33 @@ wxColour wxWindowBase::GetForegroundColour() const if ( !colFg.Ok() ) colFg = GetClassDefaultAttributes().colFg; - wxConstCast(this, wxWindowBase)->m_foregroundColour = colFg; + return colFg; } - - return m_foregroundColour; + else + return m_foregroundColour; } bool wxWindowBase::SetBackgroundColour( const wxColour &colour ) { - if ( !colour.Ok() || (colour == m_backgroundColour) ) + if ( colour == m_backgroundColour ) return false; + m_hasBgCol = colour.Ok(); + m_inheritBgCol = m_hasBgCol; m_backgroundColour = colour; - - m_hasBgCol = true; - + SetThemeEnabled( !m_hasBgCol && !m_foregroundColour.Ok() ); return true; } bool wxWindowBase::SetForegroundColour( const wxColour &colour ) { - if ( !colour.Ok() || (colour == m_foregroundColour) ) + if (colour == m_foregroundColour ) return false; + m_hasFgCol = colour.Ok(); + m_inheritFgCol = m_hasFgCol; m_foregroundColour = colour; - - m_hasFgCol = true; - + SetThemeEnabled( !m_hasFgCol && !m_backgroundColour.Ok() ); return true; } @@ -995,7 +1053,7 @@ bool wxWindowBase::SetCursor(const wxCursor& cursor) return true; } -wxFont& wxWindowBase::DoGetFont() const +wxFont wxWindowBase::GetFont() const { // logic is the same as in GetBackgroundColour() if ( !m_font.Ok() ) @@ -1006,18 +1064,14 @@ wxFont& wxWindowBase::DoGetFont() const if ( !font.Ok() ) font = GetClassDefaultAttributes().font; - wxConstCast(this, wxWindowBase)->m_font = font; + return font; } - - // cast is here for non-const GetFont() convenience - return wxConstCast(this, wxWindowBase)->m_font; + else + return m_font; } bool wxWindowBase::SetFont(const wxFont& font) { - if ( !font.Ok() ) - return false; - if ( font == m_font ) { // no change @@ -1025,8 +1079,8 @@ bool wxWindowBase::SetFont(const wxFont& font) } m_font = font; - - m_hasFont = true; + m_hasFont = font.Ok(); + m_inheritFont = m_hasFont; return true; } @@ -1625,7 +1679,7 @@ void wxWindowBase::SetSizerAndFit(wxSizer *sizer, bool deleteOld) sizer->SetSizeHints( (wxWindow*) this ); } - + void wxWindowBase::SetContainingSizer(wxSizer* sizer) { // adding a window to a sizer twice is going to result in fatal and @@ -1634,17 +1688,10 @@ void wxWindowBase::SetContainingSizer(wxSizer* sizer) // pointer; so try to detect this as early as possible wxASSERT_MSG( !sizer || m_containingSizer != sizer, _T("Adding a window to the same sizer twice?") ); - - m_containingSizer = sizer; - // If there was an initial size for this window, and if a minsize has not - // been set, then set the initial size as the minsize. This helps with - // sizer layout when a larger than GetBestSize size is needed for - // controls. - if (m_initialSize != wxDefaultSize && GetMinSize() == wxDefaultSize) - SetSizeHints(m_initialSize); + m_containingSizer = sizer; } - + #if wxUSE_CONSTRAINTS void wxWindowBase::SatisfyConstraints() @@ -1844,22 +1891,22 @@ void wxWindowBase::SetSizeConstraint(int x, int y, int w, int h) wxLayoutConstraints *constr = GetConstraints(); if ( constr ) { - if ( x != -1 ) + if ( x != wxDefaultPosition.x ) { constr->left.SetValue(x); constr->left.SetDone(true); } - if ( y != -1 ) + if ( y != wxDefaultPosition.y ) { constr->top.SetValue(y); constr->top.SetDone(true); } - if ( w != -1 ) + if ( w != wxDefaultSize.x ) { constr->width.SetValue(w); constr->width.SetDone(true); } - if ( h != -1 ) + if ( h != wxDefaultSize.y ) { constr->height.SetValue(h); constr->height.SetDone(true); @@ -1872,12 +1919,12 @@ void wxWindowBase::MoveConstraint(int x, int y) wxLayoutConstraints *constr = GetConstraints(); if ( constr ) { - if ( x != -1 ) + if ( x != wxDefaultPosition.x ) { constr->left.SetValue(x); constr->left.SetDone(true); } - if ( y != -1 ) + if ( y != wxDefaultPosition.y ) { constr->top.SetValue(y); constr->top.SetDone(true); @@ -2072,6 +2119,8 @@ void wxWindowBase::OnSysColourChanged(wxSysColourChangedEvent& event) node = node->GetNext(); } + + Refresh(); } // the default action is to populate dialog with data when it's created, @@ -2125,7 +2174,7 @@ void wxWindowBase::OnMiddleClick( wxMouseEvent& event ) wxMessageBox(wxString::Format( _T( - " wxWindows Library (%s port)\nVersion %u.%u.%u%s, compiled at %s %s\n Copyright (c) 1995-2002 wxWindows team" + " wxWidgets Library (%s port)\nVersion %u.%u.%u%s, compiled at %s %s\n Copyright (c) 1995-2002 wxWidgets team" ), port.c_str(), wxMAJOR_VERSION, @@ -2139,7 +2188,7 @@ void wxWindowBase::OnMiddleClick( wxMouseEvent& event ) __TDATE__, __TTIME__ ), - _T("wxWindows information"), + _T("wxWidgets information"), wxICON_INFORMATION | wxOK, (wxWindow *)this); } @@ -2358,6 +2407,53 @@ bool wxWindowBase::TryParent(wxEvent& event) return wxEvtHandler::TryParent(event); } +// ---------------------------------------------------------------------------- +// keyboard navigation +// ---------------------------------------------------------------------------- + +// Navigates in the specified direction. +bool wxWindowBase::Navigate(int flags) +{ + wxNavigationKeyEvent eventNav; + eventNav.SetFlags(flags); + eventNav.SetEventObject(this); + if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) ) + { + return true; + } + return false; +} + +void wxWindowBase::DoMoveInTabOrder(wxWindow *win, MoveKind move) +{ + // check that we're not a top level window + wxCHECK_RET( GetParent(), + _T("MoveBefore/AfterInTabOrder() don't work for TLWs!") ); + + // find the target window in the siblings list + wxWindowList& siblings = GetParent()->GetChildren(); + wxWindowList::compatibility_iterator i = siblings.Find(win); + wxCHECK_RET( i, _T("MoveBefore/AfterInTabOrder(): win is not a sibling") ); + + // unfortunately, when wxUSE_STL == 1 DetachNode() is not implemented so we + // can't just move the node around + wxWindow *self = (wxWindow *)this; + siblings.DeleteObject(self); + if ( move == MoveAfter ) + { + i = i->GetNext(); + } + + if ( i ) + { + siblings.Insert(i, self); + } + else // MoveAfter and win was the last sibling + { + siblings.Append(self); + } +} + // ---------------------------------------------------------------------------- // global functions // ---------------------------------------------------------------------------- @@ -2520,17 +2616,19 @@ wxAccStatus wxWindowAccessible::GetName(int childId, wxString* name) wxString title; - // If a child, leave wxWindows to call the function on the actual + // If a child, leave wxWidgets to call the function on the actual // child object. if (childId > 0) return wxACC_NOT_IMPLEMENTED; // This will eventually be replaced by specialised - // accessible classes, one for each kind of wxWindows + // accessible classes, one for each kind of wxWidgets // control or window. +#if wxUSE_BUTTON if (GetWindow()->IsKindOf(CLASSINFO(wxButton))) title = ((wxButton*) GetWindow())->GetLabel(); else +#endif title = GetWindow()->GetName(); if (!title.IsEmpty()) @@ -2681,7 +2779,7 @@ wxAccStatus wxWindowAccessible::GetRole(int childId, wxAccRole* role) if (!GetWindow()) return wxACC_FAIL; - // If a child, leave wxWindows to call the function on the actual + // If a child, leave wxWidgets to call the function on the actual // child object. if (childId > 0) return wxACC_NOT_IMPLEMENTED; @@ -2713,7 +2811,7 @@ wxAccStatus wxWindowAccessible::GetState(int childId, long* state) if (!GetWindow()) return wxACC_FAIL; - // If a child, leave wxWindows to call the function on the actual + // If a child, leave wxWidgets to call the function on the actual // child object. if (childId > 0) return wxACC_NOT_IMPLEMENTED;