MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / ModifyItem

Method ModifyItem

source/script_menu.cpp:633–678  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

631
632
633ResultType UserMenu::ModifyItem(UserMenuItem *aMenuItem, IObject *aCallback, UserMenu *aSubmenu, LPTSTR aOptions)
634// Modify the callback, submenu, or options of a menu item (exactly one of these should be NULL and the
635// other not except when updating only the options).
636// If a menu item becomes a submenu, we don't relinquish its ID in case it's ever made a normal item
637// again (avoids the need to re-lookup a unique ID).
638{
639 if (*aOptions && UpdateOptions(aMenuItem, aOptions) != OK)
640 return FAIL; // UpdateOptions displays an error message if aOptions contains invalid options.
641 if (!aCallback && !aSubmenu) // We were called only to update this item's options.
642 return OK;
643
644 if (aMenuItem->mMenuID >= ID_TRAY_FIRST && aCallback)
645 {
646 // For a custom label to work for this item, it must have a different ID.
647 MENUITEMINFO mii;
648 mii.cbSize = sizeof(mii);
649 mii.fMask = MIIM_ID;
650 mii.wID = g_script.GetFreeMenuItemID();
651 if (mMenu)
652 SetMenuItemInfo(mMenu, aMenuItem->mMenuID, FALSE, &mii);
653 aMenuItem->mMenuID = mii.wID;
654 }
655
656 aMenuItem->mCallback = aCallback; // This will be NULL if this menu item is a separator or submenu.
657 if (aMenuItem->mSubmenu == aSubmenu) // Below relies on this check.
658 return OK;
659
660 if (aSubmenu)
661 aSubmenu->AddRef();
662 if (aMenuItem->mSubmenu)
663 aMenuItem->mSubmenu->Release();
664 aMenuItem->mSubmenu = aSubmenu;
665
666 if (!mMenu)
667 return OK;
668 // Otherwise, since the OS menu exists, one of these is to be done to aMenuItem in it:
669 // 1) Change a submenu to point to a different menu.
670 // 2) Change a submenu so that it becomes a normal menu item.
671 // 3) Change a normal menu item into a submenu.
672 // Replacing or removing a submenu is known to destroy the old submenu as a side-effect,
673 // so instead of changing the submenu directly, remove the item and add it back with the
674 // correct properties.
675 RemoveMenu(mMenu, aMenuItem->mMenuID, MF_BYCOMMAND);
676 InternalAppendMenu(aMenuItem, aMenuItem->mNextMenuItem);
677 return OK;
678}
679
680
681

Callers

nothing calls this directly

Calls 3

GetFreeMenuItemIDMethod · 0.80
AddRefMethod · 0.45
ReleaseMethod · 0.45

Tested by

no test coverage detected