Chromium Code Reviews| 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/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 13 #include "third_party/skia/include/core/SkColor.h" | |
| 13 #include "ui/base/range/range.h" | 14 #include "ui/base/range/range.h" |
| 14 #include "ui/gfx/size.h" | 15 #include "ui/gfx/size.h" |
| 15 #include "ui/views/controls/link_listener.h" | 16 #include "ui/views/controls/link_listener.h" |
| 16 #include "ui/views/view.h" | 17 #include "ui/views/view.h" |
| 17 | 18 |
| 18 namespace views { | 19 namespace views { |
| 19 | 20 |
| 20 class Link; | 21 class Link; |
| 21 class StyledLabelListener; | 22 class StyledLabelListener; |
| 22 | 23 |
| 23 // A class which can apply mixed styles to a block of text. Currently, text is | 24 // A class which can apply mixed styles to a block of text. Currently, text is |
| 24 // always multiline. Trailing whitespace in the styled label text is not | 25 // always multiline. Trailing whitespace in the styled label text is not |
| 25 // supported and will be trimmed on StyledLabel construction. Leading | 26 // supported and will be trimmed on StyledLabel construction. Leading |
| 26 // whitespace is respected, provided not only whitespace fits in the first line. | 27 // whitespace is respected, provided not only whitespace fits in the first line. |
| 27 // In this case, leading whitespace is ignored. | 28 // In this case, leading whitespace is ignored. |
| 28 class VIEWS_EXPORT StyledLabel : public View, public LinkListener { | 29 class VIEWS_EXPORT StyledLabel : public View, public LinkListener { |
| 29 public: | 30 public: |
| 30 // Parameters that define label style for a styled label's text range. | 31 // Parameters that define label style for a styled label's text range. |
| 31 struct VIEWS_EXPORT RangeStyleInfo { | 32 struct VIEWS_EXPORT RangeStyleInfo { |
| 32 RangeStyleInfo(); | 33 RangeStyleInfo(); |
| 33 ~RangeStyleInfo(); | 34 ~RangeStyleInfo(); |
| 34 | 35 |
| 35 // Creates a range style info with default values for link. | 36 // Creates a range style info with default values for link. |
| 36 static RangeStyleInfo CreateForLink(); | 37 static RangeStyleInfo CreateForLink(); |
| 37 | 38 |
| 38 // The font style that will be applied to the range. Should be a bitmask of | 39 // 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 // values defined in gfx::Font::FontStyle (BOLD, ITALIC, UNDERLINE). |
| 40 int font_style; | 41 int font_style; |
| 41 | 42 |
| 43 // The text color for the range. | |
| 44 SkColor color; | |
| 45 | |
| 42 // Tooltip for the range. | 46 // Tooltip for the range. |
| 43 string16 tooltip; | 47 string16 tooltip; |
| 44 | 48 |
| 45 // If set, the whole range will be put on a single line. | 49 // If set, the whole range will be put on a single line. |
| 46 bool disable_line_wrapping; | 50 bool disable_line_wrapping; |
| 47 | 51 |
| 48 // If set, the range will be created as a link. | 52 // If set, the range will be created as a link. |
| 49 bool is_link; | 53 bool is_link; |
| 50 }; | 54 }; |
| 51 | 55 |
| 52 // Note that any trailing whitespace in |text| will be trimmed. | 56 // Note that any trailing whitespace in |text| will be trimmed. |
| 53 StyledLabel(const string16& text, StyledLabelListener* listener); | 57 StyledLabel(const string16& text, StyledLabelListener* listener); |
| 54 virtual ~StyledLabel(); | 58 virtual ~StyledLabel(); |
| 55 | 59 |
| 56 // Sets the text to be displayed, and clears any previous styling. | 60 // Sets the text to be displayed, and clears any previous styling. |
| 57 void SetText(const string16& text); | 61 void SetText(const string16& text); |
| 58 | 62 |
| 59 // Marks the given range within |text_| with style defined by |style_info|. | 63 // Marks the given range within |text_| with style defined by |style_info|. |
| 60 // |range| must be contained in |text_|. | 64 // |range| must be contained in |text_|. |
| 61 void AddStyleRange(const ui::Range& range, const RangeStyleInfo& style_info); | 65 void AddStyleRange(const ui::Range& range, const RangeStyleInfo& style_info); |
| 62 | 66 |
| 67 // Enables or disables auto-color-readability (enabled by default). If this | |
| 68 // is enabled, color_utils::GetReadableColor() will be used to ensure that | |
| 69 // the foreground colors are readable over the background color. The | |
| 70 // background color must be set with SetBackgroundColor() for this to work. | |
| 71 void SetAutoColorReadabilityEnabled(bool enabled); | |
|
Evan Stade
2013/07/08 18:14:04
is this used somewhere?
fdoray
2013/07/09 18:13:47
No. But a method with the same name in views::Labe
Evan Stade
2013/07/09 18:35:18
yes, remove dead code.
fdoray
2013/07/09 20:54:28
Done.
| |
| 72 | |
| 73 // Sets the background color. This won't be explicitly drawn, but the label | |
| 74 // will force the text color to be readable over it. | |
| 75 void SetBackgroundColor(SkColor color); | |
|
Ben Goodger (Google)
2013/07/09 17:36:15
This name implies the background will be drawn tho
fdoray
2013/07/09 18:13:47
I chose this name because it's used in views::Labe
| |
| 76 SkColor background_color() const { return background_color_; } | |
| 77 | |
| 63 // View implementation: | 78 // View implementation: |
| 64 virtual gfx::Insets GetInsets() const OVERRIDE; | 79 virtual gfx::Insets GetInsets() const OVERRIDE; |
| 65 virtual int GetHeightForWidth(int w) OVERRIDE; | 80 virtual int GetHeightForWidth(int w) OVERRIDE; |
| 66 virtual void Layout() OVERRIDE; | 81 virtual void Layout() OVERRIDE; |
| 67 | 82 |
| 68 // LinkListener implementation: | 83 // LinkListener implementation: |
| 69 virtual void LinkClicked(Link* source, int event_flags) OVERRIDE; | 84 virtual void LinkClicked(Link* source, int event_flags) OVERRIDE; |
| 70 | 85 |
| 71 private: | 86 private: |
| 72 struct StyleRange { | 87 struct StyleRange { |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 99 std::priority_queue<StyleRange> style_ranges_; | 114 std::priority_queue<StyleRange> style_ranges_; |
| 100 | 115 |
| 101 // A mapping from a view to the range it corresponds to in |text_|. Only views | 116 // A mapping from a view to the range it corresponds to in |text_|. Only views |
| 102 // that correspond to ranges with is_link style set will be added to the map. | 117 // that correspond to ranges with is_link style set will be added to the map. |
| 103 std::map<View*, ui::Range> link_targets_; | 118 std::map<View*, ui::Range> link_targets_; |
| 104 | 119 |
| 105 // This variable saves the result of the last GetHeightForWidth call in order | 120 // This variable saves the result of the last GetHeightForWidth call in order |
| 106 // to avoid repeated calculation. | 121 // to avoid repeated calculation. |
| 107 gfx::Size calculated_size_; | 122 gfx::Size calculated_size_; |
| 108 | 123 |
| 124 // Background color, for auto color readability. | |
| 125 SkColor background_color_; | |
| 126 bool background_color_set_; | |
| 127 | |
| 128 // Indicates whether the foreground colors can be modified to be readable | |
| 129 // over the background color. | |
| 130 bool auto_color_readability_; | |
| 131 | |
| 109 DISALLOW_COPY_AND_ASSIGN(StyledLabel); | 132 DISALLOW_COPY_AND_ASSIGN(StyledLabel); |
| 110 }; | 133 }; |
| 111 | 134 |
| 112 } // namespace views | 135 } // namespace views |
| 113 | 136 |
| 114 #endif // UI_VIEWS_CONTROLS_STYLED_LABEL_H_ | 137 #endif // UI_VIEWS_CONTROLS_STYLED_LABEL_H_ |
| OLD | NEW |