nobody at rubyforge.org
2006-Nov-30 01:21 UTC
[Wxruby-development] [765] trunk/wxruby2: Fixed item_data functions for ListCtrl, plus test
<!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>[765] trunk/wxruby2: Fixed item_data functions for ListCtrl, plus test</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>765</dd> <dt>Author</dt> <dd>brokentoy</dd> <dt>Date</dt> <dd>2006-11-29 20:21:25 -0500 (Wed, 29 Nov 2006)</dd> </dl> <h3>Log Message</h3> <pre>Fixed item_data functions for ListCtrl, plus test</pre> <h3>Modified Paths</h3> <ul> <li><a href="#trunkwxruby2swigclassesListCtrli">trunk/wxruby2/swig/classes/ListCtrl.i</a></li> <li><a href="#trunkwxruby2teststest_item_datarb">trunk/wxruby2/tests/test_item_data.rb</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="trunkwxruby2swigclassesListCtrli"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/swig/classes/ListCtrl.i (764 => 765)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/swig/classes/ListCtrl.i        2006-11-30 00:08:57 UTC (rev 764) +++ trunk/wxruby2/swig/classes/ListCtrl.i        2006-11-30 01:21:25 UTC (rev 765) </span><span class="lines">@@ -19,7 +19,10 @@ </span><span class="cx"> # deprecated: </span><span class="cx"> %ignore wxListCtrl::GetItemSpacing(bool isSmall) const; </span><span class="cx"> </span><ins>+// dealt with below </ins><span class="cx"> %ignore wxListCtrl::GetItem(wxListItem& info) const; </span><ins>+%ignore wxListCtrl::GetItemData; +%ignore wxListCtrl::SetItemData; </ins><span class="cx"> </span><span class="cx"> %extend wxListCtrl { </span><span class="cx"> VALUE get_item(int row, int col = -1) </span><span class="lines">@@ -36,10 +39,53 @@ </span><span class="cx"> </span><span class="cx">         return returnVal; </span><span class="cx"> } </span><ins>+ + VALUE get_item_data(int row) + { +        if ( row < 0 || row >= self->GetItemCount() ) return Qnil; +        long item_data = self->GetItemData(row); +        if ( item_data == 0 ) return Qnil; +        return (VALUE)item_data; + } + + VALUE set_item_data(int row, VALUE ruby_obj) + { +        if ( row < 0 || row >= self->GetItemCount() ) +         { +                rb_raise(rb_eIndexError, "Uninitialized item"); +         } +        long item_data = (long) ruby_obj; +        bool result = self->SetItemData(row, item_data); +        if ( result ) +         return Qtrue; +        return Qnil; + }         </ins><span class="cx"> } </span><span class="cx"> </span><ins>+%markfunc wxListCtrl "mark_wxListCtrl"; +%{ + // Prevents Ruby''s GC sweeping up items that are stored as item data + static void mark_wxListCtrl(void* ptr) { + // Checks whether the C++ object is still around first... +        VALUE rb_obj = SWIG_RubyInstanceFor(ptr); +        if ( rb_ivar_get(rb_obj, rb_intern("@__swig_dead__") ) == Qtrue ) +         return; </ins><span class="cx"> </span><ins>+        wxListCtrl* wx_lc = (wxListCtrl*) ptr; +        int count = wx_lc->GetItemCount(); +        if ( count == 0 ) return; </ins><span class="cx"> </span><ins>+        for (int i = 0; i < count; ++i) +         { +                VALUE object = (VALUE) wx_lc->GetItemData(i); +                if ( object != NULL && object != Qnil ) +                 { +                        rb_gc_mark(object); +                 } +         } + } +%} + </ins><span class="cx"> %import "include/wxObject.h" </span><span class="cx"> %import "include/wxEvtHandler.h" </span><span class="cx"> %import "include/wxWindow.h" </span></span></pre></div> <a id="trunkwxruby2teststest_item_datarb"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/tests/test_item_data.rb (764 => 765)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/tests/test_item_data.rb        2006-11-30 00:08:57 UTC (rev 764) +++ trunk/wxruby2/tests/test_item_data.rb        2006-11-30 01:21:25 UTC (rev 765) </span><span class="lines">@@ -52,6 +52,34 @@ </span><span class="cx"> f.close(true) </span><span class="cx"> end </span><span class="cx"> </span><ins>+ def test_listctrl_itemdata + f = CtrlContainerFrame.new(Wx::ListCtrl) + lc = f.control + assert_equal(nil, lc.get_item_data(-7)) + assert_equal(nil, lc.get_item_data(0)) + assert_equal(nil, lc.get_item_data(118)) + + lc.insert_item(0, ''string'') + assert_equal(nil, lc.get_item_data(0)) + + lc.set_item_data(0, ''a string'') + assert_equal(''a string'', lc.get_item_data(0)) + GC.start + assert_equal(''a string'', lc.get_item_data(0)) + + lc.insert_item(1, ''hash'') + assert_equal(nil, lc.get_item_data(1)) + + lc.set_item_data(1, { :a => 457 }) + assert_equal({ :a => 457 }, lc.get_item_data(1)) + GC.start + assert_equal({ :a => 457 }, lc.get_item_data(1)) + + assert_raises(IndexError) { lc.set_item_data(17, 3.412) } + f.close(true) + end + + </ins><span class="cx"> def test_choice_itemdata </span><span class="cx"> f = CtrlContainerFrame.new(Wx::Choice, Wx::DEFAULT_POSITION, </span><span class="cx"> Wx::DEFAULT_SIZE, %w[hash string float]) </span></span></pre> </div> </div> </body> </html>
Seemingly Similar Threads
- [ wxruby-Bugs-13676 ] xrc listctrl windows custom data crash
- [766] trunk/wxruby2: Added item_data aliases for client_data functions, update doc & tests;
- [851] trunk/wxruby2/swig/classes/ListCtrl.i: Don''t try and mark item data for virtual ListCtrls, b/c it can crash
- [741] trunk/wxruby2: Fix get_item_data, hiding TreeItemData messiness, fix GC-ing (Alex Fenton)
- TreeCtrl item_data