Dragging in an NSTextView
Dragging in an NSTextView
- Subject: Dragging in an NSTextView
- From: Glen Simmons <email@hidden>
- Date: Tue, 18 Jan 2005 18:17:38 -0600
I'm having a problem with dragging in an NSTextView subclass. I'm
getting an exception when I start the drag:
2005-01-18 17:45:32.697 MyApp[1047] *** -[NSCFDictionary
setObject:forKey:]: attempt to insert nil value
The backtrace from the exception is:
#0 0x97e54864 in -[NSException raise] ()
#1 0x97e547ec in +[NSException raise:format:] ()
#2 0x97df4c5c in -[NSCFDictionary setObject:forKey:] ()
#3 0x97e15cf4 in -[NSMutableDictionary addEntriesFromDictionary:]
()
#4 0x97e46458 in -[NSMutableAttributedString addAttributes:range:]
()
#5 0x93177074 in -[NSTextView attributedSubstringFromRange:] ()
#6 0x93136dac in -[NSTextView(NSPasteboard)
writablePasteboardTypes] ()
#7 0x00029418 in -[MyTextView writablePasteboardTypes] ()
#8 0x9332bed4 in -[NSTextView(NSDragging)
dragSelectionWithEvent:offset:slideBack:] ()
#9 0x00029f0c in -[MyTextView mouseDown:] ()
#10 0x930c1698 in -[NSWindow sendEvent:] ()
#11 0x930a94ec in -[NSApplication sendEvent:] ()
#12 0x930b2418 in -[NSApplication run] ()
#13 0x931605cc in NSApplicationMain ()
#14 0x00009b0c in main (argc=1, argv=0xbffffd34) at
/Users/glen/Development/84 Emulator/Mac/main.m:18
#15 0x000095c4 in _start (argc=1, argv=0xbffffd34, envp=0xbffffd3c)
at /SourceCache/Csu/Csu-47/crt.c:267
#16 0x8fe190f4 in __dyld__dyld_start ()
My subclass overrides -mouseDown: and handles the text selection (1).
If the user clicks on selected text and holds, I start a drag by
calling -dragSelectionWithEvent:offset:slideBack: (2). I don't think
I'm doing anything too odd here, but obviously I'm not making somebody
happy.
Thanks,
Glen
(1) I'm handling the text selection b/c my text is actually a bunch of
NSTextAttachments with small images. NSTextView stinks at selecting
image attachments. Drop a few small images in TextEdit and try to
select the middle one.
(2) Here's the full method:
- (void)mouseDown:(NSEvent *)inEvent {
if ([self isSelectable]) {
NSEvent *newEvent;
int theClickCount = [inEvent clickCount];
unsigned modFlags = [inEvent modifierFlags];
NSRange currentSelRange = [self selectedRange];
NSRect selectedRect;
NSPoint point = [self convertPoint:[inEvent locationInWindow]
fromView:nil];
unsigned theMask = NSLeftMouseUpMask | NSLeftMouseDraggedMask |
NSPeriodicMask;
BOOL clickWasInSelection;
mInitialSelectedRange = currentSelRange;
if (modFlags & NSShiftKeyMask) { // extend the selection
mIsExtendingSelection = YES;
}
// First, see if the click was outside the selected range. If so,
move the selection point, or, if
// the Shift key is down, extend the selection.
selectedRect = [[self layoutManager]
boundingRectForGlyphRange:currentSelRange
inTextContainer:[self textContainer]];
clickWasInSelection = NSPointInRect(point, selectedRect);
if (!clickWasInSelection) {
[self setSelectionToPoint:point
byExtendingSelection:mIsExtendingSelection
stillSelecting:YES];
mInitialSelectedRange = [self selectedRange];
if (theClickCount == 2 && !mIsExtendingSelection) {
[super mouseDown:inEvent];
}
} else {
// Have to see if they hold for a drag
[NSEvent startPeriodicEventsAfterDelay:0.2 withPeriod:0];
newEvent = [[self window] nextEventMatchingMask:theMask];
switch ([newEvent type]) {
case NSLeftMouseUp: {
[self setSelectionToPoint:point
byExtendingSelection:mIsExtendingSelection
stillSelecting:NO];
mInitialSelectedRange.location = NSNotFound;
break;
}
case NSLeftMouseDragged: {
[self setSelectionToPoint:point
byExtendingSelection:mIsExtendingSelection
stillSelecting:YES];
mInitialSelectedRange = [self selectedRange];
break;
}
case NSPeriodic: {
// User clicked and held, if it was on a selection, they want to
drag
BOOL dragSucceeded = NO;
[[NSCursor arrowCursor] push];
dragSucceeded = [self dragSelectionWithEvent:inEvent
offset:NSMakeSize(1.0, 1.0)
slideBack:YES];
[NSCursor pop];
break;
}
default: {
break;
}
}
[NSEvent stopPeriodicEvents];
}
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden