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/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 87 |
88 if (listener_) | 88 if (listener_) |
89 listener_->LinkClicked(this, event.flags()); | 89 listener_->LinkClicked(this, event.flags()); |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 void Link::OnMouseCaptureLost() { | 93 void Link::OnMouseCaptureLost() { |
94 SetPressed(false); | 94 SetPressed(false); |
95 } | 95 } |
96 | 96 |
| 97 void Link::OnMouseEntered(const ui::MouseEvent& event) { |
| 98 mouse_inside_ = true; |
| 99 RecalculateFont(); |
| 100 } |
| 101 |
| 102 void Link::OnMouseExited(const ui::MouseEvent& event) { |
| 103 mouse_inside_ = false; |
| 104 RecalculateFont(); |
| 105 } |
| 106 |
97 bool Link::OnKeyPressed(const ui::KeyEvent& event) { | 107 bool Link::OnKeyPressed(const ui::KeyEvent& event) { |
98 bool activate = ((event.key_code() == ui::VKEY_SPACE) || | 108 bool activate = ((event.key_code() == ui::VKEY_SPACE) || |
99 (event.key_code() == ui::VKEY_RETURN)); | 109 (event.key_code() == ui::VKEY_RETURN)); |
100 if (!activate) | 110 if (!activate) |
101 return false; | 111 return false; |
102 | 112 |
103 SetPressed(false); | 113 SetPressed(false); |
104 | 114 |
105 // Focus the link on key pressed. | 115 // Focus the link on key pressed. |
106 RequestFocus(); | 116 RequestFocus(); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 if (!pressed_) | 159 if (!pressed_) |
150 Label::SetEnabledColor(requested_enabled_color_); | 160 Label::SetEnabledColor(requested_enabled_color_); |
151 } | 161 } |
152 | 162 |
153 void Link::SetPressedColor(SkColor color) { | 163 void Link::SetPressedColor(SkColor color) { |
154 requested_pressed_color_ = color; | 164 requested_pressed_color_ = color; |
155 if (pressed_) | 165 if (pressed_) |
156 Label::SetEnabledColor(requested_pressed_color_); | 166 Label::SetEnabledColor(requested_pressed_color_); |
157 } | 167 } |
158 | 168 |
| 169 void Link::SetUnderlineOnHover(bool underline_on_hover) { |
| 170 underline_on_hover_ = underline_on_hover; |
| 171 RecalculateFont(); |
| 172 } |
| 173 |
159 void Link::Init() { | 174 void Link::Init() { |
160 static bool initialized = false; | 175 static bool initialized = false; |
161 static SkColor kDefaultEnabledColor; | 176 static SkColor kDefaultEnabledColor; |
162 static SkColor kDefaultDisabledColor; | 177 static SkColor kDefaultDisabledColor; |
163 static SkColor kDefaultPressedColor; | 178 static SkColor kDefaultPressedColor; |
164 if (!initialized) { | 179 if (!initialized) { |
165 #if defined(OS_WIN) | 180 #if defined(OS_WIN) |
166 kDefaultEnabledColor = color_utils::GetSysSkColor(COLOR_HOTLIGHT); | 181 kDefaultEnabledColor = color_utils::GetSysSkColor(COLOR_HOTLIGHT); |
167 kDefaultDisabledColor = color_utils::GetSysSkColor(COLOR_WINDOWTEXT); | 182 kDefaultDisabledColor = color_utils::GetSysSkColor(COLOR_WINDOWTEXT); |
168 kDefaultPressedColor = SkColorSetRGB(200, 0, 0); | 183 kDefaultPressedColor = SkColorSetRGB(200, 0, 0); |
169 #else | 184 #else |
170 // TODO(beng): source from theme provider. | 185 // TODO(beng): source from theme provider. |
171 kDefaultEnabledColor = SkColorSetRGB(0, 51, 153); | 186 kDefaultEnabledColor = SkColorSetRGB(0, 51, 153); |
172 kDefaultDisabledColor = SK_ColorBLACK; | 187 kDefaultDisabledColor = SK_ColorBLACK; |
173 kDefaultPressedColor = SK_ColorRED; | 188 kDefaultPressedColor = SK_ColorRED; |
174 #endif | 189 #endif |
175 | 190 |
176 initialized = true; | 191 initialized = true; |
177 } | 192 } |
178 | 193 |
179 listener_ = NULL; | 194 listener_ = NULL; |
180 pressed_ = false; | 195 pressed_ = false; |
| 196 underline_on_hover_ = false; |
| 197 mouse_inside_ = false; |
181 SetEnabledColor(kDefaultEnabledColor); | 198 SetEnabledColor(kDefaultEnabledColor); |
182 SetDisabledColor(kDefaultDisabledColor); | 199 SetDisabledColor(kDefaultDisabledColor); |
183 SetPressedColor(kDefaultPressedColor); | 200 SetPressedColor(kDefaultPressedColor); |
184 RecalculateFont(); | 201 RecalculateFont(); |
185 set_focusable(true); | 202 set_focusable(true); |
186 } | 203 } |
187 | 204 |
188 void Link::SetPressed(bool pressed) { | 205 void Link::SetPressed(bool pressed) { |
189 if (pressed_ != pressed) { | 206 if (pressed_ != pressed) { |
190 pressed_ = pressed; | 207 pressed_ = pressed; |
191 Label::SetEnabledColor(pressed_ ? | 208 Label::SetEnabledColor(pressed_ ? |
192 requested_pressed_color_ : requested_enabled_color_); | 209 requested_pressed_color_ : requested_enabled_color_); |
193 RecalculateFont(); | 210 RecalculateFont(); |
194 SchedulePaint(); | 211 SchedulePaint(); |
195 } | 212 } |
196 } | 213 } |
197 | 214 |
198 void Link::RecalculateFont() { | 215 void Link::RecalculateFont() { |
199 // The font should be underlined iff the link is enabled. | 216 if (underline_on_hover_) { |
200 if (enabled() == !(font().GetStyle() & gfx::Font::UNDERLINED)) { | 217 // For underline on hover, the font should be underlined iff the link is |
| 218 // enabled and the mouse is hovering over it. |
| 219 Label::SetFont(font().DeriveFont(0, enabled() && mouse_inside_ ? |
| 220 (font().GetStyle() | gfx::Font::UNDERLINED) : |
| 221 (font().GetStyle() & ~gfx::Font::UNDERLINED))); |
| 222 } else if (enabled() == !(font().GetStyle() & gfx::Font::UNDERLINED)) { |
| 223 // For constant underlining, the font should be underlined iff the link is |
| 224 // enabled. |
201 Label::SetFont(font().DeriveFont(0, enabled() ? | 225 Label::SetFont(font().DeriveFont(0, enabled() ? |
202 (font().GetStyle() | gfx::Font::UNDERLINED) : | 226 (font().GetStyle() | gfx::Font::UNDERLINED) : |
203 (font().GetStyle() & ~gfx::Font::UNDERLINED))); | 227 (font().GetStyle() & ~gfx::Font::UNDERLINED))); |
204 } | 228 } |
205 } | 229 } |
206 | 230 |
207 } // namespace views | 231 } // namespace views |
OLD | NEW |