NXWidgets  1.19
ccheckbox.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/src/ccheckbox.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 <stdint.h>
77 #include <stdbool.h>
78 
79 #include "nxconfig.hxx"
80 #include "cwidgetcontrol.hxx"
81 #include "ccheckbox.hxx"
82 #include "cgraphicsport.hxx"
83 #include "cbitmap.hxx"
84 #include "singletons.hxx"
85 #include "glyphs.hxx"
86 
87 /****************************************************************************
88  * Pre-Processor Definitions
89  ****************************************************************************/
90 
91 /****************************************************************************
92  * Method Implementations
93  ****************************************************************************/
94 
95 using namespace NXWidgets;
96 
97 /**
98  * Constructor.
99  *
100  * @param pWidgetControl The widget control for the display.
101  * @param x The x coordinate of the checkbox, relative to its parent.
102  * @param y The y coordinate of the checkbox, relative to its parent.
103  * @param width The width of the checkbox.
104  * @param height The height of the checkbox.
105  * @param style The style that the widget should use. If this is not
106  * specified, the widget will use the values stored in the global
107  * g_defaultWidgetStyle object. The widget will copy the properties of
108  * the style into its own internal style object.
109  */
110 
112  nxgl_coord_t x, nxgl_coord_t y,
113  nxgl_coord_t width, nxgl_coord_t height,
114  CWidgetStyle *style)
115 : CButton(pWidgetControl, x, y, width, height, *NXWidgets::g_nullString, style)
116 {
118  m_flags.borderless = false;
119 
120  // Border width is one line
121 
122  m_borderSize.top = 1;
123  m_borderSize.right = 1;
124  m_borderSize.bottom = 1;
125  m_borderSize.left = 1;
126 }
127 
128 /**
129  * Set the state of the checkbox.
130  *
131  * @param state The new checkbox state.
132  */
133 
135 {
136  if (m_state != state)
137  {
138  m_state = state;
140  redraw();
141  }
142 }
143 
144 /**
145  * Draw the area of this widget that falls within the clipping region.
146  * Called by the redraw() function to draw all visible regions.
147  *
148  * @param port The CGraphicsPort to draw to.
149  * @see redraw()
150  */
151 
153 {
154  // Get the X/Y position of the drawable region within the CheckBox
155 
156  nxgl_coord_t x = getX();
157  nxgl_coord_t y = getY();
158 
159  nxgl_coord_t width = getWidth();
160  nxgl_coord_t height = getHeight();
161 
162  // Include and offset for the border
163 
164  if (!m_flags.borderless)
165  {
166  x += m_borderSize.left;
167  y += m_borderSize.top;
168 
169  width -= (m_borderSize.left + m_borderSize.right);
170  height -= (m_borderSize.top + m_borderSize.bottom);
171  }
172 
173  // Decide which glyph to draw
174 
175  const struct SBitmap *glyph;
176 
177  switch (m_state)
178  {
179  default:
180  case CHECK_BOX_STATE_ON:
181  glyph = &g_checkBoxOn;
182  break;
183 
184  case CHECK_BOX_STATE_OFF:
185  glyph = &g_checkBoxOff;
186  break;
187 
188  case CHECK_BOX_STATE_MU:
189  glyph = &g_checkBoxMu;
190  break;
191  }
192 
193  // Don't exceed the size of the glyph
194 
195  if (width > glyph->width)
196  {
197  width = glyph->width;
198  }
199 
200  if (height > glyph->height)
201  {
202  height = glyph->height;
203  }
204 
205  // Draw the checkbox
206 
207  port->drawBitmap(x, y, width, height, glyph, 0, 0,
208  CONFIG_NXWIDGETS_TRANSPARENT_COLOR);
209 }
210 
211 /**
212  * Draw the area of this widget that falls within the clipping region.
213  * Called by the redraw() function to draw all visible regions.
214  *
215  * @param port The CGraphicsPort to draw to.
216  * @see redraw()
217  */
218 
220 {
221  // Determine the background color
222 
223  nxgl_mxpixel_t backColor;
224 
225  if (m_highlighted)
226  {
227  backColor = getSelectedBackgroundColor();
228  }
229  else
230  {
231  backColor = getBackgroundColor();
232  }
233 
234  // Draw the background (excluding the border)
235 
236  port->drawFilledRect(getX(), getY(), getWidth(), getHeight(),
237  backColor);
238 
239  // Stop drawing if the widget indicates it should not have an outline
240 
241  if (!isBorderless())
242  {
243  port->drawBevelledRect(getX(), getY(), getWidth(), getHeight(),
245  }
246 }
247 
248 /**
249  * Toggles the state of the checkbox.
250  *
251  * @param x The x coordinate of the click.
252  * @param y The y coordinate of the click.
253  */
254 
255 void CCheckBox::onClick(nxgl_coord_t x, nxgl_coord_t y)
256 {
258  {
260  }
261  else
262  {
264  }
265 }
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
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)
void drawBitmap(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, const struct SBitmap *bitmap, int bitmapX, int bitmapY)
virtual void drawContents(CGraphicsPort *port)
Definition: ccheckbox.cxx:152
nxgl_mxpixel_t getBackgroundColor(void) const
Definition: cnxwidget.hxx:716
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
Definition: ccheckbox.cxx:255
CCheckBox(const CCheckBox &checkBox)
Definition: ccheckbox.hxx:158
virtual void setState(CheckBoxState state)
Definition: ccheckbox.cxx:134
nxgl_coord_t getHeight(void) const
Definition: cnxwidget.hxx:593
const struct SBitmap g_checkBoxOff
Definition: glyphs.hxx:113
nxgl_mxpixel_t getSelectedBackgroundColor(void) const
Definition: cnxwidget.hxx:727
const struct SBitmap g_checkBoxMu
Definition: glyphs.hxx:115
void drawFilledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t color)
nxgl_coord_t height
Definition: cbitmap.hxx:108
nxgl_mxpixel_t getShineEdgeColor(void) const
Definition: cnxwidget.hxx:738
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
nxgl_coord_t width
Definition: cbitmap.hxx:107
nxgl_coord_t getY(void) const
Definition: cnxwidget.cxx:261
nxgl_coord_t getWidth(void) const
Definition: cnxwidget.hxx:582
nxgl_coord_t getX(void) const
Definition: cnxwidget.cxx:245
virtual void drawBorder(CGraphicsPort *port)
Definition: ccheckbox.cxx:219
const struct SBitmap g_checkBoxOn
Definition: glyphs.hxx:114
CheckBoxState m_state
Definition: ccheckbox.hxx:123
CNxString * g_nullString
Definition: singletons.hxx:108