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

Side by Side Diff: views/controls/label.h

Issue 8221027: Make views::Label and views::Link auto-color themselves to be readable over their background colo... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 9 years, 2 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 (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 VIEWS_CONTROLS_LABEL_H_ 5 #ifndef VIEWS_CONTROLS_LABEL_H_
6 #define VIEWS_CONTROLS_LABEL_H_ 6 #define VIEWS_CONTROLS_LABEL_H_
7 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 10
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 68
69 // Return the label text. 69 // Return the label text.
70 const string16 GetText() const; 70 const string16 GetText() const;
71 71
72 // Set URL Value - text_ is set to spec(). 72 // Set URL Value - text_ is set to spec().
73 void SetURL(const GURL& url); 73 void SetURL(const GURL& url);
74 74
75 // Return the label URL. 75 // Return the label URL.
76 const GURL GetURL() const; 76 const GURL GetURL() const;
77 77
78 // Set the color 78 // Enables or disables auto-color-readability (enabled by default). If this
79 virtual void SetColor(const SkColor& color); 79 // is enabled, then calls to set any foreground or background color will
80 // trigger an automatic mapper that uses color_utils::GetReadableColor() to
81 // ensure that the foreground colors are readable over the background color.
82 void set_auto_color_readability_enabled(bool enabled) {
83 auto_color_readability_ = enabled;
84 }
80 85
81 // Return a reference to the currently used color. 86 // Set the color. This will automatically force the color to be readable
82 virtual SkColor GetColor() const; 87 // over the current background color.
88 virtual void SetEnabledColor(const SkColor& color);
89 void SetDisabledColor(const SkColor& color);
83 90
84 // If you'll be displaying the label over some non-system background color, 91 SkColor enabled_color() const { return actual_enabled_color_; }
85 // call this with the relevant color and the label will auto-set its color to 92
86 // be readable. 93 // Set the background color. This won't be explicitly drawn, but the label
87 virtual void MakeReadableOverBackgroundColor(const SkColor& background_color); 94 // will force the text color to be readable over it.
95 void SetBackgroundColor(const SkColor& color);
88 96
89 // Set horizontal alignment. If the locale is RTL, and the RTL alignment 97 // Set horizontal alignment. If the locale is RTL, and the RTL alignment
90 // setting is set as USE_UI_ALIGNMENT, the alignment is flipped around. 98 // setting is set as USE_UI_ALIGNMENT, the alignment is flipped around.
91 // 99 //
92 // Caveat: for labels originating from a web page, the RTL alignment mode 100 // Caveat: for labels originating from a web page, the RTL alignment mode
93 // should be reset to AUTO_DETECT_ALIGNMENT before the horizontal alignment 101 // should be reset to AUTO_DETECT_ALIGNMENT before the horizontal alignment
94 // is set. Otherwise, the label's alignment specified as a parameter will be 102 // is set. Otherwise, the label's alignment specified as a parameter will be
95 // flipped in RTL locales. Please see the comments in SetRTLAlignmentMode for 103 // flipped in RTL locales. Please see the comments in SetRTLAlignmentMode for
96 // more information. 104 // more information.
97 void SetHorizontalAlignment(Alignment alignment); 105 void SetHorizontalAlignment(Alignment alignment);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 169
162 // Overridden from View: 170 // Overridden from View:
163 virtual gfx::Insets GetInsets() const; 171 virtual gfx::Insets GetInsets() const;
164 virtual int GetBaseline() const OVERRIDE; 172 virtual int GetBaseline() const OVERRIDE;
165 // Overridden to compute the size required to display this label. 173 // Overridden to compute the size required to display this label.
166 virtual gfx::Size GetPreferredSize() OVERRIDE; 174 virtual gfx::Size GetPreferredSize() OVERRIDE;
167 // Return the height necessary to display this label with the provided width. 175 // Return the height necessary to display this label with the provided width.
168 // This method is used to layout multi-line labels. It is equivalent to 176 // This method is used to layout multi-line labels. It is equivalent to
169 // GetPreferredSize().height() if the receiver is not multi-line. 177 // GetPreferredSize().height() if the receiver is not multi-line.
170 virtual int GetHeightForWidth(int w); 178 virtual int GetHeightForWidth(int w);
171 // Sets the enabled state. Setting the enabled state resets the color.
172 virtual void OnEnabledChanged() OVERRIDE;
173 virtual std::string GetClassName() const OVERRIDE; 179 virtual std::string GetClassName() const OVERRIDE;
174 virtual bool HitTest(const gfx::Point& l) const OVERRIDE; 180 virtual bool HitTest(const gfx::Point& l) const OVERRIDE;
175 // Mouse enter/exit are overridden to render mouse over background color. 181 // Mouse enter/exit are overridden to render mouse over background color.
176 // These invoke SetContainsMouse as necessary. 182 // These invoke SetContainsMouse as necessary.
177 virtual void OnMouseMoved(const MouseEvent& event) OVERRIDE; 183 virtual void OnMouseMoved(const MouseEvent& event) OVERRIDE;
178 virtual void OnMouseEntered(const MouseEvent& event) OVERRIDE; 184 virtual void OnMouseEntered(const MouseEvent& event) OVERRIDE;
179 virtual void OnMouseExited(const MouseEvent& event) OVERRIDE; 185 virtual void OnMouseExited(const MouseEvent& event) OVERRIDE;
180 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 186 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
181 // Gets the tooltip text for labels that are wider than their bounds, except 187 // Gets the tooltip text for labels that are wider than their bounds, except
182 // when the label is multiline, in which case it just returns false (no 188 // when the label is multiline, in which case it just returns false (no
(...skipping 26 matching lines...) Expand all
209 // calculations done for drawing text. 215 // calculations done for drawing text.
210 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineString); 216 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineString);
211 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineString); 217 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineString);
212 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineStringInRTL); 218 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawSingleLineStringInRTL);
213 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineStringInRTL); 219 FRIEND_TEST_ALL_PREFIXES(LabelTest, DrawMultiLineStringInRTL);
214 220
215 static gfx::Font GetDefaultFont(); 221 static gfx::Font GetDefaultFont();
216 222
217 void Init(const string16& text, const gfx::Font& font); 223 void Init(const string16& text, const gfx::Font& font);
218 224
225 void RecalculateColors();
226
219 // If the mouse is over the text, SetContainsMouse(true) is invoked, otherwise 227 // If the mouse is over the text, SetContainsMouse(true) is invoked, otherwise
220 // SetContainsMouse(false) is invoked. 228 // SetContainsMouse(false) is invoked.
221 void UpdateContainsMouse(const MouseEvent& event); 229 void UpdateContainsMouse(const MouseEvent& event);
222 230
223 // Updates whether the mouse is contained in the Label. If the new value 231 // Updates whether the mouse is contained in the Label. If the new value
224 // differs from the current value, and a mouse over background is specified, 232 // differs from the current value, and a mouse over background is specified,
225 // SchedulePaint is invoked. 233 // SchedulePaint is invoked.
226 void SetContainsMouse(bool contains_mouse); 234 void SetContainsMouse(bool contains_mouse);
227 235
228 // Returns where the text is drawn, in the receivers coordinate system. 236 // Returns where the text is drawn, in the receivers coordinate system.
229 gfx::Rect GetTextBounds() const; 237 gfx::Rect GetTextBounds() const;
230 238
231 int ComputeMultiLineFlags() const; 239 int ComputeMultiLineFlags() const;
232 240
233 gfx::Rect GetAvailableRect() const; 241 gfx::Rect GetAvailableRect() const;
234 242
235 // Returns parameters to be used for the DrawString call. 243 // Returns parameters to be used for the DrawString call.
236 void CalculateDrawStringParams(string16* paint_text, 244 void CalculateDrawStringParams(string16* paint_text,
237 gfx::Rect* text_bounds, 245 gfx::Rect* text_bounds,
238 int* flags) const; 246 int* flags) const;
239 247
240 string16 text_; 248 string16 text_;
241 GURL url_; 249 GURL url_;
242 gfx::Font font_; 250 gfx::Font font_;
243 SkColor color_; 251 SkColor requested_enabled_color_;
252 SkColor actual_enabled_color_;
253 SkColor requested_disabled_color_;
254 SkColor actual_disabled_color_;
255 SkColor background_color_;
256 bool auto_color_readability_;
244 mutable gfx::Size text_size_; 257 mutable gfx::Size text_size_;
245 mutable bool text_size_valid_; 258 mutable bool text_size_valid_;
246 bool is_multi_line_; 259 bool is_multi_line_;
247 bool allow_character_break_; 260 bool allow_character_break_;
248 bool elide_in_middle_; 261 bool elide_in_middle_;
249 bool url_set_; 262 bool url_set_;
250 Alignment horiz_alignment_; 263 Alignment horiz_alignment_;
251 string16 tooltip_text_; 264 string16 tooltip_text_;
252 // Whether the mouse is over this label. 265 // Whether the mouse is over this label.
253 bool contains_mouse_; 266 bool contains_mouse_;
(...skipping 11 matching lines...) Expand all
265 // allows this view to reserve space for a focus border that it otherwise 278 // allows this view to reserve space for a focus border that it otherwise
266 // might not have because it is not itself focusable. 279 // might not have because it is not itself focusable.
267 bool has_focus_border_; 280 bool has_focus_border_;
268 281
269 DISALLOW_COPY_AND_ASSIGN(Label); 282 DISALLOW_COPY_AND_ASSIGN(Label);
270 }; 283 };
271 284
272 } // namespace views 285 } // namespace views
273 286
274 #endif // VIEWS_CONTROLS_LABEL_H_ 287 #endif // VIEWS_CONTROLS_LABEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698