Extended the ScrollView API a bit.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@31357 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus 2009-07-01 18:50:22 +00:00
parent cd170ddc34
commit 007ea5873a
4 changed files with 180 additions and 72 deletions

View File

@ -37,6 +37,9 @@ class InternalScrollBar : public BScrollBar {
virtual void ValueChanged(float value); virtual void ValueChanged(float value);
virtual void MouseDown(BPoint where);
virtual void MouseUp(BPoint where);
private: private:
ScrollView* fScrollView; ScrollView* fScrollView;
}; };
@ -64,6 +67,26 @@ InternalScrollBar::ValueChanged(float value)
fScrollView->_ScrollValueChanged(this, value); fScrollView->_ScrollValueChanged(this, value);
} }
// MouseDown
void
InternalScrollBar::MouseDown(BPoint where)
{
if (fScrollView)
fScrollView->_SetScrolling(true);
BScrollBar::MouseDown(where);
}
// MouseUp
void
InternalScrollBar::MouseUp(BPoint where)
{
BScrollBar::MouseUp(where);
if (fScrollView)
fScrollView->_SetScrolling(false);
}
// #pragma mark -ScrollCorner // #pragma mark -ScrollCorner
class ScrollCorner : public BView { class ScrollCorner : public BView {
@ -124,7 +147,7 @@ ScrollCorner::ScrollCorner(ScrollView* scrollView)
int32 bpr = fBitmaps[0]->BytesPerRow(); int32 bpr = fBitmaps[0]->BytesPerRow();
for (int i = 0; i <= sBitmapHeight; i++, bits += bpr) for (int i = 0; i <= sBitmapHeight; i++, bits += bpr)
memcpy(bits, &sScrollCornerNormalBits[i * sBitmapHeight * 4], sBitmapWidth * 4); memcpy(bits, &sScrollCornerNormalBits[i * sBitmapHeight * 4], sBitmapWidth * 4);
//printf("setting up bitmap 1\n"); //printf("setting up bitmap 1\n");
fBitmaps[1] = new BBitmap(BRect(0.0f, 0.0f, sBitmapWidth, sBitmapHeight), sColorSpace); fBitmaps[1] = new BBitmap(BRect(0.0f, 0.0f, sBitmapWidth, sBitmapHeight), sColorSpace);
// fBitmaps[1]->SetBits((void *)sScrollCornerPushedBits, fBitmaps[1]->BitsLength(), 0L, sColorSpace); // fBitmaps[1]->SetBits((void *)sScrollCornerPushedBits, fBitmaps[1]->BitsLength(), 0L, sColorSpace);
@ -257,63 +280,35 @@ ScrollCorner::SetDragging(bool dragging)
} }
} }
// #pragma mark - ScrollView // #pragma mark - ScrollView
// constructor // constructor
ScrollView::ScrollView(BView* child, uint32 scrollingFlags, BRect frame, ScrollView::ScrollView(BView* child, uint32 scrollingFlags, BRect frame,
const char *name, uint32 resizingMode, uint32 viewFlags, const char* name, uint32 resizingMode, uint32 viewFlags,
uint32 borderStyle, uint32 borderFlags) uint32 borderStyle, uint32 borderFlags)
: BView(frame, name, resizingMode, viewFlags | B_FRAME_EVENTS | B_WILL_DRAW : BView(frame, name, resizingMode,
| B_FULL_UPDATE_ON_RESIZE), viewFlags | B_FRAME_EVENTS | B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
Scroller(), Scroller()
fChild(NULL),
fScrollingFlags(scrollingFlags),
fHScrollBar(NULL),
fVScrollBar(NULL),
fScrollCorner(NULL),
fHVisible(true),
fVVisible(true),
fCornerVisible(true),
fWindowActive(false),
fChildFocused(false),
fHSmallStep(1),
fVSmallStep(1),
fBorderStyle(borderStyle),
fBorderFlags(borderFlags)
{ {
// Set transparent view color -- our area is completely covered by _Init(child, scrollingFlags, borderStyle, borderFlags);
// our children.
SetViewColor(B_TRANSPARENT_32_BIT);
// create scroll bars
if (fScrollingFlags & (SCROLL_HORIZONTAL | SCROLL_HORIZONTAL_MAGIC)) {
fHScrollBar = new InternalScrollBar(this,
BRect(0.0, 0.0, 100.0, B_H_SCROLL_BAR_HEIGHT), B_HORIZONTAL);
AddChild(fHScrollBar);
}
if (fScrollingFlags & (SCROLL_VERTICAL | SCROLL_VERTICAL_MAGIC)) {
fVScrollBar = new InternalScrollBar(this,
BRect(0.0, 0.0, B_V_SCROLL_BAR_WIDTH, 100.0), B_VERTICAL);
AddChild(fVScrollBar);
}
// Create a scroll corner, if we can scroll into both direction.
if (fHScrollBar && fVScrollBar) {
fScrollCorner = new ScrollCorner(this);
AddChild(fScrollCorner);
}
// add child
if (child) {
fChild = child;
AddChild(child);
if (Scrollable* scrollable = dynamic_cast<Scrollable*>(child))
SetScrollTarget(scrollable);
}
} }
#ifdef __HAIKU__
// constructor
ScrollView::ScrollView(BView* child, uint32 scrollingFlags, const char* name,
uint32 viewFlags, uint32 borderStyle, uint32 borderFlags)
: BView(name, viewFlags | B_FRAME_EVENTS | B_WILL_DRAW
| B_FULL_UPDATE_ON_RESIZE),
Scroller()
{
_Init(child, scrollingFlags, borderStyle, borderFlags);
}
#endif // __HAIKU__
// destructor // destructor
ScrollView::~ScrollView() ScrollView::~ScrollView()
{ {
@ -528,6 +523,21 @@ ScrollView::VSmallStep() const
return fVSmallStep; return fVSmallStep;
} }
// IsScrolling
bool
ScrollView::IsScrolling() const
{
return fScrolling;
}
void
ScrollView::SetScrollingEnabled(bool enabled)
{
Scroller::SetScrollingEnabled(enabled);
if (IsScrollingEnabled())
SetScrollOffset(ScrollOffset());
}
// #pragma mark - // #pragma mark -
// DataRectChanged // DataRectChanged
@ -583,10 +593,69 @@ ScrollView::ScrollTargetChanged(Scrollable* /*oldTarget*/,
*/ */
} }
// _Init
void
ScrollView::_Init(BView* child, uint32 scrollingFlags, uint32 borderStyle,
uint32 borderFlags)
{
fChild = NULL;
fScrollingFlags = scrollingFlags;
fHScrollBar = NULL;
fVScrollBar = NULL;
fScrollCorner = NULL;
fHVisible = true;
fVVisible = true;
fCornerVisible = true;
fWindowActive = false;
fChildFocused = false;
fScrolling = false;
fHSmallStep = 1;
fVSmallStep = 1;
fBorderStyle = borderStyle;
fBorderFlags = borderFlags;
// Set transparent view color -- our area is completely covered by
// our children.
SetViewColor(B_TRANSPARENT_32_BIT);
// create scroll bars
if (fScrollingFlags & (SCROLL_HORIZONTAL | SCROLL_HORIZONTAL_MAGIC)) {
fHScrollBar = new InternalScrollBar(this,
BRect(0.0, 0.0, 100.0, B_H_SCROLL_BAR_HEIGHT), B_HORIZONTAL);
AddChild(fHScrollBar);
}
if (fScrollingFlags & (SCROLL_VERTICAL | SCROLL_VERTICAL_MAGIC)) {
fVScrollBar = new InternalScrollBar(this,
BRect(0.0, 0.0, B_V_SCROLL_BAR_WIDTH, 100.0), B_VERTICAL);
AddChild(fVScrollBar);
}
// Create a scroll corner, if we can scroll into both direction.
if (fHScrollBar && fVScrollBar) {
fScrollCorner = new ScrollCorner(this);
AddChild(fScrollCorner);
}
// add child
if (child) {
fChild = child;
AddChild(child);
if (Scrollable* scrollable = dynamic_cast<Scrollable*>(child))
SetScrollTarget(scrollable);
}
}
// _ScrollValueChanged // _ScrollValueChanged
void void
ScrollView::_ScrollValueChanged(InternalScrollBar* scrollBar, float value) ScrollView::_ScrollValueChanged(InternalScrollBar* scrollBar, float value)
{ {
if (!IsScrollingEnabled())
return;
switch (scrollBar->Orientation()) { switch (scrollBar->Orientation()) {
case B_HORIZONTAL: case B_HORIZONTAL:
if (fHScrollBar) if (fHScrollBar)
@ -682,14 +751,14 @@ ScrollView::_UpdateScrollBars()
dataRect.Set(0.0, 0.0, 0.0, 0.0); dataRect.Set(0.0, 0.0, 0.0, 0.0);
visibleBounds.Set(0.0, 0.0, 0.0, 0.0); visibleBounds.Set(0.0, 0.0, 0.0, 0.0);
} }
float hProportion = min(1.0f, (visibleBounds.Width() + 1.0f) / float hProportion = min_c(1.0f, (visibleBounds.Width() + 1.0f)
(dataRect.Width() + 1.0f)); / (dataRect.Width() + 1.0f));
float hMaxValue = max(dataRect.left, float hMaxValue = max_c(dataRect.left,
dataRect.right - visibleBounds.Width()); dataRect.right - visibleBounds.Width());
float vProportion = min(1.0f, (visibleBounds.Height() + 1.0f) / float vProportion = min_c(1.0f, (visibleBounds.Height() + 1.0f)
(dataRect.Height() + 1.0f)); / (dataRect.Height() + 1.0f));
float vMaxValue = max(dataRect.top, float vMaxValue = max_c(dataRect.top,
dataRect.bottom - visibleBounds.Height()); dataRect.bottom - visibleBounds.Height());
// update horizontal scroll bar // update horizontal scroll bar
if (fHScrollBar) { if (fHScrollBar) {
fHScrollBar->SetProportion(hProportion); fHScrollBar->SetProportion(hProportion);
@ -894,6 +963,7 @@ ScrollView::_MaxVisibleRect() const
} }
#ifdef __HAIKU__ #ifdef __HAIKU__
BSize BSize
ScrollView::_Size(BSize size) ScrollView::_Size(BSize size)
{ {
@ -939,5 +1009,12 @@ ScrollView::_Size(BSize size)
return BLayoutUtils::ComposeSize(ExplicitMinSize(), size); return BLayoutUtils::ComposeSize(ExplicitMinSize(), size);
} }
#endif // __HAIKU__ #endif // __HAIKU__
// _SetScrolling
void
ScrollView::_SetScrolling(bool scrolling)
{
fScrolling = scrolling;
}

View File

@ -1,11 +1,4 @@
/* // ScrollView.h
* Copyright 2006, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
* Ingo Weinhold <bonefish@cs.tu-berlin.de>
*/
#ifndef SCROLL_VIEW_H #ifndef SCROLL_VIEW_H
#define SCROLL_VIEW_H #define SCROLL_VIEW_H
@ -23,7 +16,9 @@ enum {
SCROLL_VERTICAL = 0x02, SCROLL_VERTICAL = 0x02,
SCROLL_HORIZONTAL_MAGIC = 0x04, SCROLL_HORIZONTAL_MAGIC = 0x04,
SCROLL_VERTICAL_MAGIC = 0x08, SCROLL_VERTICAL_MAGIC = 0x08,
SCROLL_VISIBLE_RECT_IS_CHILD_BOUNDS = 0x10 SCROLL_VISIBLE_RECT_IS_CHILD_BOUNDS = 0x10,
SCROLL_NO_FRAME = 0x20,
SCROLL_LIST_FRAME = 0x40,
}; };
enum { enum {
@ -36,12 +31,18 @@ enum {
class ScrollView : public BView, public Scroller { class ScrollView : public BView, public Scroller {
public: public:
ScrollView(BView* child, uint32 scrollingFlags, ScrollView(BView* child, uint32 scrollingFlags,
BRect frame, const char *name, BRect frame, const char *name,
uint32 resizingMode, uint32 viewFlags, uint32 resizingMode, uint32 viewFlags,
uint32 borderStyle = B_FANCY_BORDER, uint32 borderStyle = B_FANCY_BORDER,
uint32 borderFlags = BORDER_ALL); uint32 borderFlags = BORDER_ALL);
#ifdef __HAIKU__
ScrollView(BView* child, uint32 scrollingFlags,
const char *name, uint32 viewFlags,
uint32 borderStyle = B_FANCY_BORDER,
uint32 borderFlags = BORDER_ALL);
#endif // __HAIKU__
virtual ~ScrollView(); virtual ~ScrollView();
virtual void AllAttached(); virtual void AllAttached();
@ -50,10 +51,11 @@ class ScrollView : public BView, public Scroller {
virtual void WindowActivated(bool activated); virtual void WindowActivated(bool activated);
#ifdef __HAIKU__ #ifdef __HAIKU__
virtual BSize MinSize(); virtual BSize MinSize();
virtual BSize PreferredSize(); virtual BSize PreferredSize();
#endif
#endif // __HAIKU__
uint32 ScrollingFlags() const; uint32 ScrollingFlags() const;
void SetVisibleRectIsChildBounds(bool flag); void SetVisibleRectIsChildBounds(bool flag);
@ -74,6 +76,9 @@ class ScrollView : public BView, public Scroller {
float HSmallStep() const; float HSmallStep() const;
float VSmallStep() const; float VSmallStep() const;
virtual bool IsScrolling() const;
virtual void SetScrollingEnabled(bool enabled);
protected: protected:
virtual void DataRectChanged(BRect oldDataRect, virtual void DataRectChanged(BRect oldDataRect,
BRect newDataRect); BRect newDataRect);
@ -97,12 +102,16 @@ class ScrollView : public BView, public Scroller {
bool fCornerVisible; // scroll corner visible flag bool fCornerVisible; // scroll corner visible flag
bool fWindowActive; bool fWindowActive;
bool fChildFocused; bool fChildFocused;
bool fScrolling;
float fHSmallStep; float fHSmallStep;
float fVSmallStep; float fVSmallStep;
uint32 fBorderStyle; uint32 fBorderStyle;
uint32 fBorderFlags; uint32 fBorderFlags;
void _Init(BView* child, uint32 scrollingFlags,
uint32 borderStyle, uint32 borderFlags);
void _ScrollValueChanged( void _ScrollValueChanged(
InternalScrollBar* scrollBar, InternalScrollBar* scrollBar,
float value); float value);
@ -124,6 +133,8 @@ private:
virtual BSize _Size(BSize childSize); virtual BSize _Size(BSize childSize);
#endif #endif
void _SetScrolling(bool scrolling);
friend class InternalScrollBar; friend class InternalScrollBar;
friend class ScrollCorner; friend class ScrollCorner;
}; };

View File

@ -17,7 +17,8 @@
// constructor // constructor
Scroller::Scroller() Scroller::Scroller()
: fScrollTarget(NULL) : fScrollTarget(NULL),
fScrollingEnabled(true)
{ {
} }
@ -135,6 +136,19 @@ Scroller::VisibleRect() const
return BRect(); return BRect();
} }
// SetScrollingEnabled
void
Scroller::SetScrollingEnabled(bool enabled)
{
fScrollingEnabled = enabled;
}
// IsScrolling
bool
Scroller::IsScrolling() const
{
return false;
}
// hooks // hooks

View File

@ -5,7 +5,6 @@
* Authors: * Authors:
* Ingo Weinhold <bonefish@cs.tu-berlin.de> * Ingo Weinhold <bonefish@cs.tu-berlin.de>
*/ */
#ifndef SCROLLER_H #ifndef SCROLLER_H
#define SCROLLER_H #define SCROLLER_H
@ -31,6 +30,12 @@ class Scroller {
BRect VisibleBounds() const; BRect VisibleBounds() const;
BRect VisibleRect() const; BRect VisibleRect() const;
virtual void SetScrollingEnabled(bool enabled);
bool IsScrollingEnabled() const
{ return fScrollingEnabled; }
virtual bool IsScrolling() const;
protected: protected:
virtual void DataRectChanged(BRect oldDataRect, virtual void DataRectChanged(BRect oldDataRect,
BRect newDataRect); BRect newDataRect);
@ -45,6 +50,7 @@ protected:
protected: protected:
Scrollable* fScrollTarget; Scrollable* fScrollTarget;
bool fScrollingEnabled;
friend class Scrollable; friend class Scrollable;
}; };