WxWidgets initialization analysis-application definition and initialization

Source: Internet
Author: User
Tags wxwidgets

1. The basic structure of a wxWidgets is: an app class + A frame class,CodeFor example:

Class MyApp: Public wxapp
{
Public:
Bool oninit ();
};
Implement_app (MyApp)

2. The macro implement_app (MyApp) contains the portal and abstracts the differences between the portals of different platforms. The analysis is as follows:

The implement_app is expanded as follows:

# Define implement_app (appname )\
Implement_app_no_themes (appname )\
Implement_wx_theme_support

 

Implement_app_no_themes is expanded as follows:

# Define implement_app_no_themes (appname )\
Implement_app_no_main (appname )\
Implement_wxwin_main

 

Implement_app_no_main is expanded as follows:

# 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 ());}

In fact, implement_app_no_main defines the wxcreateapp function new appname to create an instance of the application class.

Wxappinitializer wxtheappinitializer (wxappinitializerfunction) wxcreateapp); in fact, the interface wxapp: setinitializerfunction (FN) used to create the function wxcreateapp; is set to the system and all static variables are saved.

Appname &Wxgetapp() {Return * wx_static_cast (appname *, wxapp: getinstance ();} the interface for obtaining the app instance is also defined here.

 

Implement_wxwin_main is expanded as follows:

# Define implement_wxwin_main \
Extern "C" int winapi winmain (hinstance ,\
Hinstance hprevinstance ,\
Wxw.lineargtype lpcmdline ,\
Int ncmdshow )\
{\
Return wxentry (hinstance, hprevinstance, lpcmdline, ncmdshow );\
}\
Implement_wxwin_main_borland_nonstandard

Winmain is an entry function for Windows Win32 applications. winmain includes the main function implementation of C/C ++.

Multiple parameters wxdllexport int wxentry (hinstance, hinstance wxunused (hprevinstance), invalid wxunused (pcmdline), int ncmdshow) will call the int wxentry (Int & argc, char ** argv). This function is implemented in three places. The Unicode version also needs to be converted to a non-Unicode version for function calling.Wxentryreal is used. This interface is the real library initialization entry: The analysis is as follows:

Wxentryreal is defined as follows:

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); this statement implies the library initialization process. The wxinitializer class constructor initializes:

Wxinitializer (INT argc, char ** argv) // Constructor
{
M_ OK = wxinitialize (argc, argv); // The initialization wxinitialize is called.
}

Wxinitialize calls wxentrystart. This function also has two versions. The Unicode version is also a non-Unicode version called by conversion, as shown below:

Wxappptr app (wxtheapp );
If (! App. Get ())
{
// If not, he might have used implement_app () to give us a function
// Create it
Wxappinitializerfunction fncreate = wxapp: getinitializerfunction (); // note that this sentence obtains the function for creating the app set above.

If (fncreate)
{
// He did, try to create the custom wxapp object
App. set (* fncreate) (); // The app class is constructed here, and the app variable saves this pointer. The assigned value actually calls wxapp: setinstance (PTR ); assigned ms_appinstance,

// Use the wxtheapp macro to access this variable. The wxgetapp interface defined above also obtains this variable.
 }

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) // call the app initialization interface bool wxapp: Initialize (Int & argc, wxchar ** argv, then the system is initialized.

{
Return false;
}

 

After wxentryreal calls the system initialization, it calls the oninit interface of the application app to give the app initialization time:

// App Initialization
If (! Wxtheapp-> calloninit ())
{
// Don't call onexit () If oninit () failed
Return-1;
}
This call is made to the oninit interface of the application, where the application initializes window components such as the frame main window. wxtheapp macros are widely used. the calloninit interface calls the calloninit () {return oninit ();} interface.

 

-----------------------------------------------------------------------

The above basically completes the initialization process of the library and the user's app. At the end of the wxentryreal interface, return wxtheapp-> onrun (); and enter:

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 ();
}
// EnterProgramThe main cycle starts the process of receiving, distributing, and processing events.

 

This article is complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.