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