| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "app/gfx/chrome_canvas.h" | 7 #include "app/gfx/canvas.h" |
| 8 #include "app/throb_animation.h" | 8 #include "app/throb_animation.h" |
| 9 #include "skia/ext/image_operations.h" | 9 #include "skia/ext/image_operations.h" |
| 10 | 10 |
| 11 namespace views { | 11 namespace views { |
| 12 | 12 |
| 13 static const int kDefaultWidth = 16; // Default button width if no theme. | 13 static const int kDefaultWidth = 16; // Default button width if no theme. |
| 14 static const int kDefaultHeight = 14; // Default button height if no theme. | 14 static const int kDefaultHeight = 14; // Default button height if no theme. |
| 15 | 15 |
| 16 //////////////////////////////////////////////////////////////////////////////// | 16 //////////////////////////////////////////////////////////////////////////////// |
| 17 // ImageButton, public: | 17 // ImageButton, public: |
| 18 | 18 |
| 19 ImageButton::ImageButton(ButtonListener* listener) | 19 ImageButton::ImageButton(ButtonListener* listener) |
| 20 : CustomButton(listener), | 20 : CustomButton(listener), |
| 21 h_alignment_(ALIGN_LEFT), | 21 h_alignment_(ALIGN_LEFT), |
| 22 v_alignment_(ALIGN_TOP) { | 22 v_alignment_(ALIGN_TOP) { |
| 23 // By default, we request that the ChromeCanvas passed to our View::Paint() | 23 // By default, we request that the gfx::Canvas passed to our View::Paint() |
| 24 // implementation is flipped horizontally so that the button's bitmaps are | 24 // implementation is flipped horizontally so that the button's bitmaps are |
| 25 // mirrored when the UI directionality is right-to-left. | 25 // mirrored when the UI directionality is right-to-left. |
| 26 EnableCanvasFlippingForRTLUI(true); | 26 EnableCanvasFlippingForRTLUI(true); |
| 27 } | 27 } |
| 28 | 28 |
| 29 ImageButton::~ImageButton() { | 29 ImageButton::~ImageButton() { |
| 30 } | 30 } |
| 31 | 31 |
| 32 void ImageButton::SetImage(ButtonState aState, SkBitmap* anImage) { | 32 void ImageButton::SetImage(ButtonState aState, SkBitmap* anImage) { |
| 33 images_[aState] = anImage ? *anImage : SkBitmap(); | 33 images_[aState] = anImage ? *anImage : SkBitmap(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void ImageButton::SetImageAlignment(HorizontalAlignment h_align, | 36 void ImageButton::SetImageAlignment(HorizontalAlignment h_align, |
| 37 VerticalAlignment v_align) { | 37 VerticalAlignment v_align) { |
| 38 h_alignment_ = h_align; | 38 h_alignment_ = h_align; |
| 39 v_alignment_ = v_align; | 39 v_alignment_ = v_align; |
| 40 SchedulePaint(); | 40 SchedulePaint(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 //////////////////////////////////////////////////////////////////////////////// | 43 //////////////////////////////////////////////////////////////////////////////// |
| 44 // ImageButton, View overrides: | 44 // ImageButton, View overrides: |
| 45 | 45 |
| 46 gfx::Size ImageButton::GetPreferredSize() { | 46 gfx::Size ImageButton::GetPreferredSize() { |
| 47 if (!images_[BS_NORMAL].isNull()) | 47 if (!images_[BS_NORMAL].isNull()) |
| 48 return gfx::Size(images_[BS_NORMAL].width(), images_[BS_NORMAL].height()); | 48 return gfx::Size(images_[BS_NORMAL].width(), images_[BS_NORMAL].height()); |
| 49 return gfx::Size(kDefaultWidth, kDefaultHeight); | 49 return gfx::Size(kDefaultWidth, kDefaultHeight); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void ImageButton::Paint(ChromeCanvas* canvas) { | 52 void ImageButton::Paint(gfx::Canvas* canvas) { |
| 53 // Call the base class first to paint any background/borders. | 53 // Call the base class first to paint any background/borders. |
| 54 View::Paint(canvas); | 54 View::Paint(canvas); |
| 55 | 55 |
| 56 SkBitmap img = GetImageToPaint(); | 56 SkBitmap img = GetImageToPaint(); |
| 57 | 57 |
| 58 if (!img.isNull()) { | 58 if (!img.isNull()) { |
| 59 int x = 0, y = 0; | 59 int x = 0, y = 0; |
| 60 | 60 |
| 61 if (h_alignment_ == ALIGN_CENTER) | 61 if (h_alignment_ == ALIGN_CENTER) |
| 62 x = (width() - img.width()) / 2; | 62 x = (width() - img.width()) / 2; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 145 |
| 146 bool ToggleImageButton::GetTooltipText(int x, int y, std::wstring* tooltip) { | 146 bool ToggleImageButton::GetTooltipText(int x, int y, std::wstring* tooltip) { |
| 147 if (!toggled_ || toggled_tooltip_text_.empty()) | 147 if (!toggled_ || toggled_tooltip_text_.empty()) |
| 148 return Button::GetTooltipText(x, y, tooltip); | 148 return Button::GetTooltipText(x, y, tooltip); |
| 149 | 149 |
| 150 *tooltip = toggled_tooltip_text_; | 150 *tooltip = toggled_tooltip_text_; |
| 151 return true; | 151 return true; |
| 152 } | 152 } |
| 153 | 153 |
| 154 } // namespace views | 154 } // namespace views |
| OLD | NEW |