2 // GrowlMusicVideoWindowView.m
5 // Created by Jorge Salvador Caffarena on 09/09/04.
6 // Copyright 2004 Jorge Salvador Caffarena. All rights reserved.
9 #import "GrowlMusicVideoWindowView.h"
10 #import "GrowlMusicVideoPrefs.h"
11 #import "GrowlImageAdditions.h"
13 extern CGLayerRef CGLayerCreateWithContext() __attribute__((weak_import));
15 @implementation GrowlMusicVideoWindowView
17 - (id) initWithFrame:(NSRect)frame {
18 if ((self = [super initWithFrame:frame])) {
19 cache = [[NSImage alloc] initWithSize:frame.size];
27 [titleAttributes release];
28 [textAttributes release];
29 [backgroundColor release];
36 CGLayerRelease(layer);
41 - (void) drawRect:(NSRect)rect {
42 //Make sure that we don't draw in the main thread
43 //if ([super dispatchDrawingToThread:rect]) {
44 NSGraphicsContext *context = [NSGraphicsContext currentContext];
45 CGContextRef cgContext = [context graphicsPort];
46 NSRect bounds = [self bounds];
50 READ_GROWL_PREF_INT(MUSICVIDEO_SIZE_PREF, GrowlMusicVideoPrefDomain, &sizePref);
51 NSRect titleRect, textRect;
54 if (sizePref == MUSICVIDEO_SIZE_HUGE) {
55 titleRect.origin.x = 192.0;
56 titleRect.origin.y = NSHeight(bounds) - 72.0;
57 titleRect.size.width = NSWidth(bounds) - 192.0 - 32.0;
58 titleRect.size.height = 40.0;
59 textRect.origin.y = NSHeight(bounds) - 176.0;
60 textRect.size.height = 96.0;
61 iconRect.origin.x = 32.0;
62 iconRect.origin.y = NSHeight(bounds) - 160.0;
63 iconRect.size.width = 128.0;
64 iconRect.size.height = 128.0;
66 titleRect.origin.x = 96.0;
67 titleRect.origin.y = NSHeight(bounds) - 36.0;
68 titleRect.size.width = NSWidth(bounds) - 96.0 - 16.0;
69 titleRect.size.height = 25.0;
70 textRect.origin.y = NSHeight(bounds) - 88.0,
71 textRect.size.height = 48.0;
72 iconRect.origin.x = 8.0;
73 iconRect.origin.y = NSHeight(bounds) - 88.0;
74 iconRect.size.width = 80.0;
75 iconRect.size.height = 80.0;
77 textRect.origin.x = titleRect.origin.x;
78 textRect.size.width = titleRect.size.width;
81 if (CGLayerCreateWithContext) {
83 layer = CGLayerCreateWithContext(cgContext, CGSizeMake(bounds.size.width, bounds.size.height), NULL);
84 [NSGraphicsContext setCurrentContext:
85 [NSGraphicsContext graphicsContextWithGraphicsPort:CGLayerGetContext(layer) flipped:NO]];
90 [backgroundColor set];
91 bounds.origin = NSZeroPoint;
94 [title drawInRect:titleRect withAttributes:titleAttributes];
96 [text drawInRect:textRect withAttributes:textAttributes];
99 [icon drawScaledInRect:iconRect operation:NSCompositeSourceOver fraction:1.0];
101 if (CGLayerCreateWithContext)
102 [NSGraphicsContext setCurrentContext:context];
110 [[NSColor clearColor] set];
113 // draw cache to screen
114 NSRect imageRect = rect;
115 int effect = MUSICVIDEO_EFFECT_SLIDE;
116 READ_GROWL_PREF_INT(MUSICVIDEO_EFFECT_PREF, GrowlMusicVideoPrefDomain, &effect);
117 if (effect == MUSICVIDEO_EFFECT_SLIDE) {
118 if (CGLayerCreateWithContext)
119 imageRect.origin.y = 0.0;
120 } else if (effect == MUSICVIDEO_EFFECT_WIPE) {
121 rect.size.height -= imageRect.origin.y;
122 imageRect.size.height -= imageRect.origin.y;
123 if (!CGLayerCreateWithContext)
124 imageRect.origin.y = 0.0;
125 } else if (effect == MUSICVIDEO_EFFECT_FADING) {
126 if (CGLayerCreateWithContext)
127 imageRect.origin.y = 0.0;
130 if (CGLayerCreateWithContext) {
132 cgRect.origin.x = imageRect.origin.x;
133 cgRect.origin.y = imageRect.origin.y;
134 cgRect.size.width = rect.size.width;
135 if (effect == MUSICVIDEO_EFFECT_WIPE) {
136 cgRect.size.height = rect.size.height;
137 CGContextClipToRect(cgContext, cgRect);
139 cgRect.size.height = bounds.size.height;
140 CGContextDrawLayerInRect(cgContext, cgRect, layer);
142 [cache drawInRect:rect fromRect:imageRect operation:NSCompositeSourceOver fraction:1.0];
147 - (void) setIcon:(NSImage *)anIcon {
149 icon = [anIcon retain];
150 [self setNeedsDisplay:(needsDisplay = YES)];
153 - (void) setTitle:(NSString *)aTitle {
155 title = [aTitle copy];
156 [self setNeedsDisplay:(needsDisplay = YES)];
159 - (void) setText:(NSString *)aText {
162 [self setNeedsDisplay:(needsDisplay = YES)];
165 - (void) setPriority:(int)priority {
170 key = GrowlMusicVideoVeryLowBackgroundColor;
171 textKey = GrowlMusicVideoVeryLowTextColor;
174 key = GrowlMusicVideoModerateBackgroundColor;
175 textKey = GrowlMusicVideoModerateTextColor;
178 key = GrowlMusicVideoHighBackgroundColor;
179 textKey = GrowlMusicVideoHighTextColor;
182 key = GrowlMusicVideoEmergencyBackgroundColor;
183 textKey = GrowlMusicVideoEmergencyTextColor;
187 key = GrowlMusicVideoNormalBackgroundColor;
188 textKey = GrowlMusicVideoNormalTextColor;
192 [backgroundColor release];
194 CGFloat opacityPref = MUSICVIDEO_DEFAULT_OPACITY;
195 READ_GROWL_PREF_FLOAT(MUSICVIDEO_OPACITY_PREF, GrowlMusicVideoPrefDomain, &opacityPref);
196 CGFloat alpha = opacityPref * 0.01;
198 Class NSDataClass = [NSData class];
201 READ_GROWL_PREF_VALUE(key, GrowlMusicVideoPrefDomain, NSData *, &data);
203 CFMakeCollectable(data);
204 if (data && [data isKindOfClass:NSDataClass])
205 backgroundColor = [NSUnarchiver unarchiveObjectWithData:data];
207 backgroundColor = [NSColor blackColor];
208 backgroundColor = [[backgroundColor colorWithAlphaComponent:alpha] retain];
213 READ_GROWL_PREF_VALUE(textKey, GrowlMusicVideoPrefDomain, NSData *, &data);
215 CFMakeCollectable(data);
216 if (data && [data isKindOfClass:NSDataClass])
217 textColor = [NSUnarchiver unarchiveObjectWithData:data];
219 textColor = [NSColor whiteColor];
223 CGFloat titleFontSize;
224 CGFloat textFontSize;
226 READ_GROWL_PREF_INT(MUSICVIDEO_SIZE_PREF, GrowlMusicVideoPrefDomain, &sizePref);
228 if (sizePref == MUSICVIDEO_SIZE_HUGE) {
229 titleFontSize = 32.0;
232 titleFontSize = 16.0;
236 NSShadow *textShadow = [[NSShadow alloc] init];
238 NSSize shadowSize = {0.0, -2.0};
239 [textShadow setShadowOffset:shadowSize];
240 [textShadow setShadowBlurRadius:3.0];
241 [textShadow setShadowColor:[NSColor blackColor]];
243 NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
244 [paragraphStyle setAlignment:NSLeftTextAlignment];
245 [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
246 [titleAttributes release];
247 titleAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
248 textColor, NSForegroundColorAttributeName,
249 paragraphStyle, NSParagraphStyleAttributeName,
250 [NSFont boldSystemFontOfSize:titleFontSize], NSFontAttributeName,
251 textShadow, NSShadowAttributeName,
253 [paragraphStyle release];
255 paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
256 [paragraphStyle setAlignment:NSLeftTextAlignment];
257 [textAttributes release];
258 textAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
259 textColor, NSForegroundColorAttributeName,
260 paragraphStyle, NSParagraphStyleAttributeName,
261 [NSFont messageFontOfSize:textFontSize], NSFontAttributeName,
262 textShadow, NSShadowAttributeName,
264 [paragraphStyle release];
265 [textShadow release];
272 - (void) setTarget:(id) object {
282 - (void) setAction:(SEL) selector {
287 - (BOOL) showsCloseBox {
292 - (BOOL) needsDisplay {
293 return needsDisplay && [super needsDisplay];