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, SW_BORDER, windowWidth, windowHeight) display:YES];
107 [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
109 // Setup, position, fill, and add the image view to the content view.
110 imageRect = NSMakeRect( SW_PAD,
111 (SW_PAD + ((contentHeight - imageHeight) / 2)),
114 imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
115 [imageView setImage:_image];
116 [[self contentView] addSubview:imageView];
118 return NSMakeRect( (SW_PAD + imageWidth + SW_SPACE),
119 (SW_PAD + ((contentHeight - dataHeight) / 2)),
124 - (void)buildTextWindowWithString:(NSString *)text
128 float dataWidth = 0.0;
129 float dataHeight = 0.0;
131 NSArray *lines = [text componentsSeparatedByString:@"\n"];
133 NSEnumerator *lineEnum = [lines objectEnumerator];
134 NSFont *font = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
135 NSDictionary *attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
136 ITTextField *textField;
138 // Iterate over each line to get text width and height
139 while ( (oneLine = [lineEnum nextObject]) ) {
140 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
141 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
142 // Add the height of this line to the total text height
143 dataHeight += [oneLine sizeWithAttributes:attr].height;
144 // If this line wider than the last one, set it as the text width.
145 dataWidth = ( ( dataWidth > oneLineWidth ) ? dataWidth : oneLineWidth );
148 // Add 4.0 to the final dataHeight to accomodate the shadow.
151 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
153 // Create, position, setup, fill, and add the text view to the content view.
154 textField = [[[ITTextField alloc] initWithFrame:dataRect] autorelease];
155 [textField setEditable:NO];
156 [textField setSelectable:NO];
157 [textField setBordered:NO];
158 [textField setDrawsBackground:NO];
159 [textField setFont:font];
160 [textField setTextColor:[NSColor whiteColor]];
161 [textField setCastsShadow:YES];
162 [textField setStringValue:text];
163 [textField setShadowSaturation:SW_SHADOW_SAT];
164 [[self contentView] addSubview:textField];
166 // Display the window.
167 [[self contentView] setNeedsDisplay:YES];
172 - (void)buildMeterWindowWithCharacter:(NSString *)character
179 NSFont *font = [NSFont fontWithName:@"Lucida Grande Bold" size:size];
180 NSDictionary *attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
181 NSSize charSize = [character sizeWithAttributes:attr];
182 float cellHeight = ( charSize.height + 4.0 ); // Add 4.0 for shadow
183 float cellWidth = ( (charSize.width) + SW_METER_PAD ); // Add 8.0 for Apple suck
184 float dataWidth = ( cellWidth * count );
185 NSRect dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, cellHeight)];
186 NSEnumerator *cellEnum = nil;
189 NSColor *onColor = [NSColor whiteColor];
190 NSColor *offColor = [NSColor colorWithCalibratedWhite:0.0 alpha:0.50];
191 NSMatrix *volMatrix = [[[NSMatrix alloc] initWithFrame:dataRect
192 mode:NSHighlightModeMatrix
193 cellClass:NSClassFromString(@"ITTextFieldCell")
195 numberOfColumns:count] autorelease];
197 [volMatrix setCellSize:NSMakeSize(cellWidth, cellHeight)];
198 [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
200 cellEnum = [[volMatrix cells] objectEnumerator];
202 while ( (aCell = [cellEnum nextObject]) ) {
203 [aCell setEditable:NO];
204 [aCell setSelectable:NO];
205 [aCell setBordered:NO];
206 [aCell setDrawsBackground:NO];
207 [aCell setAlignment:NSCenterTextAlignment];
208 [aCell setFont:font];
209 [aCell setStringValue:character];
210 [aCell setShadowSaturation:SW_SHADOW_SAT];
214 if ( active >= activeCount ) {
215 [aCell setCastsShadow:YES];
216 [aCell setTextColor:onColor];
218 [aCell setCastsShadow:NO];
219 [aCell setTextColor:offColor];
224 [[self contentView] addSubview:volMatrix];
225 [[self contentView] setNeedsDisplay:YES];
230 - (void)buildDialogWindowWithMessage:(NSString *)message
231 defaultButton:(NSString *)defaultTitle
232 alternateButton:(NSString *)alternateTitle
234 defaultAction:(SEL)okAction
235 alternateAction:(SEL)alternateAction
239 float textWidth = 0.0;
240 float textHeight = 0.0;
242 float cancelWidth = 0.0;
243 float wideButtonW = 0.0;
244 float buttonWidth = 0.0;
245 float dataHeight = 0.0;
246 float dataWidth = 0.0;
250 float textAddBelow = 32.0;
251 float dataMinH = 92.0;
252 float textMinH = 48.0;
253 NSArray *lines = [message componentsSeparatedByString:@"\n"];
255 NSEnumerator *lineEnum = [lines objectEnumerator];
256 ITTextField *textField;
258 ITButton *cancelButton;
259 NSColor *textColor = [NSColor whiteColor];
260 NSFont *font = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
261 NSDictionary *attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
262 NSFont *buttonFont = [NSFont fontWithName:@"Lucida Grande Bold" size:14];
263 NSDictionary *buttonAttr = [NSDictionary dictionaryWithObjectsAndKeys:
264 buttonFont , NSFontAttributeName,
265 textColor , NSForegroundColorAttributeName,
268 // Iterate over each line to get text width and height
269 while ( (oneLine = [lineEnum nextObject]) ) {
270 // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
271 float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
272 // Add the height of this line to the total text height
273 textHeight += [oneLine sizeWithAttributes:attr].height;
274 // If this line wider than the last one, set it as the text width.
275 textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
278 // Add 4.0 to the final dataHeight to accomodate the shadow.
281 // Add extra padding below the text
282 dataHeight = (textHeight + textAddBelow);
284 // Test to see if data height is tall enough
285 if ( dataHeight < dataMinH ) {
286 dataHeight = dataMinH;
289 // Make the buttons, set the titles, and size them to fit their titles
290 okButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
291 cancelButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
292 [okButton setBezelStyle:ITGrayRoundedBezelStyle];
293 [cancelButton setBezelStyle:ITGrayRoundedBezelStyle];
294 [okButton setAlignment:NSRightTextAlignment];
295 [cancelButton setAlignment:NSCenterTextAlignment];
296 [okButton setImagePosition:NSNoImage];
297 [cancelButton setImagePosition:NSNoImage];
298 [okButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:defaultTitle
299 attributes:buttonAttr] autorelease]];
300 [cancelButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:alternateTitle
301 attributes:buttonAttr] autorelease]];
302 [okButton sizeToFit];
303 [cancelButton sizeToFit];
305 // Get the button widths. Add any extra width here.
306 okWidth = ([okButton frame].size.width + SW_BUTTON_EXTRA_W);
307 cancelWidth = ([cancelButton frame].size.width + SW_BUTTON_EXTRA_W);
309 // Figure out which button is wider.
310 wideButtonW = ( (okWidth > cancelWidth) ? okWidth : cancelWidth );
312 // Get the total width of the buttons. Add the divider space.
313 buttonWidth = ( (wideButtonW * 2) + SW_BUTTON_DIV );
315 // Set the dataWidth to whichever is greater: text width or button width.
316 dataWidth = ( (textWidth > buttonWidth) ? textWidth : buttonWidth);
319 dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
321 // Set an initial vertical point for the textRect's origin.
322 textY = dataRect.origin.y + textAddBelow;
324 // Move that point up if the minimimum height of the text area is not occupied.
325 if ( textHeight < textMinH ) {
326 textY += ( (textMinH - textHeight) / 2 );
329 // Build the text rect.
330 textRect = NSMakeRect(dataRect.origin.x,
335 // Create, position, setup, fill, and add the text view to the content view.
336 textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
337 [textField setEditable:NO];
338 [textField setSelectable:NO];
339 [textField setBordered:NO];
340 [textField setDrawsBackground:NO];
341 [textField setFont:font];
342 [textField setTextColor:textColor];
343 [textField setCastsShadow:YES];
344 [textField setStringValue:message];
345 [textField setShadowSaturation:SW_SHADOW_SAT];
346 [[self contentView] addSubview:textField];
348 // Set the button frames, and add them to the content view.
349 [okButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - (wideButtonW + SW_BUTTON_PAD_R) ),
353 [cancelButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - ((wideButtonW * 2) + SW_BUTTON_DIV + SW_BUTTON_PAD_R) ),
357 [[self contentView] addSubview:okButton];
358 [[self contentView] addSubview:cancelButton];
359 NSLog(@"%@", [[self contentView] description]);
361 [self setIgnoresMouseEvents:NO];
363 // Display the window.
364 [[self contentView] setNeedsDisplay:YES];