NXWidgets  1.19
clabel.hxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/include/clabel.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  * Portions of this package derive from Woopsi (http://woopsi.org/) and
37  * portions are original efforts. It is difficult to determine at this
38  * point what parts are original efforts and which parts derive from Woopsi.
39  * However, in any event, the work of Antony Dzeryn will be acknowledged
40  * in all NxWidget files. Thanks Antony!
41  *
42  * Copyright (c) 2007-2011, Antony Dzeryn
43  * All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions are met:
47  *
48  * * Redistributions of source code must retain the above copyright
49  * notice, this list of conditions and the following disclaimer.
50  * * Redistributions in binary form must reproduce the above copyright
51  * notice, this list of conditions and the following disclaimer in the
52  * documentation and/or other materials provided with the distribution.
53  * * Neither the names "Woopsi", "Simian Zombie" nor the
54  * names of its contributors may be used to endorse or promote products
55  * derived from this software without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
58  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
59  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
60  * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
61  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
62  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
63  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
64  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
66  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67  *
68  ****************************************************************************/
69 
70 #ifndef __INCLUDE_CLABEL_HXX
71 #define __INCLUDE_CLABEL_HXX
72 
73 /****************************************************************************
74  * Included Files
75  ****************************************************************************/
76 
77 #include <nuttx/config.h>
78 
79 #include <stdint.h>
80 #include <stdbool.h>
81 
82 #include <nuttx/nx/nxglib.h>
83 
84 #include "cnxwidget.hxx"
85 #include "cwidgetstyle.hxx"
86 #include "cnxstring.hxx"
87 
88 /****************************************************************************
89  * Pre-Processor Definitions
90  ****************************************************************************/
91 
92 /****************************************************************************
93  * Implementation Classes
94  ****************************************************************************/
95 
96 #if defined(__cplusplus)
97 
98 namespace NXWidgets
99 {
100  /**
101  * Forward references
102  */
103 
104  class CWidgetControl;
105  class CRect;
106 
107  /**
108  * Single-line label widget. Can align text both vertically and
109  * horizontally in different ways.
110  */
111 
112  class CLabel : public CNxWidget
113  {
114  public:
115 
116  /**
117  * Enum of horizontal alignment options.
118  */
119 
121  {
122  TEXT_ALIGNMENT_HORIZ_CENTER = 0, /**< Centre the text */
123  TEXT_ALIGNMENT_HORIZ_LEFT = 1, /**< Align left */
124  TEXT_ALIGNMENT_HORIZ_RIGHT = 2 /**< Align right */
125  };
126 
127  /**
128  * Enum of vertical alignment options.
129  */
130 
132  {
133  TEXT_ALIGNMENT_VERT_CENTER = 0, /**< Align to centre of textbox */
134  TEXT_ALIGNMENT_VERT_TOP = 1, /**< Align to top of textbox */
135  TEXT_ALIGNMENT_VERT_BOTTOM = 2 /**< Align to bottom of textbox */
136  };
137 
138  protected:
139  CNxString m_text; /**< Text that the textbox will display */
140  struct nxgl_point_s m_align; /**< X/Y offset for text alignment */
141  TextAlignmentHoriz m_hAlignment; /**< Horizontal alignment of the text */
142  TextAlignmentVert m_vAlignment; /**< Vertical alignment of the text */
143  bool m_textChange; /**< Redraw is due to a text change */
144  bool m_highlighted; /**< Label is highlighted */
145 
146  /**
147  * Draw the area of this widget that falls within the clipping region.
148  * Called by the redraw() function to draw all visible regions.
149  * @param port The CGraphicsPort to draw to.
150  *
151  * @see redraw()
152  */
153 
154  virtual void drawContents(CGraphicsPort *port);
155 
156  /**
157  * Draw the area of this widget that falls within the clipping region.
158  * Called by the redraw() function to draw all visible regions.
159  *
160  * @param port The CGraphicsPort to draw to.
161  * @see redraw()
162  */
163 
164  virtual void drawBorder(CGraphicsPort *port);
165 
166  /**
167  * Resize the widget to the new dimensions.
168  *
169  * @param width The new width.
170  * @param height The new height.
171  */
172 
173  virtual void onResize(nxgl_coord_t width, nxgl_coord_t height);
174 
175  /**
176  * Calculate the vertical position of the string based on the font
177  *
178  * height and the alignment options.
179  */
180 
181  virtual void calculateTextPositionVertical(void);
182 
183  /**
184  * Calculate the position of the string based on its length and the
185  * alignment options.
186  */
187 
188  virtual void calculateTextPositionHorizontal(void);
189 
190  /**
191  * Updates the GUI after the text has changed.
192  */
193 
194  virtual void onTextChange(void);
195 
196  /**
197  * Copy constructor is protected to prevent usage.
198  */
199 
200  inline CLabel(const CLabel &label) : CNxWidget(label) { };
201 
202  public:
203 
204  /**
205  * Constructor for a label containing a string.
206  *
207  * @param pWidgetControl The controlling widget for the display
208  * @param x The x coordinate of the text box, relative to its parent.
209  * @param y The y coordinate of the text box, relative to its parent.
210  * @param width The width of the textbox.
211  * @param height The height of the textbox.
212  * @param text Pointer to a string to display in the textbox.
213  * @param style The style that the button should use. If this is not
214  * specified, the button will use the global default widget
215  * style.
216  */
217 
218  CLabel(CWidgetControl *pWidgetControl, nxgl_coord_t x, nxgl_coord_t y,
219  nxgl_coord_t width, nxgl_coord_t height, const CNxString &text,
220  CWidgetStyle *style = (CWidgetStyle *)NULL);
221 
222  /**
223  * Destructor.
224  */
225 
226  virtual inline ~CLabel() { }
227 
228  /**
229  * Set the horizontal alignment of text within the label.
230  *
231  * @param alignment The horizontal position of the text.
232  */
233 
234  virtual void setTextAlignmentHoriz(TextAlignmentHoriz alignment);
235 
236  /**
237  * Set the vertical alignment of text within the label.
238  *
239  * @param alignment The vertical position of the text.
240  */
241 
242  virtual void setTextAlignmentVert(TextAlignmentVert alignment);
243 
244  /**
245  * Set the horizontal alignment of text within the label.
246  *
247  * @param alignment The horizontal position of the text.
248  */
249 
250  inline const TextAlignmentHoriz getTextAlignmentHoriz(void) const
251  {
252  return m_hAlignment;
253  }
254 
255  /**
256  * Set the vertical alignment of text within the label.
257  *
258  * @param alignment The vertical position of the text.
259  */
260 
261  inline const TextAlignmentVert getTextAlignmentVert(void) const
262  {
263  return m_vAlignment;
264  }
265 
266  /**
267  * Returns the string shown in the label.
268  *
269  * @return The label's text.
270  */
271 
272  virtual inline const CNxString &getText(void) const
273  {
274  return m_text;
275  }
276 
277  /**
278  * Set the text displayed in the label.
279  *
280  * @param text String to display.
281  */
282 
283  virtual void setText(const CNxString &text);
284 
285  /**
286  * Append new text to the end of the current text displayed in the
287  * label.
288  *
289  * @param text String to append.
290  */
291 
292  virtual void appendText(const CNxString &text);
293 
294  /**
295  * Insert text at the specified index.
296  *
297  * @param text The text to insert.
298  * @param index Index at which to insert the text.
299  */
300 
301  virtual void insertText(const CNxString &text, const int index);
302 
303  /**
304  * Control the highlight state.
305  *
306  * @param highlightOn True(1), the label will be highlighted
307  */
308 
309  virtual void highlight(bool highlightOn);
310 
311  /**
312  * Return the current highlight state.
313  *
314  * @return True if the label is highlighted
315  */
316 
317  virtual inline bool isHighlighted(void) const
318  {
319  return m_highlighted;
320  }
321 
322  /**
323  * Insert the dimensions that this widget wants to have into the rect
324  * passed in as a parameter. All coordinates are relative to the
325  * widget's parent.
326  *
327  * @param rect Reference to a rect to populate with data.
328  */
329 
330  virtual void getPreferredDimensions(CRect &rect) const;
331 
332  /**
333  * Sets the font.
334  *
335  * @param font A pointer to the font to use.
336  *
337  * NOTE: This font is not deleted when the widget is destroyed!
338  */
339 
340  virtual void setFont(CNxFont *font);
341 
342  /**
343  * Is the redraw due to a text-only change?
344  *
345  * @return True if the redraw was caused by a text change
346  */
347 
348  virtual inline bool isTextChange(void) const
349  {
350  return m_textChange;
351  }
352  };
353 }
354 
355 #endif // __cplusplus
356 
357 #endif // __INCLUDE_CLABEL_HXX
virtual bool isTextChange(void) const
Definition: clabel.hxx:348
virtual void getPreferredDimensions(CRect &rect) const
Definition: clabel.cxx:226
struct nxgl_point_s m_align
Definition: clabel.hxx:140
CLabel(const CLabel &label)
Definition: clabel.hxx:200
TextAlignmentVert m_vAlignment
Definition: clabel.hxx:142
virtual void highlight(bool highlightOn)
Definition: clabel.cxx:209
virtual void setTextAlignmentHoriz(TextAlignmentHoriz alignment)
Definition: clabel.cxx:145
virtual void appendText(const CNxString &text)
Definition: clabel.cxx:184
virtual void calculateTextPositionHorizontal(void)
Definition: clabel.cxx:421
TextAlignmentHoriz m_hAlignment
Definition: clabel.hxx:141
virtual void onTextChange(void)
Definition: clabel.cxx:448
CNxString m_text
Definition: clabel.hxx:139
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)
Definition: clabel.cxx:381
virtual ~CLabel()
Definition: clabel.hxx:226
virtual void drawContents(CGraphicsPort *port)
Definition: clabel.cxx:278
virtual bool isHighlighted(void) const
Definition: clabel.hxx:317
virtual void setTextAlignmentVert(TextAlignmentVert alignment)
Definition: clabel.cxx:158
const TextAlignmentHoriz getTextAlignmentHoriz(void) const
Definition: clabel.hxx:250
const TextAlignmentVert getTextAlignmentVert(void) const
Definition: clabel.hxx:261
virtual const CNxString & getText(void) const
Definition: clabel.hxx:272
virtual void insertText(const CNxString &text, const int index)
Definition: clabel.cxx:197
virtual void calculateTextPositionVertical(void)
Definition: clabel.cxx:393
virtual void setFont(CNxFont *font)
Definition: clabel.cxx:259
virtual void drawBorder(CGraphicsPort *port)
Definition: clabel.cxx:361
virtual void setText(const CNxString &text)
Definition: clabel.cxx:171