Updating plists and versioning to release 1.5.1.
[MenuTunes.git] / StatusWindow.m
1 //
2 //  StatusWindow.m
3 //  MenuTunes
4 //
5 //  Created by Matt L. Judy on Sat Feb 22 2003.
6 //  Copyright (c) 2003 NibFile.com. All rights reserved.
7 //
8
9 #import "StatusWindow.h"
10
11
12 #define SW_PAD             24.00
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
24
25 @interface StatusWindow (Private)
26 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize;
27 @end
28
29
30 @implementation StatusWindow
31
32
33 /*************************************************************************/
34 #pragma mark -
35 #pragma mark INITIALIZATION / DEALLOCATION METHODS
36 /*************************************************************************/
37
38 - (id)initWithContentView:(NSView *)contentView
39                  exitMode:(ITTransientStatusWindowExitMode)exitMode
40            backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType
41 {
42     if ( ( self = [super initWithContentView:contentView
43                                exitMode:exitMode
44                          backgroundType:backgroundType] ) ) {
45      // Set default values.
46         _image  = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
47         _locked = NO;
48         _sizing = ITTransientStatusWindowRegular;
49     }
50     
51     return self;
52 }
53
54 - (void)dealloc
55 {
56     [_image release];
57     [super dealloc];
58 }
59
60
61 /*************************************************************************/
62 #pragma mark -
63 #pragma mark ACCESSOR METHODS
64 /*************************************************************************/
65
66 - (void)setImage:(NSImage *)newImage
67 {
68     [_image autorelease];
69     _image = [newImage copy];
70 }
71
72 - (void)setLocked:(BOOL)flag
73 {
74     _locked = flag;
75     [self setExitMode:(flag ? ITTransientStatusWindowExitOnCommand : ITTransientStatusWindowExitAfterDelay)];
76 }
77
78 - (void)setSizing:(ITTransientStatusWindowSizing)newSizing
79 {
80     _sizing = newSizing;
81 }
82
83 /*************************************************************************/
84 #pragma mark -
85 #pragma mark INSTANCE METHODS
86 /*************************************************************************/
87
88 - (void)appear:(id)sender
89 {
90     if ( ! _locked ) {
91         [super appear:sender];
92     }
93 }
94
95 - (void)vanish:(id)sender
96 {
97     if ( ! _locked ) {
98         [super vanish:sender];
99     }
100 }
101
102 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize
103 {
104     float        divisor       = 1.0;
105     NSRect       imageRect;
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)) );
125         
126     if ( _sizing == ITTransientStatusWindowSmall ) {
127         divisor = SMALL_DIVISOR;
128     } else if ( _sizing == ITTransientStatusWindowMini ) {
129         divisor = MINI_DIVISOR;
130     }
131
132 //  Get image width and height.
133     imageWidth  = ( [_image size].width  / divisor );
134     imageHeight = ( [_image size].height / divisor );
135     
136 //  Set the content height to the greater of the text and image heights.
137     contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight );
138
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) );
142     
143 //  Constrain size to max limits.  Adjust data sizes accordingly.
144     excessWidth  = (windowWidth  - maxWidth );
145     excessHeight = (windowHeight - maxHeight);
146
147     if ( excessWidth > 0.0 ) {
148         windowWidth = maxWidth;
149         dataWidth -= excessWidth;
150     }
151     
152     if ( excessHeight > 0.0 ) {
153         windowHeight = maxHeight;
154         dataHeight -= excessHeight;
155     }
156     
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) );
163     }
164     
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 );
172     }
173     
174     [self setFrame:NSMakeRect( windowOrigin.x,
175                                windowOrigin.y,
176                                windowWidth,
177                                windowHeight) display:YES animate:shouldAnimate];
178
179     [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
180     
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)),
184                             imageWidth,
185                             imageHeight );
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];
191
192     return NSMakeRect( ((SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor)),
193                        ((SW_PAD / divisor) + ((contentHeight - dataHeight) / 2)),
194                        dataWidth,
195                        dataHeight);
196 }
197
198 - (void)buildTextWindowWithString:(NSString *)text
199 {
200     if ( ! _locked ) {
201
202         float         divisor       = 1.0;
203         float         dataWidth     = 0.0;
204         float         dataHeight    = 0.0;
205         NSRect        dataRect;
206         NSArray      *lines         = [text componentsSeparatedByString:@"\n"];
207         id                        oneLine       = nil;
208         NSEnumerator *lineEnum      = [lines objectEnumerator];
209         float         baseFontSize  = 18.0;
210         ITTextField  *textField;
211         NSFont       *font;
212         NSDictionary *attr;
213
214         if ( _sizing == ITTransientStatusWindowSmall ) {
215             divisor = SMALL_DIVISOR;
216         } else if ( _sizing == ITTransientStatusWindowMini ) {
217             divisor = MINI_DIVISOR;
218         }
219
220         font = [NSFont fontWithName:@"LucidaGrande-Bold" size:(baseFontSize / divisor)];
221         attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
222         
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 );
231         }
232         
233 //      Add 4.0 to the final dataHeight to accomodate the shadow.
234         dataHeight += 4.0;
235
236         dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
237         
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];
252         
253 //      Display the window.
254         [[self contentView] setNeedsDisplay:YES];
255
256     }
257 }
258
259 - (void)buildMeterWindowWithCharacter:(NSString *)character
260                                  size:(float)size
261                                 count:(int)count
262                                active:(int)active
263 {
264     if ( ! _locked ) {
265
266         float         divisor     = 1.0;
267         NSFont       *font;
268         NSDictionary *attr;
269         NSSize        charSize;
270         float         cellHeight;
271         float         cellWidth;
272         float         dataWidth;
273         NSRect        dataRect;
274         NSEnumerator *cellEnum    = nil;
275         id            aCell       = nil;
276         int           activeCount = 0;
277         NSColor      *onColor     = [NSColor whiteColor];
278         NSColor      *offColor    = [NSColor colorWithCalibratedWhite:0.15 alpha:0.50];
279         NSMatrix     *volMatrix;
280         
281         if ( _sizing == ITTransientStatusWindowSmall ) {
282             divisor = SMALL_DIVISOR;
283         } else if ( _sizing == ITTransientStatusWindowMini ) {
284             divisor = MINI_DIVISOR;
285         }
286         
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")
297                                           numberOfRows:1
298                                        numberOfColumns:count] autorelease];
299         
300         [volMatrix setCellSize:NSMakeSize(cellWidth, cellHeight)];
301         [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
302         [volMatrix setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
303
304         cellEnum = [[volMatrix cells] objectEnumerator];
305
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];
315
316             activeCount ++;
317
318             if ( active >= activeCount ) {
319                 [aCell setCastsShadow:YES];
320                 [aCell setTextColor:onColor];
321             } else {
322                 [aCell setCastsShadow:NO];
323                 [aCell setTextColor:offColor];
324             }
325
326         }
327
328         [[self contentView] addSubview:volMatrix];
329         [[self contentView] setNeedsDisplay:YES];
330         
331     }
332 }
333
334 - (void)buildDialogWindowWithMessage:(NSString *)message
335                        defaultButton:(NSString *)defaultTitle
336                      alternateButton:(NSString *)alternateTitle
337                               target:(id)target
338                        defaultAction:(SEL)okAction
339                      alternateAction:(SEL)alternateAction
340 {
341     if ( ! _locked ) {
342
343         float         divisor       = 1.0;
344         float         textWidth     = 0.0;
345         float         textHeight    = 0.0;
346         float         okWidth       = 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;
352         NSRect        dataRect;
353         float         textY         = 0.0;
354         NSRect        textRect;
355         float         textAddBelow  = 32.0;
356         float         dataMinH      = 92.0;
357         float         textMinH      = 48.0;
358         NSArray      *lines         = [message componentsSeparatedByString:@"\n"];
359         id                        oneLine       = nil;
360         NSEnumerator *lineEnum      = [lines objectEnumerator];
361         float         baseFontSize  = 18.0;
362         ITTextField  *textField;
363         ITButton     *okButton;
364         ITButton     *cancelButton;
365         NSColor      *textColor     = [NSColor whiteColor];
366         NSFont       *font;
367         NSDictionary *attr;
368         NSFont       *buttonFont;
369         NSDictionary *buttonAttr;
370         
371         if ( _sizing == ITTransientStatusWindowSmall ) {
372             divisor = SMALL_DIVISOR;
373         } else if ( _sizing == ITTransientStatusWindowMini ) {
374             divisor = MINI_DIVISOR;
375         }
376         
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, 
383             nil];
384         
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 );
393         }
394         
395 //      Add 4.0 to the final dataHeight to accomodate the shadow.
396         textHeight += 4.0;
397         
398 //      Add extra padding below the text
399         dataHeight = (textHeight + textAddBelow);
400         
401 //      Test to see if data height is tall enough
402         if ( dataHeight < dataMinH ) {
403             dataHeight = dataMinH;
404         }
405         
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];
425         
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);
429         
430 //      Figure out which button is wider.
431         wideButtonW = ( (okWidth > cancelWidth) ? okWidth : cancelWidth );
432
433 //      Get the total width of the buttons. Add the divider space.
434         buttonWidth = ( (wideButtonW * 2) + SW_BUTTON_DIV );
435
436 //      Set the dataWidth to whichever is greater: text width or button width.
437         dataWidth = ( (textWidth > buttonWidth) ? textWidth : buttonWidth);
438         
439 //      Setup the window
440         dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
441         
442 //      Set an initial vertical point for the textRect's origin.
443         textY = dataRect.origin.y + textAddBelow;
444         
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 );
448         }
449         
450 //      Build the text rect.
451         textRect = NSMakeRect(dataRect.origin.x,
452                               textY,
453                               textWidth,
454                               textHeight);
455         
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];
468         
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) ),
471                                        SW_BUTTON_PAD_B,
472                                        wideButtonW,
473                                        24.0)];
474         [cancelButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - ((wideButtonW * 2) + SW_BUTTON_DIV + SW_BUTTON_PAD_R) ),
475                                            SW_BUTTON_PAD_B,
476                                            wideButtonW,
477                                            24.0)];
478         [[self contentView] addSubview:okButton];
479         if (alternateTitle) {
480             [[self contentView] addSubview:cancelButton];
481         }
482
483         [self setIgnoresMouseEvents:NO];
484   
485 //      Display the window.
486         [[self contentView] setNeedsDisplay:YES];
487     }
488 }
489
490 - (NSTimeInterval)animationResizeTime:(NSRect)newFrame
491 {
492     return (NSTimeInterval)0.25;
493 }
494
495 @end