NXWidgets  1.19
cbuttonarray.hxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/include/cbuttonarray.hxx
3  *
4  * Copyright (C) 2012 Gregory Nutt. All rights reserved.
5  * Author: Gregory Nutt <gnutt@nuttx.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
18  * me be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  ****************************************************************************/
35 
36 #ifndef __INCLUDE_CBUTTONARRAY_HXX
37 #define __INCLUDE_CBUTTONARRAY_HXX
38 
39 /****************************************************************************
40  * Included Files
41  ****************************************************************************/
42 
43 #include <nuttx/config.h>
44 
45 #include <stdint.h>
46 #include <stdbool.h>
47 
48 #include <nuttx/nx/nxglib.h>
49 
50 #include "clabel.hxx"
51 #include "cwidgetstyle.hxx"
52 
53 /****************************************************************************
54  * Pre-Processor Definitions
55  ****************************************************************************/
56 
57 /****************************************************************************
58  * Implementation Classes
59  ****************************************************************************/
60 
61 #if defined(__cplusplus)
62 
63 namespace NXWidgets
64 {
65  /**
66  * Forward references
67  */
68 
69  class CWidgetControl;
70  class CNxString;
71 
72  /**
73  * Manages a two-dimensional array of buttons as one widget. A two-
74  * dimensional array of buttons might also be called a "keyboard".
75  */
76 
77  class CButtonArray : public CNxWidget
78  {
79  protected:
80  uint8_t m_buttonColumns; /**< The number of columns in one row */
81  uint8_t m_buttonRows; /**< The number buttons in one column */
82  bool m_redrawButton; /**< True: Redraw button; False: redraw all */
83  bool m_cursorOn; /**< Cursor on; highlighted button displayed */
84  bool m_cursorChange; /**< True: Redraw cursor button only */
85  nxgl_coord_t m_buttonWidth; /**< The width of one button in pixels */
86  nxgl_coord_t m_buttonHeight; /**< The height of one button in rows */
87  nxgl_coord_t m_clickX; /**< The X position of the last clicked button */
88  nxgl_coord_t m_clickY; /**< The Y position of the last clicked button */
89  uint8_t m_cursorColumn; /**< The column index of the highlighted button */
90  uint8_t m_cursorRow; /**< The row index of the highlighted button */
91  CNxString *m_buttonText; /**< Text for each button */
92 
93  /**
94  * Draw the area of this widget that falls within the clipping region.
95  * Called by the redraw() function to draw all visible regions.
96  *
97  * @param port The CGraphicsPort to draw to.
98  * @see redraw()
99  */
100 
101  virtual void drawContents(CGraphicsPort *port);
102 
103  /**
104  * Draw the area of this widget that falls within the clipping region.
105  * Called by the redraw() function to draw all visible regions.
106  *
107  * @param port The CGraphicsPort to draw to.
108  * @see redraw()
109  */
110 
111  virtual void drawBorder(CGraphicsPort *port);
112 
113  /**
114  * Redraw only one button
115  *
116  * @param port The CGraphicsPort to draw to.
117  * @param column The button column index
118  * @param row The button row index
119  * @param useClicked Draw the button using the 'clicked' button style,
120  * regardless of the actual button state.
121  * @see onClick() and onRelease()
122  */
123 
124  virtual void drawButton(CGraphicsPort *port, int column, int row, bool useClicked);
125 
126  /**
127  * Redraws the button.
128  *
129  * @param x The x coordinate of the click.
130  * @param y The y coordinate of the click.
131  */
132 
133  virtual void onClick(nxgl_coord_t x, nxgl_coord_t y);
134 
135  /**
136  * Raises an action.
137  *
138  * @param x The x coordinate of the mouse.
139  * @param y The y coordinate of the mouse.
140  */
141 
142  virtual void onPreRelease(nxgl_coord_t x, nxgl_coord_t y);
143 
144  /**
145  * Redraws the button.
146  *
147  * @param x The x coordinate of the mouse.
148  * @param y The y coordinate of the mouse.
149  */
150 
151  virtual void onRelease(nxgl_coord_t x, nxgl_coord_t y);
152 
153  /**
154  * Redraws the button.
155  *
156  * @param x The x coordinate of the mouse.
157  * @param y The y coordinate of the mouse.
158  */
159 
160  virtual void onReleaseOutside(nxgl_coord_t x, nxgl_coord_t y);
161 
162  /**
163  * Convert an X/Y position to a button column/row index
164  *
165  * @param x The x position
166  * @param y The y position
167  * @param column The location to return the column index of the button
168  * of interest
169  * @param row The location to return the row index of the button of
170  * interest
171  * @return false is the position is invalid
172  */
173 
174  virtual bool posToButton(nxgl_coord_t x, nxgl_coord_t y, int &column, int &row);
175 
176  /**
177  * Updates the GUI after the text has changed.
178  */
179 
180  virtual void onTextChange(void);
181 
182  /**
183  * Copy constructor is protected to prevent usage.
184  */
185 
186  inline CButtonArray(const CButtonArray &button) : CNxWidget(button) { }
187 
188  public:
189 
190  /**
191  * Constructor for an array of buttons.
192  *
193  * @param pWidgetControl The widget control for the display.
194  * @param x The x coordinate of the button array, relative to its parent.
195  * @param y The y coordinate of the button array, relative to its parent.
196  * @param buttonColumns The number of buttons in one row of the button array
197  * @param buttonRows The number of buttons in one column of the button array
198  * @param buttonWidth The width of one button
199  * @param buttonHeight The height of one button
200  * @param style The style that the button should use. If this is not
201  * specified, the button will use the global default widget
202  * style.
203  */
204 
205  CButtonArray(CWidgetControl *pWidgetControl,
206  nxgl_coord_t x, nxgl_coord_t y,
207  uint8_t buttonColumns, uint8_t buttonRows,
208  nxgl_coord_t buttonWidth, nxgl_coord_t buttonHeight,
209  CWidgetStyle *style = (CWidgetStyle *)NULL);
210 
211  /**
212  * CButtonArray Destructor.
213  */
214 
215  ~CButtonArray(void);
216 
217  /**
218  * Returns the string shown in the label.
219  *
220  * @param column The column index of the button of interest
221  * @param row The row index of the button of interest
222  * @return The label's text.
223  */
224 
225  virtual const CNxString &getText(int column, int row) const;
226 
227  /**
228  * Set the text displayed in the label.
229  *
230  * @param column The column index of the button to set
231  * @param row The row index of the button to set
232  * @param text String to display.
233  */
234 
235  virtual void setText(int column, int row, const CNxString &text);
236 
237  /**
238  * Return the position of the last clicked button (0,0 will be returned
239  * the no button has every been clicked). The button at this position
240  * is currently clicked then, in addition, return true.
241  *
242  * @param column The location to return the column index of the button
243  * of interest
244  * @param row The location to return the row index of the button of
245  * interest
246  * @return True if any button in the array is clicked
247  */
248 
249  virtual bool isButtonClicked(int &column, int &row) const;
250 
251  /**
252  * Check if this specific button in the array is clicked
253  *
254  * @param column The column of the button to check.
255  * @param row The row of the button to check.
256  * @return True if this button is clicked
257  */
258 
259  virtual bool isThisButtonClicked(int column, int row) const;
260 
261  /**
262  * Control the cursor state.
263  *
264  * @param cursorOn True(1), the current cursor position will be highlighted
265  */
266 
267  virtual void cursor(bool cursorOn);
268 
269  /**
270  * Return the current cursor position (button indices) and an indication
271  * if the button at the cursor is currently hightlighted.
272  *
273  * @param column The location to return the column index of the button
274  * of interest
275  * @param row The location to return the row index of the button of
276  * interest
277  * @return True if the cursor is enabled and the button is highlighted
278  */
279 
280  virtual bool getCursorPosition(int &column, int &row) const;
281 
282  /**
283  * Set the cursor position (button indices). Note that the cursor
284  * does not have to be enabled to set the position.
285  *
286  * @param column The column index of the button of interest
287  * @param row The row index of the button of interest
288  * @return True if the cursor position is valid
289  */
290 
291  virtual bool setCursorPosition(int column, int row);
292 
293  /**
294  * Check if this specific button in the array is at the cursor position
295  * and highlighted.
296  *
297  * @param column The column of the button to check.
298  * @param row The row of the button to check.
299  * @return True if this button is at the cursor position and highlighted.
300  */
301 
302  virtual bool isCursorPosition(int column, int row) const;
303 
304  /**
305  * Insert the dimensions that this widget wants to have into the rect
306  * passed in as a parameter. All coordinates are relative to the
307  * widget's parent.
308  *
309  * @param rect Reference to a rect to populate with data.
310  */
311 
312  virtual void getPreferredDimensions(CRect &rect) const;
313 
314  /**
315  * Sets the font.
316  *
317  * @param font A pointer to the font to use.
318  */
319 
320  virtual void setFont(CNxFont *font);
321  };
322 }
323 
324 #endif // __cplusplus
325 
326 #endif // __INCLUDE_CBUTTONARRAY_HXX
nxgl_coord_t m_buttonHeight
virtual bool isButtonClicked(int &column, int &row) const
CButtonArray(const CButtonArray &button)
virtual bool isCursorPosition(int column, int row) const
virtual void onPreRelease(nxgl_coord_t x, nxgl_coord_t y)
virtual void onRelease(nxgl_coord_t x, nxgl_coord_t y)
virtual void getPreferredDimensions(CRect &rect) const
nxgl_coord_t m_buttonWidth
virtual bool isThisButtonClicked(int column, int row) const
virtual void onTextChange(void)
virtual void setText(int column, int row, const CNxString &text)
virtual const CNxString & getText(int column, int row) const
virtual void drawContents(CGraphicsPort *port)
virtual void setFont(CNxFont *font)
virtual void cursor(bool cursorOn)
virtual bool setCursorPosition(int column, int row)
virtual bool posToButton(nxgl_coord_t x, nxgl_coord_t y, int &column, int &row)
virtual void drawButton(CGraphicsPort *port, int column, int row, bool useClicked)
virtual void drawBorder(CGraphicsPort *port)
virtual void onReleaseOutside(nxgl_coord_t x, nxgl_coord_t y)
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
virtual bool getCursorPosition(int &column, int &row) const