diff --git a/src/ext_widgets/guage/guage.c b/src/ext_widgets/guage/guage.c index 0e6f84983..3ea03a4b9 100644 --- a/src/ext_widgets/guage/guage.c +++ b/src/ext_widgets/guage/guage.c @@ -102,18 +102,19 @@ static ret_t guage_on_paint_self(widget_t* widget, canvas_t* c) { return RET_OK; } -static const widget_vtable_t s_guage_vtable = {.size = sizeof(guage_t), - .type = WIDGET_TYPE_GUAGE, - .clone_properties = s_guage_properties, - .persistent_properties = s_guage_properties, - .create = guage_create, - .on_paint_self = guage_on_paint_self, - .set_prop = guage_set_prop, - .get_prop = guage_get_prop, - .on_destroy = guage_on_destroy}; +TK_DECL_VTABLE(guage) = {.size = sizeof(guage_t), + .type = WIDGET_TYPE_GUAGE, + .clone_properties = s_guage_properties, + .persistent_properties = s_guage_properties, + .parent = TK_PARENT_VTABLE(widget), + .create = guage_create, + .on_paint_self = guage_on_paint_self, + .set_prop = guage_set_prop, + .get_prop = guage_get_prop, + .on_destroy = guage_on_destroy}; widget_t* guage_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) { - widget_t* widget = widget_create(parent, &s_guage_vtable, x, y, w, h); + widget_t* widget = widget_create(parent, TK_REF_VTABLE(guage), x, y, w, h); guage_t* guage = GUAGE(widget); return_value_if_fail(guage != NULL, NULL); @@ -124,7 +125,7 @@ widget_t* guage_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) { } widget_t* guage_cast(widget_t* widget) { - return_value_if_fail(widget != NULL && widget->vt == &s_guage_vtable, NULL); + return_value_if_fail(WIDGET_IS_INSTANCE_OF(widget, guage), NULL); return widget; } diff --git a/src/ext_widgets/guage/guage.h b/src/ext_widgets/guage/guage.h index 56d48abf4..7c59a10d1 100644 --- a/src/ext_widgets/guage/guage.h +++ b/src/ext_widgets/guage/guage.h @@ -135,7 +135,10 @@ ret_t guage_set_image(widget_t* widget, const char* name); ret_t guage_set_draw_type(widget_t* widget, image_draw_type_t draw_type); #define WIDGET_TYPE_GUAGE "guage" -#define GUAGE(widget) ((guage_t*)(widget)) +#define GUAGE(widget) ((guage_t*)(guage_cast(WIDGET(widget)))) + +/*public for subclass and runtime type check*/ +TK_EXTERN_VTABLE(guage); END_C_DECLS diff --git a/src/ext_widgets/guage/guage_pointer.c b/src/ext_widgets/guage/guage_pointer.c old mode 100755 new mode 100644 index 15c914469..2d2bd1ff9 --- a/src/ext_widgets/guage/guage_pointer.c +++ b/src/ext_widgets/guage/guage_pointer.c @@ -174,24 +174,24 @@ static ret_t guage_pointer_on_paint_self(widget_t* widget, canvas_t* c) { static const char* s_guage_pointer_properties[] = {GUAGE_POINTER_PROP_ANGLE, WIDGET_PROP_IMAGE, NULL}; -static const widget_vtable_t s_guage_pointer_vtable = { - .size = sizeof(guage_pointer_t), - .type = WIDGET_TYPE_GUAGE_POINTER, - .clone_properties = s_guage_pointer_properties, - .persistent_properties = s_guage_pointer_properties, - .create = guage_pointer_create, - .on_paint_self = guage_pointer_on_paint_self, - .on_paint_background = widget_on_paint_null, - .set_prop = guage_pointer_set_prop, - .get_prop = guage_pointer_get_prop, - .on_destroy = guage_pointer_on_destroy}; +TK_DECL_VTABLE(guage_pointer) = {.size = sizeof(guage_pointer_t), + .type = WIDGET_TYPE_GUAGE_POINTER, + .clone_properties = s_guage_pointer_properties, + .persistent_properties = s_guage_pointer_properties, + .parent = TK_PARENT_VTABLE(widget), + .create = guage_pointer_create, + .on_paint_self = guage_pointer_on_paint_self, + .on_paint_background = widget_on_paint_null, + .set_prop = guage_pointer_set_prop, + .get_prop = guage_pointer_get_prop, + .on_destroy = guage_pointer_on_destroy}; widget_t* guage_pointer_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) { - return widget_create(parent, &s_guage_pointer_vtable, x, y, w, h); + return widget_create(parent, TK_REF_VTABLE(guage_pointer), x, y, w, h); } widget_t* guage_pointer_cast(widget_t* widget) { - return_value_if_fail(widget != NULL && widget->vt == &s_guage_pointer_vtable, NULL); + return_value_if_fail(WIDGET_IS_INSTANCE_OF(widget, guage_pointer), NULL); return widget; } diff --git a/src/ext_widgets/guage/guage_pointer.h b/src/ext_widgets/guage/guage_pointer.h index fb425dde5..8fd96a689 100644 --- a/src/ext_widgets/guage/guage_pointer.h +++ b/src/ext_widgets/guage/guage_pointer.h @@ -130,7 +130,10 @@ ret_t guage_pointer_set_image(widget_t* widget, const char* image); #define GUAGE_POINTER_PROP_ANGLE "angle" #define WIDGET_TYPE_GUAGE_POINTER "guage_pointer" -#define GUAGE_POINTER(widget) ((guage_pointer_t*)(widget)) +#define GUAGE_POINTER(widget) ((guage_pointer_t*)(guage_pointer_cast(WIDGET(widget)))) + +/*public for subclass and runtime type check*/ +TK_EXTERN_VTABLE(guage_pointer); END_C_DECLS diff --git a/tests/guage_pointer_test.cc b/tests/guage_pointer_test.cc new file mode 100644 index 000000000..d43c192e3 --- /dev/null +++ b/tests/guage_pointer_test.cc @@ -0,0 +1,10 @@ +#include "guage/guage_pointer.h" +#include "gtest/gtest.h" + +TEST(GuagePointer, cast) { + widget_t* w = guage_pointer_create(NULL, 0, 0, 100, 100); + + ASSERT_EQ(w, guage_pointer_cast(w)); + + widget_destroy(w); +} diff --git a/tests/guage_test.cc b/tests/guage_test.cc new file mode 100644 index 000000000..72e8e22ec --- /dev/null +++ b/tests/guage_test.cc @@ -0,0 +1,12 @@ +#include "widgets/window.h" +#include "guage/guage.h" +#include "gtest/gtest.h" + +TEST(Guage, cast) { + widget_t* w = window_create(NULL, 0, 0, 0, 0); + widget_t* guage = guage_create(w, 0, 0, 100, 100); + + ASSERT_EQ(guage, guage_cast(guage)); + + widget_destroy(w); +}