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的事件 */ 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; }