How to parse every node of my Tree

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
romeo9423
Experienced Solver
Experienced Solver
Posts: 72
Joined: Mon Nov 08, 2004 3:36 pm

How to parse every node of my Tree

Post by romeo9423 » Thu Jan 05, 2006 11:14 am

Hi,

I have a wxTreectrl with some items and I have attached to each item a wxItemData holding its type.
Now When I add an item I would like to parse everynode to search for a given type.
How can I do that ?

Code: Select all

enum EItemType
{
        eRoot = 0,
        eReaderGroup,
        eReaderItem,
        eCardGroup,
        eCardItem,
        eCardCmdGroup,
        eCardCmdItem
};

wxTreeItemId PrjTree::GetItem( int nParentType, int& nIcon)
{
    wxTreeItemId idItem;

    // Parse every items and compare its type
    

   HOW TO BUILD THIS LOOP
{
    idItem = // current item
    PrjItemData* pPrjItemData = GetItemData(idItem);
    if (pPrjItemData->GetType() == nParentType)
       return idItem;
}

    return idItem;
}


void PrjTree::AddItem(int nParentType,int nItemType,const wxString& strItemName)
{
    wxTreeItemId idItem;
    int nIcon;

    //idItem = GetItem( nParentType , nIcon);

    AppendItem(idItem, strItemName, nIcon, nIcon, new PrjItemData( nItemType ) );
}
Last edited by romeo9423 on Thu Jan 05, 2006 11:40 am, edited 1 time in total.

upCASE
Site Admin
Site Admin
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Re: How to parse every node of my Tree

Post by upCASE » Thu Jan 05, 2006 11:22 am

Hi!
romeo9423 wrote:I have a wxTreectrl with some items and I have attached to each item a wxItemData holding its type.
Now When I add an item I would like to parse everynode tio search for a given type.
How can I do that ?
This would imply recursively traversing the whole tree, if I understood you correctly.

To do that, start at the root node (GetRootItem). Then see if there are children to it and traverse them (GetFirstChild, GetNextChild). The second step would need recursion, so that the root item would allways be the Child.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda

romeo9423
Experienced Solver
Experienced Solver
Posts: 72
Joined: Mon Nov 08, 2004 3:36 pm

Re: How to parse every node of my Tree

Post by romeo9423 » Thu Jan 05, 2006 12:03 pm

ok thanks I found a example in sample code :

Code: Select all

void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent,
                                     wxTreeItemIdValue cookie)
{
    wxTreeItemId id;

    if ( !cookie )
        id = GetFirstChild(idParent, cookie);
    else
        id = GetNextChild(idParent, cookie);

    if ( !id.IsOk() )
        return;

    wxString text = GetItemText(id);
    wxLogMessage(text);

    if (ItemHasChildren(id))
        GetItemsRecursively(id);

    GetItemsRecursively(idParent, cookie);
}
upCASE wrote:Hi!
romeo9423 wrote:I have a wxTreectrl with some items and I have attached to each item a wxItemData holding its type.
Now When I add an item I would like to parse everynode tio search for a given type.
How can I do that ?
This would imply recursively traversing the whole tree, if I understood you correctly.

To do that, start at the root node (GetRootItem). Then see if there are children to it and traverse them (GetFirstChild, GetNextChild). The second step would need recursion, so that the root item would allways be the Child.

Post Reply