xref: /haiku/src/kits/interface/LayoutUtils.cpp (revision 9d6d3fcf5fe8308cd020cecf89dede440346f8c4)
1 /*
2  * Copyright 2006, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include <LayoutUtils.h>
7 
8 
9 // // AddSizesFloat
10 // float
11 // BLayoutUtils::AddSizesFloat(float a, float b)
12 // {
13 // 	float sum = a + b + 1;
14 // 	if (sum >= B_SIZE_UNLIMITED)
15 // 		return B_SIZE_UNLIMITED;
16 //
17 // 	return sum;
18 // }
19 //
20 // // AddSizesFloat
21 // float
22 // BLayoutUtils::AddSizesFloat(float a, float b, float c)
23 // {
24 // 	return AddSizesFloat(AddSizesFloat(a, b), c);
25 // }
26 
27 // AddSizesInt32
28 int32
29 BLayoutUtils::AddSizesInt32(int32 a, int32 b)
30 {
31 	if (a >= B_SIZE_UNLIMITED - b)
32 		return B_SIZE_UNLIMITED;
33 	return a + b;
34 }
35 
36 // AddSizesInt32
37 int32
38 BLayoutUtils::AddSizesInt32(int32 a, int32 b, int32 c)
39 {
40 	return AddSizesInt32(AddSizesInt32(a, b), c);
41 }
42 
43 // AddDistances
44 float
45 BLayoutUtils::AddDistances(float a, float b)
46 {
47 	float sum = a + b + 1;
48 	if (sum >= B_SIZE_UNLIMITED)
49 		return B_SIZE_UNLIMITED;
50 
51 	return sum;
52 }
53 
54 // AddDistances
55 float
56 BLayoutUtils::AddDistances(float a, float b, float c)
57 {
58 	return AddDistances(AddDistances(a, b), c);
59 }
60 
61 // // SubtractSizesFloat
62 // float
63 // BLayoutUtils::SubtractSizesFloat(float a, float b)
64 // {
65 // 	if (a < b)
66 // 		return -1;
67 // 	return a - b - 1;
68 // }
69 
70 // SubtractSizesInt32
71 int32
72 BLayoutUtils::SubtractSizesInt32(int32 a, int32 b)
73 {
74 	if (a < b)
75 		return 0;
76 	return a - b;
77 }
78 
79 // SubtractDistances
80 float
81 BLayoutUtils::SubtractDistances(float a, float b)
82 {
83 	if (a < b)
84 		return -1;
85 	return a - b - 1;
86 }
87 
88 // ComposeSize
89 BSize
90 BLayoutUtils::ComposeSize(BSize size, BSize layoutSize)
91 {
92 	if (!size.IsWidthSet())
93 		size.width = layoutSize.width;
94 	if (!size.IsHeightSet())
95 		size.height = layoutSize.height;
96 
97 	return size;
98 }
99 
100 // ComposeAlignment
101 BAlignment
102 BLayoutUtils::ComposeAlignment(BAlignment alignment, BAlignment layoutAlignment)
103 {
104 	if (!alignment.IsHorizontalSet())
105 		alignment.horizontal = layoutAlignment.horizontal;
106 	if (!alignment.IsVerticalSet())
107 		alignment.vertical = layoutAlignment.vertical;
108 
109 	return alignment;
110 }
111 
112 // AlignInFrame
113 BRect
114 BLayoutUtils::AlignInFrame(BRect frame, BSize maxSize, BAlignment alignment)
115 {
116 	// align according to the given alignment
117 	if (maxSize.width < frame.Width()
118 		&& alignment.horizontal != B_ALIGN_USE_FULL_WIDTH) {
119 		frame.left += (int)((frame.Width() - maxSize.width)
120 			* alignment.RelativeHorizontal());
121 		frame.right = frame.left + maxSize.width;
122 	}
123 	if (maxSize.height < frame.Height()
124 		&& alignment.vertical != B_ALIGN_USE_FULL_HEIGHT) {
125 		frame.top += (int)((frame.Height() - maxSize.height)
126 			* alignment.RelativeVertical());
127 		frame.bottom = frame.top + maxSize.height;
128 	}
129 
130 	return frame;
131 }
132 
133 // AlignInFrame
134 void
135 BLayoutUtils::AlignInFrame(BView* view, BRect frame)
136 {
137 // TODO:...
138 // 	BSize maxSize = view->MaxSize();
139 // 	BAlignment alignment = view->Alignment();
140 //
141 // 	if (view->HasHeightForWidth()) {
142 // 		// The view has height for width, so we do the horizontal alignment
143 // 		// ourselves and restrict the height max constraint respectively.
144 //
145 // 		if (maxSize.width < frame.width
146 // 			&& alignment.horizontal != B_ALIGN_USE_FULL_WIDTH) {
147 // 			frame.x += (int)((frame.width - maxSize.width)
148 // 				* alignment.RelativeHorizontal());
149 // 			frame.width = maxSize.width;
150 // 		}
151 // 		alignment.horizontal = BAlignment.B_ALIGN_USE_FULL_WIDTH;
152 //
153 // 		float minHeight;
154 // 		float maxHeight;
155 // 		float preferredHeight;
156 // 		view->GetHeightForWidth(frame.width, &minHeight, &maxHeight,
157 // 			&preferredHeight);
158 //
159 // 		frame.height = max_c(frame.height, info.min);
160 // 		maxSize.height = minHeight;
161 // 	}
162 //
163 // 	frame = AlignInFrame(frame, maxSize, alignment);
164 // 	view->SetLocation(frame.x, frame.y);
165 // 	view->SetSize(frame.getSize());
166 }
167 
168