OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "views/bubble/bubble_border.h" | 5 #include "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" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
13 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
14 #include "ui/gfx/canvas_skia.h" | 14 #include "ui/gfx/canvas_skia.h" |
15 #include "ui/gfx/path.h" | 15 #include "ui/gfx/path.h" |
16 | 16 |
17 namespace views { | 17 namespace views { |
18 | 18 |
19 // static | 19 // static |
20 SkBitmap* BubbleBorder::left_ = NULL; | 20 struct BorderImages* BubbleBorder::normal_images_ = NULL; |
21 SkBitmap* BubbleBorder::top_left_ = NULL; | 21 struct BorderImages* BubbleBorder::shadow_images_ = NULL; |
22 SkBitmap* BubbleBorder::top_ = NULL; | |
23 SkBitmap* BubbleBorder::top_right_ = NULL; | |
24 SkBitmap* BubbleBorder::right_ = NULL; | |
25 SkBitmap* BubbleBorder::bottom_right_ = NULL; | |
26 SkBitmap* BubbleBorder::bottom_ = NULL; | |
27 SkBitmap* BubbleBorder::bottom_left_ = NULL; | |
28 SkBitmap* BubbleBorder::top_arrow_ = NULL; | |
29 SkBitmap* BubbleBorder::bottom_arrow_ = NULL; | |
30 SkBitmap* BubbleBorder::left_arrow_ = NULL; | |
31 SkBitmap* BubbleBorder::right_arrow_ = NULL; | |
32 | |
33 // static | |
34 int BubbleBorder::arrow_offset_; | |
35 | 22 |
36 // The height inside the arrow image, in pixels. | 23 // The height inside the arrow image, in pixels. |
37 static const int kArrowInteriorHeight = 7; | 24 static const int kArrowInteriorHeight = 7; |
38 | 25 |
39 gfx::Rect BubbleBorder::GetBounds(const gfx::Rect& position_relative_to, | 26 gfx::Rect BubbleBorder::GetBounds(const gfx::Rect& position_relative_to, |
40 const gfx::Size& contents_size) const { | 27 const gfx::Size& contents_size) const { |
41 // Desired size is size of contents enlarged by the size of the border images. | 28 // Desired size is size of contents enlarged by the size of the border images. |
42 gfx::Size border_size(contents_size); | 29 gfx::Size border_size(contents_size); |
43 gfx::Insets insets; | 30 gfx::Insets insets; |
44 GetInsets(&insets); | 31 GetInsets(&insets); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
164 GetInsets(&insets); | 151 GetInsets(&insets); |
165 border_size.Enlarge(insets.left() + insets.right(), | 152 border_size.Enlarge(insets.left() + insets.right(), |
166 insets.top() + insets.bottom()); | 153 insets.top() + insets.bottom()); |
167 offset = std::max(arrow_offset_, | 154 offset = std::max(arrow_offset_, |
168 std::min(offset, (is_arrow_on_horizontal(arrow_location_) ? | 155 std::min(offset, (is_arrow_on_horizontal(arrow_location_) ? |
169 border_size.width() : border_size.height()) - arrow_offset_)); | 156 border_size.width() : border_size.height()) - arrow_offset_)); |
170 override_arrow_offset_ = offset; | 157 override_arrow_offset_ = offset; |
171 return override_arrow_offset_; | 158 return override_arrow_offset_; |
172 } | 159 } |
173 | 160 |
174 // static | 161 void BubbleBorder::InitClass(Shadow shadow) { |
175 void BubbleBorder::InitClass() { | 162 if (shadow == SHADOW && shadow_images_ == NULL) { |
176 static bool initialized = false; | |
177 if (!initialized) { | |
178 // Load images. | |
179 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 163 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
180 left_ = rb.GetBitmapNamed(IDR_BUBBLE_L); | 164 shadow_images_ = new BorderImages(); |
181 top_left_ = rb.GetBitmapNamed(IDR_BUBBLE_TL); | 165 shadow_images_->left = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_L); |
182 top_ = rb.GetBitmapNamed(IDR_BUBBLE_T); | 166 shadow_images_->top_left = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_TL); |
183 top_right_ = rb.GetBitmapNamed(IDR_BUBBLE_TR); | 167 shadow_images_->top = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_T); |
184 right_ = rb.GetBitmapNamed(IDR_BUBBLE_R); | 168 shadow_images_->top_right = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_TR); |
185 bottom_right_ = rb.GetBitmapNamed(IDR_BUBBLE_BR); | 169 shadow_images_->right = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_R); |
186 bottom_ = rb.GetBitmapNamed(IDR_BUBBLE_B); | 170 shadow_images_->bottom_right = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_BR); |
187 bottom_left_ = rb.GetBitmapNamed(IDR_BUBBLE_BL); | 171 shadow_images_->bottom = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_B); |
188 left_arrow_ = rb.GetBitmapNamed(IDR_BUBBLE_L_ARROW); | 172 shadow_images_->bottom_left = rb.GetBitmapNamed(IDR_BUBBLE_SHADOW_BL); |
189 top_arrow_ = rb.GetBitmapNamed(IDR_BUBBLE_T_ARROW); | 173 shadow_images_->left_arrow = new SkBitmap(); |
190 right_arrow_ = rb.GetBitmapNamed(IDR_BUBBLE_R_ARROW); | 174 shadow_images_->top_arrow = new SkBitmap(); |
191 bottom_arrow_ = rb.GetBitmapNamed(IDR_BUBBLE_B_ARROW); | 175 shadow_images_->right_arrow = new SkBitmap(); |
176 shadow_images_->bottom_arrow = new SkBitmap(); | |
177 } else if (shadow == NO_SHADOW && normal_images_ == NULL) { | |
178 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
179 normal_images_ = new BorderImages(); | |
180 normal_images_->left = rb.GetBitmapNamed(IDR_BUBBLE_L); | |
181 normal_images_->top_left = rb.GetBitmapNamed(IDR_BUBBLE_TL); | |
182 normal_images_->top = rb.GetBitmapNamed(IDR_BUBBLE_T); | |
183 normal_images_->top_right = rb.GetBitmapNamed(IDR_BUBBLE_TR); | |
184 normal_images_->right = rb.GetBitmapNamed(IDR_BUBBLE_R); | |
185 normal_images_->bottom_right = rb.GetBitmapNamed(IDR_BUBBLE_BR); | |
186 normal_images_->bottom = rb.GetBitmapNamed(IDR_BUBBLE_B); | |
187 normal_images_->bottom_left = rb.GetBitmapNamed(IDR_BUBBLE_BL); | |
188 normal_images_->left_arrow = rb.GetBitmapNamed(IDR_BUBBLE_L_ARROW); | |
189 normal_images_->top_arrow = rb.GetBitmapNamed(IDR_BUBBLE_T_ARROW); | |
190 normal_images_->right_arrow = rb.GetBitmapNamed(IDR_BUBBLE_R_ARROW); | |
191 normal_images_->bottom_arrow = rb.GetBitmapNamed(IDR_BUBBLE_B_ARROW); | |
192 } | |
192 | 193 |
193 // Calculate horizontal and vertical insets for arrow by ensuring that | 194 struct BorderImages* images = |
194 // the widest arrow and corner images will have enough room to avoid overlap | 195 shadow == SHADOW ? shadow_images_ : normal_images_; |
195 int offset_x = | 196 left_ = images->left; |
196 (std::max(top_arrow_->width(), bottom_arrow_->width()) / 2) + | 197 top_left_ = images->top_left; |
197 std::max(std::max(top_left_->width(), top_right_->width()), | 198 top_ = images->top; |
198 std::max(bottom_left_->width(), bottom_right_->width())); | 199 top_right_ = images->top_right; |
199 int offset_y = | 200 right_ = images->right; |
200 (std::max(left_arrow_->height(), right_arrow_->height()) / 2) + | 201 bottom_right_ = images->bottom_right; |
201 std::max(std::max(top_left_->height(), top_right_->height()), | 202 bottom_ = images->bottom; |
202 std::max(bottom_left_->height(), bottom_right_->height())); | 203 bottom_left_ = images->bottom_left; |
203 arrow_offset_ = std::max(offset_x, offset_y); | 204 left_arrow_ = images->left_arrow; |
205 top_arrow_ = images->top_arrow; | |
206 right_arrow_ = images->right_arrow; | |
207 bottom_arrow_ = images->bottom_arrow; | |
204 | 208 |
205 initialized = true; | 209 // Calculate horizontal and vertical insets for arrow by ensuring that |
206 } | 210 // the widest arrow and corner images will have enough room to avoid overlap |
211 int offset_x = | |
212 (std::max(top_arrow_->width(), bottom_arrow_->width()) / 2) + | |
213 std::max(std::max(top_left_->width(), top_right_->width()), | |
214 std::max(bottom_left_->width(), bottom_right_->width())); | |
215 int offset_y = | |
216 (std::max(left_arrow_->height(), right_arrow_->height()) / 2) + | |
217 std::max(std::max(top_left_->height(), top_right_->height()), | |
218 std::max(bottom_left_->height(), bottom_right_->height())); | |
219 arrow_offset_ = std::max(offset_x, offset_y); | |
sky
2011/11/15 05:06:37
This method is poorly named now that it sets both
koz (OOO until 15th September)
2011/11/15 06:35:43
Done.
| |
207 } | 220 } |
208 | 221 |
209 BubbleBorder::~BubbleBorder() {} | 222 BubbleBorder::~BubbleBorder() {} |
210 | 223 |
211 void BubbleBorder::Paint(const views::View& view, gfx::Canvas* canvas) const { | 224 void BubbleBorder::Paint(const views::View& view, gfx::Canvas* canvas) const { |
212 // Convenience shorthand variables. | 225 // Convenience shorthand variables. |
213 const int tl_width = top_left_->width(); | 226 const int tl_width = top_left_->width(); |
214 const int tl_height = top_left_->height(); | 227 const int tl_height = top_left_->height(); |
215 const int t_height = top_->height(); | 228 const int t_height = top_->height(); |
216 const int tr_width = top_right_->width(); | 229 const int tr_width = top_right_->width(); |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
449 gfx::Rect bounds(view->GetContentsBounds()); | 462 gfx::Rect bounds(view->GetContentsBounds()); |
450 SkRect rect; | 463 SkRect rect; |
451 rect.set(SkIntToScalar(bounds.x()), SkIntToScalar(bounds.y()), | 464 rect.set(SkIntToScalar(bounds.x()), SkIntToScalar(bounds.y()), |
452 SkIntToScalar(bounds.right()), SkIntToScalar(bounds.bottom())); | 465 SkIntToScalar(bounds.right()), SkIntToScalar(bounds.bottom())); |
453 SkScalar radius = SkIntToScalar(BubbleBorder::GetCornerRadius()); | 466 SkScalar radius = SkIntToScalar(BubbleBorder::GetCornerRadius()); |
454 path.addRoundRect(rect, radius, radius); | 467 path.addRoundRect(rect, radius, radius); |
455 canvas->GetSkCanvas()->drawPath(path, paint); | 468 canvas->GetSkCanvas()->drawPath(path, paint); |
456 } | 469 } |
457 | 470 |
458 } // namespace views | 471 } // namespace views |
OLD | NEW |