Home | History | Annotate | Download | only in widget
      1 // Copyright (c) 2011 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 #ifndef UI_VIEWS_WIDGET_TOOLTIP_MANAGER_WIN_H_
      6 #define UI_VIEWS_WIDGET_TOOLTIP_MANAGER_WIN_H_
      7 
      8 #include <windows.h>
      9 #include <commctrl.h>
     10 #include <string>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/memory/weak_ptr.h"
     15 #include "base/strings/string16.h"
     16 #include "ui/gfx/native_widget_types.h"
     17 #include "ui/gfx/point.h"
     18 #include "ui/views/widget/tooltip_manager.h"
     19 
     20 namespace gfx {
     21 class Point;
     22 }
     23 
     24 namespace views {
     25 
     26 class View;
     27 class Widget;
     28 
     29 // TooltipManager implementation for Windows.
     30 //
     31 // This class is intended to be used by NativeWidgetWin. To use this, you must
     32 // do the following:
     33 // Add the following to your MSG_MAP:
     34 //
     35 //   MESSAGE_RANGE_HANDLER(WM_MOUSEFIRST, WM_MOUSELAST, OnMouseRange)
     36 //   MESSAGE_RANGE_HANDLER(WM_NCMOUSEMOVE, WM_NCMOUSEMOVE, OnMouseRange)
     37 //   MSG_WM_NOTIFY(OnNotify)
     38 //
     39 // With the following implementations:
     40 //   LRESULT XXX::OnMouseRange(UINT u_msg, WPARAM w_param, LPARAM l_param,
     41 //                             BOOL& handled) {
     42 //     tooltip_manager_->OnMouse(u_msg, w_param, l_param);
     43 //     handled = FALSE;
     44 //     return 0;
     45 //   }
     46 //
     47 //   LRESULT XXX::OnNotify(int w_param, NMHDR* l_param) {
     48 //     bool handled;
     49 //     LRESULT result = tooltip_manager_->OnNotify(w_param, l_param, &handled);
     50 //     SetMsgHandled(handled);
     51 //     return result;
     52 //   }
     53 //
     54 // And of course you'll need to create the TooltipManager!
     55 //
     56 // Lastly, you'll need to override GetTooltipManager.
     57 //
     58 // See NativeWidgetWin for an example of this in action.
     59 class TooltipManagerWin : public TooltipManager {
     60  public:
     61   // Creates a TooltipManager for the specified Widget and parent window.
     62   explicit TooltipManagerWin(Widget* widget);
     63   virtual ~TooltipManagerWin();
     64 
     65   // Initializes the TooltipManager returning whether initialization was
     66   // successful. If this returns false the TooltipManager should be destroyed
     67   // and not used.
     68   bool Init();
     69 
     70   // Notification that the view hierarchy has changed in some way.
     71   virtual void UpdateTooltip() OVERRIDE;
     72 
     73   // Invoked when the tooltip text changes for the specified views.
     74   virtual void TooltipTextChanged(View* view) OVERRIDE;
     75 
     76   // Invoked when toolbar icon gets focus.
     77   virtual void ShowKeyboardTooltip(View* view) OVERRIDE;
     78 
     79   // Invoked when toolbar loses focus.
     80   virtual void HideKeyboardTooltip() OVERRIDE;
     81 
     82   // Message handlers. These forward to the tooltip control.
     83   virtual void OnMouse(UINT u_msg, WPARAM w_param, LPARAM l_param);
     84   LRESULT OnNotify(int w_param, NMHDR* l_param, bool* handled);
     85 
     86  protected:
     87   // Returns the Widget we're showing tooltips for.
     88   gfx::NativeView GetParent();
     89 
     90   // Updates the tooltip for the specified location.
     91   void UpdateTooltip(const gfx::Point& location);
     92 
     93   // Tooltip control window.
     94   HWND tooltip_hwnd_;
     95 
     96   // Tooltip information.
     97   TOOLINFO toolinfo_;
     98 
     99   // Last location of the mouse. This is in the coordinates of the rootview.
    100   gfx::Point last_mouse_pos_;
    101 
    102   // Whether or not the tooltip is showing.
    103   bool tooltip_showing_;
    104 
    105  private:
    106   // Sets the tooltip position based on the x/y position of the text. If the
    107   // tooltip fits, true is returned.
    108   bool SetTooltipPosition(int text_x, int text_y);
    109 
    110   // Calculates the preferred height for tooltips. This always returns a
    111   // positive value.
    112   int CalcTooltipHeight();
    113 
    114   // Invoked when the timer elapses and tooltip has to be destroyed.
    115   void DestroyKeyboardTooltipWindow(HWND window_to_destroy);
    116 
    117   // Hosting Widget.
    118   Widget* widget_;
    119 
    120   // The View the mouse is under. This is null if the mouse isn't under a
    121   // View.
    122   View* last_tooltip_view_;
    123 
    124   // Whether or not the view under the mouse needs to be refreshed. If this
    125   // is true, when the tooltip is asked for the view under the mouse is
    126   // refreshed.
    127   bool last_view_out_of_sync_;
    128 
    129   // Text for tooltip from the view.
    130   string16 tooltip_text_;
    131 
    132   // The clipped tooltip.
    133   string16 clipped_text_;
    134 
    135   // Number of lines in the tooltip.
    136   int line_count_;
    137 
    138   // Width of the last tooltip.
    139   int tooltip_width_;
    140 
    141   // control window for tooltip displayed using keyboard.
    142   HWND keyboard_tooltip_hwnd_;
    143 
    144   // Used to register DestroyTooltipWindow function with PostDelayedTask
    145   // function.
    146   base::WeakPtrFactory<TooltipManagerWin> keyboard_tooltip_factory_;
    147 
    148   DISALLOW_COPY_AND_ASSIGN(TooltipManagerWin);
    149 };
    150 
    151 }  // namespace views
    152 
    153 #endif  // UI_VIEWS_WIDGET_TOOLTIP_MANAGER_WIN_H_
    154