]>
Commit | Line | Data |
---|---|---|
ea98f11c VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/textcompleter.h | |
3 | // Purpose: interface of wxTextCompleter | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2011-04-13 | |
ea98f11c VZ |
6 | // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwindows.org> |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | /** | |
11 | @class wxTextCompleter | |
12 | ||
13 | Base class for custom text completer objects. | |
14 | ||
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. | |
22 | ||
85047589 VZ |
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 | |
27 | instead. | |
28 | ||
29 | @since 2.9.2 | |
30 | */ | |
31 | class wxTextCompleter | |
32 | { | |
33 | public: | |
34 | /** | |
35 | Function called to start iteration over the completions for the given | |
36 | prefix. | |
37 | ||
38 | This function could start a database query, for example, if the results | |
39 | are read from a database. | |
40 | ||
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. | |
44 | ||
45 | @param prefix | |
46 | The prefix for which completions are to be generated. | |
47 | @return | |
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. | |
50 | */ | |
51 | virtual bool Start(const wxString& prefix) = 0; | |
52 | ||
53 | /** | |
54 | Called to retrieve the next completion. | |
55 | ||
56 | All completions returned by this function should start with the prefix | |
57 | passed to the last call to Start(). | |
58 | ||
59 | Notice that, as Start(), this method is called from a worker thread | |
60 | context under MSW. | |
61 | ||
62 | @return | |
63 | The next completion or an empty string to indicate that there are | |
64 | no more of them. | |
65 | */ | |
66 | virtual wxString GetNext() = 0; | |
67 | }; | |
68 | ||
69 | /** | |
70 | A simpler base class for custom completer objects. | |
71 | ||
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). | |
75 | ||
ea98f11c VZ |
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: | |
82 | @code | |
85047589 | 83 | class MyTextCompleter : public wxTextCompleterSimple |
ea98f11c VZ |
84 | { |
85 | public: | |
86 | virtual void GetCompletions(const wxString& prefix, wxArrayString& res) | |
87 | { | |
88 | const wxString firstWord = prefix.BeforeFirst(' '); | |
89 | if ( firstWord == "white" ) | |
90 | { | |
91 | res.push_back("white pawn"); | |
92 | res.push_back("white rook"); | |
93 | } | |
94 | else if ( firstWord == "black" ) | |
95 | { | |
96 | res.push_back("black king"); | |
97 | res.push_back("black queen"); | |
98 | } | |
99 | else | |
100 | { | |
101 | res.push_back("white"); | |
102 | res.push_back("black"); | |
103 | } | |
104 | } | |
105 | }; | |
106 | ... | |
107 | wxTextCtrl *text = ...; | |
108 | text->AutoComplete(new MyTextCompleter); | |
109 | @endcode | |
110 | ||
111 | @library{wxcore} | |
112 | ||
113 | @since 2.9.2 | |
114 | */ | |
85047589 | 115 | class wxTextCompleterSimple : public wxTextCompleter |
ea98f11c VZ |
116 | { |
117 | public: | |
118 | /** | |
119 | Pure virtual method returning all possible completions for the given | |
120 | prefix. | |
121 | ||
122 | The custom completer should examine the provided prefix and return all | |
123 | the possible completions for it in the output array @a res. | |
124 | ||
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. | |
128 | ||
e71e4c93 | 129 | Notice that this function may be called from thread other than main one |
85047589 VZ |
130 | (this is currently always the case under MSW) so the appropriate |
131 | synchronization mechanism should be used to protect the shared data. | |
e71e4c93 | 132 | |
ea98f11c VZ |
133 | @param prefix |
134 | The possibly empty prefix that the user had already entered. | |
135 | @param res | |
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). | |
139 | */ | |
140 | virtual void GetCompletions(const wxString& prefix, wxArrayString& res) = 0; | |
141 | }; |