cslidervertical.hxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /****************************************************************************
  2. * apps/include/graphics/nxwidgets/cslidervertical.hxx
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. *
  22. * Portions of this package derive from Woopsi (http://woopsi.org/) and
  23. * portions are original efforts. It is difficult to determine at this
  24. * point what parts are original efforts and which parts derive from Woopsi.
  25. * However, in any event, the work of Antony Dzeryn will be acknowledged
  26. * in most NxWidget files. Thanks Antony!
  27. *
  28. * Copyright (c) 2007-2011, Antony Dzeryn
  29. * All rights reserved.
  30. *
  31. * Redistribution and use in source and binary forms, with or without
  32. * modification, are permitted provided that the following conditions are met:
  33. *
  34. * * Redistributions of source code must retain the above copyright
  35. * notice, this list of conditions and the following disclaimer.
  36. * * Redistributions in binary form must reproduce the above copyright
  37. * notice, this list of conditions and the following disclaimer in the
  38. * documentation and/or other materials provided with the distribution.
  39. * * Neither the names "Woopsi", "Simian Zombie" nor the
  40. * names of its contributors may be used to endorse or promote products
  41. * derived from this software without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
  44. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  45. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  46. * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
  47. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  48. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  49. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  50. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  51. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  52. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53. *
  54. ****************************************************************************/
  55. #ifndef __APPS_INCLUDE_GRAPHICS_NXWIDGETS_CSLIDERVERTICAL_HXX
  56. #define __APPS_INCLUDE_GRAPHICS_NXWIDGETS_CSLIDERVERTICAL_HXX
  57. /****************************************************************************
  58. * Included Files
  59. ****************************************************************************/
  60. #include <nuttx/config.h>
  61. #include <stdint.h>
  62. #include <stdbool.h>
  63. #include <nuttx/nx/nxglib.h>
  64. #include "graphics/nxwidgets/cnxwidget.hxx"
  65. #include "graphics/nxwidgets/cwidgeteventhandler.hxx"
  66. #include "graphics/nxwidgets/islider.hxx"
  67. /****************************************************************************
  68. * Pre-Processor Definitions
  69. ****************************************************************************/
  70. /****************************************************************************
  71. * Implementation Classes
  72. ****************************************************************************/
  73. #if defined(__cplusplus)
  74. namespace NXWidgets
  75. {
  76. class CSliderVerticalGrip;
  77. class CWidgetControl;
  78. /**
  79. * Widget providing a sliding "grip" that can be moved up and
  80. * down the "gutter". Essentially a scrollbar, but more
  81. * generic.
  82. */
  83. class CSliderVertical : public ISlider, public CNxWidget, public CWidgetEventHandler
  84. {
  85. protected:
  86. CSliderVerticalGrip* m_grip; /**< Pointer to the grip. */
  87. int m_minimumValue; /**< Minimum value that the grip can represent. */
  88. int m_maximumValue; /**< Maximum value that the grip can represent. */
  89. int32_t m_value; /**< Current value of the slider. */
  90. nxgl_coord_t m_minimumGripHeight; /**< Smallest height that the grip can become. */
  91. nxgl_coord_t m_pageSize; /**< Value of a page of data, used when clicking
  92. the gutter */
  93. int32_t m_gutterHeight; /**< Height of the gutter, taking into account
  94. any adjustments made to the height of the grip. */
  95. uint32_t m_contentSize; /**< Number of values in the min/max range. */
  96. /**
  97. * Get the maximum possible value that the slider can represent. Useful when
  98. * using the slider as a scrollbar, as the height of the grip prevents the full
  99. * range of values being accessed (intentionally).
  100. * The returned value is shifted left 16 places for more accuracy in fixed-point
  101. * calculations.
  102. *
  103. * @return The maximum possible value that the slider can represent.
  104. */
  105. int32_t getPhysicalMaximumValueWithBitshift(void) const;
  106. /**
  107. * Get the value represented by the top of the grip. The value is
  108. * bitshifted left 16 places for accuracy.
  109. * return The value represented by the top of the grip.
  110. */
  111. const int32_t getGripValue(void) const;
  112. /**
  113. * Draw the area of this widget that falls within the clipping region.
  114. * Called by the redraw() function to draw all visible regions.
  115. *
  116. * @param port The CGraphicsPort to draw to.
  117. * @see redraw()
  118. */
  119. virtual void drawContents(CGraphicsPort *port);
  120. /**
  121. * Draw the area of this widget that falls within the clipping region.
  122. * Called by the redraw() function to draw all visible regions.
  123. *
  124. * @param port The CGraphicsPort to draw to.
  125. * @see redraw()
  126. */
  127. virtual void drawBorder(CGraphicsPort *port);
  128. /**
  129. * Resize the slider to the new dimensions.
  130. *
  131. * @param width The new width.
  132. * @param height The new height.
  133. */
  134. virtual void onResize(nxgl_coord_t width, nxgl_coord_t height);
  135. /**
  136. * Moves the grip towards the mouse.
  137. *
  138. * @param x The x coordinate of the click.
  139. * @param y The y coordinate of the click.
  140. */
  141. virtual void onClick(nxgl_coord_t x, nxgl_coord_t y);
  142. /**
  143. * Copy constructor is protected to prevent usage.
  144. */
  145. inline CSliderVertical(const CSliderVertical &sliderVertical)
  146. : CNxWidget(sliderVertical) { }
  147. /**
  148. * Resize and redraw the grip.
  149. */
  150. void resizeGrip();
  151. public:
  152. /**
  153. * Constructor.
  154. *
  155. * @param pWidgetControl The controlling widget for the display
  156. * @param x The x coordinate of the slider, relative to its parent.
  157. * @param y The y coordinate of the slider, relative to its parent.
  158. * @param width The width of the slider.
  159. * @param height The height of the slider.
  160. */
  161. CSliderVertical(CWidgetControl *pWidgetControl,
  162. nxgl_coord_t x, nxgl_coord_t y,
  163. nxgl_coord_t width, nxgl_coord_t height);
  164. /**
  165. * Destructor.
  166. */
  167. virtual inline ~CSliderVertical(void) { }
  168. /**
  169. * Get the smallest value that the slider can represent.
  170. *
  171. * @return The smallest value.
  172. */
  173. inline const int getMinimumValue(void) const
  174. {
  175. return m_minimumValue;
  176. }
  177. /**
  178. * Get the largest value that the slider can represent.
  179. *
  180. * @return The largest value.
  181. */
  182. inline const int getMaximumValue(void) const
  183. {
  184. return m_maximumValue;
  185. }
  186. /**
  187. * Get the current value of the slider.
  188. * return The current slider value.
  189. */
  190. inline const int getValue(void) const
  191. {
  192. return m_value >> 16;
  193. }
  194. /**
  195. * Get the value represented by the height of the grip.
  196. * For sliders, this would typically be 1 (so each new
  197. * grip position is worth 1). For scrollbars, this
  198. * would be the height of the scrolling widget.
  199. *
  200. * @return The page size.
  201. */
  202. inline const nxgl_coord_t getPageSize(void) const
  203. {
  204. return m_pageSize;
  205. }
  206. /**
  207. * Set the smallest value that the slider can represent.
  208. *
  209. * @param value The smallest value.
  210. */
  211. inline void setMinimumValue(const int value)
  212. {
  213. m_minimumValue = value;
  214. m_contentSize = m_maximumValue - m_minimumValue + 1;
  215. resizeGrip();
  216. }
  217. /**
  218. * Set the largest value that the slider can represent.
  219. *
  220. * @param value The largest value.
  221. */
  222. inline void setMaximumValue(const int value)
  223. {
  224. m_maximumValue = value;
  225. m_contentSize = m_maximumValue - m_minimumValue + 1;
  226. resizeGrip();
  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. void setValue(const int value);
  235. /**
  236. * Set the value that of the slider. This will reposition and redraw
  237. * the grip. The supplied value should be shifted left 16 places.
  238. * This ensures greater accuracy than the standard setValue() method if
  239. * the slider is being used as a scrollbar.
  240. *
  241. * @param value The new value.
  242. */
  243. void setValueWithBitshift(const int32_t value);
  244. /**
  245. * Set the page size represented by the grip.
  246. *
  247. * @param pageSize The page size.
  248. * @see getPageSize().
  249. */
  250. inline void setPageSize(const nxgl_coord_t pageSize)
  251. {
  252. m_pageSize = pageSize;
  253. resizeGrip();
  254. }
  255. /**
  256. * Process events fired by the grip.
  257. *
  258. * @param e The event details.
  259. */
  260. virtual void handleDragEvent(const CWidgetEventArgs &e);
  261. /**
  262. * Get the smallest value that the slider can move through when
  263. * dragged.
  264. *
  265. * @return The smallest value that the slider can move through when
  266. * dragged.
  267. */
  268. nxgl_coord_t getMinimumStep(void) const;
  269. };
  270. }
  271. #endif // __cplusplus
  272. #endif // __APPS_INCLUDE_GRAPHICS_NXWIDGETS_CSLIDERVERTICAL_HXX