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