rich_text support typecheck

This commit is contained in:
xianjimli 2019-02-26 14:20:33 +08:00
parent df742617cf
commit b3688170ce
3 changed files with 42 additions and 10 deletions

25
src/ext_widgets/rich_text/rich_text.c Executable file → Normal file
View File

@ -181,17 +181,18 @@ static ret_t rich_text_on_destroy(widget_t* widget) {
}
static const char* s_rich_text_clone_properties[] = {NULL};
static const widget_vtable_t s_rich_text_vtable = {.size = sizeof(rich_text_t),
.type = "rich_text",
.create = rich_text_create,
.clone_properties = s_rich_text_clone_properties,
.on_event = rich_text_on_event,
.set_prop = rich_text_set_prop,
.on_destroy = rich_text_on_destroy,
.on_paint_self = rich_text_on_paint_self};
TK_DECL_VTABLE(rich_text) = {.size = sizeof(rich_text_t),
.type = "rich_text",
.parent = TK_PARENT_VTABLE(widget),
.create = rich_text_create,
.clone_properties = s_rich_text_clone_properties,
.on_event = rich_text_on_event,
.set_prop = rich_text_set_prop,
.on_destroy = rich_text_on_destroy,
.on_paint_self = rich_text_on_paint_self};
widget_t* rich_text_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
return widget_create(parent, &s_rich_text_vtable, x, y, w, h);
return widget_create(parent, TK_REF_VTABLE(rich_text), x, y, w, h);
}
ret_t rich_text_set_text(widget_t* widget, const char* text) {
@ -203,3 +204,9 @@ ret_t rich_text_set_text(widget_t* widget, const char* text) {
return RET_OK;
}
widget_t* rich_text_cast(widget_t* widget) {
return_value_if_fail(WIDGET_IS_INSTANCE_OF(widget, rich_text), NULL);
return widget;
}

View File

@ -121,7 +121,20 @@ widget_t* rich_text_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h);
*/
ret_t rich_text_set_text(widget_t* widget, const char* text);
#define RICH_TEXT(widget) ((rich_text_t*)(widget))
/**
* @method rich_text_cast
* rich_text对象(使)
* @annotation ["cast", "scriptable"]
* @param {widget_t*} widget rich_text对象
*
* @return {widget_t*} rich_text对象
*/
widget_t* rich_text_cast(widget_t* widget);
#define RICH_TEXT(widget) ((rich_text_t*)(rich_text_cast(WIDGET(widget))))
/*public for subclass and runtime type check*/
TK_EXTERN_VTABLE(rich_text);
END_C_DECLS

12
tests/rich_text_test.cc Normal file
View File

@ -0,0 +1,12 @@
#include "widgets/window.h"
#include "rich_text/rich_text.h"
#include "gtest/gtest.h"
TEST(RichText, cast) {
widget_t* w = window_create(NULL, 0, 0, 0, 0);
widget_t* rich_text = rich_text_create(w, 0, 0, 100, 100);
ASSERT_EQ(rich_text, rich_text_cast(rich_text));
widget_destroy(w);
}