<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Makes No Sense At All &#187; Cocoa</title>
	<atom:link href="http://www.katoemba.net/makesnosenseatall/tag/cocoa/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.katoemba.net/makesnosenseatall</link>
	<description>Interesting things to share around software development, music and what else comes to mind</description>
	<lastBuildDate>Sat, 21 Jan 2012 09:09:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Signing Amazon Webservices requests</title>
		<link>http://www.katoemba.net/makesnosenseatall/2009/07/11/signing-amazon-webservices-requests/</link>
		<comments>http://www.katoemba.net/makesnosenseatall/2009/07/11/signing-amazon-webservices-requests/#comments</comments>
		<pubDate>Sat, 11 Jul 2009 14:16:45 +0000</pubDate>
		<dc:creator>Berrie</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Amazon Webservices]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[iPhone SDK]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.katoemba.net/makesnosenseatall/2009/07/11/signing-amazon-webservices-requests/</guid>
		<description><![CDATA[MPoD is retrieving cover art for the currently playing song from Amazon the AWS (Amazon Web Services). From August 15 onwards, all requests made will have to be signed using a secret key that is attached to the registered AWSAccessKeyId. Searching on Google, I found various helpful bits, especially on the learn amazon web services [...]]]></description>
			<content:encoded><![CDATA[<p>MPoD is retrieving cover art for the currently playing song from Amazon the AWS (Amazon Web Services). From August 15 onwards, all requests made will have to be signed using a secret key that is attached to the registered AWSAccessKeyId. Searching on Google, I found various helpful bits, especially on the <a href="http://learnaws.com/archives/category/blog" target="_blank">learn amazon web services</a> blog. But I didn&#8217;t find a ready made implementation that I could use directly from my code.</p>
<p>Based on what I found I created a helper class AWSRequestSigner, that takes a dictionary of parameters to pass to the webservice, and returns a ready made NSURL object that can be used in an NSURLRequest object as in the following example:</p>
<pre>
<code>
NSURL *url = [AWSRequestSigner urlForParameterDict:[NSDictionary dictionaryWithObjectsAndKeys:
                                               @"Sea Of Tears",        @"Title",
                                               @"Eilen Jewell",        @"Artist",
                                               @"ItemSearch",          @"Operation",
                                               @"Images",              @"ResponseGroup",
                                               @"Music",               @"SearchIndex",
                                               @"AWSECommerceService", @"Service",
                                               nil]];
</code>
</pre>
<p>To look at the source code for the AWSRequestSigner class, download it <a href="http://www.katoemba.net/makesnosenseatall/wp-content/uploads/2009/07/AWSRequestSigner.zip" title="AWSRequestSigner.zip">here</a> and open it in XCode.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.katoemba.net/makesnosenseatall/2009/07/11/signing-amazon-webservices-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Application integration using the iPhone SDK</title>
		<link>http://www.katoemba.net/makesnosenseatall/2008/11/09/application-integration-using-the-iphone-sdk/</link>
		<comments>http://www.katoemba.net/makesnosenseatall/2008/11/09/application-integration-using-the-iphone-sdk/#comments</comments>
		<pubDate>Sun, 09 Nov 2008 08:30:40 +0000</pubDate>
		<dc:creator>Berrie</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[iPhone SDK]]></category>

		<guid isPermaLink="false">http://www.katoemba.net/makesnosenseatall/2008/11/09/application-integration-using-the-iphone-sdk/</guid>
		<description><![CDATA[Recently the successful iPhone applications Trein and iNap realized a tight integration, in that you can activate iNap directly from Trein. How to create the integration on the side of the &#8216;receiving&#8217; application, iNap in this case, is described in the iPhone SDK example &#8216;Launch Me&#8217;.
When integrating applications, you typically want to pass data from [...]]]></description>
			<content:encoded><![CDATA[<p>Recently the successful iPhone applications Trein and <a href="http://www.moop.me/inap.php" target="_blank">iNap</a> realized a tight integration, in that you can activate iNap directly from Trein. How to create the integration on the side of the &#8216;receiving&#8217; application, iNap in this case, is described in the iPhone SDK example &#8216;Launch Me&#8217;.<br/><br />
When integrating applications, you typically want to pass data from one to the other, in this post I&#8217;ll describe how the incoming parameters can easily be extracted in the receiving application.<br/><br />
The receiving application will register itself in the OS (for details see the &#8216;Launch Me&#8217; example), and the result is that it can be activated from another application or from a hyperlink in a webpage. An example of a url that activates another application is:<br/></p>
<pre>
<code>
myapp://myapp?latitude=51.3&#038;longitude=5.2&#038;title=middle of nowhere
</code>
</pre>
<p>In the receiving application the method <code>- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url</code> will be called on the application delegate object, and in this method you need to extract the parameters from the URL. The following code shows how to do that with minimal code. Like Apple&#8217;s example, it does a lot of checking to prevent undesired usage, in this case the url parameters must meet the exact specifications (i.e. no missing parameters, no extra parameters) otherwise the request will be ignored.</p>
<pre>
<code>
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
  // You should be extremely careful when handling URL requests.
  // You must take steps to validate the URL before handling it.
  // If any of the validations fails, bail out returning NO.
  if (!url) {
    // The URL is nil. There's nothing more to do.
    return NO;
  }

  // Split the url into two sections, one section with the base url and one with the parameters.
  NSArray *urlSections = [[url absoluteString] componentsSeparatedByCharactersInSet:
              [NSCharacterSet characterSetWithCharactersInString:@"?"]];

  // Check that you actually got a base section and a parameter section.
  if ([urlSections count] != 2) {
    return NO;
  }

  // Check that the base part of the url is what you expect it to be.
  if (![[urlSections objectAtIndex:0] isEqualToString:@"myapp://myapp"]) {
    return NO;
  }

  // Split up the parameter section into a parameter Array.
  NSArray *parameterArray = [[urlSections objectAtIndex:1] componentsSeparatedByCharactersInSet:
              [NSCharacterSet characterSetWithCharactersInString:@"&#038;="]];
  NSEnumerator *parameterEnum = [parameterArray objectEnumerator];
  NSString *parameter, *value;
  // The parameters values that we are expecting.
  NSString *latitude = nil, *longitude = nil, *title = nil;

  // Run through the array of parameters.
  while (parameter = [parameterEnum nextObject]) {
    // Get the value that goes with this parameter, and if it is missing break.
    if ((value = [parameterEnum nextObject]) == nil)
      break;

    // Check the parameter value against the parameters that you support/expect,
    // and assign the matching value to the right variable.
    if ([parameter isEqualToString:@"latitude"])
      latitude = value;
    else if ([parameter isEqualToString:@"longitude"])
      longitude = value;
    else if ([parameter isEqualToString:@"title"])
      title = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    else {
      // An unexpected parameter was passed, bail out.
      latitude = nil;
      longitude = nil;
      title = nil;
      break;
    }
  }

  // Check if we got all parameters (in this case all are mandatory), if not bail out.
  if (latitude == nil || longitude == nil || title == nil) {
    return NO;
  }

  // Use the parameters to do your thing.
  CLLocation *location = [[[CLLocation alloc]
                               initWithLatitude:[latitude doubleValue]
                               longitude:[longitude doubleValue]] autorelease];
  [someObject activateDestination:title location:location];

  return YES;
}
</code>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.katoemba.net/makesnosenseatall/2008/11/09/application-integration-using-the-iphone-sdk/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Multiline cells in NSTableView</title>
		<link>http://www.katoemba.net/makesnosenseatall/2008/02/05/multiline-cells-in-nstableview/</link>
		<comments>http://www.katoemba.net/makesnosenseatall/2008/02/05/multiline-cells-in-nstableview/#comments</comments>
		<pubDate>Tue, 05 Feb 2008 20:53:08 +0000</pubDate>
		<dc:creator>Berrie</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[core data]]></category>
		<category><![CDATA[data binding]]></category>
		<category><![CDATA[NSAttributedString]]></category>
		<category><![CDATA[NSTableView]]></category>

		<guid isPermaLink="false">http://www.katoemba.net/makesnosenseatall/index.php/2008/01/20/multiline-cells-in-nstableview/</guid>
		<description><![CDATA[When you&#8217;re building an application that is based on data records, and each record contains a significant amount of data that you want to display in a table row, the space per row may be too small to fit everything. You can add every data element in its own column, but this leads to horizontal [...]]]></description>
			<content:encoded><![CDATA[<p>When you&#8217;re building an application that is based on data records, and each record contains a significant amount of data that you want to display in a table row, the space per row may be too small to fit everything. You can add every data element in its own column, but this leads to horizontal scrolling which can be cumbersome. A better solution to this problem is combining multiple data elements in a single cell. I&#8217;ll demonstrate how this can be done with a core data based application using data binding through a basic example. The end result of the finalized sample will look like this:
</p>
<p><center><br />
<img src="http://www.katoemba.net/makesnosenseatall/wp-content/uploads/2008/02/multilinecell.png" width="463" height="321" alt="MultilineCell Sample App"/><br />
</center></p>
<p>For the sample I have made a core data entity named Order with 2 real attributes, orderNumber and orderLine. To combine these two elements into a single cell I add another transient attribute to the entity, named orderNumberLine. </p>
<p><center><br />
<a href="http://www.katoemba.net/makesnosenseatall/wp-content/uploads/2008/02/datamodel1.png"><img src="http://www.katoemba.net/makesnosenseatall/wp-content/uploads/2008/02/datamodel1.png" width="300" height="156" alt="Datamodel.png"/></a><br />
</center></p>
<p>Because the attribute is transient it will not become part of a file when you save the model. This is fine because whenever we need the value for the orderNumberLine attribute we will compose it from the orderNumber and orderLine values.
</p>
<p>The entity is linked to the class Order, in which we override the standard key-value method orderNumberLine:
</p>
<p>
<pre><code>
// Compose an attributed string that combines the values of the
// orderNumber and orderLine attributes.
- (NSString*)orderNumberLine
{
	NSString *tmpValue;
	NSMutableAttributedString *attrValue;
	NSDictionary *boldFont =
		[NSDictionary dictionaryWithObject:[NSFont boldSystemFontOfSize:10.0]
					forKey:NSFontAttributeName];
	NSDictionary *normalFont =
		[NSDictionary dictionaryWithObject:[NSFont systemFontOfSize:10.0]
					forKey:NSFontAttributeName];

	[self willAccessValueForKey:@"orderNumberLine"];
	// Put the order number in bold
	attrValue = [[NSMutableAttributedString alloc]
				initWithString:(self.orderNumber?self.orderNumber:@"<Unknown>")
				attributes:boldFont];
	// Then add a newline
	[attrValue appendAttributedString:[[NSAttributedString alloc]
				initWithString:@"\n"]];
	// And finally add the order line in normal font
	[attrValue appendAttributedString:[[NSAttributedString alloc]
				initWithString:(self.orderLine?[self.orderLine stringValue]:@"<Unknown>")
				attributes:normalFont]];

	tmpValue = [[NSAttributedString alloc] initWithAttributedString:attrValue];
	[self didAccessValueForKey:@"orderNumberLine"];

	return tmpValue;
}
</code></pre>
</p>
<p>The orderNumberLine method will compose a <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/Reference/Reference.html" target="_blank">NSAttributedString</a> that consist of the orderNumber and orderLine, separated by a newline. Because we&#8217;re using an attributed string we can also use different fonts for different elements, here the orderNumber is in 10 point bold, while the orderLine is in 10 point regular.<br />
<br/>We can now bind the orderNumberLine value to a cell in an <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTableView_Class/Reference/Reference.html" target="_blank">NSTableView</a> in Interface Builder, then the cell will show a 2 line value. To properly display you need to increase the row height of the table view (in this case for a 10 point font size it to 26).
</p>
<p>When you build the application adding only the orderNumberLine method to the Order class, you will notice that the cell that is bound to this method will display properly when it is redrawn (for example through scrolling or selecting the row). But when the value of either orderNumber or orderLine is changed elsewhere in the interface, the cell containing the composite string will not automatically redraw. This can easily be solved by doing an update to orderNumberLine attribute whenever one of its components (orderNumber or orderLine) changes. Then the bindings will take care that the UI is updated immediately. It doesn&#8217;t matter what you set the value of orderNumberLine to because it will be calculated upon retrieval anyway. The two following two methods demonstrate this:
</p>
<p>
<pre><code>
// Override to trigger update of transient attribute orderNumberLine
- (void)setOrderNumber:(NSString*)value
{
	[self willChangeValueForKey:@"orderNumber"];
	[self setPrimitiveValue:value forKey: @"orderNumber"];
	[self didChangeValueForKey:@"orderNumber"];

	// Set the orderNumberLine to trigger updates to bound UI elements
	// The value we set it do doesn't matter as the actual value
	// is calculated whenever the orderNumberLine is fetched.
	self.orderNumberLine = @"";
}

// Override to trigger update of transient attribute orderNumberLine
- (void)setOrderLine:(NSNumber*)value
{
	[self willChangeValueForKey:@"orderLine"];
	[self setPrimitiveValue:value forKey: @"orderLine"];
	[self didChangeValueForKey:@"orderLine"];

	// Set the orderNumberLine to trigger updates to bound UI elements
	// The value we set it do doesn't matter as the actual value
	// is calculated whenever the orderNumberLine is fetched.
	self.orderNumberLine = @"";
}
</code></pre>
</p>
<p>To conclude, by adding a transient attribute, and writing three methods we can display a composed string in a cell. As this string can be an attributed string, we can apply different formatting to the elements composing the string. To look at the complete example, download it<br />
<a href="http://www.katoemba.net/makesnosenseatall/wp-content/uploads/2008/02/multilinecell.zip" title="MultilineCell.zip">here</a> and open it in XCode.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.katoemba.net/makesnosenseatall/2008/02/05/multiline-cells-in-nstableview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Safari like button bar in Interface Builder</title>
		<link>http://www.katoemba.net/makesnosenseatall/2008/01/13/safari-like-button-bar-in-interface-builder/</link>
		<comments>http://www.katoemba.net/makesnosenseatall/2008/01/13/safari-like-button-bar-in-interface-builder/#comments</comments>
		<pubDate>Sun, 13 Jan 2008 15:08:55 +0000</pubDate>
		<dc:creator>Berrie</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Button Bar]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Mac OS X 10.5]]></category>
		<category><![CDATA[NSMatrix]]></category>

		<guid isPermaLink="false">http://www.katoemba.net/makesnosenseatall/index.php/2008/01/13/safari-like-button-bar-in-interface-builder/</guid>
		<description><![CDATA[In the previous post on the use of NSGradient I showed a screenshot of a button bar similar to the one that is used in Safari to store your favourites. For an application I&#8217;m writing I have an NSTableView with date based rows, and I want to allow easy filtering the table contents on date [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous post on the <a href="/makesnosenseatall/index.php/2008/01/09/nsview-with-gradient-background/">use of NSGradient</a> I showed a screenshot of a button bar similar to the one that is used in Safari to store your favourites. For an application I&#8217;m writing I have an <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTableView_Class/Reference/Reference.html" target="_blank">NSTableView</a> with date based rows, and I want to allow easy filtering the table contents on date ranges like month, last month, week, last week etcetera. A button bar is nice looking way of doing this, the screenshot below shows what this looks like (in Dutch, but you&#8217;ll get the idea).</p>
<p><center><img src="http://www.katoemba.net/makesnosenseatall/wp-content/uploads/2008/01/filtering-screenshot1.png" width="567" height="210"/><br />
</center></p>
<p>Until recently you had to use for example <a href="http://www.harmless.de/cocoa-code.php" target="_blank">AMButtonBar</a> to get a button bar like this, but with Leopard our developers life has again been made easier: you can now create the button bar in Interface Builder, using only standard components.<br/></p>
<p>Start by dropping an <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMatrix_Class/Reference/Reference.html" target="_blank">NSMatrix</a> component (you will find this in the objects library under Views &amp; Cells, Inputs &amp; Values) onto your view. Per default it creates a matrix of 1 column, 2 rows with radio buttons on it. Then set the correct prototype on the <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMatrix_Class/Reference/Reference.html" target="_blank">NSMatrix</a> object so that newly added buttons will have look right. Click on the little circle that is visible on the right bottom of the <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMatrix_Class/Reference/Reference.html" target="_blank">NSMatrix</a> when it&#8217;s selected,</p>
<p><center><br />
<img src="http://www.katoemba.net/makesnosenseatall/wp-content/uploads/2008/01/prototype-screenshot.png" width="362" height="83"/><br />
</center></p>
<p>then the prototype attributes are shown in the Inspector window. Select <b>Recessed</b> as <b>Style</b>, and use centered <b>Alignment</b>. Select the right value under <b>Position</b>, based on whether you want to add an image to the buttons or not. In this case I choose not to use images.</p>
<p><center><br />
<img src="http://www.katoemba.net/makesnosenseatall/wp-content/uploads/2008/01/prototype-attributes-screenshot.png" width="272" height="345"/><br />
</center></p>
<p>Once the prototype is setup properly, select the <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMatrix_Class/Reference/Reference.html" target="_blank">NSMatrix</a> object and have a look at the attributes in the Inspector window.
</p>
<p><center><br />
<img src="http://www.katoemba.net/makesnosenseatall/wp-content/uploads/2008/01/matrix-attributes-screenshot.png" width="271" height="171"/><br />
</center></p>
<p>Change the number of rows and columns to meet your needs, in this case I&#8217;m using 1 row and 6 columns. You end up with one traditional radio button (the one that was originally there) and 5 new buttons according to the prototype. Select the first button and manually set its properties so that it matches the prototype as well.<br/><br />
Next, for each button edit the button <b>Title</b> and specify a unique <b>Tag</b> value in the Inspector window, so that you can distinguish which button was clicked in the attached method in your code later on. Optionally you can add a tooltip under the Identity tab on the Inspector window.
</p>
<p>The final step is to write the method in the controller class, and attach it to the <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSMatrix_Class/Reference/Reference.html" target="_blank">NSMatrix</a> object. When you use the tag values to identify which button was clicked you will only need a single method, and don&#8217;t have to attach a method directly to the buttons. A sample implementation method:</p>
<p>
<pre><code>
- (IBAction)filterData:(id)sender
{
    int tag = [sender selectedTag];

    switch (tag) {
        case 1000: // Show all
            // Remove data  filtering so that all rows are shown
            break;
        case 1001: // Show this month
            // Filter data so that only rows with a date between
            // first and last day of current month are shown
            break;
        case 1002: // Show last month
            // Filter data so that only rows with a date between
            // first and last day of previous month are shown
            break;
        default:
            // Should never get here
            break;
    }
}
</code></pre>
</p>
<p>If you&#8217;re using coredata filtering the data is very easy, as you only need to create a <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html" target="_blank">NSPredicate</a> object in which you specify the filtering criteria, and set that predicate on the <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSArrayController_Class/Reference/Reference.html" target="_blank">NSArrayController</a> that holds the rows. This is beyond the scope of this post, if you have questions on that let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.katoemba.net/makesnosenseatall/2008/01/13/safari-like-button-bar-in-interface-builder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NSView with gradient background</title>
		<link>http://www.katoemba.net/makesnosenseatall/2008/01/09/nsview-with-gradient-background/</link>
		<comments>http://www.katoemba.net/makesnosenseatall/2008/01/09/nsview-with-gradient-background/#comments</comments>
		<pubDate>Wed, 09 Jan 2008 21:27:41 +0000</pubDate>
		<dc:creator>Berrie</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Colored view]]></category>
		<category><![CDATA[Mac OS X 10.5]]></category>
		<category><![CDATA[NSGradient]]></category>
		<category><![CDATA[NSView]]></category>

		<guid isPermaLink="false">http://www.katoemba.net/makesnosenseatall/index.php/2008/01/09/nsview-with-gradient-background/</guid>
		<description><![CDATA[In Mac OS X 10.5 a new class NSGradient is introduced, that makes it really easy to work with gradients (as you might have guessed from the name). The sample code below shows a basic yet functional implementation of an NSView subclass that has either an colored or gradient background.
ColorGradientView.h
@interface ColorGradientView : NSView
{
  NSColor [...]]]></description>
			<content:encoded><![CDATA[<p>In Mac OS X 10.5 a new class <a href="http://developer.apple.com/documentation/Cocoa/Reference/NSGradient_class/Reference/Reference.html" target="_blank">NSGradient</a> is introduced, that makes it really easy to work with gradients (as you might have guessed from the name). The sample code below shows a basic yet functional implementation of an <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html" target="_blank">NSView</a> subclass that has either an colored or gradient background.</p>
<p>ColorGradientView.h</p>
<pre><code>@interface ColorGradientView : NSView
{
  NSColor *startingColor;
  NSColor *endingColor;
  int angle;
}

// Define the variables as properties
@property(nonatomic, retain) NSColor *startingColor;
@property(nonatomic, retain) NSColor *endingColor;
@property(assign) int angle;

@end
</code></pre>
<p>ColorGradientView.m</p>
<pre><code>#import "GradientView.h"
@implementation ColorGradientView

// Automatically create accessor methods
@synthesize startingColor;
@synthesize endingColor;
@synthesize angle;

- (id)initWithFrame:(NSRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    // Initialization code here.
    [self setStartingColor:[NSColor colorWithCalibratedWhite:1.0 alpha:1.0]];
    [self setEndingColor:nil];
    [self setAngle:270];
  }
  return self;
}

- (void)drawRect:(NSRect)rect {
  if (endingColor == nil || [startingColor isEqual:endingColor]) {
    // Fill view with a standard background color
    [startingColor set];
    NSRectFill(rect);
  }
  else {
    // Fill view with a top-down gradient
    // from startingColor to endingColor
    NSGradient* aGradient = [[NSGradient alloc]
        initWithStartingColor:startingColor
        endingColor:endingColor];
    [aGradient drawInRect:[self bounds] angle:angle];
  }
}

@end
</code></pre>
<p>Note that the code is based on the new garbage collection mechanism available in Objective-C 2.0, so there are no retain or release calls.</p>
<p>In the following picture you see a gradient effect from top to bottom to give a subtle 3d appearance:</p>
<p><img src="http://www.katoemba.net/makesnosenseatall/wp-content/uploads/2008/01/gradient-screenshot.png" alt="" width="566" height="26" /></p>
<p>To achieve this, add a <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html" target="_blank">NSView</a> in Interface Builder and define its class as ColorGradientView. In your controller, add a ColorGradientView outlet (e.g. myGradientView) and in Interface Builder attach the view to the outlet. Add the following 3 lines to the <code>- (void)awakeFromNib</code> method in the controller:</p>
<pre><code>
[myGradientView setStartingColor:
    [NSColor colorWithCalibratedWhite:0.85 alpha:1.0]];
[myGradientView setEndingColor:
    [NSColor colorWithCalibratedWhite:0.7 alpha:1.0]];
[myGradientView setAngle:270];
</code></pre>
<p>In upcoming post I will go into details of how to create the Safari like buttons that you see in the screen shot.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.katoemba.net/makesnosenseatall/2008/01/09/nsview-with-gradient-background/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Build a toolbar in Interface Builder</title>
		<link>http://www.katoemba.net/makesnosenseatall/2008/01/06/build-a-toolbar-in-interface-builder/</link>
		<comments>http://www.katoemba.net/makesnosenseatall/2008/01/06/build-a-toolbar-in-interface-builder/#comments</comments>
		<pubDate>Sun, 06 Jan 2008 20:05:58 +0000</pubDate>
		<dc:creator>Berrie</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[NSToolbar]]></category>

		<guid isPermaLink="false">http://www.katoemba.net/makesnosenseatall/index.php/2008/01/06/build-a-toolbar-in-interface-builder/</guid>
		<description><![CDATA[Interface Builder 3 (IB3), part of the development tools that are shipped with Mac OS X Leopard, finally allows building an application window toolbar. Until the release of IB3 the setup had to be done entirely in code, or through a 3rd party extension to IB. Allthough the toolbar in the application I’m working on [...]]]></description>
			<content:encoded><![CDATA[<p>Interface Builder 3 (IB3), part of the development tools that are shipped with Mac OS X Leopard, finally allows building an application window toolbar. Until the release of IB3 the setup had to be done entirely in code, or through a 3rd party extension to IB. Allthough the toolbar in the application I’m working on was working fine, I couldn’t resist the temptation to move its definition into my NIB file and clean up my code a bit.</p>
<p>In the objects library, under Application there is a new set of objects filed as Toolbar. You can drop a toolbar object onto a window which will give you a standard implementation of the toolbar, with icons for Show colors, Show fonts, Print and Customize. Select the toolbar to change its default properties in the Attribute Inspector window. Note that a new toolbar has the Customizable checkbox unchecked, meaning that a user of you application can’t customize the toolbar. Simply check it to change this behaviour.</p>
<p>To change the toolbar, you need to click on it in the window where you dropped it, which shows a panel containing the allowed toolbar items.</p>
<p>
<img src="http://www.katoemba.net/makesnosenseatall/wp-content/uploads/2008/01/ib-toolbar-screenshot.png" width="634" height="365"/>
</p>
<p>From the object library you can add toolbar items, either one of the standard items or an image item. Alternatively you can drop an image directly from the media library which will create an image item. On the Attribute Inspector window you can change the attributes for an image toolbar item (on Toolbar Item Attributes), and add a Tooltip (on Toolbar Item Identity). To change the standard toolbar that will be active when the application is started, drag items from the panel onto the toolbar, or drag items you don’t want out of the toolbar.</p>
<p>The standard items will have default activities attached to them, for the image items you added yourself you will have to add an action by Control-dragging from the item to an object (usually FirstResponder or File’s Owner) and selecting the action you want executed when the item is clicked.</p>
<p>This is all really easy, as long as you keep in mind that attributes and actions are specified on the items in the panel with the allowed items, rather on the items in the toolbar itself. While most of the configuration can now be done in IB, you still need to implement the method:</p>
<p><code>
<pre>- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem</pre>
<p></code></p>
<p>in the class that is the delegate for the toolbar. In the implementation of the method you need to check for which item it was called, and return YES or NO depending on whether you want the item to be active (clickable) or not. You set tag values for the different items in IB against which you can check for which item the validateToolbarItem method is called. A sample implementation for a save button in a document based application could look like this:</p>
<p><code>
<pre>- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem
{
&nbsp;&nbsp;&nbsp;&nbsp;if ([theItem tag] == 1001) // Save toolbar item
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return [self isDocumentEdited];
&nbsp;&nbsp;&nbsp;&nbsp;return YES;
}</pre>
<p></code></p>
<p>One thing to note: if you have put a toolbar into your NIB file, you will need to save the file in either XIB3.x or NIB3.x format. Saving in NIB2.x format is not supported, so you lose backwards compatibility with IB2.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.katoemba.net/makesnosenseatall/2008/01/06/build-a-toolbar-in-interface-builder/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

