cbuttonarray.hxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /****************************************************************************
  2. * apps/include/graphics/nxwidgets/cbuttonarray.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. #ifndef __APPS_INCLUDE_GRAPHICS_NXWIDGETS_CBUTTONARRAY_HXX
  21. #define __APPS_INCLUDE_GRAPHICS_NXWIDGETS_CBUTTONARRAY_HXX
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <nuttx/config.h>
  26. #include <stdint.h>
  27. #include <stdbool.h>
  28. #include <nuttx/nx/nxglib.h>
  29. #include "graphics/nxwidgets/clabel.hxx"
  30. #include "graphics/nxwidgets/cwidgetstyle.hxx"
  31. /****************************************************************************
  32. * Pre-Processor Definitions
  33. ****************************************************************************/
  34. /****************************************************************************
  35. * Implementation Classes
  36. ****************************************************************************/
  37. #if defined(__cplusplus)
  38. namespace NXWidgets
  39. {
  40. /**
  41. * Forward references
  42. */
  43. class CWidgetControl;
  44. class CNxString;
  45. /**
  46. * Manages a two-dimensional array of buttons as one widget. A two-
  47. * dimensional array of buttons might also be called a "keyboard".
  48. */
  49. class CButtonArray : public CNxWidget
  50. {
  51. protected:
  52. uint8_t m_buttonColumns; /**< The number of columns in one row */
  53. uint8_t m_buttonRows; /**< The number buttons in one column */
  54. bool m_redrawButton; /**< True: Redraw button; False: redraw all */
  55. bool m_cursorOn; /**< Cursor on; highlighted button displayed */
  56. bool m_cursorChange; /**< True: Redraw cursor button only */
  57. nxgl_coord_t m_buttonWidth; /**< The width of one button in pixels */
  58. nxgl_coord_t m_buttonHeight; /**< The height of one button in rows */
  59. nxgl_coord_t m_clickX; /**< The X position of the last clicked button */
  60. nxgl_coord_t m_clickY; /**< The Y position of the last clicked button */
  61. uint8_t m_cursorColumn; /**< The column index of the highlighted button */
  62. uint8_t m_cursorRow; /**< The row index of the highlighted button */
  63. CNxString *m_buttonText; /**< Text for each button */
  64. /**
  65. * Draw the area of this widget that falls within the clipping region.
  66. * Called by the redraw() function to draw all visible regions.
  67. *
  68. * @param port The CGraphicsPort to draw to.
  69. * @see redraw()
  70. */
  71. virtual void drawContents(CGraphicsPort *port);
  72. /**
  73. * Draw the area of this widget that falls within the clipping region.
  74. * Called by the redraw() function to draw all visible regions.
  75. *
  76. * @param port The CGraphicsPort to draw to.
  77. * @see redraw()
  78. */
  79. virtual void drawBorder(CGraphicsPort *port);
  80. /**
  81. * Redraw only one button
  82. *
  83. * @param port The CGraphicsPort to draw to.
  84. * @param column The button column index
  85. * @param row The button row index
  86. * @param useClicked Draw the button using the 'clicked' button style,
  87. * regardless of the actual button state.
  88. * @see onClick() and onRelease()
  89. */
  90. virtual void drawButton(CGraphicsPort *port, int column, int row, bool useClicked);
  91. /**
  92. * Redraws the button.
  93. *
  94. * @param x The x coordinate of the click.
  95. * @param y The y coordinate of the click.
  96. */
  97. virtual void onClick(nxgl_coord_t x, nxgl_coord_t y);
  98. /**
  99. * Raises an action.
  100. *
  101. * @param x The x coordinate of the mouse.
  102. * @param y The y coordinate of the mouse.
  103. */
  104. virtual void onPreRelease(nxgl_coord_t x, nxgl_coord_t y);
  105. /**
  106. * Redraws the button.
  107. *
  108. * @param x The x coordinate of the mouse.
  109. * @param y The y coordinate of the mouse.
  110. */
  111. virtual void onRelease(nxgl_coord_t x, nxgl_coord_t y);
  112. /**
  113. * Redraws the button.
  114. *
  115. * @param x The x coordinate of the mouse.
  116. * @param y The y coordinate of the mouse.
  117. */
  118. virtual void onReleaseOutside(nxgl_coord_t x, nxgl_coord_t y);
  119. /**
  120. * Convert an X/Y position to a button column/row index
  121. *
  122. * @param x The x position
  123. * @param y The y position
  124. * @param column The location to return the column index of the button
  125. * of interest
  126. * @param row The location to return the row index of the button of
  127. * interest
  128. * @return false is the position is invalid
  129. */
  130. virtual bool posToButton(nxgl_coord_t x, nxgl_coord_t y, int &column, int &row);
  131. /**
  132. * Updates the GUI after the text has changed.
  133. */
  134. virtual void onTextChange(void);
  135. /**
  136. * Copy constructor is protected to prevent usage.
  137. */
  138. inline CButtonArray(const CButtonArray &button) : CNxWidget(button) { }
  139. public:
  140. /**
  141. * Constructor for an array of buttons.
  142. *
  143. * @param pWidgetControl The widget control for the display.
  144. * @param x The x coordinate of the button array, relative to its parent.
  145. * @param y The y coordinate of the button array, relative to its parent.
  146. * @param buttonColumns The number of buttons in one row of the button array
  147. * @param buttonRows The number of buttons in one column of the button array
  148. * @param buttonWidth The width of one button
  149. * @param buttonHeight The height of one button
  150. * @param style The style that the button should use. If this is not
  151. * specified, the button will use the global default widget
  152. * style.
  153. */
  154. CButtonArray(CWidgetControl *pWidgetControl,
  155. nxgl_coord_t x, nxgl_coord_t y,
  156. uint8_t buttonColumns, uint8_t buttonRows,
  157. nxgl_coord_t buttonWidth, nxgl_coord_t buttonHeight,
  158. CWidgetStyle *style = (CWidgetStyle *)NULL);
  159. /**
  160. * CButtonArray Destructor.
  161. */
  162. ~CButtonArray(void);
  163. /**
  164. * Resize the array of buttons. Button labels will be removed and will
  165. * have to be reapplied in the new geometry.
  166. *
  167. * @param buttonColumns The number of buttons in one row of the button array
  168. * @param buttonRows The number of buttons in one column of the button array
  169. * @param buttonWidth The width of one button
  170. * @param buttonHeight The height of one button
  171. */
  172. bool resizeArray(uint8_t buttonColumns, uint8_t buttonRows,
  173. nxgl_coord_t buttonWidth, nxgl_coord_t buttonHeight);
  174. /**
  175. * Returns the string shown in the label.
  176. *
  177. * @param column The column index of the button of interest
  178. * @param row The row index of the button of interest
  179. * @return The label's text.
  180. */
  181. virtual const CNxString &getText(int column, int row) const;
  182. /**
  183. * Set the text displayed in the label.
  184. *
  185. * @param column The column index of the button to set
  186. * @param row The row index of the button to set
  187. * @param text String to display.
  188. */
  189. virtual void setText(int column, int row, const CNxString &text);
  190. /**
  191. * Return the position of the last clicked button (0,0 will be returned
  192. * the no button has every been clicked). The button at this position
  193. * is currently clicked then, in addition, return true.
  194. *
  195. * @param column The location to return the column index of the button
  196. * of interest
  197. * @param row The location to return the row index of the button of
  198. * interest
  199. * @return True if any button in the array is clicked
  200. */
  201. virtual bool isButtonClicked(int &column, int &row) const;
  202. /**
  203. * Check if this specific button in the array is clicked
  204. *
  205. * @param column The column of the button to check.
  206. * @param row The row of the button to check.
  207. * @return True if this button is clicked
  208. */
  209. virtual bool isThisButtonClicked(int column, int row) const;
  210. /**
  211. * Control the cursor state.
  212. *
  213. * @param cursorOn True(1), the current cursor position will be highlighted
  214. */
  215. virtual void cursor(bool cursorOn);
  216. /**
  217. * Return the current cursor position (button indices) and an indication
  218. * if the button at the cursor is currently highlighted.
  219. *
  220. * @param column The location to return the column index of the button
  221. * of interest
  222. * @param row The location to return the row index of the button of
  223. * interest
  224. * @return True if the cursor is enabled and the button is highlighted
  225. */
  226. virtual bool getCursorPosition(int &column, int &row) const;
  227. /**
  228. * Set the cursor position (button indices). Note that the cursor
  229. * does not have to be enabled to set the position.
  230. *
  231. * @param column The column index of the button of interest
  232. * @param row The row index of the button of interest
  233. * @return True if the cursor position is valid
  234. */
  235. virtual bool setCursorPosition(int column, int row);
  236. /**
  237. * Check if this specific button in the array is at the cursor position
  238. * and highlighted.
  239. *
  240. * @param column The column of the button to check.
  241. * @param row The row of the button to check.
  242. * @return True if this button is at the cursor position and highlighted.
  243. */
  244. virtual bool isCursorPosition(int column, int row) const;
  245. /**
  246. * Insert the dimensions that this widget wants to have into the rect
  247. * passed in as a parameter. All coordinates are relative to the
  248. * widget's parent.
  249. *
  250. * @param rect Reference to a rect to populate with data.
  251. */
  252. virtual void getPreferredDimensions(CRect &rect) const;
  253. /**
  254. * Sets the font.
  255. *
  256. * @param font A pointer to the font to use.
  257. */
  258. virtual void setFont(CNxFont *font);
  259. };
  260. }
  261. #endif // __cplusplus
  262. #endif // __APPS_INCLUDE_GRAPHICS_NXWIDGETS_CBUTTONARRAY_HXX