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

Side by Side Diff: ui/views/controls/rich_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: 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/rich_label.h"
6
7 #include <deque>
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
15 namespace views {
16
17 RichLabel::RichLabel(const std::vector<string16>& text,
18 const std::vector<GURL>& urls)
19 : strings_(text),
20 urls_(urls) {}
21
22 RichLabel::~RichLabel() {}
23
24 gfx::Size RichLabel::GetPreferredSize() {
25 return gfx::Size(100, 100);
26 }
27
28 void RichLabel::Layout() {
29 RemoveAllChildViews(true);
30 gfx::Font font =
31 ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont);
32 gfx::Rect total_bounds(GetLocalBounds());
33 int height = font.GetHeight();
34 int line = 0;
35 int x = 0;
36
37 std::deque<string16> queue(strings_.begin(), strings_.end());
38 int url_index = 0;
39
40 while (!queue.empty()) {
41 string16 string = queue.front();
42 // Don't put whitespace at beginning of line.
43 if (x == 0)
44 TrimWhitespace(string, TRIM_LEADING, &string);
45 queue.pop_front();
46 gfx::Rect bounds = total_bounds;
47 bounds.set_height(2 * height);
48 bounds.set_x(x);
49 bounds.set_width(bounds.width() - x);
50
51 std::vector<string16> substrings;
52 ui::ElideRectangleText(string,
53 font,
54 bounds.width(), bounds.height(),
55 ui::IGNORE_LONG_WORDS,
56 &substrings);
57
58 View* view = NULL;
59 if (urls_[url_index].is_empty())
60 view = new Label(string);
61 else if (substrings.size() == 1)
62 view = new Link(string);
63
64 if (view) {
65 AddChildView(view);
66 view->SetBoundsRect(gfx::Rect(gfx::Point(x, line * height),
67 view->GetPreferredSize()));
68 x += view->bounds().width();
69
70 if (substrings.size() == 1) {
71 // It all fit on one line.
72 url_index++;
73 } else {
74 // Only part of the string fit on this line. Wrap the remainder to a new
75 // line.
76 queue.push_front(string.substr(substrings[0].size()));
77 line++;
78 x = 0;
79 }
80 } else {
81 // There may be a link that's too long to fit on an entire line. Don't get
82 // stuck in a loop; just ignore that link.
83 if (x != 0)
84 queue.push_front(string);
85 x = 0;
86 line++;
87 }
88 }
89 }
90
91 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698