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 #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 and leading whitespace in text is not supported |
25 // of text. | 25 // and will be trimmed on StyledLabel construction. |
26 class VIEWS_EXPORT StyledLabel : public View, public LinkListener { | 26 class VIEWS_EXPORT StyledLabel : public View, public LinkListener { |
27 public: | 27 public: |
28 // Parameters that define label style for a styled label's text range. | |
29 struct RangeStyleInfo { | |
30 RangeStyleInfo(); | |
31 ~RangeStyleInfo(); | |
32 | |
33 // The font style that will be applied to the range. Should be a bitmask of | |
34 // values defined in gfx::Font::FontStyle (BOLD, ITALIC, UNDERLINE). | |
35 int font_style; | |
36 // Tooltip for the range. | |
37 string16 tooltip; | |
38 // If set, the whole range will be put on a single line. | |
39 bool disable_line_wrapping; | |
40 // If set, the range will be created as a link. | |
41 bool linkify; | |
sky
2013/03/28 16:02:09
How about is_link.
tbarzic
2013/03/28 20:33:58
Done.
| |
42 }; | |
43 | |
44 // Note that any trailing or leading whitespace in |text| will be trimmed. | |
28 StyledLabel(const string16& text, StyledLabelListener* listener); | 45 StyledLabel(const string16& text, StyledLabelListener* listener); |
29 virtual ~StyledLabel(); | 46 virtual ~StyledLabel(); |
30 | 47 |
31 // Marks the given range within |text_| as a link. | 48 // Marks the given range within |text_| as a link. |range| should be contained |
sky
2013/03/28 16:02:09
should->must
tbarzic
2013/03/28 20:33:58
Done.
| |
49 // in |text_|. | |
32 void AddLink(const ui::Range& range); | 50 void AddLink(const ui::Range& range); |
33 | 51 |
52 // Marks the given range within |text_| with style defined by |style_info|. | |
53 // |range| should be contained in |text_|. | |
sky
2013/03/28 16:02:09
should->must
tbarzic
2013/03/28 20:33:58
Done.
| |
54 void AddStyleRange(const ui::Range& range, const RangeStyleInfo& style_info); | |
55 | |
34 // View implementation: | 56 // View implementation: |
35 virtual gfx::Insets GetInsets() const OVERRIDE; | 57 virtual gfx::Insets GetInsets() const OVERRIDE; |
36 virtual int GetHeightForWidth(int w) OVERRIDE; | 58 virtual int GetHeightForWidth(int w) OVERRIDE; |
37 virtual void Layout() OVERRIDE; | 59 virtual void Layout() OVERRIDE; |
38 | 60 |
39 // LinkListener implementation: | 61 // LinkListener implementation: |
40 virtual void LinkClicked(Link* source, int event_flags) OVERRIDE; | 62 virtual void LinkClicked(Link* source, int event_flags) OVERRIDE; |
41 | 63 |
42 private: | 64 private: |
43 struct LinkRange { | 65 struct StyleRange { |
44 explicit LinkRange(const ui::Range& range) : range(range) {} | 66 StyleRange(const ui::Range& range, |
45 ~LinkRange() {} | 67 const RangeStyleInfo& style_info) |
68 : range(range), | |
69 style_info(style_info) { | |
70 } | |
71 ~StyleRange() {} | |
46 | 72 |
47 bool operator<(const LinkRange& other) const; | 73 bool operator<(const StyleRange& other) const; |
48 | 74 |
49 ui::Range range; | 75 ui::Range range; |
76 RangeStyleInfo style_info; | |
50 }; | 77 }; |
51 | 78 |
52 // Calculates how to layout child views, creates them and sets their size | 79 // Calculates how to layout child views, creates them and sets their size |
53 // and position. |width| is the horizontal space, in pixels, that the view | 80 // and position. |width| is the horizontal space, in pixels, that the view |
54 // has to work with. If |dry_run| is true, the view hierarchy is not touched. | 81 // has to work with. If |dry_run| is true, the view hierarchy is not touched. |
55 // The return value is the height in pixels. | 82 // The return value is the height in pixels. |
56 int CalculateAndDoLayout(int width, bool dry_run); | 83 int CalculateAndDoLayout(int width, bool dry_run); |
57 | 84 |
58 // The text to display. | 85 // The text to display. |
59 string16 text_; | 86 string16 text_; |
60 | 87 |
61 // The listener that will be informed of link clicks. | 88 // The listener that will be informed of link clicks. |
62 StyledLabelListener* listener_; | 89 StyledLabelListener* listener_; |
63 | 90 |
64 // The ranges that should be linkified, sorted by start position. | 91 // The ranges that should be linkified, sorted by start position. |
65 std::priority_queue<LinkRange> link_ranges_; | 92 std::priority_queue<StyleRange> style_ranges_; |
66 | 93 |
67 // A mapping from Link* control to the range it corresponds to in |text_|. | 94 // A mapping from a view to the range it corresponds to in |text_|. Only views |
68 std::map<Link*, ui::Range> link_targets_; | 95 // that correspond to ranges with linkify style set will be added to the map. |
96 std::map<View*, ui::Range> link_targets_; | |
69 | 97 |
70 // This variable saves the result of the last GetHeightForWidth call in order | 98 // This variable saves the result of the last GetHeightForWidth call in order |
71 // to avoid repeated calculation. | 99 // to avoid repeated calculation. |
72 gfx::Size calculated_size_; | 100 gfx::Size calculated_size_; |
73 | 101 |
74 DISALLOW_COPY_AND_ASSIGN(StyledLabel); | 102 DISALLOW_COPY_AND_ASSIGN(StyledLabel); |
75 }; | 103 }; |
76 | 104 |
77 } // namespace views | 105 } // namespace views |
78 | 106 |
79 #endif // UI_VIEWS_CONTROLS_STYLED_LABEL_H_ | 107 #endif // UI_VIEWS_CONTROLS_STYLED_LABEL_H_ |
OLD | NEW |