Plugins/Displays/iCal/GrowliCalWindowController.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 //  GrowliCalWindowController.m
     3 //  Growl
     4 //
     5 //  Created by Nelson Elhage on Wed Jun 09 2004.
     6 //  Name changed from KABubbleWindowController.m by Justin Burns on Fri Nov 05 2004.
     7 //	Adapted for iCal by Takumi Murayama on Thu Aug 17 2006.
     8 //  Copyright (c) 2004-2006 The Growl Project. All rights reserved.
     9 //
    10 
    11 #import "GrowliCalWindowController.h"
    12 #import "GrowliCalWindowView.h"
    13 #import "GrowliCalPrefsController.h"
    14 #import "GrowliCalDefines.h"
    15 #import "GrowlApplicationNotification.h"
    16 #import "NSWindow+Transforms.h"
    17 #include "CFDictionaryAdditions.h"
    18 #import "GrowlWindowTransition.h"
    19 #import "GrowlFadingWindowTransition.h"
    20 
    21 @implementation GrowliCalWindowController
    22 
    23 #define GrowliCalPadding				5.0f
    24 
    25 #pragma mark -
    26 
    27 - (id) init {
    28 	screenNumber = 0U;
    29 	READ_GROWL_PREF_INT(GrowliCalScreen, GrowliCalPrefDomain, &screenNumber);
    30 	NSArray *screens = [NSScreen screens];
    31 	unsigned screensCount = [screens count];
    32 	if (screensCount) {
    33 		[self setScreen:((screensCount >= (screenNumber + 1)) ? [screens objectAtIndex:screenNumber] : [screens objectAtIndex:0])];
    34 	}
    35 
    36 	CFNumberRef prefsDuration = NULL;
    37 	READ_GROWL_PREF_VALUE(GrowliCalDuration, GrowliCalPrefDomain, CFNumberRef, &prefsDuration);	
    38 	[self setDisplayDuration:(prefsDuration ?
    39 							  [(NSNumber *)prefsDuration doubleValue] :
    40 							  GrowliCalDurationPrefDefault)];
    41 	if (prefsDuration) CFRelease(prefsDuration);
    42 
    43 	// I tried setting the width/height to zero, since the view resizes itself later.
    44 	// This made it ignore the alpha at the edges (using 1.0 instead). Why?
    45 	// A window with a frame of NSZeroRect is off-screen and doesn't respect opacity even
    46 	// if moved on screen later. -Evan
    47 	NSPanel *panel = [[NSPanel alloc] initWithContentRect:NSMakeRect(0.0f, 0.0f, 270.0f, 65.0f)
    48 												styleMask:NSBorderlessWindowMask | NSNonactivatingPanelMask
    49 												  backing:NSBackingStoreBuffered
    50 													defer:YES];
    51 	NSRect panelFrame = [panel frame];
    52 	[panel setBecomesKeyOnlyIfNeeded:YES];
    53 	[panel setHidesOnDeactivate:NO];
    54 	[panel setBackgroundColor:[NSColor clearColor]];
    55 	[panel setLevel:NSStatusWindowLevel];
    56 	[panel setSticky:YES];
    57 	[panel setAlphaValue:0.0f];
    58 	[panel setOpaque:NO];
    59 	[panel setHasShadow:YES];
    60 	[panel setCanHide:NO];
    61 	[panel setOneShot:YES];
    62 	[panel useOptimizedDrawing:YES];
    63 	[panel setMovableByWindowBackground:NO];
    64 
    65 	// Create the content view...
    66 	GrowliCalWindowView *view = [[GrowliCalWindowView alloc] initWithFrame:panelFrame];
    67 	[view setTarget:self];
    68 	[view setAction:@selector(notificationClicked:)];
    69 	[panel setContentView:view];
    70 	[view release];
    71 
    72 	// call super so everything else is set up...
    73 	if ((self = [super initWithWindow:panel])) {
    74 		// set up the transitions...
    75 		GrowlFadingWindowTransition *fader = [[GrowlFadingWindowTransition alloc] initWithWindow:panel];
    76 		[self addTransition:fader];
    77 		[self setStartPercentage:0 endPercentage:100 forTransition:fader];
    78 		[fader setAutoReverses:YES];
    79 		[fader release];
    80 	}
    81 	
    82 	[panel release];
    83 
    84 	return self;
    85 }
    86 
    87 #pragma mark -
    88 
    89 - (void) setNotification: (GrowlApplicationNotification *) theNotification {
    90 	[super setNotification:theNotification];
    91 	if (!theNotification)
    92 		return;
    93 
    94 	NSDictionary *noteDict = [notification dictionaryRepresentation];
    95 	NSString *title = [notification title];
    96 	NSString *text  = [notification notificationDescription];
    97 	NSImage *icon   = getObjectForKey(noteDict, GROWL_NOTIFICATION_ICON);
    98 	int priority    = getIntegerForKey(noteDict, GROWL_NOTIFICATION_PRIORITY);
    99 	
   100 	GrowliCalWindowView *view = [[self window] contentView];
   101 	[view setPriority:priority];
   102 	[view setTitle:title];
   103 	[view setText:text];
   104 	[view setIcon:icon];
   105 	[view sizeToFit];
   106 }
   107 
   108 #pragma mark -
   109 #pragma mark positioning methods
   110 
   111 - (NSPoint) idealOriginInRect:(NSRect)rect {
   112 	NSRect viewFrame = [[[self window] contentView] frame];
   113 	enum GrowlPosition originatingPosition = [[GrowlPositionController sharedInstance] originPosition];
   114 	NSPoint idealOrigin;
   115 	
   116 	switch(originatingPosition){
   117 		case GrowlTopRightPosition:
   118 			idealOrigin = NSMakePoint(NSMaxX(rect) - NSWidth(viewFrame) - GrowliCalPadding,
   119 									  NSMaxY(rect) - GrowliCalPadding - NSHeight(viewFrame));
   120 			break;
   121 		case GrowlTopLeftPosition:
   122 			idealOrigin = NSMakePoint(NSMinX(rect) + GrowliCalPadding,
   123 									  NSMaxY(rect) - GrowliCalPadding - NSHeight(viewFrame));
   124 			break;
   125 		case GrowlBottomLeftPosition:
   126 			idealOrigin = NSMakePoint(NSMinX(rect) + GrowliCalPadding,
   127 									  NSMinY(rect) + GrowliCalPadding);
   128 			break;
   129 		case GrowlBottomRightPosition:
   130 			idealOrigin = NSMakePoint(NSMaxX(rect) - NSWidth(viewFrame) - GrowliCalPadding,
   131 									  NSMinY(rect) + GrowliCalPadding);
   132 			break;
   133 		default:
   134 			idealOrigin = NSMakePoint(NSMaxX(rect) - NSWidth(viewFrame) - GrowliCalPadding,
   135 									  NSMaxY(rect) - GrowliCalPadding - NSHeight(viewFrame));
   136 			break;			
   137 	}
   138 	
   139 	return idealOrigin;	
   140 }
   141 
   142 - (enum GrowlExpansionDirection) primaryExpansionDirection {
   143 	enum GrowlPosition originatingPosition = [[GrowlPositionController sharedInstance] originPosition];
   144 	enum GrowlExpansionDirection directionToExpand;
   145 	
   146 	switch(originatingPosition){
   147 		case GrowlTopLeftPosition:
   148 			directionToExpand = GrowlDownExpansionDirection;
   149 			break;
   150 		case GrowlTopRightPosition:
   151 			directionToExpand = GrowlDownExpansionDirection;
   152 			break;
   153 		case GrowlBottomLeftPosition:
   154 			directionToExpand = GrowlUpExpansionDirection;
   155 			break;
   156 		case GrowlBottomRightPosition:
   157 			directionToExpand = GrowlUpExpansionDirection;
   158 			break;
   159 		default:
   160 			directionToExpand = GrowlDownExpansionDirection;
   161 			break;			
   162 	}
   163 	
   164 	return directionToExpand;
   165 }
   166 
   167 - (enum GrowlExpansionDirection) secondaryExpansionDirection {
   168 	enum GrowlPosition originatingPosition = [[GrowlPositionController sharedInstance] originPosition];
   169 	enum GrowlExpansionDirection directionToExpand;
   170 	
   171 	switch(originatingPosition){
   172 		case GrowlTopLeftPosition:
   173 			directionToExpand = GrowlRightExpansionDirection;
   174 			break;
   175 		case GrowlTopRightPosition:
   176 			directionToExpand = GrowlLeftExpansionDirection;
   177 			break;
   178 		case GrowlBottomLeftPosition:
   179 			directionToExpand = GrowlRightExpansionDirection;
   180 			break;
   181 		case GrowlBottomRightPosition:
   182 			directionToExpand = GrowlLeftExpansionDirection;
   183 			break;
   184 		default:
   185 			directionToExpand = GrowlRightExpansionDirection;
   186 			break;
   187 	}
   188 	
   189 	return directionToExpand;
   190 }
   191 
   192 - (float) requiredDistanceFromExistingDisplays {
   193 	return GrowliCalPadding;
   194 }
   195 
   196 @end