Refactor min autocomplete length handling in widgets sample
Call DoUseCustomAutoComplete() function instead of using an artificial event. Also move the menu item in the radio group with the other autocomplete-related commands as it's exclusive with them.
This commit is contained in:
@@ -192,6 +192,8 @@ protected:
|
|||||||
void OnAutoCompleteCustom(wxCommandEvent& event);
|
void OnAutoCompleteCustom(wxCommandEvent& event);
|
||||||
void OnAutoCompleteKeyLength(wxCommandEvent& event);
|
void OnAutoCompleteKeyLength(wxCommandEvent& event);
|
||||||
|
|
||||||
|
void DoUseCustomAutoComplete(size_t minLength = 1);
|
||||||
|
|
||||||
void OnSetHint(wxCommandEvent& event);
|
void OnSetHint(wxCommandEvent& event);
|
||||||
|
|
||||||
void OnUpdateTextUI(wxUpdateUIEvent& event)
|
void OnUpdateTextUI(wxUpdateUIEvent& event)
|
||||||
@@ -221,9 +223,6 @@ private:
|
|||||||
// the book containing the test pages
|
// the book containing the test pages
|
||||||
WidgetsBookCtrl *m_book;
|
WidgetsBookCtrl *m_book;
|
||||||
|
|
||||||
//
|
|
||||||
int m_prefixMinLength;
|
|
||||||
|
|
||||||
// any class wishing to process wxWidgets events must use this macro
|
// any class wishing to process wxWidgets events must use this macro
|
||||||
wxDECLARE_EVENT_TABLE();
|
wxDECLARE_EVENT_TABLE();
|
||||||
};
|
};
|
||||||
@@ -393,8 +392,6 @@ WidgetsFrame::WidgetsFrame(const wxString& title)
|
|||||||
#endif // USE_LOG
|
#endif // USE_LOG
|
||||||
m_book = NULL;
|
m_book = NULL;
|
||||||
|
|
||||||
m_prefixMinLength = 1;
|
|
||||||
|
|
||||||
#if wxUSE_MENUS
|
#if wxUSE_MENUS
|
||||||
// create the menubar
|
// create the menubar
|
||||||
wxMenuBar *mbar = new wxMenuBar;
|
wxMenuBar *mbar = new wxMenuBar;
|
||||||
@@ -452,9 +449,9 @@ WidgetsFrame::WidgetsFrame(const wxString& title)
|
|||||||
wxT("&Directories names auto-completion"));
|
wxT("&Directories names auto-completion"));
|
||||||
menuTextEntry->AppendRadioItem(TextEntry_AutoCompleteCustom,
|
menuTextEntry->AppendRadioItem(TextEntry_AutoCompleteCustom,
|
||||||
wxT("&Custom auto-completion"));
|
wxT("&Custom auto-completion"));
|
||||||
|
menuTextEntry->AppendRadioItem(TextEntry_AutoCompleteKeyLength,
|
||||||
|
wxT("Custom with &min length"));
|
||||||
menuTextEntry->AppendSeparator();
|
menuTextEntry->AppendSeparator();
|
||||||
menuTextEntry->Append(TextEntry_AutoCompleteKeyLength,
|
|
||||||
wxT("&Minimum key length for auto-completion"));
|
|
||||||
menuTextEntry->Append(TextEntry_SetHint, "Set help &hint");
|
menuTextEntry->Append(TextEntry_SetHint, "Set help &hint");
|
||||||
|
|
||||||
mbar->Append(menuTextEntry, wxT("&Text"));
|
mbar->Append(menuTextEntry, wxT("&Text"));
|
||||||
@@ -1036,6 +1033,11 @@ void WidgetsFrame::OnAutoCompleteDirectories(wxCommandEvent& WXUNUSED(event))
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WidgetsFrame::OnAutoCompleteCustom(wxCommandEvent& WXUNUSED(event))
|
void WidgetsFrame::OnAutoCompleteCustom(wxCommandEvent& WXUNUSED(event))
|
||||||
|
{
|
||||||
|
DoUseCustomAutoComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
void WidgetsFrame::DoUseCustomAutoComplete(size_t minLength)
|
||||||
{
|
{
|
||||||
wxTextEntryBase *entry = CurrentPage()->GetTextEntry();
|
wxTextEntryBase *entry = CurrentPage()->GetTextEntry();
|
||||||
wxCHECK_RET( entry, "menu item should be disabled" );
|
wxCHECK_RET( entry, "menu item should be disabled" );
|
||||||
@@ -1049,7 +1051,10 @@ void WidgetsFrame::OnAutoCompleteCustom(wxCommandEvent& WXUNUSED(event))
|
|||||||
class CustomTextCompleter : public wxTextCompleterSimple
|
class CustomTextCompleter : public wxTextCompleterSimple
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CustomTextCompleter( int length ) : m_minLength( length ) {}
|
explicit CustomTextCompleter(size_t minLength)
|
||||||
|
: m_minLength(minLength)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
virtual void GetCompletions(const wxString& prefix, wxArrayString& res) wxOVERRIDE
|
virtual void GetCompletions(const wxString& prefix, wxArrayString& res) wxOVERRIDE
|
||||||
{
|
{
|
||||||
@@ -1077,12 +1082,10 @@ void WidgetsFrame::OnAutoCompleteCustom(wxCommandEvent& WXUNUSED(event))
|
|||||||
} logCompletions(prefix, res);
|
} logCompletions(prefix, res);
|
||||||
|
|
||||||
|
|
||||||
// Normally it doesn't make sense to complete empty control, there
|
// Wait for enough text to be entered before proposing completions:
|
||||||
// are too many choices and listing them all wouldn't be helpful.
|
// this is done to avoid proposing too many of them when the
|
||||||
// Or if we know in advance that a prefix with one or two characters
|
// control is empty, for example.
|
||||||
// would still results in too many choices too.
|
if ( prefix.length() < m_minLength )
|
||||||
if ( prefix.empty() ||
|
|
||||||
prefix.length() < static_cast<size_t>(m_minLength) )
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// The only valid strings start with 3 digits so check for their
|
// The only valid strings start with 3 digits so check for their
|
||||||
@@ -1134,10 +1137,10 @@ void WidgetsFrame::OnAutoCompleteCustom(wxCommandEvent& WXUNUSED(event))
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int m_minLength;
|
size_t m_minLength;
|
||||||
};
|
};
|
||||||
|
|
||||||
if ( entry->AutoComplete( new CustomTextCompleter( m_prefixMinLength ) ) )
|
if ( entry->AutoComplete(new CustomTextCompleter(minLength)))
|
||||||
{
|
{
|
||||||
wxLogMessage("Enabled custom auto completer for \"NNN XX\" items "
|
wxLogMessage("Enabled custom auto completer for \"NNN XX\" items "
|
||||||
"(where N is a digit and X is a letter).");
|
"(where N is a digit and X is a letter).");
|
||||||
@@ -1152,16 +1155,17 @@ void WidgetsFrame::OnAutoCompleteKeyLength(wxCommandEvent& WXUNUSED(event))
|
|||||||
{
|
{
|
||||||
const wxString message = "The auto-completion is triggered if and only if\n"
|
const wxString message = "The auto-completion is triggered if and only if\n"
|
||||||
"the length of the search key (prefix) is at least [LENGTH].\n"
|
"the length of the search key (prefix) is at least [LENGTH].\n"
|
||||||
"Hint: negative values disable auto-completion.";
|
"Hint: 0 disables the length check completely.";
|
||||||
const wxString prompt = "Enter the minimum key length:";
|
const wxString prompt = "Enter the minimum key length:";
|
||||||
const wxString caption = "Minimum key length";
|
const wxString caption = "Minimum key length";
|
||||||
|
|
||||||
m_prefixMinLength = wxGetNumberFromUser(message, prompt, caption, 1, -1, 100, this);
|
int res = wxGetNumberFromUser(message, prompt, caption, 1, 0, 100, this);
|
||||||
|
if ( res == -1 )
|
||||||
|
return;
|
||||||
|
|
||||||
wxCommandEvent theEvent(wxEVT_MENU, TextEntry_AutoCompleteCustom);
|
wxLogMessage("The minimum key length for autocomplete is %d.", res);
|
||||||
ProcessEventLocally(theEvent);
|
|
||||||
|
|
||||||
wxLogMessage("The minimum key length for autocomplete is : %d.", m_prefixMinLength);
|
DoUseCustomAutoComplete(static_cast<size_t>(res));
|
||||||
}
|
}
|
||||||
|
|
||||||
void WidgetsFrame::OnSetHint(wxCommandEvent& WXUNUSED(event))
|
void WidgetsFrame::OnSetHint(wxCommandEvent& WXUNUSED(event))
|
||||||
|
Reference in New Issue
Block a user