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 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 gfx::Rect monitor_bounds = | 68 gfx::Rect monitor_bounds = |
69 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y)); | 69 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y)); |
70 // 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 |
71 // 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 |
72 // up with extraneous empty lines. | 72 // up with extraneous empty lines. |
73 return monitor_bounds.width() == 0 ? 800 : (monitor_bounds.width() + 1) / 2; | 73 return monitor_bounds.width() == 0 ? 800 : (monitor_bounds.width() + 1) / 2; |
74 } | 74 } |
75 | 75 |
76 TooltipManagerViews::TooltipManagerViews(views::View* root_view) | 76 TooltipManagerViews::TooltipManagerViews(views::View* root_view) |
77 : root_view_(root_view), | 77 : root_view_(root_view), |
78 tooltip_view_(NULL), | 78 tooltip_view_(NULL) { |
79 widget_closed_(false) { | |
80 tooltip_label_.set_background( | 79 tooltip_label_.set_background( |
81 views::Background::CreateSolidBackground(kTooltipBackground)); | 80 views::Background::CreateSolidBackground(kTooltipBackground)); |
82 tooltip_widget_.reset(CreateTooltip()); | 81 tooltip_widget_.reset(CreateTooltip()); |
83 tooltip_widget_->SetContentsView(&tooltip_label_); | 82 tooltip_widget_->SetContentsView(&tooltip_label_); |
84 tooltip_widget_->Activate(); | 83 tooltip_widget_->Activate(); |
85 tooltip_widget_->SetAlwaysOnTop(true); | 84 tooltip_widget_->SetAlwaysOnTop(true); |
86 tooltip_timer_.Start(FROM_HERE, | 85 tooltip_timer_.Start(FROM_HERE, |
87 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), | 86 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), |
88 this, &TooltipManagerViews::TooltipTimerFired); | 87 this, &TooltipManagerViews::TooltipTimerFired); |
89 MessageLoopForUI::current()->AddObserver(this); | 88 MessageLoopForUI::current()->AddObserver(this); |
90 root_view_->GetWidget()->AddObserver(this); | |
91 } | 89 } |
92 | 90 |
93 TooltipManagerViews::~TooltipManagerViews() { | 91 TooltipManagerViews::~TooltipManagerViews() { |
94 MessageLoopForUI::current()->RemoveObserver(this); | 92 MessageLoopForUI::current()->RemoveObserver(this); |
95 tooltip_widget_->CloseNow(); | 93 tooltip_widget_->CloseNow(); |
96 } | 94 } |
97 | 95 |
98 void TooltipManagerViews::UpdateTooltip() { | 96 void TooltipManagerViews::UpdateTooltip() { |
99 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); | 97 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); |
100 } | 98 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 const base::NativeEvent& event) { | 143 const base::NativeEvent& event) { |
146 if (event.message == WM_MOUSEMOVE) | 144 if (event.message == WM_MOUSEMOVE) |
147 OnMouseMoved(GET_X_LPARAM(event.lParam), GET_Y_LPARAM(event.lParam)); | 145 OnMouseMoved(GET_X_LPARAM(event.lParam), GET_Y_LPARAM(event.lParam)); |
148 return base::EVENT_CONTINUE; | 146 return base::EVENT_CONTINUE; |
149 } | 147 } |
150 | 148 |
151 void TooltipManagerViews::DidProcessEvent(const base::NativeEvent& event) { | 149 void TooltipManagerViews::DidProcessEvent(const base::NativeEvent& event) { |
152 } | 150 } |
153 #endif | 151 #endif |
154 | 152 |
155 void TooltipManagerViews::OnWidgetClosing(Widget* widget) { | |
156 tooltip_timer_.Stop(); | |
157 widget_closed_ = true; | |
158 } | |
159 | |
160 void TooltipManagerViews::TooltipTimerFired() { | 153 void TooltipManagerViews::TooltipTimerFired() { |
161 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); | 154 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); |
162 } | 155 } |
163 | 156 |
164 View* TooltipManagerViews::GetViewForTooltip(int x, int y, bool for_keyboard) { | 157 View* TooltipManagerViews::GetViewForTooltip(int x, int y, bool for_keyboard) { |
165 View* view = NULL; | 158 View* view = NULL; |
166 if (!for_keyboard) { | 159 if (!for_keyboard) { |
167 // Convert x,y from screen coordinates to |root_view_| coordinates. | 160 // Convert x,y from screen coordinates to |root_view_| coordinates. |
168 gfx::Point point(x, y); | 161 gfx::Point point(x, y); |
169 gfx::Rect r = root_view_->GetWidget()->GetClientAreaScreenBounds(); | 162 gfx::Rect r = root_view_->GetWidget()->GetClientAreaScreenBounds(); |
170 point.SetPoint(point.x() - r.x(), point.y() - r.y()); | 163 point.SetPoint(point.x() - r.x(), point.y() - r.y()); |
171 View::ConvertPointFromWidget(root_view_, &point); | 164 View::ConvertPointFromWidget(root_view_, &point); |
172 view = root_view_->GetEventHandlerForPoint(point); | 165 view = root_view_->GetEventHandlerForPoint(point); |
173 } else { | 166 } else { |
174 FocusManager* focus_manager = root_view_->GetFocusManager(); | 167 FocusManager* focus_manager = root_view_->GetFocusManager(); |
175 if (focus_manager) | 168 if (focus_manager) |
176 view = focus_manager->GetFocusedView(); | 169 view = focus_manager->GetFocusedView(); |
177 } | 170 } |
178 return view; | 171 return view; |
179 } | 172 } |
180 | 173 |
181 void TooltipManagerViews::UpdateIfRequired(int x, int y, bool for_keyboard) { | 174 void TooltipManagerViews::UpdateIfRequired(int x, int y, bool for_keyboard) { |
182 if (widget_closed_) | |
183 return; | |
184 | |
185 View* view = GetViewForTooltip(x, y, for_keyboard); | 175 View* view = GetViewForTooltip(x, y, for_keyboard); |
186 string16 tooltip_text; | 176 string16 tooltip_text; |
187 if (view) | 177 if (view) |
188 view->GetTooltipText(gfx::Point(x, y), &tooltip_text); | 178 view->GetTooltipText(gfx::Point(x, y), &tooltip_text); |
189 | 179 |
190 #if defined(USE_AURA) | 180 #if defined(USE_AURA) |
191 // In aura, and aura::Window can also have a tooltip. If the view doesnot have | 181 // In aura, and aura::Window can also have a tooltip. If the view doesnot have |
192 // a tooltip, we must also check for the aura::Window underneath the cursor. | 182 // a tooltip, we must also check for the aura::Window underneath the cursor. |
193 if (tooltip_text.empty()) { | 183 if (tooltip_text.empty()) { |
194 aura::Window* root = reinterpret_cast<aura::Window*>( | 184 aura::Window* root = reinterpret_cast<aura::Window*>( |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 if (tooltip_timer_.IsRunning()) | 247 if (tooltip_timer_.IsRunning()) |
258 tooltip_timer_.Reset(); | 248 tooltip_timer_.Reset(); |
259 curr_mouse_pos_.SetPoint(x, y); | 249 curr_mouse_pos_.SetPoint(x, y); |
260 | 250 |
261 // 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. |
262 if (tooltip_widget_->IsVisible()) | 252 if (tooltip_widget_->IsVisible()) |
263 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); | 253 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); |
264 } | 254 } |
265 | 255 |
266 } // namespace views | 256 } // namespace views |
OLD | NEW |