-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathwidget.c
240 lines (216 loc) · 7.17 KB
/
widget.c
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
/*
* Twin - A Tiny Window System
* Copyright (c) 2004 Keith Packard <[email protected]>
* Copyright (c) 2024-2025 National Cheng Kung University, Taiwan
* All rights reserved.
*/
#include <stdlib.h>
#include "twin_private.h"
static twin_path_t *_twin_path_shape(twin_shape_t shape,
twin_coord_t left,
twin_coord_t top,
twin_coord_t right,
twin_coord_t bottom,
twin_fixed_t radius)
{
twin_path_t *path = twin_path_create();
if (!path)
return NULL;
twin_fixed_t x = twin_int_to_fixed(left);
twin_fixed_t y = twin_int_to_fixed(top);
twin_fixed_t w = twin_int_to_fixed(right - left);
twin_fixed_t h = twin_int_to_fixed(bottom - top);
switch (shape) {
case TwinShapeRectangle:
twin_path_rectangle(path, x, y, w, h);
break;
case TwinShapeRoundedRectangle:
twin_path_rounded_rectangle(path, x, h, w, y, radius, radius);
break;
case TwinShapeLozenge:
twin_path_lozenge(path, x, y, w, h);
break;
case TwinShapeTab:
twin_path_tab(path, x, y, w, h, radius, radius);
break;
case TwinShapeEllipse:
twin_path_ellipse(path, x + w / 2, y + h / 2, w / 2, h / 2);
break;
}
return path;
}
void _twin_widget_paint_shape(twin_widget_t *widget,
twin_shape_t shape,
twin_coord_t left,
twin_coord_t top,
twin_coord_t right,
twin_coord_t bottom,
twin_fixed_t radius)
{
twin_pixmap_t *pixmap = widget->window->pixmap;
if (shape == TwinShapeRectangle)
twin_fill(pixmap, widget->background, TWIN_SOURCE, left, top, right,
bottom);
else {
twin_path_t *path =
_twin_path_shape(shape, left, top, right, bottom, radius);
if (path) {
twin_paint_path(pixmap, widget->background, path);
twin_path_destroy(path);
}
}
}
static void _twin_widget_paint(twin_widget_t *widget)
{
_twin_widget_paint_shape(widget, widget->shape, 0, 0,
_twin_widget_width(widget),
_twin_widget_height(widget), widget->radius);
}
twin_dispatch_result_t _twin_widget_dispatch(twin_widget_t *widget,
twin_event_t *event)
{
switch (event->kind) {
case TwinEventQueryGeometry:
widget->layout = false;
if (widget->copy_geom) {
twin_widget_t *copy = widget->copy_geom;
if (copy->layout)
(*copy->dispatch)(copy, event);
widget->preferred = copy->preferred;
return TwinDispatchDone;
}
break;
case TwinEventConfigure:
widget->extents = event->u.configure.extents;
break;
case TwinEventPaint:
_twin_widget_paint(widget);
widget->paint = false;
break;
default:
break;
}
return TwinDispatchContinue;
}
void _twin_widget_init(twin_widget_t *widget,
twin_box_t *parent,
twin_window_t *window,
twin_widget_layout_t preferred,
twin_dispatch_proc_t dispatch)
{
if (parent) {
twin_widget_t **prev;
for (prev = &parent->children; *prev; prev = &(*prev)->next)
;
widget->next = *prev;
*prev = widget;
window = parent->widget.window;
} else
widget->next = NULL;
widget->window = window;
widget->parent = parent;
widget->copy_geom = NULL;
widget->paint = true;
widget->layout = true;
widget->want_focus = false;
widget->background = 0x00000000;
widget->extents.left = widget->extents.top = 0;
widget->extents.right = widget->extents.bottom = 0;
widget->preferred = preferred;
widget->dispatch = dispatch;
widget->shape = TwinShapeRectangle;
widget->radius = twin_int_to_fixed(12);
}
void _twin_widget_queue_paint(twin_widget_t *widget)
{
while (widget->parent) {
if (widget->paint)
return;
widget->paint = true;
widget = &widget->parent->widget;
}
_twin_toplevel_queue_paint(widget);
}
void _twin_widget_queue_layout(twin_widget_t *widget)
{
while (widget->parent) {
if (widget->layout)
return;
widget->layout = true;
widget->paint = true;
widget = &widget->parent->widget;
}
_twin_toplevel_queue_layout(widget);
}
bool _twin_widget_contains(twin_widget_t *widget,
twin_coord_t x,
twin_coord_t y)
{
return (0 <= x && x < _twin_widget_width(widget) && 0 <= y &&
y < _twin_widget_height(widget));
}
void _twin_widget_bevel(twin_widget_t *widget, twin_fixed_t b, bool down)
{
twin_path_t *path = twin_path_create();
twin_fixed_t w = twin_int_to_fixed(_twin_widget_width(widget));
twin_fixed_t h = twin_int_to_fixed(_twin_widget_height(widget));
twin_argb32_t top_color, bot_color;
twin_pixmap_t *pixmap = widget->window->pixmap;
if (path) {
if (down) {
top_color = 0x80000000;
bot_color = 0x80808080;
} else {
top_color = 0x80808080;
bot_color = 0x80000000;
}
twin_path_move(path, 0, 0);
twin_path_draw(path, w, 0);
twin_path_draw(path, w - b, b);
twin_path_draw(path, b, b);
twin_path_draw(path, b, h - b);
twin_path_draw(path, 0, h);
twin_path_close(path);
twin_paint_path(pixmap, top_color, path);
twin_path_empty(path);
twin_path_move(path, b, h - b);
twin_path_draw(path, w - b, h - b);
twin_path_draw(path, w - b, b);
twin_path_draw(path, w, 0);
twin_path_draw(path, w, h);
twin_path_draw(path, 0, h);
twin_path_close(path);
twin_paint_path(pixmap, bot_color, path);
twin_path_destroy(path);
}
}
void twin_widget_children_paint(twin_box_t *box)
{
for (twin_widget_t *child = box->children; child; child = child->next)
_twin_widget_queue_paint(child);
}
twin_widget_t *twin_widget_create(twin_box_t *parent,
twin_argb32_t background,
twin_coord_t width,
twin_coord_t height,
twin_stretch_t stretch_width,
twin_stretch_t stretch_height)
{
twin_widget_t *widget = malloc(sizeof(twin_widget_t));
if (!widget)
return NULL;
twin_widget_layout_t preferred = {
.width = width,
.height = height,
.stretch_width = stretch_width,
.stretch_height = stretch_height,
};
_twin_widget_init(widget, parent, 0, preferred, _twin_widget_dispatch);
widget->background = background;
return widget;
}
void twin_widget_set(twin_widget_t *widget, twin_argb32_t background)
{
widget->background = background;
_twin_widget_queue_paint(widget);
}