OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/views/controls/link.h" | 5 #include "ui/views/controls/link.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 } | 36 } |
37 | 37 |
38 SkColor Link::GetDefaultEnabledColor() { | 38 SkColor Link::GetDefaultEnabledColor() { |
39 #if defined(OS_WIN) | 39 #if defined(OS_WIN) |
40 return color_utils::GetSysSkColor(COLOR_HOTLIGHT); | 40 return color_utils::GetSysSkColor(COLOR_HOTLIGHT); |
41 #else | 41 #else |
42 return SkColorSetRGB(0, 51, 153); | 42 return SkColorSetRGB(0, 51, 153); |
43 #endif | 43 #endif |
44 } | 44 } |
45 | 45 |
46 void Link::OnEnabledChanged() { | |
47 RecalculateFont(); | |
48 View::OnEnabledChanged(); | |
49 } | |
50 | |
51 const char* Link::GetClassName() const { | 46 const char* Link::GetClassName() const { |
52 return kViewClassName; | 47 return kViewClassName; |
53 } | 48 } |
54 | 49 |
55 gfx::NativeCursor Link::GetCursor(const ui::MouseEvent& event) { | 50 gfx::NativeCursor Link::GetCursor(const ui::MouseEvent& event) { |
56 if (!enabled()) | 51 if (!enabled()) |
57 return gfx::kNullCursor; | 52 return gfx::kNullCursor; |
58 #if defined(USE_AURA) | 53 #if defined(USE_AURA) |
59 return ui::kCursorHand; | 54 return ui::kCursorHand; |
60 #elif defined(OS_WIN) | 55 #elif defined(OS_WIN) |
61 static HCURSOR g_hand_cursor = LoadCursor(NULL, IDC_HAND); | 56 static HCURSOR g_hand_cursor = LoadCursor(NULL, IDC_HAND); |
62 return g_hand_cursor; | 57 return g_hand_cursor; |
63 #endif | 58 #endif |
64 } | 59 } |
65 | 60 |
66 void Link::OnPaint(gfx::Canvas* canvas) { | |
67 Label::OnPaint(canvas); | |
68 | |
69 if (HasFocus()) | |
70 canvas->DrawFocusRect(GetLocalBounds()); | |
71 } | |
72 | |
73 void Link::OnFocus() { | |
74 Label::OnFocus(); | |
75 // We render differently focused. | |
76 SchedulePaint(); | |
77 } | |
78 | |
79 void Link::OnBlur() { | |
80 Label::OnBlur(); | |
81 // We render differently focused. | |
82 SchedulePaint(); | |
83 } | |
84 | |
85 bool Link::HitTestRect(const gfx::Rect& rect) const { | 61 bool Link::HitTestRect(const gfx::Rect& rect) const { |
86 // We need to allow clicks on the link. So we override the implementation in | 62 // We need to allow clicks on the link. So we override the implementation in |
87 // Label and use the default implementation of View. | 63 // Label and use the default implementation of View. |
88 return View::HitTestRect(rect); | 64 return View::HitTestRect(rect); |
89 } | 65 } |
90 | 66 |
91 bool Link::OnMousePressed(const ui::MouseEvent& event) { | 67 bool Link::OnMousePressed(const ui::MouseEvent& event) { |
92 if (!enabled() || | 68 if (!enabled() || |
93 (!event.IsLeftMouseButton() && !event.IsMiddleMouseButton())) | 69 (!event.IsLeftMouseButton() && !event.IsMiddleMouseButton())) |
94 return false; | 70 return false; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 108 |
133 // Focus the link on key pressed. | 109 // Focus the link on key pressed. |
134 RequestFocus(); | 110 RequestFocus(); |
135 | 111 |
136 if (listener_) | 112 if (listener_) |
137 listener_->LinkClicked(this, event.flags()); | 113 listener_->LinkClicked(this, event.flags()); |
138 | 114 |
139 return true; | 115 return true; |
140 } | 116 } |
141 | 117 |
142 bool Link::SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) { | |
143 // Make sure we don't process space or enter as accelerators. | |
144 return (event.key_code() == ui::VKEY_SPACE) || | |
145 (event.key_code() == ui::VKEY_RETURN); | |
146 } | |
147 | |
148 void Link::GetAccessibleState(ui::AccessibleViewState* state) { | |
149 Label::GetAccessibleState(state); | |
150 state->role = ui::AccessibilityTypes::ROLE_LINK; | |
151 } | |
152 | |
153 void Link::OnGestureEvent(ui::GestureEvent* event) { | 118 void Link::OnGestureEvent(ui::GestureEvent* event) { |
154 if (!enabled()) | 119 if (!enabled()) |
155 return; | 120 return; |
156 | 121 |
157 if (event->type() == ui::ET_GESTURE_TAP_DOWN) { | 122 if (event->type() == ui::ET_GESTURE_TAP_DOWN) { |
158 SetPressed(true); | 123 SetPressed(true); |
159 } else if (event->type() == ui::ET_GESTURE_TAP) { | 124 } else if (event->type() == ui::ET_GESTURE_TAP) { |
160 RequestFocus(); | 125 RequestFocus(); |
161 if (listener_) | 126 if (listener_) |
162 listener_->LinkClicked(this, event->flags()); | 127 listener_->LinkClicked(this, event->flags()); |
163 } else { | 128 } else { |
164 SetPressed(false); | 129 SetPressed(false); |
165 return; | 130 return; |
166 } | 131 } |
167 event->SetHandled(); | 132 event->SetHandled(); |
168 } | 133 } |
169 | 134 |
| 135 bool Link::SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) { |
| 136 // Make sure we don't process space or enter as accelerators. |
| 137 return (event.key_code() == ui::VKEY_SPACE) || |
| 138 (event.key_code() == ui::VKEY_RETURN); |
| 139 } |
| 140 |
| 141 void Link::GetAccessibleState(ui::AccessibleViewState* state) { |
| 142 Label::GetAccessibleState(state); |
| 143 state->role = ui::AccessibilityTypes::ROLE_LINK; |
| 144 } |
| 145 |
| 146 void Link::OnEnabledChanged() { |
| 147 RecalculateFont(); |
| 148 View::OnEnabledChanged(); |
| 149 } |
| 150 |
| 151 void Link::OnPaint(gfx::Canvas* canvas) { |
| 152 Label::OnPaint(canvas); |
| 153 |
| 154 if (HasFocus()) |
| 155 canvas->DrawFocusRect(GetLocalBounds()); |
| 156 } |
| 157 |
| 158 void Link::OnFocus() { |
| 159 Label::OnFocus(); |
| 160 // We render differently focused. |
| 161 SchedulePaint(); |
| 162 } |
| 163 |
| 164 void Link::OnBlur() { |
| 165 Label::OnBlur(); |
| 166 // We render differently focused. |
| 167 SchedulePaint(); |
| 168 } |
| 169 |
170 void Link::SetFont(const gfx::Font& font) { | 170 void Link::SetFont(const gfx::Font& font) { |
171 Label::SetFont(font); | 171 Label::SetFont(font); |
172 RecalculateFont(); | 172 RecalculateFont(); |
173 } | 173 } |
174 | 174 |
| 175 void Link::SetText(const string16& text) { |
| 176 Label::SetText(text); |
| 177 // Disable focusability for empty links. Otherwise Label::GetInsets() will |
| 178 // give them an unconditional 1-px. inset on every side to allow for a focus |
| 179 // border, when in this case we probably wanted zero width. |
| 180 set_focusable(!text.empty()); |
| 181 } |
| 182 |
175 void Link::SetEnabledColor(SkColor color) { | 183 void Link::SetEnabledColor(SkColor color) { |
176 requested_enabled_color_ = color; | 184 requested_enabled_color_ = color; |
177 if (!pressed_) | 185 if (!pressed_) |
178 Label::SetEnabledColor(requested_enabled_color_); | 186 Label::SetEnabledColor(requested_enabled_color_); |
179 } | 187 } |
180 | 188 |
181 void Link::SetPressedColor(SkColor color) { | 189 void Link::SetPressedColor(SkColor color) { |
182 requested_pressed_color_ = color; | 190 requested_pressed_color_ = color; |
183 if (pressed_) | 191 if (pressed_) |
184 Label::SetEnabledColor(requested_pressed_color_); | 192 Label::SetEnabledColor(requested_pressed_color_); |
(...skipping 13 matching lines...) Expand all Loading... |
198 SetEnabledColor(GetDefaultEnabledColor()); | 206 SetEnabledColor(GetDefaultEnabledColor()); |
199 #if defined(OS_WIN) | 207 #if defined(OS_WIN) |
200 SetDisabledColor(color_utils::GetSysSkColor(COLOR_WINDOWTEXT)); | 208 SetDisabledColor(color_utils::GetSysSkColor(COLOR_WINDOWTEXT)); |
201 SetPressedColor(SkColorSetRGB(200, 0, 0)); | 209 SetPressedColor(SkColorSetRGB(200, 0, 0)); |
202 #else | 210 #else |
203 // TODO(beng): source from theme provider. | 211 // TODO(beng): source from theme provider. |
204 SetDisabledColor(SK_ColorBLACK); | 212 SetDisabledColor(SK_ColorBLACK); |
205 SetPressedColor(SK_ColorRED); | 213 SetPressedColor(SK_ColorRED); |
206 #endif | 214 #endif |
207 RecalculateFont(); | 215 RecalculateFont(); |
208 set_focusable(true); | 216 |
| 217 // Label::Init() calls SetText(), but if that's being called from Label(), our |
| 218 // SetText() override will not be reached (because the constructed class is |
| 219 // only a Label at the moment, not yet a Link). So so the set_focusable() |
| 220 // call explicitly here. |
| 221 set_focusable(!text().empty()); |
209 } | 222 } |
210 | 223 |
211 void Link::SetPressed(bool pressed) { | 224 void Link::SetPressed(bool pressed) { |
212 if (pressed_ != pressed) { | 225 if (pressed_ != pressed) { |
213 pressed_ = pressed; | 226 pressed_ = pressed; |
214 Label::SetEnabledColor(pressed_ ? | 227 Label::SetEnabledColor(pressed_ ? |
215 requested_pressed_color_ : requested_enabled_color_); | 228 requested_pressed_color_ : requested_enabled_color_); |
216 RecalculateFont(); | 229 RecalculateFont(); |
217 SchedulePaint(); | 230 SchedulePaint(); |
218 } | 231 } |
219 } | 232 } |
220 | 233 |
221 void Link::RecalculateFont() { | 234 void Link::RecalculateFont() { |
222 // Underline the link iff it is enabled and |underline_| is true. | 235 // Underline the link iff it is enabled and |underline_| is true. |
223 const int style = font().GetStyle(); | 236 const int style = font().GetStyle(); |
224 const int intended_style = (enabled() && underline_) ? | 237 const int intended_style = (enabled() && underline_) ? |
225 (style | gfx::Font::UNDERLINE) : (style & ~gfx::Font::UNDERLINE); | 238 (style | gfx::Font::UNDERLINE) : (style & ~gfx::Font::UNDERLINE); |
226 if (style != intended_style) | 239 if (style != intended_style) |
227 Label::SetFont(font().DeriveFont(0, intended_style)); | 240 Label::SetFont(font().DeriveFont(0, intended_style)); |
228 } | 241 } |
229 | 242 |
230 } // namespace views | 243 } // namespace views |
OLD | NEW |