I’m using a PDFViewControlMBS to display PDFs. I want to be able to tell if the user clicked on an annotation. In the MouseDown event I get the x,y coordinates, but they are relative to the control, and this doesn’t work:
dim annotation as PDFAnnotationMBS = page.AnnotationAtPoint(x, y)
I realize I have to convert the point to page space, but how? I’ve tried
but the result is always nil.
Cocoa uses coordinate system with y = 0 being bottom of main screen.
dim e as NSEventMBS = NSApplicationMBS.sharedApplication.currentEvent
This gives you the current event. And there you can get mouseLocation on screen in Cocoa coordinates. Then from there go to window coordinates, to view coordinates and then to page coordinates.
I can get the locationInWindow NSPointMBS from the event object. But what to do with that next isn’t clear. What are “window coordinates” – do you mean the offset for the control in the NSWindow?
I’ve tried this, and tried convertPointToView and convertPointToPage, without success.
Given we have locationInWindow now, can you list the conversion steps to get the correct page coordinates?I can’t find this in any of the MBS examples, so please forgive me if it’s there and I missed it.
I tried it myself:
Function MouseDown(x as Integer, y as Integer, Modifiers as Integer) Handles MouseDown as Boolean
Dim a As NSApplicationMBS = NSApplicationMBS.sharedApplication
Dim n As NSEventMBS = a.currentEvent
Dim v As PDFViewMBS = Me.View
Dim w As NSWindowMBS = Self.NSWindowMBS
Dim p As NSPointMBS = n.locationInWindow
System.DebugLog "p: "+str(p.x)+"/"+str(p.y)
Dim doc As PDFDocumentMBS = v.document
Dim page As PDFPageMBS = doc.pageAtIndex(0)
Dim r As NSPointMBS = v.convertPointFromView(p, Nil)
System.DebugLog "r: "+Str(r.x)+"/"+Str(r.y)
Dim d As NSPointMBS = v.convertPointToPage(r, page)
System.DebugLog "d: "+Str(d.x)+"/"+Str(d.y)
Thanks so much. Yes, this works. I have to say the concept of different spaces (window, page, view) is confusing. From the Apple docs I thought convertPointFromView returned the page space. But I see you then converted whatever was returned (what space is that?) had to be explicitly converted to page space. I’m sure I’ll stumble over this in the future, too, but with this example I think I’ll be able to cope. This would make a nice example!