Class: Iup::Widget

Inherits:
Object
  • Object
show all
Extended by:
AttributeBuilders
Includes:
CallbackSetter
Defined in:
lib/wrapped/widget.rb

Overview

This class provides shared functionality across all widgets in the IUP library.

Every widget has a handle, which is often a C pointer:

p Iup::Button.new.handle
# => #<FFI::Pointer address=0x0000647e530e6a80>

The Widget class provides attributes supporting appearance, including whether the widget is active, its size, font and color, and its position in the z-order list (where widgets are overlapping, and the top one is visible).

Some common callbacks are also found in this class, responding to:

  • map/unmap, where the native interface elements are created. Some child widgets require their parent to be mapped first.

  • focus events.

  • keyboard/mouse events.

Example

The following code creates a button with large red text, which reports when the mouse moves over the button and any key is pressed:

Iup::mainloop do
  btn = Iup::Button.new("hello") do
    it.fgcolor = "255 0 0"
    it.font = "Times, Bold 18"
    it.enterwindow_cb = ->{ puts "on button" }
    it.leavewindow_cb = ->{ puts "off button" }
    it.k_any = ->(k){ puts "Key #{k.chr}" }
  end

  Iup::Dialog.new(btn).show
end

Constant Summary collapse

@@controlslib =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Instance Attribute Details

#handleObject

Unique reference to widget.



44
45
46
# File 'lib/wrapped/widget.rb', line 44

def handle
  @handle
end

Instance Method Details

#activeObject

:attr: active Enables or prevents user interaction. Typically, a widget will appear greyed-out when inactive. Values ‘yes’ / ‘no’.



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

define_attribute :active

#assign_handle(name) ⇒ Object

Sets the internal handle name for this widget.



47
48
49
# File 'lib/wrapped/widget.rb', line 47

def assign_handle name
  IupLib.IupSetHandle(name, @handle)
end

#bgcolorObject

:attr: bgcolor Specify RGB values for background color, as “r g b”.



73
# File 'lib/wrapped/widget.rb', line 73

define_attribute :bgcolor

#destroyObject

When no longer required, call destroy to have the C backend reclaim the widget.



120
121
122
# File 'lib/wrapped/widget.rb', line 120

def destroy
  IupLib.IupDestroy @handle
end

#enterwindow_cb=(callback) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/wrapped/widget.rb', line 201

def enterwindow_cb= callback
  unless callback.arity.zero?
    raise ArgumentError, 'enterwindow_cb callback must take 0 arguments'
  end
  cb = Proc.new do |ih|
    callback.call
  end
  define_callback cb, 'ENTERWINDOW_CB', :plain
end

#fgcolorObject

:attr: fgcolor Specify RGB values for foreground color, as “r g b”.



78
# File 'lib/wrapped/widget.rb', line 78

define_attribute :fgcolor

#fontObject

:attr: font Sets the widget’s font, using a font description as “<font face>, <font styles> <font size>”, e.g. “Times, Bold 14”. For portability, the fonts Courier, Helvetica and Times are always mapped to appropriate native system fonts.



86
# File 'lib/wrapped/widget.rb', line 86

define_attribute :font

#getfocus_cb=(callback) ⇒ Object



167
168
169
170
171
172
173
174
175
# File 'lib/wrapped/widget.rb', line 167

def getfocus_cb= callback
  unless callback.arity.zero?
    raise ArgumentError, 'getfocus_cb callback must take 0 arguments'
  end
  cb = Proc.new do |ih|
    callback.call
  end
  define_callback cb, 'GETFOCUS_CB', :plain
end

#help_cb=(callback) ⇒ Object



253
254
255
256
257
258
259
260
261
# File 'lib/wrapped/widget.rb', line 253

def help_cb= callback
  unless callback.arity.zero?
    raise ArgumentError, 'help_cb callback must take 0 arguments'
  end
  cb = Proc.new do |ih|
    callback.call
  end
  define_callback cb, 'HELP_CB', :plain_v
end

#k_any=(callback) ⇒ Object



236
237
238
239
240
241
242
243
244
# File 'lib/wrapped/widget.rb', line 236

def k_any= callback
  unless callback.arity == 1
    raise ArgumentError, 'k_any callback must take 1 argument, a character'
  end
  cb = Proc.new do |ih, c|
    callback.call c
  end
  define_callback cb, 'K_ANY', :i_i
end

#killfocus_cb=(callback) ⇒ Object



184
185
186
187
188
189
190
191
192
# File 'lib/wrapped/widget.rb', line 184

def killfocus_cb= callback
  unless callback.arity.zero?
    raise ArgumentError, 'killfocus_cb callback must take 0 arguments'
  end
  cb = Proc.new do |ih|
    callback.call
  end
  define_callback cb, 'KILLFOCUS_CB', :plain
end

#leavewindow_cb=(callback) ⇒ Object



218
219
220
221
222
223
224
225
226
# File 'lib/wrapped/widget.rb', line 218

def leavewindow_cb= callback
  unless callback.arity.zero?
    raise ArgumentError, 'leavewindow_cb callback must take 0 arguments'
  end
  cb = Proc.new do |ih|
    callback.call
  end
  define_callback cb, 'LEAVEWINDOW_CB', :plain
end

#map_cb=(callback) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/wrapped/widget.rb', line 133

def map_cb= callback
  unless callback.arity.zero?
    raise ArgumentError, 'map_cb callback must take 0 arguments'
  end
  cb = Proc.new do |ih|
    callback.call
  end
  define_callback cb, 'MAP_CB', :plain
end

#maxsizeObject

:attr: maxsize Maximum size of widget, value as “widthxheight”.



96
# File 'lib/wrapped/widget.rb', line 96

define_attribute :maxsize

#minsizeObject

:attr: minsize Minimum size of widget, value as “widthxheight”.



101
# File 'lib/wrapped/widget.rb', line 101

define_attribute :minsize

#open_controlsObject

Ensure the controls library is opened



54
55
56
57
58
59
# File 'lib/wrapped/widget.rb', line 54

def open_controls
  unless @@controlslib
    ControlsLib.IupControlsOpen
    @@controlslib = true
  end
end

#sizeObject

:attr: size Size of the widget, in character units, value as “widthxheight”.



106
# File 'lib/wrapped/widget.rb', line 106

define_attribute :size

#unmap_cb=(callback) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/wrapped/widget.rb', line 150

def unmap_cb= callback
  unless callback.arity.zero?
    raise ArgumentError, 'unmap_cb callback must take 0 arguments'
  end
  cb = Proc.new do |ih|
    callback.call
  end
  define_callback cb, 'UNMAP_CB', :plain
end

#visibleObject

:attr: visible Shows or hides the widget. Values ‘yes’ / ‘no’.



91
# File 'lib/wrapped/widget.rb', line 91

define_attribute :visible

#widObject

:attr_reader: wid Native widget identifier.



111
# File 'lib/wrapped/widget.rb', line 111

define_reader :wid

#zorderObject

:attr_writer: zorder Alters z-order, values as ‘top’ / ‘bottom’.



116
# File 'lib/wrapped/widget.rb', line 116

define_writer :zorder