2 // GrowlITTSWController.m
5 // Created by Joseph Spiros on 2/28/09.
6 // Copyright 2009 iThink Software. All rights reserved.
9 #import "GrowlITTSWController.h"
11 #import <ITKit/ITTSWBackgroundView.h>
12 #import <ITKit/ITWindowEffect.h>
14 #import "RegexKitLite.h"
16 #import "GrowlPositioningDefines.h"
18 @interface GrowlPositionController
19 + (enum GrowlPosition)selectedOriginPosition;
22 @implementation NSImage (SmoothAdditions)
24 - (NSImage *)imageScaledSmoothlyToSize:(NSSize)scaledSize
27 NSImageRep *rep = [self bestRepresentationForDevice:nil];
29 newImage = [[NSImage alloc] initWithSize:scaledSize];
32 [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
33 [[NSGraphicsContext currentContext] setShouldAntialias:YES];
34 [rep drawInRect:NSMakeRect(3, 3, scaledSize.width - 6, scaledSize.height - 6)];
36 [newImage unlockFocus];
37 return [newImage autorelease];
42 @implementation GrowlITTSWController
46 if ( ( self = [super init] ) ) {
47 NSArray *screens = [NSScreen screens];
49 _window = [[GrowlITTSWWindow sharedWindow] retain];
51 [_window setScreen:[screens objectAtIndex:0]];
53 [_window setExitMode:ITTransientStatusWindowExitAfterDelay];
54 [_window setExitDelay:4.0];
56 [_window setSizing:ITTransientStatusWindowMini];
58 [_window setEntryEffect:[[[NSClassFromString(@"ITSlideVerticallyWindowEffect") alloc] initWithWindow:_window] autorelease]];
59 [_window setExitEffect:[[[NSClassFromString(@"ITSlideHorizontallyWindowEffect") alloc] initWithWindow:_window] autorelease]];
61 [[_window entryEffect] setEffectTime:0.8];
62 [[_window exitEffect] setEffectTime:0.8];
64 [(ITTSWBackgroundView *)[_window contentView]setBackgroundMode:
65 ITTSWBackgroundReadable];
77 - (void)showWindowWithText:(NSString *)text image:(NSImage *)image
80 NSSize oldSize = [image size];
82 if (oldSize.width > oldSize.height) {
83 newSize = NSMakeSize(110.0f, (oldSize.height * (110.0f / oldSize.width)));
85 newSize = NSMakeSize((oldSize.width * (110.0f / oldSize.height)), 110.0f);
88 image = [[[[NSImage alloc] initWithData:[image TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
90 NSArray *gothicChars = [NSArray arrayWithObjects:[NSString stringWithUTF8String:"☆"], [NSString stringWithUTF8String:"★"], nil];
91 NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
93 if (([gothicChars count] > 0) && ([text length] > 0)) {
94 NSMutableString *gothicRegex = [[NSMutableString alloc] init];
96 [gothicRegex appendString:@"["];
97 for (NSString *gothicChar in gothicChars) {
98 [gothicRegex appendString:gothicChar];
100 [gothicRegex appendString:@"]+"];
102 NSUInteger endOfLastRange = 0;
104 while (endOfLastRange != NSNotFound) {
105 foundRange = [text rangeOfRegex:gothicRegex inRange:NSMakeRange(endOfLastRange, ([text length] - endOfLastRange))];
106 if (foundRange.location != NSNotFound) {
107 [attributedText setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"AppleGothic" size:(18.0 / MINI_DIVISOR)], NSFontAttributeName, nil, nil] range:foundRange];
108 endOfLastRange = foundRange.location+foundRange.length;
109 if (endOfLastRange >= [text length]) {
110 endOfLastRange = NSNotFound;
113 endOfLastRange = NSNotFound;
118 switch ([GrowlPositionController selectedOriginPosition]) {
119 case GrowlMiddleColumnPosition:
120 [_window setVerticalPosition:ITWindowPositionMiddle];
121 [_window setHorizontalPosition:ITWindowPositionCenter];
123 case GrowlTopLeftPosition:
124 [_window setVerticalPosition:ITWindowPositionTop];
125 [_window setHorizontalPosition:ITWindowPositionLeft];
127 case GrowlBottomRightPosition:
128 [_window setVerticalPosition:ITWindowPositionBottom];
129 [_window setHorizontalPosition:ITWindowPositionRight];
131 case GrowlTopRightPosition:
132 [_window setVerticalPosition:ITWindowPositionTop];
133 [_window setHorizontalPosition:ITWindowPositionRight];
135 case GrowlBottomLeftPosition:
136 [_window setVerticalPosition:ITWindowPositionBottom];
137 [_window setHorizontalPosition:ITWindowPositionLeft];
141 [_window setImage:image];
142 [_window buildTextWindowWithString:attributedText];
143 [_window appear:self];
144 [attributedText release];