Home | History | Annotate | Download | only in widget
      1 // Copyright (c) 2012 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/aero_tooltip_manager.h"
      6 
      7 #include <windows.h>
      8 #include <commctrl.h>
      9 #include <shlobj.h>
     10 
     11 #include "base/bind.h"
     12 #include "base/message_loop/message_loop.h"
     13 #include "ui/base/l10n/l10n_util_win.h"
     14 #include "ui/base/win/dpi.h"
     15 #include "ui/base/win/hwnd_util.h"
     16 #include "ui/gfx/point.h"
     17 
     18 namespace views {
     19 
     20 ///////////////////////////////////////////////////////////////////////////////
     21 // AeroTooltipManager, public:
     22 
     23 AeroTooltipManager::AeroTooltipManager(Widget* widget)
     24     : TooltipManagerWin(widget),
     25       initial_delay_(0) {
     26 }
     27 
     28 AeroTooltipManager::~AeroTooltipManager() {
     29   if (initial_timer_)
     30     initial_timer_->Disown();
     31 }
     32 
     33 void AeroTooltipManager::OnMouse(UINT u_msg, WPARAM w_param, LPARAM l_param) {
     34   if (u_msg == WM_MOUSELEAVE) {
     35     last_mouse_pos_.SetPoint(-1, -1);
     36     UpdateTooltip();
     37     return;
     38   }
     39 
     40   if (initial_timer_)
     41     initial_timer_->Disown();
     42 
     43   if (u_msg == WM_MOUSEMOVE || u_msg == WM_NCMOUSEMOVE) {
     44     gfx::Point mouse_pos_in_pixels(l_param);
     45     gfx::Point mouse_pos = ui::win::ScreenToDIPPoint(mouse_pos_in_pixels);
     46     if (u_msg == WM_NCMOUSEMOVE) {
     47       // NC message coordinates are in screen coordinates.
     48       POINT temp = mouse_pos_in_pixels.ToPOINT();
     49       ::MapWindowPoints(HWND_DESKTOP, GetParent(), &temp, 1);
     50       mouse_pos_in_pixels.SetPoint(temp.x, temp.y);
     51       mouse_pos = ui::win::ScreenToDIPPoint(mouse_pos_in_pixels);
     52     }
     53     if (last_mouse_pos_ != mouse_pos) {
     54       last_mouse_pos_ = mouse_pos;
     55       HideKeyboardTooltip();
     56       UpdateTooltip(mouse_pos);
     57     }
     58 
     59     // Delay opening of the tooltip just in case the user moves their
     60     // mouse to another control. We defer this from Init because we get
     61     // zero if we query it too soon.
     62     if (!initial_delay_) {
     63       initial_delay_ = static_cast<int>(
     64           ::SendMessage(tooltip_hwnd_, TTM_GETDELAYTIME, TTDT_INITIAL, 0));
     65     }
     66     initial_timer_ = new InitialTimer(this);
     67     initial_timer_->Start(initial_delay_);
     68   } else {
     69     // Hide the tooltip and cancel any timers.
     70     ::SendMessage(tooltip_hwnd_, TTM_POP, 0, 0);
     71     ::SendMessage(tooltip_hwnd_, TTM_TRACKACTIVATE, false, (LPARAM)&toolinfo_);
     72     return;
     73   }
     74 }
     75 
     76 ///////////////////////////////////////////////////////////////////////////////
     77 // AeroTooltipManager, private:
     78 
     79 void AeroTooltipManager::OnTimer() {
     80   initial_timer_ = NULL;
     81 
     82   POINT pt = last_mouse_pos_.ToPOINT();
     83   ::ClientToScreen(GetParent(), &pt);
     84 
     85   // Set the position and visibility.
     86   if (!tooltip_showing_) {
     87     ::SendMessage(tooltip_hwnd_, TTM_POPUP, 0, 0);
     88     ::SendMessage(tooltip_hwnd_, TTM_TRACKPOSITION, 0, MAKELPARAM(pt.x, pt.y));
     89     ::SendMessage(tooltip_hwnd_, TTM_TRACKACTIVATE, true, (LPARAM)&toolinfo_);
     90   }
     91 }
     92 
     93 ///////////////////////////////////////////////////////////////////////////////
     94 // AeroTooltipManager::InitialTimer
     95 
     96 AeroTooltipManager::InitialTimer::InitialTimer(AeroTooltipManager* manager)
     97     : manager_(manager) {
     98 }
     99 
    100 void AeroTooltipManager::InitialTimer::Start(int time) {
    101   base::MessageLoop::current()->PostDelayedTask(
    102       FROM_HERE,
    103       base::Bind(&InitialTimer::Execute, this),
    104       base::TimeDelta::FromMilliseconds(time));
    105 }
    106 
    107 void AeroTooltipManager::InitialTimer::Disown() {
    108   manager_ = NULL;
    109 }
    110 
    111 void AeroTooltipManager::InitialTimer::Execute() {
    112   if (manager_)
    113     manager_->OnTimer();
    114 }
    115 
    116 }  // namespace views
    117