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