textentry.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/textentry.h
  3. // Purpose: declares wxTextEntry interface defining a simple text entry
  4. // Author: Vadim Zeitlin
  5. // Created: 2007-09-24
  6. // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_TEXTENTRY_H_
  10. #define _WX_TEXTENTRY_H_
  11. // wxTextPos is the position in the text (currently it's hardly used anywhere
  12. // and should probably be replaced with int anyhow)
  13. typedef long wxTextPos;
  14. class WXDLLIMPEXP_FWD_BASE wxArrayString;
  15. class WXDLLIMPEXP_FWD_CORE wxTextCompleter;
  16. class WXDLLIMPEXP_FWD_CORE wxTextEntryHintData;
  17. class WXDLLIMPEXP_FWD_CORE wxWindow;
  18. #include "wx/filefn.h" // for wxFILE and wxDIR only
  19. #include "wx/gdicmn.h" // for wxPoint
  20. // ----------------------------------------------------------------------------
  21. // wxTextEntryBase
  22. // ----------------------------------------------------------------------------
  23. class WXDLLIMPEXP_CORE wxTextEntryBase
  24. {
  25. public:
  26. wxTextEntryBase() { m_eventsBlock = 0; m_hintData = NULL; }
  27. virtual ~wxTextEntryBase();
  28. // accessing the value
  29. // -------------------
  30. // SetValue() generates a text change event, ChangeValue() doesn't
  31. virtual void SetValue(const wxString& value)
  32. { DoSetValue(value, SetValue_SendEvent); }
  33. virtual void ChangeValue(const wxString& value);
  34. // writing text inserts it at the current position replacing any current
  35. // selection, appending always inserts it at the end and doesn't remove any
  36. // existing text (but it will reset the selection if there is any)
  37. virtual void WriteText(const wxString& text) = 0;
  38. virtual void AppendText(const wxString& text);
  39. virtual wxString GetValue() const;
  40. virtual wxString GetRange(long from, long to) const;
  41. bool IsEmpty() const { return GetLastPosition() <= 0; }
  42. // editing operations
  43. // ------------------
  44. virtual void Replace(long from, long to, const wxString& value);
  45. virtual void Remove(long from, long to) = 0;
  46. virtual void Clear() { SetValue(wxString()); }
  47. void RemoveSelection();
  48. // clipboard operations
  49. // --------------------
  50. virtual void Copy() = 0;
  51. virtual void Cut() = 0;
  52. virtual void Paste() = 0;
  53. virtual bool CanCopy() const;
  54. virtual bool CanCut() const;
  55. virtual bool CanPaste() const;
  56. // undo/redo
  57. // ---------
  58. virtual void Undo() = 0;
  59. virtual void Redo() = 0;
  60. virtual bool CanUndo() const = 0;
  61. virtual bool CanRedo() const = 0;
  62. // insertion point
  63. // ---------------
  64. // note that moving insertion point removes any current selection
  65. virtual void SetInsertionPoint(long pos) = 0;
  66. virtual void SetInsertionPointEnd() { SetInsertionPoint(-1); }
  67. virtual long GetInsertionPoint() const = 0;
  68. virtual long GetLastPosition() const = 0;
  69. // selection
  70. // ---------
  71. virtual void SetSelection(long from, long to) = 0;
  72. virtual void SelectAll() { SetSelection(-1, -1); }
  73. virtual void SelectNone()
  74. { const long pos = GetInsertionPoint(); SetSelection(pos, pos); }
  75. virtual void GetSelection(long *from, long *to) const = 0;
  76. bool HasSelection() const;
  77. virtual wxString GetStringSelection() const;
  78. // auto-completion
  79. // ---------------
  80. // these functions allow to auto-complete the text already entered into the
  81. // control using either the given fixed list of strings, the paths from the
  82. // file system or an arbitrary user-defined completer
  83. //
  84. // they all return true if completion was enabled or false on error (most
  85. // commonly meaning that this functionality is not available under the
  86. // current platform)
  87. bool AutoComplete(const wxArrayString& choices)
  88. { return DoAutoCompleteStrings(choices); }
  89. bool AutoCompleteFileNames()
  90. { return DoAutoCompleteFileNames(wxFILE); }
  91. bool AutoCompleteDirectories()
  92. { return DoAutoCompleteFileNames(wxDIR); }
  93. // notice that we take ownership of the pointer and will delete it
  94. //
  95. // if the pointer is NULL auto-completion is disabled
  96. bool AutoComplete(wxTextCompleter *completer)
  97. { return DoAutoCompleteCustom(completer); }
  98. // status
  99. // ------
  100. virtual bool IsEditable() const = 0;
  101. virtual void SetEditable(bool editable) = 0;
  102. // set the max number of characters which may be entered in a single line
  103. // text control
  104. virtual void SetMaxLength(unsigned long WXUNUSED(len)) { }
  105. // hints
  106. // -----
  107. // hint is the (usually greyed out) text shown in the control as long as
  108. // it's empty and doesn't have focus, it is typically used in controls used
  109. // for searching to let the user know what is supposed to be entered there
  110. virtual bool SetHint(const wxString& hint);
  111. virtual wxString GetHint() const;
  112. // margins
  113. // -------
  114. // margins are the empty space between borders of control and the text
  115. // itself. When setting margin, use value -1 to indicate that specific
  116. // margin should not be changed.
  117. bool SetMargins(const wxPoint& pt)
  118. { return DoSetMargins(pt); }
  119. bool SetMargins(wxCoord left, wxCoord top = -1)
  120. { return DoSetMargins(wxPoint(left, top)); }
  121. wxPoint GetMargins() const
  122. { return DoGetMargins(); }
  123. // implementation only
  124. // -------------------
  125. // generate the wxEVT_TEXT event for GetEditableWindow(),
  126. // like SetValue() does and return true if the event was processed
  127. //
  128. // NB: this is public for wxRichTextCtrl use only right now, do not call it
  129. static bool SendTextUpdatedEvent(wxWindow *win);
  130. // generate the wxEVT_TEXT event for this window
  131. bool SendTextUpdatedEvent()
  132. {
  133. return SendTextUpdatedEvent(GetEditableWindow());
  134. }
  135. // generate the wxEVT_TEXT event for this window if the
  136. // events are not currently disabled
  137. void SendTextUpdatedEventIfAllowed()
  138. {
  139. if ( EventsAllowed() )
  140. SendTextUpdatedEvent();
  141. }
  142. // this function is provided solely for the purpose of forwarding text
  143. // change notifications state from one control to another, e.g. it can be
  144. // used by a wxComboBox which derives from wxTextEntry if it delegates all
  145. // of its methods to another wxTextCtrl
  146. void ForwardEnableTextChangedEvents(bool enable)
  147. {
  148. // it's important to call the functions which update m_eventsBlock here
  149. // and not just our own EnableTextChangedEvents() because our state
  150. // (i.e. the result of EventsAllowed()) must change as well
  151. if ( enable )
  152. ResumeTextChangedEvents();
  153. else
  154. SuppressTextChangedEvents();
  155. }
  156. protected:
  157. // flags for DoSetValue(): common part of SetValue() and ChangeValue() and
  158. // also used to implement WriteText() in wxMSW
  159. enum
  160. {
  161. SetValue_NoEvent = 0,
  162. SetValue_SendEvent = 1,
  163. SetValue_SelectionOnly = 2
  164. };
  165. virtual void DoSetValue(const wxString& value, int flags);
  166. virtual wxString DoGetValue() const = 0;
  167. // override this to return the associated window, it will be used for event
  168. // generation and also by generic hints implementation
  169. virtual wxWindow *GetEditableWindow() = 0;
  170. // margins functions
  171. virtual bool DoSetMargins(const wxPoint& pt);
  172. virtual wxPoint DoGetMargins() const;
  173. // the derived classes should override these virtual methods to implement
  174. // auto-completion, they do the same thing as their public counterparts but
  175. // have different names to allow overriding just one of them without hiding
  176. // the other one(s)
  177. virtual bool DoAutoCompleteStrings(const wxArrayString& WXUNUSED(choices))
  178. { return false; }
  179. virtual bool DoAutoCompleteFileNames(int WXUNUSED(flags)) // wxFILE | wxDIR
  180. { return false; }
  181. virtual bool DoAutoCompleteCustom(wxTextCompleter *completer);
  182. // class which should be used to temporarily disable text change events
  183. //
  184. // if suppress argument in ctor is false, nothing is done
  185. class EventsSuppressor
  186. {
  187. public:
  188. EventsSuppressor(wxTextEntryBase *text, bool suppress = true)
  189. : m_text(text),
  190. m_suppress(suppress)
  191. {
  192. if ( m_suppress )
  193. m_text->SuppressTextChangedEvents();
  194. }
  195. ~EventsSuppressor()
  196. {
  197. if ( m_suppress )
  198. m_text->ResumeTextChangedEvents();
  199. }
  200. private:
  201. wxTextEntryBase *m_text;
  202. bool m_suppress;
  203. };
  204. friend class EventsSuppressor;
  205. private:
  206. // suppress or resume the text changed events generation: don't use these
  207. // functions directly, use EventsSuppressor class above instead
  208. void SuppressTextChangedEvents()
  209. {
  210. if ( !m_eventsBlock++ )
  211. EnableTextChangedEvents(false);
  212. }
  213. void ResumeTextChangedEvents()
  214. {
  215. if ( !--m_eventsBlock )
  216. EnableTextChangedEvents(true);
  217. }
  218. // this must be overridden in the derived classes if our implementation of
  219. // SetValue() or Replace() is used to disable (and enable back) generation
  220. // of the text changed events
  221. //
  222. // initially the generation of the events is enabled
  223. virtual void EnableTextChangedEvents(bool WXUNUSED(enable)) { }
  224. // return true if the events are currently not suppressed
  225. bool EventsAllowed() const { return m_eventsBlock == 0; }
  226. // if this counter is non-null, events are blocked
  227. unsigned m_eventsBlock;
  228. // hint-related stuff, only allocated if/when SetHint() is used
  229. wxTextEntryHintData *m_hintData;
  230. // It needs to call our Do{Get,Set}Value() to work with the real control
  231. // contents.
  232. friend class wxTextEntryHintData;
  233. };
  234. #ifdef __WXUNIVERSAL__
  235. // TODO: we need to use wxTextEntryDelegate here, but for now just prevent
  236. // the GTK/MSW classes from being used in wxUniv build
  237. class WXDLLIMPEXP_CORE wxTextEntry : public wxTextEntryBase
  238. {
  239. };
  240. #elif defined(__WXGTK20__)
  241. #include "wx/gtk/textentry.h"
  242. #elif defined(__WXMAC__)
  243. #include "wx/osx/textentry.h"
  244. #elif defined(__WXMSW__)
  245. #include "wx/msw/textentry.h"
  246. #elif defined(__WXMOTIF__)
  247. #include "wx/motif/textentry.h"
  248. #elif defined(__WXPM__)
  249. #include "wx/os2/textentry.h"
  250. #else
  251. // no platform-specific implementation of wxTextEntry yet
  252. class WXDLLIMPEXP_CORE wxTextEntry : public wxTextEntryBase
  253. {
  254. };
  255. #endif
  256. #endif // _WX_TEXTENTRY_H_