NXWidgets  1.19
csliderhorizontal.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/src/csliderhorizontal.cxx
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 "cwidgetcontrol.hxx"
80 #include "csliderhorizontal.hxx"
82 #include "cgraphicsport.hxx"
83 
84 /****************************************************************************
85  * Pre-Processor Definitions
86  ****************************************************************************/
87 
88 /****************************************************************************
89  * Method Implementations
90  ****************************************************************************/
91 
92 using namespace NXWidgets;
93 
94 /**
95  * Constructor.
96  *
97  * @param pWidgetControl The controlling widget for the display
98  * @param x The x coordinate of the slider, relative to its parent.
99  * @param y The y coordinate of the slider, relative to its parent.
100  * @param width The width of the slider.
101  * @param height The height of the slider.
102  */
103 
105  nxgl_coord_t x, nxgl_coord_t y,
106  nxgl_coord_t width, nxgl_coord_t height)
107 : CNxWidget(pWidgetControl, x, y, width, height, WIDGET_DRAGGABLE)
108 {
109  m_minimumValue = 0;
110  m_maximumValue = 0;
111  m_contentSize = 0;
112  m_value = 0;
113  m_minimumGripWidth = 10;
114  m_pageSize = 1;
115 
116  m_flags.permeable = false;
117  m_flags.borderless = false;
118  m_flags.doubleClickable = false;
119 
120  // Create grip
121 
122  CRect rect;
123  getClientRect(rect);
124 
125  m_grip = new CSliderHorizontalGrip(pWidgetControl, rect.getX(), rect.getY(),
126  rect.getWidth(), rect.getHeight());
128  addWidget(m_grip);
129 
130  m_gutterWidth = rect.getWidth();
131 }
132 
133 /**
134  * Set the value that of the slider. This will reposition
135  * and redraw the grip.
136  *
137  * @param value The new value.
138  */
139 
140 void CSliderHorizontal::setValue(const int value)
141 {
142  setValueWithBitshift((int32_t)value << 16);
143 }
144 
145 /**
146  * Set the value that of the slider. This will reposition and redraw
147  * the grip. The supplied value should be bitshifted left 16 places.
148  * This ensures greater accuracy than the standard setValue() method if
149  * the slider is being used as a scrollbar.
150  *
151  * @param value The new value.
152  */
153 
155 {
156  CRect rect;
157  getClientRect(rect);
158 
159  // Can the grip move?
160 
161  if ((rect.getWidth() > m_grip->getWidth()) && (m_maximumValue != m_minimumValue))
162  {
163  int32_t newValue = value;
164  int32_t maxValue = getPhysicalMaximumValueWithBitshift();
165 
166  // Limit to max/min values
167 
168  if (newValue > maxValue)
169  {
170  newValue = maxValue;
171  }
172 
173  if (newValue >> 16 < m_minimumValue)
174  {
175  newValue = m_minimumValue << 16;
176  }
177 
178  uint32_t scrollRatio = newValue / m_contentSize;
179  int32_t newGripX = m_gutterWidth * scrollRatio;
180  newGripX += newGripX & 0x8000;
181  newGripX >>= 16;
182  newGripX += rect.getX();
183 
184  m_grip->moveTo(newGripX, rect.getY());
185 
186  // Update stored value if necessary
187 
188  if (m_value != newValue)
189  {
190  m_value = newValue;
192  }
193  }
194 }
195 
196 /**
197  * Process events fired by the grip.
198  *
199  * @param e The event details.
200  */
201 
203 {
204  // Handle grip events
205 
206  if ((e.getSource() == m_grip) && (e.getSource() != NULL))
207  {
208  int32_t newValue = getGripValue() >> 16;
209 
210  // Grip has moved - compare values and raise event if the
211  // value has changed. Compare using integer values rather
212  // than fixed-point.
213 
214  if (m_value >> 16 != newValue)
215  {
216  m_value = newValue << 16;
218  }
219  }
220 }
221 
222 /**
223  * Get the smallest value that the slider can move through when
224  * dragged.
225  *
226  * @return The smallest value that the slider can move through when
227  * dragged.
228  */
229 
230 nxgl_coord_t CSliderHorizontal::getMinimumStep(void) const
231 {
232  // If the ratio of content to gutter is greater than or equal to one,
233  // the minimum step that the slider can represent will be that ratio.
234 
235  uint32_t gutterRatio = m_contentSize << 16 / m_gutterWidth;
236  gutterRatio += gutterRatio & 0x8000;
237  gutterRatio >>= 16;
238 
239  if (gutterRatio > 0)
240  {
241  return gutterRatio;
242  }
243 
244  return 1;
245 }
246 
247 /**
248  * Get the maximum possible value that the slider can represent. Useful when
249  * using the slider as a scrollbar, as the height of the grip prevents the full
250  * range of values being accessed (intentionally).
251  * The returned value is bitshfted left 16 places for more accuracy in fixed-point
252  * calculations.
253  *
254  * @return The maximum possible value that the slider can represent.
255  */
256 
258 {
259  uint32_t maxX = m_gutterWidth - m_grip->getWidth();
260  uint32_t scrollRatio = (maxX << 16) / m_gutterWidth;
261  int32_t value = (scrollRatio * m_contentSize);
262 
263  return value;
264 }
265 
266 /**
267  * Get the value represented by the top of the grip.
268  * return The value represented by the top of the grip.
269  */
270 
271 const int32_t CSliderHorizontal::getGripValue(void) const
272 {
273  // Calculate the current value represented by the top of the grip
274 
275  CRect rect;
276  getClientRect(rect);
277 
278  uint32_t gripPos = ((m_grip->getX() - getX()) - rect.getX());
279  uint32_t scrollRatio = (gripPos << 16) / m_gutterWidth;
280  int32_t value = (scrollRatio * m_contentSize);
281 
282  return value;
283 }
284 
285 /**
286  * Draw the area of this widget that falls within the clipping region.
287  * Called by the redraw() function to draw all visible regions.
288  *
289  * @param port The CGraphicsPort to draw to.
290  * @see redraw()
291  */
292 
294 {
295  CRect rect;
296  getRect(rect);
297 
298  port->drawFilledRect(rect.getX(), rect.getY(),
299  rect.getWidth(), rect.getHeight(),
301 }
302 
303 /**
304  * Draw the area of this widget that falls within the clipping region.
305  * Called by the redraw() function to draw all visible regions.
306  *
307  * @param port The CGraphicsPort to draw to.
308  * @see redraw()
309  */
310 
312 {
313  // Stop drawing if the widget indicates it should not have an outline
314 
315  if (!isBorderless())
316  {
317  port->drawBevelledRect(getX(), getY(), getWidth(), getHeight(),
319  }
320 }
321 
322 /**
323  * Resize the slider to the new dimensions.
324  *
325  * @param width The new width.
326  * @param height The new height.
327  */
328 
329 void CSliderHorizontal::onResize(nxgl_coord_t width, nxgl_coord_t height)
330 {
331  // Remember current values
332 
333  int32_t oldValue = m_value;
334  bool events = raisesEvents();
335 
336  // Disable event raising
337 
338  setRaisesEvents(false);
339  resizeGrip();
340 
341  // Set back to current value
342 
343  setValue(oldValue);
344 
345  // Reset event raising
346 
347  setRaisesEvents(events);
348 }
349 
350 /**
351  * Moves the grip towards the mouse.
352  *
353  * @param x The x coordinate of the click.
354  * @param y The y coordinate of the click.
355  */
356 
357 void CSliderHorizontal::onClick(nxgl_coord_t x, nxgl_coord_t y)
358 {
359  // Which way should the grip move?
360 
361  if (x > m_grip->getX())
362  {
363  // Move grip right
364 
366  }
367  else
368  {
369  // Move grip left
370 
372  }
373 }
374 
375 /**
376  * Resize and redraw the grip.
377  */
378 
380 {
381  // Get available size
382 
383  CRect rect;
384  getClientRect(rect);
385 
386  int32_t gripRatio = (m_pageSize << 16) / m_contentSize;
387  int32_t gripSize = rect.getWidth() * gripRatio;
388  gripSize >>= 16;
389  m_gutterWidth = rect.getWidth();
390 
391  if (gripSize < m_minimumGripWidth)
392  {
393  // TODO: Need to implement scaling here. If we resize the grip to be
394  // artificially larger, we effectively reduce the scale (not just the
395  // height) of the gutter. Each position in the gutter needs to be
396  // reduced in value.
397  }
398 
399  m_grip->resize(gripSize, rect.getHeight());
400 }
401 
402 
CSliderHorizontal(const CSliderHorizontal &sliderHorizontal)
const T & getSource(void) const
Definition: teventargs.hxx:127
bool isBorderless(void) const
Definition: cnxwidget.hxx:549
nxgl_mxpixel_t getShadowEdgeColor(void) const
Definition: cnxwidget.hxx:749
const int32_t getGripValue(void) const
nxgl_coord_t getHeight(void) const
Definition: crect.hxx:204
bool moveTo(nxgl_coord_t x, nxgl_coord_t y)
Definition: cnxwidget.cxx:990
nxgl_coord_t getMinimumStep(void) const
void getRect(CRect &rect) const
Definition: cnxwidget.cxx:448
void setRaisesEvents(bool raises)
Definition: cnxwidget.hxx:887
nxgl_coord_t getWidth(void) const
Definition: crect.hxx:181
void drawBevelledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t shineColor, nxgl_mxpixel_t shadowColor)
nxgl_coord_t getX(void) const
Definition: crect.hxx:160
bool raisesEvents(void) const
Definition: cnxwidget.hxx:898
void setValue(const int value)
bool resize(nxgl_coord_t width, nxgl_coord_t height)
Definition: cnxwidget.cxx:1079
int32_t getPhysicalMaximumValueWithBitshift(void) const
nxgl_coord_t getHeight(void) const
Definition: cnxwidget.hxx:593
void getClientRect(CRect &rect) const
Definition: cnxwidget.cxx:421
void addWidgetEventHandler(CWidgetEventHandler *eventHandler)
Definition: cnxwidget.hxx:854
nxgl_mxpixel_t getSelectedBackgroundColor(void) const
Definition: cnxwidget.hxx:727
void drawFilledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t color)
virtual void handleDragEvent(const CWidgetEventArgs &e)
virtual void drawContents(CGraphicsPort *port)
nxgl_mxpixel_t getShineEdgeColor(void) const
Definition: cnxwidget.hxx:738
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
void addWidget(CNxWidget *widget)
Definition: cnxwidget.cxx:1293
nxgl_coord_t getY(void) const
Definition: cnxwidget.cxx:261
nxgl_coord_t getWidth(void) const
Definition: cnxwidget.hxx:582
nxgl_coord_t getX(void) const
Definition: cnxwidget.cxx:245
nxgl_coord_t getY(void) const
Definition: crect.hxx:170
virtual void drawBorder(CGraphicsPort *port)
CSliderHorizontalGrip * m_grip
void setValueWithBitshift(const int32_t value)
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)