Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / views / controls / link.cc
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
3 // found in the LICENSE file.
4
5 #include "ui/views/controls/link.h"
6
7 #include "build/build_config.h"
8
9 #include "base/logging.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/base/accessibility/accessible_view_state.h"
12 #include "ui/events/event.h"
13 #include "ui/events/keycodes/keyboard_codes.h"
14 #include "ui/gfx/canvas.h"
15 #include "ui/gfx/color_utils.h"
16 #include "ui/gfx/font_list.h"
17 #include "ui/views/controls/link_listener.h"
18
19 #if defined(USE_AURA)
20 #include "ui/base/cursor/cursor.h"
21 #endif
22
23 namespace views {
24
25 const char Link::kViewClassName[] = "Link";
26
27 Link::Link() : Label(base::string16()) {
28   Init();
29 }
30
31 Link::Link(const base::string16& title) : Label(title) {
32   Init();
33 }
34
35 Link::~Link() {
36 }
37
38 SkColor Link::GetDefaultEnabledColor() {
39 #if defined(OS_WIN)
40   return color_utils::GetSysSkColor(COLOR_HOTLIGHT);
41 #else
42   return SkColorSetRGB(0, 51, 153);
43 #endif
44 }
45
46 const char* Link::GetClassName() const {
47   return kViewClassName;
48 }
49
50 gfx::NativeCursor Link::GetCursor(const ui::MouseEvent& event) {
51   if (!enabled())
52     return gfx::kNullCursor;
53 #if defined(USE_AURA)
54   return ui::kCursorHand;
55 #elif defined(OS_WIN)
56   static HCURSOR g_hand_cursor = LoadCursor(NULL, IDC_HAND);
57   return g_hand_cursor;
58 #endif
59 }
60
61 bool Link::HitTestRect(const gfx::Rect& rect) const {
62   // We need to allow clicks on the link. So we override the implementation in
63   // Label and use the default implementation of View.
64   return View::HitTestRect(rect);
65 }
66
67 bool Link::OnMousePressed(const ui::MouseEvent& event) {
68   if (!enabled() ||
69       (!event.IsLeftMouseButton() && !event.IsMiddleMouseButton()))
70     return false;
71   SetPressed(true);
72   return true;
73 }
74
75 bool Link::OnMouseDragged(const ui::MouseEvent& event) {
76   SetPressed(enabled() &&
77              (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) &&
78              HitTestPoint(event.location()));
79   return true;
80 }
81
82 void Link::OnMouseReleased(const ui::MouseEvent& event) {
83   // Change the highlight first just in case this instance is deleted
84   // while calling the controller
85   OnMouseCaptureLost();
86   if (enabled() &&
87       (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) &&
88       HitTestPoint(event.location())) {
89     // Focus the link on click.
90     RequestFocus();
91
92     if (listener_)
93       listener_->LinkClicked(this, event.flags());
94   }
95 }
96
97 void Link::OnMouseCaptureLost() {
98   SetPressed(false);
99 }
100
101 bool Link::OnKeyPressed(const ui::KeyEvent& event) {
102   bool activate = ((event.key_code() == ui::VKEY_SPACE) ||
103                    (event.key_code() == ui::VKEY_RETURN));
104   if (!activate)
105     return false;
106
107   SetPressed(false);
108
109   // Focus the link on key pressed.
110   RequestFocus();
111
112   if (listener_)
113     listener_->LinkClicked(this, event.flags());
114
115   return true;
116 }
117
118 void Link::OnGestureEvent(ui::GestureEvent* event) {
119   if (!enabled())
120     return;
121
122   if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
123     SetPressed(true);
124   } else if (event->type() == ui::ET_GESTURE_TAP) {
125     RequestFocus();
126     if (listener_)
127       listener_->LinkClicked(this, event->flags());
128   } else {
129     SetPressed(false);
130     return;
131   }
132   event->SetHandled();
133 }
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::OnFocus() {
152   Label::OnFocus();
153   // We render differently focused.
154   SchedulePaint();
155 }
156
157 void Link::OnBlur() {
158   Label::OnBlur();
159   // We render differently focused.
160   SchedulePaint();
161 }
162
163 void Link::SetFontList(const gfx::FontList& font_list) {
164   Label::SetFontList(font_list);
165   RecalculateFont();
166 }
167
168 void Link::SetText(const base::string16& text) {
169   Label::SetText(text);
170   // Disable focusability for empty links.  Otherwise Label::GetInsets() will
171   // give them an unconditional 1-px. inset on every side to allow for a focus
172   // border, when in this case we probably wanted zero width.
173   SetFocusable(!text.empty());
174 }
175
176 void Link::SetEnabledColor(SkColor color) {
177   requested_enabled_color_ = color;
178   if (!pressed_)
179     Label::SetEnabledColor(requested_enabled_color_);
180 }
181
182 void Link::SetPressedColor(SkColor color) {
183   requested_pressed_color_ = color;
184   if (pressed_)
185     Label::SetEnabledColor(requested_pressed_color_);
186 }
187
188 void Link::SetUnderline(bool underline) {
189   if (underline_ == underline)
190     return;
191   underline_ = underline;
192   RecalculateFont();
193 }
194
195 void Link::Init() {
196   listener_ = NULL;
197   pressed_ = false;
198   underline_ = true;
199   SetEnabledColor(GetDefaultEnabledColor());
200 #if defined(OS_WIN)
201   SetDisabledColor(color_utils::GetSysSkColor(COLOR_WINDOWTEXT));
202   SetPressedColor(SkColorSetRGB(200, 0, 0));
203 #else
204   // TODO(beng): source from theme provider.
205   SetDisabledColor(SK_ColorBLACK);
206   SetPressedColor(SK_ColorRED);
207 #endif
208   RecalculateFont();
209
210   // Label::Init() calls SetText(), but if that's being called from Label(), our
211   // SetText() override will not be reached (because the constructed class is
212   // only a Label at the moment, not yet a Link).  So so the set_focusable()
213   // call explicitly here.
214   SetFocusable(!text().empty());
215 }
216
217 void Link::SetPressed(bool pressed) {
218   if (pressed_ != pressed) {
219     pressed_ = pressed;
220     Label::SetEnabledColor(pressed_ ?
221         requested_pressed_color_ : requested_enabled_color_);
222     RecalculateFont();
223     SchedulePaint();
224   }
225 }
226
227 void Link::RecalculateFont() {
228   // Underline the link iff it is enabled and |underline_| is true.
229   const int style = font_list().GetFontStyle();
230   const int intended_style = (enabled() && underline_) ?
231       (style | gfx::Font::UNDERLINE) : (style & ~gfx::Font::UNDERLINE);
232   if (style != intended_style)
233     Label::SetFontList(font_list().DeriveWithStyle(intended_style));
234 }
235
236 }  // namespace views