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-5 of 5 total  RSS 

Having Rails & Cocoa play together

// description of your code here
Play a Quartz composition from within a Rails application.
# Render a Quartz Composition ".qtz" file to buffer, as jpeg data
# Written by PLR from Pierlis (http://pierlis.com)
# Use at your own risk, reuse at will.

# Cocoa and the Quartz framework must have been included in config/environment.rb.

class Composition
  
  # creates a composition from a .qtz file whose path is qtz_path
  def initialize(qtz_path, width = 350, height = 100)
    @qtz = QCComposition.compositionWithFile(qtz_path)
    raise "Can't find a valid composition at “#{qtz_path}”." if @qtz.nil?
    @renderer = QCRenderer.alloc.initOffScreenWithSize_colorSpace_composition(
                        [width, height],
                        CGColorSpaceCreateWithName(KCGColorSpaceGenericRGB),
                        @qtz)
  end
  
  # renders the composition at time, with parameters, and return the jpeg data.
  def render(time = 0, parameters = {})
    # Only pass in parameters expected by the Qtz composition
    input_keys = @qtz.inputKeys
    parameters.each do |key, value|
      @renderer.setValue_forInputKey(value, key) if input_keys.include? key
    end
    # And render the composition
    rendering_ok = @renderer.renderAtTime_arguments(time, nil)
    raise "Unable to render Quartz Composition" unless rendering_ok == 1
    
    # now convert the result to jpeg
    bitmapRep = @renderer.createSnapshotImageOfType('NSBitmapImageRep')
    jpegimagedata = bitmapRep.representationUsingType_properties(NSJPEGFileType, 
      {NSImageCompressionFactor => 0.8}
      ).rubyString 
  end
end


See more on : <http://pierlis.com/blog/2008/1/2/having-rails-cocoa-play-together>

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-5 of 5 total  RSS