More advanced features: the hello demo

The 'Hello wxWindows' demo (source files, hello.cc and hello.h) shows off some more wxWindows features. When run, two windows pop up. One is the 'main window', with two subwindows - a panel containing various 'widgets', and a text window. The other contains a canvas, drawing some simple shapes, and allowing the user to doodle on it by dragging with the left mouse button. The canvas contents can be scaled and printed out, either to a printer supported by Windows or to PostScript, writing to a file or invoking the printer directly. Under Windows, the graphic may be copied to the clipboard as a metafile.

Both frames can be resized, and the subwindows will be resized in an appropriate manner. The text subwindow can be scrolled; on the panel, a button can be pressed for the program to prompt the user with text with which to set the status bar. Clicking on the list box writes a line of text into the text window.

The File menu has options for selecting the 'mapping mode' (logical dimensions) used in drawing graphics, a zoom option, and an option for loading a file into the text subwindow using a file selector tool.

The Timer menu allows the user to switch a timer on and off; when on, some text gets written to the text subwindow every five seconds.

The Cursor menu enables the canvas cursor to be changed, and lets the potential wxWindows programmer view the available standard cursors.

The About option of the Help menu pops up a dialog box with some information.

This represents a fair amount of GUI functionality for a relatively small program. This is because wxWindows calls are high level (creating a working text window is a single call) and because an object-oriented approach is taken, where much default functionality is provided.

Note also the lack of explicit coordinates or sizes in the panel item creation calls. This is the preferred approach, leaving wxWindows to lay out the items from left to right and top to bottom, with the user interjecting the occasional NewLine call. Explicit positioning is not recommended since it is less device independent, but can be achieved by using more parameters to the creation calls, or by using SetSize after an item has been created. Coordinates and sizes default to -1, which tells wxWindows to choose appropriate positioning and sizing. In this example, the windows are explicitly sized, but you may size a frame or panel to fit around its contents by calling Fit.

MyFrame's OnSize member sizes each subwindow in proportion to the new size of the frame. MyCanvas's OnPaint draws a couple of lines, a rectangle and a spline whenever the canvas requires repainting (e.g. on creation, and when exposed). The OnEvent member checks for mouse dragging, and draws a line from the last point to the current position. Scrolling the canvas and subsequent repainting is handled automatically by wxWindows.

Finally, two callback functions demonstrate popping up dialog boxes, setting the status line, and inserting text into a text window.

This demo can provide a template for your own application. Gradually modify it for your own needs, and you will rapidly be writing portable X and Windows 3.1 programs!