-
Notifications
You must be signed in to change notification settings - Fork 2
/
layout.h
246 lines (197 loc) · 5.6 KB
/
layout.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright (C) 2018 Mark Hills <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
/*
* Layout functions for use by low-level UI code
*/
#ifndef LAYOUT_H
#define LAYOUT_H
#define LAYOUT_VERTICAL 0x01
#define LAYOUT_SECONDARY 0x02
#define LAYOUT_PIXELS 0x04 /* default is our own screen units */
typedef signed short pix_t;
/*
* A rectangle defines an on-screen area, and other attributes
* for anything that gets into the area
*/
struct rect {
pix_t x, y, w, h; /* pixels */
float scale;
};
struct layout {
unsigned char flags;
float portion;
unsigned int distance, space; /* may be pixels, or units */
};
/*
* Helper function to make layout specs
*/
static inline struct layout absolute(unsigned char flags, unsigned int distance,
unsigned int space)
{
struct layout l;
l.flags = flags;
l.portion = 0.0;
l.distance = distance;
l.space = space;
return l;
}
static inline struct layout from_left(unsigned int distance,
unsigned int space)
{
return absolute(0, distance, space);
}
static inline struct layout from_right(unsigned int distance,
unsigned int space)
{
return absolute(LAYOUT_SECONDARY, distance, space);
}
static inline struct layout from_top(unsigned int distance,
unsigned int space)
{
return absolute(LAYOUT_VERTICAL, distance, space);
}
static inline struct layout from_bottom(unsigned int distance,
unsigned int space)
{
return absolute(LAYOUT_VERTICAL | LAYOUT_SECONDARY, distance, space);
}
static inline struct layout portion(unsigned char flags, double f,
unsigned int space)
{
struct layout l;
l.flags = flags;
l.portion = f;
l.distance = 0;
l.space = space;
return l;
}
static inline struct layout columns(unsigned int n, unsigned int total,
unsigned int space)
{
assert(n < total);
return portion(0, 1.0 / (total - n), space);
}
static inline struct layout rows(unsigned int n, unsigned int total,
unsigned int space)
{
assert(n < total);
return portion(LAYOUT_VERTICAL, 1.0 / (total - n), space);
}
/*
* Take an existing layout spec and request that it be in pixels
*
* Most dimensions are done in terms of 'screen units' but sometimes
* we need to apply layout based on a pixel measurement (eg. returned
* to us when drawing text)
*/
static inline struct layout pixels(struct layout j)
{
struct layout r;
r = j;
r.flags |= LAYOUT_PIXELS;
return r;
}
/*
* Create a new rectangle from pixels
*/
static inline struct rect rect(pix_t x, pix_t y, pix_t w, pix_t h, float scale)
{
struct rect r;
r.x = x;
r.y = y;
r.w = w;
r.h = h;
r.scale = scale;
return r;
}
/*
* Apply a layout request to split two rectangles
*
* The caller is allowed to use the same rectangle for output
* as is the input.
*/
static inline void split(const struct rect x, const struct layout spec,
struct rect *a, struct rect *b)
{
unsigned char flags;
signed short p, q, full, distance, space;
struct rect discard, in;
in = x; /* allow caller to re-use x as an output */
if (!a)
a = &discard;
if (!b)
b = &discard;
flags = spec.flags;
if (flags & LAYOUT_VERTICAL)
full = in.h;
else
full = in.w;
if (flags & LAYOUT_PIXELS) {
space = spec.space;
distance = spec.distance;
} else {
space = spec.space * in.scale;
distance = spec.distance * in.scale;
}
if (spec.portion != 0.0)
distance = spec.portion * full - space / 2;
if (flags & LAYOUT_SECONDARY) {
p = full - distance - space;
q = full - distance;
} else {
p = distance;
q = distance + space;
}
if (flags & LAYOUT_VERTICAL) {
*a = rect(in.x, in.y, in.w, p, in.scale);
*b = rect(in.x, in.y + q, in.w, in.h - q, in.scale);
} else {
*a = rect(in.x, in.y, p, in.h, in.scale);
*b = rect(in.x + q, in.y, in.w - q, in.h, in.scale);
}
}
/*
* Shrink a rectangle to leave a border on all sides
*/
static inline struct rect shrink(const struct rect in, int distance)
{
struct rect out;
out = in;
distance *= in.scale;
if (distance * 2 < in.w) {
out.x = in.x + distance;
out.w = in.w - distance * 2;
}
if (distance * 2 < in.h) {
out.y = in.y + distance;
out.h = in.h - distance * 2;
}
return out;
}
/*
* Calculate the number of lines we can expect to fit if we
* do splits of the given row height
*/
static inline unsigned int count_rows(struct rect in, unsigned int row_height)
{
unsigned int pixels;
pixels = row_height * in.scale;
return in.h / pixels;
}
#endif