xref: /haiku/src/kits/interface/ControlLook.cpp (revision 445d4fd926c569e7b9ae28017da86280aaecbae2)
1 /*
2  * Copyright 2012-2020 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include <ControlLook.h>
7 
8 #include <algorithm>
9 #include <binary_compatibility/Interface.h>
10 
11 
12 namespace BPrivate {
13 
14 
15 BControlLook::BControlLook()
16 	:
17 	fCachedWorkspace(-1)
18 {
19 }
20 
21 
22 BControlLook::~BControlLook()
23 {
24 }
25 
26 
27 float
28 BControlLook::ComposeSpacing(float spacing)
29 {
30 	switch ((int)spacing) {
31 		case B_USE_DEFAULT_SPACING:
32 		case B_USE_ITEM_SPACING:
33 			return be_control_look->DefaultItemSpacing();
34 		case B_USE_HALF_ITEM_SPACING:
35 			return ceilf(be_control_look->DefaultItemSpacing() * 0.5f);
36 		case B_USE_WINDOW_SPACING:
37 			return be_control_look->DefaultItemSpacing();
38 		case B_USE_SMALL_SPACING:
39 			return ceilf(be_control_look->DefaultItemSpacing() * 0.7f);
40 		case B_USE_CORNER_SPACING:
41 			return ceilf(be_control_look->DefaultItemSpacing() * 1.272f);
42 		case B_USE_BIG_SPACING:
43 			return ceilf(be_control_look->DefaultItemSpacing() * 1.8f);
44 
45 		case B_USE_BORDER_SPACING:
46 			return std::max(1.0f, floorf(be_control_look->DefaultItemSpacing() / 11.0f));
47 	}
48 
49 	return spacing;
50 }
51 
52 
53 BSize
54 BControlLook::ComposeIconSize(int32 size)
55 {
56 	float scale = be_plain_font->Size() / 12.0f;
57 	if (scale < 1.0f)
58 		scale = 1.0f;
59 
60 	const int32 scaled = (int32)(size * scale);
61 	return BSize(scaled - 1, scaled - 1);
62 }
63 
64 
65 bool
66 BControlLook::ShouldDraw(BView* view, const BRect& rect, const BRect& updateRect)
67 {
68 	if (!rect.IsValid())
69 		return false;
70 
71 	BPoint points[4];
72 	points[0] = rect.LeftTop();
73 	points[1] = rect.RightBottom();
74 	points[2] = rect.LeftBottom();
75 	points[3] = rect.RightTop();
76 
77 	view->TransformTo(B_VIEW_COORDINATES).Apply(points, 4);
78 
79 	BRect dest;
80 	dest.left = dest.right = points[0].x;
81 	dest.top = dest.bottom = points[0].y;
82 	for (int i = 1; i < 4; i++) {
83 		dest.left = std::min(dest.left, points[i].x);
84 		dest.right = std::max(dest.right, points[i].x);
85 		dest.top = std::min(dest.top, points[i].y);
86 		dest.bottom = std::max(dest.bottom, points[i].y);
87 	}
88 	dest.left = floorf(dest.left);
89 	dest.right = ceilf(dest.right);
90 	dest.top = floorf(dest.top);
91 	dest.bottom = ceilf(dest.bottom);
92 
93 	return dest.Intersects(updateRect);
94 }
95 
96 
97 void
98 BControlLook::DrawLabel(BView* view, const char* label, const BBitmap* icon,
99 	BRect rect, const BRect& updateRect, const rgb_color& base, uint32 flags,
100 	const rgb_color* textColor)
101 {
102 	DrawLabel(view, label, icon, rect, updateRect, base, flags,
103 		DefaultLabelAlignment(), textColor);
104 }
105 
106 
107 void
108 BControlLook::GetInsets(frame_type frameType, background_type backgroundType,
109 	uint32 flags, float& _left, float& _top, float& _right, float& _bottom)
110 {
111 	GetFrameInsets(frameType, flags, _left, _top, _right, _bottom);
112 
113 	float left, top, right, bottom;
114 	GetBackgroundInsets(backgroundType, flags, left, top, right, bottom);
115 
116 	_left += left;
117 	_top += top;
118 	_right += right;
119 	_bottom += bottom;
120 }
121 
122 
123 float
124 BControlLook::GetScrollBarWidth(orientation orientation)
125 {
126 	return ComposeSpacing(B_USE_CORNER_SPACING);
127 }
128 
129 
130 void
131 BControlLook::SetBackgroundInfo(const BMessage& backgroundInfo)
132 {
133 	fBackgroundInfo = backgroundInfo;
134 	fCachedWorkspace = -1;
135 }
136 
137 
138 extern "C" void
139 B_IF_GCC_2(_ReservedControlLook1__Q28BPrivate12BControlLook,
140 		_ZN8BPrivate12BControlLook21_ReservedControlLook1Ev)(
141 	BControlLook* controlLook, BView* view, BRect& rect,
142 	const BRect& updateRect, const rgb_color& base, uint32 flags,
143 	uint32 borders, border_style borderStyle, uint32 side)
144 {
145 	controlLook->DrawTabFrame(view, rect, updateRect, base, flags, borders,
146 		borderStyle, side);
147 }
148 
149 
150 extern "C" void
151 B_IF_GCC_2(_ReservedControlLook2__Q28BPrivate12BControlLook,
152 		_ZN8BPrivate12BControlLook21_ReservedControlLook2Ev)(
153 	BControlLook* controlLook, BView* view, BRect rect,
154 		const BRect& updateRect, const rgb_color& base, uint32 flags,
155 		int32 direction, orientation orientation, bool down)
156 {
157 	controlLook->DrawScrollBarButton(view, rect, updateRect, base, flags,
158 		direction, orientation, down);
159 }
160 
161 
162 extern "C" void
163 B_IF_GCC_2(_ReservedControlLook3__Q28BPrivate12BControlLook,
164 		_ZN8BPrivate12BControlLook21_ReservedControlLook3Ev)(
165 	BControlLook* controlLook, BView* view, BRect rect,
166 		const BRect& updateRect, const rgb_color& base, uint32 flags,
167 		int32 direction, orientation orientation, uint32 knobStyle)
168 {
169 	controlLook->DrawScrollBarThumb(view, rect, updateRect, base, flags,
170 		orientation, knobStyle);
171 }
172 
173 
174 extern "C" void
175 B_IF_GCC_2(_ReservedControlLook4__Q28BPrivate12BControlLook,
176 		_ZN8BPrivate12BControlLook21_ReservedControlLook4Ev)(
177 	BControlLook* controlLook, BView* view, BRect rect,
178 		const BRect& updateRect, const rgb_color& base, uint32 flags,
179 		orientation orientation)
180 {
181 	controlLook->DrawScrollBarBorder(view, rect, updateRect, base, flags,
182 		orientation);
183 }
184 
185 
186 extern "C" float
187 B_IF_GCC_2(_ReservedControlLook5__Q28BPrivate12BControlLook,
188 		_ZN8BPrivate12BControlLook21_ReservedControlLook5Ev)(
189 	BControlLook* controlLook, orientation orientation)
190 {
191 	return controlLook->GetScrollBarWidth(orientation);
192 }
193 
194 
195 void BControlLook::_ReservedControlLook6() {}
196 void BControlLook::_ReservedControlLook7() {}
197 void BControlLook::_ReservedControlLook8() {}
198 void BControlLook::_ReservedControlLook9() {}
199 void BControlLook::_ReservedControlLook10() {}
200 
201 
202 // Initialized in InterfaceDefs.cpp
203 BControlLook* be_control_look = NULL;
204 
205 } // namespace BPrivate
206