//UiBase.h
#pragma once
#include <list>
namespace dui
{
using std::list;
struct Event
{
int type;
void* sender;
int argl;
char* argu;
};
class IControl{
public:
virtual bool OnCreate(Event* _event)=0;
//virtual bool OnPaint(Event* _event)=0;
};
typedef bool (IControl::*EVENT_PROC)(Event*);
class EventManager
{
public:
bool Notify(EVENT_PROC method,Event* _event);
void AddNotifier(IControl* control);
void RemoveNotifier(IControl* control);
public:
list<IControl*> m_ctrls;
};
class Button:public IControl
{
public:
virtual bool OnCreate(Event* _event);
//virtual bool OnPaint(Event* _event);
};
class Edit:public IControl
{
public:
virtual bool OnCreate(Event* _event);
//virtual bool OnPaint(Event* _event);
};
}
//UiBase.cpp
#include "stdafx.h"
#include "UiBase.h"
using namespace dui;
void EventManager::AddNotifier(IControl* control)
{
m_ctrls.push_back(control);
}
void EventManager::RemoveNotifier(IControl* control)
{
list<IControl*>::iterator iter;
for(iter = m_ctrls.begin();iter!=m_ctrls.end();)
{
if ( *iter == control){
iter = m_ctrls.erase(iter);
}
else{
iter++;
}
}
}
bool EventManager::Notify(EVENT_PROC method,Event* _event)
{
printf("\r\n\r\nEventManager::Notify() Entry\r\n--------------------------------------\r\n");
list<IControl*>::iterator iter;
for(iter = m_ctrls.begin();iter!=m_ctrls.end();iter++)
{
(*iter->*method)(_event);
//if (!(*iter->*method)(_event))
// return false;
}
return true;
}
bool Button::OnCreate(Event* _event){
printf("Button OnCreate() Entry,event.type=%d \r\n",_event->type);
return true;
}
bool Edit::OnCreate(Event* _event){
printf("Edit OnCreate() Entry,event.type=%d \r\n",_event->type);
return true;
}
//Main.cpp
#include "stdafx.h"
#include "UiBase.h"
int _tmain()
{
using namespace dui;
Event _event;
_event.type = 0x1001;
EventManager event_manager;
Button* btn = new Button();
event_manager.AddNotifier(btn);
Edit* edit = new Edit();
event_manager.AddNotifier(edit);
event_manager.Notify(&IControl::OnCreate,&_event);
event_manager.RemoveNotifier(btn);
event_manager.Notify(&IControl::OnCreate,&_event);
system("pause");
return 0;
}