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/widget/tooltip_manager_aura.h"
6
7 #include "base/logging.h"
8 #include "ui/aura/client/screen_position_client.h"
9 #include "ui/aura/window_event_dispatcher.h"
10 #include "ui/aura/window_tree_host.h"
11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/gfx/rect.h"
13 #include "ui/gfx/screen.h"
14 #include "ui/views/widget/widget.h"
15 #include "ui/wm/public/tooltip_client.h"
16
17 namespace views {
18
19 // static
GetTooltipHeight()20 int TooltipManager::GetTooltipHeight() {
21 // Not used for linux and chromeos.
22 NOTIMPLEMENTED();
23 return 0;
24 }
25
26 ////////////////////////////////////////////////////////////////////////////////
27 // TooltipManagerAura public:
28
TooltipManagerAura(Widget * widget)29 TooltipManagerAura::TooltipManagerAura(Widget* widget) : widget_(widget) {
30 aura::client::SetTooltipText(GetWindow(), &tooltip_text_);
31 }
32
~TooltipManagerAura()33 TooltipManagerAura::~TooltipManagerAura() {
34 aura::client::SetTooltipText(GetWindow(), NULL);
35 }
36
37 // static
GetDefaultFontList()38 const gfx::FontList& TooltipManagerAura::GetDefaultFontList() {
39 return ui::ResourceBundle::GetSharedInstance().GetFontList(
40 ui::ResourceBundle::BaseFont);
41 }
42
43 // static
UpdateTooltipManagerForCapture(Widget * source)44 void TooltipManagerAura::UpdateTooltipManagerForCapture(Widget* source) {
45 if (!source->HasCapture())
46 return;
47
48 aura::Window* root_window = source->GetNativeView()->GetRootWindow();
49 if (!root_window)
50 return;
51
52 gfx::Point screen_loc(
53 root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot());
54 aura::client::ScreenPositionClient* screen_position_client =
55 aura::client::GetScreenPositionClient(root_window);
56 if (!screen_position_client)
57 return;
58 screen_position_client->ConvertPointToScreen(root_window, &screen_loc);
59 gfx::Screen* screen = gfx::Screen::GetScreenFor(root_window);
60 aura::Window* target = screen->GetWindowAtScreenPoint(screen_loc);
61 if (!target)
62 return;
63 gfx::Point target_loc(screen_loc);
64 screen_position_client =
65 aura::client::GetScreenPositionClient(target->GetRootWindow());
66 if (!screen_position_client)
67 return;
68 screen_position_client->ConvertPointFromScreen(target, &target_loc);
69 target = target->GetEventHandlerForPoint(target_loc);
70 while (target) {
71 Widget* target_widget = Widget::GetWidgetForNativeView(target);
72 if (target_widget == source)
73 return;
74
75 if (target_widget) {
76 if (target_widget->GetTooltipManager())
77 target_widget->GetTooltipManager()->UpdateTooltip();
78 return;
79 }
80 target = target->parent();
81 }
82 }
83
84 ////////////////////////////////////////////////////////////////////////////////
85 // TooltipManagerAura, TooltipManager implementation:
86
GetFontList() const87 const gfx::FontList& TooltipManagerAura::GetFontList() const {
88 return GetDefaultFontList();
89 }
90
UpdateTooltip()91 void TooltipManagerAura::UpdateTooltip() {
92 aura::Window* root_window = GetWindow()->GetRootWindow();
93 if (aura::client::GetTooltipClient(root_window)) {
94 gfx::Point view_point =
95 root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot();
96 aura::Window::ConvertPointToTarget(root_window, GetWindow(), &view_point);
97 View* view = GetViewUnderPoint(view_point);
98 UpdateTooltipForTarget(view, view_point, root_window);
99 }
100 }
101
TooltipTextChanged(View * view)102 void TooltipManagerAura::TooltipTextChanged(View* view) {
103 aura::Window* root_window = GetWindow()->GetRootWindow();
104 if (aura::client::GetTooltipClient(root_window)) {
105 gfx::Point view_point =
106 root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot();
107 aura::Window::ConvertPointToTarget(root_window, GetWindow(), &view_point);
108 View* target = GetViewUnderPoint(view_point);
109 if (target != view)
110 return;
111 UpdateTooltipForTarget(view, view_point, root_window);
112 }
113 }
114
GetViewUnderPoint(const gfx::Point & point)115 View* TooltipManagerAura::GetViewUnderPoint(const gfx::Point& point) {
116 View* root_view = widget_->GetRootView();
117 if (root_view)
118 return root_view->GetTooltipHandlerForPoint(point);
119 return NULL;
120 }
121
UpdateTooltipForTarget(View * target,const gfx::Point & point,aura::Window * root_window)122 void TooltipManagerAura::UpdateTooltipForTarget(View* target,
123 const gfx::Point& point,
124 aura::Window* root_window) {
125 if (target) {
126 gfx::Point view_point = point;
127 View::ConvertPointFromWidget(target, &view_point);
128 base::string16 new_tooltip_text;
129 if (!target->GetTooltipText(view_point, &new_tooltip_text))
130 tooltip_text_.clear();
131 else
132 tooltip_text_ = new_tooltip_text;
133 } else {
134 tooltip_text_.clear();
135 }
136
137 aura::client::SetTooltipId(GetWindow(), target);
138
139 aura::client::GetTooltipClient(root_window)->UpdateTooltip(GetWindow());
140 }
141
GetWindow()142 aura::Window* TooltipManagerAura::GetWindow() {
143 return widget_->GetNativeView();
144 }
145
146 } // namespace views.
147