Skip to content

Commit ffabd75

Browse files
committed
Most of the App Menu Stuff, need to think about destruction.
1 parent 934adb5 commit ffabd75

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

examples/video/01-menubar/menubar.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,13 @@ void CreateMenuBar_1()
106106
// We can't create a top level checkable .
107107
SDL_assert(!SDL_CreateMenuItem(menu_bar_1, "Incognito", SDL_MENUITEM_CHECKABLE, MENU_BAR_INCOGNITO));
108108

109-
SDL_CreateMenuItem(menu_bar_1, "Exit", SDL_MENUITEM_BUTTON, MENU_BAR_EXIT);
109+
SDL_MenuItem* app_menu = SDL_GetMenuBarAppMenu(menu_bar_1);
110+
if (app_menu) {
111+
SDL_assert(!SDL_CreateMenuItem(menu_bar_1, "Exit", SDL_MENUITEM_BUTTON, MENU_BAR_EXIT));
112+
SDL_CreateMenuItem(app_menu, "Exit", SDL_MENUITEM_BUTTON, MENU_BAR_EXIT);
113+
} else {
114+
SDL_CreateMenuItem(menu_bar_1, "Exit", SDL_MENUITEM_BUTTON, MENU_BAR_EXIT);
115+
}
110116
}
111117

112118
SDL_SetWindowMenuBar(window_1, menu_bar_1);
@@ -147,7 +153,13 @@ void CreateMenuBar_2()
147153
// We can't create a top level checkable .
148154
SDL_assert(!SDL_CreateMenuItem(menu_bar_2, "Incognito_2", SDL_MENUITEM_CHECKABLE, MENU_BAR_INCOGNITO));
149155

150-
SDL_CreateMenuItem(menu_bar_2, "Exit", SDL_MENUITEM_BUTTON, MENU_BAR_EXIT);
156+
SDL_MenuItem* app_menu = SDL_GetMenuBarAppMenu(menu_bar_2);
157+
if (app_menu) {
158+
SDL_assert(!SDL_CreateMenuItem(menu_bar_2, "Exit", SDL_MENUITEM_BUTTON, MENU_BAR_EXIT));
159+
SDL_CreateMenuItem(app_menu, "Exit", SDL_MENUITEM_BUTTON, MENU_BAR_EXIT);
160+
} else {
161+
SDL_CreateMenuItem(menu_bar_2, "Exit", SDL_MENUITEM_BUTTON, MENU_BAR_EXIT);
162+
}
151163
}
152164

153165
SDL_SetWindowMenuBar(window_2, menu_bar_2);

include/SDL3/SDL_video.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3062,7 +3062,7 @@ extern SDL_DECLSPEC bool SDLCALL SDL_DisableScreenSaver(void);
30623062

30633063
extern SDL_DECLSPEC SDL_MenuItem *SDL_CreateMenuBar();
30643064

3065-
#define SDL_PROP_MENUITEM_CREATE_LABEL "SDL.menuitem.create.label"
3065+
extern SDL_DECLSPEC SDL_MenuItem *SDL_GetMenuBarAppMenu(SDL_MenuItem *menu_bar);
30663066

30673067
/**
30683068
* menu_as_item must be a SDL_MENUITEM_MENUBAR or SDL_MENUITEM_SUBMENU

src/video/SDL_video.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6321,15 +6321,15 @@ SDL_MenuItem *SDL_CreateMenuItem(SDL_MenuItem *menu_bar_as_item, const char *lab
63216321
}
63226322

63236323

6324-
SDL_MenuItem *SDL_GetMenuBarAppMenu(SDL_MenuBar *menu_bar)
6324+
SDL_MenuItem *SDL_GetMenuBarAppMenu(SDL_MenuItem *menu_bar)
63256325
{
63266326
CHECK_MENUITEM_MAGIC(menu_bar, NULL);
63276327

6328-
if (!menu_bar->app_menu) {
6328+
if (!menu_bar->menu_bar.app_menu) {
63296329
SDL_SetError("This platform doesn't support an Application menu.");
63306330
}
63316331

6332-
return menu_bar->app_menu;
6332+
return menu_bar->menu_bar.app_menu;
63336333
}
63346334

63356335
Sint64 SDL_GetMenuChildItems(SDL_MenuItem *menu_as_item)

src/video/cocoa/SDL_cocoavideo.m

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ bool Cocoa_CreateMenuBar(SDL_MenuBar *menu_bar)
360360
app_menu->common.type = SDL_MENUITEM_SUBMENU;
361361
app_menu->common.enabled = true;
362362
app_menu->common.parent = (SDL_MenuItem*)menu_bar;
363+
app_menu->common.menu_bar = menu_bar;
363364

364365
PlatformMenuData* app_menu_platform_data = [PlatformMenuData new];
365366
app_menu->common.platform_data = (void*)CFBridgingRetain(app_menu_platform_data);
@@ -372,6 +373,7 @@ bool Cocoa_CreateMenuBar(SDL_MenuBar *menu_bar)
372373
[platform_menu->menu addItem:app_menu_platform_data->menu_item];
373374

374375
menu_bar->common.item_common.platform_data = (void*)CFBridgingRetain(platform_menu);
376+
menu_bar->app_menu = app_menu;
375377

376378
return true;
377379
}
@@ -398,6 +400,11 @@ bool Cocoa_SetWindowMenuBar(SDL_Window *window, SDL_MenuBar *menu_bar)
398400

399401
bool Cocoa_CreateMenuItemAt(SDL_MenuItem *menu_item, size_t index, const char *name, Uint16 event_type)
400402
{
403+
if ((menu_item->common.parent->common.type == SDL_MENUITEM_MENUBAR) && (menu_item->common.type != SDL_MENUITEM_SUBMENU)) {
404+
SDL_SetError("No top level Checkables or Buttons on this platform");
405+
return false;
406+
}
407+
401408
PlatformMenuData* platform_data = [PlatformMenuData new];
402409
menu_item->common.platform_data = (void*)CFBridgingRetain(platform_data);
403410
platform_data->user_event_type = event_type;
@@ -420,14 +427,7 @@ bool Cocoa_CreateMenuItemAt(SDL_MenuItem *menu_item, size_t index, const char *n
420427
[platform_data->menu_item setAction:@selector(Cocoa_PlatformMenuData_MenuButtonClicked:)];
421428
[platform_data->menu_item setTarget:platform_data];
422429
[platform_data->menu_item setEnabled:true];
423-
424-
if ((menu_item->common.parent->common.type == SDL_MENUITEM_MENUBAR) && (menu_item->common.type != SDL_MENUITEM_SUBMENU)) {
425-
NSMenu* app_menu = [[parent_platform_data->menu itemAtIndex:(NSInteger)0] submenu];
426-
[app_menu addItem:platform_data->menu_item];
427-
428-
} else {
429-
[parent_platform_data->menu insertItem:platform_data->menu_item atIndex:(NSInteger)index];
430-
}
430+
[parent_platform_data->menu insertItem:platform_data->menu_item atIndex:(NSInteger)index];
431431
}
432432
return true;
433433
}

0 commit comments

Comments
 (0)