NXWidgets  1.19
clabel.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/src/clabel.cxx
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 most 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 /****************************************************************************
71  * Included Files
72  ****************************************************************************/
73 
74 #include <nuttx/config.h>
75 
76 #include <sys/types.h>
77 #include <stdint.h>
78 #include <stdbool.h>
79 
80 #include <nuttx/nx/nxglib.h>
81 
82 #include "nxconfig.hxx"
83 #include "clabel.hxx"
84 #include "cgraphicsport.hxx"
85 
86 /****************************************************************************
87  * Pre-Processor Definitions
88  ****************************************************************************/
89 
90 /****************************************************************************
91  * CLabel Method Implementations
92  ****************************************************************************/
93 
94 using namespace NXWidgets;
95 
96 /**
97  * Constructor for a label containing a string.
98  *
99  * @param pWidgetControl The controlling widget for the display
100  * @param x The x coordinate of the text box, relative to its parent.
101  * @param y The y coordinate of the text box, relative to its parent.
102  * @param width The width of the textbox.
103  * @param height The height of the textbox.
104  * @param text Pointer to a string to display in the textbox.
105  * @param style The style that the button should use. If this is not
106  * specified, the button will use the global default widget
107  * style.
108  */
109 
111  nxgl_coord_t x, nxgl_coord_t y,
112  nxgl_coord_t width, nxgl_coord_t height,
113  const CNxString &text, CWidgetStyle *style)
114  : CNxWidget(pWidgetControl, x, y, width, height, 0, style)
115 {
116  // Default is centered text
117 
120 
121  // The border thickness is 1 pixel
122 
123  m_borderSize.top = 1;
124  m_borderSize.right = 1;
125  m_borderSize.bottom = 1;
126  m_borderSize.left = 1;
127 
128  m_align.x = 0;
129  m_align.y = 0;
130 
131  m_textChange = false;
132  m_highlighted = false;
133  setText(text);
134 
137 }
138 
139 /**
140  * Set the horizontal alignment of text within the label.
141  *
142  * @param alignment The horizontal position of the text.
143  */
144 
146 {
147  m_hAlignment = alignment;
149  redraw();
150 }
151 
152 /**
153  * Set the vertical alignment of text within the label.
154  *
155  * @param alignment The vertical position of the text.
156  */
157 
159 {
160  m_vAlignment = alignment;
162  redraw();
163 }
164 
165 /**
166  * Set the text displayed in the label.
167  *
168  * @param text String to display.
169  */
170 
171 void CLabel::setText(const CNxString &text)
172 {
173  m_text = text;
174  onTextChange();
175 }
176 
177 /**
178  * Append new text to the end of the current text displayed in the
179  * label.
180  *
181  * @param text String to append.
182  */
183 
184 void CLabel::appendText(const CNxString &text)
185 {
186  m_text.append(text);
187  onTextChange();
188 }
189 
190 /**
191  * Insert text at the specified index.
192  *
193  * @param text The text to insert.
194  * @param index Index at which to insert the text.
195  */
196 
197 void CLabel::insertText(const CNxString &text, const int index)
198 {
199  m_text.insert(text, index);
200  onTextChange();
201 }
202 
203 /**
204  * Control the highlight state.
205  *
206  * @param highlightOn True(1), the label will be highlighted
207  */
208 
209 void CLabel::highlight(bool highlightOn)
210 {
211  if (highlightOn != m_highlighted)
212  {
213  m_highlighted = highlightOn;
214  redraw();
215  }
216 }
217 
218 /**
219  * Insert the dimensions that this widget wants to have into the rect
220  * passed in as a parameter. All coordinates are relative to the
221  * widget's parent.
222  *
223  * @param rect Reference to a rect to populate with data.
224  */
225 
227 {
228  nxgl_coord_t width;
229  nxgl_coord_t height;
230 
231  if (!m_flags.borderless)
232  {
234  height = m_borderSize.top + m_borderSize.bottom;
235  }
236  else
237  {
238  width = 0;
239  height = 0;
240  }
241 
242  width += getFont()->getStringWidth(m_text);
243  height += getFont()->getHeight();
244 
245  rect.setX(m_rect.getX());
246  rect.setY(m_rect.getY());
247  rect.setWidth(width);
248  rect.setHeight(height);
249 }
250 
251 /**
252  * Sets the font.
253  *
254  * @param font A pointer to the font to use.
255  *
256  * NOTE: This font is not deleted when the widget is destroyed!
257  */
258 
260 {
261  m_style.font = font;
262 
263  // Need to recalculate the text position as the font may have changed size
264 
267  redraw();
268 }
269 
270 /**
271  * Draw the area of this widget that falls within the clipping region.
272  * Called by the redraw() function to draw all visible regions.
273  * @param port The CGraphicsPort to draw to.
274  *
275  * @see redraw()
276  */
277 
279 {
280  // Get the drawing area (excluding the border)
281 
282  CRect rect;
283  getRect(rect);
284 
285  // Pick the text and background colors
286 
287  nxgl_mxpixel_t textColor;
288  nxgl_mxpixel_t backColor;
289 
290  if (!isEnabled())
291  {
292  textColor = getDisabledTextColor();
293  backColor = getBackgroundColor();
294  }
295  else if (m_highlighted)
296  {
297  textColor = getSelectedTextColor();
298  backColor = getSelectedBackgroundColor();
299  }
300  else
301  {
302  textColor = getEnabledTextColor();
303  backColor = getBackgroundColor();
304  }
305 
306  // Draw the background (excluding the border)
307 
308 #ifndef CONFIG_NXWIDGETS_FLICKERFREE
309  port->drawFilledRect(rect.getX(), rect.getY(),
310  rect.getWidth(), rect.getHeight(), backColor);
311 #endif
312 
313  // Get the X/Y position of the text within the Label
314 
315  struct nxgl_point_s pos;
316  pos.x = rect.getX() + m_align.x;
317  pos.y = rect.getY() + m_align.y;
318 
319 #ifdef CONFIG_NXWIDGETS_FLICKERFREE
320  CNxFont* font = getFont();
321  int height = font->getHeight();
322  int width = font->getStringWidth(m_text);
323 
324  // Draw the background (excluding the border and the text area)
325  // Left
326 
327  port->drawFilledRect(rect.getX(), rect.getY(),
328  pos.x - rect.getX(), rect.getHeight(), backColor);
329 
330  // Right
331 
332  port->drawFilledRect(pos.x + width, rect.getY(),
333  rect.getX2() - (pos.x + width) + 1,
334  rect.getHeight(), backColor);
335 
336  // Top
337 
338  port->drawFilledRect(pos.x, rect.getY(), width, pos.y - rect.getY(),
339  backColor);
340 
341  // Bottom
342 
343  port->drawFilledRect(pos.x, pos.y + height, width,
344  rect.getY2() - (pos.y + height) + 1, backColor);
345 #endif
346 
347  // Add the text using the selected color and background color
348 
349  port->drawText(&pos, &rect, getFont(), m_text, 0, m_text.getLength(),
350  textColor, backColor);
351 }
352 
353 /**
354  * Draw the area of this widget that falls within the clipping region.
355  * Called by the redraw() function to draw all visible regions.
356  *
357  * @param port The CGraphicsPort to draw to.
358  * @see redraw()
359  */
360 
362 {
363  // Check if the widget indicates it should have an outline: That
364  // (1) the outline is enabled and (2) that this is not just a text-only
365  // redraw
366 
367  if (!isBorderless() && !isTextChange())
368  {
369  port->drawBevelledRect(getX(), getY(), getWidth(), getHeight(),
371  }
372 }
373 
374 /**
375  * Resize the widget to the new dimensions.
376  *
377  * @param width The new width.
378  * @param height The new height.
379  */
380 
381 void CLabel::onResize(nxgl_coord_t width, nxgl_coord_t height)
382 {
385 }
386 
387 /**
388  * Calculate the vertical position of the string based on the font
389  *
390  * height and the alignment options.
391  */
392 
394 {
395  CRect rect;
396  getClientRect(rect);
397 
398  nxgl_coord_t height = rect.getHeight();
399 
400  switch (m_vAlignment)
401  {
403  m_align.y = (height - getFont()->getHeight()) >> 1;
404  break;
405 
407  m_align.y = 0;
408  break;
409 
411  m_align.y = height - getFont()->getHeight();
412  break;
413  }
414 }
415 
416 /**
417  * Calculate the position of the string based on its length and the
418  * alignment options.
419  */
420 
422 {
423  CRect rect;
424  getClientRect(rect);
425 
426  nxgl_coord_t width = rect.getWidth();
427 
428  switch (m_hAlignment)
429  {
431  m_align.x = (width - getFont()->getStringWidth(m_text)) >> 1;
432  break;
433 
435  m_align.x = 0;
436  break;
437 
439  m_align.x = width - getFont()->getStringWidth(m_text);
440  break;
441  }
442 }
443 
444 /**
445  * Updates the GUI after the text has changed.
446  */
447 
449 {
452  m_textChange = true;
453  redraw();
454  m_textChange = false;
456 }
457 
458 
WidgetBorderSize m_borderSize
Definition: cnxwidget.hxx:208
bool isBorderless(void) const
Definition: cnxwidget.hxx:549
nxgl_mxpixel_t getShadowEdgeColor(void) const
Definition: cnxwidget.hxx:749
nxgl_coord_t getY2(void) const
Definition: crect.hxx:319
nxgl_coord_t getHeight(void) const
Definition: crect.hxx:204
void getRect(CRect &rect) const
Definition: cnxwidget.cxx:448
virtual bool isTextChange(void) const
Definition: clabel.hxx:348
nxgl_coord_t getWidth(void) const
Definition: crect.hxx:181
void drawBevelledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t shineColor, nxgl_mxpixel_t shadowColor)
nxgl_coord_t getX(void) const
Definition: crect.hxx:160
nxgl_mxpixel_t getBackgroundColor(void) const
Definition: cnxwidget.hxx:716
nxgl_coord_t getX2(void) const
Definition: crect.hxx:308
virtual void getPreferredDimensions(CRect &rect) const
Definition: clabel.cxx:226
void append(const CNxString &text)
Definition: cnxstring.cxx:267
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
bool isEnabled(void) const
Definition: cnxwidget.cxx:381
nxgl_coord_t getStringWidth(const CNxString &text) const
Definition: cnxfont.cxx: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
nxgl_mxpixel_t getEnabledTextColor(void) const
Definition: cnxwidget.hxx:782
TextAlignmentHoriz m_hAlignment
Definition: clabel.hxx:141
nxgl_coord_t getHeight(void) const
Definition: cnxwidget.hxx:593
void setHeight(nxgl_coord_t height)
Definition: crect.hxx:261
void getClientRect(CRect &rect) const
Definition: cnxwidget.cxx:421
virtual void onTextChange(void)
Definition: clabel.cxx:448
void setX(nxgl_coord_t x)
Definition: crect.hxx:229
nxgl_mxpixel_t getSelectedBackgroundColor(void) const
Definition: cnxwidget.hxx:727
CNxString m_text
Definition: clabel.hxx:139
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)
Definition: clabel.cxx:381
void drawFilledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t color)
virtual void drawContents(CGraphicsPort *port)
Definition: clabel.cxx:278
CNxFont * getFont(void) const
Definition: cnxwidget.hxx:705
nxgl_mxpixel_t getShineEdgeColor(void) const
Definition: cnxwidget.hxx:738
const uint8_t getHeight(void) const
Definition: cnxfont.hxx:229
virtual void setTextAlignmentVert(TextAlignmentVert alignment)
Definition: clabel.cxx:158
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
const unsigned int getLength(void) const
Definition: cnxstring.hxx:325
nxgl_coord_t getY(void) const
Definition: cnxwidget.cxx:261
void setWidth(nxgl_coord_t width)
Definition: crect.hxx:250
nxgl_coord_t getWidth(void) const
Definition: cnxwidget.hxx:582
nxgl_coord_t getX(void) const
Definition: cnxwidget.cxx:245
nxgl_coord_t getY(void) const
Definition: crect.hxx:170
CWidgetStyle m_style
Definition: cnxwidget.hxx:183
nxgl_mxpixel_t getDisabledTextColor(void) const
Definition: cnxwidget.hxx:771
virtual void insertText(const CNxString &text, const int index)
Definition: clabel.cxx:197
virtual void calculateTextPositionVertical(void)
Definition: clabel.cxx:393
void insert(const CNxString &text, const int index)
Definition: cnxstring.cxx:295
void setY(nxgl_coord_t y)
Definition: crect.hxx:240
virtual void setFont(CNxFont *font)
Definition: clabel.cxx:259
virtual void drawBorder(CGraphicsPort *port)
Definition: clabel.cxx:361
void drawText(struct nxgl_point_s *pos, CRect *bound, CNxFont *font, const CNxString &string)
virtual void setText(const CNxString &text)
Definition: clabel.cxx:171
nxgl_mxpixel_t getSelectedTextColor(void) const
Definition: cnxwidget.hxx:793