wxTreeCtrl

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Juzbrig
In need of some credit
In need of some credit
Posts: 4
Joined: Sat Jul 12, 2008 4:59 am

wxTreeCtrl

Post by Juzbrig »

Hi.
Let me start by an example

genre1
-artist1
--album1
--album2
-artist2
genre2
etc

I want to do that under every child, and under every parent there can be only one branch with the same value(text). I have a dialog in which one is entering the genre, artist, album, but when the genres/artist alredy exisxt it shouldn't create another one with the same vaule(name) but add to the existing one.

The problem is I need a function that loooks like this
bool exists(?? parent, wxString value_)
{
}

to check if there is a vaule with tah name if yes, I could override it or cancel

and like this
wxTreeItemId Gimme_ID(?? parent, wxString value_)
{
}

to get the ID and function like this (oposite to
GetItemText() )

But from what I noticed almost every function
http://docs.wxwidgets.org/trunk/classwx ... 9e546127a8
takes wxTreeItemId as an argument while I want oposite situation and I cant do it. User would enter VAULES in text not wxTreeItemId's. Any ideas ?:(
Juzbrig
In need of some credit
In need of some credit
Posts: 4
Joined: Sat Jul 12, 2008 4:59 am

Post by Juzbrig »

Ok I think if I will be able to save this (which btw I have to do anyway) in a file I would be able to manipulate it: search in it, change, etc.

But how save a structure such as wxTreeCtrl or wxListCtrl into a file? So far I know only how to save strings (wxFile). But eventually I have to save it in order to reopen it after application restart.
Sof_T
Can't get richer than this
Can't get richer than this
Posts: 864
Joined: Thu Jul 28, 2005 9:48 pm
Location: New Forest, United Kingdom

Post by Sof_T »

Here are a couple of functions to help, they return a true value if the artist or genre is found. You need to pass in a string containing the name of the artist or genre and a wxTreeItemId. If the function returns true the wxTreeItemId will contain the location of the item.

Code: Select all

bool JuzbrigFrm::FindArtist(const wxString& Artist, wxTreeItemId& id)
{
    bool RetVal = false;
    wxTreeItemId root = WxTreeCtrl1->GetRootItem();
    wxTreeItemIdValue cookie;
    wxTreeItemId genreId = WxTreeCtrl1->GetFirstChild(root, cookie);
    while(genreId.IsOk())
    {
        wxTreeItemIdValue cookie2;
        wxTreeItemId artistID = WxTreeCtrl1->GetFirstChild(genreId, cookie2);
        while(artistID.IsOk())
        {
            if(WxTreeCtrl1->GetItemText(artistID).CmpNoCase(Artist) == 0)
            {
                RetVal = true;
                id = artistID;
                break;
            }
            artistID = WxTreeCtrl1->GetNextChild(genreId, cookie2);
        }
        genreId = WxTreeCtrl1->GetNextChild(root, cookie);
    }
    return RetVal;

}
bool JuzbrigFrm::FindGenre(const wxString& Genre, wxTreeItemId& id)
{
    bool RetVal = false;
    wxTreeItemId root = WxTreeCtrl1->GetRootItem();
    wxTreeItemIdValue cookie;
    wxTreeItemId genreId = WxTreeCtrl1->GetFirstChild(root, cookie);
    while(genreId.IsOk())
    {
        if(WxTreeCtrl1->GetItemText(genreId).CmpNoCase(Genre) == 0)
        {
            RetVal = true;
            id = genreId;
            break;
        }
        genreId = WxTreeCtrl1->GetNextChild(root, cookie);
    }
    return RetVal;
}
I have also attached a sample program which uses these functions.

Sof.T[/code]
You do not have the required permissions to view the files attached to this post.
The home of Sof.T http://www.sof-t.site88.net/
Author of Programming with wxDevC++
http://sourceforge.net/projects/wxdevcpp-book/
Juzbrig
In need of some credit
In need of some credit
Posts: 4
Joined: Sat Jul 12, 2008 4:59 am

Post by Juzbrig »

Jeee thanks, I really wasn't expecting that sb will write a sample for me :D

Anyway, if only you could tell me way to save some things in the program.
As I mentioned in previous post, saving modified TreeCtrl to to file, or saving other stuff I will do later. So far I only know how to save strings.

ps.This program is very imortant to me, I will ask you somethimes but not to often, only if im stuck like for two days :)