Plugins/Displays/MusicVideo/GrowlMusicVideoWindowController.m
author boredzo
Sun Jul 06 15:10:15 2008 +0000 (2008-07-06)
changeset 4135 9d0747a53f45
parent 3958 157225f6bfee
child 4246 4f52d1d98978
child 4771 d398be175a6e
permissions -rw-r--r--
CFRelease the duration preference value if we get one. Found by the clang static analyzer.
     1 //
     2 //  GrowlMusicVideoWindowController.m
     3 //  Display Plugins
     4 //
     5 //  Created by Jorge Salvador Caffarena on 09/09/04.
     6 //  Copyright 2004 Jorge Salvador Caffarena. All rights reserved.
     7 //
     8 
     9 #import "GrowlMusicVideoWindowController.h"
    10 #import "GrowlFadingWindowTransition.h"
    11 #import "GrowlMusicVideoWindowView.h"
    12 #import "GrowlMusicVideoPrefs.h"
    13 #import "NSWindow+Transforms.h"
    14 #import "GrowlSlidingWindowTransition.h"
    15 #import "GrowlWipeWindowTransition.h"
    16 #include "CFDictionaryAdditions.h"
    17 #import "GrowlApplicationNotification.h"
    18 
    19 @implementation GrowlMusicVideoWindowController
    20 
    21 - (id) init {
    22 	int sizePref = MUSICVIDEO_SIZE_NORMAL;
    23 
    24 	screenNumber = 0U;
    25 	READ_GROWL_PREF_INT(MUSICVIDEO_SCREEN_PREF, GrowlMusicVideoPrefDomain, &screenNumber);
    26 	NSArray *screens = [NSScreen screens];
    27 	unsigned screensCount = [screens count];
    28 	if (screensCount) {
    29 		[self setScreen:((screensCount >= (screenNumber + 1)) ? [screens objectAtIndex:screenNumber] : [screens objectAtIndex:0])];
    30 	}
    31 	
    32 	NSRect sizeRect;
    33 	NSRect screen = [[self screen] frame];
    34 	READ_GROWL_PREF_INT(MUSICVIDEO_SIZE_PREF, GrowlMusicVideoPrefDomain, &sizePref);
    35 	sizeRect.origin = screen.origin;
    36 	sizeRect.size.width = screen.size.width;
    37 	if (sizePref == MUSICVIDEO_SIZE_HUGE)
    38 		sizeRect.size.height = 192.0f;
    39 	else
    40 		sizeRect.size.height = 96.0f;
    41 	frameHeight = sizeRect.size.height;
    42 
    43 	READ_GROWL_PREF_INT(MUSICVIDEO_SIZE_PREF, GrowlMusicVideoPrefDomain, &sizePref);
    44 	NSPanel *panel = [[NSPanel alloc] initWithContentRect:sizeRect
    45 												styleMask:NSBorderlessWindowMask
    46 												  backing:NSBackingStoreBuffered
    47 													defer:YES];
    48 	NSRect panelFrame = [panel frame];
    49 	[panel setBecomesKeyOnlyIfNeeded:YES];
    50 	[panel setHidesOnDeactivate:NO];
    51 	[panel setBackgroundColor:[NSColor clearColor]];
    52 	[panel setLevel:NSStatusWindowLevel];
    53 	[panel setIgnoresMouseEvents:YES];
    54 	[panel setSticky:YES];
    55 	[panel setOpaque:NO];
    56 	[panel setHasShadow:NO];
    57 	[panel setCanHide:NO];
    58 	[panel setOneShot:YES];
    59 	[panel useOptimizedDrawing:YES];
    60 	[panel setDelegate:self];
    61 
    62 	GrowlMusicVideoWindowView *view = [[GrowlMusicVideoWindowView alloc] initWithFrame:panelFrame];
    63 
    64 	[view setTarget:self];
    65 	[view setAction:@selector(notificationClicked:)]; // Not used for now
    66 
    67 	[panel setContentView:view]; // retains subview
    68 	[view release];
    69 
    70 	[panel setFrameTopLeftPoint:screen.origin];
    71 
    72 	// call super so everything else is set up...
    73 	if ((self = [super initWithWindow:panel])) {
    74 		CFNumberRef prefsDuration = NULL;
    75 		READ_GROWL_PREF_VALUE(MUSICVIDEO_DURATION_PREF, GrowlMusicVideoPrefDomain, CFNumberRef, &prefsDuration);
    76 		[self setDisplayDuration:(prefsDuration ?
    77 								  [(NSNumber *)prefsDuration doubleValue] :
    78 								  GrowlMusicVideoDurationPrefDefault)];
    79 		if (prefsDuration) CFRelease(prefsDuration);
    80 		
    81 		//The default duration for transitions is far too long for the music video effect.
    82 		[self setTransitionDuration:0.3];
    83 
    84 		MusicVideoEffectType effect = MUSICVIDEO_EFFECT_SLIDE;
    85 		READ_GROWL_PREF_INT(MUSICVIDEO_EFFECT_PREF, GrowlMusicVideoPrefDomain, &effect);
    86 		switch (effect)
    87 		{
    88 			case MUSICVIDEO_EFFECT_SLIDE:
    89 			{
    90 				//slider effect
    91 				GrowlSlidingWindowTransition *slider = [[GrowlSlidingWindowTransition alloc] initWithWindow:panel];
    92 				[slider setFromOrigin:NSMakePoint(NSMinX(screen),NSMinY(screen)-frameHeight) toOrigin:NSMakePoint(NSMinX(screen),NSMinY(screen))];
    93 				[self setStartPercentage:0 endPercentage:100 forTransition:slider];
    94 				[slider setAutoReverses:YES];
    95 				[self addTransition:slider];
    96 				[slider release];
    97 				break;
    98 			}
    99 			case MUSICVIDEO_EFFECT_FADING:
   100 			{
   101 				GrowlFadingWindowTransition *fader = [[GrowlFadingWindowTransition alloc] initWithWindow:panel];
   102 				[self addTransition:fader];
   103 				[self setStartPercentage:0 endPercentage:100 forTransition:fader];
   104 				[fader setAutoReverses:YES];
   105 				[fader release];
   106 				
   107 				// I am adding in a sliding transition from screen,screen to screen,screen to make sure the window is properly positioned during the animation - swr
   108 				GrowlSlidingWindowTransition *slider = [[GrowlSlidingWindowTransition alloc] initWithWindow:panel];
   109 				[slider setFromOrigin:NSMakePoint(NSMinX(screen),NSMinY(screen)) toOrigin:NSMakePoint(NSMinX(screen),NSMinY(screen))];
   110 				[self setStartPercentage:0 endPercentage:100 forTransition:slider];
   111 				[slider setAutoReverses:YES];
   112 				[self addTransition:slider];
   113 				[slider release];
   114 				break;
   115 			}
   116 			case MUSICVIDEO_EFFECT_WIPE:
   117 			{
   118 				NSLog(@"Wipe not implemented");
   119 				//wipe effect
   120 				//[panel setFrameOrigin:NSMakePoint( 0, 0)];
   121 				//GrowlWipeWindowTransition *wiper = [[GrowlWipeWindowTransition alloc] initWithWindow:panel];
   122 				// save for scale effect [wiper setFromOrigin:NSMakePoint(0,0) toOrigin:NSMakePoint(NSMaxX(screen), frameHeight)];
   123 				//[wiper setFromOrigin:NSMakePoint(NSMaxX(screen), 0) toOrigin:NSMakePoint(NSMaxX(screen), frameHeight)];
   124 				//[self setStartPercentage:0 endPercentage:100 forTransition:wiper];
   125 				//[wiper setAutoReverses:YES];
   126 				//[self addTransition:wiper];
   127 				//[wiper release];
   128 				break;
   129 			}
   130 		}
   131 	}
   132 	
   133 	[panel release];
   134 	
   135 	return self;
   136 
   137 }
   138 
   139 - (void) setNotification: (GrowlApplicationNotification *) theNotification {
   140 	[super setNotification:theNotification];
   141 	if (!theNotification)
   142 		return;
   143 
   144 	NSDictionary *noteDict = [notification dictionaryRepresentation];
   145 	NSString *title = [notification title];
   146 	NSString *text  = [notification notificationDescription];
   147 	NSImage *icon   = getObjectForKey(noteDict, GROWL_NOTIFICATION_ICON);
   148 	int prio        = getIntegerForKey(noteDict, GROWL_NOTIFICATION_PRIORITY);
   149 
   150 	NSPanel *panel = (NSPanel *)[self window];
   151 	GrowlMusicVideoWindowView *view = [panel contentView];
   152 	[view setPriority:prio];
   153 	[view setTitle:title];
   154 	[view setText:text];
   155 	[view setIcon:icon];
   156 }
   157 
   158 @end