Class: Iup::Button

Inherits:
Widget show all
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.

(1) A button with some text and an action

Iup::Button.new('click me', ->{
  puts 'clicked'
  Iup::DEFAULT
})

(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'
  Iup::DEFAULT
end

Iup:Button.new('', ->{ click_fn }) do |b|
  b.image = img
  b.padding = '50x20'
end

(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.

Iup::Button.new('e&xit', ->{ puts 'exit'; Iup::CLOSE }) do |b|
  b.image = img
  b.imageposition = 'top'
end

Instance Attribute Summary

Attributes inherited from Widget

#handle

Instance Method Summary collapse

Methods included from ButtonCallback

#button_cb=

Methods included from ImageAttributes

#image, #image=, #iminactive, #iminactive=, #impress, #impress=

Methods included from AttributeReference

#attribute_reference

Methods inherited from Widget

#active, #assign_handle, #bgcolor, #destroy, #enterwindow_cb=, #fgcolor, #font, #getfocus_cb=, #help_cb=, #k_any=, #killfocus_cb=, #leavewindow_cb=, #map_cb=, #maxsize, #minsize, #open_controls, #size, #unmap_cb=, #visible, #wid, #zorder

Methods included from AttributeBuilders

#define_attribute, #define_id_attribute, #define_id_reader, #define_id_writer, #define_property_attribute, #define_property_reader, #define_property_writer, #define_reader, #define_writer

Methods included from CallbackSetter

#define_callback

Constructor Details

#initialize(title = '', callback = nil) {|_self| ... } ⇒ Button

Creates an instance of a button. If a block is given, the new instance is yielded to it.

  • title - the text to display on the button.

  • callback - procedure to call when button is left-clicked, passed to #action.

Yields:

  • (_self)

Yield Parameters:

  • _self (Iup::Button)

    the object that the method was called on



45
46
47
48
49
50
51
52
# File 'lib/wrapped/button.rb', line 45

def initialize(title='', callback = nil)
  @handle = IupLib.IupButton(title, nil)

  self.action = callback unless callback.nil?

  # run any provided block on instance, to set up further attributes
  yield self if block_given?
end

Instance Method Details

#action=(callback) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/wrapped/button.rb', line 139

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

#alignmentObject

:attr: alignment Sets the horizontal and vertical alignment. The value is a string “horizontal:vertical”, with horizontal options ALEFT, ACENTER, ARIGHT and vertical options ATOP, ACENTER, ABOTTOM. Partial values also accepted, e.g. “ARIGHT”, “:ATOP”.



63
# File 'lib/wrapped/button.rb', line 63

define_attribute :alignment

#canfocusObject

:attr: canfocus Enables the control to gain focus. Values ‘yes’ / ‘no’.



68
# File 'lib/wrapped/button.rb', line 68

define_attribute :canfocus

#expandObject

:attr: expand Allows button to fill available space in indicated direction. Values ‘no’ / ‘horizontal’ / ‘vertical’ / ‘yes’.



74
# File 'lib/wrapped/button.rb', line 74

define_attribute :expand

#flatObject

:attr: alignment Sets the horizontal and vertical alignment. The value is a string “horizontal:vertical”, with options ALEFT, ACENTER, ARIGHT or none.



81
# File 'lib/wrapped/button.rb', line 81

define_attribute :flat

#imagepositionObject

:attr: imageposition Position of image relative to text. Values ‘left’ / ‘right’ / ‘top’ / ‘bottom’/



93
# File 'lib/wrapped/button.rb', line 93

define_attribute :imageposition

#impressborderObject

:attr: impressborder If set, shows button borders even if impress defined. Values ‘yes’ / ‘no’.



87
# File 'lib/wrapped/button.rb', line 87

define_attribute :impressborder

#paddingObject

:attr: padding Margin in x and y directions, value as “mxn”.



98
# File 'lib/wrapped/button.rb', line 98

define_attribute :padding

#positionObject

:attr_reader: position Returns position in pixels within client window as “x,y”.



103
# File 'lib/wrapped/button.rb', line 103

define_reader :position

#rastersizeObject

:attr: rastersize Size of the button, in pixels, value as “widthxheight”.



108
# File 'lib/wrapped/button.rb', line 108

define_attribute :rastersize

#screenpositionObject

:attr_reader: screenposition Returns position in pixels on screen as “x,y”.



113
# File 'lib/wrapped/button.rb', line 113

define_reader :screenposition

#spacingObject

:attr: spacing Space between image and text, value as a number.



118
# File 'lib/wrapped/button.rb', line 118

define_attribute :spacing

#tipObject

:attr: tip Tooltip string.



123
# File 'lib/wrapped/button.rb', line 123

define_attribute :tip

#titleObject

:attr: title Displayed on the button as a text label.



128
# File 'lib/wrapped/button.rb', line 128

define_attribute :title