]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/textcompleter.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/textcompleter.h
3 // Purpose: interface of wxTextCompleter
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwindows.org>
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 @class wxTextCompleter
13 Base class for custom text completer objects.
15 Custom completer objects used with wxTextEntry::AutoComplete() must derive
16 from this class and implement its pure virtual method returning the
17 completions. You would typically use a custom completer when the total
18 number of completions is too big for performance to be acceptable if all of
19 them need to be returned at once but if they can be generated
20 hierarchically, i.e. only the first component initially, then the second
21 one after the user finished entering the first one and so on.
23 When inheriting from this class you need to implement its two pure virtual
24 methods. This allows to return the results incrementally and may or not be
25 convenient depending on where do they come from. If you prefer to return
26 all the completions at once, you should inherit from wxTextCompleterSimple
35 Function called to start iteration over the completions for the given
38 This function could start a database query, for example, if the results
39 are read from a database.
41 Notice that under some platforms (currently MSW only) it is called from
42 another thread context and so the appropriate synchronization mechanism
43 should be used to access any data also used by the main UI thread.
46 The prefix for which completions are to be generated.
48 @true to continue with calling GetNext() or @false to indicate that
49 there are no matches and GetNext() shouldn't be called at all.
51 virtual bool Start(const wxString
& prefix
) = 0;
54 Called to retrieve the next completion.
56 All completions returned by this function should start with the prefix
57 passed to the last call to Start().
59 Notice that, as Start(), this method is called from a worker thread
63 The next completion or an empty string to indicate that there are
66 virtual wxString
GetNext() = 0;
70 A simpler base class for custom completer objects.
72 This class may be simpler to use than the base wxTextCompleter as it allows
73 to implement only a single virtual method instead of two of them (at the
74 price of storing all completions in a temporary array).
76 Here is a simple example of a custom completer that completes the names of
77 some chess pieces. Of course, as the total list here has only four items it
78 would have been much simpler to just specify the array containing all the
79 completions in this example but the same approach could be used when the
80 total number of completions is much higher provided the number of
81 possibilities for each word is still relatively small:
83 class MyTextCompleter : public wxTextCompleterSimple
86 virtual void GetCompletions(const wxString& prefix, wxArrayString& res)
88 const wxString firstWord = prefix.BeforeFirst(' ');
89 if ( firstWord == "white" )
91 res.push_back("white pawn");
92 res.push_back("white rook");
94 else if ( firstWord == "black" )
96 res.push_back("black king");
97 res.push_back("black queen");
101 res.push_back("white");
102 res.push_back("black");
107 wxTextCtrl *text = ...;
108 text->AutoComplete(new MyTextCompleter);
115 class wxTextCompleterSimple
: public wxTextCompleter
119 Pure virtual method returning all possible completions for the given
122 The custom completer should examine the provided prefix and return all
123 the possible completions for it in the output array @a res.
125 Please notice that the returned values should start with the prefix,
126 otherwise they will be simply ignored, making adding them to the array
127 in the first place useless.
129 Notice that this function may be called from thread other than main one
130 (this is currently always the case under MSW) so the appropriate
131 synchronization mechanism should be used to protect the shared data.
134 The possibly empty prefix that the user had already entered.
136 Initially empty array that should be filled with all possible
137 completions (possibly none if there are no valid possibilities
138 starting with the given prefix).
140 virtual void GetCompletions(const wxString
& prefix
, wxArrayString
& res
) = 0;