* Exposed ValidScrollOffsetFor() as public method.

* Make validating the scroll offset in SetDataRect() optional.
 * Introduced SetDataRectAndScrollOffset() to set both atomically.
   This avoids some unwanted feedback when having to set them
   both anyway, but hook methods are called and re-enter back into
   the client code.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41179 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2011-04-04 09:25:07 +00:00
parent e62d9c79e1
commit 59383a8c46
2 changed files with 78 additions and 42 deletions
@@ -1,9 +1,6 @@
/*
* Copyright 2006, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Ingo Weinhold <[email protected]>
* Copyright 2001-2009, Ingo Weinhold <[email protected]>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "Scrollable.h"
@@ -13,8 +10,6 @@
#include "Scroller.h"
using namespace std;
// constructor
Scrollable::Scrollable()
: fDataRect(0.0, 0.0, 0.0, 0.0),
@@ -67,7 +62,7 @@ Scrollable::ScrollSource() const
//
// Sets the data rect.
void
Scrollable::SetDataRect(BRect dataRect)
Scrollable::SetDataRect(BRect dataRect, bool validateScrollOffset)
{
if (fDataRect != dataRect && dataRect.IsValid()) {
BRect oldDataRect = fDataRect;
@@ -78,9 +73,11 @@ Scrollable::SetDataRect(BRect dataRect)
if (fScrollSource)
fScrollSource->DataRectChanged(oldDataRect, fDataRect);
// adjust the scroll offset, if necessary
BPoint offset = _ValidScrollOffsetFor(fScrollOffset);
if (offset != fScrollOffset)
SetScrollOffset(offset);
if (validateScrollOffset) {
BPoint offset = ValidScrollOffsetFor(fScrollOffset);
if (offset != fScrollOffset)
SetScrollOffset(offset);
}
}
}
@@ -100,7 +97,7 @@ void
Scrollable::SetScrollOffset(BPoint offset)
{
// adjust the supplied offset to be valid
offset = _ValidScrollOffsetFor(offset);
offset = ValidScrollOffsetFor(offset);
if (fScrollOffset != offset) {
BPoint oldOffset = fScrollOffset;
fScrollOffset = offset;
@@ -121,6 +118,62 @@ Scrollable::ScrollOffset() const
return fScrollOffset;
}
// SetDataRect
//
// Sets the data rect.
void
Scrollable::SetDataRectAndScrollOffset(BRect dataRect, BPoint offset)
{
if (fDataRect != dataRect && dataRect.IsValid()) {
BRect oldDataRect = fDataRect;
fDataRect = dataRect;
// notify ourselves
DataRectChanged(oldDataRect, fDataRect);
// notify scroller
if (fScrollSource) {
fScrollSource->SetScrollingEnabled(false);
fScrollSource->DataRectChanged(oldDataRect, fDataRect);
}
// adjust the scroll offset, if necessary
offset = ValidScrollOffsetFor(offset);
if (offset != fScrollOffset)
SetScrollOffset(offset);
if (fScrollSource)
fScrollSource->SetScrollingEnabled(true);
}
}
// ValidScrollOffsetFor
//
// Returns the valid scroll offset next to the supplied offset.
BPoint
Scrollable::ValidScrollOffsetFor(BPoint offset) const
{
return ValidScrollOffsetFor(offset, fDataRect);
}
// ValidScrollOffsetFor
//
// Returns the valid scroll offset next to the supplied offset.
BPoint
Scrollable::ValidScrollOffsetFor(BPoint offset, const BRect& dataRect) const
{
float maxX = max_c(dataRect.left, dataRect.right - fVisibleWidth);
float maxY = max_c(dataRect.top, dataRect.bottom - fVisibleHeight);
// adjust the offset to be valid
if (offset.x < dataRect.left)
offset.x = dataRect.left;
else if (offset.x > maxX)
offset.x = maxX;
if (offset.y < dataRect.top)
offset.y = dataRect.top;
else if (offset.y > maxY)
offset.y = maxY;
return offset;
}
// SetVisibleSize
//
// Sets the visible size.
@@ -141,7 +194,7 @@ Scrollable::SetVisibleSize(float width, float height)
fVisibleWidth, fVisibleHeight);
}
// adjust the scroll offset, if necessary
BPoint offset = _ValidScrollOffsetFor(fScrollOffset);
BPoint offset = ValidScrollOffsetFor(fScrollOffset);
if (offset != fScrollOffset)
SetScrollOffset(offset);
}
@@ -207,23 +260,4 @@ Scrollable::ScrollSourceChanged(Scroller* /*oldSource*/,
{
}
// _ValidScrollOffsetFor
//
// Returns the valid scroll offset next to the supplied offset.
BPoint
Scrollable::_ValidScrollOffsetFor(BPoint offset) const
{
float maxX = max(fDataRect.left, fDataRect.right - fVisibleWidth);
float maxY = max(fDataRect.top, fDataRect.bottom - fVisibleHeight);
// adjust the offset to be valid
if (offset.x < fDataRect.left)
offset.x = fDataRect.left;
else if (offset.x > maxX)
offset.x = maxX;
if (offset.y < fDataRect.top)
offset.y = fDataRect.top;
else if (offset.y > maxY)
offset.y = maxY;
return offset;
}
@@ -1,11 +1,7 @@
/*
* Copyright 2006, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Ingo Weinhold <[email protected]>
* Copyright 2001-2009, Ingo Weinhold <[email protected]>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef SCROLLABLE_H
#define SCROLLABLE_H
@@ -22,12 +18,20 @@ class Scrollable {
void SetScrollSource(Scroller* source);
Scroller* ScrollSource() const;
void SetDataRect(BRect dataRect);
void SetDataRect(BRect dataRect,
bool validateScrollOffset = true);
BRect DataRect() const;
void SetScrollOffset(BPoint offset);
virtual void SetScrollOffset(BPoint offset);
BPoint ScrollOffset() const;
void SetDataRectAndScrollOffset(BRect dataRect,
BPoint offset);
BPoint ValidScrollOffsetFor(BPoint offset) const;
BPoint ValidScrollOffsetFor(BPoint offset,
const BRect& dataRect) const;
void SetVisibleSize(float width, float height);
BRect VisibleBounds() const;
BRect VisibleRect() const;
@@ -50,8 +54,6 @@ protected:
float fVisibleWidth;
float fVisibleHeight;
Scroller* fScrollSource;
BPoint _ValidScrollOffsetFor(BPoint offset) const;
};