nobody at rubyforge.org
2006-Dec-05 19:07 UTC
[Wxruby-development] [779] trunk/wxruby2: Added TreeCtrl#traverse method and documentation
<!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>[779] trunk/wxruby2: Added TreeCtrl#traverse method and documentation</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>779</dd> <dt>Author</dt> <dd>brokentoy</dd> <dt>Date</dt> <dd>2006-12-05 14:07:34 -0500 (Tue, 05 Dec 2006)</dd> </dl> <h3>Log Message</h3> <pre>Added TreeCtrl#traverse method and documentation</pre> <h3>Modified Paths</h3> <ul> <li><a href="#trunkwxruby2doctextiletreectrltxtl">trunk/wxruby2/doc/textile/treectrl.txtl</a></li> <li><a href="#trunkwxruby2swigclassesTreeCtrli">trunk/wxruby2/swig/classes/TreeCtrl.i</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="trunkwxruby2doctextiletreectrltxtl"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/doc/textile/treectrl.txtl (778 => 779)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/doc/textile/treectrl.txtl        2006-12-03 17:30:10 UTC (rev 778) +++ trunk/wxruby2/doc/textile/treectrl.txtl        2006-12-05 19:07:34 UTC (rev 779) </span><span class="lines">@@ -182,6 +182,7 @@ </span><span class="cx"> * "TreeCtrl#sort_children":#TreeCtrl_sortchildren </span><span class="cx"> * "TreeCtrl#toggle":#TreeCtrl_toggle </span><span class="cx"> * "TreeCtrl#toggle_item_selection":#TreeCtrl_toggleitemselection </span><ins>+* "TreeCtrl#traverse":#TreeCtrl_traverse </ins><span class="cx"> * "TreeCtrl#unselect":#TreeCtrl_unselect </span><span class="cx"> * "TreeCtrl#unselect_all":#TreeCtrl_unselectall </span><span class="cx"> * "TreeCtrl#unselect_item":#TreeCtrl_unselectitem </span><span class="lines">@@ -981,7 +982,18 @@ </span><span class="cx"> Toggles the given item between selected and unselected states. For </span><span class="cx"> multiselection controls only. </span><span class="cx"> </span><ins>+h3(#TreeCtrl_traverse). TreeCtrl#traverse </ins><span class="cx"> </span><ins>+ *toggle_item_selection*(%(arg-type)TreeItemId% start_item = root_id) { | item_id | ... } + +Recurses over the treectrl''s items, passing each TreeItemId successively +into the passed block. This TreeItemId can be used as an argument to +many other methods in this class. + +The @start_item@ argument will be the first item_id passed into the +block, followed by its children and descendants, depth first. If +@start_item@ is not specified, it defaults to the root item of the tree. + </ins><span class="cx"> h3(#TreeCtrl_unselect). TreeCtrl#unselect </span><span class="cx"> </span><span class="cx"> *unselect*() </span></span></pre></div> <a id="trunkwxruby2swigclassesTreeCtrli"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/swig/classes/TreeCtrl.i (778 => 779)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/swig/classes/TreeCtrl.i        2006-12-03 17:30:10 UTC (rev 778) +++ trunk/wxruby2/swig/classes/TreeCtrl.i        2006-12-05 19:07:34 UTC (rev 779) </span><span class="lines">@@ -15,18 +15,10 @@ </span><span class="cx"> %{ </span><span class="cx"> class wxRbTreeItemData : public wxTreeItemData { </span><span class="cx"> public: </span><del>- wxRbTreeItemData(VALUE obj = Qnil) { - m_obj = obj; - } - - VALUE GetRubyObject() { -         return m_obj; - } - - void SetRubyObject(VALUE obj) { -         m_obj = obj; - } - </del><ins>+ wxRbTreeItemData(VALUE obj = Qnil) { m_obj = obj; } + VALUE GetRubyObject() { return m_obj; } + void SetRubyObject(VALUE obj) { m_obj = obj; } +protected: </ins><span class="cx"> VALUE m_obj; </span><span class="cx"> }; </span><span class="cx"> %} </span><span class="lines">@@ -51,20 +43,17 @@ </span><span class="cx">          $result = ruby_item_data->GetRubyObject(); </span><span class="cx">         } </span><span class="cx"> } </span><ins>+// End item data fixes </ins><span class="cx"> </span><ins>+ </ins><span class="cx"> // GC handling for item data objects </span><span class="cx"> %{ </span><del>- static void RecursivelyGCMarkFrom(wxTreeCtrl *tree_ctrl, wxTreeItemId base_id ) </del><ins>+ // general recursion over a treectrl, starting from a base_id + // the function rec_func will be called in turn for each tree item, + // rec_func should be a funtion that receives a treectrl pointer and an ItemId + static void RecurseOverTreeIds(wxTreeCtrl *tree_ctrl, const wxTreeItemId& base_id, void(*rec_func)(void *, const wxTreeItemId&) ) </ins><span class="cx"> { </span><del>-         -        // check if there''s item data, and mark it -        wxRbTreeItemData* ruby_item_data = (wxRbTreeItemData *)tree_ctrl->GetItemData(base_id); -        if ( ruby_item_data != NULL ) -         { -                VALUE ruby_obj = ruby_item_data->GetRubyObject(); -                rb_gc_mark(ruby_obj); -         } - </del><ins>+        rec_func(tree_ctrl, base_id); </ins><span class="cx">         // recurse through children </span><span class="cx">         if ( tree_ctrl->ItemHasChildren(base_id) ) </span><span class="cx">          { </span><span class="lines">@@ -72,12 +61,25 @@ </span><span class="cx">                 wxTreeItemId child = tree_ctrl->GetFirstChild(base_id, cookie); </span><span class="cx">                 while ( child.IsOk() ) </span><span class="cx">                  { </span><del>-                        RecursivelyGCMarkFrom(tree_ctrl, child); -                        child = tree_ctrl->GetNextSibling(child); </del><ins>+                        RecurseOverTreeIds(tree_ctrl, child, *rec_func); +                        child = tree_ctrl->GetNextChild(base_id, cookie); </ins><span class="cx">                  } </span><span class="cx">          } </span><span class="cx"> } </span><span class="cx"> </span><ins>+ static void DoGCMarkItemData(void *ptr, const wxTreeItemId& item_id) + { +        wxTreeCtrl* tree_ctrl = (wxTreeCtrl*) ptr; +        // check if there''s item data, and mark it +        wxRbTreeItemData* ruby_item_data = (wxRbTreeItemData *)tree_ctrl->GetItemData(item_id); +        if ( ruby_item_data != NULL ) +         { +                VALUE ruby_obj = ruby_item_data->GetRubyObject(); +                rb_gc_mark(ruby_obj); +         } + } + + // SWIG''s entry point function for GC mark </ins><span class="cx"> static void mark_wxTreeCtrl(void *ptr) </span><span class="cx"> { </span><span class="cx">         VALUE rb_obj = SWIG_RubyInstanceFor(ptr); </span><span class="lines">@@ -86,8 +88,17 @@ </span><span class="cx"> </span><span class="cx">         wxTreeCtrl* tree_ctrl = (wxTreeCtrl*) ptr; </span><span class="cx">         wxTreeItemId root_id = tree_ctrl->GetRootItem(); </span><del>-        RecursivelyGCMarkFrom(tree_ctrl, root_id); </del><ins>+        RecurseOverTreeIds(tree_ctrl, root_id, &DoGCMarkItemData); </ins><span class="cx"> } </span><ins>+ + // function for internal implementation of TreeCtrl#traverse + static void DoTreeCtrlYielding(void *ptr, const wxTreeItemId& item_id) + { +        // create a copy to wrap and give to ruby +        wxTreeItemId *copy_id = new wxTreeItemId(item_id.m_pItem); +        VALUE rb_item_id = SWIG_NewPointerObj(copy_id, SWIGTYPE_p_wxTreeItemId, SWIG_POINTER_OWN); +        rb_yield(rb_item_id); + } </ins><span class="cx"> %} </span><span class="cx"> %markfunc wxTreeCtrl "mark_wxTreeCtrl"; </span><span class="cx"> </span><span class="lines">@@ -105,7 +116,24 @@ </span><span class="cx"> </span><span class="cx"> </span><span class="cx"> %extend wxTreeCtrl { </span><del>-         </del><ins>+ + VALUE traverse(const wxTreeItemId& item_id = wxTreeItemId() ) + { +        wxTreeItemId base_id; + +        if ( item_id.IsOk() ) +         { +                base_id = item_id; +         } +        else +         { +                base_id = self->GetRootItem(); +         } + +        RecurseOverTreeIds(self, base_id, &DoTreeCtrlYielding); +        return Qnil; + } + </ins><span class="cx">         //Change signature so it returns an array of the TreeItemId and the Cookie. </span><span class="cx">         //This behavior matches that used by wxPython and wxPerl. </span><span class="cx">         VALUE get_first_child(const wxTreeItemId& item) </span></span></pre> </div> </div> </body> </html>
Seemingly Similar Threads
- [741] trunk/wxruby2: Fix get_item_data, hiding TreeItemData messiness, fix GC-ing (Alex Fenton)
- [797] trunk/wxruby2/swig/classes/TreeCtrl.i: Traverse method moved to wxSugar
- [1046] trunk/wxruby2/swig/classes/TreeCtrl.i: Prevent crashes in TreeCtrl from premature deletion of associated ImageList
- TreeCtrl update
- [831] trunk/wxruby2: Use simple ruby Integers to represent TreeItemIds