NXWidgets: cglyphbutton.cxx Source File

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
116 CGlyphButton::CGlyphButton(CWidgetControl *pWidgetControl,
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
148 void CGlyphButton::getPreferredDimensions(CRect &rect) const
149 {
150 nxgl_coord_t width = m_bitmapNormal->width;
151 nxgl_coord_t height = m_bitmapNormal->height;
152
153 if (!m_flags.borderless)
154 {
155 width = m_borderSize.left + m_borderSize.right;
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
173 void CGlyphButton::drawContents(CGraphicsPort *port)
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
217 void CGlyphButton::drawBorder(CGraphicsPort *port)
218 {
219 port->drawFilledRect(getX(), getY(), getWidth(),
220 getHeight(), getBackgroundColor());
221 drawOutline(port);
222 }
223
224 /**
225 * Draws the outline of the button.
226 *
227 * @param port Graphics port to draw to.
228 */
229
230 void CGlyphButton::drawOutline(CGraphicsPort *port)
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 {
283 m_widgetEventHandlers->raiseActionEvent();
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 {
295 m_widgetEventHandlers->raiseReleaseEvent(x, y);
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 }
NXWidgets::CNxWidget
Definition: cnxwidget.hxx:121
NXWidgets::CGlyphButton::CGlyphButton
CGlyphButton(const CGlyphButton &button)
Definition: cglyphbutton.hxx:189
NXWidgets::CNxWidget::m_borderSize
WidgetBorderSize m_borderSize
Definition: cnxwidget.hxx:208
NXWidgets::CNxWidget::isClicked
bool isClicked(void) const
Definition: cnxwidget.hxx:560
NXWidgets::CGlyphButton::onRelease
virtual void onRelease(nxgl_coord_t x, nxgl_coord_t y)
Definition: cglyphbutton.cxx:293
NXWidgets::CNxWidget::isBorderless
bool isBorderless(void) const
Definition: cnxwidget.hxx:549
NXWidgets::CNxWidget::getShadowEdgeColor
nxgl_mxpixel_t getShadowEdgeColor(void) const
Definition: cnxwidget.hxx:749
NXWidgets::CWidgetStyle
Definition: cwidgetstyle.hxx:125
NXWidgets::CWidgetControl
Definition: cwidgetcontrol.hxx:95
singletons.hxx
NXWidgets::CNxWidget::getRect
void getRect(CRect &rect) const
Definition: cnxwidget.cxx:448
NXWidgets::CGlyphButton::m_bitmapX
nxgl_coord_t m_bitmapX
Definition: cglyphbutton.hxx:119
NXWidgets::CGraphicsPort::drawBevelledRect
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)
Definition: cgraphicsport.cxx:323
NXWidgets::CRect::getX
nxgl_coord_t getX(void) const
Definition: crect.hxx:160
NXWidgets::CGlyphButton::m_bitmapNormal
FAR const struct SBitmap * m_bitmapNormal
Definition: cglyphbutton.hxx:117
NXWidgets::CWidgetEventHandlerList::raiseActionEvent
void raiseActionEvent(void)
Definition: cwidgeteventhandlerlist.cxx:507
cgraphicsport.hxx
NXWidgets::CGraphicsPort::drawBitmap
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)
Definition: cgraphicsport.cxx:348
NXWidgets::CNxWidget::getBackgroundColor
nxgl_mxpixel_t getBackgroundColor(void) const
Definition: cnxwidget.hxx:716
NXWidgets::CGlyphButton::drawBorder
virtual void drawBorder(CGraphicsPort *port)
Definition: cglyphbutton.cxx:217
NXWidgets::CGraphicsPort::drawBitmapGreyScale
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)
Definition: cgraphicsport.cxx:528
NXWidgets::CNxWidget::isEnabled
bool isEnabled(void) const
Definition: cnxwidget.cxx:381
NXWidgets::CGlyphButton::onPreRelease
virtual void onPreRelease(nxgl_coord_t x, nxgl_coord_t y)
Definition: cglyphbutton.cxx:281
NXWidgets::CNxWidget::redraw
void redraw(void)
Definition: cnxwidget.cxx:483
NXWidgets::CGlyphButton::m_bitmapY
nxgl_coord_t m_bitmapY
Definition: cglyphbutton.hxx:120
NXWidgets
Definition: cbgwindow.hxx:65
NXWidgets::CNxWidget::getHeight
nxgl_coord_t getHeight(void) const
Definition: cnxwidget.hxx:593
NXWidgets::CRect::setHeight
void setHeight(nxgl_coord_t height)
Definition: crect.hxx:261
NXWidgets::CNxWidget::m_rect
CRect m_rect
Definition: cnxwidget.hxx:172
NXWidgets::CNxWidget::WidgetBorderSize::left
uint8_t left
Definition: cnxwidget.hxx:167
cglyphbutton.hxx
NXWidgets::CRect::setX
void setX(nxgl_coord_t x)
Definition: crect.hxx:229
nxconfig.hxx
NXWidgets::CGlyphButton::onClick
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
Definition: cglyphbutton.cxx:269
NXWidgets::CNxWidget::WidgetBorderSize::right
uint8_t right
Definition: cnxwidget.hxx:165
NXWidgets::CGraphicsPort::drawFilledRect
void drawFilledRect(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, nxgl_coord_t height, nxgl_mxpixel_t color)
Definition: cgraphicsport.cxx:280
NXWidgets::CNxWidget::WidgetBorderSize::bottom
uint8_t bottom
Definition: cnxwidget.hxx:166
NXWidgets::SBitmap::height
nxgl_coord_t height
Definition: cbitmap.hxx:108
NXWidgets::CNxWidget::getShineEdgeColor
nxgl_mxpixel_t getShineEdgeColor(void) const
Definition: cnxwidget.hxx:738
NXWidgets::CNxWidget::WidgetBorderSize::top
uint8_t top
Definition: cnxwidget.hxx:164
NXWidgets::CNxWidget::m_widgetEventHandlers
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
NXWidgets::CRect
Definition: crect.hxx:101
NXWidgets::SBitmap::width
nxgl_coord_t width
Definition: cbitmap.hxx:107
NXWidgets::CGlyphButton::getPreferredDimensions
virtual void getPreferredDimensions(CRect &rect) const
Definition: cglyphbutton.cxx:148
NXWidgets::CGlyphButton::drawOutline
virtual void drawOutline(CGraphicsPort *port)
Definition: cglyphbutton.cxx:230
NXWidgets::CGlyphButton::drawContents
virtual void drawContents(CGraphicsPort *port)
Definition: cglyphbutton.cxx:173
NXWidgets::CNxWidget::getY
nxgl_coord_t getY(void) const
Definition: cnxwidget.cxx:261
NXWidgets::CRect::setWidth
void setWidth(nxgl_coord_t width)
Definition: crect.hxx:250
cbitmap.hxx
NXWidgets::CNxWidget::getWidth
nxgl_coord_t getWidth(void) const
Definition: cnxwidget.hxx:582
NXWidgets::CNxWidget::getX
nxgl_coord_t getX(void) const
Definition: cnxwidget.cxx:245
NXWidgets::SBitmap
Definition: cbitmap.hxx:103
NXWidgets::CRect::getY
nxgl_coord_t getY(void) const
Definition: crect.hxx:170
bitmap
static const NXWidgets::SRlePaletteBitmapEntry bitmap[]
Definition: glyph_mediaplayer24x24.cxx:85
NXWidgets::CNxWidget::Flags::clicked
uint8_t clicked
Definition: cnxwidget.hxx:144
NXWidgets::CNxWidget::Flags::borderless
uint8_t borderless
Definition: cnxwidget.hxx:148
NXWidgets::CRect::setY
void setY(nxgl_coord_t y)
Definition: crect.hxx:240
NXWidgets::CGlyphButton::m_bitmapClicked
FAR const struct SBitmap * m_bitmapClicked
Definition: cglyphbutton.hxx:118
NXWidgets::CGlyphButton::onReleaseOutside
virtual void onReleaseOutside(nxgl_coord_t x, nxgl_coord_t y)
Definition: cglyphbutton.cxx:306
NXWidgets::CGraphicsPort
Definition: cgraphicsport.hxx:106
NXWidgets::CWidgetEventHandlerList::raiseReleaseEvent
void raiseReleaseEvent(nxgl_coord_t x, nxgl_coord_t y)
Definition: cwidgeteventhandlerlist.cxx:206
NXWidgets::CNxWidget::m_flags
Flags m_flags
Definition: cnxwidget.hxx:187