OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_VIEWS_CONTROLS_STYLED_LABEL_H_ | 5 #ifndef UI_VIEWS_CONTROLS_STYLED_LABEL_H_ |
6 #define UI_VIEWS_CONTROLS_STYLED_LABEL_H_ | 6 #define UI_VIEWS_CONTROLS_STYLED_LABEL_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 | 10 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 // readable over it. | 86 // readable over it. |
87 void SetDisplayedOnBackgroundColor(SkColor color); | 87 void SetDisplayedOnBackgroundColor(SkColor color); |
88 SkColor displayed_on_background_color() const { | 88 SkColor displayed_on_background_color() const { |
89 return displayed_on_background_color_; | 89 return displayed_on_background_color_; |
90 } | 90 } |
91 | 91 |
92 void set_auto_color_readability_enabled(bool auto_color_readability) { | 92 void set_auto_color_readability_enabled(bool auto_color_readability) { |
93 auto_color_readability_enabled_ = auto_color_readability; | 93 auto_color_readability_enabled_ = auto_color_readability; |
94 } | 94 } |
95 | 95 |
96 // views::View: | 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); |
| 103 |
| 104 // View: |
97 const char* GetClassName() const override; | 105 const char* GetClassName() const override; |
98 gfx::Insets GetInsets() const override; | 106 gfx::Insets GetInsets() const override; |
| 107 gfx::Size GetPreferredSize() const override; |
99 int GetHeightForWidth(int w) const override; | 108 int GetHeightForWidth(int w) const override; |
100 void Layout() override; | 109 void Layout() override; |
101 void PreferredSizeChanged() override; | 110 void PreferredSizeChanged() override; |
102 | 111 |
103 // LinkListener implementation: | 112 // LinkListener implementation: |
104 void LinkClicked(Link* source, int event_flags) override; | 113 void LinkClicked(Link* source, int event_flags) override; |
105 | 114 |
106 private: | 115 private: |
107 struct StyleRange { | 116 struct StyleRange { |
108 StyleRange(const gfx::Range& range, | 117 StyleRange(const gfx::Range& range, |
109 const RangeStyleInfo& style_info) | 118 const RangeStyleInfo& style_info) |
110 : range(range), | 119 : range(range), |
111 style_info(style_info) { | 120 style_info(style_info) { |
112 } | 121 } |
113 ~StyleRange() {} | 122 ~StyleRange() {} |
114 | 123 |
115 bool operator<(const StyleRange& other) const; | 124 bool operator<(const StyleRange& other) const; |
116 | 125 |
117 gfx::Range range; | 126 gfx::Range range; |
118 RangeStyleInfo style_info; | 127 RangeStyleInfo style_info; |
119 }; | 128 }; |
120 typedef std::list<StyleRange> StyleRanges; | 129 typedef std::list<StyleRange> StyleRanges; |
121 | 130 |
122 // Calculates how to layout child views, creates them and sets their size | 131 // Calculates how to layout child views, creates them and sets their size and |
123 // and position. |width| is the horizontal space, in pixels, that the view | 132 // position. |width| is the horizontal space, in pixels, that the view has to |
124 // has to work with. If |dry_run| is true, the view hierarchy is not touched. | 133 // work with. If |dry_run| is true, the view hierarchy is not touched. Caches |
125 // The return value is the necessary size. | 134 // the results in |calculated_size_|, |width_at_last_layout_|, and |
| 135 // |width_at_last_size_calculation_|. Returns the needed size. |
126 gfx::Size CalculateAndDoLayout(int width, bool dry_run); | 136 gfx::Size CalculateAndDoLayout(int width, bool dry_run); |
127 | 137 |
128 // The text to display. | 138 // The text to display. |
129 base::string16 text_; | 139 base::string16 text_; |
130 | 140 |
131 // Fonts used to display text. Can be augmented by RangeStyleInfo. | 141 // Fonts used to display text. Can be augmented by RangeStyleInfo. |
132 gfx::FontList font_list_; | 142 gfx::FontList font_list_; |
133 | 143 |
134 // Line height. | 144 // Line height. |
135 int specified_line_height_; | 145 int specified_line_height_; |
136 | 146 |
137 // The default style to use for any part of the text that isn't within | 147 // The default style to use for any part of the text that isn't within |
138 // a range in |style_ranges_|. | 148 // a range in |style_ranges_|. |
139 RangeStyleInfo default_style_info_; | 149 RangeStyleInfo default_style_info_; |
140 | 150 |
141 // The listener that will be informed of link clicks. | 151 // The listener that will be informed of link clicks. |
142 StyledLabelListener* listener_; | 152 StyledLabelListener* listener_; |
143 | 153 |
144 // The ranges that should be linkified, sorted by start position. | 154 // The ranges that should be linkified, sorted by start position. |
145 StyleRanges style_ranges_; | 155 StyleRanges style_ranges_; |
146 | 156 |
147 // A mapping from a view to the range it corresponds to in |text_|. Only views | 157 // A mapping from a view to the range it corresponds to in |text_|. Only views |
148 // that correspond to ranges with is_link style set will be added to the map. | 158 // that correspond to ranges with is_link style set will be added to the map. |
149 std::map<View*, gfx::Range> link_targets_; | 159 std::map<View*, gfx::Range> link_targets_; |
150 | 160 |
151 // This variable saves the result of the last GetHeightForWidth call in order | 161 // This variable saves the result of the last GetHeightForWidth call in order |
152 // to avoid repeated calculation. | 162 // to avoid repeated calculation. |
153 mutable gfx::Size calculated_size_; | 163 mutable gfx::Size calculated_size_; |
| 164 mutable int width_at_last_size_calculation_; |
154 int width_at_last_layout_; | 165 int width_at_last_layout_; |
155 | 166 |
156 // Background color on which the label is drawn, for auto color readability. | 167 // Background color on which the label is drawn, for auto color readability. |
157 SkColor displayed_on_background_color_; | 168 SkColor displayed_on_background_color_; |
158 bool displayed_on_background_color_set_; | 169 bool displayed_on_background_color_set_; |
159 | 170 |
160 // Controls whether the text is automatically re-colored to be readable on the | 171 // Controls whether the text is automatically re-colored to be readable on the |
161 // background. | 172 // background. |
162 bool auto_color_readability_enabled_; | 173 bool auto_color_readability_enabled_; |
163 | 174 |
164 DISALLOW_COPY_AND_ASSIGN(StyledLabel); | 175 DISALLOW_COPY_AND_ASSIGN(StyledLabel); |
165 }; | 176 }; |
166 | 177 |
167 } // namespace views | 178 } // namespace views |
168 | 179 |
169 #endif // UI_VIEWS_CONTROLS_STYLED_LABEL_H_ | 180 #endif // UI_VIEWS_CONTROLS_STYLED_LABEL_H_ |
OLD | NEW |