NXWidgets: cprogressbar.cxx Source File

NXWidgets 1.19

cprogressbar.cxx
Go to the documentation of this file.
1 /****************************************************************************
2 * NxWidgets/libnxwidgets/src/cprogressbar.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 #include <cstdio>
80 #include <cstring>
81
82 #include <nuttx/nx/nxglib.h>
83
84 #include "cprogressbar.hxx"
85 #include "cgraphicsport.hxx"
86
87 /****************************************************************************
88 * Pre-Processor Definitions
89 ****************************************************************************/
90
91 /****************************************************************************
92 * CButton Method Implementations
93 ****************************************************************************/
94
95 using namespace NXWidgets;
96
97 /**
98 * Constructor.
99 *
100 * @param pWidgetControl The widget control for the display.
101 * @param x The x coordinate of the progress bar, relative to its parent.
102 * @param y The y coordinate of the progress bar, relative to its parent.
103 * @param width The width of the progress bar.
104 * @param height The height of the progress bar.
105 */
106
107 CProgressBar::CProgressBar(CWidgetControl *pWidgetControl,
108 nxgl_coord_t x, nxgl_coord_t y,
109 nxgl_coord_t width, nxgl_coord_t height)
110 : CNxWidget(pWidgetControl, x, y, width, height, 0)
111 {
112 m_minimumValue = 0;
113 m_maximumValue = 0;
114 m_value = 0;
115 m_showPercentageText = true;
116 m_flags.borderless = false;
117 }
118
119 /**
120 * Set the value that of the progress bar.
121 *
122 * @param value The new value.
123 */
124
125 void CProgressBar::setValue(const int16_t value)
126 {
127 m_value = value;
128
129 // Limit to max/min values
130
131 if (m_value > m_maximumValue)
132 {
133 m_value = m_maximumValue;
134 }
135
136 if (m_value < m_minimumValue)
137 {
138 m_value = m_minimumValue;
139 }
140
141 redraw();
142 }
143
144 /**
145 * Draw the area of this widget that falls within the clipping region.
146 * Called by the redraw() function to draw all visible regions.
147 *
148 * @param port The CGraphicsPort to draw to.
149 * @see redraw()
150 */
151
152 void CProgressBar::drawContents(CGraphicsPort *port)
153 {
154 // Get the drawing window, accounting for the presence of any border
155
156 CRect rect;
157 getRect(rect);
158
159 // Calculate ratio of pixels to value range (max fractional value of 255)
160
161 uint32_t ratio = ((uint32_t)rect.getWidth() << 8) / (uint32_t)(m_maximumValue - m_minimumValue);
162
163 // Convert value using ratio, rounding up and shifting down
164
165 int16_t barWidth = ((m_value * ratio) + 128) >> 8;
166
167 // Draw filled region
168
169 port->drawFilledRect(rect.getX(), rect.getY(), barWidth, rect.getHeight(), getHighlightColor());
170
171 // Draw unfilled background
172
173 if (barWidth < rect.getWidth())
174 {
175 port->drawFilledRect(rect.getX() + barWidth, rect.getY(),
176 rect.getWidth() - barWidth, rect.getHeight(),
177 getBackgroundColor());
178 }
179
180 // Draw completion percentage text
181
182 if (m_showPercentageText)
183 {
184 char text[6];
185 sprintf(text, "%d%%", (100 * m_value) / (m_maximumValue - m_minimumValue));
186
187 struct nxgl_point_s pos;
188 pos.x = rect.getX() +
189 ((rect.getWidth() - getFont()->getStringWidth(text)) >> 1);
190 pos.y = rect.getY() +
191 ((rect.getHeight() - getFont()->getHeight()) >> 1);
192
193 // Determine the background and text color
194
195 nxgl_mxpixel_t textColor;
196
197 if (!isEnabled())
198 {
199 textColor = getDisabledTextColor();
200 }
201 else
202 {
203 textColor = getEnabledTextColor();
204 }
205
206 // And draw the text using the selected color
207
208 port->drawText(&pos, &rect, getFont(), text, 0, strlen(text),
209 textColor);
210 }
211 }
212
213 /**
214 * Draw the area of this widget that falls within the clipping region.
215 * Called by the redraw() function to draw all visible regions.
216 *
217 * @param port The CGraphicsPort to draw to.
218 * @see redraw()
219 */
220
221 void CProgressBar::drawBorder(CGraphicsPort *port)
222 {
223 // Stop drawing if the widget indicates it should not have an outline
224
225 if (!isBorderless())
226 {
227 port->drawBevelledRect(getX(), getY(), getWidth(), getHeight(),
228 getShadowEdgeColor(), getShineEdgeColor());
229 }
230 }
NXWidgets::CProgressBar::CProgressBar
CProgressBar(const CProgressBar &progressBar)
Definition: cprogressbar.hxx:141
NXWidgets::CNxWidget
Definition: cnxwidget.hxx:121
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::CRect::getHeight
nxgl_coord_t getHeight(void) const
Definition: crect.hxx:204
NXWidgets::CWidgetControl
Definition: cwidgetcontrol.hxx:95
NXWidgets::CProgressBar::m_minimumValue
int16_t m_minimumValue
Definition: cprogressbar.hxx:111
NXWidgets::CNxWidget::getRect
void getRect(CRect &rect) const
Definition: cnxwidget.cxx:448
NXWidgets::CRect::getWidth
nxgl_coord_t getWidth(void) const
Definition: crect.hxx:181
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
cgraphicsport.hxx
NXWidgets::CNxWidget::getBackgroundColor
nxgl_mxpixel_t getBackgroundColor(void) const
Definition: cnxwidget.hxx:716
NXWidgets::CNxWidget::isEnabled
bool isEnabled(void) const
Definition: cnxwidget.cxx:381
NXWidgets::CNxFont::getStringWidth
nxgl_coord_t getStringWidth(const CNxString &text) const
Definition: cnxfont.cxx:142
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::CProgressBar::setValue
void setValue(const int16_t value)
Definition: cprogressbar.cxx:125
NXWidgets::CProgressBar::drawContents
virtual void drawContents(CGraphicsPort *port)
Definition: cprogressbar.cxx:152
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::CProgressBar::m_maximumValue
int16_t m_maximumValue
Definition: cprogressbar.hxx:112
NXWidgets::CNxFont::getHeight
const uint8_t getHeight(void) const
Definition: cnxfont.hxx:229
NXWidgets::CRect
Definition: crect.hxx:101
NXWidgets::CProgressBar::drawBorder
virtual void drawBorder(CGraphicsPort *port)
Definition: cprogressbar.cxx:221
cprogressbar.hxx
NXWidgets::CNxWidget::getHighlightColor
nxgl_mxpixel_t getHighlightColor(void) const
Definition: cnxwidget.hxx:760
NXWidgets::CProgressBar::m_value
int16_t m_value
Definition: cprogressbar.hxx:113
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::CRect::getY
nxgl_coord_t getY(void) const
Definition: crect.hxx:170
NXWidgets::CNxWidget::getDisabledTextColor
nxgl_mxpixel_t getDisabledTextColor(void) const
Definition: cnxwidget.hxx:771
NXWidgets::CProgressBar::m_showPercentageText
bool m_showPercentageText
Definition: cprogressbar.hxx:114
NXWidgets::CNxWidget::Flags::borderless
uint8_t borderless
Definition: cnxwidget.hxx:148
NXWidgets::CGraphicsPort::drawText
void drawText(struct nxgl_point_s *pos, CRect *bound, CNxFont *font, const CNxString &string)
Definition: cgraphicsport.cxx:609
NXWidgets::CGraphicsPort
Definition: cgraphicsport.hxx:106
NXWidgets::CNxWidget::m_flags
Flags m_flags
Definition: cnxwidget.hxx:187