Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / views / controls / button / text_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_TEXT_BUTTON_H_
6 #define UI_VIEWS_CONTROLS_BUTTON_TEXT_BUTTON_H_
7
8 #include <string>
9
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string16.h"
13 #include "third_party/skia/include/core/SkColor.h"
14 #include "ui/gfx/font_list.h"
15 #include "ui/gfx/image/image_skia.h"
16 #include "ui/views/border.h"
17 #include "ui/views/controls/button/custom_button.h"
18 #include "ui/views/native_theme_delegate.h"
19 #include "ui/views/painter.h"
20
21 namespace views {
22
23 // A Border subclass for TextButtons that allows configurable insets for the
24 // button.
25 class VIEWS_EXPORT TextButtonBorder : public Border {
26  public:
27   TextButtonBorder();
28   virtual ~TextButtonBorder();
29
30   void SetInsets(const gfx::Insets& insets);
31
32   // Border:
33   virtual void Paint(const View& view, gfx::Canvas* canvas) OVERRIDE;
34   virtual gfx::Insets GetInsets() const OVERRIDE;
35   virtual gfx::Size GetMinimumSize() const OVERRIDE;
36
37  private:
38   gfx::Insets insets_;
39
40   DISALLOW_COPY_AND_ASSIGN(TextButtonBorder);
41 };
42
43
44 // A Border subclass that paints a TextButton's background layer -- basically
45 // the button frame in the hot/pushed states.
46 //
47 // Note that this type of button is not focusable by default and will not be
48 // part of the focus chain.  Call SetFocusable(true) to make it part of the
49 // focus chain.
50 class VIEWS_EXPORT TextButtonDefaultBorder : public TextButtonBorder {
51  public:
52   TextButtonDefaultBorder();
53   virtual ~TextButtonDefaultBorder();
54
55   // TextButtonDefaultBorder takes and retains ownership of these |painter|s.
56   void set_normal_painter(Painter* painter) { normal_painter_.reset(painter); }
57   void set_hot_painter(Painter* painter) { hot_painter_.reset(painter); }
58   void set_pushed_painter(Painter* painter) { pushed_painter_.reset(painter); }
59
60  private:
61   // TextButtonBorder:
62   virtual void Paint(const View& view, gfx::Canvas* canvas) OVERRIDE;
63   virtual gfx::Size GetMinimumSize() const OVERRIDE;
64
65   scoped_ptr<Painter> normal_painter_;
66   scoped_ptr<Painter> hot_painter_;
67   scoped_ptr<Painter> pushed_painter_;
68
69   int vertical_padding_;
70
71   DISALLOW_COPY_AND_ASSIGN(TextButtonDefaultBorder);
72 };
73
74
75 // A Border subclass that paints a TextButton's background layer using the
76 // platform's native theme look.  This handles normal/disabled/hot/pressed
77 // states, with possible animation between states.
78 class VIEWS_EXPORT TextButtonNativeThemeBorder : public TextButtonBorder {
79  public:
80   explicit TextButtonNativeThemeBorder(NativeThemeDelegate* delegate);
81   virtual ~TextButtonNativeThemeBorder();
82
83   // TextButtonBorder:
84   virtual void Paint(const View& view, gfx::Canvas* canvas) OVERRIDE;
85   // We don't override GetMinimumSize(), since there's no easy way to calculate
86   // the minimum size required by the various theme components.
87
88  private:
89   // The delegate the controls the appearance of this border.
90   NativeThemeDelegate* delegate_;
91
92   DISALLOW_COPY_AND_ASSIGN(TextButtonNativeThemeBorder);
93 };
94
95
96 // A base class for different types of buttons, like push buttons, radio
97 // buttons, and checkboxes, that do not depend on native components for look and
98 // feel. TextButton reserves space for the largest string passed to SetText. To
99 // reset the cached max size invoke ClearMaxTextSize.
100 class VIEWS_EXPORT TextButtonBase : public CustomButton,
101                                     public NativeThemeDelegate {
102  public:
103   // The menu button's class name.
104   static const char kViewClassName[];
105
106   virtual ~TextButtonBase();
107
108   // Call SetText once per string in your set of possible values at button
109   // creation time, so that it can contain the largest of them and avoid
110   // resizing the button when the text changes.
111   virtual void SetText(const base::string16& text);
112   const base::string16& text() const { return text_; }
113
114   enum TextAlignment {
115     ALIGN_LEFT,
116     ALIGN_CENTER,
117     ALIGN_RIGHT
118   };
119
120   void set_alignment(TextAlignment alignment) { alignment_ = alignment; }
121
122   const gfx::Animation* GetAnimation() const;
123
124   void SetIsDefault(bool is_default);
125   bool is_default() const { return is_default_; }
126
127   // Set whether the button text can wrap on multiple lines.
128   // Default is false.
129   void SetMultiLine(bool multi_line);
130
131   // Return whether the button text can wrap on multiple lines.
132   bool multi_line() const { return multi_line_; }
133
134   // TextButton remembers the maximum display size of the text passed to
135   // SetText. This method resets the cached maximum display size to the
136   // current size.
137   void ClearMaxTextSize();
138
139   void set_min_width(int min_width) { min_width_ = min_width; }
140   void set_min_height(int min_height) { min_height_ = min_height; }
141   void set_max_width(int max_width) { max_width_ = max_width; }
142   const gfx::FontList& font_list() const { return font_list_; }
143   void SetFontList(const gfx::FontList& font_list);
144
145   void SetEnabledColor(SkColor color);
146   void SetDisabledColor(SkColor color);
147   void SetHighlightColor(SkColor color);
148   void SetHoverColor(SkColor color);
149
150   // Enables a drop shadow underneath the text.
151   void SetTextShadowColors(SkColor active_color, SkColor inactive_color);
152
153   // Sets the drop shadow's offset from the text.
154   void SetTextShadowOffset(int x, int y);
155
156   // Disables shadows.
157   void ClearEmbellishing();
158
159   // Sets whether or not to show the hot and pushed states for the button icon
160   // (if present) in addition to the normal state.  Defaults to true.
161   bool show_multiple_icon_states() const { return show_multiple_icon_states_; }
162   void SetShowMultipleIconStates(bool show_multiple_icon_states);
163
164   void SetFocusPainter(scoped_ptr<Painter> focus_painter);
165   Painter* focus_painter() { return focus_painter_.get(); }
166
167   // Paint the button into the specified canvas. If |mode| is |PB_FOR_DRAG|, the
168   // function paints a drag image representation into the canvas.
169   enum PaintButtonMode { PB_NORMAL, PB_FOR_DRAG };
170   virtual void PaintButton(gfx::Canvas* canvas, PaintButtonMode mode);
171
172   // Overridden from View:
173   virtual gfx::Size GetPreferredSize() OVERRIDE;
174   virtual gfx::Size GetMinimumSize() OVERRIDE;
175   virtual int GetHeightForWidth(int w) OVERRIDE;
176   virtual void OnEnabledChanged() OVERRIDE;
177   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
178   virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
179   virtual const char* GetClassName() const OVERRIDE;
180   virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
181
182  protected:
183   TextButtonBase(ButtonListener* listener, const base::string16& text);
184
185   // Called when enabled or disabled state changes, or the colors for those
186   // states change.
187   virtual void UpdateColor();
188
189   // Updates text_size_ and max_text_size_ from the current text/font. This is
190   // invoked when the font list or text changes.
191   void UpdateTextSize();
192
193   // Calculate the size of the text size without setting any of the members.
194   void CalculateTextSize(gfx::Size* text_size, int max_width);
195
196   void set_color_enabled(SkColor color) { color_enabled_ = color; }
197   void set_color_disabled(SkColor color) { color_disabled_ = color; }
198   void set_color_hover(SkColor color) { color_hover_ = color; }
199
200   bool use_enabled_color_from_theme() const {
201     return use_enabled_color_from_theme_;
202   }
203
204   bool use_disabled_color_from_theme() const {
205     return use_disabled_color_from_theme_;
206   }
207
208   bool use_hover_color_from_theme() const {
209     return use_hover_color_from_theme_;
210   }
211
212   // Overridden from NativeThemeDelegate:
213   virtual gfx::Rect GetThemePaintRect() const OVERRIDE;
214   virtual ui::NativeTheme::State GetThemeState(
215       ui::NativeTheme::ExtraParams* params) const OVERRIDE;
216   virtual const gfx::Animation* GetThemeAnimation() const OVERRIDE;
217   virtual ui::NativeTheme::State GetBackgroundThemeState(
218       ui::NativeTheme::ExtraParams* params) const OVERRIDE;
219   virtual ui::NativeTheme::State GetForegroundThemeState(
220       ui::NativeTheme::ExtraParams* params) const OVERRIDE;
221
222   // Overridden from View:
223   virtual void OnFocus() OVERRIDE;
224   virtual void OnBlur() OVERRIDE;
225
226   virtual void GetExtraParams(ui::NativeTheme::ExtraParams* params) const;
227
228   virtual gfx::Rect GetTextBounds() const;
229
230   int ComputeCanvasStringFlags() const;
231
232   // Calculate the bounds of the content of this button, including any extra
233   // width needed on top of the text width.
234   gfx::Rect GetContentBounds(int extra_width) const;
235
236   // The text string that is displayed in the button.
237   base::string16 text_;
238
239   // The size of the text string.
240   gfx::Size text_size_;
241
242   // Track the size of the largest text string seen so far, so that
243   // changing text_ will not resize the button boundary.
244   gfx::Size max_text_size_;
245
246   // The alignment of the text string within the button.
247   TextAlignment alignment_;
248
249   // The font list used to paint the text.
250   gfx::FontList font_list_;
251
252   // Flag indicating if a shadow should be drawn behind the text.
253   bool has_text_shadow_;
254   // Optional shadow text colors for active and inactive widget states.
255   SkColor active_text_shadow_color_;
256   SkColor inactive_text_shadow_color_;
257   // Space between the text and its shadow. Defaults to (1,1).
258   gfx::Point text_shadow_offset_;
259
260   // The dimensions of the button will be at least these values.
261   int min_width_;
262   int min_height_;
263
264   // The width of the button will never be larger than this value. A value <= 0
265   // indicates the width is not constrained.
266   int max_width_;
267
268   // Whether or not to show the hot and pushed icon states.
269   bool show_multiple_icon_states_;
270
271   // Whether or not the button appears and behaves as the default button in its
272   // current context.
273   bool is_default_;
274
275   // Whether the text button should handle its text string as multi-line.
276   bool multi_line_;
277
278  private:
279   // Text color.
280   SkColor color_;
281
282   // State colors.
283   SkColor color_enabled_;
284   SkColor color_disabled_;
285   SkColor color_highlight_;
286   SkColor color_hover_;
287
288   // True if the specified color should be used from the theme.
289   bool use_enabled_color_from_theme_;
290   bool use_disabled_color_from_theme_;
291   bool use_highlight_color_from_theme_;
292   bool use_hover_color_from_theme_;
293
294   scoped_ptr<Painter> focus_painter_;
295
296   DISALLOW_COPY_AND_ASSIGN(TextButtonBase);
297 };
298
299
300 // A button which displays text and/or and icon that can be changed in response
301 // to actions. TextButton reserves space for the largest string passed to
302 // SetText. To reset the cached max size invoke ClearMaxTextSize.
303 class VIEWS_EXPORT TextButton : public TextButtonBase {
304  public:
305   // The button's class name.
306   static const char kViewClassName[];
307
308   TextButton(ButtonListener* listener, const base::string16& text);
309   virtual ~TextButton();
310
311   void set_icon_text_spacing(int icon_text_spacing) {
312     icon_text_spacing_ = icon_text_spacing;
313   }
314
315   // Sets the icon.
316   virtual void SetIcon(const gfx::ImageSkia& icon);
317   virtual void SetHoverIcon(const gfx::ImageSkia& icon);
318   virtual void SetPushedIcon(const gfx::ImageSkia& icon);
319
320   bool HasIcon() const { return !icon_.isNull(); }
321
322   // Meanings are reversed for right-to-left layouts.
323   enum IconPlacement {
324     ICON_ON_LEFT,
325     ICON_ON_RIGHT,
326     ICON_CENTERED  // Centered is valid only when text is empty.
327   };
328
329   IconPlacement icon_placement() { return icon_placement_; }
330   void set_icon_placement(IconPlacement icon_placement) {
331     // ICON_CENTERED works only when |text_| is empty.
332     DCHECK((icon_placement != ICON_CENTERED) || text_.empty());
333     icon_placement_ = icon_placement;
334   }
335
336   void set_ignore_minimum_size(bool ignore_minimum_size);
337
338   void set_full_justification(bool full_justification);
339
340   // Overridden from View:
341   virtual gfx::Size GetPreferredSize() OVERRIDE;
342   virtual const char* GetClassName() const OVERRIDE;
343
344   // Overridden from TextButtonBase:
345   virtual void PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) OVERRIDE;
346
347  protected:
348   gfx::ImageSkia icon() const { return icon_; }
349
350   virtual const gfx::ImageSkia& GetImageToPaint() const;
351
352   // Overridden from NativeThemeDelegate:
353   virtual ui::NativeTheme::Part GetThemePart() const OVERRIDE;
354
355   // Overridden from TextButtonBase:
356   virtual void GetExtraParams(
357       ui::NativeTheme::ExtraParams* params) const OVERRIDE;
358   virtual gfx::Rect GetTextBounds() const OVERRIDE;
359
360  private:
361   // The position of the icon.
362   IconPlacement icon_placement_;
363
364   // An icon displayed with the text.
365   gfx::ImageSkia icon_;
366
367   // An optional different version of the icon for hover state.
368   gfx::ImageSkia icon_hover_;
369   bool has_hover_icon_;
370
371   // An optional different version of the icon for pushed state.
372   gfx::ImageSkia icon_pushed_;
373   bool has_pushed_icon_;
374
375   // Space between icon and text.
376   int icon_text_spacing_;
377
378   // True if the button should ignore the minimum size for the platform. Default
379   // is true. Set to false to prevent narrower buttons.
380   bool ignore_minimum_size_;
381
382   // True if the icon and the text are aligned along both the left and right
383   // margins of the button.
384   bool full_justification_;
385
386   DISALLOW_COPY_AND_ASSIGN(TextButton);
387 };
388
389 }  // namespace views
390
391 #endif  // UI_VIEWS_CONTROLS_BUTTON_TEXT_BUTTON_H_