textcompleter.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/textcompleter.h
  3. // Purpose: interface of wxTextCompleter
  4. // Author: Vadim Zeitlin
  5. // Created: 2011-04-13
  6. // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwindows.org>
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. /**
  10. @class wxTextCompleter
  11. Base class for custom text completer objects.
  12. Custom completer objects used with wxTextEntry::AutoComplete() must derive
  13. from this class and implement its pure virtual method returning the
  14. completions. You would typically use a custom completer when the total
  15. number of completions is too big for performance to be acceptable if all of
  16. them need to be returned at once but if they can be generated
  17. hierarchically, i.e. only the first component initially, then the second
  18. one after the user finished entering the first one and so on.
  19. When inheriting from this class you need to implement its two pure virtual
  20. methods. This allows to return the results incrementally and may or not be
  21. convenient depending on where do they come from. If you prefer to return
  22. all the completions at once, you should inherit from wxTextCompleterSimple
  23. instead.
  24. @since 2.9.2
  25. */
  26. class wxTextCompleter
  27. {
  28. public:
  29. /**
  30. Function called to start iteration over the completions for the given
  31. prefix.
  32. This function could start a database query, for example, if the results
  33. are read from a database.
  34. Notice that under some platforms (currently MSW only) it is called from
  35. another thread context and so the appropriate synchronization mechanism
  36. should be used to access any data also used by the main UI thread.
  37. @param prefix
  38. The prefix for which completions are to be generated.
  39. @return
  40. @true to continue with calling GetNext() or @false to indicate that
  41. there are no matches and GetNext() shouldn't be called at all.
  42. */
  43. virtual bool Start(const wxString& prefix) = 0;
  44. /**
  45. Called to retrieve the next completion.
  46. All completions returned by this function should start with the prefix
  47. passed to the last call to Start().
  48. Notice that, as Start(), this method is called from a worker thread
  49. context under MSW.
  50. @return
  51. The next completion or an empty string to indicate that there are
  52. no more of them.
  53. */
  54. virtual wxString GetNext() = 0;
  55. };
  56. /**
  57. A simpler base class for custom completer objects.
  58. This class may be simpler to use than the base wxTextCompleter as it allows
  59. to implement only a single virtual method instead of two of them (at the
  60. price of storing all completions in a temporary array).
  61. Here is a simple example of a custom completer that completes the names of
  62. some chess pieces. Of course, as the total list here has only four items it
  63. would have been much simpler to just specify the array containing all the
  64. completions in this example but the same approach could be used when the
  65. total number of completions is much higher provided the number of
  66. possibilities for each word is still relatively small:
  67. @code
  68. class MyTextCompleter : public wxTextCompleterSimple
  69. {
  70. public:
  71. virtual void GetCompletions(const wxString& prefix, wxArrayString& res)
  72. {
  73. const wxString firstWord = prefix.BeforeFirst(' ');
  74. if ( firstWord == "white" )
  75. {
  76. res.push_back("white pawn");
  77. res.push_back("white rook");
  78. }
  79. else if ( firstWord == "black" )
  80. {
  81. res.push_back("black king");
  82. res.push_back("black queen");
  83. }
  84. else
  85. {
  86. res.push_back("white");
  87. res.push_back("black");
  88. }
  89. }
  90. };
  91. ...
  92. wxTextCtrl *text = ...;
  93. text->AutoComplete(new MyTextCompleter);
  94. @endcode
  95. @library{wxcore}
  96. @since 2.9.2
  97. */
  98. class wxTextCompleterSimple : public wxTextCompleter
  99. {
  100. public:
  101. /**
  102. Pure virtual method returning all possible completions for the given
  103. prefix.
  104. The custom completer should examine the provided prefix and return all
  105. the possible completions for it in the output array @a res.
  106. Please notice that the returned values should start with the prefix,
  107. otherwise they will be simply ignored, making adding them to the array
  108. in the first place useless.
  109. Notice that this function may be called from thread other than main one
  110. (this is currently always the case under MSW) so the appropriate
  111. synchronization mechanism should be used to protect the shared data.
  112. @param prefix
  113. The possibly empty prefix that the user had already entered.
  114. @param res
  115. Initially empty array that should be filled with all possible
  116. completions (possibly none if there are no valid possibilities
  117. starting with the given prefix).
  118. */
  119. virtual void GetCompletions(const wxString& prefix, wxArrayString& res) = 0;
  120. };