Question

I put several controls (button,textfield,...) in a NSBox. is it possible to disable NSBox that user can't access controls (means can't click on button or write in textfield)?

how about nsview ?

Was it helpful?

Solution

Or, if you have a custom NSBox, you can override NSView's -hitTest: (conditionally)

- (NSView *)hitTest:(NSPoint)aPoint {
    if (!enabled) return nil;
    else return [super hitTest:aPoint];
}

To stop the window from sending events to all your subviews.

To provide visual feedback, conditionally drawing some sort of overlay in the custom NSBox's -drawRect method would work.

OTHER TIPS

An NSBox is basically just a view with a border, there's no way to "disable" it. If you want to disable all the controls in a box, you could loop through all its subviews and disable them , or another way I've done this is to put an overlay view over the whole box and override mouseDown in that overlay (to capture any mouseDown events so they aren't queued in the event loop). You can also give the overlay a semi-transparent white color so the box has a disabled appearance.

Yes, you just need to look at the subviews of the NSBox, which is typically just one NSView, and then your actual controls will be under the subviews of that.

Here's a quick C-style function I wrote to enable/disable most common UI controls, including NSBox...

void SetObjEnabled(NSObject * Obj, bool Enabled)
{
    //Universal way to enable/disable a UI object, including NSBox contents

    NSControl * C = (NSControl *)Obj;

    if([C respondsToSelector:@selector(setEnabled:)])
        [C setEnabled:Enabled];

    if([C.className compare:@"NSTextField"] == NSOrderedSame)
    {
        NSTextField * Ct = (NSTextField*)C;
        if(!Enabled)
            [Ct setTextColor:[NSColor disabledControlTextColor]];
        else //Enabled
            [Ct setTextColor:[NSColor controlTextColor]];
    }
    else if([C.className compare:@"NSBox"] == NSOrderedSame)
    {
        NSBox * Cb = (NSBox*)C;

        //There is typically just one subview at this level
        for(NSView * Sub in Cb.subviews)
        {
            //Here is where we'll get the actual objects within the NSBox
            for(NSView * SubSub in Sub.subviews)
                SetObjEnabled(SubSub, Enabled);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top