- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / controls / button / image_button.h
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/base/layout.h"
11 #include "ui/gfx/image/image_skia.h"
12 #include "ui/views/controls/button/custom_button.h"
13
14 namespace views {
15
16 // An image button.
17
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
20 // focus chain.
21
22 class VIEWS_EXPORT ImageButton : public CustomButton {
23  public:
24   static const char kViewClassName[];
25
26   enum HorizontalAlignment {
27     ALIGN_LEFT = 0,
28     ALIGN_CENTER,
29     ALIGN_RIGHT
30   };
31
32   enum VerticalAlignment {
33     ALIGN_TOP = 0,
34     ALIGN_MIDDLE,
35     ALIGN_BOTTOM
36   };
37
38   explicit ImageButton(ButtonListener* listener);
39   virtual ~ImageButton();
40
41   // Returns the image for a given |state|.
42   virtual const gfx::ImageSkia& GetImage(ButtonState state) const;
43
44   // Set the image the button should use for the provided state.
45   virtual void SetImage(ButtonState state, const gfx::ImageSkia* image);
46
47   // Set the background details.
48   void SetBackground(SkColor color,
49                      const gfx::ImageSkia* image,
50                      const gfx::ImageSkia* mask);
51
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);
55
56   // Sets how the image is laid out within the button's bounds.
57   void SetImageAlignment(HorizontalAlignment h_align,
58                          VerticalAlignment v_align);
59
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;
64
65   // Sets preferred size, so it could be correctly positioned in layout even if
66   // it is NULL.
67   void SetPreferredSize(const gfx::Size& preferred_size) {
68     preferred_size_ = preferred_size;
69   }
70
71   // Whether we should draw our images resources horizontally flipped.
72   void SetDrawImageMirrored(bool mirrored) {
73     draw_image_mirrored_ = mirrored;
74   }
75
76  protected:
77   // Returns the image to paint. This is invoked from paint and returns a value
78   // from images.
79   virtual gfx::ImageSkia GetImageToPaint();
80
81   // Updates button background for |scale_factor|.
82   void UpdateButtonBackground(ui::ScaleFactor scale_factor);
83
84   // The images used to render the different states of this button.
85   gfx::ImageSkia images_[STATE_COUNT];
86
87   gfx::ImageSkia background_image_;
88
89   // Image to draw on top of normal / hot / pushed image.  Usually empty.
90   gfx::ImageSkia overlay_image_;
91
92  private:
93   FRIEND_TEST_ALL_PREFIXES(ImageButtonTest, Basics);
94   FRIEND_TEST_ALL_PREFIXES(ImageButtonTest, ImagePositionWithBorder);
95
96   // Returns the correct position of the image for painting.
97   gfx::Point ComputeImagePaintPosition(const gfx::ImageSkia& image);
98
99   // Image alignment.
100   HorizontalAlignment h_alignment_;
101   VerticalAlignment v_alignment_;
102   gfx::Size preferred_size_;
103
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
107   // resources.
108   bool draw_image_mirrored_;
109
110   DISALLOW_COPY_AND_ASSIGN(ImageButton);
111 };
112
113 ////////////////////////////////////////////////////////////////////////////////
114 //
115 // ToggleImageButton
116 //
117 // A toggle-able ImageButton.  It swaps out its graphics when toggled.
118 //
119 ////////////////////////////////////////////////////////////////////////////////
120 class VIEWS_EXPORT ToggleImageButton : public ImageButton {
121  public:
122   explicit ToggleImageButton(ButtonListener* listener);
123   virtual ~ToggleImageButton();
124
125   // Change the toggled state.
126   void SetToggled(bool toggled);
127
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);
132
133   // Set the tooltip text displayed when the button is toggled.
134   void SetToggledTooltipText(const string16& tooltip);
135
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;
140
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;
145
146  private:
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];
151
152   // True if the button is currently toggled.
153   bool toggled_;
154
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_;
158
159   DISALLOW_COPY_AND_ASSIGN(ToggleImageButton);
160 };
161
162 }  // namespace views
163
164 #endif  // UI_VIEWS_CONTROLS_BUTTON_IMAGE_BUTTON_H_