wxWidgets源码分析(3) - 消息映射表
消息映射表
消息是GUI程序的核心,所有的操作行为均通过消息传递。
静态消息映射表
使用静态EventTable将事件号和处理代码绑定起来,用法示例:
// 声明
class debugWXFrame: public wxFrame
{
DECLARE_EVENT_TABLE()
};
// 实现
BEGIN_EVENT_TABLE(debugWXFrame,wxFrame)
EVT_MENU(ID_MenuUser, debugWXFrame::OnCheckMenu)
END_EVENT_TABLE()
先看下定义, wxDECLARE_EVENT_TABLE用于在当前类中声明一些数据,大部分都是静态数据,另外提供了GetEventTable来访问这个表;
#define DECLARE_EVENT_TABLE() wxDECLARE_EVENT_TABLE();
#define wxDECLARE_EVENT_TABLE() \
private: \
static const wxEventTableEntry sm_eventTableEntries[]; \
protected: \
static const wxEventTable sm_eventTable; \
virtual const wxEventTable* GetEventTable() const; \
static wxEventHashTable sm_eventHashTable; \
virtual wxEventHashTable& GetEventHashTable() const
下面是实现,用于初始化这些静态变量,所有的消息定义都是位于wxBEGIN_EVENT_TABLE和END_EVENT_TABLE之间:
sm_eventTableEntries保存消息映射表,也就是消息ID和消息处理函数关系;- 实现
GetEventTable方法供调用者使用; sm_eventHashTable用于保存HashTable,查找速率会提升;
#define BEGIN_EVENT_TABLE(a,b) wxBEGIN_EVENT_TABLE(a,b)
#define END_EVENT_TABLE() wxEND_EVENT_TABLE()
#define wxBEGIN_EVENT_TABLE(theClass, baseClass) \
const wxEventTable theClass::sm_eventTable = \
{ &baseClass::sm_eventTable, &theClass::sm_eventTableEntries[0] }; \
const wxEventTable *theClass::GetEventTable() const \
{ return &theClass::sm_eventTable; } \
wxEventHashTable theClass::sm_eventHashTable(theClass::sm_eventTable); \
wxEventHashTable &theClass::GetEventHashTable() const \
{ return theClass::sm_eventHashTable; } \
const wxEventTableEntry theClass::sm_eventTableEntries[] = { \
#define wxEND_EVENT_TABLE() \
wxDECLARE_EVENT_TABLE_TERMINATOR() };
位于wxBEGIN_EVENT_TABLE和END_EVENT_TABLE之间的是消息处理项,比如上面的EVT_MENU(ID_MenuUser, debugWXFrame::OnCheckMenu)就是一项,我们来展开这项:
// 用户使用这个宏来定义事件映射
#define EVT_MENU(winid, func) wx__DECLARE_EVT1(wxEVT_MENU, winid, wxCommandEventHandler(func))
// 事件映射内部宏
#define wx__DECLARE_EVT2(evt, id1, id2, fn) \
wxDECLARE_EVENT_TABLE_ENTRY(evt, id1, id2, fn, NULL),
#define wx__DECLARE_EVT1(evt, id, fn) \
wx__DECLARE_EVT2(evt, id, wxID_ANY, fn)
// 根据传递进来的参数创建一个wxEventTableEntry对象
#define wxDECLARE_EVENT_TABLE_ENTRY(type, winid, idLast, fn, obj) \
wxEventTableEntry(type, winid, idLast, wxNewEventTableFunctor(type, fn), obj)
那么EVT_MENU(ID_MenuUser, debugWXFrame::OnCheckMenu)展开后就得到:
wxEventTableEntry(wxEVT_MENU, winid, wxID_ANY,
wxNewEventTableFunctor(wxEVT_MENU, wxCommandEventHandler(fn)), NULL)
wxNewEventTableFunctor用于实例化一个wxObjectEventFunctor对象,wxCommandEventHandler用于强转消息处理函数,传递给wxNewEventTableFunctor使用。
#define wxCommandEventHandler(func) \
wxEVENT_HANDLER_CAST(wxCommandEventFunction, func)
#define wxEVENT_HANDLER_CAST( functype, func ) \
( wxObjectEventFunction )( wxEventFunction )wxStaticCastEvent( functype, &func )
inline wxObjectEventFunctor *
wxNewEventTableFunctor(const wxEventType& WXUNUSED(evtType),
wxObjectEventFunction method)
{
return new wxObjectEventFunctor(method, NULL);
}
这样就可以静态创建多个wxEventTableEntry对象,加入到sm_eventTableEntries中。
静态消息映射表处理过程
消息处理过程,wxEvtHandler::TryHereOnly会调用静态表查询处理:
bool wxEvtHandler::TryHereOnly(wxEvent& event)
{
// Then static per-class event tables
if ( GetEventHashTable().HandleEvent(event, this) )
return true;
}
上面GetEventHashTable方法将返回一个wxEventHashTable对象,随后调用这个对象的HandleEvent方法:
wxEventHashTable根据消息类型将Table分段,这样可以快速查找到指定类型的table;- 遍历指定类型的table,对每个
wxEventTableEntry调用ProcessEventIfMatchesId
bool wxEventHashTable::HandleEvent(wxEvent &event, wxEvtHandler *self)
{
// Find all entries for the given event type.
wxEventType eventType = event.GetEventType();
const EventTypeTablePointer eTTnode = m_eventTypeTable[eventType % m_size];
if (eTTnode && eTTnode->eventType == eventType)
{
const wxEventTableEntryPointerArray&
eventEntryTable = eTTnode->eventEntryTable;
const size_t count = eventEntryTable.GetCount();
for (size_t n = 0; n < count; n++)
{
const wxEventTableEntry& entry = *eventEntryTable[n];
if ( wxEvtHandler::ProcessEventIfMatchesId(entry, self, event) )
return true;
}
}
return false;
}
wxEvtHandler::ProcessEventIfMatchesId处理流程如下:
- 检查当前收到的消息是否与本项
wxEventTableEntryBase匹配 - 调用用户的处理函数
m_fn执行,这里m_fn也是经过了好几层的封装,有兴趣的继续跟踪。
bool wxEvtHandler::ProcessEventIfMatchesId(const wxEventTableEntryBase& entry,
wxEvtHandler *handler,
wxEvent& event)
{
int tableId1 = entry.m_id,
tableId2 = entry.m_lastId;
// match only if the event type is the same and the id is either -1 in
// the event table (meaning "any") or the event id matches the id
// specified in the event table either exactly or by falling into
// range between first and last
if ((tableId1 == wxID_ANY) ||
(tableId2 == wxID_ANY && tableId1 == event.GetId()) ||
(tableId2 != wxID_ANY &&
(event.GetId() >= tableId1 && event.GetId() <= tableId2)))
{
event.Skip(false);
event.m_callbackUserData = entry.m_callbackUserData;
(*entry.m_fn)(handler, event);
if (!event.GetSkipped())
return true;
}
return false;
}
动态消息映射表
动态映射表项的增加和删除是通过wxEvtHandler::DoBind和wxEvtHandler::DoUnbind执行的,wxWidgets提供了多种接口执行这个操作。
需要注意的是:如果传递进wxEvtHandler对象,则在处理的时候调用用户指定的wxEvtHandler对象进行处理;如果未指定,则直接使用当前的wxEvtHandler对象进行处理。
提供类似功能的还有Connect接口,但是Connect用起来比较复杂,建议后续代码全部使用Bind接口,这里不做赘述。
Bind接口是模板函数,提供三种接口:
- 直接绑定全局函数,此时只有event类型和function参数必须,建议使用;
- 绑定Funtor函数,用户需要自己用
wxNewEventFunctor进行封装,不建议使用; - 使用类内部函数,同时需要指定类指针,建议使用。
三种接口声明如下:
template <typename EventTag, typename EventArg>
void Bind(const EventTag& eventType,
void (*function)(EventArg &),
int winid = wxID_ANY,
int lastId = wxID_ANY,
wxObject *userData = NULL)
template <typename EventTag, typename Functor>
void Bind(const EventTag& eventType,
const Functor &functor,
int winid = wxID_ANY,
int lastId = wxID_ANY,
wxObject *userData = NULL)
template <typename EventTag, typename Class,
typename EventArg, typename EventHandler>
void Bind(const EventTag &eventType,
void (Class::*method)(EventArg &),
EventHandler *handler,
int winid = wxID_ANY,
int lastId = wxID_ANY,
wxObject *userData = NULL)
动态消息映射表处理过程
与静态消息入口相同,在wxEvtHandler::TryHereOnly得到调用:
bool wxEvtHandler::TryHereOnly(wxEvent& event)
{
// Handle per-instance dynamic event tables first
if ( m_dynamicEvents && SearchDynamicEventTable(event) )
return true;
}
动态表查找比较简单,动态消息表存在m_dynamicEvents链表中,只要出去链表的每一项逐个调用ProcessEventIfMatchesId就可以了。
bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
{
wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
while (node)
{
wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
node = node->GetNext();
if ( event.GetEventType() == entry->m_eventType )
{
wxEvtHandler *handler = entry->m_fn->GetEvtHandler();
if ( !handler )
handler = this;
if ( ProcessEventIfMatchesId(*entry, handler, event) )
return true;
}
}
return false;
}
wxWidgets源码分析(3) - 消息映射表的更多相关文章
- wxWidgets源码分析(4) - 消息处理过程
目录 消息处理过程 消息如何到达wxWidgets Win32消息与wxWidgets消息的转换 菜单消息处理 消息处理链(基于wxEvtHandler) 消息处理链(基于wxWindow) 总结 消 ...
- wxWidgets源码分析(8) - MVC架构
目录 MVC架构 wxDocManager文档管理器 模板类创建文档对象 视图对象的创建 创建顺序 框架菜单命令的执行过程 wxDocParentFrame菜单入口 wxDocManager类的处理 ...
- 源码分析RocketMQ消息轨迹
目录 1.发送消息轨迹流程 1.1 DefaultMQProducer构造函数 1.2 SendMessageTraceHookImpl钩子函数 1.3 TraceDispatcher实现原理 2. ...
- 源码分析 Kafka 消息发送流程(文末附流程图)
温馨提示:本文基于 Kafka 2.2.1 版本.本文主要是以源码的手段一步一步探究消息发送流程,如果对源码不感兴趣,可以直接跳到文末查看消息发送流程图与消息发送本地缓存存储结构. 从上文 初识 Ka ...
- 源码分析Kafka 消息拉取流程
目录 1.KafkaConsumer poll 详解 2.Fetcher 类详解 本节重点讨论 Kafka 的消息拉起流程. @(本节目录) 1.KafkaConsumer poll 详解 消息拉起主 ...
- Akka源码分析-Remote-收消息
上一遍博客中,我们分析了网络链接建立的过程,一旦建立就可以正常的收发消息了.发送消息的细节不再分析,因为对于本地的actor来说这个过程相对简单,它只是创立链接然后给指定的netty网路服务发送消息就 ...
- Akka源码分析-Remote-发消息
上一篇博客我们介绍了remote模式下Actor的创建,其实与local的创建并没有太大区别,一般情况下还是使用LocalActorRef创建了Actor.那么发消息是否意味着也是相同的呢? 既然ac ...
- 源码分析 Kafka 消息发送流程
Futuresend(ProducerRecord<K, V> record) Futuresend(ProducerRecord<K, V> record, Callback ...
- wxWidgets源码分析(9) - wxString
目录 wxString wxString的中文字符支持 Windows Linux Unicode Linux UTF-8 总结 wxString与通用字符串的转换 wxString对象的创建 将wx ...
随机推荐
- SparkCore2
二.RDD编程 2.5 RDD中的函数传递 在实际开发中我们往往需要自己定义一些对于RDD的操作,那么此时需要主要的是,初始化工作是在Driver端进行的,而实际运行程序是在Executor端进行的, ...
- 嵌入式设备上卷积神经网络推理时memory的优化
以前的神经网络几乎都是部署在云端(服务器上),设备端采集到数据通过网络发送给服务器做inference(推理),结果再通过网络返回给设备端.如今越来越多的神经网络部署在嵌入式设备端上,即inferen ...
- Educational Codeforces Round 91 (Rated for Div. 2) D. Berserk And Fireball
题目链接:https://codeforces.com/contest/1380/problem/D 题意 给出一个大小为 $n$ 的排列 $a$ 和一个序列 $b$,有两种操作: 花费 $x$ 消除 ...
- Panasonic Programming Contest (AtCoder Beginner Contest 186) E.Throne (数学,线性同余方程)
题意:有围着一圈的\(N\)把椅子,其中有一个是冠位,你在离冠位顺时针\(S\)把椅子的位置,你每次可以顺时针走\(K\)个椅子,问最少要走多少次才能登上冠位,或者走不到冠位. 题解:这题和洛谷那个青 ...
- AcWing 238.银河英雄传说 (边带权并查集)
题意:有\(n\)列,有\(T\)条指令,若指令格式为\(M\),则将第\(i\)号的所有战舰移到第\(j\)号所在列的后面,若指令格式为\(C\),询问\(i\)和\(j\)是否在同一列,如果在,问 ...
- Codeforces Round #641 div2 B. Orac and Models (DP)
题意:有一个长度为\(n\)的序列\(a\),求一个最长上升子序列,且这个子序列的元素在\(a\)中的位置满足\(i_{j+1}modi_{j}=0\),求这个子序列的最大长度. 题意:这题假如我们用 ...
- Codeforces Round #655 (Div. 2) B. Omkar and Last Class of Math (数学)
题意:给你一个正整数\(n\),求两个正整数\(a\)和\(b\),使得\(a+b=n\),并且\(LCM(a,b)\)要尽可能的小. 题解:首先对于偶数,构造\(\frac{n}{2}\)和\(\f ...
- 踏上Revit二次开发之路 2 从“HelloWorld”入手
2 从"HelloWorld"入手 在欧特克的官方网页上有个叫<My First Plug-in Training>的项目,号称可以让一个完全没有编程基础的人照着做出一 ...
- Mac下anaconda的安装和基本使用
Mac下anaconda的安装和基本使用 安装 在conda官网下载安装conda. 打开terminal输入conda -V,回车显示conda的版本说明安装成功. 将conda更新到最新版本 co ...
- 自动化将 word 转为 pdf,再将pdf转为图片!
参考: https://blog.csdn.net/ynyn2013/article/details/49120731 https://www.jianshu.com/p/f57cc64b9f5e 一 ...