Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Is NSTextView dragging from self to self?

Is NSTextView dragging from self to self?
Thread Tools
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Jul 26, 2004, 07:11 PM
 
Is there any way to query an NSTextView to determine if it is currently in the process of doing a drag operation from itself to itself? Ie, currently performing a drop operation for the purpose of rearranging it's content (without deleting or adding any content).

What I want to be able to do is something like this (using the existing NSTextView delegate method shown below, because it is already working for all the other conditions I have in place):

Code:
- (BOOL)textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString { if ( [aTextView isDoingSelfContainedDragOperation] || <other coditions> ) return YES; return NO; }
Where "isDoingSefContainedDragOperation" is a fictitious method of my NSTextView subclass. How could I write such a fictitious method to return YES if the text view is in the process of performing a drop of some of its own dragged text into itself?
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 27, 2004, 12:03 AM
 
A dragging destination will receive NSDraggingDestination messages. The argument to these methods can be used to find out the source of the drag.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Brass  (op)
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Jul 27, 2004, 12:59 AM
 
Thanks Chuckit. I'm now overriding "performDragOperation" and "concludeDragOperation" (and invoking super's implementation in both cases).

However, I can't figure out how to determine the point in the text where the drop is going to happen.

I need to make sure that it can only be dropped in certain places within the text. I'm still scouring the doco for this one, but if anyone can save me the time, I'd be grateful.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 28, 2004, 02:38 PM
 
Sorry I'm late in responding. Don't know if you've figured this out yet, but here is some code for getting a character index from a point in an NSTextView. It's generalized from the highlighting method in Apple's LayoutManagerDemo example. (Note: I actually just recently wrote this, and I haven't had a chance to test it yet in the project it's meant for, but it ought to work.)

Code:
@implementation NSTextView ( PointToCharIndex ) // point is in the view's coordinate system. // flag indicates whether the point is actually on the character // or just near the character - (unsigned)characterIndexForPoint:(NSPoint)point isOnCharacter:(BOOL *)flag { NSLayoutManager *layout = [self layoutManager]; NSTextContainer *container = [self textContainer); unsigned glyphIndex; unsigned charIndex; point.x -= [self textContainerOrigin].x; point.y -= [self textContainerOrigin].y; glyphIndex = [layoutManager glyphIndexForPoint:point inTextContainer:textContainer]; if (flag !- NULL) { *flag = NSPointInRect(point, [layoutManager boundingRectForGlyphRange:NSMakeRange(glyphIndex, 1) inTextContainer:textContainer]); } return [layoutManager characterIndexForGlyphAtIndex:glyphIndex]; } @end
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Brass  (op)
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Jul 28, 2004, 06:01 PM
 
Thanks Chuckit, you're right on the mark again!

I eventually found the hints I needed (convertPoint:fromView: and the layout manager's glyphIndexForPoint:...) at the old Cocoamamasam archives - there's no way I would have found them in Apple's doco myself (and am surprised there is no single NSTextView method to give the text index of there the drag is potentially about to be dropped).

I used these methods in the "draggingUpdated:" method.

Came up with something similar in function to what you suggested, and it works nicely now!

Code:
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender { NSDictionary *attributes; NSRange attributeRange; float fracGlyph; NSPoint dragPoint = [self convertPoint:[sender draggingLocation] fromView:nil]; // nil to convert from window coordinates int dragTextIndex = [[self layoutManager] glyphIndexForPoint:dragPoint inTextContainer:[self textContainer] fractionOfDistanceThroughGlyph:&fracGlyph]; NSAttributedString *theString = [self textStorage]; if ( fracGlyph > 0.5 && dragTextIndex < [theString length] ) dragTextIndex++; if ( dragTextIndex == [theString length] ) // at end of all text and cannot use as an index of the text return [super draggingUpdated:sender]; attributes = [theString attributesAtIndex:dragTextIndex effectiveRange:&attributeRange]; if ( ! [[attributes allKeys] containsObject:@"field_name"] // not in a field attribute || dragTextIndex == ( attributeRange.location + attributeRange.length ) // at end of field || dragTextIndex == attributeRange.location // at begining of field ) { return [super draggingUpdated:sender]; } return NSDragOperationNone; }
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Jul 28, 2004, 10:19 PM
 
Originally posted by Brass:
I eventually found the hints I needed (convertPoint:fromView: and the layout manager's glyphIndexForPoint:...) at the old Cocoamamasam archives - there's no way I would have found them in Apple's doco myself (and am surprised there is no single NSTextView method to give the text index of there the drag is potentially about to be dropped).
Apple is working on making the text system more flexible � and it is a lot more customizeable than it used to be � but there are still a lot of places where it's kind of a black box. NSTextView really doesn't seem meant to be customized very much by hand; only through delegation, and there aren't any delegate methods that need such information.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:16 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,