Skip to content

HowTo : Frame with panel

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

Creating a wxRuby Frame with Panel

In this section we’ll fully setup a proper Window using wxRuby.

The below code provides a basic implementation of a Wx::Frame used to create the window.

Within this frame is a Wx::Panel widget, which is essentially the "content window" in which all other widgets will be placed. It is possible to create multiple panels as well, creating several areas in which to place widgets. The panel constructor has multiple arguments, but only the (required) parent argument (specifying self, which refers to the frame in this context) is passed.

require 'wx'

class MyFrame < Wx::Frame
  def initialize(title)
    super(nil, title: title)
    panel = Wx::Panel.new(self)
  end
end

Wx::App.run { MyFrame.new('wxRuby Guide').show }

The super() method (calling the inherited Wx::Frame constructor) is provided with an argument for title, otherwise the arguments for the Wx::Frame constructor are left to their default values. Since this window is our main window, parent is passed as nil.

The #show method is inherited from the Wx::Window class (which Wx::Frame is ultimately derived from) and responsible for displaying the window on screen. There will be nothing displayed without this line.

Running the above code will produce the following output:

screenshot1

The syntax for creating a Wx::Frame is as follows:

frame = Wx::Frame.new(parent, id, title, pos, size, style, name)
  • parent : Wx::Window
    The parent window. Can be nil.
  • id : Integer
    Optional Window ID. Default value is Wx::ID_ANY, which gives it the next available ID.
  • title : String
    The caption/name that appears on the topleft corner of the wxRuby Window.
  • pos : Array(Integer,Integer) or Wx::Point
    Optional coordinates for the position of the topleft corner of the window. Default is Wx::DEFAULT_POSITION.
  • size : Array(Integer,Integer) or Wx::Size
    Optional dimensions of the window. Default is Wx::DEFAULT_SIZE.
  • style : Integer
    Optional styling mask for the window. Default is Wx::DEFAULT_FRAME_STYLE.
  • name: Optional name for the window. Default is 'frame'.

Note: As with all windows, keyword constructor alternatives for all arguments but the 'parent' argument are available. See here for more information.

Styling the Window

Here we’ll take a look at some styling options, which will add extra functionality to our window.

Below the available frame window styles. The first style in this list is the default style that is already present whenever you create a Frame. It is in fact a combination of several styles from this list. Hence, if you change the style from the default to some other style, you will lose all of those default styles (in that case you may need to combine various styles yourself).

Frame window style Description
Wx::DEFAULT_FRAME_STYLE A combination of several styles:
Wx::MINIMIZE_BOX | Wx::MAXIMIZE_BOX | Wx::RESIZE_BORDER | Wx::SYSTEM_MENU | Wx::CAPTION | Wx::CLOSE_BOX | Wx::CLIP_CHILDREN
Wx::ICONIZE Display the Frame minimized (WXMSW only)
Wx::CAPTION Puts a Caption Bar on the Frame. You must have this style on for Wx::MINIMIZE_BOX, Wx::MAXIMIZE_BOX and Wx::CLOSE_BOX to work properly on most systems.
Wx::MINIMIZE Display the Frame minimized (WXMSW only)
Wx::MINIMIZE_BOX Enables the minimize option on the Window.
Wx::MAXIMIZE Display the Frame maximized (WXMSW and WXGTK Only)
Wx::MAXIMIZE_BOX Enables the maximize option on the Window.
Wx::CLOSE_BOX Enables the close option on the Window.
Wx::STAY_ON_TOP Makes the Window stay on top of all other windows.
Wx::SYSTEM_MENU Displays a system menu containing the list of various windows commands in the window title bar.
Wx::RESIZE_BORDER Allows for resizing along the borders of the window.
Wx::FRAME_TOOL_WINDOW Causes a frame with a small title bar to be created; the frame does not appear in the taskbar under Windows or GTK.
Wx::FRAME_NO_TASKBAR Creates an otherwise normal frame but it does not appear in the taskbar with WXMSW or WXGTK (note that it will minimize to the desktop window under Windows which may seem strange to the users and thus it might be better to use this style only without Wx::MINIMIZE_BOX style). In WXGTK, the flag is respected only if the window manager supports _NET_WM_STATE_SKIP_TASKBAR hint.
Wx::FRAME_FLOAT_ON_PARENT The frame will always be on top of its parent (unlike Wx::STAY_ON_TOP). A frame created with this style must have a non-null parent.
Wx::FRAME_SHAPED Windows with this style are allowed to have their shape changed with the #set_shape method.

On top of these frame specific styles the common Wx::Window styles can also be used (make sure to carefully read the documentation before using these).

Using Window Styles in wxRuby

The following code snippet shows how to use a window style on a Wx::Frame in wxRuby.

frame = Wx::Frame.new(nil, title: 'Example', style: Wx::MINIMIZE)

See the table above to understand what this style does.

You can also pass in several styles by using the | (bitwise or) operator as shown below.

frame = Wx::Frame.new(nil, title: 'Example', style: Wx::MINIMIZE | Wx::CLOSE_BOX | Wx::RESIZE_BORDER)

Wx::Frame Methods

The following table shows a list of often used methods of (frame) windows.

Method Description
show Displays the Frame on-screen.
centre Centers the Frame to the Screen.
set_size Used to change the size of the size of the Frame.
close Closes the Frame safely.
destroy Destroys the Frame immediately.
disable Disables the Frame.
hide Hides the window.

Check out the reference documentation for the full list of available methods.

Clone this wiki locally