Select MenuItem

  • Hi,
    I click on a button and build my own NSMenu then I popup the menu with

        [NSMenu popUpContextMenu:

    It works well. Now I would pre-select a MenuItem so when I click on the
    button the menu pops-up putting the MenuItem under the mouse.
    How can I do that?

    Best Regards
    --
    Lorenzo
    email: <archidea...>
  • On Jan 4, 2008, at 6:01 PM, Lorenzo wrote:

    > Hi,
    > I click on a button and build my own NSMenu then I popup the menu with
    >
    > [NSMenu popUpContextMenu:
    >
    > It works well. Now I would pre-select a MenuItem so when I click on
    > the
    > button the menu pops-up putting the MenuItem under the mouse.
    > How can I do that?

    You should not use a contextual menu to simulate a popup menu.  If
    you want to programmatically trigger a popup menu, create an
    NSPopUpButtonCell, assign it the menu and initial item index you
    want, and then call -[NSPopUpButtonCell performClickWithFrame:inView:]

    The NSPopUpButtonCell can be transient - it's fine to create it right
    when you receive the click and destroy it afterwards.

    -Peter
  • Thanks Peter, I succeded with this code:

        NSPopUpButtonCell *btnCell = [[NSPopUpButtonCell alloc] init];
        [btnCell setMenu:myContextMenu];
        [btnCell performClickWithFrame:[sender frame]
                inView:[sender superview]];

        [btnCell release];
        [myContextMenu release];

    Best Regards
    --
    Lorenzo
    email: <archidea...>

    > From: Peter Ammon <pammon...>
    > Date: Fri, 04 Jan 2008 18:07:27 -0800
    > To: Lorenzo <archidea...>
    > Cc: <cocoa-dev...>
    > Subject: Re: Select MenuItem
    >
    >
    > On Jan 4, 2008, at 6:01 PM, Lorenzo wrote:
    >
    >> Hi,
    >> I click on a button and build my own NSMenu then I popup the menu with
    >>
    >> [NSMenu popUpContextMenu:
    >>
    >> It works well. Now I would pre-select a MenuItem so when I click on
    >> the
    >> button the menu pops-up putting the MenuItem under the mouse.
    >> How can I do that?
    >
    > You should not use a contextual menu to simulate a popup menu.  If
    > you want to programmatically trigger a popup menu, create an
    > NSPopUpButtonCell, assign it the menu and initial item index you
    > want, and then call -[NSPopUpButtonCell performClickWithFrame:inView:]
    >
    > The NSPopUpButtonCell can be transient - it's fine to create it right
    > when you receive the click and destroy it afterwards.
    >
    > -Peter
    >
previous month january 2008 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 31      
Go to today