Module: WxSugar::EnumerableControl
- Includes:
- Enumerable
- Included in:
- Wx::ControlWithItems, Wx::ListCtrl
- Defined in:
- lib/wx_sugar/enumerable_controls.rb
Overview
This module enhances the family of GUI controls which display a list of labelled items and allow the user to select one or more items. Classes of this type include Wx::Choice, Wx::ComboBox and Wx::ListBox. This module allows easy iteration over the contents of these object using Ruby’s each
and Enumerable methods.
Note that including this file on its own won’t enable this behaviour for any classes; you should load it via the class extensions for control_with_items.rb
and listctrl.rb
.
Defined Under Namespace
Modules: ClassMethods Classes: ItemCollection, ItemDataCollection, ListItemTextCollection, StringsCollection
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(str) ⇒ Object
Appends this string to the control.
-
#delete_if ⇒ Object
Deletes any items in the control for which the passed block evaluates to
true
. -
#each(&block) ⇒ Object
Iterates over the items in the control.
-
#index(value) ⇒ Object
Returns the index of the item in the control corresponding to
value
.
Class Method Details
.included(klass) ⇒ Object
19 20 21 |
# File 'lib/wx_sugar/enumerable_controls.rb', line 19 def self.included(klass) klass.extend ClassMethods end |
Instance Method Details
#<<(str) ⇒ Object
Appends this string to the control
63 64 65 |
# File 'lib/wx_sugar/enumerable_controls.rb', line 63 def <<(str) append(str) end |
#delete_if ⇒ Object
Deletes any items in the control for which the passed block evaluates to true
.
69 70 71 72 73 |
# File 'lib/wx_sugar/enumerable_controls.rb', line 69 def delete_if deletions = [] each { | i | deletions << i if yield i } deletions.reverse.each { | i | delete(i) } end |
#each(&block) ⇒ Object
Iterates over the items in the control. In its simplest form, it passes the index of each item into the passed block:
control.each | i |
puts "String at #{i} is '#{control.string(i)}'"
end
The passed block may have up to three block parameters, i
, string
, and data
. The second parameter, if requested, is filled with the string label of the item within the control. The third parameter is filled with the client data associated with the item. Note that if you don’t need the string or client data, it is more efficient to iterate over the indexes only.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/wx_sugar/enumerable_controls.rb', line 36 def each(&block) last = get_count - 1 case block.arity when 1 0.upto(last) { | i | yield i } when 2 0.upto(last) { | i | yield i, get_string(i) } when 3 0.upto(last) { | i | yield i, get_string(i), get_item_data(i) } else raise ArgumentError, "Invalid number of block parameters" end end |
#index(value) ⇒ Object
Returns the index of the item in the control corresponding to value
. value
may be a string label, or it may be any ruby object set as client data to a list item. Returns nil
if nothing matches.
53 54 55 56 57 58 59 60 |
# File 'lib/wx_sugar/enumerable_controls.rb', line 53 def index(value) # -1 means 'not found' in WxWidgets if value.kind_of?(String) && ( i = find_string(value, true) ) && i > -1 return i end indices = (0 ... get_count).to_a return indices.find { | i | get_item_data(i) == value } end |