summaryrefslogtreecommitdiffstats
path: root/ui/views/widget/tooltip_manager_views.cc
blob: fa3cb63750140b9f22146a741745f150c664310c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/widget/tooltip_manager_views.h"

#if defined(USE_X11)
#include <X11/Xlib.h>
#include <X11/extensions/XInput2.h>
#endif

#if defined(OS_WIN)
#include <windowsx.h>
#endif

#include "base/event_types.h"
#include "base/logging.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "third_party/skia/include/core/SkColor.h"
#if defined(USE_AURA)
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/event.h"
#include "ui/aura/window.h"
#endif
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/font.h"
#include "ui/gfx/screen.h"
#include "ui/views/events/event.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/widget/native_widget.h"
#include "views/background.h"
#include "views/border.h"
#include "views/view.h"

namespace {
SkColor kTooltipBackground = 0xFF7F7F00;
int kTooltipTimeoutMs = 500;

// FIXME: get cursor offset from actual cursor size.
int kCursorOffsetX = 10;
int kCursorOffsetY = 15;
}

namespace views {

// static
int TooltipManager::GetTooltipHeight() {
  // Not used for linux and chromeos.
  NOTREACHED();
  return 0;
}

// static
gfx::Font TooltipManager::GetDefaultFont() {
  return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont);
}

// static
int TooltipManager::GetMaxWidth(int x, int y) {
  // FIXME: change this. This is for now just copied from TooltipManagerGtk.

  // We always display the tooltip inside the root view. So the max width is
  // the width of the view.
  gfx::Rect monitor_bounds =
      gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y));
  // GtkLabel (gtk_label_ensure_layout) forces wrapping at this size. We mirror
  // the size here otherwise tooltips wider than the size used by gtklabel end
  // up with extraneous empty lines.
  return monitor_bounds.width() == 0 ? 800 : (monitor_bounds.width() + 1) / 2;
}

TooltipManagerViews::TooltipManagerViews(views::View* root_view)
    : root_view_(root_view),
      tooltip_view_(NULL) {
  tooltip_label_.set_background(
      views::Background::CreateSolidBackground(kTooltipBackground));
  tooltip_widget_.reset(CreateTooltip());
  tooltip_widget_->SetContentsView(&tooltip_label_);
  tooltip_widget_->Activate();
  tooltip_widget_->SetAlwaysOnTop(true);
}

TooltipManagerViews::~TooltipManagerViews() {
  tooltip_widget_->CloseNow();
}

void TooltipManagerViews::UpdateForMouseEvent(const MouseEvent& event) {
  switch (event.type()) {
    case ui::ET_MOUSE_EXITED:
      // Mouse is exiting this widget. Stop showing the tooltip and the timer.
      if (tooltip_timer_.IsRunning())
        tooltip_timer_.Stop();
      if (tooltip_widget_->IsVisible())
        tooltip_widget_->Hide();
      break;
    case ui::ET_MOUSE_ENTERED:
      // Mouse just entered this widget. Start the timer to show the tooltip.
      if (tooltip_timer_.IsRunning())
        tooltip_timer_.Stop();
      tooltip_timer_.Start(FROM_HERE,
          base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs),
          this, &TooltipManagerViews::TooltipTimerFired);
      break;
    case ui::ET_MOUSE_MOVED:
      OnMouseMoved(event.location().x(), event.location().y());
      break;
    case ui::ET_MOUSE_PRESSED:
    case ui::ET_MOUSE_RELEASED:
    case ui::ET_MOUSE_DRAGGED:
    case ui::ET_MOUSEWHEEL:
      // Hide the tooltip for click, release, drag, wheel events.
      if (tooltip_widget_->IsVisible())
        tooltip_widget_->Hide();
      break;
    default:
      NOTIMPLEMENTED();
  }
}

void TooltipManagerViews::UpdateTooltip() {
  UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false);
}

void TooltipManagerViews::TooltipTextChanged(View* view) {
  if (tooltip_widget_->IsVisible())
    UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false);
}

void TooltipManagerViews::ShowKeyboardTooltip(View* view) {
  NOTREACHED();
}

void TooltipManagerViews::HideKeyboardTooltip() {
  NOTREACHED();
}

void TooltipManagerViews::TooltipTimerFired() {
  UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false);
}

View* TooltipManagerViews::GetViewForTooltip(int x, int y, bool for_keyboard) {
  View* view = NULL;
  if (!for_keyboard) {
    // Convert x,y from screen coordinates to |root_view_| coordinates.
    gfx::Point point(x, y);
    View::ConvertPointFromWidget(root_view_, &point);
    view = root_view_->GetEventHandlerForPoint(point);
  } else {
    FocusManager* focus_manager = root_view_->GetFocusManager();
    if (focus_manager)
      view = focus_manager->GetFocusedView();
  }
  return view;
}

void TooltipManagerViews::UpdateIfRequired(int x, int y, bool for_keyboard) {
  View* view = GetViewForTooltip(x, y, for_keyboard);
  string16 tooltip_text;
  if (view)
    view->GetTooltipText(gfx::Point(x, y), &tooltip_text);

#if defined(USE_AURA)
  // In aura, and aura::Window can also have a tooltip. If the view doesnot have
  // a tooltip, we must also check for the aura::Window underneath the cursor.
  if (tooltip_text.empty()) {
    aura::Window* root = reinterpret_cast<aura::Window*>(
        root_view_->GetWidget()->GetNativeView());
    if (root) {
      aura::Window* window = root->GetEventHandlerForPoint(gfx::Point(x, y));
      if (window) {
        void* property = window->GetProperty(aura::kTooltipTextKey);
        if (property)
          tooltip_text = *reinterpret_cast<string16*>(property);
      }
    }
  }
#endif

  if (tooltip_view_ != view || tooltip_text_ != tooltip_text) {
    tooltip_view_ = view;
    tooltip_text_ = tooltip_text;
    Update();
  }
}

void TooltipManagerViews::Update() {
  if (tooltip_text_.empty()) {
    tooltip_widget_->Hide();
  } else {
    int max_width, line_count;
    string16 tooltip_text(tooltip_text_);
    TrimTooltipToFit(&tooltip_text, &max_width, &line_count,
                     curr_mouse_pos_.x(), curr_mouse_pos_.y());
    tooltip_label_.SetText(tooltip_text);

    SetTooltipBounds(curr_mouse_pos_, max_width,
                     tooltip_label_.GetPreferredSize().height());
    tooltip_widget_->Show();
  }
}

void TooltipManagerViews::SetTooltipBounds(gfx::Point mouse_pos,
                                           int tooltip_width,
                                           int tooltip_height) {
  gfx::Rect tooltip_rect(mouse_pos.x(), mouse_pos.y(), tooltip_width,
                         tooltip_height);

  tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY);
  gfx::Rect monitor_bounds =
      gfx::Screen::GetMonitorAreaNearestPoint(tooltip_rect.origin());
  tooltip_widget_->SetBounds(tooltip_rect.AdjustToFit(monitor_bounds));
}

Widget* TooltipManagerViews::CreateTooltip() {
  Widget* widget = new Widget;
  Widget::InitParams params;
  // For aura, since we set the type to TOOLTIP_TYPE, the widget will get
  // auto-parented to the MenuAndTooltipsContainer.
  params.type = Widget::InitParams::TYPE_TOOLTIP;
  params.keep_on_top = true;
  params.accept_events = false;
  params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  widget->Init(params);
  widget->SetOpacity(0xFF);
  return widget;
}

void TooltipManagerViews::OnMouseMoved(int x, int y) {
  if (tooltip_timer_.IsRunning())
    tooltip_timer_.Reset();
  curr_mouse_pos_.SetPoint(x, y);

  // If tooltip is visible, we may want to hide it. If it is not, we are ok.
  if (tooltip_widget_->IsVisible())
    UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false);
}

}  // namespace views