NXWidgets  1.19
cscrollbarpanel.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/src/cscrollbarpanel.cxx
3  *
4  * Copyright (C) 2012 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 "cscrollbarpanel.hxx"
80 #include "cbutton.hxx"
81 
82 /****************************************************************************
83  * Pre-Processor Definitions
84  ****************************************************************************/
85 
86 /****************************************************************************
87  * Method Implementations
88  ****************************************************************************/
89 
90 using namespace NXWidgets;
91 
92 /**
93  * Constructor.
94  *
95  * @param pWidgetControl The widget control for the display.
96  * @param x The x coordinate of the widget.
97  * @param y The y coordinate of the widget.
98  * @param width The width of the widget.
99  * @param height The height of the widget.
100  * @param flags The usual widget flags.
101  * @param style The style that the widget should use. If this is not
102  * specified, the widget will use the values stored in the global
103  * g_defaultWidgetStyle object. The widget will copy the properties of
104  * the style into its own internal style object.
105  */
106 
108  nxgl_coord_t x, nxgl_coord_t y,
109  nxgl_coord_t width, nxgl_coord_t height,
110  uint32_t flags, CWidgetStyle *style)
111 : CNxWidget(pWidgetControl, x, y, width, height, flags, style)
112 {
113  m_scrollbarWidth = 10;
114  m_scrollbarHeight = 10;
115  m_hasVerticalScrollbar = true;
117 
118  m_borderSize.top = 0;
119  m_borderSize.right = 0;
120  m_borderSize.left = 0;
121  m_borderSize.bottom = 0;
122  m_flags.borderless = true;
123 
124  m_widgetControl = pWidgetControl;
125  m_panel = (CScrollingPanel *)NULL;
128 
129  buildUI();
130 }
131 
132 /**
133  * Scroll the panel by the specified amounts.
134  *
135  * @param dx The horizontal distance to scroll.
136  * @param dy The vertical distance to scroll.
137  */
138 
139 void CScrollbarPanel::scroll(int32_t dx, int32_t dy)
140 {
141  m_panel->scroll(dx, dy);
142 }
143 
144 /**
145  * Reposition the panel's scrolling region to the specified coordinates.
146  *
147  * @param x The new x coordinate of the scrolling region.
148  * @param y The new y coordinate of the scrolling region.
149  */
150 
151 void CScrollbarPanel::jump(int32_t x, int32_t y)
152 {
153  m_panel->jump(x, y);
154 }
155 
156 /**
157  * Set whether or not horizontal scrolling is allowed.
158  *
159  * @param allow True to allow horizontal scrolling; false to deny it.
160  */
161 
163 {
165 
167  {
168  if (allow)
169  {
171  }
172  else
173  {
175  }
176  }
177 }
178 
179 /**
180  * Set whether or not horizontal scrolling is allowed.
181  *
182  * @param allow True to allow horizontal scrolling; false to deny it.
183  */
184 
186 {
188 
190  {
191  if (allow)
192  {
194  }
195  else
196  {
198  }
199  }
200 }
201 
202 /**
203  * Sets the width of the virtual canvas.
204  *
205  * @param width The width of the virtual canvas.
206  */
207 
208 void CScrollbarPanel::setCanvasWidth(const int32_t width)
209 {
210  m_panel->setCanvasWidth(width);
212 }
213 
214 /**
215  * Sets the height of the virtual canvas.
216  *
217  * @param height The height of the virtual canvas.
218  */
219 
220 void CScrollbarPanel::setCanvasHeight(const int32_t height)
221 {
222  m_panel->setCanvasHeight(height);
224 }
225 
226 /**
227  * Returns true if vertical scrolling is allowed.
228  *
229  * @return True if vertical scrolling is allowed.
230  */
231 
233 {
234  return m_panel->allowsVerticalScroll();
235 }
236 
237 /**
238  * Returns true if horizontal scrolling is allowed.
239  *
240  * @return True if horizontal scrolling is allowed.
241  */
242 
244 {
246 }
247 
248 /**
249  * Gets the x coordinate of the virtual canvas.
250  *
251  * @return The x coordinate of the virtual canvas.
252  */
253 
254 const int32_t CScrollbarPanel::getCanvasX(void) const
255 {
256  return m_panel->getCanvasX();
257 }
258 
259 /**
260  * Gets the y coordinate of the virtual canvas.
261  *
262  * @return The y coordinate of the virtual canvas.
263  */
264 
265 const int32_t CScrollbarPanel::getCanvasY(void) const
266 {
267  return m_panel->getCanvasY();
268 }
269 
270 /**
271  * Gets the width of the virtual canvas.
272  *
273  * @return The width of the virtual canvas.
274  */
275 
276 const int32_t CScrollbarPanel::getCanvasWidth(void) const
277 {
278  return m_panel->getCanvasWidth();
279 }
280 
281 /**
282  * Gets the height of the virtual canvas.
283  *
284  * @return The height of the virtual canvas.
285  */
286 
287 const int32_t CScrollbarPanel::getCanvasHeight(void) const
288 {
289  return m_panel->getCanvasHeight();
290 }
291 
292 /**
293  * Handle a widget scroll event.
294  *
295  * @param e The event data.
296  */
297 
299 {
300  if (e.getSource() != NULL)
301  {
302  if (e.getSource() == m_panel)
303  {
304  if (m_scrollbarVertical != NULL)
305  {
309  }
310  else if (m_scrollbarHorizontal != NULL)
311  {
315  }
316  }
317  }
318 }
319 
320 /**
321  * Handle a widget value change event.
322  *
323  * @param e The event data.
324  */
325 
327 {
328  if (e.getSource() != NULL)
329  {
330  if (e.getSource() == m_scrollbarVertical)
331  {
332  if (m_panel != NULL)
333  {
334  m_panel->setRaisesEvents(false);
336  m_panel->setRaisesEvents(true);
337  }
338  }
339  else if (e.getSource() == m_scrollbarHorizontal)
340  {
341  if (m_panel != NULL)
342  {
343  m_panel->setRaisesEvents(false);
345  m_panel->setRaisesEvents(true);
346  }
347  }
348  }
349 }
350 
351 /**
352  * Creates the child widgets.
353  */
354 
356 {
357  CRect rect;
358  getClientRect(rect);
359 
360  nxgl_coord_t panelWidth = rect.getWidth();
362  {
363  panelWidth -= m_scrollbarWidth;
364  }
365 
366  nxgl_coord_t panelHeight = rect.getHeight();
368  {
369  panelHeight -= m_scrollbarHeight;
370  }
371 
372  m_panel = new CScrollingPanel(m_widgetControl, rect.getX(), rect.getY(),
373  panelWidth, panelHeight, 0, &m_style);
375 
376  CRect panelRect;
377  m_panel->getClientRect(panelRect);
378 
379  // Disable content scrolling by default, as the panel cannot be drawn to
380  // without some additional programmer effort
381 
382  m_panel->setContentScrolled(false);
383 
384  // Adjust scrollbar dimensions based on scrollbar visibility
385 
386  nxgl_coord_t verticalScrollHeight = rect.getHeight();
388  {
389  verticalScrollHeight -= m_scrollbarHeight;
390  }
391 
392  nxgl_coord_t horizontalScrollWidth = rect.getWidth();
394  {
395  horizontalScrollWidth -= m_scrollbarWidth;
396  }
397 
399  {
402  rect.getX(), rect.getHeight() - m_scrollbarHeight,
403  horizontalScrollWidth, m_scrollbarHeight,
404  &m_style);
410  }
411 
413  {
416  rect.getWidth() - m_scrollbarWidth,
417  rect.getY(), m_scrollbarWidth,
418  verticalScrollHeight, &m_style);
424  }
425 }
426 
427 /**
428  * Draw the area of this widget that falls within the clipping region.
429  * Called by the redraw() function to draw all visible regions.
430  *
431  * @param port The CGraphicsPort to draw to.
432  * @see redraw()
433  */
434 
436 {
438 }
void setMinimumValue(const int value)
bool allowsVerticalScroll(void) const
WidgetBorderSize m_borderSize
Definition: cnxwidget.hxx:208
virtual bool allowsHorizontalScroll(void) const
const T & getSource(void) const
Definition: teventargs.hxx:127
CScrollbarPanel(const CScrollbarPanel &scrollbarPanel)
virtual void setCanvasHeight(const int32_t height)
virtual const int32_t getCanvasHeight(void) const
nxgl_coord_t getHeight(void) const
Definition: crect.hxx:204
virtual void setCanvasWidth(const int32_t width)
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
virtual void setAllowsHorizontalScroll(bool allow)
nxgl_mxpixel_t getBackgroundColor(void) const
Definition: cnxwidget.hxx:716
void setContentScrolled(bool scrolled)
CWidgetControl * m_widgetControl
virtual void jump(int32_t x, int32_t y)
virtual const int32_t getCanvasWidth(void) const
bool allowsHorizontalScroll(void) const
nxgl_coord_t getHeight(void) const
Definition: cnxwidget.hxx:593
void setPageSize(const nxgl_coord_t pageSize)
void getClientRect(CRect &rect) const
Definition: cnxwidget.cxx:421
virtual const int32_t getCanvasHeight(void) const
void addWidgetEventHandler(CWidgetEventHandler *eventHandler)
Definition: cnxwidget.hxx:854
CScrollbarHorizontal * m_scrollbarHorizontal
virtual void setAllowsVerticalScroll(bool allow)
virtual const int32_t getCanvasWidth(void) const
CScrollbarVertical * m_scrollbarVertical
virtual void setCanvasHeight(const int32_t height)
void drawFilledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t color)
virtual const int32_t getCanvasX(void) const
void setAllowsHorizontalScroll(bool allow)
void handleScrollEvent(const CWidgetEventArgs &e)
virtual const int32_t getCanvasY(void) const
virtual void scroll(int32_t dx, int32_t dy)
virtual const int32_t getCanvasX(void) const
virtual void drawContents(CGraphicsPort *port)
virtual const int32_t getCanvasY(void) const
void addWidget(CNxWidget *widget)
Definition: cnxwidget.cxx:1293
virtual void scroll(int32_t dx, int32_t dy)
void handleValueChangeEvent(const CWidgetEventArgs &e)
void setAllowsVerticalScroll(bool allow)
nxgl_coord_t getWidth(void) const
Definition: cnxwidget.hxx:582
void setMaximumValue(const int value)
virtual bool allowsVerticalScroll(void) const
nxgl_coord_t getY(void) const
Definition: crect.hxx:170
CWidgetStyle m_style
Definition: cnxwidget.hxx:183
virtual void setCanvasWidth(const int32_t width)
virtual void jump(int32_t x, int32_t y)
void setPageSize(const nxgl_coord_t pageSize)