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