control.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: control.h
  3. // Purpose: interface of wxControl
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. Flags used by wxControl::Ellipsize function.
  9. */
  10. enum wxEllipsizeFlags
  11. {
  12. /// No special flags.
  13. wxELLIPSIZE_FLAGS_NONE = 0,
  14. /**
  15. Take mnemonics into account when calculating the text width.
  16. With this flag when calculating the size of the passed string,
  17. mnemonics characters (see wxControl::SetLabel) will be automatically
  18. reduced to a single character. This leads to correct calculations only
  19. if the string passed to Ellipsize() will be used with
  20. wxControl::SetLabel. If you don't want ampersand to be interpreted as
  21. mnemonics (e.g. because you use wxControl::SetLabelText) then don't use
  22. this flag.
  23. */
  24. wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS = 1,
  25. /**
  26. Expand tabs in spaces when calculating the text width.
  27. This flag tells wxControl::Ellipsize() to calculate the width of tab
  28. characters @c '\\t' as 6 spaces.
  29. */
  30. wxELLIPSIZE_FLAGS_EXPAND_TABS = 2,
  31. /// The default flags for wxControl::Ellipsize.
  32. wxELLIPSIZE_FLAGS_DEFAULT = wxELLIPSIZE_FLAGS_PROCESS_MNEMONICS|
  33. wxELLIPSIZE_FLAGS_EXPAND_TABS
  34. };
  35. /**
  36. The different ellipsization modes supported by the
  37. wxControl::Ellipsize function.
  38. */
  39. enum wxEllipsizeMode
  40. {
  41. /// Don't ellipsize the text at all. @since 2.9.1
  42. wxELLIPSIZE_NONE,
  43. /// Put the ellipsis at the start of the string, if the string needs ellipsization.
  44. wxELLIPSIZE_START,
  45. /// Put the ellipsis in the middle of the string, if the string needs ellipsization.
  46. wxELLIPSIZE_MIDDLE,
  47. /// Put the ellipsis at the end of the string, if the string needs ellipsization.
  48. wxELLIPSIZE_END
  49. };
  50. /**
  51. @class wxControl
  52. This is the base class for a control or "widget".
  53. A control is generally a small window which processes user input and/or
  54. displays one or more item of data.
  55. @beginEventEmissionTable{wxClipboardTextEvent}
  56. @event{EVT_TEXT_COPY(id, func)}
  57. Some or all of the controls content was copied to the clipboard.
  58. @event{EVT_TEXT_CUT(id, func)}
  59. Some or all of the controls content was cut (i.e. copied and
  60. deleted).
  61. @event{EVT_TEXT_PASTE(id, func)}
  62. Clipboard content was pasted into the control.
  63. @endEventTable
  64. @library{wxcore}
  65. @category{ctrl}
  66. @see wxValidator
  67. */
  68. class wxControl : public wxWindow
  69. {
  70. public:
  71. /**
  72. Constructs a control.
  73. @param parent
  74. Pointer to a parent window.
  75. @param id
  76. Control identifier. If wxID_ANY, will automatically create an identifier.
  77. @param pos
  78. Control position. wxDefaultPosition indicates that wxWidgets
  79. should generate a default position for the control.
  80. @param size
  81. Control size. wxDefaultSize indicates that wxWidgets should generate
  82. a default size for the window. If no suitable size can be found, the
  83. window will be sized to 20x20 pixels so that the window is visible but
  84. obviously not correctly sized.
  85. @param style
  86. Control style. For generic window styles, please see wxWindow.
  87. @param validator
  88. Control validator.
  89. @param name
  90. Control name.
  91. */
  92. wxControl(wxWindow *parent, wxWindowID id,
  93. const wxPoint& pos = wxDefaultPosition,
  94. const wxSize& size = wxDefaultSize, long style = 0,
  95. const wxValidator& validator = wxDefaultValidator,
  96. const wxString& name = wxControlNameStr);
  97. /**
  98. Default constructor to allow 2-phase creation.
  99. */
  100. wxControl();
  101. bool Create(wxWindow *parent, wxWindowID id,
  102. const wxPoint& pos = wxDefaultPosition,
  103. const wxSize& size = wxDefaultSize, long style = 0,
  104. const wxValidator& validator = wxDefaultValidator,
  105. const wxString& name = wxControlNameStr);
  106. /**
  107. Simulates the effect of the user issuing a command to the item.
  108. @see wxCommandEvent
  109. */
  110. virtual void Command(wxCommandEvent& event);
  111. /**
  112. Returns the control's label, as it was passed to SetLabel().
  113. Note that the returned string may contains mnemonics ("&" characters) if they were
  114. passed to the SetLabel() function; use GetLabelText() if they are undesired.
  115. Also note that the returned string is always the string which was passed to
  116. SetLabel() but may be different from the string passed to SetLabelText()
  117. (since this last one escapes mnemonic characters).
  118. */
  119. wxString GetLabel() const;
  120. /**
  121. Returns the control's label without mnemonics.
  122. Note that because of the stripping of the mnemonics the returned string may differ
  123. from the string which was passed to SetLabel() but should always be the same which
  124. was passed to SetLabelText().
  125. */
  126. wxString GetLabelText() const;
  127. /**
  128. Determine the size needed by the control to leave the given area for
  129. its text.
  130. This function is mostly useful with control displaying short amounts of
  131. text that can be edited by the user, e.g. wxTextCtrl, wxComboBox,
  132. wxSearchCtrl etc. Typically it is used to size these controls for the
  133. maximal amount of input they are supposed to contain, for example:
  134. @code
  135. // Create a control for post code entry.
  136. wxTextCtrl* postcode = new wxTextCtrl(this, ...);
  137. // And set its initial and minimal size to be big enough for
  138. // entering 5 digits.
  139. postcode->SetInitialSize(
  140. postcode->GetSizeFromTextSize(
  141. postcode->GetTextExtent("99999")));
  142. @endcode
  143. Currently this method is only implemented for wxTextCtrl, wxComboBox
  144. and wxChoice in wxMSW and wxGTK.
  145. @param xlen The horizontal extent of the area to leave for text, in
  146. pixels.
  147. @param ylen The vertical extent of the area to leave for text, in
  148. pixels. By default -1 meaning that the vertical component of the
  149. returned size should be the default height of this control.
  150. @return The size that the control should have to leave the area of the
  151. specified size for its text. May return wxDefaultSize if this
  152. method is not implemented for this particular control under the
  153. current platform.
  154. @since 2.9.5
  155. */
  156. wxSize GetSizeFromTextSize(int xlen, int ylen = -1) const;
  157. /**
  158. @overload
  159. */
  160. wxSize GetSizeFromTextSize(const wxSize& tsize) const;
  161. /**
  162. Sets the control's label.
  163. All "&" characters in the @a label are special and indicate that the
  164. following character is a @e mnemonic for this control and can be used to
  165. activate it from the keyboard (typically by using @e Alt key in
  166. combination with it). To insert a literal ampersand character, you need
  167. to double it, i.e. use "&&". If this behaviour is undesirable, use
  168. SetLabelText() instead.
  169. */
  170. void SetLabel(const wxString& label);
  171. /**
  172. Sets the control's label to exactly the given string.
  173. Unlike SetLabel(), this function shows exactly the @a text passed to it
  174. in the control, without interpreting ampersands in it in any way.
  175. Notice that it means that the control can't have any mnemonic defined
  176. for it using this function.
  177. @see EscapeMnemonics()
  178. */
  179. void SetLabelText(const wxString& text);
  180. // NB: when writing docs for the following function remember that Doxygen
  181. // will always expand HTML entities (e.g. ") and thus we need to
  182. // write e.g. "<" to have in the output the "<" string.
  183. /**
  184. Sets the controls label to a string using markup.
  185. Simple markup supported by this function can be used to apply different
  186. fonts or colours to different parts of the control label when supported.
  187. If markup is not supported by the control or platform, it is simply
  188. stripped and SetLabel() is used with the resulting string.
  189. For example,
  190. @code
  191. wxStaticText *text;
  192. ...
  193. text->SetLabelMarkup("<b>&amp;Bed</b> &amp;mp; "
  194. "<span foreground='red'>breakfast</span> "
  195. "available <big>HERE</big>");
  196. @endcode
  197. would show the string using bold, red and big for the corresponding
  198. words under wxGTK but will simply show the string "Bed &amp; breakfast
  199. available HERE" on the other platforms. In any case, the "B" of "Bed"
  200. will be underlined to indicate that it can be used as a mnemonic for
  201. this control.
  202. The supported tags are:
  203. <TABLE>
  204. <TR>
  205. <TD><b>Tag</b></TD>
  206. <TD><b>Description</b></TD>
  207. </TR>
  208. <TR>
  209. <TD>&lt;b&gt;</TD>
  210. <TD>bold text</TD>
  211. </TR>
  212. <TR>
  213. <TD>&lt;big&gt;</TD>
  214. <TD>bigger text</TD>
  215. </TR>
  216. <TR>
  217. <TD>&lt;i&gt;</TD>
  218. <TD>italic text</TD>
  219. </TR>
  220. <TR>
  221. <TD>&lt;s&gt;</TD>
  222. <TD>strike-through text</TD>
  223. </TR>
  224. <TR>
  225. <TD>&lt;small&gt;</TD>
  226. <TD>smaller text</TD>
  227. </TR>
  228. <TR>
  229. <TD>&lt;tt&gt;</TD>
  230. <TD>monospaced text</TD>
  231. </TR>
  232. <TR>
  233. <TD>&lt;u&gt;</TD>
  234. <TD>underlined text</TD>
  235. </TR>
  236. <TR>
  237. <TD>&lt;span&gt;</TD>
  238. <TD>generic formatter tag, see the table below for supported
  239. attributes.
  240. </TD>
  241. </TR>
  242. </TABLE>
  243. Supported @c &lt;span&gt; attributes:
  244. <TABLE>
  245. <TR>
  246. <TD><b>Name</b></TD>
  247. <TD><b>Description</b></TD>
  248. </TR>
  249. <TR>
  250. <TD>foreground, fgcolor, color</TD>
  251. <TD>Foreground text colour, can be a name or RGB value.</TD>
  252. </TR>
  253. <TR>
  254. <TD>background, bgcolor</TD>
  255. <TD>Background text colour, can be a name or RGB value.</TD>
  256. </TR>
  257. <TR>
  258. <TD>font_family, face</TD>
  259. <TD>Font face name.</TD>
  260. </TR>
  261. <TR>
  262. <TD>font_weight, weight</TD>
  263. <TD>Numeric value in 0..900 range or one of "ultralight",
  264. "light", "normal" (all meaning non-bold), "bold", "ultrabold"
  265. and "heavy" (all meaning bold).</TD>
  266. </TR>
  267. <TR>
  268. <TD>font_style, style</TD>
  269. <TD>Either "oblique" or "italic" (both with the same meaning)
  270. or "normal".</TD>
  271. </TR>
  272. <TR>
  273. <TD>size</TD>
  274. <TD>The font size can be specified either as "smaller" or
  275. "larger" relatively to the current font, as a CSS font size
  276. name ("xx-small", "x-small", "small", "medium", "large",
  277. "x-large" or "xx-large") or as a number giving font size in
  278. 1024th parts of a point, i.e. 10240 for a 10pt font.</TD>
  279. </TR>
  280. </TABLE>
  281. This markup language is a strict subset of Pango markup (described at
  282. http://library.gnome.org/devel/pango/unstable/PangoMarkupFormat.html)
  283. and any tags and span attributes not documented above can't be used
  284. under non-GTK platforms.
  285. Also note that you need to escape the following special characters:
  286. <TABLE>
  287. <TR>
  288. <TD><b>Special character</b></TD>
  289. <TD><b>Escape as</b></TD>
  290. </TR>
  291. <TR>
  292. <TD>@c &amp;</TD>
  293. <TD>@c &amp;amp; or as @c &amp;&amp;</TD>
  294. </TR>
  295. <TR>
  296. <TD>@c &apos;</TD>
  297. <TD>@c &amp;apos;</TD>
  298. </TR>
  299. <TR>
  300. <TD>@c &quot;</TD>
  301. <TD>@c &amp;quot;</TD>
  302. </TR>
  303. <TR>
  304. <TD>@c &lt;</TD>
  305. <TD>@c &amp;lt;</TD>
  306. </TR>
  307. <TR>
  308. <TD>@c &gt;</TD>
  309. <TD>@c &amp;gt;</TD>
  310. </TR>
  311. </TABLE>
  312. The non-escaped ampersand @c &amp; characters are interpreted as
  313. mnemonics as with wxControl::SetLabel.
  314. @param markup
  315. String containing markup for the label. It may contain markup tags
  316. described above and newline characters but currently only wxGTK and
  317. wxOSX support multiline labels with markup, the generic
  318. implementation (also used in wxMSW) only handles single line markup
  319. labels. Notice that the string must be well-formed (e.g. all tags
  320. must be correctly closed) and won't be shown at all otherwise.
  321. @return
  322. @true if the new label was set (even if markup in it was ignored)
  323. or @false if we failed to parse the markup. In this case the label
  324. remains unchanged.
  325. Currently wxButton supports markup in all major ports (wxMSW, wxGTK and
  326. wxOSX/Cocoa) while wxStaticText supports it in wxGTK and wxOSX and its
  327. generic version (which can be used under MSW if markup support is
  328. required). Extending support to more controls is planned in the future.
  329. @since 2.9.2
  330. */
  331. bool SetLabelMarkup(const wxString& markup);
  332. public: // static functions
  333. /**
  334. Returns the given @a label string without mnemonics ("&" characters).
  335. */
  336. static wxString GetLabelText(const wxString& label);
  337. /**
  338. Returns the given @a str string without mnemonics ("&" characters).
  339. @note This function is identical to GetLabelText() and is provided
  340. mostly for symmetry with EscapeMnemonics().
  341. */
  342. static wxString RemoveMnemonics(const wxString& str);
  343. /**
  344. Escapes the special mnemonics characters ("&") in the given string.
  345. This function can be helpful if you need to set the controls label to a
  346. user-provided string. If the string contains ampersands, they wouldn't
  347. appear on the display but be used instead to indicate that the
  348. character following the first of them can be used as a control mnemonic.
  349. While this can sometimes be desirable (e.g. to allow the user to
  350. configure mnemonics of the controls), more often you will want to use
  351. this function before passing a user-defined string to SetLabel().
  352. Alternatively, if the label is entirely user-defined, you can just call
  353. SetLabelText() directly -- but this function must be used if the label
  354. is a combination of a part defined by program containing the control
  355. mnemonics and a user-defined part.
  356. @param text
  357. The string such as it should appear on the display.
  358. @return
  359. The same string with the ampersands in it doubled.
  360. */
  361. static wxString EscapeMnemonics(const wxString& text);
  362. /**
  363. Replaces parts of the @a label string with ellipsis, if needed, so
  364. that it fits into @a maxWidth pixels if possible.
  365. Note that this function does @em not guarantee that the returned string
  366. will always be shorter than @a maxWidth; if @a maxWidth is extremely
  367. small, ellipsized text may be larger.
  368. @param label
  369. The string to ellipsize
  370. @param dc
  371. The DC used to retrieve the character widths through the
  372. wxDC::GetPartialTextExtents() function.
  373. @param mode
  374. The ellipsization mode. This is the setting which determines
  375. which part of the string should be replaced by the ellipsis.
  376. See ::wxEllipsizeMode enumeration values for more info.
  377. @param maxWidth
  378. The maximum width of the returned string in pixels.
  379. This argument determines how much characters of the string need to
  380. be removed (and replaced by ellipsis).
  381. @param flags
  382. One or more of the ::wxEllipsizeFlags enumeration values combined.
  383. */
  384. static wxString Ellipsize(const wxString& label, const wxDC& dc,
  385. wxEllipsizeMode mode, int maxWidth,
  386. int flags = wxELLIPSIZE_FLAGS_DEFAULT);
  387. };