NSButton Subclass - 2 states plus tracking image

  • I need an NSButton with 5 different graphics and two states:

    State 0  (enabled & disabled graphics)
    State 1  (enabled & disabled graphics)
    Pressed/Tracking state

    I was thinking to subclass NSButton and in IB use "Momentary Change" so that
    I can set the alt image to always be my Pressed/Tracking state and the
    subclass will change the main image to one of the other 4 images as needed.

    The problem I see is that Momentary Change buttons are really single state
    so I think my action method for the button would have to also toggle a value
    variable and I may need to override value/setValue to use my custom value.

    If I set up a two state button, how can I provide a custom tracking image...
    Eg when drawRect is called, how can I tell that my button is being pressed
    and tracked?

    Is there an existing class out there that will just do this? I haven't come
    across one.

    Thanks,

    Trygve
  • Hi Trygve,

    I don't you should override -value or anything like that. NSOnState
    and NSOffState should do fine.

    You should draw your pressed art if and only if -isHighlighted is true.

    It's probably appropriate to subclass NSButtonCell for this and draw
    what you like. In IB3, you can select the cell from a button and
    choose a custom subclass.

    Concerning what to override, -drawWithFrame:inView: is the top level
    drawing method for cells, the analog of -drawRect:. -
    drawBezelWithFrame:inView: is called from -drawWithFrame:inView:, and
    is used to draw the part of a button that interior text and images sit
    on. This the glassy looking part of a standard aqua push button, for
    example. Finally, -drawInteriorWithFrame:inView: is also called from -
    drawWithFrame:inView:, and this method draws the text and/or images
    that sit on top of the bezel.

    -Ken

    On Feb 23, 2008, at 4:19 AM, Trygve Inda <cocoa...> wrote:

    > I need an NSButton with 5 different graphics and two states:
    >
    > State 0  (enabled & disabled graphics)
    > State 1  (enabled & disabled graphics)
    > Pressed/Tracking state
    >
    > I was thinking to subclass NSButton and in IB use "Momentary Change"
    > so that
    > I can set the alt image to always be my Pressed/Tracking state and the
    > subclass will change the main image to one of the other 4 images as
    > needed.
    >
    > The problem I see is that Momentary Change buttons are really single
    > state
    > so I think my action method for the button would have to also toggle
    > a value
    > variable and I may need to override value/setValue to use my custom
    > value.
    >
    > If I set up a two state button, how can I provide a custom tracking
    > image...
    > Eg when drawRect is called, how can I tell that my button is being
    > pressed
    > and tracked?
    >
    > Is there an existing class out there that will just do this? I
    > haven't come
    > across one.
    >
    > Thanks,
    >
    > Trygve
  • > Hi Trygve,
    >
    > I don't you should override -value or anything like that. NSOnState
    > and NSOffState should do fine.
    >
    > You should draw your pressed art if and only if -isHighlighted is true.
    >
    > It's probably appropriate to subclass NSButtonCell for this and draw
    > what you like. In IB3, you can select the cell from a button and
    > choose a custom subclass.
    >
    > Concerning what to override, -drawWithFrame:inView: is the top level
    > drawing method for cells, the analog of -drawRect:. -
    > drawBezelWithFrame:inView: is called from -drawWithFrame:inView:, and
    > is used to draw the part of a button that interior text and images sit
    > on. This the glassy looking part of a standard aqua push button, for
    > example. Finally, -drawInteriorWithFrame:inView: is also called from -
    > drawWithFrame:inView:, and this method draws the text and/or images
    > that sit on top of the bezel.
    >
    > -Ken

    How is best to do this in Tiger with IB2?

    T
  • Subclass NSButton and override the -initWithCoder: method. After
    calling super's implementation, replace the cell with one of your
    custom class.

    Mike.

    On 23 Feb 2008, at 23:02, Trygve Inda wrote:

    >> Hi Trygve,
    >>
    >> I don't you should override -value or anything like that. NSOnState
    >> and NSOffState should do fine.
    >>
    >> You should draw your pressed art if and only if -isHighlighted is
    >> true.
    >>
    >> It's probably appropriate to subclass NSButtonCell for this and draw
    >> what you like. In IB3, you can select the cell from a button and
    >> choose a custom subclass.
    >>
    >> Concerning what to override, -drawWithFrame:inView: is the top level
    >> drawing method for cells, the analog of -drawRect:. -
    >> drawBezelWithFrame:inView: is called from -drawWithFrame:inView:, and
    >> is used to draw the part of a button that interior text and images
    >> sit
    >> on. This the glassy looking part of a standard aqua push button, for
    >> example. Finally, -drawInteriorWithFrame:inView: is also called
    >> from -
    >> drawWithFrame:inView:, and this method draws the text and/or images
    >> that sit on top of the bezel.
    >>
    >> -Ken
    >
    > How is best to do this in Tiger with IB2?
    >
    > T