Skip to content

Commit 9b2a5f9

Browse files
committed
Rewrote SetWindowMenuBar, need to test it.
1 parent 3d39b39 commit 9b2a5f9

File tree

6 files changed

+42
-28
lines changed

6 files changed

+42
-28
lines changed

src/video/SDL_sysvideo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ struct SDL_VideoDevice
372372
bool (*ReconfigureWindow)(SDL_VideoDevice *_this, SDL_Window *window, SDL_WindowFlags flags);
373373

374374
bool (*CreateMenuBar)(SDL_MenuBar *menu_bar);
375-
bool (*SetWindowMenuBar)(SDL_MenuBar *menu_bar);
375+
bool (*SetWindowMenuBar)(SDL_Window *window, SDL_MenuBar *menu_bar);
376376
bool (*CreateMenuItemAt)(SDL_MenuItem *menu_item, size_t index, const char *name, Uint16 event_type);
377377
bool (*SetMenuItemLabel)(SDL_MenuItem *menu_item, const char *label);
378378
bool (*GetMenuItemChecked)(SDL_MenuItem *menu_item, bool *checked);

src/video/SDL_video.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6164,39 +6164,37 @@ bool SDL_SetWindowMenuBar(SDL_Window* window, SDL_MenuItem* menu_bar)
61646164
if (!_this) {
61656165
return SDL_UninitializedVideo();
61666166
}
6167-
6168-
// Same Window/MenuBar combination, no need to do anything.
6169-
if (menu_bar->menu_bar.window == window) {
6170-
return true;
6171-
}
61726167

61736168
// Passed NULL to the Window, user wants to retake ownership of the menubar.
61746169
if (!menu_bar && window->menu_bar) {
6175-
bool success = _this->SetWindowMenuBar(NULL);
6170+
bool success = _this->SetWindowMenuBar(window, NULL);
61766171

61776172
SDL_SetObjectValid(window->menu_bar, SDL_OBJECT_TYPE_MENUBAR, true);
61786173
window->menu_bar = NULL;
61796174

61806175
return success;
61816176
}
61826177

6178+
// Same Window/MenuBar combination, no need to do anything.
6179+
if (menu_bar->menu_bar.window == window) {
6180+
return true;
6181+
}
6182+
61836183
if (menu_bar->common.type != SDL_MENUITEM_MENUBAR) {
61846184
SDL_SetError("Can't set menu Item that isn't a Menu onto a Window.");
61856185
return false;
61866186
}
6187-
6187+
6188+
// menu_bar is already on another window, null out the menubar on that window
6189+
// before we add this menubar to the given window.
61886190
if (menu_bar->menu_bar.window) {
6189-
bool success = _this->SetWindowMenuBar(NULL);
6190-
SDL_SetObjectValid(menu_bar->menu_bar.window->menu_bar, SDL_OBJECT_TYPE_MENUBAR, true);
6191-
window->menu_bar = NULL;
6192-
menu_bar->menu_bar.window->menu_bar
6193-
6194-
menu_bar->menu_bar.window = NULL;
6191+
_this->SetWindowMenuBar(menu_bar->menu_bar.window, NULL);
61956192
}
61966193

61976194
SDL_MenuBar *menu_bar_real = (SDL_MenuBar*)menu_bar;
61986195
menu_bar_real->window = window;
61996196

6197+
// Window has an existing MenuBar, release it back to the user.
62006198
if (window->menu_bar) {
62016199
SDL_SetObjectValid(window->menu_bar, SDL_OBJECT_TYPE_MENUBAR, true);
62026200
}
@@ -6205,7 +6203,7 @@ bool SDL_SetWindowMenuBar(SDL_Window* window, SDL_MenuItem* menu_bar)
62056203

62066204
SDL_SetObjectValid(menu_bar_real, SDL_OBJECT_TYPE_MENUBAR, false);
62076205

6208-
return _this->SetWindowMenuBar(menu_bar_real);
6206+
return _this->SetWindowMenuBar(window, menu_bar_real);
62096207
}
62106208

62116209
void SDL_CleanupMenubars()

src/video/cocoa/SDL_cocoavideo.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,13 @@ bool Cocoa_CreateMenuBar(SDL_MenuBar *menu_bar)
370370
return true;
371371
}
372372

373-
bool Cocoa_SetWindowMenuBar(SDL_MenuBar *menu_bar)
373+
bool Cocoa_SetWindowMenuBar(SDL_Window *window, SDL_MenuBar *menu_bar)
374374
{
375+
if (menu_bar == NULL) {
376+
[NSApp setMainMenu:nil];
377+
return true;
378+
}
379+
375380
// We don't actually set the menubar until the window is in focus
376381
if (!menu_bar->window->keyboard_focus) {
377382
return true;

src/video/cocoa/SDL_cocoawindow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ extern bool Cocoa_SyncWindow(SDL_VideoDevice *_this, SDL_Window *window);
197197
extern void Cocoa_MenuVisibilityCallback(void *userdata, const char *name, const char *oldValue, const char *newValue);
198198

199199
extern bool Cocoa_CreateMenuBar(SDL_MenuBar *menu_bar);
200-
extern bool Cocoa_SetWindowMenuBar(SDL_MenuBar *menu_bar);
200+
extern bool Cocoa_SetWindowMenuBar(SDL_Window *window, SDL_MenuBar *menu_bar);
201201
extern bool Cocoa_CreateMenuItemAt(SDL_MenuItem *menu_item, size_t index, const char *name, Uint16 event_type);
202202
extern bool Cocoa_SetMenuItemLabel(SDL_MenuItem *menu_item, const char *label);
203203
extern bool Cocoa_GetMenuItemChecked(SDL_MenuItem *menu_item, bool *checked);

src/video/windows/SDL_windowsvideo.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,8 +920,19 @@ bool Win32_CreateMenuBar(SDL_MenuBar *menu_bar)
920920
return true;
921921
}
922922

923-
bool Win32_SetWindowMenuBar(SDL_MenuBar *menu_bar)
923+
bool Win32_SetWindowMenuBar(SDL_Window *window, SDL_MenuBar *menu_bar)
924924
{
925+
if (!menu_bar) {
926+
const SDL_WindowData *data = window->internal;
927+
928+
if (!SetMenu(data->hwnd, (HMENU)NULL)) {
929+
WIN_SetError("Unable to unset MenuBar");
930+
return false;
931+
}
932+
933+
return true;
934+
}
935+
925936
const PlatformMenuData *menu_platform_data = (PlatformMenuData *)menu_bar->common.item_common.platform_data;
926937
const SDL_WindowData *data = menu_bar->common.item_common.menu_bar->window->internal;
927938

src/video/windows/SDL_windowsvideo.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -672,15 +672,15 @@ typedef struct PlatformMenuData
672672
Uint16 user_event_type;
673673
} PlatformMenuData;
674674

675-
extern bool Cocoa_CreateMenuBar(SDL_MenuBar *menu_bar);
676-
extern bool Cocoa_SetWindowMenuBar(SDL_MenuBar *menu_bar);
677-
extern bool Cocoa_CreateMenuItemAt(SDL_MenuItem *menu_item, size_t index, const char *name, Uint16 event_type);
678-
679-
extern bool Cocoa_SetMenuItemLabel(SDL_MenuItem *menu_item, const char *label);
680-
extern bool Cocoa_GetMenuItemChecked(SDL_MenuItem *menu_item, bool *checked);
681-
extern bool Cocoa_SetMenuItemChecked(SDL_MenuItem *menu_item, bool checked);
682-
extern bool Cocoa_GetMenuItemEnabled(SDL_MenuItem *menu_item, bool *enabled);
683-
extern bool Cocoa_SetMenuItemEnabled(SDL_MenuItem *menu_item, bool enabled);
684-
extern bool Cocoa_DestroyMenuItem(SDL_MenuItem *menu_item);
675+
extern bool Win32_CreateMenuBar(SDL_MenuBar *menu_bar);
676+
extern bool Win32_SetWindowMenuBar(SDL_Window *window, SDL_MenuBar *menu_bar);
677+
extern bool Win32_CreateMenuItemAt(SDL_MenuItem *menu_item, size_t index, const char *name, Uint16 event_type);
678+
679+
extern bool Win32_SetMenuItemLabel(SDL_MenuItem *menu_item, const char *label);
680+
extern bool Win32_GetMenuItemChecked(SDL_MenuItem *menu_item, bool *checked);
681+
extern bool Win32_SetMenuItemChecked(SDL_MenuItem *menu_item, bool checked);
682+
extern bool Win32_GetMenuItemEnabled(SDL_MenuItem *menu_item, bool *enabled);
683+
extern bool Win32_SetMenuItemEnabled(SDL_MenuItem *menu_item, bool enabled);
684+
extern bool Win32_DestroyMenuItem(SDL_MenuItem *menu_item);
685685

686686
#endif // SDL_windowsvideo_h_

0 commit comments

Comments
 (0)