5 // Created by Matt L. Judy on Sat Feb 22 2003.
6 // Copyright (c) 2003 NibFile.com. All rights reserved.
9 #import "StatusWindow.h"
12 @interface StatusWindow (Private)
13 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize;
17 @implementation StatusWindow
20 /*************************************************************************/
22 #pragma mark INITIALIZATION / DEALLOCATION METHODS
23 /*************************************************************************/
25 - (id)initWithContentView:(NSView *)contentView
26 exitMode:(ITTransientStatusWindowExitMode)exitMode
27 backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType
29 if ( ( self = [super initWithContentView:contentView
31 backgroundType:backgroundType] ) ) {
32 // Set default values.
33 _image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
47 /*************************************************************************/
49 #pragma mark ACCESSOR METHODS
50 /*************************************************************************/
52 - (void)setImage:(NSImage *)newImage
55 _image = [newImage copy];
58 - (void)setLocked:(BOOL)flag
61 [self setExitMode:(flag ? ITTransientStatusWindowExitOnCommand : ITTransientStatusWindowExitAfterDelay)];
65 /*************************************************************************/
67 #pragma mark INSTANCE METHODS
68 /*************************************************************************/
70 - (void)appear:(id)sender
73 [super appear:sender];
77 - (void)vanish:(id)sender
80 [super vanish:sender];
84 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize
87 float imageWidth = 0.0;
88 float imageHeight = 0.0;
89 float dataWidth = dataSize.width;
90 float dataHeight = dataSize.height;
91 float contentHeight = 0.0;
92 float windowWidth = 0.0;
93 float windowHeight = 0.0;
94 NSImageView *imageView;
96 // Get image width and height.
97 imageWidth = [_image size].width;
98 imageHeight = [_image size].height;
100 // Set the content height to the greater of the text and image heights.
101 contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight );
103 // Setup the Window, and remove all its contentview's subviews.
104 windowWidth = ( SW_PAD + imageWidth + SW_SPACE + dataWidth + SW_PAD );
105 windowHeight = ( SW_PAD + contentHeight + SW_PAD );
106 [self setFrame:NSMakeRect( (SW_BORDER + [[self screen] visibleFrame].origin.x),
107 (SW_BORDER + [[self screen] visibleFrame].origin.y),
109 windowHeight) display:YES];
110 [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
112 // Setup, position, fill, and add the image view to the content view.
113 imageRect = NSMakeRect( SW_PAD,
114 (SW_PAD + ((contentHeight - imageHeight) / 2)),
117 imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
118 [imageView setImage:_image];
119 [[self contentView] addSubview:imageView];
121 return NSMakeRect( (SW_PAD + imageWidth + SW_SPACE),
122 (SW_PAD + ((contentHeight - dataHeight) / 2)),
127 - (void)buildTextWindowWithString:(NSString *)text
131 float dataWidth = 0.0;
132 float dataHeight = 0.0;
134 NSArray *lines = [text componentsSeparatedByString:@"\n"];
136 NSEnumerator *lineEnum = [lines objectEnumerator];
137 NSFont *font = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
138 NSDictionary *attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
139 ITTextField *textField;
141 // Iterate over each line to get text width and height
142 while ( (oneLine = [lineEnum nextObject]) ) {
143 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
144 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
145 // Add the height of this line to the total text height
146 dataHeight += [oneLine sizeWithAttributes:attr].height;
147 // If this line wider than the last one, set it as the text width.
148 dataWidth = ( ( dataWidth > oneLineWidth ) ? dataWidth : oneLineWidth );
151 // Add 4.0 to the final dataHeight to accomodate the shadow.
154 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
156 // Create, position, setup, fill, and add the text view to the content view.
157 textField = [[[ITTextField alloc] initWithFrame:dataRect] autorelease];
158 [textField setEditable:NO];
159 [textField setSelectable:NO];
160 [textField setBordered:NO];
161 [textField setDrawsBackground:NO];
162 [textField setFont:font];
163 [textField setTextColor:[NSColor whiteColor]];
164 [textField setCastsShadow:YES];
165 [textField setStringValue:text];
166 [textField setShadowSaturation:SW_SHADOW_SAT];
167 [[self contentView] addSubview:textField];
169 // Display the window.
170 [[self contentView] setNeedsDisplay:YES];
175 - (void)buildMeterWindowWithCharacter:(NSString *)character
182 NSFont *font = [NSFont fontWithName:@"Lucida Grande Bold" size:size];
183 NSDictionary *attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
184 NSSize charSize = [character sizeWithAttributes:attr];
185 float cellHeight = ( charSize.height + 4.0 ); // Add 4.0 for shadow
186 float cellWidth = ( (charSize.width) + SW_METER_PAD ); // Add 8.0 for Apple suck
187 float dataWidth = ( cellWidth * count );
188 NSRect dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, cellHeight)];
189 NSEnumerator *cellEnum = nil;
192 NSColor *onColor = [NSColor whiteColor];
193 NSColor *offColor = [NSColor colorWithCalibratedWhite:0.15 alpha:0.50];
194 NSMatrix *volMatrix = [[[NSMatrix alloc] initWithFrame:dataRect
195 mode:NSHighlightModeMatrix
196 cellClass:NSClassFromString(@"ITTextFieldCell")
198 numberOfColumns:count] autorelease];
200 [volMatrix setCellSize:NSMakeSize(cellWidth, cellHeight)];
201 [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
203 cellEnum = [[volMatrix cells] objectEnumerator];
205 while ( (aCell = [cellEnum nextObject]) ) {
206 [aCell setEditable:NO];
207 [aCell setSelectable:NO];
208 [aCell setBordered:NO];
209 [aCell setDrawsBackground:NO];
210 [aCell setAlignment:NSCenterTextAlignment];
211 [aCell setFont:font];
212 [aCell setStringValue:character];
213 [aCell setShadowSaturation:SW_SHADOW_SAT];
217 if ( active >= activeCount ) {
218 [aCell setCastsShadow:YES];
219 [aCell setTextColor:onColor];
221 [aCell setCastsShadow:NO];
222 [aCell setTextColor:offColor];
227 [[self contentView] addSubview:volMatrix];
228 [[self contentView] setNeedsDisplay:YES];
233 - (void)buildDialogWindowWithMessage:(NSString *)message
234 defaultButton:(NSString *)defaultTitle
235 alternateButton:(NSString *)alternateTitle
237 defaultAction:(SEL)okAction
238 alternateAction:(SEL)alternateAction
242 float textWidth = 0.0;
243 float textHeight = 0.0;
245 float cancelWidth = 0.0;
246 float wideButtonW = 0.0;
247 float buttonWidth = 0.0;
248 float dataHeight = 0.0;
249 float dataWidth = 0.0;
253 float textAddBelow = 32.0;
254 float dataMinH = 92.0;
255 float textMinH = 48.0;
256 NSArray *lines = [message componentsSeparatedByString:@"\n"];
258 NSEnumerator *lineEnum = [lines objectEnumerator];
259 ITTextField *textField;
261 ITButton *cancelButton;
262 NSColor *textColor = [NSColor whiteColor];
263 NSFont *font = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
264 NSDictionary *attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
265 NSFont *buttonFont = [NSFont fontWithName:@"Lucida Grande Bold" size:14];
266 NSDictionary *buttonAttr = [NSDictionary dictionaryWithObjectsAndKeys:
267 buttonFont , NSFontAttributeName,
268 textColor , NSForegroundColorAttributeName,
271 // Iterate over each line to get text width and height
272 while ( (oneLine = [lineEnum nextObject]) ) {
273 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
274 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
275 // Add the height of this line to the total text height
276 textHeight += [oneLine sizeWithAttributes:attr].height;
277 // If this line wider than the last one, set it as the text width.
278 textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
281 // Add 4.0 to the final dataHeight to accomodate the shadow.
284 // Add extra padding below the text
285 dataHeight = (textHeight + textAddBelow);
287 // Test to see if data height is tall enough
288 if ( dataHeight < dataMinH ) {
289 dataHeight = dataMinH;
292 // Make the buttons, set the titles, and size them to fit their titles
293 okButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
294 cancelButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
295 [okButton setTarget:target];
296 [cancelButton setTarget:target];
297 [okButton setAction:okAction];
298 [cancelButton setAction:alternateAction];
299 [okButton setBezelStyle:ITGrayRoundedBezelStyle];
300 [cancelButton setBezelStyle:ITGrayRoundedBezelStyle];
301 [okButton setAlignment:NSRightTextAlignment];
302 [cancelButton setAlignment:NSCenterTextAlignment];
303 [okButton setImagePosition:NSNoImage];
304 [cancelButton setImagePosition:NSNoImage];
305 [okButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:defaultTitle
306 attributes:buttonAttr] autorelease]];
307 [cancelButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:alternateTitle
308 attributes:buttonAttr] autorelease]];
309 [okButton sizeToFit];
310 [cancelButton sizeToFit];
312 // Get the button widths. Add any extra width here.
313 okWidth = ([okButton frame].size.width + SW_BUTTON_EXTRA_W);
314 cancelWidth = ([cancelButton frame].size.width + SW_BUTTON_EXTRA_W);
316 // Figure out which button is wider.
317 wideButtonW = ( (okWidth > cancelWidth) ? okWidth : cancelWidth );
319 // Get the total width of the buttons. Add the divider space.
320 buttonWidth = ( (wideButtonW * 2) + SW_BUTTON_DIV );
322 // Set the dataWidth to whichever is greater: text width or button width.
323 dataWidth = ( (textWidth > buttonWidth) ? textWidth : buttonWidth);
326 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
328 // Set an initial vertical point for the textRect's origin.
329 textY = dataRect.origin.y + textAddBelow;
331 // Move that point up if the minimimum height of the text area is not occupied.
332 if ( textHeight < textMinH ) {
333 textY += ( (textMinH - textHeight) / 2 );
336 // Build the text rect.
337 textRect = NSMakeRect(dataRect.origin.x,
342 // Create, position, setup, fill, and add the text view to the content view.
343 textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
344 [textField setEditable:NO];
345 [textField setSelectable:NO];
346 [textField setBordered:NO];
347 [textField setDrawsBackground:NO];
348 [textField setFont:font];
349 [textField setTextColor:textColor];
350 [textField setCastsShadow:YES];
351 [textField setStringValue:message];
352 [textField setShadowSaturation:SW_SHADOW_SAT];
353 [[self contentView] addSubview:textField];
355 // Set the button frames, and add them to the content view.
356 [okButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - (wideButtonW + SW_BUTTON_PAD_R) ),
360 [cancelButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - ((wideButtonW * 2) + SW_BUTTON_DIV + SW_BUTTON_PAD_R) ),
364 [[self contentView] addSubview:okButton];
365 [[self contentView] addSubview:cancelButton];
367 [self setIgnoresMouseEvents:NO];
369 // Display the window.
370 [[self contentView] setNeedsDisplay:YES];