1a5b50d1fSIngo Weinhold/*
2a5b50d1fSIngo Weinhold * Copyright 2007, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3a5b50d1fSIngo Weinhold * All rights reserved. Distributed under the terms of the MIT License.
4a5b50d1fSIngo Weinhold */
5a5b50d1fSIngo Weinhold
6a5b50d1fSIngo Weinhold#include "WrapperView.h"
7a5b50d1fSIngo Weinhold
8a5b50d1fSIngo Weinhold#include <LayoutUtils.h>
9a5b50d1fSIngo Weinhold#include <View.h>
10a5b50d1fSIngo Weinhold
11a5b50d1fSIngo Weinhold
12a5b50d1fSIngo WeinholdWrapperView::WrapperView(BView* view)
13a5b50d1fSIngo Weinhold	: View(),
14a5b50d1fSIngo Weinhold	  fView(view),
15a5b50d1fSIngo Weinhold	  fInsets(1, 1, 1, 1)
16a5b50d1fSIngo Weinhold{
17a5b50d1fSIngo Weinhold	SetViewColor((rgb_color){255, 0, 0, 255});
18a5b50d1fSIngo Weinhold}
19a5b50d1fSIngo Weinhold
20a5b50d1fSIngo Weinhold
21a5b50d1fSIngo WeinholdBView*
22a5b50d1fSIngo WeinholdWrapperView::GetView() const
23a5b50d1fSIngo Weinhold{
24a5b50d1fSIngo Weinhold	return fView;
25a5b50d1fSIngo Weinhold}
26a5b50d1fSIngo Weinhold
27a5b50d1fSIngo Weinhold
28a5b50d1fSIngo WeinholdBSize
29a5b50d1fSIngo WeinholdWrapperView::MinSize()
30a5b50d1fSIngo Weinhold{
31a5b50d1fSIngo Weinhold	return _FromViewSize(fView->MinSize());
32a5b50d1fSIngo Weinhold}
33a5b50d1fSIngo Weinhold
34a5b50d1fSIngo Weinhold
35a5b50d1fSIngo WeinholdBSize
36a5b50d1fSIngo WeinholdWrapperView::MaxSize()
37a5b50d1fSIngo Weinhold{
38a5b50d1fSIngo Weinhold	return _FromViewSize(fView->MaxSize());
39a5b50d1fSIngo Weinhold}
40a5b50d1fSIngo Weinhold
41a5b50d1fSIngo Weinhold
42a5b50d1fSIngo WeinholdBSize
43a5b50d1fSIngo WeinholdWrapperView::PreferredSize()
44a5b50d1fSIngo Weinhold{
45a5b50d1fSIngo Weinhold	return _FromViewSize(fView->PreferredSize());
46a5b50d1fSIngo Weinhold}
47a5b50d1fSIngo Weinhold
48a5b50d1fSIngo Weinhold
49a5b50d1fSIngo Weinholdvoid
50a5b50d1fSIngo WeinholdWrapperView::AddedToContainer()
51a5b50d1fSIngo Weinhold{
52a5b50d1fSIngo Weinhold	_UpdateViewFrame();
53a5b50d1fSIngo Weinhold
54a5b50d1fSIngo Weinhold	Container()->AddChild(fView);
55a5b50d1fSIngo Weinhold}
56a5b50d1fSIngo Weinhold
57a5b50d1fSIngo Weinhold
58a5b50d1fSIngo Weinholdvoid
59a5b50d1fSIngo WeinholdWrapperView::RemovingFromContainer()
60a5b50d1fSIngo Weinhold{
61a5b50d1fSIngo Weinhold	Container()->RemoveChild(fView);
62a5b50d1fSIngo Weinhold}
63a5b50d1fSIngo Weinhold
64a5b50d1fSIngo Weinhold
65a5b50d1fSIngo Weinholdvoid
66a5b50d1fSIngo WeinholdWrapperView::FrameChanged(BRect oldFrame, BRect newFrame)
67a5b50d1fSIngo Weinhold{
68a5b50d1fSIngo Weinhold	_UpdateViewFrame();
69a5b50d1fSIngo Weinhold}
70a5b50d1fSIngo Weinhold
71a5b50d1fSIngo Weinhold
72a5b50d1fSIngo Weinholdvoid
73a5b50d1fSIngo WeinholdWrapperView::_UpdateViewFrame()
74a5b50d1fSIngo Weinhold{
75a5b50d1fSIngo Weinhold	BRect frame(_ViewFrameInContainer());
76a5b50d1fSIngo Weinhold	fView->MoveTo(frame.LeftTop());
77a5b50d1fSIngo Weinhold	fView->ResizeTo(frame.Width(), frame.Height());
78a5b50d1fSIngo Weinhold}
79a5b50d1fSIngo Weinhold
80a5b50d1fSIngo Weinhold
81a5b50d1fSIngo WeinholdBRect
82a5b50d1fSIngo WeinholdWrapperView::_ViewFrame() const
83a5b50d1fSIngo Weinhold{
84a5b50d1fSIngo Weinhold	BRect viewFrame(Bounds());
85a5b50d1fSIngo Weinhold	viewFrame.left += fInsets.left;
86a5b50d1fSIngo Weinhold	viewFrame.top += fInsets.top;
87a5b50d1fSIngo Weinhold	viewFrame.right -= fInsets.right;
88a5b50d1fSIngo Weinhold	viewFrame.bottom -= fInsets.bottom;
89a5b50d1fSIngo Weinhold
90a5b50d1fSIngo Weinhold	return viewFrame;
91a5b50d1fSIngo Weinhold}
92a5b50d1fSIngo Weinhold
93a5b50d1fSIngo Weinhold
94a5b50d1fSIngo WeinholdBRect
95a5b50d1fSIngo WeinholdWrapperView::_ViewFrameInContainer() const
96a5b50d1fSIngo Weinhold{
97a5b50d1fSIngo Weinhold	return ConvertToContainer(_ViewFrame());
98a5b50d1fSIngo Weinhold}
99a5b50d1fSIngo Weinhold
100a5b50d1fSIngo Weinhold
101a5b50d1fSIngo WeinholdBSize
102a5b50d1fSIngo WeinholdWrapperView::_FromViewSize(BSize size) const
103a5b50d1fSIngo Weinhold{
104a5b50d1fSIngo Weinhold	float horizontalInsets = fInsets.left + fInsets.right - 1;
105a5b50d1fSIngo Weinhold	float verticalInsets = fInsets.top + fInsets.bottom - 1;
106a5b50d1fSIngo Weinhold	return BSize(BLayoutUtils::AddDistances(size.width, horizontalInsets),
107a5b50d1fSIngo Weinhold		BLayoutUtils::AddDistances(size.height, verticalInsets));
108a5b50d1fSIngo Weinhold}
109