OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "gfx/native_theme_linux.h" | 5 #include "gfx/native_theme_linux.h" |
6 | 6 |
| 7 #include <limits> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "gfx/codec/png_codec.h" |
| 11 #include "gfx/color_utils.h" |
| 12 #include "gfx/gfx_module.h" |
8 #include "gfx/size.h" | 13 #include "gfx/size.h" |
9 #include "gfx/rect.h" | 14 #include "gfx/rect.h" |
| 15 #include "grit/gfx_resources.h" |
| 16 #include "third_party/skia/include/effects/SkGradientShader.h" |
10 | 17 |
11 namespace gfx { | 18 namespace gfx { |
12 | 19 |
13 unsigned int NativeThemeLinux::button_length_ = 14; | 20 unsigned int NativeThemeLinux::button_length_ = 14; |
14 unsigned int NativeThemeLinux::scrollbar_width_ = 15; | 21 unsigned int NativeThemeLinux::scrollbar_width_ = 15; |
15 unsigned int NativeThemeLinux::thumb_inactive_color_ = 0xeaeaea; | 22 unsigned int NativeThemeLinux::thumb_inactive_color_ = 0xeaeaea; |
16 unsigned int NativeThemeLinux::thumb_active_color_ = 0xf4f4f4; | 23 unsigned int NativeThemeLinux::thumb_active_color_ = 0xf4f4f4; |
17 unsigned int NativeThemeLinux::track_color_ = 0xd3d3d3; | 24 unsigned int NativeThemeLinux::track_color_ = 0xd3d3d3; |
18 | 25 |
| 26 // These are the default dimensions of radio buttons and checkboxes. |
| 27 static const int kCheckboxAndRadioWidth = 13; |
| 28 static const int kCheckboxAndRadioHeight = 13; |
| 29 |
| 30 // These sizes match the sizes in Chromium Win. |
| 31 static const int kSliderThumbWidth = 11; |
| 32 static const int kSliderThumbHeight = 21; |
| 33 |
| 34 static const SkColor kSliderTrackBackgroundColor = |
| 35 SkColorSetRGB(0xe3, 0xdd, 0xd8); |
| 36 static const SkColor kSliderThumbLightGrey = SkColorSetRGB(0xf4, 0xf2, 0xef); |
| 37 static const SkColor kSliderThumbDarkGrey = SkColorSetRGB(0xea, 0xe5, 0xe0); |
| 38 static const SkColor kSliderThumbBorderDarkGrey = |
| 39 SkColorSetRGB(0x9d, 0x96, 0x8e); |
| 40 |
19 #if !defined(OS_CHROMEOS) | 41 #if !defined(OS_CHROMEOS) |
20 // Chromeos has a different look. | 42 // Chromeos has a different look. |
21 // static | 43 // static |
22 NativeThemeLinux* NativeThemeLinux::instance() { | 44 NativeThemeLinux* NativeThemeLinux::instance() { |
23 // The global NativeThemeLinux instance. | 45 // The global NativeThemeLinux instance. |
24 static NativeThemeLinux s_native_theme; | 46 static NativeThemeLinux s_native_theme; |
25 return &s_native_theme; | 47 return &s_native_theme; |
26 } | 48 } |
27 #endif | 49 #endif |
28 | 50 |
| 51 // Get lightness adjusted color. |
| 52 static SkColor BrightenColor(const color_utils::HSL& hsl, SkAlpha alpha, |
| 53 double lightness_amount) { |
| 54 color_utils::HSL adjusted = hsl; |
| 55 adjusted.l += lightness_amount; |
| 56 if (adjusted.l > 1.0) |
| 57 adjusted.l = 1.0; |
| 58 if (adjusted.l < 0.0) |
| 59 adjusted.l = 0.0; |
| 60 |
| 61 return color_utils::HSLToSkColor(adjusted, alpha); |
| 62 } |
| 63 |
| 64 static SkBitmap* GfxGetBitmapNamed(int key) { |
| 65 base::StringPiece data = GfxModule::GetResource(key); |
| 66 if (!data.size()) { |
| 67 NOTREACHED() << "Unable to load image resource " << key; |
| 68 return NULL; |
| 69 } |
| 70 |
| 71 SkBitmap bitmap; |
| 72 if (!gfx::PNGCodec::Decode( |
| 73 reinterpret_cast<const unsigned char*>(data.data()), |
| 74 data.size(), &bitmap)) { |
| 75 NOTREACHED() << "Unable to decode image resource " << key; |
| 76 return NULL; |
| 77 } |
| 78 |
| 79 return new SkBitmap(bitmap); |
| 80 } |
| 81 |
29 NativeThemeLinux::NativeThemeLinux() { | 82 NativeThemeLinux::NativeThemeLinux() { |
30 } | 83 } |
31 | 84 |
32 NativeThemeLinux::~NativeThemeLinux() { | 85 NativeThemeLinux::~NativeThemeLinux() { |
33 } | 86 } |
34 | 87 |
35 gfx::Size NativeThemeLinux::GetSize(Part part) const { | 88 gfx::Size NativeThemeLinux::GetPartSize(Part part) const { |
36 switch (part) { | 89 switch (part) { |
37 case kScrollbarDownArrow: | 90 case kScrollbarDownArrow: |
38 case kScrollbarUpArrow: | 91 case kScrollbarUpArrow: |
39 return gfx::Size(scrollbar_width_, button_length_); | 92 return gfx::Size(scrollbar_width_, button_length_); |
40 case kScrollbarLeftArrow: | 93 case kScrollbarLeftArrow: |
41 case kScrollbarRightArrow: | 94 case kScrollbarRightArrow: |
42 return gfx::Size(button_length_, scrollbar_width_); | 95 return gfx::Size(button_length_, scrollbar_width_); |
43 case kScrollbarHorizontalThumb: | 96 case kScrollbarHorizontalThumb: |
44 // This matches Firefox on Linux. | 97 // This matches Firefox on Linux. |
45 return gfx::Size(2 * scrollbar_width_, scrollbar_width_); | 98 return gfx::Size(2 * scrollbar_width_, scrollbar_width_); |
46 case kScrollbarVerticalThumb: | 99 case kScrollbarVerticalThumb: |
47 // This matches Firefox on Linux. | 100 // This matches Firefox on Linux. |
48 return gfx::Size(scrollbar_width_, 2 * scrollbar_width_); | 101 return gfx::Size(scrollbar_width_, 2 * scrollbar_width_); |
49 break; | 102 break; |
50 case kScrollbarHorizontalTrack: | 103 case kScrollbarHorizontalTrack: |
51 return gfx::Size(0, scrollbar_width_); | 104 return gfx::Size(0, scrollbar_width_); |
52 case kScrollbarVerticalTrack: | 105 case kScrollbarVerticalTrack: |
53 return gfx::Size(scrollbar_width_, 0); | 106 return gfx::Size(scrollbar_width_, 0); |
| 107 case kCheckbox: |
| 108 case kRadio: |
| 109 return gfx::Size(kCheckboxAndRadioWidth, kCheckboxAndRadioHeight); |
| 110 case kSliderThumb: |
| 111 // These sizes match the sizes in Chromium Win. |
| 112 return gfx::Size(kSliderThumbWidth, kSliderThumbHeight); |
| 113 case kInnerSpinButton: |
| 114 return gfx::Size(scrollbar_width_, 0); |
| 115 case kPushButton: |
| 116 case kTextField: |
| 117 case kMenuList: |
| 118 case kSliderTrack: |
| 119 case kProgressBar: |
| 120 return gfx::Size(); // No default size. |
54 } | 121 } |
55 return gfx::Size(); | 122 return gfx::Size(); |
56 } | 123 } |
57 | 124 |
58 void NativeThemeLinux::PaintArrowButton( | 125 void NativeThemeLinux::PaintArrowButton( |
59 skia::PlatformCanvas* canvas, | 126 skia::PlatformCanvas* canvas, |
60 const gfx::Rect& rect, Part direction, State state) { | 127 const gfx::Rect& rect, Part direction, State state) { |
61 int widthMiddle, lengthMiddle; | 128 int widthMiddle, lengthMiddle; |
62 SkPaint paint; | 129 SkPaint paint; |
63 if (direction == kScrollbarUpArrow || direction == kScrollbarDownArrow) { | 130 if (direction == kScrollbarUpArrow || direction == kScrollbarDownArrow) { |
64 widthMiddle = rect.width() / 2 + 1; | 131 widthMiddle = rect.width() / 2 + 1; |
65 lengthMiddle = rect.height() / 2 + 1; | 132 lengthMiddle = rect.height() / 2 + 1; |
66 } else { | 133 } else { |
67 lengthMiddle = rect.width() / 2 + 1; | 134 lengthMiddle = rect.width() / 2 + 1; |
68 widthMiddle = rect.height() / 2 + 1; | 135 widthMiddle = rect.height() / 2 + 1; |
69 } | 136 } |
70 | 137 |
71 // Calculate button color. | 138 // Calculate button color. |
72 SkScalar trackHSV[3]; | 139 SkScalar trackHSV[3]; |
73 SkColorToHSV(track_color_, trackHSV); | 140 SkColorToHSV(track_color_, trackHSV); |
74 SkColor buttonColor = SaturateAndBrighten(trackHSV, 0, 0.2); | 141 SkColor buttonColor = SaturateAndBrighten(trackHSV, 0, 0.2); |
75 SkColor backgroundColor = buttonColor; | 142 SkColor backgroundColor = buttonColor; |
76 if (state == kPressed) { | 143 if (state == kPressed) { |
77 SkScalar buttonHSV[3]; | 144 SkScalar buttonHSV[3]; |
78 SkColorToHSV(buttonColor, buttonHSV); | 145 SkColorToHSV(buttonColor, buttonHSV); |
79 buttonColor = SaturateAndBrighten(buttonHSV, 0, -0.1); | 146 buttonColor = SaturateAndBrighten(buttonHSV, 0, -0.1); |
80 } else if (state == kHover) { | 147 } else if (state == kHovered) { |
81 SkScalar buttonHSV[3]; | 148 SkScalar buttonHSV[3]; |
82 SkColorToHSV(buttonColor, buttonHSV); | 149 SkColorToHSV(buttonColor, buttonHSV); |
83 buttonColor = SaturateAndBrighten(buttonHSV, 0, 0.05); | 150 buttonColor = SaturateAndBrighten(buttonHSV, 0, 0.05); |
84 } | 151 } |
85 | 152 |
86 SkIRect skrect; | 153 SkIRect skrect; |
87 skrect.set(rect.x(), rect.y(), rect.x() + rect.width(), rect.y() | 154 skrect.set(rect.x(), rect.y(), rect.x() + rect.width(), rect.y() |
88 + rect.height()); | 155 + rect.height()); |
89 // Paint the background (the area visible behind the rounded corners). | 156 // Paint the background (the area visible behind the rounded corners). |
90 paint.setColor(backgroundColor); | 157 paint.setColor(backgroundColor); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 const ExtraParams& extra) { | 255 const ExtraParams& extra) { |
189 switch (part) { | 256 switch (part) { |
190 case kScrollbarDownArrow: | 257 case kScrollbarDownArrow: |
191 case kScrollbarUpArrow: | 258 case kScrollbarUpArrow: |
192 case kScrollbarLeftArrow: | 259 case kScrollbarLeftArrow: |
193 case kScrollbarRightArrow: | 260 case kScrollbarRightArrow: |
194 PaintArrowButton(canvas, rect, part, state); | 261 PaintArrowButton(canvas, rect, part, state); |
195 break; | 262 break; |
196 case kScrollbarHorizontalThumb: | 263 case kScrollbarHorizontalThumb: |
197 case kScrollbarVerticalThumb: | 264 case kScrollbarVerticalThumb: |
198 PaintThumb(canvas, part, state, rect); | 265 PaintScrollbarThumb(canvas, part, state, rect); |
199 break; | 266 break; |
200 case kScrollbarHorizontalTrack: | 267 case kScrollbarHorizontalTrack: |
201 case kScrollbarVerticalTrack: | 268 case kScrollbarVerticalTrack: |
202 PaintTrack(canvas, part, state, extra.scrollbar_track, rect); | 269 PaintScrollbarTrack(canvas, part, state, extra.scrollbar_track, rect); |
| 270 break; |
| 271 case kCheckbox: |
| 272 PaintCheckbox(canvas, state, rect, extra.button); |
| 273 break; |
| 274 case kRadio: |
| 275 PaintRadio(canvas, state, rect, extra.button); |
| 276 break; |
| 277 case kPushButton: |
| 278 PaintButton(canvas, state, rect, extra.button); |
| 279 break; |
| 280 case kTextField: |
| 281 PaintTextField(canvas, state, rect, extra.text_field); |
| 282 break; |
| 283 case kMenuList: |
| 284 PaintMenuList(canvas, state, rect, extra.menu_list); |
| 285 break; |
| 286 case kSliderTrack: |
| 287 PaintSliderTrack(canvas, state, rect, extra.slider); |
| 288 break; |
| 289 case kSliderThumb: |
| 290 PaintSliderThumb(canvas, state, rect, extra.slider); |
| 291 break; |
| 292 case kInnerSpinButton: |
| 293 PaintInnerSpinButton(canvas, state, rect, extra.inner_spin); |
| 294 break; |
| 295 case kProgressBar: |
| 296 PaintProgressBar(canvas, state, rect, extra.progress_bar); |
203 break; | 297 break; |
204 } | 298 } |
205 } | 299 } |
206 | 300 |
207 void NativeThemeLinux::PaintTrack(skia::PlatformCanvas* canvas, | 301 void NativeThemeLinux::PaintScrollbarTrack(skia::PlatformCanvas* canvas, |
208 Part part, | 302 Part part, |
209 State state, | 303 State state, |
210 const ScrollbarTrackExtraParams& extra_params, | 304 const ScrollbarTrackExtraParams& extra_params, |
211 const gfx::Rect& rect) { | 305 const gfx::Rect& rect) { |
212 SkPaint paint; | 306 SkPaint paint; |
213 SkIRect skrect; | 307 SkIRect skrect; |
214 | 308 |
215 skrect.set(rect.x(), rect.y(), rect.right(), rect.bottom()); | 309 skrect.set(rect.x(), rect.y(), rect.right(), rect.bottom()); |
216 SkScalar track_hsv[3]; | 310 SkScalar track_hsv[3]; |
217 SkColorToHSV(track_color_, track_hsv); | 311 SkColorToHSV(track_color_, track_hsv); |
218 paint.setColor(SaturateAndBrighten(track_hsv, 0, 0)); | 312 paint.setColor(SaturateAndBrighten(track_hsv, 0, 0)); |
219 canvas->drawIRect(skrect, paint); | 313 canvas->drawIRect(skrect, paint); |
220 | 314 |
221 SkScalar thumb_hsv[3]; | 315 SkScalar thumb_hsv[3]; |
222 SkColorToHSV(thumb_inactive_color_, thumb_hsv); | 316 SkColorToHSV(thumb_inactive_color_, thumb_hsv); |
223 | 317 |
224 paint.setColor(OutlineColor(track_hsv, thumb_hsv)); | 318 paint.setColor(OutlineColor(track_hsv, thumb_hsv)); |
225 DrawBox(canvas, rect, paint); | 319 DrawBox(canvas, rect, paint); |
226 } | 320 } |
227 | 321 |
228 void NativeThemeLinux::PaintThumb(skia::PlatformCanvas* canvas, | 322 void NativeThemeLinux::PaintScrollbarThumb(skia::PlatformCanvas* canvas, |
229 Part part, | 323 Part part, |
230 State state, | 324 State state, |
231 const gfx::Rect& rect) { | 325 const gfx::Rect& rect) { |
232 const bool hovered = state == kHover; | 326 const bool hovered = state == kHovered; |
233 const int midx = rect.x() + rect.width() / 2; | 327 const int midx = rect.x() + rect.width() / 2; |
234 const int midy = rect.y() + rect.height() / 2; | 328 const int midy = rect.y() + rect.height() / 2; |
235 const bool vertical = part == kScrollbarVerticalThumb; | 329 const bool vertical = part == kScrollbarVerticalThumb; |
236 | 330 |
237 SkScalar thumb[3]; | 331 SkScalar thumb[3]; |
238 SkColorToHSV(hovered ? thumb_active_color_ : thumb_inactive_color_, thumb); | 332 SkColorToHSV(hovered ? thumb_active_color_ : thumb_inactive_color_, thumb); |
239 | 333 |
240 SkPaint paint; | 334 SkPaint paint; |
241 paint.setColor(SaturateAndBrighten(thumb, 0, 0.02)); | 335 paint.setColor(SaturateAndBrighten(thumb, 0, 0.02)); |
242 | 336 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 paint); | 391 paint); |
298 DrawVertLine(canvas, | 392 DrawVertLine(canvas, |
299 midx + inter_grippy_offset, | 393 midx + inter_grippy_offset, |
300 midy - grippy_half_width, | 394 midy - grippy_half_width, |
301 midy + grippy_half_width, | 395 midy + grippy_half_width, |
302 paint); | 396 paint); |
303 } | 397 } |
304 } | 398 } |
305 } | 399 } |
306 | 400 |
| 401 void NativeThemeLinux::PaintCheckbox(skia::PlatformCanvas* canvas, |
| 402 State state, |
| 403 const gfx::Rect& rect, |
| 404 const ButtonExtraParams& button) { |
| 405 static SkBitmap* image_disabled_indeterminate = GfxGetBitmapNamed( |
| 406 IDR_LINUX_CHECKBOX_DISABLED_INDETERMINATE); |
| 407 static SkBitmap* image_indeterminate = GfxGetBitmapNamed( |
| 408 IDR_LINUX_CHECKBOX_INDETERMINATE); |
| 409 static SkBitmap* image_disabled_on = GfxGetBitmapNamed( |
| 410 IDR_LINUX_CHECKBOX_DISABLED_ON); |
| 411 static SkBitmap* image_on = GfxGetBitmapNamed(IDR_LINUX_CHECKBOX_ON); |
| 412 static SkBitmap* image_disabled_off = GfxGetBitmapNamed( |
| 413 IDR_LINUX_CHECKBOX_DISABLED_OFF); |
| 414 static SkBitmap* image_off = GfxGetBitmapNamed(IDR_LINUX_CHECKBOX_OFF); |
| 415 |
| 416 SkBitmap* image = NULL; |
| 417 if (button.indeterminate) { |
| 418 image = state == kDisabled ? image_disabled_indeterminate |
| 419 : image_indeterminate; |
| 420 } else if (button.checked) { |
| 421 image = state == kDisabled ? image_disabled_on : image_on; |
| 422 } else { |
| 423 image = state == kDisabled ? image_disabled_off : image_off; |
| 424 } |
| 425 |
| 426 gfx::Rect bounds = rect.Center(gfx::Size(image->width(), image->height())); |
| 427 DrawBitmapInt(canvas, *image, 0, 0, image->width(), image->height(), |
| 428 bounds.x(), bounds.y(), bounds.width(), bounds.height()); |
| 429 } |
| 430 |
| 431 void NativeThemeLinux::PaintRadio(skia::PlatformCanvas* canvas, |
| 432 State state, |
| 433 const gfx::Rect& rect, |
| 434 const ButtonExtraParams& button) { |
| 435 static SkBitmap* image_disabled_on = GfxGetBitmapNamed( |
| 436 IDR_LINUX_RADIO_DISABLED_ON); |
| 437 static SkBitmap* image_on = GfxGetBitmapNamed(IDR_LINUX_RADIO_ON); |
| 438 static SkBitmap* image_disabled_off = GfxGetBitmapNamed( |
| 439 IDR_LINUX_RADIO_DISABLED_OFF); |
| 440 static SkBitmap* image_off = GfxGetBitmapNamed(IDR_LINUX_RADIO_OFF); |
| 441 |
| 442 SkBitmap* image = NULL; |
| 443 if (state == kDisabled) |
| 444 image = button.checked ? image_disabled_on : image_disabled_off; |
| 445 else |
| 446 image = button.checked ? image_on : image_off; |
| 447 |
| 448 gfx::Rect bounds = rect.Center(gfx::Size(image->width(), image->height())); |
| 449 DrawBitmapInt(canvas, *image, 0, 0, image->width(), image->height(), |
| 450 bounds.x(), bounds.y(), bounds.width(), bounds.height()); |
| 451 } |
| 452 |
| 453 void NativeThemeLinux::PaintButton(skia::PlatformCanvas* canvas, |
| 454 State state, |
| 455 const gfx::Rect& rect, |
| 456 const ButtonExtraParams& button) { |
| 457 SkPaint paint; |
| 458 SkRect skrect; |
| 459 const int kRight = rect.right(); |
| 460 const int kBottom = rect.bottom(); |
| 461 SkColor base_color = button.background_color; |
| 462 |
| 463 color_utils::HSL base_hsl; |
| 464 color_utils::SkColorToHSL(base_color, &base_hsl); |
| 465 |
| 466 // Our standard gradient is from 0xdd to 0xf8. This is the amount of |
| 467 // increased luminance between those values. |
| 468 SkColor light_color(BrightenColor(base_hsl, SkColorGetA(base_color), 0.105)); |
| 469 |
| 470 // If the button is too small, fallback to drawing a single, solid color |
| 471 if (rect.width() < 5 || rect.height() < 5) { |
| 472 paint.setColor(base_color); |
| 473 skrect.set(rect.x(), rect.y(), kRight, kBottom); |
| 474 canvas->drawRect(skrect, paint); |
| 475 return; |
| 476 } |
| 477 |
| 478 const int kBorderAlpha = state == kHovered ? 0x80 : 0x55; |
| 479 paint.setARGB(kBorderAlpha, 0, 0, 0); |
| 480 canvas->drawLine(rect.x() + 1, rect.y(), kRight - 1, rect.y(), paint); |
| 481 canvas->drawLine(kRight - 1, rect.y() + 1, kRight - 1, kBottom - 1, paint); |
| 482 canvas->drawLine(rect.x() + 1, kBottom - 1, kRight - 1, kBottom - 1, paint); |
| 483 canvas->drawLine(rect.x(), rect.y() + 1, rect.x(), kBottom - 1, paint); |
| 484 |
| 485 paint.setColor(SK_ColorBLACK); |
| 486 const int kLightEnd = state == kPressed ? 1 : 0; |
| 487 const int kDarkEnd = !kLightEnd; |
| 488 SkPoint gradient_bounds[2]; |
| 489 gradient_bounds[kLightEnd].set(SkIntToScalar(rect.x()), |
| 490 SkIntToScalar(rect.y())); |
| 491 gradient_bounds[kDarkEnd].set(SkIntToScalar(rect.x()), |
| 492 SkIntToScalar(kBottom - 1)); |
| 493 SkColor colors[2]; |
| 494 colors[0] = light_color; |
| 495 colors[1] = base_color; |
| 496 |
| 497 SkShader* shader = SkGradientShader::CreateLinear( |
| 498 gradient_bounds, colors, NULL, 2, SkShader::kClamp_TileMode, NULL); |
| 499 paint.setStyle(SkPaint::kFill_Style); |
| 500 paint.setShader(shader); |
| 501 shader->unref(); |
| 502 |
| 503 skrect.set(rect.x() + 1, rect.y() + 1, kRight - 1, kBottom - 1); |
| 504 canvas->drawRect(skrect, paint); |
| 505 |
| 506 paint.setShader(NULL); |
| 507 paint.setColor(BrightenColor(base_hsl, SkColorGetA(base_color), -0.0588)); |
| 508 canvas->drawPoint(rect.x() + 1, rect.y() + 1, paint); |
| 509 canvas->drawPoint(kRight - 2, rect.y() + 1, paint); |
| 510 canvas->drawPoint(rect.x() + 1, kBottom - 2, paint); |
| 511 canvas->drawPoint(kRight - 2, kBottom - 2, paint); |
| 512 } |
| 513 |
| 514 void NativeThemeLinux::PaintTextField(skia::PlatformCanvas* canvas, |
| 515 State state, |
| 516 const gfx::Rect& rect, |
| 517 const TextFieldExtraParams& text) { |
| 518 // The following drawing code simulates the user-agent css border for |
| 519 // text area and text input so that we do not break layout tests. Once we |
| 520 // have decided the desired looks, we should update the code here and |
| 521 // the layout test expectations. |
| 522 SkRect bounds; |
| 523 bounds.set(rect.x(), rect.y(), rect.right() - 1, rect.bottom() - 1); |
| 524 |
| 525 SkPaint fill_paint; |
| 526 fill_paint.setStyle(SkPaint::kFill_Style); |
| 527 fill_paint.setColor(text.background_color); |
| 528 canvas->drawRect(bounds, fill_paint); |
| 529 |
| 530 if (text.is_text_area) { |
| 531 // Draw text area border: 1px solid black |
| 532 SkPaint stroke_paint; |
| 533 fill_paint.setStyle(SkPaint::kStroke_Style); |
| 534 fill_paint.setColor(SK_ColorBLACK); |
| 535 canvas->drawRect(bounds, fill_paint); |
| 536 } else { |
| 537 // Draw text input and listbox inset border |
| 538 // Text Input: 2px inset #eee |
| 539 // Listbox: 1px inset #808080 |
| 540 const SkColor kLightColor = text.is_listbox ? |
| 541 SkColorSetRGB(0x80, 0x80, 0x80) : SkColorSetRGB(0xee, 0xee, 0xee); |
| 542 const SkColor kDarkColor = text.is_listbox ? |
| 543 SkColorSetRGB(0x2c, 0x2c, 0x2c) : SkColorSetRGB(0x9a, 0x9a, 0x9a); |
| 544 const int kBorderWidth = text.is_listbox ? 1 : 2; |
| 545 |
| 546 SkPaint dark_paint; |
| 547 dark_paint.setAntiAlias(true); |
| 548 dark_paint.setStyle(SkPaint::kFill_Style); |
| 549 dark_paint.setColor(kDarkColor); |
| 550 |
| 551 SkPaint light_paint; |
| 552 light_paint.setAntiAlias(true); |
| 553 light_paint.setStyle(SkPaint::kFill_Style); |
| 554 light_paint.setColor(kLightColor); |
| 555 |
| 556 int left = rect.x(); |
| 557 int top = rect.y(); |
| 558 int right = rect.right(); |
| 559 int bottom = rect.bottom(); |
| 560 |
| 561 SkPath path; |
| 562 path.incReserve(4); |
| 563 |
| 564 // Top |
| 565 path.moveTo(SkIntToScalar(left), SkIntToScalar(top)); |
| 566 path.lineTo(SkIntToScalar(left + kBorderWidth), |
| 567 SkIntToScalar(top + kBorderWidth)); |
| 568 path.lineTo(SkIntToScalar(right - kBorderWidth), |
| 569 SkIntToScalar(top + kBorderWidth)); |
| 570 path.lineTo(SkIntToScalar(right), SkIntToScalar(top)); |
| 571 canvas->drawPath(path, dark_paint); |
| 572 |
| 573 // Bottom |
| 574 path.reset(); |
| 575 path.moveTo(SkIntToScalar(left + kBorderWidth), |
| 576 SkIntToScalar(bottom - kBorderWidth)); |
| 577 path.lineTo(SkIntToScalar(left), SkIntToScalar(bottom)); |
| 578 path.lineTo(SkIntToScalar(right), SkIntToScalar(bottom)); |
| 579 path.lineTo(SkIntToScalar(right - kBorderWidth), |
| 580 SkIntToScalar(bottom - kBorderWidth)); |
| 581 canvas->drawPath(path, light_paint); |
| 582 |
| 583 // Left |
| 584 path.reset(); |
| 585 path.moveTo(SkIntToScalar(left), SkIntToScalar(top)); |
| 586 path.lineTo(SkIntToScalar(left), SkIntToScalar(bottom)); |
| 587 path.lineTo(SkIntToScalar(left + kBorderWidth), |
| 588 SkIntToScalar(bottom - kBorderWidth)); |
| 589 path.lineTo(SkIntToScalar(left + kBorderWidth), |
| 590 SkIntToScalar(top + kBorderWidth)); |
| 591 canvas->drawPath(path, dark_paint); |
| 592 |
| 593 // Right |
| 594 path.reset(); |
| 595 path.moveTo(SkIntToScalar(right - kBorderWidth), |
| 596 SkIntToScalar(top + kBorderWidth)); |
| 597 path.lineTo(SkIntToScalar(right - kBorderWidth), SkIntToScalar(bottom)); |
| 598 path.lineTo(SkIntToScalar(right), SkIntToScalar(bottom)); |
| 599 path.lineTo(SkIntToScalar(right), SkIntToScalar(top)); |
| 600 canvas->drawPath(path, light_paint); |
| 601 } |
| 602 } |
| 603 |
| 604 void NativeThemeLinux::PaintMenuList(skia::PlatformCanvas* canvas, |
| 605 State state, |
| 606 const gfx::Rect& rect, |
| 607 const MenuListExtraParams& menu_list) { |
| 608 ButtonExtraParams button = { 0 }; |
| 609 button.background_color = menu_list.background_color; |
| 610 PaintButton(canvas, state, rect, button); |
| 611 |
| 612 SkPaint paint; |
| 613 paint.setColor(SK_ColorBLACK); |
| 614 paint.setAntiAlias(true); |
| 615 paint.setStyle(SkPaint::kFill_Style); |
| 616 |
| 617 SkPath path; |
| 618 path.moveTo(menu_list.arrow_x, menu_list.arrow_y - 3); |
| 619 path.rLineTo(6, 0); |
| 620 path.rLineTo(-3, 6); |
| 621 path.close(); |
| 622 canvas->drawPath(path, paint); |
| 623 } |
| 624 |
| 625 void NativeThemeLinux::PaintSliderTrack(skia::PlatformCanvas* canvas, |
| 626 State state, |
| 627 const gfx::Rect& rect, |
| 628 const SliderExtraParams& slider) { |
| 629 const int kMidX = rect.x() + rect.width() / 2; |
| 630 const int kMidY = rect.y() + rect.height() / 2; |
| 631 |
| 632 SkPaint paint; |
| 633 paint.setColor(kSliderTrackBackgroundColor); |
| 634 |
| 635 SkRect skrect; |
| 636 if (slider.vertical) { |
| 637 skrect.set(std::max(rect.x(), kMidX - 2), |
| 638 rect.y(), |
| 639 std::min(rect.right(), kMidX + 2), |
| 640 rect.bottom()); |
| 641 } else { |
| 642 skrect.set(rect.x(), |
| 643 std::max(rect.y(), kMidY - 2), |
| 644 rect.right(), |
| 645 std::min(rect.bottom(), kMidY + 2)); |
| 646 } |
| 647 canvas->drawRect(skrect, paint); |
| 648 } |
| 649 |
| 650 void NativeThemeLinux::PaintSliderThumb(skia::PlatformCanvas* canvas, |
| 651 State state, |
| 652 const gfx::Rect& rect, |
| 653 const SliderExtraParams& slider) { |
| 654 const bool hovered = (state == kHovered) || slider.in_drag; |
| 655 const int kMidX = rect.x() + rect.width() / 2; |
| 656 const int kMidY = rect.y() + rect.height() / 2; |
| 657 |
| 658 SkPaint paint; |
| 659 paint.setColor(hovered ? SK_ColorWHITE : kSliderThumbLightGrey); |
| 660 |
| 661 SkIRect skrect; |
| 662 if (slider.vertical) |
| 663 skrect.set(rect.x(), rect.y(), kMidX + 1, rect.bottom()); |
| 664 else |
| 665 skrect.set(rect.x(), rect.y(), rect.right(), kMidY + 1); |
| 666 |
| 667 canvas->drawIRect(skrect, paint); |
| 668 |
| 669 paint.setColor(hovered ? kSliderThumbLightGrey : kSliderThumbDarkGrey); |
| 670 |
| 671 if (slider.vertical) |
| 672 skrect.set(kMidX + 1, rect.y(), rect.right(), rect.bottom()); |
| 673 else |
| 674 skrect.set(rect.x(), kMidY + 1, rect.right(), rect.bottom()); |
| 675 |
| 676 canvas->drawIRect(skrect, paint); |
| 677 |
| 678 paint.setColor(kSliderThumbBorderDarkGrey); |
| 679 DrawBox(canvas, rect, paint); |
| 680 |
| 681 if (rect.height() > 10 && rect.width() > 10) { |
| 682 DrawHorizLine(canvas, kMidX - 2, kMidX + 2, kMidY, paint); |
| 683 DrawHorizLine(canvas, kMidX - 2, kMidX + 2, kMidY - 3, paint); |
| 684 DrawHorizLine(canvas, kMidX - 2, kMidX + 2, kMidY + 3, paint); |
| 685 } |
| 686 } |
| 687 |
| 688 void NativeThemeLinux::PaintInnerSpinButton(skia::PlatformCanvas* canvas, |
| 689 State state, |
| 690 const gfx::Rect& rect, |
| 691 const InnerSpinButtonExtraParams& spin_button) { |
| 692 if (spin_button.read_only) |
| 693 state = kDisabled; |
| 694 |
| 695 State north_state = state; |
| 696 State south_state = state; |
| 697 if (spin_button.spin_up) |
| 698 south_state = south_state != kDisabled ? kNormal : kDisabled; |
| 699 else |
| 700 north_state = north_state != kDisabled ? kNormal : kDisabled; |
| 701 |
| 702 gfx::Rect half = rect; |
| 703 half.set_height(rect.height() / 2); |
| 704 PaintArrowButton(canvas, half, kScrollbarUpArrow, north_state); |
| 705 |
| 706 half.set_y(rect.y() + rect.height() / 2); |
| 707 PaintArrowButton(canvas, half, kScrollbarDownArrow, south_state); |
| 708 } |
| 709 |
| 710 void NativeThemeLinux::PaintProgressBar(skia::PlatformCanvas* canvas, |
| 711 State state, |
| 712 const gfx::Rect& rect, |
| 713 const ProgressBarExtraParams& progress_bar) { |
| 714 static SkBitmap* bar_image = GfxGetBitmapNamed(IDR_PROGRESS_BAR); |
| 715 static SkBitmap* value_image = GfxGetBitmapNamed(IDR_PROGRESS_VALUE); |
| 716 static SkBitmap* left_border_image = GfxGetBitmapNamed( |
| 717 IDR_PROGRESS_BORDER_LEFT); |
| 718 static SkBitmap* right_border_image = GfxGetBitmapNamed( |
| 719 IDR_PROGRESS_BORDER_RIGHT); |
| 720 |
| 721 double tile_scale = static_cast<double>(rect.height()) / |
| 722 bar_image->height(); |
| 723 |
| 724 int new_tile_width = static_cast<int>(bar_image->width() * tile_scale); |
| 725 double tile_scale_x = static_cast<double>(new_tile_width) / |
| 726 bar_image->width(); |
| 727 |
| 728 DrawTiledImage(canvas, *bar_image, 0, 0, tile_scale_x, tile_scale, |
| 729 rect.x(), rect.y(), rect.width(), rect.height()); |
| 730 |
| 731 if (progress_bar.value_rect_width) { |
| 732 |
| 733 new_tile_width = static_cast<int>(value_image->width() * tile_scale); |
| 734 tile_scale_x = static_cast<double>(new_tile_width) / |
| 735 value_image->width(); |
| 736 |
| 737 DrawTiledImage(canvas, *value_image, 0, 0, tile_scale_x, tile_scale, |
| 738 progress_bar.value_rect_x, |
| 739 progress_bar.value_rect_y, |
| 740 progress_bar.value_rect_width, |
| 741 progress_bar.value_rect_height); |
| 742 } |
| 743 |
| 744 int dest_left_border_width = static_cast<int>(left_border_image->width() * |
| 745 tile_scale); |
| 746 SkRect dest_rect = { |
| 747 SkIntToScalar(rect.x()), |
| 748 SkIntToScalar(rect.y()), |
| 749 SkIntToScalar(rect.x() + dest_left_border_width), |
| 750 SkIntToScalar(rect.bottom()) |
| 751 }; |
| 752 canvas->drawBitmapRect(*left_border_image, NULL, dest_rect); |
| 753 |
| 754 int dest_right_border_width = static_cast<int>(right_border_image->width() * |
| 755 tile_scale); |
| 756 dest_rect.set(SkIntToScalar(rect.right() - dest_right_border_width), |
| 757 SkIntToScalar(rect.y()), |
| 758 SkIntToScalar(rect.right()), |
| 759 SkIntToScalar(rect.bottom())); |
| 760 canvas->drawBitmapRect(*right_border_image, NULL, dest_rect); |
| 761 } |
| 762 |
| 763 bool NativeThemeLinux::IntersectsClipRectInt( |
| 764 skia::PlatformCanvas* canvas, int x, int y, int w, int h) { |
| 765 SkRect clip; |
| 766 return canvas->getClipBounds(&clip) && |
| 767 clip.intersect(SkIntToScalar(x), SkIntToScalar(y), SkIntToScalar(x + w), |
| 768 SkIntToScalar(y + h)); |
| 769 } |
| 770 |
307 void NativeThemeLinux::DrawVertLine(SkCanvas* canvas, | 771 void NativeThemeLinux::DrawVertLine(SkCanvas* canvas, |
308 int x, | 772 int x, |
309 int y1, | 773 int y1, |
310 int y2, | 774 int y2, |
311 const SkPaint& paint) const { | 775 const SkPaint& paint) const { |
312 SkIRect skrect; | 776 SkIRect skrect; |
313 skrect.set(x, y1, x + 1, y2 + 1); | 777 skrect.set(x, y1, x + 1, y2 + 1); |
314 canvas->drawIRect(skrect, paint); | 778 canvas->drawIRect(skrect, paint); |
315 } | 779 } |
316 | 780 |
(...skipping 11 matching lines...) Expand all Loading... |
328 const gfx::Rect& rect, | 792 const gfx::Rect& rect, |
329 const SkPaint& paint) const { | 793 const SkPaint& paint) const { |
330 const int right = rect.x() + rect.width() - 1; | 794 const int right = rect.x() + rect.width() - 1; |
331 const int bottom = rect.y() + rect.height() - 1; | 795 const int bottom = rect.y() + rect.height() - 1; |
332 DrawHorizLine(canvas, rect.x(), right, rect.y(), paint); | 796 DrawHorizLine(canvas, rect.x(), right, rect.y(), paint); |
333 DrawVertLine(canvas, right, rect.y(), bottom, paint); | 797 DrawVertLine(canvas, right, rect.y(), bottom, paint); |
334 DrawHorizLine(canvas, rect.x(), right, bottom, paint); | 798 DrawHorizLine(canvas, rect.x(), right, bottom, paint); |
335 DrawVertLine(canvas, rect.x(), rect.y(), bottom, paint); | 799 DrawVertLine(canvas, rect.x(), rect.y(), bottom, paint); |
336 } | 800 } |
337 | 801 |
| 802 void NativeThemeLinux::DrawBitmapInt( |
| 803 skia::PlatformCanvas* canvas, const SkBitmap& bitmap, |
| 804 int src_x, int src_y, int src_w, int src_h, |
| 805 int dest_x, int dest_y, int dest_w, int dest_h) { |
| 806 DLOG_ASSERT(src_x + src_w < std::numeric_limits<int16_t>::max() && |
| 807 src_y + src_h < std::numeric_limits<int16_t>::max()); |
| 808 if (src_w <= 0 || src_h <= 0 || dest_w <= 0 || dest_h <= 0) { |
| 809 NOTREACHED() << "Attempting to draw bitmap to/from an empty rect!"; |
| 810 return; |
| 811 } |
| 812 |
| 813 if (!IntersectsClipRectInt(canvas, dest_x, dest_y, dest_w, dest_h)) |
| 814 return; |
| 815 |
| 816 SkRect dest_rect = { SkIntToScalar(dest_x), |
| 817 SkIntToScalar(dest_y), |
| 818 SkIntToScalar(dest_x + dest_w), |
| 819 SkIntToScalar(dest_y + dest_h) }; |
| 820 |
| 821 if (src_w == dest_w && src_h == dest_h) { |
| 822 // Workaround for apparent bug in Skia that causes image to occasionally |
| 823 // shift. |
| 824 SkIRect src_rect = { src_x, src_y, src_x + src_w, src_y + src_h }; |
| 825 canvas->drawBitmapRect(bitmap, &src_rect, dest_rect); |
| 826 return; |
| 827 } |
| 828 |
| 829 // Make a bitmap shader that contains the bitmap we want to draw. This is |
| 830 // basically what SkCanvas.drawBitmap does internally, but it gives us |
| 831 // more control over quality and will use the mipmap in the source image if |
| 832 // it has one, whereas drawBitmap won't. |
| 833 SkShader* shader = SkShader::CreateBitmapShader(bitmap, |
| 834 SkShader::kRepeat_TileMode, |
| 835 SkShader::kRepeat_TileMode); |
| 836 SkMatrix shader_scale; |
| 837 shader_scale.setScale(SkFloatToScalar(static_cast<float>(dest_w) / src_w), |
| 838 SkFloatToScalar(static_cast<float>(dest_h) / src_h)); |
| 839 shader_scale.preTranslate(SkIntToScalar(-src_x), SkIntToScalar(-src_y)); |
| 840 shader_scale.postTranslate(SkIntToScalar(dest_x), SkIntToScalar(dest_y)); |
| 841 shader->setLocalMatrix(shader_scale); |
| 842 |
| 843 // The rect will be filled by the bitmap. |
| 844 SkPaint p; |
| 845 p.setFilterBitmap(true); |
| 846 p.setShader(shader); |
| 847 shader->unref(); |
| 848 canvas->drawRect(dest_rect, p); |
| 849 } |
| 850 |
| 851 void NativeThemeLinux::DrawTiledImage(SkCanvas* canvas, |
| 852 const SkBitmap& bitmap, |
| 853 int src_x, int src_y, double tile_scale_x, double tile_scale_y, |
| 854 int dest_x, int dest_y, int w, int h) const { |
| 855 SkShader* shader = SkShader::CreateBitmapShader(bitmap, |
| 856 SkShader::kRepeat_TileMode, |
| 857 SkShader::kRepeat_TileMode); |
| 858 if (tile_scale_x != 1.0 || tile_scale_y != 1.0) { |
| 859 SkMatrix shader_scale; |
| 860 shader_scale.setScale(SkDoubleToScalar(tile_scale_x), |
| 861 SkDoubleToScalar(tile_scale_y)); |
| 862 shader->setLocalMatrix(shader_scale); |
| 863 } |
| 864 |
| 865 SkPaint paint; |
| 866 paint.setShader(shader); |
| 867 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); |
| 868 |
| 869 // CreateBitmapShader returns a Shader with a reference count of one, we |
| 870 // need to unref after paint takes ownership of the shader. |
| 871 shader->unref(); |
| 872 canvas->save(); |
| 873 canvas->translate(SkIntToScalar(dest_x - src_x), |
| 874 SkIntToScalar(dest_y - src_y)); |
| 875 canvas->clipRect(SkRect::MakeXYWH(src_x, src_y, w, h)); |
| 876 canvas->drawPaint(paint); |
| 877 canvas->restore(); |
| 878 } |
| 879 |
338 SkScalar NativeThemeLinux::Clamp(SkScalar value, | 880 SkScalar NativeThemeLinux::Clamp(SkScalar value, |
339 SkScalar min, | 881 SkScalar min, |
340 SkScalar max) const { | 882 SkScalar max) const { |
341 return std::min(std::max(value, min), max); | 883 return std::min(std::max(value, min), max); |
342 } | 884 } |
343 | 885 |
344 SkColor NativeThemeLinux::SaturateAndBrighten(SkScalar* hsv, | 886 SkColor NativeThemeLinux::SaturateAndBrighten(SkScalar* hsv, |
345 SkScalar saturate_amount, | 887 SkScalar saturate_amount, |
346 SkScalar brighten_amount) const { | 888 SkScalar brighten_amount) const { |
347 SkScalar color[3]; | 889 SkScalar color[3]; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 | 932 |
391 void NativeThemeLinux::SetScrollbarColors(unsigned inactive_color, | 933 void NativeThemeLinux::SetScrollbarColors(unsigned inactive_color, |
392 unsigned active_color, | 934 unsigned active_color, |
393 unsigned track_color) const { | 935 unsigned track_color) const { |
394 thumb_inactive_color_ = inactive_color; | 936 thumb_inactive_color_ = inactive_color; |
395 thumb_active_color_ = active_color; | 937 thumb_active_color_ = active_color; |
396 track_color_ = track_color; | 938 track_color_ = track_color; |
397 } | 939 } |
398 | 940 |
399 } // namespace gfx | 941 } // namespace gfx |
OLD | NEW |