NXWidgets  1.19
cglyphsliderhorizontal.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/src/cglyphsliderhorizontal.cxx
3  *
4  * Copyright (C) 2013 Ken Pettit. All rights reserved.
5  * Author: Ken Pettit <pettitkd@gmail.com>
6  * Gregory Nutt <gnutt@nuttx.org> (updates to Ken's fundamental work)
7  *
8  * Based on and copied from csliderhorizontal.cxx written by Gregory Nutt
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
21  * me be used to endorse or promote products derived from this software
22  * without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
31  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  ****************************************************************************
38  *
39  * Portions of this package derive from Woopsi (http://woopsi.org/) and
40  * portions are original efforts. It is difficult to determine at this
41  * point what parts are original efforts and which parts derive from Woopsi.
42  * However, in any event, the work of Antony Dzeryn will be acknowledged
43  * in most NxWidget files. Thanks Antony!
44  *
45  * Copyright (c) 2007-2011, Antony Dzeryn
46  * All rights reserved.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions are met:
50  *
51  * * Redistributions of source code must retain the above copyright
52  * notice, this list of conditions and the following disclaimer.
53  * * Redistributions in binary form must reproduce the above copyright
54  * notice, this list of conditions and the following disclaimer in the
55  * documentation and/or other materials provided with the distribution.
56  * * Neither the names "Woopsi", "Simian Zombie" nor the
57  * names of its contributors may be used to endorse or promote products
58  * derived from this software without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
61  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
62  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
63  * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
64  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
65  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
66  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
67  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
69  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70  *
71  ****************************************************************************/
72 
73 /****************************************************************************
74  * Included Files
75  ****************************************************************************/
76 
77 #include <nuttx/config.h>
78 
79 #include <cstdint>
80 #include <cstdbool>
81 
82 #include "ibitmap.hxx"
83 #include "cwidgetcontrol.hxx"
86 #include "cimage.hxx"
87 #include "cgraphicsport.hxx"
88 
89 /****************************************************************************
90  * Pre-Processor Definitions
91  ****************************************************************************/
92 
93 /****************************************************************************
94  * Method Implementations
95  ****************************************************************************/
96 
97 using namespace NXWidgets;
98 
99 /**
100  * Constructor.
101  *
102  * @param control The widget control instance for the window.
103  * @param x The x coordinate of the slider, relative to its parent.
104  * @param y The y coordinate of the slider, relative to its parent.
105  * @param width The width of the slider.
106  * @param thickness The thickness of the slider.
107  * @param gripBitmap The slider grip image
108  * @param fillColor The color to use when filling the grip
109  * @param fill True: The grip will be filled with fillColor
110  */
111 
113  nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width,
114  nxgl_coord_t thickness, IBitmap *gripBitmap,
115  nxwidget_pixel_t fillColor, bool fill)
116 :CNxWidget(control, x, y, width, thickness, WIDGET_DRAGGABLE)
117 {
118  // Initialize state data
119 
120  m_minimumValue = 0;
121  m_maximumValue = 0;
122  m_contentSize = 0;
123  m_value = 0;
124  m_minimumGripWidth = 10;
125  m_pageSize = 1;
126  m_fillColor = fillColor;
127  m_fill = fill;
128  m_barThickness = thickness;
129 
130  // Correct the height of the widget. The widget height was initially set
131  // to the thickness of the grip. But the grip image is normally a little
132  // taller than the slider is thick.
133  //
134  // Do use the resize method here; we are not ready for the resize events.
135 
136  nxgl_coord_t gripHeight = gripBitmap->getHeight() + 4;
137  if (gripHeight > thickness)
138  {
139  // Reset the widget height to the height of the grip image
140 
141  m_rect.setHeight(gripHeight);
142  }
143 
144  // Set widget attributes
145 
146  m_flags.permeable = false;
147  m_flags.borderless = false;
148  m_flags.doubleClickable = false;
149 
150  // Set the "gutter" width
151 
152  CRect rect;
153  getClientRect(rect);
154  m_gutterWidth = rect.getWidth();
155 
156  // Create the grip
157 
158  m_grip = new CGlyphSliderHorizontalGrip(control, x, y,
159  gripBitmap->getWidth() + 4,
160  gripHeight, gripBitmap);
161  m_grip->setBorderless(true);
163  addWidget(m_grip);
164 }
165 
166 /**
167  * Set the value of the slider. This will reposition and redraw the grip.
168  *
169  * @param value The new value.
170  */
171 
172 void CGlyphSliderHorizontal::setValue(const int value)
173 {
174  setValueWithBitshift((int32_t)value << 16);
175 }
176 
177 /**
178  * Set the value that of the slider. This will reposition and redraw
179  * the grip. The supplied value should be shifted left 16 places.
180  * This ensures greater accuracy than the standard setValue() method if
181  * the slider is being used as a scrollbar.
182  *
183  * @param value The new value.
184  */
185 
187 {
188  CRect rect;
189  getClientRect(rect);
190 
191  // Can the grip move?
192 
193  if ((rect.getWidth() > m_grip->getWidth()) &&
195  {
196  int32_t newValue = value;
197  int32_t maxValue = getPhysicalMaximumValueWithBitshift();
198 
199  // Limit to max/min values
200 
201  if (newValue > maxValue)
202  {
203  newValue = maxValue;
204  }
205 
206  if (newValue >> 16 < m_minimumValue)
207  {
208  newValue = m_minimumValue << 16;
209  }
210 
211  uint32_t scrollRatio = newValue / m_contentSize;
212  int32_t newGripX = m_gutterWidth * scrollRatio;
213  newGripX += newGripX & 0x8000;
214  newGripX >>= 16;
215  newGripX += rect.getX();
216 
217  m_grip->moveTo(newGripX, (rect.getHeight() - m_grip->getHeight()) >> 1);
218 
219  // Update stored value if necessary
220 
221  if (m_value != newValue)
222  {
223  m_value = newValue;
225  }
226  }
227 }
228 
229 /**
230  * Process events fired by the grip.
231  *
232  * @param e The event details.
233  */
234 
236 {
237  // Handle grip events
238 
239  if (e.getSource() == m_grip && e.getSource() != NULL)
240  {
241  int32_t newValue = getGripValue() >> 16;
242 
243  // Grip has moved - compare values and raise event if the
244  // value has changed. Compare using integer values rather
245  // than fixed-point.
246 
247  if (m_value >> 16 != newValue)
248  {
249  m_value = newValue << 16;
251  }
252  }
253 }
254 
255 /**
256  * Get the smallest value that the slider can move through when
257  * dragged.
258  *
259  * @return The smallest value that the slider can move through when
260  * dragged.
261  */
262 
264 {
265  // If the ratio of content to gutter is greater than or equal to one,
266  // the minimum step that the slider can represent will be that ratio.
267 
268  uint32_t gutterRatio = m_contentSize << 16 / m_gutterWidth;
269  gutterRatio += gutterRatio & 0x8000;
270  gutterRatio >>= 16;
271 
272  if (gutterRatio > 0)
273  {
274  return gutterRatio;
275  }
276 
277  return 1;
278 }
279 
280 /**
281  * Get the maximum possible value that the slider can represent. Useful when
282  * using the slider as a scrollbar, as the height of the grip prevents the full
283  * range of values being accessed (intentionally).
284  * The returned value is shifted left 16 places for more accuracy in fixed-point
285  * calculations.
286  *
287  * @return The maximum possible value that the slider can represent.
288  */
289 
291 {
292  uint32_t maxX = m_gutterWidth - m_grip->getWidth();
293  uint32_t scrollRatio = (maxX << 16) / m_gutterWidth;
294  int32_t value = (scrollRatio * m_contentSize);
295 
296  return value;
297 }
298 
299 /**
300  * Get the value represented by the top of the grip.
301  * return The value represented by the top of the grip.
302  */
303 
304 const int32_t CGlyphSliderHorizontal::getGripValue(void) const
305 {
306  // Calculate the current value represented by the top of the grip
307 
308  CRect rect;
309  getClientRect(rect);
310 
311  uint32_t gripPos = ((m_grip->getX() - getX()) - rect.getX());
312  uint32_t scrollRatio = (gripPos << 16) / m_gutterWidth;
313  int32_t value = (scrollRatio * m_contentSize);
314 
315  return value;
316 }
317 
318 /**
319  * Draw the area of this widget that falls within the clipping region.
320  * Called by the redraw() function to draw all visible regions.
321  *
322  * @param port The CGraphicsPort to draw to.
323  * @see redraw()
324  */
325 
327 {
328  int halfGripWidth = m_grip->getWidth() >> 1;
329  int halfBar = m_barThickness >> 1;
330  int halfHeight = getHeight() >> 1;
331  int width;
332  nxwidget_pixel_t color;
333 
334  // Fill in (erase) the region to the left and right of the fuel guage
335 
336  width =
337  m_grip->getX() > halfGripWidth ? halfGripWidth : m_grip->getX() - getX();
338  port->drawFilledRect(getX(), getY(), width, getHeight(),
340 
341  // Fill in (erase) the region above the "fuel gauge"
342 
343  port->drawFilledRect(getX(), getY(), getWidth(), halfHeight - halfBar,
345 
346  // Fill in the bar area to the left of the grip
347 
348  if (m_fill)
349  color = m_fillColor;
350  else
351  color = getBackgroundColor();
352  if (m_grip->getX() > halfGripWidth)
353  port->drawFilledRect(getX() + 1 + halfGripWidth,
354  getY() + halfHeight - halfBar + 1,
355  m_grip->getX() - getX() - halfGripWidth, m_barThickness - 2,
356  color);
357 
358  // Fill in bar the area to the right of the grip
359 
360  width = getWidth() - (m_grip->getX() - getX() + m_grip->getWidth()) - 1 -
361  halfGripWidth;
362  if (width > 0)
363  port->drawFilledRect(m_grip->getX() + m_grip->getWidth(),
364  getY() + halfHeight - halfBar + 1, width,
365  m_barThickness - 2, getBackgroundColor());
366 
367  // Fill in (erase) the area to right of the bar
368 
369  width = m_grip->getX() - getX() + m_grip->getWidth() <
370  getWidth() - halfGripWidth ? halfGripWidth : getWidth() - (m_grip->getX() -
371  getX() +
372  halfGripWidth);
373  if (width > 0)
374  port->drawFilledRect(getX() + getWidth() - halfGripWidth, getY(), width,
376 
377  // Fill in (erase) the region below the "fuel gauge"
378 
379  port->drawFilledRect(getX(), getY() + halfHeight + (m_barThickness - halfBar),
380  getWidth(), halfHeight - (m_barThickness - halfBar),
382 
383  // Now redraw the grip
384 
386  m_grip->redraw();
387 }
388 
389 /**
390  * Draw the area of this widget that falls within the clipping region.
391  * Called by the redraw() function to draw all visible regions.
392  *
393  * @param port The CGraphicsPort to draw to.
394  * @see redraw()
395  */
396 
398 {
399  // Stop drawing if the widget indicates it should not have an outline
400 
401  if (!isBorderless())
402  {
403  int y1, y2;
404  int x = getX();
405  int gripWidth = m_grip->getWidth();
406  int halfGripWidth = gripWidth >> 1;
407  int gripX = m_grip->getX();
408  int width;
409  nxwidget_pixel_t shine;
410  nxwidget_pixel_t shadow;
411 
412  y1 = getY() + (getHeight() >> 1) - (m_barThickness >> 1);
413  y2 = getY() + (getHeight() >> 1) + m_barThickness -
414  (m_barThickness >> 1) - 1;
415 
416  // To the Left of the grip. Only draw if the icon isn't covering the
417  // edge of the bar
418 
419  width = gripX - x - halfGripWidth;
420  if (width > 0)
421  {
422  // If we aren't filling, then don't draw a "raised" bar
423 
424  if (m_fill)
425  {
426  shine = getShineEdgeColor();
427  shadow = getShadowEdgeColor();
428  }
429  else
430  {
431  shadow = getShineEdgeColor();
432  shine = getShadowEdgeColor();
433  }
434 
435  port->drawHorizLine(x + halfGripWidth, y1, width, shine);
436  port->drawHorizLine(x + halfGripWidth, y2, width, shadow);
437  }
438 
439  // To the Right of the grip
440  // Only draw if the icon isn't covering the edge of the bar
441 
442  width = getWidth() - (gripX - x + gripWidth) - 1 - halfGripWidth;
443  if (width > 0)
444  {
445 
446  port->drawHorizLine(gripX + gripWidth, y1,
447  getWidth() - (gripX - x + gripWidth) - 1 -
448  halfGripWidth, getShadowEdgeColor());
449  port->drawHorizLine(gripX + gripWidth, y2,
450  getWidth() - (gripX - x + gripWidth) - 1 -
451  halfGripWidth, getShineEdgeColor());
452  }
453 
454  // Left edge
455 
456  if (gripX > x + halfGripWidth)
457  {
458  port->drawVertLine(x + halfGripWidth, y1, m_barThickness,
460  }
461 
462  // Right edge
463 
464  if (gripX + gripWidth < x + getWidth() - 1 - halfGripWidth)
465  {
466  port->drawVertLine(x + getWidth() - 1 - halfGripWidth,
468  }
469  }
470 }
471 
472 /**
473  * Resize the slider to the new dimensions.
474  *
475  * @param width The new width.
476  * @param height The new height.
477  */
478 
479 void CGlyphSliderHorizontal::onResize(nxgl_coord_t width, nxgl_coord_t height)
480 {
481  // Remember current values
482 
483  int32_t oldValue = m_value;
484  bool events = raisesEvents();
485 
486  // Disable event raising
487 
488  setRaisesEvents(false);
489  // resizeGrip();
490 
491  // Set back to current value
492 
493  setValue(oldValue);
494 
495  // Reset event raising
496 
497  setRaisesEvents(events);
498 }
499 
500 /**
501  * Moves the grip towards the mouse.
502  *
503  * @param x The x coordinate of the click.
504  * @param y The y coordinate of the click.
505  */
506 
507 void CGlyphSliderHorizontal::onClick(nxgl_coord_t x, nxgl_coord_t y)
508 {
509  // Which way should the grip move?
510 
511  if (x > m_grip->getX())
512  {
513  // Move grip right
514 
516  }
517  else
518  {
519  // Move grip left
520 
522  }
523 
524  redraw();
525 }
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
void drawVertLine(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t height, nxgl_mxpixel_t color)
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 setRaisesEvents(bool raises)
Definition: cnxwidget.hxx:887
nxgl_coord_t getWidth(void) const
Definition: crect.hxx:181
nxgl_coord_t getX(void) const
Definition: crect.hxx:160
CGlyphSliderHorizontal(CWidgetControl *control, nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t thickness, IBitmap *gripBitmap, nxwidget_pixel_t fillColor, bool fill=true)
bool raisesEvents(void) const
Definition: cnxwidget.hxx:898
nxgl_mxpixel_t getBackgroundColor(void) const
Definition: cnxwidget.hxx:716
virtual void drawBorder(CGraphicsPort *port)
void drawHorizLine(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_mxpixel_t color)
virtual const nxgl_coord_t getHeight(void) const =0
uint8_t nxwidget_pixel_t
Definition: nxconfig.hxx:442
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
nxgl_coord_t getHeight(void) const
Definition: cnxwidget.hxx:593
void setHeight(nxgl_coord_t height)
Definition: crect.hxx:261
void getClientRect(CRect &rect) const
Definition: cnxwidget.cxx:421
virtual void handleDragEvent(const CWidgetEventArgs &e)
void addWidgetEventHandler(CWidgetEventHandler *eventHandler)
Definition: cnxwidget.hxx:854
void drawFilledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t color)
nxgl_mxpixel_t getShineEdgeColor(void) const
Definition: cnxwidget.hxx:738
int32_t getPhysicalMaximumValueWithBitshift(void) const
void enableDrawing(void)
Definition: cnxwidget.hxx:917
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
void addWidget(CNxWidget *widget)
Definition: cnxwidget.cxx:1293
virtual const nxgl_coord_t getWidth(void) const =0
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)
nxgl_coord_t getY(void) const
Definition: cnxwidget.cxx:261
nxgl_coord_t getWidth(void) const
Definition: cnxwidget.hxx:582
void setValueWithBitshift(const int32_t value)
nxgl_coord_t getX(void) const
Definition: cnxwidget.cxx:245
void setBorderless(bool isBorderless)
Definition: cnxwidget.cxx:461
virtual void drawContents(CGraphicsPort *port)