• 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: Converting point struct from superview to subview
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Converting point struct from superview to subview


  • Subject: Re: Converting point struct from superview to subview
  • From: Douglas Davidson <email@hidden>
  • Date: Fri, 24 Jun 2005 08:57:13 -0700


On Jun 24, 2005, at 8:37 AM, Ian was here wrote:


I have an NSTextView positioned within an NSView. I am

trying to get the mouse coordinates within the

NSTextView. I'm using the following code in the

NSTextView subclass:


NSPoint    p = [self convertPoint:[NSEvent

mouseLocation] fromView:[self superview]];    // Get

mouse coordinates within the NSTextView.


I'm getting some really strange coordinates. My goal

is to allow a user to double click an image in the

text view and bring up an image editor. I use the

following code to get the image's coordinates, which

seems to work OK:


[[self layoutManager]

locationForGlyphAtIndex:range.location]


This has been driving me nuts!! Any thoughtful

suggestions would be greatly appreciated. Thank you.



First, none of this is actually necessary for handling clicks on attachments in a text view.  NSTextView has delegate methods, -textView:clickedOnCell:inRect:atIndex: and -textView:doubleClickedOnCell:inRect:atIndex:, which you can implement to avoid having to deal with anything about events or locations.

Second, even if you do deal with events and locations, you probably don't want [NSEvent mouseLocation].  It returns the current mouse location in screen coordinates, independent of any events.  The much more usual Cocoa pattern is to receive an event (mouse down, mouse moved, etc.) and retrieve a mouse location from it.  Second, even if you did want to use +mouseLocation, it's in screen coordinates, not your superview's coordinates.  

When you handle an event, remember that (a) events express their location in window coordinates, and (b) the layout manager works exclusively in container coordinates, which are not the same as NSTextView's view coordinates--they are offset by the text view's textContainerOrigin.  The LayoutManagerDemo example shows the whole process very clearly.

Douglas Davidson

 _______________________________________________
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

References: 
 >Converting point struct from superview to subview (From: Ian was here <email@hidden>)

  • Prev by Date: Re: Converting point struct from superview to subview
  • Next by Date: Re: What type of window for this?
  • Previous by thread: Re: Converting point struct from superview to subview (SOLVED)
  • Next by thread: What type of window for this?
  • Index(es):
    • Date
    • Thread