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
83 /*************************************************************************/
85 #pragma mark INSTANCE METHODS
86 /*************************************************************************/
88 - (void)appear:(id)sender
91 [super appear:sender];
95 - (void)vanish:(id)sender
98 [super vanish:sender];
102 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize
106 float imageWidth = 0.0;
107 float imageHeight = 0.0;
108 float dataWidth = dataSize.width;
109 float dataHeight = dataSize.height;
110 float contentHeight = 0.0;
111 float windowWidth = 0.0;
112 float windowHeight = 0.0;
113 NSRect visibleFrame = [[self screen] visibleFrame];
114 NSPoint screenOrigin = visibleFrame.origin;
115 float screenWidth = visibleFrame.size.width;
116 float screenHeight = visibleFrame.size.height;
117 float maxWidth = ( screenWidth - (SW_BORDER * 2) );
118 float maxHeight = ( screenHeight - (SW_BORDER * 2) );
119 float excessWidth = 0.0;
120 float excessHeight = 0.0;
121 NSPoint windowOrigin;
122 ITImageView *imageView;
123 BOOL shouldAnimate = ( ! (([self visibilityState] == ITWindowAppearingState) ||
124 ([self visibilityState] == ITWindowVanishingState)) );
126 if ( _sizing == ITTransientStatusWindowSmall ) {
127 divisor = SMALL_DIVISOR;
128 } else if ( _sizing == ITTransientStatusWindowMini ) {
129 divisor = MINI_DIVISOR;
132 // Get image width and height.
133 imageWidth = ( [_image size].width / divisor );
134 imageHeight = ( [_image size].height / divisor );
136 // Set the content height to the greater of the text and image heights.
137 contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight );
139 // Setup the Window, and remove all its contentview's subviews.
140 windowWidth = ( (SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor) + dataWidth + (SW_PAD / divisor) );
141 windowHeight = ( (SW_PAD / divisor) + contentHeight + (SW_PAD / divisor) );
143 // Constrain size to max limits. Adjust data sizes accordingly.
144 excessWidth = (windowWidth - maxWidth );
145 excessHeight = (windowHeight - maxHeight);
147 if ( excessWidth > 0.0 ) {
148 windowWidth = maxWidth;
149 dataWidth -= excessWidth;
152 if ( excessHeight > 0.0 ) {
153 windowHeight = maxHeight;
154 dataHeight -= excessHeight;
157 if ( [self horizontalPosition] == ITWindowPositionLeft ) {
158 windowOrigin.x = ( SW_BORDER + screenOrigin.x );
159 } else if ( [self horizontalPosition] == ITWindowPositionCenter ) {
160 windowOrigin.x = ( screenOrigin.x + (screenWidth / 2) - (windowWidth / 2) );
161 } else if ( [self horizontalPosition] == ITWindowPositionRight ) {
162 windowOrigin.x = ( screenOrigin.x + screenWidth - (windowWidth + SW_BORDER) );
165 if ( [self verticalPosition] == ITWindowPositionTop ) {
166 windowOrigin.y = ( screenOrigin.y + screenHeight - (windowHeight + SW_BORDER) );
167 } else if ( [self verticalPosition] == ITWindowPositionMiddle ) {
168 // Middle-oriented windows should be slightly proud of the screen's middle.
169 windowOrigin.y = ( (screenOrigin.y + (screenHeight / 2) - (windowHeight / 2)) + (screenHeight / 8) );
170 } else if ( [self verticalPosition] == ITWindowPositionBottom ) {
171 windowOrigin.y = ( SW_BORDER + screenOrigin.y );
174 [self setFrame:NSMakeRect( windowOrigin.x,
177 windowHeight) display:YES animate:shouldAnimate];
179 [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
181 // Setup, position, fill, and add the image view to the content view.
182 imageRect = NSMakeRect( (SW_PAD / divisor) + 4,
183 ((SW_PAD / divisor) + ((contentHeight - imageHeight) / 2)),
186 imageView = [[[ITImageView alloc] initWithFrame:imageRect] autorelease];
187 [imageView setAutoresizingMask:(NSViewMinYMargin | NSViewMaxYMargin)];
188 [imageView setImage:_image];
189 [imageView setCastsShadow:YES];
190 [[self contentView] addSubview:imageView];
192 return NSMakeRect( ((SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor)),
193 ((SW_PAD / divisor) + ((contentHeight - dataHeight) / 2)),
198 - (void)buildTextWindowWithString:(NSString *)text
203 float dataWidth = 0.0;
204 float dataHeight = 0.0;
206 NSArray *lines = [text componentsSeparatedByString:@"\n"];
208 NSEnumerator *lineEnum = [lines objectEnumerator];
209 float baseFontSize = 18.0;
210 ITTextField *textField;
214 if ( _sizing == ITTransientStatusWindowSmall ) {
215 divisor = SMALL_DIVISOR;
216 } else if ( _sizing == ITTransientStatusWindowMini ) {
217 divisor = MINI_DIVISOR;
220 font = [NSFont fontWithName:@"LucidaGrande-Bold" size:(baseFontSize / divisor)];
221 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
223 // Iterate over each line to get text width and height
224 while ( (oneLine = [lineEnum nextObject]) ) {
225 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
226 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
227 // Add the height of this line to the total text height
228 dataHeight += [oneLine sizeWithAttributes:attr].height;
229 // If this line wider than the last one, set it as the text width.
230 dataWidth = ( ( dataWidth > oneLineWidth ) ? dataWidth : oneLineWidth );
233 // Add 4.0 to the final dataHeight to accomodate the shadow.
236 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
238 // Create, position, setup, fill, and add the text view to the content view.
239 textField = [[[ITTextField alloc] initWithFrame:dataRect] autorelease];
240 [textField setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
241 [textField setEditable:NO];
242 [textField setSelectable:NO];
243 [textField setBordered:NO];
244 [textField setDrawsBackground:NO];
245 [textField setFont:font];
246 [textField setTextColor:[NSColor whiteColor]];
247 [textField setCastsShadow:YES];
248 [[textField cell] setWraps:NO];
249 [textField setStringValue:text];
250 [textField setShadowSaturation:SW_SHADOW_SAT];
251 [[self contentView] addSubview:textField];
253 // Display the window.
254 [[self contentView] setNeedsDisplay:YES];
259 - (void)buildMeterWindowWithCharacter:(NSString *)character
274 NSEnumerator *cellEnum = nil;
277 NSColor *onColor = [NSColor whiteColor];
278 NSColor *offColor = [NSColor colorWithCalibratedWhite:0.15 alpha:0.50];
281 if ( _sizing == ITTransientStatusWindowSmall ) {
282 divisor = SMALL_DIVISOR;
283 } else if ( _sizing == ITTransientStatusWindowMini ) {
284 divisor = MINI_DIVISOR;
287 font = [NSFont fontWithName:@"LucidaGrande-Bold" size:( size / divisor )];
288 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
289 charSize = [character sizeWithAttributes:attr];
290 cellHeight = ( charSize.height + 4.0 ); // Add 4.0 for shadow
291 cellWidth = ( (charSize.width) + (SW_METER_PAD / divisor) );
292 dataWidth = ( cellWidth * count );
293 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, cellHeight)];
294 volMatrix = [[[NSMatrix alloc] initWithFrame:dataRect
295 mode:NSHighlightModeMatrix
296 cellClass:NSClassFromString(@"ITTextFieldCell")
298 numberOfColumns:count] autorelease];
300 [volMatrix setCellSize:NSMakeSize(cellWidth, cellHeight)];
301 [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
302 [volMatrix setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
304 cellEnum = [[volMatrix cells] objectEnumerator];
306 while ( (aCell = [cellEnum nextObject]) ) {
307 [aCell setEditable:NO];
308 [aCell setSelectable:NO];
309 [aCell setBordered:NO];
310 [aCell setDrawsBackground:NO];
311 [aCell setAlignment:NSCenterTextAlignment];
312 [aCell setFont:font];
313 [aCell setStringValue:character];
314 [aCell setShadowSaturation:SW_SHADOW_SAT];
318 if ( active >= activeCount ) {
319 [aCell setCastsShadow:YES];
320 [aCell setTextColor:onColor];
322 [aCell setCastsShadow:NO];
323 [aCell setTextColor:offColor];
328 [[self contentView] addSubview:volMatrix];
329 [[self contentView] setNeedsDisplay:YES];
334 - (void)buildDialogWindowWithMessage:(NSString *)message
335 defaultButton:(NSString *)defaultTitle
336 alternateButton:(NSString *)alternateTitle
338 defaultAction:(SEL)okAction
339 alternateAction:(SEL)alternateAction
344 float textWidth = 0.0;
345 float textHeight = 0.0;
347 float cancelWidth = 0.0;
348 float wideButtonW = 0.0;
349 float buttonWidth = 0.0;
350 float dataHeight = 0.0;
351 float dataWidth = 0.0;
355 float textAddBelow = 32.0;
356 float dataMinH = 92.0;
357 float textMinH = 48.0;
358 NSArray *lines = [message componentsSeparatedByString:@"\n"];
360 NSEnumerator *lineEnum = [lines objectEnumerator];
361 float baseFontSize = 18.0;
362 ITTextField *textField;
364 ITButton *cancelButton;
365 NSColor *textColor = [NSColor whiteColor];
369 NSDictionary *buttonAttr;
371 if ( _sizing == ITTransientStatusWindowSmall ) {
372 divisor = SMALL_DIVISOR;
373 } else if ( _sizing == ITTransientStatusWindowMini ) {
374 divisor = MINI_DIVISOR;
377 font = [NSFont fontWithName:@"LucidaGrande-Bold" size:(baseFontSize / divisor)];
378 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
379 buttonFont = [NSFont fontWithName:@"LucidaGrande-Bold" size:(14 / divisor)];
380 buttonAttr = [NSDictionary dictionaryWithObjectsAndKeys:
381 buttonFont , NSFontAttributeName,
382 textColor , NSForegroundColorAttributeName,
385 // Iterate over each line to get text width and height
386 while ( (oneLine = [lineEnum nextObject]) ) {
387 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
388 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
389 // Add the height of this line to the total text height
390 textHeight += [oneLine sizeWithAttributes:attr].height;
391 // If this line wider than the last one, set it as the text width.
392 textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
395 // Add 4.0 to the final dataHeight to accomodate the shadow.
398 // Add extra padding below the text
399 dataHeight = (textHeight + textAddBelow);
401 // Test to see if data height is tall enough
402 if ( dataHeight < dataMinH ) {
403 dataHeight = dataMinH;
406 // Make the buttons, set the titles, and size them to fit their titles
407 okButton = [[[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)] autorelease];
408 cancelButton = [[[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)] autorelease];
409 [okButton setTarget:target];
410 [cancelButton setTarget:target];
411 [okButton setAction:okAction];
412 [cancelButton setAction:alternateAction];
413 [okButton setBezelStyle:ITGrayRoundedBezelStyle];
414 [cancelButton setBezelStyle:ITGrayRoundedBezelStyle];
415 [okButton setAlignment:NSRightTextAlignment];
416 [cancelButton setAlignment:NSCenterTextAlignment];
417 [okButton setImagePosition:NSNoImage];
418 [cancelButton setImagePosition:NSNoImage];
419 [okButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:defaultTitle
420 attributes:buttonAttr] autorelease]];
421 [cancelButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:alternateTitle
422 attributes:buttonAttr] autorelease]];
423 [okButton sizeToFit];
424 [cancelButton sizeToFit];
426 // Get the button widths. Add any extra width here.
427 okWidth = ([okButton frame].size.width + SW_BUTTON_EXTRA_W);
428 cancelWidth = ([cancelButton frame].size.width + SW_BUTTON_EXTRA_W);
430 // Figure out which button is wider.
431 wideButtonW = ( (okWidth > cancelWidth) ? okWidth : cancelWidth );
433 // Get the total width of the buttons. Add the divider space.
434 buttonWidth = ( (wideButtonW * 2) + SW_BUTTON_DIV );
436 // Set the dataWidth to whichever is greater: text width or button width.
437 dataWidth = ( (textWidth > buttonWidth) ? textWidth : buttonWidth);
440 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
442 // Set an initial vertical point for the textRect's origin.
443 textY = dataRect.origin.y + textAddBelow;
445 // Move that point up if the minimimum height of the text area is not occupied.
446 if ( textHeight < textMinH ) {
447 textY += ( (textMinH - textHeight) / 2 );
450 // Build the text rect.
451 textRect = NSMakeRect(dataRect.origin.x,
456 // Create, position, setup, fill, and add the text view to the content view.
457 textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
458 [textField setEditable:NO];
459 [textField setSelectable:NO];
460 [textField setBordered:NO];
461 [textField setDrawsBackground:NO];
462 [textField setFont:font];
463 [textField setTextColor:textColor];
464 [textField setCastsShadow:YES];
465 [textField setStringValue:message];
466 [textField setShadowSaturation:SW_SHADOW_SAT];
467 [[self contentView] addSubview:textField];
469 // Set the button frames, and add them to the content view.
470 [okButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - (wideButtonW + SW_BUTTON_PAD_R) ),
474 [cancelButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - ((wideButtonW * 2) + SW_BUTTON_DIV + SW_BUTTON_PAD_R) ),
478 [[self contentView] addSubview:okButton];
479 if (alternateTitle) {
480 [[self contentView] addSubview:cancelButton];
483 [self setIgnoresMouseEvents:NO];
485 // Display the window.
486 [[self contentView] setNeedsDisplay:YES];
490 - (NSTimeInterval)animationResizeTime:(NSRect)newFrame
492 return (NSTimeInterval)0.25;