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/menu/menu_image_util.h" | 5 #include "ui/views/controls/menu/menu_image_util.h" |
6 | 6 |
7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
| 8 #include "base/logging.h" |
8 #include "grit/ui_resources.h" | 9 #include "grit/ui_resources.h" |
9 #include "third_party/skia/include/effects/SkGradientShader.h" | 10 #include "third_party/skia/include/effects/SkGradientShader.h" |
10 #include "ui/base/resource/resource_bundle.h" | 11 #include "ui/base/resource/resource_bundle.h" |
11 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
12 #include "ui/gfx/image/image.h" | 13 #include "ui/gfx/image/image.h" |
13 #include "ui/gfx/image/image_skia.h" | 14 #include "ui/gfx/image/image_skia.h" |
| 15 #include "ui/gfx/image/image_skia_source.h" |
14 #include "ui/gfx/point.h" | 16 #include "ui/gfx/point.h" |
15 #include "ui/gfx/size.h" | 17 #include "ui/gfx/size.h" |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 // Size of the radio button inciator. | 21 // Size of the radio button inciator. |
20 const int kSelectedIndicatorSize = 5; | 22 const int kSelectedIndicatorSize = 5; |
21 const int kIndicatorSize = 10; | 23 const int kIndicatorSize = 10; |
22 | 24 |
23 // Used for the radio indicator. See theme_draw for details. | 25 // Used for the radio indicator. See theme_draw for details. |
24 const double kGradientStop = .5; | 26 const double kGradientStop = .5; |
25 const SkColor kGradient0 = SkColorSetRGB(255, 255, 255); | 27 const SkColor kGradient0 = SkColorSetRGB(255, 255, 255); |
26 const SkColor kGradient1 = SkColorSetRGB(255, 255, 255); | 28 const SkColor kGradient1 = SkColorSetRGB(255, 255, 255); |
27 const SkColor kGradient2 = SkColorSetRGB(0xD8, 0xD8, 0xD8); | 29 const SkColor kGradient2 = SkColorSetRGB(0xD8, 0xD8, 0xD8); |
28 const SkColor kBaseStroke = SkColorSetRGB(0x8F, 0x8F, 0x8F); | 30 const SkColor kBaseStroke = SkColorSetRGB(0x8F, 0x8F, 0x8F); |
29 const SkColor kRadioButtonIndicatorGradient0 = SkColorSetRGB(0, 0, 0); | 31 const SkColor kRadioButtonIndicatorGradient0 = SkColorSetRGB(0, 0, 0); |
30 const SkColor kRadioButtonIndicatorGradient1 = SkColorSetRGB(0x83, 0x83, 0x83); | 32 const SkColor kRadioButtonIndicatorGradient1 = SkColorSetRGB(0x83, 0x83, 0x83); |
31 const SkColor kIndicatorStroke = SkColorSetRGB(0, 0, 0); | 33 const SkColor kIndicatorStroke = SkColorSetRGB(0, 0, 0); |
32 | 34 |
33 gfx::ImageSkia* CreateRadioButtonImage(bool selected) { | 35 class RadioButtonImageSource : public gfx::ImageSkiaSource { |
34 // + 2 (1px on each side) to cover rounding error. | 36 public: |
35 gfx::Canvas canvas(gfx::Size(kIndicatorSize + 2, kIndicatorSize + 2), false); | 37 explicit RadioButtonImageSource(bool selected) : selected_(selected) { |
36 canvas.Translate(gfx::Point(1, 1)); | 38 } |
| 39 virtual ~RadioButtonImageSource() {} |
37 | 40 |
38 SkPoint gradient_points[3]; | 41 virtual gfx::ImageSkiaRep GetImageForScale( |
39 gradient_points[0].iset(0, 0); | 42 ui::ScaleFactor scale_factor) OVERRIDE { |
40 gradient_points[1].iset(0, static_cast<int>(kIndicatorSize * kGradientStop)); | 43 float scale = GetScaleFactorScale(scale_factor); |
41 gradient_points[2].iset(0, kIndicatorSize); | 44 // + 2 (1px on each side) to cover rounding error. |
42 SkColor gradient_colors[3] = { kGradient0, kGradient1, kGradient2 }; | 45 gfx::Size size(kIndicatorSize + 2, kIndicatorSize + 2); |
43 SkShader* shader = SkGradientShader::CreateLinear( | 46 gfx::Canvas canvas(size.Scale(scale), false); |
44 gradient_points, gradient_colors, NULL, arraysize(gradient_points), | 47 canvas.Scale(scale, scale); |
45 SkShader::kClamp_TileMode, NULL); | 48 canvas.Translate(gfx::Point(1, 1)); |
46 SkPaint paint; | |
47 paint.setStyle(SkPaint::kFill_Style); | |
48 paint.setAntiAlias(true); | |
49 paint.setShader(shader); | |
50 shader->unref(); | |
51 int radius = kIndicatorSize / 2; | |
52 canvas.sk_canvas()->drawCircle(radius, radius, radius, paint); | |
53 | 49 |
54 paint.setStrokeWidth(SkIntToScalar(0)); | 50 SkPoint gradient_points[3]; |
55 paint.setShader(NULL); | 51 gradient_points[0].iset(0, 0); |
56 paint.setStyle(SkPaint::kStroke_Style); | 52 gradient_points[1].iset(0, |
57 paint.setColor(kBaseStroke); | 53 static_cast<int>(kIndicatorSize * kGradientStop)); |
58 canvas.sk_canvas()->drawCircle(radius, radius, radius, paint); | 54 gradient_points[2].iset(0, kIndicatorSize); |
59 | 55 SkColor gradient_colors[3] = { kGradient0, kGradient1, kGradient2 }; |
60 if (selected) { | 56 SkShader* shader = SkGradientShader::CreateLinear( |
61 SkPoint selected_gradient_points[2]; | 57 gradient_points, gradient_colors, NULL, arraysize(gradient_points), |
62 selected_gradient_points[0].iset(0, 0); | 58 SkShader::kClamp_TileMode, NULL); |
63 selected_gradient_points[1].iset(0, kSelectedIndicatorSize); | 59 SkPaint paint; |
64 SkColor selected_gradient_colors[2] = { kRadioButtonIndicatorGradient0, | 60 paint.setStyle(SkPaint::kFill_Style); |
65 kRadioButtonIndicatorGradient1 }; | 61 paint.setAntiAlias(true); |
66 shader = SkGradientShader::CreateLinear( | |
67 selected_gradient_points, selected_gradient_colors, NULL, | |
68 arraysize(selected_gradient_points), SkShader::kClamp_TileMode, NULL); | |
69 paint.setShader(shader); | 62 paint.setShader(shader); |
70 shader->unref(); | 63 shader->unref(); |
71 paint.setStyle(SkPaint::kFill_Style); | 64 int radius = kIndicatorSize / 2; |
72 canvas.sk_canvas()->drawCircle(radius, radius, | 65 canvas.sk_canvas()->drawCircle(radius, radius, radius, paint); |
73 kSelectedIndicatorSize / 2, paint); | |
74 | |
75 paint.setStrokeWidth(SkIntToScalar(0)); | 66 paint.setStrokeWidth(SkIntToScalar(0)); |
76 paint.setShader(NULL); | 67 paint.setShader(NULL); |
77 paint.setStyle(SkPaint::kStroke_Style); | 68 paint.setStyle(SkPaint::kStroke_Style); |
78 paint.setColor(kIndicatorStroke); | 69 paint.setColor(kBaseStroke); |
79 canvas.sk_canvas()->drawCircle(radius, radius, | 70 canvas.sk_canvas()->drawCircle(radius, radius, radius, paint); |
80 kSelectedIndicatorSize / 2, paint); | 71 |
| 72 if (selected_) { |
| 73 SkPoint selected_gradient_points[2]; |
| 74 selected_gradient_points[0].iset(0, 0); |
| 75 selected_gradient_points[1].iset(0, kSelectedIndicatorSize); |
| 76 SkColor selected_gradient_colors[2] = { kRadioButtonIndicatorGradient0, |
| 77 kRadioButtonIndicatorGradient1 }; |
| 78 shader = SkGradientShader::CreateLinear( |
| 79 selected_gradient_points, selected_gradient_colors, NULL, |
| 80 arraysize(selected_gradient_points), SkShader::kClamp_TileMode, NULL); |
| 81 paint.setShader(shader); |
| 82 shader->unref(); |
| 83 paint.setStyle(SkPaint::kFill_Style); |
| 84 canvas.sk_canvas()->drawCircle(radius, radius, |
| 85 kSelectedIndicatorSize / 2, paint); |
| 86 |
| 87 paint.setStrokeWidth(SkIntToScalar(0)); |
| 88 paint.setShader(NULL); |
| 89 paint.setStyle(SkPaint::kStroke_Style); |
| 90 paint.setColor(kIndicatorStroke); |
| 91 canvas.sk_canvas()->drawCircle(radius, radius, |
| 92 kSelectedIndicatorSize / 2, paint); |
| 93 } |
| 94 LOG(ERROR) << "Generating:" << selected_; |
| 95 return gfx::ImageSkiaRep(canvas.ExtractBitmap(), scale_factor); |
81 } | 96 } |
82 return new gfx::ImageSkia(canvas.ExtractBitmap()); | 97 |
| 98 private: |
| 99 bool selected_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(RadioButtonImageSource); |
| 102 }; |
| 103 |
| 104 gfx::ImageSkia* CreateRadioButtonImage(bool selected) { |
| 105 return new gfx::ImageSkia(new RadioButtonImageSource(selected), |
| 106 gfx::Size(kIndicatorSize + 2, kIndicatorSize + 2)); |
83 } | 107 } |
84 | 108 |
85 gfx::ImageSkia* GetRtlSubmenuArrowImage() { | 109 gfx::ImageSkia* GetRtlSubmenuArrowImage() { |
86 static gfx::ImageSkia* kRtlArrow = NULL; | 110 static gfx::ImageSkia* kRtlArrow = NULL; |
87 if (!kRtlArrow) { | 111 if (!kRtlArrow) { |
88 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 112 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
89 const gfx::ImageSkia* r = rb.GetImageNamed(IDR_MENU_ARROW).ToImageSkia(); | 113 const gfx::ImageSkia* r = rb.GetImageNamed(IDR_MENU_ARROW).ToImageSkia(); |
90 gfx::Canvas canvas(gfx::Size(r->width(), r->height()), false); | 114 gfx::Canvas canvas(gfx::Size(r->width(), r->height()), false); |
91 canvas.Scale(-1, 1); | 115 canvas.Scale(-1, 1); |
92 canvas.DrawImageInt(*r, - r->width(), 0); | 116 canvas.DrawImageInt(*r, - r->width(), 0); |
(...skipping 13 matching lines...) Expand all Loading... |
106 return selected ? kRadioOn : kRadioOff; | 130 return selected ? kRadioOn : kRadioOff; |
107 } | 131 } |
108 | 132 |
109 const gfx::ImageSkia* GetSubmenuArrowImage() { | 133 const gfx::ImageSkia* GetSubmenuArrowImage() { |
110 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 134 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
111 return base::i18n::IsRTL() ? GetRtlSubmenuArrowImage() | 135 return base::i18n::IsRTL() ? GetRtlSubmenuArrowImage() |
112 : rb.GetImageNamed(IDR_MENU_ARROW).ToImageSkia(); | 136 : rb.GetImageNamed(IDR_MENU_ARROW).ToImageSkia(); |
113 } | 137 } |
114 | 138 |
115 } // namespace views | 139 } // namespace views |
OLD | NEW |