Searching a treectrl

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
hmtksteve
Earned a small fee
Earned a small fee
Posts: 10
Joined: Fri Mar 25, 2005 2:35 am

Searching a treectrl

Post by hmtksteve »

here is my code

Code: Select all


    Buffer.Append(ItemName);
    do
    {
        if(text.Len()==0)
        {
            otherid = theTree->GetFirstChild(branch, cookie);
        }
        if(otherid.IsOk())
        {
           do
           {
               // valid
               text = theTree->GetItemText(otherid);
               if(Buffer.Cmp( text )== 0)
               {
                   // We have a match!
                   OnNewRoot(otherid, ItemName, names, values, i);
                   return;
               }
               otherid = theTree->GetNextChild(branch, cookie);
            }while(otherid.IsOk());
        }
        otherid = theTree->GetNextSibling(branch);
    }while(otherid.IsOk());
    OnNewRoot (theTree->GetRootItem(), ItemName, names, values, i);

What I'm trying to do is pass several variables:

ItemName = name of tree item
names = wxStringArray of subitems
values = wxStringArray of subitem values
i = number of subitems

What I want to have happen is for the function to search the tree for an instance of "ItemName" If it finds it I want the id stored in "otherid".

At this time it is not working properly as it will not find an "ItemName" if it is inside another tree and instead creates a new tree off of root!

So if my tree is:

Code: Select all


root
    +Hardware // ItemName
        +Hammer // names[0]
            +qty // values[0]


and I try to pass it some extra info on hammers

Code: Select all


Hammer // ItemName
    +Qty // names[0]
        +5 //values[0]
    +Color //names[1]
        +Green //values[1]
Instead of having the tree update all the info under hammer, it creates a new branch off of root for "Hammer". I need the code to find that "Hammer" already exists as a child of another branch and send it's data accordingly!

just for reference:

Code: Select all


void OnNewRoot (const wxTreeItemId &node, char *ItemName, wxArrayString names, wxArrayString values, int i)
{
    int x=0;
    wxTreeItemId id;
    wxTreeItemId kid = node;
    wxString Buffer;

    // Add department node
    Buffer=theTree->GetItemText(node);
    if(Buffer.Cmp( ItemName )!= 0)
    {
    kid = theTree->AppendItem(theTree->GetRootItem(),
                        ItemName,
                        -1,
                        -1,
                        NULL);
                        x=0;

    }
    theTree->DeleteChildren(kid);
    do
    {
        // these are to be children!
        theTree->AppendItem((theTree->AppendItem(kid,
                             names[x],
                             -1,
                             -1,
                             NULL)),
                             values[x],
                             -1,
                             -1,
                             NULL);
    x++;                   
    }while (x<i);
    theTree->Expand(kid);

}
_________________
What I think is happening is that if a branch has multiple children with children of their own only the first child is searched as when it hits the end of that branch it goes back to the top level and looks for a new sibling... I'm guessing (real late, getting punchy) that I need to tell the searching function to back up and look for more children from the branch being searched before finding the next sibling of the branch being searched.
Sebastian Rose
Earned a small fee
Earned a small fee
Posts: 17
Joined: Mon Mar 28, 2005 8:17 pm
Location: Hannover, Germany
Contact:

Post by Sebastian Rose »

This is how I copy branches. If you want to move the branch, delte the old one after copying it.

For searching an item with a certain text, I use the functions here: http://www.basti-rose.de/intranet/knowl ... t=56&pub=1 (just llok at traverse() and search()), and for copying http://www.basti-rose.de/intranet/knowl ... t=59&pub=1. Moving is done by copy to new, and delting old. If you work with 2 or more different trees, it might be better to define static functions.
XP prof: --- wxWidgets 2.6.2 --- Mingw Gcc
Debian testing: --- wxWidgets 2.6.2 --- gcc 4.0.2-2 --- Gtk2 2.6.10
Post Reply