#import #import #import #import @interface FakeClass : NSObject @end @implementation FakeClass @end OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options); void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview); struct Kara { CGFloat x; CGFloat y; CGFloat direction; }; typedef struct Kara Kara; /* ----------------------------------------------------------------------------- Generate a preview for file This function's job is to create preview for designated file ----------------------------------------------------------------------------- */ void HandleError(NSError *error) { } NSPoint ConvertXMLElelementToPoint(NSXMLElement *element) { NSPoint point; point.x = [[[element attributeForName:@"x"] stringValue] integerValue]; point.y = [[[element attributeForName:@"y"] stringValue] integerValue]; return point; } int fieldBorderLength = 10; NSPoint ConvertPointToFlipped(NSPoint point, NSSize world) { NSPoint flippedPoint; flippedPoint.x = point.x; flippedPoint.y = world.height-point.y-1; return flippedPoint; } void DrawBackground(NSSize world) { NSColor *greenBackground = [NSColor colorWithCalibratedRed:180/255.0 green:230/255.0 blue:180/255.0 alpha:1]; [greenBackground set]; NSRectFill(NSMakeRect(0, 0, world.width*fieldBorderLength, world.height*fieldBorderLength)); } void DrawField(NSSize world) { for (int y = 0; y 0) { NSXMLElement *karaElement = [node.children objectAtIndex:0]; kara.x = [[[karaElement attributeForName:@"x"] stringValue] integerValue]; kara.y = [[[karaElement attributeForName:@"y"] stringValue] integerValue]; kara.direction = [[[karaElement attributeForName:@"direction"] stringValue] integerValue]; karaExists = YES; } } } NSSize world = NSMakeSize(worldWidth, worldHeight); NSSize field = NSMakeSize(worldWidth*fieldBorderLength, worldHeight*fieldBorderLength); CGContextRef cgContext = QLPreviewRequestCreateContext(preview, *(CGSize *)&field, false, NULL); NSGraphicsContext* context = [NSGraphicsContext graphicsContextWithGraphicsPort:(void *)cgContext flipped:YES]; if (context) { [NSGraphicsContext setCurrentContext:context]; DrawBackground(world); DrawField(world); DrawObjects(trees, @"Tree", world); DrawObjects(leafs, @"Shamrock", world); DrawObjects(mushrooms, @"Mushroom", world); if (karaExists) { DrawKara(kara, world); } QLPreviewRequestFlushContext(preview, cgContext); CFRelease(cgContext); } return noErr; } OSStatus GenerateProgramPreview(QLPreviewRequestRef preview, NSXMLElement *root) { return noErr; } OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options) { NSXMLDocument *xmlDoc; NSError *err=nil; NSURL *furl = (__bridge NSURL *)url; if (!furl) { NSLog(@"Can't create an URL from file %@.", furl.path); return noErr; } xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl options:(NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA) error:&err]; if (xmlDoc == nil) { xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl options:NSXMLDocumentTidyXML error:&err]; } if (xmlDoc == nil) { if (err) { HandleError(err); } return noErr; } if (err) { HandleError(err); return noErr; } NSXMLElement *root = xmlDoc.rootElement; if ([furl.pathExtension compare:@"world" options:NSCaseInsensitiveSearch] == NSOrderedSame) { return GenerateWorldPreview(preview, root); } else if ([furl.pathExtension compare:@"kara" options:NSCaseInsensitiveSearch] == NSOrderedSame) { return GenerateProgramPreview(preview, root); } return noErr; } void CancelPreviewGeneration(void *thisInterface, QLPreviewRequestRef preview) { // Implement only if supported }