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

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

Issue 12543032: Add views::RichLabel, a class which creates multi-line text with mixed (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: other interface Created 7 years, 9 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
OLDNEW
(Empty)
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 #include "ui/views/controls/styled_label.h"
6
7 #include <vector>
8
9 #include "base/string_util.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/base/text/text_elider.h"
12 #include "ui/views/controls/label.h"
13 #include "ui/views/controls/link.h"
14 #include "ui/views/controls/styled_label_listener.h"
15
16 namespace views {
17
18 bool StyledLabel::LinkRange::operator<(const StyledLabel::LinkRange& other)
19 const {
20 // Intentionally reversed so the priority queue is sorted by smallest first.
21 return range.start() > other.range.start();
22 }
23
24 StyledLabel::StyledLabel(const string16& text, StyledLabelListener* listener)
msw 2013/03/13 03:22:07 nit: move the 2nd arg to a new line.
sky 2013/03/13 14:33:00 Not necessary. You only need put each arg on its o
25 : text_(text),
26 listener_(listener) {}
27
28 StyledLabel::~StyledLabel() {}
29
30 void StyledLabel::SetLink(const ui::Range& range) {
31 DCHECK(ui::Range(0, text_.size()).Contains(range));
32 link_ranges_.push(LinkRange(range));
sky 2013/03/13 14:33:00 Doesn't this need to mark the calculated_size_ inv
Evan Stade 2013/03/14 00:30:46 Done.
33 }
34
35 int StyledLabel::GetHeightForWidth(int w) {
36 if (w != calculated_size_.width())
37 calculated_size_ = gfx::Size(w, CalculateAndDoLayout(w, true));
38
39 return calculated_size_.height();
40 }
41
42 void StyledLabel::Layout() {
43 CalculateAndDoLayout(GetLocalBounds().width(), false);
sky 2013/03/13 14:33:00 Honor the border here and in CalculateAndDoLayout.
Evan Stade 2013/03/14 00:30:46 Done.
44 }
45
46 int StyledLabel::CalculateAndDoLayout(int width, bool dry_run) {
47 if (width == 0)
48 return 0;
49
50 if (!dry_run) {
51 RemoveAllChildViews(true);
52 link_targets_.clear();
53 }
54
55 gfx::Font font =
56 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont);
57
58 int line_height = font.GetHeight();
59 // The index of the line we're on.
60 int line = 0;
61 // The x position (in pixels) of the line we're on.
62 int x = 0;
63
64 string16 remaining_string = text_;
65 std::priority_queue<LinkRange> link_ranges = link_ranges_;
66
67 // Iterate over the text, creating a bunch of labels and links and laying them
68 // out in the appropriate positions.
69 while (!remaining_string.empty()) {
70 // Don't put whitespace at beginning of a line.
71 if (x == 0)
72 TrimWhitespace(remaining_string, TRIM_LEADING, &remaining_string);
73
74 ui::Range range(ui::Range::InvalidRange());
75 if (!link_ranges.empty())
76 range = link_ranges.top().range;
77
78 gfx::Rect chunk_bounds(x, 0, width - x, 2 * line_height);
79 std::vector<string16> substrings;
80 ui::ElideRectangleText(remaining_string,
81 font,
82 chunk_bounds.width(),
83 chunk_bounds.height(),
84 ui::IGNORE_LONG_WORDS,
85 &substrings);
86
87 string16 chunk = substrings[0];
88 if (chunk.empty()) {
89 // Nothing fit on this line. Start a new line. If x is 0, there's no room
90 // for anything. Just abort.
91 if (x == 0)
92 break;
93
94 x = 0;
95 line++;
96 continue;
97 }
98
99 scoped_ptr<View> view;
100 size_t position = text_.size() - remaining_string.size();
101 if (position >= range.start()) {
102 // This chunk is a link.
103 if (chunk.size() < range.length() && x != 0) {
104 // Don't wrap links. Try to fit them entirely on one line.
105 x = 0;
106 line++;
107 continue;
108 }
109
110 chunk = chunk.substr(0, range.length());
111 Link* link = new Link(chunk);
112 link->set_listener(this);
113 if (!dry_run)
114 link_targets_[link] = range;
115 view.reset(link);
sky 2013/03/13 14:33:00 Do links have the same preferred height as labels?
Evan Stade 2013/03/14 00:30:46 nope, fixed by adding a focus border (which should
116 link_ranges.pop();
117 } else {
118 // This chunk is normal text.
119 if (position + chunk.size() > range.start())
120 chunk = chunk.substr(0, range.start() - position);
121
122 view.reset(new Label(chunk));
123 }
124
125 gfx::Size view_size = view->GetPreferredSize();
126 if (!dry_run) {
127 view->SetBoundsRect(gfx::Rect(gfx::Point(x, line * line_height),
128 view_size));
129 AddChildView(view.release());
130 }
131 x += view_size.width();
132
133 remaining_string = remaining_string.substr(chunk.size());
134 }
135
136 return (line + 1) * line_height;
137 }
138
139 void StyledLabel::LinkClicked(Link* source, int event_flags) {
140 listener_->StyledLabelLinkClicked(link_targets_[source], event_flags);
141 }
142
143 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698