Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / views / controls / button / label_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_LABEL_BUTTON_H_
6 #define UI_VIEWS_CONTROLS_BUTTON_LABEL_BUTTON_H_
7
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "third_party/skia/include/core/SkColor.h"
11 #include "ui/gfx/image/image_skia.h"
12 #include "ui/views/controls/button/custom_button.h"
13 #include "ui/views/controls/image_view.h"
14 #include "ui/views/controls/label.h"
15 #include "ui/views/native_theme_delegate.h"
16
17 namespace views {
18
19 class LabelButtonBorder;
20 class Painter;
21
22 // LabelButton is an alternative to TextButton, it's not focusable by default.
23 class VIEWS_EXPORT LabelButton : public CustomButton,
24                                  public NativeThemeDelegate {
25  public:
26   // The length of the hover fade animation.
27   static const int kHoverAnimationDurationMs;
28
29   static const char kViewClassName[];
30
31   LabelButton(ButtonListener* listener, const base::string16& text);
32   virtual ~LabelButton();
33
34   // Get or set the image shown for the specified button state.
35   // GetImage returns the image for STATE_NORMAL if the state's image is empty.
36   virtual const gfx::ImageSkia& GetImage(ButtonState for_state);
37   void SetImage(ButtonState for_state, const gfx::ImageSkia& image);
38
39   // Get or set the text shown on the button.
40   const base::string16& GetText() const;
41   void SetText(const base::string16& text);
42
43   // Set the text color shown for the specified button state.
44   void SetTextColor(ButtonState for_state, SkColor color);
45
46   // Get or set the text's multi-line property to break on '\n', etc.
47   bool GetTextMultiLine() const;
48   void SetTextMultiLine(bool text_multi_line);
49
50   // Get or set the font list used by this button.
51   const gfx::FontList& GetFontList() const;
52   void SetFontList(const gfx::FontList& font_list);
53
54   // Set the elide behavior of this button.
55   void SetElideBehavior(Label::ElideBehavior elide_behavior);
56
57   // Get or set the horizontal alignment used for the button; reversed in RTL.
58   // The optional image will lead the text, unless the button is right-aligned.
59   gfx::HorizontalAlignment GetHorizontalAlignment() const;
60   void SetHorizontalAlignment(gfx::HorizontalAlignment alignment);
61
62   // Call set_min_size(gfx::Size()) to clear the monotonically increasing size.
63   void set_min_size(const gfx::Size& min_size) { min_size_ = min_size; }
64   void set_min_width(const int min_width) { min_size_.set_width(min_width); }
65   void set_max_size(const gfx::Size& max_size) { max_size_ = max_size; }
66
67   // Get or set the option to handle the return key; false by default.
68   bool is_default() const { return is_default_; }
69   void SetIsDefault(bool is_default);
70
71   // Get or set the button's overall style; the default is |STYLE_TEXTBUTTON|.
72   ButtonStyle style() const { return style_; }
73   void SetStyle(ButtonStyle style);
74
75   void SetFocusPainter(scoped_ptr<Painter> focus_painter);
76
77   // View:
78   virtual gfx::Size GetPreferredSize() OVERRIDE;
79   virtual void Layout() OVERRIDE;
80   virtual const char* GetClassName() const OVERRIDE;
81
82  protected:
83   ImageView* image() const { return image_; }
84   Label* label() const { return label_; }
85
86   // View:
87   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
88   virtual void OnFocus() OVERRIDE;
89   virtual void OnBlur() OVERRIDE;
90
91   // Fill |params| with information about the button.
92   virtual void GetExtraParams(ui::NativeTheme::ExtraParams* params) const;
93
94   // Resets colors from the NativeTheme, explicitly set colors are unchanged.
95   virtual void ResetColorsFromNativeTheme();
96
97   // Updates the image view to contain the appropriate button state image.
98   void UpdateImage();
99
100   // Updates our border with a specific Border instance which has different
101   // insets, etc. This may wrap the border in an object which will draw a
102   // native style border.
103   void UpdateThemedBorder(scoped_ptr<Border> border);
104
105   // NativeThemeDelegate:
106   virtual gfx::Rect GetThemePaintRect() const OVERRIDE;
107
108  private:
109   FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Init);
110   FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Label);
111   FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Image);
112   FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, LabelAndImage);
113   FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, FontList);
114
115   // CustomButton:
116   virtual void StateChanged() OVERRIDE;
117
118   // View:
119   virtual void ChildPreferredSizeChanged(View* child) OVERRIDE;
120   virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
121
122   // NativeThemeDelegate:
123   virtual ui::NativeTheme::Part GetThemePart() const OVERRIDE;
124   virtual ui::NativeTheme::State GetThemeState(
125       ui::NativeTheme::ExtraParams* params) const OVERRIDE;
126   virtual const gfx::Animation* GetThemeAnimation() const OVERRIDE;
127   virtual ui::NativeTheme::State GetBackgroundThemeState(
128       ui::NativeTheme::ExtraParams* params) const OVERRIDE;
129   virtual ui::NativeTheme::State GetForegroundThemeState(
130       ui::NativeTheme::ExtraParams* params) const OVERRIDE;
131
132   // The image and label shown in the button.
133   ImageView* image_;
134   Label* label_;
135
136   // The cached font lists in the normal and bold style.
137   gfx::FontList cached_normal_font_list_;
138   gfx::FontList cached_bold_font_list_;
139
140   // The images and colors for each button state.
141   gfx::ImageSkia button_state_images_[STATE_COUNT];
142   SkColor button_state_colors_[STATE_COUNT];
143
144   // Used to track whether SetTextColor() has been invoked.
145   bool explicitly_set_colors_[STATE_COUNT];
146
147   // |min_size_| increases monotonically with the preferred size.
148   gfx::Size min_size_;
149   // |max_size_| may be set to clamp the preferred size.
150   gfx::Size max_size_;
151
152   // Flag indicating default handling of the return key via an accelerator.
153   // Whether or not the button appears or behaves as the default button in its
154   // current context;
155   bool is_default_;
156
157   // The button's overall style.
158   ButtonStyle style_;
159
160   scoped_ptr<Painter> focus_painter_;
161
162   DISALLOW_COPY_AND_ASSIGN(LabelButton);
163 };
164
165 }  // namespace views
166
167 #endif  // UI_VIEWS_CONTROLS_BUTTON_LABEL_BUTTON_H_