Plugins/Displays/MusicVideo/GrowlMusicVideoWindowView.m
author Rudy Richter
Sat Aug 01 20:43:39 2009 -0400 (2009-08-01)
changeset 4259 0e9b6b0b1e25
parent 4246 4f52d1d98978
child 4541 9a290d3de636
permissions -rw-r--r--
Plugins: clang warnings
boredzo@2402
     1
//
boredzo@2402
     2
//  GrowlMusicVideoWindowView.m
boredzo@2402
     3
//  Display Plugins
boredzo@2402
     4
//
boredzo@2402
     5
//  Created by Jorge Salvador Caffarena on 09/09/04.
boredzo@2402
     6
//  Copyright 2004 Jorge Salvador Caffarena. All rights reserved.
boredzo@2402
     7
//
boredzo@2402
     8
boredzo@2402
     9
#import "GrowlMusicVideoWindowView.h"
boredzo@2402
    10
#import "GrowlMusicVideoPrefs.h"
boredzo@2402
    11
#import "GrowlImageAdditions.h"
boredzo@2402
    12
rudy@2945
    13
extern CGLayerRef CGLayerCreateWithContext() __attribute__((weak_import));
rudy@2945
    14
boredzo@2402
    15
@implementation GrowlMusicVideoWindowView
boredzo@2402
    16
boredzo@2402
    17
- (id) initWithFrame:(NSRect)frame {
boredzo@2402
    18
	if ((self = [super initWithFrame:frame])) {
boredzo@2402
    19
		cache = [[NSImage alloc] initWithSize:frame.size];
boredzo@2402
    20
		needsDisplay = YES;
boredzo@2402
    21
	}
boredzo@2402
    22
boredzo@2402
    23
	return self;
boredzo@2402
    24
}
boredzo@2402
    25
boredzo@2402
    26
- (void) dealloc {
boredzo@2402
    27
	[titleAttributes release];
boredzo@2402
    28
	[textAttributes  release];
boredzo@2402
    29
	[backgroundColor release];
boredzo@2402
    30
	[textColor       release];
boredzo@2402
    31
	[icon            release];
boredzo@2402
    32
	[title           release];
boredzo@2402
    33
	[text            release];
boredzo@2402
    34
	[cache           release];
ingmarstein@2553
    35
	if (layer)
ingmarstein@2553
    36
		CGLayerRelease(layer);
boredzo@2402
    37
boredzo@2402
    38
	[super dealloc];
boredzo@2402
    39
}
boredzo@2402
    40
boredzo@2402
    41
- (void) drawRect:(NSRect)rect {
ofri@2708
    42
	//Make sure that we don't draw in the main thread
rudy@2814
    43
	//if ([super dispatchDrawingToThread:rect]) {
ofri@2708
    44
		NSGraphicsContext *context = [NSGraphicsContext currentContext];
ofri@2708
    45
		CGContextRef cgContext = [context graphicsPort];
ofri@2708
    46
		NSRect bounds = [self bounds];
ofri@2708
    47
		if (needsDisplay) {
ofri@2708
    48
			// rects and sizes
ofri@2708
    49
			int sizePref = 0;
rudy@2814
    50
			READ_GROWL_PREF_INT(MUSICVIDEO_SIZE_PREF, GrowlMusicVideoPrefDomain, &sizePref);
ofri@2708
    51
			NSRect titleRect, textRect;
ofri@2708
    52
			NSRect iconRect;
ingmarstein@2721
    53
ofri@2708
    54
			if (sizePref == MUSICVIDEO_SIZE_HUGE) {
Rudy@4246
    55
				titleRect.origin.x = 192.0;
Rudy@4246
    56
				titleRect.origin.y = NSHeight(bounds) - 72.0;
Rudy@4246
    57
				titleRect.size.width = NSWidth(bounds) - 192.0 - 32.0;
Rudy@4246
    58
				titleRect.size.height = 40.0;
Rudy@4246
    59
				textRect.origin.y = NSHeight(bounds) - 176.0;
Rudy@4246
    60
				textRect.size.height = 96.0;
Rudy@4246
    61
				iconRect.origin.x = 32.0;
Rudy@4246
    62
				iconRect.origin.y = NSHeight(bounds) - 160.0;
Rudy@4246
    63
				iconRect.size.width = 128.0;
Rudy@4246
    64
				iconRect.size.height = 128.0;
ofri@2708
    65
			} else {
Rudy@4246
    66
				titleRect.origin.x = 96.0;
Rudy@4246
    67
				titleRect.origin.y = NSHeight(bounds) - 36.0;
Rudy@4246
    68
				titleRect.size.width = NSWidth(bounds) - 96.0 - 16.0;
Rudy@4246
    69
				titleRect.size.height = 25.0;
Rudy@4246
    70
				textRect.origin.y = NSHeight(bounds) - 88.0,
Rudy@4246
    71
					textRect.size.height = 48.0;
Rudy@4246
    72
				iconRect.origin.x = 8.0;
Rudy@4246
    73
				iconRect.origin.y = NSHeight(bounds) - 88.0;
Rudy@4246
    74
				iconRect.size.width = 80.0;
Rudy@4246
    75
				iconRect.size.height = 80.0;
ofri@2708
    76
			}
ofri@2708
    77
			textRect.origin.x = titleRect.origin.x;
ofri@2708
    78
			textRect.size.width = titleRect.size.width;
ingmarstein@2721
    79
ofri@2708
    80
			//draw to cache
ofri@2708
    81
			if (CGLayerCreateWithContext) {
ofri@2708
    82
				if (!layer)
ofri@2708
    83
					layer = CGLayerCreateWithContext(cgContext, CGSizeMake(bounds.size.width, bounds.size.height), NULL);
ofri@2708
    84
				[NSGraphicsContext setCurrentContext:
ofri@2708
    85
					[NSGraphicsContext graphicsContextWithGraphicsPort:CGLayerGetContext(layer) flipped:NO]];
ofri@2708
    86
			} else {
ofri@2708
    87
				[cache lockFocus];
ofri@2708
    88
			}
ingmarstein@2721
    89
ofri@2708
    90
			[backgroundColor set];
ofri@2708
    91
			bounds.origin = NSZeroPoint;
ofri@2708
    92
			NSRectFill(bounds);
ingmarstein@2721
    93
ofri@2708
    94
			[title drawInRect:titleRect withAttributes:titleAttributes];
ingmarstein@2721
    95
ofri@2708
    96
			[text drawInRect:textRect withAttributes:textAttributes];
ingmarstein@2721
    97
ofri@2708
    98
			[icon setFlipped:NO];
Rudy@4246
    99
			[icon drawScaledInRect:iconRect operation:NSCompositeSourceOver fraction:1.0];
ingmarstein@2721
   100
ofri@2708
   101
			if (CGLayerCreateWithContext)
ofri@2708
   102
				[NSGraphicsContext setCurrentContext:context];
ofri@2708
   103
			else
ofri@2708
   104
				[cache unlockFocus];
ingmarstein@2721
   105
ofri@2708
   106
			needsDisplay = NO;
ofri@2708
   107
		}
ingmarstein@2721
   108
ofri@2708
   109
		// draw background
ofri@2708
   110
		[[NSColor clearColor] set];
ofri@2708
   111
		NSRectFill(rect);
ingmarstein@2721
   112
ofri@2708
   113
		// draw cache to screen
ofri@2708
   114
		NSRect imageRect = rect;
ofri@2708
   115
		int effect = MUSICVIDEO_EFFECT_SLIDE;
rudy@3282
   116
		READ_GROWL_PREF_INT(MUSICVIDEO_EFFECT_PREF, GrowlMusicVideoPrefDomain, &effect);
ofri@2708
   117
		if (effect == MUSICVIDEO_EFFECT_SLIDE) {
ofri@2708
   118
			if (CGLayerCreateWithContext)
Rudy@4246
   119
				imageRect.origin.y = 0.0;
ofri@2708
   120
		} else if (effect == MUSICVIDEO_EFFECT_WIPE) {
ofri@2708
   121
			rect.size.height -= imageRect.origin.y;
ofri@2708
   122
			imageRect.size.height -= imageRect.origin.y;
ofri@2708
   123
			if (!CGLayerCreateWithContext)
Rudy@4246
   124
				imageRect.origin.y = 0.0;
rudy@3282
   125
		} else if (effect == MUSICVIDEO_EFFECT_FADING) {
rudy@3282
   126
			if (CGLayerCreateWithContext)
Rudy@4246
   127
				imageRect.origin.y = 0.0;		
ofri@2708
   128
		}
ingmarstein@2721
   129
ofri@2708
   130
		if (CGLayerCreateWithContext) {
ofri@2708
   131
			CGRect cgRect;
ofri@2708
   132
			cgRect.origin.x = imageRect.origin.x;
ofri@2708
   133
			cgRect.origin.y = imageRect.origin.y;
ofri@2708
   134
			cgRect.size.width = rect.size.width;
ofri@2708
   135
			if (effect == MUSICVIDEO_EFFECT_WIPE) {
ofri@2708
   136
				cgRect.size.height = rect.size.height;
ofri@2708
   137
				CGContextClipToRect(cgContext, cgRect);
ofri@2708
   138
			}
ofri@2708
   139
			cgRect.size.height = bounds.size.height;
ofri@2708
   140
			CGContextDrawLayerInRect(cgContext, cgRect, layer);
boredzo@2402
   141
		} else {
Rudy@4246
   142
			[cache drawInRect:rect fromRect:imageRect operation:NSCompositeSourceOver fraction:1.0];
boredzo@2402
   143
		}
rudy@2814
   144
	//}
boredzo@2402
   145
}
boredzo@2402
   146
boredzo@2402
   147
- (void) setIcon:(NSImage *)anIcon {
boredzo@2402
   148
	[icon autorelease];
boredzo@2402
   149
	icon = [anIcon retain];
boredzo@2402
   150
	[self setNeedsDisplay:(needsDisplay = YES)];
boredzo@2402
   151
}
boredzo@2402
   152
boredzo@2402
   153
- (void) setTitle:(NSString *)aTitle {
boredzo@2402
   154
	[title autorelease];
boredzo@2402
   155
	title = [aTitle copy];
boredzo@2402
   156
	[self setNeedsDisplay:(needsDisplay = YES)];
boredzo@2402
   157
}
boredzo@2402
   158
boredzo@2402
   159
- (void) setText:(NSString *)aText {
boredzo@2402
   160
	[text autorelease];
boredzo@2402
   161
	text = [aText copy];
boredzo@2402
   162
	[self setNeedsDisplay:(needsDisplay = YES)];
boredzo@2402
   163
}
boredzo@2402
   164
boredzo@2402
   165
- (void) setPriority:(int)priority {
boredzo@2402
   166
	NSString *key;
boredzo@2402
   167
	NSString *textKey;
boredzo@2402
   168
	switch (priority) {
boredzo@2402
   169
		case -2:
boredzo@2402
   170
			key = GrowlMusicVideoVeryLowBackgroundColor;
boredzo@2402
   171
			textKey = GrowlMusicVideoVeryLowTextColor;
boredzo@2402
   172
			break;
boredzo@2402
   173
		case -1:
boredzo@2402
   174
			key = GrowlMusicVideoModerateBackgroundColor;
boredzo@2402
   175
			textKey = GrowlMusicVideoModerateTextColor;
boredzo@2402
   176
			break;
boredzo@2402
   177
		case 1:
boredzo@2402
   178
			key = GrowlMusicVideoHighBackgroundColor;
boredzo@2402
   179
			textKey = GrowlMusicVideoHighTextColor;
boredzo@2402
   180
			break;
boredzo@2402
   181
		case 2:
boredzo@2402
   182
			key = GrowlMusicVideoEmergencyBackgroundColor;
boredzo@2402
   183
			textKey = GrowlMusicVideoEmergencyTextColor;
boredzo@2402
   184
			break;
boredzo@2402
   185
		case 0:
boredzo@2402
   186
		default:
boredzo@2402
   187
			key = GrowlMusicVideoNormalBackgroundColor;
boredzo@2402
   188
			textKey = GrowlMusicVideoNormalTextColor;
boredzo@2402
   189
			break;
boredzo@2402
   190
	}
boredzo@2402
   191
boredzo@2402
   192
	[backgroundColor release];
boredzo@2402
   193
Rudy@4246
   194
	CGFloat opacityPref = MUSICVIDEO_DEFAULT_OPACITY;
rudy@2814
   195
	READ_GROWL_PREF_FLOAT(MUSICVIDEO_OPACITY_PREF, GrowlMusicVideoPrefDomain, &opacityPref);
Rudy@4246
   196
	CGFloat alpha = opacityPref * 0.01;
boredzo@2402
   197
boredzo@2402
   198
	Class NSDataClass = [NSData class];
boredzo@2402
   199
	NSData *data = nil;
boredzo@2402
   200
rudy@2814
   201
	READ_GROWL_PREF_VALUE(key, GrowlMusicVideoPrefDomain, NSData *, &data);
Rudy@4259
   202
	if(data)
Rudy@4259
   203
		CFMakeCollectable(data);
boredzo@2402
   204
	if (data && [data isKindOfClass:NSDataClass])
boredzo@2402
   205
		backgroundColor = [NSUnarchiver unarchiveObjectWithData:data];
boredzo@2402
   206
	else
boredzo@2402
   207
		backgroundColor = [NSColor blackColor];
boredzo@2402
   208
	backgroundColor = [[backgroundColor colorWithAlphaComponent:alpha] retain];
boredzo@2402
   209
	[data release];
boredzo@2402
   210
	data = nil;
boredzo@2402
   211
boredzo@2402
   212
	[textColor release];
rudy@2814
   213
	READ_GROWL_PREF_VALUE(textKey, GrowlMusicVideoPrefDomain, NSData *, &data);
Rudy@4259
   214
	if(data)
Rudy@4259
   215
		CFMakeCollectable(data);
boredzo@2402
   216
	if (data && [data isKindOfClass:NSDataClass])
boredzo@2402
   217
		textColor = [NSUnarchiver unarchiveObjectWithData:data];
boredzo@2402
   218
	else
boredzo@2402
   219
		textColor = [NSColor whiteColor];
boredzo@2402
   220
	[textColor retain];
boredzo@2402
   221
	[data release];
boredzo@2402
   222
Rudy@4246
   223
	CGFloat titleFontSize;
Rudy@4246
   224
	CGFloat textFontSize;
boredzo@2402
   225
	int sizePref = 0;
rudy@2814
   226
	READ_GROWL_PREF_INT(MUSICVIDEO_SIZE_PREF, GrowlMusicVideoPrefDomain, &sizePref);
boredzo@2402
   227
boredzo@2402
   228
	if (sizePref == MUSICVIDEO_SIZE_HUGE) {
Rudy@4246
   229
		titleFontSize = 32.0;
Rudy@4246
   230
		textFontSize = 20.0;
boredzo@2402
   231
	} else {
Rudy@4246
   232
		titleFontSize = 16.0;
Rudy@4246
   233
		textFontSize = 12.0;
boredzo@2402
   234
	}
boredzo@2402
   235
boredzo@2402
   236
	NSShadow *textShadow = [[NSShadow alloc] init];
boredzo@2402
   237
Rudy@4246
   238
	NSSize shadowSize = {0.0, -2.0};
boredzo@2402
   239
	[textShadow setShadowOffset:shadowSize];
Rudy@4246
   240
	[textShadow setShadowBlurRadius:3.0];
boredzo@2402
   241
	[textShadow setShadowColor:[NSColor blackColor]];
boredzo@2402
   242
boredzo@2402
   243
	NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
boredzo@2402
   244
	[paragraphStyle setAlignment:NSLeftTextAlignment];
boredzo@2402
   245
	[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
boredzo@2402
   246
	[titleAttributes release];
boredzo@2402
   247
	titleAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
boredzo@2402
   248
		textColor,                                   NSForegroundColorAttributeName,
boredzo@2402
   249
		paragraphStyle,                              NSParagraphStyleAttributeName,
boredzo@2402
   250
		[NSFont boldSystemFontOfSize:titleFontSize], NSFontAttributeName,
boredzo@2402
   251
		textShadow,                                  NSShadowAttributeName,
boredzo@2402
   252
		nil];
boredzo@2402
   253
	[paragraphStyle release];
boredzo@2402
   254
boredzo@2402
   255
	paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
boredzo@2402
   256
	[paragraphStyle setAlignment:NSLeftTextAlignment];
boredzo@2402
   257
	[textAttributes release];
boredzo@2402
   258
	textAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
boredzo@2402
   259
		textColor,                               NSForegroundColorAttributeName,
boredzo@2402
   260
		paragraphStyle,                          NSParagraphStyleAttributeName,
boredzo@2402
   261
		[NSFont messageFontOfSize:textFontSize], NSFontAttributeName,
boredzo@2402
   262
		textShadow,                              NSShadowAttributeName,
boredzo@2402
   263
		nil];
boredzo@2402
   264
	[paragraphStyle release];
boredzo@2402
   265
	[textShadow release];
boredzo@2402
   266
}
boredzo@2402
   267
boredzo@2402
   268
- (id) target {
boredzo@2402
   269
	return target;
boredzo@2402
   270
}
boredzo@2402
   271
boredzo@2402
   272
- (void) setTarget:(id) object {
boredzo@2402
   273
	target = object;
boredzo@2402
   274
}
boredzo@2402
   275
boredzo@2402
   276
#pragma mark -
boredzo@2402
   277
boredzo@2402
   278
- (SEL) action {
boredzo@2402
   279
	return action;
boredzo@2402
   280
}
boredzo@2402
   281
boredzo@2402
   282
- (void) setAction:(SEL) selector {
boredzo@2402
   283
	action = selector;
boredzo@2402
   284
}
boredzo@2402
   285
vinay@3127
   286
vinay@3127
   287
- (BOOL) showsCloseBox {
vinay@3127
   288
    return NO;
vinay@3127
   289
}
boredzo@2402
   290
#pragma mark -
boredzo@2402
   291
boredzo@2402
   292
- (BOOL) needsDisplay {
boredzo@2402
   293
	return needsDisplay && [super needsDisplay];
boredzo@2402
   294
}
boredzo@2402
   295
boredzo@2402
   296
@end