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 #include "ui/views/controls/styled_label.h" | 5 #include "ui/views/controls/styled_label.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "ui/base/text/text_elider.h" | 10 #include "ui/base/text/text_elider.h" |
11 #include "ui/views/controls/label.h" | 11 #include "ui/views/controls/label.h" |
12 #include "ui/views/controls/link.h" | 12 #include "ui/views/controls/link.h" |
13 #include "ui/views/controls/styled_label_listener.h" | 13 #include "ui/views/controls/styled_label_listener.h" |
14 | 14 |
15 namespace views { | 15 namespace views { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // Calculates the height of a line of text. Currently returns the height of | 19 // Calculates the height of a line of text. Currently returns the height of |
20 // a label. | 20 // a label. |
21 int CalculateLineHeight() { | 21 int CalculateLineHeight() { |
22 Label label; | 22 Label label; |
23 return label.GetPreferredSize().height(); | 23 return label.GetPreferredSize().height(); |
24 } | 24 } |
25 | 25 |
| 26 scoped_ptr<View> CreateLabelRange(const string16& text, |
| 27 const StyledLabel::RangeStyleInfo& style_info, |
| 28 views::LinkListener* link_listener) { |
| 29 scoped_ptr<Label> result; |
| 30 |
| 31 if (style_info.linkify) { |
| 32 Link* link = new Link(text); |
| 33 link->set_listener(link_listener); |
| 34 link->SetUnderline(style_info.font_style & gfx::Font::UNDERLINE); |
| 35 result.reset(link); |
| 36 } else { |
| 37 Label* label = new Label(text); |
| 38 // Give the label a focus border so that its preferred size matches |
| 39 // links' preferred sizes |
| 40 label->SetHasFocusBorder(true); |
| 41 |
| 42 result.reset(label); |
| 43 } |
| 44 |
| 45 if (!style_info.tooltip.empty()) |
| 46 result->SetTooltipText(style_info.tooltip); |
| 47 if (style_info.font_style != gfx::Font::NORMAL) |
| 48 result->SetFont(result->font().DeriveFont(0, style_info.font_style)); |
| 49 |
| 50 return scoped_ptr<View>(result.release()); |
| 51 } |
| 52 |
26 } // namespace | 53 } // namespace |
27 | 54 |
28 bool StyledLabel::LinkRange::operator<( | 55 |
29 const StyledLabel::LinkRange& other) const { | 56 StyledLabel::RangeStyleInfo::RangeStyleInfo() |
| 57 : font_style(gfx::Font::NORMAL), |
| 58 disable_line_wrapping(false), |
| 59 linkify(false) { |
| 60 } |
| 61 |
| 62 StyledLabel::RangeStyleInfo::~RangeStyleInfo() {} |
| 63 |
| 64 bool StyledLabel::StyleRange::operator<( |
| 65 const StyledLabel::StyleRange& other) const { |
30 // Intentionally reversed so the priority queue is sorted by smallest first. | 66 // Intentionally reversed so the priority queue is sorted by smallest first. |
31 return range.start() > other.range.start(); | 67 return range.start() > other.range.start(); |
32 } | 68 } |
33 | 69 |
34 StyledLabel::StyledLabel(const string16& text, StyledLabelListener* listener) | 70 StyledLabel::StyledLabel(const string16& text, StyledLabelListener* listener) |
35 : text_(text), | 71 : text_(text), |
36 listener_(listener) {} | 72 listener_(listener) {} |
37 | 73 |
38 StyledLabel::~StyledLabel() {} | 74 StyledLabel::~StyledLabel() {} |
39 | 75 |
40 void StyledLabel::AddLink(const ui::Range& range) { | 76 void StyledLabel::AddLink(const ui::Range& range) { |
41 DCHECK(!range.is_reversed()); | 77 DCHECK(!range.is_reversed()); |
42 DCHECK(!range.is_empty()); | 78 DCHECK(!range.is_empty()); |
43 DCHECK(ui::Range(0, text_.size()).Contains(range)); | 79 DCHECK(ui::Range(0, text_.size()).Contains(range)); |
44 link_ranges_.push(LinkRange(range)); | 80 |
| 81 RangeStyleInfo style_info; |
| 82 style_info.disable_line_wrapping = true; |
| 83 style_info.linkify = true; |
| 84 style_info.font_style = gfx::Font::UNDERLINE; |
| 85 style_ranges_.push(StyleRange(range, style_info)); |
| 86 |
45 calculated_size_ = gfx::Size(); | 87 calculated_size_ = gfx::Size(); |
46 InvalidateLayout(); | 88 InvalidateLayout(); |
47 } | 89 } |
| 90 |
| 91 void StyledLabel::AddStyleRange(const ui::Range& range, |
| 92 const RangeStyleInfo& style_info) { |
| 93 DCHECK(!range.is_reversed()); |
| 94 DCHECK(!range.is_empty()); |
| 95 DCHECK(ui::Range(0, text_.size()).Contains(range)); |
| 96 |
| 97 style_ranges_.push(StyleRange(range, style_info)); |
| 98 |
| 99 calculated_size_ = gfx::Size(); |
| 100 InvalidateLayout(); |
| 101 } |
48 | 102 |
49 gfx::Insets StyledLabel::GetInsets() const { | 103 gfx::Insets StyledLabel::GetInsets() const { |
50 gfx::Insets insets = View::GetInsets(); | 104 gfx::Insets insets = View::GetInsets(); |
51 const gfx::Insets focus_border_padding(1, 1, 1, 1); | 105 const gfx::Insets focus_border_padding(1, 1, 1, 1); |
52 insets += focus_border_padding; | 106 insets += focus_border_padding; |
53 return insets; | 107 return insets; |
54 } | 108 } |
55 | 109 |
56 int StyledLabel::GetHeightForWidth(int w) { | 110 int StyledLabel::GetHeightForWidth(int w) { |
57 if (w != calculated_size_.width()) | 111 if (w != calculated_size_.width()) |
(...skipping 21 matching lines...) Expand all Loading... |
79 return 0; | 133 return 0; |
80 | 134 |
81 const int line_height = CalculateLineHeight(); | 135 const int line_height = CalculateLineHeight(); |
82 // The index of the line we're on. | 136 // The index of the line we're on. |
83 int line = 0; | 137 int line = 0; |
84 // The x position (in pixels) of the line we're on, relative to content | 138 // The x position (in pixels) of the line we're on, relative to content |
85 // bounds. | 139 // bounds. |
86 int x = 0; | 140 int x = 0; |
87 | 141 |
88 string16 remaining_string = text_; | 142 string16 remaining_string = text_; |
89 std::priority_queue<LinkRange> link_ranges = link_ranges_; | 143 std::priority_queue<StyleRange> style_ranges = style_ranges_; |
90 | 144 |
91 // Iterate over the text, creating a bunch of labels and links and laying them | 145 // Iterate over the text, creating a bunch of labels and links and laying them |
92 // out in the appropriate positions. | 146 // out in the appropriate positions. |
93 while (!remaining_string.empty()) { | 147 while (!remaining_string.empty()) { |
94 // Don't put whitespace at beginning of a line. | 148 // Don't put whitespace at beginning of a line. |
95 if (x == 0) | 149 if (x == 0) |
96 TrimWhitespace(remaining_string, TRIM_LEADING, &remaining_string); | 150 TrimWhitespace(remaining_string, TRIM_LEADING, &remaining_string); |
97 | 151 |
98 ui::Range range(ui::Range::InvalidRange()); | 152 ui::Range range(ui::Range::InvalidRange()); |
99 if (!link_ranges.empty()) | 153 if (!style_ranges.empty()) |
100 range = link_ranges.top().range; | 154 range = style_ranges.top().range; |
| 155 |
| 156 const size_t position = text_.size() - remaining_string.size(); |
101 | 157 |
102 const gfx::Rect chunk_bounds(x, 0, width - x, 2 * line_height); | 158 const gfx::Rect chunk_bounds(x, 0, width - x, 2 * line_height); |
103 std::vector<string16> substrings; | 159 std::vector<string16> substrings; |
| 160 gfx::Font text_font; |
| 161 // If the start of the remaining text is inside a styled range, the font |
| 162 // style may differ from the base font. The font specified by the range |
| 163 // should be used when eliding text. |
| 164 if (position >= range.start()) { |
| 165 text_font = |
| 166 text_font.DeriveFont(0, style_ranges.top().style_info.font_style); |
| 167 } |
104 ui::ElideRectangleText(remaining_string, | 168 ui::ElideRectangleText(remaining_string, |
105 gfx::Font(), | 169 text_font, |
106 chunk_bounds.width(), | 170 chunk_bounds.width(), |
107 chunk_bounds.height(), | 171 chunk_bounds.height(), |
108 ui::IGNORE_LONG_WORDS, | 172 ui::IGNORE_LONG_WORDS, |
109 &substrings); | 173 &substrings); |
110 | 174 |
| 175 // This happens when only whitespace is left. |
| 176 if (substrings.empty()) |
| 177 break; |
| 178 |
111 string16 chunk = substrings[0]; | 179 string16 chunk = substrings[0]; |
112 if (chunk.empty()) { | 180 if (chunk.empty()) { |
113 // Nothing fit on this line. Start a new line. If x is 0, there's no room | 181 // Nothing fit on this line. Start a new line. If x is 0, there's no room |
114 // for anything. Just abort. | 182 // for anything. Just abort. |
115 if (x == 0) | 183 if (x == 0) |
116 break; | 184 break; |
117 | 185 |
118 x = 0; | 186 x = 0; |
119 line++; | 187 line++; |
120 continue; | 188 continue; |
121 } | 189 } |
122 | 190 |
123 scoped_ptr<View> view; | 191 scoped_ptr<View> view; |
124 const size_t position = text_.size() - remaining_string.size(); | |
125 if (position >= range.start()) { | 192 if (position >= range.start()) { |
126 // This chunk is a link. | 193 const RangeStyleInfo& style_info = style_ranges.top().style_info; |
127 if (chunk.size() < range.length() && x != 0) { | 194 |
128 // Don't wrap links. Try to fit them entirely on one line. | 195 if (style_info.disable_line_wrapping && chunk.size() < range.length() && |
| 196 position == range.start() && x != 0) { |
| 197 // If the chunk should not be wrapped, try to fit it entirely on the |
| 198 // next line. |
129 x = 0; | 199 x = 0; |
130 line++; | 200 line++; |
131 continue; | 201 continue; |
132 } | 202 } |
133 | 203 |
134 chunk = chunk.substr(0, range.length()); | 204 chunk = chunk.substr(0, std::min(chunk.size(), range.end() - position)); |
135 Link* link = new Link(chunk); | 205 |
136 link->set_listener(this); | 206 view = CreateLabelRange(chunk, style_info, this); |
137 if (!dry_run) | 207 |
138 link_targets_[link] = range; | 208 if (style_info.linkify && !dry_run) |
139 view.reset(link); | 209 link_targets_[view.get()] = range; |
140 link_ranges.pop(); | 210 |
| 211 if (position + chunk.size() >= range.end()) |
| 212 style_ranges.pop(); |
141 } else { | 213 } else { |
142 // This chunk is normal text. | 214 // This chunk is normal text. |
143 if (position + chunk.size() > range.start()) | 215 if (position + chunk.size() > range.start()) |
144 chunk = chunk.substr(0, range.start() - position); | 216 chunk = chunk.substr(0, range.start() - position); |
145 | 217 view = CreateLabelRange(chunk, RangeStyleInfo(), this); |
146 Label* label = new Label(chunk); | |
147 // Give the label a focus border so that its preferred size matches | |
148 // links' preferred sizes. | |
149 label->SetHasFocusBorder(true); | |
150 view.reset(label); | |
151 } | 218 } |
152 | 219 |
153 // Lay out the views to overlap by 1 pixel to compensate for their border | 220 // Lay out the views to overlap by 1 pixel to compensate for their border |
154 // spacing. Otherwise, "<a>link</a>," will render as "link ,". | 221 // spacing. Otherwise, "<a>link</a>," will render as "link ,". |
155 const int overlap = 1; | 222 const int overlap = 1; |
156 const gfx::Size view_size = view->GetPreferredSize(); | 223 const gfx::Size view_size = view->GetPreferredSize(); |
157 DCHECK_EQ(line_height, view_size.height() - 2 * overlap); | 224 DCHECK_EQ(line_height, view_size.height() - 2 * overlap); |
158 if (!dry_run) { | 225 if (!dry_run) { |
159 view->SetBoundsRect(gfx::Rect( | 226 view->SetBoundsRect(gfx::Rect( |
160 gfx::Point(GetInsets().left() + x - overlap, | 227 gfx::Point(GetInsets().left() + x - overlap, |
161 GetInsets().top() + line * line_height - overlap), | 228 GetInsets().top() + line * line_height - overlap), |
162 view_size)); | 229 view_size)); |
163 AddChildView(view.release()); | 230 AddChildView(view.release()); |
164 } | 231 } |
165 x += view_size.width() - 2 * overlap; | 232 x += view_size.width() - 2 * overlap; |
166 | 233 |
167 remaining_string = remaining_string.substr(chunk.size()); | 234 remaining_string = remaining_string.substr(chunk.size()); |
168 } | 235 } |
169 | 236 |
170 return (line + 1) * line_height + GetInsets().height(); | 237 return (line + 1) * line_height + GetInsets().height(); |
171 } | 238 } |
172 | 239 |
173 } // namespace views | 240 } // namespace views |
OLD | NEW |