Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / views / controls / styled_label.h
blob57638c50f82b08602e72efbdf047eec68b97d24b
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.
5 #ifndef UI_VIEWS_CONTROLS_STYLED_LABEL_H_
6 #define UI_VIEWS_CONTROLS_STYLED_LABEL_H_
8 #include <list>
9 #include <map>
11 #include "base/basictypes.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/geometry/size.h"
16 #include "ui/gfx/range/range.h"
17 #include "ui/views/controls/link_listener.h"
18 #include "ui/views/view.h"
20 namespace views {
22 class Link;
23 class StyledLabelListener;
25 // A class which can apply mixed styles to a block of text. Currently, text is
26 // always multiline. Trailing whitespace in the styled label text is not
27 // supported and will be trimmed on StyledLabel construction. Leading
28 // whitespace is respected, provided not only whitespace fits in the first line.
29 // In this case, leading whitespace is ignored.
30 class VIEWS_EXPORT StyledLabel : public View, public LinkListener {
31 public:
32 // Internal class name.
33 static const char kViewClassName[];
35 // Parameters that define label style for a styled label's text range.
36 struct VIEWS_EXPORT RangeStyleInfo {
37 RangeStyleInfo();
38 ~RangeStyleInfo();
40 // Creates a range style info with default values for link.
41 static RangeStyleInfo CreateForLink();
43 // The font style that will be applied to the range. Should be a bitmask of
44 // values defined in gfx::Font::FontStyle (BOLD, ITALIC, UNDERLINE).
45 int font_style;
47 // The text color for the range.
48 SkColor color;
50 // Tooltip for the range.
51 base::string16 tooltip;
53 // If set, the whole range will be put on a single line.
54 bool disable_line_wrapping;
56 // If set, the range will be created as a link.
57 bool is_link;
60 // Note that any trailing whitespace in |text| will be trimmed.
61 StyledLabel(const base::string16& text, StyledLabelListener* listener);
62 ~StyledLabel() override;
64 // Sets the text to be displayed, and clears any previous styling.
65 void SetText(const base::string16& text);
67 // Sets the fonts used by all labels. Can be augemented by styling set by
68 // AddStyleRange and SetDefaultStyle.
69 void SetBaseFontList(const gfx::FontList& font_list);
71 // Marks the given range within |text_| with style defined by |style_info|.
72 // |range| must be contained in |text_|.
73 void AddStyleRange(const gfx::Range& range, const RangeStyleInfo& style_info);
75 // Sets the default style to use for any part of the text that isn't within
76 // a range set by AddStyleRange.
77 void SetDefaultStyle(const RangeStyleInfo& style_info);
79 // Get or set the distance in pixels between baselines of multi-line text.
80 // Default is 0, indicating the distance between lines should be the standard
81 // one for the label's text, font list, and platform.
82 void SetLineHeight(int height);
84 // Sets the color of the background on which the label is drawn. This won't
85 // be explicitly drawn, but the label will force the text color to be
86 // readable over it.
87 void SetDisplayedOnBackgroundColor(SkColor color);
88 SkColor displayed_on_background_color() const {
89 return displayed_on_background_color_;
92 void set_auto_color_readability_enabled(bool auto_color_readability) {
93 auto_color_readability_enabled_ = auto_color_readability;
96 // Resizes the label so its width is set to the width of the longest line and
97 // its height deduced accordingly.
98 // This is only intended for multi-line labels and is useful when the label's
99 // text contains several lines separated with \n.
100 // |max_width| is the maximum width that will be used (longer lines will be
101 // wrapped). If 0, no maximum width is enforced.
102 void SizeToFit(int max_width);
104 // View:
105 const char* GetClassName() const override;
106 gfx::Insets GetInsets() const override;
107 gfx::Size GetPreferredSize() const override;
108 int GetHeightForWidth(int w) const override;
109 void Layout() override;
110 void PreferredSizeChanged() override;
112 // LinkListener implementation:
113 void LinkClicked(Link* source, int event_flags) override;
115 private:
116 struct StyleRange {
117 StyleRange(const gfx::Range& range,
118 const RangeStyleInfo& style_info)
119 : range(range),
120 style_info(style_info) {
122 ~StyleRange() {}
124 bool operator<(const StyleRange& other) const;
126 gfx::Range range;
127 RangeStyleInfo style_info;
129 typedef std::list<StyleRange> StyleRanges;
131 // Calculates how to layout child views, creates them and sets their size and
132 // position. |width| is the horizontal space, in pixels, that the view has to
133 // work with. If |dry_run| is true, the view hierarchy is not touched. Caches
134 // the results in |calculated_size_|, |width_at_last_layout_|, and
135 // |width_at_last_size_calculation_|. Returns the needed size.
136 gfx::Size CalculateAndDoLayout(int width, bool dry_run);
138 // The text to display.
139 base::string16 text_;
141 // Fonts used to display text. Can be augmented by RangeStyleInfo.
142 gfx::FontList font_list_;
144 // Line height.
145 int specified_line_height_;
147 // The default style to use for any part of the text that isn't within
148 // a range in |style_ranges_|.
149 RangeStyleInfo default_style_info_;
151 // The listener that will be informed of link clicks.
152 StyledLabelListener* listener_;
154 // The ranges that should be linkified, sorted by start position.
155 StyleRanges style_ranges_;
157 // A mapping from a view to the range it corresponds to in |text_|. Only views
158 // that correspond to ranges with is_link style set will be added to the map.
159 std::map<View*, gfx::Range> link_targets_;
161 // This variable saves the result of the last GetHeightForWidth call in order
162 // to avoid repeated calculation.
163 mutable gfx::Size calculated_size_;
164 mutable int width_at_last_size_calculation_;
165 int width_at_last_layout_;
167 // Background color on which the label is drawn, for auto color readability.
168 SkColor displayed_on_background_color_;
169 bool displayed_on_background_color_set_;
171 // Controls whether the text is automatically re-colored to be readable on the
172 // background.
173 bool auto_color_readability_enabled_;
175 DISALLOW_COPY_AND_ASSIGN(StyledLabel);
178 } // namespace views
180 #endif // UI_VIEWS_CONTROLS_STYLED_LABEL_H_