|
CreatePlugin
How to create a CarFrontEnd plugin
PurposeThis section will show you how to setup and build a Xcode project to create a CarFrontEnd plugin, then install it. The complete example project that is discussed here is available in the Plugins directory of the source tree as "SamplePlugin". PrerequisitesYou must have the CarFrontEndAPI framework installed on your development host. For the purposes of this discussion, it will be assumed that you have installed it as /Library/Frameworks/CarFrontEndAPI.framework. You should also be using the latest version of Xcode which is available from Apple's developer website. Project Creation
#import <Cocoa/Cocoa.h>
#import <CarFrontEndAPI/CarFrontEndAPI.h>
@interface SamplePlugin : NSObject <CarFrontEndProtocol> {
id owner;
IBOutlet NSView *samplePluginView;
}
- (id) initWithPluginManager: (id) pluginManager;
- (NSString *) name;
- (void) initalize;
- (NSImage *) pluginButtonImage;
- (NSView *) contentViewForSize: (NSSize) size;
- (void) removePluginFromView;
- (IBAction) buttonClicked: (id) sender;
@end#import "SamplePlugin.h"
static SamplePlugin *sharedSP = nil;
@implementation SamplePlugin
- (id) init {
return([self initWithPluginManager:nil]);
}
- (id) initWithPluginManager: (id) pluginManager {
if (sharedSP != nil) {
[self release];
return(sharedSP);
}
[super init];
owner = [pluginManager retain];
// Setup for a single instance.
sharedSP = self;
return(self);
}
- (NSString *) name {
return(@"Sample Plugin");
}
- (void) initalize {
// No-op for this example.
// Should generate the button image here rather than on demand.
}
- (NSImage *) pluginButtonImage {
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:[NSFont fontWithName:@"Helvetica" size:26]
forKey:NSFontAttributeName];
[attributes setObject:[NSColor whiteColor]
forKey:NSForegroundColorAttributeName];
NSSize size = [[self name] sizeWithAttributes:attributes];
NSImage *image = [[[NSImage alloc] initWithSize:size] autorelease];
[image lockFocus];
[[self name] drawAtPoint:NSZeroPoint withAttributes:attributes];
[image unlockFocus];
return(image);
}
- (NSView *) contentViewForSize: (NSSize) size {
// We are ignoring the size value, but it is there incase you have differnt
// views based on the size that CarFrontEnd sends you.
if (samplePluginView == nil) {
[NSBundle loadNibNamed:@"SamplePlugin" owner:self];
}
return(samplePluginView);
}
- (void) removePluginFromView {
// No-op
// If you need to do something when your view is no longer displayed,
// add the code here.
}
- (IBAction) buttonClicked: (id) sender {
if ([[sender stringValue] isEqualToString:@"Click"]) {
[sender setStringValue:@"Clock"];
} else if ([[sender stringValue] isEqualToString:@"Clock"]) {
[sender setStringValue:@"Click"];
}
}
@endInstallationCopy the built plugin to your ~/Library/Application Support/CarFrontEnd/Plugins directory and launch CarFrontEnd. If everything went according to plan, your new plugin should be available. |
Sign in to add a comment