OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "gfx/native_theme_linux.h" | |
6 | |
7 #include <limits> | |
8 | |
9 #include "base/logging.h" | |
10 #include "gfx/codec/png_codec.h" | |
11 #include "gfx/color_utils.h" | |
12 #include "gfx/gfx_module.h" | |
13 #include "gfx/size.h" | |
14 #include "gfx/rect.h" | |
15 #include "grit/gfx_resources.h" | |
16 #include "third_party/skia/include/effects/SkGradientShader.h" | |
17 | |
18 namespace gfx { | |
19 | |
20 unsigned int NativeThemeLinux::button_length_ = 14; | |
21 unsigned int NativeThemeLinux::scrollbar_width_ = 15; | |
22 unsigned int NativeThemeLinux::thumb_inactive_color_ = 0xeaeaea; | |
23 unsigned int NativeThemeLinux::thumb_active_color_ = 0xf4f4f4; | |
24 unsigned int NativeThemeLinux::track_color_ = 0xd3d3d3; | |
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 | |
41 #if !defined(OS_CHROMEOS) | |
42 // Chromeos has a different look. | |
43 // static | |
44 NativeThemeLinux* NativeThemeLinux::instance() { | |
45 // The global NativeThemeLinux instance. | |
46 static NativeThemeLinux s_native_theme; | |
47 return &s_native_theme; | |
48 } | |
49 #endif | |
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 | |
82 NativeThemeLinux::NativeThemeLinux() { | |
83 } | |
84 | |
85 NativeThemeLinux::~NativeThemeLinux() { | |
86 } | |
87 | |
88 gfx::Size NativeThemeLinux::GetPartSize(Part part) const { | |
89 switch (part) { | |
90 case kScrollbarDownArrow: | |
91 case kScrollbarUpArrow: | |
92 return gfx::Size(scrollbar_width_, button_length_); | |
93 case kScrollbarLeftArrow: | |
94 case kScrollbarRightArrow: | |
95 return gfx::Size(button_length_, scrollbar_width_); | |
96 case kScrollbarHorizontalThumb: | |
97 // This matches Firefox on Linux. | |
98 return gfx::Size(2 * scrollbar_width_, scrollbar_width_); | |
99 case kScrollbarVerticalThumb: | |
100 // This matches Firefox on Linux. | |
101 return gfx::Size(scrollbar_width_, 2 * scrollbar_width_); | |
102 break; | |
103 case kScrollbarHorizontalTrack: | |
104 return gfx::Size(0, scrollbar_width_); | |
105 case kScrollbarVerticalTrack: | |
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. | |
121 } | |
122 return gfx::Size(); | |
123 } | |
124 | |
125 void NativeThemeLinux::PaintArrowButton( | |
126 skia::PlatformCanvas* canvas, | |
127 const gfx::Rect& rect, Part direction, State state) { | |
128 int widthMiddle, lengthMiddle; | |
129 SkPaint paint; | |
130 if (direction == kScrollbarUpArrow || direction == kScrollbarDownArrow) { | |
131 widthMiddle = rect.width() / 2 + 1; | |
132 lengthMiddle = rect.height() / 2 + 1; | |
133 } else { | |
134 lengthMiddle = rect.width() / 2 + 1; | |
135 widthMiddle = rect.height() / 2 + 1; | |
136 } | |
137 | |
138 // Calculate button color. | |
139 SkScalar trackHSV[3]; | |
140 SkColorToHSV(track_color_, trackHSV); | |
141 SkColor buttonColor = SaturateAndBrighten(trackHSV, 0, 0.2); | |
142 SkColor backgroundColor = buttonColor; | |
143 if (state == kPressed) { | |
144 SkScalar buttonHSV[3]; | |
145 SkColorToHSV(buttonColor, buttonHSV); | |
146 buttonColor = SaturateAndBrighten(buttonHSV, 0, -0.1); | |
147 } else if (state == kHovered) { | |
148 SkScalar buttonHSV[3]; | |
149 SkColorToHSV(buttonColor, buttonHSV); | |
150 buttonColor = SaturateAndBrighten(buttonHSV, 0, 0.05); | |
151 } | |
152 | |
153 SkIRect skrect; | |
154 skrect.set(rect.x(), rect.y(), rect.x() + rect.width(), rect.y() | |
155 + rect.height()); | |
156 // Paint the background (the area visible behind the rounded corners). | |
157 paint.setColor(backgroundColor); | |
158 canvas->drawIRect(skrect, paint); | |
159 | |
160 // Paint the button's outline and fill the middle | |
161 SkPath outline; | |
162 switch (direction) { | |
163 case kScrollbarUpArrow: | |
164 outline.moveTo(rect.x() + 0.5, rect.y() + rect.height() + 0.5); | |
165 outline.rLineTo(0, -(rect.height() - 2)); | |
166 outline.rLineTo(2, -2); | |
167 outline.rLineTo(rect.width() - 5, 0); | |
168 outline.rLineTo(2, 2); | |
169 outline.rLineTo(0, rect.height() - 2); | |
170 break; | |
171 case kScrollbarDownArrow: | |
172 outline.moveTo(rect.x() + 0.5, rect.y() - 0.5); | |
173 outline.rLineTo(0, rect.height() - 2); | |
174 outline.rLineTo(2, 2); | |
175 outline.rLineTo(rect.width() - 5, 0); | |
176 outline.rLineTo(2, -2); | |
177 outline.rLineTo(0, -(rect.height() - 2)); | |
178 break; | |
179 case kScrollbarRightArrow: | |
180 outline.moveTo(rect.x() - 0.5, rect.y() + 0.5); | |
181 outline.rLineTo(rect.width() - 2, 0); | |
182 outline.rLineTo(2, 2); | |
183 outline.rLineTo(0, rect.height() - 5); | |
184 outline.rLineTo(-2, 2); | |
185 outline.rLineTo(-(rect.width() - 2), 0); | |
186 break; | |
187 case kScrollbarLeftArrow: | |
188 outline.moveTo(rect.x() + rect.width() + 0.5, rect.y() + 0.5); | |
189 outline.rLineTo(-(rect.width() - 2), 0); | |
190 outline.rLineTo(-2, 2); | |
191 outline.rLineTo(0, rect.height() - 5); | |
192 outline.rLineTo(2, 2); | |
193 outline.rLineTo(rect.width() - 2, 0); | |
194 break; | |
195 default: | |
196 break; | |
197 } | |
198 outline.close(); | |
199 | |
200 paint.setStyle(SkPaint::kFill_Style); | |
201 paint.setColor(buttonColor); | |
202 canvas->drawPath(outline, paint); | |
203 | |
204 paint.setAntiAlias(true); | |
205 paint.setStyle(SkPaint::kStroke_Style); | |
206 SkScalar thumbHSV[3]; | |
207 SkColorToHSV(thumb_inactive_color_, thumbHSV); | |
208 paint.setColor(OutlineColor(trackHSV, thumbHSV)); | |
209 canvas->drawPath(outline, paint); | |
210 | |
211 // If the button is disabled or read-only, the arrow is drawn with the | |
212 // outline color. | |
213 if (state != kDisabled) | |
214 paint.setColor(SK_ColorBLACK); | |
215 | |
216 paint.setAntiAlias(false); | |
217 paint.setStyle(SkPaint::kFill_Style); | |
218 | |
219 SkPath path; | |
220 // The constants in this block of code are hand-tailored to produce good | |
221 // looking arrows without anti-aliasing. | |
222 switch (direction) { | |
223 case kScrollbarUpArrow: | |
224 path.moveTo(rect.x() + widthMiddle - 4, rect.y() + lengthMiddle + 2); | |
225 path.rLineTo(7, 0); | |
226 path.rLineTo(-4, -4); | |
227 break; | |
228 case kScrollbarDownArrow: | |
229 path.moveTo(rect.x() + widthMiddle - 4, rect.y() + lengthMiddle - 3); | |
230 path.rLineTo(7, 0); | |
231 path.rLineTo(-4, 4); | |
232 break; | |
233 case kScrollbarRightArrow: | |
234 path.moveTo(rect.x() + lengthMiddle - 3, rect.y() + widthMiddle - 4); | |
235 path.rLineTo(0, 7); | |
236 path.rLineTo(4, -4); | |
237 break; | |
238 case kScrollbarLeftArrow: | |
239 path.moveTo(rect.x() + lengthMiddle + 1, rect.y() + widthMiddle - 5); | |
240 path.rLineTo(0, 9); | |
241 path.rLineTo(-4, -4); | |
242 break; | |
243 default: | |
244 break; | |
245 } | |
246 path.close(); | |
247 | |
248 canvas->drawPath(path, paint); | |
249 } | |
250 | |
251 void NativeThemeLinux::Paint(skia::PlatformCanvas* canvas, | |
252 Part part, | |
253 State state, | |
254 const gfx::Rect& rect, | |
255 const ExtraParams& extra) { | |
256 switch (part) { | |
257 case kScrollbarDownArrow: | |
258 case kScrollbarUpArrow: | |
259 case kScrollbarLeftArrow: | |
260 case kScrollbarRightArrow: | |
261 PaintArrowButton(canvas, rect, part, state); | |
262 break; | |
263 case kScrollbarHorizontalThumb: | |
264 case kScrollbarVerticalThumb: | |
265 PaintScrollbarThumb(canvas, part, state, rect); | |
266 break; | |
267 case kScrollbarHorizontalTrack: | |
268 case kScrollbarVerticalTrack: | |
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); | |
297 break; | |
298 } | |
299 } | |
300 | |
301 void NativeThemeLinux::PaintScrollbarTrack(skia::PlatformCanvas* canvas, | |
302 Part part, | |
303 State state, | |
304 const ScrollbarTrackExtraParams& extra_params, | |
305 const gfx::Rect& rect) { | |
306 SkPaint paint; | |
307 SkIRect skrect; | |
308 | |
309 skrect.set(rect.x(), rect.y(), rect.right(), rect.bottom()); | |
310 SkScalar track_hsv[3]; | |
311 SkColorToHSV(track_color_, track_hsv); | |
312 paint.setColor(SaturateAndBrighten(track_hsv, 0, 0)); | |
313 canvas->drawIRect(skrect, paint); | |
314 | |
315 SkScalar thumb_hsv[3]; | |
316 SkColorToHSV(thumb_inactive_color_, thumb_hsv); | |
317 | |
318 paint.setColor(OutlineColor(track_hsv, thumb_hsv)); | |
319 DrawBox(canvas, rect, paint); | |
320 } | |
321 | |
322 void NativeThemeLinux::PaintScrollbarThumb(skia::PlatformCanvas* canvas, | |
323 Part part, | |
324 State state, | |
325 const gfx::Rect& rect) { | |
326 const bool hovered = state == kHovered; | |
327 const int midx = rect.x() + rect.width() / 2; | |
328 const int midy = rect.y() + rect.height() / 2; | |
329 const bool vertical = part == kScrollbarVerticalThumb; | |
330 | |
331 SkScalar thumb[3]; | |
332 SkColorToHSV(hovered ? thumb_active_color_ : thumb_inactive_color_, thumb); | |
333 | |
334 SkPaint paint; | |
335 paint.setColor(SaturateAndBrighten(thumb, 0, 0.02)); | |
336 | |
337 SkIRect skrect; | |
338 if (vertical) | |
339 skrect.set(rect.x(), rect.y(), midx + 1, rect.y() + rect.height()); | |
340 else | |
341 skrect.set(rect.x(), rect.y(), rect.x() + rect.width(), midy + 1); | |
342 | |
343 canvas->drawIRect(skrect, paint); | |
344 | |
345 paint.setColor(SaturateAndBrighten(thumb, 0, -0.02)); | |
346 | |
347 if (vertical) { | |
348 skrect.set( | |
349 midx + 1, rect.y(), rect.x() + rect.width(), rect.y() + rect.height()); | |
350 } else { | |
351 skrect.set( | |
352 rect.x(), midy + 1, rect.x() + rect.width(), rect.y() + rect.height()); | |
353 } | |
354 | |
355 canvas->drawIRect(skrect, paint); | |
356 | |
357 SkScalar track[3]; | |
358 SkColorToHSV(track_color_, track); | |
359 paint.setColor(OutlineColor(track, thumb)); | |
360 DrawBox(canvas, rect, paint); | |
361 | |
362 if (rect.height() > 10 && rect.width() > 10) { | |
363 const int grippy_half_width = 2; | |
364 const int inter_grippy_offset = 3; | |
365 if (vertical) { | |
366 DrawHorizLine(canvas, | |
367 midx - grippy_half_width, | |
368 midx + grippy_half_width, | |
369 midy - inter_grippy_offset, | |
370 paint); | |
371 DrawHorizLine(canvas, | |
372 midx - grippy_half_width, | |
373 midx + grippy_half_width, | |
374 midy, | |
375 paint); | |
376 DrawHorizLine(canvas, | |
377 midx - grippy_half_width, | |
378 midx + grippy_half_width, | |
379 midy + inter_grippy_offset, | |
380 paint); | |
381 } else { | |
382 DrawVertLine(canvas, | |
383 midx - inter_grippy_offset, | |
384 midy - grippy_half_width, | |
385 midy + grippy_half_width, | |
386 paint); | |
387 DrawVertLine(canvas, | |
388 midx, | |
389 midy - grippy_half_width, | |
390 midy + grippy_half_width, | |
391 paint); | |
392 DrawVertLine(canvas, | |
393 midx + inter_grippy_offset, | |
394 midy - grippy_half_width, | |
395 midy + grippy_half_width, | |
396 paint); | |
397 } | |
398 } | |
399 } | |
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 | |
771 void NativeThemeLinux::DrawVertLine(SkCanvas* canvas, | |
772 int x, | |
773 int y1, | |
774 int y2, | |
775 const SkPaint& paint) const { | |
776 SkIRect skrect; | |
777 skrect.set(x, y1, x + 1, y2 + 1); | |
778 canvas->drawIRect(skrect, paint); | |
779 } | |
780 | |
781 void NativeThemeLinux::DrawHorizLine(SkCanvas* canvas, | |
782 int x1, | |
783 int x2, | |
784 int y, | |
785 const SkPaint& paint) const { | |
786 SkIRect skrect; | |
787 skrect.set(x1, y, x2 + 1, y + 1); | |
788 canvas->drawIRect(skrect, paint); | |
789 } | |
790 | |
791 void NativeThemeLinux::DrawBox(SkCanvas* canvas, | |
792 const gfx::Rect& rect, | |
793 const SkPaint& paint) const { | |
794 const int right = rect.x() + rect.width() - 1; | |
795 const int bottom = rect.y() + rect.height() - 1; | |
796 DrawHorizLine(canvas, rect.x(), right, rect.y(), paint); | |
797 DrawVertLine(canvas, right, rect.y(), bottom, paint); | |
798 DrawHorizLine(canvas, rect.x(), right, bottom, paint); | |
799 DrawVertLine(canvas, rect.x(), rect.y(), bottom, paint); | |
800 } | |
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 | |
880 SkScalar NativeThemeLinux::Clamp(SkScalar value, | |
881 SkScalar min, | |
882 SkScalar max) const { | |
883 return std::min(std::max(value, min), max); | |
884 } | |
885 | |
886 SkColor NativeThemeLinux::SaturateAndBrighten(SkScalar* hsv, | |
887 SkScalar saturate_amount, | |
888 SkScalar brighten_amount) const { | |
889 SkScalar color[3]; | |
890 color[0] = hsv[0]; | |
891 color[1] = Clamp(hsv[1] + saturate_amount, 0.0, 1.0); | |
892 color[2] = Clamp(hsv[2] + brighten_amount, 0.0, 1.0); | |
893 return SkHSVToColor(color); | |
894 } | |
895 | |
896 SkColor NativeThemeLinux::OutlineColor(SkScalar* hsv1, SkScalar* hsv2) const { | |
897 // GTK Theme engines have way too much control over the layout of | |
898 // the scrollbar. We might be able to more closely approximate its | |
899 // look-and-feel, if we sent whole images instead of just colors | |
900 // from the browser to the renderer. But even then, some themes | |
901 // would just break. | |
902 // | |
903 // So, instead, we don't even try to 100% replicate the look of | |
904 // the native scrollbar. We render our own version, but we make | |
905 // sure to pick colors that blend in nicely with the system GTK | |
906 // theme. In most cases, we can just sample a couple of pixels | |
907 // from the system scrollbar and use those colors to draw our | |
908 // scrollbar. | |
909 // | |
910 // This works fine for the track color and the overall thumb | |
911 // color. But it fails spectacularly for the outline color used | |
912 // around the thumb piece. Not all themes have a clearly defined | |
913 // outline. For some of them it is partially transparent, and for | |
914 // others the thickness is very unpredictable. | |
915 // | |
916 // So, instead of trying to approximate the system theme, we | |
917 // instead try to compute a reasonable looking choice based on the | |
918 // known color of the track and the thumb piece. This is difficult | |
919 // when trying to deal both with high- and low-contrast themes, | |
920 // and both with positive and inverted themes. | |
921 // | |
922 // The following code has been tested to look OK with all of the | |
923 // default GTK themes. | |
924 SkScalar min_diff = Clamp((hsv1[1] + hsv2[1]) * 1.2, 0.28, 0.5); | |
925 SkScalar diff = Clamp(fabs(hsv1[2] - hsv2[2]) / 2, min_diff, 0.5); | |
926 | |
927 if (hsv1[2] + hsv2[2] > 1.0) | |
928 diff = -diff; | |
929 | |
930 return SaturateAndBrighten(hsv2, -0.2, diff); | |
931 } | |
932 | |
933 void NativeThemeLinux::SetScrollbarColors(unsigned inactive_color, | |
934 unsigned active_color, | |
935 unsigned track_color) const { | |
936 thumb_inactive_color_ = inactive_color; | |
937 thumb_active_color_ = active_color; | |
938 track_color_ = track_color; | |
939 } | |
940 | |
941 } // namespace gfx | |
OLD | NEW |