Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: ui/views/controls/styled_label.cc

Issue 12906002: Add ability to defined ranges with different styles in StyledLabel (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/views/controls/styled_label.h ('k') | ui/views/controls/styled_label_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.is_link) {
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 is_link(false) {
60 }
61
62 StyledLabel::RangeStyleInfo::~RangeStyleInfo() {}
63
64 // static
65 StyledLabel::RangeStyleInfo StyledLabel::RangeStyleInfo::CreateForLink() {
66 RangeStyleInfo result;
67 result.disable_line_wrapping = true;
68 result.is_link = true;
69 result.font_style = gfx::Font::UNDERLINE;
70 return result;
71 }
72
73 bool StyledLabel::StyleRange::operator<(
74 const StyledLabel::StyleRange& other) const {
30 // Intentionally reversed so the priority queue is sorted by smallest first. 75 // Intentionally reversed so the priority queue is sorted by smallest first.
31 return range.start() > other.range.start(); 76 return range.start() > other.range.start();
32 } 77 }
33 78
34 StyledLabel::StyledLabel(const string16& text, StyledLabelListener* listener) 79 StyledLabel::StyledLabel(const string16& text, StyledLabelListener* listener)
35 : text_(text), 80 : listener_(listener) {
36 listener_(listener) {} 81 TrimWhitespace(text, TRIM_TRAILING, &text_);
82 }
37 83
38 StyledLabel::~StyledLabel() {} 84 StyledLabel::~StyledLabel() {}
39 85
40 void StyledLabel::AddLink(const ui::Range& range) { 86 void StyledLabel::AddStyleRange(const ui::Range& range,
87 const RangeStyleInfo& style_info) {
41 DCHECK(!range.is_reversed()); 88 DCHECK(!range.is_reversed());
42 DCHECK(!range.is_empty()); 89 DCHECK(!range.is_empty());
43 DCHECK(ui::Range(0, text_.size()).Contains(range)); 90 DCHECK(ui::Range(0, text_.size()).Contains(range));
44 link_ranges_.push(LinkRange(range)); 91
92 style_ranges_.push(StyleRange(range, style_info));
93
45 calculated_size_ = gfx::Size(); 94 calculated_size_ = gfx::Size();
46 InvalidateLayout(); 95 InvalidateLayout();
47 } 96 }
48 97
49 gfx::Insets StyledLabel::GetInsets() const { 98 gfx::Insets StyledLabel::GetInsets() const {
50 gfx::Insets insets = View::GetInsets(); 99 gfx::Insets insets = View::GetInsets();
51 const gfx::Insets focus_border_padding(1, 1, 1, 1); 100 const gfx::Insets focus_border_padding(1, 1, 1, 1);
52 insets += focus_border_padding; 101 insets += focus_border_padding;
53 return insets; 102 return insets;
54 } 103 }
(...skipping 24 matching lines...) Expand all
79 return 0; 128 return 0;
80 129
81 const int line_height = CalculateLineHeight(); 130 const int line_height = CalculateLineHeight();
82 // The index of the line we're on. 131 // The index of the line we're on.
83 int line = 0; 132 int line = 0;
84 // The x position (in pixels) of the line we're on, relative to content 133 // The x position (in pixels) of the line we're on, relative to content
85 // bounds. 134 // bounds.
86 int x = 0; 135 int x = 0;
87 136
88 string16 remaining_string = text_; 137 string16 remaining_string = text_;
89 std::priority_queue<LinkRange> link_ranges = link_ranges_; 138 std::priority_queue<StyleRange> style_ranges = style_ranges_;
90 139
91 // Iterate over the text, creating a bunch of labels and links and laying them 140 // Iterate over the text, creating a bunch of labels and links and laying them
92 // out in the appropriate positions. 141 // out in the appropriate positions.
93 while (!remaining_string.empty()) { 142 while (!remaining_string.empty()) {
94 // Don't put whitespace at beginning of a line. 143 // Don't put whitespace at beginning of a line with an exception for the
95 if (x == 0) 144 // first line (so the text's leading whitespace is respected).
145 if (x == 0 && line > 0)
Evan Stade 2013/03/29 19:35:48 I don't think you need to make this change. |posit
tbarzic 2013/03/29 20:00:57 yeah, but this way we actually respect leading whi
Evan Stade 2013/03/29 23:14:58 ok, lgtm
96 TrimWhitespace(remaining_string, TRIM_LEADING, &remaining_string); 146 TrimWhitespace(remaining_string, TRIM_LEADING, &remaining_string);
97 147
98 ui::Range range(ui::Range::InvalidRange()); 148 ui::Range range(ui::Range::InvalidRange());
99 if (!link_ranges.empty()) 149 if (!style_ranges.empty())
100 range = link_ranges.top().range; 150 range = style_ranges.top().range;
151
152 const size_t position = text_.size() - remaining_string.size();
101 153
102 const gfx::Rect chunk_bounds(x, 0, width - x, 2 * line_height); 154 const gfx::Rect chunk_bounds(x, 0, width - x, 2 * line_height);
103 std::vector<string16> substrings; 155 std::vector<string16> substrings;
156 gfx::Font text_font;
157 // If the start of the remaining text is inside a styled range, the font
158 // style may differ from the base font. The font specified by the range
159 // should be used when eliding text.
160 if (position >= range.start()) {
161 text_font =
162 text_font.DeriveFont(0, style_ranges.top().style_info.font_style);
163 }
104 ui::ElideRectangleText(remaining_string, 164 ui::ElideRectangleText(remaining_string,
105 gfx::Font(), 165 text_font,
106 chunk_bounds.width(), 166 chunk_bounds.width(),
107 chunk_bounds.height(), 167 chunk_bounds.height(),
108 ui::IGNORE_LONG_WORDS, 168 ui::IGNORE_LONG_WORDS,
109 &substrings); 169 &substrings);
110 170
171 DCHECK(!substrings.empty());
111 string16 chunk = substrings[0]; 172 string16 chunk = substrings[0];
112 if (chunk.empty()) { 173 if (chunk.empty()) {
113 // Nothing fit on this line. Start a new line. If x is 0, there's no room 174 // Nothing fits on this line. Start a new line.
114 // for anything. Just abort. 175 // If x is 0, first line may have leading whitespace that doesn't fit in a
115 if (x == 0) 176 // single line, so try trimming those. Otherwise there is no room for
177 // anything; abort.
178 if (x == 0) {
179 if (line == 0) {
180 TrimWhitespace(remaining_string, TRIM_LEADING, &remaining_string);
181 continue;
182 }
116 break; 183 break;
184 }
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.is_link && !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
OLDNEW
« no previous file with comments | « ui/views/controls/styled_label.h ('k') | ui/views/controls/styled_label_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698