NXWidgets  1.19
cglyphsliderhorizontalgrip.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/src/cglyphsliderhorizontalgrip.cxx
3  *
4  * Copyright (C) 2013 Ken Pettit. All rights reserved.
5  * Author: Ken Pettit <pettitkd@gmail.com>
6  *
7  * Mostly copied from CSliderHorizontalGrip written by Gregory Nutt
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
20  * me be used to endorse or promote products derived from this software
21  * without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
30  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  ****************************************************************************
37  *
38  * Portions of this package derive from Woopsi (http://woopsi.org/) and
39  * portions are original efforts. It is difficult to determine at this
40  * point what parts are original efforts and which parts derive from Woopsi.
41  * However, in any event, the work of Antony Dzeryn will be acknowledged
42  * in most NxWidget files. Thanks Antony!
43  *
44  * Copyright (c) 2007-2011, Antony Dzeryn
45  * All rights reserved.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions are met:
49  *
50  * * Redistributions of source code must retain the above copyright
51  * notice, this list of conditions and the following disclaimer.
52  * * Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in the
54  * documentation and/or other materials provided with the distribution.
55  * * Neither the names "Woopsi", "Simian Zombie" nor the
56  * names of its contributors may be used to endorse or promote products
57  * derived from this software without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
60  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
61  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
62  * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
63  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
64  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
65  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
66  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
68  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69  *
70  ****************************************************************************/
71 
72 /****************************************************************************
73  * Included Files
74  ****************************************************************************/
75 
77 #include "ibitmap.hxx"
78 
79 /****************************************************************************
80  * Pre-Processor Definitions
81  ****************************************************************************/
82 
83 /****************************************************************************
84  * Method Implementations
85  ****************************************************************************/
86 
87 using namespace NXWidgets;
88 
89 /**
90  * Constructor.
91  *
92  * @param pWidgetControl The controlling widget for the display
93  * @param x The x coordinate of the grip, relative to its parent.
94  * @param y The y coordinate of the grip, relative to its parent.
95  * @param width The width of the grip.
96  * @param height The height of the grip.
97  */
98 
100  nxgl_coord_t x, nxgl_coord_t y,
101  nxgl_coord_t width, nxgl_coord_t height,
102  IBitmap *pGripBitmap)
103 : CImage(pWidgetControl, 0, (height>>1) - (pGripBitmap->getHeight() >> 1),
104  pGripBitmap->getWidth(), pGripBitmap->getHeight(),
105  pGripBitmap, 0)
106 {
107  // Make the widget draggable
108 
109  setDraggable(true);
110 }
111 
112 /**
113  * Resize the slider to the new dimensions.
114  *
115  * @param width The new width.
116  * @param height The new height.
117  */
118 
119 void CGlyphSliderHorizontalGrip::onResize(nxgl_coord_t width, nxgl_coord_t height)
120 {
121 }
122 
123 /**
124  * Starts dragging the grip and redraws it.
125  *
126  * @param x The x coordinate of the click.
127  * @param y The y coordinate of the click.
128  */
129 
130 void CGlyphSliderHorizontalGrip::onClick(nxgl_coord_t x, nxgl_coord_t y)
131 {
132  startDragging(x, y);
133  redraw();
134 }
135 
136 /**
137  * Redraws the grip.
138  *
139  * @param x The x coordinate of the mouse.
140  * @param y The y coordinate of the mouse.
141  */
142 
143 void CGlyphSliderHorizontalGrip::onRelease(nxgl_coord_t x, nxgl_coord_t y)
144 {
145  redraw();
146 }
147 
148 /**
149  * Redraws the grip.
150  *
151  * @param x The x coordinate of the mouse.
152  * @param y The y coordinate of the mouse.
153  */
154 
155 void CGlyphSliderHorizontalGrip::onReleaseOutside(nxgl_coord_t x, nxgl_coord_t y)
156 {
157  redraw();
158 }
159 
160 /**
161  * Moves the grip to follow the mouse.
162  *
163  * @param x The x coordinate of the mouse.
164  * @param y The y coordinate of the mouse.
165  * @param vX The horizontal distance of the drag.
166  * @param vY The vertical distance of the drag.
167  */
168 
169 void CGlyphSliderHorizontalGrip::onDrag(nxgl_coord_t x, nxgl_coord_t y,
170  nxgl_coord_t vX, nxgl_coord_t vY)
171 {
172  // Work out where we're moving to
173 
174  nxgl_coord_t destX = x - m_grabPointX - m_parent->getX();
175 
176  // Do we need to move?
177 
178  if (destX != m_rect.getX())
179  {
180  // Get parent rect
181 
182  CRect rect;
183  m_parent->getClientRect(rect);
184 
185  // Prevent grip from moving outside parent
186 
187  if (destX < rect.getX())
188  {
189  destX = rect.getX();
190  }
191  else
192  {
193  if (destX + getWidth() > rect.getWidth() + rect.getX())
194  {
195  destX = (rect.getWidth() + rect.getX()) - getWidth() ;
196  }
197  }
198 
199  // Move to new location
200 
201  moveTo(destX, (rect.getHeight() - getHeight()) >> 1);
202  m_parent->redraw();
203  }
204 }
nxgl_coord_t getHeight(void) const
Definition: crect.hxx:204
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
bool moveTo(nxgl_coord_t x, nxgl_coord_t y)
Definition: cnxwidget.cxx:990
nxgl_coord_t getWidth(void) const
Definition: crect.hxx:181
nxgl_coord_t getX(void) const
Definition: crect.hxx:160
nxgl_coord_t m_grabPointX
Definition: cnxwidget.hxx:176
virtual void onResize(nxgl_coord_t width, nxgl_coord_t height)
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)
CGlyphSliderHorizontalGrip(CWidgetControl *pWidgetControl, nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, IBitmap *pGripBitmap)
void setDraggable(bool isDraggable)
Definition: cnxwidget.hxx:820
nxgl_coord_t getWidth(void) const
Definition: cnxwidget.hxx:582
nxgl_coord_t getX(void) const
Definition: cnxwidget.cxx:245
virtual void onReleaseOutside(nxgl_coord_t x, nxgl_coord_t y)
virtual void onDrag(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t vX, nxgl_coord_t vY)
void startDragging(nxgl_coord_t x, nxgl_coord_t y)
Definition: cnxwidget.cxx:1542
CNxWidget * m_parent
Definition: cnxwidget.hxx:202