From fdca44f913cb87e076b26bc3d0decefe333af7b4 Mon Sep 17 00:00:00 2001
From: Bjorn Winckler <bjorn.winckler@gmail.com>
Date: Sun, 22 Jun 2008 00:27:51 +0200
Subject: [PATCH] Rudimentary plugin menu support

---
 src/MacVim/MMAppController.h |    5 ++++
 src/MacVim/MMAppController.m |   51 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 0 deletions(-)

diff --git a/src/MacVim/MMAppController.h b/src/MacVim/MMAppController.h
index fcec67f..366e5a5 100644
--- a/src/MacVim/MMAppController.h
+++ b/src/MacVim/MMAppController.h
@@ -24,6 +24,7 @@
     NSMenu              *defaultMainMenu;
     NSMenuItem          *appMenuItemTemplate;
     NSMenuItem          *recentFilesMenuItem;
+    NSMenuItem          *pluginMenuItem;
 }
 
 + (MMAppController *)sharedInstance;
@@ -32,6 +33,10 @@
 - (void)removeVimController:(id)controller;
 - (void)windowControllerWillOpen:(MMWindowController *)windowController;
 - (void)setMainMenu:(NSMenu *)mainMenu;
+
+- (void)addItemToPluginMenu:(NSMenuItem *)item;
+- (void)removeItemFromPluginMenu:(NSMenuItem *)item;
+
 - (IBAction)newWindow:(id)sender;
 - (IBAction)fileOpen:(id)sender;
 - (IBAction)selectNextWindow:(id)sender;
diff --git a/src/MacVim/MMAppController.m b/src/MacVim/MMAppController.m
index 445532f..ea8cd4f 100644
--- a/src/MacVim/MMAppController.m
+++ b/src/MacVim/MMAppController.m
@@ -100,6 +100,8 @@ static int executeInLoginShell(NSString *path, NSArray *args);
 - (NSMutableDictionary *)extractArgumentsFromOdocEvent:
     (NSAppleEventDescriptor *)desc;
 - (void)passArguments:(NSDictionary *)args toVimController:(MMVimController*)vc;
+- (void)removePluginMenu;
+- (void)addPluginMenuToMenu:(NSMenu *)mainMenu;
 @end
 
 
@@ -149,6 +151,13 @@ static int executeInLoginShell(NSString *path, NSArray *args);
         vimControllers = [NSMutableArray new];
         pidArguments = [NSMutableDictionary new];
 
+        pluginMenuItem = [[NSMenuItem alloc] initWithTitle:@"Plugin"
+                                                    action:NULL
+                                             keyEquivalent:@""];
+        NSMenu *submenu = [[NSMenu alloc] initWithTitle:@"Plugin"];
+        [pluginMenuItem setSubmenu:submenu];
+        [submenu release];
+
         // NOTE: Do not use the default connection since the Logitech Control
         // Center (LCC) input manager steals and this would cause MacVim to
         // never open any windows.  (This is a bug in LCC but since they are
@@ -189,6 +198,7 @@ static int executeInLoginShell(NSString *path, NSArray *args);
     [openSelectionString release];  openSelectionString = nil;
     [recentFilesMenuItem release];  recentFilesMenuItem = nil;
     [defaultMainMenu release];  defaultMainMenu = nil;
+    [pluginMenuItem release];  pluginMenuItem = nil;
     [appMenuItemTemplate release];  appMenuItemTemplate = nil;
 
     [super dealloc];
@@ -666,6 +676,26 @@ static int executeInLoginShell(NSString *path, NSArray *args);
         }
     }
     [NSApp setWindowsMenu:windowsMenu];
+
+    // Move plugin menu from old to new main menu.
+    [self removePluginMenu];
+    [self addPluginMenuToMenu:mainMenu];
+}
+
+- (void)addItemToPluginMenu:(NSMenuItem *)item
+{
+    NSMenu *menu = [pluginMenuItem submenu];
+    [menu addItem:item];
+    if ([menu numberOfItems] == 1)
+        [self addPluginMenuToMenu:[NSApp mainMenu]];
+}
+
+- (void)removeItemFromPluginMenu:(NSMenuItem *)item
+{
+    NSMenu *menu = [pluginMenuItem submenu];
+    [menu removeItem:item];
+    if ([menu numberOfItems] == 0)
+        [self removePluginMenu];
 }
 
 - (IBAction)newWindow:(id)sender
@@ -1243,6 +1273,27 @@ static int executeInLoginShell(NSString *path, NSArray *args);
         [vc addVimInput:buildSearchTextCommand(searchText)];
 }
 
+- (void)removePluginMenu
+{
+    if ([pluginMenuItem menu])
+        [[pluginMenuItem menu] removeItem:pluginMenuItem];
+}
+
+- (void)addPluginMenuToMenu:(NSMenu *)mainMenu
+{
+    NSMenu *windowsMenu = [mainMenu findWindowsMenu];
+
+    if ([[pluginMenuItem submenu] numberOfItems] > 0) {
+        int idx = windowsMenu ? [mainMenu indexOfItemWithSubmenu:windowsMenu]
+                              : -1;
+        if (idx > 0) {
+            [mainMenu insertItem:pluginMenuItem atIndex:idx];
+        } else {
+            [mainMenu addItem:pluginMenuItem];
+        }
+    }
+}
+
 @end // MMAppController (Private)
 
 
-- 
1.5.5.1

