2 // GrowlITTSWController.m
5 // Created by Joseph Spiros on 2/28/09.
6 // Copyright 2009 iThink Software. All rights reserved.
9 #import "GrowlITTSWController.h"
10 #import "GrowlITTSWPrefs.h"
12 #import <ITKit/ITWindowEffect.h>
13 #import <ITKit/ITTSWBackgroundView.h>
15 #import "RegexKitLite.h"
17 @implementation NSImage (SmoothAdditions)
19 - (NSImage *)imageScaledSmoothlyToSize:(NSSize)scaledSize {
21 NSImageRep *rep = [self bestRepresentationForDevice:nil];
23 newImage = [[NSImage alloc] initWithSize:scaledSize];
26 [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
27 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
28 [rep drawInRect:NSMakeRect(3, 3, scaledSize.width - 6, scaledSize.height - 6)];
30 [newImage unlockFocus];
31 return [newImage autorelease];
36 @interface GrowlITTSWController (Private)
37 - (void)syncWithPrefs;
40 @implementation GrowlITTSWController
43 if ( ( self = [super init] ) ) {
44 _window = [[GrowlITTSWWindow sharedWindow] retain];
45 [_window setExitMode:ITTransientStatusWindowExitAfterDelay];
47 [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(syncWithPrefs) name:@"GrowlPreferencesChanged" object:nil];
58 - (void)syncWithPrefs {
59 NSScreen *screen = [GrowlITTSWPrefs screen];
60 ITHorizontalWindowPosition horizontalPosition = [GrowlITTSWPrefs horizontalPosition];
61 ITVerticalWindowPosition verticalPosition = [GrowlITTSWPrefs verticalPosition];
63 Class appearanceEffect = [GrowlITTSWPrefs appearanceEffect];
64 float appearanceSpeed = [GrowlITTSWPrefs appearanceSpeed];
65 Class vanishEffect = [GrowlITTSWPrefs vanishEffect];
66 float vanishSpeed = [GrowlITTSWPrefs vanishSpeed];
67 float vanishDelay = [GrowlITTSWPrefs vanishDelay];
69 ITTSWBackgroundMode backgroundStyle = [GrowlITTSWPrefs backgroundStyle];
70 NSColor *backgroundColor = [GrowlITTSWPrefs backgroundColor];
71 ITTransientStatusWindowSizing windowSize = [GrowlITTSWPrefs windowSize];
73 if ([_window screen] != screen) {
74 [_window setScreen:screen];
76 if ([_window horizontalPosition] != horizontalPosition) {
77 [_window setHorizontalPosition:horizontalPosition];
79 if ([_window verticalPosition] != verticalPosition) {
80 [_window setVerticalPosition:verticalPosition];
83 if (![[_window entryEffect] isKindOfClass:appearanceEffect]) {
84 [_window setEntryEffect:[[[appearanceEffect alloc] initWithWindow:_window] autorelease]];
86 if ([[_window entryEffect] effectTime] != appearanceSpeed) {
87 [[_window entryEffect] setEffectTime:appearanceSpeed];
89 if (![[_window exitEffect] isKindOfClass:vanishEffect]) {
90 [_window setExitEffect:[[[vanishEffect alloc] initWithWindow:_window] autorelease]];
92 if ([[_window exitEffect] effectTime] != vanishSpeed) {
93 [[_window exitEffect] setEffectTime:vanishSpeed];
95 if ([_window exitDelay] != vanishDelay) {
96 [_window setExitDelay:vanishDelay];
99 if ([(ITTSWBackgroundView *)[_window contentView] backgroundMode] != backgroundStyle) {
100 [(ITTSWBackgroundView *)[_window contentView] setBackgroundMode:backgroundStyle];
102 if (([(ITTSWBackgroundView *)[_window contentView] backgroundMode] == ITTSWBackgroundColored) && ![[(ITTSWBackgroundView *)[_window contentView] backgroundColor] isEqual:backgroundColor]) {
103 [(ITTSWBackgroundView *)[_window contentView] setBackgroundColor:backgroundColor];
105 if ([_window sizing] != windowSize) {
106 [_window setSizing:windowSize];
110 - (void)showWindowWithText:(NSString *)text image:(NSImage *)image {
112 NSSize oldSize = [image size];
114 if (oldSize.width > oldSize.height) {
115 newSize = NSMakeSize(110.0f, (oldSize.height * (110.0f / oldSize.width)));
117 newSize = NSMakeSize((oldSize.width * (110.0f / oldSize.height)), 110.0f);
120 image = [[[[NSImage alloc] initWithData:[image TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
122 NSArray *gothicChars = [NSArray arrayWithObjects:[NSString stringWithUTF8String:"☆"], [NSString stringWithUTF8String:"★"], nil];
123 NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
125 if (([gothicChars count] > 0) && ([text length] > 0)) {
126 NSMutableString *gothicRegex = [[NSMutableString alloc] init];
128 [gothicRegex appendString:@"["];
129 for (NSString *gothicChar in gothicChars) {
130 [gothicRegex appendString:gothicChar];
132 [gothicRegex appendString:@"]+"];
134 NSUInteger endOfLastRange = 0;
136 while (endOfLastRange != NSNotFound) {
137 foundRange = [text rangeOfRegex:gothicRegex inRange:NSMakeRange(endOfLastRange, ([text length] - endOfLastRange))];
138 if (foundRange.location != NSNotFound) {
139 [attributedText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"AppleGothic" size:(18.0 / MINI_DIVISOR)], NSFontAttributeName, nil, nil] range:foundRange];
140 endOfLastRange = foundRange.location+foundRange.length;
141 if (endOfLastRange >= [text length]) {
142 endOfLastRange = NSNotFound;
145 endOfLastRange = NSNotFound;
150 [_window setImage:image];
151 [_window buildTextWindowWithString:attributedText];
152 [_window appear:self];
153 [attributedText release];