NXWidgets: cradiobutton.cxx Source File

NXWidgets 1.19

cradiobutton.cxx
Go to the documentation of this file.
1 /****************************************************************************
2 * NxWidgets/libnxwidgets/src/cradiobutton.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 "cwidgetcontrol.hxx"
83 #include "cradiobutton.hxx"
84 #include "cradiobuttongroup.hxx"
85 #include "cgraphicsport.hxx"
86 #include "cbitmap.hxx"
87 #include "glyphs.hxx"
88 #include "singletons.hxx"
89
90 /****************************************************************************
91 * Pre-Processor Definitions
92 ****************************************************************************/
93
94 /****************************************************************************
95 * CLabel Method Implementations
96 ****************************************************************************/
97
98 using namespace NXWidgets;
99
100 /**
101 * Constructor.
102 *
103 * @param pWidgetControl The controlling widget for the display.
104 * @param x The x coordinate of the radio button, relative to its
105 * parent.
106 * @param y The y coordinate of the radio button, relative to its
107 * parent.
108 * @param width The width of the radio button.
109 * @param height The height of the radio button.
110 * @param style The style that the widget should use. If this is not
111 * specified, the widget will use the values stored in the global
112 * defaultCWidgetStyle object. The widget will copy the properties of
113 * the style into its own internal style object.
114 */
115
116 CRadioButton::CRadioButton(CWidgetControl *pWidgetControl,
117 nxgl_coord_t x, nxgl_coord_t y,
118 nxgl_coord_t width, nxgl_coord_t height,
119 CWidgetStyle *style)
120 : CButton(pWidgetControl, x, y, width, height, *NXWidgets::g_nullString, style)
121 {
122 m_state = RADIO_BUTTON_STATE_OFF;
123 m_flags.borderless = true;
124 }
125
126 /**
127 * Set the state of the radio button.
128 *
129 * @param state The new radio button state.
130 */
131
132 void CRadioButton::setState(CRadioButton::RadioButtonState state)
133 {
134 if (m_state != state)
135 {
136 m_state = state;
137
138 if (m_state != RADIO_BUTTON_STATE_OFF)
139 {
140 ((CRadioButtonGroup*)m_parent)->setSelectedWidget(this);
141 }
142
143 m_widgetEventHandlers->raiseValueChangeEvent();
144 redraw();
145 }
146 }
147
148 /**
149 * Draw the area of this widget that falls within the clipping region.
150 * Called by the redraw() function to draw all visible regions.
151 *
152 * @param port The CGraphicsPort to draw to.
153 * @see redraw()
154 */
155
156 void CRadioButton::drawContents(CGraphicsPort *port)
157 {
158 // Get the X/Y position of the drawable region within the Label
159
160 nxgl_coord_t x = getX();
161 nxgl_coord_t y = getY();
162
163 nxgl_coord_t width = getWidth();
164 nxgl_coord_t height = getHeight();
165
166 // Include and offset for the border
167
168 if (!m_flags.borderless)
169 {
170 x += m_borderSize.left;
171 y += m_borderSize.top;
172
173 width -= (m_borderSize.left + m_borderSize.right);
174 height -= (m_borderSize.top + m_borderSize.bottom);
175 }
176
177 // Decide which glyph to draw
178
179 const struct SBitmap *glyph;
180
181 switch (m_state)
182 {
183 default:
184 case RADIO_BUTTON_STATE_ON:
185 glyph = &g_radioButtonOn;
186 break;
187
188 case RADIO_BUTTON_STATE_OFF:
189 glyph = &g_radioButtonOff;
190 break;
191
192 case RADIO_BUTTON_STATE_MU:
193 glyph = &g_radioButtonMu;
194 break;
195 }
196
197 // Don't exceed the size of the glyph
198
199 if (width > glyph->width)
200 {
201 width = glyph->width;
202 }
203
204 if (height > glyph->height)
205 {
206 height = glyph->height;
207 }
208
209 // Draw button
210
211 port->drawBitmap(x, y, width, height, glyph, 0, 0,
212 CONFIG_NXWIDGETS_TRANSPARENT_COLOR);
213 }
214
215 /**
216 * Sets the radiobutton's state to "on".
217 *
218 * @param x The x coordinate of the click.
219 * @param y The y coordinate of the click.
220 */
221
222 void CRadioButton::onClick(nxgl_coord_t x, nxgl_coord_t y)
223 {
224 setState(RADIO_BUTTON_STATE_ON);
225 }
NXWidgets::CRadioButton::m_state
RadioButtonState m_state
Definition: cradiobutton.hxx:128
NXWidgets::CNxWidget::m_borderSize
WidgetBorderSize m_borderSize
Definition: cnxwidget.hxx:208
NXWidgets::CWidgetStyle
Definition: cwidgetstyle.hxx:125
NXWidgets::CRadioButton::RADIO_BUTTON_STATE_ON
Definition: cradiobutton.hxx:122
NXWidgets::CWidgetControl
Definition: cwidgetcontrol.hxx:95
singletons.hxx
cradiobutton.hxx
glyphs.hxx
cgraphicsport.hxx
NXWidgets::CGraphicsPort::drawBitmap
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)
Definition: cgraphicsport.cxx:348
NXWidgets::CRadioButton::drawContents
virtual void drawContents(CGraphicsPort *port)
Definition: cradiobutton.cxx:156
cwidgetcontrol.hxx
NXWidgets::CNxWidget::redraw
void redraw(void)
Definition: cnxwidget.cxx:483
NXWidgets
Definition: cbgwindow.hxx:65
NXWidgets::CNxWidget::getHeight
nxgl_coord_t getHeight(void) const
Definition: cnxwidget.hxx:593
NXWidgets::g_radioButtonOff
const struct SBitmap g_radioButtonOff
Definition: glyphs.hxx:111
NXWidgets::CNxWidget::WidgetBorderSize::left
uint8_t left
Definition: cnxwidget.hxx:167
NXWidgets::CRadioButton::RADIO_BUTTON_STATE_OFF
Definition: cradiobutton.hxx:121
NXWidgets::CRadioButton::CRadioButton
CRadioButton(const CRadioButton &radioButton)
Definition: cradiobutton.hxx:159
NXWidgets::CNxWidget::WidgetBorderSize::right
uint8_t right
Definition: cnxwidget.hxx:165
NXWidgets::CRadioButton::RADIO_BUTTON_STATE_MU
Definition: cradiobutton.hxx:123
NXWidgets::CNxWidget::WidgetBorderSize::bottom
uint8_t bottom
Definition: cnxwidget.hxx:166
NXWidgets::SBitmap::height
nxgl_coord_t height
Definition: cbitmap.hxx:108
NXWidgets::g_radioButtonMu
const struct SBitmap g_radioButtonMu
Definition: glyphs.hxx:112
NXWidgets::CNxWidget::WidgetBorderSize::top
uint8_t top
Definition: cnxwidget.hxx:164
NXWidgets::CNxWidget::m_widgetEventHandlers
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
NXWidgets::SBitmap::width
nxgl_coord_t width
Definition: cbitmap.hxx:107
NXWidgets::g_radioButtonOn
const struct SBitmap g_radioButtonOn
Definition: glyphs.hxx:110
NXWidgets::CRadioButton::RadioButtonState
RadioButtonState
Definition: cradiobutton.hxx:119
NXWidgets::CNxWidget::getY
nxgl_coord_t getY(void) const
Definition: cnxwidget.cxx:261
cbitmap.hxx
NXWidgets::CNxWidget::getWidth
nxgl_coord_t getWidth(void) const
Definition: cnxwidget.hxx:582
NXWidgets::CNxWidget::getX
nxgl_coord_t getX(void) const
Definition: cnxwidget.cxx:245
NXWidgets::SBitmap
Definition: cbitmap.hxx:103
NXWidgets::CRadioButton::setState
virtual void setState(RadioButtonState state)
Definition: cradiobutton.cxx:132
NXWidgets::CNxWidget::Flags::borderless
uint8_t borderless
Definition: cnxwidget.hxx:148
NXWidgets::CRadioButton::onClick
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
Definition: cradiobutton.cxx:222
NXWidgets::CButton
Definition: cbutton.hxx:109
cradiobuttongroup.hxx
NXWidgets::CWidgetEventHandlerList::raiseValueChangeEvent
void raiseValueChangeEvent(void)
Definition: cwidgeteventhandlerlist.cxx:443
NXWidgets::g_nullString
CNxString * g_nullString
Definition: singletons.hxx:108
NXWidgets::CNxWidget::m_parent
CNxWidget * m_parent
Definition: cnxwidget.hxx:202
NXWidgets::CRadioButtonGroup
Definition: cradiobuttongroup.hxx:112
NXWidgets::CGraphicsPort
Definition: cgraphicsport.hxx:106
NXWidgets::CNxWidget::m_flags
Flags m_flags
Definition: cnxwidget.hxx:187