Skip to content

Commit 7c6f511

Browse files
committed
I think it's been fixed.
1 parent de3194b commit 7c6f511

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

include/SDL3/SDL_video.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3025,6 +3025,7 @@ extern SDL_DECLSPEC SDL_MenuItem *SDL_CreateMenuItemAt(SDL_MenuItem *menu_bar_as
30253025
// event_type will be ignored if type == SDL_MENU
30263026
// On MacOS, buttoms created under a menubar will go into the "App" submenu
30273027
extern SDL_DECLSPEC SDL_MenuItem *SDL_CreateMenuItem(SDL_MenuItem *menu_bar_as_item, const char *name, SDL_MenuItemType type, Uint16 event_type);
3028+
extern SDL_DECLSPEC SDL_MenuItem *SDL_CreateMenuItemWithProperties(SDL_MenuItem *menu_bar_as_item, SDL_PropertiesID props);
30283029

30293030
// -1 on error
30303031
extern SDL_DECLSPEC Sint64 SDL_ChildItems(SDL_MenuItem *menu_bar_as_item);

src/video/SDL_sysvideo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,4 +674,6 @@ extern SDL_Capitalization SDL_GetTextInputCapitalization(SDL_PropertiesID props)
674674
extern bool SDL_GetTextInputAutocorrect(SDL_PropertiesID props);
675675
extern bool SDL_GetTextInputMultiline(SDL_PropertiesID props);
676676

677+
extern Uint32 SDL_GetIndexInMenu(SDL_MenuItem *menu_item);
678+
677679
#endif // SDL_sysvideo_h_

src/video/SDL_video.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6095,7 +6095,7 @@ SDL_MenuItem *SDL_CreateMenuItemAt(SDL_MenuItem *menu_bar_as_item, size_t index,
60956095
if (menu->child_list) {
60966096
SDL_MenuItem *current = menu->child_list;
60976097

6098-
for (size_t i = 1; i < index; ++i) {
6098+
for (size_t i = 1; (i < index) && current; ++i) {
60996099
current = current->common.next;
61006100
}
61016101

@@ -6117,6 +6117,24 @@ SDL_MenuItem *SDL_CreateMenuItemAt(SDL_MenuItem *menu_bar_as_item, size_t index,
61176117
return menu_item;
61186118
}
61196119

6120+
6121+
Uint32 SDL_GetIndexInMenu(SDL_MenuItem *menu_item)
6122+
{
6123+
Uint32 i = 0;
6124+
SDL_MenuItem *current = menu_item->common.prev;
6125+
while (current) {
6126+
current = current->common.prev;
6127+
++i;
6128+
}
6129+
return i;
6130+
}
6131+
6132+
6133+
SDL_MenuItem* SDL_CreateMenuItemWithProperties(SDL_MenuItem* menu_bar_as_item, SDL_PropertiesID props)
6134+
{
6135+
return NULL;
6136+
}
6137+
61206138
SDL_MenuItem *SDL_CreateMenuItem(SDL_MenuItem *menu_bar_as_item, const char *name, SDL_MenuItemType type, Uint16 event_type)
61216139
{
61226140
CHECK_MENUITEM_MAGIC(menu_bar_as_item, NULL);
@@ -6126,7 +6144,7 @@ SDL_MenuItem *SDL_CreateMenuItem(SDL_MenuItem *menu_bar_as_item, const char *nam
61266144
}
61276145

61286146
SDL_Menu_CommonData *menu = (SDL_Menu_CommonData *)menu_bar_as_item;
6129-
return SDL_CreateMenuItemAt(menu_bar_as_item, -1, name, type, event_type);
6147+
return SDL_CreateMenuItemAt(menu_bar_as_item, menu->children, name, type, event_type);
61306148
}
61316149

61326150

src/video/windows/SDL_windowsvideo.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -842,13 +842,12 @@ bool SDLCALL Win32_CreateMenuBar(SDL_MenuBar *menu_bar)
842842

843843
static 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
894898
static 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)
905910
static 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)
930937
static 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)
945953
static 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)
956965
static 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)
967977
static 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

Comments
 (0)