NXWidgets  1.19
csliderhorizontalgrip.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/src/csliderhorizontalgrip.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 "nxconfig.hxx"
80 #include "cwidgetcontrol.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 grip, relative to its parent.
99  * @param y The y coordinate of the grip, relative to its parent.
100  * @param width The width of the grip.
101  * @param height The height of the grip.
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 }
110 
111 /**
112  * Draw the area of this widget that falls within the clipping region.
113  * Called by the redraw() function to draw all visible regions.
114  *
115  * @param port The CGraphicsPort to draw to.
116  * @see redraw()
117  */
118 
120 {
121  CRect rect;
122  getRect(rect);
123 
124  nxwidget_pixel_t color;
125  if (!m_flags.clicked)
126  {
127  color = getSelectedBackgroundColor();
128  }
129  else
130  {
131  color = getHighlightColor();
132  }
133 
134  port->drawFilledRect(rect.getX(), rect.getY(),
135  rect.getWidth(), rect.getHeight(),
136  color);
137 }
138 
139 /**
140  * Draw the area of this widget that falls within the clipping region.
141  * Called by the redraw() function to draw all visible regions.
142  *
143  * @param port The CGraphicsPort to draw to.
144  * @see redraw()
145  */
146 
148 {
149  // Stop drawing if the widget indicates it should not have an outline
150 
151  if (!isBorderless())
152  {
153  if (isClicked())
154  {
155  port->drawBevelledRect(getX(), getY(), getWidth(), getHeight(),
157  }
158  else
159  {
160  port->drawBevelledRect(getX(), getY(), getWidth(), getHeight(),
162  }
163  }
164 }
165 
166 /**
167  * Starts dragging the grip and redraws it.
168  *
169  * @param x The x coordinate of the click.
170  * @param y The y coordinate of the click.
171  */
172 
173 void CSliderHorizontalGrip::onClick(nxgl_coord_t x, nxgl_coord_t y)
174 {
175  startDragging(x, y);
176  redraw();
177 }
178 
179 /**
180  * Redraws the grip.
181  *
182  * @param x The x coordinate of the mouse.
183  * @param y The y coordinate of the mouse.
184  */
185 
186 void CSliderHorizontalGrip::onRelease(nxgl_coord_t x, nxgl_coord_t y)
187 {
188  redraw();
189 }
190 
191 /**
192  * Redraws the grip.
193  *
194  * @param x The x coordinate of the mouse.
195  * @param y The y coordinate of the mouse.
196  */
197 
198 void CSliderHorizontalGrip::onReleaseOutside(nxgl_coord_t x, nxgl_coord_t y)
199 {
200  redraw();
201 }
202 
203 /**
204  * Moves the grip to follow the mouse.
205  *
206  * @param x The x coordinate of the mouse.
207  * @param y The y coordinate of the mouse.
208  * @param vX The horizontal distance of the drag.
209  * @param vY The vertical distance of the drag.
210  */
211 
212 void CSliderHorizontalGrip::onDrag(nxgl_coord_t x, nxgl_coord_t y,
213  nxgl_coord_t vX, nxgl_coord_t vY)
214 {
215  // Work out where we're moving to
216 
217  nxgl_coord_t destX = x - m_grabPointX - m_parent->getX();
218 
219  // Do we need to move?
220 
221  if (destX != m_rect.getX())
222  {
223  // Get parent rect
224 
225  CRect rect;
226  m_parent->getClientRect(rect);
227 
228  // Prevent grip from moving outside parent
229 
230  if (destX < rect.getX())
231  {
232  destX = rect.getX();
233  }
234  else
235  {
236  if (destX + getWidth() > rect.getWidth() + rect.getX())
237  {
238  destX = (rect.getWidth() + rect.getX()) - getWidth() ;
239  }
240  }
241 
242  // Move to new location
243 
244  moveTo(destX, rect.getY());
245  }
246 }
bool isClicked(void) const
Definition: cnxwidget.hxx:560
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
CSliderHorizontalGrip(const CSliderHorizontalGrip &sliderHorizontalGrip)
void getRect(CRect &rect) const
Definition: cnxwidget.cxx:448
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
virtual void onDrag(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t vX, nxgl_coord_t vY)
nxgl_coord_t m_grabPointX
Definition: cnxwidget.hxx:176
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 getClientRect(CRect &rect) const
Definition: cnxwidget.cxx:421
virtual void onRelease(nxgl_coord_t x, nxgl_coord_t y)
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)
nxgl_mxpixel_t getShineEdgeColor(void) const
Definition: cnxwidget.hxx:738
virtual void onReleaseOutside(nxgl_coord_t x, nxgl_coord_t y)
nxgl_mxpixel_t getHighlightColor(void) const
Definition: cnxwidget.hxx:760
virtual void drawContents(CGraphicsPort *port)
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)
void startDragging(nxgl_coord_t x, nxgl_coord_t y)
Definition: cnxwidget.cxx:1542
CNxWidget * m_parent
Definition: cnxwidget.hxx:202