2008-02-18 19:04:03 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2008-02-24 12:26:29 -05:00
|
|
|
// Name: helloworld.h
|
2008-02-18 19:04:03 -05:00
|
|
|
// Purpose: topic overview
|
|
|
|
// Author: wxWidgets team
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2008-03-12 04:50:42 -04:00
|
|
|
/**
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-12 04:50:42 -04:00
|
|
|
@page overview_helloworld Hello World Example
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-24 12:26:29 -05:00
|
|
|
Many people have requested a mini-sample to be published here
|
2008-02-18 19:04:03 -05:00
|
|
|
so that some quick judgment concerning syntax
|
2008-02-24 12:26:29 -05:00
|
|
|
and basic principles can be made, so here we go.
|
|
|
|
|
|
|
|
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>)
|
|
|
|
or using one global include (<tt>@#include "wx/wx.h"</tt>). This is
|
2008-02-18 19:04:03 -05:00
|
|
|
also useful on platforms which support precompiled headers such
|
2008-02-24 12:26:29 -05:00
|
|
|
as all major compilers on the Windows platform and GCC on Unix platforms.
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
@code
|
|
|
|
//
|
|
|
|
// file name: hworld.cpp
|
|
|
|
//
|
|
|
|
// purpose: wxWidgets "Hello world"
|
|
|
|
//
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
// For compilers that support precompilation, includes "wx/wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
@endcode
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
Practically every app should define a new class derived from wxApp.
|
|
|
|
By overriding wxApp's OnInit() the program can be initialized,
|
2008-02-19 08:28:24 -05:00
|
|
|
e.g. by creating a new main window.
|
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
@code
|
|
|
|
class MyApp: public wxApp
|
|
|
|
{
|
|
|
|
virtual bool OnInit();
|
|
|
|
};
|
|
|
|
@endcode
|
2008-02-19 08:28:24 -05:00
|
|
|
|
|
|
|
The main window is created by deriving a class from wxFrame and
|
2008-02-18 19:04:03 -05:00
|
|
|
giving it a menu and a status bar in its constructor. Also, any class
|
|
|
|
that wishes to respond to any "event" (such as mouse clicks or
|
2008-02-19 08:28:24 -05:00
|
|
|
messages from the menu or a button) must declare an event table
|
2008-03-20 09:45:17 -04:00
|
|
|
using the macro below.
|
2008-02-24 12:26:29 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
displaying an "About" window. These handlers should not be virtual.
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
@code
|
|
|
|
class MyFrame: public wxFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
void OnQuit(wxCommandEvent& event);
|
|
|
|
void OnAbout(wxCommandEvent& event);
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
private:
|
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
};
|
|
|
|
@endcode
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
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.
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
@code
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
ID_Quit = 1,
|
|
|
|
ID_About,
|
|
|
|
};
|
|
|
|
@endcode
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
We then proceed to actually implement an event table in which the events
|
|
|
|
are routed to their respective handler functions in the class MyFrame.
|
2008-02-24 12:26:29 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
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
|
|
|
|
a window on the screen. If -1 is given as the ID, the given handler will be
|
|
|
|
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.
|
2008-02-24 12:26:29 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
The origin of the event can still be distinguished in the event handler as
|
|
|
|
the (only) parameter in an event handler is a reference to a wxEvent object,
|
|
|
|
which holds various information about the event (such as the ID of and a
|
|
|
|
pointer to the class, which emitted the event).
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
@code
|
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
|
|
|
EVT_MENU(ID_Quit, MyFrame::OnQuit)
|
|
|
|
EVT_MENU(ID_About, MyFrame::OnAbout)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
@endcode
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
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.
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
@code
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
@endcode
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
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 main window (or several). The frame should get a title bar text ("Hello World")
|
|
|
|
and a position and start-up size. One frame can also be declared to be the
|
|
|
|
top window. Returning @true indicates a successful initialization.
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
@code
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
|
|
|
MyFrame *frame = new MyFrame( "Hello World", wxPoint(50,50), wxSize(450,340) );
|
2008-03-02 10:18:23 -05:00
|
|
|
frame->Show( true );
|
2008-02-18 19:04:03 -05:00
|
|
|
SetTopWindow( frame );
|
2008-02-24 12:26:29 -05:00
|
|
|
return true;
|
2008-02-18 19:04:03 -05:00
|
|
|
}
|
|
|
|
@endcode
|
2008-02-19 08:28:24 -05:00
|
|
|
|
|
|
|
In the constructor of the main window (or later on) we create a menu with two menu
|
|
|
|
items as well as a status bar to be shown at the bottom of the main window. Both have
|
2008-02-18 19:04:03 -05:00
|
|
|
to be "announced" to the frame with respective calls.
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
@code
|
|
|
|
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
|
2008-02-24 12:26:29 -05:00
|
|
|
: wxFrame((wxFrame *)NULL, -1, title, pos, size)
|
2008-02-18 19:04:03 -05:00
|
|
|
{
|
|
|
|
wxMenu *menuFile = new wxMenu;
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-03-02 10:18:23 -05:00
|
|
|
menuFile->Append( ID_About, "" );
|
|
|
|
menuFile->AppendSeparator();
|
|
|
|
menuFile->Append( ID_Quit, "E" );
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
wxMenuBar *menuBar = new wxMenuBar;
|
2008-03-02 10:18:23 -05:00
|
|
|
menuBar->Append( menuFile, "" );
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
SetMenuBar( menuBar );
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
CreateStatusBar();
|
|
|
|
SetStatusText( "Welcome to wxWidgets!" );
|
|
|
|
}
|
|
|
|
@endcode
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
Here are the actual event handlers. MyFrame::OnQuit() closes the main window
|
|
|
|
by calling Close(). The parameter @true indicates that other windows have no veto
|
2008-02-19 08:28:24 -05:00
|
|
|
power such as after asking "Do you really want to close?". If there is no other
|
2008-02-18 19:04:03 -05:00
|
|
|
main window left, the application will quit.
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
@code
|
|
|
|
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
2008-02-24 12:26:29 -05:00
|
|
|
Close( true );
|
2008-02-18 19:04:03 -05:00
|
|
|
}
|
|
|
|
@endcode
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
MyFrame::OnAbout() will display a small window with some text in it. In this
|
|
|
|
case a typical "About" window with information about the program.
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-18 19:04:03 -05:00
|
|
|
@code
|
|
|
|
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
|
|
|
|
{
|
|
|
|
wxMessageBox( "This is a wxWidgets' Hello world sample",
|
|
|
|
"About Hello World", wxOK | wxICON_INFORMATION );
|
|
|
|
}
|
|
|
|
@endcode
|
2008-02-19 08:28:24 -05:00
|
|
|
|
2008-02-24 12:26:29 -05:00
|
|
|
*/
|
2008-02-19 08:28:24 -05:00
|
|
|
|