| 1 |
// |
| 2 |
// NSMenuAdditions.m |
| 3 |
// |
| 4 |
// Created by Toshi Nagata on 2017/10/12. |
| 5 |
// |
| 6 |
/* |
| 7 |
Copyright (c) 2004-2017 Toshi Nagata. All rights reserved. |
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation version 2 of the License. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
*/ |
| 18 |
|
| 19 |
#import "NSMenuAdditions.h" |
| 20 |
|
| 21 |
|
| 22 |
@implementation NSMenu (MyMenuAddition) |
| 23 |
|
| 24 |
- (void)changeMenuTitleAttributes:(NSDictionary *)attributes |
| 25 |
{ |
| 26 |
int i; |
| 27 |
NSArray *ary = [self itemArray]; |
| 28 |
for (i = (int)[ary count] - 1; i >= 0; i--) { |
| 29 |
NSMenuItem *item = [ary objectAtIndex:i]; |
| 30 |
NSMenu *submenu; |
| 31 |
NSAttributedString *astr = [[[NSAttributedString alloc] initWithString:[item title] attributes:attributes] autorelease]; |
| 32 |
[item setAttributedTitle:astr]; |
| 33 |
submenu = [item submenu]; |
| 34 |
if (submenu != nil) |
| 35 |
[submenu changeMenuTitleAttributes:attributes]; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
static NSMenuItem * |
| 40 |
searchMenuItemWithTagSub(NSMenu *menu, int tag) |
| 41 |
{ |
| 42 |
int i; |
| 43 |
NSMenuItem *item; |
| 44 |
for (i = (int)[menu numberOfItems] - 1; i >= 0; i--) { |
| 45 |
item = (NSMenuItem *)[menu itemAtIndex: i]; |
| 46 |
if ([item tag] == tag) |
| 47 |
return item; |
| 48 |
if ([item hasSubmenu]) { |
| 49 |
item = searchMenuItemWithTagSub([item submenu], tag); |
| 50 |
if (item != nil) |
| 51 |
return item; |
| 52 |
} |
| 53 |
} |
| 54 |
return nil; |
| 55 |
} |
| 56 |
|
| 57 |
- (NSMenuItem *)searchMenuItemWithTag:(int)tag |
| 58 |
{ |
| 59 |
return searchMenuItemWithTagSub(self, tag); |
| 60 |
} |
| 61 |
|
| 62 |
- (NSMenu *)findSubmenuContainingItem:(NSMenuItem *)anItem outIndex:(int *)outIndex |
| 63 |
{ |
| 64 |
int i; |
| 65 |
NSMenuItem *item; |
| 66 |
for (i = (int)[self numberOfItems] - 1; i >= 0; i--) { |
| 67 |
item = (NSMenuItem *)[self itemAtIndex: i]; |
| 68 |
if (item == anItem) { |
| 69 |
if (outIndex != NULL) |
| 70 |
*outIndex = i; |
| 71 |
return self; |
| 72 |
} |
| 73 |
if ([item hasSubmenu]) { |
| 74 |
NSMenu *menu = [[item submenu] findSubmenuContainingItem:anItem outIndex:outIndex]; |
| 75 |
if (menu != nil) |
| 76 |
return menu; |
| 77 |
} |
| 78 |
} |
| 79 |
return nil; |
| 80 |
} |
| 81 |
|
| 82 |
@end |