How to use `GSToolSelect` `selectInRect:withModifier:`, or how to create a selection

I’m trying to figure out how to use the selectInRect:withModifier: method in GSToolSelect. I made a subclass tool and want to be able to make a custom selection of nodes & handles. When I try to use the method, though, it doesn’t seem to create a selection. Any ideas on what I’m doing wrong or how I should be calling it (if that’s indeed the correct method to use)?

Also, I’m confused about what withModifier accepts. Does it take NSEventModifierFlags (https://developer.apple.com/documentation/appkit/nseventmodifierflags?language=objc)?

Here’s some test code I’m working with:

@interface Hydroplane : GSToolSelect
@end


@implementation Hydroplane

// ...

- (void)mouseDown:(NSEvent *)theEvent {
    NSPoint origin = [theEvent locationInWindow];

    NSRect selection_area = NSMakeRect(
        origin.x,
        origin.y,
        1000.0,
        1000.0
    );

    [self selectInRect:selection_area withModifier:0];
}

// ...

@end

The origin is not calculated correctly. You need to calculate the position in the active layer (position inside the view + position of the layer) and there is a scaling.

So you like to calculate a special selection rect? Or actually your own selection behavior. In the later case, do not overwrite mouseDown: but selectInRect:

The modifier contain information if the shift, command, control or option keys are pressed.

Thanks very much, that’s exactly the information I was looking for!

What I really want to do is create a selection from the following actions:

  1. Click in edit area
  2. Hold Shift
  3. Click in edit area
  4. All nodes in the rect delimited by click 1 point and click 2 point are selected

In that case, would it make sense for modifierFlag to have type NSEventModifierFlags (or some custom type) instead of NSUInteger to make the code more self-documenting? Right now it’s hard to infer that from the method signature:

- (void)selectInRect:(NSRect)Rect withModifier:(NSUInteger)modifierFlag;

Thanks again for your help!

You can use this to get the location relative to the currently active layer:

NSPoint clickedPoint = [_editViewController.graphicView getActiveLocation:theEvent];

The NSEventModifierFlags type is only available since macOS 10.10 and I didn’t touched that part of the code since I needed to compile on 10.9.