wxSearchCtrl like Google Maps

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
pvn
Earned some good credits
Earned some good credits
Posts: 105
Joined: Mon Dec 26, 2016 5:21 am
Contact:

wxSearchCtrl like Google Maps

Post by pvn »

wxSearchCtrl has a search button, and auto complete like the search input in Google Maps.

However, in Google Maps the autocomplete makes a list of items show beneath the input text control.

If I want to implement a similar feature in wxWidgets, what would be the place to start?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSearchCtrl like Google Maps

Post by doublemax »

Any class that derives from wxTextEntry has autocomplete functionality:
http://docs.wxwidgets.org/trunk/classwx_text_entry.html

Just use a wxComboBox and use a static list of strings or derive your own class from wxTextCompleter for more fancy stuff.
Use the source, Luke!
pvn
Earned some good credits
Earned some good credits
Posts: 105
Joined: Mon Dec 26, 2016 5:21 am
Contact:

Re: wxSearchCtrl like Google Maps

Post by pvn »

ok,
in wxComboBox, is there a way to have the strings expanded without clicking the drop down menu?

my usage is:
1) user types a character, then combo expands with a string list returned from a search
2) user presses enter, then combo expands with the search
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSearchCtrl like Google Maps

Post by doublemax »

That's what it does.

Just try it out, it's only a few lines of code when you use a static list of items:

Code: Select all

wxArrayString items;
items.Add( wxT("Ananas") );
items.Add( wxT("Apple") );
items.Add( wxT("Banana") );
items.Add( wxT("Batteship") );
items.Add( wxT("Cherry") );
items.Add( wxT("Coconut") );

m_combo_box = new wxComboBox( panel, ITEM_COMBO_ID, wxEmptyString, wxDefaultPosition, wxDefaultSize, items, wxTE_PROCESS_ENTER);
m_combo_box->AutoComplete( items );
Use the source, Luke!
pvn
Earned some good credits
Earned some good credits
Posts: 105
Joined: Mon Dec 26, 2016 5:21 am
Contact:

Re: wxSearchCtrl like Google Maps

Post by pvn »

ok,
Popup() is the method that expands the list
pvn
Earned some good credits
Earned some good credits
Posts: 105
Joined: Mon Dec 26, 2016 5:21 am
Contact:

Re: wxSearchCtrl like Google Maps

Post by pvn »

that example works perfectly

but in my case , the combo is initially empty

Code: Select all

wxArrayString m_results;
  wxComboBox* m_combo;
I create it with

Code: Select all

 wxComboBox* combo = new wxComboBox(toolbar, ID_SEARCH_CTRL,
    m_str_search, wxDefaultPosition, wxSize(400, -1),
    m_results, wxTE_PROCESS_ENTER);
  toolbar->AddControl(combo);
  m_combo = combo;

then when pressing enter

Code: Select all

//m_results is filled
m_combo->Append(m_results);
m_combo->Popup();
this all works fine, but I am not sure how to add the auto complete here

adding

Code: Select all

m_combo->AutoComplete(m_results);
causes the combo not to display anything
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSearchCtrl like Google Maps

Post by doublemax »

Which platform and wxWidgets version are you using?
Popup() is the method that expands the list
That should not be necessary to let the autocomplete choices appear. As soon as you type "a" into the text field, the popup with "Ananas" and "Apple" should appear.

Maybe just a single wxTextCtrl is more suitable for your purpose than a wxComboBox.
Use the source, Luke!
pvn
Earned some good credits
Earned some good credits
Posts: 105
Joined: Mon Dec 26, 2016 5:21 am
Contact:

Re: wxSearchCtrl like Google Maps

Post by pvn »

the autocomplete is working.
I want to call Popup() after the search so that it is visible immediately, just like Google maps does

Code: Select all

wxArrayString items;
        items.Add(wxT("Ananas"));
        items.Add(wxT("Apple"));
        items.Add(wxT("Banana"));
        items.Add(wxT("Batteship"));
        items.Add(wxT("Cherry"));
        items.Add(wxT("Coconut"));

        m_combo->Append(items);
        m_combo->AutoComplete(items);
        m_combo->Popup();
Post Reply