NXWidgets: cradiobuttongroup.cxx Source File

NXWidgets 1.19

cradiobuttongroup.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 "cradiobuttongroup.hxx"
83 #include "cradiobutton.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. Note that the group determines its width and height
98 * from the position and dimensions of its children.
99 *
100 * @param pWidgetControl The controlling widget for the display.
101 * @param x The x coordinate of the group.
102 * @param y The y coordinate of the group.
103 * @param style The style that the button should use. If this is not
104 * specified, the button will use the global default widget
105 * style.
106 */
107
108 CRadioButtonGroup::CRadioButtonGroup(CWidgetControl *pWidgetControl,
109 nxgl_coord_t x, nxgl_coord_t y,
110 CWidgetStyle *style)
111 : CNxWidget(pWidgetControl, x, y, 0, 0, WIDGET_BORDERLESS, style)
112 {
113 m_widgetControl = pWidgetControl;
114 m_selectedWidget = (CRadioButton *)NULL;
115 }
116
117 /**
118 * Simple method for adding a new radio button to the group.
119 * This should be used in preference to the usual addWidget() method,
120 * as this method automatically resizes the group.
121 *
122 * @param x The x coordinate of the new button, relative to this
123 * widget.
124 * @param y The y coordinate of the new button, relative to this
125 * widget.
126 * @param width The width of the new button.
127 * @param height The height of the new button.
128 */
129
130 CRadioButton *CRadioButtonGroup::newRadioButton(nxgl_coord_t x, nxgl_coord_t y,
131 nxgl_coord_t width, nxgl_coord_t height)
132 {
133 CRadioButton *newButton = new CRadioButton(m_widgetControl, x, y, width, height, &m_style);
134 newButton->addWidgetEventHandler(this);
135 addWidget(newButton);
136
137 // Do we need to resize?
138
139 nxgl_coord_t newWidth = getWidth();
140 nxgl_coord_t newHeight = getHeight();
141
142 if (newWidth < x + width)
143 {
144 newWidth = x + width;
145 }
146
147 if (newHeight < y + height)
148 {
149 newHeight = y + height;
150 }
151
152 resize(newWidth, newHeight);
153 return newButton;
154 }
155
156 /**
157 * Gets a pointer to the selected widget.
158 *
159 * @return Pointer to the selected widget.
160 */
161
162 const CRadioButton *CRadioButtonGroup::getSelectedWidget(void) const
163 {
164 return (CRadioButton *)m_selectedWidget;
165 }
166
167 /**
168 * Gets the index of the selected widget.
169 *
170 * @return The index of the selected widget.
171 */
172
173 const int CRadioButtonGroup::getSelectedIndex(void) const
174 {
175 for (int i = 0; i < m_children.size(); i++)
176 {
177 if (((CRadioButton*)m_children[i]) == m_selectedWidget)
178 {
179 return i;
180 }
181 }
182
183 // Nothing selected
184
185 return -1;
186 }
187
188 /**
189 * Sets the selected radio button to the supplied widget.
190 *
191 * @param widget The radio button to select.
192 */
193
194 void CRadioButtonGroup::setSelectedWidget(CRadioButton* widget)
195 {
196 if (m_selectedWidget != widget)
197 {
198 if (m_selectedWidget != NULL)
199 {
200 m_selectedWidget->setState(CRadioButton::RADIO_BUTTON_STATE_OFF);
201 }
202
203 m_selectedWidget = widget;
204
205 if (m_selectedWidget != NULL)
206 {
207 m_selectedWidget->setState(CRadioButton::RADIO_BUTTON_STATE_ON);
208 }
209
210 m_widgetEventHandlers->raiseValueChangeEvent();
211 }
212 }
213
214 /**
215 * Selects the widget at the specified index.
216 *
217 * @param index The index of the widget to select.
218 */
219
220 void CRadioButtonGroup::setSelectedIndex(int index)
221 {
222 if (index < m_children.size())
223 {
224 setSelectedWidget((CRadioButton*)m_children[index]);
225 m_widgetEventHandlers->raiseValueChangeEvent();
226 }
227 }
228
229 /**
230 * Insert the dimensions that this widget wants to have into the rect
231 * passed in as a parameter. All coordinates are relative to the
232 * widget's parent. Value is based on the length of the largest string
233 * in the set of options.
234 *
235 * @param rect Reference to a rect to populate with data.
236 */
237
238 void CRadioButtonGroup::getPreferredDimensions(CRect& rect) const
239 {
240 nxgl_coord_t width = 0;
241 nxgl_coord_t height = 0;
242
243 if (!m_flags.borderless)
244 {
245 width = m_borderSize.left + m_borderSize.right;
246 height = m_borderSize.top + m_borderSize.bottom;
247 }
248
249 nxgl_coord_t widgetX = 0;
250 nxgl_coord_t widgetY = 0;
251
252 nxgl_coord_t maxX = 0;
253 nxgl_coord_t maxY = 0;
254
255 // Locate largest x and y coords within children
256
257 for (int i = 0; i < m_children.size(); ++i)
258 {
259 widgetX = m_children[i]->getX() + m_children[i]->getWidth();
260 widgetY = m_children[i]->getY() + m_children[i]->getHeight();
261
262 if (widgetX > maxX)
263 {
264 maxX = widgetX;
265 }
266
267 if (widgetY > maxY)
268 {
269 maxY = widgetY;
270 }
271 }
272
273 rect.setX(m_rect.getX());
274 rect.setY(m_rect.getY());
275 rect.setWidth(width + maxX - getX());
276 rect.setHeight(height + maxY - getY());
277 }
278
279 /**
280 * Handle a mouse click event.
281 *
282 * @param e The event data.
283 */
284
285 void CRadioButtonGroup::handleClickEvent(const CWidgetEventArgs &e)
286 {
287 m_widgetEventHandlers->raiseClickEvent(e.getX(), e.getY());
288 }
289
290 /**
291 * Handle a mouse double-click event.
292 *
293 * @param e The event data.
294 */
295
296 void CRadioButtonGroup::handleDoubleClickEvent(const CWidgetEventArgs &e)
297 {
298 m_widgetEventHandlers->raiseDoubleClickEvent(e.getX(), e.getY());
299 }
300
301 /**
302 * Handle a mouse button release event that occurred within the bounds of
303 * the source widget.
304 * @param e The event data.
305 */
306
307 void CRadioButtonGroup::handleReleaseEvent(const CWidgetEventArgs &e)
308 {
309 m_widgetEventHandlers->raiseReleaseEvent(e.getX(), e.getY());
310 }
311
312 /**
313 * Handle a mouse button release event that occurred outside the bounds of
314 * the source widget.
315 *
316 * @param e The event data.
317 */
318
319 void CRadioButtonGroup::handleReleaseOutsideEvent(const CWidgetEventArgs &e)
320 {
321 // Child raised a release outside event, but we need to raise a different
322 // event if the release occurred within the bounds of this parent widget
323
324 if (checkCollision(e.getX(), e.getY()))
325 {
326 m_widgetEventHandlers->raiseReleaseEvent(e.getX(), e.getY());
327 }
328 else
329 {
330 m_widgetEventHandlers->raiseReleaseOutsideEvent(e.getX(), e.getY());
331 }
332 }
333
334 /**
335 * Draw the area of this widget that falls within the clipping region.
336 * Called by the redraw() function to draw all visible regions.
337 * @param port The CGraphicsPort to draw to.
338 * @see redraw()
339 */
340
341 void CRadioButtonGroup::drawContents(CGraphicsPort *port)
342 {
343 port->drawFilledRect(getX(), getY(), getWidth(), getHeight(), getBackgroundColor());
344 }
NXWidgets::CNxWidget
Definition: cnxwidget.hxx:121
NXWidgets::CRadioButtonGroup::CRadioButtonGroup
CRadioButtonGroup(const CRadioButtonGroup &radioButtonGroup)
Definition: cradiobuttongroup.hxx:131
NXWidgets::CNxWidget::m_borderSize
WidgetBorderSize m_borderSize
Definition: cnxwidget.hxx:208
NXWidgets::CRadioButtonGroup::setSelectedIndex
virtual void setSelectedIndex(int index)
Definition: cradiobuttongroup.cxx:220
NXWidgets::CWidgetStyle
Definition: cwidgetstyle.hxx:125
NXWidgets::CRadioButton::RADIO_BUTTON_STATE_ON
Definition: cradiobutton.hxx:122
NXWidgets::CWidgetControl
Definition: cwidgetcontrol.hxx:95
NXWidgets::CRadioButtonGroup::getSelectedIndex
virtual const int getSelectedIndex(void) const
Definition: cradiobuttongroup.cxx:173
NXWidgets::CRadioButton
Definition: cradiobutton.hxx:111
NXWidgets::CRadioButtonGroup::newRadioButton
CRadioButton * newRadioButton(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height)
Definition: cradiobuttongroup.cxx:130
cradiobutton.hxx
NXWidgets::CRect::getX
nxgl_coord_t getX(void) const
Definition: crect.hxx:160
cgraphicsport.hxx
NXWidgets::CNxWidget::getBackgroundColor
nxgl_mxpixel_t getBackgroundColor(void) const
Definition: cnxwidget.hxx:716
NXWidgets::CWidgetEventHandlerList::raiseDoubleClickEvent
void raiseDoubleClickEvent(nxgl_coord_t x, nxgl_coord_t y)
Definition: cwidgeteventhandlerlist.cxx:186
NXWidgets::CRadioButtonGroup::getPreferredDimensions
virtual void getPreferredDimensions(CRect &rect) const
Definition: cradiobuttongroup.cxx:238
NXWidgets::CRadioButtonGroup::handleDoubleClickEvent
virtual void handleDoubleClickEvent(const CWidgetEventArgs &e)
Definition: cradiobuttongroup.cxx:296
NXWidgets::CNxWidget::resize
bool resize(nxgl_coord_t width, nxgl_coord_t height)
Definition: cnxwidget.cxx:1079
NXWidgets::CNxWidget::m_widgetControl
CWidgetControl * m_widgetControl
Definition: cnxwidget.hxx:171
NXWidgets::CWidgetEventArgs::getX
const nxgl_coord_t getX(void) const
Definition: cwidgeteventargs.hxx:168
NXWidgets
Definition: cbgwindow.hxx:65
NXWidgets::CNxWidget::getHeight
nxgl_coord_t getHeight(void) const
Definition: cnxwidget.hxx:593
NXWidgets::CRect::setHeight
void setHeight(nxgl_coord_t height)
Definition: crect.hxx:261
NXWidgets::CNxWidget::m_rect
CRect m_rect
Definition: cnxwidget.hxx:172
NXWidgets::CNxWidget::WidgetBorderSize::left
uint8_t left
Definition: cnxwidget.hxx:167
NXWidgets::CRadioButton::RADIO_BUTTON_STATE_OFF
Definition: cradiobutton.hxx:121
NXWidgets::CNxWidget::addWidgetEventHandler
void addWidgetEventHandler(CWidgetEventHandler *eventHandler)
Definition: cnxwidget.hxx:854
NXWidgets::CRadioButtonGroup::setSelectedWidget
virtual void setSelectedWidget(CRadioButton *widget)
Definition: cradiobuttongroup.cxx:194
NXWidgets::CRect::setX
void setX(nxgl_coord_t x)
Definition: crect.hxx:229
NXWidgets::CRadioButtonGroup::drawContents
virtual void drawContents(CGraphicsPort *port)
Definition: cradiobuttongroup.cxx:341
NXWidgets::CNxWidget::WidgetBorderSize::right
uint8_t right
Definition: cnxwidget.hxx:165
NXWidgets::CGraphicsPort::drawFilledRect
void drawFilledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t color)
Definition: cgraphicsport.cxx:280
NXWidgets::CRadioButtonGroup::getSelectedWidget
virtual const CRadioButton * getSelectedWidget(void) const
Definition: cradiobuttongroup.cxx:162
NXWidgets::CNxWidget::WidgetBorderSize::bottom
uint8_t bottom
Definition: cnxwidget.hxx:166
NXWidgets::CNxWidget::checkCollision
virtual bool checkCollision(nxgl_coord_t x, nxgl_coord_t y) const
Definition: cnxwidget.cxx:1229
NXWidgets::CWidgetEventHandlerList::raiseReleaseOutsideEvent
void raiseReleaseOutsideEvent(nxgl_coord_t x, nxgl_coord_t y)
Definition: cwidgeteventhandlerlist.cxx:226
NXWidgets::CNxWidget::WidgetBorderSize::top
uint8_t top
Definition: cnxwidget.hxx:164
NXWidgets::CWidgetEventArgs::getY
const nxgl_coord_t getY(void) const
Definition: cwidgeteventargs.hxx:180
NXWidgets::CWidgetEventHandlerList::raiseClickEvent
void raiseClickEvent(nxgl_coord_t x, nxgl_coord_t y)
Definition: cwidgeteventhandlerlist.cxx:166
NXWidgets::CRadioButtonGroup::handleReleaseOutsideEvent
virtual void handleReleaseOutsideEvent(const CWidgetEventArgs &e)
Definition: cradiobuttongroup.cxx:319
NXWidgets::CNxWidget::m_widgetEventHandlers
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
NXWidgets::CRadioButtonGroup::handleReleaseEvent
virtual void handleReleaseEvent(const CWidgetEventArgs &e)
Definition: cradiobuttongroup.cxx:307
NXWidgets::CWidgetEventArgs
Definition: cwidgeteventargs.hxx:127
NXWidgets::CRect
Definition: crect.hxx:101
NXWidgets::CNxWidget::addWidget
void addWidget(CNxWidget *widget)
Definition: cnxwidget.cxx:1293
NXWidgets::CNxWidget::getY
nxgl_coord_t getY(void) const
Definition: cnxwidget.cxx:261
NXWidgets::CRect::setWidth
void setWidth(nxgl_coord_t width)
Definition: crect.hxx:250
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::CRect::getY
nxgl_coord_t getY(void) const
Definition: crect.hxx:170
NXWidgets::CNxWidget::m_style
CWidgetStyle m_style
Definition: cnxwidget.hxx:183
NXWidgets::CRadioButton::setState
virtual void setState(RadioButtonState state)
Definition: cradiobutton.cxx:132
NXWidgets::CNxWidget::Flags::borderless
uint8_t borderless
Definition: cnxwidget.hxx:148
NXWidgets::CRect::setY
void setY(nxgl_coord_t y)
Definition: crect.hxx:240
NXWidgets::CNxWidget::m_children
TNxArray< CNxWidget * > m_children
Definition: cnxwidget.hxx:204
NXWidgets::CRadioButtonGroup::m_selectedWidget
CRadioButton * m_selectedWidget
Definition: cradiobuttongroup.hxx:116
NXWidgets::CRadioButtonGroup::handleClickEvent
virtual void handleClickEvent(const CWidgetEventArgs &e)
Definition: cradiobuttongroup.cxx:285
cradiobuttongroup.hxx
NXWidgets::CWidgetEventHandlerList::raiseValueChangeEvent
void raiseValueChangeEvent(void)
Definition: cwidgeteventhandlerlist.cxx:443
NXWidgets::CGraphicsPort
Definition: cgraphicsport.hxx:106
NXWidgets::CWidgetEventHandlerList::raiseReleaseEvent
void raiseReleaseEvent(nxgl_coord_t x, nxgl_coord_t y)
Definition: cwidgeteventhandlerlist.cxx:206
NXWidgets::CNxWidget::m_flags
Flags m_flags
Definition: cnxwidget.hxx:187