Add ICU message format support
[chromium-blink-merge.git] / ui / native_theme / common_theme.cc
blob462f7e9414cc44d701b7ca774fa0cbd8f0395840
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
3 // found in the LICENSE file.
5 #include "ui/native_theme/common_theme.h"
7 #include "base/logging.h"
8 #include "third_party/skia/include/core/SkCanvas.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/gfx/canvas.h"
11 #include "ui/gfx/color_utils.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/image/image_skia.h"
14 #include "ui/gfx/skia_util.h"
15 #include "ui/resources/grit/ui_resources.h"
17 namespace {
19 // Theme colors returned by GetSystemColor().
21 // MenuItem:
22 const SkColor kMenuBackgroundColor = SK_ColorWHITE;
23 const SkColor kMenuHighlightBackgroundColor = SkColorSetRGB(0x42, 0x81, 0xF4);
24 const SkColor kMenuInvertedSchemeHighlightBackgroundColor =
25 SkColorSetRGB(48, 48, 48);
26 const SkColor kMenuBorderColor = SkColorSetRGB(0xBA, 0xBA, 0xBA);
27 const SkColor kEnabledMenuButtonBorderColor = SkColorSetARGB(36, 0, 0, 0);
28 const SkColor kFocusedMenuButtonBorderColor = SkColorSetARGB(72, 0, 0, 0);
29 const SkColor kHoverMenuButtonBorderColor = SkColorSetARGB(72, 0, 0, 0);
30 const SkColor kMenuSeparatorColor = SkColorSetRGB(0xE9, 0xE9, 0xE9);
31 const SkColor kEnabledMenuItemForegroundColor = SK_ColorBLACK;
32 const SkColor kDisabledMenuItemForegroundColor = SkColorSetRGB(161, 161, 146);
33 const SkColor kHoverMenuItemBackgroundColor =
34 SkColorSetARGB(204, 255, 255, 255);
35 // Button:
36 const SkColor kButtonHoverBackgroundColor = SkColorSetRGB(0xEA, 0xEA, 0xEA);
37 const SkColor kBlueButtonEnabledColor = SK_ColorWHITE;
38 const SkColor kBlueButtonDisabledColor = SK_ColorWHITE;
39 const SkColor kBlueButtonPressedColor = SK_ColorWHITE;
40 const SkColor kBlueButtonHoverColor = SK_ColorWHITE;
41 const SkColor kBlueButtonShadowColor = SkColorSetRGB(0x53, 0x8C, 0xEA);
42 // Material colors:
43 const SkColor kGoogleBlue = SkColorSetRGB(0x42, 0x85, 0xF4);
44 const SkColor kChromeIconGrey = SkColorSetRGB(0x5A, 0x5A, 0x5A);
45 // Material spinner/throbber:
46 const SkColor kThrobberWaitingColor = SkColorSetRGB(0xA6, 0xA6, 0xA6);
47 const SkColor kThrobberLightColor = SkColorSetRGB(0xF4, 0xF8, 0xFD);
49 } // namespace
51 namespace ui {
53 bool CommonThemeGetSystemColor(NativeTheme::ColorId color_id, SkColor* color) {
54 switch (color_id) {
55 // MenuItem
56 case NativeTheme::kColorId_MenuBorderColor:
57 *color = kMenuBorderColor;
58 break;
59 case NativeTheme::kColorId_EnabledMenuButtonBorderColor:
60 *color = kEnabledMenuButtonBorderColor;
61 break;
62 case NativeTheme::kColorId_FocusedMenuButtonBorderColor:
63 *color = kFocusedMenuButtonBorderColor;
64 break;
65 case NativeTheme::kColorId_HoverMenuButtonBorderColor:
66 *color = kHoverMenuButtonBorderColor;
67 break;
68 case NativeTheme::kColorId_MenuSeparatorColor:
69 *color = kMenuSeparatorColor;
70 break;
71 case NativeTheme::kColorId_MenuBackgroundColor:
72 *color = kMenuBackgroundColor;
73 break;
74 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor:
75 *color = kMenuHighlightBackgroundColor;
76 break;
77 case NativeTheme::kColorId_HoverMenuItemBackgroundColor:
78 *color = kHoverMenuItemBackgroundColor;
79 break;
80 case NativeTheme::kColorId_EnabledMenuItemForegroundColor:
81 *color = kEnabledMenuItemForegroundColor;
82 break;
83 case NativeTheme::kColorId_DisabledMenuItemForegroundColor:
84 *color = kDisabledMenuItemForegroundColor;
85 break;
86 case NativeTheme::kColorId_DisabledEmphasizedMenuItemForegroundColor:
87 *color = SK_ColorBLACK;
88 break;
89 case NativeTheme::kColorId_SelectedMenuItemForegroundColor:
90 *color = SK_ColorWHITE;
91 break;
92 case NativeTheme::kColorId_ButtonDisabledColor:
93 *color = kDisabledMenuItemForegroundColor;
94 break;
95 // Button
96 case NativeTheme::kColorId_ButtonHoverBackgroundColor:
97 *color = kButtonHoverBackgroundColor;
98 break;
99 case NativeTheme::kColorId_BlueButtonEnabledColor:
100 *color = kBlueButtonEnabledColor;
101 break;
102 case NativeTheme::kColorId_BlueButtonDisabledColor:
103 *color = kBlueButtonDisabledColor;
104 break;
105 case NativeTheme::kColorId_BlueButtonPressedColor:
106 *color = kBlueButtonPressedColor;
107 break;
108 case NativeTheme::kColorId_BlueButtonHoverColor:
109 *color = kBlueButtonHoverColor;
110 break;
111 case NativeTheme::kColorId_BlueButtonShadowColor:
112 *color = kBlueButtonShadowColor;
113 break;
114 // Material icons
115 case NativeTheme::kColorId_ChromeIconGrey:
116 *color = kChromeIconGrey;
117 break;
118 case NativeTheme::kColorId_GoogleBlue:
119 *color = kGoogleBlue;
120 break;
121 // Material spinner/throbber
122 case NativeTheme::kColorId_ThrobberSpinningColor:
123 *color = kGoogleBlue;
124 break;
125 case NativeTheme::kColorId_ThrobberWaitingColor:
126 *color = kThrobberWaitingColor;
127 break;
128 case NativeTheme::kColorId_ThrobberLightColor:
129 *color = kThrobberLightColor;
130 break;
131 default:
132 return false;
135 if (color_utils::IsInvertedColorScheme()) {
136 switch (color_id) {
137 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor:
138 *color = kMenuInvertedSchemeHighlightBackgroundColor;
139 break;
140 default:
141 *color = color_utils::InvertColor(*color);
144 return true;
147 gfx::Size CommonThemeGetPartSize(NativeTheme::Part part,
148 NativeTheme::State state,
149 const NativeTheme::ExtraParams& extra) {
150 gfx::Size size;
151 switch (part) {
152 case NativeTheme::kComboboxArrow:
153 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
154 IDR_MENU_DROPARROW).Size();
156 case NativeTheme::kMenuCheck: {
157 const gfx::ImageSkia* check =
158 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
159 IDR_MENU_CHECK_CHECKED).ToImageSkia();
160 size.SetSize(check->width(), check->height());
161 break;
163 default:
164 break;
167 return size;
170 void CommonThemePaintComboboxArrow(SkCanvas* canvas, const gfx::Rect& rect) {
171 gfx::ImageSkia* arrow = ui::ResourceBundle::GetSharedInstance().
172 GetImageSkiaNamed(IDR_MENU_DROPARROW);
173 CommonThemeCreateCanvas(canvas)->DrawImageInt(*arrow, rect.x(), rect.y());
176 void CommonThemePaintMenuSeparator(SkCanvas* canvas, const gfx::Rect& rect) {
177 SkColor color;
178 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor, &color);
179 SkPaint paint;
180 paint.setColor(kMenuSeparatorColor);
181 int position_y = rect.y() + rect.height() / 2;
182 canvas->drawLine(rect.x(), position_y, rect.right(), position_y, paint);
185 void CommonThemePaintMenuGutter(SkCanvas* canvas, const gfx::Rect& rect) {
186 SkColor color;
187 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor, &color);
188 SkPaint paint;
189 paint.setColor(kMenuSeparatorColor);
190 int position_x = rect.x() + rect.width() / 2;
191 canvas->drawLine(position_x, rect.y(), position_x, rect.bottom(), paint);
194 void CommonThemePaintMenuBackground(SkCanvas* canvas, const gfx::Rect& rect) {
195 SkColor color;
196 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor, &color);
197 SkPaint paint;
198 paint.setColor(color);
199 canvas->drawRect(gfx::RectToSkRect(rect), paint);
202 void CommonThemePaintMenuItemBackground(SkCanvas* canvas,
203 NativeTheme::State state,
204 const gfx::Rect& rect) {
205 SkColor color;
206 SkPaint paint;
207 switch (state) {
208 case NativeTheme::kNormal:
209 case NativeTheme::kDisabled:
210 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor,
211 &color);
212 paint.setColor(color);
213 break;
214 case NativeTheme::kHovered:
215 CommonThemeGetSystemColor(
216 NativeTheme::kColorId_FocusedMenuItemBackgroundColor, &color);
217 paint.setColor(color);
218 break;
219 default:
220 NOTREACHED() << "Invalid state " << state;
221 break;
223 canvas->drawRect(gfx::RectToSkRect(rect), paint);
226 // static
227 scoped_ptr<gfx::Canvas> CommonThemeCreateCanvas(SkCanvas* sk_canvas) {
228 // TODO(pkotwicz): Do something better and don't infer device
229 // scale factor from canvas scale.
230 SkMatrix m = sk_canvas->getTotalMatrix();
231 float device_scale = static_cast<float>(SkScalarAbs(m.getScaleX()));
232 return make_scoped_ptr(new gfx::Canvas(sk_canvas, device_scale));
235 } // namespace ui