NXWidgets  1.19
cslidervertical.hxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/include/cslidervertical.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 #ifndef __INCLUDE_CSLIDERVERTICAL_HXX
71 #define __INCLUDE_CSLIDERVERTICAL_HXX
72 
73 /****************************************************************************
74  * Included Files
75  ****************************************************************************/
76 
77 #include <nuttx/config.h>
78 
79 #include <stdint.h>
80 #include <stdbool.h>
81 
82 #include <nuttx/nx/nxglib.h>
83 
84 #include "cnxwidget.hxx"
85 #include "cwidgeteventhandler.hxx"
86 #include "islider.hxx"
87 
88 /****************************************************************************
89  * Pre-Processor Definitions
90  ****************************************************************************/
91 
92 /****************************************************************************
93  * Implementation Classes
94  ****************************************************************************/
95 
96 #if defined(__cplusplus)
97 
98 namespace NXWidgets
99 {
100  class CSliderVerticalGrip;
101  class CWidgetControl;
102 
103  /**
104  * Widget providing a sliding "grip" that can be moved up and
105  * down the "gutter". Essentially a scrollbar, but more
106  * generic.
107  */
108 
109  class CSliderVertical : public ISlider, public CNxWidget, public CWidgetEventHandler
110  {
111  protected:
112  CSliderVerticalGrip* m_grip; /**< Pointer to the grip. */
113  int m_minimumValue; /**< Minimum value that the grip can represent. */
114  int m_maximumValue; /**< Maximum value that the grip can represent. */
115  int32_t m_value; /**< Current value of the slider. */
116  nxgl_coord_t m_minimumGripHeight; /**< Smallest height that the grip can become. */
117  nxgl_coord_t m_pageSize; /**< Value of a page of data, used when clicking
118  the gutter */
119  int32_t m_gutterHeight; /**< Height of the gutter, taking into account
120  any adjustments made to the height of the grip. */
121  uint32_t m_contentSize; /**< Number of values in the min/max range. */
122 
123  /**
124  * Get the maximum possible value that the slider can represent. Useful when
125  * using the slider as a scrollbar, as the height of the grip prevents the full
126  * range of values being accessed (intentionally).
127  * The returned value is shifted left 16 places for more accuracy in fixed-point
128  * calculations.
129  *
130  * @return The maximum possible value that the slider can represent.
131  */
132 
133  int32_t getPhysicalMaximumValueWithBitshift(void) const;
134 
135  /**
136  * Get the value represented by the top of the grip. The value is
137  * bitshifted left 16 places for accuracy.
138  * return The value represented by the top of the grip.
139  */
140 
141  const int32_t getGripValue(void) const;
142 
143  /**
144  * Draw the area of this widget that falls within the clipping region.
145  * Called by the redraw() function to draw all visible regions.
146  *
147  * @param port The CGraphicsPort to draw to.
148  * @see redraw()
149  */
150 
151  virtual void drawContents(CGraphicsPort *port);
152 
153  /**
154  * Draw the area of this widget that falls within the clipping region.
155  * Called by the redraw() function to draw all visible regions.
156  *
157  * @param port The CGraphicsPort to draw to.
158  * @see redraw()
159  */
160 
161  virtual void drawBorder(CGraphicsPort *port);
162 
163  /**
164  * Resize the slider to the new dimensions.
165  *
166  * @param width The new width.
167  * @param height The new height.
168  */
169 
170  virtual void onResize(nxgl_coord_t width, nxgl_coord_t height);
171 
172  /**
173  * Moves the grip towards the mouse.
174  *
175  * @param x The x coordinate of the click.
176  * @param y The y coordinate of the click.
177  */
178 
179  virtual void onClick(nxgl_coord_t x, nxgl_coord_t y);
180 
181  /**
182  * Copy constructor is protected to prevent usage.
183  */
184 
185  inline CSliderVertical(const CSliderVertical &sliderVertical)
186  : CNxWidget(sliderVertical) { }
187 
188  /**
189  * Resize and redraw the grip.
190  */
191 
192  void resizeGrip();
193 
194  public:
195 
196  /**
197  * Constructor.
198  *
199  * @param pWidgetControl The controlling widget for the display
200  * @param x The x coordinate of the slider, relative to its parent.
201  * @param y The y coordinate of the slider, relative to its parent.
202  * @param width The width of the slider.
203  * @param height The height of the slider.
204  */
205 
206  CSliderVertical(CWidgetControl *pWidgetControl,
207  nxgl_coord_t x, nxgl_coord_t y,
208  nxgl_coord_t width, nxgl_coord_t height);
209 
210  /**
211  * Destructor.
212  */
213 
214  virtual inline ~CSliderVertical(void) { }
215 
216  /**
217  * Get the smallest value that the slider can represent.
218  *
219  * @return The smallest value.
220  */
221 
222  inline const int getMinimumValue(void) const
223  {
224  return m_minimumValue;
225  }
226 
227  /**
228  * Get the largest value that the slider can represent.
229  *
230  * @return The largest value.
231  */
232 
233  inline const int getMaximumValue(void) const
234  {
235  return m_maximumValue;
236  }
237 
238  /**
239  * Get the current value of the slider.
240  * return The current slider value.
241  */
242 
243  inline const int getValue(void) const
244  {
245  return m_value >> 16;
246  }
247 
248  /**
249  * Get the value represented by the height of the grip.
250  * For sliders, this would typically be 1 (so each new
251  * grip position is worth 1). For scrollbars, this
252  * would be the height of the scrolling widget.
253  *
254  * @return The page size.
255  */
256 
257  inline const nxgl_coord_t getPageSize(void) const
258  {
259  return m_pageSize;
260  }
261 
262  /**
263  * Set the smallest value that the slider can represent.
264  *
265  * @param value The smallest value.
266  */
267 
268  inline void setMinimumValue(const int value)
269  {
270  m_minimumValue = value;
271  m_contentSize = m_maximumValue - m_minimumValue + 1;
272  resizeGrip();
273  }
274 
275  /**
276  * Set the largest value that the slider can represent.
277  *
278  * @param value The largest value.
279  */
280 
281  inline void setMaximumValue(const int value)
282  {
283  m_maximumValue = value;
284  m_contentSize = m_maximumValue - m_minimumValue + 1;
285  resizeGrip();
286  }
287 
288  /**
289  * Set the value that of the slider. This will reposition
290  * and redraw the grip.
291  *
292  * @param value The new value.
293  */
294 
295  void setValue(const int value);
296 
297  /**
298  * Set the value that of the slider. This will reposition and redraw
299  * the grip. The supplied value should be shifted left 16 places.
300  * This ensures greater accuracy than the standard setValue() method if
301  * the slider is being used as a scrollbar.
302  *
303  * @param value The new value.
304  */
305 
306  void setValueWithBitshift(const int32_t value);
307 
308  /**
309  * Set the page size represented by the grip.
310  *
311  * @param pageSize The page size.
312  * @see getPageSize().
313  */
314 
315  inline void setPageSize(const nxgl_coord_t pageSize)
316  {
317  m_pageSize = pageSize;
318  resizeGrip();
319  }
320 
321  /**
322  * Process events fired by the grip.
323  *
324  * @param e The event details.
325  */
326 
327  virtual void handleDragEvent(const CWidgetEventArgs &e);
328 
329  /**
330  * Get the smallest value that the slider can move through when
331  * dragged.
332  *
333  * @return The smallest value that the slider can move through when
334  * dragged.
335  */
336 
337  nxgl_coord_t getMinimumStep(void) const;
338  };
339 }
340 
341 #endif // __cplusplus
342 
343 #endif // __INCLUDE_CSLIDERVERTICAL_HXX
344 
virtual void handleDragEvent(const CWidgetEventArgs &e)
nxgl_coord_t getMinimumStep(void) const
virtual void drawContents(CGraphicsPort *port)
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
const int32_t getGripValue(void) const
void setValue(const int value)
void setPageSize(const nxgl_coord_t pageSize)
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)
int32_t getPhysicalMaximumValueWithBitshift(void) const
void setMaximumValue(const int value)
const int getMinimumValue(void) const
void setValueWithBitshift(const int32_t value)
const int getValue(void) const
CSliderVertical(const CSliderVertical &sliderVertical)
virtual void drawBorder(CGraphicsPort *port)
const nxgl_coord_t getPageSize(void) const
void setMinimumValue(const int value)
const int getMaximumValue(void) const
CSliderVerticalGrip * m_grip