NXWidgets  1.19
cnumericedit.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/include/cnumericedit.cxx
3  *
4  * Copyright (C) 2012-2013 Gregory Nutt. All rights reserved.
5  * Author: Gregory Nutt <gnutt@nuttx.org>
6  * Petteri Aimonen <jpa@kapsi.fi>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
19  * me be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  ****************************************************************************
36  *
37  * Portions of this package derive from Woopsi (http://woopsi.org/) and
38  * portions are original efforts. It is difficult to determine at this
39  * point what parts are original efforts and which parts derive from Woopsi.
40  * However, in any event, the work of Antony Dzeryn will be acknowledged
41  * in all NxWidget files. Thanks Antony!
42  *
43  * Copyright (c) 2007-2011, Antony Dzeryn
44  * All rights reserved.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions are met:
48  *
49  * * Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  * * Redistributions in binary form must reproduce the above copyright
52  * notice, this list of conditions and the following disclaimer in the
53  * documentation and/or other materials provided with the distribution.
54  * * Neither the names "Woopsi", "Simian Zombie" nor the
55  * names of its contributors may be used to endorse or promote products
56  * derived from this software without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
59  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
60  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
61  * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
62  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
63  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
64  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
65  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
67  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68  *
69  ****************************************************************************/
70 
71 /****************************************************************************
72  * Included Files
73  ****************************************************************************/
74 
75 #include <nuttx/config.h>
76 
77 #include <stdint.h>
78 #include <stdbool.h>
79 #include <stdio.h>
80 #include <limits.h>
81 
82 #include <nuttx/nx/nxglib.h>
83 #include <debug.h>
84 
85 #include "cnumericedit.hxx"
86 #include "cbutton.hxx"
87 #include "clabel.hxx"
88 #include "cnxtimer.hxx"
89 
90 /****************************************************************************
91  * Pre-Processor Definitions
92  ****************************************************************************/
93 
94 /****************************************************************************
95  * CNumericEdit Method Implementations
96  ****************************************************************************/
97 
98 using namespace NXWidgets;
99 
100 // Label that passes drag events
101 
102 class CDraggableLabel: public CLabel
103 {
104 public:
105  CDraggableLabel(CWidgetControl *pWidgetControl, nxgl_coord_t x, nxgl_coord_t y,
106  nxgl_coord_t width, nxgl_coord_t height, const CNxString &text,
107  CWidgetStyle *style = (CWidgetStyle *)NULL):
108  CLabel(pWidgetControl, x, y, width, height, text, style)
109  {
110  setDraggable(true);
111  }
112 
113  virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
114  {
115  startDragging(x, y);
116  }
117 };
118 
119 /**
120  * Constructor for a numeric edit control.
121  *
122  * @param pWidgetControl The controlling widget for the display
123  * @param x The x coordinate of the text box, relative to its parent.
124  * @param y The y coordinate of the text box, relative to its parent.
125  * @param width The width of the textbox.
126  * @param height The height of the textbox.
127  * @param style The style that the button should use. If this is not
128  * specified, the button will use the global default widget
129  * style.
130  */
131 
132 CNumericEdit::CNumericEdit(CWidgetControl *pWidgetControl, nxgl_coord_t x, nxgl_coord_t y,
133  nxgl_coord_t width, nxgl_coord_t height,
134  CWidgetStyle *style)
135 : CNxWidget(pWidgetControl, x, y, width, height, 0, style)
136 {
137  m_label = new CDraggableLabel(pWidgetControl, height, 0, width - 2 * height, height, CNxString("0"), style);
140 
141  m_button_minus = new CButton(pWidgetControl, 0, 0, height, height, CNxString("-"));
144 
145  m_button_plus = new CButton(pWidgetControl, width - height, 0, height, height, CNxString("+"));
148 
149  m_timer = new CNxTimer(pWidgetControl, 100, true);
152 
153  m_minimum = INT_MIN;
154  m_maximum = INT_MAX;
155  m_increment = 1;
156  setValue(0);
157 }
158 
160 {
161  // CNxWidget destroys all children
162 
163  m_label = 0;
164  m_button_minus = 0;
165  m_button_plus = 0;
166 }
167 
169 {
170 }
171 
173 {
174  m_label->setFont(font);
175 }
176 
177 void CNumericEdit::onResize(nxgl_coord_t width, nxgl_coord_t height)
178 {
179 }
180 
182 {
183  if (e.getSource() == m_button_plus)
184  {
186  m_timercount = 0;
187  m_timer->start();
188  }
189  else if (e.getSource() == m_button_minus)
190  {
192  m_timercount = 0;
193  m_timer->start();
194  }
195 }
196 
198 {
199  if (e.getSource() == m_button_plus || e.getSource() == m_button_minus)
200  {
201  m_timer->stop();
202  }
203 }
204 
206 {
207  if (e.getSource() == m_button_plus || e.getSource() == m_button_minus)
208  {
209  m_timer->stop();
210  }
211 }
212 
214 {
215  if (e.getSource() == m_timer)
216  {
217  m_timercount++;
218 
219  // Increment the value at increasing speed.
220  // Ignore the first 3 timer ticks so that single clicks
221  // only increment by one.
222 
223  int increment = 0;
224  if (m_timercount > 50)
225  {
226  increment = m_increment * 100;
227  }
228  else if (m_timercount > 20)
229  {
230  increment = m_increment * 10;
231  }
232  else if (m_timercount > 3)
233  {
234  increment = m_increment;
235  }
236 
237  if (m_button_minus->isClicked())
238  {
239  setValue(m_value - increment);
240  }
241  else if (m_button_plus->isClicked())
242  {
243  setValue(m_value + increment);
244  }
245  }
246 }
247 
249 {
250  int x = e.getX() - m_label->getX();
251  int width = m_label->getWidth();
252  int value = m_minimum + (m_maximum - m_minimum) * x / width;
253  setValue(value / m_increment * m_increment);
254 }
255 
256 void CNumericEdit::setValue(int value)
257 {
258  if (value < m_minimum) value = m_minimum;
259  if (value > m_maximum) value = m_maximum;
260 
261  m_value = value;
262 
263  char buf[10];
264  snprintf(buf, sizeof(buf), "%d", m_value);
265  CNxString text(buf);
266 
267  m_label->setText(text);
268 
270 
271  redraw();
272 }
bool isClicked(void) const
Definition: cnxwidget.hxx:560
const T & getSource(void) const
Definition: teventargs.hxx:127
virtual void handleActionEvent(const CWidgetEventArgs &e)
virtual void handleClickEvent(const CWidgetEventArgs &e)
virtual void handleReleaseOutsideEvent(const CWidgetEventArgs &e)
const nxgl_coord_t getX(void) const
void addWidgetEventHandler(CWidgetEventHandler *eventHandler)
Definition: cnxwidget.hxx:854
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
virtual void setFont(CNxFont *font)
virtual void getPreferredDimensions(CRect &rect) const
virtual void handleDragEvent(const CWidgetEventArgs &e)
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
void addWidget(CNxWidget *widget)
Definition: cnxwidget.cxx:1293
virtual void handleReleaseEvent(const CWidgetEventArgs &e)
void setValue(int value)
nxgl_coord_t getWidth(void) const
Definition: cnxwidget.hxx:582
nxgl_coord_t getX(void) const
Definition: cnxwidget.cxx:245
CDraggableLabel(CWidgetControl *pWidgetControl, nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, const CNxString &text, CWidgetStyle *style=(CWidgetStyle *) NULL)
CNumericEdit(const CNumericEdit &num)
void start(void)
Definition: cnxtimer.cxx:154
virtual void setFont(CNxFont *font)
Definition: clabel.cxx:259
virtual void setText(const CNxString &text)
Definition: clabel.cxx:171