Skip to content

Commit a4bfb8e

Browse files
committed
Added way more error checking, switched to insertAt API(still appending rn tho)
1 parent 7df4751 commit a4bfb8e

File tree

9 files changed

+294
-157
lines changed

9 files changed

+294
-157
lines changed

examples/video/01-menubar/menubar.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,38 @@ static SDL_EventType_MenuExt EVENT_START = (SDL_EventType_MenuExt)0;
3535

3636
void CreateMenuBar()
3737
{
38-
SDL_MenuBar* bar = SDL_CreateMenuBar(window);
38+
SDL_MenuItem* bar = SDL_CreateMenuBar(window);
3939

4040
{
41-
SDL_MenuItem* menu = SDL_CreateMenuBarItem(bar, "File", SDL_MENU, MENU_BAR_LAST);
42-
SDL_CreateMenuItem((SDL_Menu*)menu, "New Window", SDL_MENU_BUTTON, MENU_BAR_FILE_NEW_WINDOW);
43-
SDL_MenuItem* checkable = SDL_CreateMenuItem((SDL_Menu*)menu, "Autosave Tabs on Close", SDL_MENU_CHECKABLE, MENU_BAR_FILE_AUTOSAVE_TABS_ON_CLOSE);
41+
SDL_MenuItem* menu = SDL_CreateMenuItem(bar, "File", SDL_MENU, MENU_BAR_LAST);
42+
SDL_CreateMenuItem(menu, "New Window", SDL_MENU_BUTTON, MENU_BAR_FILE_NEW_WINDOW);
43+
SDL_MenuItem* checkable = SDL_CreateMenuItem(menu, "Autosave Tabs on Close", SDL_MENU_CHECKABLE, MENU_BAR_FILE_AUTOSAVE_TABS_ON_CLOSE);
4444

45-
SDL_CheckMenuItem(checkable, true);
45+
SDL_CheckMenuItem(checkable);
4646
}
4747

4848
{
49-
SDL_MenuItem* menu = SDL_CreateMenuBarItem(bar, "Bookmarks", SDL_MENU, MENU_BAR_LAST);
50-
SDL_MenuItem* main_bookmarks = SDL_CreateMenuItem((SDL_Menu*)menu, "Bookmarks Toolbar", SDL_MENU, MENU_BAR_LAST);
51-
SDL_CreateMenuItem((SDL_Menu*)main_bookmarks, "SDL GitHub", SDL_MENU_BUTTON, MENU_BAR_BOOKMARKS_TOOLBAR_GITHUB);
52-
SDL_CreateMenuItem((SDL_Menu*)main_bookmarks, "SDL Wiki", SDL_MENU_BUTTON, MENU_BAR_BOOKMARKS_TOOLBAR_WIKI);
53-
SDL_CreateMenuItem((SDL_Menu*)main_bookmarks, "SDL Discord", SDL_MENU_BUTTON, MENU_BAR_BOOKMARKS_TOOLBAR_DISCORD);
49+
SDL_MenuItem* menu = SDL_CreateMenuItem(bar, "Bookmarks", SDL_MENU, MENU_BAR_LAST);
50+
SDL_MenuItem* main_bookmarks = SDL_CreateMenuItem(menu, "Bookmarks Toolbar", SDL_MENU, MENU_BAR_LAST);
51+
SDL_CreateMenuItem(main_bookmarks, "SDL GitHub", SDL_MENU_BUTTON, MENU_BAR_BOOKMARKS_TOOLBAR_GITHUB);
52+
SDL_CreateMenuItem(main_bookmarks, "SDL Wiki", SDL_MENU_BUTTON, MENU_BAR_BOOKMARKS_TOOLBAR_WIKI);
53+
SDL_CreateMenuItem(main_bookmarks, "SDL Discord", SDL_MENU_BUTTON, MENU_BAR_BOOKMARKS_TOOLBAR_DISCORD);
5454

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

58-
SDL_EnableMenuItem(other_bookmarks, false);
58+
SDL_DisableMenuItem(other_bookmarks);
5959
}
6060

6161
{
6262
// We can't create a top level checkable .
63-
SDL_MenuItem* checkable = SDL_CreateMenuBarItem(bar, "Incognito", SDL_MENU_CHECKABLE, MENU_BAR_INCOGNITO);
63+
SDL_MenuItem* checkable = SDL_CreateMenuItem(bar, "Incognito", SDL_MENU_CHECKABLE, MENU_BAR_INCOGNITO);
6464
SDL_assert(!checkable);
6565

66-
SDL_MenuItem* disabled = SDL_CreateMenuBarItem(bar, "Disabled Top-Level Button", SDL_MENU_BUTTON, MENU_BAR_TOP_LEVEL_BUTTON);
67-
SDL_EnableMenuItem(disabled, false);
66+
SDL_MenuItem* disabled = SDL_CreateMenuItem(bar, "Disabled Top-Level Button", SDL_MENU_BUTTON, MENU_BAR_TOP_LEVEL_BUTTON);
67+
SDL_DisableMenuItem(disabled);
6868

69-
SDL_CreateMenuBarItem(bar, "Exit", SDL_MENU_BUTTON, MENU_BAR_EXIT);
69+
SDL_CreateMenuItem(bar, "Exit", SDL_MENU_BUTTON, MENU_BAR_EXIT);
7070
}
7171

7272
EVENT_START = (SDL_EventType_MenuExt)SDL_RegisterEvents(MENU_BAR_LAST);

include/SDL3/SDL_video.h

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3012,16 +3012,35 @@ typedef enum SDL_MenuItemType
30123012
SDL_MENU_CHECKABLE,
30133013
} SDL_MenuItemType;
30143014

3015-
typedef struct SDL_MenuBar SDL_MenuBar;
3016-
typedef struct SDL_Menu SDL_Menu;
30173015
typedef union SDL_MenuItem SDL_MenuItem;
30183016

3019-
extern SDL_DECLSPEC SDL_MenuBar* SDL_CreateMenuBar(SDL_Window*window);
3020-
extern SDL_DECLSPEC SDL_MenuItem* SDL_CreateMenuBarItem(SDL_MenuBar*menu_bar, const char *name, SDL_MenuItemType type, Uint16 event_type);
3021-
extern SDL_DECLSPEC SDL_MenuItem* SDL_CreateMenuItem(SDL_Menu*menu_bar, const char *name, SDL_MenuItemType type, Uint16 event_type);
3022-
extern SDL_DECLSPEC bool SDL_CheckMenuItem(SDL_MenuItem*menu_item, bool checked);
3023-
extern SDL_DECLSPEC bool SDL_EnableMenuItem(SDL_MenuItem*menu_item, bool enabled);
3024-
extern SDL_DECLSPEC bool SDL_DestroyMenuBar(SDL_MenuBar*menu_bar);
3017+
extern SDL_DECLSPEC SDL_MenuItem *SDL_CreateMenuBar(SDL_Window *window);
3018+
3019+
// Must be a SDL_MENUBAR or SDL_MENU
3020+
// On MacOS, buttoms created under a menubar will go into the "App" submenu
3021+
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);
3022+
3023+
// Must be a SDL_MENUBAR or SDL_MENU
3024+
// On MacOS, buttoms created under a menubar will go into the "App" submenu
3025+
extern SDL_DECLSPEC SDL_MenuItem *SDL_CreateMenuItem(SDL_MenuItem *menu_bar_as_item, const char *name, SDL_MenuItemType type, Uint16 event_type);
3026+
3027+
// -1 on error
3028+
extern SDL_DECLSPEC Sint64 SDL_ChildItems(SDL_MenuItem *menu_bar_as_item);
3029+
3030+
3031+
// Must be a SDL_MENU_CHECKABLE
3032+
extern SDL_DECLSPEC bool SDL_CheckMenuItem(SDL_MenuItem *menu_item);
3033+
3034+
// Must be a SDL_MENU_CHECKABLE
3035+
extern SDL_DECLSPEC bool SDL_UncheckMenuItem(SDL_MenuItem *menu_item);
3036+
3037+
// Must be a SDL_MENU_CHECKABLE
3038+
extern SDL_DECLSPEC bool SDL_MenuItemChecked(SDL_MenuItem *menu_item, bool *checked);
3039+
3040+
extern SDL_DECLSPEC bool SDL_MenuItemEnabled(SDL_MenuItem *menu_item, bool *enabled);
3041+
extern SDL_DECLSPEC bool SDL_EnableMenuItem(SDL_MenuItem *menu_item);
3042+
extern SDL_DECLSPEC bool SDL_DisableMenuItem(SDL_MenuItem *menu_item);
3043+
extern SDL_DECLSPEC bool SDL_DestroyMenuItem(SDL_MenuItem *menu_item);
30253044

30263045
/**
30273046
* \name OpenGL support functions

src/dynapi/SDL_dynapi.sym

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,12 +1253,17 @@ SDL3_0.0.0 {
12531253
SDL_PutAudioStreamPlanarData;
12541254
SDL_GetEventDescription;
12551255
SDL_PutAudioStreamDataNoCopy;
1256-
SDL_CreateMenuBar;
1257-
SDL_CreateMenuBarItem;
1258-
SDL_CreateMenuItem;
1259-
SDL_CheckMenuItem;
1260-
SDL_EnableMenuItem;
1261-
SDL_DestroyMenuBar;
1256+
SDL_CreateMenuBar
1257+
SDL_CreateMenuItemAt
1258+
SDL_CreateMenuItem
1259+
SDL_ChildItems
1260+
SDL_CheckMenuItem
1261+
SDL_UncheckMenuItem
1262+
SDL_MenuItemChecked
1263+
SDL_MenuItemEnabled
1264+
SDL_EnableMenuItem
1265+
SDL_DisableMenuItem
1266+
SDL_DestroyMenuItem
12621267
# extra symbols go here (don't modify this line)
12631268
local: *;
12641269
};

src/dynapi/SDL_dynapi_overrides.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,8 +1279,13 @@
12791279
#define SDL_GetEventDescription SDL_GetEventDescription_REAL
12801280
#define SDL_PutAudioStreamDataNoCopy SDL_PutAudioStreamDataNoCopy_REAL
12811281
#define SDL_CreateMenuBar SDL_CreateMenuBar_REAL
1282-
#define SDL_CreateMenuBarItem SDL_CreateMenuBarItem_REAL
1282+
#define SDL_CreateMenuItemAt SDL_CreateMenuItemAt_REAL
12831283
#define SDL_CreateMenuItem SDL_CreateMenuItem_REAL
1284+
#define SDL_ChildItems SDL_ChildItems_REAL
12841285
#define SDL_CheckMenuItem SDL_CheckMenuItem_REAL
1286+
#define SDL_UncheckMenuItem SDL_UncheckMenuItem_REAL
1287+
#define SDL_MenuItemChecked SDL_MenuItemChecked_REAL
1288+
#define SDL_MenuItemEnabled SDL_MenuItemEnabled_REAL
12851289
#define SDL_EnableMenuItem SDL_EnableMenuItem_REAL
1286-
#define SDL_DestroyMenuBar SDL_DestroyMenuBar_REAL
1290+
#define SDL_DisableMenuItem SDL_DisableMenuItem_REAL
1291+
#define SDL_DestroyMenuItem SDL_DestroyMenuItem_REAL

src/dynapi/SDL_dynapi_procs.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,9 +1286,14 @@ SDL_DYNAPI_PROC(SDL_Renderer*,SDL_CreateGPURenderer,(SDL_Window *a,SDL_GPUShader
12861286
SDL_DYNAPI_PROC(bool,SDL_PutAudioStreamPlanarData,(SDL_AudioStream *a,const void * const*b,int c,int d),(a,b,c,d),return)
12871287
SDL_DYNAPI_PROC(int,SDL_GetEventDescription,(const SDL_Event *a,char *b,int c),(a,b,c),return)
12881288
SDL_DYNAPI_PROC(bool,SDL_PutAudioStreamDataNoCopy,(SDL_AudioStream *a,const void *b,int c,SDL_AudioStreamDataCompleteCallback d,void *e),(a,b,c,d,e),return)
1289-
SDL_DYNAPI_PROC(SDL_MenuBar*, SDL_CreateMenuBar,(SDL_Window*a),(a),return)
1290-
SDL_DYNAPI_PROC(SDL_MenuItem*,SDL_CreateMenuBarItem,(SDL_MenuBar*a,const char*b,SDL_MenuItemType c,Uint16 d),(a,b,c,d),return)
1291-
SDL_DYNAPI_PROC(SDL_MenuItem*,SDL_CreateMenuItem,(SDL_Menu*a,const char*b,SDL_MenuItemType c,Uint16 d),(a,b,c,d),return)
1292-
SDL_DYNAPI_PROC(bool, SDL_CheckMenuItem,(SDL_MenuItem*a, bool b),(a,b),return)
1293-
SDL_DYNAPI_PROC(bool, SDL_EnableMenuItem,(SDL_MenuItem*a, bool b),(a,b),return)
1294-
SDL_DYNAPI_PROC(bool, SDL_DestroyMenuBar,(SDL_MenuBar*a),(a),return)
1289+
SDL_DYNAPI_PROC(SDL_MenuItem*,SDL_CreateMenuBar,(SDL_Window *a),(a),return)
1290+
SDL_DYNAPI_PROC(SDL_MenuItem*,SDL_CreateMenuItemAt,(SDL_MenuItem *a,size_t b,const char *c,SDL_MenuItemType d, Uint16 e), (a,b,c,d,e), return)
1291+
SDL_DYNAPI_PROC(SDL_MenuItem*,SDL_CreateMenuItem,(SDL_MenuItem *a, const char *b, SDL_MenuItemType c, Uint16 d),(a,b,c,d), return)
1292+
SDL_DYNAPI_PROC(Sint64,SDL_ChildItems, (SDL_MenuItem *a),(a),return)
1293+
SDL_DYNAPI_PROC(bool,SDL_CheckMenuItem,(SDL_MenuItem *a),(a),return)
1294+
SDL_DYNAPI_PROC(bool,SDL_UncheckMenuItem,(SDL_MenuItem *a),(a),return)
1295+
SDL_DYNAPI_PROC(bool,SDL_MenuItemChecked,(SDL_MenuItem *a,bool *b),(a, b),return)
1296+
SDL_DYNAPI_PROC(bool,SDL_MenuItemEnabled,(SDL_MenuItem *a,bool *b),(a, b),return)
1297+
SDL_DYNAPI_PROC(bool,SDL_EnableMenuItem,(SDL_MenuItem *a),(a),return)
1298+
SDL_DYNAPI_PROC(bool,SDL_DisableMenuItem,(SDL_MenuItem *a),(a),return)
1299+
SDL_DYNAPI_PROC(bool,SDL_DestroyMenuItem,(SDL_MenuItem *a),(a),return)

src/video/SDL_sysvideo.h

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ typedef enum
211211
} SDL_FullscreenResult;
212212

213213

214+
typedef struct SDL_MenuBar SDL_MenuBar;
215+
typedef struct SDL_Menu SDL_Menu;
216+
214217
typedef struct SDL_MenuItem_CommonData
215218
{
216219
void *platform_data;
@@ -221,14 +224,21 @@ typedef struct SDL_MenuItem_CommonData
221224
SDL_MenuItemType type;
222225
} SDL_MenuItem_CommonData;
223226

227+
typedef struct SDL_Menu_CommonData
228+
{
229+
SDL_MenuItem_CommonData item_common;
230+
SDL_MenuItem *child_list;
231+
Sint64 children;
232+
} SDL_Menu_CommonData;
233+
224234
typedef struct SDL_MenuBar
225235
{
226-
SDL_MenuItem_CommonData common;
236+
SDL_Menu_CommonData common;
227237
} SDL_MenuBar;
228238

229239
typedef struct SDL_Menu
230240
{
231-
SDL_MenuItem_CommonData common;
241+
SDL_Menu_CommonData common;
232242
} SDL_Menu;
233243

234244
typedef struct SDL_MenuItem_Button
@@ -245,6 +255,7 @@ typedef struct SDL_MenuItem_Checkable
245255
typedef union SDL_MenuItem
246256
{
247257
SDL_MenuItem_CommonData common;
258+
SDL_Menu_CommonData menu_common;
248259
SDL_MenuBar menu_bar;
249260
SDL_Menu menu;
250261
SDL_MenuItem_Button button;
@@ -356,14 +367,15 @@ struct SDL_VideoDevice
356367
bool (*SetWindowFocusable)(SDL_VideoDevice *_this, SDL_Window *window, bool focusable);
357368
bool (*SyncWindow)(SDL_VideoDevice *_this, SDL_Window *window);
358369

359-
360370
bool (*CreateMenuBar)(SDL_MenuBar *menu_bar);
361-
bool (*CreateMenuBarItem)(SDL_MenuItem *menu_item, const char *name, Uint16 event_type);
362-
bool (*CreateMenuItem)(SDL_MenuItem *menu_item, const char *name, Uint16 event_type);
363-
bool (*CheckMenuItem)(SDL_MenuItem *menu_item, bool checked);
364-
bool (*EnableMenuItem)(SDL_MenuItem *menu_item, bool enabled);
371+
bool (*CreateMenuItemAt)(SDL_MenuItem *menu_item, size_t index, const char *name, Uint16 event_type);
372+
bool (*CheckMenuItem)(SDL_MenuItem *menu_item);
373+
bool (*UncheckMenuItem)(SDL_MenuItem *menu_item);
374+
bool (*MenuItemChecked)(SDL_MenuItem *menu_item, bool *checked);
375+
bool (*MenuItemEnabled)(SDL_MenuItem *menu_item, bool *enabled);
376+
bool (*EnableMenuItem)(SDL_MenuItem *menu_item);
377+
bool (*DisableMenuItem)(SDL_MenuItem *menu_item);
365378
bool (*DestroyMenuItem)(SDL_MenuItem *menu_item);
366-
bool (*DestroyMenuBar)(SDL_MenuBar *menu_bar);
367379

368380
/* * * */
369381
/*

0 commit comments

Comments
 (0)