From 79c3db48cf593ccbe64f6c845af0777caebde2a8 Mon Sep 17 00:00:00 2001 From: ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com> Date: Tue, 14 May 2024 17:46:25 +0200 Subject: New member function Fl_Menu_Bar::play_menu(const char *title) --- src/Fl_MacOS_Sys_Menu_Bar.mm | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/Fl_MacOS_Sys_Menu_Bar.mm') diff --git a/src/Fl_MacOS_Sys_Menu_Bar.mm b/src/Fl_MacOS_Sys_Menu_Bar.mm index d51734729..aa8560966 100644 --- a/src/Fl_MacOS_Sys_Menu_Bar.mm +++ b/src/Fl_MacOS_Sys_Menu_Bar.mm @@ -726,4 +726,36 @@ void fl_mac_set_about(Fl_Callback *cb, void *user_data, int shortcut) { Fl_Sys_Menu_Bar::about(cb, user_data); } + +void Fl_MacOS_Sys_Menu_Bar_Driver::play_menu(const char *menu_name) { + // Use the accessibility interface to programmatically open a menu of the system menubar + char *ts = remove_ampersand(menu_name); + NSString *mac_name = [NSString stringWithUTF8String:ts]; + free(ts); + AXUIElementRef appElement = AXUIElementCreateApplication(getpid()); + AXUIElementRef menuBar; + AXError error = AXUIElementCopyAttributeValue(appElement, kAXMenuBarAttribute, (CFTypeRef *)&menuBar); + if (error) return; + CFIndex count = -1; + error = AXUIElementGetAttributeValueCount(menuBar, kAXChildrenAttribute, &count); + if (error) { CFRelease(menuBar); return; } + NSArray *children = nil; + error = AXUIElementCopyAttributeValues(menuBar, kAXChildrenAttribute, 0, count, (CFArrayRef *)&children); + if (error) { CFRelease(menuBar); return; } + for (id child in children) { + AXUIElementRef element = (AXUIElementRef)child; + id title; + AXError error = AXUIElementCopyAttributeValue(element, kAXTitleAttribute, (CFTypeRef *)&title); + if (!error && [title isEqualToString:mac_name]) { + AXUIElementPerformAction(element, kAXPressAction); + CFRelease(title); + break; + } + CFRelease(title); + } + CFRelease(menuBar); + [children release]; + CFRelease(appElement); +} + #endif /* __APPLE__ */ -- cgit v1.2.3