One of the nicest things I have found about Cappuccino is its ease of customization. It's often assumed that heavy frameworks limit your options, but with good design they can also expose customization logic in such a way that it is quick and easy to create widgets that perform exactly how you want1.
When I was putting together the first draft of Mahou, I was having trouble finding an intuitive way to provide a hyperlink within my application. Cappuccino focuses on application development, and doesn't play particularly well with the internet at large (although, this is the matter of widgets being created, not an insurmountable barrier).
My solution to the click issue was to subclass CPTextField to create a textfield that takes you to an arbitrary URL when clicked. I'll use this simple example as a brief introduction to extending Cappuccino.
Once you've picked the class to subclass, you have to figure out where in the inheritance tree to subclass it. For example, the CPTextField has a fairly long inheritance chain: CPObject -> CPResponder -> CPView -> CPControl -> CPTextField.
This is usually accomplished by stepping up the inheritance tree and looking for appropriate methods. The different classes focus on different subsets of functionality (CPView handles sizing and placement, CPResponder handles UI events, CPControl handles UI display, and so on).
In my case, I knew that the functionality I wanted to override would lie somewhere in the CPResponder class, because I wanted to customize responding to a UI event.
We continue to focus in by figuring out the specific class methods we'll want to override. For my purposes, the textfield needed to do something when clicked, so I browsed through the documentation and decided I would need to subclass either the mouseDown: or mouseUp methods.
After flipping a coin (and weighing UI responsiveness against the principle of least surprise) I decided to go with mouseDown:.
The first draft of the Objective-J code came out like this:
However, it seemed pretty likely that I would end up wanting more than one clickable link, so hardcoding the URL wasn't really going to cut it. So I made the five minute investment to make the code reusable.
Ah. The joys of accessors and mutators permeate Cappuccino just as they do Cocoa.
The last step is to use WLURLLabel in some code, which will look like this:
varmyFrame=CGRectMake(0,0,100,30);varlabel=[[WLURLLabelalloc]initWithFrame:myFrame];[labelsetStringValue:@"Click here to visit Google."];[labelsetUrl:@"http://www.google.com"];[labelsetFont[CPFontboldSystemFontOfSize:12.0]];// changing color makes links more obvious[fieldsetTextColor:[CPColorwhiteColor]];[contentViewaddSubview:label];
All in all, customizing content in Cappuccino is a fairly simple process. What isn't obvious from such a simple example is that even making large and seemingly complex changes will follow this exact same pattern.
Many of the advantages here are simply because of the import mechanism in Objective-J. Having a coherent import mechanism makes it easy to spread your code out into multiple organized files, which makes code more reusable and modifications quicker to make.
Let me know if you there are any questions or comments!
For those who may be interested, I am simply using the Pygment's Objective-C syntax highligher for the snippets of code. It ends up doing a pretty reasonable job.↩