/** 直接将事件发送至事件处理器进行处理 */ 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; }
/** 检测事件名是否已经存在(已注册) */ int $(IsExistEventName)( LCUI_EventBox box, const char *event_name ) { if( RBTree_CustomSearch( &box->event_name, (const void*)event_name) ) { 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; }
/** 绑定指定名称的事件 */ int $(Bind)( LCUI_EventBox box, const char *event_name, EventCallBack func, void *func_data, void (*destroy_data)(void*) ) { int id; LCUI_RBTreeNode *node; /* 查找事件槽记录 */ node = RBTree_CustomSearch( &box->event_name, (const void*)event_name ); /** 没有就注册一个事件槽 */ if( !node ) { id = ++box->event_id; if( $(AddEvent)( box, event_name, id ) != 0 ) { return -1; } } else { id = node->key; } return $(BindById)( box, id, func, func_data, destroy_data ); }
/** 将事件投递给事件处理器,等待处理 */ 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; }
/** 将事件投递给事件处理器,等待处理 */ 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; }