OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #import "ui/base/cocoa/tool_tip_base_view.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 // Below is the nasty tooltip stuff -- copied from WebKit's WebHTMLView.mm |
| 10 // with minor modifications for code style and commenting. |
| 11 // |
| 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. |
| 15 // |
| 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 |
| 20 |
| 21 |
| 22 /* |
| 23 * Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. |
| 24 * (C) 2006, 2007 Graham Dennis (graham.dennis@gmail.com) |
| 25 * |
| 26 * Redistribution and use in source and binary forms, with or without |
| 27 * modification, are permitted provided that the following conditions |
| 28 * are met: |
| 29 * |
| 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. |
| 38 * |
| 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. |
| 49 */ |
| 50 |
| 51 // Any non-zero value will do, but using something recognizable might help us |
| 52 // debug some day. |
| 53 const NSTrackingRectTag kTrackingRectTag = 0xBADFACE; |
| 54 |
| 55 @implementation ToolTipBaseView |
| 56 |
| 57 // Override of a public NSView method, replacing the inherited functionality. |
| 58 // See above for rationale. |
| 59 - (NSTrackingRectTag)addTrackingRect:(NSRect)rect |
| 60 owner:(id)owner |
| 61 userData:(void *)data |
| 62 assumeInside:(BOOL)assumeInside { |
| 63 DCHECK(trackingRectOwner_ == nil); |
| 64 trackingRectOwner_ = owner; |
| 65 trackingRectUserData_ = data; |
| 66 return kTrackingRectTag; |
| 67 } |
| 68 |
| 69 // Override of (apparently) a private NSView method(!) See above for rationale. |
| 70 - (NSTrackingRectTag)_addTrackingRect:(NSRect)rect |
| 71 owner:(id)owner |
| 72 userData:(void *)data |
| 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; |
| 80 } |
| 81 |
| 82 // Override of (apparently) a private NSView method(!) See above for rationale. |
| 83 - (void)_addTrackingRects:(NSRect *)rects |
| 84 owner:(id)owner |
| 85 userDataList:(void **)userDataList |
| 86 assumeInsideList:(BOOL *)assumeInsideList |
| 87 trackingNums:(NSTrackingRectTag *)trackingNums |
| 88 count:(int)count { |
| 89 DCHECK(count == 1); |
| 90 DCHECK(trackingNums[0] == 0 || trackingNums[0] == kTrackingRectTag); |
| 91 DCHECK(trackingRectOwner_ == nil); |
| 92 trackingRectOwner_ = owner; |
| 93 trackingRectUserData_ = userDataList[0]; |
| 94 trackingNums[0] = kTrackingRectTag; |
| 95 } |
| 96 |
| 97 // Override of a public NSView method, replacing the inherited functionality. |
| 98 // See above for rationale. |
| 99 - (void)removeTrackingRect:(NSTrackingRectTag)tag { |
| 100 if (tag == 0) |
| 101 return; |
| 102 |
| 103 if (tag == kTrackingRectTag) { |
| 104 trackingRectOwner_ = nil; |
| 105 return; |
| 106 } |
| 107 |
| 108 if (tag == lastToolTipTag_) { |
| 109 [super removeTrackingRect:tag]; |
| 110 lastToolTipTag_ = 0; |
| 111 return; |
| 112 } |
| 113 |
| 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). |
| 116 NOTREACHED(); |
| 117 } |
| 118 |
| 119 // Override of (apparently) a private NSView method(!) |
| 120 - (void)_removeTrackingRects:(NSTrackingRectTag *)tags count:(int)count { |
| 121 for (int i = 0; i < count; ++i) { |
| 122 int tag = tags[i]; |
| 123 if (tag == 0) |
| 124 continue; |
| 125 DCHECK(tag == kTrackingRectTag); |
| 126 trackingRectOwner_ = nil; |
| 127 } |
| 128 } |
| 129 |
| 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 |
| 135 location:NSZeroPoint |
| 136 modifierFlags:0 |
| 137 timestamp:0 |
| 138 windowNumber:windowNumber |
| 139 context:NULL |
| 140 eventNumber:0 |
| 141 trackingNumber:kTrackingRectTag |
| 142 userData:trackingRectUserData_]; |
| 143 [trackingRectOwner_ mouseExited:fakeEvent]; |
| 144 } |
| 145 |
| 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 |
| 151 location:NSZeroPoint |
| 152 modifierFlags:0 |
| 153 timestamp:0 |
| 154 windowNumber:windowNumber |
| 155 context:NULL |
| 156 eventNumber:0 |
| 157 trackingNumber:kTrackingRectTag |
| 158 userData:trackingRectUserData_]; |
| 159 [trackingRectOwner_ mouseEntered:fakeEvent]; |
| 160 } |
| 161 |
| 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_)) { |
| 169 return; |
| 170 } |
| 171 |
| 172 if (toolTip_) { |
| 173 [self _sendToolTipMouseExited]; |
| 174 } |
| 175 |
| 176 toolTip_.reset([toolTip copy]); |
| 177 |
| 178 if (toolTip) { |
| 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 |
| 184 owner:self |
| 185 userData:NULL]; |
| 186 [self _sendToolTipMouseEntered]; |
| 187 } |
| 188 } |
| 189 |
| 190 // NSView calls this to get the text when displaying the tooltip. |
| 191 - (NSString *)view:(NSView *)view |
| 192 stringForToolTip:(NSToolTipTag)tag |
| 193 point:(NSPoint)point |
| 194 userData:(void *)data { |
| 195 return [[toolTip_ copy] autorelease]; |
| 196 } |
| 197 |
| 198 @end |
OLD | NEW |