• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTextView paste:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTextView paste:


  • Subject: Re: NSTextView paste:
  • From: Seth Willits <email@hidden>
  • Date: Mon, 13 Aug 2012 17:43:46 -0700

On Aug 13, 2012, at 3:55 PM, ecir hana wrote:

> Thank you for the reply! Unfortunately, there seem to be a tiny bug with undo. My textview has "setRichText:" set to NO so that it triggers the first condition. When the textview looks like this:
>
> aaa
> bbb
>
> and I select and copy "aaa", then select "bbb", delete it by hitting "<--", paste "aaa", and then I try to undo, it does nothing, i.e. the last line remains "bbb", instead of "". In menubar, there it even says "Undo Paste" but it does nothing.
>
> Any idea would that could be?


The insertText: isn't being registered with the undo manager properly. That was the wrong method to use. (NSTextView & friends can be confusing.) Pretty sure this is always correct:


- (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard type:(NSString *)type
{
	if ([type isEqual:NSStringPboardType]) {
		if ([[pboard stringForType:type] isEqual:@"foo"]) {
			NSRange range = [self selectedRange];

			if ([self shouldChangeTextInRange:range replacementString:@"bar"]) {
				[self replaceCharactersInRange:range withString:@"bar"];
			}

			return YES;
		}
	}

	return [super readSelectionFromPasteboard:pboard type:type];
}





But in case I missed something else again, a simple workaround is to just swizzle the string on the pasteboard like so:


- (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard type:(NSString *)type
{
	if ([type isEqual:NSStringPboardType]) {
		if ([[pboard stringForType:type] isEqual:@"foo"]) {
			BOOL result;

			[pboard setString:@"bar" forType:NSStringPboardType];
			result = [super readSelectionFromPasteboard:pboard type:type];
			[pboard setString:@"foo" forType:NSStringPboardType];

			return result;
		}
	}

	return [super readSelectionFromPasteboard:pboard type:type];
}


;-)



But really, the first one should be fine.



--
Seth Willits




_______________________________________________

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

  • Follow-Ups:
    • Re: NSTextView paste:
      • From: ecir hana <email@hidden>
References: 
 >NSTextView paste: (From: ecir hana <email@hidden>)
 >Re: NSTextView paste: (From: Seth Willits <email@hidden>)
 >Re: NSTextView paste: (From: ecir hana <email@hidden>)

  • Prev by Date: Re: Help Indexer error - what does it MEAN?
  • Next by Date: Re: Using NSPipe to get system command output
  • Previous by thread: Re: NSTextView paste:
  • Next by thread: Re: NSTextView paste:
  • Index(es):
    • Date
    • Thread