Re: "Tokenizing" NSTextView content [solved]
Re: "Tokenizing" NSTextView content [solved]
- Subject: Re: "Tokenizing" NSTextView content [solved]
- From: Rob Keniger <email@hidden>
- Date: Fri, 19 Jun 2009 17:40:55 +1000
On 19/06/2009, at 12:32 PM, Rob Keniger wrote:
I need to display a text view which will contain HTML-like syntax
and allow the user to drag "objects" into the view.
I have been able to use NSTextAttachment to handle the display of
the dragged objects just fine, but what I want to do is restrict the
user from dragging the object anywhere except between the "tags" in
the document.
It turns out this was easier than I thought.
I subclassed NSTextView and in -dragOperationForDraggingInfo:type: I
check to see if the current location is inside a tag and return
NSDragOperationNone if so.
- (NSDragOperation)dragOperationForDraggingInfo:(id < NSDraggingInfo
>)dragInfo type:(NSString *)type
{
if([type isEqualToString:@"MyType"])
{
//get the current character index that the mouse is over
NSPoint location = [self convertPoint:[dragInfo draggingLocation]
fromView:nil];
NSUInteger currentDragLocation = [self
characterIndexForInsertionAtPoint:location];
//scan forward from the current point to find the location of the
next tag close character
NSString* string = [[self string]
substringFromIndex:currentDragLocation];
NSScanner* scanner = [NSScanner scannerWithString:string];
[scanner scanUpToString:@">" intoString:NULL];
NSUInteger closingTagLocation = [scanner scanLocation];
//rescan to find the next tag open character
[scanner setScanLocation:0];
[scanner scanUpToString:@"<" intoString:NULL];
//if we're inside a tag the closing character will come first
NSUInteger openingTagLocation = [scanner scanLocation];
if(openingTagLocation > closingTagLocation)
return NSDragOperationNone;
}
//otherwise just do the normal thing
return [super dragOperationForDraggingInfo:dragInfo type:type];
}
--
Rob Keniger
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden