NXWidgets: cbutton.cxx Source File

NXWidgets 1.19

cbutton.cxx
Go to the documentation of this file.
1 /****************************************************************************
2 * NxWidgets/libnxwidgets/src/cbutton.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 "cbutton.hxx"
83 #include "cgraphicsport.hxx"
84
85 /****************************************************************************
86 * Pre-Processor Definitions
87 ****************************************************************************/
88
89 /****************************************************************************
90 * CButton Method Implementations
91 ****************************************************************************/
92
93 using namespace NXWidgets;
94
95 /**
96 * Constructor for buttons that display a string.
97 *
98 * @param pWidgetControl The controlling widget for the display.
99 * @param x The x coordinate of the button, relative to its parent.
100 * @param y The y coordinate of the button, relative to its parent.
101 * @param width The width of the button.
102 * @param height The height of the button.
103 * @param text The text for the button to display.
104 * @param style The style that the button should use. If this is not
105 * specified, the button will use the values stored in the global
106 * g_defaultWidgetStyle object. The button will copy the properties of
107 * the style into its own internal style object.
108 */
109
110 CButton::CButton(CWidgetControl *pWidgetControl,
111 nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width,
112 nxgl_coord_t height, const CNxString &text, CWidgetStyle *style)
113 : CLabel(pWidgetControl, x, y, width, height, text, style)
114 {
115 }
116
117 /**
118 * Draws the outline of the button.
119 *
120 * @param port Graphics port to draw to.
121 */
122
123 void CButton::drawOutline(CGraphicsPort *port)
124 {
125 drawOutline(port, isClicked());
126 }
127
128 /**
129 * Draws the outline of the button.
130 *
131 * @param port Graphics port to draw to.
132 * @param useClicked Present outline using the 'clicked' style
133 */
134
135 void CButton::drawOutline(CGraphicsPort *port, bool useClicked)
136 {
137 // Stop drawing if the widget indicates it should not have an outline
138
139 if (isBorderless())
140 {
141 return;
142 }
143
144 // Work out which colors to use
145
146 nxgl_coord_t color1;
147 nxgl_coord_t color2;
148
149 if (useClicked)
150 {
151 // Bevelled into the screen
152
153 color1 = getShadowEdgeColor();
154 color2 = getShineEdgeColor();
155 }
156 else
157 {
158 // Bevelled out of the screen
159
160 color1 = getShineEdgeColor();
161 color2 = getShadowEdgeColor();
162 }
163
164 port->drawBevelledRect(getX(), getY(), getWidth(), getHeight(), color1, color2);
165 }
166
167 /**
168 * Draw the area of this widget that falls within the clipping region.
169 * Called by the redraw() function to draw all visible regions.
170 *
171 * @param port The CGraphicsPort to draw to.
172 * @see redraw()
173 */
174
175 void CButton::drawContents(CGraphicsPort *port)
176 {
177 drawContents(port, isClicked());
178 }
179
180 /**
181 * Draw the area of this widget that falls within the clipping region.
182 * Called by the redraw() function to draw all visible regions.
183 *
184 * @param port The CGraphicsPort to draw to.
185 * @param useClicked Present contents using the 'clicked' style
186 * @see redraw()
187 */
188
189 void CButton::drawContents(CGraphicsPort *port, bool useClicked)
190
191 {
192 // Get the draw region (excluding any borders)
193
194 CRect rect;
195 getRect(rect);
196
197 // Get the X/Y position of the text within the button
198
199 struct nxgl_point_s pos;
200 pos.x = rect.getX() + m_align.x;
201 pos.y = rect.getY() + m_align.y;
202
203 // Pick the text color
204
205 nxgl_mxpixel_t textColor;
206 if (!isEnabled())
207 {
208 textColor = getDisabledTextColor();
209 }
210 else if (useClicked)
211 {
212 textColor = getSelectedTextColor();
213 }
214 else
215 {
216 textColor = getEnabledTextColor();
217 }
218
219 // And draw the button text
220
221 port->drawText(&pos, &rect, getFont(), m_text, 0, m_text.getLength(), textColor);
222 }
223
224 /**
225 * Draw the area of this widget that falls within the clipping region.
226 * Called by the redraw() function to draw all visible regions.
227 *
228 * @param port The CGraphicsPort to draw to.
229 * @see redraw()
230 */
231
232 void CButton::drawBorder(CGraphicsPort *port)
233 {
234 drawBorder(port, isClicked());
235 }
236
237 /**
238 * Draw the area of this widget that falls within the clipping region.
239 * Called by the redraw() function to draw all visible regions.
240 *
241 * @param port The CGraphicsPort to draw to.
242 * @param useClicked Present border using the 'clicked' style
243 * @see redraw()
244 */
245
246 void CButton::drawBorder(CGraphicsPort *port, bool useClicked)
247 {
248 // Determine the background color
249
250 nxgl_mxpixel_t backColor;
251
252 if (useClicked || m_highlighted)
253 {
254 backColor = getSelectedBackgroundColor();
255 }
256 else
257 {
258 backColor = getBackgroundColor();
259 }
260
261 port->drawFilledRect(getX(), getY(), getWidth(), getHeight(), backColor);
262 drawOutline(port);
263 }
264
265 /**
266 * Redraws the button.
267 *
268 * @param x The x coordinate of the click.
269 * @param y The y coordinate of the click.
270 */
271
272 void CButton::onClick(nxgl_coord_t x, nxgl_coord_t y)
273 {
274 redraw();
275 }
276
277 /**
278 * Raises an action.
279 *
280 * @param x The x coordinate of the mouse.
281 * @param y The y coordinate of the mouse.
282 */
283
284 void CButton::onPreRelease(nxgl_coord_t x, nxgl_coord_t y)
285 {
286 m_widgetEventHandlers->raiseActionEvent();
287 }
288
289 /**
290 * Redraws the button.
291 *
292 * @param x The x coordinate of the mouse.
293 * @param y The y coordinate of the mouse.
294 */
295
296 void CButton::onRelease(nxgl_coord_t x, nxgl_coord_t y)
297 {
298 redraw();
299 }
300
301 /**
302 * Redraws the button.
303 *
304 * @param x The x coordinate of the mouse.
305 * @param y The y coordinate of the mouse.
306 */
307
308 void CButton::onReleaseOutside(nxgl_coord_t x, nxgl_coord_t y)
309 {
310 redraw();
311 }
NXWidgets::CNxWidget::isClicked
bool isClicked(void) const
Definition: cnxwidget.hxx:560
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::CButton::CButton
CButton(const CButton &button)
Definition: cbutton.hxx:212
NXWidgets::CWidgetStyle
Definition: cwidgetstyle.hxx:125
NXWidgets::CWidgetControl
Definition: cwidgetcontrol.hxx:95
NXWidgets::CNxWidget::getRect
void getRect(CRect &rect) const
Definition: cnxwidget.cxx:448
NXWidgets::CButton::onReleaseOutside
virtual void onReleaseOutside(nxgl_coord_t x, nxgl_coord_t y)
Definition: cbutton.cxx:308
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::CWidgetEventHandlerList::raiseActionEvent
void raiseActionEvent(void)
Definition: cwidgeteventhandlerlist.cxx:507
NXWidgets::CButton::drawBorder
virtual void drawBorder(CGraphicsPort *port)
Definition: cbutton.cxx:232
cgraphicsport.hxx
NXWidgets::CNxWidget::getBackgroundColor
nxgl_mxpixel_t getBackgroundColor(void) const
Definition: cnxwidget.hxx:716
NXWidgets::CButton::drawOutline
virtual void drawOutline(CGraphicsPort *port)
Definition: cbutton.cxx:123
NXWidgets::CLabel::m_align
struct nxgl_point_s m_align
Definition: clabel.hxx:140
NXWidgets::CLabel::m_highlighted
bool m_highlighted
Definition: clabel.hxx:144
NXWidgets::CButton::onClick
virtual void onClick(nxgl_coord_t x, nxgl_coord_t y)
Definition: cbutton.cxx:272
NXWidgets::CNxWidget::isEnabled
bool isEnabled(void) const
Definition: cnxwidget.cxx:381
NXWidgets::CNxWidget::redraw
void redraw(void)
Definition: cnxwidget.cxx:483
NXWidgets::CNxWidget::getEnabledTextColor
nxgl_mxpixel_t getEnabledTextColor(void) const
Definition: cnxwidget.hxx:782
NXWidgets
Definition: cbgwindow.hxx:65
NXWidgets::CNxWidget::getHeight
nxgl_coord_t getHeight(void) const
Definition: cnxwidget.hxx:593
NXWidgets::CButton::onPreRelease
virtual void onPreRelease(nxgl_coord_t x, nxgl_coord_t y)
Definition: cbutton.cxx:284
NXWidgets::CNxWidget::getSelectedBackgroundColor
nxgl_mxpixel_t getSelectedBackgroundColor(void) const
Definition: cnxwidget.hxx:727
NXWidgets::CLabel::m_text
CNxString m_text
Definition: clabel.hxx:139
NXWidgets::CButton::drawContents
virtual void drawContents(CGraphicsPort *port)
Definition: cbutton.cxx:175
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::getFont
CNxFont * getFont(void) const
Definition: cnxwidget.hxx:705
NXWidgets::CNxWidget::getShineEdgeColor
nxgl_mxpixel_t getShineEdgeColor(void) const
Definition: cnxwidget.hxx:738
NXWidgets::CNxWidget::m_widgetEventHandlers
CWidgetEventHandlerList * m_widgetEventHandlers
Definition: cnxwidget.hxx:191
NXWidgets::CNxString::getLength
const unsigned int getLength(void) const
Definition: cnxstring.hxx:325
NXWidgets::CRect
Definition: crect.hxx:101
cbutton.hxx
NXWidgets::CNxWidget::getY
nxgl_coord_t getY(void) const
Definition: cnxwidget.cxx:261
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::CNxString
Definition: cnxstring.hxx:121
NXWidgets::CRect::getY
nxgl_coord_t getY(void) const
Definition: crect.hxx:170
NXWidgets::CLabel
Definition: clabel.hxx:112
NXWidgets::CNxWidget::getDisabledTextColor
nxgl_mxpixel_t getDisabledTextColor(void) const
Definition: cnxwidget.hxx:771
NXWidgets::CButton::onRelease
virtual void onRelease(nxgl_coord_t x, nxgl_coord_t y)
Definition: cbutton.cxx:296
NXWidgets::CGraphicsPort::drawText
void drawText(struct nxgl_point_s *pos, CRect *bound, CNxFont *font, const CNxString &string)
Definition: cgraphicsport.cxx:609
NXWidgets::CNxWidget::getSelectedTextColor
nxgl_mxpixel_t getSelectedTextColor(void) const
Definition: cnxwidget.hxx:793
NXWidgets::CGraphicsPort
Definition: cgraphicsport.hxx:106