Wx::Button

From WxPerl wiki

Jump to: navigation, search

A button is a control that contains a text string, and is one of the most common elements of a GUI. It may be placed on a dialog box or panel, or indeed almost any other window.

Contents

Inheritance

Wx::Button derives from Wx::Control, Wx::Window, Wx::EvtHandler, and Wx::Object.

Styles

The following styles apply to buttons. These symbols are exported by the Wx module (individually, or by using the :button tag, or by using the :everything tag).

  • wxBU_LEFT — Left-justifies the label. Windows and GTK+ only.
  • wxBU_TOP — Aligns the label to the top of the button. Windows and GTK+ only.
  • wxBU_RIGHT — Right-justifies the bitmap label. Windows and GTK+ only.
  • wxBU_BOTTOM — Aligns the label to the bottom of the button. Windows and GTK+ only.
  • wxBU_EXACTFIT — Creates the button as small as possible instead of making it of the standard size (which is the default behaviour ).
  • wxNO_BORDER — Creates a flat button. Windows and GTK+ only.

These values may be combined with the | (or) operator.

Events

EVT_BUTTON ($handling_object, $button, \&handler );

To attach an event handler to a button:

Example

 use Wx::Event qw( EVT_BUTTON );
 EVT_BUTTON( 
            $self,           # Object to bind to
            $BTNID,          # ButtonID (or button object)
            \&ButtonClicked  # Method to execute
 );

The handler (the ButtonClicked method in the above example), will be invoked with two parameters: the object instance (like all object methods in Perl—$self in the example above), and the button event (of type Wx::CommandEvent).

Constructor

$button = Wx::Button->new($parent, $id, $label $pos, $size, $style, $validator, $name);

Parameters

  • $parent — The parent of this control. Usually a Wx::Panel or Wx::Dialog object.
  • $id — An integer specifying the window identifier. Usually you will pass -1 to let the system assign an ID automatically.
  • $label — The text to be displayed on the button. Optional.
  • $pos — The window position, a reference to an array containing an x and a y coordinate. The special value [-1, -1] tells the system to choose the window's starting position. Optional.
  • $size — The size of the window frame, a reference to an array containing a width and a height. The special value [-1, -1] tells the system to use a default size. Optional.
  • $style — The button style to use (see above). Optional.
  • $validator — Window validator. Optional.
  • $name — The name of the button. This parameter is used to associate a name with the button, which allows certain windowing systems to search for the button by name, and allows X resources values to be applied to the button by name. Optional.

Methods

Create

$bool = $button->Create(@options);

Used in something called "two-step frame construction". I have not been able to find details on what this is, or why it would be useful.

GetLabel

$str = $button->GetLabel;

Returns the string label for the button.

GetDefaultSize

$size = $button->GetDefaultSize;

Returns the default size for buttons. It's a good idea to make all the buttons in a dialog the same size; this function retrieves the (platform and current font dependent) size that should be best suited for this.

SetDefault

$button->SetDefault;

Sets the button to be the default item for the panel or dialog box.

Remarks

Under Windows, only dialog box buttons respond to this function. As normal under Windows and Motif, pressing return causes the default button to be depressed when the return key is pressed. See also Wx::Window::SetFocus which sets the keyboard focus for windows and text panel items, and Wx::TopLevelWindow::SetDefaultItem.

Note that under Motif, calling this function immediately after creation of a button and before the creation of other buttons will cause misalignment of the row of buttons, since default buttons are larger. To get around this, call SetDefault after you have created a row of buttons: wxWidgets will then set the size of all buttons currently on the panel to the same size.

SetLabel

$button->SetLabel($new_label);

Sets the string label for the button.

See also

See Jouke's tutorial - http://www.perl.com/pub/a/2001/09/12/wxtutorial1.html - for a full example.

Personal tools
Google AdSense