# getMenuForIndex called twice on focus change

**URL:** <https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149>\
**Category:** MacOSX and iOS\
**Created:** [July 12, 2011, 11:01am UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149 "2011-07-12T11:01:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![tim\_is\_tof](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tim\_is\_tof](https://forum.juce.com/u/tim_is_tof)\
**Post date:** [July 12, 2011, 11:01am UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/1 "2011-07-12T11:01:36Z")

</div>

Hello,

It seems that when using the native osx menu bar, The MenuBarModel::getMenuForIndex function is called on every focus change. Well, it’s being called twice actually. The function will not get called when clicking on any of the menu’s, as happens when using the juce menu instead.

In the application I’m developing, there are a number of elements in a grid. Each element can have different settings, that should be reflected in the main menu. For reasons unclear to me, switching focus on these elements doesn’t cause the menu callback function to be called, resulting in a incorrectly filled-out menu.

I’m curious to know what actually causes the getMenuForIndex to be called. The version i’m using is the git version as of 14-05-2011. Any significant between then and the release?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 12, 2011, 11:12am UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/2 "2011-07-12T11:12:27Z")

</div>

It’s called whenever anything loses or gains focus… If you can find a situation where it doesn’t work, please give me some example code so I can try it myself.

---

<div class="post-metadata">

**Author:** ![tim\_is\_tof](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tim\_is\_tof](https://forum.juce.com/u/tim_is_tof)\
**Post date:** [July 12, 2011, 11:54am UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/3 "2011-07-12T11:54:26Z")

</div>

Hmm… I presume this is because you don’t receive any callbacks when the user clicks on the menu, so you have to fill it pre-emtively?

The problematic code is rather complex and to tell the thruth, I don’t know how it works 🙂 I quess it’s due to something having the wrong order… Could you point me to where the code that initiates the menu update is?

---

<div class="post-metadata">

**Author:** ![tim\_is\_tof](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tim\_is\_tof](https://forum.juce.com/u/tim_is_tof)\
**Post date:** [July 12, 2011, 12:24pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/4 "2011-07-12T12:24:09Z")

</div>

Okay I’ve tracked it to Component::grabFocusInternal… That seems pretty obvious. I should be able to get to the bottom of this.  
BTW, the repeated calling of getMenuForIndex happens in the Juce Demo as well.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 12, 2011, 1:13pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/5 "2011-07-12T13:13:01Z")

</div>

There’s nothing inherently wrong with it getting called more than once… Why are you talking about that as if it’s a bug?

---

<div class="post-metadata">

**Author:** ![tim\_is\_tof](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tim\_is\_tof](https://forum.juce.com/u/tim_is_tof)\
**Post date:** [July 12, 2011, 1:41pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/6 "2011-07-12T13:41:42Z")

</div>

No It’s not really a bug, I just meant to point out that the Demo exhibits this behaviour. I’m not stating that there is a bug, I’m just curious why the menu isn’t “filled” when it’s about to open.

If I have a look at the NSMenuDelegate documentation, it seems to me that menuNeedsUpdate: (NSMenu\*) menu should be able to fill the menu just before it’s about to display it. Although it seems this is never called (running osx 10.6)…

The reason why I want to have the menu’s updated this ways is because of a strange threading / timing issue that stems from the stringing of asyncupdates. This eventually returns me the wrong object (the one that was selected previously). The code then proceeds to create a menu based on the wrong object.  
If the menu were to be built prior to displaying it, I’m able to base it on the correct object.

---

<div class="post-metadata">

**Author:** ![tim\_is\_tof](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tim\_is\_tof](https://forum.juce.com/u/tim_is_tof)\
**Post date:** [July 12, 2011, 2:13pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/7 "2011-07-12T14:13:46Z")

</div>

Okay, let me rephrase this.

When running a previous version of our app that compiles against the svn rev. 684 of Juce, the following code is called when clicking on a menu: (juce\_mac\_MainMenu.mm)

`- (void) menuNeedsUpdate: (NSMenu*) menu;
{
    if (JuceMainMenuHandler::instance != 0)
        JuceMainMenuHandler::instance->updateMenus();
}`

The version of juce I having problems with (Git from 14-05-11) has the same code, but the difference is that it isn’t called. This breaks the menu update. Why isn’t this called any longer?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 12, 2011, 2:38pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/8 "2011-07-12T14:38:37Z")

</div>

It’s an OS callback - if it’s not getting called, then the OS isn’t calling it any more… Not sure why!

---

<div class="post-metadata">

**Author:** ![tim\_is\_tof](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tim\_is\_tof](https://forum.juce.com/u/tim_is_tof)\
**Post date:** [July 13, 2011, 11:12am UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/9 "2011-07-13T11:12:08Z")

</div>

I hope it was due to something like the base SDK or Development target, but I switched them both to 10.4 (as was in the build that worked), but the menuNeedsUpdate still isn’t called. It’s not a debug issue, because I can verify that menuItemInvoked is called.

The only seemingly meaningful differences between the two Mac\_MainMenu.mm’s seem to be the not doing in place menu updating (some keyboard focus issue) and changing around some nulptrs.

Am I correct when I think that all communication between OSX and the menu goes through the MenuDelegate?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 13, 2011, 11:30am UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/10 "2011-07-13T11:30:12Z")

</div>

> [@](#):
>
> Am I correct when I think that all communication between OSX and the menu goes through the MenuDelegate?

Yes, I think so.

---

<div class="post-metadata">

**Author:** ![tim\_is\_tof](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tim\_is\_tof](https://forum.juce.com/u/tim_is_tof)\
**Post date:** [July 13, 2011, 1:24pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/11 "2011-07-13T13:24:49Z")

</div>

After some researching i found that creating new submenus is to blame for the lack of updates.  
When changing

[code]void updateSubMenu (NSMenuItem\* parentItem, const PopupMenu& menuToCopy,  
const String& name, const int menuId, const int tag)  
{  
// Note: This method used to update the contents of the existing menu in-place, but that caused  
// weird side-effects which messed-up keyboard focus when switching between windows. By creating  
// a new menu and replacing the old one with it, that problem seems to be avoided…  
NSMenu\* menu = [[NSMenu alloc] initWithTitle: juceStringToNS (name)];

```
    PopupMenu::MenuItemIterator iter (menuToCopy);
    while (iter.next())
        addMenuItem (iter, menu, menuId, tag);

    [menu setAutoenablesItems: false];
    [menu update];
    [parentItem setTag: tag];
    [parentItem setSubmenu: menu];
    [menu release];
}[/code]

```

into

[code] void updateSubMenu (NSMenuItem\* parentItem, const PopupMenu& menuToCopy,  
const String& name, const int menuId, const int tag)  
{  
[parentItem setTag: tag];  
NSMenu\* menu = [parentItem submenu];

```
    [menu setTitle: juceStringToNS (name)];

    while ([menu numberOfItems] > 0)
        [menu removeItemAtIndex: 0];

    PopupMenu::MenuItemIterator iter (menuToCopy);

    while (iter.next())
        addMenuItem (iter, menu, menuId, tag);

    [menu setAutoenablesItems: false];
    [menu update];
}[/code]

```

(that is: reverting the function back to rev 684), I get the updates from OSX again. In the newer code, it seems like you’re replacing the current menu with a new one. But, it seems you don’t set a delegate on this new menu. Regrettably, setting a delegate on the new menu (after [menu release]) doesn’t bring back updates…

_edit_ oops you DO set the delegate… my bad.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 13, 2011, 1:45pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/12 "2011-07-13T13:45:54Z")

</div>

But if the delegate wasn’t working, then none of the menu items would actually work, because the delegate receives the callback when an item is selected…

---

<div class="post-metadata">

**Author:** ![tim\_is\_tof](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tim\_is\_tof](https://forum.juce.com/u/tim_is_tof)\
**Post date:** [July 13, 2011, 2:42pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/13 "2011-07-13T14:42:55Z")

</div>

Yes, of course. But do you have any idea what could be the cause then?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 13, 2011, 2:53pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/14 "2011-07-13T14:53:30Z")

</div>

I don’t know, but I still don’t really understand what the problem is!

It sounds to me like an extra callback was being accidentally triggered by the old code, and your program was relying on that broken behaviour…?

---

<div class="post-metadata">

**Author:** ![tim\_is\_tof](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tim\_is\_tof](https://forum.juce.com/u/tim_is_tof)\
**Post date:** [July 13, 2011, 3:15pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/15 "2011-07-13T15:15:49Z")

</div>

The problem is that the menu’s are now being updated upon focus change, whereas before they where being updated when you actually clicked on the menu, just before displaying it.

More specifically: a _system_ callback isn’t being made anymore since you changed the offending code. As the newer code actually breaks a system callback mechanism, I would hardly say that my program relies on broken / accidental functionality.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 13, 2011, 3:59pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/16 "2011-07-13T15:59:54Z")

</div>

Ok, gotcha… but I really don’t know what to suggest! The change I made is essential, it can’t be removed without breaking things, I guess there’s something a bit screwy inside the Apple menu code. Too deep in other stuff to investigate right now, but keep nagging me!

---

<div class="post-metadata">

**Author:** ![tim\_is\_tof](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tim\_is\_tof](https://forum.juce.com/u/tim_is_tof)\
**Post date:** [July 18, 2011, 2:13pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/17 "2011-07-18T14:13:07Z")

</div>

Good, I’ll keep nagging you in the future 🙂

But for now: do you know exactly what it is that your new code has fixed? In the code it says something about keyboard focus… If we knew that, we could make a workaround that fixes this in another way while still keeping the old code that keeps the apple-menu-callback system alive.

---

<div class="post-metadata">

**Author:** ![tim\_is\_tof](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tim\_is\_tof](https://forum.juce.com/u/tim_is_tof)\
**Post date:** [July 25, 2011, 4:30pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/18 "2011-07-25T16:30:27Z")

</div>

Well? Do you remember what it was?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 25, 2011, 4:59pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/19 "2011-07-25T16:59:55Z")

</div>

I think it was this one:  
[http://www.rawmaterialsoftware.com/viewtopic.php?f=2&t=7350&hilit=mac+menu](http://www.rawmaterialsoftware.com/viewtopic.php?f=2&t=7350&hilit=mac+menu)

---

<div class="post-metadata">

**Author:** ![tim\_is\_tof](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@tim\_is\_tof](https://forum.juce.com/u/tim_is_tof)\
**Post date:** [July 26, 2011, 1:32pm UTC](https://forum.juce.com/t/getmenuforindex-called-twice-on-focus-change/7149/20 "2011-07-26T13:32:50Z")

</div>

If the behaviour described in the thread would cause a crash with the old code I would have no trouble reproducing this behaviour. But it doesn’t happen, so I guess it the new code fixes something else than you suggested.
