This about does it. I want to do a couple more things, especially multiple screen...
[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
125     if ( _sizing == StatusWindowSmall ) {
126         divisor = SMALL_DIVISOR;
127     } else if ( _sizing == StatusWindowMini ) {
128         divisor = MINI_DIVISOR;
129     }
130
131 //  Get image width and height.
132     imageWidth  = ( [_image size].width  / divisor );
133     imageHeight = ( [_image size].height / divisor );
134     
135 //  Set the content height to the greater of the text and image heights.
136     contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight );
137
138 //  Setup the Window, and remove all its contentview's subviews.
139     windowWidth  = ( (SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor) + dataWidth + (SW_PAD / divisor) );
140     windowHeight = ( (SW_PAD / divisor) + contentHeight + (SW_PAD / divisor) );
141     
142 //  Constrain size to max limits.  Adjust data sizes accordingly.
143     excessWidth  = (windowWidth  - maxWidth );
144     excessHeight = (windowHeight - maxHeight);
145
146     if ( excessWidth > 0.0 ) {
147         windowWidth = maxWidth;
148         dataWidth -= excessWidth;
149     }
150     
151     if ( excessHeight > 0.0 ) {
152         windowHeight = maxHeight;
153         dataHeight -= excessHeight;
154     }
155     
156     if ( [self horizontalPosition] == ITWindowPositionLeft ) {
157         windowOrigin.x = ( SW_BORDER + screenOrigin.x );
158     } else if ( [self horizontalPosition] == ITWindowPositionCenter ) {
159         windowOrigin.x = ( screenOrigin.x + (screenWidth / 2) - (windowWidth / 2) );
160     } else if ( [self horizontalPosition] == ITWindowPositionRight ) {
161         windowOrigin.x = ( screenOrigin.x + screenWidth - (windowWidth + SW_BORDER) );
162     }
163     
164     if ( [self verticalPosition] == ITWindowPositionTop ) {
165         windowOrigin.y = ( screenOrigin.y + screenHeight - (windowHeight + SW_BORDER) );
166     } else if ( [self verticalPosition] == ITWindowPositionMiddle ) {
167 //      Middle-oriented windows should be slightly proud of the screen's middle.
168         windowOrigin.y = ( (screenOrigin.y + (screenHeight / 2) - (windowHeight / 2)) + (screenHeight / 8) );
169     } else if ( [self verticalPosition] == ITWindowPositionBottom ) {
170         windowOrigin.y = ( SW_BORDER + screenOrigin.y );
171     }
172     
173     [self setFrame:NSMakeRect( windowOrigin.x,
174                                windowOrigin.y,
175                                windowWidth,
176                                windowHeight) display:YES animate:YES];
177
178     [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
179     
180 //  Setup, position, fill, and add the image view to the content view.
181     imageRect = NSMakeRect( (SW_PAD / divisor),
182                             ((SW_PAD / divisor) + ((contentHeight - imageHeight) / 2)),
183                             imageWidth,
184                             imageHeight );
185     imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
186     [imageView setAutoresizingMask:(NSViewMinYMargin | NSViewMaxYMargin)];
187     [imageView setImage:_image];
188     [[self contentView] addSubview:imageView];
189
190     return NSMakeRect( ((SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor)),
191                        ((SW_PAD / divisor) + ((contentHeight - dataHeight) / 2)),
192                        dataWidth,
193                        dataHeight);
194 }
195
196 - (void)buildTextWindowWithString:(NSString *)text
197 {
198     if ( ! _locked ) {
199
200         float         divisor       = 1.0;
201         float         dataWidth     = 0.0;
202         float         dataHeight    = 0.0;
203         NSRect        dataRect;
204         NSArray      *lines         = [text componentsSeparatedByString:@"\n"];
205         id                        oneLine       = nil;
206         NSEnumerator *lineEnum      = [lines objectEnumerator];
207         float         baseFontSize  = 18.0;
208         ITTextField  *textField;
209         NSFont       *font;
210         NSDictionary *attr;
211
212         if ( _sizing == StatusWindowSmall ) {
213             divisor = SMALL_DIVISOR;
214         } else if ( _sizing == StatusWindowMini ) {
215             divisor = MINI_DIVISOR;
216         }
217
218         font = [NSFont fontWithName:@"Lucida Grande Bold" size:(baseFontSize / divisor)];
219         attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
220         
221 //      Iterate over each line to get text width and height
222         while ( (oneLine = [lineEnum nextObject]) ) {
223 //          Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
224             float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
225 //          Add the height of this line to the total text height
226             dataHeight += [oneLine sizeWithAttributes:attr].height;
227 //          If this line wider than the last one, set it as the text width.
228             dataWidth = ( ( dataWidth > oneLineWidth ) ? dataWidth : oneLineWidth );
229         }
230         
231 //      Add 4.0 to the final dataHeight to accomodate the shadow.
232         dataHeight += 4.0;
233
234         dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
235         
236 //      Create, position, setup, fill, and add the text view to the content view.
237         textField = [[[ITTextField alloc] initWithFrame:dataRect] autorelease];
238         [textField setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
239         [textField setEditable:NO];
240         [textField setSelectable:NO];
241         [textField setBordered:NO];
242         [textField setDrawsBackground:NO];
243         [textField setFont:font];
244         [textField setTextColor:[NSColor whiteColor]];
245         [textField setCastsShadow:YES];
246         [[textField cell] setWraps:NO];
247         [textField setStringValue:text];
248         [textField setShadowSaturation:SW_SHADOW_SAT];
249         [[self contentView] addSubview:textField];
250         
251 //      Display the window.
252         [[self contentView] setNeedsDisplay:YES];
253
254     }
255 }
256
257 - (void)buildMeterWindowWithCharacter:(NSString *)character
258                                  size:(float)size
259                                 count:(int)count
260                                active:(int)active
261 {
262     if ( ! _locked ) {
263
264         float         divisor     = 1.0;
265         NSFont       *font;
266         NSDictionary *attr;
267         NSSize        charSize;
268         float         cellHeight;
269         float         cellWidth;
270         float         dataWidth;
271         NSRect        dataRect;
272         NSEnumerator *cellEnum    = nil;
273         id            aCell       = nil;
274         int           activeCount = 0;
275         NSColor      *onColor     = [NSColor whiteColor];
276         NSColor      *offColor    = [NSColor colorWithCalibratedWhite:0.15 alpha:0.50];
277         NSMatrix     *volMatrix;
278         
279         if ( _sizing == StatusWindowSmall ) {
280             divisor = SMALL_DIVISOR;
281         } else if ( _sizing == StatusWindowMini ) {
282             divisor = MINI_DIVISOR;
283         }
284         
285         font        = [NSFont fontWithName:@"Lucida Grande Bold" size:( size / divisor )];
286         attr        = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
287         charSize    = [character sizeWithAttributes:attr];
288         cellHeight  = ( charSize.height + 4.0 );  // Add 4.0 for shadow
289         cellWidth   = ( (charSize.width) + (SW_METER_PAD / divisor) );
290         dataWidth   = ( cellWidth * count );
291         dataRect    = [self setupWindowWithDataSize:NSMakeSize(dataWidth, cellHeight)];
292         volMatrix   = [[[NSMatrix alloc] initWithFrame:dataRect
293                                                   mode:NSHighlightModeMatrix
294                                              cellClass:NSClassFromString(@"ITTextFieldCell")
295                                           numberOfRows:1
296                                        numberOfColumns:count] autorelease];
297         
298         [volMatrix setCellSize:NSMakeSize(cellWidth, cellHeight)];
299         [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
300         [volMatrix setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
301
302         cellEnum = [[volMatrix cells] objectEnumerator];
303
304         while ( (aCell = [cellEnum nextObject]) ) {
305             [aCell setEditable:NO];
306             [aCell setSelectable:NO];
307             [aCell setBordered:NO];
308             [aCell setDrawsBackground:NO];
309             [aCell setAlignment:NSCenterTextAlignment];
310             [aCell setFont:font];
311             [aCell setStringValue:character];
312             [aCell setShadowSaturation:SW_SHADOW_SAT];
313
314             activeCount ++;
315
316             if ( active >= activeCount ) {
317                 [aCell setCastsShadow:YES];
318                 [aCell setTextColor:onColor];
319             } else {
320                 [aCell setCastsShadow:NO];
321                 [aCell setTextColor:offColor];
322             }
323
324         }
325
326         [[self contentView] addSubview:volMatrix];
327         [[self contentView] setNeedsDisplay:YES];
328         
329     }
330 }
331
332 - (void)buildDialogWindowWithMessage:(NSString *)message
333                        defaultButton:(NSString *)defaultTitle
334                      alternateButton:(NSString *)alternateTitle
335                               target:(id)target
336                        defaultAction:(SEL)okAction
337                      alternateAction:(SEL)alternateAction
338 {
339     if ( ! _locked ) {
340
341         float         textWidth     = 0.0;
342         float         textHeight    = 0.0;
343         float         okWidth       = 0.0;
344         float         cancelWidth   = 0.0;
345         float         wideButtonW   = 0.0;
346         float         buttonWidth   = 0.0;
347         float         dataHeight    = 0.0;
348         float         dataWidth     = 0.0;
349         NSRect        dataRect;
350         float         textY         = 0.0;
351         NSRect        textRect;
352         float         textAddBelow  = 32.0;
353         float         dataMinH      = 92.0;
354         float         textMinH      = 48.0;
355         NSArray      *lines         = [message componentsSeparatedByString:@"\n"];
356         id                        oneLine       = nil;
357         NSEnumerator *lineEnum      = [lines objectEnumerator];
358         ITTextField  *textField;
359         ITButton     *okButton;
360         ITButton     *cancelButton;
361         NSColor      *textColor     = [NSColor whiteColor];
362         NSFont       *font          = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
363         NSDictionary *attr          = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
364         NSFont       *buttonFont    = [NSFont fontWithName:@"Lucida Grande Bold" size:14];
365         NSDictionary *buttonAttr    = [NSDictionary dictionaryWithObjectsAndKeys:
366             buttonFont , NSFontAttributeName,
367             textColor  , NSForegroundColorAttributeName, 
368             nil];
369         
370 //      Iterate over each line to get text width and height
371         while ( (oneLine = [lineEnum nextObject]) ) {
372 //          Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
373             float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
374 //          Add the height of this line to the total text height
375             textHeight += [oneLine sizeWithAttributes:attr].height;
376 //          If this line wider than the last one, set it as the text width.
377             textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
378         }
379         
380 //      Add 4.0 to the final dataHeight to accomodate the shadow.
381         textHeight += 4.0;
382         
383 //      Add extra padding below the text
384         dataHeight = (textHeight + textAddBelow);
385         
386 //      Test to see if data height is tall enough
387         if ( dataHeight < dataMinH ) {
388             dataHeight = dataMinH;
389         }
390         
391 //      Make the buttons, set the titles, and size them to fit their titles
392         okButton     = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
393         cancelButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
394         [okButton     setTarget:target];
395         [cancelButton setTarget:target];
396         [okButton     setAction:okAction];
397         [cancelButton setAction:alternateAction];
398         [okButton     setBezelStyle:ITGrayRoundedBezelStyle];
399         [cancelButton setBezelStyle:ITGrayRoundedBezelStyle];
400         [okButton     setAlignment:NSRightTextAlignment];
401         [cancelButton setAlignment:NSCenterTextAlignment];
402         [okButton     setImagePosition:NSNoImage];
403         [cancelButton setImagePosition:NSNoImage];
404         [okButton     setAttributedTitle:[[[NSAttributedString alloc] initWithString:defaultTitle
405                                                                           attributes:buttonAttr] autorelease]];
406         [cancelButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:alternateTitle
407                                                                           attributes:buttonAttr] autorelease]];
408         [okButton     sizeToFit];
409         [cancelButton sizeToFit];
410         
411 //      Get the button widths.  Add any extra width here.
412         okWidth     = ([okButton     frame].size.width + SW_BUTTON_EXTRA_W);
413         cancelWidth = ([cancelButton frame].size.width + SW_BUTTON_EXTRA_W);
414         
415 //      Figure out which button is wider.
416         wideButtonW = ( (okWidth > cancelWidth) ? okWidth : cancelWidth );
417
418 //      Get the total width of the buttons. Add the divider space.
419         buttonWidth = ( (wideButtonW * 2) + SW_BUTTON_DIV );
420
421 //      Set the dataWidth to whichever is greater: text width or button width.
422         dataWidth = ( (textWidth > buttonWidth) ? textWidth : buttonWidth);
423         
424 //      Setup the window
425         dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
426         
427 //      Set an initial vertical point for the textRect's origin.
428         textY = dataRect.origin.y + textAddBelow;
429         
430 //      Move that point up if the minimimum height of the text area is not occupied.
431         if ( textHeight < textMinH ) {
432             textY += ( (textMinH - textHeight) / 2 );
433         }
434         
435 //      Build the text rect.
436         textRect = NSMakeRect(dataRect.origin.x,
437                               textY,
438                               textWidth,
439                               textHeight);
440         
441 //      Create, position, setup, fill, and add the text view to the content view.
442         textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
443         [textField setEditable:NO];
444         [textField setSelectable:NO];
445         [textField setBordered:NO];
446         [textField setDrawsBackground:NO];
447         [textField setFont:font];
448         [textField setTextColor:textColor];
449         [textField setCastsShadow:YES];
450         [textField setStringValue:message];
451         [textField setShadowSaturation:SW_SHADOW_SAT];
452         [[self contentView] addSubview:textField];
453         
454 //      Set the button frames, and add them to the content view.
455         [okButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - (wideButtonW + SW_BUTTON_PAD_R) ),
456                                        SW_BUTTON_PAD_B,
457                                        wideButtonW,
458                                        24.0)];
459         [cancelButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - ((wideButtonW * 2) + SW_BUTTON_DIV + SW_BUTTON_PAD_R) ),
460                                            SW_BUTTON_PAD_B,
461                                            wideButtonW,
462                                            24.0)];
463         [[self contentView] addSubview:okButton];
464         [[self contentView] addSubview:cancelButton];
465
466         [self setIgnoresMouseEvents:NO];
467   
468 //      Display the window.
469         [[self contentView] setNeedsDisplay:YES];
470     }
471 }
472
473 - (NSTimeInterval)animationResizeTime:(NSRect)newFrame
474 {
475     return (NSTimeInterval)0.25;
476 }
477
478 @end