NXWidgets: cscrollbarhorizontal.cxx Source File

NXWidgets 1.19

cscrollbarhorizontal.cxx
Go to the documentation of this file.
1 /****************************************************************************
2 * NxWidgets/libnxwidgets/include/cscrollbarhorizontal.hxx
3 *
4 * Copyright (C) 2012, 2014 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 <stdint.h>
77 #include <stdbool.h>
78
79 #include <nuttx/nx/nxglib.h>
80
81 #include "nxconfig.hxx"
82 #include "cnxwidget.hxx"
83 #include "cwidgeteventhandler.hxx"
84 #include "islider.hxx"
85 #include "cscrollbarhorizontal.hxx"
86 #include "cglyphbutton.hxx"
87 #include "csliderhorizontal.hxx"
88 #include "cnxtimer.hxx"
89 #include "glyphs.hxx"
90
91 /****************************************************************************
92 * Pre-Processor Definitions
93 ****************************************************************************/
94
95 /****************************************************************************
96 * Method Implementation
97 ****************************************************************************/
98
99 using namespace NXWidgets;
100
101 /**
102 * Constructor.
103 *
104 * @param pWidgetControl The widget control instance for the window.
105 * @param x The x coordinate of the slider, relative to its parent.
106 * @param y The y coordinate of the slider, relative to its parent.
107 * @param width The width of the slider.
108 * @param height The height of the slider.
109 * @param style The style that the widget should use. If this is not
110 * specified, the widget will use the values stored in the global
111 * g_defaultWidgetStyle object. The widget will copy the properties of
112 * the style into its own internal style object.
113 */
114
115 CScrollbarHorizontal::CScrollbarHorizontal(CWidgetControl *pWidgetControl,
116 nxgl_coord_t x, nxgl_coord_t y,
117 nxgl_coord_t width, nxgl_coord_t height,
118 CWidgetStyle* style)
119 : CNxWidget(pWidgetControl, x, y, width, height, WIDGET_BORDERLESS, style)
120 {
121 m_buttonWidth = 10;
122
123 // Create the children
124
125 m_slider = new CSliderHorizontal(pWidgetControl, m_buttonWidth, 0,
126 width - (m_buttonWidth << 1), height);
127 m_slider->addWidgetEventHandler(this);
128
129 // Set up the left glyph button
130
131 m_leftButton = new CGlyphButton(pWidgetControl,
132 0, 0,
133 m_buttonWidth, height,
134 0, 0,
135 &g_arrowLeft, &g_arrowLeft, &m_style);
136 m_leftButton->addWidgetEventHandler(this);
137
138 // Set up the right glyph button
139
140 m_rightButton = new CGlyphButton(pWidgetControl,
141 width - m_buttonWidth, 0,
142 m_buttonWidth, height,
143 0, 0,
144 &g_arrowRight, &g_arrowRight, &m_style);
145 m_rightButton->addWidgetEventHandler(this);
146
147 // Create timer
148
149 m_scrollTimeout = 10;
150 m_timer = new CNxTimer(pWidgetControl, m_scrollTimeout, true);
151 m_timer->addWidgetEventHandler(this);
152
153 addWidget(m_slider);
154 addWidget(m_leftButton);
155 addWidget(m_rightButton);
156 addWidget(m_timer);
157 }
158
159 /**
160 * Get the smallest value that the slider can represent.
161 *
162 * @return The smallest value.
163 */
164
165 const int CScrollbarHorizontal::getMinimumValue(void) const
166 {
167 return m_slider->getMinimumValue();
168 }
169
170 /**
171 * Get the largest value that the slider can represent.
172 *
173 * @return The largest value.
174 */
175
176 const int CScrollbarHorizontal::getMaximumValue(void) const
177 {
178 return m_slider->getMaximumValue();
179 }
180
181 /**
182 * Get the current value of the slider.
183 *
184 * @return The current slider value.
185 */
186
187 const int CScrollbarHorizontal::getValue(void) const
188 {
189 return m_slider->getValue();
190 }
191
192 /**
193 * Get the value represented by the height of the grip.
194 * For sliders, this would typically be 1 (so each new
195 * grip position is worth 1). For scrollbars, this
196 * would be the height of the scrolling widget.
197 *
198 * @return The page size.
199 */
200
201 const nxgl_coord_t CScrollbarHorizontal::getPageSize(void) const
202 {
203 return m_slider->getPageSize();
204 }
205
206 /**
207 * Set the smallest value that the slider can represent.
208 *
209 * @param value The smallest value.
210 */
211
212 void CScrollbarHorizontal::setMinimumValue(const int value)
213 {
214 m_slider->setMinimumValue(value);
215 }
216
217 /**
218 * Set the largest value that the slider can represent.
219 *
220 * @param value The largest value.
221 */
222
223 void CScrollbarHorizontal::setMaximumValue(const int value)
224 {
225 m_slider->setMaximumValue(value);
226 }
227
228 /**
229 * Set the value that of the slider. This will reposition
230 * and redraw the grip.
231 *
232 * @param value The new value.
233 */
234
235 void CScrollbarHorizontal::setValue(const int value)
236 {
237 m_slider->setValue(value);
238 }
239
240 /**
241 * Set the value that of the slider. This will reposition and redraw
242 * the grip. The supplied value should be bitshifted left 16 places.
243 * This ensures greater accuracy than the standard setValue() method if
244 * the slider is being used as a scrollbar.
245 *
246 * @param value The new value.
247 */
248
249 void CScrollbarHorizontal::setValueWithBitshift(const int32_t value)
250 {
251 m_slider->setValueWithBitshift(value);
252 }
253
254 /**
255 * Set the page size represented by the grip.
256 *
257 * @param pageSize The page size.
258 * @see getPageSize().
259 */
260
261 void CScrollbarHorizontal::setPageSize(nxgl_coord_t pageSize)
262 {
263 m_slider->setPageSize(pageSize);
264 }
265
266 /**
267 * Process events fired by the grip.
268 *
269 * @param e The event details.
270 */
271
272 void CScrollbarHorizontal::handleActionEvent(const CWidgetEventArgs &e)
273 {
274 // Check which widget fired the event
275
276 if (e.getSource() == m_timer)
277 {
278 // Which widget is clicked?
279
280 if (m_leftButton->isClicked())
281 {
282 // Move the grip left
283
284 m_slider->setValue(m_slider->getValue() - m_slider->getMinimumStep());
285 }
286 else if (m_rightButton->isClicked())
287 {
288 // Move the grip right
289
290 m_slider->setValue(m_slider->getValue() + m_slider->getMinimumStep());
291 }
292 }
293 }
294
295 /**
296 * Process events fired by the grip.
297 *
298 * @param e The event details.
299 */
300
301 void CScrollbarHorizontal::handleClickEvent(const CWidgetEventArgs &e)
302 {
303 if (e.getSource() == m_leftButton)
304 {
305 // Start the timer
306
307 m_timer->start();
308
309 // Move the grip left
310
311 m_slider->setValue(m_slider->getValue() - m_slider->getMinimumStep());
312 }
313 else if (e.getSource() == m_rightButton)
314 {
315 // Start the timer
316
317 m_timer->start();
318
319 // Move the grip right
320
321 m_slider->setValue(m_slider->getValue() + m_slider->getMinimumStep());
322 }
323 }
324
325 /**
326 * Process events fired by the grip.
327 *
328 * @param e The event details.
329 */
330
331 void CScrollbarHorizontal::handleReleaseEvent(const CWidgetEventArgs &e)
332 {
333 if ((e.getSource() == m_leftButton) || (e.getSource() == m_rightButton))
334 {
335 // Stop the timer
336
337 m_timer->stop();
338 }
339 }
340
341 /**
342 * Process events fired by the grip.
343 *
344 * @param e The event details.
345 */
346
347 void CScrollbarHorizontal::handleReleaseOutsideEvent(const CWidgetEventArgs &e)
348 {
349 if ((e.getSource() == m_leftButton) || (e.getSource() == m_rightButton))
350 {
351 // Stop the timer
352
353 m_timer->stop();
354 }
355 }
356
357 /**
358 * Process events fired by the grip.
359 *
360 * @param e The event details.
361 */
362
363 void CScrollbarHorizontal::handleValueChangeEvent(const CWidgetEventArgs &e)
364 {
365 if (e.getSource() == m_slider)
366 {
367 m_widgetEventHandlers->raiseValueChangeEvent();
368 }
369 }
370
371 /**
372 * Resize the scrollbar to the new dimensions.
373 *
374 * @param width The new width.
375 * @param height The new height.
376 */
377
378 void CScrollbarHorizontal::onResize(nxgl_coord_t width, nxgl_coord_t height)
379 {
380 // Remember current values
381
382 nxgl_coord_t value = getValue();
383 bool events = raisesEvents();
384
385 // Disable event raising
386
387 setRaisesEvents(false);
388
389 // Resize and move children
390
391 m_slider->resize(width - (m_buttonWidth << 1), height);
392 m_leftButton->moveTo(m_slider->getWidth(), 0);
393 m_rightButton->moveTo(m_slider->getWidth() + m_buttonWidth, 0);
394
395 // Set back to current value
396
397 setValue(value);
398
399 // Reset event raising
400
401 setRaisesEvents(events);
402 }
NXWidgets::CGlyphButton
Definition: cglyphbutton.hxx:113
NXWidgets::CNxWidget
Definition: cnxwidget.hxx:121
NXWidgets::CNxWidget::isClicked
bool isClicked(void) const
Definition: cnxwidget.hxx:560
NXWidgets::TEventArgs::getSource
const T & getSource(void) const
Definition: teventargs.hxx:127
NXWidgets::CWidgetStyle
Definition: cwidgetstyle.hxx:125
NXWidgets::CNxWidget::moveTo
bool moveTo(nxgl_coord_t x, nxgl_coord_t y)
Definition: cnxwidget.cxx:990
NXWidgets::CWidgetControl
Definition: cwidgetcontrol.hxx:95
NXWidgets::CSliderHorizontal::getMinimumStep
nxgl_coord_t getMinimumStep(void) const
Definition: csliderhorizontal.cxx:230
NXWidgets::CScrollbarHorizontal::getPageSize
const nxgl_coord_t getPageSize(void) const
Definition: cscrollbarhorizontal.cxx:201
NXWidgets::CScrollbarHorizontal::m_buttonWidth
nxgl_coord_t m_buttonWidth
Definition: cscrollbarhorizontal.hxx:120
NXWidgets::CNxWidget::setRaisesEvents
void setRaisesEvents(bool raises)
Definition: cnxwidget.hxx:887
glyphs.hxx
NXWidgets::CNxWidget::raisesEvents
bool raisesEvents(void) const
Definition: cnxwidget.hxx:898
NXWidgets::CSliderHorizontal::setPageSize
void setPageSize(const nxgl_coord_t pageSize)
Definition: csliderhorizontal.hxx:315
NXWidgets::CSliderHorizontal::setMaximumValue
void setMaximumValue(const int value)
Definition: csliderhorizontal.hxx:281
NXWidgets::CScrollbarHorizontal::handleActionEvent
virtual void handleActionEvent(const CWidgetEventArgs &e)
Definition: cscrollbarhorizontal.cxx:272
NXWidgets::CSliderHorizontal::setValue
void setValue(const int value)
Definition: csliderhorizontal.cxx:140
NXWidgets::CSliderHorizontal::setMinimumValue
void setMinimumValue(const int value)
Definition: csliderhorizontal.hxx:268
NXWidgets::CScrollbarHorizontal::m_slider
CSliderHorizontal * m_slider
Definition: cscrollbarhorizontal.hxx:117
NXWidgets::CSliderHorizontal
Definition: csliderhorizontal.hxx:109
NXWidgets::CScrollbarHorizontal::m_rightButton
CGlyphButton * m_rightButton
Definition: cscrollbarhorizontal.hxx:119
NXWidgets::CNxWidget::resize
bool resize(nxgl_coord_t width, nxgl_coord_t height)
Definition: cnxwidget.cxx:1079
islider.hxx
NXWidgets::CScrollbarHorizontal::handleReleaseOutsideEvent
virtual void handleReleaseOutsideEvent(const CWidgetEventArgs &e)
Definition: cscrollbarhorizontal.cxx:347
NXWidgets
Definition: cbgwindow.hxx:65
NXWidgets::CSliderHorizontal::getMaximumValue
const int getMaximumValue(void) const
Definition: csliderhorizontal.hxx:232
NXWidgets::CScrollbarHorizontal::setPageSize
void setPageSize(const nxgl_coord_t pageSize)
Definition: cscrollbarhorizontal.cxx:261
NXWidgets::CScrollbarHorizontal::getMaximumValue
const int getMaximumValue(void) const
Definition: cscrollbarhorizontal.cxx:176
cglyphbutton.hxx
NXWidgets::CScrollbarHorizontal::setValueWithBitshift
void setValueWithBitshift(const int32_t value)
Definition: cscrollbarhorizontal.cxx:249
NXWidgets::CScrollbarHorizontal::handleValueChangeEvent
virtual void handleValueChangeEvent(const CWidgetEventArgs &e)
Definition: cscrollbarhorizontal.cxx:363
NXWidgets::CNxWidget::addWidgetEventHandler
void addWidgetEventHandler(CWidgetEventHandler *eventHandler)
Definition: cnxwidget.hxx:854
nxconfig.hxx
NXWidgets::CNxTimer
Definition: cnxtimer.hxx:110
cnxtimer.hxx
NXWidgets::g_arrowLeft
const struct SBitmap g_arrowLeft
Definition: glyphs.hxx:120
NXWidgets::CScrollbarHorizontal::m_timer
CNxTimer * m_timer
Definition: cscrollbarhorizontal.hxx:123
csliderhorizontal.hxx
NXWidgets::CSliderHorizontal::getPageSize
const nxgl_coord_t getPageSize(void) const
Definition: csliderhorizontal.hxx:257
NXWidgets::CNxWidget::m_widgetEventHandlers
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
NXWidgets::CScrollbarHorizontal::m_leftButton
CGlyphButton * m_leftButton
Definition: cscrollbarhorizontal.hxx:118
NXWidgets::CWidgetEventArgs
Definition: cwidgeteventargs.hxx:127
NXWidgets::CNxWidget::addWidget
void addWidget(CNxWidget *widget)
Definition: cnxwidget.cxx:1293
NXWidgets::CScrollbarHorizontal::handleClickEvent
virtual void handleClickEvent(const CWidgetEventArgs &e)
Definition: cscrollbarhorizontal.cxx:301
NXWidgets::CScrollbarHorizontal::m_scrollTimeout
uint8_t m_scrollTimeout
Definition: cscrollbarhorizontal.hxx:121
NXWidgets::CScrollbarHorizontal::handleReleaseEvent
virtual void handleReleaseEvent(const CWidgetEventArgs &e)
Definition: cscrollbarhorizontal.cxx:331
NXWidgets::CScrollbarHorizontal::setMinimumValue
void setMinimumValue(const int value)
Definition: cscrollbarhorizontal.cxx:212
NXWidgets::CNxWidget::getWidth
nxgl_coord_t getWidth(void) const
Definition: cnxwidget.hxx:582
NXWidgets::CNxTimer::stop
void stop(void)
Definition: cnxtimer.cxx:176
NXWidgets::CNxWidget::m_style
CWidgetStyle m_style
Definition: cnxwidget.hxx:183
NXWidgets::CScrollbarHorizontal::CScrollbarHorizontal
CScrollbarHorizontal(const CScrollbarHorizontal &scrollbarHorizontal)
Definition: cscrollbarhorizontal.hxx:138
NXWidgets::CScrollbarHorizontal::onResize
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)
Definition: cscrollbarhorizontal.cxx:378
cnxwidget.hxx
NXWidgets::CNxTimer::start
void start(void)
Definition: cnxtimer.cxx:154
NXWidgets::CSliderHorizontal::setValueWithBitshift
void setValueWithBitshift(const int32_t value)
Definition: csliderhorizontal.cxx:154
NXWidgets::g_arrowRight
const struct SBitmap g_arrowRight
Definition: glyphs.hxx:121
cscrollbarhorizontal.hxx
NXWidgets::CScrollbarHorizontal::getMinimumValue
const int getMinimumValue(void) const
Definition: cscrollbarhorizontal.cxx:165
NXWidgets::CSliderHorizontal::getMinimumValue
const int getMinimumValue(void) const
Definition: csliderhorizontal.hxx:221
NXWidgets::CWidgetEventHandlerList::raiseValueChangeEvent
void raiseValueChangeEvent(void)
Definition: cwidgeteventhandlerlist.cxx:443
NXWidgets::CScrollbarHorizontal::setValue
void setValue(const int value)
Definition: cscrollbarhorizontal.cxx:235
cwidgeteventhandler.hxx
NXWidgets::CSliderHorizontal::getValue
const int getValue(void) const
Definition: csliderhorizontal.hxx:243
NXWidgets::CScrollbarHorizontal::setMaximumValue
void setMaximumValue(const int value)
Definition: cscrollbarhorizontal.cxx:223
NXWidgets::CScrollbarHorizontal::getValue
const int getValue(void) const
Definition: cscrollbarhorizontal.cxx:187