Added an instance method to NSMenu that will remove all of the items in
[ITKit.git] / ITCategory-NSMenu.m
1 #import "ITCategory-NSMenu.h"
2
3 @interface NSMenu (HACKHACKHACKHACK)
4 - (id)_menuImpl;
5 @end
6
7 extern void _NSGetMenuItemForCommandKeyEvent (NSMenu *menu, void *chicken, void *taco, void *food); 
8 extern MenuRef _NSGetCarbonMenu( NSMenu *menu);
9
10 @implementation NSMenu (ITCategory)
11
12 - (void)indentItem:(id <NSMenuItem>)item {
13     [self indentItem:item toLevel:1];
14 }
15
16 - (void)indentItemAtIndex:(int)index {
17     [self indentItemAtIndex:index toLevel:1];
18 }
19
20 - (void)indentItem:(id <NSMenuItem>)item toLevel:(int)indentLevel {
21     [self indentItemAtIndex:[self indexOfItem:item] toLevel:indentLevel];
22 }
23
24 - (void)indentItemAtIndex:(int)index toLevel:(int)indentLevel {
25     if ([[self itemAtIndex:index] respondsToSelector:@selector(setIndentationLevel:)]) {
26         (void)[[self itemAtIndex:index] setIndentationLevel:indentLevel];
27     } else {
28         MenuRef carbonMenu = [self menuRef];
29         if (carbonMenu) {
30             SetMenuItemIndent(carbonMenu, index + 1, indentLevel);
31         }
32     }
33 }
34
35 - (MenuRef)menuRef {
36     MenuRef carbonMenu;
37     int w00t, m00f;
38     
39     if( [self respondsToSelector:@selector(_menuImpl)] ) {
40         (void)[self _menuImpl];
41     } else {
42         return nil;
43     }
44     
45     _NSGetMenuItemForCommandKeyEvent(self, NULL, &w00t, &m00f); 
46     carbonMenu = _NSGetCarbonMenu(self);
47     return carbonMenu;
48 }
49
50 - (void)removeAllItems {
51         int numOfItems = [self numberOfItems];
52         int i = numOfItems;
53         
54         while (i != 0) {
55                 [self removeItemAtIndex:(i-1)];
56                 i--;
57         }
58 }
59
60 @end