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