William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | //
// AIMenuItemView.m
// Adium
//
// Created by Evan Schoenberg on 12/20/05.
//
#import "AIMenuItemView.h"
#define CHECK_UNICODE 0x2713 /* Unicode CHECK MARK*/
#define MENU_ITEM_HEIGHT 17
#define MENU_ITEM_SPACING 2
@interface AIMenuItemView ()
@end
@implementation AIMenuItemView
- (void)_initMenuItemView
{
currentHoveredIndex = -1;
}
- (void)awakeFromNib
{
if ([[self superclass] instancesRespondToSelector:@selector(awakeFromNib)]) {
[super awakeFromNib];
}
if (delegate && [delegate respondsToSelector:@selector(menuForMenuItemView:)]) {
[self setMenu:[delegate menuForMenuItemView:self]];
}
[self _initMenuItemView];
}
- (id)initWithFrame:(NSRect)inFrame
{
if ((self = [super initWithFrame:inFrame])) {
[self _initMenuItemView];
}
return self;
}
- (void)dealloc
{
[menu release];
[trackingTags release];
[menuItemAttributes release];
[disabledMenuItemAttributes release];
[hoveredMenuItemAttributes release];
[super dealloc];
}
#pragma mark Menu
/*!
* @brief Set the menu we display
*/
- (void)setMenu:(NSMenu *)inMenu
{
// NSLog(@"Set menu: %@",inMenu);
if (menu != inMenu) {
[menu release];
menu = [inMenu retain];
}
[self setNeedsDisplay:YES];
if ([delegate respondsToSelector:@selector(menuItemViewDidChangeMenu:)]) {
[delegate menuItemViewDidChangeMenu:self];
}
// NSLog(@"set menu reset");
[self resetCursorRects];
}
- (NSMenu *)menu
{
return menu;
}
@synthesize delegate;
#pragma mark Index and Point/Rect Correlation
/*!
* @brief Return the index at a point
*
* @param inPoint The point in our local coordinates
*/
- (NSInteger)indexAtPoint:(NSPoint)inPoint
{
CGFloat heightFromTop = [self frame].size.height - inPoint.y;
NSInteger index = 0;
while ((heightFromTop - (((MENU_ITEM_HEIGHT + MENU_ITEM_SPACING) * index) + MENU_ITEM_HEIGHT)) > 0) {
index++;
}
return index;
}
/*!
* @brief Return the rect (in local coordinates) for a menu item by index
*/
- (NSRect)rectForIndex:(NSInteger)index
{
NSRect myFrame = [self frame];
return NSMakeRect(0,
(myFrame.size.height - (((MENU_ITEM_HEIGHT + MENU_ITEM_SPACING) * index) + MENU_ITEM_HEIGHT)),
myFrame.size.width,
MENU_ITEM_HEIGHT);
}
#pragma mark Drawing
- (void)drawRect:(NSRect)inRect
{
NSInteger i, numberOfMenuItems;
BOOL willDisplayACheckbox = NO;
numberOfMenuItems = [menu numberOfItems];
//Determine if one or more menu items is in a non-off (that it, on or mixed) state.
for (i = 0; i < numberOfMenuItems; i++) {
NSMenuItem *menuItem = [menu itemAtIndex:i];
if ([menuItem state] != NSOffState) {
willDisplayACheckbox = YES;
break;
}
}
//Now do the actual drawing
for (i = 0; i < numberOfMenuItems; i++) {
NSRect menuItemRect = [self rectForIndex:i];
if (NSIntersectsRect(menuItemRect,inRect)) {
NSMenuItem *menuItem = [menu itemAtIndex:i];
if ([menuItem isSeparatorItem]) {
//Draw the separatorItem line centered in the menu item rect
menuItemRect.origin.y = (menuItemRect.origin.y + (menuItemRect.size.height / 2) - 1);
menuItemRect.size.height = 1;
[[NSColor grayColor] set];
NSRectFill(menuItemRect);
} else {
NSAttributedString *title;
BOOL currentlyHovered = ((currentHoveredIndex == i) && [menuItem isEnabled]);
if (currentlyHovered) {
//Draw a selectedMenuItemColor box if we are hovered...
[[NSColor selectedMenuItemColor] set];
NSRectFill(menuItemRect);
}
//Move in so the highlight drawn above has proper borders around a checkmark
menuItemRect.origin.x += 1;
menuItemRect.size.width -= 1;
//Indent the menu item if appropriate
CGFloat indentation = [menuItem indentationLevel] * 5.0;
menuItemRect.origin.x += indentation;
menuItemRect.size.width -= indentation;
if ([menuItem state] == NSOnState) {
NSImage *onStateImage;
NSSize size;
if (currentlyHovered) {
//If we're currently hovered, we need to turn our checkmark white...
NSImage *originalImage = [menuItem onStateImage];
size = [originalImage size];
onStateImage = [[[NSImage alloc] initWithSize:[originalImage size]] autorelease];
[onStateImage lockFocus];
//Fill the new image with white
[[NSColor whiteColor] set];
NSRectFill(NSMakeRect(0, 0, size.width, size.height));
//But only keep the white where originalImage (the checkmark) exists
[originalImage drawInRect:NSMakeRect(0, 0, size.width, size.height)
fromRect:NSMakeRect(0, 0, size.width, size.height)
operation:NSCompositeDestinationAtop
fraction:1.0];
[onStateImage unlockFocus];
} else {
onStateImage = [menuItem onStateImage];
size = [onStateImage size];
}
[onStateImage drawAtPoint:NSMakePoint(menuItemRect.origin.x,
menuItemRect.origin.y + ((menuItemRect.size.height - size.height) / 2))
fromRect:NSMakeRect(0, 0, size.width, size.height)
operation:NSCompositeSourceOver
fraction:(currentlyHovered ? 1.0 : 0.85)];
}
NSDictionary *currentTextAttributes;
if (currentlyHovered) {
//We're displaying the hovered menu item
if (!hoveredMenuItemAttributes) {
hoveredMenuItemAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSFont menuFontOfSize:13], NSFontAttributeName,
[NSColor selectedMenuItemTextColor], NSForegroundColorAttributeName,
nil];
}
currentTextAttributes = hoveredMenuItemAttributes;
} else if (![menuItem isEnabled]) {
//We're displaying a disabled menu item
if (!disabledMenuItemAttributes) {
disabledMenuItemAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSFont menuFontOfSize:13], NSFontAttributeName,
[NSColor disabledControlTextColor], NSForegroundColorAttributeName,
nil];
}
currentTextAttributes = disabledMenuItemAttributes;
} else {
//We're displaying a non-hovered menu item
if (!menuItemAttributes) {
menuItemAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSFont menuFontOfSize:13], NSFontAttributeName,
nil];
}
currentTextAttributes = menuItemAttributes;
}
title = [[NSAttributedString alloc] initWithString:[menuItem title]
attributes:currentTextAttributes];
menuItemRect.origin.x += 2;
menuItemRect.size.width -= 2;
if (willDisplayACheckbox) {
//Shift right, and shorten our width, for indentation like a real menu, leaving space for the checkmark if it's needed
#define CHECKMARK_WIDTH 9
menuItemRect.origin.x += CHECKMARK_WIDTH;
menuItemRect.size.width -= CHECKMARK_WIDTH;
}
[title drawInRect:menuItemRect];
[title release];
}
}
}
}
#pragma mark Sizing
- (void)sizeToFit
{
NSRect frame = [self frame];
CGFloat change = (([[self menu] numberOfItems] * (MENU_ITEM_HEIGHT + MENU_ITEM_SPACING)) - MENU_ITEM_SPACING) - frame.size.height;
frame.size.height += change;
frame.origin.y -= change;
[self setFrame:frame];
}
#pragma mark Hovering tracking
/*!
* @brief The mouse is hovering over a point
*
* @param inPoint The point in our local coordinates, or NULL if we are no longer hovering
*/
- (void)setHoveringAtPoint:(NSPoint)inPoint
{
if (!NSEqualPoints(inPoint, NSZeroPoint)) {
currentHoveredIndex = [self indexAtPoint:inPoint];
} else {
currentHoveredIndex = -1;
}
[self setNeedsDisplay:YES];
}
//Cursor entered one of our tracking rects
- (void)mouseEntered:(NSEvent *)theEvent
{
[self setHoveringAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
[super mouseEntered:theEvent];
}
//Cursor left one of our tracking rects
- (void)mouseExited:(NSEvent *)theEvent
{
[self setHoveringAtPoint:NSZeroPoint];
[super mouseExited:theEvent];
}
- (void)mouseDown:(NSEvent *)theEvent
{
if (currentHoveredIndex != -1) {
NSMenuItem *menuItem = [[self menu] itemAtIndex:currentHoveredIndex];
[[menuItem target] performSelector:[menuItem action] withObject:menuItem];
}
[super mouseDown:theEvent];
}
#pragma mark Tracking rects
/*!
* @brief Remove all our tracking rects
*/
- (void)removeTrackingRects
{
NSNumber *trackingTag;
for (trackingTag in trackingTags) {
[self removeTrackingRect:[trackingTag integerValue]];
}
[trackingTags release]; trackingTags = nil;
}
//Reset our cursor tracking
- (void)resetCursorRects
{
//Stop any existing tracking
if (trackingTags) {
[self removeTrackingRects];
}
//Add tracking rects if our superview and window are ready
if ([self superview] && [self window]) {
NSInteger i, numberOfMenuItems;
trackingTags = [[NSMutableSet alloc] init];
numberOfMenuItems = [menu numberOfItems];
for (i = 0; i < numberOfMenuItems; i++) {
NSTrackingRectTag trackingTag;
NSRect trackRect = [self rectForIndex:i];
NSPoint localPoint = [self convertPoint:[[self window] convertScreenToBase:[NSEvent mouseLocation]]
fromView:nil];
BOOL mouseInside = NSPointInRect(localPoint, trackRect);
trackingTag = [self addTrackingRect:trackRect owner:self userData:nil assumeInside:mouseInside];
[trackingTags addObject:[NSNumber numberWithInteger:trackingTag]];
NSLog(@"Added tracking rect %ld for %@ (%@)",trackingTag,NSStringFromRect(trackRect), mouseInside ? @"inside" : @"outside");
if (mouseInside) [self mouseEntered:nil];
}
}
}
@end
|