NXWidgets: cnumericedit.cxx Source File

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);
138 m_label->addWidgetEventHandler(this);
139 addWidget(m_label);
140
141 m_button_minus = new CButton(pWidgetControl, 0, 0, height, height, CNxString("-"));
142 m_button_minus->addWidgetEventHandler(this);
143 addWidget(m_button_minus);
144
145 m_button_plus = new CButton(pWidgetControl, width - height, 0, height, height, CNxString("+"));
146 m_button_plus->addWidgetEventHandler(this);
147 addWidget(m_button_plus);
148
149 m_timer = new CNxTimer(pWidgetControl, 100, true);
150 m_timer->addWidgetEventHandler(this);
151 addWidget(m_timer);
152
153 m_minimum = INT_MIN;
154 m_maximum = INT_MAX;
155 m_increment = 1;
156 setValue(0);
157 }
158
159 CNumericEdit::~CNumericEdit()
160 {
161 // CNxWidget destroys all children
162
163 m_label = 0;
164 m_button_minus = 0;
165 m_button_plus = 0;
166 }
167
168 void CNumericEdit::getPreferredDimensions(CRect &rect) const
169 {
170 }
171
172 void CNumericEdit::setFont(CNxFont *font)
173 {
174 m_label->setFont(font);
175 }
176
177 void CNumericEdit::onResize(nxgl_coord_t width, nxgl_coord_t height)
178 {
179 }
180
181 void CNumericEdit::handleClickEvent(const CWidgetEventArgs &e)
182 {
183 if (e.getSource() == m_button_plus)
184 {
185 setValue(m_value + m_increment);
186 m_timercount = 0;
187 m_timer->start();
188 }
189 else if (e.getSource() == m_button_minus)
190 {
191 setValue(m_value - m_increment);
192 m_timercount = 0;
193 m_timer->start();
194 }
195 }
196
197 void CNumericEdit::handleReleaseEvent(const CWidgetEventArgs &e)
198 {
199 if (e.getSource() == m_button_plus || e.getSource() == m_button_minus)
200 {
201 m_timer->stop();
202 }
203 }
204
205 void CNumericEdit::handleReleaseOutsideEvent(const CWidgetEventArgs &e)
206 {
207 if (e.getSource() == m_button_plus || e.getSource() == m_button_minus)
208 {
209 m_timer->stop();
210 }
211 }
212
213 void CNumericEdit::handleActionEvent(const CWidgetEventArgs &e)
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
248 void CNumericEdit::handleDragEvent(const CWidgetEventArgs &e)
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
269 m_widgetEventHandlers->raiseValueChangeEvent();
270
271 redraw();
272 }
NXWidgets::CNxWidget
Definition: cnxwidget.hxx:121
NXWidgets::CNumericEdit::~CNumericEdit
virtual ~CNumericEdit()
Definition: cnumericedit.cxx:159
NXWidgets::CNxWidget::isClicked
bool isClicked(void) const
Definition: cnxwidget.hxx:560
NXWidgets::TEventArgs::getSource
const T & getSource(void) const
Definition: teventargs.hxx:127
NXWidgets::CNumericEdit::handleActionEvent
virtual void handleActionEvent(const CWidgetEventArgs &e)
Definition: cnumericedit.cxx:213
NXWidgets::CWidgetStyle
Definition: cwidgetstyle.hxx:125
clabel.hxx
NXWidgets::CWidgetControl
Definition: cwidgetcontrol.hxx:95
NXWidgets::CNumericEdit::handleClickEvent
virtual void handleClickEvent(const CWidgetEventArgs &e)
Definition: cnumericedit.cxx:181
NXWidgets::CNumericEdit::m_timercount
int m_timercount
Definition: cnumericedit.hxx:126
NXWidgets::CNxWidget::redraw
void redraw(void)
Definition: cnxwidget.cxx:483
NXWidgets::CNumericEdit::handleReleaseOutsideEvent
virtual void handleReleaseOutsideEvent(const CWidgetEventArgs &e)
Definition: cnumericedit.cxx:205
NXWidgets::CWidgetEventArgs::getX
const nxgl_coord_t getX(void) const
Definition: cwidgeteventargs.hxx:168
NXWidgets
Definition: cbgwindow.hxx:65
NXWidgets::CNxWidget::addWidgetEventHandler
void addWidgetEventHandler(CWidgetEventHandler *eventHandler)
Definition: cnxwidget.hxx:854
NXWidgets::CNumericEdit::m_minimum
int m_minimum
Definition: cnumericedit.hxx:123
NXWidgets::CNxTimer
Definition: cnxtimer.hxx:110
CDraggableLabel::onClick
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
Definition: cnumericedit.cxx:113
cnxtimer.hxx
NXWidgets::CNxFont
Definition: cnxfont.hxx:70
NXWidgets::CNumericEdit::setFont
virtual void setFont(CNxFont *font)
Definition: cnumericedit.cxx:172
NXWidgets::CNumericEdit::getPreferredDimensions
virtual void getPreferredDimensions(CRect &rect) const
Definition: cnumericedit.cxx:168
NXWidgets::CNumericEdit::handleDragEvent
virtual void handleDragEvent(const CWidgetEventArgs &e)
Definition: cnumericedit.cxx:248
NXWidgets::CNumericEdit::onResize
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)
Definition: cnumericedit.cxx:177
NXWidgets::CNumericEdit::m_increment
int m_increment
Definition: cnumericedit.hxx:125
NXWidgets::CNxWidget::m_widgetEventHandlers
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
NXWidgets::CWidgetEventArgs
Definition: cwidgeteventargs.hxx:127
NXWidgets::CRect
Definition: crect.hxx:101
cbutton.hxx
NXWidgets::CNumericEdit::m_button_minus
CButton * m_button_minus
Definition: cnumericedit.hxx:119
NXWidgets::CNxWidget::addWidget
void addWidget(CNxWidget *widget)
Definition: cnxwidget.cxx:1293
NXWidgets::CNumericEdit::handleReleaseEvent
virtual void handleReleaseEvent(const CWidgetEventArgs &e)
Definition: cnumericedit.cxx:197
NXWidgets::CNumericEdit::setValue
void setValue(int value)
Definition: cnumericedit.cxx:256
NXWidgets::CNxWidget::getWidth
nxgl_coord_t getWidth(void) const
Definition: cnxwidget.hxx:582
NXWidgets::CNumericEdit::m_button_plus
CButton * m_button_plus
Definition: cnumericedit.hxx:120
NXWidgets::CNxWidget::getX
nxgl_coord_t getX(void) const
Definition: cnxwidget.cxx:245
NXWidgets::CNxTimer::stop
void stop(void)
Definition: cnxtimer.cxx:176
NXWidgets::CNxString
Definition: cnxstring.hxx:121
CDraggableLabel::CDraggableLabel
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)
Definition: cnumericedit.cxx:105
NXWidgets::CLabel
Definition: clabel.hxx:112
NXWidgets::CNumericEdit::m_label
CLabel * m_label
Definition: cnumericedit.hxx:118
NXWidgets::CNumericEdit::m_maximum
int m_maximum
Definition: cnumericedit.hxx:124
NXWidgets::CNumericEdit::CNumericEdit
CNumericEdit(const CNumericEdit &num)
Definition: cnumericedit.hxx:151
NXWidgets::CNxTimer::start
void start(void)
Definition: cnxtimer.cxx:154
NXWidgets::CButton
Definition: cbutton.hxx:109
CDraggableLabel
Definition: cnumericedit.cxx:102
NXWidgets::CLabel::setFont
virtual void setFont(CNxFont *font)
Definition: clabel.cxx:259
NXWidgets::CWidgetEventHandlerList::raiseValueChangeEvent
void raiseValueChangeEvent(void)
Definition: cwidgeteventhandlerlist.cxx:443
NXWidgets::CNumericEdit::m_timer
CNxTimer * m_timer
Definition: cnumericedit.hxx:121
NXWidgets::CNumericEdit::m_value
int m_value
Definition: cnumericedit.hxx:122
NXWidgets::CLabel::setText
virtual void setText(const CNxString &text)
Definition: clabel.cxx:171
cnumericedit.hxx