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

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

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
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 #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 <map> 8 #include <map>
9 #include <queue> 9 #include <queue>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/string16.h" 12 #include "base/string16.h"
13 #include "ui/base/range/range.h" 13 #include "ui/base/range/range.h"
14 #include "ui/gfx/size.h" 14 #include "ui/gfx/size.h"
15 #include "ui/views/controls/link_listener.h" 15 #include "ui/views/controls/link_listener.h"
16 #include "ui/views/view.h" 16 #include "ui/views/view.h"
17 17
18 namespace views { 18 namespace views {
19 19
20 class Link; 20 class Link;
21 class StyledLabelListener; 21 class StyledLabelListener;
22 22
23 // A class which can apply mixed styles to a block of text. Currently, text is 23 // A class which can apply mixed styles to a block of text. Currently, text is
24 // always multiline, and the only style that may be applied is linkifying ranges 24 // always multiline. Trailing whitespace in the styled label text is not
25 // of text. 25 // supported and will be trimmed on StyledLabel construction. Leading
26 // whitespace is respected, provided not only whitespace fits in the first line.
27 // In this case, leading whitespace is ignored.
26 class VIEWS_EXPORT StyledLabel : public View, public LinkListener { 28 class VIEWS_EXPORT StyledLabel : public View, public LinkListener {
27 public: 29 public:
30 // Parameters that define label style for a styled label's text range.
31 struct VIEWS_EXPORT RangeStyleInfo {
32 RangeStyleInfo();
33 ~RangeStyleInfo();
34
35 // Creates a range style info with default values for link.
36 static RangeStyleInfo CreateForLink();
37
38 // The font style that will be applied to the range. Should be a bitmask of
39 // values defined in gfx::Font::FontStyle (BOLD, ITALIC, UNDERLINE).
40 int font_style;
41
42 // Tooltip for the range.
43 string16 tooltip;
44
45 // If set, the whole range will be put on a single line.
46 bool disable_line_wrapping;
47
48 // If set, the range will be created as a link.
49 bool is_link;
50 };
51
52 // Note that any trailing whitespace in |text| will be trimmed.
28 StyledLabel(const string16& text, StyledLabelListener* listener); 53 StyledLabel(const string16& text, StyledLabelListener* listener);
29 virtual ~StyledLabel(); 54 virtual ~StyledLabel();
30 55
31 // Sets the text to be displayed, and clears any previous styling. 56 // Sets the text to be displayed, and clears any previous styling.
32 void SetText(const string16& text); 57 void SetText(const string16& text);
33 58
34 // Marks the given range within |text_| as a link. 59 // Marks the given range within |text_| with style defined by |style_info|.
35 void AddLink(const ui::Range& range); 60 // |range| must be contained in |text_|.
61 void AddStyleRange(const ui::Range& range, const RangeStyleInfo& style_info);
36 62
37 // View implementation: 63 // View implementation:
38 virtual gfx::Insets GetInsets() const OVERRIDE; 64 virtual gfx::Insets GetInsets() const OVERRIDE;
39 virtual int GetHeightForWidth(int w) OVERRIDE; 65 virtual int GetHeightForWidth(int w) OVERRIDE;
40 virtual void Layout() OVERRIDE; 66 virtual void Layout() OVERRIDE;
41 67
42 // LinkListener implementation: 68 // LinkListener implementation:
43 virtual void LinkClicked(Link* source, int event_flags) OVERRIDE; 69 virtual void LinkClicked(Link* source, int event_flags) OVERRIDE;
44 70
45 private: 71 private:
46 struct LinkRange { 72 struct StyleRange {
47 explicit LinkRange(const ui::Range& range) : range(range) {} 73 StyleRange(const ui::Range& range,
48 ~LinkRange() {} 74 const RangeStyleInfo& style_info)
75 : range(range),
76 style_info(style_info) {
77 }
78 ~StyleRange() {}
49 79
50 bool operator<(const LinkRange& other) const; 80 bool operator<(const StyleRange& other) const;
51 81
52 ui::Range range; 82 ui::Range range;
83 RangeStyleInfo style_info;
53 }; 84 };
54 85
55 // Calculates how to layout child views, creates them and sets their size 86 // Calculates how to layout child views, creates them and sets their size
56 // and position. |width| is the horizontal space, in pixels, that the view 87 // and position. |width| is the horizontal space, in pixels, that the view
57 // has to work with. If |dry_run| is true, the view hierarchy is not touched. 88 // has to work with. If |dry_run| is true, the view hierarchy is not touched.
58 // The return value is the height in pixels. 89 // The return value is the height in pixels.
59 int CalculateAndDoLayout(int width, bool dry_run); 90 int CalculateAndDoLayout(int width, bool dry_run);
60 91
61 // The text to display. 92 // The text to display.
62 string16 text_; 93 string16 text_;
63 94
64 // The listener that will be informed of link clicks. 95 // The listener that will be informed of link clicks.
65 StyledLabelListener* listener_; 96 StyledLabelListener* listener_;
66 97
67 // The ranges that should be linkified, sorted by start position. 98 // The ranges that should be linkified, sorted by start position.
68 std::priority_queue<LinkRange> link_ranges_; 99 std::priority_queue<StyleRange> style_ranges_;
69 100
70 // A mapping from Link* control to the range it corresponds to in |text_|. 101 // A mapping from a view to the range it corresponds to in |text_|. Only views
71 std::map<Link*, ui::Range> link_targets_; 102 // that correspond to ranges with is_link style set will be added to the map.
103 std::map<View*, ui::Range> link_targets_;
72 104
73 // This variable saves the result of the last GetHeightForWidth call in order 105 // This variable saves the result of the last GetHeightForWidth call in order
74 // to avoid repeated calculation. 106 // to avoid repeated calculation.
75 gfx::Size calculated_size_; 107 gfx::Size calculated_size_;
76 108
77 DISALLOW_COPY_AND_ASSIGN(StyledLabel); 109 DISALLOW_COPY_AND_ASSIGN(StyledLabel);
78 }; 110 };
79 111
80 } // namespace views 112 } // namespace views
81 113
82 #endif // UI_VIEWS_CONTROLS_STYLED_LABEL_H_ 114 #endif // UI_VIEWS_CONTROLS_STYLED_LABEL_H_
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/autofill/autofill_dialog_views.cc ('k') | ui/views/controls/styled_label.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698