0

In my program I am able to determine whether a mouseclick was made within a certain NSRect. How can I open a new NSWindow by clicking this NSRect?

2 Answers2

2

If you want to display an existing window (which you created with Interface Builder) you just call makeKeyAndOrderFront on your window object.
If you want to create a new window programmatically you find an answer here.

Community
  • 1
  • 1
Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
0

To handle events, you'd implement the relevant methods of NSResponder in your NSView or NSViewController subclass. For instance, you could implement mouseDown: and -mouseUp: to handle mouse clicks (in a fairly simplistic manner), like so:

- (void) mouseDown: (NSEvent *) event
{
    if ( [event type] != NSLeftMouseDown )
    {
        // not the left button, let other things handle it
        [super mouseDown: event];
        return;
    }

    NSPoint location = [self convertPoint: [event locationInWindow] fromView: nil];
    if ( !NSPointInRect(location, self.theRect) )
    {
        [super mouseDown: event];
        return;
    }

    self.hasMouseDown = YES;
}

- (void) mouseUp: (NSEvent *) event
{
    if ( (!self.hasMouseDown) || ([event type] != NSLeftMouseUp) )
    {
        [super mouseUp: event];
        return;
    }

    NSPoint location = [self convertPoint: [event locationInWindow] fromView: nil];
    if ( !NSPointInRect(location, self.theRect) )
    {
        [super mouseDown: event];
        return;
    }

    self.hasMouseDown = NO;

    // mouse went down and up within the target rect, so you can do stuff now
}
Jim Dovey
  • 11,166
  • 2
  • 33
  • 40