Implemented searching notification for characters which should use the AppleGothic...
[GrowlITTSW.git] / GrowlITTSWController.m
1 //
2 //  GrowlITTSWController.m
3 //  Growl
4 //
5 //  Created by Joseph Spiros on 2/28/09.
6 //  Copyright 2009 iThink Software. All rights reserved.
7 //
8
9 #import "GrowlITTSWController.h"
10
11 #import <ITKit/ITTSWBackgroundView.h>
12 #import <ITKit/ITWindowEffect.h>
13
14 #import "RegexKitLite.h"
15
16 #import "GrowlPositioningDefines.h"
17
18 @interface GrowlPositionController
19 + (enum GrowlPosition)selectedOriginPosition;
20 @end
21
22 @implementation NSImage (SmoothAdditions)
23
24 - (NSImage *)imageScaledSmoothlyToSize:(NSSize)scaledSize
25 {
26     NSImage *newImage;
27     NSImageRep *rep = [self bestRepresentationForDevice:nil];
28     
29     newImage = [[NSImage alloc] initWithSize:scaledSize];
30     [newImage lockFocus];
31     {
32         [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
33         [[NSGraphicsContext currentContext] setShouldAntialias:YES];
34         [rep drawInRect:NSMakeRect(3, 3, scaledSize.width - 6, scaledSize.height - 6)];
35     }
36     [newImage unlockFocus];
37     return [newImage autorelease];
38 }
39
40 @end
41
42 @implementation GrowlITTSWController
43
44 - (id)init
45 {
46     if ( ( self = [super init] ) ) {
47                 NSArray *screens = [NSScreen screens];
48         
49                 _window = [[GrowlITTSWWindow sharedWindow] retain];
50                 
51                 [_window setScreen:[screens objectAtIndex:0]];
52                 
53         [_window setExitMode:ITTransientStatusWindowExitAfterDelay];
54         [_window setExitDelay:4.0];
55         
56         [_window setSizing:ITTransientStatusWindowMini];
57         
58         [_window setEntryEffect:[[[NSClassFromString(@"ITSlideVerticallyWindowEffect") alloc] initWithWindow:_window] autorelease]];
59         [_window setExitEffect:[[[NSClassFromString(@"ITSlideHorizontallyWindowEffect") alloc] initWithWindow:_window] autorelease]];
60         
61         [[_window entryEffect] setEffectTime:0.8];
62         [[_window exitEffect]  setEffectTime:0.8];
63         
64         [(ITTSWBackgroundView *)[_window contentView]setBackgroundMode:
65                  ITTSWBackgroundReadable];
66     }
67     
68     return self;
69 }
70
71 - (void)dealloc
72 {
73     [_window release];
74     [super dealloc];
75 }
76
77 - (void)showWindowWithText:(NSString *)text image:(NSImage *)image
78 {
79         NSSize newSize;
80         NSSize oldSize = [image size];
81         
82         if (oldSize.width > oldSize.height) {
83                 newSize = NSMakeSize(110.0f, (oldSize.height * (110.0f / oldSize.width)));
84         } else {
85                 newSize = NSMakeSize((oldSize.width * (110.0f / oldSize.height)), 110.0f);
86         }
87         
88         image = [[[[NSImage alloc] initWithData:[image TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
89         
90         NSArray *gothicChars = [NSArray arrayWithObjects:[NSString stringWithUTF8String:"☆"], [NSString stringWithUTF8String:"★"], nil];
91         NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
92         
93         if (([gothicChars count] > 0) && ([text length] > 0)) {
94                 NSMutableString *gothicRegex = [[NSMutableString alloc] init];
95                 
96                 [gothicRegex appendString:@"["];
97                 for (NSString *gothicChar in gothicChars) {
98                         [gothicRegex appendString:gothicChar];
99                 }
100                 [gothicRegex appendString:@"]+"];
101                 
102                 NSUInteger endOfLastRange = 0;
103                 NSRange foundRange;
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;
111                                 }
112                         } else {
113                                 endOfLastRange = NSNotFound;
114                         }
115                 }
116         }
117         
118         switch ([GrowlPositionController selectedOriginPosition]) {
119                 case GrowlMiddleColumnPosition:
120                         [_window setVerticalPosition:ITWindowPositionMiddle];
121                         [_window setHorizontalPosition:ITWindowPositionCenter];
122                         break;
123                 case GrowlTopLeftPosition:
124                         [_window setVerticalPosition:ITWindowPositionTop];
125                         [_window setHorizontalPosition:ITWindowPositionLeft];
126                         break;
127                 case GrowlBottomRightPosition:
128                         [_window setVerticalPosition:ITWindowPositionBottom];
129                         [_window setHorizontalPosition:ITWindowPositionRight];
130                         break;
131                 case GrowlTopRightPosition:
132                         [_window setVerticalPosition:ITWindowPositionTop];
133                         [_window setHorizontalPosition:ITWindowPositionRight];
134                         break;
135                 case GrowlBottomLeftPosition:
136                         [_window setVerticalPosition:ITWindowPositionBottom];
137                         [_window setHorizontalPosition:ITWindowPositionLeft];
138                         break;
139         }
140         
141         [_window setImage:image];
142     [_window buildTextWindowWithString:attributedText];
143     [_window appear:self];
144         [attributedText release];
145 }
146
147 @end