9
9
#include <sys/time.h>
10
10
#include <time.h>
11
11
12
- #include "twin_private.h"
12
+ #include <twin.h>
13
13
14
14
#include "apps_clock.h"
15
15
30
30
#define APPS_CLOCK_BORDER 0xffbababa
31
31
#define APPS_CLOCK_BORDER_WIDTH D(0.01)
32
32
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
+ }
34
37
35
38
typedef struct {
36
- twin_widget_t widget ;
37
39
twin_timeout_t * timeout ;
38
- } apps_clock_t ;
40
+ } apps_clock_data_t ;
39
41
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 )
41
44
{
42
45
twin_fixed_t scale ;
43
46
44
47
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 );
47
50
48
51
twin_path_translate (path , TWIN_FIXED_ONE + APPS_CLOCK_BORDER_WIDTH * 3 ,
49
52
TWIN_FIXED_ONE + APPS_CLOCK_BORDER_WIDTH * 3 );
50
53
51
54
twin_path_rotate (path , - TWIN_ANGLE_90 );
52
55
}
53
56
54
- static void apps_clock_hand (apps_clock_t * clock ,
57
+ static void apps_clock_hand (twin_custom_widget_t * clock ,
55
58
twin_angle_t angle ,
56
59
twin_fixed_t len ,
57
60
twin_fixed_t fill_width ,
@@ -87,7 +90,7 @@ static void apps_clock_hand(apps_clock_t *clock,
87
90
twin_path_destroy (stroke );
88
91
}
89
92
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 )
91
94
{
92
95
char text [7 ];
93
96
twin_text_metrics_t metrics ;
@@ -118,7 +121,7 @@ static twin_angle_t apps_clock_minute_angle(int min)
118
121
return min * TWIN_ANGLE_360 / 60 ;
119
122
}
120
123
121
- static void _apps_clock_face (apps_clock_t * clock )
124
+ static void _apps_clock_face (twin_custom_widget_t * clock )
122
125
{
123
126
twin_path_t * path = twin_path_create ();
124
127
int m ;
@@ -173,7 +176,7 @@ static twin_time_t _apps_clock_interval(void)
173
176
return 1000 - (tv .tv_usec / 1000 );
174
177
}
175
178
176
- static void _apps_clock_paint (apps_clock_t * clock )
179
+ static void _apps_clock_paint (twin_custom_widget_t * clock )
177
180
{
178
181
struct timeval tv ;
179
182
twin_angle_t second_angle , minute_angle , hour_angle ;
@@ -199,47 +202,49 @@ static void _apps_clock_paint(apps_clock_t *clock)
199
202
APPS_CLOCK_SECOND , APPS_CLOCK_SECOND_OUT );
200
203
}
201
204
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 )
204
206
{
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 );
207
210
return _apps_clock_interval ();
208
211
}
209
212
210
213
static twin_dispatch_result_t _apps_clock_dispatch (twin_widget_t * widget ,
211
214
twin_event_t * event )
212
215
{
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 ;
214
219
215
- if (_twin_widget_dispatch (widget , event ) == TwinDispatchDone )
216
- return TwinDispatchDone ;
217
220
switch (event -> kind ) {
218
221
case TwinEventPaint :
219
- _apps_clock_paint (clock );
222
+ _apps_clock_paint (custom );
220
223
break ;
221
224
default :
222
225
break ;
223
226
}
224
227
return TwinDispatchContinue ;
225
228
}
226
229
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 )
230
231
{
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 ;
235
243
}
236
244
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 )
238
246
{
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 );
243
248
}
244
249
245
250
void apps_clock_start (twin_screen_t * screen ,
@@ -251,7 +256,7 @@ void apps_clock_start(twin_screen_t *screen,
251
256
{
252
257
twin_toplevel_t * toplevel = twin_toplevel_create (
253
258
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 );
255
260
(void ) clock ;
256
261
twin_toplevel_show (toplevel );
257
262
}
0 commit comments