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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to