nobody at rubyforge.org
2007-Apr-02 18:47 UTC
[Wxruby-development] [937] trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb: Initial commit of enumerable_controls.rb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!-- #msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; } #msg dt { float: left; width: 6em; font-weight: bold; } #msg dt:after { content:'':'';} #msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; } #msg dl a { font-weight: bold} #msg dl a:link { color:#fc3; } #msg dl a:active { color:#ff0; } #msg dl a:visited { color:#cc6; } h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; } #msg ul, pre { overflow: auto; } #header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; } #patch { width: 100%; } #patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;} #patch .propset h4, #patch .binary h4 {margin:0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;} #patch .propset .diff, #patch .binary .diff {padding:10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, .info {color:#888;background:#fff;} --></style> <title>[937] trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb: Initial commit of enumerable_controls.rb</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>937</dd> <dt>Author</dt> <dd>brokentoy</dd> <dt>Date</dt> <dd>2007-04-02 14:47:50 -0400 (Mon, 02 Apr 2007)</dd> </dl> <h3>Log Message</h3> <pre>Initial commit of enumerable_controls.rb</pre> <h3>Added Paths</h3> <ul> <li><a href="#trunkwxsugarlibwx_sugarenumerable_controlsrb">trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="trunkwxsugarlibwx_sugarenumerable_controlsrb"></a> <div class="addfile"><h4>Added: trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb (0 => 937)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb         (rev 0) +++ trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb        2007-04-02 18:47:50 UTC (rev 937) </span><span class="lines">@@ -0,0 +1,112 @@ </span><ins>+# 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+. +module WxSugar::EnumerableControl + include Enumerable + module ClassMethods + # Used internally to define a proxy object for items. + def collection(name, coll_class) + define_method(name) { coll_class.new(self) } + end + end + + def self.included(klass) + klass.extend ClassMethods + end + + # 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. + 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 + + # 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. + def index(value) + if value.kind_of?(String) and i = find_string(value) + return i + end + indices = (0 ... get_count).to_a + return indices.find { | i | get_item_data(i) == value } + end + + # Appends this string to the control + def <<(str) + append(str) + end + + # Deletes any items in the control for which the passed block + # evaluates to +true+. + def delete_if + deletions = [] + each { | i | deletions << i if yield i } + deletions.reverse.each { | i | delete(i) } + end + + # These classes provide proxy accessors for items within the control. + class ItemCollection + attr_reader :cwi + def initialize(cwi) + @cwi = cwi + end + end + + # A proxy class for the item_data functions. + class ItemDataCollection < ItemCollection + def each + cwi.each { | i | yield cwi.get_item_data(i) } + end + + def [](i) + cwi.get_item_data(i) + end + + def []=(i, obj) + cwi.set_item_data(i, obj) + end + end + + # A proxy class for classes that use get/set_string to set labels, + # inheriting from ControlWithItems. + class StringsCollection < ItemCollection + def each + cwi.each { | i | yield cwi.get_string(i) } + end + + def [](i) + cwi.get_string(i) + end + + def []=(i, str) + cwi.set_string(i, str) + end + end +end </ins></span></pre> </div> </div> </body> </html>
Seemingly Similar Threads
- [943] trunk/wxsugar/lib/wx_sugar/enumerable_controls.rb: Fix bug with false positives from #find_string, bounds-checking for
- [940] trunk/wxsugar/lib/wx_sugar/wx_classes/treectrl.rb: Align #traverse usage with #each in enumerable_controls.rb
- [938] trunk/wxsugar/lib/wx_sugar/wx_classes: Enable enumerable_controls for ListCtrl and ControlWithItems family
- Bug? in wxSugar enumerable_controls.rb
- [945] trunk/wxsugar/lib/wx_sugar.rb: Add note about enumerable_controls, reorder list of extensions