/** 解除事件连接 */ int $(Unbind)( LCUI_EventBox box, int handler_id ) { int i, n; LCUI_RBTreeNode *node; LCUI_EventSlot *slot; LCUI_EventHandler *handler; if( !(node = RBTree_Search(&box->event_handler, handler_id)) ) { return -1; } if( !(node = RBTree_Search(&box->event_slot, (int)(node->data))) ) { return -2; } slot = (LCUI_EventSlot*)node->data; LinkedList_Goto( &slot->handlers, 0 ); n = LinkedList_GetTotal( &slot->handlers ); for( i=0; i<n; ++i ) { handler = (LCUI_EventHandler*)LinkedList_Get( &slot->handlers ); if( handler->id == handler_id ) { LinkedList_Delete( &slot->handlers ); RBTree_Erase( &box->event_handler, handler_id ); return 0; } LinkedList_ToNext( &slot->handlers ); } return -3; }
/** 直接将事件发送至事件处理器进行处理 */ int $(Send)( LCUI_EventBox box, const char *name, void *data ) { int i, n; LCUI_Event event; LCUI_RBTreeNode *node; LCUI_EventSlot *slot; LCUI_EventHandler *handler; if( !(node = RBTree_CustomSearch(&box->event_name, (const void*)name)) ) { return -1; } if( !(node = RBTree_Search(&box->event_slot, node->key)) ) { return -2; } slot = (LCUI_EventSlot*)node->data; event.id = slot->id; event.name = slot->name; event.data = data; event.destroy_data = NULL; n = LinkedList_GetTotal( &slot->handlers ); LinkedList_Goto( &slot->handlers, 0 ); for( i=0; i<n; ++i ) { handler = (LCUI_EventHandler*)LinkedList_Get( &slot->handlers ); handler->func( &event, handler->func_data ); LinkedList_ToNext( &slot->handlers ); } return 0; }
/** 检测事件ID是否已经存在 */ int $(IsExistEventId)( LCUI_EventBox box, int id ) { if( RBTree_Search( &box->event_slot, id ) ) { return 1; } return 0; }
static int $($RegisterEvent)( LCUI_EventBox box, const char *event_name, int id ) { LCUI_RBTreeNode *node; LCUI_EventSlot *slot; /* 查找事件槽记录 */ if( node = RBTree_CustomSearch( &box->event_name, (const void*)event_name ) ) { return -1; } if( node = RBTree_Search( &box->used_evnet_id, id ) ) { return -2; } /* 新建一个事件槽 */ slot = NEW_ONE(LCUI_EventSlot); slot->name = (char*)malloc(sizeof(char)*(strlen(event_name)+1)); slot->id = id; strcpy( slot->name, event_name ); LinkedList_Init( &slot->handlers, sizeof(LCUI_EventHandler) ); /* 添加事件槽记录 */ RBTree_Insert( &box->event_slot, slot->id, slot ); /* 添加事件名记录 */ node = RBTree_CustomInsert( &box->event_name, (const void*)event_name, &slot->name ); /* 结点的 key 就是事件槽的 id */ node->key = slot->id; return 0; }
/** 绑定指定ID的事件 */ int $(BindById)( LCUI_EventBox box, int event_id, EventCallBack func, void *func_data, void (*destroy_data)(void*) ) { LCUI_RBTreeNode *node; LCUI_EventSlot *slot; LCUI_EventHandler *handler; void *data; node = RBTree_Search( &box->event_slot, event_id ); if( !node ) { return -1; } slot = (LCUI_EventSlot*)node->data; handler = NEW_ONE(LCUI_EventHandler); DEBUG_MSG("eventbox: %p, handler_id: %d\n", box, box->handler_id); handler->id = ++box->handler_id; handler->func = func; handler->func_data = func_data; handler->destroy_data = destroy_data; LinkedList_Append( &slot->handlers, handler ); /* 将int类型的值转换成void×类型的值 */ data = &slot->id; data = *(void**)data; RBTree_Insert( &box->event_handler, handler->id, data ); return handler->id; }
/** 获取指定事件ID的名称 */ const char *$(GetEventName)( LCUI_EventBox box, int id ) { LCUI_RBTreeNode *node; if( node = RBTree_Search( &box->event_slot, id ) ) { return (const char*)node->data; } return NULL; }
/** 注册事件,指定事件名称和ID */ int $(RegisterEventWithId)( LCUI_EventBox box, const char *event_name, int id ) { /** 如果该ID已经被使用 */ if( RBTree_Search( &box->used_evnet_id, id ) ) { return -1; } RBTree_Insert( &box->used_evnet_id, id, NULL ); return $($RegisterEvent)( box, event_name, id ); }
/** 将事件投递给事件处理器,等待处理 */ int $(Post)( LCUI_EventBox box, const char *name, void *data, void (*destroy_data)(void*) ) { LCUI_Event event; LCUI_RBTreeNode *node; LCUI_EventSlot *slot; LinkedList *elist = &box->events[box->current]; if( !(node = RBTree_CustomSearch(&box->event_name, (const void*)name)) ) { return -1; } if( !(node = RBTree_Search(&box->event_slot, node->key)) ) { return -2; } slot = (LCUI_EventSlot*)node->data; event.id = slot->id; event.name =slot->name; event.data = data; event.destroy_data = destroy_data; LinkedList_AddDataCopy( elist, &event ); return 0; }
/** 绑定指定ID的事件 */ int $(BindById)( LCUI_EventBox box, int event_id, EventCallBack func, void *func_data, void (*destroy_data)(void*) ) { LCUI_RBTreeNode *node; LCUI_EventSlot *slot; LCUI_EventHandler *handler; node = RBTree_Search( &box->event_slot, event_id ); if( !node ) { return -1; } slot = (LCUI_EventSlot*)node->data; handler = NEW_ONE(LCUI_EventHandler); handler->id = ++box->handler_id; handler->func = func; handler->func_data = func_data; handler->destroy_data = destroy_data; LinkedList_AddData( &slot->handlers, handler ); RBTree_Insert( &box->event_handler, handler->id, (void*)(slot->id) ); return handler->id; }
/** 将事件投递给事件处理器,等待处理 */ int $(Post)( LCUI_EventBox box, const char *name, void *data, void (*destroy_data)(void*) ) { LCUI_Event *e; LCUI_RBTreeNode *node; LCUI_EventSlot *slot; LinkedList *elist = &box->events[box->current]; DEBUG_MSG("eventbox: %p, handler_id: %d\n", box, box->handler_id); if( !(node = RBTree_CustomSearch(&box->event_name, (const void*)name)) ) { return -1; } if( !(node = RBTree_Search(&box->event_slot, node->key)) ) { return -2; } e = (LCUI_Event*)LinkedList_Alloc( elist ); slot = (LCUI_EventSlot*)node->data; e->id = slot->id; e->name = slot->name; e->data = data; e->destroy_data = destroy_data; return 0; }