| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #ifndef VIEWS_WIDGET_TOOLTIP_MANAGER_H_ | 5 #ifndef VIEWS_WIDGET_TOOLTIP_MANAGER_H_ |
| 6 #define VIEWS_WIDGET_TOOLTIP_MANAGER_H_ | 6 #define VIEWS_WIDGET_TOOLTIP_MANAGER_H_ |
| 7 | 7 |
| 8 #include <windows.h> | 8 #include <string> |
| 9 #include <commctrl.h> | |
| 10 | 9 |
| 11 #include <string> | |
| 12 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 13 #include "base/task.h" | |
| 14 | 11 |
| 15 namespace gfx { | 12 namespace gfx { |
| 16 class Font; | 13 class Font; |
| 17 } | 14 } // namespace gfx |
| 18 | 15 |
| 19 namespace views { | 16 namespace views { |
| 20 | 17 |
| 21 class View; | 18 class View; |
| 22 class Widget; | |
| 23 | 19 |
| 24 // TooltipManager takes care of the wiring to support tooltips for Views. | 20 // TooltipManager takes care of the wiring to support tooltips for Views. You |
| 25 // This class is intended to be used by Widgets. To use this, you must | 21 // almost never need to interact directly with TooltipManager, rather look to |
| 26 // do the following: | 22 // the various tooltip methods on View. |
| 27 // Add the following to your MSG_MAP: | |
| 28 // | |
| 29 // MESSAGE_RANGE_HANDLER(WM_MOUSEFIRST, WM_MOUSELAST, OnMouseRange) | |
| 30 // MESSAGE_RANGE_HANDLER(WM_NCMOUSEMOVE, WM_NCMOUSEMOVE, OnMouseRange) | |
| 31 // MSG_WM_NOTIFY(OnNotify) | |
| 32 // | |
| 33 // With the following implementations: | |
| 34 // LRESULT XXX::OnMouseRange(UINT u_msg, WPARAM w_param, LPARAM l_param, | |
| 35 // BOOL& handled) { | |
| 36 // tooltip_manager_->OnMouse(u_msg, w_param, l_param); | |
| 37 // handled = FALSE; | |
| 38 // return 0; | |
| 39 // } | |
| 40 // | |
| 41 // LRESULT XXX::OnNotify(int w_param, NMHDR* l_param) { | |
| 42 // bool handled; | |
| 43 // LRESULT result = tooltip_manager_->OnNotify(w_param, l_param, &handled); | |
| 44 // SetMsgHandled(handled); | |
| 45 // return result; | |
| 46 // } | |
| 47 // | |
| 48 // And of course you'll need to create the TooltipManager! | |
| 49 // | |
| 50 // Lastly, you'll need to override GetTooltipManager. | |
| 51 // | |
| 52 // See XPFrame for an example of this in action. | |
| 53 class TooltipManager { | 23 class TooltipManager { |
| 54 public: | 24 public: |
| 55 // Returns the height of tooltips. This should only be invoked from within | 25 // Returns the height of tooltips. This should only be invoked from within |
| 56 // GetTooltipTextOrigin. | 26 // GetTooltipTextOrigin. |
| 57 static int GetTooltipHeight(); | 27 static int GetTooltipHeight(); |
| 58 | 28 |
| 59 // Returns the default font used by tooltips. | 29 // Returns the default font used by tooltips. |
| 60 static gfx::Font GetDefaultFont(); | 30 static gfx::Font GetDefaultFont(); |
| 61 | 31 |
| 62 // Returns the separator for lines of text in a tooltip. | 32 // Returns the separator for lines of text in a tooltip. |
| 63 static const std::wstring& GetLineSeparator(); | 33 static const std::wstring& GetLineSeparator(); |
| 64 | 34 |
| 65 // Creates a TooltipManager for the specified Widget and parent window. | 35 TooltipManager() {} |
| 66 TooltipManager(Widget* widget, HWND parent); | 36 virtual ~TooltipManager() {} |
| 67 virtual ~TooltipManager(); | |
| 68 | 37 |
| 69 // Notification that the view hierarchy has changed in some way. | 38 // Notification that the view hierarchy has changed in some way. |
| 70 void UpdateTooltip(); | 39 virtual void UpdateTooltip() = 0; |
| 71 | 40 |
| 72 // Invoked when the tooltip text changes for the specified views. | 41 // Invoked when the tooltip text changes for the specified views. |
| 73 void TooltipTextChanged(View* view); | 42 virtual void TooltipTextChanged(View* view) = 0; |
| 74 | 43 |
| 75 // Invoked when toolbar icon gets focus. | 44 // Invoked when toolbar icon gets focus. |
| 76 void ShowKeyboardTooltip(View* view); | 45 virtual void ShowKeyboardTooltip(View* view) = 0; |
| 77 | 46 |
| 78 // Invoked when toolbar loses focus. | 47 // Invoked when toolbar loses focus. |
| 79 void HideKeyboardTooltip(); | 48 virtual void HideKeyboardTooltip() = 0; |
| 80 | |
| 81 // Message handlers. These forward to the tooltip control. | |
| 82 virtual void OnMouse(UINT u_msg, WPARAM w_param, LPARAM l_param); | |
| 83 LRESULT OnNotify(int w_param, NMHDR* l_param, bool* handled); | |
| 84 // Not used directly by TooltipManager, but provided for AeroTooltipManager. | |
| 85 virtual void OnMouseLeave() {} | |
| 86 | |
| 87 protected: | |
| 88 virtual void Init(); | |
| 89 | |
| 90 // Updates the tooltip for the specified location. | |
| 91 void UpdateTooltip(int x, int y); | |
| 92 | |
| 93 // Parent window the tooltip is added to. | |
| 94 HWND parent_; | |
| 95 | |
| 96 // Tooltip control window. | |
| 97 HWND tooltip_hwnd_; | |
| 98 | |
| 99 // Tooltip information. | |
| 100 TOOLINFO toolinfo_; | |
| 101 | |
| 102 // Last location of the mouse. This is in the coordinates of the rootview. | |
| 103 int last_mouse_x_; | |
| 104 int last_mouse_y_; | |
| 105 | |
| 106 // Whether or not the tooltip is showing. | |
| 107 bool tooltip_showing_; | |
| 108 | |
| 109 private: | |
| 110 // Sets the tooltip position based on the x/y position of the text. If the | |
| 111 // tooltip fits, true is returned. | |
| 112 bool SetTooltipPosition(int text_x, int text_y); | |
| 113 | |
| 114 // Calculates the preferred height for tooltips. This always returns a | |
| 115 // positive value. | |
| 116 int CalcTooltipHeight(); | |
| 117 | |
| 118 // Trims the tooltip to fit, setting text to the clipped result, width to the | |
| 119 // width (in pixels) of the clipped text and line_count to the number of lines | |
| 120 // of text in the tooltip. | |
| 121 void TrimTooltipToFit(std::wstring* text, | |
| 122 int* width, | |
| 123 int* line_count, | |
| 124 int position_x, | |
| 125 int position_y, | |
| 126 HWND window); | |
| 127 | |
| 128 // Invoked when the timer elapses and tooltip has to be destroyed. | |
| 129 void DestroyKeyboardTooltipWindow(HWND window_to_destroy); | |
| 130 | |
| 131 // Hosting Widget. | |
| 132 Widget* widget_; | |
| 133 | |
| 134 // The View the mouse is under. This is null if the mouse isn't under a | |
| 135 // View. | |
| 136 View* last_tooltip_view_; | |
| 137 | |
| 138 // Whether or not the view under the mouse needs to be refreshed. If this | |
| 139 // is true, when the tooltip is asked for the view under the mouse is | |
| 140 // refreshed. | |
| 141 bool last_view_out_of_sync_; | |
| 142 | |
| 143 // Text for tooltip from the view. | |
| 144 std::wstring tooltip_text_; | |
| 145 | |
| 146 // The clipped tooltip. | |
| 147 std::wstring clipped_text_; | |
| 148 | |
| 149 // Number of lines in the tooltip. | |
| 150 int line_count_; | |
| 151 | |
| 152 // Width of the last tooltip. | |
| 153 int tooltip_width_; | |
| 154 | |
| 155 // Height for a tooltip; lazily calculated. | |
| 156 static int tooltip_height_; | |
| 157 | |
| 158 // control window for tooltip displayed using keyboard. | |
| 159 HWND keyboard_tooltip_hwnd_; | |
| 160 | |
| 161 // Used to register DestroyTooltipWindow function with PostDelayedTask | |
| 162 // function. | |
| 163 ScopedRunnableMethodFactory<TooltipManager> keyboard_tooltip_factory_; | |
| 164 | |
| 165 DISALLOW_EVIL_CONSTRUCTORS(TooltipManager); | |
| 166 }; | 49 }; |
| 167 | 50 |
| 168 } // namespace views | 51 } // namespace views |
| 169 | 52 |
| 170 #endif // VIEWS_WIDGET_TOOLTIP_MANAGER_H_ | 53 #endif // VIEWS_WIDGET_TOOLTIP_MANAGER_H_ |
| OLD | NEW |