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 
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 
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 
174 {
175  for (int i = 0; i < m_children.size(); i++)
176  {
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 
195 {
196  if (m_selectedWidget != widget)
197  {
198  if (m_selectedWidget != NULL)
199  {
201  }
202 
203  m_selectedWidget = widget;
204 
205  if (m_selectedWidget != NULL)
206  {
208  }
209 
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 
221 {
222  if (index < m_children.size())
223  {
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 
239 {
240  nxgl_coord_t width = 0;
241  nxgl_coord_t height = 0;
242 
243  if (!m_flags.borderless)
244  {
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 
286 {
288 }
289 
290 /**
291  * Handle a mouse double-click event.
292  *
293  * @param e The event data.
294  */
295 
297 {
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 
308 {
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 
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  {
327  }
328  else
329  {
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 
342 {
344 }
CRadioButtonGroup(const CRadioButtonGroup &radioButtonGroup)
WidgetBorderSize m_borderSize
Definition: cnxwidget.hxx:208
virtual void setSelectedIndex(int index)
virtual const int getSelectedIndex(void) const
CRadioButton * newRadioButton(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height)
nxgl_coord_t getX(void) const
Definition: crect.hxx:160
nxgl_mxpixel_t getBackgroundColor(void) const
Definition: cnxwidget.hxx:716
void raiseDoubleClickEvent(nxgl_coord_t x, nxgl_coord_t y)
virtual void getPreferredDimensions(CRect &rect) const
virtual void handleDoubleClickEvent(const CWidgetEventArgs &e)
bool resize(nxgl_coord_t width, nxgl_coord_t height)
Definition: cnxwidget.cxx:1079
CWidgetControl * m_widgetControl
Definition: cnxwidget.hxx:171
const nxgl_coord_t getX(void) const
nxgl_coord_t getHeight(void) const
Definition: cnxwidget.hxx:593
void setHeight(nxgl_coord_t height)
Definition: crect.hxx:261
void addWidgetEventHandler(CWidgetEventHandler *eventHandler)
Definition: cnxwidget.hxx:854
virtual void setSelectedWidget(CRadioButton *widget)
void setX(nxgl_coord_t x)
Definition: crect.hxx:229
virtual void drawContents(CGraphicsPort *port)
void drawFilledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t color)
virtual const CRadioButton * getSelectedWidget(void) const
virtual bool checkCollision(nxgl_coord_t x, nxgl_coord_t y) const
Definition: cnxwidget.cxx:1229
void raiseReleaseOutsideEvent(nxgl_coord_t x, nxgl_coord_t y)
const nxgl_coord_t getY(void) const
void raiseClickEvent(nxgl_coord_t x, nxgl_coord_t y)
virtual void handleReleaseOutsideEvent(const CWidgetEventArgs &e)
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
virtual void handleReleaseEvent(const CWidgetEventArgs &e)
void addWidget(CNxWidget *widget)
Definition: cnxwidget.cxx:1293
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
virtual void setState(RadioButtonState state)
void setY(nxgl_coord_t y)
Definition: crect.hxx:240
TNxArray< CNxWidget * > m_children
Definition: cnxwidget.hxx:204
virtual void handleClickEvent(const CWidgetEventArgs &e)
void raiseReleaseEvent(nxgl_coord_t x, nxgl_coord_t y)