OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/views/widget/tooltip_manager_views.h" | |
6 | |
7 #if defined(USE_X11) | |
8 #include <X11/Xlib.h> | |
9 #include <X11/extensions/XInput2.h> | |
10 #endif | |
11 | |
12 #if defined(OS_WIN) | |
13 #include <windowsx.h> | |
14 #endif | |
15 | |
16 #include "base/event_types.h" | |
17 #include "base/logging.h" | |
18 #include "base/time.h" | |
19 #include "base/utf_string_conversions.h" | |
20 #include "third_party/skia/include/core/SkColor.h" | |
21 #if defined(USE_AURA) | |
22 #include "ui/aura/client/aura_constants.h" | |
23 #include "ui/aura/event.h" | |
24 #include "ui/aura/window.h" | |
25 #endif | |
26 #include "ui/base/resource/resource_bundle.h" | |
27 #include "ui/gfx/font.h" | |
28 #include "ui/gfx/screen.h" | |
29 #include "ui/views/background.h" | |
30 #include "ui/views/border.h" | |
31 #include "ui/views/events/event.h" | |
32 #include "ui/views/focus/focus_manager.h" | |
33 #include "ui/views/view.h" | |
34 #include "ui/views/widget/native_widget.h" | |
35 | |
36 namespace { | |
37 SkColor kTooltipBackground = 0xFF7F7F00; | |
38 int kTooltipTimeoutMs = 500; | |
39 | |
40 // FIXME: get cursor offset from actual cursor size. | |
41 int kCursorOffsetX = 10; | |
42 int kCursorOffsetY = 15; | |
43 } | |
44 | |
45 namespace views { | |
46 | |
47 // static | |
48 int TooltipManager::GetTooltipHeight() { | |
49 // Not used for linux and chromeos. | |
50 NOTREACHED(); | |
51 return 0; | |
52 } | |
53 | |
54 // static | |
55 gfx::Font TooltipManager::GetDefaultFont() { | |
56 return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); | |
57 } | |
58 | |
59 // static | |
60 int TooltipManager::GetMaxWidth(int x, int y) { | |
61 // FIXME: change this. This is for now just copied from TooltipManagerGtk. | |
62 | |
63 // We always display the tooltip inside the root view. So the max width is | |
64 // the width of the view. | |
65 gfx::Rect monitor_bounds = | |
66 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y)); | |
67 // GtkLabel (gtk_label_ensure_layout) forces wrapping at this size. We mirror | |
68 // the size here otherwise tooltips wider than the size used by gtklabel end | |
69 // up with extraneous empty lines. | |
70 return monitor_bounds.width() == 0 ? 800 : (monitor_bounds.width() + 1) / 2; | |
71 } | |
72 | |
73 TooltipManagerViews::TooltipManagerViews(views::View* root_view) | |
74 : root_view_(root_view), | |
75 tooltip_view_(NULL) { | |
76 tooltip_label_.set_background( | |
77 views::Background::CreateSolidBackground(kTooltipBackground)); | |
78 tooltip_widget_.reset(CreateTooltip()); | |
79 tooltip_widget_->SetContentsView(&tooltip_label_); | |
80 tooltip_widget_->Activate(); | |
81 tooltip_widget_->SetAlwaysOnTop(true); | |
82 } | |
83 | |
84 TooltipManagerViews::~TooltipManagerViews() { | |
85 tooltip_widget_->CloseNow(); | |
86 } | |
87 | |
88 void TooltipManagerViews::UpdateForMouseEvent(const MouseEvent& event) { | |
89 switch (event.type()) { | |
90 case ui::ET_MOUSE_EXITED: | |
91 // Mouse is exiting this widget. Stop showing the tooltip and the timer. | |
92 if (tooltip_timer_.IsRunning()) | |
93 tooltip_timer_.Stop(); | |
94 if (tooltip_widget_->IsVisible()) | |
95 tooltip_widget_->Hide(); | |
96 break; | |
97 case ui::ET_MOUSE_ENTERED: | |
98 // Mouse just entered this widget. Start the timer to show the tooltip. | |
99 if (tooltip_timer_.IsRunning()) | |
100 tooltip_timer_.Stop(); | |
101 tooltip_timer_.Start(FROM_HERE, | |
102 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), | |
103 this, &TooltipManagerViews::TooltipTimerFired); | |
104 break; | |
105 case ui::ET_MOUSE_MOVED: | |
106 OnMouseMoved(event.location().x(), event.location().y()); | |
107 break; | |
108 case ui::ET_MOUSE_PRESSED: | |
109 case ui::ET_MOUSE_RELEASED: | |
110 case ui::ET_MOUSE_DRAGGED: | |
111 case ui::ET_MOUSEWHEEL: | |
112 // Hide the tooltip for click, release, drag, wheel events. | |
113 if (tooltip_widget_->IsVisible()) | |
114 tooltip_widget_->Hide(); | |
115 break; | |
116 default: | |
117 NOTIMPLEMENTED(); | |
118 } | |
119 } | |
120 | |
121 void TooltipManagerViews::UpdateTooltip() { | |
122 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); | |
123 } | |
124 | |
125 void TooltipManagerViews::TooltipTextChanged(View* view) { | |
126 if (tooltip_widget_->IsVisible()) | |
127 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); | |
128 } | |
129 | |
130 void TooltipManagerViews::ShowKeyboardTooltip(View* view) { | |
131 NOTREACHED(); | |
132 } | |
133 | |
134 void TooltipManagerViews::HideKeyboardTooltip() { | |
135 NOTREACHED(); | |
136 } | |
137 | |
138 void TooltipManagerViews::TooltipTimerFired() { | |
139 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); | |
140 } | |
141 | |
142 View* TooltipManagerViews::GetViewForTooltip(int x, int y, bool for_keyboard) { | |
143 View* view = NULL; | |
144 if (!for_keyboard) { | |
145 // Convert x,y from screen coordinates to |root_view_| coordinates. | |
146 gfx::Point point(x, y); | |
147 View::ConvertPointFromWidget(root_view_, &point); | |
148 view = root_view_->GetEventHandlerForPoint(point); | |
149 } else { | |
150 FocusManager* focus_manager = root_view_->GetFocusManager(); | |
151 if (focus_manager) | |
152 view = focus_manager->GetFocusedView(); | |
153 } | |
154 return view; | |
155 } | |
156 | |
157 void TooltipManagerViews::UpdateIfRequired(int x, int y, bool for_keyboard) { | |
158 View* view = GetViewForTooltip(x, y, for_keyboard); | |
159 string16 tooltip_text; | |
160 if (view) | |
161 view->GetTooltipText(gfx::Point(x, y), &tooltip_text); | |
162 | |
163 #if defined(USE_AURA) | |
164 // In aura, and aura::Window can also have a tooltip. If the view doesnot have | |
165 // a tooltip, we must also check for the aura::Window underneath the cursor. | |
166 if (tooltip_text.empty()) { | |
167 aura::Window* root = reinterpret_cast<aura::Window*>( | |
168 root_view_->GetWidget()->GetNativeView()); | |
169 if (root) { | |
170 aura::Window* window = root->GetEventHandlerForPoint(gfx::Point(x, y)); | |
171 if (window) { | |
172 void* property = window->GetProperty(aura::kTooltipTextKey); | |
173 if (property) | |
174 tooltip_text = *reinterpret_cast<string16*>(property); | |
175 } | |
176 } | |
177 } | |
178 #endif | |
179 | |
180 if (tooltip_view_ != view || tooltip_text_ != tooltip_text) { | |
181 tooltip_view_ = view; | |
182 tooltip_text_ = tooltip_text; | |
183 Update(); | |
184 } | |
185 } | |
186 | |
187 void TooltipManagerViews::Update() { | |
188 if (tooltip_text_.empty()) { | |
189 tooltip_widget_->Hide(); | |
190 } else { | |
191 int max_width, line_count; | |
192 string16 tooltip_text(tooltip_text_); | |
193 TrimTooltipToFit(&tooltip_text, &max_width, &line_count, | |
194 curr_mouse_pos_.x(), curr_mouse_pos_.y()); | |
195 tooltip_label_.SetText(tooltip_text); | |
196 | |
197 SetTooltipBounds(curr_mouse_pos_, max_width, | |
198 tooltip_label_.GetPreferredSize().height()); | |
199 tooltip_widget_->Show(); | |
200 } | |
201 } | |
202 | |
203 void TooltipManagerViews::SetTooltipBounds(gfx::Point mouse_pos, | |
204 int tooltip_width, | |
205 int tooltip_height) { | |
206 gfx::Rect tooltip_rect(mouse_pos.x(), mouse_pos.y(), tooltip_width, | |
207 tooltip_height); | |
208 | |
209 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); | |
210 gfx::Rect monitor_bounds = | |
211 gfx::Screen::GetMonitorAreaNearestPoint(tooltip_rect.origin()); | |
212 tooltip_widget_->SetBounds(tooltip_rect.AdjustToFit(monitor_bounds)); | |
213 } | |
214 | |
215 Widget* TooltipManagerViews::CreateTooltip() { | |
216 Widget* widget = new Widget; | |
217 Widget::InitParams params; | |
218 // For aura, since we set the type to TOOLTIP_TYPE, the widget will get | |
219 // auto-parented to the MenuAndTooltipsContainer. | |
220 params.type = Widget::InitParams::TYPE_TOOLTIP; | |
221 params.keep_on_top = true; | |
222 params.accept_events = false; | |
223 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
224 widget->Init(params); | |
225 widget->SetOpacity(0xFF); | |
226 return widget; | |
227 } | |
228 | |
229 void TooltipManagerViews::OnMouseMoved(int x, int y) { | |
230 if (tooltip_timer_.IsRunning()) | |
231 tooltip_timer_.Reset(); | |
232 curr_mouse_pos_.SetPoint(x, y); | |
233 | |
234 // If tooltip is visible, we may want to hide it. If it is not, we are ok. | |
235 if (tooltip_widget_->IsVisible()) | |
236 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); | |
237 } | |
238 | |
239 } // namespace views | |
OLD | NEW |