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 3 // found in the LICENSE file. 4 5 #ifndef UI_VIEWS_CONTROLS_BUTTON_IMAGE_BUTTON_H_ 6 #define UI_VIEWS_CONTROLS_BUTTON_IMAGE_BUTTON_H_ 7 8 #include "base/gtest_prod_util.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "ui/gfx/image/image_skia.h" 11 #include "ui/views/controls/button/custom_button.h" 12 13 namespace views { 14 15 // An image button. 16 17 // Note that this type of button is not focusable by default and will not be 18 // part of the focus chain. Call set_focusable(true) to make it part of the 19 // focus chain. 20 21 class VIEWS_EXPORT ImageButton : public CustomButton { 22 public: 23 enum HorizontalAlignment { 24 ALIGN_LEFT = 0, 25 ALIGN_CENTER, 26 ALIGN_RIGHT 27 }; 28 29 enum VerticalAlignment { 30 ALIGN_TOP = 0, 31 ALIGN_MIDDLE, 32 ALIGN_BOTTOM 33 }; 34 35 explicit ImageButton(ButtonListener* listener); 36 virtual ~ImageButton(); 37 38 // Returns the image for a given |state|. 39 virtual const gfx::ImageSkia& GetImage(ButtonState state) const; 40 41 // Set the image the button should use for the provided state. 42 virtual void SetImage(ButtonState state, const gfx::ImageSkia* image); 43 44 // Set the background details. 45 void SetBackground(SkColor color, 46 const gfx::ImageSkia* image, 47 const gfx::ImageSkia* mask); 48 49 // Set an |image| to draw on top of the normal / hot / pushed image. 50 // Pass NULL for no image. 51 void SetOverlayImage(const gfx::ImageSkia* image); 52 53 // Sets how the image is laid out within the button's bounds. 54 void SetImageAlignment(HorizontalAlignment h_align, 55 VerticalAlignment v_align); 56 57 // Overridden from View: 58 virtual gfx::Size GetPreferredSize() OVERRIDE; 59 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; 60 61 // Sets preferred size, so it could be correctly positioned in layout even if 62 // it is NULL. 63 void SetPreferredSize(const gfx::Size& preferred_size) { 64 preferred_size_ = preferred_size; 65 } 66 67 protected: 68 // Returns the image to paint. This is invoked from paint and returns a value 69 // from images. 70 virtual gfx::ImageSkia GetImageToPaint(); 71 72 // Updates button background for |scale_factor|. 73 void UpdateButtonBackground(ui::ScaleFactor scale_factor); 74 75 // The images used to render the different states of this button. 76 gfx::ImageSkia images_[STATE_COUNT]; 77 78 gfx::ImageSkia background_image_; 79 80 // Image to draw on top of normal / hot / pushed image. Usually empty. 81 gfx::ImageSkia overlay_image_; 82 83 private: 84 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest, Basics); 85 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest, ImagePositionWithBorder); 86 87 // Returns the correct position of the image for painting. 88 gfx::Point ComputeImagePaintPosition(const gfx::ImageSkia& image); 89 90 // Image alignment. 91 HorizontalAlignment h_alignment_; 92 VerticalAlignment v_alignment_; 93 gfx::Size preferred_size_; 94 95 DISALLOW_COPY_AND_ASSIGN(ImageButton); 96 }; 97 98 //////////////////////////////////////////////////////////////////////////////// 99 // 100 // ToggleImageButton 101 // 102 // A toggle-able ImageButton. It swaps out its graphics when toggled. 103 // 104 //////////////////////////////////////////////////////////////////////////////// 105 class VIEWS_EXPORT ToggleImageButton : public ImageButton { 106 public: 107 explicit ToggleImageButton(ButtonListener* listener); 108 virtual ~ToggleImageButton(); 109 110 // Change the toggled state. 111 void SetToggled(bool toggled); 112 113 // Like ImageButton::SetImage(), but to set the graphics used for the 114 // "has been toggled" state. Must be called for each button state 115 // before the button is toggled. 116 void SetToggledImage(ButtonState state, const gfx::ImageSkia* image); 117 118 // Set the tooltip text displayed when the button is toggled. 119 void SetToggledTooltipText(const string16& tooltip); 120 121 // Overridden from ImageButton: 122 virtual const gfx::ImageSkia& GetImage(ButtonState state) const OVERRIDE; 123 virtual void SetImage(ButtonState state, 124 const gfx::ImageSkia* image) OVERRIDE; 125 126 // Overridden from View: 127 virtual bool GetTooltipText(const gfx::Point& p, 128 string16* tooltip) const OVERRIDE; 129 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 130 131 private: 132 // The parent class's images_ member is used for the current images, 133 // and this array is used to hold the alternative images. 134 // We swap between the two when toggling. 135 gfx::ImageSkia alternate_images_[STATE_COUNT]; 136 137 // True if the button is currently toggled. 138 bool toggled_; 139 140 // The parent class's tooltip_text_ is displayed when not toggled, and 141 // this one is shown when toggled. 142 string16 toggled_tooltip_text_; 143 144 DISALLOW_COPY_AND_ASSIGN(ToggleImageButton); 145 }; 146 147 } // namespace views 148 149 #endif // UI_VIEWS_CONTROLS_BUTTON_IMAGE_BUTTON_H_ 150