| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ui/views/bubble/bubble_border.h" | 5 #include "ui/views/bubble/bubble_border.h" |
| 6 | 6 |
| 7 #include <algorithm> // for std::max | 7 #include <algorithm> // for std::max |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "grit/ui_resources.h" | 10 #include "grit/ui_resources.h" |
| 11 #include "grit/ui_resources_standard.h" | 11 #include "grit/ui_resources_standard.h" |
| 12 #include "third_party/skia/include/core/SkBitmap.h" | |
| 13 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gfx/image/image_skia.h" |
| 15 #include "ui/gfx/skia_util.h" | 15 #include "ui/gfx/skia_util.h" |
| 16 | 16 |
| 17 namespace views { | 17 namespace views { |
| 18 | 18 |
| 19 struct BubbleBorder::BorderImages { | 19 struct BubbleBorder::BorderImages { |
| 20 BorderImages() | 20 BorderImages() |
| 21 : left(NULL), | 21 : left(NULL), |
| 22 top_left(NULL), | 22 top_left(NULL), |
| 23 top(NULL), | 23 top(NULL), |
| 24 top_right(NULL), | 24 top_right(NULL), |
| 25 right(NULL), | 25 right(NULL), |
| 26 bottom_right(NULL), | 26 bottom_right(NULL), |
| 27 bottom(NULL), | 27 bottom(NULL), |
| 28 bottom_left(NULL), | 28 bottom_left(NULL), |
| 29 left_arrow(NULL), | 29 left_arrow(NULL), |
| 30 top_arrow(NULL), | 30 top_arrow(NULL), |
| 31 right_arrow(NULL), | 31 right_arrow(NULL), |
| 32 bottom_arrow(NULL), | 32 bottom_arrow(NULL), |
| 33 border_thickness(0) { | 33 border_thickness(0) { |
| 34 } | 34 } |
| 35 | 35 |
| 36 SkBitmap* left; | 36 gfx::ImageSkia* left; |
| 37 SkBitmap* top_left; | 37 gfx::ImageSkia* top_left; |
| 38 SkBitmap* top; | 38 gfx::ImageSkia* top; |
| 39 SkBitmap* top_right; | 39 gfx::ImageSkia* top_right; |
| 40 SkBitmap* right; | 40 gfx::ImageSkia* right; |
| 41 SkBitmap* bottom_right; | 41 gfx::ImageSkia* bottom_right; |
| 42 SkBitmap* bottom; | 42 gfx::ImageSkia* bottom; |
| 43 SkBitmap* bottom_left; | 43 gfx::ImageSkia* bottom_left; |
| 44 SkBitmap* left_arrow; | 44 gfx::ImageSkia* left_arrow; |
| 45 SkBitmap* top_arrow; | 45 gfx::ImageSkia* top_arrow; |
| 46 SkBitmap* right_arrow; | 46 gfx::ImageSkia* right_arrow; |
| 47 SkBitmap* bottom_arrow; | 47 gfx::ImageSkia* bottom_arrow; |
| 48 int border_thickness; | 48 int border_thickness; |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 // static | 51 // static |
| 52 struct BubbleBorder::BorderImages* BubbleBorder::normal_images_ = NULL; | 52 struct BubbleBorder::BorderImages* BubbleBorder::normal_images_ = NULL; |
| 53 struct BubbleBorder::BorderImages* BubbleBorder::shadow_images_ = NULL; | 53 struct BubbleBorder::BorderImages* BubbleBorder::shadow_images_ = NULL; |
| 54 | 54 |
| 55 // The height inside the arrow image, in pixels. | 55 // The height inside the arrow image, in pixels. |
| 56 static const int kArrowInteriorHeight = 7; | 56 static const int kArrowInteriorHeight = 7; |
| 57 | 57 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 84 gfx::Rect BubbleBorder::GetBounds(const gfx::Rect& position_relative_to, | 84 gfx::Rect BubbleBorder::GetBounds(const gfx::Rect& position_relative_to, |
| 85 const gfx::Size& contents_size) const { | 85 const gfx::Size& contents_size) const { |
| 86 // Desired size is size of contents enlarged by the size of the border images. | 86 // Desired size is size of contents enlarged by the size of the border images. |
| 87 gfx::Size border_size(contents_size); | 87 gfx::Size border_size(contents_size); |
| 88 gfx::Insets insets; | 88 gfx::Insets insets; |
| 89 GetInsets(&insets); | 89 GetInsets(&insets); |
| 90 border_size.Enlarge(insets.width(), insets.height()); | 90 border_size.Enlarge(insets.width(), insets.height()); |
| 91 | 91 |
| 92 // Screen position depends on the arrow location. | 92 // Screen position depends on the arrow location. |
| 93 // The arrow should overlap the target by some amount since there is space | 93 // The arrow should overlap the target by some amount since there is space |
| 94 // for shadow between arrow tip and bitmap bounds. | 94 // for shadow between arrow tip and image bounds. |
| 95 const int kArrowOverlap = 3; | 95 const int kArrowOverlap = 3; |
| 96 int x = position_relative_to.x(); | 96 int x = position_relative_to.x(); |
| 97 int y = position_relative_to.y(); | 97 int y = position_relative_to.y(); |
| 98 int w = position_relative_to.width(); | 98 int w = position_relative_to.width(); |
| 99 int h = position_relative_to.height(); | 99 int h = position_relative_to.height(); |
| 100 int arrow_offset = override_arrow_offset_ ? override_arrow_offset_ : | 100 int arrow_offset = override_arrow_offset_ ? override_arrow_offset_ : |
| 101 arrow_offset_; | 101 arrow_offset_; |
| 102 | 102 |
| 103 // Calculate bubble x coordinate. | 103 // Calculate bubble x coordinate. |
| 104 switch (arrow_location_) { | 104 switch (arrow_location_) { |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 border_size.width() : border_size.height()) - arrow_offset_)); | 217 border_size.width() : border_size.height()) - arrow_offset_)); |
| 218 override_arrow_offset_ = offset; | 218 override_arrow_offset_ = offset; |
| 219 return override_arrow_offset_; | 219 return override_arrow_offset_; |
| 220 } | 220 } |
| 221 | 221 |
| 222 // static | 222 // static |
| 223 BubbleBorder::BorderImages* BubbleBorder::GetBorderImages(Shadow shadow) { | 223 BubbleBorder::BorderImages* BubbleBorder::GetBorderImages(Shadow shadow) { |
| 224 if (shadow == SHADOW && shadow_images_ == NULL) { | 224 if (shadow == SHADOW && shadow_images_ == NULL) { |
| 225 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 225 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 226 shadow_images_ = new BorderImages(); | 226 shadow_images_ = new BorderImages(); |
| 227 shadow_images_->left = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_L); | 227 shadow_images_->left = rb.GetImageSkiaNamed(IDR_BUBBLE_SHADOW_L); |
| 228 shadow_images_->top_left = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_TL); | 228 shadow_images_->top_left = rb.GetImageSkiaNamed(IDR_BUBBLE_SHADOW_TL); |
| 229 shadow_images_->top = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_T); | 229 shadow_images_->top = rb.GetImageSkiaNamed(IDR_BUBBLE_SHADOW_T); |
| 230 shadow_images_->top_right = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_TR); | 230 shadow_images_->top_right = rb.GetImageSkiaNamed(IDR_BUBBLE_SHADOW_TR); |
| 231 shadow_images_->right = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_R); | 231 shadow_images_->right = rb.GetImageSkiaNamed(IDR_BUBBLE_SHADOW_R); |
| 232 shadow_images_->bottom_right = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_BR); | 232 shadow_images_->bottom_right = rb.GetImageSkiaNamed(IDR_BUBBLE_SHADOW_BR); |
| 233 shadow_images_->bottom = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_B); | 233 shadow_images_->bottom = rb.GetImageSkiaNamed(IDR_BUBBLE_SHADOW_B); |
| 234 shadow_images_->bottom_left = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_BL); | 234 shadow_images_->bottom_left = rb.GetImageSkiaNamed(IDR_BUBBLE_SHADOW_BL); |
| 235 shadow_images_->left_arrow = new SkBitmap(); | 235 shadow_images_->left_arrow = new gfx::ImageSkia(); |
| 236 shadow_images_->top_arrow = new SkBitmap(); | 236 shadow_images_->top_arrow = new gfx::ImageSkia(); |
| 237 shadow_images_->right_arrow = new SkBitmap(); | 237 shadow_images_->right_arrow = new gfx::ImageSkia(); |
| 238 shadow_images_->bottom_arrow = new SkBitmap(); | 238 shadow_images_->bottom_arrow = new gfx::ImageSkia(); |
| 239 shadow_images_->border_thickness = 10; | 239 shadow_images_->border_thickness = 10; |
| 240 } else if (shadow == NO_SHADOW && normal_images_ == NULL) { | 240 } else if (shadow == NO_SHADOW && normal_images_ == NULL) { |
| 241 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 241 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 242 normal_images_ = new BorderImages(); | 242 normal_images_ = new BorderImages(); |
| 243 normal_images_->left = rb.GetBitmapNamed(IDR_BUBBLE_L); | 243 normal_images_->left = rb.GetImageSkiaNamed(IDR_BUBBLE_L); |
| 244 normal_images_->top_left = rb.GetBitmapNamed(IDR_BUBBLE_TL); | 244 normal_images_->top_left = rb.GetImageSkiaNamed(IDR_BUBBLE_TL); |
| 245 normal_images_->top = rb.GetBitmapNamed(IDR_BUBBLE_T); | 245 normal_images_->top = rb.GetImageSkiaNamed(IDR_BUBBLE_T); |
| 246 normal_images_->top_right = rb.GetBitmapNamed(IDR_BUBBLE_TR); | 246 normal_images_->top_right = rb.GetImageSkiaNamed(IDR_BUBBLE_TR); |
| 247 normal_images_->right = rb.GetBitmapNamed(IDR_BUBBLE_R); | 247 normal_images_->right = rb.GetImageSkiaNamed(IDR_BUBBLE_R); |
| 248 normal_images_->bottom_right = rb.GetBitmapNamed(IDR_BUBBLE_BR); | 248 normal_images_->bottom_right = rb.GetImageSkiaNamed(IDR_BUBBLE_BR); |
| 249 normal_images_->bottom = rb.GetBitmapNamed(IDR_BUBBLE_B); | 249 normal_images_->bottom = rb.GetImageSkiaNamed(IDR_BUBBLE_B); |
| 250 normal_images_->bottom_left = rb.GetBitmapNamed(IDR_BUBBLE_BL); | 250 normal_images_->bottom_left = rb.GetImageSkiaNamed(IDR_BUBBLE_BL); |
| 251 normal_images_->left_arrow = rb.GetBitmapNamed(IDR_BUBBLE_L_ARROW); | 251 normal_images_->left_arrow = rb.GetImageSkiaNamed(IDR_BUBBLE_L_ARROW); |
| 252 normal_images_->top_arrow = rb.GetBitmapNamed(IDR_BUBBLE_T_ARROW); | 252 normal_images_->top_arrow = rb.GetImageSkiaNamed(IDR_BUBBLE_T_ARROW); |
| 253 normal_images_->right_arrow = rb.GetBitmapNamed(IDR_BUBBLE_R_ARROW); | 253 normal_images_->right_arrow = rb.GetImageSkiaNamed(IDR_BUBBLE_R_ARROW); |
| 254 normal_images_->bottom_arrow = rb.GetBitmapNamed(IDR_BUBBLE_B_ARROW); | 254 normal_images_->bottom_arrow = rb.GetImageSkiaNamed(IDR_BUBBLE_B_ARROW); |
| 255 normal_images_->border_thickness = 0; | 255 normal_images_->border_thickness = 0; |
| 256 } | 256 } |
| 257 return shadow == SHADOW ? shadow_images_ : normal_images_; | 257 return shadow == SHADOW ? shadow_images_ : normal_images_; |
| 258 } | 258 } |
| 259 | 259 |
| 260 BubbleBorder::~BubbleBorder() {} | 260 BubbleBorder::~BubbleBorder() {} |
| 261 | 261 |
| 262 void BubbleBorder::Paint(const views::View& view, gfx::Canvas* canvas) const { | 262 void BubbleBorder::Paint(const views::View& view, gfx::Canvas* canvas) const { |
| 263 // Convenience shorthand variables. | 263 // Convenience shorthand variables. |
| 264 const int tl_width = images_->top_left->width(); | 264 const int tl_width = images_->top_left->width(); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 canvas->TileImageInt(*images_->bottom, left + bl_width, bottom - b_height, | 415 canvas->TileImageInt(*images_->bottom, left + bl_width, bottom - b_height, |
| 416 width - bl_width - br_width, b_height); | 416 width - bl_width - br_width, b_height); |
| 417 } | 417 } |
| 418 | 418 |
| 419 // Bottom left corner. | 419 // Bottom left corner. |
| 420 canvas->DrawBitmapInt(*images_->bottom_left, left, bottom - bl_height); | 420 canvas->DrawBitmapInt(*images_->bottom_left, left, bottom - bl_height); |
| 421 } | 421 } |
| 422 | 422 |
| 423 void BubbleBorder::DrawEdgeWithArrow(gfx::Canvas* canvas, | 423 void BubbleBorder::DrawEdgeWithArrow(gfx::Canvas* canvas, |
| 424 bool is_horizontal, | 424 bool is_horizontal, |
| 425 SkBitmap* edge, | 425 gfx::ImageSkia* edge, |
| 426 SkBitmap* arrow, | 426 gfx::ImageSkia* arrow, |
| 427 int start_x, | 427 int start_x, |
| 428 int start_y, | 428 int start_y, |
| 429 int before_arrow, | 429 int before_arrow, |
| 430 int after_arrow, | 430 int after_arrow, |
| 431 int offset) const { | 431 int offset) const { |
| 432 /* Here's what the parameters mean: | 432 /* Here's what the parameters mean: |
| 433 * start_x | 433 * start_x |
| 434 * . | 434 * . |
| 435 * . ┌───┐ ┬ offset | 435 * . ┌───┐ ┬ offset |
| 436 * start_y..........┌────┬────────┤ ▲ ├────────┬────┐ | 436 * start_y..........┌────┬────────┤ ▲ ├────────┬────┐ |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 paint.setColor(border_->background_color()); | 512 paint.setColor(border_->background_color()); |
| 513 SkPath path; | 513 SkPath path; |
| 514 gfx::Rect bounds(view->GetContentsBounds()); | 514 gfx::Rect bounds(view->GetContentsBounds()); |
| 515 bounds.Inset(-border_->border_thickness(), -border_->border_thickness()); | 515 bounds.Inset(-border_->border_thickness(), -border_->border_thickness()); |
| 516 SkScalar radius = SkIntToScalar(BubbleBorder::GetCornerRadius()); | 516 SkScalar radius = SkIntToScalar(BubbleBorder::GetCornerRadius()); |
| 517 path.addRoundRect(gfx::RectToSkRect(bounds), radius, radius); | 517 path.addRoundRect(gfx::RectToSkRect(bounds), radius, radius); |
| 518 canvas->DrawPath(path, paint); | 518 canvas->DrawPath(path, paint); |
| 519 } | 519 } |
| 520 | 520 |
| 521 } // namespace views | 521 } // namespace views |
| OLD | NEW |