OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "ui/views/corewm/tooltip_win.h" | 5 #include "ui/views/corewm/tooltip_win.h" |
6 | 6 |
7 #include <winuser.h> | 7 #include <winuser.h> |
8 | 8 |
9 #include "base/debug/stack_trace.h" | 9 #include "base/debug/stack_trace.h" |
10 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "ui/base/l10n/l10n_util_win.h" | 12 #include "ui/base/l10n/l10n_util_win.h" |
13 #include "ui/gfx/rect.h" | 13 #include "ui/gfx/rect.h" |
14 #include "ui/gfx/screen.h" | 14 #include "ui/gfx/screen.h" |
15 #include "ui/gfx/win/dpi.h" | 15 #include "ui/gfx/win/dpi.h" |
16 | 16 |
17 | |
18 namespace views { | 17 namespace views { |
19 namespace corewm { | 18 namespace corewm { |
20 | 19 |
21 TooltipWin::TooltipWin(HWND parent) | 20 TooltipWin::TooltipWin(HWND parent) |
22 : parent_hwnd_(parent), | 21 : parent_hwnd_(parent), |
23 tooltip_hwnd_(NULL), | 22 tooltip_hwnd_(NULL), |
24 showing_(false) { | 23 showing_(false) { |
25 memset(&toolinfo_, 0, sizeof(toolinfo_)); | 24 memset(&toolinfo_, 0, sizeof(toolinfo_)); |
26 toolinfo_.cbSize = sizeof(toolinfo_); | 25 toolinfo_.cbSize = sizeof(toolinfo_); |
27 toolinfo_.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_ABSOLUTE; | 26 toolinfo_.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_ABSOLUTE; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 | 70 |
72 l10n_util::AdjustUIFontForWindow(tooltip_hwnd_); | 71 l10n_util::AdjustUIFontForWindow(tooltip_hwnd_); |
73 | 72 |
74 SendMessage(tooltip_hwnd_, TTM_ADDTOOL, 0, | 73 SendMessage(tooltip_hwnd_, TTM_ADDTOOL, 0, |
75 reinterpret_cast<LPARAM>(&toolinfo_)); | 74 reinterpret_cast<LPARAM>(&toolinfo_)); |
76 return true; | 75 return true; |
77 } | 76 } |
78 | 77 |
79 void TooltipWin::PositionTooltip() { | 78 void TooltipWin::PositionTooltip() { |
80 // This code only runs for non-metro, so GetNativeScreen() is fine. | 79 // This code only runs for non-metro, so GetNativeScreen() is fine. |
| 80 gfx::Point screen_point = gfx::win::DIPToScreenPoint(location_); |
81 gfx::Display display( | 81 gfx::Display display( |
82 gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint(location_)); | 82 gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint(screen_point)); |
83 | 83 |
84 DWORD tooltip_size = SendMessage(tooltip_hwnd_, TTM_GETBUBBLESIZE, 0, | 84 DWORD tooltip_size = SendMessage(tooltip_hwnd_, TTM_GETBUBBLESIZE, 0, |
85 reinterpret_cast<LPARAM>(&toolinfo_)); | 85 reinterpret_cast<LPARAM>(&toolinfo_)); |
86 // 20 accounts for visible cursor size. I tried using SM_CYCURSOR but that's | 86 gfx::Size size(LOWORD(tooltip_size), HIWORD(tooltip_size)); |
87 // way too big (32 on win7 default). | 87 size = gfx::win::DIPToScreenSize(size); |
88 // TODO(sky): figure out the right way to determine offset. | |
89 const int initial_y = location_.y(); | |
90 gfx::Rect tooltip_bounds(location_.x(), initial_y + 20, | |
91 LOWORD(tooltip_size), HIWORD(tooltip_size)); | |
92 tooltip_bounds.AdjustToFit(display.work_area()); | |
93 if (tooltip_bounds.y() < initial_y) | |
94 tooltip_bounds.set_y(initial_y - tooltip_bounds.height() - 2); | |
95 | 88 |
96 // Convert the tooltip bounds to pixel coordinates. SetWindowPos works in | 89 const int cursoroffset = cursor_height_provider_.GetCursorHeight(); |
97 // pixel coordinates. | 90 screen_point.Offset(0, cursoroffset); |
98 tooltip_bounds = gfx::win::DIPToScreenRect(tooltip_bounds); | 91 gfx::Rect tooltip_bounds(screen_point, size); |
| 92 tooltip_bounds.AdjustToFit(gfx::win::DIPToScreenRect(display.work_area())); |
99 SetWindowPos(tooltip_hwnd_, NULL, tooltip_bounds.x(), tooltip_bounds.y(), 0, | 93 SetWindowPos(tooltip_hwnd_, NULL, tooltip_bounds.x(), tooltip_bounds.y(), 0, |
100 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE); | 94 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE); |
101 } | 95 } |
102 | 96 |
103 void TooltipWin::SetText(aura::Window* window, | 97 void TooltipWin::SetText(aura::Window* window, |
104 const base::string16& tooltip_text, | 98 const base::string16& tooltip_text, |
105 const gfx::Point& location) { | 99 const gfx::Point& location) { |
106 if (!EnsureTooltipWindow()) | 100 if (!EnsureTooltipWindow()) |
107 return; | 101 return; |
108 | 102 |
109 // See comment in header for details on why |location_| is needed. | 103 // See comment in header for details on why |location_| is needed. |
110 location_ = location; | 104 location_ = location; |
111 | 105 |
112 // Without this we get a flicker of the tooltip appearing at 0x0. Not sure | 106 // Without this we get a flicker of the tooltip appearing at 0x0. Not sure |
113 // why. | 107 // why. |
114 SetWindowPos(tooltip_hwnd_, NULL, 0, 0, 0, 0, | 108 SetWindowPos(tooltip_hwnd_, NULL, 0, 0, 0, 0, |
115 SWP_HIDEWINDOW | SWP_NOACTIVATE | SWP_NOMOVE | | 109 SWP_HIDEWINDOW | SWP_NOACTIVATE | SWP_NOMOVE | |
116 SWP_NOREPOSITION | SWP_NOSIZE | SWP_NOZORDER); | 110 SWP_NOREPOSITION | SWP_NOSIZE | SWP_NOZORDER); |
117 | 111 |
118 base::string16 adjusted_text(tooltip_text); | 112 base::string16 adjusted_text(tooltip_text); |
119 base::i18n::AdjustStringForLocaleDirection(&adjusted_text); | 113 base::i18n::AdjustStringForLocaleDirection(&adjusted_text); |
120 toolinfo_.lpszText = const_cast<WCHAR*>(adjusted_text.c_str()); | 114 toolinfo_.lpszText = const_cast<WCHAR*>(adjusted_text.c_str()); |
121 SendMessage(tooltip_hwnd_, TTM_SETTOOLINFO, 0, | 115 SendMessage(tooltip_hwnd_, TTM_SETTOOLINFO, 0, |
122 reinterpret_cast<LPARAM>(&toolinfo_)); | 116 reinterpret_cast<LPARAM>(&toolinfo_)); |
123 | 117 |
124 // This code only runs for non-metro, so GetNativeScreen() is fine. | 118 // This code only runs for non-metro, so GetNativeScreen() is fine. |
| 119 const gfx::Point screen_point = gfx::win::DIPToScreenPoint(location_); |
125 gfx::Display display( | 120 gfx::Display display( |
126 gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint(location_)); | 121 gfx::Screen::GetNativeScreen()->GetDisplayNearestPoint(screen_point)); |
127 const gfx::Rect monitor_bounds = display.bounds(); | 122 const gfx::Rect monitor_bounds = display.bounds(); |
128 int max_width = (monitor_bounds.width() + 1) / 2; | 123 int max_width = (monitor_bounds.width() + 1) / 2; |
129 SendMessage(tooltip_hwnd_, TTM_SETMAXTIPWIDTH, 0, max_width); | 124 SendMessage(tooltip_hwnd_, TTM_SETMAXTIPWIDTH, 0, max_width); |
130 } | 125 } |
131 | 126 |
132 void TooltipWin::Show() { | 127 void TooltipWin::Show() { |
133 if (!EnsureTooltipWindow()) | 128 if (!EnsureTooltipWindow()) |
134 return; | 129 return; |
135 | 130 |
136 SendMessage(tooltip_hwnd_, TTM_TRACKACTIVATE, | 131 SendMessage(tooltip_hwnd_, TTM_TRACKACTIVATE, |
137 TRUE, reinterpret_cast<LPARAM>(&toolinfo_)); | 132 TRUE, reinterpret_cast<LPARAM>(&toolinfo_)); |
138 SetWindowPos(tooltip_hwnd_, HWND_TOPMOST, 0, 0, 0, 0, | 133 SetWindowPos(tooltip_hwnd_, HWND_TOPMOST, 0, 0, 0, 0, |
139 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOSIZE); | 134 SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOSIZE); |
140 } | 135 } |
141 | 136 |
142 void TooltipWin::Hide() { | 137 void TooltipWin::Hide() { |
143 if (!tooltip_hwnd_) | 138 if (!tooltip_hwnd_) |
144 return; | 139 return; |
145 | 140 |
146 SendMessage(tooltip_hwnd_, TTM_TRACKACTIVATE, FALSE, | 141 SendMessage(tooltip_hwnd_, TTM_TRACKACTIVATE, FALSE, |
147 reinterpret_cast<LPARAM>(&toolinfo_)); | 142 reinterpret_cast<LPARAM>(&toolinfo_)); |
148 } | 143 } |
149 | 144 |
150 bool TooltipWin::IsVisible() { | 145 bool TooltipWin::IsVisible() { |
151 return showing_; | 146 return showing_; |
152 } | 147 } |
153 | 148 |
154 } // namespace corewm | 149 } // namespace corewm |
155 } // namespace views | 150 } // namespace views |
OLD | NEW |