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
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
3538typedef 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
210213static 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
245250void 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