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

Re: Submenu woes



On 28/10/2006, at 7:34 PM, Rosyna wrote:
The item at index 0 is the title of the menu. Menus are index 1 based, so if you want the first item in specialmenuref to have a submenu, pass 1 as the index instead of zero. In fact, SetMenuItemHierarchicalMenu() may be returning an error in your code.


Sorry, the OSStatus I get back from SetMenuItemHierarchicalMenu() is zero. 

Also, since you are using SetMenuItemHierarchicalMenu(), you don't need to use InsertMenu() afterwards.

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.

First, I make a new project : a Carbon C++ Application
Then I open main.cpp, and I paste the code I sent the list before, (or the latest version which is below) - at line 13,
and I add a line at MainWindow::Create (which is now line 80), calling the new function.

  MenuTest();

One thing I can tell you is that if you do it this way, you need to set up your windows etc before you instantiate your MainWindow class. 

So 
MenuTest();  
goes above 
MainWindow* wind = new MainWindow();

If you don't, you will get no menus and no explanation!

I press command-y, the project compiles and begins debugging, the window appears, and the menuitems entered at root level work as expected, I click them and down drops a submenu. That works.

(screenshot removed in the name of netiquette)

Yet, for some strange reason, the "Special Menu" does not work as I have assumed in the code, based on a Carbon app I got from Apple, called Menu Views - ( http://developer.apple.com/samplecode/MenuViews/listing5.html

(screenshot removed, but I have it if you need proof that i can run it without errors)
In the picture above, I have clicked the "Special" menu, and nothing dropped down, rather disappointingly :(


FYI,

Discussion

Using SetMenuItemHierarchicalMenu, it is possible to directly specify the submenu for a menu item without specifying its menu ID. It is not necessary to insert the submenu into the hierarchical portion of the menubar, and it is not necessary for the submenu to have a unique menu ID. Simply use 0 as the menu ID for the submenu, and identify selections from the menu by command ID.

(from the Menu Manager Reference, ADC Oct 06) - SetMenuItemHierarchicalMenu


//---------------------------------------------------------------------------------------------------------
// • CreateSubmenus
// Creates some sample submenus.
//---------------------------------------------------------------------------------------------------------
static void CreateSubmenus( MenuRef topMenu, int count )
{
int i;
MenuRef menu = topMenu;


for ( i = 1; i <= count; i++ )
{
MenuRef submenu;


DuplicateMenu( topMenu, &submenu );
SetMenuItemHierarchicalMenu( menu, 4, submenu );
menu = submenu;
}


// clear the submenu link in the bottom-most menu (which otherwise will point to the first submenu)
SetMenuItemHierarchicalMenu( menu, 4, NULL );
}


Here is a slightly different, but not working, version of the generic menu maker... this is what I pasted into line 13 of main.cpp (I didn't do anything else to the boilerplate c++ application that xcode built for me)

#define DOES_NOT_WORK
void MenuTest();
bool isSpecialCommandsSubMenu(char * menustring);
bool isSpecialCommandsSubMenu(char * menustring){
  return (menustring[0] == 'f');
}
void MenuTest() {
  char * menustrings[5] = {"one", "two", "three", "four", "five"};
  char * submenustrings[2] = {"subone", "subtwo"};
  int basemenuindex = 201;
  MenuRef specialmenuref;
  CreateNewMenu(++basemenuindex, 0, &specialmenuref);
  SetMenuTitleWithCFString(specialmenuref, CFSTR("Special")); 
  InsertMenu(specialmenuref, 0);
  for (int mi = 0; mi < 5; ++mi) {
    MenuRef m1ref;
    CFStringRef m1string = 
      CFStringCreateWithCString(kCFAllocatorDefault, menustrings[mi],0);
    CreateNewMenu(++basemenuindex, 0, &m1ref);
    SetMenuTitleWithCFString(m1ref, m1string); 
    for (int smi = 0; smi < 2; ++smi) {
      CFStringRef m2string = 
      CFStringCreateWithCString(kCFAllocatorDefault,submenustrings[smi],0);
      InsertMenuItemTextWithCFString(m1ref,m2string,0,0,0);
    }
    if (isSpecialCommandsSubMenu(menustrings[mi])) {
#ifdef DOES_NOT_WORK
      SetMenuID(m1ref, 0);
      SetMenuItemHierarchicalMenu(specialmenuref,  0, m1ref);
      InsertMenu(m1ref, kInsertHierarchicalMenu);
#else
      InsertMenu(m1ref, 0);
#endif
    } else {
      InsertMenu(m1ref, 0);
    }
  }
  DrawMenuBar();
}


I changed it a little bit



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

    if (isSpecialCommandsSubMenu(menustrings[mi])) {
//here is the problem
      SetMenuID(m1ref, 0);
      SetMenuItemHierarchicalMenu(specialmenuref,  0, m1ref);
      InsertMenu(m1ref, kInsertHierarchicalMenu);
    } else {
//this works fine
      InsertMenu(m1ref, 0);
    }
  }
  DrawMenuBar();
}

I can add menus directly to the root space quite easily, and I think I'm doing what the example code does, it must be something simple?

-- 


Sincerely,
Rosyna Keller
Technical Support/Carbon troll/Always needs a hug

Unsanity: Unsane Tools for Insanely Great People

It's either this, or imagining Phil Schiller in a thong.

 _______________________________________________
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>)



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.