<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: cocoa code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 01:58:44 GMT</pubDate>
    <description>DZone Snippets: cocoa code</description>
    <item>
      <title>Having Rails &amp; Cocoa play together</title>
      <link>http://snippets.dzone.com/posts/show/4944</link>
      <description>// description of your code here&lt;br /&gt;Play a Quartz composition from within a Rails application.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Render a Quartz Composition ".qtz" file to buffer, as jpeg data&lt;br /&gt;# Written by PLR from Pierlis (http://pierlis.com)&lt;br /&gt;# Use at your own risk, reuse at will.&lt;br /&gt;&lt;br /&gt;# Cocoa and the Quartz framework must have been included in config/environment.rb.&lt;br /&gt;&lt;br /&gt;class Composition&lt;br /&gt;  &lt;br /&gt;  # creates a composition from a .qtz file whose path is qtz_path&lt;br /&gt;  def initialize(qtz_path, width = 350, height = 100)&lt;br /&gt;    @qtz = QCComposition.compositionWithFile(qtz_path)&lt;br /&gt;    raise "Can't find a valid composition at &#8220;#{qtz_path}&#8221;." if @qtz.nil?&lt;br /&gt;    @renderer = QCRenderer.alloc.initOffScreenWithSize_colorSpace_composition(&lt;br /&gt;                        [width, height],&lt;br /&gt;                        CGColorSpaceCreateWithName(KCGColorSpaceGenericRGB),&lt;br /&gt;                        @qtz)&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  # renders the composition at time, with parameters, and return the jpeg data.&lt;br /&gt;  def render(time = 0, parameters = {})&lt;br /&gt;    # Only pass in parameters expected by the Qtz composition&lt;br /&gt;    input_keys = @qtz.inputKeys&lt;br /&gt;    parameters.each do |key, value|&lt;br /&gt;      @renderer.setValue_forInputKey(value, key) if input_keys.include? key&lt;br /&gt;    end&lt;br /&gt;    # And render the composition&lt;br /&gt;    rendering_ok = @renderer.renderAtTime_arguments(time, nil)&lt;br /&gt;    raise "Unable to render Quartz Composition" unless rendering_ok == 1&lt;br /&gt;    &lt;br /&gt;    # now convert the result to jpeg&lt;br /&gt;    bitmapRep = @renderer.createSnapshotImageOfType('NSBitmapImageRep')&lt;br /&gt;    jpegimagedata = bitmapRep.representationUsingType_properties(NSJPEGFileType, &lt;br /&gt;      {NSImageCompressionFactor =&gt; 0.8}&lt;br /&gt;      ).rubyString &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;See more on : &lt;http://pierlis.com/blog/2008/1/2/having-rails-cocoa-play-together&gt;</description>
      <pubDate>Thu, 03 Jan 2008 14:47:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4944</guid>
      <author>pierlis (Pierlis)</author>
    </item>
    <item>
      <title>How to draw your own table header... (with borders)</title>
      <link>http://snippets.dzone.com/posts/show/3931</link>
      <description>// It's a bit  hacky at the beginning, but nstableheadercell wasn't exactly making things easy for me&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;- (void)_drawThemeContents:(NSRect)cellFrame highlighted:(BOOL)highlighted inView:(NSTableHeaderView *)view;&lt;br /&gt;{&lt;br /&gt;	int index = [view columnAtPoint:NSMakePoint(cellFrame.origin.x + 1,cellFrame.origin.y + 1)];&lt;br /&gt;	NSRect headerRect;&lt;br /&gt;	if(index != -1)&lt;br /&gt;		headerRect = [view headerRectOfColumn:index];&lt;br /&gt;	else&lt;br /&gt;		headerRect = NSZeroRect;&lt;br /&gt;	&lt;br /&gt;	[headerImage drawInRect:cellFrame&lt;br /&gt;				   fromRect:NSZeroRect&lt;br /&gt;				  operation:NSCompositeSourceOver&lt;br /&gt;				   fraction:1.0];&lt;br /&gt;	&lt;br /&gt;	[[NSColor colorWithCalibratedRed:207.0/255.0&lt;br /&gt;							   green:207.0/255.0&lt;br /&gt;								blue:207.0/255.0&lt;br /&gt;							   alpha:1.0] set];&lt;br /&gt;	NSRectFill(NSMakeRect(headerRect.origin.x, headerRect.origin.y + 1, headerRect.size.width, headerRect.size.height - 2));&lt;br /&gt;	[headerImage drawInRect:NSMakeRect(headerRect.origin.x, headerRect.origin.y, headerRect.size.width - 1, headerRect.size.height)&lt;br /&gt;				   fromRect:NSZeroRect&lt;br /&gt;				  operation:NSCompositeSourceOver&lt;br /&gt;				   fraction:1.0];&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 28 Apr 2007 19:48:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3931</guid>
      <author>aptiva (Fj&#195;&#182;lnir &#195;?sgeirsson)</author>
    </item>
    <item>
      <title>How to make the system notify you of audio cd insertion</title>
      <link>http://snippets.dzone.com/posts/show/3856</link>
      <description>// Uses the DiskArbitration framework to notify ya of when new audio cds are inserted&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;NSDictionary *match = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"IOCDMedia", [NSNumber numberWithBool:YES], nil] forKeys:[NSArray arrayWithObjects:(NSString *)kDADiskDescriptionMediaKindKey, kDADiskDescriptionMediaWholeKey, nil]];&lt;br /&gt;		&lt;br /&gt;_session = DASessionCreate(kCFAllocatorDefault);&lt;br /&gt;DASessionScheduleWithRunLoop(_session, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);&lt;br /&gt;DARegisterDiskAppearedCallback(_session, (CFDictionaryRef)match, diskAppearedCallback, NULL);&lt;br /&gt;DARegisterDiskDisappearedCallback(_session, (CFDictionaryRef)match, diskDisappearedCallback, NULL);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 23 Apr 2007 12:05:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3856</guid>
      <author>aptiva (Fj&#195;&#182;lnir &#195;?sgeirsson)</author>
    </item>
    <item>
      <title>Objective-C and Cocoa: Human-readable file size from number of bytes</title>
      <link>http://snippets.dzone.com/posts/show/3038</link>
      <description>// Returns a human-readable string showing the file size from the number of bytes&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;- (NSString *)stringFromFileSize:(int)theSize&lt;br /&gt;{&lt;br /&gt;	float floatSize = theSize;&lt;br /&gt;	if (theSize&lt;1023)&lt;br /&gt;		return([NSString stringWithFormat:@"%i bytes",theSize]);&lt;br /&gt;	floatSize = floatSize / 1024;&lt;br /&gt;	if (floatSize&lt;1023)&lt;br /&gt;		return([NSString stringWithFormat:@"%1.1f KB",floatSize]);&lt;br /&gt;	floatSize = floatSize / 1024;&lt;br /&gt;	if (floatSize&lt;1023)&lt;br /&gt;		return([NSString stringWithFormat:@"%1.1f MB",floatSize]);&lt;br /&gt;	floatSize = floatSize / 1024;&lt;br /&gt;&lt;br /&gt;	// Add as many as you like&lt;br /&gt;&lt;br /&gt;	return([NSString stringWithFormat:@"%1.1f GB",floatSize]);&lt;br /&gt;}&lt;/code&gt;</description>
      <pubDate>Sun, 26 Nov 2006 05:51:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3038</guid>
      <author>maximile (Max Williams)</author>
    </item>
    <item>
      <title>Calculate a NSView's frame in QuickDraw coordinates</title>
      <link>http://snippets.dzone.com/posts/show/918</link>
      <description>&lt;code&gt;@interface NSView (frameAsQuickDrawRect)&lt;br /&gt;- (Rect)frameAsQuickDrawRect;&lt;br /&gt;@end&lt;br /&gt;@implementation NSView (frameAsQuickDrawRect)&lt;br /&gt;- (Rect)frameAsQuickDrawRect {&lt;br /&gt;    NSRect  nsrect = [self convertRect:[self bounds] toView:[[self window] contentView]];&lt;br /&gt;    float   windowHeight = NSHeight( [[[self window] contentView] frame] );&lt;br /&gt;    &lt;br /&gt;    float   nsrectHeight = NSHeight( nsrect );&lt;br /&gt;    &lt;br /&gt;    Rect qdrect;&lt;br /&gt;    qdrect.top = windowHeight - (nsrect.origin.y + nsrectHeight);&lt;br /&gt;    qdrect.left = nsrect.origin.x;&lt;br /&gt;    qdrect.bottom = qdrect.top + nsrectHeight;&lt;br /&gt;    qdrect.right = qdrect.left + NSWidth( nsrect );&lt;br /&gt;    &lt;br /&gt;    return qdrect;&lt;br /&gt;}&lt;br /&gt;@end&lt;/code&gt;</description>
      <pubDate>Mon, 28 Nov 2005 14:34:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/918</guid>
      <author>rentzsch (Jonathan 'Wolf' Rentzsch)</author>
    </item>
  </channel>
</rss>
