Plugins/Displays/Nano/GrowlNanoWindowController.m
author boredzo
Sun Jul 06 15:10:15 2008 +0000 (2008-07-06)
changeset 4135 9d0747a53f45
parent 3954 2dfcd1414225
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 //  GrowlNanoWindowController.m
     3 //  Display Plugins
     4 //
     5 //  Created by Rudy Richter on 12/12/2005.
     6 //  Copyright 2005-2006, The Growl Project. All rights reserved.
     7 //
     8 
     9 
    10 #import "GrowlNanoWindowController.h"
    11 #import "GrowlNanoWindowView.h"
    12 #import "GrowlNanoPrefs.h"
    13 #import "NSWindow+Transforms.h"
    14 #import "GrowlSlidingWindowTransition.h"
    15 #import "GrowlWipeWindowTransition.h"
    16 #import "GrowlFadingWindowTransition.h"
    17 
    18 #import "GrowlApplicationNotification.h"
    19 #include "CFDictionaryAdditions.h"
    20 
    21 @implementation GrowlNanoWindowController
    22 
    23 - (id) init {
    24 	int sizePref = Nano_SIZE_NORMAL;
    25 
    26 	//define our duration
    27 	CFNumberRef prefsDuration = NULL;
    28 	READ_GROWL_PREF_VALUE(Nano_DURATION_PREF, GrowlNanoPrefDomain, CFNumberRef, &prefsDuration);
    29 	[self setDisplayDuration:(prefsDuration ?
    30 							  [(NSNumber *)prefsDuration doubleValue] :
    31 							  GrowlNanoDurationPrefDefault)];
    32 	if (prefsDuration) CFRelease(prefsDuration);
    33 
    34 	screenNumber = 0U;
    35 	READ_GROWL_PREF_INT(Nano_SCREEN_PREF, GrowlNanoPrefDomain, &screenNumber);
    36 	NSArray *screens = [NSScreen screens];
    37 	unsigned int screensCount = [screens count];
    38 	if (screensCount) {
    39 		[self setScreen:((screensCount >= (screenNumber + 1)) ? [screens objectAtIndex:screenNumber] : [screens objectAtIndex:0])];
    40 	}
    41 				 
    42 	NSRect sizeRect;
    43 	NSRect screen = [[self screen] frame];
    44 	READ_GROWL_PREF_INT(Nano_SIZE_PREF, GrowlNanoPrefDomain, &sizePref);
    45 	sizeRect.origin = screen.origin;
    46 	if (sizePref == Nano_SIZE_HUGE) {
    47 		sizeRect.size.height = 50.0f;	
    48 		sizeRect.size.width = 270.0f;
    49 	} else {
    50 		sizeRect.size.height = 25.0f;
    51 		sizeRect.size.width = 185.0f;
    52 	}
    53 	frameHeight = sizeRect.size.height;
    54 
    55 	READ_GROWL_PREF_INT(Nano_SIZE_PREF, GrowlNanoPrefDomain, &sizePref);
    56 	NSPanel *panel = [[NSPanel alloc] initWithContentRect:sizeRect
    57 												styleMask:NSBorderlessWindowMask
    58 												  backing:NSBackingStoreBuffered
    59 													defer:YES];
    60 	NSRect panelFrame = [panel frame];
    61 	[panel setBecomesKeyOnlyIfNeeded:YES];
    62 	[panel setHidesOnDeactivate:NO];
    63 	[panel setBackgroundColor:[NSColor clearColor]];
    64 	[panel setLevel:NSFloatingWindowLevel];
    65 	[panel setIgnoresMouseEvents:YES];
    66 	[panel setSticky:YES];
    67 	[panel setOpaque:NO];
    68 	[panel setHasShadow:NO];
    69 	[panel setCanHide:NO];
    70 	[panel setOneShot:YES];
    71 	[panel useOptimizedDrawing:YES];
    72 	[panel setDelegate:self];
    73 
    74 	GrowlNanoWindowView *view = [[GrowlNanoWindowView alloc] initWithFrame:panelFrame];
    75 
    76 	[view setTarget:self];
    77 	[view setAction:@selector(notificationClicked:)]; // Not used for now
    78 
    79 	[panel setContentView:view]; // retains subview
    80 	[view release];
    81 	
    82 	float xPosition = NSMaxX(screen) - (NSMaxX(screen) / 4);
    83 	float yPosition = NSMaxY(screen);
    84 	if([NSMenu menuBarVisible])
    85 		yPosition-=[NSMenuView menuBarHeight];
    86 	
    87 	[panel setFrameOrigin:NSMakePoint(xPosition, yPosition)];
    88 
    89 	// call super so everything else is set up...
    90 	if ((self = [super initWithWindow:panel])) {
    91 		NanoEffectType effect = Nano_EFFECT_SLIDE;
    92 		READ_GROWL_PREF_INT(Nano_EFFECT_PREF, GrowlNanoPrefDomain, &effect);
    93 
    94 		switch (effect) {
    95 			case Nano_EFFECT_SLIDE:
    96 			{
    97 				//slider effect
    98 				GrowlSlidingWindowTransition *slider = [[GrowlSlidingWindowTransition alloc] initWithWindow:panel];
    99 				[slider setFromOrigin:NSMakePoint(xPosition, yPosition) toOrigin:NSMakePoint(xPosition, yPosition - frameHeight)];
   100 				[slider setAutoReverses:YES];
   101 				[self addTransition:slider];
   102 				[self setStartPercentage:0 endPercentage:100 forTransition:slider];
   103 
   104 				[slider release];
   105 				
   106 				break;
   107 			}
   108 			case Nano_EFFECT_WIPE:
   109 			{
   110 				//wipe effect
   111 				[panel setFrameOrigin:NSMakePoint(xPosition, NSMaxY(screen))];
   112 				GrowlWipeWindowTransition *wiper = [[GrowlWipeWindowTransition alloc] initWithWindow:panel];
   113 				[wiper setFromOrigin:NSMakePoint(xPosition, yPosition) toOrigin:NSMakePoint(xPosition, yPosition - frameHeight)];
   114 				[wiper setAutoReverses:YES];
   115 				[self addTransition:wiper];
   116 				[self setStartPercentage:0 endPercentage:100 forTransition:wiper];
   117 
   118 				[wiper release];
   119 				
   120 				NSLog(@"Wipe not implemented");
   121 				break;
   122 			}
   123 			case Nano_EFFECT_FADE:
   124 			{
   125 				[panel setAlphaValue:0.0];
   126 				[panel setFrameOrigin:NSMakePoint(xPosition, yPosition - frameHeight)];
   127 
   128 				GrowlFadingWindowTransition *fader = [[GrowlFadingWindowTransition alloc] initWithWindow:panel];
   129 				[self addTransition:fader];
   130 				[self setStartPercentage:0 endPercentage:100 forTransition:fader];
   131 				[fader setAutoReverses:YES];
   132 				[fader release];
   133 				break;
   134 			}
   135 		}
   136 	}
   137 	
   138 	[panel release];
   139 
   140 	return self;
   141 }
   142 
   143 - (void) setNotification: (GrowlApplicationNotification *) theNotification {
   144 	[super setNotification:theNotification];
   145 	if (!theNotification)
   146 		return;
   147 
   148 	NSDictionary *noteDict = [notification dictionaryRepresentation];
   149 	NSString *title = [notification title];
   150 	NSString *text  = [notification notificationDescription];
   151 	NSImage *icon   = getObjectForKey(noteDict, GROWL_NOTIFICATION_ICON);
   152 	int prio        = getIntegerForKey(noteDict, GROWL_NOTIFICATION_PRIORITY);
   153 
   154 	NSPanel *panel = (NSPanel *)[self window];
   155 	GrowlNanoWindowView *view = [panel contentView];
   156 	[view setPriority:prio];
   157 	[view setTitle:title];
   158 	[view setText:text];
   159 	[view setIcon:icon];
   160 }
   161 
   162 #pragma mark -
   163 
   164 @end