OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 // This file implements a simple generic version of the WebThemeEngine, |
| 6 // which is used to draw all the native controls on a web page. We use this |
| 7 // file when running in layout test mode in order to remove any |
| 8 // platform-specific rendering differences due to themes, colors, etc. |
| 9 // |
| 10 |
| 11 #include "content/shell/renderer/test_runner/WebTestThemeControlWin.h" |
| 12 |
| 13 #include "content/shell/renderer/test_runner/TestCommon.h" |
| 14 #include "skia/ext/skia_utils_win.h" |
| 15 #include "third_party/skia/include/core/SkCanvas.h" |
| 16 #include "third_party/skia/include/core/SkPaint.h" |
| 17 #include "third_party/skia/include/core/SkPath.h" |
| 18 |
| 19 #include <algorithm> |
| 20 |
| 21 using namespace blink; |
| 22 using namespace std; |
| 23 |
| 24 namespace WebTestRunner { |
| 25 |
| 26 namespace { |
| 27 |
| 28 const SkColor edgeColor = SK_ColorBLACK; |
| 29 const SkColor readOnlyColor = SkColorSetRGB(0xe9, 0xc2, 0xa6); |
| 30 const SkColor fgColor = SK_ColorBLACK; |
| 31 |
| 32 // These are indexed by WebTestThemeControlWin::State, *not* WebThemeEngine::Sta
te. |
| 33 const SkColor bgColors[] = { |
| 34 SK_ColorBLACK, // Unknown (not used) |
| 35 SkColorSetRGB(0xc9, 0xc9, 0xc9), // Disabled |
| 36 SkColorSetRGB(0xf3, 0xe0, 0xd0), // Readonly |
| 37 SkColorSetRGB(0x89, 0xc4, 0xff), // Normal |
| 38 SkColorSetRGB(0x43, 0xf9, 0xff), // Hot |
| 39 SkColorSetRGB(0x20, 0xf6, 0xcc), // Hover |
| 40 SkColorSetRGB(0x00, 0xf3, 0xac), // Focused |
| 41 SkColorSetRGB(0xa9, 0xff, 0x12), // Pressed |
| 42 SkColorSetRGB(0xcc, 0xcc, 0xcc) // Indeterminate (not used) |
| 43 }; |
| 44 |
| 45 SkIRect validate(const SkIRect& rect, WebTestThemeControlWin::Type ctype) |
| 46 { |
| 47 switch (ctype) { |
| 48 case WebTestThemeControlWin::UncheckedBoxType: |
| 49 case WebTestThemeControlWin::CheckedBoxType: |
| 50 case WebTestThemeControlWin::UncheckedRadioType: |
| 51 case WebTestThemeControlWin::CheckedRadioType: { |
| 52 SkIRect retval = rect; |
| 53 |
| 54 // The maximum width and height is 13. |
| 55 // Center the square in the passed rectangle. |
| 56 const int maxControlSize = 13; |
| 57 int controlSize = std::min(rect.width(), rect.height()); |
| 58 controlSize = std::min(controlSize, maxControlSize); |
| 59 |
| 60 retval.fLeft = rect.fLeft + (rect.width() / 2) - (controlSize / 2); |
| 61 retval.fRight = retval.fLeft + controlSize - 1; |
| 62 retval.fTop = rect.fTop + (rect.height() / 2) - (controlSize / 2); |
| 63 retval.fBottom = retval.fTop + controlSize - 1; |
| 64 |
| 65 return retval; |
| 66 } |
| 67 |
| 68 default: |
| 69 return rect; |
| 70 } |
| 71 } |
| 72 |
| 73 } |
| 74 |
| 75 WebTestThemeControlWin::WebTestThemeControlWin(SkCanvas* canvas, const SkIRect&
irect, Type ctype, State cstate) |
| 76 : m_canvas(canvas) |
| 77 , m_irect(validate(irect, ctype)) |
| 78 , m_type(ctype) |
| 79 , m_state(cstate) |
| 80 , m_left(m_irect.fLeft) |
| 81 , m_right(m_irect.fRight) |
| 82 , m_top(m_irect.fTop) |
| 83 , m_bottom(m_irect.fBottom) |
| 84 , m_height(m_irect.height()) |
| 85 , m_width(m_irect.width()) |
| 86 , m_edgeColor(edgeColor) |
| 87 , m_bgColor(bgColors[cstate]) |
| 88 , m_fgColor(fgColor) |
| 89 { |
| 90 } |
| 91 |
| 92 WebTestThemeControlWin::~WebTestThemeControlWin() |
| 93 { |
| 94 } |
| 95 |
| 96 void WebTestThemeControlWin::box(const SkIRect& rect, SkColor fillColor) |
| 97 { |
| 98 SkPaint paint; |
| 99 |
| 100 paint.setStyle(SkPaint::kFill_Style); |
| 101 paint.setColor(fillColor); |
| 102 m_canvas->drawIRect(rect, paint); |
| 103 |
| 104 paint.setColor(m_edgeColor); |
| 105 paint.setStyle(SkPaint::kStroke_Style); |
| 106 m_canvas->drawIRect(rect, paint); |
| 107 } |
| 108 |
| 109 void WebTestThemeControlWin::line(int x0, int y0, int x1, int y1, SkColor color) |
| 110 { |
| 111 SkPaint paint; |
| 112 paint.setColor(color); |
| 113 m_canvas->drawLine(SkIntToScalar(x0), SkIntToScalar(y0), SkIntToScalar(x1),
SkIntToScalar(y1), paint); |
| 114 } |
| 115 |
| 116 void WebTestThemeControlWin::triangle(int x0, int y0, int x1, int y1, int x2, in
t y2, SkColor color) |
| 117 { |
| 118 SkPath path; |
| 119 SkPaint paint; |
| 120 |
| 121 paint.setColor(color); |
| 122 paint.setStyle(SkPaint::kFill_Style); |
| 123 path.incReserve(4); |
| 124 path.moveTo(SkIntToScalar(x0), SkIntToScalar(y0)); |
| 125 path.lineTo(SkIntToScalar(x1), SkIntToScalar(y1)); |
| 126 path.lineTo(SkIntToScalar(x2), SkIntToScalar(y2)); |
| 127 path.close(); |
| 128 m_canvas->drawPath(path, paint); |
| 129 |
| 130 paint.setColor(m_edgeColor); |
| 131 paint.setStyle(SkPaint::kStroke_Style); |
| 132 m_canvas->drawPath(path, paint); |
| 133 } |
| 134 |
| 135 void WebTestThemeControlWin::roundRect(SkColor color) |
| 136 { |
| 137 SkRect rect; |
| 138 SkScalar radius = SkIntToScalar(5); |
| 139 SkPaint paint; |
| 140 |
| 141 rect.set(m_irect); |
| 142 paint.setColor(color); |
| 143 paint.setStyle(SkPaint::kFill_Style); |
| 144 m_canvas->drawRoundRect(rect, radius, radius, paint); |
| 145 |
| 146 paint.setColor(m_edgeColor); |
| 147 paint.setStyle(SkPaint::kStroke_Style); |
| 148 m_canvas->drawRoundRect(rect, radius, radius, paint); |
| 149 } |
| 150 |
| 151 void WebTestThemeControlWin::oval(SkColor color) |
| 152 { |
| 153 SkRect rect; |
| 154 SkPaint paint; |
| 155 |
| 156 rect.set(m_irect); |
| 157 paint.setColor(color); |
| 158 paint.setStyle(SkPaint::kFill_Style); |
| 159 m_canvas->drawOval(rect, paint); |
| 160 |
| 161 paint.setColor(m_edgeColor); |
| 162 paint.setStyle(SkPaint::kStroke_Style); |
| 163 m_canvas->drawOval(rect, paint); |
| 164 } |
| 165 |
| 166 void WebTestThemeControlWin::circle(SkScalar radius, SkColor color) |
| 167 { |
| 168 SkScalar cy = SkIntToScalar(m_top + m_height / 2); |
| 169 SkScalar cx = SkIntToScalar(m_left + m_width / 2); |
| 170 SkPaint paint; |
| 171 |
| 172 paint.setColor(color); |
| 173 paint.setStyle(SkPaint::kFill_Style); |
| 174 m_canvas->drawCircle(cx, cy, radius, paint); |
| 175 |
| 176 paint.setColor(m_edgeColor); |
| 177 paint.setStyle(SkPaint::kStroke_Style); |
| 178 m_canvas->drawCircle(cx, cy, radius, paint); |
| 179 } |
| 180 |
| 181 void WebTestThemeControlWin::nestedBoxes(int indentLeft, int indentTop, int inde
ntRight, int indentBottom, SkColor outerColor, SkColor innerColor) |
| 182 { |
| 183 SkIRect lirect; |
| 184 box(m_irect, outerColor); |
| 185 lirect.set(m_irect.fLeft + indentLeft, m_irect.fTop + indentTop, m_irect.fRi
ght - indentRight, m_irect.fBottom - indentBottom); |
| 186 box(lirect, innerColor); |
| 187 } |
| 188 |
| 189 void WebTestThemeControlWin::markState() |
| 190 { |
| 191 // The horizontal lines in a read only control are spaced by this amount. |
| 192 const int readOnlyLineOffset = 5; |
| 193 |
| 194 // The length of a triangle side for the corner marks. |
| 195 const int triangleSize = 5; |
| 196 |
| 197 switch (m_state) { |
| 198 case UnknownState: |
| 199 case DisabledState: |
| 200 case NormalState: |
| 201 case IndeterminateState: |
| 202 // Don't visually mark these states (color is enough). |
| 203 break; |
| 204 case ReadOnlyState: |
| 205 // Drawing lines across the control. |
| 206 for (int i = m_top + readOnlyLineOffset; i < m_bottom; i += readOnlyLine
Offset) |
| 207 line(m_left + 1, i, m_right - 1, i, readOnlyColor); |
| 208 break; |
| 209 |
| 210 case HotState: |
| 211 // Draw a triangle in the upper left corner of the control. |
| 212 triangle(m_left, m_top, m_left + triangleSize, m_top, m_left, m_top + tr
iangleSize, m_edgeColor); |
| 213 break; |
| 214 |
| 215 case HoverState: |
| 216 // Draw a triangle in the upper right corner of the control. |
| 217 triangle(m_right, m_top, m_right, m_top + triangleSize, m_right - triang
leSize, m_top, m_edgeColor); |
| 218 break; |
| 219 |
| 220 case FocusedState: |
| 221 // Draw a triangle in the bottom right corner of the control. |
| 222 triangle(m_right, m_bottom, m_right - triangleSize, m_bottom, m_right, m
_bottom - triangleSize, m_edgeColor); |
| 223 break; |
| 224 |
| 225 case PressedState: |
| 226 // Draw a triangle in the bottom left corner of the control. |
| 227 triangle(m_left, m_bottom, m_left, m_bottom - triangleSize, m_left + tri
angleSize, m_bottom, m_edgeColor); |
| 228 break; |
| 229 |
| 230 default: |
| 231 BLINK_ASSERT_NOT_REACHED(); |
| 232 break; |
| 233 } |
| 234 } |
| 235 |
| 236 void WebTestThemeControlWin::draw() |
| 237 { |
| 238 int halfWidth = m_width / 2; |
| 239 int halfHeight = m_height / 2; |
| 240 int quarterWidth = m_width / 4; |
| 241 int quarterHeight = m_height / 4; |
| 242 |
| 243 // Indent amounts for the check in a checkbox or radio button. |
| 244 const int checkIndent = 3; |
| 245 |
| 246 // Indent amounts for short and long sides of the scrollbar notches. |
| 247 const int notchLongOffset = 1; |
| 248 const int notchShortOffset = 4; |
| 249 const int noOffset = 0; |
| 250 |
| 251 // Indent amounts for the short and long sides of a scroll thumb box. |
| 252 const int thumbLongIndent = 0; |
| 253 const int thumbShortIndent = 2; |
| 254 |
| 255 // Indents for the crosshatch on a scroll grip. |
| 256 const int gripLongIndent = 3; |
| 257 const int gripShortIndent = 5; |
| 258 |
| 259 // Indents for the the slider track. |
| 260 const int sliderIndent = 2; |
| 261 |
| 262 switch (m_type) { |
| 263 case UnknownType: |
| 264 BLINK_ASSERT_NOT_REACHED(); |
| 265 break; |
| 266 |
| 267 case TextFieldType: |
| 268 // We render this by hand outside of this function. |
| 269 BLINK_ASSERT_NOT_REACHED(); |
| 270 break; |
| 271 |
| 272 case PushButtonType: |
| 273 // push buttons render as a rounded rectangle |
| 274 roundRect(m_bgColor); |
| 275 break; |
| 276 |
| 277 case UncheckedBoxType: |
| 278 // Unchecked boxes are simply plain boxes. |
| 279 box(m_irect, m_bgColor); |
| 280 break; |
| 281 |
| 282 case CheckedBoxType: |
| 283 nestedBoxes(checkIndent, checkIndent, checkIndent, checkIndent, m_bgColo
r, m_fgColor); |
| 284 break; |
| 285 |
| 286 case IndeterminateCheckboxType: |
| 287 // Indeterminate checkbox is a box containing '-'. |
| 288 nestedBoxes(checkIndent, halfHeight, checkIndent, halfHeight, m_bgColor,
m_fgColor); |
| 289 break; |
| 290 |
| 291 case UncheckedRadioType: |
| 292 circle(SkIntToScalar(halfHeight), m_bgColor); |
| 293 break; |
| 294 |
| 295 case CheckedRadioType: |
| 296 circle(SkIntToScalar(halfHeight), m_bgColor); |
| 297 circle(SkIntToScalar(halfHeight - checkIndent), m_fgColor); |
| 298 break; |
| 299 |
| 300 case HorizontalScrollTrackBackType: { |
| 301 // Draw a box with a notch at the left. |
| 302 int longOffset = halfHeight - notchLongOffset; |
| 303 int shortOffset = m_width - notchShortOffset; |
| 304 nestedBoxes(noOffset, longOffset, shortOffset, longOffset, m_bgColor, m_
edgeColor); |
| 305 break; |
| 306 } |
| 307 |
| 308 case HorizontalScrollTrackForwardType: { |
| 309 // Draw a box with a notch at the right. |
| 310 int longOffset = halfHeight - notchLongOffset; |
| 311 int shortOffset = m_width - notchShortOffset; |
| 312 nestedBoxes(shortOffset, longOffset, noOffset, longOffset, m_bgColor, m_
fgColor); |
| 313 break; |
| 314 } |
| 315 |
| 316 case VerticalScrollTrackBackType: { |
| 317 // Draw a box with a notch at the top. |
| 318 int longOffset = halfWidth - notchLongOffset; |
| 319 int shortOffset = m_height - notchShortOffset; |
| 320 nestedBoxes(longOffset, noOffset, longOffset, shortOffset, m_bgColor, m_
fgColor); |
| 321 break; |
| 322 } |
| 323 |
| 324 case VerticalScrollTrackForwardType: { |
| 325 // Draw a box with a notch at the bottom. |
| 326 int longOffset = halfWidth - notchLongOffset; |
| 327 int shortOffset = m_height - notchShortOffset; |
| 328 nestedBoxes(longOffset, shortOffset, longOffset, noOffset, m_bgColor, m_
fgColor); |
| 329 break; |
| 330 } |
| 331 |
| 332 case HorizontalScrollThumbType: |
| 333 // Draw a narrower box on top of the outside box. |
| 334 nestedBoxes(thumbLongIndent, thumbShortIndent, thumbLongIndent, thumbSho
rtIndent, m_bgColor, m_bgColor); |
| 335 break; |
| 336 |
| 337 case VerticalScrollThumbType: |
| 338 // Draw a shorter box on top of the outside box. |
| 339 nestedBoxes(thumbShortIndent, thumbLongIndent, thumbShortIndent, thumbLo
ngIndent, m_bgColor, m_bgColor); |
| 340 break; |
| 341 |
| 342 case HorizontalSliderThumbType: |
| 343 case VerticalSliderThumbType: |
| 344 // Slider thumbs are ovals. |
| 345 oval(m_bgColor); |
| 346 break; |
| 347 |
| 348 case HorizontalScrollGripType: { |
| 349 // Draw a horizontal crosshatch for the grip. |
| 350 int longOffset = halfWidth - gripLongIndent; |
| 351 line(m_left + gripLongIndent, m_top + halfHeight, m_right - gripLongInde
nt, m_top + halfHeight, m_fgColor); |
| 352 line(m_left + longOffset, m_top + gripShortIndent, m_left + longOffset,
m_bottom - gripShortIndent, m_fgColor); |
| 353 line(m_right - longOffset, m_top + gripShortIndent, m_right - longOffset
, m_bottom - gripShortIndent, m_fgColor); |
| 354 break; |
| 355 } |
| 356 |
| 357 case VerticalScrollGripType: { |
| 358 // Draw a vertical crosshatch for the grip. |
| 359 int longOffset = halfHeight - gripLongIndent; |
| 360 line(m_left + halfWidth, m_top + gripLongIndent, m_left + halfWidth, m_b
ottom - gripLongIndent, m_fgColor); |
| 361 line(m_left + gripShortIndent, m_top + longOffset, m_right - gripShortIn
dent, m_top + longOffset, m_fgColor); |
| 362 line(m_left + gripShortIndent, m_bottom - longOffset, m_right - gripShor
tIndent, m_bottom - longOffset, m_fgColor); |
| 363 break; |
| 364 } |
| 365 |
| 366 case LeftArrowType: |
| 367 // Draw a left arrow inside a box. |
| 368 box(m_irect, m_bgColor); |
| 369 triangle(m_right - quarterWidth, m_top + quarterHeight, m_right - quarte
rWidth, m_bottom - quarterHeight, m_left + quarterWidth, m_top + halfHeight, m_f
gColor); |
| 370 break; |
| 371 |
| 372 case RightArrowType: |
| 373 // Draw a left arrow inside a box. |
| 374 box(m_irect, m_bgColor); |
| 375 triangle(m_left + quarterWidth, m_top + quarterHeight, m_right - quarter
Width, m_top + halfHeight, m_left + quarterWidth, m_bottom - quarterHeight, m_fg
Color); |
| 376 break; |
| 377 |
| 378 case UpArrowType: |
| 379 // Draw an up arrow inside a box. |
| 380 box(m_irect, m_bgColor); |
| 381 triangle(m_left + quarterWidth, m_bottom - quarterHeight, m_left + halfW
idth, m_top + quarterHeight, m_right - quarterWidth, m_bottom - quarterHeight, m
_fgColor); |
| 382 break; |
| 383 |
| 384 case DownArrowType: |
| 385 // Draw a down arrow inside a box. |
| 386 box(m_irect, m_bgColor); |
| 387 triangle(m_left + quarterWidth, m_top + quarterHeight, m_right - quarter
Width, m_top + quarterHeight, m_left + halfWidth, m_bottom - quarterHeight, m_fg
Color); |
| 388 break; |
| 389 |
| 390 case HorizontalSliderTrackType: { |
| 391 // Draw a narrow rect for the track plus box hatches on the ends. |
| 392 SkIRect lirect; |
| 393 lirect = m_irect; |
| 394 lirect.inset(noOffset, halfHeight - sliderIndent); |
| 395 box(lirect, m_bgColor); |
| 396 line(m_left, m_top, m_left, m_bottom, m_edgeColor); |
| 397 line(m_right, m_top, m_right, m_bottom, m_edgeColor); |
| 398 break; |
| 399 } |
| 400 |
| 401 case VerticalSliderTrackType: { |
| 402 // Draw a narrow rect for the track plus box hatches on the ends. |
| 403 SkIRect lirect; |
| 404 lirect = m_irect; |
| 405 lirect.inset(halfWidth - sliderIndent, noOffset); |
| 406 box(lirect, m_bgColor); |
| 407 line(m_left, m_top, m_right, m_top, m_edgeColor); |
| 408 line(m_left, m_bottom, m_right, m_bottom, m_edgeColor); |
| 409 break; |
| 410 } |
| 411 |
| 412 case DropDownButtonType: |
| 413 // Draw a box with a big down arrow on top. |
| 414 box(m_irect, m_bgColor); |
| 415 triangle(m_left + quarterWidth, m_top, m_right - quarterWidth, m_top, m_
left + halfWidth, m_bottom, m_fgColor); |
| 416 break; |
| 417 |
| 418 default: |
| 419 BLINK_ASSERT_NOT_REACHED(); |
| 420 break; |
| 421 } |
| 422 |
| 423 markState(); |
| 424 } |
| 425 |
| 426 // Because rendering a text field is dependent on input |
| 427 // parameters the other controls don't have, we render it directly |
| 428 // rather than trying to overcomplicate draw() further. |
| 429 void WebTestThemeControlWin::drawTextField(bool drawEdges, bool fillContentArea,
SkColor color) |
| 430 { |
| 431 SkPaint paint; |
| 432 |
| 433 if (fillContentArea) { |
| 434 paint.setColor(color); |
| 435 paint.setStyle(SkPaint::kFill_Style); |
| 436 m_canvas->drawIRect(m_irect, paint); |
| 437 } |
| 438 if (drawEdges) { |
| 439 paint.setColor(m_edgeColor); |
| 440 paint.setStyle(SkPaint::kStroke_Style); |
| 441 m_canvas->drawIRect(m_irect, paint); |
| 442 } |
| 443 |
| 444 markState(); |
| 445 } |
| 446 |
| 447 void WebTestThemeControlWin::drawProgressBar(const SkIRect& fillRect) |
| 448 { |
| 449 SkPaint paint; |
| 450 |
| 451 paint.setColor(m_bgColor); |
| 452 paint.setStyle(SkPaint::kFill_Style); |
| 453 m_canvas->drawIRect(m_irect, paint); |
| 454 |
| 455 // Emulate clipping |
| 456 SkIRect tofill; |
| 457 tofill.intersect(m_irect, fillRect); |
| 458 paint.setColor(m_fgColor); |
| 459 paint.setStyle(SkPaint::kFill_Style); |
| 460 m_canvas->drawIRect(tofill, paint); |
| 461 |
| 462 markState(); |
| 463 } |
| 464 |
| 465 } |
OLD | NEW |