diff options
Diffstat (limited to 'chrome/views')
-rw-r--r-- | chrome/views/aero_tooltip_manager.cc | 1 | ||||
-rw-r--r-- | chrome/views/tooltip_manager.cc | 95 | ||||
-rw-r--r-- | chrome/views/tooltip_manager.h | 21 |
3 files changed, 109 insertions, 8 deletions
diff --git a/chrome/views/aero_tooltip_manager.cc b/chrome/views/aero_tooltip_manager.cc index a6f3ec5..f0827f7 100644 --- a/chrome/views/aero_tooltip_manager.cc +++ b/chrome/views/aero_tooltip_manager.cc @@ -63,6 +63,7 @@ void AeroTooltipManager::OnMouse(UINT u_msg, WPARAM w_param, LPARAM l_param) { if (last_mouse_x_ != x || last_mouse_y_ != y) { last_mouse_x_ = x; last_mouse_y_ = y; + HideKeyboardTooltip(); UpdateTooltip(x, y); } diff --git a/chrome/views/tooltip_manager.cc b/chrome/views/tooltip_manager.cc index 3eac713..54c3070 100644 --- a/chrome/views/tooltip_manager.cc +++ b/chrome/views/tooltip_manager.cc @@ -31,6 +31,7 @@ #include "chrome/common/gfx/chrome_font.h" #include "base/logging.h" +#include "base/message_loop.h" #include "chrome/common/l10n_util.h" #include "chrome/common/gfx/url_elider.h" #include "chrome/common/win_util.h" @@ -44,6 +45,10 @@ namespace ChromeViews { //static int TooltipManager::tooltip_height_ = 0; +// Default timeout for the tooltip displayed using keyboard. +// Timeout is mentioned in milliseconds. +static const int kDefaultTimeout = 4000; + // Maximum number of lines we allow in the tooltip. static const int kMaxLines = 6; @@ -101,7 +106,10 @@ TooltipManager::TooltipManager(ViewContainer* container, HWND parent) tooltip_showing_(false), last_tooltip_view_(NULL), last_view_out_of_sync_(false), - tooltip_width_(0) { + tooltip_width_(0), + keyboard_tooltip_hwnd_(NULL), +#pragma warning(suppress: 4355) + keyboard_tooltip_factory_(this) { DCHECK(container && parent); Init(); } @@ -109,6 +117,8 @@ TooltipManager::TooltipManager(ViewContainer* container, HWND parent) TooltipManager::~TooltipManager() { if (tooltip_hwnd_) DestroyWindow(tooltip_hwnd_); + if (keyboard_tooltip_hwnd_) + DestroyWindow(keyboard_tooltip_hwnd_); } void TooltipManager::Init() { @@ -154,7 +164,7 @@ void TooltipManager::TooltipTextChanged(View* view) { LRESULT TooltipManager::OnNotify(int w_param, NMHDR* l_param, bool* handled) { *handled = false; - if (l_param->hwndFrom == tooltip_hwnd_) { + if (l_param->hwndFrom == tooltip_hwnd_ && keyboard_tooltip_hwnd_ == NULL) { switch (l_param->code) { case TTN_GETDISPINFO: { if (last_view_out_of_sync_) { @@ -183,7 +193,8 @@ LRESULT TooltipManager::OnNotify(int w_param, NMHDR* l_param, bool* handled) { !tooltip_text_.empty()) { // View has a valid tip, copy it into TOOLTIPINFO. clipped_text_ = tooltip_text_; - TrimTooltipToFit(&clipped_text_, &tooltip_width_, &line_count_); + TrimTooltipToFit(&clipped_text_, &tooltip_width_, &line_count_, + last_mouse_x_, last_mouse_y_, tooltip_hwnd_); tooltip_info->lpszText = const_cast<WCHAR*>(clipped_text_.c_str()); } else { tooltip_text_.clear(); @@ -279,18 +290,21 @@ int TooltipManager::CalcTooltipHeight() { void TooltipManager::TrimTooltipToFit(std::wstring* text, int* max_width, - int* line_count) { + int* line_count, + int position_x, + int position_y, + HWND window) { *max_width = 0; *line_count = 0; // Determine the available width for the tooltip. - CPoint screen_loc(last_mouse_x_, last_mouse_y_); + CPoint screen_loc(position_x, position_y); View::ConvertPointToScreen(view_container_->GetRootView(), &screen_loc); gfx::Rect monitor_bounds = win_util::GetMonitorBoundsForRect(gfx::Rect(screen_loc.x, screen_loc.y, 0, 0)); RECT tooltip_margin; - SendMessage(tooltip_hwnd_, TTM_GETMARGIN, 0, (LPARAM)&tooltip_margin); + SendMessage(window, TTM_GETMARGIN, 0, (LPARAM)&tooltip_margin); const int available_width = monitor_bounds.width() - tooltip_margin.left - tooltip_margin.right; if (available_width <= 0) @@ -352,6 +366,7 @@ void TooltipManager::OnMouse(UINT u_msg, WPARAM w_param, LPARAM l_param) { if (u_msg != WM_MOUSEMOVE || last_mouse_x_ != x || last_mouse_y_ != y) { last_mouse_x_ = x; last_mouse_y_ = y; + HideKeyboardTooltip(); UpdateTooltip(x, y); } // Forward the message onto the tooltip. @@ -363,4 +378,70 @@ void TooltipManager::OnMouse(UINT u_msg, WPARAM w_param, LPARAM l_param) { SendMessage(tooltip_hwnd_, TTM_RELAYEVENT, 0, (LPARAM)&msg); } -} // namespace ChromeViews +void TooltipManager::ShowKeyboardTooltip(View* focused_view) { + if (tooltip_showing_) { + SendMessage(tooltip_hwnd_, TTM_POP, 0, 0); + tooltip_text_.clear(); + } + HideKeyboardTooltip(); + std::wstring tooltip_text; + if (!focused_view->GetTooltipText(0, 0, &tooltip_text)) + return ; + CRect bounds; + focused_view->GetBounds(&bounds); + CPoint screen_point; + focused_view->ConvertPointToScreen(focused_view, &screen_point); + CPoint relative_point_coordinates; + focused_view->ConvertPointToViewContainer(focused_view, + &relative_point_coordinates); + keyboard_tooltip_hwnd_ = CreateWindowEx( + WS_EX_TRANSPARENT | l10n_util::GetExtendedTooltipStyles(), + TOOLTIPS_CLASS, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL); + SendMessage(keyboard_tooltip_hwnd_, TTM_SETMAXTIPWIDTH, 0, + std::numeric_limits<short>::max()); + int tooltip_width; + int line_count; + TrimTooltipToFit(&tooltip_text, &tooltip_width, &line_count, + relative_point_coordinates.x, relative_point_coordinates.y, + keyboard_tooltip_hwnd_); + TOOLINFO keyboard_toolinfo; + memset(&keyboard_toolinfo, 0, sizeof(keyboard_toolinfo)); + keyboard_toolinfo.cbSize = sizeof(keyboard_toolinfo); + keyboard_toolinfo.hwnd = parent_; + keyboard_toolinfo.uFlags = TTF_TRACK | TTF_TRANSPARENT | TTF_IDISHWND ; + keyboard_toolinfo.lpszText = const_cast<WCHAR*>(tooltip_text.c_str()); + SendMessage(keyboard_tooltip_hwnd_, TTM_ADDTOOL, 0, + reinterpret_cast<LPARAM>(&keyboard_toolinfo)); + SendMessage(keyboard_tooltip_hwnd_, TTM_TRACKACTIVATE, TRUE, + reinterpret_cast<LPARAM>(&keyboard_toolinfo)); + if (!tooltip_height_) + tooltip_height_ = CalcTooltipHeight(); + RECT rect_bounds = {screen_point.x, screen_point.y + bounds.Height(), + screen_point.x + tooltip_width, + screen_point.y + bounds.Height() + + line_count * tooltip_height_ }; + gfx::Rect monitor_bounds = + win_util::GetMonitorBoundsForRect(gfx::Rect(rect_bounds)); + rect_bounds = gfx::Rect(rect_bounds).AdjustToFit(monitor_bounds).ToRECT(); + ::SetWindowPos(keyboard_tooltip_hwnd_, NULL, rect_bounds.left, + rect_bounds.top, 0, 0, + SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOSIZE); + MessageLoop::current()->PostDelayedTask(FROM_HERE, + keyboard_tooltip_factory_.NewRunnableMethod( + &TooltipManager::DestroyKeyboardTooltipWindow, keyboard_tooltip_hwnd_), + kDefaultTimeout); +} + +void TooltipManager::HideKeyboardTooltip() { + if (keyboard_tooltip_hwnd_ != NULL) { + SendMessage(keyboard_tooltip_hwnd_, WM_CLOSE, 0, 0); + keyboard_tooltip_hwnd_ = NULL; + } +} + +void TooltipManager::DestroyKeyboardTooltipWindow(HWND window_to_destroy) { + if (keyboard_tooltip_hwnd_ == window_to_destroy) + HideKeyboardTooltip(); +} + +} // namespace ChromeViews
\ No newline at end of file diff --git a/chrome/views/tooltip_manager.h b/chrome/views/tooltip_manager.h index 9c77f91..c229b52 100644 --- a/chrome/views/tooltip_manager.h +++ b/chrome/views/tooltip_manager.h @@ -92,6 +92,12 @@ class TooltipManager { // Invoked when the tooltip text changes for the specified views. void TooltipTextChanged(View* view); + // Invoked when toolbar icon gets focus. + void ShowKeyboardTooltip(View* view); + + // Invoked when toolbar loses focus. + void HideKeyboardTooltip(); + // Message handlers. These forward to the tooltip control. virtual void OnMouse(UINT u_msg, WPARAM w_param, LPARAM l_param); LRESULT OnNotify(int w_param, NMHDR* l_param, bool* handled); @@ -132,7 +138,13 @@ class TooltipManager { // of text in the tooltip. void TrimTooltipToFit(std::wstring* text, int* width, - int* line_count); + int* line_count, + int position_x, + int position_y, + HWND window); + + // Invoked when the timer elapses and tooltip has to be destroyed. + void DestroyKeyboardTooltipWindow(HWND window_to_destroy); // Hosting view container. ViewContainer* view_container_; @@ -161,6 +173,13 @@ class TooltipManager { // Height for a tooltip; lazily calculated. static int tooltip_height_; + // control window for tooltip displayed using keyboard. + HWND keyboard_tooltip_hwnd_; + + // Used to register DestroyTooltipWindow function with postdelayedtask + // function. + ScopedRunnableMethodFactory<TooltipManager> keyboard_tooltip_factory_; + DISALLOW_EVIL_CONSTRUCTORS(TooltipManager); }; |