OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/controls/progress_bar.h" | 5 #include "ui/views/controls/progress_bar.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 *tooltip = tooltip_text_; | 176 *tooltip = tooltip_text_; |
177 return !tooltip_text_.empty(); | 177 return !tooltip_text_.empty(); |
178 } | 178 } |
179 | 179 |
180 void ProgressBar::GetAccessibleState(ui::AccessibleViewState* state) { | 180 void ProgressBar::GetAccessibleState(ui::AccessibleViewState* state) { |
181 state->role = ui::AccessibilityTypes::ROLE_PROGRESSBAR; | 181 state->role = ui::AccessibilityTypes::ROLE_PROGRESSBAR; |
182 state->state = ui::AccessibilityTypes::STATE_READONLY; | 182 state->state = ui::AccessibilityTypes::STATE_READONLY; |
183 } | 183 } |
184 | 184 |
185 gfx::Size ProgressBar::GetPreferredSize() { | 185 gfx::Size ProgressBar::GetPreferredSize() { |
186 return gfx::Size(100, 11); | 186 gfx::Size pref_size(100, 11); |
| 187 gfx::Insets insets = GetInsets(); |
| 188 pref_size.Enlarge(insets.width(), insets.height()); |
| 189 return pref_size; |
187 } | 190 } |
188 | 191 |
189 const char* ProgressBar::GetClassName() const { | 192 const char* ProgressBar::GetClassName() const { |
190 return kViewClassName; | 193 return kViewClassName; |
191 } | 194 } |
192 | 195 |
193 void ProgressBar::OnPaint(gfx::Canvas* canvas) { | 196 void ProgressBar::OnPaint(gfx::Canvas* canvas) { |
| 197 gfx::Rect content_bounds = GetContentsBounds(); |
| 198 int bar_left = content_bounds.x(); |
| 199 int bar_top = content_bounds.y(); |
| 200 int bar_width = content_bounds.width(); |
| 201 int bar_height = content_bounds.height(); |
| 202 |
194 const int progress_width = | 203 const int progress_width = |
195 static_cast<int>(width() * GetNormalizedValue() + 0.5); | 204 static_cast<int>(bar_width * GetNormalizedValue() + 0.5); |
196 | 205 |
197 // Draw background. | 206 // Draw background. |
198 FillRoundRect(canvas, | 207 FillRoundRect(canvas, |
199 0, 0, width(), height(), | 208 bar_left, bar_top, bar_width, bar_height, |
200 kCornerRadius, | 209 kCornerRadius, |
201 kBackgroundColor, kBackgroundColor, | 210 kBackgroundColor, kBackgroundColor, |
202 false); | 211 false); |
203 StrokeRoundRect(canvas, | 212 StrokeRoundRect(canvas, |
204 0, 0, | 213 bar_left, bar_top, |
205 width(), height(), | 214 bar_width, bar_height, |
206 kCornerRadius, | 215 kCornerRadius, |
207 kBackgroundBorderColor, | 216 kBackgroundBorderColor, |
208 kBorderWidth); | 217 kBorderWidth); |
209 | 218 |
210 if (progress_width > 1) { | 219 if (progress_width > 1) { |
211 // Draw inner if wide enough. | 220 // Draw inner if wide enough. |
212 if (progress_width > kBorderWidth * 2) { | 221 if (progress_width > kBorderWidth * 2) { |
213 canvas->Save(); | 222 canvas->Save(); |
214 | 223 |
215 SkPath inner_path; | 224 SkPath inner_path; |
216 AddRoundRectPathWithPadding( | 225 AddRoundRectPathWithPadding( |
217 0, 0, progress_width, height(), | 226 bar_left, bar_top, progress_width, bar_height, |
218 kCornerRadius, | 227 kCornerRadius, |
219 0, | 228 0, |
220 &inner_path); | 229 &inner_path); |
221 canvas->ClipPath(inner_path); | 230 canvas->ClipPath(inner_path); |
222 | 231 |
223 const SkColor bar_colors[] = { | 232 const SkColor bar_colors[] = { |
224 kBarTopColor, | 233 kBarTopColor, |
225 kBarTopColor, | 234 kBarTopColor, |
226 kBarColorStart, | 235 kBarColorStart, |
227 kBarColorEnd, | 236 kBarColorEnd, |
228 kBarColorEnd, | 237 kBarColorEnd, |
229 }; | 238 }; |
230 // We want a thin 1-pixel line for kBarTopColor. | 239 // We want a thin 1-pixel line for kBarTopColor. |
231 SkScalar scalar_height = SkIntToScalar(height()); | 240 SkScalar scalar_height = SkIntToScalar(bar_height); |
232 SkScalar highlight_width = SkScalarDiv(SK_Scalar1, scalar_height); | 241 SkScalar highlight_width = SkScalarDiv(SK_Scalar1, scalar_height); |
233 SkScalar border_width = SkScalarDiv(SkIntToScalar(kBorderWidth), | 242 SkScalar border_width = SkScalarDiv(SkIntToScalar(kBorderWidth), |
234 scalar_height); | 243 scalar_height); |
235 const SkScalar bar_points[] = { | 244 const SkScalar bar_points[] = { |
236 0, | 245 0, |
237 border_width, | 246 border_width, |
238 border_width + highlight_width, | 247 border_width + highlight_width, |
239 SK_Scalar1 - border_width, | 248 SK_Scalar1 - border_width, |
240 SK_Scalar1, | 249 SK_Scalar1, |
241 }; | 250 }; |
242 | 251 |
243 const SkColor disabled_bar_colors[] = { | 252 const SkColor disabled_bar_colors[] = { |
244 kDisabledBarColorStart, | 253 kDisabledBarColorStart, |
245 kDisabledBarColorStart, | 254 kDisabledBarColorStart, |
246 kDisabledBarColorEnd, | 255 kDisabledBarColorEnd, |
247 kDisabledBarColorEnd, | 256 kDisabledBarColorEnd, |
248 }; | 257 }; |
249 | 258 |
250 const SkScalar disabled_bar_points[] = { | 259 const SkScalar disabled_bar_points[] = { |
251 0, | 260 0, |
252 border_width, | 261 border_width, |
253 SK_Scalar1 - border_width, | 262 SK_Scalar1 - border_width, |
254 SK_Scalar1 | 263 SK_Scalar1 |
255 }; | 264 }; |
256 | 265 |
257 // Do not start from (kBorderWidth, kBorderWidth) because it makes gaps | 266 // Do not start from (kBorderWidth, kBorderWidth) because it makes gaps |
258 // between the inner and the border. | 267 // between the inner and the border. |
259 FillRoundRect(canvas, | 268 FillRoundRect(canvas, |
260 0, 0, | 269 bar_left, bar_top, |
261 progress_width, height(), | 270 progress_width, bar_height, |
262 kCornerRadius, | 271 kCornerRadius, |
263 enabled() ? bar_colors : disabled_bar_colors, | 272 enabled() ? bar_colors : disabled_bar_colors, |
264 enabled() ? bar_points : disabled_bar_points, | 273 enabled() ? bar_points : disabled_bar_points, |
265 enabled() ? arraysize(bar_colors) : | 274 enabled() ? arraysize(bar_colors) : |
266 arraysize(disabled_bar_colors), | 275 arraysize(disabled_bar_colors), |
267 false); | 276 false); |
268 | 277 |
269 if (enabled()) { | 278 if (enabled()) { |
270 // Draw the highlight to the right. | 279 // Draw the highlight to the right. |
271 const SkColor highlight_colors[] = { | 280 const SkColor highlight_colors[] = { |
(...skipping 15 matching lines...) Expand all Loading... |
287 std::max(0, progress_width - kHighlightWidth - kBorderWidth); | 296 std::max(0, progress_width - kHighlightWidth - kBorderWidth); |
288 p[0].iset(highlight_left, 0); | 297 p[0].iset(highlight_left, 0); |
289 p[1].iset(progress_width, 0); | 298 p[1].iset(progress_width, 0); |
290 skia::RefPtr<SkShader> s = | 299 skia::RefPtr<SkShader> s = |
291 skia::AdoptRef(SkGradientShader::CreateLinear( | 300 skia::AdoptRef(SkGradientShader::CreateLinear( |
292 p, highlight_colors, highlight_points, | 301 p, highlight_colors, highlight_points, |
293 arraysize(highlight_colors), SkShader::kClamp_TileMode, NULL)); | 302 arraysize(highlight_colors), SkShader::kClamp_TileMode, NULL)); |
294 paint.setShader(s.get()); | 303 paint.setShader(s.get()); |
295 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); | 304 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); |
296 canvas->DrawRect(gfx::Rect(highlight_left, 0, | 305 canvas->DrawRect(gfx::Rect(highlight_left, 0, |
297 kHighlightWidth + kBorderWidth, height()), | 306 kHighlightWidth + kBorderWidth, bar_height), |
298 paint); | 307 paint); |
299 } | 308 } |
300 | 309 |
301 canvas->Restore(); | 310 canvas->Restore(); |
302 } | 311 } |
303 | 312 |
304 // Draw bar stroke | 313 // Draw bar stroke |
305 StrokeRoundRect(canvas, | 314 StrokeRoundRect(canvas, |
306 0, 0, progress_width, height(), | 315 bar_left, bar_top, progress_width, bar_height, |
307 kCornerRadius, | 316 kCornerRadius, |
308 enabled() ? kBarBorderColor : kDisabledBarBorderColor, | 317 enabled() ? kBarBorderColor : kDisabledBarBorderColor, |
309 kBorderWidth); | 318 kBorderWidth); |
310 } | 319 } |
311 } | 320 } |
312 | 321 |
313 } // namespace views | 322 } // namespace views |
OLD | NEW |