Use native Cocoa tooltips instead of Mozilla's ToolTip class.

This makes tooltips behave more normally, and fixes some of the bugs. Patch by Jens Alfke (snej@google.com)
BUG=15655
TEST=tooltips should feel more like native tooltips

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20019 0039d316-1c4b-4281-b951-d872f2087c98
parent b5eaa17a
...@@ -35,10 +35,17 @@ class RWHVMEditCommandHelper; ...@@ -35,10 +35,17 @@ class RWHVMEditCommandHelper;
BOOL canBeKeyView_; BOOL canBeKeyView_;
BOOL closeOnDeactivate_; BOOL closeOnDeactivate_;
scoped_ptr<RWHVMEditCommandHelper> editCommand_helper_; scoped_ptr<RWHVMEditCommandHelper> editCommand_helper_;
// These are part of the magic tooltip code from WebKit's WebHTMLView:
id trackingRectOwner_; // (not retained)
void *trackingRectUserData_;
NSTrackingRectTag lastToolTipTag_;
NSString* toolTip_;
} }
- (void)setCanBeKeyView:(BOOL)can; - (void)setCanBeKeyView:(BOOL)can;
- (void)setCloseOnDeactivate:(BOOL)b; - (void)setCloseOnDeactivate:(BOOL)b;
- (void)setToolTipAtMousePoint:(NSString *)string;
@end @end
...@@ -141,13 +148,8 @@ class RenderWidgetHostViewMac : public RenderWidgetHostView { ...@@ -141,13 +148,8 @@ class RenderWidgetHostViewMac : public RenderWidgetHostView {
// true if the View is not visible. // true if the View is not visible.
bool is_hidden_; bool is_hidden_;
// Tooltips
// The text to be shown in the tooltip, supplied by the renderer. // The text to be shown in the tooltip, supplied by the renderer.
std::wstring tooltip_text_; std::wstring tooltip_text_;
// Used to display tooltips. We can't use the [NSView -setToolTip:] methods
// because we need to be able to show and hide the tooltip without the mouse
// leaving a region and NSView isn't set up for that to happen.
scoped_nsobject<ToolTip> tooltip_;
// Factory used to safely scope delayed calls to ShutdownHost(). // Factory used to safely scope delayed calls to ShutdownHost().
ScopedRunnableMethodFactory<RenderWidgetHostViewMac> shutdown_factory_; ScopedRunnableMethodFactory<RenderWidgetHostViewMac> shutdown_factory_;
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include "chrome/browser/renderer_host/render_widget_host.h" #include "chrome/browser/renderer_host/render_widget_host.h"
#include "chrome/common/native_web_keyboard_event.h" #include "chrome/common/native_web_keyboard_event.h"
#include "skia/ext/platform_canvas.h" #include "skia/ext/platform_canvas.h"
#import "third_party/mozilla/include/ToolTip.h"
#include "webkit/api/public/mac/WebInputEventFactory.h" #include "webkit/api/public/mac/WebInputEventFactory.h"
#include "webkit/api/public/WebInputEvent.h" #include "webkit/api/public/WebInputEvent.h"
#include "webkit/glue/webmenurunner_mac.h" #include "webkit/glue/webmenurunner_mac.h"
...@@ -108,11 +107,6 @@ void RenderWidgetHostViewMac::WasHidden() { ...@@ -108,11 +107,6 @@ void RenderWidgetHostViewMac::WasHidden() {
// everything again when we become selected again. // everything again when we become selected again.
is_hidden_ = true; is_hidden_ = true;
// We can't have tooltips floating around after the tools they're tipping
// about are hidden, can we?
tooltip_.reset(NULL);
tooltip_text_.clear();
// If we have a renderer, then inform it that we are being hidden so it can // If we have a renderer, then inform it that we are being hidden so it can
// reduce its resource utilization. // reduce its resource utilization.
render_widget_host_->WasHidden(); render_widget_host_->WasHidden();
...@@ -267,18 +261,7 @@ void RenderWidgetHostViewMac::Destroy() { ...@@ -267,18 +261,7 @@ void RenderWidgetHostViewMac::Destroy() {
// Called from the renderer to tell us what the tooltip text should be. It // Called from the renderer to tell us what the tooltip text should be. It
// calls us frequently so we need to cache the value to prevent doing a lot // calls us frequently so we need to cache the value to prevent doing a lot
// of repeat work. We cannot simply use [-NSView setToolTip:] because NSView // of repeat work.
// can't handle the case where the tooltip text changes while the mouse is
// still inside the view. Since the page elements that get tooltips are all
// contained within this view (and are unknown to the NSView system), we
// are forced to implement our own tooltips with child windows.
// TODO(pinkerton): Do we want these tooltips to time out after a certain time?
// Gecko does this automatically in the back-end, hence the ToolTip class not
// needing that functionality. We can either modify ToolTip or add this
// functionality here with a timer.
// TODO(pinkerton): This code really needs to live at a higher level because
// right now it allows multiple views in multiple tabs to each be displaying
// a tooltip simultaneously (http://crbug.com/14178).
void RenderWidgetHostViewMac::SetTooltipText(const std::wstring& tooltip_text) { void RenderWidgetHostViewMac::SetTooltipText(const std::wstring& tooltip_text) {
if (tooltip_text != tooltip_text_ && [[cocoa_view_ window] isKeyWindow]) { if (tooltip_text != tooltip_text_ && [[cocoa_view_ window] isKeyWindow]) {
tooltip_text_ = tooltip_text; tooltip_text_ = tooltip_text;
...@@ -292,19 +275,7 @@ void RenderWidgetHostViewMac::SetTooltipText(const std::wstring& tooltip_text) { ...@@ -292,19 +275,7 @@ void RenderWidgetHostViewMac::SetTooltipText(const std::wstring& tooltip_text) {
display_text = tooltip_text_.substr(0, kMaxTooltipLength); display_text = tooltip_text_.substr(0, kMaxTooltipLength);
NSString* tooltip_nsstring = base::SysWideToNSString(display_text); NSString* tooltip_nsstring = base::SysWideToNSString(display_text);
if ([tooltip_nsstring length] == 0) { [cocoa_view_ setToolTipAtMousePoint:tooltip_nsstring];
tooltip_.reset(NULL); // The dtor closes the tooltip.
} else {
// Get the current mouse location in the window's coordinate system and
// use that as the point for displaying the tooltip.
if (!tooltip_.get())
tooltip_.reset([[ToolTip alloc] init]);
NSPoint event_point =
[[cocoa_view_ window] mouseLocationOutsideOfEventStream];
[tooltip_ showToolTipAtPoint:event_point
withString:tooltip_nsstring
overWindow:[cocoa_view_ window]];
}
} }
} }
...@@ -367,6 +338,10 @@ void RenderWidgetHostViewMac::ShutdownHost() { ...@@ -367,6 +338,10 @@ void RenderWidgetHostViewMac::ShutdownHost() {
// Do not touch any members at this point, |this| has been deleted. // Do not touch any members at this point, |this| has been deleted.
} }
// RenderWidgetHostViewCocoa ---------------------------------------------------
@implementation RenderWidgetHostViewCocoa @implementation RenderWidgetHostViewCocoa
// Tons of stuff goes here, where we grab events going on in Cocoaland and send // Tons of stuff goes here, where we grab events going on in Cocoaland and send
...@@ -387,6 +362,7 @@ void RenderWidgetHostViewMac::ShutdownHost() { ...@@ -387,6 +362,7 @@ void RenderWidgetHostViewMac::ShutdownHost() {
- (void)dealloc { - (void)dealloc {
delete renderWidgetHostView_; delete renderWidgetHostView_;
[toolTip_ release];
[super dealloc]; [super dealloc];
} }
...@@ -570,5 +546,192 @@ void RenderWidgetHostViewMac::ShutdownHost() { ...@@ -570,5 +546,192 @@ void RenderWidgetHostViewMac::ShutdownHost() {
return renderWidgetHostView_; return renderWidgetHostView_;
} }
// Below is the nasty tooltip stuff -- copied from WebKit's WebHTMLView.mm
// with minor modifications for code style and commenting.
//
// The 'public' interface is -setToolTipAtMousePoint:. This differs from
// -setToolTip: in that the updated tooltip takes effect immediately,
// without the user's having to move the mouse out of and back into the view.
//
// Unfortunately, doing this requires sending fake mouseEnter/Exit events to
// the view, which in turn requires overriding some internal tracking-rect
// methods (to keep track of its owner & userdata, which need to be filled out
// in the fake events.) --snej 7/6/09
/*
* Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
* (C) 2006, 2007 Graham Dennis (graham.dennis@gmail.com)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Any non-zero value will do, but using something recognizable might help us
// debug some day.
static const NSTrackingRectTag kTrackingRectTag = 0xBADFACE;
// Override of a public NSView method, replacing the inherited functionality.
// See above for rationale.
- (NSTrackingRectTag)addTrackingRect:(NSRect)rect
owner:(id)owner
userData:(void *)data
assumeInside:(BOOL)assumeInside {
DCHECK(trackingRectOwner_ == nil);
trackingRectOwner_ = owner;
trackingRectUserData_ = data;
return kTrackingRectTag;
}
// Override of (apparently) a private NSView method(!) See above for rationale.
- (NSTrackingRectTag)_addTrackingRect:(NSRect)rect
owner:(id)owner
userData:(void *)data
assumeInside:(BOOL)assumeInside
useTrackingNum:(int)tag {
DCHECK(tag == 0 || tag == kTrackingRectTag);
DCHECK(trackingRectOwner_ == nil);
trackingRectOwner_ = owner;
trackingRectUserData_ = data;
return kTrackingRectTag;
}
// Override of (apparently) a private NSView method(!) See above for rationale.
- (void)_addTrackingRects:(NSRect *)rects
owner:(id)owner
userDataList:(void **)userDataList
assumeInsideList:(BOOL *)assumeInsideList
trackingNums:(NSTrackingRectTag *)trackingNums
count:(int)count {
DCHECK(count == 1);
DCHECK(trackingNums[0] == 0 || trackingNums[0] == kTrackingRectTag);
DCHECK(trackingRectOwner_ == nil);
trackingRectOwner_ = owner;
trackingRectUserData_ = userDataList[0];
trackingNums[0] = kTrackingRectTag;
}
// Override of a public NSView method, replacing the inherited functionality.
// See above for rationale.
- (void)removeTrackingRect:(NSTrackingRectTag)tag {
if (tag == 0)
return;
if (tag == kTrackingRectTag) {
trackingRectOwner_ = nil;
return;
}
if (tag == lastToolTipTag_) {
[super removeTrackingRect:tag];
lastToolTipTag_ = 0;
return;
}
// If any other tracking rect is being removed, we don't know how it was
// created and it's possible there's a leak involved (see Radar 3500217).
NOTREACHED();
}
// Override of (apparently) a private NSView method(!)
- (void)_removeTrackingRects:(NSTrackingRectTag *)tags count:(int)count {
for (int i = 0; i < count; ++i) {
int tag = tags[i];
if (tag == 0)
continue;
DCHECK(tag == kTrackingRectTag);
trackingRectOwner_ = nil;
}
}
// Sends a fake NSMouseExited event to the view for its current tracking rect.
- (void)_sendToolTipMouseExited {
// Nothing matters except window, trackingNumber, and userData.
int windowNumber = [[self window] windowNumber];
NSEvent *fakeEvent = [NSEvent enterExitEventWithType:NSMouseExited
location:NSMakePoint(0, 0)
modifierFlags:0
timestamp:0
windowNumber:windowNumber
context:NULL
eventNumber:0
trackingNumber:kTrackingRectTag
userData:trackingRectUserData_];
[trackingRectOwner_ mouseExited:fakeEvent];
}
// Sends a fake NSMouseEntered event to the view for its current tracking rect.
- (void)_sendToolTipMouseEntered {
// Nothing matters except window, trackingNumber, and userData.
int windowNumber = [[self window] windowNumber];
NSEvent *fakeEvent = [NSEvent enterExitEventWithType:NSMouseEntered
location:NSMakePoint(0, 0)
modifierFlags:0
timestamp:0
windowNumber:windowNumber
context:NULL
eventNumber:0
trackingNumber:kTrackingRectTag
userData:trackingRectUserData_];
[trackingRectOwner_ mouseEntered:fakeEvent];
}
// Sets the view's current tooltip, to be displayed at the current mouse
// location. (This does not make the tooltip appear -- as usual, it only
// appears after a delay.) Pass null to remove the tooltip.
- (void)setToolTipAtMousePoint:(NSString *)string {
NSString *toolTip = [string length] == 0 ? nil : string;
NSString *oldToolTip = toolTip_;
if ((toolTip == nil || oldToolTip == nil) ? toolTip == oldToolTip
: [toolTip isEqualToString:oldToolTip]) {
return;
}
if (oldToolTip) {
[self _sendToolTipMouseExited];
[oldToolTip release];
}
toolTip_ = [toolTip copy];
if (toolTip) {
// See radar 3500217 for why we remove all tooltips
// rather than just the single one we created.
[self removeAllToolTips];
NSRect wideOpenRect = NSMakeRect(-100000, -100000, 200000, 200000);
lastToolTipTag_ = [self addToolTipRect:wideOpenRect
owner:self
userData:NULL];
[self _sendToolTipMouseEntered];
}
}
// NSView calls this to get the text when displaying the tooltip.
- (NSString *)view:(NSView *)view
stringForToolTip:(NSToolTipTag)tag
point:(NSPoint)point
userData:(void *)data {
return [[toolTip_ copy] autorelease];
}
@end @end
...@@ -1856,8 +1856,6 @@ ...@@ -1856,8 +1856,6 @@
'../third_party/mozilla/include/NSScreen+Utils.m', '../third_party/mozilla/include/NSScreen+Utils.m',
'../third_party/mozilla/include/NSWorkspace+Utils.h', '../third_party/mozilla/include/NSWorkspace+Utils.h',
'../third_party/mozilla/include/NSWorkspace+Utils.m', '../third_party/mozilla/include/NSWorkspace+Utils.m',
'../third_party/mozilla/include/ToolTip.h',
'../third_party/mozilla/include/ToolTip.mm',
], ],
'include_dirs': [ 'include_dirs': [
'../third_party/GTM', '../third_party/GTM',
......
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Richard Schreyer
* Josh Aas <josh@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#import <Cocoa/Cocoa.h>
@class ToolTipView;
@interface ToolTip : NSObject
{
NSWindow* mTooltipWindow;
NSTextField* mTextField;
NSTextView* mTextView;
}
// Display a tooltip at |point| (in window coordinates) in |inWindow|.
- (void)showToolTipAtPoint:(NSPoint)point
withString:(NSString*)string
overWindow:(NSWindow*)inWindow;
- (void)closeToolTip;
@end
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Richard Schreyer
* Josh Aas <josh@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#import "ToolTip.h"
#import "NSScreen+Utils.h"
@interface ToolTip (ToolTipPrivateMethods)
- (void)parentWindowDidResignKey:(NSNotification*)inNotification;
@end
const float kBorderPadding = 2.0;
const float kMaxTextFieldWidth = 250.0;
const float kVOffset = 20.0;
@implementation ToolTip
- (id)init
{
if ((self = [super init]))
{
// the ref from -alloc is balanced by the -release in dealloc
mTooltipWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0.0, 0.0, kMaxTextFieldWidth, 0.0)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:YES];
// we don't want closing the window to release it, because we aren't always in control
// of the close (AppKit may do it on quit).
[mTooltipWindow setReleasedWhenClosed:NO];
// Create a textfield as the content of our new window.
// Field occupies all but the top 2 and bottom 2 pixels of the panel (bug 149635)
mTextView = [[NSTextView alloc] initWithFrame:NSMakeRect(0.0, kBorderPadding, kMaxTextFieldWidth, 0.0)];
[[mTooltipWindow contentView] addSubview:mTextView];
[mTextView release]; // window holds ref
// set up the panel
[mTooltipWindow setHasShadow:YES];
[mTooltipWindow setBackgroundColor:[NSColor colorWithCalibratedRed:1.0 green:1.0 blue:0.81 alpha:1.0]];
// set up the text view
[mTextView setDrawsBackground:NO];
[mTextView setEditable:NO];
[mTextView setSelectable:NO];
[mTextView setFont:[NSFont toolTipsFontOfSize:[NSFont smallSystemFontSize]]];
[mTextView setMinSize:NSMakeSize(0.0, 0.0)];
[mTextView setHorizontallyResizable:YES];
[mTextView setVerticallyResizable:YES];
}
return self;
}
- (void)dealloc
{
[self closeToolTip];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[mTooltipWindow close]; // we set the window not to release on -close
[mTooltipWindow release];
[super dealloc];
}
- (void)showToolTipAtPoint:(NSPoint)windowPoint
withString:(NSString*)string
overWindow:(NSWindow*)inWindow
{
if ([string length] == 0)
return;
NSPoint point = [inWindow convertBaseToScreen:windowPoint];
NSScreen* screen = [NSScreen screenForPoint:point];
if (!screen)
screen = [NSScreen mainScreen];
if (!screen)
return;
// register for window losing key status notifications, so we can hide the tooltip
// on window deactivation
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(parentWindowDidResignKey:)
name:NSWindowDidResignKeyNotification
object:inWindow];
NSRect screenFrame = [screen visibleFrame];
NSSize screenSize = screenFrame.size;
// for some reason, text views suffer from hysteresis; the answer you get this time
// depends on what you had in there before. so clear state first.
[mTextView setString:@""];
[mTextView setFrame:NSMakeRect(0, kBorderPadding, 0, 0)];
// -sizeToFit sucks. For some reason it likes to wrap short words, so
// we measure the text by hand and set that as the min width.
NSSize stringSize = [string sizeWithAttributes:[NSDictionary dictionaryWithObject:[NSFont toolTipsFontOfSize:[NSFont smallSystemFontSize]] forKey:NSFontAttributeName]];
float textViewWidth = ceil(stringSize.width);
if (textViewWidth > kMaxTextFieldWidth)
textViewWidth = kMaxTextFieldWidth;
textViewWidth += 2.0 * 5.0; // magic numbers required to make the text not wrap. No, this isn't -textContainerInset.
// set up the text view
[mTextView setMaxSize:NSMakeSize(kMaxTextFieldWidth, screenSize.height - 2 * kBorderPadding)]; // do this here since we know screen size
[mTextView setString:string]; // do this after setting max size, before setting constrained frame size,
// reset to max width - it will not grow horizontally when resizing, only vertically
[mTextView setConstrainedFrameSize:NSMakeSize(kMaxTextFieldWidth, 0.0)];
// to avoid wrapping when we don't want it, set the min width
[mTextView setMinSize:NSMakeSize(textViewWidth, 0.0)];
// finally, do the buggy sizeToFit
[mTextView sizeToFit];
// The first time we sizeToFit a text field on Leopard, it decides that
// 0 would be a good height. We disagree, so make it try again.
NSRect textViewFrame = [mTextView frame];
if (textViewFrame.size.height < 1.0) {
[mTextView sizeToFit];
textViewFrame = [mTextView frame];
}
// set the origin back where its supposed to be
[mTextView setFrameOrigin:NSMakePoint(0, kBorderPadding)];
// size the panel correctly, taking border into account
NSSize textSize = textViewFrame.size;
textSize.height += kBorderPadding + kBorderPadding;
[mTooltipWindow setContentSize:textSize];
// We try to put the top left point right below the cursor. If that doesn't fit
// on screen, put the bottom left point above the cursor.
if (point.y - kVOffset - textSize.height > NSMinY(screenFrame)) {
point.y -= kVOffset;
[mTooltipWindow setFrameTopLeftPoint:point];
}
else {
point.y += kVOffset / 2.5;
[mTooltipWindow setFrameOrigin:point];
}
// if it doesn't fit on screen horizontally, adjust so that it does
float amountOffScreenX = NSMaxX(screenFrame) - NSMaxX([mTooltipWindow frame]);
if (amountOffScreenX < 0) {
NSRect movedFrame = [mTooltipWindow frame];
movedFrame.origin.x += amountOffScreenX;
[mTooltipWindow setFrame:movedFrame display:NO];
}
// add as a child window
[inWindow addChildWindow:mTooltipWindow ordered:NSWindowAbove];
// show the panel
[mTooltipWindow orderFront:nil];
}
- (void)closeToolTip
{
// we can get -closeToolTip even if we didn't show it
if ([mTooltipWindow isVisible])
{
[[mTooltipWindow parentWindow] removeChildWindow:mTooltipWindow];
[mTooltipWindow orderOut:nil];
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)parentWindowDidResignKey:(NSNotification*)inNotification
{
[self closeToolTip];
}
@end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment