Fixed the point argument to -view:stringForToolTip:point:userData: (should be relative to the tracking rectangle, was relative to the window). Added support for displaying tooltips from NSToolTipAttributeName attributes in NSTextView.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@31550 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
David Chisnall 2010-10-22 16:38:29 +00:00
parent 3521c1f7c4
commit 4600fb5bbf
3 changed files with 101 additions and 1 deletions

View file

@ -56,6 +56,10 @@
- (NSToolTipTag) addToolTipRect: (NSRect)aRect
owner: (id)anObject
userData: (void *)data;
/**
* Removes all of the tool tips in a given rectangle.
*/
- (void)removeToolTipsInRect: (NSRect)aRect;
/** Return the number of tooltip rectangles active.
*/

View file

@ -28,6 +28,7 @@
#import <Foundation/NSGeometry.h>
#import <Foundation/NSString.h>
#import <Foundation/NSTimer.h>
#import <Foundation/NSIndexSet.h>
#import "AppKit/NSAttributedString.h"
#import "AppKit/NSBezierPath.h"
@ -38,6 +39,7 @@
#import "AppKit/NSPanel.h"
#import "GNUstepGUI/GSTrackingRect.h"
#import "GSToolTips.h"
#import "GSFastEnumeration.h"
@interface NSWindow (GNUstepPrivate)
@ -288,9 +290,16 @@ static BOOL restoreMouseMoved;
if ([[provider object] respondsToSelector:
@selector(view:stringForToolTip:point:userData:)] == YES)
{
// According to the Apple docs, the point is relative to the tracking
// rectangle
NSPoint p = [theEvent locationInWindow];
NSPoint origin =
[view convertRect: [provider viewRect] toView: nil].origin;
p.x -= origin.x;
p.y -= origin.y;
toolTipString = [[provider object] view: view
stringForToolTip: [theEvent trackingNumber]
point: [theEvent locationInWindow]
point: p
userData: [provider data]];
}
else
@ -393,6 +402,28 @@ static BOOL restoreMouseMoved;
toolTipTag = -1;
}
- (void)removeToolTipsInRect: (NSRect)aRect
{
NSUInteger idx = 0;
NSMutableIndexSet *indexes = [NSMutableIndexSet new];
FOR_IN(GSTrackingRect*, rect, ((NSViewPtr)view)->_tracking_rects)
if ((rect->owner == self) && NSContainsRect(aRect, rect->rectangle))
{
RELEASE((GSTTProvider*)rect->user_data);
rect->user_data = 0;
[indexes addIndex: idx];
[rect invalidate];
}
idx++;
END_FOR_IN(((NSViewPtr)view)->_tracking_rects)
[((NSViewPtr)view)->_tracking_rects removeObjectsAtIndexes: indexes];
if ([((NSViewPtr)view)->_tracking_rects count] == 0)
{
((NSViewPtr)view)->_rFlags.has_trkrects = 0;
}
[indexes release];
}
- (void) removeToolTip: (NSToolTipTag)tag
{
NSEnumerator *enumerator;
@ -406,6 +437,7 @@ static BOOL restoreMouseMoved;
RELEASE((GSTTProvider*)rect->user_data);
rect->user_data = 0;
[view removeTrackingRect: tag];
return;
}
}
}

View file

@ -87,6 +87,8 @@
#import "AppKit/NSWindow.h"
#import "GSGuiPrivate.h"
#import "GSTextFinder.h"
#import "GSToolTips.h"
#import "GSFastEnumeration.h"
/*
@ -3836,8 +3838,70 @@ Figure out how the additional layout stuff is supposed to work.
turnedOn: _drawInsertionPointNow];
}
}
// Remove any existing tooltips in the redrawn rectangle.
[[GSToolTips tipsForView: self] removeToolTipsInRect: rect];
{
NSRange r;
NSUInteger i = drawnRange.location;
NSUInteger end = i + drawnRange.length;
while (i < end)
{
// Find the next tooltip
id text = [_textStorage attribute: NSToolTipAttributeName
atIndex: i
effectiveRange: &r];
// Give up if there are not tooltips
if (nil == text) { return; }
// If there is one, find the rectangles it uses.
NSUInteger rectCount;
NSRectArray rects =
[_layoutManager rectArrayForCharacterRange: r
withinSelectedCharacterRange: NSMakeRange(0, 0)
inTextContainer: _textContainer
rectCount: &rectCount];
NSUInteger j;
// Add this object as the tooltip provider for each rectangle
for (j=0 ; j<rectCount ; j++)
{
[self addToolTipRect: rects[j]
owner: self
userData: nil];
}
i = r.location + r.length;
}
}
}
- (NSString*)view: (NSView *)view
stringForToolTip: (NSToolTipTag)tag
point: (NSPoint)point
userData: (void*)userData
{
// Find the rectangle for this tag
FOR_IN(GSTrackingRect*, rect, _tracking_rects)
if (rect->tag == tag)
{
// Origin is in window coordinate space
NSPoint origin = rect->rectangle.origin;
// Point is an offset from this origin - translate it to the window's
// coordinate space
point.x += origin.x;
point.y += origin.y;
// Then translate it to the view's coordinate space
point = [self convertPoint: point
fromView: nil];
// Find out what the corresponding text is.
NSUInteger startIndex = [self _characterIndexForPoint: point
respectFraction: NO];
// Look up what the tooltip text should be.
return [_textStorage attribute: NSToolTipAttributeName
atIndex: startIndex
effectiveRange: NULL];
}
END_FOR_IN(((NSViewPtr)view)->_tracking_rects)
return nil;
}
- (void) updateInsertionPointStateAndRestartTimer: (BOOL)restartFlag
{