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

Side by Side Diff: chrome/views/controls/label.cc

Issue 92004: Fix focus rects for checkboxes and radio buttons:... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 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
« no previous file with comments | « chrome/views/controls/label.h ('k') | chrome/views/controls/label_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 #include "chrome/views/controls/label.h" 5 #include "chrome/views/controls/label.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "chrome/common/gfx/chrome_canvas.h" 11 #include "chrome/common/gfx/chrome_canvas.h"
12 #include "chrome/common/gfx/chrome_font.h" 12 #include "chrome/common/gfx/chrome_font.h"
13 #include "chrome/common/gfx/insets.h" 13 #include "chrome/common/gfx/insets.h"
14 #include "chrome/common/gfx/text_elider.h" 14 #include "chrome/common/gfx/text_elider.h"
15 #include "chrome/common/l10n_util.h" 15 #include "chrome/common/l10n_util.h"
16 #include "chrome/common/resource_bundle.h" 16 #include "chrome/common/resource_bundle.h"
17 #include "chrome/views/background.h" 17 #include "chrome/views/background.h"
18 18
19 namespace views { 19 namespace views {
20 20
21 const char Label::kViewClassName[] = "chrome/views/Label"; 21 const char Label::kViewClassName[] = "chrome/views/Label";
22 22
23 static const SkColor kEnabledColor = SK_ColorBLACK; 23 static const SkColor kEnabledColor = SK_ColorBLACK;
24 static const SkColor kDisabledColor = SkColorSetRGB(161, 161, 146); 24 static const SkColor kDisabledColor = SkColorSetRGB(161, 161, 146);
25 static const int kFocusBorderPadding = 1;
25 26
26 Label::Label() { 27 Label::Label() {
27 Init(L"", GetDefaultFont()); 28 Init(L"", GetDefaultFont());
28 } 29 }
29 30
30 Label::Label(const std::wstring& text) { 31 Label::Label(const std::wstring& text) {
31 Init(text, GetDefaultFont()); 32 Init(text, GetDefaultFont());
32 } 33 }
33 34
34 Label::Label(const std::wstring& text, const ChromeFont& font) { 35 Label::Label(const std::wstring& text, const ChromeFont& font) {
35 Init(text, font); 36 Init(text, font);
36 } 37 }
37 38
38 void Label::Init(const std::wstring& text, const ChromeFont& font) { 39 void Label::Init(const std::wstring& text, const ChromeFont& font) {
39 contains_mouse_ = false; 40 contains_mouse_ = false;
40 font_ = font; 41 font_ = font;
41 text_size_valid_ = false; 42 text_size_valid_ = false;
42 SetText(text); 43 SetText(text);
43 url_set_ = false; 44 url_set_ = false;
44 color_ = kEnabledColor; 45 color_ = kEnabledColor;
45 horiz_alignment_ = ALIGN_CENTER; 46 horiz_alignment_ = ALIGN_CENTER;
46 is_multi_line_ = false; 47 is_multi_line_ = false;
47 collapse_when_hidden_ = false; 48 collapse_when_hidden_ = false;
48 rtl_alignment_mode_ = USE_UI_ALIGNMENT; 49 rtl_alignment_mode_ = USE_UI_ALIGNMENT;
50 paint_as_focused_ = false;
49 } 51 }
50 52
51 Label::~Label() { 53 Label::~Label() {
52 } 54 }
53 55
54 gfx::Size Label::GetPreferredSize() { 56 gfx::Size Label::GetPreferredSize() {
55 gfx::Size prefsize; 57 gfx::Size prefsize;
56 58
57 // Return a size of (0, 0) if the label is not visible and if the 59 // Return a size of (0, 0) if the label is not visible and if the
58 // collapse_when_hidden_ flag is set. 60 // collapse_when_hidden_ flag is set.
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 CalculateDrawStringParams(&paint_text, &text_bounds, &flags); 138 CalculateDrawStringParams(&paint_text, &text_bounds, &flags);
137 canvas->DrawStringInt(paint_text, 139 canvas->DrawStringInt(paint_text,
138 font_, 140 font_,
139 color_, 141 color_,
140 text_bounds.x(), 142 text_bounds.x(),
141 text_bounds.y(), 143 text_bounds.y(),
142 text_bounds.width(), 144 text_bounds.width(),
143 text_bounds.height(), 145 text_bounds.height(),
144 flags); 146 flags);
145 147
146 if (is_multi_line_) { 148 // The focus border always hugs the text, regardless of the label's bounds.
147 PaintFocusBorder(canvas); 149 if (HasFocus() || paint_as_focused_) {
148 } else { 150 int w = text_bounds.width();
149 // We'll draw the focus border ourselves, so it is around the text. 151 int h = 0;
150 if (HasFocus()) 152 // We explicitly OR in MULTI_LINE here since SizeStringInt seems to return
151 canvas->DrawFocusRect(text_bounds.x(), 153 // an incorrect height for single line text when the MULTI_LINE flag isn't
152 text_bounds.y(), 154 // specified. o_O...
153 text_bounds.width(), 155 ChromeCanvas::SizeStringInt(paint_text, font_, &w, &h,
154 text_bounds.height()); 156 flags | ChromeCanvas::MULTI_LINE);
157 gfx::Rect focus_rect = text_bounds;
158 focus_rect.set_width(w);
159 focus_rect.set_height(h);
160 focus_rect.Inset(-kFocusBorderPadding, -kFocusBorderPadding);
161 canvas->DrawFocusRect(focus_rect.x(), focus_rect.y(), focus_rect.width(),
162 focus_rect.height());
155 } 163 }
156 } 164 }
157 165
158 void Label::PaintBackground(ChromeCanvas* canvas) { 166 void Label::PaintBackground(ChromeCanvas* canvas) {
159 const Background* bg = contains_mouse_ ? GetMouseOverBackground() : NULL; 167 const Background* bg = contains_mouse_ ? GetMouseOverBackground() : NULL;
160 if (!bg) 168 if (!bg)
161 bg = background(); 169 bg = background();
162 if (bg) 170 if (bg)
163 bg->Paint(canvas, this); 171 bg->Paint(canvas, this);
164 } 172 }
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 return mouse_over_background_.get(); 326 return mouse_over_background_.get();
319 } 327 }
320 328
321 void Label::SetEnabled(bool enabled) { 329 void Label::SetEnabled(bool enabled) {
322 if (enabled == enabled_) 330 if (enabled == enabled_)
323 return; 331 return;
324 View::SetEnabled(enabled); 332 View::SetEnabled(enabled);
325 SetColor(enabled ? kEnabledColor : kDisabledColor); 333 SetColor(enabled ? kEnabledColor : kDisabledColor);
326 } 334 }
327 335
336 gfx::Insets Label::GetInsets() const {
337 gfx::Insets insets = View::GetInsets();
338 if (IsFocusable() || paint_as_focused_) {
339 insets += gfx::Insets(kFocusBorderPadding, kFocusBorderPadding,
340 kFocusBorderPadding, kFocusBorderPadding);
341 }
342 return insets;
343 }
344
328 // static 345 // static
329 ChromeFont Label::GetDefaultFont() { 346 ChromeFont Label::GetDefaultFont() {
330 return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); 347 return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont);
331 } 348 }
332 349
333 void Label::UpdateContainsMouse(const MouseEvent& event) { 350 void Label::UpdateContainsMouse(const MouseEvent& event) {
334 SetContainsMouse(GetTextBounds().Contains(event.x(), event.y())); 351 SetContainsMouse(GetTextBounds().Contains(event.x(), event.y()));
335 } 352 }
336 353
337 void Label::SetContainsMouse(bool contains_mouse) { 354 void Label::SetContainsMouse(bool contains_mouse) {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 427
411 bool Label::GetAccessibleState(VARIANT* state) { 428 bool Label::GetAccessibleState(VARIANT* state) {
412 DCHECK(state); 429 DCHECK(state);
413 430
414 state->lVal |= STATE_SYSTEM_READONLY; 431 state->lVal |= STATE_SYSTEM_READONLY;
415 return true; 432 return true;
416 } 433 }
417 #endif // defined(OS_WIN) 434 #endif // defined(OS_WIN)
418 435
419 } // namespace views 436 } // namespace views
OLDNEW
« no previous file with comments | « chrome/views/controls/label.h ('k') | chrome/views/controls/label_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698