123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- #import "AppDelegate.h"
- #import "DemoSVGObject.h"
- #ifndef DEBUG
- #define DEBUG 0
- #endif
- @interface RoseReturnFunc : NSObject
- @property (weak) SVGKImageView *theView;
- @property (strong) NSObject<DemoSVGObject> *imagePath;
- @end
- @implementation RoseReturnFunc
- @end
- @interface AppDelegate ()
- @property (readwrite, strong) NSArray *svgArray;
- @end
- @implementation AppDelegate
- - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
- {
- NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
- NSString *pname;
-
- _cacheEnabled = NO;
-
- NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager] enumeratorAtPath:[[NSBundle mainBundle] resourcePath]];
-
- while (pname = [dirEnum nextObject]) {
-
- if ([[dirEnum fileAttributes][NSFileType] isEqualToString:NSFileTypeDirectory]) {
- if (!(NSOrderedSame == [[pname pathExtension] caseInsensitiveCompare:@"lproj"])) {
- [dirEnum skipDescendants];
- }
- continue;
- }
- if (NSOrderedSame == [[[pname lastPathComponent] pathExtension] caseInsensitiveCompare:@"svg"]) {
- DemoSVGObject *tmpObj = [[DemoSVGBundleObject alloc] initWithName:[pname lastPathComponent]];
- if (![tmpArray containsObject:tmpObj]) {
- [tmpArray addObject:tmpObj];
- }
- }
- }
-
-
-
- @autoreleasepool {
- [tmpArray sortUsingComparator:^NSComparisonResult(id rhs, id lhs) {
- NSString *rhsString = [rhs fileName];
- NSString *lhsString = [lhs fileName];
- NSComparisonResult result = [rhsString localizedStandardCompare:lhsString];
- return result;
- }];
- }
-
- self.svgArray = [[NSArray alloc] initWithArray:tmpArray];
- }
- - (void)tableViewSelectionDidChange:(NSNotification *)notification
- {
- NSTableView *tmpView = [notification object];
- NSInteger selRow = [tmpView selectedRow];
- if (selRow > -1 && selRow < [self.svgArray count]) {
- NSObject<DemoSVGObject> *tmpObj = (self.svgArray)[selRow];
- SVGKImage *theImage;
- SVGKImageView *theImageView;
- NSWindow *imageWindow;
- if (tmpView == self.fastTable) {
- imageWindow = self.quickWindow;
- theImageView = self.fastView;
- } else if (tmpView == self.layeredTable) {
- imageWindow = self.layeredWindow;
- theImageView = self.layeredView;
- } else {
- NSLog(@"This shouldn't happen...");
- return;
- }
- if ([tmpObj.fullFileName hasPrefix:@"Reinel_compass_rose"]) {
- RoseReturnFunc *theFunc = [RoseReturnFunc new];
- theFunc.theView = theImageView;
- theFunc.imagePath = tmpObj;
- NSBeginAlertSheet(@"Complex SVG", @"No", @"Yes", nil, imageWindow, self,
- @selector(sheetDidEnd:returnCode:contextInfo:), NULL, (void*)CFBridgingRetain(theFunc),
- @"The image \"%@\" has rendering issues on SVGKit. If you want to load the image, "
- @"it will probably crash the app or, more likely, cause the view to become unresponsive."
- @"\n\nAre you sure you want to load the image?", tmpObj.fileName);
- return;
- }
-
- if ([tmpObj isKindOfClass:[DemoSVGBundleObject class]]) {
-
- theImage = [SVGKImage imageNamed:tmpObj.fullFileName inBundle:((DemoSVGBundleObject*)tmpObj).theBundle];
- } else {
- theImage = [SVGKImage imageWithContentsOfURL:[tmpObj svgURL]];
- }
-
- if (![theImage hasSize]) {
- theImage.size = NSMakeSize(32, 32);
- }
-
- theImageView.frameSize = theImage.size;
- theImageView.image = theImage;
- }else
- NSBeep();
- }
- static inline NSString *exceptionInfo(NSException *e)
- {
- NSString *debugStr;
- #if 0
- debugStr = [NSString stringWithFormat:@", call stack: { %@ }", [NSDictionary dictionaryWithObjects:e.callStackReturnAddresses forKeys:e.callStackSymbols]];
- #else
- debugStr = [NSString stringWithFormat:@", call stack symbols: {%@}",e.callStackSymbols];
- #endif
-
- return [NSString stringWithFormat:@"Exception name: \"%@\" reason: %@%@", e.name, e.reason, DEBUG ? debugStr : @""];
- }
- - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
- {
- CFTypeRef CFCtx = contextInfo;
- RoseReturnFunc *theFunc = CFBridgingRelease(CFCtx);
-
- if (returnCode != NSAlertDefaultReturn) {
- SVGKImage *tmpImage;
- @try {
- tmpImage = [SVGKImage imageWithContentsOfURL:theFunc.imagePath.svgURL];
- }
- @catch (NSException *e) {
- NSLog(@"EXCEPTION while loading %@: %@", theFunc.imagePath.fileName, exceptionInfo(e));
- return;
- }
- if (![tmpImage hasSize]) {
- tmpImage.size = NSMakeSize(32, 32);
- }
-
- @try {
- theFunc.theView.image = tmpImage;
- theFunc.theView.frameSize = tmpImage.size;
- }
- @catch (NSException *e) {
- theFunc.theView.image = nil;
- theFunc.theView.frameSize = NSMakeSize(100, 100);
- NSLog(@"EXCEPTION while setting image %@ %@: %@", tmpImage, theFunc.imagePath.fileName, exceptionInfo(e));
- }
- }
- }
- - (IBAction)showLayeredWindow:(id)sender
- {
- if (![self.layeredWindow isVisible]) {
- [self.layeredWindow orderFront:nil];
- }
- }
- - (IBAction)showFastWindow:(id)sender
- {
- if (![self.quickWindow isVisible]) {
- [self.quickWindow orderFront:nil];
- }
- }
- - (IBAction)clearSVGCache:(id)sender
- {
- }
- @synthesize cacheEnabled = _cacheEnabled;
- - (void)setCacheEnabled:(BOOL)cacheEnabled
- {
- _cacheEnabled = cacheEnabled;
- }
- @end
|