Popup Button question

Discussion in 'Mac Programming' started by MarkHamilton, Jun 26, 2005.

  1. MarkHamilton macrumors newbie

    Joined:
    Jun 21, 2005
    Location:
    Albuquerque, NM, USA
    #1
    Last question, I promise. (Well, at least for today.)

    I've got a popup button menu with half-a-dozen selections in it. I've got a command event handler that is correctly invoked for each selection. However, I want to retrieve the string associated with each selection for display elsewhere on the window. (I want to define the strings only in the nib file, rather than hardcoding them in the app as well.)

    How do I retrieve the current string? (Yet another instance of a poorly documented control. Is is just me? :confused: )
     
  2. caveman_uk Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #2
    Is this using carbon like the other question?
     
  3. MarkHamilton thread starter macrumors newbie

    Joined:
    Jun 21, 2005
    Location:
    Albuquerque, NM, USA
    #3
    Yes, Carbon. Sorry for not making that clear.
     
  4. MarkHamilton thread starter macrumors newbie

    Joined:
    Jun 21, 2005
    Location:
    Albuquerque, NM, USA
    #4
    Well, if no one has an answer, could someone suggest another venue for asking this question?
     
  5. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #5
    Since you are using Carbon and NIBs here is the simple solution:

    CopyMenuItemTextAsCFString(MenuRef inMenu, MenuItemIndex inItem, CFStringRef * outString);

    This will place in outString the text of the specified menu item. You pass in the menu's MenuRef and the index number (1-based) of the menu item you want to get the text for.

    This is all listed in the header file Menus.h.

    You can quickly look up API functions in their headers by command-double clicking on any function call in Xcode.

    You'd use this API function like so:

    Code:
    {
        CFString myMenuItemText;
    			
        CopyMenuItemTextAsCFString(someMenu, 1, &menuItemText);
    			
    }
    You must get the MenuRef from the pop-up menu control. This would be defined in Controls.h, search for the pop-up menu control section.
     
  6. MarkHamilton thread starter macrumors newbie

    Joined:
    Jun 21, 2005
    Location:
    Albuquerque, NM, USA
    #6
    Thanks, Sayer. That was just what I needed. I clearly need to read through the header files instead of depending on the documentation. ;) Two down, one to go.

    Also, for the lurkers out there, here's the solution I came up with. I hope someone else finds it useful.


    Code:
    OSStatus CommandEH
        (
        EventHandlerCallRef inHandler,
        EventRef event,
        void *ud
        )
    {
        OSStatus err = noErr;
    
        UserDataPtr user_data = (UserDataPtr)ud;
        HICommand commandStruct;
    
        err = GetEventParameter (event, kEventParamDirectObject,
                                             typeHICommand, NULL, sizeof(HICommand),
                                             NULL, &commandStruct);
        if (err != noErr)
            fprintf(stderr, "CantGetEventParameter (HICommand): %ld\n", err);
    
        switch (commandStruct.commandID)
            {
            case 'TA01':
            case 'TA03':
            case 'TA02':
                {
                char in_buff[255];
                CFStringRef string;
                MenuRef    menu_ref = commandStruct.menu.menuRef;
                MenuItemIndex menu_idx = commandStruct.menu.menuItemIndex;
    
                /* This is another way of getting the MenuRef. */
    /*
                {
                ControlID  menu_cid = {'MEH2', 129};
                ControlRef menu_ctl;
    
                err = GetControlByID (user_data->controls_window, &menu_cid, &menu_ctl);
                require_noerr( err, CantGetControl );
    			
                err = GetControlData(menu_cref, kControlMenuPart, kControlPopupButtonMenuRefTag, sizeof(menu_ref), &menu_ref, NULL);
                require_noerr( err, CantGetControl );
                }
    */
    			
                err = CopyMenuItemTextAsCFString(menu_ref, menu_idx, &string);
                require_noerr( err, CantGetMenuText );
    
                /* Here you'd do something real with the string. I'm just printing it as an example. */
                CFStringGetCString (string, in_buff, sizeof(in_buff), GetApplicationTextEncoding());
                fprintf(stderr, "%s\n", in_buff);
    
                CFRelease(string);
                }
                break;
            }
    
        err = CallNextEventHandler( inHandler, event );
    
        return err;
    
    CantGetControl:
        fprintf(stderr, "CantGetControl: %ld\n", err);
        return err;
    CantGetMenuText:
        fprintf(stderr, "CantGetMenuText: %ld\n", err);
        return err;
    }
    
     

Share This Page