Change name of window menu items
[MacVim.git] / src / MacVim / Miscellaneous.m
blob44f662298a75c5d94075d44f4aa77b9e7c7f7597
1 /* vi:set ts=8 sts=4 sw=4 ft=objc:
2  *
3  * VIM - Vi IMproved            by Bram Moolenaar
4  *                              MacVim GUI port by Bjorn Winckler
5  *
6  * Do ":help uganda"  in Vim to read copying and usage conditions.
7  * Do ":help credits" in Vim to see a list of people who contributed.
8  * See README.txt for an overview of the Vim source code.
9  */
11 #import "MacVim.h"
12 #import "Miscellaneous.h"
16 // NSUserDefaults keys
17 NSString *MMTabMinWidthKey              = @"MMTabMinWidth";
18 NSString *MMTabMaxWidthKey              = @"MMTabMaxWidth";
19 NSString *MMTabOptimumWidthKey          = @"MMTabOptimumWidth";
20 NSString *MMShowAddTabButtonKey         = @"MMShowAddTabButton";
21 NSString *MMTextInsetLeftKey            = @"MMTextInsetLeft";
22 NSString *MMTextInsetRightKey           = @"MMTextInsetRight";
23 NSString *MMTextInsetTopKey             = @"MMTextInsetTop";
24 NSString *MMTextInsetBottomKey          = @"MMTextInsetBottom";
25 NSString *MMTypesetterKey               = @"MMTypesetter";
26 NSString *MMCellWidthMultiplierKey      = @"MMCellWidthMultiplier";
27 NSString *MMBaselineOffsetKey           = @"MMBaselineOffset";
28 NSString *MMTranslateCtrlClickKey       = @"MMTranslateCtrlClick";
29 NSString *MMTopLeftPointKey             = @"MMTopLeftPoint";
30 NSString *MMOpenInCurrentWindowKey      = @"MMOpenInCurrentWindow";
31 NSString *MMNoFontSubstitutionKey       = @"MMNoFontSubstitution";
32 NSString *MMLoginShellKey               = @"MMLoginShell";
33 NSString *MMAtsuiRendererKey            = @"MMAtsuiRenderer";
34 NSString *MMUntitledWindowKey           = @"MMUntitledWindow";
35 NSString *MMTexturedWindowKey           = @"MMTexturedWindow";
36 NSString *MMZoomBothKey                 = @"MMZoomBoth";
37 NSString *MMCurrentPreferencePaneKey    = @"MMCurrentPreferencePane";
38 NSString *MMLoginShellCommandKey        = @"MMLoginShellCommand";
39 NSString *MMLoginShellArgumentKey       = @"MMLoginShellArgument";
40 NSString *MMDialogsTrackPwdKey          = @"MMDialogsTrackPwd";
41 #ifdef MM_ENABLE_PLUGINS
42 NSString *MMShowLeftPlugInContainerKey  = @"MMShowLeftPlugInContainer";
43 #endif
44 NSString *MMOpenLayoutKey               = @"MMOpenLayout";
45 NSString *MMVerticalSplitKey            = @"MMVerticalSplit";
46 NSString *MMPreloadCacheSizeKey         = @"MMPreloadCacheSize";
47 NSString *MMLastWindowClosedBehaviorKey = @"MMLastWindowClosedBehavior";
48 NSString *MMLoadDefaultFontKey          = @"MMLoadDefaultFont";
52 @implementation NSIndexSet (MMExtras)
54 + (id)indexSetWithVimList:(NSString *)list
56     NSMutableIndexSet *idxSet = [NSMutableIndexSet indexSet];
57     NSArray *array = [list componentsSeparatedByString:@"\n"];
58     unsigned i, count = [array count];
60     for (i = 0; i < count; ++i) {
61         NSString *entry = [array objectAtIndex:i];
62         if ([entry intValue] > 0)
63             [idxSet addIndex:i];
64     }
66     return idxSet;
69 @end // NSIndexSet (MMExtras)
74 @implementation NSDocumentController (MMExtras)
76 - (void)noteNewRecentFilePath:(NSString *)path
78     NSURL *url = [NSURL fileURLWithPath:path];
79     if (url)
80         [self noteNewRecentDocumentURL:url];
83 - (void)noteNewRecentFilePaths:(NSArray *)paths
85     NSEnumerator *e = [paths objectEnumerator];
86     NSString *path;
87     while ((path = [e nextObject]))
88         [self noteNewRecentFilePath:path];
91 @end // NSDocumentController (MMExtras)
96 @implementation NSSavePanel (MMExtras)
98 - (void)hiddenFilesButtonToggled:(id)sender
100     [self setShowsHiddenFiles:[sender intValue]];
103 - (void)setShowsHiddenFiles:(BOOL)show
105     // This is undocumented stuff, so be careful. This does the same as
106     //     [[self _navView] setShowsHiddenFiles:show];
107     // but does not produce warnings.
109     if (![self respondsToSelector:@selector(_navView)])
110         return;
112     id navView = [self performSelector:@selector(_navView)];
113     if (![navView respondsToSelector:@selector(setShowsHiddenFiles:)])
114         return;
116     // performSelector:withObject: does not support a BOOL
117     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
118         [navView methodSignatureForSelector:@selector(setShowsHiddenFiles:)]];
119     [invocation setTarget:navView];
120     [invocation setSelector:@selector(setShowsHiddenFiles:)];
121     [invocation setArgument:&show atIndex:2];
122     [invocation invoke];
125 @end // NSSavePanel (MMExtras)
130 @implementation NSMenu (MMExtras)
132 - (int)indexOfItemWithAction:(SEL)action
134     int i, count = [self numberOfItems];
135     for (i = 0; i < count; ++i) {
136         NSMenuItem *item = [self itemAtIndex:i];
137         if ([item action] == action)
138             return i;
139     }
141     return -1;
144 - (NSMenuItem *)itemWithAction:(SEL)action
146     int idx = [self indexOfItemWithAction:action];
147     return idx >= 0 ? [self itemAtIndex:idx] : nil;
150 - (NSMenu *)findMenuContainingItemWithAction:(SEL)action
152     // NOTE: We only look for the action in the submenus of 'self'
153     int i, count = [self numberOfItems];
154     for (i = 0; i < count; ++i) {
155         NSMenu *menu = [[self itemAtIndex:i] submenu];
156         NSMenuItem *item = [menu itemWithAction:action];
157         if (item) return menu;
158     }
160     return nil;
163 - (NSMenu *)findWindowsMenu
165     return [self findMenuContainingItemWithAction:
166         @selector(performMiniaturize:)];
169 - (NSMenu *)findApplicationMenu
171     // TODO: Just return [self itemAtIndex:0]?
172     return [self findMenuContainingItemWithAction:@selector(terminate:)];
175 - (NSMenu *)findServicesMenu
177     // NOTE!  Our heuristic for finding the "Services" menu is to look for the
178     // second item before the "Hide MacVim" menu item on the "MacVim" menu.
179     // (The item before "Hide MacVim" should be a separator, but this is not
180     // important as long as the item before that is the "Services" menu.)
182     NSMenu *appMenu = [self findApplicationMenu];
183     if (!appMenu) return nil;
185     int idx = [appMenu indexOfItemWithAction: @selector(hide:)];
186     if (idx-2 < 0) return nil;  // idx == -1, if selector not found
188     return [[appMenu itemAtIndex:idx-2] submenu];
191 - (NSMenu *)findFileMenu
193     return [self findMenuContainingItemWithAction:@selector(performClose:)];
196 @end // NSMenu (MMExtras)
201 @implementation NSToolbar (MMExtras)
203 - (int)indexOfItemWithItemIdentifier:(NSString *)identifier
205     NSArray *items = [self items];
206     int i, count = [items count];
207     for (i = 0; i < count; ++i) {
208         id item = [items objectAtIndex:i];
209         if ([[item itemIdentifier] isEqual:identifier])
210             return i;
211     }
213     return NSNotFound;
216 - (NSToolbarItem *)itemAtIndex:(int)idx
218     NSArray *items = [self items];
219     if (idx < 0 || idx >= [items count])
220         return nil;
222     return [items objectAtIndex:idx];
225 - (NSToolbarItem *)itemWithItemIdentifier:(NSString *)identifier
227     int idx = [self indexOfItemWithItemIdentifier:identifier];
228     return idx != NSNotFound ? [self itemAtIndex:idx] : nil;
231 @end // NSToolbar (MMExtras)
236 @implementation NSTabView (MMExtras)
238 - (void)removeAllTabViewItems
240     NSArray *existingItems = [self tabViewItems];
241     NSEnumerator *e = [existingItems objectEnumerator];
242     NSTabViewItem *item;
243     while (item = [e nextObject]){
244         [self removeTabViewItem:item];
245     }
248 @end // NSTabView (MMExtras)
253 @implementation NSNumber (MMExtras)
255 - (int)tag
257     return [self intValue];
260 @end // NSNumber (MMExtras)
265     NSView *
266 showHiddenFilesView()
268     // Return a new button object for each NSOpenPanel -- several of them
269     // could be displayed at once.
270     // If the accessory view should get more complex, it should probably be
271     // loaded from a nib file.
272     NSButton *button = [[[NSButton alloc]
273         initWithFrame:NSMakeRect(0, 0, 140, 18)] autorelease];
274     [button setTitle:
275         NSLocalizedString(@"Show Hidden Files", @"Show Hidden Files Checkbox")];
276     [button setButtonType:NSSwitchButton];
278     [button setTarget:nil];
279     [button setAction:@selector(hiddenFilesButtonToggled:)];
281     // Use the regular control size (checkbox is a bit smaller without this)
282     NSControlSize buttonSize = NSRegularControlSize;
283     float fontSize = [NSFont systemFontSizeForControlSize:buttonSize];
284     NSCell *theCell = [button cell];
285     NSFont *theFont = [NSFont fontWithName:[[theCell font] fontName]
286                                       size:fontSize];
287     [theCell setFont:theFont];
288     [theCell setControlSize:buttonSize];
289     [button sizeToFit];
291     return button;