Positioning Fixes. The window might still do some weirdness if you change its positi...
[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 = StatusWindowRegular;
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:(StatusWindowSizing)newSizing
79 {
80     _sizing = newSizing;
81 }
82
83
84 /*************************************************************************/
85 #pragma mark -
86 #pragma mark INSTANCE METHODS
87 /*************************************************************************/
88
89 - (void)appear:(id)sender
90 {
91     if ( ! _locked ) {
92         [super appear:sender];
93     }
94 }
95
96 - (void)vanish:(id)sender
97 {
98     if ( ! _locked ) {
99         [super vanish:sender];
100     }
101 }
102
103 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize
104 {
105     float        divisor       = 1.0;
106     NSRect       imageRect;
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;
124     BOOL         shouldAnimate = ( ! (([self visibilityState] == ITWindowAppearingState) ||
125                                       ([self visibilityState] == ITWindowVanishingState)) );
126         
127     if ( _sizing == StatusWindowSmall ) {
128         divisor = SMALL_DIVISOR;
129     } else if ( _sizing == StatusWindowMini ) {
130         divisor = MINI_DIVISOR;
131     }
132
133 //  Get image width and height.
134     imageWidth  = ( [_image size].width  / divisor );
135     imageHeight = ( [_image size].height / divisor );
136     
137 //  Set the content height to the greater of the text and image heights.
138     contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight );
139
140 //  Setup the Window, and remove all its contentview's subviews.
141     windowWidth  = ( (SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor) + dataWidth + (SW_PAD / divisor) );
142     windowHeight = ( (SW_PAD / divisor) + contentHeight + (SW_PAD / divisor) );
143     
144 //  Constrain size to max limits.  Adjust data sizes accordingly.
145     excessWidth  = (windowWidth  - maxWidth );
146     excessHeight = (windowHeight - maxHeight);
147
148     if ( excessWidth > 0.0 ) {
149         windowWidth = maxWidth;
150         dataWidth -= excessWidth;
151     }
152     
153     if ( excessHeight > 0.0 ) {
154         windowHeight = maxHeight;
155         dataHeight -= excessHeight;
156     }
157     
158     if ( [self horizontalPosition] == ITWindowPositionLeft ) {
159         windowOrigin.x = ( SW_BORDER + screenOrigin.x );
160     } else if ( [self horizontalPosition] == ITWindowPositionCenter ) {
161         windowOrigin.x = ( screenOrigin.x + (screenWidth / 2) - (windowWidth / 2) );
162     } else if ( [self horizontalPosition] == ITWindowPositionRight ) {
163         windowOrigin.x = ( screenOrigin.x + screenWidth - (windowWidth + SW_BORDER) );
164     }
165     
166     if ( [self verticalPosition] == ITWindowPositionTop ) {
167         windowOrigin.y = ( screenOrigin.y + screenHeight - (windowHeight + SW_BORDER) );
168     } else if ( [self verticalPosition] == ITWindowPositionMiddle ) {
169 //      Middle-oriented windows should be slightly proud of the screen's middle.
170         windowOrigin.y = ( (screenOrigin.y + (screenHeight / 2) - (windowHeight / 2)) + (screenHeight / 8) );
171     } else if ( [self verticalPosition] == ITWindowPositionBottom ) {
172         windowOrigin.y = ( SW_BORDER + screenOrigin.y );
173     }
174     
175     [self setFrame:NSMakeRect( windowOrigin.x,
176                                windowOrigin.y,
177                                windowWidth,
178                                windowHeight) display:YES animate:shouldAnimate];
179
180     [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
181     
182 //  Setup, position, fill, and add the image view to the content view.
183     imageRect = NSMakeRect( (SW_PAD / divisor),
184                             ((SW_PAD / divisor) + ((contentHeight - imageHeight) / 2)),
185                             imageWidth,
186                             imageHeight );
187     imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
188     [imageView setAutoresizingMask:(NSViewMinYMargin | NSViewMaxYMargin)];
189     [imageView setImage:_image];
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 == StatusWindowSmall ) {
215             divisor = SMALL_DIVISOR;
216         } else if ( _sizing == StatusWindowMini ) {
217             divisor = MINI_DIVISOR;
218         }
219
220         font = [NSFont fontWithName:@"Lucida Grande 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 == StatusWindowSmall ) {
282             divisor = SMALL_DIVISOR;
283         } else if ( _sizing == StatusWindowMini ) {
284             divisor = MINI_DIVISOR;
285         }
286         
287         font        = [NSFont fontWithName:@"Lucida Grande 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         textWidth     = 0.0;
344         float         textHeight    = 0.0;
345         float         okWidth       = 0.0;
346         float         cancelWidth   = 0.0;
347         float         wideButtonW   = 0.0;
348         float         buttonWidth   = 0.0;
349         float         dataHeight    = 0.0;
350         float         dataWidth     = 0.0;
351         NSRect        dataRect;
352         float         textY         = 0.0;
353         NSRect        textRect;
354         float         textAddBelow  = 32.0;
355         float         dataMinH      = 92.0;
356         float         textMinH      = 48.0;
357         NSArray      *lines         = [message componentsSeparatedByString:@"\n"];
358         id                        oneLine       = nil;
359         NSEnumerator *lineEnum      = [lines objectEnumerator];
360         ITTextField  *textField;
361         ITButton     *okButton;
362         ITButton     *cancelButton;
363         NSColor      *textColor     = [NSColor whiteColor];
364         NSFont       *font          = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
365         NSDictionary *attr          = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
366         NSFont       *buttonFont    = [NSFont fontWithName:@"Lucida Grande Bold" size:14];
367         NSDictionary *buttonAttr    = [NSDictionary dictionaryWithObjectsAndKeys:
368             buttonFont , NSFontAttributeName,
369             textColor  , NSForegroundColorAttributeName, 
370             nil];
371         
372 //      Iterate over each line to get text width and height
373         while ( (oneLine = [lineEnum nextObject]) ) {
374 //          Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
375             float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
376 //          Add the height of this line to the total text height
377             textHeight += [oneLine sizeWithAttributes:attr].height;
378 //          If this line wider than the last one, set it as the text width.
379             textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
380         }
381         
382 //      Add 4.0 to the final dataHeight to accomodate the shadow.
383         textHeight += 4.0;
384         
385 //      Add extra padding below the text
386         dataHeight = (textHeight + textAddBelow);
387         
388 //      Test to see if data height is tall enough
389         if ( dataHeight < dataMinH ) {
390             dataHeight = dataMinH;
391         }
392         
393 //      Make the buttons, set the titles, and size them to fit their titles
394         okButton     = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
395         cancelButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
396         [okButton     setTarget:target];
397         [cancelButton setTarget:target];
398         [okButton     setAction:okAction];
399         [cancelButton setAction:alternateAction];
400         [okButton     setBezelStyle:ITGrayRoundedBezelStyle];
401         [cancelButton setBezelStyle:ITGrayRoundedBezelStyle];
402         [okButton     setAlignment:NSRightTextAlignment];
403         [cancelButton setAlignment:NSCenterTextAlignment];
404         [okButton     setImagePosition:NSNoImage];
405         [cancelButton setImagePosition:NSNoImage];
406         [okButton     setAttributedTitle:[[[NSAttributedString alloc] initWithString:defaultTitle
407                                                                           attributes:buttonAttr] autorelease]];
408         [cancelButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:alternateTitle
409                                                                           attributes:buttonAttr] autorelease]];
410         [okButton     sizeToFit];
411         [cancelButton sizeToFit];
412         
413 //      Get the button widths.  Add any extra width here.
414         okWidth     = ([okButton     frame].size.width + SW_BUTTON_EXTRA_W);
415         cancelWidth = ([cancelButton frame].size.width + SW_BUTTON_EXTRA_W);
416         
417 //      Figure out which button is wider.
418         wideButtonW = ( (okWidth > cancelWidth) ? okWidth : cancelWidth );
419
420 //      Get the total width of the buttons. Add the divider space.
421         buttonWidth = ( (wideButtonW * 2) + SW_BUTTON_DIV );
422
423 //      Set the dataWidth to whichever is greater: text width or button width.
424         dataWidth = ( (textWidth > buttonWidth) ? textWidth : buttonWidth);
425         
426 //      Setup the window
427         dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
428         
429 //      Set an initial vertical point for the textRect's origin.
430         textY = dataRect.origin.y + textAddBelow;
431         
432 //      Move that point up if the minimimum height of the text area is not occupied.
433         if ( textHeight < textMinH ) {
434             textY += ( (textMinH - textHeight) / 2 );
435         }
436         
437 //      Build the text rect.
438         textRect = NSMakeRect(dataRect.origin.x,
439                               textY,
440                               textWidth,
441                               textHeight);
442         
443 //      Create, position, setup, fill, and add the text view to the content view.
444         textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
445         [textField setEditable:NO];
446         [textField setSelectable:NO];
447         [textField setBordered:NO];
448         [textField setDrawsBackground:NO];
449         [textField setFont:font];
450         [textField setTextColor:textColor];
451         [textField setCastsShadow:YES];
452         [textField setStringValue:message];
453         [textField setShadowSaturation:SW_SHADOW_SAT];
454         [[self contentView] addSubview:textField];
455         
456 //      Set the button frames, and add them to the content view.
457         [okButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - (wideButtonW + SW_BUTTON_PAD_R) ),
458                                        SW_BUTTON_PAD_B,
459                                        wideButtonW,
460                                        24.0)];
461         [cancelButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - ((wideButtonW * 2) + SW_BUTTON_DIV + SW_BUTTON_PAD_R) ),
462                                            SW_BUTTON_PAD_B,
463                                            wideButtonW,
464                                            24.0)];
465         [[self contentView] addSubview:okButton];
466         [[self contentView] addSubview:cancelButton];
467
468         [self setIgnoresMouseEvents:NO];
469   
470 //      Display the window.
471         [[self contentView] setNeedsDisplay:YES];
472     }
473 }
474
475 - (NSTimeInterval)animationResizeTime:(NSRect)newFrame
476 {
477     return (NSTimeInterval)0.25;
478 }
479
480 @end