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 
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);
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,
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,
146 
147  // Create timer
148 
149  m_scrollTimeout = 10;
150  m_timer = new CNxTimer(pWidgetControl, m_scrollTimeout, true);
152 
157 }
158 
159 /**
160  * Get the smallest value that the slider can represent.
161  *
162  * @return The smallest value.
163  */
164 
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 
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 
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 
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 
250 {
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 
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 
285  }
286  else if (m_rightButton->isClicked())
287  {
288  // Move the grip right
289 
291  }
292  }
293 }
294 
295 /**
296  * Process events fired by the grip.
297  *
298  * @param e The event details.
299  */
300 
302 {
303  if (e.getSource() == m_leftButton)
304  {
305  // Start the timer
306 
307  m_timer->start();
308 
309  // Move the grip left
310 
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 
322  }
323 }
324 
325 /**
326  * Process events fired by the grip.
327  *
328  * @param e The event details.
329  */
330 
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 
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 
364 {
365  if (e.getSource() == m_slider)
366  {
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);
394 
395  // Set back to current value
396 
397  setValue(value);
398 
399  // Reset event raising
400 
401  setRaisesEvents(events);
402 }
bool isClicked(void) const
Definition: cnxwidget.hxx:560
const T & getSource(void) const
Definition: teventargs.hxx:127
bool moveTo(nxgl_coord_t x, nxgl_coord_t y)
Definition: cnxwidget.cxx:990
nxgl_coord_t getMinimumStep(void) const
const nxgl_coord_t getPageSize(void) const
void setRaisesEvents(bool raises)
Definition: cnxwidget.hxx:887
bool raisesEvents(void) const
Definition: cnxwidget.hxx:898
void setPageSize(const nxgl_coord_t pageSize)
void setMaximumValue(const int value)
virtual void handleActionEvent(const CWidgetEventArgs &e)
void setValue(const int value)
void setMinimumValue(const int value)
bool resize(nxgl_coord_t width, nxgl_coord_t height)
Definition: cnxwidget.cxx:1079
virtual void handleReleaseOutsideEvent(const CWidgetEventArgs &e)
const int getMaximumValue(void) const
void setPageSize(const nxgl_coord_t pageSize)
void setValueWithBitshift(const int32_t value)
virtual void handleValueChangeEvent(const CWidgetEventArgs &e)
void addWidgetEventHandler(CWidgetEventHandler *eventHandler)
Definition: cnxwidget.hxx:854
const struct SBitmap g_arrowLeft
Definition: glyphs.hxx:120
const nxgl_coord_t getPageSize(void) const
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
void addWidget(CNxWidget *widget)
Definition: cnxwidget.cxx:1293
virtual void handleClickEvent(const CWidgetEventArgs &e)
virtual void handleReleaseEvent(const CWidgetEventArgs &e)
nxgl_coord_t getWidth(void) const
Definition: cnxwidget.hxx:582
CWidgetStyle m_style
Definition: cnxwidget.hxx:183
CScrollbarHorizontal(const CScrollbarHorizontal &scrollbarHorizontal)
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)
void start(void)
Definition: cnxtimer.cxx:154
void setValueWithBitshift(const int32_t value)
const struct SBitmap g_arrowRight
Definition: glyphs.hxx:121
const int getMinimumValue(void) const
const int getValue(void) const