Skip to content

HowTo : Application aspects

Martin Corino edited this page Mar 21, 2024 · 3 revisions
     About      FAQ      User Guide      Reference documentation

Application Structure

To get a wxRuby application going, you will need to derive a Wx::App class and implement Wx::App#on_init in which you will typically create your application main top-level window.

This window can be a Wx::Frame or a Wx::Dialog and may contain one or more instances of classes such as Wx::Panel, Wx::SplitterWindow or other windows and controls. These windows can be created from Ruby code or loaded from resource definitions in XRC format.

A frame can have a Wx::MenuBar, a Wx::ToolBar, a Wx::StatusBar, and a Wx::Icon for when the frame is iconized.

A Wx::Panel is used to place controls (classes derived from Wx::Control) which are used for user interaction. Examples of controls are Wx::Button, Wx::CheckBox, Wx::Choice, Wx::ListBox, Wx::RadioBox, and Wx::Slider. Such controls need to be positioned correctly – and also repositioned when the top-level window is resized by the user – and to do this you use Wx::Sizer-derived classes, such as Wx::BoxSizer and Wx::FlexGridSizer, to layout everything correctly.

Instances of Wx::Dialog can also be used for controls and they have the advantage of not requiring a separate panel inside them.

Instead of creating a dialog box and populating it with items, it is possible to choose one of the convenient common dialog classes, such as Wx::MessageDialog and Wx::FileDialog.

Drawing on the Screen

You never draw directly onto a window – you use either one of the older device context (DC) classes or the newer graphics context (GC) one, that support features such as alpha transparency or anti-aliasing. Wx::DC is the base for Wx::ClientDC, Wx::PaintDC, Wx::MemoryDC, Wx::BufferedDC, Wx::BufferedPaintDC, Wx::AutoBufferedPaintDC, Wx::PostScriptDC, Wx::ScreenDC, Wx::MirrorDC, Wx::SVGFileDC, Wx::GCDC and Wx::PrinterDC. If your drawing functions have Wx::DC as a parameter, you can pass any of these DCs to the function, and thus use the same code to draw to several different devices. You can draw using the member functions of [Wx::DC](https://mcorino.github.io/wxRuby3/Wx/DC.html, such as Wx::DC#draw_line and Wx::DC#draw_text. Control colour on a window (Wx::Colour)) with brushes (Wx::Brush) and pens (Wx::Pen).

With Wx::GraphicsContext, you create it using one of the methods of Wx::GraphicsRenderer and then construct your drawing from Wx::GraphicsPath objects, finally using Wx::GraphicsContext#stroke_path or Wx::GraphicsContext#fill_path.

Event Handling

GUI programs spend most of their time waiting for the user-initiated events – and then processing them. To do it, you use either one of the named event connector methods (like Wx::EvtHandler#evt_menu, Wx::EvtHandler#evt_button etc.) or Wx::EvtHandler#connect to specify the handler for an event of the given time. Event handlers receive the object describing the event, such as Wx::KeyEvent or Wx::MouseEvent, and perform whichever action corresponds to it. See events handling overview for much more information about this subject.

Clone this wiki locally