Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Submenu woes are over!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Submenu woes are over!




On Oct 28, 2006, at 12:11 PM, David Walters wrote:

Hi List,

http://www.carbondev.com/site/?page=Programming+Menus

As I feared, the code you posted has some issues:

- It leaks the strings you create with CFStringCreateWithCString. For simple sample code like this, there's no reason to use C-strings to create CFStrings when you can just define CFStrings directly with CFSTR. Keep sample code as simple as possible so people don't get sidetracked by issues that aren't relevant to what you're trying to demonstrate.

- The following is only likely to confuse people:

  int basemenuindex = 201;
  MenuRef specialmenuref;
  CreateNewMenu(++basemenuindex, 0, &specialmenuref);

First, basemenuindex is a MenuID, not an index. Second, it's pointless to assign it a value and then increment it before using it. This may have had value in your experimentation or your application, but it's pointless and possibly confusing in sample code. IMO, the following would be better:

const
MenuID	kSampleMenuID = 201;
MenuRef	sampleMenu;
CreateNewMenu(kSampleMenuID, 0, &sampleMenu);

I used MenuID because the declaration of CreateNewMenu uses MenuID. Being consistent never hurts when dealing with newbies.

FWIW, the occasional uppercase character tends to make variable and function names easier for some people to read.

Thanks to Larry and Rosya, who quite patiently told me the same thing over and over until I finally got it :)

Well, you fancy know-everything-because-you're-so-experienced-on- other-platforms people sometimes take a while to stop acting like you know everything and start paying attention to what we low level Mac programmers who have been doing this for years are trying to tell you. Welcome to the Mac. :-)


Larry


_david

On 29/10/2006, at 3:03 AM, Laurence Harris wrote:


On Oct 28, 2006, at 10:21 AM, David Walters wrote:

On 28/10/2006, at 10:22 PM, Rosyna wrote:

Ack, at 10/28/06, David Walters said:

I'm afraid I'm a little slow on the uptake with your answer, and I tried passing a 0 to every function that required a menuID - but I couldn't get it to run.

That's because SetMenuItemHierarchicalMenu() doesn't take a menu id. It takes a MenuItemIndex, you're passing 0, which isn't a valid index in this case.


Notice the text you quoted from the Menu Manager Reference and the sample code you pasted. The sample code correctly passes an index (4). The index is the menu item you want to attach the submenu to.

ok, how do we know that 4 is correct?

I don't have that code in front of me, but 4 is just the fourth menu item.


I don't see the same mechanics you do because I can't find a reason why 4 is the correct index to pass in the example from apple.

I noticed Larry was using Appendblahblah instead of Insertblahblah,

This works fine too:

void MenuTest()
{
MenuRef barMenu, subMenu;
CFStringRef mainItems[] = { CFSTR("m1"), CFSTR("m2"), CFSTR ("m3"), CFSTR("m4"), CFSTR("m5") },
subItems[] = { CFSTR("s1"), CFSTR("s2"), CFSTR("s3"), CFSTR ("s4"), CFSTR("s5") };

CreateNewMenu( 2020, 0, &barMenu );
SetMenuTitleWithCFString( barMenu, CFSTR("Test") );
InsertMenu( barMenu, 0 );

for ( int i = 0; i < 5; ++i )
{
InsertMenuItemTextWithCFString( barMenu, mainItems[ i ], 0, 0, 0 );
CreateNewMenu( 0, 0, &subMenu );
InsertMenuItemTextWithCFString( subMenu, subItems[ i ], 0, 0, 0 );
SetMenuItemHierarchicalMenu( barMenu, 1, subMenu );
}
}


and I changed the example and now it works - except for the submenus on "five"... so I can't get Insertblahblah to work.

I can't tell from this exactly what isn't working.

Larry



_______________________________________________ Do not post admin requests to the list. They will be ignored. Carbon-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Submenu woes (From: David Walters <email@hidden>)
 >Re: Submenu woes (From: Rosyna <email@hidden>)
 >Re: Submenu woes (From: David Walters <email@hidden>)
 >Re: Submenu woes (From: Rosyna <email@hidden>)
 >Re: Submenu woes (From: David Walters <email@hidden>)
 >Re: Submenu woes (From: Laurence Harris <email@hidden>)
 >Re: Submenu woes are over! (From: David Walters <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.