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.
5 #ifndef UI_VIEWS_CONTROLS_BUTTON_IMAGE_BUTTON_H_
6 #define UI_VIEWS_CONTROLS_BUTTON_IMAGE_BUTTON_H_
8 #include "base/gtest_prod_util.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "ui/base/layout.h"
11 #include "ui/gfx/image/image_skia.h"
12 #include "ui/views/controls/button/custom_button.h"
18 // Note that this type of button is not focusable by default and will not be
19 // part of the focus chain. Call set_focusable(true) to make it part of the
22 class VIEWS_EXPORT ImageButton : public CustomButton {
24 static const char kViewClassName[];
26 enum HorizontalAlignment {
32 enum VerticalAlignment {
38 explicit ImageButton(ButtonListener* listener);
39 virtual ~ImageButton();
41 // Returns the image for a given |state|.
42 virtual const gfx::ImageSkia& GetImage(ButtonState state) const;
44 // Set the image the button should use for the provided state.
45 virtual void SetImage(ButtonState state, const gfx::ImageSkia* image);
47 // Set the background details.
48 void SetBackground(SkColor color,
49 const gfx::ImageSkia* image,
50 const gfx::ImageSkia* mask);
52 // Set an |image| to draw on top of the normal / hot / pushed image.
53 // Pass NULL for no image.
54 void SetOverlayImage(const gfx::ImageSkia* image);
56 // Sets how the image is laid out within the button's bounds.
57 void SetImageAlignment(HorizontalAlignment h_align,
58 VerticalAlignment v_align);
60 // Overridden from View:
61 virtual gfx::Size GetPreferredSize() OVERRIDE;
62 virtual const char* GetClassName() const OVERRIDE;
63 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
65 // Sets preferred size, so it could be correctly positioned in layout even if
67 void SetPreferredSize(const gfx::Size& preferred_size) {
68 preferred_size_ = preferred_size;
71 // Whether we should draw our images resources horizontally flipped.
72 void SetDrawImageMirrored(bool mirrored) {
73 draw_image_mirrored_ = mirrored;
77 // Returns the image to paint. This is invoked from paint and returns a value
79 virtual gfx::ImageSkia GetImageToPaint();
81 // Updates button background for |scale_factor|.
82 void UpdateButtonBackground(ui::ScaleFactor scale_factor);
84 // The images used to render the different states of this button.
85 gfx::ImageSkia images_[STATE_COUNT];
87 gfx::ImageSkia background_image_;
89 // Image to draw on top of normal / hot / pushed image. Usually empty.
90 gfx::ImageSkia overlay_image_;
93 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest, Basics);
94 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest, ImagePositionWithBorder);
96 // Returns the correct position of the image for painting.
97 gfx::Point ComputeImagePaintPosition(const gfx::ImageSkia& image);
100 HorizontalAlignment h_alignment_;
101 VerticalAlignment v_alignment_;
102 gfx::Size preferred_size_;
104 // Whether we draw our resources horizontally flipped. This can happen in the
105 // linux titlebar, where image resources were designed to be flipped so a
106 // small curved corner in the close button designed to fit into the frame
108 bool draw_image_mirrored_;
110 DISALLOW_COPY_AND_ASSIGN(ImageButton);
113 ////////////////////////////////////////////////////////////////////////////////
117 // A toggle-able ImageButton. It swaps out its graphics when toggled.
119 ////////////////////////////////////////////////////////////////////////////////
120 class VIEWS_EXPORT ToggleImageButton : public ImageButton {
122 explicit ToggleImageButton(ButtonListener* listener);
123 virtual ~ToggleImageButton();
125 // Change the toggled state.
126 void SetToggled(bool toggled);
128 // Like ImageButton::SetImage(), but to set the graphics used for the
129 // "has been toggled" state. Must be called for each button state
130 // before the button is toggled.
131 void SetToggledImage(ButtonState state, const gfx::ImageSkia* image);
133 // Set the tooltip text displayed when the button is toggled.
134 void SetToggledTooltipText(const string16& tooltip);
136 // Overridden from ImageButton:
137 virtual const gfx::ImageSkia& GetImage(ButtonState state) const OVERRIDE;
138 virtual void SetImage(ButtonState state,
139 const gfx::ImageSkia* image) OVERRIDE;
141 // Overridden from View:
142 virtual bool GetTooltipText(const gfx::Point& p,
143 string16* tooltip) const OVERRIDE;
144 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
147 // The parent class's images_ member is used for the current images,
148 // and this array is used to hold the alternative images.
149 // We swap between the two when toggling.
150 gfx::ImageSkia alternate_images_[STATE_COUNT];
152 // True if the button is currently toggled.
155 // The parent class's tooltip_text_ is displayed when not toggled, and
156 // this one is shown when toggled.
157 string16 toggled_tooltip_text_;
159 DISALLOW_COPY_AND_ASSIGN(ToggleImageButton);
164 #endif // UI_VIEWS_CONTROLS_BUTTON_IMAGE_BUTTON_H_