1 // Copyright 2015 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.
5 #import "ui/base/cocoa/tool_tip_base_view.h"
7 #include "base/logging.h"
9 // Below is the nasty tooltip stuff -- copied from WebKit's WebHTMLView.mm
10 // with minor modifications for code style and commenting.
12 // The 'public' interface is -setToolTipAtMousePoint:. This differs from
13 // -setToolTip: in that the updated tooltip takes effect immediately,
14 // without the user's having to move the mouse out of and back into the view.
16 // Unfortunately, doing this requires sending fake mouseEnter/Exit events to
17 // the view, which in turn requires overriding some internal tracking-rect
18 // methods (to keep track of its owner & userdata, which need to be filled out
19 // in the fake events.) --snej 7/6/09
23 * Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
24 * (C) 2006, 2007 Graham Dennis (graham.dennis@gmail.com)
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
36 * its contributors may be used to endorse or promote products derived
37 * from this software without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
40 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
41 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
43 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
44 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
48 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 // Any non-zero value will do, but using something recognizable might help us
53 const NSTrackingRectTag kTrackingRectTag = 0xBADFACE;
55 @implementation ToolTipBaseView
57 // Override of a public NSView method, replacing the inherited functionality.
58 // See above for rationale.
59 - (NSTrackingRectTag)addTrackingRect:(NSRect)rect
62 assumeInside:(BOOL)assumeInside {
63 DCHECK(trackingRectOwner_ == nil);
64 trackingRectOwner_ = owner;
65 trackingRectUserData_ = data;
66 return kTrackingRectTag;
69 // Override of (apparently) a private NSView method(!) See above for rationale.
70 - (NSTrackingRectTag)_addTrackingRect:(NSRect)rect
73 assumeInside:(BOOL)assumeInside
74 useTrackingNum:(int)tag {
75 DCHECK(tag == 0 || tag == kTrackingRectTag);
76 DCHECK(trackingRectOwner_ == nil);
77 trackingRectOwner_ = owner;
78 trackingRectUserData_ = data;
79 return kTrackingRectTag;
82 // Override of (apparently) a private NSView method(!) See above for rationale.
83 - (void)_addTrackingRects:(NSRect *)rects
85 userDataList:(void **)userDataList
86 assumeInsideList:(BOOL *)assumeInsideList
87 trackingNums:(NSTrackingRectTag *)trackingNums
90 DCHECK(trackingNums[0] == 0 || trackingNums[0] == kTrackingRectTag);
91 DCHECK(trackingRectOwner_ == nil);
92 trackingRectOwner_ = owner;
93 trackingRectUserData_ = userDataList[0];
94 trackingNums[0] = kTrackingRectTag;
97 // Override of a public NSView method, replacing the inherited functionality.
98 // See above for rationale.
99 - (void)removeTrackingRect:(NSTrackingRectTag)tag {
103 if (tag == kTrackingRectTag) {
104 trackingRectOwner_ = nil;
108 if (tag == lastToolTipTag_) {
109 [super removeTrackingRect:tag];
114 // If any other tracking rect is being removed, we don't know how it was
115 // created and it's possible there's a leak involved (see Radar 3500217).
119 // Override of (apparently) a private NSView method(!)
120 - (void)_removeTrackingRects:(NSTrackingRectTag *)tags count:(int)count {
121 for (int i = 0; i < count; ++i) {
125 DCHECK(tag == kTrackingRectTag);
126 trackingRectOwner_ = nil;
130 // Sends a fake NSMouseExited event to the view for its current tracking rect.
131 - (void)_sendToolTipMouseExited {
132 // Nothing matters except window, trackingNumber, and userData.
133 int windowNumber = [[self window] windowNumber];
134 NSEvent* fakeEvent = [NSEvent enterExitEventWithType:NSMouseExited
138 windowNumber:windowNumber
141 trackingNumber:kTrackingRectTag
142 userData:trackingRectUserData_];
143 [trackingRectOwner_ mouseExited:fakeEvent];
146 // Sends a fake NSMouseEntered event to the view for its current tracking rect.
147 - (void)_sendToolTipMouseEntered {
148 // Nothing matters except window, trackingNumber, and userData.
149 int windowNumber = [[self window] windowNumber];
150 NSEvent* fakeEvent = [NSEvent enterExitEventWithType:NSMouseEntered
154 windowNumber:windowNumber
157 trackingNumber:kTrackingRectTag
158 userData:trackingRectUserData_];
159 [trackingRectOwner_ mouseEntered:fakeEvent];
162 // Sets the view's current tooltip, to be displayed at the current mouse
163 // location. (This does not make the tooltip appear -- as usual, it only
164 // appears after a delay.) Pass null to remove the tooltip.
165 - (void)setToolTipAtMousePoint:(NSString *)string {
166 NSString *toolTip = [string length] == 0 ? nil : string;
167 if ((toolTip && toolTip_ && [toolTip isEqualToString:toolTip_]) ||
168 (!toolTip && !toolTip_)) {
173 [self _sendToolTipMouseExited];
176 toolTip_.reset([toolTip copy]);
179 // See radar 3500217 for why we remove all tooltips
180 // rather than just the single one we created.
181 [self removeAllToolTips];
182 NSRect wideOpenRect = NSMakeRect(-100000, -100000, 200000, 200000);
183 lastToolTipTag_ = [self addToolTipRect:wideOpenRect
186 [self _sendToolTipMouseEntered];
190 // NSView calls this to get the text when displaying the tooltip.
191 - (NSString *)view:(NSView *)view
192 stringForToolTip:(NSToolTipTag)tag
194 userData:(void *)data {
195 return [[toolTip_ copy] autorelease];