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
23 @interface StatusWindow (Private)
24 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize;
28 @implementation StatusWindow
31 /*************************************************************************/
33 #pragma mark INITIALIZATION / DEALLOCATION METHODS
34 /*************************************************************************/
36 - (id)initWithContentView:(NSView *)contentView
37 exitMode:(ITTransientStatusWindowExitMode)exitMode
38 backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType
40 if ( ( self = [super initWithContentView:contentView
42 backgroundType:backgroundType] ) ) {
43 // Set default values.
44 _image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
46 _sizing = ITTransientStatusWindowRegular;
59 /*************************************************************************/
61 #pragma mark ACCESSOR METHODS
62 /*************************************************************************/
64 - (void)setImage:(NSImage *)newImage
67 _image = [newImage copy];
70 - (void)setLocked:(BOOL)flag
73 [self setExitMode:(flag ? ITTransientStatusWindowExitOnCommand : ITTransientStatusWindowExitAfterDelay)];
76 - (void)setSizing:(ITTransientStatusWindowSizing)newSizing
81 /*************************************************************************/
83 #pragma mark INSTANCE METHODS
84 /*************************************************************************/
86 - (void)appear:(id)sender
89 [super appear:sender];
93 - (void)vanish:(id)sender
96 [super vanish:sender];
100 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize
104 float imageWidth = 0.0;
105 float imageHeight = 0.0;
106 float dataWidth = dataSize.width;
107 float dataHeight = dataSize.height;
108 float contentHeight = 0.0;
109 float windowWidth = 0.0;
110 float windowHeight = 0.0;
111 NSRect visibleFrame = [[self screen] visibleFrame];
112 NSPoint screenOrigin = visibleFrame.origin;
113 float screenWidth = visibleFrame.size.width;
114 float screenHeight = visibleFrame.size.height;
115 float maxWidth = ( screenWidth - (SW_BORDER * 2) );
116 float maxHeight = ( screenHeight - (SW_BORDER * 2) );
117 float excessWidth = 0.0;
118 float excessHeight = 0.0;
119 NSPoint windowOrigin = NSZeroPoint;
120 ITImageView *imageView;
121 BOOL shouldAnimate = ( ! (([self visibilityState] == ITWindowAppearingState) ||
122 ([self visibilityState] == ITWindowVanishingState)) );
124 if ( _sizing == ITTransientStatusWindowSmall ) {
125 divisor = SMALL_DIVISOR;
126 } else if ( _sizing == ITTransientStatusWindowMini ) {
127 divisor = MINI_DIVISOR;
130 // Get image width and height.
131 imageWidth = ( [_image size].width / divisor );
132 imageHeight = ( [_image size].height / divisor );
134 // Set the content height to the greater of the text and image heights.
135 contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight );
137 // Setup the Window, and remove all its contentview's subviews.
138 windowWidth = ( (SW_PAD / divisor) + imageWidth + ((dataWidth > 0) ? (SW_SPACE / divisor) + dataWidth : 0) + (SW_PAD / divisor) );
139 windowHeight = ( (SW_PAD / divisor) + contentHeight + (SW_PAD / divisor) );
141 // Constrain size to max limits. Adjust data sizes accordingly.
142 excessWidth = (windowWidth - maxWidth );
143 excessHeight = (windowHeight - maxHeight);
145 if ( excessWidth > 0.0 ) {
146 windowWidth = maxWidth;
147 dataWidth -= excessWidth;
150 if ( excessHeight > 0.0 ) {
151 windowHeight = maxHeight;
152 dataHeight -= excessHeight;
155 if ( [self horizontalPosition] == ITWindowPositionLeft ) {
156 windowOrigin.x = ( SW_BORDER + screenOrigin.x );
157 } else if ( [self horizontalPosition] == ITWindowPositionCenter ) {
158 windowOrigin.x = ( screenOrigin.x + (screenWidth / 2) - (windowWidth / 2) );
159 } else if ( [self horizontalPosition] == ITWindowPositionRight ) {
160 windowOrigin.x = ( screenOrigin.x + screenWidth - (windowWidth + SW_BORDER) );
163 if ( [self verticalPosition] == ITWindowPositionTop ) {
164 windowOrigin.y = ( screenOrigin.y + screenHeight - (windowHeight + SW_BORDER) );
165 } else if ( [self verticalPosition] == ITWindowPositionMiddle ) {
166 // Middle-oriented windows should be slightly proud of the screen's middle.
167 windowOrigin.y = ( (screenOrigin.y + (screenHeight / 2) - (windowHeight / 2)) + (screenHeight / 8) );
168 } else if ( [self verticalPosition] == ITWindowPositionBottom ) {
169 windowOrigin.y = ( SW_BORDER + screenOrigin.y );
172 [self setFrame:NSMakeRect( windowOrigin.x,
175 windowHeight) display:YES animate:shouldAnimate];
177 [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
179 // Setup, position, fill, and add the image view to the content view.
180 imageRect = NSMakeRect( (SW_PAD / divisor) + ((dataWidth > 0) ? 4 : 0),
181 ((SW_PAD / divisor) + ((contentHeight - imageHeight) / 2)),
184 imageView = [[[ITImageView alloc] initWithFrame:imageRect] autorelease];
185 [imageView setAutoresizingMask:(NSViewMinYMargin | NSViewMaxYMargin)];
186 [imageView setImage:_image];
187 [imageView setCastsShadow:YES];
188 [[self contentView] addSubview:imageView];
190 return NSMakeRect( ((SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor)),
191 ((SW_PAD / divisor) + ((contentHeight - dataHeight) / 2)),
196 - (void)buildImageWindowWithImage:(NSImage *)image
202 if (_sizing == ITTransientStatusWindowSmall) {
203 divisor = SMALL_DIVISOR;
204 } else if (_sizing == ITTransientStatusWindowMini) {
205 divisor = MINI_DIVISOR;
208 [self setImage:image];
209 dataRect = [self setupWindowWithDataSize:NSMakeSize(0, 0)]; //We have no text, so there is no data
210 [[self contentView] setNeedsDisplay:YES];
214 - (void)buildTextWindowWithString:(id)text
219 float dataWidth = 0.0;
220 float dataHeight = 0.0;
222 NSArray *lines = [(([text isKindOfClass:[NSString class]]) ? text : [text mutableString]) componentsSeparatedByString:@"\n"];
224 NSEnumerator *lineEnum = [lines objectEnumerator];
225 float baseFontSize = 18.0;
226 ITTextField *textField;
230 if ( _sizing == ITTransientStatusWindowSmall ) {
231 divisor = SMALL_DIVISOR;
232 } else if ( _sizing == ITTransientStatusWindowMini ) {
233 divisor = MINI_DIVISOR;
236 font = [NSFont fontWithName:@"LucidaGrande-Bold" size:(baseFontSize / divisor)];
237 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
239 // Iterate over each line to get text width and height
240 while ( (oneLine = [lineEnum nextObject]) ) {
241 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
242 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
243 // Add the height of this line to the total text height
244 dataHeight += [oneLine sizeWithAttributes:attr].height;
245 // If this line wider than the last one, set it as the text width.
246 dataWidth = ( ( dataWidth > oneLineWidth ) ? dataWidth : oneLineWidth );
249 // Add 4.0 to the final dataHeight to accomodate the shadow.
252 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
254 // Create, position, setup, fill, and add the text view to the content view.
255 textField = [[[ITTextField alloc] initWithFrame:dataRect] autorelease];
256 [textField setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
257 [textField setEditable:NO];
258 [textField setSelectable:NO];
259 [textField setBordered:NO];
260 [textField setDrawsBackground:NO];
261 [textField setFont:font];
262 [textField setTextColor:[NSColor whiteColor]];
263 [textField setCastsShadow:YES];
264 [[textField cell] setWraps:NO];
266 if ([text isKindOfClass:[NSString class]]) {
267 [textField setStringValue:text];
269 [textField setAttributedStringValue:text];
272 [textField setShadowSaturation:SW_SHADOW_SAT];
273 [[self contentView] addSubview:textField];
275 // Display the window.
276 [[self contentView] setNeedsDisplay:YES];
277 _textField = textField;
281 - (void)buildMeterWindowWithCharacter:(NSString *)character
296 NSEnumerator *cellEnum = nil;
299 NSColor *onColor = [NSColor whiteColor];
300 NSColor *offColor = [NSColor colorWithCalibratedWhite:0.15 alpha:0.50];
303 if ( _sizing == ITTransientStatusWindowSmall ) {
304 divisor = SMALL_DIVISOR;
305 } else if ( _sizing == ITTransientStatusWindowMini ) {
306 divisor = MINI_DIVISOR;
309 font = [NSFont fontWithName:@"AppleGothic" size:( size / divisor )];
310 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
311 charSize = [character sizeWithAttributes:attr];
312 cellHeight = ( charSize.height + 4.0 ); // Add 4.0 for shadow
313 cellWidth = ( (charSize.width) + (SW_METER_PAD / divisor) );
314 dataWidth = ( cellWidth * count );
315 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, cellHeight)];
316 volMatrix = [[[NSMatrix alloc] initWithFrame:dataRect
317 mode:NSHighlightModeMatrix
318 cellClass:NSClassFromString(@"ITTextFieldCell")
320 numberOfColumns:count] autorelease];
322 [volMatrix setCellSize:NSMakeSize(cellWidth, cellHeight)];
323 [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
324 [volMatrix setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
326 cellEnum = [[volMatrix cells] objectEnumerator];
328 while ( (aCell = [cellEnum nextObject]) ) {
329 [aCell setEditable:NO];
330 [aCell setSelectable:NO];
331 [aCell setBordered:NO];
332 [aCell setDrawsBackground:NO];
333 [aCell setAlignment:NSCenterTextAlignment];
334 [aCell setFont:font];
335 [aCell setStringValue:character];
336 [aCell setShadowSaturation:SW_SHADOW_SAT];
340 if ( active >= activeCount ) {
341 [aCell setCastsShadow:YES];
342 [aCell setTextColor:onColor];
344 [aCell setCastsShadow:NO];
345 [aCell setTextColor:offColor];
350 [[self contentView] addSubview:volMatrix];
351 [[self contentView] setNeedsDisplay:YES];
356 - (void)buildDialogWindowWithMessage:(NSString *)message
357 defaultButton:(NSString *)defaultTitle
358 alternateButton:(NSString *)alternateTitle
360 defaultAction:(SEL)okAction
361 alternateAction:(SEL)alternateAction
366 float textWidth = 0.0;
367 float textHeight = 0.0;
369 float cancelWidth = 0.0;
370 float wideButtonW = 0.0;
371 float buttonWidth = 0.0;
372 float dataHeight = 0.0;
373 float dataWidth = 0.0;
377 float textAddBelow = 32.0;
378 float dataMinH = 92.0;
379 float textMinH = 48.0;
380 NSArray *lines = [message componentsSeparatedByString:@"\n"];
382 NSEnumerator *lineEnum = [lines objectEnumerator];
383 float baseFontSize = 18.0;
384 ITTextField *textField;
386 ITButton *cancelButton;
387 NSColor *textColor = [NSColor whiteColor];
391 NSDictionary *buttonAttr;
393 if ( _sizing == ITTransientStatusWindowSmall ) {
394 divisor = SMALL_DIVISOR;
395 } else if ( _sizing == ITTransientStatusWindowMini ) {
396 divisor = MINI_DIVISOR;
399 font = [NSFont fontWithName:@"LucidaGrande-Bold" size:(baseFontSize / divisor)];
400 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
401 buttonFont = [NSFont fontWithName:@"LucidaGrande-Bold" size:(14 / divisor)];
402 buttonAttr = [NSDictionary dictionaryWithObjectsAndKeys:
403 buttonFont , NSFontAttributeName,
404 textColor , NSForegroundColorAttributeName,
407 // Iterate over each line to get text width and height
408 while ( (oneLine = [lineEnum nextObject]) ) {
409 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
410 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
411 // Add the height of this line to the total text height
412 textHeight += [oneLine sizeWithAttributes:attr].height;
413 // If this line wider than the last one, set it as the text width.
414 textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
417 // Add 4.0 to the final dataHeight to accomodate the shadow.
420 // Add extra padding below the text
421 dataHeight = (textHeight + textAddBelow);
423 // Test to see if data height is tall enough
424 if ( dataHeight < dataMinH ) {
425 dataHeight = dataMinH;
428 // Make the buttons, set the titles, and size them to fit their titles
429 okButton = [[[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)] autorelease];
430 cancelButton = [[[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)] autorelease];
431 [okButton setTarget:target];
432 [cancelButton setTarget:target];
433 [okButton setAction:okAction];
434 [cancelButton setAction:alternateAction];
435 [okButton setBezelStyle:ITGrayRoundedBezelStyle];
436 [cancelButton setBezelStyle:ITGrayRoundedBezelStyle];
437 [okButton setAlignment:NSRightTextAlignment];
438 [cancelButton setAlignment:NSCenterTextAlignment];
439 [okButton setImagePosition:NSNoImage];
440 [cancelButton setImagePosition:NSNoImage];
441 [okButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:defaultTitle
442 attributes:buttonAttr] autorelease]];
443 [cancelButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:alternateTitle
444 attributes:buttonAttr] autorelease]];
445 [okButton sizeToFit];
446 [cancelButton sizeToFit];
448 // Get the button widths. Add any extra width here.
449 okWidth = ([okButton frame].size.width + SW_BUTTON_EXTRA_W);
450 cancelWidth = ([cancelButton frame].size.width + SW_BUTTON_EXTRA_W);
452 // Figure out which button is wider.
453 wideButtonW = ( (okWidth > cancelWidth) ? okWidth : cancelWidth );
455 // Get the total width of the buttons. Add the divider space.
456 buttonWidth = ( (wideButtonW * 2) + SW_BUTTON_DIV );
458 // Set the dataWidth to whichever is greater: text width or button width.
459 dataWidth = ( (textWidth > buttonWidth) ? textWidth : buttonWidth);
462 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
464 // Set an initial vertical point for the textRect's origin.
465 textY = dataRect.origin.y + textAddBelow;
467 // Move that point up if the minimimum height of the text area is not occupied.
468 if ( textHeight < textMinH ) {
469 textY += ( (textMinH - textHeight) / 2 );
472 // Build the text rect.
473 textRect = NSMakeRect(dataRect.origin.x,
478 // Create, position, setup, fill, and add the text view to the content view.
479 textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
480 [textField setEditable:NO];
481 [textField setSelectable:NO];
482 [textField setBordered:NO];
483 [textField setDrawsBackground:NO];
484 [textField setFont:font];
485 [textField setTextColor:textColor];
486 [textField setCastsShadow:YES];
487 [textField setStringValue:message];
488 [textField setShadowSaturation:SW_SHADOW_SAT];
489 [[self contentView] addSubview:textField];
491 // Set the button frames, and add them to the content view.
492 [okButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - (wideButtonW + SW_BUTTON_PAD_R) ),
496 [cancelButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - ((wideButtonW * 2) + SW_BUTTON_DIV + SW_BUTTON_PAD_R) ),
500 [[self contentView] addSubview:okButton];
501 if (alternateTitle) {
502 [[self contentView] addSubview:cancelButton];
505 [self setIgnoresMouseEvents:NO];
507 // Display the window.
508 [[self contentView] setNeedsDisplay:YES];
512 - (void)updateTime:(NSString *)time range:(NSRange)range
514 NSMutableAttributedString *string = [[_textField attributedStringValue] mutableCopy];
515 [string replaceCharactersInRange:range withString:time];
516 [_textField setAttributedStringValue:[string autorelease]];
517 [[self contentView] setNeedsDisplay:YES];
520 - (NSTimeInterval)animationResizeTime:(NSRect)newFrame
522 return (NSTimeInterval)0.25;