textentry.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/textentry.h
  3. // Purpose: interface of wxTextEntry
  4. // Author: Vadim Zeitlin
  5. // Created: 2009-03-01 (extracted from wx/textctrl.h)
  6. // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwindows.org>
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. /**
  10. wxTextPos is a position in the text
  11. */
  12. typedef long wxTextPos;
  13. /**
  14. @class wxTextEntry
  15. Common base class for single line text entry fields.
  16. This class is not a control itself, as it doesn't derive from wxWindow.
  17. Instead it is used as a base class by other controls, notably wxTextCtrl
  18. and wxComboBox and gathers the methods common to both of them.
  19. @library{wxcore}
  20. @category{ctrl}
  21. @see wxTextCtrl, wxComboBox
  22. @since 2.9.0
  23. */
  24. class wxTextEntry
  25. {
  26. public:
  27. /**
  28. Appends the text to the end of the text control.
  29. @param text
  30. Text to write to the text control.
  31. @remarks
  32. After the text is appended, the insertion point will be at the
  33. end of the text control. If this behaviour is not desired, the
  34. programmer should use GetInsertionPoint() and SetInsertionPoint().
  35. @see WriteText()
  36. */
  37. virtual void AppendText(const wxString& text);
  38. /**
  39. Call this function to enable auto-completion of the text typed in a
  40. single-line text control using the given @a choices.
  41. Notice that currently this function is only implemented in wxGTK2,
  42. wxMSW and wxOSX/Cocoa (for wxTextCtrl only, but not for wxComboBox)
  43. ports and does nothing under the other platforms.
  44. @since 2.9.0
  45. @return
  46. @true if the auto-completion was enabled or @false if the operation
  47. failed, typically because auto-completion is not supported by the
  48. current platform.
  49. @see AutoCompleteFileNames()
  50. */
  51. bool AutoComplete(const wxArrayString& choices);
  52. /**
  53. Enable auto-completion using the provided completer object.
  54. This method should be used instead of AutoComplete() overload taking
  55. the array of possible completions if the total number of strings is too
  56. big as it allows to return the completions dynamically, depending on
  57. the text already entered by user and so is more efficient.
  58. The specified @a completer object will be used to retrieve the list of
  59. possible completions for the already entered text and will be deleted
  60. by wxTextEntry itself when it's not needed any longer.
  61. Notice that you need to include @c wx/textcompleter.h in order to
  62. define your class inheriting from wxTextCompleter.
  63. Currently this method is only implemented in wxMSW and wxOSX/Cocoa (for
  64. wxTextCtrl only, but not for wxComboBox).
  65. @since 2.9.2
  66. @param completer
  67. The object to be used for generating completions if non-@NULL. If
  68. it is @NULL, auto-completion is disabled. The wxTextEntry object
  69. takes ownership of this pointer and will delete it in any case
  70. (i.e. even if this method returns @false).
  71. @return
  72. @true if the auto-completion was enabled or @false if the operation
  73. failed, typically because auto-completion is not supported by the
  74. current platform.
  75. @see wxTextCompleter
  76. */
  77. bool AutoComplete(wxTextCompleter *completer);
  78. /**
  79. Call this function to enable auto-completion of the text typed in a
  80. single-line text control using all valid file system paths.
  81. Notice that currently this function is only implemented in wxMSW port
  82. and does nothing under the other platforms.
  83. @since 2.9.0
  84. @return
  85. @true if the auto-completion was enabled or @false if the operation
  86. failed, typically because auto-completion is not supported by the
  87. current platform.
  88. @see AutoComplete()
  89. */
  90. bool AutoCompleteFileNames();
  91. /**
  92. Call this function to enable auto-completion of the text using the file
  93. system directories.
  94. Unlike AutoCompleteFileNames() which completes both file names and
  95. directories, this function only completes the directory names.
  96. Notice that currently this function is only implemented in wxMSW port
  97. and does nothing under the other platforms.
  98. @since 2.9.3
  99. @return
  100. @true if the auto-completion was enabled or @false if the operation
  101. failed, typically because auto-completion is not supported by the
  102. current platform.
  103. @see AutoComplete()
  104. */
  105. bool AutoCompleteDirectories();
  106. /**
  107. Returns @true if the selection can be copied to the clipboard.
  108. */
  109. virtual bool CanCopy() const;
  110. /**
  111. Returns @true if the selection can be cut to the clipboard.
  112. */
  113. virtual bool CanCut() const;
  114. /**
  115. Returns @true if the contents of the clipboard can be pasted into the
  116. text control.
  117. On some platforms (Motif, GTK) this is an approximation and returns
  118. @true if the control is editable, @false otherwise.
  119. */
  120. virtual bool CanPaste() const;
  121. /**
  122. Returns @true if there is a redo facility available and the last
  123. operation can be redone.
  124. */
  125. virtual bool CanRedo() const;
  126. /**
  127. Returns @true if there is an undo facility available and the last
  128. operation can be undone.
  129. */
  130. virtual bool CanUndo() const;
  131. /**
  132. Sets the new text control value.
  133. It also marks the control as not-modified which means that IsModified()
  134. would return @false immediately after the call to ChangeValue().
  135. The insertion point is set to the start of the control (i.e. position
  136. 0) by this function.
  137. This functions does not generate the @c wxEVT_TEXT
  138. event but otherwise is identical to SetValue().
  139. See @ref overview_events_prog for more information.
  140. @since 2.7.1
  141. @param value
  142. The new value to set. It may contain newline characters if the text
  143. control is multi-line.
  144. */
  145. virtual void ChangeValue(const wxString& value);
  146. /**
  147. Clears the text in the control.
  148. Note that this function will generate a @c wxEVT_TEXT
  149. event, i.e. its effect is identical to calling @c SetValue("").
  150. */
  151. virtual void Clear();
  152. /**
  153. Copies the selected text to the clipboard.
  154. */
  155. virtual void Copy();
  156. /**
  157. Copies the selected text to the clipboard and removes it from the control.
  158. */
  159. virtual void Cut();
  160. /**
  161. Returns the insertion point, or cursor, position.
  162. This is defined as the zero based index of the character position to
  163. the right of the insertion point. For example, if the insertion point
  164. is at the end of the single-line text control, it is equal to
  165. GetLastPosition().
  166. Notice that insertion position is, in general, different from the index
  167. of the character the cursor position at in the string returned by
  168. GetValue(). While this is always the case for the single line controls,
  169. multi-line controls can use two characters @c "\\r\\n" as line
  170. separator (this is notably the case under MSW) meaning that indices in
  171. the control and its string value are offset by 1 for every line.
  172. Hence to correctly get the character at the current cursor position,
  173. taking into account that there can be none if the cursor is at the end
  174. of the string, you could do the following:
  175. @code
  176. wxString GetCurrentChar(wxTextCtrl *tc)
  177. {
  178. long pos = tc->GetInsertionPoint();
  179. if ( pos == tc->GetLastPosition() )
  180. return wxString();
  181. return tc->GetRange(pos, pos + 1);
  182. }
  183. @endcode
  184. */
  185. virtual long GetInsertionPoint() const;
  186. /**
  187. Returns the zero based index of the last position in the text control,
  188. which is equal to the number of characters in the control.
  189. */
  190. virtual wxTextPos GetLastPosition() const;
  191. /**
  192. Returns the string containing the text starting in the positions
  193. @a from and up to @a to in the control.
  194. The positions must have been returned by another wxTextCtrl method.
  195. Please note that the positions in a multiline wxTextCtrl do @b not
  196. correspond to the indices in the string returned by GetValue() because
  197. of the different new line representations (@c CR or @c CR LF) and so
  198. this method should be used to obtain the correct results instead of
  199. extracting parts of the entire value. It may also be more efficient,
  200. especially if the control contains a lot of data.
  201. */
  202. virtual wxString GetRange(long from, long to) const;
  203. /**
  204. Gets the current selection span.
  205. If the returned values are equal, there was no selection. Please note
  206. that the indices returned may be used with the other wxTextCtrl methods
  207. but don't necessarily represent the correct indices into the string
  208. returned by GetValue() for multiline controls under Windows (at least,)
  209. you should use GetStringSelection() to get the selected text.
  210. @param from
  211. The returned first position.
  212. @param to
  213. The returned last position.
  214. @beginWxPerlOnly
  215. In wxPerl this method takes no parameters and returns a
  216. 2-element list (from, to).
  217. @endWxPerlOnly
  218. */
  219. virtual void GetSelection(long* from, long* to) const;
  220. /**
  221. Gets the text currently selected in the control.
  222. If there is no selection, the returned string is empty.
  223. */
  224. virtual wxString GetStringSelection() const;
  225. /**
  226. Gets the contents of the control.
  227. Notice that for a multiline text control, the lines will be separated
  228. by (Unix-style) @c \\n characters, even under Windows where they are
  229. separated by a @c \\r\\n sequence in the native control.
  230. */
  231. virtual wxString GetValue() const;
  232. /**
  233. Returns @true if the controls contents may be edited by user (note that
  234. it always can be changed by the program).
  235. In other words, this functions returns @true if the control hasn't been
  236. put in read-only mode by a previous call to SetEditable().
  237. */
  238. virtual bool IsEditable() const;
  239. /**
  240. Returns @true if the control is currently empty.
  241. This is the same as @c GetValue().empty() but can be much more
  242. efficient for the multiline controls containing big amounts of text.
  243. @since 2.7.1
  244. */
  245. virtual bool IsEmpty() const;
  246. /**
  247. Pastes text from the clipboard to the text item.
  248. */
  249. virtual void Paste();
  250. /**
  251. If there is a redo facility and the last operation can be redone,
  252. redoes the last operation.
  253. Does nothing if there is no redo facility.
  254. */
  255. virtual void Redo();
  256. /**
  257. Removes the text starting at the first given position up to
  258. (but not including) the character at the last position.
  259. This function puts the current insertion point position at @a to as a
  260. side effect.
  261. @param from
  262. The first position.
  263. @param to
  264. The last position.
  265. */
  266. virtual void Remove(long from, long to);
  267. /**
  268. Replaces the text starting at the first position up to
  269. (but not including) the character at the last position with the given text.
  270. This function puts the current insertion point position at @a to as a
  271. side effect.
  272. @param from
  273. The first position.
  274. @param to
  275. The last position.
  276. @param value
  277. The value to replace the existing text with.
  278. */
  279. virtual void Replace(long from, long to, const wxString& value);
  280. /**
  281. Makes the text item editable or read-only, overriding the
  282. @b wxTE_READONLY flag.
  283. @param editable
  284. If @true, the control is editable. If @false, the control is
  285. read-only.
  286. @see IsEditable()
  287. */
  288. virtual void SetEditable(bool editable);
  289. /**
  290. Sets the insertion point at the given position.
  291. @param pos
  292. Position to set, in the range from 0 to GetLastPosition() inclusive.
  293. */
  294. virtual void SetInsertionPoint(long pos);
  295. /**
  296. Sets the insertion point at the end of the text control.
  297. This is equivalent to calling wxTextCtrl::SetInsertionPoint() with
  298. wxTextCtrl::GetLastPosition() argument.
  299. */
  300. virtual void SetInsertionPointEnd();
  301. /**
  302. This function sets the maximum number of characters the user can enter
  303. into the control.
  304. In other words, it allows to limit the text value length to @a len not
  305. counting the terminating @c NUL character.
  306. If @a len is 0, the previously set max length limit, if any, is discarded
  307. and the user may enter as much text as the underlying native text control widget
  308. supports (typically at least 32Kb).
  309. If the user tries to enter more characters into the text control when it
  310. already is filled up to the maximal length, a @c wxEVT_TEXT_MAXLEN
  311. event is sent to notify the program about it (giving it the possibility
  312. to show an explanatory message, for example) and the extra input is discarded.
  313. Note that in wxGTK this function may only be used with single line text controls.
  314. */
  315. virtual void SetMaxLength(unsigned long len);
  316. /**
  317. Selects the text starting at the first position up to (but not
  318. including) the character at the last position.
  319. If both parameters are equal to -1 all text in the control is selected.
  320. Notice that the insertion point will be moved to @a from by this
  321. function.
  322. @param from
  323. The first position.
  324. @param to
  325. The last position.
  326. @see SelectAll()
  327. */
  328. virtual void SetSelection(long from, long to);
  329. /**
  330. Selects all text in the control.
  331. @see SetSelection()
  332. */
  333. virtual void SelectAll();
  334. /**
  335. Deselects selected text in the control.
  336. @since 2.9.5
  337. */
  338. virtual void SelectNone();
  339. /**
  340. Sets a hint shown in an empty unfocused text control.
  341. The hints are usually used to indicate to the user what is supposed to
  342. be entered into the given entry field, e.g. a common use of them is to
  343. show an explanation of what can be entered in a wxSearchCtrl.
  344. The hint is shown (usually greyed out) for an empty control until it
  345. gets focus and is shown again if the control loses it and remains
  346. empty. It won't be shown once the control has a non-empty value,
  347. although it will be shown again if the control contents is cleared.
  348. Because of this, it generally only makes sense to use hints with the
  349. controls which are initially empty.
  350. Notice that hints are known as <em>cue banners</em> under MSW or
  351. <em>placeholder strings</em> under OS X.
  352. @remarks For the platforms without native hints support (and currently
  353. only the MSW port does have it and even there it is only used under
  354. Windows Vista and later only), the implementation has several known
  355. limitations. Notably, the hint display will not be properly updated
  356. if you change wxTextEntry contents programmatically when the hint
  357. is displayed using methods other than SetValue() or ChangeValue()
  358. or others which use them internally (e.g. Clear()). In other words,
  359. currently you should avoid calling methods such as WriteText() or
  360. Replace() when using hints and the text control is empty.
  361. @remarks Hints can only be used for single line text controls,
  362. native multi-line text controls don't support hints under any
  363. platform and hence wxWidgets doesn't provide them neither.
  364. @since 2.9.0
  365. */
  366. virtual bool SetHint(const wxString& hint);
  367. /**
  368. Returns the current hint string.
  369. See SetHint() for more information about hints.
  370. @since 2.9.0
  371. */
  372. virtual wxString GetHint() const;
  373. //@{
  374. /**
  375. Attempts to set the control margins. When margins are given as wxPoint,
  376. x indicates the left and y the top margin. Use -1 to indicate that
  377. an existing value should be used.
  378. @return
  379. @true if setting of all requested margins was successful.
  380. @since 2.9.1
  381. */
  382. bool SetMargins(const wxPoint& pt);
  383. bool SetMargins(wxCoord left, wxCoord top = -1);
  384. //@}
  385. /**
  386. Returns the margins used by the control. The @c x field of the returned
  387. point is the horizontal margin and the @c y field is the vertical one.
  388. @remarks If given margin cannot be accurately determined, its value
  389. will be set to -1. On some platforms you cannot obtain valid
  390. margin values until you have called SetMargins().
  391. @see SetMargins()
  392. @since 2.9.1
  393. */
  394. wxPoint GetMargins() const;
  395. /**
  396. Sets the new text control value.
  397. It also marks the control as not-modified which means that IsModified()
  398. would return @false immediately after the call to SetValue().
  399. The insertion point is set to the start of the control (i.e. position
  400. 0) by this function.
  401. Note that, unlike most other functions changing the controls values,
  402. this function generates a @c wxEVT_TEXT event. To avoid
  403. this you can use ChangeValue() instead.
  404. @param value
  405. The new value to set. It may contain newline characters if the text
  406. control is multi-line.
  407. */
  408. virtual void SetValue(const wxString& value);
  409. /**
  410. If there is an undo facility and the last operation can be undone,
  411. undoes the last operation.
  412. Does nothing if there is no undo facility.
  413. */
  414. virtual void Undo();
  415. /**
  416. Writes the text into the text control at the current insertion position.
  417. @param text
  418. Text to write to the text control.
  419. @remarks
  420. Newlines in the text string are the only control characters
  421. allowed, and they will cause appropriate line breaks.
  422. See operator<<() and AppendText() for more convenient ways of
  423. writing to the window.
  424. After the write operation, the insertion point will be at the end
  425. of the inserted text, so subsequent write operations will be appended.
  426. To append text after the user may have interacted with the control,
  427. call wxTextCtrl::SetInsertionPointEnd() before writing.
  428. */
  429. virtual void WriteText(const wxString& text);
  430. };