2 #import "ITTransientStatusWindow.h"
3 #import "ITTextField.h"
9 #define SW_IMAGE @"Library"
11 @interface Controller (ITStatusItemSupport)
12 - (void)createStatusItem;
13 - (void)removeStatusItem;
17 @implementation Controller
21 [self createStatusItem];
22 [testTextField setCastsShadow:YES];
23 statusWindow = [ITTransientStatusWindow sharedWindow];
24 // [tabView setAllowsDragging:YES];
27 /*************************************************************************/
29 #pragma mark ITStatusItem SUPPORT
30 /*************************************************************************/
32 - (void)createStatusItem
34 statusItem = [[ITStatusItem alloc] initWithStatusBar:[NSStatusBar systemStatusBar]
35 withLength:NSVariableStatusItemLength];
37 if ( [showImageCheckBox state] == NSOnState ) {
38 [statusItem setImage:[NSImage imageNamed:@"ITStatusItem"]];
41 if ( [useInvertedCheckBox state] == NSOnState ) {
42 [statusItem setAlternateImage:[NSImage imageNamed:@"ITStatusItemInv"]];
45 if ( [showTitleCheckBox state] == NSOnState ) {
46 [statusItem setTitle:@"ITStatusItem"];
49 [statusItem setMenu:statusItemMenu];
52 - (void)removeStatusItem
54 [[statusItem statusBar] removeStatusItem:statusItem];
55 [statusItem autorelease];
59 - (IBAction)toggleStatusItem:(id)sender
61 if ( [sender state] == NSOnState ) {
62 [self createStatusItem];
63 [showImageCheckBox setEnabled:YES];
64 [showTitleCheckBox setEnabled:YES];
65 [useInvertedCheckBox setEnabled:YES];
67 [self removeStatusItem];
68 [showImageCheckBox setEnabled:NO];
69 [useInvertedCheckBox setEnabled:NO];
70 [showTitleCheckBox setEnabled:NO];
74 - (IBAction)toggleImage:(id)sender
76 if ( [sender state] == NSOnState ) {
77 [statusItem setImage:[NSImage imageNamed:@"ITStatusItem"]];
78 [statusItem setAlternateImage:[NSImage imageNamed:@"ITStatusItemInv"]];
79 [useInvertedCheckBox setEnabled:YES];
80 [useInvertedCheckBox setState:NSOnState];
82 [statusItem setImage:nil];
83 [statusItem setAlternateImage:nil];
84 [useInvertedCheckBox setEnabled:NO];
85 [useInvertedCheckBox setState:NSOffState];
89 - (IBAction)toggleInvertedImage:(id)sender
91 if ( [sender state] == NSOnState ) {
92 [statusItem setAlternateImage:[NSImage imageNamed:@"ITStatusItemInv"]];
94 [statusItem setAlternateImage:nil];
98 - (IBAction)toggleTitle:(id)sender
100 if ( [sender state] == NSOnState ) {
101 [statusItem setTitle:@"ITStatusItem"];
103 [statusItem setTitle:nil];
108 /*************************************************************************/
110 #pragma mark ITTextView SUPPORT
111 /*************************************************************************/
113 - (IBAction)toggleCastsShadow:(id)sender
115 [testTextField setCastsShadow:([sender state] == NSOnState)];
119 /*************************************************************************/
121 #pragma mark ITTransientStatusWindow SUPPORT
122 /*************************************************************************/
124 - (IBAction)buildStatusWindow:(id)sender
126 NSImageView *imageView = nil;
127 ITTextField *textField = nil;
128 NSImage *image = [NSImage imageNamed:SW_IMAGE];
132 float imageWidth = 0.0;
133 float imageHeight = 0.0;
134 float textWidth = 0.0;
135 float textHeight = 0.0;
136 float contentHeight = 0.0;
137 float windowWidth = 0.0;
138 float windowHeight = 0.0;
140 NSString *text = [swSampleTextView string];
141 NSArray *lines = [text componentsSeparatedByString:@"\n"];
143 NSEnumerator *lineEnum = [lines objectEnumerator];
145 NSFont *font = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
146 NSDictionary *attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
148 // Get image width and height.
149 imageWidth = [image size].width;
150 imageHeight = [image size].height;
152 // Iterate over each line to get text width and height
153 while ( oneLine = [lineEnum nextObject] ) {
154 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
155 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
156 // Add the height of this line to the total text height
157 textHeight += [oneLine sizeWithAttributes:attr].height;
158 // If this line wider than the last one, set it as the text width.
159 textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
162 // Add 4.0 to the final textHeight to accomodate the shadow.
165 // Set the content height to the greater of the text and image heights.
166 contentHeight = ( ( imageHeight > textHeight ) ? imageHeight : textHeight );
168 // Setup the Window, and remove all its contentview's subviews.
169 windowWidth = ( SW_PAD + imageWidth + SW_SPACE + textWidth + SW_PAD );
170 windowHeight = ( SW_PAD + contentHeight + SW_PAD );
171 [statusWindow setFrame:NSMakeRect(SW_BORDER, SW_BORDER, windowWidth, windowHeight) display:YES];
172 [[[statusWindow contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
174 // Setup, position, fill, and add the image view to the content view.
175 imageRect = NSMakeRect( SW_PAD,
176 (SW_PAD + ((contentHeight - imageHeight) / 2)),
179 imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
180 [imageView setImage:image];
181 [[statusWindow contentView] addSubview:imageView];
183 // Setup, position, fill, and add the text view to the content view.
184 textRect = NSMakeRect( (SW_PAD + imageWidth + SW_SPACE),
185 (SW_PAD + ((contentHeight - textHeight) / 2)),
188 textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
189 [textField setBordered:NO];
190 [textField setDrawsBackground:NO];
191 [textField setFont:[NSFont fontWithName:@"Lucida Grande Bold" size:18]];
192 [textField setTextColor:[NSColor whiteColor]];
193 [textField setCastsShadow:YES];
194 [textField setStringValue:text];
195 [[statusWindow contentView] addSubview:textField];
197 [[statusWindow contentView] setNeedsDisplay:YES];
201 - (IBAction)foo:(id)sender
204 maxLineHeight = ( ( maxLineHeight > [cdImage size].height ) ? maxLineHeight : [cdImage size].height );
206 totalWidth = ( ITTSWPADDING + [cdImage size].width + ITTSWSPACING + maxLineWidth + ITTSWPADDING );
207 totalHeight = ( ITTSWPADDING + maxLineHeight + ITTSWPADDING );
209 totalWidth = ( ( totalWidth > ITTSWMINW ) ? totalWidth : ITTSWMINW );
212 [statusWindow setFrame:NSMakeRect(72.0, 72.0, totalWidth, totalHeight)
215 textField = [[[ITTextField alloc] initWithFrame:
216 NSMakeRect((24.0 + [cdImage size].width + 32.0), 24.0, maxLineWidth, maxLineHeight)] autorelease];
217 [[statusWindow contentView] addSubview:textField];
218 [textField setBordered:NO];
219 [textField setDrawsBackground:NO];
220 [textField setFont:[NSFont fontWithName:@"Lucida Grande Bold" size:18]];
221 [textField setTextColor:[NSColor whiteColor]];
222 [textField setCastsShadow:YES];
223 [textField setStringValue:text];
226 [[statusWindow contentView] lockFocus];
227 [cdImage compositeToPoint:NSMakePoint(ITTSWPADDING, ( ITTSWPADDING + ((maxLineHeight - [cdImage size].height) / ITTSWPADDING)) )
228 operation:NSCompositeSourceOver];
229 [[statusWindow contentView] unlockFocus];
233 - (IBAction)showStatusWindow:(id)sender
235 // [[statusWindow contentView] setNeedsDisplay:YES];
236 [statusWindow setFrame:NSMakeRect( (0.0 - NSWidth([statusWindow frame])),
238 NSWidth([statusWindow frame]),
239 NSHeight([statusWindow frame]) )
241 [statusWindow orderFront:self];
242 [statusWindow setFrame:NSMakeRect( SW_BORDER,
244 NSWidth([statusWindow frame]),
245 NSHeight([statusWindow frame]) )
250 - (IBAction)hideStatusWindow:(id)sender
252 [statusWindow orderOut:self];
255 /*************************************************************************/
257 #pragma mark NSWindow DELEGATE METHODS
258 /*************************************************************************/
260 - (void)windowWillMiniaturize:(NSNotification *)note
262 [[note object] setMiniwindowImage:[NSImage imageNamed:@"ITStatusItem"]];