Class: Iup::List

Inherits:
Widget show all
Includes:
ButtonCallback, DragDropAttributes
Defined in:
lib/wrapped/list.rb

Overview

A dynamic control presenting a list of options to the user. The list may be visible, or a hidden, drop-down list. Optionally, items may be editable.

Example

(1) Simple list, responding to double-click selection:

Iup::List.new do |l|
  l.item(1, "Java")
  l.item(2, "Ruby")
  l.item(3, "Scheme")
  l.spacing = 4
  l.size = "80x100"
  l.dblclick_cb = ->(index, text) {
    puts "DBL CLICK ON #{index} with #{text}"
    Iup::DEFAULT
  }
end

(2) Dropdown list, responding to changes in value:

Iup::List.new do |l|
  l.item(1, "Java")
  l.item(2, "Ruby")
  l.item(3, "Scheme")
  l.spacing = 4
  l.dropdown = 'yes'
  l.valuechanged_cb = ->{
    puts "New value index is #{l.value}, text is #{l.item(l.value)}"
    Iup::DEFAULT
  }
end

Note that the C implementation of Iup’s list is 1-indexed: this is preserved in the wrapped version, for consistency.

Instance Attribute Summary

Attributes inherited from Widget

#handle

Instance Method Summary collapse

Methods included from ButtonCallback

#button_cb=

Methods included from DragDropAttributes

#dragbegin_cb=, #dragdata_cb=, #dragdatasize_cb=, #dragend_cb=, #dragsource, #dragsourcemove, #dragtypes, #dropdata_cb=, #dropmotion_cb=, #droptarget, #droptypes

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 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 CallbackSetter

#define_callback

Constructor Details

#initialize {|_self| ... } ⇒ List

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Iup::List)

    the object that the method was called on



71
72
73
74
75
76
# File 'lib/wrapped/list.rb', line 71

def initialize 
  @handle = IupLib.IupList(nil)

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

Instance Method Details

#action=(callback) ⇒ Object



392
393
394
395
396
397
398
399
400
# File 'lib/wrapped/list.rb', line 392

def action= callback
  unless callback.arity == 3
    raise ArgumentError, 'action callback must take 3 arguments: (text, item, state)'
  end
  cb = Proc.new do |ih, text, item, state|
    callback.call text, item, state
  end
  define_callback cb, 'ACTION', :sii_i
end

#appendObject

:attr_writer: append (with editbox set) appends given string to end of list.



83
# File 'lib/wrapped/list.rb', line 83

define_writer :append

#appenditem(text, image = nil) ⇒ Object

Adds given item to end of list.

  • text - required text label

  • image - optional image reference / name.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/wrapped/list.rb', line 88

def appenditem(text, image=nil)
  IupLib.IupSetAttribute(@handle, 'APPENDITEM', text)
  case image
  when NilClass
    ;
  when String
    IupLib.SetAttibute(@handle, "IMAGE#{count}", image)
  when ImageWidget
    image_name = IupLib.IupGetName(name).first
    if image_name.nil? or image_name.empty?
      image_name = SecureRandom.uuid
      image.assign_handle image_name
    end
    IupLib.IupSetAttribute(@handle, "IMAGE#{count}", image_name)
  end
end

#autohideObject

:attr: autohide If set, scrollbars are only shown if necessary, values ‘yes’ / ‘no’.



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

define_attribute :autohide

#canfocusObject

:attr: canfocus If set, the control can gain focus, values ‘yes’ / ‘no’.



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

define_attribute :canfocus

#caretObject

:attr: caret (with editbox set) ‘col’, in single-line mode, or ‘lin,col’ in multi-line mode.



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

define_attribute :caret

#caret_cb=(callback) ⇒ Object



414
415
416
417
418
419
420
421
422
# File 'lib/wrapped/list.rb', line 414

def caret_cb= callback
  unless callback.arity == 3
    raise ArgumentError, 'caret_cb callback must take 3 arguments: (line, column, position)'
  end
  cb = Proc.new do |ih, lin, col, pos|
    callback.call lin, col, pos
  end
  define_callback cb, 'CARET_CB', :iii_i
end

#caretposObject

:attr: caretpos (with editbox set) Index of character of the insertion point.



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

define_attribute :caretpos

#clipboardObject

:attr_writer: clipboard (with editbox set) ‘clear’ / ‘copy’ / ‘cut’ / ‘paste’. Access the clipboard with the current selection.



129
# File 'lib/wrapped/list.rb', line 129

define_writer :clipboard

#countObject

:attr_reader: count Returns the number of items in the list.



134
# File 'lib/wrapped/list.rb', line 134

define_reader :count

#dblclick_cb=(callback) ⇒ Object



433
434
435
436
437
438
439
440
441
# File 'lib/wrapped/list.rb', line 433

def dblclick_cb= callback
  unless callback.arity == 2
    raise ArgumentError, 'dblclick_cb callback must take 2 arguments: index, text'
  end
  cb = Proc.new do |ih, index, text|
    callback.call index, text
  end
  define_callback cb, 'DBLCLICK_CB', :is_i
end

#dragdrop_cb=(callback) ⇒ Object



456
457
458
459
460
461
462
463
464
# File 'lib/wrapped/list.rb', line 456

def dragdrop_cb= callback
  unless callback.arity == 4
    raise ArgumentError, 'dragdrop_cb callback must take 4 arguments: (drag_id, drop_id, isshift, iscontrol)'
  end
  cb = Proc.new do |ih, drag_id, drop_id, isshift, iscontrol|
    callback.call drag_id.to_i, drop_id.to_i, (isshift != 0), (iscontrol != 0)
  end
  define_callback cb, 'DRAGDROP_CB', :iiii_i
end

#dragdroplistObject

:attr: dragdroplist If yes, enables drag and drop between lists: ‘yes’ / ‘no’.



139
# File 'lib/wrapped/list.rb', line 139

define_attribute :dragdroplist

:attr: dropdown ‘yes’ / ‘no’, if set, only the selected item is visible.



151
# File 'lib/wrapped/list.rb', line 151

define_attribute :dropdown



474
475
476
477
478
479
480
481
482
# File 'lib/wrapped/list.rb', line 474

def dropdown_cb= callback
  unless callback.arity == 1
    raise ArgumentError, 'dropdown_cb callback must take 1 argument, shown'
  end
  cb = Proc.new do |ih, state|
    callback.call (state == 1)
  end
  define_callback cb, 'DROPDOWN_CB', :i_i
end

#dropfilestargetObject

:attr: dropfilestarget Enable or disable the drop of files: ‘no’ / ‘yes’. Automatically set to ‘yes’ if dropfiles_cb is defined when list is mapped.



146
# File 'lib/wrapped/list.rb', line 146

define_attribute :dropfilestarget

#edit_cb=(callback) ⇒ Object



494
495
496
497
498
499
500
501
502
# File 'lib/wrapped/list.rb', line 494

def edit_cb= callback
  unless callback.arity == 2
    raise ArgumentError, 'edit_cb callback must take 2 arguments: (character, new_value)'
  end
  cb = Proc.new do |ih, c, text|
    callback.call c, text
  end
  define_callback cb, 'EDIT_CB', :is_i
end

#editboxObject

:attr: editbox ‘yes’ / ‘no’, if set adds an editable box to the list.



156
# File 'lib/wrapped/list.rb', line 156

define_attribute :editbox

#expandObject

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



162
# File 'lib/wrapped/list.rb', line 162

define_attribute :expand

#insertObject

:attr_writer: insert (with editbox set) Places a given string at current caret position, overwriting any current selection.



168
# File 'lib/wrapped/list.rb', line 168

define_writer :insert

#insertitem(index, text, image = nil) ⇒ Object

Insert item at given index.

  • index - index to place item (1-indexed)

  • text - required text label

  • image - optional image reference / name.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/wrapped/list.rb', line 174

def insertitem(index, text, image=nil)
  IupLib.IupSetAttribute @handle, "INSERTITEM#{index}", text
  case image
  when NilClass
    ;
  when String
    IupLib.IupSetAttibute @handle, "IMAGE#{index}", image
  when ImageWidget
    image_name = IupLib.IupGetName(name).first
    if image_name.nil? or image_name.empty?
      image_name = SecureRandom.uuid
      image.assign_handle image_name
    end
    IupLib.IupSetAttribute @handle, "IMAGE#{index}", image_name
  end
end

#item(index, text = nil, image = nil) ⇒ Object

:call-seq:

list.item(index)  # retrieves item 
list.item(index, text) # sets text 
list.item(index, text, image) # sets text and image

Accesses given index position.

  • index - index of item (1-indexed)

  • text - text label (nil to retrieve)

  • image - optional image reference / name.



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/wrapped/list.rb', line 200

def item(index, text=nil, image=nil)
  if text.nil?
    IupLib.IupGetAttribute(@handle, index.to_s).first
  else
    IupLib.IupSetAttribute(@handle, index.to_s, text)
    case image
    when NilClass
      ;
    when String
      IupLib.IupSetAttribute(@handle, "IMAGE#{index}", image)
    when ImageWidget
      IupLib.IupSetAttributeHandle(@handle, "IMAGE#{index}", image.handle)
    end
    text
  end
end

#maskObject

:attr: mask (with editbox set) Defines a mask to filter text input. See pre-defined masks.



221
# File 'lib/wrapped/list.rb', line 221

define_attribute :mask

#motion_cb=(callback) ⇒ Object



513
514
515
516
517
518
519
520
521
# File 'lib/wrapped/list.rb', line 513

def motion_cb= callback
  unless callback.arity == 3
    raise ArgumentError, 'motion_cb callback must take 3 arguments: (x, y, state)'
  end
  cb = Proc.new do |ih, x, y, state|
    callback.call x, y, state
  end
  define_callback cb, 'MOTION_CB', :iis_i
end

#multipleObject

:attr: multiple If set, allows selection of multiple items: values ‘yes’ / ‘no’. Only available if editbox = dropdown = no.



227
# File 'lib/wrapped/list.rb', line 227

define_attribute :multiple

#multiselect_cb=(callback) ⇒ Object



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/wrapped/list.rb', line 532

def multiselect_cb= callback
  unless callback.arity == 2
    raise ArgumentError, 'multiselect_cb callback must take 2 arguments, the array of selected and array of deselected items'
  end
  cb = Proc.new do |ih, val_ptr|
    val = FFI::Pointer.new(val_ptr).read_string
    selected = []
    deselected = []
    val.split('').each_with_index do |item, index|
      selected << index+1 if item == '+'
      deselected << index+1 if item == '-'
    end
    callback.call selected, deselected
  end
  define_callback cb, 'MULTISELECT_CB', :s_i
end

#ncObject

:attr: nc (with editbox set) Maximum number of characters allowed for keyboard input (0 allows an indefinite number).



233
# File 'lib/wrapped/list.rb', line 233

define_attribute :nc

#paddingObject

:attr: padding (with editbox set) Margin in x and y directions, value as “mxn”.



238
# File 'lib/wrapped/list.rb', line 238

define_attribute :padding

#positionObject

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



243
# File 'lib/wrapped/list.rb', line 243

define_reader :position

#rastersizeObject

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



248
# File 'lib/wrapped/list.rb', line 248

define_attribute :rastersize

#readonlyObject

:attr: readonly (with editbox set) If set, prevents the user changing contents: values ‘yes’ / ‘no’.



254
# File 'lib/wrapped/list.rb', line 254

define_attribute :readonly

#removeitem(item) ⇒ Object

Removes given item from the list:

  • list - index of item to remove (1-indexed), or ‘all’, to remove all items.



258
259
260
# File 'lib/wrapped/list.rb', line 258

def removeitem item
  IupLib.IupSetAttribute(@handle, "REMOVEITEM", item.to_s)
end

#screenpositionObject

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



265
# File 'lib/wrapped/list.rb', line 265

define_reader :screenposition

#scrollbarObject

:attr: scrollbar When in multiline mode, enables a scrollbar. Values ‘vertical’ / ‘horizontal’ / ‘yes’ / ‘no’.



271
# File 'lib/wrapped/list.rb', line 271

define_attribute :scrollbar

#scrolltoObject

:attr_writer: scrollto (with editbox set) ‘col’ Scrolls to make given column number visible, in single-line mode. ‘lin/col’ Scrolls to make line and column number visible, in multi-line mode.



279
# File 'lib/wrapped/list.rb', line 279

define_writer :scrollto

#scrolltoposObject

:attr_writer: scrolltopos (with editbox set) Scrolls to make character ‘number’ visible.



284
# File 'lib/wrapped/list.rb', line 284

define_writer :scrolltopos

#selectedtextObject

:attr: selectedtext (with editbox set) Reads or overwrites the current selection. Does nothing if no text is selected.



290
# File 'lib/wrapped/list.rb', line 290

define_attribute :selectedtext

#selectionObject

:attr: selection (with editbox set) Selects text as ‘col1:col2’ (single-line) / ‘lin1,col1:lin2,col2’ (multi-line) / ‘all’ / ‘none’.



296
# File 'lib/wrapped/list.rb', line 296

define_attribute :selection

#selectionposObject

:attr: selectionpos (with editbox set) Selects text between character positions: ‘pos1:pos2’ / ‘all’ / ‘none’.



302
# File 'lib/wrapped/list.rb', line 302

define_attribute :selectionpos

#showdragdropObject

:attr: showdragdrop If set, enables internal drag/drop: values ‘yes’ / ‘no’.



307
# File 'lib/wrapped/list.rb', line 307

define_attribute :showdragdrop

#showdropdownObject

:attr_writer: showdropdown Shows the dropdown list, if dropdown=yes.



312
# File 'lib/wrapped/list.rb', line 312

define_writer :showdropdown

#sortObject

:attr: sort Forces items to be sorted alphabetically. Insert/append operations will ignore the index number.



320
# File 'lib/wrapped/list.rb', line 320

define_attribute :sort

#spacingObject

:attr: spacing Space between items, value as a number.



325
# File 'lib/wrapped/list.rb', line 325

define_attribute :spacing

#tipObject

:attr: tip Tooltip string.



330
# File 'lib/wrapped/list.rb', line 330

define_attribute :tip

#valueObject



341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/wrapped/list.rb', line 341

def value 
  item_str = IupLib.IupGetAttribute(@handle, 'VALUE').first
  if multiple == 'YES'
    result = []
    item_str.split('').each_with_index do |item, index|
      result << index+1 if item == '+'
    end
    return result
  else
    return item_str
  end
end

#value=(val) ⇒ Object

:nodoc:



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'lib/wrapped/list.rb', line 354

def value= val # :nodoc:
  if multiple == 'YES'
    result = ""
    count.to_i.times do |i|
      if val.include?(i+1)
        result << '+'
      else
        result << '-'
      end
    end
    IupLib.IupSetAttribute(@handle, 'VALUE', result)
  else
    IupLib.IupSetAttribute(@handle, 'VALUE', val.to_s)
  end
end

#valuechanged_cb=(callback) ⇒ Object



556
557
558
559
560
561
562
563
564
# File 'lib/wrapped/list.rb', line 556

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

#visiblecolumnsObject

:attr: visiblecolumns The minimum number of visible columns, defaults to 5.



373
# File 'lib/wrapped/list.rb', line 373

define_attribute :visiblecolumns

#visiblelinesObject

:attr: visiblelines The minimum number of visible lines, when dropdown=no.



378
# File 'lib/wrapped/list.rb', line 378

define_attribute :visiblelines