A minimal wxWindows program

The best way to get a feel for how to use a tool is to see a small example. The supplied demo 'minimal' (source file minimal.cc) shows a rudimentary wxWindows program. It has a main window with a panel inside it, displaying a message. There is a menu bar with a File menu which in turn has a Quit option, and the program has its own icon. Under Windows 3.1, the system menu shows the usual options including Minimize, Maximize and Close, and under X, there is a similar pull-down system menu provided by the current window manager. The window is resizeable, and the panel automatically resizes to fit its parent.

Look at minimal.cc.

/*
 * File:     minimal.cc
 * Purpose:  Minimal wxWindows app
 *
 *                       wxWindows 1.50
 * Copyright (c) 1993 Artificial Intelligence Applications Institute,
 *                   The University of Edinburgh
 *
 *                     Author: Julian Smart
 *                        Date: 7-9-93
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice, author statement and this permission
 * notice appear in all copies of this software and related documentation.
 *
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
 * IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL THE ARTIFICIAL INTELLIGENCE APPLICATIONS INSTITUTE OR THE
 * UNIVERSITY OF EDINBURGH BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF
 * DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH
 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "wx.h"

// Define a new application type
class MyApp: public wxApp
{ public:
    wxFrame *OnInit(void);
};

// Define a new frame type
class MyFrame: public wxFrame
{ public:
    MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
    void OnMenuCommand(int id);
};

// ID for the menu quit command
#define MINIMAL_QUIT 1

// This statement initializes the whole application and calls OnInit
MyApp myApp;

// `Main program' equivalent, creating windows and returning main app frame
wxFrame *MyApp::OnInit(void)
{
  // Create the main frame window
  MyFrame *frame = new MyFrame(NULL, "Minimal wxWindows App", 50, 50, 400, 300);

  // Give it an icon
#ifdef wx_msw
  frame->SetIcon(new wxIcon("aiai_icn"));
#endif
#ifdef wx_x
  frame->SetIcon(new wxIcon("aiai.xbm"));
#endif

  // Make a menubar
  wxMenu *file_menu = new wxMenu;
  file_menu->Append(MINIMAL_QUIT, "Quit");
  wxMenuBar *menu_bar = new wxMenuBar;
  menu_bar->Append(file_menu, "File");
  frame->SetMenuBar(menu_bar);

  // Make a panel with a message
  wxPanel *panel = new wxPanel(frame, 0, 0, 400, 300);
  (void)new wxMessage(panel, "Hello, this is a minimal wxWindows program!", 0, 0);

  // Show the frame
  frame->Show(TRUE);

  // Return the main frame window
  return frame;
}

// My frame constructor
MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h):
  wxFrame(frame, title, x, y, w, h)
{}

// Intercept menu commands
void MyFrame::OnMenuCommand(int id)
{
  switch (id) {
    case MINIMAL_QUIT:
      delete this;
    break;
  }
}

The statement #include "wx.h" provides the program with access to all the wxWindows classes and functions.

The first class declaration, MyApp, declares a new application, overriding one member function OnInit. This is an essential part of writing a wxWindows program, since OnInit is the equivalent of main in a normal C++ program.

The class MyFrame declares a constructor, and a message handler for intercepting menu commands.

The definition of the global variable myApp looks innocuous enough but this starts the whole application going simply by being defined.

The OnInit MyApp member function does the initialization of the program. It creates a main frame, sets the icon, creates a menu bar, a panel, and a panel item.

The MyFrame constructor may seem a little pointless, but it fulfils the requirements of C++ syntax in defining the constructor in terms of its parent's constructor.

The OnMenuCommand definition intercepts menu commands for the main frame. If the option chosen is Quit, the application terminates by deleting the main frame. Normally any other existing frames should be deleted (subframes are deleted automatically); these calls are usually put in the frame's OnClose handler so that a system-generated OnClose event will enable the program to clean itself up first. System-generated OnClose events delete the main frame after calling OnClose, so this should not be done from within OnClose.