5 // Created by Matt L. Judy on Sat Feb 22 2003.
6 // Copyright (c) 2003 NibFile.com. All rights reserved.
9 #import "StatusWindow.h"
13 #define SW_SPACE 24.00
14 #define SW_MINW 211.00
15 #define SW_BORDER 32.00
16 #define SW_METER_PAD 4.00
17 #define SW_BUTTON_PAD_R 30.00
18 #define SW_BUTTON_PAD_B 24.00
19 #define SW_BUTTON_DIV 12.00
20 #define SW_BUTTON_EXTRA_W 8.00
21 #define SW_SHADOW_SAT 1.25
22 #define SMALL_DIVISOR 1.33333
23 #define MINI_DIVISOR 1.66667
25 @interface StatusWindow (Private)
26 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize;
30 @implementation StatusWindow
33 /*************************************************************************/
35 #pragma mark INITIALIZATION / DEALLOCATION METHODS
36 /*************************************************************************/
38 - (id)initWithContentView:(NSView *)contentView
39 exitMode:(ITTransientStatusWindowExitMode)exitMode
40 backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType
42 if ( ( self = [super initWithContentView:contentView
44 backgroundType:backgroundType] ) ) {
45 // Set default values.
46 _image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
48 _sizing = StatusWindowRegular;
61 /*************************************************************************/
63 #pragma mark ACCESSOR METHODS
64 /*************************************************************************/
66 - (void)setImage:(NSImage *)newImage
69 _image = [newImage copy];
72 - (void)setLocked:(BOOL)flag
75 [self setExitMode:(flag ? ITTransientStatusWindowExitOnCommand : ITTransientStatusWindowExitAfterDelay)];
78 - (void)setSizing:(StatusWindowSizing)newSizing
84 /*************************************************************************/
86 #pragma mark INSTANCE METHODS
87 /*************************************************************************/
89 - (void)appear:(id)sender
92 [super appear:sender];
96 - (void)vanish:(id)sender
99 [super vanish:sender];
103 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize
107 float imageWidth = 0.0;
108 float imageHeight = 0.0;
109 float dataWidth = dataSize.width;
110 float dataHeight = dataSize.height;
111 float contentHeight = 0.0;
112 float windowWidth = 0.0;
113 float windowHeight = 0.0;
114 ITImageView *imageView;
116 if ( _sizing == StatusWindowSmall ) {
117 divisor = SMALL_DIVISOR;
118 } else if ( _sizing == StatusWindowMini ) {
119 divisor = MINI_DIVISOR;
122 // Get image width and height.
123 imageWidth = ( [_image size].width / divisor );
124 imageHeight = ( [_image size].height / divisor );
126 // Set the content height to the greater of the text and image heights.
127 contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight );
129 // Setup the Window, and remove all its contentview's subviews.
130 windowWidth = ( (SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor) + dataWidth + (SW_PAD / divisor) );
131 windowHeight = ( (SW_PAD / divisor) + contentHeight + (SW_PAD / divisor) );
132 [self setFrame:NSMakeRect( (SW_BORDER + [[self screen] visibleFrame].origin.x),
133 (SW_BORDER + [[self screen] visibleFrame].origin.y),
135 windowHeight) display:YES animate:YES];
136 [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
138 // Setup, position, fill, and add the image view to the content view.
139 imageRect = NSMakeRect( (SW_PAD / divisor),
140 ((SW_PAD / divisor) + ((contentHeight - imageHeight) / 2)),
143 imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
144 [imageView setImage:_image];
145 [[self contentView] addSubview:imageView];
147 return NSMakeRect( ((SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor)),
148 ((SW_PAD / divisor) + ((contentHeight - dataHeight) / 2)),
153 - (void)buildTextWindowWithString:(NSString *)text
158 float dataWidth = 0.0;
159 float dataHeight = 0.0;
161 NSArray *lines = [text componentsSeparatedByString:@"\n"];
163 NSEnumerator *lineEnum = [lines objectEnumerator];
164 float baseFontSize = 18.0;
165 ITTextField *textField;
169 if ( _sizing == StatusWindowSmall ) {
170 divisor = SMALL_DIVISOR;
171 } else if ( _sizing == StatusWindowMini ) {
172 divisor = MINI_DIVISOR;
175 font = [NSFont fontWithName:@"Lucida Grande Bold" size:(baseFontSize / divisor)];
176 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
178 // Iterate over each line to get text width and height
179 while ( (oneLine = [lineEnum nextObject]) ) {
180 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
181 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
182 // Add the height of this line to the total text height
183 dataHeight += [oneLine sizeWithAttributes:attr].height;
184 // If this line wider than the last one, set it as the text width.
185 dataWidth = ( ( dataWidth > oneLineWidth ) ? dataWidth : oneLineWidth );
188 // Add 4.0 to the final dataHeight to accomodate the shadow.
191 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
193 // Create, position, setup, fill, and add the text view to the content view.
194 textField = [[[ITTextField alloc] initWithFrame:dataRect] autorelease];
195 [textField setEditable:NO];
196 [textField setSelectable:NO];
197 [textField setBordered:NO];
198 [textField setDrawsBackground:NO];
199 [textField setFont:font];
200 [textField setTextColor:[NSColor whiteColor]];
201 [textField setCastsShadow:YES];
202 [textField setStringValue:text];
203 [textField setShadowSaturation:SW_SHADOW_SAT];
204 [[self contentView] addSubview:textField];
206 // Display the window.
207 [[self contentView] setNeedsDisplay:YES];
212 - (void)buildMeterWindowWithCharacter:(NSString *)character
227 NSEnumerator *cellEnum = nil;
230 NSColor *onColor = [NSColor whiteColor];
231 NSColor *offColor = [NSColor colorWithCalibratedWhite:0.15 alpha:0.50];
234 if ( _sizing == StatusWindowSmall ) {
235 divisor = SMALL_DIVISOR;
236 } else if ( _sizing == StatusWindowMini ) {
237 divisor = MINI_DIVISOR;
240 font = [NSFont fontWithName:@"Lucida Grande Bold" size:( size / divisor )];
241 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
242 charSize = [character sizeWithAttributes:attr];
243 cellHeight = ( charSize.height + 4.0 ); // Add 4.0 for shadow
244 cellWidth = ( (charSize.width) + (SW_METER_PAD / divisor) );
245 dataWidth = ( cellWidth * count );
246 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, cellHeight)];
247 volMatrix = [[[NSMatrix alloc] initWithFrame:dataRect
248 mode:NSHighlightModeMatrix
249 cellClass:NSClassFromString(@"ITTextFieldCell")
251 numberOfColumns:count] autorelease];
253 [volMatrix setCellSize:NSMakeSize(cellWidth, cellHeight)];
254 [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
256 cellEnum = [[volMatrix cells] objectEnumerator];
258 while ( (aCell = [cellEnum nextObject]) ) {
259 [aCell setEditable:NO];
260 [aCell setSelectable:NO];
261 [aCell setBordered:NO];
262 [aCell setDrawsBackground:NO];
263 [aCell setAlignment:NSCenterTextAlignment];
264 [aCell setFont:font];
265 [aCell setStringValue:character];
266 [aCell setShadowSaturation:SW_SHADOW_SAT];
270 if ( active >= activeCount ) {
271 [aCell setCastsShadow:YES];
272 [aCell setTextColor:onColor];
274 [aCell setCastsShadow:NO];
275 [aCell setTextColor:offColor];
280 [[self contentView] addSubview:volMatrix];
281 [[self contentView] setNeedsDisplay:YES];
286 - (void)buildDialogWindowWithMessage:(NSString *)message
287 defaultButton:(NSString *)defaultTitle
288 alternateButton:(NSString *)alternateTitle
290 defaultAction:(SEL)okAction
291 alternateAction:(SEL)alternateAction
295 float textWidth = 0.0;
296 float textHeight = 0.0;
298 float cancelWidth = 0.0;
299 float wideButtonW = 0.0;
300 float buttonWidth = 0.0;
301 float dataHeight = 0.0;
302 float dataWidth = 0.0;
306 float textAddBelow = 32.0;
307 float dataMinH = 92.0;
308 float textMinH = 48.0;
309 NSArray *lines = [message componentsSeparatedByString:@"\n"];
311 NSEnumerator *lineEnum = [lines objectEnumerator];
312 ITTextField *textField;
314 ITButton *cancelButton;
315 NSColor *textColor = [NSColor whiteColor];
316 NSFont *font = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
317 NSDictionary *attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
318 NSFont *buttonFont = [NSFont fontWithName:@"Lucida Grande Bold" size:14];
319 NSDictionary *buttonAttr = [NSDictionary dictionaryWithObjectsAndKeys:
320 buttonFont , NSFontAttributeName,
321 textColor , NSForegroundColorAttributeName,
324 // Iterate over each line to get text width and height
325 while ( (oneLine = [lineEnum nextObject]) ) {
326 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
327 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
328 // Add the height of this line to the total text height
329 textHeight += [oneLine sizeWithAttributes:attr].height;
330 // If this line wider than the last one, set it as the text width.
331 textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
334 // Add 4.0 to the final dataHeight to accomodate the shadow.
337 // Add extra padding below the text
338 dataHeight = (textHeight + textAddBelow);
340 // Test to see if data height is tall enough
341 if ( dataHeight < dataMinH ) {
342 dataHeight = dataMinH;
345 // Make the buttons, set the titles, and size them to fit their titles
346 okButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
347 cancelButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
348 [okButton setTarget:target];
349 [cancelButton setTarget:target];
350 [okButton setAction:okAction];
351 [cancelButton setAction:alternateAction];
352 [okButton setBezelStyle:ITGrayRoundedBezelStyle];
353 [cancelButton setBezelStyle:ITGrayRoundedBezelStyle];
354 [okButton setAlignment:NSRightTextAlignment];
355 [cancelButton setAlignment:NSCenterTextAlignment];
356 [okButton setImagePosition:NSNoImage];
357 [cancelButton setImagePosition:NSNoImage];
358 [okButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:defaultTitle
359 attributes:buttonAttr] autorelease]];
360 [cancelButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:alternateTitle
361 attributes:buttonAttr] autorelease]];
362 [okButton sizeToFit];
363 [cancelButton sizeToFit];
365 // Get the button widths. Add any extra width here.
366 okWidth = ([okButton frame].size.width + SW_BUTTON_EXTRA_W);
367 cancelWidth = ([cancelButton frame].size.width + SW_BUTTON_EXTRA_W);
369 // Figure out which button is wider.
370 wideButtonW = ( (okWidth > cancelWidth) ? okWidth : cancelWidth );
372 // Get the total width of the buttons. Add the divider space.
373 buttonWidth = ( (wideButtonW * 2) + SW_BUTTON_DIV );
375 // Set the dataWidth to whichever is greater: text width or button width.
376 dataWidth = ( (textWidth > buttonWidth) ? textWidth : buttonWidth);
379 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
381 // Set an initial vertical point for the textRect's origin.
382 textY = dataRect.origin.y + textAddBelow;
384 // Move that point up if the minimimum height of the text area is not occupied.
385 if ( textHeight < textMinH ) {
386 textY += ( (textMinH - textHeight) / 2 );
389 // Build the text rect.
390 textRect = NSMakeRect(dataRect.origin.x,
395 // Create, position, setup, fill, and add the text view to the content view.
396 textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
397 [textField setEditable:NO];
398 [textField setSelectable:NO];
399 [textField setBordered:NO];
400 [textField setDrawsBackground:NO];
401 [textField setFont:font];
402 [textField setTextColor:textColor];
403 [textField setCastsShadow:YES];
404 [textField setStringValue:message];
405 [textField setShadowSaturation:SW_SHADOW_SAT];
406 [[self contentView] addSubview:textField];
408 // Set the button frames, and add them to the content view.
409 [okButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - (wideButtonW + SW_BUTTON_PAD_R) ),
413 [cancelButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - ((wideButtonW * 2) + SW_BUTTON_DIV + SW_BUTTON_PAD_R) ),
417 [[self contentView] addSubview:okButton];
418 [[self contentView] addSubview:cancelButton];
420 [self setIgnoresMouseEvents:NO];
422 // Display the window.
423 [[self contentView] setNeedsDisplay:YES];