Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

How to draw your own table header... (with borders)

// It's a bit hacky at the beginning, but nstableheadercell wasn't exactly making things easy for me

- (void)_drawThemeContents:(NSRect)cellFrame highlighted:(BOOL)highlighted inView:(NSTableHeaderView *)view;
{
	int index = [view columnAtPoint:NSMakePoint(cellFrame.origin.x + 1,cellFrame.origin.y + 1)];
	NSRect headerRect;
	if(index != -1)
		headerRect = [view headerRectOfColumn:index];
	else
		headerRect = NSZeroRect;
	
	[headerImage drawInRect:cellFrame
				   fromRect:NSZeroRect
				  operation:NSCompositeSourceOver
				   fraction:1.0];
	
	[[NSColor colorWithCalibratedRed:207.0/255.0
							   green:207.0/255.0
								blue:207.0/255.0
							   alpha:1.0] set];
	NSRectFill(NSMakeRect(headerRect.origin.x, headerRect.origin.y + 1, headerRect.size.width, headerRect.size.height - 2));
	[headerImage drawInRect:NSMakeRect(headerRect.origin.x, headerRect.origin.y, headerRect.size.width - 1, headerRect.size.height)
				   fromRect:NSZeroRect
				  operation:NSCompositeSourceOver
				   fraction:1.0];
}

How to make the system notify you of audio cd insertion

// Uses the DiskArbitration framework to notify ya of when new audio cds are inserted

NSDictionary *match = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"IOCDMedia", [NSNumber numberWithBool:YES], nil] forKeys:[NSArray arrayWithObjects:(NSString *)kDADiskDescriptionMediaKindKey, kDADiskDescriptionMediaWholeKey, nil]];
		
_session = DASessionCreate(kCFAllocatorDefault);
DASessionScheduleWithRunLoop(_session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
DARegisterDiskAppearedCallback(_session, (CFDictionaryRef)match, diskAppearedCallback, NULL);
DARegisterDiskDisappearedCallback(_session, (CFDictionaryRef)match, diskDisappearedCallback, NULL);

Objective-C and Cocoa: Human-readable file size from number of bytes

// Returns a human-readable string showing the file size from the number of bytes

- (NSString *)stringFromFileSize:(int)theSize
{
	float floatSize = theSize;
	if (theSize<1023)
		return([NSString stringWithFormat:@"%i bytes",theSize]);
	floatSize = floatSize / 1024;
	if (floatSize<1023)
		return([NSString stringWithFormat:@"%1.1f KB",floatSize]);
	floatSize = floatSize / 1024;
	if (floatSize<1023)
		return([NSString stringWithFormat:@"%1.1f MB",floatSize]);
	floatSize = floatSize / 1024;

	// Add as many as you like

	return([NSString stringWithFormat:@"%1.1f GB",floatSize]);
}

Calculate a NSView's frame in QuickDraw coordinates

@interface NSView (frameAsQuickDrawRect)
- (Rect)frameAsQuickDrawRect;
@end
@implementation NSView (frameAsQuickDrawRect)
- (Rect)frameAsQuickDrawRect {
    NSRect  nsrect = [self convertRect:[self bounds] toView:[[self window] contentView]];
    float   windowHeight = NSHeight( [[[self window] contentView] frame] );
    
    float   nsrectHeight = NSHeight( nsrect );
    
    Rect qdrect;
    qdrect.top = windowHeight - (nsrect.origin.y + nsrectHeight);
    qdrect.left = nsrect.origin.x;
    qdrect.bottom = qdrect.top + nsrectHeight;
    qdrect.right = qdrect.left + NSWidth( nsrect );
    
    return qdrect;
}
@end
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS