35 qCDebug(lcQpaMenus) <<
"Destructing" <<
this <<
"with" << m_nativeMenu;
36 for (
auto menu : std::as_const(m_menus)) {
39 NSMenuItem *item = nativeItemForMenu(menu);
40 if (menu->attachedItem() == item)
41 menu->setAttachedItem(nil);
44 [m_nativeMenu release];
45 static_menubars.removeOne(
this);
47 if (!m_window.isNull() && m_window->menubar() ==
this) {
48 m_window->setMenubar(
nullptr);
53 qDeleteAll(children());
85void QCocoaMenuBar::insertMenu(QPlatformMenu *platformMenu, QPlatformMenu *before)
90 qCDebug(lcQpaMenus) <<
"Inserting" << menu <<
"before" << before <<
"into" <<
this;
92 if (m_menus.contains(QPointer<QCocoaMenu>(menu))) {
93 qCWarning(lcQpaMenus,
"This menu already belongs to the menubar, remove it first");
97 if (beforeMenu && !m_menus.contains(QPointer<QCocoaMenu>(beforeMenu))) {
98 qCWarning(lcQpaMenus,
"The before menu does not belong to the menubar");
102 int insertionIndex = beforeMenu ? m_menus.indexOf(beforeMenu) : m_menus.size();
103 m_menus.insert(insertionIndex, menu);
106 QMacAutoReleasePool pool;
107 NSMenuItem *item = [[[NSMenuItem alloc] init] autorelease];
108 item.tag =
reinterpret_cast<NSInteger>(menu);
114 NSMenuItem *beforeItem = nativeItemForMenu(beforeMenu);
115 NSInteger nativeIndex = [m_nativeMenu indexOfItem:beforeItem];
116 [m_nativeMenu insertItem:item atIndex:nativeIndex];
118 [m_nativeMenu addItem:item];
122 syncMenu_helper(menu,
false );
124 if (needsImmediateUpdate())
131 if (!m_menus.contains(menu)) {
132 qCWarning(lcQpaMenus) <<
"Trying to remove" << menu <<
"that does not belong to" <<
this;
136 NSMenuItem *item = nativeItemForMenu(menu);
137 if (menu->attachedItem() == item)
138 menu->setAttachedItem(nil);
139 m_menus.removeOne(menu);
141 QMacAutoReleasePool pool;
144 NSInteger nativeIndex = [m_nativeMenu indexOfItem:item];
145 [m_nativeMenu removeItemAtIndex:nativeIndex];
155 QMacAutoReleasePool pool;
158 for (QCocoaMenuItem *item : cocoaMenu->items())
159 cocoaMenu->syncMenuItem_helper(item, menubarUpdate);
161 BOOL shouldHide = YES;
166 for (NSMenuItem *item in cocoaMenu->nsMenu().itemArray)
167 if (!item.separatorItem && !item.hidden) {
173 if (NSMenuItem *menuItem = cocoaMenu->attachedItem()) {
176 NSString *menuTitle = cocoaMenu->nsMenu().title;
202 if ([menuTitle isEqual:@
"Edit"] || [menuTitle isEqual:tr(
"Edit").toNSString()]) {
203 menuItem.title = qt_mac_AppKitString(@
"InputManager", @
"Edit");
207 menuItem.title = menuTitle;
210 menuItem.hidden = shouldHide;
266 QMacAutoReleasePool pool;
270 QWindow *win = cw ? cw->window() :
nullptr;
271 if (win && (win->flags() & Qt::Popup) == Qt::Popup) {
275 if ((win->flags() & Qt::WindowType_Mask) != Qt::Tool)
277 NSApplication *app = [NSApplication sharedApplication];
278 if (![app.delegate isKindOfClass:[QCocoaApplicationDelegate
class]])
281 QCocoaApplicationDelegate *appDelegate = app.delegate;
282 if (!appDelegate.inLaunch)
292 qCDebug(lcQpaMenus) <<
"Updating" << mb <<
"immediately for" << cw;
294 bool disableForModal = mb->shouldDisable(cw);
296 for (
auto menu : std::as_const(mb->m_menus)) {
299 NSMenuItem *item = mb->nativeItemForMenu(menu);
300 menu->setAttachedItem(item);
301 menu->setMenuParent(mb);
303 mb->syncMenu_helper(menu,
true );
304 menu->propagateEnabledState(!disableForModal);
307 QCocoaMenuLoader *loader = [QCocoaMenuLoader sharedMenuLoader];
308 [loader ensureAppMenuInMenu:mb->nsMenu()];
310 NSMutableSet *mergedItems = [[NSMutableSet setWithCapacity:mb->merged().count()] retain];
311 for (
auto mergedItem : mb->merged()) {
312 [mergedItems addObject:mergedItem->nsItem()];
313 mergedItem->syncMerged();
317 for (NSMenuItem *mergeable in [loader mergeable]) {
318 if (![mergedItems containsObject:mergeable]) {
319 mergeable.hidden = YES;
320 mergeable.enabled = NO;
324 [mergedItems release];
326 NSMenu *newMainMenu = mb->nsMenu();
327 if (NSApp.mainMenu == newMainMenu) {
335 qCDebug(lcQpaMenus) <<
"Clearing main menu temporarily";
336 NSApp.mainMenu = nil;
338 NSApp.mainMenu = newMainMenu;
341 [loader qtTranslateApplicationMenu];
349 NSApplication *app = NSApplication.sharedApplication;
353 NSMenu *mainMenu = app.mainMenu;
354 NSMenuItem *winMenuItem = [[[NSMenuItem alloc] initWithTitle:@
"QtWindowMenu"
355 action:nil keyEquivalent:@
""] autorelease];
357 winMenuItem.hidden = YES;
359 winMenuItem.submenu = [[[NSMenu alloc] initWithTitle:@
"QtWindowMenu"] autorelease];
365 auto *dummyItem = [[NSMenuItem alloc] initWithTitle:@
"" action:nil keyEquivalent:@
""];
366 dummyItem.hidden = YES;
367 [winMenuItem.submenu addItem:[dummyItem autorelease]];
369 [mainMenu insertItem:winMenuItem atIndex:mainMenu.itemArray.count];
370 app.windowsMenu = winMenuItem.submenu;
379 for (NSWindow *win in app.windows) {
380 win.excludedFromWindowsMenu = !win.excludedFromWindowsMenu;
381 win.excludedFromWindowsMenu = !win.excludedFromWindowsMenu;