NSButton Behaviors

  • Making progress here, but I came up with another question.

    This might be unusual, but there is a reason for the behavior I am
    trying to achieve -- it works with my interface, trust me!

    I need to create a button that shows its alternate image while the
    button is down and the normal image when the button is released.  The
    "Momentary Change" mode does this.

    HOWEVER, I also need a bound variable (or some similar notification
    method back to the program) to be set based on whether or not the
    button is highlighted -- like a "Toggle" button -- rather than just
    triggering an action on button release.

    I can't identify any of the standard modes that will do this.

    Does this require subclassing NSButton, or is there some other clever
    way to accomplish this feat which I'm currently missing?

    Any hints would be appreciated...

    Thank you!

    -----------------------------------------------------------
    Frank D. Engel, Jr.  <fde101...>

    $ ln -s /usr/share/kjvbible /usr/manual
    $ true | cat /usr/manual | grep "John 3:16"
    John 3:16 For God so loved the world, that he gave his only begotten
    Son, that whosoever believeth in him should not perish, but have
    everlasting life.
    $
  • On Sep 28, 2007, at 9:05 PM, Frank D. Engel, Jr. wrote:

    > HOWEVER, I also need a bound variable (or some similar notification
    > method back to the program) to be set based on whether or not the
    > button is highlighted -- like a "Toggle" button -- rather than just
    > triggering an action on button release.
    >

      How about taking a look at the NSButtonCell that belongs to the
    button? An NSCell (or its subclass, such as NSButtonCell) already has
    an -isHighlighted method. You can always subclass and extend ...

    http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Cla
    sses/NSCell_Class/Reference/Reference.html


      If what you're trying to do seems incredibly difficult, you're
    probably doing it wrong. ;-)

    --
    I.S.
  • I had what I suspect was a similar problem: In an editor interface,
    users could press buttons to specify a selection tool.  Some
    selection tools were simple and some complex.  There could only ever
    be one selection tool in effect at any given moment, so a matrix of
    radio buttons for selection tools was ideal.  However, I needed to
    indicate which selection tool was in effect with a different icon
    that the icon use to indicate the selection tool was merely
    available.  If you click the button for the tool that is currently in
    effect, the result is no tool in effect.  With all this behavior, a
    simple matrix of radio buttons is insufficient.  I think this is very
    similar to your needs.

    I put the buttons in a matrix in "Track" mode and used the following
    code in the action method that was sent by pressing any of the
    selection tool buttons: (The code ensures exclusive selection and
    works if the matrix itself sends the action or if the contained
    buttons send the action)

    - (IBAction)takeCurrentEditorSelectionToolFromRepresentedObject:(id)
    sender
    {
        id          cell;

        if([sender respondsToSelector:@selector(representedObject)] &&
    [sender respondsToSelector:@selector(state)])
        {  // sender provides all of the information needed
          cell = sender;
        }
        else if([sender respondsToSelector:@selector(selectedCell)])
        {  // sender can provide a cell that may have the needed information
          cell =  [sender selectedCell];

          if([sender respondsToSelector:@selector(cells)])
          {  // make selection exclusive or empty
              NSArray        *allCells = [sender cells];
              int            i;

              for(i = [allCells count] - 1; i >= 0; i--)
              {
                if(cell != [allCells objectAtIndex:i])
                {
                    [[allCells objectAtIndex:i] setState:NSOffState];
                }
              }
          }
        }

        if(NSOffState == [cell state])
        {
          [self setCurrentSelectionTool:nil];
        }
        else
        {
          [self setCurrentSelectionTool:[cell representedObject]];
        }
    }
  • Sorry my reply was so long delayed.

    This is completely different from what I need.

    I need to invoke an action in the background *during* the time that
    the user has the mouse down on the button, and stop performing the
    action when the button is released.

    If I am reading this correctly, the code you sent would not allow for
    that?

    On Sep 28, 2007, at 10:10 PM, Erik Buck wrote:

    > I put the buttons in a matrix in "Track" mode and used the
    > following code in the action method that was sent by pressing any
    > of the selection tool buttons: (The code ensures exclusive
    > selection and works if the matrix itself sends the action or if the
    > contained buttons send the action)

    -----------------------------------------------------------
    Frank D. Engel, Jr.  <fde101...>

    $ ln -s /usr/share/kjvbible /usr/manual
    $ true | cat /usr/manual | grep "John 3:16"
    John 3:16 For God so loved the world, that he gave his only begotten
    Son, that whosoever believeth in him should not perish, but have
    everlasting life.
    $
  • I tried setting up a binding against the "highlighted" of the [btn
    cell], but that doesn't seem to work; I'm not sure if "highlighted"
    is not applicable to NSButtonCell, or if I did something silly, but
    at any rate I can't seem to get it working with a binding.

    I also tried a binding to the "state" but that doesn't help much
    either, considering what I get out of it.  I tried both "highlighted"
    and "state" with different button behaviors and use NSLog to watch
    what was happening, but I couldn't find anything that would do what I
    needed.

    I don't mind doing a subclass, but since I'm still adjusting to Cocoa
    somewhat, could someone give a hint as to what exactly it is that I
    should be overriding?

    I'm thinking about looking for a way to override the "highlighted"
    methods of the cell and just calling the superclass version while
    using NSLog to see if anything actually happens, but I'm not sure if
    that's a good approach here or not.

    Considering that the binding didn't seem to result in any calls from
    any mode of button behavior, I suspect that the "highlighted" isn't
    being used for buttons at all, so I'm rather doubtful that this is a
    worthwhile pursuit?

    On Sep 28, 2007, at 9:12 PM, I. Savant wrote:

    > On Sep 28, 2007, at 9:05 PM, Frank D. Engel, Jr. wrote:
    >
    >> HOWEVER, I also need a bound variable (or some similar
    >> notification method back to the program) to be set based on
    >> whether or not the button is highlighted -- like a "Toggle" button
    >> -- rather than just triggering an action on button release.
    >>
    >
    > How about taking a look at the NSButtonCell that belongs to the
    > button? An NSCell (or its subclass, such as NSButtonCell) already
    > has an -isHighlighted method. You can always subclass and extend ...
    >
    > http://developer.apple.com/documentation/Cocoa/Reference/
    > ApplicationKit/Classes/NSCell_Class/Reference/Reference.html
    >
    > If what you're trying to do seems incredibly difficult, you're
    > probably doing it wrong. ;-)
    >
    > --
    > I.S.
    >
    >
    >
    >

    -----------------------------------------------------------
    Frank D. Engel, Jr.  <fde101...>

    $ ln -s /usr/share/kjvbible /usr/manual
    $ true | cat /usr/manual | grep "John 3:16"
    John 3:16 For God so loved the world, that he gave his only begotten
    Son, that whosoever believeth in him should not perish, but have
    everlasting life.
    $
  • I was about to code up a simple NSButton subclass that sends its
    action message continuously while held down, but then I checked the
    documentation:

    See NSButton: http://developer.apple.com/documentation/Cocoa/
    Reference/ApplicationKit/Classes/NSButton_Class/Reference/Reference.html

    "setPeriodicDelay:interval:
    Sets the message delay and interval periods for a continuous button.

    - (void)setPeriodicDelay:(float)delay interval:(float)interval

    Parameters

    delay
    The amount of time (in seconds) that a continuous button will pause
    before starting to periodically send action messages to the target
    object. The maximum allowed value is 60.0 seconds; if a larger value
    is supplied, it is ignored, and 60.0 seconds is used.

    interval
    The amount of time (in seconds) between each action message. The
    maximum value is 60.0 seconds; if a larger value is supplied, it is
    ignored, and 60.0 seconds is used.

    Discussion

    The delay and interval values are used if the button is configured
    (by a setContinuous: message) to continuously send the action message
    to the target object while tracking the mouse.

    Availability

    Available in Mac OS X v10.0 and later.
    See Also

    – setContinuous: (NSControl)"

    I just made a button continuous in IB, and indeed it sends its action
    message continuously as long as it is held down.
  • Excellent!

    Somehow I missed that option.

    I just did an experiment and I can build on this to get what I need.

    Thank you!

    On Oct 1, 2007, at 8:51 PM, Erik Buck wrote:

    > I was about to code up a simple NSButton subclass that sends its
    > action message continuously while held down, but then I checked the
    > documentation:
    >
    > See NSButton: http://developer.apple.com/documentation/Cocoa/
    > Reference/ApplicationKit/Classes/NSButton_Class/Reference/
    > Reference.html
    >
    > "setPeriodicDelay:interval:
    > Sets the message delay and interval periods for a continuous button.
    >
    > - (void)setPeriodicDelay:(float)delay interval:(float)interval
    >
    > Parameters
    >
    > delay
    > The amount of time (in seconds) that a continuous button will pause
    > before starting to periodically send action messages to the target
    > object. The maximum allowed value is 60.0 seconds; if a larger
    > value is supplied, it is ignored, and 60.0 seconds is used.
    >
    > interval
    > The amount of time (in seconds) between each action message. The
    > maximum value is 60.0 seconds; if a larger value is supplied, it is
    > ignored, and 60.0 seconds is used.
    >
    > Discussion
    >
    > The delay and interval values are used if the button is configured
    > (by a setContinuous: message) to continuously send the action
    > message to the target object while tracking the mouse.
    >
    > Availability
    >
    > Available in Mac OS X v10.0 and later.
    > See Also
    >
    > – setContinuous: (NSControl)"
    >
    > I just made a button continuous in IB, and indeed it sends its
    > action message continuously as long as it is held down.
    >

    -----------------------------------------------------------
    Frank D. Engel, Jr.  <fde101...>

    $ ln -s /usr/share/kjvbible /usr/manual
    $ true | cat /usr/manual | grep "John 3:16"
    John 3:16 For God so loved the world, that he gave his only begotten
    Son, that whosoever believeth in him should not perish, but have
    everlasting life.
    $
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today