OLD | NEW |
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 #include "views/widget/tooltip_manager_views.h" | 5 #include "views/widget/tooltip_manager_views.h" |
6 | 6 |
7 #if defined(USE_X11) | 7 #if defined(USE_X11) |
8 #include <X11/Xlib.h> | 8 #include <X11/Xlib.h> |
9 #include <X11/extensions/XInput2.h> | 9 #include <X11/extensions/XInput2.h> |
10 #endif | 10 #endif |
11 | 11 |
12 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
13 #include <windowsx.h> | 13 #include <windowsx.h> |
14 #endif | 14 #endif |
15 | 15 |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/time.h" | 17 #include "base/time.h" |
18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
19 #include "third_party/skia/include/core/SkColor.h" | 19 #include "third_party/skia/include/core/SkColor.h" |
| 20 #if defined(USE_AURA) |
| 21 #include "ui/aura/aura_constants.h" |
| 22 #include "ui/aura/event.h" |
| 23 #include "ui/aura/window.h" |
| 24 #endif |
20 #include "ui/base/resource/resource_bundle.h" | 25 #include "ui/base/resource/resource_bundle.h" |
21 #include "ui/gfx/font.h" | 26 #include "ui/gfx/font.h" |
22 #include "ui/gfx/screen.h" | 27 #include "ui/gfx/screen.h" |
23 #include "views/background.h" | 28 #include "views/background.h" |
24 #include "views/border.h" | 29 #include "views/border.h" |
25 #include "views/events/event.h" | 30 #include "views/events/event.h" |
26 #include "views/focus/focus_manager.h" | 31 #include "views/focus/focus_manager.h" |
27 #include "views/view.h" | 32 #include "views/view.h" |
28 #include "views/widget/native_widget.h" | 33 #include "views/widget/native_widget.h" |
29 #include "views/widget/root_view.h" | |
30 | 34 |
31 #if defined(USE_WAYLAND) | 35 #if defined(USE_WAYLAND) |
32 #include "ui/wayland/events/wayland_event.h" | 36 #include "ui/wayland/events/wayland_event.h" |
33 #endif | 37 #endif |
34 | 38 |
35 namespace { | 39 namespace { |
36 SkColor kTooltipBackground = 0xFF7F7F00; | 40 SkColor kTooltipBackground = 0xFF7F7F00; |
37 int kTooltipTimeoutMs = 500; | 41 int kTooltipTimeoutMs = 500; |
38 | 42 |
39 // FIXME: get cursor offset from actual cursor size. | 43 // FIXME: get cursor offset from actual cursor size. |
(...skipping 22 matching lines...) Expand all Loading... |
62 // We always display the tooltip inside the root view. So the max width is | 66 // We always display the tooltip inside the root view. So the max width is |
63 // the width of the view. | 67 // the width of the view. |
64 gfx::Rect monitor_bounds = | 68 gfx::Rect monitor_bounds = |
65 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y)); | 69 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y)); |
66 // GtkLabel (gtk_label_ensure_layout) forces wrapping at this size. We mirror | 70 // GtkLabel (gtk_label_ensure_layout) forces wrapping at this size. We mirror |
67 // the size here otherwise tooltips wider than the size used by gtklabel end | 71 // the size here otherwise tooltips wider than the size used by gtklabel end |
68 // up with extraneous empty lines. | 72 // up with extraneous empty lines. |
69 return monitor_bounds.width() == 0 ? 800 : (monitor_bounds.width() + 1) / 2; | 73 return monitor_bounds.width() == 0 ? 800 : (monitor_bounds.width() + 1) / 2; |
70 } | 74 } |
71 | 75 |
72 TooltipManagerViews::TooltipManagerViews(internal::RootView* root_view) | 76 TooltipManagerViews::TooltipManagerViews(views::View* root_view) |
73 : root_view_(root_view), | 77 : root_view_(root_view), |
74 tooltip_view_(NULL) { | 78 tooltip_view_(NULL) { |
75 tooltip_label_.set_background( | 79 tooltip_label_.set_background( |
76 views::Background::CreateSolidBackground(kTooltipBackground)); | 80 views::Background::CreateSolidBackground(kTooltipBackground)); |
77 tooltip_widget_.reset(CreateTooltip()); | 81 tooltip_widget_.reset(CreateTooltip()); |
78 tooltip_widget_->SetContentsView(&tooltip_label_); | 82 tooltip_widget_->SetContentsView(&tooltip_label_); |
79 tooltip_widget_->Activate(); | 83 tooltip_widget_->Activate(); |
80 tooltip_widget_->SetAlwaysOnTop(true); | 84 tooltip_widget_->SetAlwaysOnTop(true); |
81 tooltip_timer_.Start(FROM_HERE, | 85 tooltip_timer_.Start(FROM_HERE, |
82 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), | 86 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), |
(...skipping 25 matching lines...) Expand all Loading... |
108 | 112 |
109 #if defined(USE_WAYLAND) | 113 #if defined(USE_WAYLAND) |
110 base::MessagePumpObserver::EventStatus TooltipManagerViews::WillProcessEvent( | 114 base::MessagePumpObserver::EventStatus TooltipManagerViews::WillProcessEvent( |
111 ui::WaylandEvent* event) { | 115 ui::WaylandEvent* event) { |
112 if (event->type == ui::WAYLAND_MOTION) | 116 if (event->type == ui::WAYLAND_MOTION) |
113 OnMouseMoved(event->motion.x, event->motion.y); | 117 OnMouseMoved(event->motion.x, event->motion.y); |
114 return base::MessagePumpObserver::EVENT_CONTINUE; | 118 return base::MessagePumpObserver::EVENT_CONTINUE; |
115 } | 119 } |
116 #elif defined(USE_X11) | 120 #elif defined(USE_X11) |
117 base::EventStatus TooltipManagerViews::WillProcessEvent( | 121 base::EventStatus TooltipManagerViews::WillProcessEvent( |
118 const base::NativeEvent& event) { | 122 const base::NativeEvent& native_event) { |
119 XGenericEventCookie* cookie = &event->xcookie; | 123 if (!ui::IsMouseEvent(native_event)) |
120 if (cookie->evtype == XI_Motion) { | 124 return base::EVENT_CONTINUE; |
121 const XIDeviceEvent* xievent = static_cast<XIDeviceEvent*>(cookie->data); | 125 #if defined(USE_AURA) |
122 OnMouseMoved(static_cast<int>(xievent->event_x), | 126 aura::MouseEvent event(native_event); |
123 static_cast<int>(xievent->event_y)); | 127 #else |
| 128 MouseEvent event(native_event); |
| 129 #endif |
| 130 switch (event.type()) { |
| 131 case ui::ET_MOUSE_MOVED: |
| 132 OnMouseMoved(event.x(), event.y()); |
| 133 default: |
| 134 break; |
124 } | 135 } |
125 return base::EVENT_CONTINUE; | 136 return base::EVENT_CONTINUE; |
126 } | 137 } |
127 | 138 |
128 void TooltipManagerViews::DidProcessEvent(const base::NativeEvent& event) { | 139 void TooltipManagerViews::DidProcessEvent(const base::NativeEvent& event) { |
129 } | 140 } |
130 #elif defined(OS_WIN) | 141 #elif defined(OS_WIN) |
131 base::EventStatus TooltipManagerViews::WillProcessEvent( | 142 base::EventStatus TooltipManagerViews::WillProcessEvent( |
132 const base::NativeEvent& event) { | 143 const base::NativeEvent& event) { |
133 if (event.message == WM_MOUSEMOVE) | 144 if (event.message == WM_MOUSEMOVE) |
134 OnMouseMoved(GET_X_LPARAM(event.lParam), GET_Y_LPARAM(event.lParam)); | 145 OnMouseMoved(GET_X_LPARAM(event.lParam), GET_Y_LPARAM(event.lParam)); |
135 return base::EVENT_CONTINUE; | 146 return base::EVENT_CONTINUE; |
136 } | 147 } |
137 | 148 |
138 void TooltipManagerViews::DidProcessEvent(const base::NativeEvent& event) { | 149 void TooltipManagerViews::DidProcessEvent(const base::NativeEvent& event) { |
139 } | 150 } |
140 #endif | 151 #endif |
141 | 152 |
142 void TooltipManagerViews::TooltipTimerFired() { | 153 void TooltipManagerViews::TooltipTimerFired() { |
143 if (tooltip_widget_->IsVisible()) { | 154 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); |
144 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); | |
145 } else { | |
146 tooltip_view_ = GetViewForTooltip(curr_mouse_pos_.x(), curr_mouse_pos_.y(), | |
147 false); | |
148 Update(); | |
149 } | |
150 } | 155 } |
151 | 156 |
152 View* TooltipManagerViews::GetViewForTooltip(int x, int y, bool for_keyboard) { | 157 View* TooltipManagerViews::GetViewForTooltip(int x, int y, bool for_keyboard) { |
153 View* view = NULL; | 158 View* view = NULL; |
154 if (!for_keyboard) { | 159 if (!for_keyboard) { |
155 view = root_view_->GetEventHandlerForPoint(gfx::Point(x, y)); | 160 view = root_view_->GetEventHandlerForPoint(gfx::Point(x, y)); |
156 } else { | 161 } else { |
157 FocusManager* focus_manager = root_view_->GetFocusManager(); | 162 FocusManager* focus_manager = root_view_->GetFocusManager(); |
158 if (focus_manager) | 163 if (focus_manager) |
159 view = focus_manager->GetFocusedView(); | 164 view = focus_manager->GetFocusedView(); |
160 } | 165 } |
161 return view; | 166 return view; |
162 } | 167 } |
163 | 168 |
164 void TooltipManagerViews::UpdateIfRequired(int x, int y, bool for_keyboard) { | 169 void TooltipManagerViews::UpdateIfRequired(int x, int y, bool for_keyboard) { |
165 View* view = GetViewForTooltip(x, y, for_keyboard); | 170 View* view = GetViewForTooltip(x, y, for_keyboard); |
166 string16 tooltip_text; | 171 string16 tooltip_text; |
167 if (view) | 172 if (view) |
168 view->GetTooltipText(gfx::Point(x, y), &tooltip_text); | 173 view->GetTooltipText(gfx::Point(x, y), &tooltip_text); |
169 | 174 |
| 175 #if defined(USE_AURA) |
| 176 // In aura, and aura::Window can also have a tooltip. If the view doesnot have |
| 177 // a tooltip, we must also check for the aura::Window underneath the cursor. |
| 178 if (tooltip_text.empty()) { |
| 179 aura::Window* root = reinterpret_cast<aura::Window*>( |
| 180 root_view_->GetWidget()->GetNativeView()); |
| 181 if (root) { |
| 182 aura::Window* window = root->GetEventHandlerForPoint(gfx::Point(x, y)); |
| 183 if (window) { |
| 184 void* property = window->GetProperty(aura::kTooltipTextKey); |
| 185 if (property) |
| 186 tooltip_text = *reinterpret_cast<string16*>(property); |
| 187 } |
| 188 } |
| 189 } |
| 190 #endif |
| 191 |
170 if (tooltip_view_ != view || tooltip_text_ != tooltip_text) { | 192 if (tooltip_view_ != view || tooltip_text_ != tooltip_text) { |
171 tooltip_view_ = view; | 193 tooltip_view_ = view; |
| 194 tooltip_text_ = tooltip_text; |
172 Update(); | 195 Update(); |
173 } | 196 } |
174 } | 197 } |
175 | 198 |
176 void TooltipManagerViews::Update() { | 199 void TooltipManagerViews::Update() { |
177 if (!tooltip_view_ || | |
178 !tooltip_view_->GetTooltipText(gfx::Point(), &tooltip_text_)) | |
179 tooltip_text_.clear(); | |
180 | |
181 if (tooltip_text_.empty()) { | 200 if (tooltip_text_.empty()) { |
182 tooltip_widget_->Hide(); | 201 tooltip_widget_->Hide(); |
183 } else { | 202 } else { |
184 int max_width, line_count; | 203 int max_width, line_count; |
185 string16 tooltip_text(tooltip_text_); | 204 string16 tooltip_text(tooltip_text_); |
186 TrimTooltipToFit(&tooltip_text, &max_width, &line_count, | 205 TrimTooltipToFit(&tooltip_text, &max_width, &line_count, |
187 curr_mouse_pos_.x(), curr_mouse_pos_.y()); | 206 curr_mouse_pos_.x(), curr_mouse_pos_.y()); |
188 tooltip_label_.SetText(tooltip_text); | 207 tooltip_label_.SetText(tooltip_text); |
189 | 208 |
190 SetTooltipBounds(curr_mouse_pos_, max_width, | 209 SetTooltipBounds(curr_mouse_pos_, max_width, |
(...skipping 11 matching lines...) Expand all Loading... |
202 | 221 |
203 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); | 222 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); |
204 gfx::Rect monitor_bounds = | 223 gfx::Rect monitor_bounds = |
205 gfx::Screen::GetMonitorAreaNearestPoint(tooltip_rect.origin()); | 224 gfx::Screen::GetMonitorAreaNearestPoint(tooltip_rect.origin()); |
206 tooltip_widget_->SetBounds(tooltip_rect.AdjustToFit(monitor_bounds)); | 225 tooltip_widget_->SetBounds(tooltip_rect.AdjustToFit(monitor_bounds)); |
207 } | 226 } |
208 | 227 |
209 Widget* TooltipManagerViews::CreateTooltip() { | 228 Widget* TooltipManagerViews::CreateTooltip() { |
210 Widget* widget = new Widget; | 229 Widget* widget = new Widget; |
211 Widget::InitParams params; | 230 Widget::InitParams params; |
| 231 // For aura, since we set the type to TOOLTIP_TYPE, the widget will get |
| 232 // auto-parented to the MenuAndTooltipsContainer. |
212 params.type = Widget::InitParams::TYPE_TOOLTIP; | 233 params.type = Widget::InitParams::TYPE_TOOLTIP; |
213 params.keep_on_top = true; | 234 params.keep_on_top = true; |
214 params.accept_events = false; | 235 params.accept_events = false; |
215 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 236 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
216 widget->Init(params); | 237 widget->Init(params); |
217 widget->SetOpacity(0x00); | 238 widget->SetOpacity(0x00); |
218 return widget; | 239 return widget; |
219 } | 240 } |
220 | 241 |
221 void TooltipManagerViews::OnMouseMoved(int x, int y) { | 242 void TooltipManagerViews::OnMouseMoved(int x, int y) { |
222 if (tooltip_timer_.IsRunning()) | 243 if (tooltip_timer_.IsRunning()) |
223 tooltip_timer_.Reset(); | 244 tooltip_timer_.Reset(); |
224 curr_mouse_pos_.SetPoint(x, y); | 245 curr_mouse_pos_.SetPoint(x, y); |
225 | 246 |
226 // If tooltip is visible, we may want to hide it. If it is not, we are ok. | 247 // If tooltip is visible, we may want to hide it. If it is not, we are ok. |
227 if (tooltip_widget_->IsVisible()) | 248 if (tooltip_widget_->IsVisible()) |
228 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); | 249 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); |
229 } | 250 } |
230 | 251 |
231 } // namespace views | 252 } // namespace views |
OLD | NEW |