Class: Iup::Button
- Includes:
- ButtonCallback, ImageAttributes
- Defined in:
- lib/wrapped/button.rb
Overview
A button can display some text, an image, or both.
When clicked, a specified action function is called.
The action should return Iup::DEFAULT, or, if the button should exit the application, Iup::CLOSE.
# Example 1: A button with some text and an action
Button.new 'click me', ->{
puts 'clicked'
DEFAULT
}
# Example 2: a button with an image stored in +img+ and
# action specified by a separate method; some padding around
# the image within the button.
def click_fn
puts 'clicked'
DEFAULT
end
Button.new '', ->{ click_fn } do
image img
padding '50x20'
end
# Example 3: a button with text and image, image placed above
# text. The text contains an & before the "x", so the action
# can be triggered using ALT+x, and closes the application.
Button.new 'e&xit', ->{ puts 'exit'; CLOSE } do
image img
imageposition 'top'
end
Attributes
- alignment
-
Sets the horizontal and vertical alignment. The value is a string “horizontal:vertical”, with options ALEFT, ACENTER, ARIGHT or none.
- canfocus
-
Enables the control to gain focus. Values ‘yes’ / ‘no’.
- expand
-
Allows button to fill available space in indicated direction. Values ‘no’ / ‘horizontal’ / ‘vertical’ / ‘yes’.
- flat
-
If set, hides the button’s border until mouse enters the button area. Values ‘yes’ / ‘no’.
- impressborder
-
If set, shows button borders even if impress defined. Values ‘yes’ / ‘no’.
- imageposition
-
Position of image relative to text. Values ‘left’ / ‘right’ / ‘top’ / ‘bottom’/
- padding
-
Margin in x and y directions, value as “mxn”.
- position
-
read-only returns position in pixels within client window as “x,y”.
- rastersize
-
Size of the button, in pixels, value as “widthxheight”.
- screenposition
-
read-only returns position in pixels on screen as “x,y”.
- spacing
-
Space between image and text, value as a number.
- tip
-
Tooltip string.
- title
-
Label text displayed on the button.
Instance Attribute Summary
Attributes inherited from Widget
Instance Method Summary collapse
-
#action(callback) ⇒ Object
Action generated when the button 1 (usually left) is selected.
-
#initialize(title = '', callback = nil, &block) ⇒ Button
constructor
Creates an instance of a button.
Methods included from ButtonCallback
Methods included from ImageAttributes
Methods included from AttributeReference
Methods inherited from Widget
#assign_handle, #enterwindow_cb, #getfocus_cb, #help_cb, #k_any, #killfocus_cb, #leavewindow_cb, #map_cb, #open_controls, #unmap_cb
Methods included from AttributeBuilders
#define_attribute, #define_id_attribute, #define_id_readonly, #define_id_writeonly, #define_property_attribute, #define_property_writeonly, #define_readonly, #define_writeonly
Methods included from CallbackSetter
Constructor Details
#initialize(title = '', callback = nil, &block) ⇒ Button
Creates an instance of a button.
- title
-
the text to display on the button.
- callback
-
procedure to call when button is left-clicked.
- block
-
optional block to set up the button’s attributes.
66 67 68 69 70 71 72 73 |
# File 'lib/wrapped/button.rb', line 66 def initialize title='', callback = nil, &block @handle = IupLib.IupButton title, nil action callback unless callback.nil? # run any provided block on instance, to set up further attributes self.instance_eval &block if block_given? end |
Instance Method Details
#action(callback) ⇒ Object
Action generated when the button 1 (usually left) is selected.
95 96 97 98 99 100 101 102 103 |
# File 'lib/wrapped/button.rb', line 95 def action callback unless callback.arity.zero? raise ArgumentError, 'action must take 0 arguments' end cb = Proc.new do |ih| callback.call end define_callback cb, 'ACTION', :plain end |