Update and improve "Hello world" example in the documentation.

The example was corrupted by transition to Doxygen (the menu item labels got
eaten), fix it to actually work.

Also use this opportunity to improve and modernize it.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@67006 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin 2011-02-23 23:35:48 +00:00
parent f18b5ee74c
commit 2e7352635e

View File

@ -10,42 +10,37 @@
@page overview_helloworld Hello World Example @page overview_helloworld Hello World Example
Many people have requested a mini-sample to be published here This page shows a very simple wxWidgets program that can be used as a skeleton
so that some quick judgment concerning syntax for your own code. While it does nothing very useful, it introduces a couple of
and basic principles can be made, so here we go. important concepts and explains how to write a working wxWidgets application.
First, you have to include wxWidgets' header files, of course. This can First, you have to include wxWidgets' header files, of course. This can
be done on a file by file basis (such as <tt>@#include "wx/window.h"</tt>) be done on a file by file basis (such as @c wx/window.h</tt>) or using one
or using one global include (<tt>@#include "wx/wx.h"</tt>). This is global include (@c wx/wx.h) which includes most of the commonly needed headers
also useful on platforms which support precompiled headers such (although not all of them as there are simply too many wxWidgets headers to
as all major compilers on the Windows platform and GCC on Unix platforms. pull in all of them). For the platforms with support for precompiled headers,
as indicated by @c WX_PRECOMP, this global header is already included by @c
wx/wxprec.h so we only include it for the other ones:
@code @code
// // wxWidgets "Hello world" Program
// file name: hworld.cpp
//
// purpose: wxWidgets "Hello world"
//
// 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>
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP #ifndef WX_PRECOMP
#include "wx/wx.h" #include <wx/wx.h>
#endif #endif
@endcode @endcode
Practically every app should define a new class derived from wxApp. Practically every app should define a new class derived from wxApp. By
By overriding wxApp's OnInit() the program can be initialized, overriding wxApp's OnInit() virtual method the program can be initialized, e.g.
e.g. by creating a new main window. by creating a new main window.
@code @code
class MyApp: public wxApp class MyApp: public wxApp
{ {
public:
virtual bool OnInit(); virtual bool OnInit();
}; };
@endcode @endcode
@ -57,8 +52,10 @@ messages from the menu or a button) must declare an event table
using the macro below. using the macro below.
Finally, the way to react to such events must be done in "handlers". Finally, the way to react to such events must be done in "handlers".
In our sample, we react to two menu items, one for "Quit" and one for In our sample, we react to three menu items, one for our custom menu
displaying an "About" window. These handlers should not be virtual. command and two for the standard "Exit" and "About" commands (any program
should normally implement the latter two). Notice that these handlers
don't need to be neither virtual nor public.
@code @code
class MyFrame: public wxFrame class MyFrame: public wxFrame
@ -66,31 +63,33 @@ class MyFrame: public wxFrame
public: public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event); private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event); void OnAbout(wxCommandEvent& event);
private: wxDECLARE_EVENT_TABLE();
DECLARE_EVENT_TABLE()
}; };
@endcode @endcode
In order to be able to react to a menu command, it must be given a unique In order to be able to react to a menu command, it must be given a unique
identifier such as a const or an enum. identifier which can be defined as a const variable or an enum element. The
latter is often used because typically many such constants will be needed:
@code @code
enum enum
{ {
ID_Quit = 1, ID_Hello = 1
ID_About,
}; };
@endcode @endcode
Notice that you don't need to define identifiers for the "About" and "Exit"
We then proceed to actually implement an event table in which the events We then proceed to actually implement an event table in which the events
are routed to their respective handler functions in the class MyFrame. are routed to their respective handler functions in the class MyFrame.
There are predefined macros for routing all common events, ranging from There are predefined macros for routing all common events, ranging from
the selection of a list box entry to a resize event when a user resizes the selection of a list box entry to a resize event when a user resizes
a window on the screen. If -1 is given as the ID, the given handler will be a window on the screen. If @c wxID_ANY is given as the ID, the given handler will be
invoked for any event of the specified type, so that you could add just invoked for any event of the specified type, so that you could add just
one entry in the event table for all menu commands or all button commands etc. one entry in the event table for all menu commands or all button commands etc.
@ -100,17 +99,18 @@ which holds various information about the event (such as the ID of and a
pointer to the class, which emitted the event). pointer to the class, which emitted the event).
@code @code
BEGIN_EVENT_TABLE(MyFrame, wxFrame) wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Quit, MyFrame::OnQuit) EVT_MENU(ID_Hello, MyFrame::OnHello)
EVT_MENU(ID_About, MyFrame::OnAbout) EVT_MENU(wxID_EXIT, MyFrame::OnExit)
END_EVENT_TABLE() EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
wxEND_EVENT_TABLE()
@endcode @endcode
As in all programs there must be a "main" function. Under wxWidgets main is implemented As in all programs there must be a "main" function. Under wxWidgets main is implemented
using this macro, which creates an application instance and starts the program. using this macro, which creates an application instance and starts the program.
@code @code
IMPLEMENT_APP(MyApp) wxIMPLEMENT_APP(MyApp)
@endcode @endcode
As mentioned above, wxApp::OnInit() is called upon startup and should be As mentioned above, wxApp::OnInit() is called upon startup and should be
@ -128,22 +128,26 @@ bool MyApp::OnInit()
} }
@endcode @endcode
In the constructor of the main window (or later on) we create a menu with two menu 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 window. Both have items as well as a status bar to be shown at the bottom of the main window. Both have
to be "announced" to the frame with respective calls. to be associated with the frame with respective calls.
@code @code
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, -1, title, pos, size) : wxFrame(NULL, wxID_ANY, title, pos, size)
{ {
wxMenu *menuFile = new wxMenu; wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
menuFile->Append( ID_About, "" ); "Help string shown in status bar for this menu item");
menuFile->AppendSeparator(); menuFile->AppendSeparator();
menuFile->Append( ID_Quit, "E" ); menuFile->Append(wxID_EXIT);
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar; wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, "" ); menuBar->Append( menuFile, "&File" );
menuBar->Append( menuHelp, "&Help" );
SetMenuBar( menuBar ); SetMenuBar( menuBar );
@ -152,13 +156,19 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
} }
@endcode @endcode
Here are the actual event handlers. MyFrame::OnQuit() closes the main window Notice that we don't need to specify the labels for the standard menu items
by calling Close(). The parameter @true indicates that other windows have no veto @c wxID_ABOUT and @c wxID_EXIT, they will be given standard (even correctly
power such as after asking "Do you really want to close?". If there is no other translated) labels and also standard accelerators correct for the current
main window left, the application will quit. platform making your program behaviour more native. For this reason you should
prefer reusing the standard ids (see @ref page_stockitems) if possible.
Here are the standard event handlers implementations. MyFrame::OnExit() closes
the main window by calling Close(). The parameter @true indicates that other
windows have no veto power such as after asking "Do you really want to close?".
If there is no other main window left, the application will quit.
@code @code
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnExit(wxCommandEvent& event)
{ {
Close( true ); Close( true );
} }
@ -168,10 +178,109 @@ MyFrame::OnAbout() will display a small window with some text in it. In this
case a typical "About" window with information about the program. case a typical "About" window with information about the program.
@code @code
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnAbout(wxCommandEvent& event)
{ {
wxMessageBox( "This is a wxWidgets' Hello world sample", wxMessageBox( "This is a wxWidgets' Hello world sample",
"About Hello World", wxOK | wxICON_INFORMATION ); "About Hello World", wxOK | wxICON_INFORMATION );
}
@endcode
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
befits a hello world example:
@code
void MyFrame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
}
@endcode
Here is the entire program that can be copied and pasted:
@code
// wxWidgets "Hello world" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
class MyApp: public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};
enum
{
ID_Hello = 1
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Hello, MyFrame::OnHello)
EVT_MENU(wxID_EXIT, MyFrame::OnExit)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
wxEND_EVENT_TABLE()
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame( "Hello World", wxPoint(50,50), wxSize(450,340) );
frame->Show( true );
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
"Help string shown in status bar for this menu item");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, "&File" );
menuBar->Append( menuHelp, "&Help" );
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText( "Welcome to wxWidgets!" );
}
void MyFrame::OnExit(wxCommandEvent& event)
{
Close( true );
}
void MyFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox( "This is a wxWidgets' Hello world sample",
"About Hello World", wxOK | wxICON_INFORMATION );
}
void MyFrame::OnHello(wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
} }
@endcode @endcode