From e5519102a70d239a6f39e8d9d2cfc0b5c80404fe Mon Sep 17 00:00:00 2001 From: Bjorn Winckler Date: Wed, 23 Jan 2008 19:06:58 +0100 Subject: [PATCH] Ensure menus are updated when window focus changes --- src/MacVim/MMVimController.h | 1 + src/MacVim/MMVimController.m | 93 ++++++++++++++------------------- src/MacVim/MMWindowController.m | 1 + 3 files changed, 41 insertions(+), 54 deletions(-) diff --git a/src/MacVim/MMVimController.h b/src/MacVim/MMVimController.h index 14fa504dc5..65b60ff365 100644 --- a/src/MacVim/MMVimController.h +++ b/src/MacVim/MMVimController.h @@ -57,4 +57,5 @@ - (BOOL)sendMessageNow:(int)msgid data:(NSData *)data timeout:(NSTimeInterval)timeout; - (void)addVimInput:(NSString *)string; +- (void)updateMainMenu; @end diff --git a/src/MacVim/MMVimController.m b/src/MacVim/MMVimController.m index 442b5ddb06..a0d4559dcf 100644 --- a/src/MacVim/MMVimController.m +++ b/src/MacVim/MMVimController.m @@ -71,7 +71,6 @@ static NSTimeInterval MMResendInterval = 0.5; title:(NSString *)title tip:(NSString *)tip keyEquivalent:(int)key modifiers:(int)mask action:(NSString *)action atIndex:(int)idx; -- (void)updateMainMenu; - (NSToolbarItem *)toolbarItemForTag:(int)tag index:(int *)index; - (void)addToolbarItemToDictionaryWithTag:(int)tag label:(NSString *)title toolTip:(NSString *)tip icon:(NSString *)icon; @@ -111,14 +110,6 @@ static NSTimeInterval MMResendInterval = 0.5; selector:@selector(connectionDidDie:) name:NSConnectionDidDieNotification object:connection]; - - // TODO: What if [windowController window] is the full-screen window? - [[NSNotificationCenter defaultCenter] - addObserver:self - selector:@selector(windowDidBecomeMain:) - name:NSWindowDidBecomeMainNotification - object:[windowController window]]; - isInitialized = YES; } @@ -513,12 +504,6 @@ static NSTimeInterval MMResendInterval = 0.5; } } -- (void)windowDidBecomeMain:(NSNotification *)notification -{ - if (isInitialized) - [self updateMainMenu]; -} - - (NSToolbarItem *)toolbar:(NSToolbar *)theToolbar itemForItemIdentifier:(NSString *)itemId willBeInsertedIntoToolbar:(BOOL)flag @@ -541,6 +526,45 @@ static NSTimeInterval MMResendInterval = 0.5; return nil; } +- (void)updateMainMenu +{ + NSMenu *mainMenu = [NSApp mainMenu]; + + // Stop NSApp from updating the Window menu. + [NSApp setWindowsMenu:nil]; + + // Remove all menus from main menu (except the MacVim menu). + int i, count = [mainMenu numberOfItems]; + for (i = count-1; i > 0; --i) { + [mainMenu removeItemAtIndex:i]; + } + + // Add menus from 'mainMenuItems' to main menu. + count = [mainMenuItems count]; + for (i = 0; i < count; ++i) { + [mainMenu addItem:[mainMenuItems objectAtIndex:i]]; + } + + // Set the new Window menu. + // TODO! Need to look for 'Window' in all localized languages. + NSMenu *windowMenu = [[mainMenu itemWithTitle:@"Window"] submenu]; + if (windowMenu) { + // Remove all AppKit owned menu items (tag == 0); they will be added + // again when setWindowsMenu: is called. + count = [windowMenu numberOfItems]; + for (i = count-1; i >= 0; --i) { + NSMenuItem *item = [windowMenu itemAtIndex:i]; + if (![item tag]) { + [windowMenu removeItem:item]; + } + } + + [NSApp setWindowsMenu:windowMenu]; + } + + shouldUpdateMainMenu = NO; +} + @end // MMVimController @@ -1061,45 +1085,6 @@ static NSTimeInterval MMResendInterval = 0.5; } } -- (void)updateMainMenu -{ - NSMenu *mainMenu = [NSApp mainMenu]; - - // Stop NSApp from updating the Window menu. - [NSApp setWindowsMenu:nil]; - - // Remove all menus from main menu (except the MacVim menu). - int i, count = [mainMenu numberOfItems]; - for (i = count-1; i > 0; --i) { - [mainMenu removeItemAtIndex:i]; - } - - // Add menus from 'mainMenuItems' to main menu. - count = [mainMenuItems count]; - for (i = 0; i < count; ++i) { - [mainMenu addItem:[mainMenuItems objectAtIndex:i]]; - } - - // Set the new Window menu. - // TODO! Need to look for 'Window' in all localized languages. - NSMenu *windowMenu = [[mainMenu itemWithTitle:@"Window"] submenu]; - if (windowMenu) { - // Remove all AppKit owned menu items (tag == 0); they will be added - // again when setWindowsMenu: is called. - count = [windowMenu numberOfItems]; - for (i = count-1; i >= 0; --i) { - NSMenuItem *item = [windowMenu itemAtIndex:i]; - if (![item tag]) { - [windowMenu removeItem:item]; - } - } - - [NSApp setWindowsMenu:windowMenu]; - } - - shouldUpdateMainMenu = NO; -} - - (NSToolbarItem *)toolbarItemForTag:(int)tag index:(int *)index { if (!toolbar) return nil; diff --git a/src/MacVim/MMWindowController.m b/src/MacVim/MMWindowController.m index acf09f8b09..c3bc4a4079 100644 --- a/src/MacVim/MMWindowController.m +++ b/src/MacVim/MMWindowController.m @@ -609,6 +609,7 @@ - (void)windowDidBecomeMain:(NSNotification *)notification { + [vimController updateMainMenu]; [vimController sendMessage:GotFocusMsgID data:nil]; if ([vimView textView]) {