| 1 | /* |
|---|
| 2 | * SapphireFrontRowCompat.m |
|---|
| 3 | * Sapphire |
|---|
| 4 | * |
|---|
| 5 | * Created by Graham Booker on Oct. 29, 2007. |
|---|
| 6 | * Copyright 2007 Sapphire Development Team and/or www.nanopi.net |
|---|
| 7 | * All rights reserved. |
|---|
| 8 | * |
|---|
| 9 | * This program is free software; you can redistribute it and/or modify it under the terms of the GNU |
|---|
| 10 | * General Public License as published by the Free Software Foundation; either version 3 of the License, |
|---|
| 11 | * or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
|---|
| 14 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|---|
| 15 | * Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License along with this program; if not, |
|---|
| 18 | * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #import "SapphireFrontRowCompat.h" |
|---|
| 22 | #import <ExceptionHandling/NSExceptionHandler.h> |
|---|
| 23 | #import "SapphireButtonControl.h" |
|---|
| 24 | |
|---|
| 25 | /*Yes, this is the wrong class, but otherwise gcc gripes about BRImage class not existing; this removes warnings so no harm*/ |
|---|
| 26 | @interface SapphireFrontRowCompat (compat) |
|---|
| 27 | + (id)imageWithPath:(NSString *)path; |
|---|
| 28 | - (id)downsampledImageForMaxSize:(NSSize)size; |
|---|
| 29 | + (id)imageWithCGImageRef:(CGImageRef)ref; |
|---|
| 30 | - (CGImageRef)image; |
|---|
| 31 | @end |
|---|
| 32 | |
|---|
| 33 | NSData *CreateBitmapDataFromImage(CGImageRef image, unsigned int width, unsigned int height); |
|---|
| 34 | |
|---|
| 35 | /*Yes, wrong class and other wrong things, just to shut up warnings*/ |
|---|
| 36 | @interface BRAdornedMenuItemLayer (compat) |
|---|
| 37 | + (id)folderMenuItem; |
|---|
| 38 | + (id)menuItem; |
|---|
| 39 | - (void)setTitle:(NSString *)title; |
|---|
| 40 | - (void)setTitle:(NSString *)title withAttributes:(NSDictionary *)attributes; |
|---|
| 41 | - (void)setRightJustifiedText:(NSString *)text; |
|---|
| 42 | - (void)setLeftIconInfo:(BRTexture *)icon; |
|---|
| 43 | - (void)setRightIconInfo:(BRTexture *)icon; |
|---|
| 44 | @end |
|---|
| 45 | |
|---|
| 46 | @interface BRThemeInfo (compat) |
|---|
| 47 | - (id)selectedSettingImage; |
|---|
| 48 | - (id)unplayedPodcastImage; |
|---|
| 49 | - (id)returnToImage; |
|---|
| 50 | @end |
|---|
| 51 | |
|---|
| 52 | @interface BRButtonControl (compat) |
|---|
| 53 | - (id)initWithMasterLayerSize:(NSSize)fp8; |
|---|
| 54 | @end |
|---|
| 55 | |
|---|
| 56 | @interface BRTextControl (compat) |
|---|
| 57 | - (void)setText:(NSString *)text withAttributes:(NSDictionary *)attr; |
|---|
| 58 | - (NSRect)controllerFrame; /*technically wrong; it is really a CGRect*/ |
|---|
| 59 | @end |
|---|
| 60 | |
|---|
| 61 | @interface NSException (compat) |
|---|
| 62 | - (NSArray *)callStackReturnAddresses; |
|---|
| 63 | @end |
|---|
| 64 | |
|---|
| 65 | @interface BRAlertController (compat) |
|---|
| 66 | + (BRAlertController *)alertOfType:(int)type titled:(NSString *)title primaryText:(NSString *)primaryText secondaryText:(NSString *)secondaryText; |
|---|
| 67 | @end |
|---|
| 68 | |
|---|
| 69 | @interface BROptionDialog (compat) |
|---|
| 70 | - (void)setPrimaryInfoText:(NSString *)text withAttributes:(NSDictionary *)attributes; |
|---|
| 71 | @end |
|---|
| 72 | |
|---|
| 73 | @interface BRTextWithSpinnerController (compat) |
|---|
| 74 | - (BRTextWithSpinnerController *)initWithTitle:(NSString *)title text:(NSString *)text isNetworkDependent:(BOOL)networkDependent; |
|---|
| 75 | @end |
|---|
| 76 | |
|---|
| 77 | @implementation SapphireFrontRowCompat |
|---|
| 78 | |
|---|
| 79 | static BOOL usingFrontRow = NO; |
|---|
| 80 | |
|---|
| 81 | + (void)initialize |
|---|
| 82 | { |
|---|
| 83 | if(NSClassFromString(@"BRAdornedMenuItemLayer") == nil) |
|---|
| 84 | usingFrontRow = YES; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | + (BOOL)usingFrontRow |
|---|
| 88 | { |
|---|
| 89 | return usingFrontRow; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | + (id)imageAtPath:(NSString *)path |
|---|
| 93 | { |
|---|
| 94 | Class cls = NSClassFromString(@"BRImage"); |
|---|
| 95 | return [cls imageWithPath:path]; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | + (id)imageAtPath:(NSString *)path scene:(BRRenderScene *)scene |
|---|
| 99 | { |
|---|
| 100 | if(usingFrontRow) { |
|---|
| 101 | return [self imageAtPath:path]; |
|---|
| 102 | } else { |
|---|
| 103 | NSURL *url = [NSURL fileURLWithPath:path]; |
|---|
| 104 | BRTexture *ret = nil; |
|---|
| 105 | CGImageRef imageRef = NULL; |
|---|
| 106 | CGImageSourceRef sourceRef; |
|---|
| 107 | |
|---|
| 108 | sourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL); |
|---|
| 109 | if(sourceRef) { |
|---|
| 110 | imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, NULL); |
|---|
| 111 | CFRelease(sourceRef); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | if(imageRef != NULL) { |
|---|
| 115 | /*Create a texture*/ |
|---|
| 116 | ret = [BRBitmapTexture textureWithImage:imageRef context:[scene resourceContext] mipmap:YES]; |
|---|
| 117 | CFRelease(imageRef); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | return ret; |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | + (BRAdornedMenuItemLayer *)textMenuItemForScene:(BRRenderScene *)scene folder:(BOOL)folder |
|---|
| 125 | { |
|---|
| 126 | if(usingFrontRow) |
|---|
| 127 | { |
|---|
| 128 | if(folder) |
|---|
| 129 | return [NSClassFromString(@"BRTextMenuItemLayer") folderMenuItem]; |
|---|
| 130 | else |
|---|
| 131 | return [NSClassFromString(@"BRTextMenuItemLayer") menuItem]; |
|---|
| 132 | } |
|---|
| 133 | else |
|---|
| 134 | { |
|---|
| 135 | if(folder) |
|---|
| 136 | return [NSClassFromString(@"BRAdornedMenuItemLayer") adornedFolderMenuItemWithScene:scene]; |
|---|
| 137 | else |
|---|
| 138 | return [NSClassFromString(@"BRAdornedMenuItemLayer") adornedMenuItemWithScene:scene]; |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | + (void)setTitle:(NSString *)title forMenu:(BRAdornedMenuItemLayer *)menu |
|---|
| 143 | { |
|---|
| 144 | if(usingFrontRow) |
|---|
| 145 | [menu setTitle:title]; |
|---|
| 146 | else |
|---|
| 147 | [[menu textItem] setTitle:title]; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | + (void)setTitle:(NSString *)title withAttributes:(NSDictionary *)attributes forMenu:(BRAdornedMenuItemLayer *)menu |
|---|
| 151 | { |
|---|
| 152 | if(usingFrontRow) |
|---|
| 153 | [menu setTitle:title withAttributes:attributes]; |
|---|
| 154 | else |
|---|
| 155 | [[menu textItem] setTitle:title withAttributes:attributes]; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | + (NSString *)titleForMenu:(BRAdornedMenuItemLayer *)menu { |
|---|
| 159 | if(usingFrontRow) |
|---|
| 160 | return [menu title]; |
|---|
| 161 | else |
|---|
| 162 | return [[menu textItem] title]; |
|---|
| 163 | } |
|---|
| 164 | + (void)setRightJustifiedText:(NSString *)text forMenu:(BRAdornedMenuItemLayer *)menu |
|---|
| 165 | { |
|---|
| 166 | if(usingFrontRow) |
|---|
| 167 | [menu setRightJustifiedText:text]; |
|---|
| 168 | else |
|---|
| 169 | [[menu textItem] setRightJustifiedText:text]; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | + (void)setLeftIcon:(BRTexture *)icon forMenu:(BRAdornedMenuItemLayer *)menu |
|---|
| 173 | { |
|---|
| 174 | if(usingFrontRow) |
|---|
| 175 | [menu setLeftIconInfo:[NSDictionary dictionaryWithObjectsAndKeys: |
|---|
| 176 | icon, @"BRMenuIconImageKey", |
|---|
| 177 | nil]]; |
|---|
| 178 | else |
|---|
| 179 | [menu setLeftIcon:icon]; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | + (void)setRightIcon:(BRTexture *)icon forMenu:(BRAdornedMenuItemLayer *)menu |
|---|
| 183 | { |
|---|
| 184 | if(usingFrontRow) |
|---|
| 185 | [menu setRightIconInfo:[NSDictionary dictionaryWithObjectsAndKeys: |
|---|
| 186 | icon, @"BRMenuIconImageKey", |
|---|
| 187 | nil]]; |
|---|
| 188 | else |
|---|
| 189 | [menu setRightIcon:icon]; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | + (id)selectedSettingImageForScene:(BRRenderScene *)scene |
|---|
| 193 | { |
|---|
| 194 | if(usingFrontRow) |
|---|
| 195 | return [[BRThemeInfo sharedTheme] selectedSettingImage]; |
|---|
| 196 | else |
|---|
| 197 | return [[BRThemeInfo sharedTheme] selectedSettingImageForScene:scene]; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | + (id)unplayedPodcastImageForScene:(BRRenderScene *)scene |
|---|
| 201 | { |
|---|
| 202 | if(usingFrontRow) |
|---|
| 203 | return [[BRThemeInfo sharedTheme] unplayedPodcastImage]; |
|---|
| 204 | else |
|---|
| 205 | return [[BRThemeInfo sharedTheme] unplayedPodcastImageForScene:scene]; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | + (id)returnToImageForScene:(BRRenderScene *)scene { |
|---|
| 209 | if(usingFrontRow) |
|---|
| 210 | return [[BRThemeInfo sharedTheme] returnToImage]; |
|---|
| 211 | else |
|---|
| 212 | return [[BRThemeInfo sharedTheme] returnToImageForScene:scene]; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | + (NSRect)frameOfController:(id)controller |
|---|
| 216 | { |
|---|
| 217 | if(usingFrontRow) |
|---|
| 218 | { |
|---|
| 219 | return [controller controllerFrame]; |
|---|
| 220 | } |
|---|
| 221 | else |
|---|
| 222 | return [[controller masterLayer] frame]; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | + (void)setText:(NSString *)text withAtrributes:(NSDictionary *)attributes forControl:(BRTextControl *)control |
|---|
| 226 | { |
|---|
| 227 | if(usingFrontRow) |
|---|
| 228 | [control setText:text withAttributes:attributes]; |
|---|
| 229 | else |
|---|
| 230 | { |
|---|
| 231 | if(attributes != nil) |
|---|
| 232 | [control setTextAttributes:attributes]; |
|---|
| 233 | [control setText:text]; |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | + (void)addDividerAtIndex:(int)index toList:(BRListControl *)list |
|---|
| 238 | { |
|---|
| 239 | if(usingFrontRow) |
|---|
| 240 | [list addDividerAtIndex:index withLabel:@""]; |
|---|
| 241 | else |
|---|
| 242 | [list addDividerAtIndex:index]; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | + (void)addSublayer:(id)sub toControl:(id)controller |
|---|
| 246 | { |
|---|
| 247 | if(usingFrontRow) |
|---|
| 248 | [[controller layer] addSublayer:sub]; |
|---|
| 249 | else |
|---|
| 250 | [[controller masterLayer] addSublayer:sub]; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | + (void)insertSublayer:(id)sub toControl:(id)controller atIndex:(long)index { |
|---|
| 254 | if(usingFrontRow) |
|---|
| 255 | [[controller layer] insertSublayer:sub atIndex:index]; |
|---|
| 256 | else |
|---|
| 257 | [[controller masterLayer] insertSublayer:sub atIndex:index]; |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | + (BRHeaderControl *)newHeaderControlWithScene:(BRRenderScene *)scene |
|---|
| 261 | { |
|---|
| 262 | if(usingFrontRow) |
|---|
| 263 | return [[BRHeaderControl alloc] init]; |
|---|
| 264 | else |
|---|
| 265 | return [[BRHeaderControl alloc] initWithScene:scene]; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | + (BRButtonControl *)newButtonControlWithScene:(BRRenderScene *)scene masterLayerSize:(NSSize)size; |
|---|
| 269 | { |
|---|
| 270 | if(usingFrontRow) |
|---|
| 271 | return [[SapphireButtonControl alloc] initWithMasterLayerSize:size]; |
|---|
| 272 | else |
|---|
| 273 | return [[BRButtonControl alloc] initWithScene:scene masterLayerSize:size]; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | + (BRTextControl *)newTextControlWithScene:(BRRenderScene *)scene |
|---|
| 277 | { |
|---|
| 278 | if(usingFrontRow) |
|---|
| 279 | return [[BRTextControl alloc] init]; |
|---|
| 280 | else |
|---|
| 281 | return [[BRTextControl alloc] initWithScene:scene]; |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | + (BRProgressBarWidget *)newProgressBarWidgetWithScene:(BRRenderScene *)scene |
|---|
| 285 | { |
|---|
| 286 | if(usingFrontRow) |
|---|
| 287 | return [[BRProgressBarWidget alloc] init]; |
|---|
| 288 | else |
|---|
| 289 | return [[BRProgressBarWidget alloc] initWithScene:scene]; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | + (BRMarchingIconLayer *)newMarchingIconLayerWithScene:(BRRenderScene *)scene |
|---|
| 293 | { |
|---|
| 294 | if(usingFrontRow) |
|---|
| 295 | return [[BRMarchingIconLayer alloc] init]; |
|---|
| 296 | else |
|---|
| 297 | return [[BRMarchingIconLayer alloc] initWithScene:scene]; |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | + (BRImageLayer *)newImageLayerWithScene:(BRRenderScene *)scene { |
|---|
| 301 | if(usingFrontRow) |
|---|
| 302 | return [[BRImageLayer alloc] init]; |
|---|
| 303 | else |
|---|
| 304 | return [BRImageLayer layerWithScene:scene]; |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | + (void)setImage:(id)image forLayer:(BRImageLayer *)layer { |
|---|
| 308 | if(usingFrontRow) |
|---|
| 309 | // this cast is not proper, it just makes a warning disappear. |
|---|
| 310 | [layer setImage:(CGImageRef)image]; |
|---|
| 311 | else |
|---|
| 312 | [layer setTexture:image]; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | + (BRImageLayer *)newImageLayerWithImage:(id)image scene:(BRRenderScene *)scene { |
|---|
| 316 | BRImageLayer *result = [self newImageLayerWithScene:scene]; |
|---|
| 317 | [self setImage:image forLayer:result]; |
|---|
| 318 | return result; |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | + (void)renderScene:(BRRenderScene *)scene |
|---|
| 322 | { |
|---|
| 323 | if(!usingFrontRow) |
|---|
| 324 | [scene renderScene]; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | + (BRAlertController *)alertOfType:(int)type titled:(NSString *)title primaryText:(NSString *)primaryText secondaryText:(NSString *)secondaryText withScene:(BRRenderScene *)scene { |
|---|
| 328 | if(usingFrontRow) |
|---|
| 329 | return [BRAlertController alertOfType:type |
|---|
| 330 | titled:title |
|---|
| 331 | primaryText:primaryText |
|---|
| 332 | secondaryText:secondaryText]; |
|---|
| 333 | else |
|---|
| 334 | return [BRAlertController alertOfType:type |
|---|
| 335 | titled:title |
|---|
| 336 | primaryText:primaryText |
|---|
| 337 | secondaryText:secondaryText |
|---|
| 338 | withScene:scene]; |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | + (BROptionDialog *)optionDialogWithScene:(BRRenderScene *)scene { |
|---|
| 342 | if(usingFrontRow) |
|---|
| 343 | return [[BROptionDialog alloc] init]; |
|---|
| 344 | else |
|---|
| 345 | return [[BROptionDialog alloc] initWithScene:scene]; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | + (void)setOptionDialogPrimaryInfoText:(NSString *)primaryInfoText withAttributes:(NSDictionary *)attributes optionDialog:(BROptionDialog *)dialog { |
|---|
| 349 | if(usingFrontRow) { |
|---|
| 350 | [dialog setPrimaryInfoText:primaryInfoText withAttributes:attributes]; |
|---|
| 351 | } else { |
|---|
| 352 | [dialog setPrimaryInfoText:primaryInfoText]; |
|---|
| 353 | [dialog setPrimaryInfoTextAttributes:attributes]; |
|---|
| 354 | } |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | + (BRTextWithSpinnerController *)textWithSpinnerControllerTitled:(NSString *)title text:(NSString *)text isNetworkDependent:(BOOL)networkDependent scene:(BRRenderScene *)scene { |
|---|
| 358 | if(usingFrontRow) |
|---|
| 359 | return [[BRTextWithSpinnerController alloc] initWithTitle:title text:text isNetworkDependent:networkDependent]; |
|---|
| 360 | else |
|---|
| 361 | return [[BRTextWithSpinnerController alloc] initWithScene:scene title:title text:text showBack:NO isNetworkDependent:NO]; |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | + (NSArray *)callStackReturnAddressesForException:(NSException *)exception |
|---|
| 365 | { |
|---|
| 366 | if([exception respondsToSelector:@selector(callStackReturnAddresses)]) |
|---|
| 367 | { |
|---|
| 368 | NSArray *ret = [exception callStackReturnAddresses]; |
|---|
| 369 | if([ret count]) |
|---|
| 370 | return ret; |
|---|
| 371 | } |
|---|
| 372 | return [[exception userInfo] objectForKey:NSStackTraceKey]; |
|---|
| 373 | } |
|---|
| 374 | @end |
|---|