Converting NSTextView coordinates to screen coordinates

cocoa, nspopover, nstextview

Solution

I got it.

- (NSRect)rectForPopover
{    
    NSRect rect = [self firstRectForCharacterRange:[self selectedRange]];
    NSRect txtViewBounds = [self convertRectToBacking:[self bounds]];
    txtViewBounds = [self.window convertRectToScreen:txtViewBounds];

    rect = [[self superview] convertRect:rect toView:nil];
    rect = [self.window convertRectToScreen:rect];

    rect.origin = NSMakePoint(rect.origin.x - txtViewBounds.origin.x - self.window.frame.origin.x, rect.origin.y);

    return rect;
}

Problem

In my app I want to show popover in NSTextView and need to convert coordinates of selected rect in NSTextView to screen coordinates. I've tried this code: ``` NSWindow* viewWindow = [self window]; NSRect rect = [self firstRectForCharacterRange:[self selectedRange]]; rect = [[self superview] convertRect:rect toView:nil]; //converting to NSClipView coordinate system rect = [viewWindow convertRectToScreen:rect]; return rect; ``` It works almost good, but returned rect has extremely "broken" origin by "x". E.g., if `rect.origin.x` is 670 at the beginning, in the end it is equal to 1022. Any ideas? Thanks.

Original source