mac: Fix a bug in ui_gfx_render_text_mac.
[chromium-blink-merge.git] / ui / native_theme / common_theme.cc
blobfdd297458c4965ae9733a7f4f19f527010c2a23a
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 spinner/throbber:
43 const SkColor kThrobberSpinningColor = SkColorSetRGB(0x42, 0x81, 0xF4);
44 const SkColor kThrobberWaitingColor = SkColorSetRGB(0xA6, 0xA6, 0xA6);
45 const SkColor kThrobberLightColor = SkColorSetRGB(0xF4, 0xF8, 0xFD);
47 } // namespace
49 namespace ui {
51 bool CommonThemeGetSystemColor(NativeTheme::ColorId color_id, SkColor* color) {
52 switch (color_id) {
53 // MenuItem
54 case NativeTheme::kColorId_MenuBorderColor:
55 *color = kMenuBorderColor;
56 break;
57 case NativeTheme::kColorId_EnabledMenuButtonBorderColor:
58 *color = kEnabledMenuButtonBorderColor;
59 break;
60 case NativeTheme::kColorId_FocusedMenuButtonBorderColor:
61 *color = kFocusedMenuButtonBorderColor;
62 break;
63 case NativeTheme::kColorId_HoverMenuButtonBorderColor:
64 *color = kHoverMenuButtonBorderColor;
65 break;
66 case NativeTheme::kColorId_MenuSeparatorColor:
67 *color = kMenuSeparatorColor;
68 break;
69 case NativeTheme::kColorId_MenuBackgroundColor:
70 *color = kMenuBackgroundColor;
71 break;
72 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor:
73 *color = kMenuHighlightBackgroundColor;
74 break;
75 case NativeTheme::kColorId_HoverMenuItemBackgroundColor:
76 *color = kHoverMenuItemBackgroundColor;
77 break;
78 case NativeTheme::kColorId_EnabledMenuItemForegroundColor:
79 *color = kEnabledMenuItemForegroundColor;
80 break;
81 case NativeTheme::kColorId_DisabledMenuItemForegroundColor:
82 *color = kDisabledMenuItemForegroundColor;
83 break;
84 case NativeTheme::kColorId_DisabledEmphasizedMenuItemForegroundColor:
85 *color = SK_ColorBLACK;
86 break;
87 case NativeTheme::kColorId_SelectedMenuItemForegroundColor:
88 *color = SK_ColorWHITE;
89 break;
90 case NativeTheme::kColorId_ButtonDisabledColor:
91 *color = kDisabledMenuItemForegroundColor;
92 break;
93 // Button
94 case NativeTheme::kColorId_ButtonHoverBackgroundColor:
95 *color = kButtonHoverBackgroundColor;
96 break;
97 case NativeTheme::kColorId_BlueButtonEnabledColor:
98 *color = kBlueButtonEnabledColor;
99 break;
100 case NativeTheme::kColorId_BlueButtonDisabledColor:
101 *color = kBlueButtonDisabledColor;
102 break;
103 case NativeTheme::kColorId_BlueButtonPressedColor:
104 *color = kBlueButtonPressedColor;
105 break;
106 case NativeTheme::kColorId_BlueButtonHoverColor:
107 *color = kBlueButtonHoverColor;
108 break;
109 case NativeTheme::kColorId_BlueButtonShadowColor:
110 *color = kBlueButtonShadowColor;
111 break;
112 // Material spinner/throbber
113 case NativeTheme::kColorId_ThrobberSpinningColor:
114 *color = kThrobberSpinningColor;
115 break;
116 case NativeTheme::kColorId_ThrobberWaitingColor:
117 *color = kThrobberWaitingColor;
118 break;
119 case NativeTheme::kColorId_ThrobberLightColor:
120 *color = kThrobberLightColor;
121 break;
122 default:
123 return false;
126 if (color_utils::IsInvertedColorScheme()) {
127 switch (color_id) {
128 case NativeTheme::kColorId_FocusedMenuItemBackgroundColor:
129 *color = kMenuInvertedSchemeHighlightBackgroundColor;
130 break;
131 default:
132 *color = color_utils::InvertColor(*color);
135 return true;
138 gfx::Size CommonThemeGetPartSize(NativeTheme::Part part,
139 NativeTheme::State state,
140 const NativeTheme::ExtraParams& extra) {
141 gfx::Size size;
142 switch (part) {
143 case NativeTheme::kComboboxArrow:
144 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
145 IDR_MENU_DROPARROW).Size();
147 case NativeTheme::kMenuCheck: {
148 const gfx::ImageSkia* check =
149 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
150 IDR_MENU_CHECK_CHECKED).ToImageSkia();
151 size.SetSize(check->width(), check->height());
152 break;
154 default:
155 break;
158 return size;
161 void CommonThemePaintComboboxArrow(SkCanvas* canvas, const gfx::Rect& rect) {
162 gfx::ImageSkia* arrow = ui::ResourceBundle::GetSharedInstance().
163 GetImageSkiaNamed(IDR_MENU_DROPARROW);
164 CommonThemeCreateCanvas(canvas)->DrawImageInt(*arrow, rect.x(), rect.y());
167 void CommonThemePaintMenuSeparator(SkCanvas* canvas, const gfx::Rect& rect) {
168 SkColor color;
169 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuSeparatorColor, &color);
170 SkPaint paint;
171 paint.setColor(kMenuSeparatorColor);
172 int position_y = rect.y() + rect.height() / 2;
173 canvas->drawLine(rect.x(), position_y, rect.right(), position_y, paint);
176 void CommonThemePaintMenuGutter(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_x = rect.x() + rect.width() / 2;
182 canvas->drawLine(position_x, rect.y(), position_x, rect.bottom(), paint);
185 void CommonThemePaintMenuBackground(SkCanvas* canvas, const gfx::Rect& rect) {
186 SkColor color;
187 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor, &color);
188 SkPaint paint;
189 paint.setColor(color);
190 canvas->drawRect(gfx::RectToSkRect(rect), paint);
193 void CommonThemePaintMenuItemBackground(SkCanvas* canvas,
194 NativeTheme::State state,
195 const gfx::Rect& rect) {
196 SkColor color;
197 SkPaint paint;
198 switch (state) {
199 case NativeTheme::kNormal:
200 case NativeTheme::kDisabled:
201 CommonThemeGetSystemColor(NativeTheme::kColorId_MenuBackgroundColor,
202 &color);
203 paint.setColor(color);
204 break;
205 case NativeTheme::kHovered:
206 CommonThemeGetSystemColor(
207 NativeTheme::kColorId_FocusedMenuItemBackgroundColor, &color);
208 paint.setColor(color);
209 break;
210 default:
211 NOTREACHED() << "Invalid state " << state;
212 break;
214 canvas->drawRect(gfx::RectToSkRect(rect), paint);
217 // static
218 scoped_ptr<gfx::Canvas> CommonThemeCreateCanvas(SkCanvas* sk_canvas) {
219 // TODO(pkotwicz): Do something better and don't infer device
220 // scale factor from canvas scale.
221 SkMatrix m = sk_canvas->getTotalMatrix();
222 float device_scale = static_cast<float>(SkScalarAbs(m.getScaleX()));
223 return make_scoped_ptr(new gfx::Canvas(sk_canvas, device_scale));
226 } // namespace ui