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"
20 // Note that this type of button is not focusable by default and will not be
21 // part of the focus chain. Call SetFocusable(true) to make it part of the
24 class VIEWS_EXPORT ImageButton
: public CustomButton
{
26 static const char kViewClassName
[];
28 enum HorizontalAlignment
{
34 enum VerticalAlignment
{
40 explicit ImageButton(ButtonListener
* listener
);
41 ~ImageButton() override
;
43 // Returns the image for a given |state|.
44 virtual const gfx::ImageSkia
& GetImage(ButtonState state
) const;
46 // Set the image the button should use for the provided state.
47 virtual void SetImage(ButtonState state
, const gfx::ImageSkia
* image
);
49 // Set the background details.
50 void SetBackground(SkColor color
,
51 const gfx::ImageSkia
* image
,
52 const gfx::ImageSkia
* mask
);
54 // Sets how the image is laid out within the button's bounds.
55 void SetImageAlignment(HorizontalAlignment h_align
,
56 VerticalAlignment v_align
);
58 void SetFocusPainter(scoped_ptr
<Painter
> focus_painter
);
60 // The minimum size of the contents (not including the border). The contents
61 // will be at least this size, but may be larger if the image itself is
63 const gfx::Size
& minimum_image_size() const { return minimum_image_size_
; }
64 void SetMinimumImageSize(const gfx::Size
& size
);
66 // Whether we should draw our images resources horizontally flipped.
67 void SetDrawImageMirrored(bool mirrored
) {
68 draw_image_mirrored_
= mirrored
;
71 // Overridden from View:
72 gfx::Size
GetPreferredSize() const override
;
73 const char* GetClassName() const override
;
74 void OnPaint(gfx::Canvas
* canvas
) override
;
77 // Overridden from View:
78 void OnFocus() override
;
79 void OnBlur() override
;
81 // Returns the image to paint. This is invoked from paint and returns a value
83 virtual gfx::ImageSkia
GetImageToPaint();
85 // Updates button background for |scale_factor|.
86 void UpdateButtonBackground(ui::ScaleFactor scale_factor
);
88 Painter
* focus_painter() { return focus_painter_
.get(); }
90 // The images used to render the different states of this button.
91 gfx::ImageSkia images_
[STATE_COUNT
];
93 gfx::ImageSkia background_image_
;
96 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest
, Basics
);
97 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest
, ImagePositionWithBorder
);
98 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest
, LeftAlignedMirrored
);
99 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest
, RightAlignedMirrored
);
101 // Returns the correct position of the image for painting.
102 gfx::Point
ComputeImagePaintPosition(const gfx::ImageSkia
& image
);
105 HorizontalAlignment h_alignment_
;
106 VerticalAlignment v_alignment_
;
107 gfx::Size minimum_image_size_
;
109 // Whether we draw our resources horizontally flipped. This can happen in the
110 // linux titlebar, where image resources were designed to be flipped so a
111 // small curved corner in the close button designed to fit into the frame
113 bool draw_image_mirrored_
;
115 scoped_ptr
<Painter
> focus_painter_
;
117 DISALLOW_COPY_AND_ASSIGN(ImageButton
);
120 ////////////////////////////////////////////////////////////////////////////////
124 // A toggle-able ImageButton. It swaps out its graphics when toggled.
126 ////////////////////////////////////////////////////////////////////////////////
127 class VIEWS_EXPORT ToggleImageButton
: public ImageButton
{
129 explicit ToggleImageButton(ButtonListener
* listener
);
130 ~ToggleImageButton() override
;
132 // Change the toggled state.
133 void SetToggled(bool toggled
);
135 // Like ImageButton::SetImage(), but to set the graphics used for the
136 // "has been toggled" state. Must be called for each button state
137 // before the button is toggled.
138 void SetToggledImage(ButtonState state
, const gfx::ImageSkia
* image
);
140 // Set the tooltip text displayed when the button is toggled.
141 void SetToggledTooltipText(const base::string16
& tooltip
);
143 // Overridden from ImageButton:
144 const gfx::ImageSkia
& GetImage(ButtonState state
) const override
;
145 void SetImage(ButtonState state
, const gfx::ImageSkia
* image
) override
;
147 // Overridden from View:
148 bool GetTooltipText(const gfx::Point
& p
,
149 base::string16
* tooltip
) const override
;
150 void GetAccessibleState(ui::AXViewState
* state
) override
;
153 // The parent class's images_ member is used for the current images,
154 // and this array is used to hold the alternative images.
155 // We swap between the two when toggling.
156 gfx::ImageSkia alternate_images_
[STATE_COUNT
];
158 // True if the button is currently toggled.
161 // The parent class's tooltip_text_ is displayed when not toggled, and
162 // this one is shown when toggled.
163 base::string16 toggled_tooltip_text_
;
165 DISALLOW_COPY_AND_ASSIGN(ToggleImageButton
);
170 #endif // UI_VIEWS_CONTROLS_BUTTON_IMAGE_BUTTON_H_