Improve wording and formatting in "Hello World" example
Minor fixes to the "Hello World" example in the manual to make its style more consistent and the text more readable. Closes https://github.com/wxWidgets/wxWidgets/pull/657
This commit is contained in:
@@ -24,7 +24,7 @@ them). For the platforms with support for precompiled headers, as indicated by
|
|||||||
only include it for the other ones:
|
only include it for the other ones:
|
||||||
|
|
||||||
@code
|
@code
|
||||||
// wxWidgets "Hello world" Program
|
// wxWidgets "Hello World" Program
|
||||||
|
|
||||||
// For compilers that support precompilation, includes "wx/wx.h".
|
// For compilers that support precompilation, includes "wx/wx.h".
|
||||||
#include <wx/wxprec.h>
|
#include <wx/wxprec.h>
|
||||||
@@ -35,8 +35,8 @@ only include it for the other ones:
|
|||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
Practically every app should define a new class derived from wxApp. By
|
Practically every app should define a new class derived from wxApp. By
|
||||||
overriding wxApp's OnInit() virtual method the program can be initialized, e.g.
|
overriding wxApp's OnInit() virtual method the program can be initialized,
|
||||||
by creating a new main window.
|
e.g. by creating a new main window.
|
||||||
|
|
||||||
@code
|
@code
|
||||||
class MyApp : public wxApp
|
class MyApp : public wxApp
|
||||||
@@ -48,17 +48,17 @@ public:
|
|||||||
|
|
||||||
The main window is created by deriving a class from wxFrame and giving it a
|
The main window is created by deriving a class from wxFrame and giving it a
|
||||||
menu and a status bar in its constructor. Also, any class that wishes to
|
menu and a status bar in its constructor. Also, any class that wishes to
|
||||||
respond to any "event" (such as mouse clicks or messages from the menu or a
|
respond to an "event" (such as mouse clicks, messages from the menu, or a
|
||||||
button) must declare an event table using the macro below.
|
button) must declare an event table using the macro below.
|
||||||
|
|
||||||
Finally, the way to react to such events must be done in "event handlers" which
|
Finally, reacting to such events is done via "event handlers" which are
|
||||||
are just functions (or functors, including lambdas if you're using C++11)
|
just functions (or functors, including lambdas if you're using C++11)
|
||||||
taking the @c event parameter of the type corresponding to the event being
|
taking the @c event parameter of the type corresponding to the event being
|
||||||
handled, e.g. wxCommandEvent for the events from simple controls such as
|
handled, e.g. wxCommandEvent for the events from simple controls such as
|
||||||
buttons, text fields and also menu items. In our sample, we react to three menu
|
buttons, text fields and also menu items. In our example, we react to three
|
||||||
items, one for our custom menu command and two for the standard "Exit" and
|
menu items: our custom "Hello", and the "Exit" and "About" items (any program
|
||||||
"About" commands (any program should normally implement the latter two). Notice
|
should normally implement the latter two). Notice that these handlers don't
|
||||||
that these handlers don't need to be neither virtual nor public.
|
need to be virtual or public.
|
||||||
|
|
||||||
@code
|
@code
|
||||||
class MyFrame : public wxFrame
|
class MyFrame : public wxFrame
|
||||||
@@ -84,13 +84,13 @@ enum
|
|||||||
};
|
};
|
||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
Notice that you don't need to define identifiers for the "About" and "Exit" as
|
Notice that you don't need to define identifiers for "About" and "Exit", as
|
||||||
wxWidgets already predefines and the standard values such as wxID_ABOUT and
|
wxWidgets already predefines standard values such as wxID_ABOUT and wxID_EXIT.
|
||||||
wxID_EXIT should be used whenever possible, as they can be handled in a special
|
You should use these whenever possible, as they can be handled in a special
|
||||||
way by a particular platform.
|
way by a particular platform.
|
||||||
|
|
||||||
As in all programs there must be a "main" function. Under wxWidgets main is
|
As in all programs, there must be a "main" function. Under wxWidgets, main is
|
||||||
implemented inside ::wxIMPLEMENT_APP() macro, which creates an application
|
implemented inside the ::wxIMPLEMENT_APP() macro, which creates an application
|
||||||
instance of the specified class and starts running the GUI event loop. It is
|
instance of the specified class and starts running the GUI event loop. It is
|
||||||
used simply as:
|
used simply as:
|
||||||
|
|
||||||
@@ -100,10 +100,10 @@ wxIMPLEMENT_APP(MyApp);
|
|||||||
|
|
||||||
As mentioned above, wxApp::OnInit() is called upon startup and should be used
|
As mentioned above, wxApp::OnInit() is called upon startup and should be used
|
||||||
to initialize the program, maybe showing a "splash screen" and creating the
|
to initialize the program, maybe showing a "splash screen" and creating the
|
||||||
main window (or several). As frames are created hidden by default, to allow
|
main window (or several). Frames are created hidden by default, to allow the
|
||||||
creating their child windows before showing them, we also need to explicitly
|
creation of child windows before displaying them. We thus need to explicitly
|
||||||
show it to make it appear on the screen. Finally, we return @true from this
|
show them. Finally, we return @true from this method to indicate successful
|
||||||
method to indicate successful initialization:
|
initialization:
|
||||||
|
|
||||||
@code
|
@code
|
||||||
bool MyApp::OnInit()
|
bool MyApp::OnInit()
|
||||||
@@ -114,9 +114,9 @@ bool MyApp::OnInit()
|
|||||||
}
|
}
|
||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
In the constructor of the main window (or later on) we create a menu with our
|
In the constructor of the main window (or later on), we create a menu with our
|
||||||
menu items as well as a status bar to be shown at the bottom of the main
|
menu items, as well as a status bar to be shown at the bottom of the main
|
||||||
window. Both have to be associated with the frame with respective calls.
|
window. Both have to be bound to the frame with respective calls.
|
||||||
|
|
||||||
@code
|
@code
|
||||||
MyFrame::MyFrame()
|
MyFrame::MyFrame()
|
||||||
@@ -144,27 +144,28 @@ MyFrame::MyFrame()
|
|||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
Notice that we don't need to specify the labels for the standard menu items
|
Notice that we don't need to specify the labels for the standard menu items
|
||||||
@c wxID_ABOUT and @c wxID_EXIT, they will be given standard (even correctly
|
@c wxID_ABOUT and @c wxID_EXIT — they will be given standard (even correctly
|
||||||
translated) labels and also standard accelerators correct for the current
|
translated) labels and standard accelerators correct for the current
|
||||||
platform making your program behaviour more native. For this reason you should
|
platform, making our program behaviour more native. For this reason, you
|
||||||
prefer reusing the standard ids (see @ref page_stockitems) if possible.
|
should prefer reusing the standard ids (see @ref page_stockitems) where
|
||||||
|
possible.
|
||||||
|
|
||||||
We also have to actually connect our event handlers to the events we want to
|
We also have to connect our event handlers to the events we want to handle in
|
||||||
handle in them, by calling Bind() to send all the menu events, identified by
|
them. We do this by calling Bind() to send all the menu events (identified by
|
||||||
wxEVT_MENU event type, with the specified ID to the given function. The
|
wxEVT_MENU event type) with the specified ID to the given function. The
|
||||||
parameters we pass to Bind() are
|
parameters we pass to Bind() are
|
||||||
|
|
||||||
-# The event type, e.g. wxEVT_MENU or wxEVT_BUTTON or wxEVT_SIZE or one
|
-# The event type, e.g. wxEVT_MENU, wxEVT_BUTTON, wxEVT_SIZE, or one
|
||||||
of many other events used by wxWidgets.
|
of many other events used by wxWidgets.
|
||||||
-# Pointer to the member function to call and the object to call it on. In
|
-# A Pointer to the method to call, and the object to call it on. In
|
||||||
this case we just call our own function, hence we pass `this` for this
|
this case, we just call our own function, and pass the `this` pointer
|
||||||
object itself, but we could call a member function of another object too.
|
for the object itself. We could instead call the method of another object,
|
||||||
And we could could also use a non-member function here, and, in fact,
|
or a non-member function — in fact, any object that can be called with a
|
||||||
anything that can be called passing it a wxCommandEvent could be used here.
|
wxCommandEvent, can be used here.
|
||||||
-# The optional identifier allowing to select just some events of wxEVT_MENU
|
-# An optional identifier, allowing us to select just some events of wxEVT_MENU
|
||||||
type, namely those from the menu item with the given ID, instead of handling
|
type, namely those from the menu item with the given ID, instead of handling
|
||||||
all of them in the provided handler. This is especially useful with the menu
|
all of them in the provided handler. This is mainly useful with menu items
|
||||||
items and rarely used with other kinds of events.
|
and rarely with other kinds of events.
|
||||||
|
|
||||||
@code
|
@code
|
||||||
... continued from above ...
|
... continued from above ...
|
||||||
@@ -193,14 +194,14 @@ case a typical "About" window with information about the program.
|
|||||||
@code
|
@code
|
||||||
void MyFrame::OnAbout(wxCommandEvent& event)
|
void MyFrame::OnAbout(wxCommandEvent& event)
|
||||||
{
|
{
|
||||||
wxMessageBox( "This is a wxWidgets' Hello world sample",
|
wxMessageBox("This is a wxWidgets Hello World example",
|
||||||
"About Hello World", wxOK | wxICON_INFORMATION);
|
"About Hello World", wxOK | wxICON_INFORMATION);
|
||||||
}
|
}
|
||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
The implementation of custom menu command handler may perform whatever task
|
The implementation of custom menu command handler may perform whatever task
|
||||||
your program needs to do, in this case we will simply show a message from it as
|
your program needs to do, in this case we will simply show a message from it as
|
||||||
befits a hello world example:
|
befits a Hello World example:
|
||||||
|
|
||||||
@code
|
@code
|
||||||
void MyFrame::OnHello(wxCommandEvent& event)
|
void MyFrame::OnHello(wxCommandEvent& event)
|
||||||
@@ -223,7 +224,7 @@ void MyFrame::OnHello(wxCommandEvent& event)
|
|||||||
Here is the entire program that can be copied and pasted:
|
Here is the entire program that can be copied and pasted:
|
||||||
|
|
||||||
@code
|
@code
|
||||||
// wxWidgets "Hello world" Program
|
// wxWidgets "Hello World" Program
|
||||||
|
|
||||||
// For compilers that support precompilation, includes "wx/wx.h".
|
// For compilers that support precompilation, includes "wx/wx.h".
|
||||||
#include <wx/wxprec.h>
|
#include <wx/wxprec.h>
|
||||||
@@ -296,7 +297,7 @@ void MyFrame::OnExit(wxCommandEvent& event)
|
|||||||
|
|
||||||
void MyFrame::OnAbout(wxCommandEvent& event)
|
void MyFrame::OnAbout(wxCommandEvent& event)
|
||||||
{
|
{
|
||||||
wxMessageBox( "This is a wxWidgets' Hello world sample",
|
wxMessageBox("This is a wxWidgets Hello World example",
|
||||||
"About Hello World", wxOK | wxICON_INFORMATION);
|
"About Hello World", wxOK | wxICON_INFORMATION);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user