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 "views/controls/button/image_button.h" | 5 #include "views/controls/button/image_button.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "ui/base/animation/throb_animation.h" | 8 #include "ui/base/animation/throb_animation.h" |
9 #include "ui/gfx/canvas.h" | 9 #include "ui/gfx/canvas.h" |
10 #include "ui/gfx/skbitmap_operations.h" | 10 #include "ui/gfx/skbitmap_operations.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 //////////////////////////////////////////////////////////////////////////////// | 58 //////////////////////////////////////////////////////////////////////////////// |
59 // ImageButton, View overrides: | 59 // ImageButton, View overrides: |
60 | 60 |
61 gfx::Size ImageButton::GetPreferredSize() { | 61 gfx::Size ImageButton::GetPreferredSize() { |
62 if (!images_[BS_NORMAL].isNull()) | 62 if (!images_[BS_NORMAL].isNull()) |
63 return gfx::Size(images_[BS_NORMAL].width(), images_[BS_NORMAL].height()); | 63 return gfx::Size(images_[BS_NORMAL].width(), images_[BS_NORMAL].height()); |
64 return preferred_size_; | 64 return preferred_size_; |
65 } | 65 } |
66 | 66 |
| 67 //////////////////////////////////////////////////////////////////////////////// |
| 68 // ImageButton, protected: |
| 69 |
| 70 SkBitmap ImageButton::GetImageToPaint() { |
| 71 SkBitmap img; |
| 72 |
| 73 if (!images_[BS_HOT].isNull() && hover_animation_->is_animating()) { |
| 74 img = SkBitmapOperations::CreateBlendedBitmap(images_[BS_NORMAL], |
| 75 images_[BS_HOT], hover_animation_->GetCurrentValue()); |
| 76 } else { |
| 77 img = images_[state_]; |
| 78 } |
| 79 |
| 80 return !img.isNull() ? img : images_[BS_NORMAL]; |
| 81 } |
| 82 |
| 83 //////////////////////////////////////////////////////////////////////////////// |
| 84 // ImageButton, View overrides, protected: |
| 85 |
67 void ImageButton::OnPaint(gfx::Canvas* canvas) { | 86 void ImageButton::OnPaint(gfx::Canvas* canvas) { |
68 // Call the base class first to paint any background/borders. | 87 // Call the base class first to paint any background/borders. |
69 View::OnPaint(canvas); | 88 View::OnPaint(canvas); |
70 | 89 |
71 SkBitmap img = GetImageToPaint(); | 90 SkBitmap img = GetImageToPaint(); |
72 | 91 |
73 if (!img.isNull()) { | 92 if (!img.isNull()) { |
74 int x = 0, y = 0; | 93 int x = 0, y = 0; |
75 | 94 |
76 if (h_alignment_ == ALIGN_CENTER) | 95 if (h_alignment_ == ALIGN_CENTER) |
77 x = (width() - img.width()) / 2; | 96 x = (width() - img.width()) / 2; |
78 else if (h_alignment_ == ALIGN_RIGHT) | 97 else if (h_alignment_ == ALIGN_RIGHT) |
79 x = width() - img.width(); | 98 x = width() - img.width(); |
80 | 99 |
81 if (v_alignment_ == ALIGN_MIDDLE) | 100 if (v_alignment_ == ALIGN_MIDDLE) |
82 y = (height() - img.height()) / 2; | 101 y = (height() - img.height()) / 2; |
83 else if (v_alignment_ == ALIGN_BOTTOM) | 102 else if (v_alignment_ == ALIGN_BOTTOM) |
84 y = height() - img.height(); | 103 y = height() - img.height(); |
85 | 104 |
86 if (!background_image_.empty()) | 105 if (!background_image_.empty()) |
87 canvas->DrawBitmapInt(background_image_, x, y); | 106 canvas->DrawBitmapInt(background_image_, x, y); |
88 canvas->DrawBitmapInt(img, x, y); | 107 canvas->DrawBitmapInt(img, x, y); |
89 } | 108 } |
90 OnPaintFocusBorder(canvas); | 109 OnPaintFocusBorder(canvas); |
91 } | 110 } |
92 | 111 |
93 //////////////////////////////////////////////////////////////////////////////// | 112 //////////////////////////////////////////////////////////////////////////////// |
94 // ImageButton, protected: | |
95 | |
96 SkBitmap ImageButton::GetImageToPaint() { | |
97 SkBitmap img; | |
98 | |
99 if (!images_[BS_HOT].isNull() && hover_animation_->is_animating()) { | |
100 img = SkBitmapOperations::CreateBlendedBitmap(images_[BS_NORMAL], | |
101 images_[BS_HOT], hover_animation_->GetCurrentValue()); | |
102 } else { | |
103 img = images_[state_]; | |
104 } | |
105 | |
106 return !img.isNull() ? img : images_[BS_NORMAL]; | |
107 } | |
108 | |
109 //////////////////////////////////////////////////////////////////////////////// | |
110 // ToggleImageButton, public: | 113 // ToggleImageButton, public: |
111 | 114 |
112 ToggleImageButton::ToggleImageButton(ButtonListener* listener) | 115 ToggleImageButton::ToggleImageButton(ButtonListener* listener) |
113 : ImageButton(listener), | 116 : ImageButton(listener), |
114 toggled_(false) { | 117 toggled_(false) { |
115 } | 118 } |
116 | 119 |
117 ToggleImageButton::~ToggleImageButton() { | 120 ToggleImageButton::~ToggleImageButton() { |
118 } | 121 } |
119 | 122 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 bool ToggleImageButton::GetTooltipText(const gfx::Point& p, | 168 bool ToggleImageButton::GetTooltipText(const gfx::Point& p, |
166 std::wstring* tooltip) { | 169 std::wstring* tooltip) { |
167 if (!toggled_ || toggled_tooltip_text_.empty()) | 170 if (!toggled_ || toggled_tooltip_text_.empty()) |
168 return Button::GetTooltipText(p, tooltip); | 171 return Button::GetTooltipText(p, tooltip); |
169 | 172 |
170 *tooltip = UTF16ToWideHack(toggled_tooltip_text_); | 173 *tooltip = UTF16ToWideHack(toggled_tooltip_text_); |
171 return true; | 174 return true; |
172 } | 175 } |
173 | 176 |
174 } // namespace views | 177 } // namespace views |
OLD | NEW |