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 = ITTransientStatusWindowRegular;
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:(ITTransientStatusWindowSizing)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 NSRect visibleFrame = [[self screen] visibleFrame];
115 NSPoint screenOrigin = visibleFrame.origin;
116 float screenWidth = visibleFrame.size.width;
117 float screenHeight = visibleFrame.size.height;
118 float maxWidth = ( screenWidth - (SW_BORDER * 2) );
119 float maxHeight = ( screenHeight - (SW_BORDER * 2) );
120 float excessWidth = 0.0;
121 float excessHeight = 0.0;
122 NSPoint windowOrigin;
123 ITImageView *imageView;
124 BOOL shouldAnimate = ( ! (([self visibilityState] == ITWindowAppearingState) ||
125 ([self visibilityState] == ITWindowVanishingState)) );
127 if ( _sizing == ITTransientStatusWindowSmall ) {
128 divisor = SMALL_DIVISOR;
129 } else if ( _sizing == ITTransientStatusWindowMini ) {
130 divisor = MINI_DIVISOR;
133 // Get image width and height.
134 imageWidth = ( [_image size].width / divisor );
135 imageHeight = ( [_image size].height / divisor );
137 // Set the content height to the greater of the text and image heights.
138 contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight );
140 // Setup the Window, and remove all its contentview's subviews.
141 windowWidth = ( (SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor) + dataWidth + (SW_PAD / divisor) );
142 windowHeight = ( (SW_PAD / divisor) + contentHeight + (SW_PAD / divisor) );
144 // Constrain size to max limits. Adjust data sizes accordingly.
145 excessWidth = (windowWidth - maxWidth );
146 excessHeight = (windowHeight - maxHeight);
148 if ( excessWidth > 0.0 ) {
149 windowWidth = maxWidth;
150 dataWidth -= excessWidth;
153 if ( excessHeight > 0.0 ) {
154 windowHeight = maxHeight;
155 dataHeight -= excessHeight;
158 if ( [self horizontalPosition] == ITWindowPositionLeft ) {
159 windowOrigin.x = ( SW_BORDER + screenOrigin.x );
160 } else if ( [self horizontalPosition] == ITWindowPositionCenter ) {
161 windowOrigin.x = ( screenOrigin.x + (screenWidth / 2) - (windowWidth / 2) );
162 } else if ( [self horizontalPosition] == ITWindowPositionRight ) {
163 windowOrigin.x = ( screenOrigin.x + screenWidth - (windowWidth + SW_BORDER) );
166 if ( [self verticalPosition] == ITWindowPositionTop ) {
167 windowOrigin.y = ( screenOrigin.y + screenHeight - (windowHeight + SW_BORDER) );
168 } else if ( [self verticalPosition] == ITWindowPositionMiddle ) {
169 // Middle-oriented windows should be slightly proud of the screen's middle.
170 windowOrigin.y = ( (screenOrigin.y + (screenHeight / 2) - (windowHeight / 2)) + (screenHeight / 8) );
171 } else if ( [self verticalPosition] == ITWindowPositionBottom ) {
172 windowOrigin.y = ( SW_BORDER + screenOrigin.y );
175 [self setFrame:NSMakeRect( windowOrigin.x,
178 windowHeight) display:YES animate:shouldAnimate];
180 [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
182 // Setup, position, fill, and add the image view to the content view.
183 imageRect = NSMakeRect( (SW_PAD / divisor) + 4,
184 ((SW_PAD / divisor) + ((contentHeight - imageHeight) / 2)),
187 imageView = [[[ITImageView alloc] initWithFrame:imageRect] autorelease];
188 [imageView setAutoresizingMask:(NSViewMinYMargin | NSViewMaxYMargin)];
189 [imageView setImage:_image];
190 [imageView setCastsShadow:YES];
191 [[self contentView] addSubview:imageView];
193 return NSMakeRect( ((SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor)),
194 ((SW_PAD / divisor) + ((contentHeight - dataHeight) / 2)),
199 - (void)buildTextWindowWithString:(NSString *)text
204 float dataWidth = 0.0;
205 float dataHeight = 0.0;
207 NSArray *lines = [text componentsSeparatedByString:@"\n"];
209 NSEnumerator *lineEnum = [lines objectEnumerator];
210 float baseFontSize = 18.0;
211 ITTextField *textField;
215 if ( _sizing == ITTransientStatusWindowSmall ) {
216 divisor = SMALL_DIVISOR;
217 } else if ( _sizing == ITTransientStatusWindowMini ) {
218 divisor = MINI_DIVISOR;
221 font = [NSFont fontWithName:@"Lucida Grande Bold" size:(baseFontSize / divisor)];
222 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
224 // Iterate over each line to get text width and height
225 while ( (oneLine = [lineEnum nextObject]) ) {
226 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
227 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
228 // Add the height of this line to the total text height
229 dataHeight += [oneLine sizeWithAttributes:attr].height;
230 // If this line wider than the last one, set it as the text width.
231 dataWidth = ( ( dataWidth > oneLineWidth ) ? dataWidth : oneLineWidth );
234 // Add 4.0 to the final dataHeight to accomodate the shadow.
237 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
239 // Create, position, setup, fill, and add the text view to the content view.
240 textField = [[[ITTextField alloc] initWithFrame:dataRect] autorelease];
241 [textField setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
242 [textField setEditable:NO];
243 [textField setSelectable:NO];
244 [textField setBordered:NO];
245 [textField setDrawsBackground:NO];
246 [textField setFont:font];
247 [textField setTextColor:[NSColor whiteColor]];
248 [textField setCastsShadow:YES];
249 [[textField cell] setWraps:NO];
250 [textField setStringValue:text];
251 [textField setShadowSaturation:SW_SHADOW_SAT];
252 [[self contentView] addSubview:textField];
254 // Display the window.
255 [[self contentView] setNeedsDisplay:YES];
260 - (void)buildMeterWindowWithCharacter:(NSString *)character
275 NSEnumerator *cellEnum = nil;
278 NSColor *onColor = [NSColor whiteColor];
279 NSColor *offColor = [NSColor colorWithCalibratedWhite:0.15 alpha:0.50];
282 if ( _sizing == ITTransientStatusWindowSmall ) {
283 divisor = SMALL_DIVISOR;
284 } else if ( _sizing == ITTransientStatusWindowMini ) {
285 divisor = MINI_DIVISOR;
288 font = [NSFont fontWithName:@"Lucida Grande Bold" size:( size / divisor )];
289 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
290 charSize = [character sizeWithAttributes:attr];
291 cellHeight = ( charSize.height + 4.0 ); // Add 4.0 for shadow
292 cellWidth = ( (charSize.width) + (SW_METER_PAD / divisor) );
293 dataWidth = ( cellWidth * count );
294 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, cellHeight)];
295 volMatrix = [[[NSMatrix alloc] initWithFrame:dataRect
296 mode:NSHighlightModeMatrix
297 cellClass:NSClassFromString(@"ITTextFieldCell")
299 numberOfColumns:count] autorelease];
301 [volMatrix setCellSize:NSMakeSize(cellWidth, cellHeight)];
302 [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
303 [volMatrix setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
305 cellEnum = [[volMatrix cells] objectEnumerator];
307 while ( (aCell = [cellEnum nextObject]) ) {
308 [aCell setEditable:NO];
309 [aCell setSelectable:NO];
310 [aCell setBordered:NO];
311 [aCell setDrawsBackground:NO];
312 [aCell setAlignment:NSCenterTextAlignment];
313 [aCell setFont:font];
314 [aCell setStringValue:character];
315 [aCell setShadowSaturation:SW_SHADOW_SAT];
319 if ( active >= activeCount ) {
320 [aCell setCastsShadow:YES];
321 [aCell setTextColor:onColor];
323 [aCell setCastsShadow:NO];
324 [aCell setTextColor:offColor];
329 [[self contentView] addSubview:volMatrix];
330 [[self contentView] setNeedsDisplay:YES];
335 - (void)buildDialogWindowWithMessage:(NSString *)message
336 defaultButton:(NSString *)defaultTitle
337 alternateButton:(NSString *)alternateTitle
339 defaultAction:(SEL)okAction
340 alternateAction:(SEL)alternateAction
345 float textWidth = 0.0;
346 float textHeight = 0.0;
348 float cancelWidth = 0.0;
349 float wideButtonW = 0.0;
350 float buttonWidth = 0.0;
351 float dataHeight = 0.0;
352 float dataWidth = 0.0;
356 float textAddBelow = 32.0;
357 float dataMinH = 92.0;
358 float textMinH = 48.0;
359 NSArray *lines = [message componentsSeparatedByString:@"\n"];
361 NSEnumerator *lineEnum = [lines objectEnumerator];
362 float baseFontSize = 18.0;
363 ITTextField *textField;
365 ITButton *cancelButton;
366 NSColor *textColor = [NSColor whiteColor];
370 NSDictionary *buttonAttr;
372 if ( _sizing == ITTransientStatusWindowSmall ) {
373 divisor = SMALL_DIVISOR;
374 } else if ( _sizing == ITTransientStatusWindowMini ) {
375 divisor = MINI_DIVISOR;
378 font = [NSFont fontWithName:@"Lucida Grande Bold" size:(baseFontSize / divisor)];
379 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
380 buttonFont = [NSFont fontWithName:@"Lucida Grande Bold" size:(14 / divisor)];
381 buttonAttr = [NSDictionary dictionaryWithObjectsAndKeys:
382 buttonFont , NSFontAttributeName,
383 textColor , NSForegroundColorAttributeName,
386 // Iterate over each line to get text width and height
387 while ( (oneLine = [lineEnum nextObject]) ) {
388 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
389 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
390 // Add the height of this line to the total text height
391 textHeight += [oneLine sizeWithAttributes:attr].height;
392 // If this line wider than the last one, set it as the text width.
393 textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
396 // Add 4.0 to the final dataHeight to accomodate the shadow.
399 // Add extra padding below the text
400 dataHeight = (textHeight + textAddBelow);
402 // Test to see if data height is tall enough
403 if ( dataHeight < dataMinH ) {
404 dataHeight = dataMinH;
407 // Make the buttons, set the titles, and size them to fit their titles
408 okButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
409 cancelButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
410 [okButton setTarget:target];
411 [cancelButton setTarget:target];
412 [okButton setAction:okAction];
413 [cancelButton setAction:alternateAction];
414 [okButton setBezelStyle:ITGrayRoundedBezelStyle];
415 [cancelButton setBezelStyle:ITGrayRoundedBezelStyle];
416 [okButton setAlignment:NSRightTextAlignment];
417 [cancelButton setAlignment:NSCenterTextAlignment];
418 [okButton setImagePosition:NSNoImage];
419 [cancelButton setImagePosition:NSNoImage];
420 [okButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:defaultTitle
421 attributes:buttonAttr] autorelease]];
422 [cancelButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:alternateTitle
423 attributes:buttonAttr] autorelease]];
424 [okButton sizeToFit];
425 [cancelButton sizeToFit];
427 // Get the button widths. Add any extra width here.
428 okWidth = ([okButton frame].size.width + SW_BUTTON_EXTRA_W);
429 cancelWidth = ([cancelButton frame].size.width + SW_BUTTON_EXTRA_W);
431 // Figure out which button is wider.
432 wideButtonW = ( (okWidth > cancelWidth) ? okWidth : cancelWidth );
434 // Get the total width of the buttons. Add the divider space.
435 buttonWidth = ( (wideButtonW * 2) + SW_BUTTON_DIV );
437 // Set the dataWidth to whichever is greater: text width or button width.
438 dataWidth = ( (textWidth > buttonWidth) ? textWidth : buttonWidth);
441 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
443 // Set an initial vertical point for the textRect's origin.
444 textY = dataRect.origin.y + textAddBelow;
446 // Move that point up if the minimimum height of the text area is not occupied.
447 if ( textHeight < textMinH ) {
448 textY += ( (textMinH - textHeight) / 2 );
451 // Build the text rect.
452 textRect = NSMakeRect(dataRect.origin.x,
457 // Create, position, setup, fill, and add the text view to the content view.
458 textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
459 [textField setEditable:NO];
460 [textField setSelectable:NO];
461 [textField setBordered:NO];
462 [textField setDrawsBackground:NO];
463 [textField setFont:font];
464 [textField setTextColor:textColor];
465 [textField setCastsShadow:YES];
466 [textField setStringValue:message];
467 [textField setShadowSaturation:SW_SHADOW_SAT];
468 [[self contentView] addSubview:textField];
470 // Set the button frames, and add them to the content view.
471 [okButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - (wideButtonW + SW_BUTTON_PAD_R) ),
475 [cancelButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - ((wideButtonW * 2) + SW_BUTTON_DIV + SW_BUTTON_PAD_R) ),
479 [[self contentView] addSubview:okButton];
480 [[self contentView] addSubview:cancelButton];
482 [self setIgnoresMouseEvents:NO];
484 // Display the window.
485 [[self contentView] setNeedsDisplay:YES];
489 - (NSTimeInterval)animationResizeTime:(NSRect)newFrame
491 return (NSTimeInterval)0.25;