ctextbox.hxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /****************************************************************************
  2. * apps/include/graphics/nxwidgets/ctextbox.hxx
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. *
  22. * Portions of this package derive from Woopsi (http://woopsi.org/) and
  23. * portions are original efforts. It is difficult to determine at this
  24. * point what parts are original efforts and which parts derive from Woopsi.
  25. * However, in any event, the work of Antony Dzeryn will be acknowledged
  26. * in most NxWidget files. Thanks Antony!
  27. *
  28. * Copyright (c) 2007-2011, Antony Dzeryn
  29. * All rights reserved.
  30. *
  31. * Redistribution and use in source and binary forms, with or without
  32. * modification, are permitted provided that the following conditions are met:
  33. *
  34. * * Redistributions of source code must retain the above copyright
  35. * notice, this list of conditions and the following disclaimer.
  36. * * Redistributions in binary form must reproduce the above copyright
  37. * notice, this list of conditions and the following disclaimer in the
  38. * documentation and/or other materials provided with the distribution.
  39. * * Neither the names "Woopsi", "Simian Zombie" nor the
  40. * names of its contributors may be used to endorse or promote products
  41. * derived from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
  44. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  45. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  46. * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
  47. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  48. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  49. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  50. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  51. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  52. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53. *
  54. ****************************************************************************/
  55. #ifndef __APPS_INCLUDE_GRAPHICS_NXWIDGETS_CTEXTBOX_HXX
  56. #define __APPS_INCLUDE_GRAPHICS_NXWIDGETS_CTEXTBOX_HXX
  57. /****************************************************************************
  58. * Included Files
  59. ****************************************************************************/
  60. #include <nuttx/config.h>
  61. #include <stdint.h>
  62. #include <stdbool.h>
  63. #include <nuttx/nx/nxglib.h>
  64. #include "graphics/nxwidgets/clabel.hxx"
  65. #include "graphics/nxwidgets/cnxstring.hxx"
  66. #include "graphics/nxwidgets/cwidgetstyle.hxx"
  67. #include "graphics/nxwidgets/cwidgeteventargs.hxx"
  68. #include "graphics/nxwidgets/itextbox.hxx"
  69. /****************************************************************************
  70. * Pre-Processor Definitions
  71. ****************************************************************************/
  72. /****************************************************************************
  73. * Implementation Classes
  74. ****************************************************************************/
  75. #if defined(__cplusplus)
  76. namespace NXWidgets
  77. {
  78. /**
  79. * Forward references.
  80. */
  81. class CWidgetControl;
  82. class CNxTimer;
  83. class CNxString;
  84. /**
  85. * Single-line textbox widget. Can align text both vertically and
  86. * horizontally in different ways. The widget gains this functionality by
  87. * inheriting from the CLabel class. However, if the amount of text exceeds
  88. * the dimensions of the widget, the widget will ignore its horizontal
  89. * alignment settings and switch to left-aligned instead. This ensures that
  90. * moving the cursor over the text will scroll through it correctly.
  91. */
  92. class CTextBox : public ITextBox, public CLabel, public CWidgetEventHandler
  93. {
  94. protected:
  95. int m_cursorPos; /**< Position of the cursor within the string. */
  96. uint8_t m_showCursor; /**< Controls cursor visibility. */
  97. bool m_wrapCursor; /**< True wrap cursor at the ends of the text */
  98. /**
  99. * Redraws the widget
  100. */
  101. inline void onBlur(void);
  102. /**
  103. * Draw the area of this widget that falls within the clipping region.
  104. * Called by the redraw() function to draw all visible regions.
  105. *
  106. * @param port The CGraphicsPort to draw to.
  107. * @see redraw()
  108. */
  109. virtual void drawContents(CGraphicsPort *port);
  110. /**
  111. * Moves the cursor without redrawing.
  112. *
  113. * @param position New cursor position.
  114. * @return True if the cursor position changed
  115. */
  116. virtual bool repositionCursor(const int position);
  117. /**
  118. * Move the cursor to the specified coordinates. The coordinates
  119. * are expected to be the result of a click, and therefore in
  120. * world-space rather than widget-space.
  121. */
  122. void moveCursorToClickLocation(nxgl_coord_t x, nxgl_coord_t y);
  123. /**
  124. * Draw the area of this widget that falls within the clipping region.
  125. * Called by the redraw() function to draw all visible regions.
  126. *
  127. * @param port The CGraphicsPort to draw to.
  128. * @see redraw()
  129. */
  130. virtual void drawBorder(CGraphicsPort *port);
  131. /**
  132. * Moves the cursor to the clicked coordinates.
  133. *
  134. * @param x The x coordinates of the click.
  135. * @param y The y coordinates of the click.
  136. */
  137. virtual void onClick(nxgl_coord_t x, nxgl_coord_t y);
  138. /**
  139. * Does nothing.
  140. *
  141. * @param x The x coordinates of the click.
  142. * @param y The y coordinates of the click.
  143. */
  144. virtual void onDoubleClick(nxgl_coord_t x, nxgl_coord_t y);
  145. /**
  146. * Return true if the cursor is visible
  147. */
  148. virtual bool isCursorVisible(void) const;
  149. /**
  150. * Get the x coordinate of the cursor in pixels relative
  151. * to the left-hand edge of the client rect.
  152. *
  153. * @return The x coordinate of the cursor in pixels.
  154. */
  155. virtual const nxgl_coord_t getCursorXPos(void) const;
  156. /**
  157. * Get the width of the cursor in pixels.
  158. *
  159. * @return The width of the cursor in pixels.
  160. */
  161. virtual nxgl_coord_t getCursorWidth(void) const;
  162. /**
  163. * Calculate the horizontal position of the string based on its length
  164. * and the alignment options. Alignment options are overridden if the
  165. * width of the string exceeds the width of the textbox.
  166. */
  167. virtual void calculateTextPositionHorizontal(void);
  168. /**
  169. * Copy constructor is protected to prevent usage.
  170. */
  171. inline CTextBox(const CTextBox& textbox) : CLabel(textbox) { };
  172. public:
  173. /**
  174. * Constructor for a textbox containing a string.
  175. *
  176. * @param pWidgetControl The controlling widget for the window
  177. * @param x The x coordinate of the text box, relative to its parent.
  178. * @param y The y coordinate of the text box, relative to its parent.
  179. * @param width The width of the textbox.
  180. * @param height The height of the textbox.
  181. * @param text Pointer to a string to display in the textbox.
  182. * @param style The style that the button should use. If this is not
  183. * specified, the button will use the global default widget
  184. * style.
  185. */
  186. CTextBox(CWidgetControl *pWidgetControl, nxgl_coord_t x, nxgl_coord_t y,
  187. nxgl_coord_t width, nxgl_coord_t height, const CNxString &text,
  188. CWidgetStyle *style = (CWidgetStyle *)NULL);
  189. /**
  190. * Sets the cursor display mode.
  191. *
  192. * @param cursorMode Determines cursor display mode
  193. */
  194. virtual void showCursor(EShowCursor cursorMode);
  195. /**
  196. * Shows the cursor in default mode (only when the TextBox has focus).
  197. */
  198. virtual inline void showCursor(void)
  199. {
  200. showCursor(SHOW_CURSOR_ONFOCUS);
  201. }
  202. /**
  203. * Hides the cursor.
  204. */
  205. virtual inline void hideCursor(void)
  206. {
  207. showCursor(SHOW_CURSOR_NEVER);
  208. }
  209. /**
  210. * Enables/disables cursor wrapping
  211. *
  212. * @param wrap True enables cursor wrapping
  213. */
  214. virtual inline void wrapCursor(bool wrap)
  215. {
  216. m_wrapCursor = wrap;
  217. }
  218. /**
  219. * Set the text displayed in the label.
  220. *
  221. * @param text String to display.
  222. */
  223. virtual void setText(const CNxString &text);
  224. /**
  225. * Append new text to the end of the current text displayed in the
  226. * label.
  227. *
  228. * @param text String to append.
  229. */
  230. virtual void appendText(const CNxString &text);
  231. /**
  232. * Remove all characters from the string from the start index onwards.
  233. *
  234. * @param startIndex Index to remove from.
  235. */
  236. virtual void removeText(const unsigned int startIndex);
  237. /**
  238. * Remove specified number of characters from the string from the
  239. * start index onwards.
  240. *
  241. * @param startIndex Index to remove from.
  242. * @param count Number of characters to remove.
  243. */
  244. virtual void removeText(const unsigned int startIndex, const unsigned int count);
  245. /**
  246. * Insert text at the specified index.
  247. *
  248. * @param text The text to insert.
  249. * @param index Index at which to insert the text.
  250. */
  251. virtual void insertText(const CNxString &text, const unsigned int index);
  252. /**
  253. * Insert text at the current cursor position.
  254. *
  255. * @param text The text to insert.
  256. */
  257. virtual void insertTextAtCursor(const CNxString &text);
  258. /**
  259. * Move the cursor to the text position specified. 0 indicates the
  260. * start of the string. If position is greater than the length of the
  261. * string, the cursor is moved to the end of the string.
  262. *
  263. * @param position The new cursor position.
  264. */
  265. virtual void moveCursorToPosition(const int position);
  266. /**
  267. * Get the cursor position. This is the index within the string that
  268. * the cursor is currently positioned over.
  269. *
  270. * @return position The cursor position.
  271. */
  272. virtual inline const int getCursorPosition(void) const
  273. {
  274. return m_cursorPos;
  275. }
  276. /**
  277. * Handle a keyboard press event. Replaces CWidgetEventHandler method.
  278. *
  279. * @param e The event data.
  280. */
  281. void handleKeyPressEvent(const CWidgetEventArgs &e);
  282. /**
  283. * Handle a cursor control event. Replaces CWidgetEventHandler method.
  284. *
  285. * @param e The event data.
  286. */
  287. void handleCursorControlEvent(const CWidgetEventArgs &e);
  288. };
  289. }
  290. #endif // __cplusplus
  291. #endif // __APPS_INCLUDE_GRAPHICS_NXWIDGETS_CTEXTBOX_HXX