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