mre下的控件实现(二、Widget基础类)
//widget.h 所有控件的基类 //mre本身只有简单的GDI函数,没有控件,在mre里写应用都 //要自己实现自己的control,这里把control的基类widget实现了。 #ifndef __WIDGET_H__ #define __WIDGET_H__ #include "typedef.h" #define MAX_WIDGET_CHILD 0x20 struct _Widget; typedef struct _Widget Widget; struct _Base; typedef struct _Base Base; enum _ControlType { CONTROL_TYPE_VIEW, CONTROL_TYPE_BUTTON, CONTROL_TYPE_LABEL, CONTROL_TYPE_NR }; typedef enum _ControlType ControlType; typedef int (*OnKeyEventFunc) (Widget* thiz, int event, int keycode); typedef int (*OnPenEventFunc) (Widget* thiz, int event, int x, int y); typedef int (*OnPaintFunc) (Widget* thiz); typedef int (*OnDestroyFunc)(Widget* thiz); typedef struct _Rect { int x_pos; int y_pos; int width; int height; } Rect; struct _Widget { OnKeyEventFunc on_key_event; OnPenEventFunc on_pen_event; OnPaintFunc on_paint; OnDestroyFunc on_destroy; Widget* parent; Widget* child[MAX_WIDGET_CHILD]; Base* base; void* extra; }; static _INLINE_ int widget_on_key_evt(Widget* thiz, int event, int keycode) { if(NULL != thiz) thiz->on_key_event( thiz, event, keycode); } static _INLINE_ int widget_on_pen_evt(Widget* thiz, int event, int x, int y) { if(NULL != thiz) thiz->on_pen_event( thiz, event, x, y); } static _INLINE_ int widget_on_paint(Widget* thiz) { if(NULL != thiz) thiz->on_paint( thiz); } static _INLINE_ int widget_on_destroy(Widget* thiz) { if(NULL != thiz) thiz->on_destroy( thiz); } /* * 函数名:widget_init * 说明:初始化widget,主要是初始化base * 参数说明: */ void widget_init(Widget* thiz, ControlType type, int id, Rect rect, int* layer); void widget_deinit(Widget* thiz); /* * 函数名:widget_add_child * 说明:增加子控件 * 参数说明: */ int widget_add_child(Widget* thiz, Widget* child); /* * 函数名:widget_set_back_color * 说明:设置背景颜色 * 参数说明: */ void widget_set_back_color(Widget* thiz, vm_graphic_color color); /* * 函数名:widget_get_back_color * 说明:获取背景颜色 * 参数说明: */ vm_graphic_color widget_get_back_color(Widget* thiz); /* * 函数名:widget_set_fore_color * 说明:设置前景颜色 * 参数说明: */ void widget_set_fore_color(Widget* thiz, vm_graphic_color color); /* * 函数名:widget_get_fore_color * 说明:获取前景颜色 * 参数说明: */ vm_graphic_color widget_get_fore_color(Widget* thiz); /* * 函数名:widget_set_visible * 说明:设置是否可见 * 参数说明: */ void widget_set_visible(Widget* thiz,int visible); /* * 函数名:widget_is_visible * 说明:获取是否可见 * 参数说明: */ int widget_is_visible(Widget* thiz); /* * 函数名:widget_set_focus * 说明:设置字体大小 * 参数说明: */ void widget_set_focus(Widget* thiz, int focus); /* * 函数名:widget_is_focus * 说明:是否是当前焦点 * 参数说明: */ int widget_is_focus(Widget* thiz); /* * 函数名:widget_get_parent * 说明:获取父窗体 * 参数说明: */ Widget* widget_get_parent(Widget* thiz); /* * 函数名:widget_get_type * 说明:获取窗体类型 * 参数说明: */ ControlType widget_get_type(Widget* thiz); /* * 函数名:widget_is_point_in_area * 说明:判断坐标点是否在控件区域内 * 参数说明: */ int widget_is_point_in_area(Widget* thiz, int x_pos, int y_pos); /* * 函数名:widget_set_layer_index * 说明:设置图层下标 * 参数说明: */ void widget_set_layer_index(Widget* thiz, int layer_index); /* * 函数名:widget_get_layer_index * 说明:获取图层下标 * 参数说明: */ int widget_get_layer_index(Widget* thiz); /* * 函数名:widget_get_layer * 说明:获取图层数组 * 参数说明: */ int* widget_get_layer(Widget* thiz); /* * 函数名:widget_get_x_pos * 说明:获取相对x坐标 * 参数说明: */ int widget_get_x_pos(Widget* thiz); /* * 函数名:widget_get_x_pos_abs * 说明:获取绝对x坐标 * 参数说明: */ int widget_get_x_pos_abs(Widget* thiz); /* * 函数名:widget_get_y_pos * 说明:获取相对y坐标 * 参数说明: */ int widget_get_y_pos(Widget* thiz); /* * 函数名:widget_get_y_pos_abs * 说明:获取绝对y坐标 * 参数说明: */ int widget_get_y_pos_abs(Widget* thiz); /* * 函数名:widget_get_width * 说明:增加窗体宽度 * 参数说明: */ int widget_get_width(Widget* thiz); /* * 函数名:widget_get_height * 说明:获取窗体高度 * 参数说明: */ int widget_get_height(Widget* thiz); #endif /*__WIDGET_H__*/
//widget.c 窗体(控件)公共的接口 #include "widget.h" struct _Base { int id; int offset_x; int offset_y; Rect rect; ControlType type; int tab_order; int visible; int focus; int backdraw; vm_graphic_color fore_color; vm_graphic_color back_color; VMINT *layer; VMINT layer_index; }; void widget_init(Widget* thiz, ControlType type, int id, Rect rect, int* layer) { thiz->base = WBH_MALLOC(sizeof(Base)); thiz->base->rect.x_pos = rect.x_pos; thiz->base->rect.y_pos = rect.y_pos; thiz->base->rect.height = rect.height; thiz->base->rect.width = rect.width; thiz->base->visible = TRUE; thiz->base->layer = layer; thiz->base->type = type; thiz->base->back_color.vm_color_565 = VM_COLOR_WHITE; thiz->base->fore_color.vm_color_565 = VM_COLOR_BLACK; } void widget_deinit(Widget* thiz) { if(NULL != thiz && NULL != thiz->base) WBH_FREE(thiz->base); } Widget* widget_get_parent(Widget* thiz) { if(NULL != thiz) return thiz->parent; } void widget_set_back_color(Widget* thiz, vm_graphic_color color) { if(NULL != thiz && NULL != thiz->base) thiz->base->back_color = color; } vm_graphic_color widget_get_back_color(Widget* thiz) { if(NULL != thiz && NULL != thiz->base) return thiz->base->back_color; } void widget_set_fore_color(Widget* thiz, vm_graphic_color color) { if(NULL != thiz && NULL != thiz->base) thiz->base->fore_color = color; } vm_graphic_color widget_get_fore_color(Widget* thiz) { if(NULL != thiz && NULL != thiz->base) return thiz->base->fore_color; } void widget_set_visible(Widget* thiz,int visible) { if(NULL != thiz && NULL != thiz->base) thiz->base->visible = visible; } int widget_is_visible(Widget* thiz) { int ret = FALSE; if(NULL != thiz && NULL != thiz->base) ret = thiz->base->visible; return ret; } void widget_set_focus(Widget* thiz, int focus) { if(NULL != thiz && NULL != thiz->base) thiz->base->focus = focus; } int widget_is_focus(Widget* thiz) { int ret = FALSE; if(NULL != thiz && NULL != thiz->base) ret = thiz->base->focus; return ret; } ControlType widget_get_type(Widget* thiz) { ControlType ret = CONTROL_TYPE_NR; if(NULL != thiz && NULL != thiz->base) ret = thiz->base->type; return ret; } int widget_is_point_in_area(Widget* thiz, int x_pos, int y_pos) { int ret = FALSE; if(NULL != thiz && NULL != thiz->base) { Rect rect = thiz->base->rect; if(x_pos >= rect.x_pos && x_pos <= rect.x_pos + rect.width && y_pos >= rect.y_pos && y_pos <= rect.y_pos + rect.height) ret = TRUE; } return ret; } void widget_set_layer_index(Widget* thiz, int layer_index) { if(NULL != thiz && NULL != thiz->base) thiz->base->layer_index = layer_index; } int widget_get_layer_index(Widget* thiz) { if(NULL != thiz && NULL != thiz->base) return thiz->base->layer_index; } int* widget_get_layer(Widget* thiz) { int* ret = NULL; if(NULL != thiz && NULL != thiz->base) ret = thiz->base->layer; return ret; } int widget_get_x_pos(Widget* thiz) { int ret = 0; if(NULL != thiz && NULL != thiz->base) ret = thiz->base->rect.x_pos; return ret; } int widget_get_x_pos_abs(Widget* thiz) { int ret = 0; Widget* widget = widget_get_parent(thiz); ret = widget_get_x_pos(thiz); while(NULL != widget) { ret += widget_get_x_pos(widget); widget = widget_get_parent(widget); } return ret; } int widget_get_y_pos(Widget* thiz) { int ret = 0; if(NULL != thiz && NULL != thiz->base) ret = thiz->base->rect.y_pos; return ret; } int widget_get_y_pos_abs(Widget* thiz) { int ret = 0; Widget* widget = widget_get_parent(thiz); ret = widget_get_y_pos(thiz); while(NULL != widget) { ret += widget_get_y_pos(widget); widget = widget_get_parent(widget); } return ret; } int widget_get_width(Widget* thiz) { int ret = 0; if(NULL != thiz && NULL != thiz->base) ret = thiz->base->rect.width; return ret; } int widget_get_height(Widget* thiz) { int ret = 0; if(NULL != thiz && NULL != thiz->base) ret = thiz->base->rect.height; return ret; } int widget_add_child(Widget* thiz, Widget* child) { int i = -1; if(NULL != thiz) { for(i = 0; i < MAX_WIDGET_CHILD; ++i) { if(NULL == thiz->child[i] && NULL != child) { thiz->child[i] = child; child->parent = thiz; break; } } } return i; }
不积跬步无以至千里,不积小流无以成江河。