Skip to content

Commit de3194b

Browse files
committed
Not working, gotta switch
1 parent a4bfb8e commit de3194b

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

examples/video/01-menubar/menubar.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void CreateMenuBar()
5252
SDL_CreateMenuItem(main_bookmarks, "SDL Wiki", SDL_MENU_BUTTON, MENU_BAR_BOOKMARKS_TOOLBAR_WIKI);
5353
SDL_CreateMenuItem(main_bookmarks, "SDL Discord", SDL_MENU_BUTTON, MENU_BAR_BOOKMARKS_TOOLBAR_DISCORD);
5454

55-
SDL_MenuItem* other_bookmarks = SDL_CreateMenuItem(menu, "Other Bookmarks", SDL_MENU, MENU_BAR_LAST);
55+
SDL_MenuItem *other_bookmarks = SDL_CreateMenuItem(main_bookmarks, "Other Bookmarks", SDL_MENU, MENU_BAR_LAST);
5656
SDL_CreateMenuItem(other_bookmarks, "Stack Overflow", SDL_MENU_BUTTON, MENU_BAR_BOOKMARKS_OTHER_BOOKMARKS_STACKOVERFLOW);
5757

5858
SDL_DisableMenuItem(other_bookmarks);

include/SDL3/SDL_video.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,11 +3016,13 @@ typedef union SDL_MenuItem SDL_MenuItem;
30163016

30173017
extern SDL_DECLSPEC SDL_MenuItem *SDL_CreateMenuBar(SDL_Window *window);
30183018

3019-
// Must be a SDL_MENUBAR or SDL_MENU
3019+
// menu_bar_as_item must be a SDL_MENUBAR or SDL_MENU
3020+
// event_type will be ignored if type == SDL_MENU
30203021
// On MacOS, buttoms created under a menubar will go into the "App" submenu
30213022
extern SDL_DECLSPEC SDL_MenuItem *SDL_CreateMenuItemAt(SDL_MenuItem *menu_bar_as_item, size_t index, const char *name, SDL_MenuItemType type, Uint16 event_type);
30223023

3023-
// Must be a SDL_MENUBAR or SDL_MENU
3024+
// menu_bar_as_item must be a SDL_MENUBAR or SDL_MENU
3025+
// event_type will be ignored if type == SDL_MENU
30243026
// On MacOS, buttoms created under a menubar will go into the "App" submenu
30253027
extern SDL_DECLSPEC SDL_MenuItem *SDL_CreateMenuItem(SDL_MenuItem *menu_bar_as_item, const char *name, SDL_MenuItemType type, Uint16 event_type);
30263028

src/video/SDL_video.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6093,13 +6093,21 @@ SDL_MenuItem *SDL_CreateMenuItemAt(SDL_MenuItem *menu_bar_as_item, size_t index,
60936093

60946094
// Get the last item in the list and insert our new item.
60956095
if (menu->child_list) {
6096-
SDL_MenuItem *common = menu->child_list;
6096+
SDL_MenuItem *current = menu->child_list;
60976097

6098-
while (common->common.next)
6099-
common = common->common.next;
6098+
for (size_t i = 1; i < index; ++i) {
6099+
current = current->common.next;
6100+
}
6101+
6102+
SDL_assert(current);
6103+
6104+
if (current->common.next) {
6105+
current->common.next->common.prev = menu_item;
6106+
menu_item->common.next = current->common.next;
6107+
}
61006108

6101-
common->common.next = menu_item;
6102-
menu_item->common.prev = common;
6109+
current->common.next = menu_item;
6110+
menu_item->common.prev = current;
61036111
} else {
61046112
menu->child_list = menu_item;
61056113
}
@@ -6118,7 +6126,7 @@ SDL_MenuItem *SDL_CreateMenuItem(SDL_MenuItem *menu_bar_as_item, const char *nam
61186126
}
61196127

61206128
SDL_Menu_CommonData *menu = (SDL_Menu_CommonData *)menu_bar_as_item;
6121-
return SDL_CreateMenuItemAt(menu_bar_as_item, menu->children, name, type, event_type);
6129+
return SDL_CreateMenuItemAt(menu_bar_as_item, -1, name, type, event_type);
61226130
}
61236131

61246132

src/video/windows/SDL_windowsvideo.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -816,11 +816,6 @@ static PlatformMenuData *CreatePlatformMenuData(HMENU owner_handle, UINT_PTR sel
816816
return platform;
817817
}
818818

819-
static HWND GetHwndFromWindow(SDL_Window *window)
820-
{
821-
return (HWND)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
822-
}
823-
824819
// PlatformMenuData platform_data =
825820

826821
bool SDLCALL Win32_CreateMenuBar(SDL_MenuBar *menu_bar)
@@ -833,8 +828,9 @@ bool SDLCALL Win32_CreateMenuBar(SDL_MenuBar *menu_bar)
833828
}
834829

835830
menu_bar->common.item_common.platform_data = (void*)CreatePlatformMenuData(NULL, (UINT_PTR)menu_handle);
831+
const SDL_WindowData *data = menu_bar->common.item_common.window->internal;
836832

837-
if (!SetMenu(GetHwndFromWindow(menu_bar->common.item_common.window), menu_handle)) {
833+
if (!SetMenu(data->hwnd, menu_handle)) {
838834
WIN_SetError("Unable to set MenuBar");
839835
SDL_free(menu_bar->common.item_common.platform_data);
840836
DestroyMenu(menu_handle);
@@ -877,15 +873,18 @@ static bool Win32_CreateMenuItemAt(SDL_MenuItem *menu_item, size_t index, const
877873
flags |= MF_STRING;
878874
}
879875

876+
flags |= MF_BYPOSITION;
877+
880878
platform_data->self_handle = (UINT_PTR)event_type;
881879
}
882880

883-
if (!AppendMenuA((HMENU)menu_platform_data->self_handle, flags, platform_data->self_handle, name)) {
881+
if (!InsertMenuA((HMENU)menu_platform_data->self_handle, (UINT)index, flags, platform_data->self_handle, name)) {
884882
return WIN_SetError("Unable to append item to Menu.");
885883
}
886884

885+
const SDL_WindowData *data = menu_item->common.window->internal;
887886

888-
if (!DrawMenuBar(GetHwndFromWindow(menu_item->common.window))) {
887+
if (!DrawMenuBar(data->hwnd)) {
889888
return WIN_SetError("Unable to draw menu bar");
890889
}
891890

0 commit comments

Comments
 (0)