Here I will hopefully explain how to build a plugin for CarFrontEnd
For anyone that wants to make a plugin, PLEASE let me know and keep me posted on how you are doing and what help/changes you need. I will be glad to support anyone that wants to take a crack at it.
First you will need to include the CarFrontEndAPI framework in your project. Then you need to import the "CarFrontendAPI/CarFrontEndAPI.h" header file, you need to make sure your plugins extension is ".cfep" (CarFrontEnd Plugin), and finally you need to make your main object conform to the CarFrontEndProtocol protocol.
If you want to use my buttons (I still need to fix those images), you need to use a Bezel Button and set it's custom class to "CarFrontEndButton" (import CarFrontEndButton.h from CarFrontEndAPI). I will get around to a IB palette at some point...
The CarFrontEndAPI.h imports the Protocol's header file:
Code:
/*
* CarFrontEndAPI - CarFrontEndProtocol.h - David Whittle (iamgnat@gmail.com)
* Copyright (C) 2007 David Whittle (iamgnat@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import <Foundation/Foundation.h>
@protocol CarFrontEndProtocol <NSObject>
// The init method that the PluginManager will call.
- (id) initWithPluginManager: (id) pluginManager;
// Return the name of the plugin.
- (NSString *) name;
// Allow the plugin to perform any startup initialization that may be desired
// This is called shortly after the plugin is loaded, but is guaranteed to be
// called before the first call to pluginButton or contentViewForSize:.
// This will be called regardless of if the plugin is ever used, so care
// should be taken to only perform work that is absolutely required.
- (void) initalize;
// Return the image to be used for the for the plugin buttons.
// The current size is 119x45, but this is subject to change so make sure
// your image will scale up or down effectively.
// Please cache this data where possible as it will be called as this will
// be called on a frequent basis (0.1 - 0.2 seconds). This allows you to
// perform rough animations (e.g. CPU monitor, etc..)
- (NSImage *) pluginButtonImage;
// Return the view to be used for the content view.
// The client will pass the view size so that you may supply different
// views based on the size. The client will resize the supplied view
// before it is displayed.
- (NSView *) contentViewForSize: (NSSize) size;
// Allow the plugin to perform actions when it's view is replaced.
// Ideally you should stop all processing that is not needed (e.g. UI updates,
// etc..), but obviously some cases will not fit this scenario.
- (void) removePluginFromView;
@end
The notes in the header hopefully give you a good enough idea of what you need to/can do.
Here are the source files for the SamplePlugin plugin. Hopefully they will show you the gory details of what needs to be done. These two files represent all the code for the plugin.
SamplePlugin.h:
Code:
/*
* SamplePlugin - SamplePlugin.h - gnat (iamgnat@gmail.com)
* Copyright (C) 2007 gnat (iamgnat@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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
SamplePlugin.m:
Code:
/*
* SamplePlugin - SamplePlugin.m - gnat (iamgnat@gmail.com)
* Copyright (C) 2007 gnat (iamgnat@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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"];
}
}
@end
Ask away
-dave