Ticket #45: SapphireFrontRowCompat.m

File SapphireFrontRowCompat.m, 13.9 KB (added by wazza, 3 years ago)
Line 
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
33NSData *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- (NSSize)renderedSizeWithMaxSize:(NSSize)maxSize; /*technically wrong; it is really a CGSize*/
60@end
61
62@interface NSException (compat)
63- (NSArray *)callStackReturnAddresses;
64@end
65
66@interface BRAlertController (compat)
67+ (BRAlertController *)alertOfType:(int)type titled:(NSString *)title primaryText:(NSString *)primaryText secondaryText:(NSString *)secondaryText;
68@end
69
70@interface BROptionDialog (compat)
71- (void)setPrimaryInfoText:(NSString *)text withAttributes:(NSDictionary *)attributes;
72@end
73
74@interface BRTextWithSpinnerController (compat)
75- (BRTextWithSpinnerController *)initWithTitle:(NSString *)title text:(NSString *)text isNetworkDependent:(BOOL)networkDependent;
76@end
77
78@interface BRControl (compat)
79- (void)insertControl:(id)control atIndex:(long)index;
80@end
81
82@interface BRWaitSpinnerControl (compat)
83- (void)setSpins:(BOOL)spin;
84@end
85
86@interface BRTextEntryControl (compat)
87- (id)initWithTextEntryStyle:(int)style;
88@end
89
90
91@implementation SapphireFrontRowCompat
92
93static BOOL usingFrontRow = NO;
94static BOOL usingTakeTwo = NO;
95static BOOL usingTakeTwoDotTwo = NO;
96static BOOL usingTakeTwoDotThree = NO;
97
98+ (void)initialize
99{
100        if(NSClassFromString(@"BRAdornedMenuItemLayer") == nil)
101                usingFrontRow = YES;
102       
103        if(NSClassFromString(@"BRBaseAppliance") != nil)
104                usingTakeTwo = YES;
105       
106        if(NSClassFromString(@"BRVideoPlayerController") == nil)
107                usingTakeTwoDotTwo = YES;
108       
109        if([(Class)NSClassFromString(@"BRController") instancesRespondToSelector:@selector(wasExhumed)])
110                usingTakeTwoDotThree = YES;
111}
112
113+ (BOOL)usingFrontRow
114{
115        return usingFrontRow;
116}
117
118+ (BOOL)usingTakeTwo
119{
120        return usingTakeTwo;
121}
122
123+ (BOOL)usingTakeTwoDotTwo
124{
125        return usingTakeTwoDotTwo;
126}
127
128+ (BOOL)usingTakeTwoDotThree
129{
130        return usingTakeTwoDotThree;
131}
132
133+ (id)imageAtPath:(NSString *)path
134{
135  if(usingFrontRow) {
136    Class cls = NSClassFromString(@"BRImage");
137    return [cls imageWithPath:path];
138  } else {
139    // this returns a CGImageRef
140    NSURL             *url      = [NSURL fileURLWithPath:path];
141    CGImageRef        imageRef  = NULL;
142    CGImageSourceRef  sourceRef;
143   
144    sourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
145    if(sourceRef) {
146      imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, NULL);
147      CFRelease(sourceRef);
148    }
149   
150    return [(id)imageRef autorelease];
151        }
152}
153
154+ (id)imageAtPath:(NSString *)path scene:(BRRenderScene *)scene
155{
156  if(usingFrontRow) {
157    return [self imageAtPath:path];
158  } else {
159    CGImageRef imageRef  = (CGImageRef)[self imageAtPath:path];
160    BRTexture  *ret      = nil;
161   
162    if(imageRef != NULL) {
163      /*Create a texture*/
164      ret = [BRBitmapTexture textureWithImage:imageRef context:[scene resourceContext] mipmap:YES];
165    }
166   
167    return ret;
168  }
169}
170
171+ (id)coverartAsImage: (CGImageRef)imageRef
172{
173        // Non-FR - return CGImageRef
174        if (!usingFrontRow) {
175                NSLog(@"sending direct CGImageRef");
176                return (id)imageRef;
177        }
178
179        // FR - return BRImage
180        NSLog( @"sending BRImage instance" );
181        Class cls = NSClassFromString(@"BRImage");
182        return (id)[cls imageWithCGImageRef:imageRef];
183}
184
185+ (BRAdornedMenuItemLayer *)textMenuItemForScene:(BRRenderScene *)scene folder:(BOOL)folder
186{
187        if(usingFrontRow)
188        {
189                if(folder)
190                        return [NSClassFromString(@"BRTextMenuItemLayer") folderMenuItem];
191                else
192                        return [NSClassFromString(@"BRTextMenuItemLayer") menuItem];           
193        }
194        else
195        {
196                if(folder)
197                        return [NSClassFromString(@"BRAdornedMenuItemLayer") adornedFolderMenuItemWithScene:scene];
198                else
199                        return [NSClassFromString(@"BRAdornedMenuItemLayer") adornedMenuItemWithScene:scene];           
200        }
201}
202
203+ (void)setTitle:(NSString *)title forMenu:(BRAdornedMenuItemLayer *)menu
204{
205        if(usingFrontRow)
206                [menu setTitle:title];
207        else
208                [[menu textItem] setTitle:title];
209}
210
211+ (void)setTitle:(NSString *)title withAttributes:(NSDictionary *)attributes forMenu:(BRAdornedMenuItemLayer *)menu
212{
213        if(usingFrontRow)
214                [menu setTitle:title withAttributes:attributes];
215        else
216                [[menu textItem] setTitle:title withAttributes:attributes];
217}
218
219+ (NSString *)titleForMenu:(BRAdornedMenuItemLayer *)menu {
220  if(usingFrontRow)
221    return [menu title];
222  else
223    return [[menu textItem] title];
224}
225+ (void)setRightJustifiedText:(NSString *)text forMenu:(BRAdornedMenuItemLayer *)menu
226{
227        if(usingFrontRow)
228                [menu setRightJustifiedText:text];
229        else
230                [[menu textItem] setRightJustifiedText:text];
231}
232
233+ (void)setLeftIcon:(BRTexture *)icon forMenu:(BRAdornedMenuItemLayer *)menu
234{
235        if(usingFrontRow)
236                [menu setLeftIconInfo:[NSDictionary dictionaryWithObjectsAndKeys:
237                                                           icon, @"BRMenuIconImageKey",
238                                                           nil]];
239        else
240                [menu setLeftIcon:icon];
241}
242
243+ (void)setRightIcon:(BRTexture *)icon forMenu:(BRAdornedMenuItemLayer *)menu
244{
245        if(usingFrontRow)
246                 [menu setRightIconInfo:[NSDictionary dictionaryWithObjectsAndKeys:
247                                                                 icon, @"BRMenuIconImageKey",
248                                                                 nil]];
249        else
250                [menu setRightIcon:icon];
251}
252
253+ (id)selectedSettingImageForScene:(BRRenderScene *)scene
254{
255        if(usingFrontRow)
256                return [[BRThemeInfo sharedTheme] selectedSettingImage];
257        else
258                return [[BRThemeInfo sharedTheme] selectedSettingImageForScene:scene];
259}
260
261+ (id)unplayedPodcastImageForScene:(BRRenderScene *)scene
262{
263        if(usingFrontRow)
264                return [[BRThemeInfo sharedTheme] unplayedPodcastImage];
265        else
266                return [[BRThemeInfo sharedTheme] unplayedPodcastImageForScene:scene];
267}
268
269+ (id)returnToImageForScene:(BRRenderScene *)scene {
270  if(usingFrontRow)
271    return [[BRThemeInfo sharedTheme] returnToImage];
272  else
273    return [[BRThemeInfo sharedTheme] returnToImageForScene:scene];
274}
275
276+ (NSRect)frameOfController:(id)controller
277{
278        if(usingTakeTwo)
279                // ATV2
280                return [controller frame];
281        else if(usingFrontRow)
282                // 10.5
283                return [controller controllerFrame];
284        else
285                return [[controller masterLayer] frame];
286}
287
288+ (void)setText:(NSString *)text withAtrributes:(NSDictionary *)attributes forControl:(BRTextControl *)control
289{
290        if(usingFrontRow)
291                [control setText:text withAttributes:attributes];
292        else
293        {
294                if(attributes != nil)
295                        [control setTextAttributes:attributes];
296                [control setText:text];
297        }
298}
299
300+ (NSSize)textControl:(BRTextControl *)text renderedSizeWithMaxSize:(NSSize)maxSize
301{
302        if(usingTakeTwo)
303                return [text renderedSizeWithMaxSize:maxSize];
304       
305        [text setMaximumSize:maxSize];
306        return [text renderedSize];
307}
308
309+ (void)addDividerAtIndex:(int)index toList:(BRListControl *)list
310{
311        if(usingFrontRow)
312                [list addDividerAtIndex:index withLabel:@""];
313        else
314                [list addDividerAtIndex:index];
315}
316
317+ (void)addSublayer:(id)sub toControl:(id)controller
318{
319        if(usingFrontRow) {
320    // ATV2
321    if(NSClassFromString(@"BRPanel") == nil)
322      [controller addControl:sub];
323    // 10.5
324    else
325      [[controller layer] addSublayer:sub];
326  }
327        else
328                [[controller masterLayer] addSublayer:sub];
329}
330
331+ (void)insertSublayer:(id)sub toControl:(id)controller atIndex:(long)index {
332  if(usingFrontRow) {
333    // ATV2
334    if(NSClassFromString(@"BRPanel") == nil)
335      [controller insertControl:sub atIndex:index];
336    // 10.5
337    else
338      [[controller layer] insertSublayer:sub atIndex:index];
339  } else
340    [[controller masterLayer] insertSublayer:sub atIndex:index];
341}
342
343+ (BRHeaderControl *)newHeaderControlWithScene:(BRRenderScene *)scene
344{
345        if(usingFrontRow)
346                return [[BRHeaderControl alloc] init];
347        else
348                return [[BRHeaderControl alloc] initWithScene:scene];
349}
350
351+ (BRButtonControl *)newButtonControlWithScene:(BRRenderScene *)scene  masterLayerSize:(NSSize)size;
352{
353        if(usingFrontRow)
354                return [[SapphireButtonControl alloc] initWithMasterLayerSize:size];
355        else
356                return [[BRButtonControl alloc] initWithScene:scene masterLayerSize:size];
357}
358
359+ (BRTextControl *)newTextControlWithScene:(BRRenderScene *)scene
360{
361        if(usingFrontRow)
362                return [[BRTextControl alloc] init];
363        else
364                return [[BRTextControl alloc] initWithScene:scene];
365}
366
367+ (BRTextEntryControl *)newTextEntryControlWithScene:(BRRenderScene *)scene
368{
369        if(usingFrontRow)
370        {
371                if(usingTakeTwoDotTwo)
372                        return [[BRTextEntryControl alloc] initWithTextEntryStyle:1];
373                return [[BRTextEntryControl alloc] initWithTextEntryStyle:0];
374        }
375        else
376                return [[BRTextEntryControl alloc] initWithScene:scene];
377}
378
379+ (BRProgressBarWidget *)newProgressBarWidgetWithScene:(BRRenderScene *)scene
380{
381        if(usingFrontRow)
382                return [[BRProgressBarWidget alloc] init];
383        else
384                return [[BRProgressBarWidget alloc] initWithScene:scene];
385}
386
387+ (BRMarchingIconLayer *)newMarchingIconLayerWithScene:(BRRenderScene *)scene
388{
389        if(usingTakeTwo)
390                return nil;
391        if(usingFrontRow)
392                return [[BRMarchingIconLayer alloc] init];
393        else
394                return [[BRMarchingIconLayer alloc] initWithScene:scene];
395}
396
397+ (BRImageLayer *)newImageLayerWithScene:(BRRenderScene *)scene {
398  // 10.5
399  if(usingFrontRow && NSClassFromString(@"BRImageLayer") != nil)
400    return [[BRImageLayer alloc] init];
401  // ATV2
402  else if(usingFrontRow)
403    return [[NSClassFromString(@"BRImageControl") alloc] init];
404  else
405    return [BRImageLayer layerWithScene:scene];
406}
407
408+ (void)setImage:(id)image forLayer:(BRImageLayer *)layer {
409  if(usingFrontRow)
410    // this cast is not proper, it just makes a warning disappear.
411    [layer setImage:(CGImageRef)image];
412  else
413    [layer setTexture:image];
414}
415
416+ (BRImageLayer *)newImageLayerWithImage:(id)image scene:(BRRenderScene *)scene {
417  BRImageLayer *result = [self newImageLayerWithScene:scene];
418  [self setImage:image forLayer:result];
419  return result;
420}
421
422+ (void)renderScene:(BRRenderScene *)scene
423{
424        if(!usingFrontRow)
425                [scene renderScene];
426}
427
428+ (BRAlertController *)alertOfType:(int)type titled:(NSString *)title primaryText:(NSString *)primaryText secondaryText:(NSString *)secondaryText withScene:(BRRenderScene *)scene {
429  if(usingFrontRow)
430    return [BRAlertController alertOfType:type
431                                   titled:title
432                              primaryText:primaryText
433                            secondaryText:secondaryText];
434  else
435    return [BRAlertController alertOfType:type
436                                   titled:title
437                              primaryText:primaryText
438                            secondaryText:secondaryText
439                                    withScene:scene];
440}
441
442+ (BROptionDialog *)newOptionDialogWithScene:(BRRenderScene *)scene {
443  if(usingFrontRow)
444    return [[BROptionDialog alloc] init];
445  else
446    return [[BROptionDialog alloc] initWithScene:scene];
447}
448
449+ (void)setOptionDialogPrimaryInfoText:(NSString *)primaryInfoText withAttributes:(NSDictionary *)attributes optionDialog:(BROptionDialog *)dialog {
450  if(usingFrontRow) {
451    [dialog setPrimaryInfoText:primaryInfoText withAttributes:attributes];
452  } else {
453    [dialog setPrimaryInfoText:primaryInfoText];
454    [dialog setPrimaryInfoTextAttributes:attributes];
455  }
456}
457
458+ (BRTextWithSpinnerController *)newTextWithSpinnerControllerTitled:(NSString *)title text:(NSString *)text isNetworkDependent:(BOOL)networkDependent scene:(BRRenderScene *)scene {
459  if(usingFrontRow)
460    return [[BRTextWithSpinnerController alloc] initWithTitle:title text:text isNetworkDependent:networkDependent];
461  else
462    return [[BRTextWithSpinnerController alloc] initWithScene:scene title:title text:text showBack:NO isNetworkDependent:NO];
463}
464
465+ (void)setSpinner:(BRWaitSpinnerControl *)spinner toSpin:(BOOL)spin
466{
467        if([spinner respondsToSelector:@selector(setSpins:)])
468                [spinner setSpins:spin];
469        else if([spinner respondsToSelector:@selector(startSpinning)])
470        {
471                if(spin)
472                        [spinner startSpinning];
473                else
474                        [spinner stopSpinning];
475        }
476}
477
478+ (NSArray *)callStackReturnAddressesForException:(NSException *)exception
479{
480        if([exception respondsToSelector:@selector(callStackReturnAddresses)])
481        {
482                NSArray *ret = [exception callStackReturnAddresses];
483                if([ret count])
484                        return ret;
485        }
486        return [[exception userInfo] objectForKey:NSStackTraceKey];
487}
488
489+ (RUIPreferences *)sharedFrontRowPreferences {
490  Class preferencesClass = NSClassFromString(@"RUIPreferences");
491  if(!preferencesClass) preferencesClass = NSClassFromString(@"BRPreferences");
492 
493  if(preferencesClass)
494    return [preferencesClass sharedFrontRowPreferences];
495  else
496    return nil;
497}
498
499@end