| 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/event.h" |
| 22 #include "ui/aura/window.h" |
| 23 #include "ui/aura_shell/aura_constants.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 // Convert x,y from screen coordinates to |root_view_| coordinates. |
| 161 gfx::Point point(x, y); |
| 162 gfx::Rect r = root_view_->GetWidget()->GetClientAreaScreenBounds(); |
| 163 point.SetPoint(point.x() - r.x(), point.y() - r.y()); |
| 164 View::ConvertPointFromWidget(root_view_, &point); |
| 165 view = root_view_->GetEventHandlerForPoint(point); |
| 156 } else { | 166 } else { |
| 157 FocusManager* focus_manager = root_view_->GetFocusManager(); | 167 FocusManager* focus_manager = root_view_->GetFocusManager(); |
| 158 if (focus_manager) | 168 if (focus_manager) |
| 159 view = focus_manager->GetFocusedView(); | 169 view = focus_manager->GetFocusedView(); |
| 160 } | 170 } |
| 161 return view; | 171 return view; |
| 162 } | 172 } |
| 163 | 173 |
| 164 void TooltipManagerViews::UpdateIfRequired(int x, int y, bool for_keyboard) { | 174 void TooltipManagerViews::UpdateIfRequired(int x, int y, bool for_keyboard) { |
| 165 View* view = GetViewForTooltip(x, y, for_keyboard); | 175 View* view = GetViewForTooltip(x, y, for_keyboard); |
| 166 string16 tooltip_text; | 176 string16 tooltip_text; |
| 167 if (view) | 177 if (view) |
| 168 view->GetTooltipText(gfx::Point(x, y), &tooltip_text); | 178 view->GetTooltipText(gfx::Point(x, y), &tooltip_text); |
| 169 | 179 |
| 180 #if defined(USE_AURA) |
| 181 // In aura, and aura::Window can also have a tooltip. If the view doesnot have |
| 182 // a tooltip, we must also check for the aura::Window underneath the cursor. |
| 183 if (tooltip_text.empty()) { |
| 184 aura::Window* root = reinterpret_cast<aura::Window*>( |
| 185 root_view_->GetWidget()->GetNativeView()); |
| 186 if (root) { |
| 187 aura::Window* window = root->GetEventHandlerForPoint(gfx::Point(x, y)); |
| 188 if (window) { |
| 189 void* property = window->GetProperty(aura_shell::kTooltipTextKey); |
| 190 if (property) |
| 191 tooltip_text = *reinterpret_cast<string16*>(property); |
| 192 } |
| 193 } |
| 194 } |
| 195 #endif |
| 196 |
| 170 if (tooltip_view_ != view || tooltip_text_ != tooltip_text) { | 197 if (tooltip_view_ != view || tooltip_text_ != tooltip_text) { |
| 171 tooltip_view_ = view; | 198 tooltip_view_ = view; |
| 199 tooltip_text_ = tooltip_text; |
| 172 Update(); | 200 Update(); |
| 173 } | 201 } |
| 174 } | 202 } |
| 175 | 203 |
| 176 void TooltipManagerViews::Update() { | 204 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()) { | 205 if (tooltip_text_.empty()) { |
| 182 tooltip_widget_->Hide(); | 206 tooltip_widget_->Hide(); |
| 183 } else { | 207 } else { |
| 184 int max_width, line_count; | 208 int max_width, line_count; |
| 185 string16 tooltip_text(tooltip_text_); | 209 string16 tooltip_text(tooltip_text_); |
| 186 TrimTooltipToFit(&tooltip_text, &max_width, &line_count, | 210 TrimTooltipToFit(&tooltip_text, &max_width, &line_count, |
| 187 curr_mouse_pos_.x(), curr_mouse_pos_.y()); | 211 curr_mouse_pos_.x(), curr_mouse_pos_.y()); |
| 188 tooltip_label_.SetText(tooltip_text); | 212 tooltip_label_.SetText(tooltip_text); |
| 189 | 213 |
| 190 SetTooltipBounds(curr_mouse_pos_, max_width, | 214 SetTooltipBounds(curr_mouse_pos_, max_width, |
| 191 tooltip_label_.GetPreferredSize().height()); | 215 tooltip_label_.GetPreferredSize().height()); |
| 192 tooltip_widget_->Show(); | 216 tooltip_widget_->Show(); |
| 193 } | 217 } |
| 194 } | 218 } |
| 195 | 219 |
| 196 void TooltipManagerViews::SetTooltipBounds(gfx::Point mouse_pos, | 220 void TooltipManagerViews::SetTooltipBounds(gfx::Point mouse_pos, |
| 197 int tooltip_width, | 221 int tooltip_width, |
| 198 int tooltip_height) { | 222 int tooltip_height) { |
| 199 View::ConvertPointToScreen(root_view_, &mouse_pos); | |
| 200 gfx::Rect tooltip_rect(mouse_pos.x(), mouse_pos.y(), tooltip_width, | 223 gfx::Rect tooltip_rect(mouse_pos.x(), mouse_pos.y(), tooltip_width, |
| 201 tooltip_height); | 224 tooltip_height); |
| 202 | 225 |
| 203 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); | 226 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); |
| 204 gfx::Rect monitor_bounds = | 227 gfx::Rect monitor_bounds = |
| 205 gfx::Screen::GetMonitorAreaNearestPoint(tooltip_rect.origin()); | 228 gfx::Screen::GetMonitorAreaNearestPoint(tooltip_rect.origin()); |
| 206 tooltip_widget_->SetBounds(tooltip_rect.AdjustToFit(monitor_bounds)); | 229 tooltip_widget_->SetBounds(tooltip_rect.AdjustToFit(monitor_bounds)); |
| 207 } | 230 } |
| 208 | 231 |
| 209 Widget* TooltipManagerViews::CreateTooltip() { | 232 Widget* TooltipManagerViews::CreateTooltip() { |
| 210 Widget* widget = new Widget; | 233 Widget* widget = new Widget; |
| 211 Widget::InitParams params; | 234 Widget::InitParams params; |
| 235 // For aura, since we set the type to TOOLTIP_TYPE, the widget will get |
| 236 // auto-parented to the MenuAndTooltipsContainer. |
| 212 params.type = Widget::InitParams::TYPE_TOOLTIP; | 237 params.type = Widget::InitParams::TYPE_TOOLTIP; |
| 213 params.keep_on_top = true; | 238 params.keep_on_top = true; |
| 214 params.accept_events = false; | 239 params.accept_events = false; |
| 215 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 240 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 216 widget->Init(params); | 241 widget->Init(params); |
| 217 widget->SetOpacity(0x00); | 242 widget->SetOpacity(0xFF); |
| 218 return widget; | 243 return widget; |
| 219 } | 244 } |
| 220 | 245 |
| 221 void TooltipManagerViews::OnMouseMoved(int x, int y) { | 246 void TooltipManagerViews::OnMouseMoved(int x, int y) { |
| 222 if (tooltip_timer_.IsRunning()) | 247 if (tooltip_timer_.IsRunning()) |
| 223 tooltip_timer_.Reset(); | 248 tooltip_timer_.Reset(); |
| 224 curr_mouse_pos_.SetPoint(x, y); | 249 curr_mouse_pos_.SetPoint(x, y); |
| 225 | 250 |
| 226 // If tooltip is visible, we may want to hide it. If it is not, we are ok. | 251 // If tooltip is visible, we may want to hide it. If it is not, we are ok. |
| 227 if (tooltip_widget_->IsVisible()) | 252 if (tooltip_widget_->IsVisible()) |
| 228 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); | 253 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); |
| 229 } | 254 } |
| 230 | 255 |
| 231 } // namespace views | 256 } // namespace views |
| OLD | NEW |