Use int64 time stamp when storing metadata to the HTTP cache.
[chromium-blink-merge.git] / ui / views / corewm / tooltip_win.cc
blob46635d2ba5d137b818eabf946bd582bb6a8b82e4
1 // Copyright 2013 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.
5 #include "ui/views/corewm/tooltip_win.h"
7 #include <winuser.h>
9 #include "base/debug/stack_trace.h"
10 #include "base/i18n/rtl.h"
11 #include "base/logging.h"
12 #include "ui/base/l10n/l10n_util_win.h"
13 #include "ui/gfx/geometry/rect.h"
14 #include "ui/gfx/screen.h"
15 #include "ui/gfx/win/dpi.h"
16 #include "ui/views/corewm/cursor_height_provider_win.h"
18 namespace views {
19 namespace corewm {
21 TooltipWin::TooltipWin(HWND parent)
22 : parent_hwnd_(parent),
23 tooltip_hwnd_(NULL),
24 showing_(false) {
25 memset(&toolinfo_, 0, sizeof(toolinfo_));
26 toolinfo_.cbSize = sizeof(toolinfo_);
27 toolinfo_.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_ABSOLUTE;
28 toolinfo_.uId = reinterpret_cast<UINT_PTR>(parent_hwnd_);
29 toolinfo_.hwnd = parent_hwnd_;
30 toolinfo_.lpszText = NULL;
31 toolinfo_.lpReserved = NULL;
32 SetRectEmpty(&toolinfo_.rect);
35 TooltipWin::~TooltipWin() {
36 if (tooltip_hwnd_)
37 DestroyWindow(tooltip_hwnd_);
40 bool TooltipWin::HandleNotify(int w_param, NMHDR* l_param, LRESULT* l_result) {
41 if (tooltip_hwnd_ == NULL)
42 return false;
44 switch (l_param->code) {
45 case TTN_POP:
46 showing_ = false;
47 return true;
48 case TTN_SHOW:
49 *l_result = TRUE;
50 PositionTooltip();
51 showing_ = true;
52 return true;
53 default:
54 break;
56 return false;
59 bool TooltipWin::EnsureTooltipWindow() {
60 if (tooltip_hwnd_)
61 return true;
63 tooltip_hwnd_ = CreateWindowEx(
64 WS_EX_TRANSPARENT | l10n_util::GetExtendedTooltipStyles(),
65 TOOLTIPS_CLASS, NULL, TTS_NOPREFIX | WS_POPUP, 0, 0, 0, 0,
66 parent_hwnd_, NULL, NULL, NULL);
67 if (!tooltip_hwnd_) {
68 PLOG(WARNING) << "tooltip creation failed, disabling tooltips";
69 return false;
72 l10n_util::AdjustUIFontForWindow(tooltip_hwnd_);
74 SendMessage(tooltip_hwnd_, TTM_ADDTOOL, 0,
75 reinterpret_cast<LPARAM>(&toolinfo_));
76 return true;
79 void TooltipWin::PositionTooltip() {
80 // This code only runs for non-metro, so GetNativeScreen() is fine.
81 gfx::Point screen_point = gfx::win::DIPToScreenPoint(location_);
82 const int cursoroffset = GetCurrentCursorVisibleHeight();
83 screen_point.Offset(0, cursoroffset);
85 DWORD tooltip_size = SendMessage(tooltip_hwnd_, TTM_GETBUBBLESIZE, 0,
86 reinterpret_cast<LPARAM>(&toolinfo_));
87 const gfx::Size size(LOWORD(tooltip_size), HIWORD(tooltip_size));
89 const gfx::Display display(
90 gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint(screen_point));
92 gfx::Rect tooltip_bounds(screen_point, size);
93 tooltip_bounds.AdjustToFit(gfx::win::DIPToScreenRect(display.work_area()));
94 SetWindowPos(tooltip_hwnd_, NULL, tooltip_bounds.x(), tooltip_bounds.y(), 0,
95 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
98 int TooltipWin::GetMaxWidth(const gfx::Point& location,
99 aura::Window* context) const {
100 // This code only runs for non-metro, so GetNativeScreen() is fine.
101 const gfx::Point screen_point = gfx::win::DIPToScreenPoint(location);
102 gfx::Display display(
103 gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint(screen_point));
104 const gfx::Rect monitor_bounds = display.bounds();
105 return (monitor_bounds.width() + 1) / 2;
108 void TooltipWin::SetText(aura::Window* window,
109 const base::string16& tooltip_text,
110 const gfx::Point& location) {
111 if (!EnsureTooltipWindow())
112 return;
114 // See comment in header for details on why |location_| is needed.
115 location_ = location;
117 // Without this we get a flicker of the tooltip appearing at 0x0. Not sure
118 // why.
119 SetWindowPos(tooltip_hwnd_, NULL, 0, 0, 0, 0,
120 SWP_HIDEWINDOW | SWP_NOACTIVATE | SWP_NOMOVE |
121 SWP_NOREPOSITION | SWP_NOSIZE | SWP_NOZORDER);
123 base::string16 adjusted_text(tooltip_text);
124 base::i18n::AdjustStringForLocaleDirection(&adjusted_text);
125 toolinfo_.lpszText = const_cast<WCHAR*>(adjusted_text.c_str());
126 SendMessage(tooltip_hwnd_, TTM_SETTOOLINFO, 0,
127 reinterpret_cast<LPARAM>(&toolinfo_));
129 int max_width = GetMaxWidth(location_, window);
130 SendMessage(tooltip_hwnd_, TTM_SETMAXTIPWIDTH, 0, max_width);
133 void TooltipWin::Show() {
134 if (!EnsureTooltipWindow())
135 return;
137 SendMessage(tooltip_hwnd_, TTM_TRACKACTIVATE,
138 TRUE, reinterpret_cast<LPARAM>(&toolinfo_));
139 SetWindowPos(tooltip_hwnd_, HWND_TOPMOST, 0, 0, 0, 0,
140 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOSIZE);
143 void TooltipWin::Hide() {
144 if (!tooltip_hwnd_)
145 return;
147 SendMessage(tooltip_hwnd_, TTM_TRACKACTIVATE, FALSE,
148 reinterpret_cast<LPARAM>(&toolinfo_));
151 bool TooltipWin::IsVisible() {
152 return showing_;
155 } // namespace corewm
156 } // namespace views