1/*
2 * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5
6
7#include "ScrollableGroupView.h"
8
9#include <ScrollBar.h>
10#include <SpaceLayoutItem.h>
11
12
13ScrollableGroupView::ScrollableGroupView()
14	:
15	BGroupView(B_VERTICAL, 0.0)
16{
17	AddChild(BSpaceLayoutItem::CreateGlue());
18}
19
20
21BSize
22ScrollableGroupView::MinSize()
23{
24	BSize minSize = BGroupView::MinSize();
25	return BSize(minSize.width, 80);
26}
27
28
29void
30ScrollableGroupView::DoLayout()
31{
32	BGroupView::DoLayout();
33
34	BScrollBar* scrollBar = ScrollBar(B_VERTICAL);
35
36	if (scrollBar == NULL)
37		return;
38
39	BRect layoutArea = GroupLayout()->LayoutArea();
40	float layoutHeight = layoutArea.Height();
41	// Min size is not reliable with HasHeightForWidth() children,
42	// since it does not reflect how those children are currently
43	// laid out, but what their theoretical minimum size would be.
44
45	BLayoutItem* lastItem = GroupLayout()->ItemAt(
46		GroupLayout()->CountItems() - 1);
47	if (lastItem != NULL)
48		layoutHeight = lastItem->Frame().bottom;
49
50	float viewHeight = Bounds().Height();
51
52	float max = layoutHeight- viewHeight;
53	scrollBar->SetRange(0, max);
54	if (layoutHeight > 0)
55		scrollBar->SetProportion(viewHeight / layoutHeight);
56	else
57		scrollBar->SetProportion(1);
58}
59