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) );
133 #warning Adjust frame for positioning here.
135 [self setFrame:NSMakeRect( (SW_BORDER + [[self screen] visibleFrame].origin.x),
136 (SW_BORDER + [[self screen] visibleFrame].origin.y),
138 windowHeight) display:YES animate:YES];
139 [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
141 // Setup, position, fill, and add the image view to the content view.
142 imageRect = NSMakeRect( (SW_PAD / divisor),
143 ((SW_PAD / divisor) + ((contentHeight - imageHeight) / 2)),
146 imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
147 [imageView setImage:_image];
148 [[self contentView] addSubview:imageView];
150 return NSMakeRect( ((SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor)),
151 ((SW_PAD / divisor) + ((contentHeight - dataHeight) / 2)),
156 - (void)buildTextWindowWithString:(NSString *)text
161 float dataWidth = 0.0;
162 float dataHeight = 0.0;
164 NSArray *lines = [text componentsSeparatedByString:@"\n"];
166 NSEnumerator *lineEnum = [lines objectEnumerator];
167 float baseFontSize = 18.0;
168 ITTextField *textField;
172 if ( _sizing == StatusWindowSmall ) {
173 divisor = SMALL_DIVISOR;
174 } else if ( _sizing == StatusWindowMini ) {
175 divisor = MINI_DIVISOR;
178 font = [NSFont fontWithName:@"Lucida Grande Bold" size:(baseFontSize / divisor)];
179 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
181 // Iterate over each line to get text width and height
182 while ( (oneLine = [lineEnum nextObject]) ) {
183 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
184 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
185 // Add the height of this line to the total text height
186 dataHeight += [oneLine sizeWithAttributes:attr].height;
187 // If this line wider than the last one, set it as the text width.
188 dataWidth = ( ( dataWidth > oneLineWidth ) ? dataWidth : oneLineWidth );
191 // Add 4.0 to the final dataHeight to accomodate the shadow.
194 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
196 // Create, position, setup, fill, and add the text view to the content view.
197 textField = [[[ITTextField alloc] initWithFrame:dataRect] autorelease];
198 [textField setEditable:NO];
199 [textField setSelectable:NO];
200 [textField setBordered:NO];
201 [textField setDrawsBackground:NO];
202 [textField setFont:font];
203 [textField setTextColor:[NSColor whiteColor]];
204 [textField setCastsShadow:YES];
205 [textField setStringValue:text];
206 [textField setShadowSaturation:SW_SHADOW_SAT];
207 [[self contentView] addSubview:textField];
209 // Display the window.
210 [[self contentView] setNeedsDisplay:YES];
215 - (void)buildMeterWindowWithCharacter:(NSString *)character
230 NSEnumerator *cellEnum = nil;
233 NSColor *onColor = [NSColor whiteColor];
234 NSColor *offColor = [NSColor colorWithCalibratedWhite:0.15 alpha:0.50];
237 if ( _sizing == StatusWindowSmall ) {
238 divisor = SMALL_DIVISOR;
239 } else if ( _sizing == StatusWindowMini ) {
240 divisor = MINI_DIVISOR;
243 font = [NSFont fontWithName:@"Lucida Grande Bold" size:( size / divisor )];
244 attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
245 charSize = [character sizeWithAttributes:attr];
246 cellHeight = ( charSize.height + 4.0 ); // Add 4.0 for shadow
247 cellWidth = ( (charSize.width) + (SW_METER_PAD / divisor) );
248 dataWidth = ( cellWidth * count );
249 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, cellHeight)];
250 volMatrix = [[[NSMatrix alloc] initWithFrame:dataRect
251 mode:NSHighlightModeMatrix
252 cellClass:NSClassFromString(@"ITTextFieldCell")
254 numberOfColumns:count] autorelease];
256 [volMatrix setCellSize:NSMakeSize(cellWidth, cellHeight)];
257 [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
259 cellEnum = [[volMatrix cells] objectEnumerator];
261 while ( (aCell = [cellEnum nextObject]) ) {
262 [aCell setEditable:NO];
263 [aCell setSelectable:NO];
264 [aCell setBordered:NO];
265 [aCell setDrawsBackground:NO];
266 [aCell setAlignment:NSCenterTextAlignment];
267 [aCell setFont:font];
268 [aCell setStringValue:character];
269 [aCell setShadowSaturation:SW_SHADOW_SAT];
273 if ( active >= activeCount ) {
274 [aCell setCastsShadow:YES];
275 [aCell setTextColor:onColor];
277 [aCell setCastsShadow:NO];
278 [aCell setTextColor:offColor];
283 [[self contentView] addSubview:volMatrix];
284 [[self contentView] setNeedsDisplay:YES];
289 - (void)buildDialogWindowWithMessage:(NSString *)message
290 defaultButton:(NSString *)defaultTitle
291 alternateButton:(NSString *)alternateTitle
293 defaultAction:(SEL)okAction
294 alternateAction:(SEL)alternateAction
298 float textWidth = 0.0;
299 float textHeight = 0.0;
301 float cancelWidth = 0.0;
302 float wideButtonW = 0.0;
303 float buttonWidth = 0.0;
304 float dataHeight = 0.0;
305 float dataWidth = 0.0;
309 float textAddBelow = 32.0;
310 float dataMinH = 92.0;
311 float textMinH = 48.0;
312 NSArray *lines = [message componentsSeparatedByString:@"\n"];
314 NSEnumerator *lineEnum = [lines objectEnumerator];
315 ITTextField *textField;
317 ITButton *cancelButton;
318 NSColor *textColor = [NSColor whiteColor];
319 NSFont *font = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
320 NSDictionary *attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
321 NSFont *buttonFont = [NSFont fontWithName:@"Lucida Grande Bold" size:14];
322 NSDictionary *buttonAttr = [NSDictionary dictionaryWithObjectsAndKeys:
323 buttonFont , NSFontAttributeName,
324 textColor , NSForegroundColorAttributeName,
327 // Iterate over each line to get text width and height
328 while ( (oneLine = [lineEnum nextObject]) ) {
329 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
330 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
331 // Add the height of this line to the total text height
332 textHeight += [oneLine sizeWithAttributes:attr].height;
333 // If this line wider than the last one, set it as the text width.
334 textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
337 // Add 4.0 to the final dataHeight to accomodate the shadow.
340 // Add extra padding below the text
341 dataHeight = (textHeight + textAddBelow);
343 // Test to see if data height is tall enough
344 if ( dataHeight < dataMinH ) {
345 dataHeight = dataMinH;
348 // Make the buttons, set the titles, and size them to fit their titles
349 okButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
350 cancelButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
351 [okButton setTarget:target];
352 [cancelButton setTarget:target];
353 [okButton setAction:okAction];
354 [cancelButton setAction:alternateAction];
355 [okButton setBezelStyle:ITGrayRoundedBezelStyle];
356 [cancelButton setBezelStyle:ITGrayRoundedBezelStyle];
357 [okButton setAlignment:NSRightTextAlignment];
358 [cancelButton setAlignment:NSCenterTextAlignment];
359 [okButton setImagePosition:NSNoImage];
360 [cancelButton setImagePosition:NSNoImage];
361 [okButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:defaultTitle
362 attributes:buttonAttr] autorelease]];
363 [cancelButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:alternateTitle
364 attributes:buttonAttr] autorelease]];
365 [okButton sizeToFit];
366 [cancelButton sizeToFit];
368 // Get the button widths. Add any extra width here.
369 okWidth = ([okButton frame].size.width + SW_BUTTON_EXTRA_W);
370 cancelWidth = ([cancelButton frame].size.width + SW_BUTTON_EXTRA_W);
372 // Figure out which button is wider.
373 wideButtonW = ( (okWidth > cancelWidth) ? okWidth : cancelWidth );
375 // Get the total width of the buttons. Add the divider space.
376 buttonWidth = ( (wideButtonW * 2) + SW_BUTTON_DIV );
378 // Set the dataWidth to whichever is greater: text width or button width.
379 dataWidth = ( (textWidth > buttonWidth) ? textWidth : buttonWidth);
382 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
384 // Set an initial vertical point for the textRect's origin.
385 textY = dataRect.origin.y + textAddBelow;
387 // Move that point up if the minimimum height of the text area is not occupied.
388 if ( textHeight < textMinH ) {
389 textY += ( (textMinH - textHeight) / 2 );
392 // Build the text rect.
393 textRect = NSMakeRect(dataRect.origin.x,
398 // Create, position, setup, fill, and add the text view to the content view.
399 textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
400 [textField setEditable:NO];
401 [textField setSelectable:NO];
402 [textField setBordered:NO];
403 [textField setDrawsBackground:NO];
404 [textField setFont:font];
405 [textField setTextColor:textColor];
406 [textField setCastsShadow:YES];
407 [textField setStringValue:message];
408 [textField setShadowSaturation:SW_SHADOW_SAT];
409 [[self contentView] addSubview:textField];
411 // Set the button frames, and add them to the content view.
412 [okButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - (wideButtonW + SW_BUTTON_PAD_R) ),
416 [cancelButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - ((wideButtonW * 2) + SW_BUTTON_DIV + SW_BUTTON_PAD_R) ),
420 [[self contentView] addSubview:okButton];
421 [[self contentView] addSubview:cancelButton];
423 [self setIgnoresMouseEvents:NO];
425 // Display the window.
426 [[self contentView] setNeedsDisplay:YES];