Skip to content

Commit c9df0a6

Browse files
authored
Merge pull request #108 from sysprog21/apps-no-private-header
Implement public custom widget API
2 parents f4d5b80 + d36a485 commit c9df0a6

File tree

8 files changed

+624
-172
lines changed

8 files changed

+624
-172
lines changed

apps/animation.c

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@
66

77
#include <stdlib.h>
88

9-
#include "twin_private.h"
9+
#include <twin.h>
1010

1111
#include "apps_animation.h"
1212

13-
#define _apps_animation_pixmap(animation) ((animation)->widget.window->pixmap)
13+
static inline twin_pixmap_t *_apps_animation_pixmap(
14+
twin_custom_widget_t *animation)
15+
{
16+
return twin_custom_widget_pixmap(animation);
17+
}
1418

1519
typedef struct {
16-
twin_widget_t widget;
1720
twin_pixmap_t *pix;
1821
twin_timeout_t *timeout;
19-
} apps_animation_t;
22+
} apps_animation_data_t;
2023

21-
static void _apps_animation_paint(apps_animation_t *anim)
24+
static void _apps_animation_paint(twin_custom_widget_t *custom)
2225
{
26+
apps_animation_data_t *anim =
27+
(apps_animation_data_t *) twin_custom_widget_data(custom);
2328
twin_pixmap_t *current_frame = NULL;
2429

2530
if (twin_pixmap_is_animated(anim->pix)) {
@@ -34,15 +39,17 @@ static void _apps_animation_paint(apps_animation_t *anim)
3439
.source_kind = TWIN_PIXMAP,
3540
.u.pixmap = current_frame,
3641
};
37-
twin_composite(_apps_animation_pixmap(anim), 0, 0, &srcop, 0, 0, NULL, 0, 0,
38-
TWIN_SOURCE, current_frame->width, current_frame->height);
42+
twin_composite(_apps_animation_pixmap(custom), 0, 0, &srcop, 0, 0, NULL, 0,
43+
0, TWIN_SOURCE, current_frame->width, current_frame->height);
3944
}
4045

41-
static twin_time_t _apps_animation_timeout(twin_time_t maybe_unused now,
42-
void *closure)
46+
static twin_time_t _apps_animation_timeout(twin_time_t now, void *closure)
4347
{
44-
apps_animation_t *anim = closure;
45-
_twin_widget_queue_paint(&anim->widget);
48+
(void) now; /* unused parameter */
49+
twin_custom_widget_t *custom = closure;
50+
apps_animation_data_t *anim =
51+
(apps_animation_data_t *) twin_custom_widget_data(custom);
52+
twin_custom_widget_queue_paint(custom);
4653
twin_animation_t *a = anim->pix->animation;
4754
twin_time_t delay = twin_animation_get_current_delay(a);
4855
return delay;
@@ -51,42 +58,49 @@ static twin_time_t _apps_animation_timeout(twin_time_t maybe_unused now,
5158
static twin_dispatch_result_t _apps_animation_dispatch(twin_widget_t *widget,
5259
twin_event_t *event)
5360
{
54-
apps_animation_t *anim = (apps_animation_t *) widget;
55-
if (_twin_widget_dispatch(widget, event) == TwinDispatchDone)
56-
return TwinDispatchDone;
61+
twin_custom_widget_t *custom = twin_widget_get_custom(widget);
62+
if (!custom)
63+
return TwinDispatchContinue;
64+
5765
switch (event->kind) {
5866
case TwinEventPaint:
59-
_apps_animation_paint(anim);
67+
_apps_animation_paint(custom);
6068
break;
6169
default:
6270
break;
6371
}
6472
return TwinDispatchContinue;
6573
}
6674

67-
static void _apps_animation_init(apps_animation_t *anim,
68-
twin_box_t *parent,
69-
twin_dispatch_proc_t dispatch)
75+
static twin_custom_widget_t *_apps_animation_init(twin_box_t *parent,
76+
twin_pixmap_t *pix)
7077
{
71-
static const twin_widget_layout_t preferred = {0, 0, 1, 1};
72-
_twin_widget_init(&anim->widget, parent, 0, preferred, dispatch);
78+
twin_custom_widget_t *custom = twin_custom_widget_create(
79+
parent, 0, 0, 0, 1, 1, _apps_animation_dispatch,
80+
sizeof(apps_animation_data_t));
81+
if (!custom)
82+
return NULL;
83+
84+
apps_animation_data_t *anim =
85+
(apps_animation_data_t *) twin_custom_widget_data(custom);
86+
anim->pix = pix;
7387

7488
if (twin_pixmap_is_animated(anim->pix)) {
7589
twin_animation_t *a = anim->pix->animation;
7690
twin_time_t delay = twin_animation_get_current_delay(a);
77-
anim->timeout = twin_set_timeout(_apps_animation_timeout, delay, anim);
91+
anim->timeout =
92+
twin_set_timeout(_apps_animation_timeout, delay, custom);
7893
} else {
7994
anim->timeout = NULL;
8095
}
96+
97+
return custom;
8198
}
8299

83-
static apps_animation_t *apps_animation_create(twin_box_t *parent,
84-
twin_pixmap_t *pix)
100+
static twin_custom_widget_t *apps_animation_create(twin_box_t *parent,
101+
twin_pixmap_t *pix)
85102
{
86-
apps_animation_t *anim = malloc(sizeof(apps_animation_t));
87-
anim->pix = pix;
88-
_apps_animation_init(anim, parent, _apps_animation_dispatch);
89-
return anim;
103+
return _apps_animation_init(parent, pix);
90104
}
91105

92106
void apps_animation_start(twin_screen_t *screen,
@@ -99,7 +113,7 @@ void apps_animation_start(twin_screen_t *screen,
99113
twin_toplevel_t *toplevel =
100114
twin_toplevel_create(screen, TWIN_ARGB32, TwinWindowApplication, x, y,
101115
pix->width, pix->height, name);
102-
apps_animation_t *anim = apps_animation_create(&toplevel->box, pix);
116+
twin_custom_widget_t *anim = apps_animation_create(&toplevel->box, pix);
103117
(void) anim;
104118
twin_toplevel_show(toplevel);
105119
}

apps/clock.c

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <sys/time.h>
1010
#include <time.h>
1111

12-
#include "twin_private.h"
12+
#include <twin.h>
1313

1414
#include "apps_clock.h"
1515

@@ -30,28 +30,31 @@
3030
#define APPS_CLOCK_BORDER 0xffbababa
3131
#define APPS_CLOCK_BORDER_WIDTH D(0.01)
3232

33-
#define _apps_clock_pixmap(clock) ((clock)->widget.window->pixmap)
33+
static inline twin_pixmap_t *_apps_clock_pixmap(twin_custom_widget_t *clock)
34+
{
35+
return twin_custom_widget_pixmap(clock);
36+
}
3437

3538
typedef struct {
36-
twin_widget_t widget;
3739
twin_timeout_t *timeout;
38-
} apps_clock_t;
40+
} apps_clock_data_t;
3941

40-
static void apps_clock_set_transform(apps_clock_t *clock, twin_path_t *path)
42+
static void apps_clock_set_transform(twin_custom_widget_t *clock,
43+
twin_path_t *path)
4144
{
4245
twin_fixed_t scale;
4346

4447
scale = (TWIN_FIXED_ONE - APPS_CLOCK_BORDER_WIDTH * 3) / 2;
45-
twin_path_scale(path, _twin_widget_width(clock) * scale,
46-
_twin_widget_height(clock) * scale);
48+
twin_path_scale(path, twin_custom_widget_width(clock) * scale,
49+
twin_custom_widget_height(clock) * scale);
4750

4851
twin_path_translate(path, TWIN_FIXED_ONE + APPS_CLOCK_BORDER_WIDTH * 3,
4952
TWIN_FIXED_ONE + APPS_CLOCK_BORDER_WIDTH * 3);
5053

5154
twin_path_rotate(path, -TWIN_ANGLE_90);
5255
}
5356

54-
static void apps_clock_hand(apps_clock_t *clock,
57+
static void apps_clock_hand(twin_custom_widget_t *clock,
5558
twin_angle_t angle,
5659
twin_fixed_t len,
5760
twin_fixed_t fill_width,
@@ -87,7 +90,7 @@ static void apps_clock_hand(apps_clock_t *clock,
8790
twin_path_destroy(stroke);
8891
}
8992

90-
static void _apps_clock_date(apps_clock_t *clock, struct tm *t)
93+
static void _apps_clock_date(twin_custom_widget_t *clock, struct tm *t)
9194
{
9295
char text[7];
9396
twin_text_metrics_t metrics;
@@ -118,7 +121,7 @@ static twin_angle_t apps_clock_minute_angle(int min)
118121
return min * TWIN_ANGLE_360 / 60;
119122
}
120123

121-
static void _apps_clock_face(apps_clock_t *clock)
124+
static void _apps_clock_face(twin_custom_widget_t *clock)
122125
{
123126
twin_path_t *path = twin_path_create();
124127
int m;
@@ -173,7 +176,7 @@ static twin_time_t _apps_clock_interval(void)
173176
return 1000 - (tv.tv_usec / 1000);
174177
}
175178

176-
static void _apps_clock_paint(apps_clock_t *clock)
179+
static void _apps_clock_paint(twin_custom_widget_t *clock)
177180
{
178181
struct timeval tv;
179182
twin_angle_t second_angle, minute_angle, hour_angle;
@@ -199,47 +202,49 @@ static void _apps_clock_paint(apps_clock_t *clock)
199202
APPS_CLOCK_SECOND, APPS_CLOCK_SECOND_OUT);
200203
}
201204

202-
static twin_time_t _apps_clock_timeout(twin_time_t maybe_unused now,
203-
void *closure)
205+
static twin_time_t _apps_clock_timeout(twin_time_t now, void *closure)
204206
{
205-
apps_clock_t *clock = closure;
206-
_twin_widget_queue_paint(&clock->widget);
207+
(void) now; /* unused parameter */
208+
twin_custom_widget_t *clock = closure;
209+
twin_custom_widget_queue_paint(clock);
207210
return _apps_clock_interval();
208211
}
209212

210213
static twin_dispatch_result_t _apps_clock_dispatch(twin_widget_t *widget,
211214
twin_event_t *event)
212215
{
213-
apps_clock_t *clock = (apps_clock_t *) widget;
216+
twin_custom_widget_t *custom = twin_widget_get_custom(widget);
217+
if (!custom)
218+
return TwinDispatchContinue;
214219

215-
if (_twin_widget_dispatch(widget, event) == TwinDispatchDone)
216-
return TwinDispatchDone;
217220
switch (event->kind) {
218221
case TwinEventPaint:
219-
_apps_clock_paint(clock);
222+
_apps_clock_paint(custom);
220223
break;
221224
default:
222225
break;
223226
}
224227
return TwinDispatchContinue;
225228
}
226229

227-
static void _apps_clock_init(apps_clock_t *clock,
228-
twin_box_t *parent,
229-
twin_dispatch_proc_t dispatch)
230+
static twin_custom_widget_t *_apps_clock_init(twin_box_t *parent)
230231
{
231-
static const twin_widget_layout_t preferred = {0, 0, 1, 1};
232-
_twin_widget_init(&clock->widget, parent, 0, preferred, dispatch);
233-
clock->timeout =
234-
twin_set_timeout(_apps_clock_timeout, _apps_clock_interval(), clock);
232+
twin_custom_widget_t *custom = twin_custom_widget_create(
233+
parent, 0, 0, 0, 1, 1, _apps_clock_dispatch, sizeof(apps_clock_data_t));
234+
if (!custom)
235+
return NULL;
236+
237+
apps_clock_data_t *data =
238+
(apps_clock_data_t *) twin_custom_widget_data(custom);
239+
data->timeout =
240+
twin_set_timeout(_apps_clock_timeout, _apps_clock_interval(), custom);
241+
242+
return custom;
235243
}
236244

237-
static apps_clock_t *apps_clock_create(twin_box_t *parent)
245+
static twin_custom_widget_t *apps_clock_create(twin_box_t *parent)
238246
{
239-
apps_clock_t *clock = malloc(sizeof(apps_clock_t));
240-
241-
_apps_clock_init(clock, parent, _apps_clock_dispatch);
242-
return clock;
247+
return _apps_clock_init(parent);
243248
}
244249

245250
void apps_clock_start(twin_screen_t *screen,
@@ -251,7 +256,7 @@ void apps_clock_start(twin_screen_t *screen,
251256
{
252257
twin_toplevel_t *toplevel = twin_toplevel_create(
253258
screen, TWIN_ARGB32, TwinWindowApplication, x, y, w, h, name);
254-
apps_clock_t *clock = apps_clock_create(&toplevel->box);
259+
twin_custom_widget_t *clock = apps_clock_create(&toplevel->box);
255260
(void) clock;
256261
twin_toplevel_show(toplevel);
257262
}

0 commit comments

Comments
 (0)