@@ -842,13 +842,12 @@ bool SDLCALL Win32_CreateMenuBar(SDL_MenuBar *menu_bar)
842842
843843static bool Win32_CreateMenuItemAt (SDL_MenuItem * menu_item , size_t index , const char * name , Uint16 event_type )
844844{
845+ SDL_Menu_CommonData * parent = menu_item -> common .parent ;
845846 PlatformMenuData * menu_platform_data = (PlatformMenuData * )menu_item -> common .parent -> common .platform_data ;
846847 PlatformMenuData * platform_data = CreatePlatformMenuData ((HMENU )menu_platform_data -> self_handle , menu_item -> common .type );
847848 menu_item -> common .platform_data = (void * )platform_data ;
848849 platform_data -> user_event_type = event_type ;
849-
850850 UINT flags = 0 ;
851-
852851 bool top_level_menu = menu_item -> common .parent -> common .type == SDL_MENUBAR ;
853852
854853 if (!top_level_menu ) {
@@ -878,6 +877,11 @@ static bool Win32_CreateMenuItemAt(SDL_MenuItem *menu_item, size_t index, const
878877 platform_data -> self_handle = (UINT_PTR )event_type ;
879878 }
880879
880+ // To add items at the back, we need to set the index to -1.
881+ if (index == parent -> children ) {
882+ index = -1 ;
883+ }
884+
881885 if (!InsertMenuA ((HMENU )menu_platform_data -> self_handle , (UINT )index , flags , platform_data -> self_handle , name )) {
882886 return WIN_SetError ("Unable to append item to Menu." );
883887 }
@@ -894,8 +898,9 @@ static bool Win32_CreateMenuItemAt(SDL_MenuItem *menu_item, size_t index, const
894898static bool Win32_CheckMenuItem (SDL_MenuItem * menu_item )
895899{
896900 PlatformMenuData * platform_data = (PlatformMenuData * )menu_item -> common .platform_data ;
901+ Uint32 i = SDL_GetIndexInMenu (menu_item );
897902
898- if (!CheckMenuItem (platform_data -> owner_handle , ( UINT ) platform_data -> self_handle , MF_BYCOMMAND | MF_CHECKED )) {
903+ if (!CheckMenuItem (platform_data -> owner_handle , i , MF_BYPOSITION | MF_CHECKED )) {
899904 return WIN_SetError ("Unable to check menu item." );
900905 }
901906
@@ -905,19 +910,21 @@ static bool Win32_CheckMenuItem(SDL_MenuItem *menu_item)
905910static bool Win32_UncheckMenuItem (SDL_MenuItem * menu_item )
906911{
907912 PlatformMenuData * platform_data = (PlatformMenuData * )menu_item -> common .platform_data ;
913+ Uint32 i = SDL_GetIndexInMenu (menu_item );
908914
909- if (!CheckMenuItem (platform_data -> owner_handle , ( UINT ) platform_data -> self_handle , MF_BYCOMMAND | MF_UNCHECKED )) {
915+ if (!CheckMenuItem (platform_data -> owner_handle , i , MF_BYPOSITION | MF_UNCHECKED )) {
910916 return WIN_SetError ("Unable to check menu item." );
911917 }
912918
913919 return true;
914920}
915921
916- static bool Win32_MenuItemChecked (SDL_MenuItem * menu_item , bool * checked )
922+ static bool Win32_MenuItemChecked (SDL_MenuItem * menu_item , bool * checked )
917923{
918924 PlatformMenuData * platform_data = (PlatformMenuData * )menu_item -> common .platform_data ;
925+ Uint32 i = SDL_GetIndexInMenu (menu_item );
919926
920- UINT flags = GetMenuState (platform_data -> owner_handle , ( UINT ) platform_data -> self_handle , MF_BYCOMMAND );
927+ UINT flags = GetMenuState (platform_data -> owner_handle , i , MF_BYPOSITION );
921928
922929 if (flags == -1 ) {
923930 return WIN_SetError ("Unable to get menu_item check state." );
@@ -930,8 +937,9 @@ static bool Win32_MenuItemChecked(SDL_MenuItem *menu_item, bool* checked)
930937static bool Win32_MenuItemEnabled (SDL_MenuItem * menu_item , bool * enabled )
931938{
932939 PlatformMenuData * platform_data = (PlatformMenuData * )menu_item -> common .platform_data ;
940+ Uint32 i = SDL_GetIndexInMenu (menu_item );
933941
934- UINT flags = GetMenuState (platform_data -> owner_handle , ( UINT ) platform_data -> self_handle , MF_BYCOMMAND );
942+ UINT flags = GetMenuState (platform_data -> owner_handle , i , MF_BYPOSITION );
935943
936944 if (flags == -1 ) {
937945 return WIN_SetError ("Unable to get menu_item check state." );
@@ -945,8 +953,9 @@ static bool Win32_MenuItemEnabled(SDL_MenuItem *menu_item, bool *enabled)
945953static bool Win32_EnableMenuItem (SDL_MenuItem * menu_item )
946954{
947955 PlatformMenuData * platform_data = (PlatformMenuData * )menu_item -> common .platform_data ;
956+ Uint32 i = SDL_GetIndexInMenu (menu_item );
948957
949- if (!EnableMenuItem (platform_data -> owner_handle , ( UINT ) platform_data -> self_handle , MF_BYCOMMAND | MF_ENABLED )) {
958+ if (!EnableMenuItem (platform_data -> owner_handle , i , MF_BYPOSITION | MF_ENABLED )) {
950959 return WIN_SetError ("Unable to enable menu item." );
951960 }
952961
@@ -956,8 +965,9 @@ static bool Win32_EnableMenuItem(SDL_MenuItem *menu_item)
956965static bool Win32_DisableMenuItem (SDL_MenuItem * menu_item )
957966{
958967 PlatformMenuData * platform_data = (PlatformMenuData * )menu_item -> common .platform_data ;
968+ Uint32 i = SDL_GetIndexInMenu (menu_item );
959969
960- if (!EnableMenuItem (platform_data -> owner_handle , ( UINT ) platform_data -> self_handle , MF_BYCOMMAND | MF_GRAYED )) {
970+ if (!EnableMenuItem (platform_data -> owner_handle , i , MF_BYPOSITION | MF_GRAYED )) {
961971 return WIN_SetError ("Unable to enable menu item." );
962972 }
963973
@@ -967,13 +977,14 @@ static bool Win32_DisableMenuItem(SDL_MenuItem *menu_item)
967977static bool Win32_DestroyMenuItem (SDL_MenuItem * menu_item )
968978{
969979 PlatformMenuData * platform_data = (PlatformMenuData * )menu_item -> common .platform_data ;
980+ Uint32 i = SDL_GetIndexInMenu (menu_item );
970981
971982 if (menu_item -> common .type == SDL_MENUBAR ) {
972983 if (!DestroyMenu ((HMENU )platform_data -> self_handle )) {
973984 return WIN_SetError ("Unable to remove menu item." );
974985 }
975986 } else {
976- DeleteMenu ((HMENU )platform_data -> self_handle , platform_data -> user_event_type , MF_BYCOMMAND );
987+ DeleteMenu ((HMENU )platform_data -> self_handle , i , MF_BYPOSITION );
977988 }
978989
979990 SDL_free (menu_item -> common .platform_data );
0 commit comments