wxWidgets初始化分析-应用定义和初始化
2012-08-19 17:02
232 查看
1. 一个wxWidgets的基本结构是:一个App类+一个Frame类,代码如:
class MyApp: public wxApp
{
public:
bool OnInit();
};
IMPLEMENT_APP(MyApp)
2. IMPLEMENT_APP(MyApp) 这个宏包含了入口,抽象了不同平台入口的差异,分析如下:
IMPLEMENT_APP展开如下:
#define IMPLEMENT_APP(appname) \
IMPLEMENT_APP_NO_THEMES(appname) \
IMPLEMENT_WX_THEME_SUPPORT
IMPLEMENT_APP_NO_THEMES展开如下:
#define IMPLEMENT_APP_NO_THEMES(appname) \
IMPLEMENT_APP_NO_MAIN(appname) \
IMPLEMENT_WXWIN_MAIN
IMPLEMENT_APP_NO_MAIN展开如下:
#define IMPLEMENT_APP_NO_MAIN(appname) \
wxAppConsole *wxCreateApp() \
{ \
wxAppConsole::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, \
"your program"); \
return new appname; \
} \
wxAppInitializer \
wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
DECLARE_APP(appname) \
appname& wxGetApp() { return *wx_static_cast(appname*, wxApp::GetInstance()); }
实际上IMPLEMENT_APP_NO_MAIN定义了wxCreateApp函数new appname创建出应用类的实例.
wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp);实际上把创建函数wxCreateApp用接口wxApp::SetInitializerFunction(fn);设置进了系统,都静态变量保存的.
appname& wxGetApp() { return *wx_static_cast(appname*, wxApp::GetInstance()); }这个获取App实例的接口也在此定义.
IMPLEMENT_WXWIN_MAIN展开如下:
#define IMPLEMENT_WXWIN_MAIN \
extern "C" int WINAPI WinMain(HINSTANCE hInstance, \
HINSTANCE hPrevInstance, \
wxCmdLineArgType lpCmdLine, \
int nCmdShow) \
{ \
return wxEntry(hInstance, hPrevInstance, lpCmdLine, nCmdShow); \
} \
IMPLEMENT_WXWIN_MAIN_BORLAND_NONSTANDARD
WinMain是Windows平台Win32应用的入口函数,WinMain包含了C/C++的main函数实现.
多参WXDLLEXPORT int wxEntry(HINSTANCE hInstance,HINSTANCE WXUNUSED(hPrevInstance),wxCmdLineArgType WXUNUSED(pCmdLine),int nCmdShow),会在最后调用2参的int wxEntry(int& argc, char **argv),此函数实现有3处,Unicode版本的也是要转换成非Unicode版本的函数调用,最终会调用到wxEntryReal,此接口是真正的库初始化入口:分析如下
wxEntryReal定义如下:
int wxEntryReal(int& argc, wxChar **argv)
{
// library initialization
wxInitializer initializer(argc, argv);
if ( !initializer.IsOk() )
{
#if wxUSE_LOG
// flush any log messages explaining why we failed
delete wxLog::SetActiveTarget(NULL);
#endif
return -1;
}
wxTRY
{
// app initialization
if ( !wxTheApp->CallOnInit() )
{
// don't call OnExit() if OnInit() failed
return -1;
}
// ensure that OnExit() is called if OnInit() had succeeded
class CallOnExit
{
public:
~CallOnExit() { wxTheApp->OnExit(); }
} callOnExit;
WX_SUPPRESS_UNUSED_WARN(callOnExit);
// app execution
return wxTheApp->OnRun();
}
wxCATCH_ALL( wxTheApp->OnUnhandledException(); return -1; )
}
wxInitializer initializer(argc, argv);这句隐含了库的初始化过程,wxInitializer类的构造函数进行了初始化:
wxInitializer(int argc, char **argv) //构造函数
{
m_ok = wxInitialize(argc, argv); //调用了初始化wxInitialize
}
wxInitialize调用了wxEntryStart,此函数也2个版本,unicode的版本也是经过转换调用的非unicode版本,如下:
wxAppPtr app(wxTheApp);
if ( !app.get() )
{
// if not, he might have used IMPLEMENT_APP() to give us a function to
// create it
wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction(); // 注意此句取得了上边设置的创建App的函数
if ( fnCreate )
{
// he did, try to create the custom wxApp object
app.Set((*fnCreate)()); // 此处构造了应用App类,app变量保存了此指针,这个赋值实际调用wxApp::SetInstance(ptr);赋值了ms_appInstance,
//通过wxTheApp宏访问,上边定义的wxGetApp接口,也是获取此变量.
}
if ( !app.get() )
{
// either IMPLEMENT_APP() was not used at all or it failed -- in any
// case we still need something
app.Set(new wxDummyConsoleApp);
}
// wxApp initialization: this can be customized
// --------------------------------------------
if ( !app->Initialize(argc, argv) ) // 调用app初始化接口,bool wxApp::Initialize(int& argc, wxChar **argv)此接口被调用,进而初始化了系统
{
return false;
}
wxEntryReal调用了系统初始化后,又调用了应用App的OnInit接口,给App初始化的时机:
// app initialization
if ( !wxTheApp->CallOnInit() )
{
// don't call OnExit() if OnInit() failed
return -1;
}
此调用会多态到应用的OnInit接口,应用在此接口中初始化了Frame主窗口之类的窗口组件.wxTheApp宏用的比较多.CallOnInit接口调用了 CallOnInit() { return OnInit(); }接口。
-----------------------------------------------------------------------
以上基本完成了库和用户的App初始化过程,在wxEntryReal接口的最后,return wxTheApp->OnRun();进入:
int wxAppBase::OnRun()
{
// see the comment in ctor: if the initial value hasn't been changed, use
// the default Yes from now on
if ( m_exitOnFrameDelete == Later )
{
m_exitOnFrameDelete = Yes;
}
//else: it has been changed, assume the user knows what he is doing
return MainLoop();
}
//进入了程序主循环,开始了事件接收、分发、处理的过程,指导程序结束.
本文完.
class MyApp: public wxApp
{
public:
bool OnInit();
};
IMPLEMENT_APP(MyApp)
2. IMPLEMENT_APP(MyApp) 这个宏包含了入口,抽象了不同平台入口的差异,分析如下:
IMPLEMENT_APP展开如下:
#define IMPLEMENT_APP(appname) \
IMPLEMENT_APP_NO_THEMES(appname) \
IMPLEMENT_WX_THEME_SUPPORT
IMPLEMENT_APP_NO_THEMES展开如下:
#define IMPLEMENT_APP_NO_THEMES(appname) \
IMPLEMENT_APP_NO_MAIN(appname) \
IMPLEMENT_WXWIN_MAIN
IMPLEMENT_APP_NO_MAIN展开如下:
#define IMPLEMENT_APP_NO_MAIN(appname) \
wxAppConsole *wxCreateApp() \
{ \
wxAppConsole::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, \
"your program"); \
return new appname; \
} \
wxAppInitializer \
wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp); \
DECLARE_APP(appname) \
appname& wxGetApp() { return *wx_static_cast(appname*, wxApp::GetInstance()); }
实际上IMPLEMENT_APP_NO_MAIN定义了wxCreateApp函数new appname创建出应用类的实例.
wxAppInitializer wxTheAppInitializer((wxAppInitializerFunction) wxCreateApp);实际上把创建函数wxCreateApp用接口wxApp::SetInitializerFunction(fn);设置进了系统,都静态变量保存的.
appname& wxGetApp() { return *wx_static_cast(appname*, wxApp::GetInstance()); }这个获取App实例的接口也在此定义.
IMPLEMENT_WXWIN_MAIN展开如下:
#define IMPLEMENT_WXWIN_MAIN \
extern "C" int WINAPI WinMain(HINSTANCE hInstance, \
HINSTANCE hPrevInstance, \
wxCmdLineArgType lpCmdLine, \
int nCmdShow) \
{ \
return wxEntry(hInstance, hPrevInstance, lpCmdLine, nCmdShow); \
} \
IMPLEMENT_WXWIN_MAIN_BORLAND_NONSTANDARD
WinMain是Windows平台Win32应用的入口函数,WinMain包含了C/C++的main函数实现.
多参WXDLLEXPORT int wxEntry(HINSTANCE hInstance,HINSTANCE WXUNUSED(hPrevInstance),wxCmdLineArgType WXUNUSED(pCmdLine),int nCmdShow),会在最后调用2参的int wxEntry(int& argc, char **argv),此函数实现有3处,Unicode版本的也是要转换成非Unicode版本的函数调用,最终会调用到wxEntryReal,此接口是真正的库初始化入口:分析如下
wxEntryReal定义如下:
int wxEntryReal(int& argc, wxChar **argv)
{
// library initialization
wxInitializer initializer(argc, argv);
if ( !initializer.IsOk() )
{
#if wxUSE_LOG
// flush any log messages explaining why we failed
delete wxLog::SetActiveTarget(NULL);
#endif
return -1;
}
wxTRY
{
// app initialization
if ( !wxTheApp->CallOnInit() )
{
// don't call OnExit() if OnInit() failed
return -1;
}
// ensure that OnExit() is called if OnInit() had succeeded
class CallOnExit
{
public:
~CallOnExit() { wxTheApp->OnExit(); }
} callOnExit;
WX_SUPPRESS_UNUSED_WARN(callOnExit);
// app execution
return wxTheApp->OnRun();
}
wxCATCH_ALL( wxTheApp->OnUnhandledException(); return -1; )
}
wxInitializer initializer(argc, argv);这句隐含了库的初始化过程,wxInitializer类的构造函数进行了初始化:
wxInitializer(int argc, char **argv) //构造函数
{
m_ok = wxInitialize(argc, argv); //调用了初始化wxInitialize
}
wxInitialize调用了wxEntryStart,此函数也2个版本,unicode的版本也是经过转换调用的非unicode版本,如下:
wxAppPtr app(wxTheApp);
if ( !app.get() )
{
// if not, he might have used IMPLEMENT_APP() to give us a function to
// create it
wxAppInitializerFunction fnCreate = wxApp::GetInitializerFunction(); // 注意此句取得了上边设置的创建App的函数
if ( fnCreate )
{
// he did, try to create the custom wxApp object
app.Set((*fnCreate)()); // 此处构造了应用App类,app变量保存了此指针,这个赋值实际调用wxApp::SetInstance(ptr);赋值了ms_appInstance,
//通过wxTheApp宏访问,上边定义的wxGetApp接口,也是获取此变量.
}
if ( !app.get() )
{
// either IMPLEMENT_APP() was not used at all or it failed -- in any
// case we still need something
app.Set(new wxDummyConsoleApp);
}
// wxApp initialization: this can be customized
// --------------------------------------------
if ( !app->Initialize(argc, argv) ) // 调用app初始化接口,bool wxApp::Initialize(int& argc, wxChar **argv)此接口被调用,进而初始化了系统
{
return false;
}
wxEntryReal调用了系统初始化后,又调用了应用App的OnInit接口,给App初始化的时机:
// app initialization
if ( !wxTheApp->CallOnInit() )
{
// don't call OnExit() if OnInit() failed
return -1;
}
此调用会多态到应用的OnInit接口,应用在此接口中初始化了Frame主窗口之类的窗口组件.wxTheApp宏用的比较多.CallOnInit接口调用了 CallOnInit() { return OnInit(); }接口。
-----------------------------------------------------------------------
以上基本完成了库和用户的App初始化过程,在wxEntryReal接口的最后,return wxTheApp->OnRun();进入:
int wxAppBase::OnRun()
{
// see the comment in ctor: if the initial value hasn't been changed, use
// the default Yes from now on
if ( m_exitOnFrameDelete == Later )
{
m_exitOnFrameDelete = Yes;
}
//else: it has been changed, assume the user knows what he is doing
return MainLoop();
}
//进入了程序主循环,开始了事件接收、分发、处理的过程,指导程序结束.
本文完.
相关文章推荐
- PHP中函数的声明定义及应用举例详细分析
- java基础数组的定义与初始化以及集合框架的应用
- Sonar6.0应用之三:集成Eclipse实时代码质量分析(附Eclipse初始化)
- Sonar6.0应用之三:集成Eclipse实时代码质量分析(附Eclipse初始化)
- IOS 应用初始化和终止分析
- netty源码分析(三)Netty服务端ServerBootstrap的初始化与反射在其中的应用分析
- C语言学习6 :指针的定义,指针类型要合法,指针要初始化,指针做函数参数,数组和指针的通用性,指针+1所代表的空间,void * 指针,交换函数中的指针,数组和字符型指针区别,字符型指针的应用,使用指针完成字符操作函数
- 在控制您的业务下载分析功能,在你的iPhone商业应用
- linux 网桥代码分析 二 网桥初始化
- DICOM标准及应用——第二讲 DICOM信息模型和信息定义
- WCF技术的不同应用场景及其实现分析
- Spring Security3源码分析-FilterChainProxy初始化(转)
- iOS攻防 - (八)使用Introspy追踪和分析iOS应用
- 声明、定义、初始化
- Android Launcher分析和修改4——初始化加载数据
- Excel在统计分析中的应用—第六章—抽样与抽样分布-Part3(分层抽样)
- JavaScript进阶 函数、定义函数、函数应用、对象
- KWP2000协议:协议应用及实例分析
- 工作流现状和应用分析
- Arm linux 内核移植及系统初始化过程分析