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

Method RenameItem

source/script_menu.cpp:749–814  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

747
748
749ResultType UserMenu::RenameItem(UserMenuItem *aMenuItem, LPTSTR aNewName)
750// Caller should specify "" for aNewName to convert aMenuItem into a separator.
751// Returns FAIL if the new name conflicts with an existing name.
752{
753 if (_tcslen(aNewName) > MAX_MENU_NAME_LENGTH)
754 return FAIL; // Caller should display error if desired.
755
756 // Preserve any additional type flags set by options, but exclude the main type bits.
757 // Also clear MFT_OWNERDRAW (if set by the script), since it changes the meaning of dwTypeData.
758 // MSDN: "The MFT_BITMAP, MFT_SEPARATOR, and MFT_STRING values cannot be combined with one another."
759 UINT new_type = (aMenuItem->mMenuType & ~(MFT_BITMAP | MFT_SEPARATOR | MFT_STRING | MFT_OWNERDRAW))
760 | (*aNewName ? MFT_STRING : MFT_SEPARATOR);
761
762 if (!mMenu) // Just update the member variables for later use when the menu is created.
763 {
764 aMenuItem->mMenuType = (WORD)new_type;
765 return UpdateName(aMenuItem, aNewName);
766 }
767
768 MENUITEMINFO mii;
769 mii.cbSize = sizeof(mii);
770 mii.fMask = 0;
771
772 if (*aNewName)
773 {
774 if (aMenuItem->mMenuType & MFT_SEPARATOR)
775 {
776 // Since this item is currently a separator, the system will have disabled it.
777 // Set the item's state to what it should be:
778 mii.fMask |= MIIM_STATE;
779 mii.fState = aMenuItem->mMenuState;
780 }
781 }
782 else // converting into a separator
783 {
784 // Testing shows that if an item is converted into a separator and back into a
785 // normal item, it retains its submenu. So don't set the submenu to NULL, since
786 // it's not necessary and would result in the OS destroying the submenu:
787 //if (aMenuItem->mSubmenu) // Converting submenu into a separator.
788 //{
789 // mii.fMask |= MIIM_SUBMENU;
790 // mii.hSubMenu = NULL;
791 //}
792 }
793
794 mii.fMask |= MIIM_TYPE;
795 mii.fType = new_type;
796 mii.dwTypeData = aNewName;
797
798 // v1.1.04: If the new and old names both have accelerators, call UpdateAccelerators() if they
799 // are different. Otherwise call it if only one is NULL (i.e. accelerator was added or removed).
800 LPTSTR old_accel = _tcschr(aMenuItem->mName, '\t'), new_accel = _tcschr(aNewName, '\t');
801 bool update_accel = old_accel && new_accel ? _tcsicmp(old_accel, new_accel) : old_accel != new_accel;
802
803 // Failure is rare enough in the below that no attempt is made to undo the above:
804 BOOL result = SetMenuItemInfo(mMenu, aMenuItem->mMenuID, FALSE, &mii);
805 UPDATE_GUI_MENU_BARS(mMenuType, mMenu) // Verified as being necessary.
806 if ( !(result && UpdateName(aMenuItem, aNewName)) )

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected