nobody at rubyforge.org
2006-Nov-16 21:12 UTC
[Wxruby-development] [741] trunk/wxruby2: Fix get_item_data, hiding TreeItemData messiness, fix GC-ing (Alex Fenton)
<!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>[741] trunk/wxruby2: Fix get_item_data, hiding TreeItemData messiness, fix GC-ing (Alex Fenton)</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>741</dd> <dt>Author</dt> <dd>brokentoy</dd> <dt>Date</dt> <dd>2006-11-16 16:12:52 -0500 (Thu, 16 Nov 2006)</dd> </dl> <h3>Log Message</h3> <pre>Fix get_item_data, hiding TreeItemData messiness, fix GC-ing (Alex Fenton)</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 (740 => 741)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/doc/textile/treectrl.txtl        2006-11-15 18:41:10 UTC (rev 740) +++ trunk/wxruby2/doc/textile/treectrl.txtl        2006-11-16 21:12:52 UTC (rev 741) </span><span class="lines">@@ -1,11 +1,10 @@ </span><span class="cx"> h1(#wxtreectrl). Wx::TreeCtrl </span><span class="cx"> </span><del>-A tree control presents information as a hierarchy, with items that may be expanded -to show further items. Items in a tree control are referenced by TreeItemId handles, -which may be tested for validity by calling TreeItemId::IsOk. </del><ins>+A tree control presents information as a hierarchy, with items that may +be expanded to show further items. Desktop OSes commonly make use of +this type of control to present a navigable representation of folders +and files on a hard drive. </ins><span class="cx"> </span><del>-To intercept events from a tree control, use the event table macros described in "TreeEvent":treeevent.html. - </del><span class="cx"> h2. Derived from </span><span class="cx"> </span><span class="cx"> "Control":control.html </span><span class="lines">@@ -18,32 +17,37 @@ </span><span class="cx"> </span><span class="cx"> h2. Window styles </span><span class="cx"> </span><ins>+The behaviour and appearance of a TreeCtrl can be modified in several +ways by applying styles when constructing the widget. </ins><span class="cx"> </span><ins>+|@TR_EDIT_LABELS@|Use this style if you wish the user to be able to edit labels in the tree control.| +|@TR_NO_BUTTONS@|For convenience to document that no buttons are to be drawn.| +|@TR_HAS_BUTTONS@|Use this style to show + and - buttons to the left of parent items.| +|@TR_NO_LINES@|Use this style to hide vertical level connectors.| +|@TR_FULL_ROW_HIGHLIGHT@|Use this style to have the background colour and the selection highlight extend over the entire horizontal row of the tree control window. (This flag is ignored under Windows unless you specify TR_NO_LINES as well.) | +|@TR_LINES_AT_ROOT@|Use this style to show lines between root nodes. Only applicable if TR_HIDE_ROOT is set and TR_NO_LINES is not set.| +|@TR_HIDE_ROOT@|Use this style to suppress the display of the root node,effectively causing the first-level nodes to appear as a series of root nodes.| +|@TR_ROW_LINES@|Use this style to draw a contrasting border between displayed rows.| +|@TR_HAS_VARIABLE_ROW_HEIGHT@|Use this style to cause row heights to be just big enough to fit the content. If not set, all rows use the largest row height.The default is that this flag is unset.Generic only.| +|@TR_SINGLE@|For convenience to document that only one item may be selected at a time. Selecting another item causes the current selection, if any,to be deselected. This is the default.| +|@TR_MULTIPLE@|Use this style to allow a range of items to be selected.If a second range is selected, the current range, if any, is deselected.| +|@TR_EXTENDED@|Use this style to allow disjoint items to be selected. (Only partially implemented; may not work in all cases.)| +|@TR_DEFAULT_STYLE@|The set of flags that are closest to the defaults for the native control for a particular toolkit.| </ins><span class="cx"> </span><del>-|@TR_EDIT_LABELS@|Use this styleif you wish the user to be able to edit labels in the tree control.| -|@TR_NO_BUTTONS@|For convenienceto document that no buttons are to be drawn.| -|@TR_HAS_BUTTONS@|Use this styleto show + and - buttons to the left of parent items.| -|@TR_NO_LINES@|Use this styleto hide vertical level connectors.| -|@TR_FULL_ROW_HIGHLIGHT@|Use this style to have the backgroundcolour and the selection highlight extend over the entire horizontalrow of the tree control window. (This flag is ignored under Windows unless you specify TR_NO_LINES as well.) | -|@TR_LINES_AT_ROOT@|Use this styleto show lines between root nodes.Only applicable if TR_HIDE_ROOT is set and TR_NO_LINES is not set.| -|@TR_HIDE_ROOT@|Use this styleto suppress the display of the root node,effectively causing the first-level nodesto appear as a series of root nodes.| -|@TR_ROW_LINES@|Use this styleto draw a contrasting border between displayed rows.| -|@TR_HAS_VARIABLE_ROW_HEIGHT@|Use this styleto cause row heights to be just big enough to fit the content.If not set, all rows use the largest row height.The default is that this flag is unset.Generic only.| -|@TR_SINGLE@|For convenienceto document that only one item may be selected at a time.Selecting another item causes the current selection, if any,to be deselected. This is the default.| -|@TR_MULTIPLE@|Use this styleto allow a range of items to be selected.If a second range is selected, the current range, if any, is deselected.| -|@TR_EXTENDED@|Use this styleto allow disjoint items to be selected. (Only partially implemented; may not work in all cases.)| -|@TR_DEFAULT_STYLE@|The set of flags that areclosest to the defaults for the native control for a particular toolkit.| - - </del><span class="cx"> See also "window styles overview":windowstyles.html. </span><span class="cx"> </span><del>-h2. Event handling </del><ins>+h2. Event hooks </ins><span class="cx"> </span><del>-To process input from a tree control, use these event handler macros to direct input to member -functions that take a "TreeEvent":treeevent.html argument. </del><ins>+TreeCtrls can generate a large number of events, reflecting the variety +of ways in which users can interact with such controls. Handled events +include selecting items, expanding branches, dragging items, and editing +item labels as well as standard mouse and keyboard interactions. To +intercept events from a tree control, use the event table macros +described below and in "TreeEvent":treeevent.html. </ins><span class="cx"> </span><ins>+To process input from a tree control, use these event methods to direct +input to blocks that take a "TreeEvent":treeevent.html argument. </ins><span class="cx"> </span><del>- </del><span class="cx"> |*evt_tree_begin_drag(id) { | event | ... }*|Begin dragging with the left mouse button.| </span><span class="cx"> |*evt_tree_begin_rdrag(id) { | event | ... }*|Begin dragging with the right mouse button.| </span><span class="cx"> |*evt_tree_end_drag(id) { | event | ... }*|End dragging with the left or right mouse button.| </span><span class="lines">@@ -66,11 +70,31 @@ </span><span class="cx"> |*evt_tree_item_menu(id) { | event | ... }*|The context menu for the selected item has been requested, either by a right click or by using the menu key.| </span><span class="cx"> |*evt_tree_state_image_click(id) { | event | ... }*|The state image has been clicked. Windows only.| </span><span class="cx"> </span><del>- </del><span class="cx"> h2. See also </span><span class="cx"> </span><del>-"TreeItemData":treeitemdata.html, "TreeCtrl overview":treectrloverview.html, "ListBox":listbox.html, "ListCtrl":listctrl.html, "ImageList":imagelist.html, "TreeEvent":treeevent.html </del><ins>+"TreeItemData":treeitemdata.html, "ListBox":listbox.html, "ListCtrl":listctrl.html, "ImageList":imagelist.html, "TreeEvent":treeevent.html </ins><span class="cx"> </span><ins>+h2. Working with TreeCtrl items + +In wxRuby, individual items (including branches) in a tree control are +referenced by TreeItemIds. These ids are returned by methods such as +"append_item":#TreeCtrl_appenditem which add items to a tree, and by +TreeEvents which can be queried to find which item is being acted +upon. The ids can then be passed in as arguments to methods which modify +items within the tree, such as "set_item_text":#TreeCtrl_setitemtext. +As well as the text label, tree items can have several other properties, +such as being bold, italic, and icons for expanded and collapsed +states. TreeItemIds which may be tested for validity by calling +TreeItemId#is_ok. + +TreeCtrls can be linked to non-GUI application objects by using the +item_data methods. Methods which add items to the tree all accept an +optional @item_data@ argument, which may be any normal Ruby +object. Alternatively, a Ruby object can be explicitly associated with a +TreeCtrl item by calling "set_item_data":#TreeCtrl_setitemdata . The +same Ruby object can later be retrieved from a TreeCtrlItemId by calling +"get_item_data":#TreeCtrl_getitemdata . + </ins><span class="cx"> h2. Win32 notes </span><span class="cx"> </span><span class="cx"> TreeCtrl class uses the standard common treeview control under Win32 </span><span class="lines">@@ -78,9 +102,8 @@ </span><span class="cx"> library are known to have bugs with handling the tree control colours: the </span><span class="cx"> usual symptom is that the expanded items leave black (or otherwise incorrectly </span><span class="cx"> coloured) background behind them, especially for the controls using non </span><del>-default background colour. The recommended solution is to upgrade the @comctl32.dll@ -to a newer version: see -http://www.microsoft.com/downloads/release.asp?ReleaseID=11916http://www.microsoft.com/downloads/release.asp?ReleaseID=11916. </del><ins>+default background colour. The recommended solution is to upgrade the +@comctl32.dll@ to a newer version: see "http://www.microsoft.com/downloads/release.asp?ReleaseID=11916":http://www.microsoft.com/downloads/release.asp?ReleaseID=11916. </ins><span class="cx"> </span><span class="cx"> <div id="methods"> </span><span class="cx"> </span><span class="lines">@@ -165,8 +188,6 @@ </span><span class="cx"> </span><span class="cx"> </div> </span><span class="cx"> </span><del>- - </del><span class="cx"> h3(#TreeCtrl_constr). TreeCtrl.new </span><span class="cx"> </span><span class="cx"> *TreeCtrl.new*(%(arg-type)Window% parent, %(arg-type)Integer% id, </span><span class="lines">@@ -180,7 +201,7 @@ </span><span class="cx"> </span><span class="cx"> h4. Parameters </span><span class="cx"> </span><del>-* _parent_ Parent window. Must not be @NULL@. </del><ins>+* _parent_ Parent window. Must be a valid Wx::Window, and not nil. </ins><span class="cx"> * _id_ Window identifier. A value of -1 indicates a default value. </span><span class="cx"> * _pos_ Window position. </span><span class="cx"> * _size_ Window size. If the default size (-1, -1) is specified then the window is sized </span><span class="lines">@@ -194,16 +215,11 @@ </span><span class="cx"> "TreeCtrl#create":treectrl.html#TreeCtrl_create, "Validator":validator.html </span><span class="cx"> </span><span class="cx"> </span><del>- *destructor*() - -Destructor, destroying the list control. - - </del><span class="cx"> h3(#TreeCtrl_addroot). TreeCtrl#add_root </span><span class="cx"> </span><span class="cx"> "TreeItemId":treeitemid.html *add_root*(%(arg-type)String% text, %(arg-type)Integer% image = -1, </span><span class="cx"> %(arg-type)Integer% selImage = -1, </span><del>- %(arg-type)TreeItemData% data = @NULL@) </del><ins>+ %(arg-type)Object% item_data = @nil@) </ins><span class="cx"> </span><span class="cx"> Adds the root node to the tree, returning the new item. </span><span class="cx"> </span><span class="lines">@@ -219,7 +235,7 @@ </span><span class="cx"> "TreeItemId":treeitemid.html *append_item*(%(arg-type)TreeItemId% parent, %(arg-type)String% text, </span><span class="cx"> %(arg-type)Integer% image = -1, </span><span class="cx"> %(arg-type)Integer% selImage = -1, </span><del>- %(arg-type)TreeItemData% data = @NULL@) </del><ins>+ %(arg-type)Object% item_data = @nil@) </ins><span class="cx"> </span><span class="cx"> Appends an item to the end of the branch identified by _parent_, return a new item id. </span><span class="cx"> </span><span class="lines">@@ -240,7 +256,7 @@ </span><span class="cx"> </span><span class="cx"> Setting or assigning the button image list enables the display of image buttons. </span><span class="cx"> Once enabled, the only way to disable the display of button images is to set </span><del>-the button image list to @NULL@. </del><ins>+the button image list to @nil@. </ins><span class="cx"> </span><span class="cx"> This function is only available in the generic version. </span><span class="cx"> </span><span class="lines">@@ -480,16 +496,11 @@ </span><span class="cx"> </span><span class="cx"> h3(#TreeCtrl_getitemdata). TreeCtrl#get_item_data </span><span class="cx"> </span><del>- "TreeItemData":treeitemdata.html *get_item_data*(%(arg-type)TreeItemId% item) </del><ins>+ Object *get_item_data*(%(arg-type)TreeItemId% item) </ins><span class="cx"> </span><span class="cx"> Returns the tree item data associated with the item. </span><span class="cx"> </span><del>-h4. See also </del><span class="cx"> </span><del>-"TreeItemData":treeitemdata.html - - - </del><span class="cx"> h3(#TreeCtrl_getitemfont). TreeCtrl#get_item_font </span><span class="cx"> </span><span class="cx"> "Font":font.html *get_item_font*(%(arg-type)TreeItemId% item) </span><span class="lines">@@ -659,12 +670,6 @@ </span><span class="cx"> function can be called only if the control has the TR_MULTIPLE style. </span><span class="cx"> </span><span class="cx"> Returns the number of selected items. </span><del>- - - - - - </del><span class="cx"> h3(#TreeCtrl_getstateimagelist). TreeCtrl#get_state_image_list </span><span class="cx"> </span><span class="cx"> "ImageList":imagelist.html *get_state_image_list*() </span><span class="lines">@@ -705,13 +710,13 @@ </span><span class="cx"> %(arg-type)TreeItemId% previous, %(arg-type)String% text, </span><span class="cx"> %(arg-type)Integer% image = -1, </span><span class="cx"> %(arg-type)Integer% selImage = -1, </span><del>- %(arg-type)TreeItemData% data = @NULL@) </del><ins>+ %(arg-type)Object% item_data = @nil@) </ins><span class="cx"> </span><span class="cx"> "TreeItemId":treeitemid.html *insert_item*(%(arg-type)TreeItemId% parent, </span><span class="cx"> %(arg-type)size_t% before, %(arg-type)String% text, </span><span class="cx"> %(arg-type)Integer% image = -1, </span><span class="cx"> %(arg-type)Integer% selImage = -1, </span><del>- %(arg-type)TreeItemData% data = @NULL@) </del><ins>+ %(arg-type)Object% item_data = @nil@) </ins><span class="cx"> </span><span class="cx"> Inserts an item after a given one (_previous_) or before one identified by its position (_before_). </span><span class="cx"> _before_ must be less than the number of children. </span><span class="lines">@@ -781,7 +786,7 @@ </span><span class="cx"> "TreeItemId":treeitemid.html *prepend_item*(%(arg-type)TreeItemId% parent, %(arg-type)String% text, </span><span class="cx"> %(arg-type)Integer% image = -1, </span><span class="cx"> %(arg-type)Integer% selImage = -1, </span><del>- %(arg-type)TreeItemData% data = @NULL@) </del><ins>+ %(arg-type)Object% item_data = @nil@) </ins><span class="cx"> </span><span class="cx"> Appends an item as the first child of _parent_, return a new item id. </span><span class="cx"> </span><span class="lines">@@ -817,7 +822,7 @@ </span><span class="cx"> </span><span class="cx"> Setting or assigning the button image list enables the display of image buttons. </span><span class="cx"> Once enabled, the only way to disable the display of button images is to set </span><del>-the button image list to @NULL@. </del><ins>+the button image list to @nil@. </ins><span class="cx"> </span><span class="cx"> This function is only available in the generic version. </span><span class="cx"> </span><span class="lines">@@ -861,7 +866,7 @@ </span><span class="cx"> </span><span class="cx"> h3(#TreeCtrl_setitemdata). TreeCtrl#set_item_data </span><span class="cx"> </span><del>- *set_item_data*(%(arg-type)TreeItemId% item, %(arg-type)TreeItemData% data) </del><ins>+ *set_item_data*(%(arg-type)TreeItemId% item, %(arg-type)Object% data) </ins><span class="cx"> </span><span class="cx"> Sets the item client data. </span><span class="cx"> </span><span class="lines">@@ -959,7 +964,7 @@ </span><span class="cx"> </span><span class="cx"> h4. See also </span><span class="cx"> </span><del>-"TreeItemData":treeitemdata.html, "on_compare_items":#TreeCtrl_oncompareitems </del><ins>+"on_compare_items":#TreeCtrl_oncompareitems </ins><span class="cx"> </span><span class="cx"> </span><span class="cx"> h3(#TreeCtrl_toggle). TreeCtrl#toggle </span></span></pre></div> <a id="trunkwxruby2swigclassesTreeCtrli"></a> <div class="modfile"><h4>Modified: trunk/wxruby2/swig/classes/TreeCtrl.i (740 => 741)</h4> <pre class="diff"><span> <span class="info">--- trunk/wxruby2/swig/classes/TreeCtrl.i        2006-11-15 18:41:10 UTC (rev 740) +++ trunk/wxruby2/swig/classes/TreeCtrl.i        2006-11-16 21:12:52 UTC (rev 741) </span><span class="lines">@@ -9,10 +9,88 @@ </span><span class="cx"> #include <wx/treectrl.h> </span><span class="cx"> %} </span><span class="cx"> </span><del>-// not sure why these aren''t working -#%ignore wxTreeCtrl::SetStateImageList; </del><ins>+ // ITEM DATA fixes - This is done so the API user never sees a +// TreeItemData object - where in Wx C++ such an object +// would be passed or returned by a method, any Ruby object may be used. +%{ +class wxRbTreeItemData : public wxTreeItemData { +public: + wxRbTreeItemData(VALUE obj = Qnil) { + m_obj = obj; + } </ins><span class="cx"> </span><ins>+ VALUE GetRubyObject() { +         return m_obj; + } </ins><span class="cx"> </span><ins>+ void SetRubyObject(VALUE obj) { +         m_obj = obj; + } + + VALUE m_obj; +}; +%} + + +// typemaps for setting and getting ruby objects as itemdata. +%typemap(in) wxTreeItemData* "$1 = new wxRbTreeItemData($input);" + +%typemap(directorin) wxTreeItemData* { + wxRbTreeItemData* ruby_item_data = (wxRbTreeItemData *)$1; + $input = ruby_item_data->GetRubyObject(); +} + +%typemap(out) wxTreeItemData* { + if ( $1 == NULL ) +        { +         $result = Qnil; +        } + else +         { +         wxRbTreeItemData* ruby_item_data = (wxRbTreeItemData *)$1; +         $result = ruby_item_data->GetRubyObject(); +        } +} + +// GC handling for item data objects +%{ + static void RecursivelyGCMarkFrom(wxTreeCtrl *tree_ctrl, wxTreeItemId base_id ) + { +         +        // 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); +         } + +        // recurse through children +        if ( tree_ctrl->ItemHasChildren(base_id) ) +         { +                wxTreeItemIdValue cookie; +                wxTreeItemId child = tree_ctrl->GetFirstChild(base_id, cookie); +                while ( child.IsOk() ) +                 { +                        RecursivelyGCMarkFrom(tree_ctrl, child); +                        child = tree_ctrl->GetNextSibling(child); +                 } +         } + } + + static void mark_wxTreeCtrl(void *ptr) + { +        VALUE rb_obj = SWIG_RubyInstanceFor(ptr); +        if ( rb_ivar_get(rb_obj, rb_intern("@__swig_dead__") ) == Qtrue ) +         return; + +        wxTreeCtrl* tree_ctrl = (wxTreeCtrl*) ptr; +        wxTreeItemId root_id = tree_ctrl->GetRootItem(); +        RecursivelyGCMarkFrom(tree_ctrl, root_id); + } +%} +%markfunc wxTreeCtrl "mark_wxTreeCtrl"; + </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 class="lines">@@ -25,6 +103,7 @@ </span><span class="cx"> </span><span class="cx"> %include "include/wxTreeCtrl.h" </span><span class="cx"> </span><ins>+ </ins><span class="cx"> %extend wxTreeCtrl { </span><span class="cx">          </span><span class="cx">         //Change signature so it returns an array of the TreeItemId and the Cookie. </span><span class="lines">@@ -54,3 +133,4 @@ </span><span class="cx">                 return array;                 </span><span class="cx">         } </span><span class="cx"> } </span><ins>+ </ins></span></pre> </div> </div> </body> </html>
Reasonably Related Threads
- [779] trunk/wxruby2: Added TreeCtrl#traverse method and documentation
- [831] trunk/wxruby2: Use simple ruby Integers to represent TreeItemIds
- [835] trunk/wxruby2/doc/textile: Updated documentation about TreeItemId
- [1046] trunk/wxruby2/swig/classes/TreeCtrl.i: Prevent crashes in TreeCtrl from premature deletion of associated ImageList
- [797] trunk/wxruby2/swig/classes/TreeCtrl.i: Traverse method moved to wxSugar