f600159ee3466c09f7e5039720ea5f4cc64e3c76
[platform/framework/web/crosswalk.git] / src / ui / views / controls / styled_label.h
1 // Copyright 2013 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_STYLED_LABEL_H_
6 #define UI_VIEWS_CONTROLS_STYLED_LABEL_H_
7
8 #include <list>
9 #include <map>
10
11 #include "base/basictypes.h"
12 #include "base/strings/string16.h"
13 #include "third_party/skia/include/core/SkColor.h"
14 #include "ui/gfx/range/range.h"
15 #include "ui/gfx/size.h"
16 #include "ui/views/controls/link_listener.h"
17 #include "ui/views/view.h"
18
19 namespace views {
20
21 class Link;
22 class StyledLabelListener;
23
24 // A class which can apply mixed styles to a block of text. Currently, text is
25 // always multiline. Trailing whitespace in the styled label text is not
26 // supported and will be trimmed on StyledLabel construction. Leading
27 // whitespace is respected, provided not only whitespace fits in the first line.
28 // In this case, leading whitespace is ignored.
29 class VIEWS_EXPORT StyledLabel : public View, public LinkListener {
30  public:
31   // Parameters that define label style for a styled label's text range.
32   struct VIEWS_EXPORT RangeStyleInfo {
33     RangeStyleInfo();
34     ~RangeStyleInfo();
35
36     // Creates a range style info with default values for link.
37     static RangeStyleInfo CreateForLink();
38
39     // The font style that will be applied to the range. Should be a bitmask of
40     // values defined in gfx::Font::FontStyle (BOLD, ITALIC, UNDERLINE).
41     int font_style;
42
43     // The text color for the range.
44     SkColor color;
45
46     // Tooltip for the range.
47     base::string16 tooltip;
48
49     // If set, the whole range will be put on a single line.
50     bool disable_line_wrapping;
51
52     // If set, the range will be created as a link.
53     bool is_link;
54   };
55
56   // Note that any trailing whitespace in |text| will be trimmed.
57   StyledLabel(const base::string16& text, StyledLabelListener* listener);
58   virtual ~StyledLabel();
59
60   // Sets the text to be displayed, and clears any previous styling.
61   void SetText(const base::string16& text);
62
63   // Marks the given range within |text_| with style defined by |style_info|.
64   // |range| must be contained in |text_|.
65   void AddStyleRange(const gfx::Range& range, const RangeStyleInfo& style_info);
66
67   // Sets the default style to use for any part of the text that isn't within
68   // a range set by AddStyleRange.
69   void SetDefaultStyle(const RangeStyleInfo& style_info);
70
71   // Sets the color of the background on which the label is drawn. This won't
72   // be explicitly drawn, but the label will force the text color to be
73   // readable over it.
74   void SetDisplayedOnBackgroundColor(SkColor color);
75   SkColor displayed_on_background_color() const {
76     return displayed_on_background_color_;
77   }
78
79   void set_auto_color_readability_enabled(bool auto_color_readability) {
80     auto_color_readability_enabled_ = auto_color_readability;
81   }
82
83   // View implementation:
84   virtual gfx::Insets GetInsets() const OVERRIDE;
85   virtual int GetHeightForWidth(int w) OVERRIDE;
86   virtual void Layout() OVERRIDE;
87   virtual void PreferredSizeChanged() OVERRIDE;
88
89   // LinkListener implementation:
90   virtual void LinkClicked(Link* source, int event_flags) OVERRIDE;
91
92  private:
93   struct StyleRange {
94     StyleRange(const gfx::Range& range,
95                const RangeStyleInfo& style_info)
96         : range(range),
97           style_info(style_info) {
98     }
99     ~StyleRange() {}
100
101     bool operator<(const StyleRange& other) const;
102
103     gfx::Range range;
104     RangeStyleInfo style_info;
105   };
106   typedef std::list<StyleRange> StyleRanges;
107
108   // Calculates how to layout child views, creates them and sets their size
109   // and position. |width| is the horizontal space, in pixels, that the view
110   // has to work with. If |dry_run| is true, the view hierarchy is not touched.
111   // The return value is the necessary size.
112   gfx::Size CalculateAndDoLayout(int width, bool dry_run);
113
114   // The text to display.
115   base::string16 text_;
116
117   // The default style to use for any part of the text that isn't within
118   // a range in |style_ranges_|.
119   RangeStyleInfo default_style_info_;
120
121   // The listener that will be informed of link clicks.
122   StyledLabelListener* listener_;
123
124   // The ranges that should be linkified, sorted by start position.
125   StyleRanges style_ranges_;
126
127   // A mapping from a view to the range it corresponds to in |text_|. Only views
128   // that correspond to ranges with is_link style set will be added to the map.
129   std::map<View*, gfx::Range> link_targets_;
130
131   // This variable saves the result of the last GetHeightForWidth call in order
132   // to avoid repeated calculation.
133   gfx::Size calculated_size_;
134
135   // Background color on which the label is drawn, for auto color readability.
136   SkColor displayed_on_background_color_;
137   bool displayed_on_background_color_set_;
138
139   // Controls whether the text is automatically re-colored to be readable on the
140   // background.
141   bool auto_color_readability_enabled_;
142
143   DISALLOW_COPY_AND_ASSIGN(StyledLabel);
144 };
145
146 }  // namespace views
147
148 #endif  // UI_VIEWS_CONTROLS_STYLED_LABEL_H_