Using wxSearchCtrl to search in a wxListCtr

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
User avatar
Azrael
Knows some wx things
Knows some wx things
Posts: 37
Joined: Sat Aug 05, 2017 2:44 pm
Location: Italy

Using wxSearchCtrl to search in a wxListCtr

Post by Azrael »

Hello,
I hope it's the right subforum.
I want to create a simple mp3 player and I want to have a search function. I created a wxSearchCtrl but I discovered that it doesn't have a function that does the job for me. I began writing my own searching function but I'm having some problems.
This is what I wrote so far:

Code: Select all

// text is defined in another part of the code. text = searchCtrl -> GetLineText(0);
searchItem ( wxString text, wxListCrl * listCtrl){
    int count = 0 ;
    for ( long i = 0; i != listCtrl -> GetItemCount(); i++ ){
        if( listCtrl -> GetItemText( i, 1 ) . IsSameAs(text) || listCtrl -> GetItemText( i, 1) . StartsWith(text) )
            cout ++;
    }
    cout << "Found " << count << " elements" << endl;
}
As you can see the function is really basic... I don't want to keep the code as it is now. Also because I don't have information about list items, I know only the index.I don't know how to access the list items using only their index. I want the items in the list that correspond to the string the user is searching for to be selected ( like SetState ( wx_LIST_STATE_SELECTED ) ) or brought to the top of the list but I don't know how to do that.
What should I do ?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Using wxSearchCtrl to search in a wxListCtr

Post by doublemax »

I think from a user's point of view he want's to see *only* the search results. For an easy solution i'd suggest to keep the list of songs separate from the wxListCtrl and whenever the search string changes, you populate the wxListCtrl from scratch with only the songs that match the search.

You can use wxListCtrl::SetItemData() to store the real index (or any other information) in the listitem itself.
Use the source, Luke!
User avatar
Azrael
Knows some wx things
Knows some wx things
Posts: 37
Joined: Sat Aug 05, 2017 2:44 pm
Location: Italy

Re: Using wxSearchCtrl to search in a wxListCtr

Post by Azrael »

Bear with me please... So, I keep my songs separate. What would you suggest ? The first thing that came into my mind were the STL containers. Are they good choice or should I use something else ?
coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: Using wxSearchCtrl to search in a wxListCtr

Post by coderrc »

Off the top of my head, I'd make a song/audio file class.
something simple like

Code: Select all

class song
{
std::string filename;
std::string title;
std::string author;
... other tags

... getters
... setters
};
then use a vector to store all the songs and populate the list based on your filter
psuedocode:

Code: Select all

void populateList(){

for( song s in songsVector)
{
   if ( filter is not empty)
   {
      if( s matches filter){
       addSongToList(s);
       }
    }
    else
    {
    addSongToList(s);
    }
}
ideally, whenever the filter changes, you should update the gui list. Although it might be best to set a wxTimer so that you can wait a few hundred ms between updates.

eg if the user takes 250ms to type "man with t"
without a timer, you would filter "m" then "ma" then "man" then "man " then ...
but with a 300ms timer, you would only filter once on "man with t", which should be noticeably faster since the UI isnt grinding away on useless updates.
User avatar
Azrael
Knows some wx things
Knows some wx things
Posts: 37
Joined: Sat Aug 05, 2017 2:44 pm
Location: Italy

Re: Using wxSearchCtrl to search in a wxListCtr

Post by Azrael »

coderrc wrote: ideally, whenever the filter changes, you should update the gui list. Although it might be best to set a wxTimer so that you can wait a few hundred ms between updates.
I didn't think about a timer. It seems a great solution. Thank you!
Post Reply