NXWidgets  1.19
cglyphbutton.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/src/cglyphbutton.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 <sys/types.h>
77 #include <stdint.h>
78 #include <stdbool.h>
79 
80 #include <nuttx/nx/nxglib.h>
81 
82 #include "nxconfig.hxx"
83 #include "cglyphbutton.hxx"
84 #include "cgraphicsport.hxx"
85 #include "cbitmap.hxx"
86 #include "singletons.hxx"
87 
88 /****************************************************************************
89  * Pre-Processor Definitions
90  ****************************************************************************/
91 
92 /****************************************************************************
93  * CButton Method Implementations
94  ****************************************************************************/
95 
96 using namespace NXWidgets;
97 
98 /**
99  * Constructor.
100  *
101  * @param pWidgetControl The widget control for the display.
102  * @param x The x coordinate of the button.
103  * @param y The y coordinate of the button.
104  * @param width The width of the button.
105  * @param height The height of the button.
106  * @param normalGlyph Glyph to display when unclicked.
107  * @param clickedGlyph Glyph to display when clicked.
108  * @param bitmapX The x coordinate at which the bitmaps will be drawn.
109  * @param bitmapY The y coordinate at which the bitmaps will be drawn.
110  * @param style The style that the button should use. If this is not
111  * specified, the button will use the values stored in the global
112  * g_defaultWidgetStyle object. The button will copy the properties of
113  * the style into its own internal style object.
114  */
115 
117  nxgl_coord_t x, nxgl_coord_t y,
118  nxgl_coord_t width, nxgl_coord_t height,
119  nxgl_coord_t bitmapX, nxgl_coord_t bitmapY,
120  FAR const struct SBitmap *normalGlyph,
121  FAR const struct SBitmap *clickedGlyph,
122  CWidgetStyle *style)
123 : CNxWidget(pWidgetControl, x, y, width, height, 0, style)
124 {
125  // The border should be set to the minimum so that the bitmap
126  // image can extend all the way to the border.
127 
128  m_borderSize.top = 1;
129  m_borderSize.right = 1;
130  m_borderSize.bottom = 1;
131  m_borderSize.left = 1;
132 
133  // Save the bitmap state data
134 
135  m_bitmapX = bitmapX;
136  m_bitmapY = bitmapY;
137  m_bitmapNormal = normalGlyph;
138  m_bitmapClicked = clickedGlyph;
139 }
140 
141 /**
142  * Insert the dimensions that this widget wants to have into the rect
143  * passed in as a parameter. All coordinates are relative to the widget's
144  * parent.
145  * @param rect Reference to a rect to populate with data.
146  */
147 
149 {
150  nxgl_coord_t width = m_bitmapNormal->width;
151  nxgl_coord_t height = m_bitmapNormal->height;
152 
153  if (!m_flags.borderless)
154  {
156  height = m_borderSize.top + m_borderSize.bottom;
157  }
158 
159  rect.setX(m_rect.getX());
160  rect.setY(m_rect.getY());
161  rect.setWidth(width);
162  rect.setHeight(height);
163 }
164 
165 /**
166  * Draw the area of this widget that falls within the clipping region.
167  * Called by the redraw() function to draw all visible regions.
168  *
169  * @param port The CGraphicsPort to draw to.
170  * @see redraw()
171  */
172 
174 {
175  // Get the drawable region within the Label
176 
177  CRect rect;
178  getRect(rect);
179 
180  // Pick the glyph to show
181 
182  FAR const struct SBitmap *bitmap;
183  if (m_flags.clicked)
184  {
185  bitmap = m_bitmapClicked;
186  }
187  else
188  {
189  bitmap = m_bitmapNormal;
190  }
191 
192  // Then draw it -- in greyscale if not enabled
193 
194  if (isEnabled())
195  {
196  port->drawBitmap(rect.getX() + m_bitmapX, rect.getY() + m_bitmapY,
197  bitmap->width, bitmap->height,
198  bitmap, 0, 0,
199  CONFIG_NXWIDGETS_TRANSPARENT_COLOR);
200  }
201  else
202  {
203  port->drawBitmapGreyScale(rect.getX() + m_bitmapX, rect.getY() + m_bitmapY,
204  bitmap->width, bitmap->height,
205  bitmap, 0, 0);
206  }
207 }
208 
209 /**
210  * Draw the area of this widget that falls within the clipping region.
211  * Called by the redraw() function to draw all visible regions.
212  *
213  * @param port The CGraphicsPort to draw to.
214  * @see redraw()
215  */
216 
218 {
219  port->drawFilledRect(getX(), getY(), getWidth(),
221  drawOutline(port);
222 }
223 
224 /**
225  * Draws the outline of the button.
226  *
227  * @param port Graphics port to draw to.
228  */
229 
231 {
232  // Don't draw if the widget indicates it should not have an outline
233 
234  if (!isBorderless())
235  {
236  // Determine which colors to use
237 
238  nxgl_coord_t color1;
239  nxgl_coord_t color2;
240 
241  if (isClicked())
242  {
243  // Bevelled into the screen
244 
245  color1 = getShadowEdgeColor();
246  color2 = getShineEdgeColor();
247  }
248  else
249  {
250  // Bevelled out of the screen
251 
252  color1 = getShineEdgeColor();
253  color2 = getShadowEdgeColor();
254  }
255 
256  port->drawBevelledRect(getX(), getY(),
257  getWidth(), getHeight(),
258  color1, color2);
259  }
260 }
261 
262 /**
263  * Redraws the button.
264  *
265  * @param x The x coordinate of the click.
266  * @param y The y coordinate of the click.
267  */
268 
269 void CGlyphButton::onClick(nxgl_coord_t x, nxgl_coord_t y)
270 {
271  redraw();
272 }
273 
274 /**
275  * Raises an action event.
276  *
277  * @param x The x coordinate of the mouse.
278  * @param y The y coordinate of the mouse.
279  */
280 
281 void CGlyphButton::onPreRelease(nxgl_coord_t x, nxgl_coord_t y)
282 {
284 }
285 
286 /**
287  * Raises a release event and redraws the button.
288  *
289  * @param x The x coordinate of the mouse.
290  * @param y The y coordinate of the mouse.
291  */
292 
293 void CGlyphButton::onRelease(nxgl_coord_t x, nxgl_coord_t y)
294 {
296  redraw();
297 }
298 
299 /**
300  * Redraws the button.
301  *
302  * @param x The x coordinate of the mouse.
303  * @param y The y coordinate of the mouse.
304  */
305 
306 void CGlyphButton::onReleaseOutside(nxgl_coord_t x, nxgl_coord_t y)
307 {
308  redraw();
309 }
CGlyphButton(const CGlyphButton &button)
WidgetBorderSize m_borderSize
Definition: cnxwidget.hxx:208
bool isClicked(void) const
Definition: cnxwidget.hxx:560
virtual void onRelease(nxgl_coord_t x, nxgl_coord_t y)
bool isBorderless(void) const
Definition: cnxwidget.hxx:549
nxgl_mxpixel_t getShadowEdgeColor(void) const
Definition: cnxwidget.hxx:749
void getRect(CRect &rect) const
Definition: cnxwidget.cxx:448
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
FAR const struct SBitmap * m_bitmapNormal
void drawBitmap(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, const struct SBitmap *bitmap, int bitmapX, int bitmapY)
nxgl_mxpixel_t getBackgroundColor(void) const
Definition: cnxwidget.hxx:716
virtual void drawBorder(CGraphicsPort *port)
void drawBitmapGreyScale(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, const struct SBitmap *bitmap, int bitmapX, int bitmapY)
bool isEnabled(void) const
Definition: cnxwidget.cxx:381
virtual void onPreRelease(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 setX(nxgl_coord_t x)
Definition: crect.hxx:229
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
void drawFilledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t color)
nxgl_coord_t height
Definition: cbitmap.hxx:108
nxgl_mxpixel_t getShineEdgeColor(void) const
Definition: cnxwidget.hxx:738
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
nxgl_coord_t width
Definition: cbitmap.hxx:107
virtual void getPreferredDimensions(CRect &rect) const
virtual void drawOutline(CGraphicsPort *port)
virtual void drawContents(CGraphicsPort *port)
nxgl_coord_t getY(void) const
Definition: cnxwidget.cxx:261
void setWidth(nxgl_coord_t width)
Definition: crect.hxx:250
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
static const NXWidgets::SRlePaletteBitmapEntry bitmap[]
void setY(nxgl_coord_t y)
Definition: crect.hxx:240
FAR const struct SBitmap * m_bitmapClicked
virtual void onReleaseOutside(nxgl_coord_t x, nxgl_coord_t y)
void raiseReleaseEvent(nxgl_coord_t x, nxgl_coord_t y)