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/font_list.h" 15 #include "ui/gfx/range/range.h" 16 #include "ui/gfx/size.h" 17 #include "ui/views/controls/link_listener.h" 18 #include "ui/views/view.h" 19 20 namespace views { 21 22 class Link; 23 class StyledLabelListener; 24 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 // Parameters that define label style for a styled label's text range. 33 struct VIEWS_EXPORT RangeStyleInfo { 34 RangeStyleInfo(); 35 ~RangeStyleInfo(); 36 37 // Creates a range style info with default values for link. 38 static RangeStyleInfo CreateForLink(); 39 40 // The font style that will be applied to the range. Should be a bitmask of 41 // values defined in gfx::Font::FontStyle (BOLD, ITALIC, UNDERLINE). 42 int font_style; 43 44 // The text color for the range. 45 SkColor color; 46 47 // Tooltip for the range. 48 base::string16 tooltip; 49 50 // If set, the whole range will be put on a single line. 51 bool disable_line_wrapping; 52 53 // If set, the range will be created as a link. 54 bool is_link; 55 }; 56 57 // Note that any trailing whitespace in |text| will be trimmed. 58 StyledLabel(const base::string16& text, StyledLabelListener* listener); 59 virtual ~StyledLabel(); 60 61 // Sets the text to be displayed, and clears any previous styling. 62 void SetText(const base::string16& text); 63 64 // Sets the fonts used by all labels. Can be augemented by styling set by 65 // AddStyleRange and SetDefaultStyle. 66 void SetBaseFontList(const gfx::FontList& font_list); 67 68 // Marks the given range within |text_| with style defined by |style_info|. 69 // |range| must be contained in |text_|. 70 void AddStyleRange(const gfx::Range& range, const RangeStyleInfo& style_info); 71 72 // Sets the default style to use for any part of the text that isn't within 73 // a range set by AddStyleRange. 74 void SetDefaultStyle(const RangeStyleInfo& style_info); 75 76 // Get or set the distance in pixels between baselines of multi-line text. 77 // Default is 0, indicating the distance between lines should be the standard 78 // one for the label's text, font list, and platform. 79 void SetLineHeight(int height); 80 81 // Sets the color of the background on which the label is drawn. This won't 82 // be explicitly drawn, but the label will force the text color to be 83 // readable over it. 84 void SetDisplayedOnBackgroundColor(SkColor color); 85 SkColor displayed_on_background_color() const { 86 return displayed_on_background_color_; 87 } 88 89 void set_auto_color_readability_enabled(bool auto_color_readability) { 90 auto_color_readability_enabled_ = auto_color_readability; 91 } 92 93 // View implementation: 94 virtual gfx::Insets GetInsets() const OVERRIDE; 95 virtual int GetHeightForWidth(int w) const OVERRIDE; 96 virtual void Layout() OVERRIDE; 97 virtual void PreferredSizeChanged() OVERRIDE; 98 99 // LinkListener implementation: 100 virtual void LinkClicked(Link* source, int event_flags) OVERRIDE; 101 102 private: 103 struct StyleRange { 104 StyleRange(const gfx::Range& range, 105 const RangeStyleInfo& style_info) 106 : range(range), 107 style_info(style_info) { 108 } 109 ~StyleRange() {} 110 111 bool operator<(const StyleRange& other) const; 112 113 gfx::Range range; 114 RangeStyleInfo style_info; 115 }; 116 typedef std::list<StyleRange> StyleRanges; 117 118 // Calculates how to layout child views, creates them and sets their size 119 // and position. |width| is the horizontal space, in pixels, that the view 120 // has to work with. If |dry_run| is true, the view hierarchy is not touched. 121 // The return value is the necessary size. 122 gfx::Size CalculateAndDoLayout(int width, bool dry_run); 123 124 // The text to display. 125 base::string16 text_; 126 127 // Fonts used to display text. Can be augmented by RangeStyleInfo. 128 gfx::FontList font_list_; 129 130 // Line height. 131 int specified_line_height_; 132 133 // The default style to use for any part of the text that isn't within 134 // a range in |style_ranges_|. 135 RangeStyleInfo default_style_info_; 136 137 // The listener that will be informed of link clicks. 138 StyledLabelListener* listener_; 139 140 // The ranges that should be linkified, sorted by start position. 141 StyleRanges style_ranges_; 142 143 // A mapping from a view to the range it corresponds to in |text_|. Only views 144 // that correspond to ranges with is_link style set will be added to the map. 145 std::map<View*, gfx::Range> link_targets_; 146 147 // This variable saves the result of the last GetHeightForWidth call in order 148 // to avoid repeated calculation. 149 mutable gfx::Size calculated_size_; 150 151 // Background color on which the label is drawn, for auto color readability. 152 SkColor displayed_on_background_color_; 153 bool displayed_on_background_color_set_; 154 155 // Controls whether the text is automatically re-colored to be readable on the 156 // background. 157 bool auto_color_readability_enabled_; 158 159 DISALLOW_COPY_AND_ASSIGN(StyledLabel); 160 }; 161 162 } // namespace views 163 164 #endif // UI_VIEWS_CONTROLS_STYLED_LABEL_H_ 165