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 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;
125 if ( _sizing == StatusWindowSmall ) {
126 divisor = SMALL_DIVISOR;
127 } else if ( _sizing == StatusWindowMini ) {
128 divisor = MINI_DIVISOR;
131 // Get image width and height.
132 imageWidth = ( [_image size].width / divisor );
133 imageHeight = ( [_image size].height / divisor );
135 // Set the content height to the greater of the text and image heights.
136 contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight );
138 // Setup the Window, and remove all its contentview's subviews.
139 windowWidth = ( (SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor) + dataWidth + (SW_PAD / divisor) );
140 windowHeight = ( (SW_PAD / divisor) + contentHeight + (SW_PAD / divisor) );
142 // Constrain size to max limits. Adjust data sizes accordingly.
143 excessWidth = (windowWidth - maxWidth );
144 excessHeight = (windowHeight - maxHeight);
146 if ( excessWidth > 0.0 ) {
147 windowWidth = maxWidth;
148 dataWidth -= excessWidth;
151 if ( excessHeight > 0.0 ) {
152 windowHeight = maxHeight;
153 dataHeight -= excessHeight;
156 if ( [self horizontalPosition] == ITWindowPositionLeft ) {
157 windowOrigin.x = ( SW_BORDER + screenOrigin.x );
158 } else if ( [self horizontalPosition] == ITWindowPositionCenter ) {
159 windowOrigin.x = ( screenOrigin.x + (screenWidth / 2) - (windowWidth / 2) );
160 } else if ( [self horizontalPosition] == ITWindowPositionRight ) {
161 windowOrigin.x = ( screenOrigin.x + screenWidth - (windowWidth + SW_BORDER) );
164 if ( [self verticalPosition] == ITWindowPositionTop ) {
165 windowOrigin.y = ( screenOrigin.y + screenHeight - (windowHeight + SW_BORDER) );
166 } else if ( [self verticalPosition] == ITWindowPositionMiddle ) {
167 // Middle-oriented windows should be slightly proud of the screen's middle.
168 windowOrigin.y = ( (screenOrigin.y + (screenHeight / 2) - (windowHeight / 2)) + (screenHeight / 8) );
169 } else if ( [self verticalPosition] == ITWindowPositionBottom ) {
170 windowOrigin.y = ( SW_BORDER + screenOrigin.y );
173 [self setFrame:NSMakeRect( windowOrigin.x,
176 windowHeight) display:YES animate:YES];
178 [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
180 // Setup, position, fill, and add the image view to the content view.
181 imageRect = NSMakeRect( (SW_PAD / divisor),
182 ((SW_PAD / divisor) + ((contentHeight - imageHeight) / 2)),
185 imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
186 [imageView setAutoresizingMask:(NSViewMinYMargin | NSViewMaxYMargin)];
187 [imageView setImage:_image];
188 [[self contentView] addSubview:imageView];
190 return NSMakeRect( ((SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor)),
191 ((SW_PAD / divisor) + ((contentHeight - dataHeight) / 2)),
196 - (void)buildTextWindowWithString:(NSString *)text
201 float dataWidth = 0.0;
202 float dataHeight = 0.0;
204 NSArray *lines = [text componentsSeparatedByString:@"\n"];
206 NSEnumerator *lineEnum = [lines objectEnumerator];
207 float baseFontSize = 18.0;
208 ITTextField *textField;
212 if ( _sizing == StatusWindowSmall ) {
213 divisor = SMALL_DIVISOR;
214 } else if ( _sizing == StatusWindowMini ) {
215 divisor = MINI_DIVISOR;
218 font = [NSFont fontWithName:@"Lucida Grande Bold" size:(baseFontSize / divisor)];
219 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
221 // Iterate over each line to get text width and height
222 while ( (oneLine = [lineEnum nextObject]) ) {
223 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
224 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
225 // Add the height of this line to the total text height
226 dataHeight += [oneLine sizeWithAttributes:attr].height;
227 // If this line wider than the last one, set it as the text width.
228 dataWidth = ( ( dataWidth > oneLineWidth ) ? dataWidth : oneLineWidth );
231 // Add 4.0 to the final dataHeight to accomodate the shadow.
234 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
236 // Create, position, setup, fill, and add the text view to the content view.
237 textField = [[[ITTextField alloc] initWithFrame:dataRect] autorelease];
238 [textField setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
239 [textField setEditable:NO];
240 [textField setSelectable:NO];
241 [textField setBordered:NO];
242 [textField setDrawsBackground:NO];
243 [textField setFont:font];
244 [textField setTextColor:[NSColor whiteColor]];
245 [textField setCastsShadow:YES];
246 [[textField cell] setWraps:NO];
247 [textField setStringValue:text];
248 [textField setShadowSaturation:SW_SHADOW_SAT];
249 [[self contentView] addSubview:textField];
251 // Display the window.
252 [[self contentView] setNeedsDisplay:YES];
257 - (void)buildMeterWindowWithCharacter:(NSString *)character
272 NSEnumerator *cellEnum = nil;
275 NSColor *onColor = [NSColor whiteColor];
276 NSColor *offColor = [NSColor colorWithCalibratedWhite:0.15 alpha:0.50];
279 if ( _sizing == StatusWindowSmall ) {
280 divisor = SMALL_DIVISOR;
281 } else if ( _sizing == StatusWindowMini ) {
282 divisor = MINI_DIVISOR;
285 font = [NSFont fontWithName:@"Lucida Grande Bold" size:( size / divisor )];
286 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
287 charSize = [character sizeWithAttributes:attr];
288 cellHeight = ( charSize.height + 4.0 ); // Add 4.0 for shadow
289 cellWidth = ( (charSize.width) + (SW_METER_PAD / divisor) );
290 dataWidth = ( cellWidth * count );
291 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, cellHeight)];
292 volMatrix = [[[NSMatrix alloc] initWithFrame:dataRect
293 mode:NSHighlightModeMatrix
294 cellClass:NSClassFromString(@"ITTextFieldCell")
296 numberOfColumns:count] autorelease];
298 [volMatrix setCellSize:NSMakeSize(cellWidth, cellHeight)];
299 [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
300 [volMatrix setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
302 cellEnum = [[volMatrix cells] objectEnumerator];
304 while ( (aCell = [cellEnum nextObject]) ) {
305 [aCell setEditable:NO];
306 [aCell setSelectable:NO];
307 [aCell setBordered:NO];
308 [aCell setDrawsBackground:NO];
309 [aCell setAlignment:NSCenterTextAlignment];
310 [aCell setFont:font];
311 [aCell setStringValue:character];
312 [aCell setShadowSaturation:SW_SHADOW_SAT];
316 if ( active >= activeCount ) {
317 [aCell setCastsShadow:YES];
318 [aCell setTextColor:onColor];
320 [aCell setCastsShadow:NO];
321 [aCell setTextColor:offColor];
326 [[self contentView] addSubview:volMatrix];
327 [[self contentView] setNeedsDisplay:YES];
332 - (void)buildDialogWindowWithMessage:(NSString *)message
333 defaultButton:(NSString *)defaultTitle
334 alternateButton:(NSString *)alternateTitle
336 defaultAction:(SEL)okAction
337 alternateAction:(SEL)alternateAction
341 float textWidth = 0.0;
342 float textHeight = 0.0;
344 float cancelWidth = 0.0;
345 float wideButtonW = 0.0;
346 float buttonWidth = 0.0;
347 float dataHeight = 0.0;
348 float dataWidth = 0.0;
352 float textAddBelow = 32.0;
353 float dataMinH = 92.0;
354 float textMinH = 48.0;
355 NSArray *lines = [message componentsSeparatedByString:@"\n"];
357 NSEnumerator *lineEnum = [lines objectEnumerator];
358 ITTextField *textField;
360 ITButton *cancelButton;
361 NSColor *textColor = [NSColor whiteColor];
362 NSFont *font = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
363 NSDictionary *attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
364 NSFont *buttonFont = [NSFont fontWithName:@"Lucida Grande Bold" size:14];
365 NSDictionary *buttonAttr = [NSDictionary dictionaryWithObjectsAndKeys:
366 buttonFont , NSFontAttributeName,
367 textColor , NSForegroundColorAttributeName,
370 // Iterate over each line to get text width and height
371 while ( (oneLine = [lineEnum nextObject]) ) {
372 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
373 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
374 // Add the height of this line to the total text height
375 textHeight += [oneLine sizeWithAttributes:attr].height;
376 // If this line wider than the last one, set it as the text width.
377 textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
380 // Add 4.0 to the final dataHeight to accomodate the shadow.
383 // Add extra padding below the text
384 dataHeight = (textHeight + textAddBelow);
386 // Test to see if data height is tall enough
387 if ( dataHeight < dataMinH ) {
388 dataHeight = dataMinH;
391 // Make the buttons, set the titles, and size them to fit their titles
392 okButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
393 cancelButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
394 [okButton setTarget:target];
395 [cancelButton setTarget:target];
396 [okButton setAction:okAction];
397 [cancelButton setAction:alternateAction];
398 [okButton setBezelStyle:ITGrayRoundedBezelStyle];
399 [cancelButton setBezelStyle:ITGrayRoundedBezelStyle];
400 [okButton setAlignment:NSRightTextAlignment];
401 [cancelButton setAlignment:NSCenterTextAlignment];
402 [okButton setImagePosition:NSNoImage];
403 [cancelButton setImagePosition:NSNoImage];
404 [okButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:defaultTitle
405 attributes:buttonAttr] autorelease]];
406 [cancelButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:alternateTitle
407 attributes:buttonAttr] autorelease]];
408 [okButton sizeToFit];
409 [cancelButton sizeToFit];
411 // Get the button widths. Add any extra width here.
412 okWidth = ([okButton frame].size.width + SW_BUTTON_EXTRA_W);
413 cancelWidth = ([cancelButton frame].size.width + SW_BUTTON_EXTRA_W);
415 // Figure out which button is wider.
416 wideButtonW = ( (okWidth > cancelWidth) ? okWidth : cancelWidth );
418 // Get the total width of the buttons. Add the divider space.
419 buttonWidth = ( (wideButtonW * 2) + SW_BUTTON_DIV );
421 // Set the dataWidth to whichever is greater: text width or button width.
422 dataWidth = ( (textWidth > buttonWidth) ? textWidth : buttonWidth);
425 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
427 // Set an initial vertical point for the textRect's origin.
428 textY = dataRect.origin.y + textAddBelow;
430 // Move that point up if the minimimum height of the text area is not occupied.
431 if ( textHeight < textMinH ) {
432 textY += ( (textMinH - textHeight) / 2 );
435 // Build the text rect.
436 textRect = NSMakeRect(dataRect.origin.x,
441 // Create, position, setup, fill, and add the text view to the content view.
442 textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
443 [textField setEditable:NO];
444 [textField setSelectable:NO];
445 [textField setBordered:NO];
446 [textField setDrawsBackground:NO];
447 [textField setFont:font];
448 [textField setTextColor:textColor];
449 [textField setCastsShadow:YES];
450 [textField setStringValue:message];
451 [textField setShadowSaturation:SW_SHADOW_SAT];
452 [[self contentView] addSubview:textField];
454 // Set the button frames, and add them to the content view.
455 [okButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - (wideButtonW + SW_BUTTON_PAD_R) ),
459 [cancelButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - ((wideButtonW * 2) + SW_BUTTON_DIV + SW_BUTTON_PAD_R) ),
463 [[self contentView] addSubview:okButton];
464 [[self contentView] addSubview:cancelButton];
466 [self setIgnoresMouseEvents:NO];
468 // Display the window.
469 [[self contentView] setNeedsDisplay:YES];
473 - (NSTimeInterval)animationResizeTime:(NSRect)newFrame
475 return (NSTimeInterval)0.25;