Fixed a bug in updateTime:
[MenuTunes.git] / StatusWindow.m
index ca229ee..3a84d8b 100755 (executable)
@@ -9,6 +9,17 @@
 #import "StatusWindow.h"
 
 
+#define SW_PAD             24.00
+#define SW_SPACE           24.00
+#define SW_MINW           211.00
+#define SW_BORDER          32.00
+#define SW_METER_PAD        4.00
+#define SW_BUTTON_PAD_R    30.00
+#define SW_BUTTON_PAD_B    24.00
+#define SW_BUTTON_DIV      12.00
+#define SW_BUTTON_EXTRA_W   8.00
+#define SW_SHADOW_SAT       1.25
+
 @interface StatusWindow (Private)
 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize;
 @end
@@ -30,8 +41,9 @@
                                exitMode:exitMode
                          backgroundType:backgroundType] ) ) {
      // Set default values.
-        _image       = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
-        _locked      = NO;
+        _image  = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
+        _locked = NO;
+        _sizing = ITTransientStatusWindowRegular;
     }
     
     return self;
@@ -39,7 +51,7 @@
 
 - (void)dealloc
 {
-    [_image     release];
+    [_image release];
     [super dealloc];
 }
 
     [self setExitMode:(flag ? ITTransientStatusWindowExitOnCommand : ITTransientStatusWindowExitAfterDelay)];
 }
 
+- (void)setSizing:(ITTransientStatusWindowSizing)newSizing
+{
+    _sizing = newSizing;
+}
 
 /*************************************************************************/
 #pragma mark -
@@ -83,6 +99,7 @@
 
 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize
 {
+    float        divisor       = 1.0;
     NSRect       imageRect;
     float        imageWidth    = 0.0;
     float        imageHeight   = 0.0;
     float        contentHeight = 0.0;
     float        windowWidth   = 0.0;
     float        windowHeight  = 0.0;
-    NSImageView *imageView;
+    NSRect       visibleFrame  = [[self screen] visibleFrame];
+    NSPoint      screenOrigin  = visibleFrame.origin;
+    float        screenWidth   = visibleFrame.size.width;
+    float        screenHeight  = visibleFrame.size.height;
+    float        maxWidth      = ( screenWidth  - (SW_BORDER * 2) );
+    float        maxHeight     = ( screenHeight - (SW_BORDER * 2) );
+    float        excessWidth   = 0.0;
+    float        excessHeight  = 0.0;
+    NSPoint      windowOrigin  = NSZeroPoint;
+    ITImageView *imageView;
+    BOOL         shouldAnimate = ( ! (([self visibilityState] == ITWindowAppearingState) ||
+                                      ([self visibilityState] == ITWindowVanishingState)) );
+       
+    if ( _sizing == ITTransientStatusWindowSmall ) {
+        divisor = SMALL_DIVISOR;
+    } else if ( _sizing == ITTransientStatusWindowMini ) {
+        divisor = MINI_DIVISOR;
+    }
 
 //  Get image width and height.
-    imageWidth  = [_image size].width;
-    imageHeight = [_image size].height;
+    imageWidth  = ( [_image size].width  / divisor );
+    imageHeight = ( [_image size].height / divisor );
     
 //  Set the content height to the greater of the text and image heights.
     contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight );
 
 //  Setup the Window, and remove all its contentview's subviews.
-    windowWidth  = ( SW_PAD + imageWidth + SW_SPACE + dataWidth + SW_PAD );
-    windowHeight = ( SW_PAD + contentHeight + SW_PAD );
-    [self setFrame:NSMakeRect( (SW_BORDER + [[self screen] visibleFrame].origin.x),
-                               (SW_BORDER + [[self screen] visibleFrame].origin.y),
+    windowWidth  = ( (SW_PAD / divisor) + imageWidth + ((dataWidth > 0) ? (SW_SPACE / divisor) + dataWidth : 0) + (SW_PAD / divisor) );
+    windowHeight = ( (SW_PAD / divisor) + contentHeight + (SW_PAD / divisor) );
+    
+//  Constrain size to max limits.  Adjust data sizes accordingly.
+    excessWidth  = (windowWidth  - maxWidth );
+    excessHeight = (windowHeight - maxHeight);
+
+    if ( excessWidth > 0.0 ) {
+        windowWidth = maxWidth;
+        dataWidth -= excessWidth;
+    }
+    
+    if ( excessHeight > 0.0 ) {
+        windowHeight = maxHeight;
+        dataHeight -= excessHeight;
+    }
+    
+    if ( [self horizontalPosition] == ITWindowPositionLeft ) {
+        windowOrigin.x = ( SW_BORDER + screenOrigin.x );
+    } else if ( [self horizontalPosition] == ITWindowPositionCenter ) {
+        windowOrigin.x = ( screenOrigin.x + (screenWidth / 2) - (windowWidth / 2) );
+    } else if ( [self horizontalPosition] == ITWindowPositionRight ) {
+        windowOrigin.x = ( screenOrigin.x + screenWidth - (windowWidth + SW_BORDER) );
+    }
+    
+    if ( [self verticalPosition] == ITWindowPositionTop ) {
+        windowOrigin.y = ( screenOrigin.y + screenHeight - (windowHeight + SW_BORDER) );
+    } else if ( [self verticalPosition] == ITWindowPositionMiddle ) {
+//      Middle-oriented windows should be slightly proud of the screen's middle.
+        windowOrigin.y = ( (screenOrigin.y + (screenHeight / 2) - (windowHeight / 2)) + (screenHeight / 8) );
+    } else if ( [self verticalPosition] == ITWindowPositionBottom ) {
+        windowOrigin.y = ( SW_BORDER + screenOrigin.y );
+    }
+    
+    [self setFrame:NSMakeRect( windowOrigin.x,
+                               windowOrigin.y,
                                windowWidth,
-                               windowHeight) display:YES];
-    [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
+                               windowHeight) display:YES animate:shouldAnimate];
 
+    [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
+    
 //  Setup, position, fill, and add the image view to the content view.
-    imageRect = NSMakeRect( SW_PAD,
-                            (SW_PAD + ((contentHeight - imageHeight) / 2)),
+    imageRect = NSMakeRect( (SW_PAD / divisor) + ((dataWidth > 0) ? 4 : 0),
+                            ((SW_PAD / divisor) + ((contentHeight - imageHeight) / 2)),
                             imageWidth,
                             imageHeight );
-    imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
+    imageView = [[[ITImageView alloc] initWithFrame:imageRect] autorelease];
+    [imageView setAutoresizingMask:(NSViewMinYMargin | NSViewMaxYMargin)];
     [imageView setImage:_image];
+    [imageView setCastsShadow:YES];
     [[self contentView] addSubview:imageView];
 
-    return NSMakeRect( (SW_PAD + imageWidth + SW_SPACE),
-                       (SW_PAD + ((contentHeight - dataHeight) / 2)),
+    return NSMakeRect( ((SW_PAD / divisor) + imageWidth + (SW_SPACE / divisor)),
+                       ((SW_PAD / divisor) + ((contentHeight - dataHeight) / 2)),
                        dataWidth,
                        dataHeight);
 }
 
-- (void)buildTextWindowWithString:(NSString *)text
+- (void)buildImageWindowWithImage:(NSImage *)image
+{
+       if (!_locked) {
+               float divisor = 1.0;
+               NSRect dataRect;
+               
+               if (_sizing == ITTransientStatusWindowSmall) {
+                       divisor = SMALL_DIVISOR;
+               } else if (_sizing == ITTransientStatusWindowMini) {
+                       divisor = MINI_DIVISOR;
+               }
+               
+               [self setImage:image];
+               dataRect = [self setupWindowWithDataSize:NSMakeSize(0, 0)]; //We have no text, so there is no data
+               [[self contentView] setNeedsDisplay:YES];
+       }
+}
+
+- (void)buildTextWindowWithString:(id)text
 {
     if ( ! _locked ) {
 
+        float         divisor       = 1.0;
         float         dataWidth     = 0.0;
         float         dataHeight    = 0.0;
         NSRect        dataRect;
-        NSArray      *lines         = [text componentsSeparatedByString:@"\n"];
+        NSArray      *lines                    = [(([text isKindOfClass:[NSString class]]) ? text : [text mutableString]) componentsSeparatedByString:@"\n"];
         id                       oneLine       = nil;
-        NSEnumerator *lineEnum      = [lines objectEnumerator];
-        NSFont       *font          = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
-        NSDictionary *attr          = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
+        NSEnumerator *lineEnum         = [lines objectEnumerator];
+        float         baseFontSize  = 18.0;
         ITTextField  *textField;
-        
+        NSFont       *font;
+        NSDictionary *attr;
+
+        if ( _sizing == ITTransientStatusWindowSmall ) {
+            divisor = SMALL_DIVISOR;
+        } else if ( _sizing == ITTransientStatusWindowMini ) {
+            divisor = MINI_DIVISOR;
+        }
+               
+               font = [NSFont fontWithName:@"LucidaGrande-Bold" size:(baseFontSize / divisor)];
+               attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
+               
 //      Iterate over each line to get text width and height
         while ( (oneLine = [lineEnum nextObject]) ) {
 //          Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
         
 //      Create, position, setup, fill, and add the text view to the content view.
         textField = [[[ITTextField alloc] initWithFrame:dataRect] autorelease];
+        [textField setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
         [textField setEditable:NO];
         [textField setSelectable:NO];
         [textField setBordered:NO];
         [textField setFont:font];
         [textField setTextColor:[NSColor whiteColor]];
         [textField setCastsShadow:YES];
-        [textField setStringValue:text];
+        [[textField cell] setWraps:NO];
+               
+               if ([text isKindOfClass:[NSString class]]) {
+                       [textField setStringValue:text];
+               } else {
+                       [textField setAttributedStringValue:text];
+               }
+               
         [textField setShadowSaturation:SW_SHADOW_SAT];
         [[self contentView] addSubview:textField];
         
 //      Display the window.
         [[self contentView] setNeedsDisplay:YES];
-
+               _textField = textField;
     }
 }
 
 {
     if ( ! _locked ) {
 
-        NSFont       *font        = [NSFont fontWithName:@"Lucida Grande Bold" size:size];
-        NSDictionary *attr        = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
-        NSSize        charSize    = [character sizeWithAttributes:attr];
-        float         cellHeight  = ( charSize.height + 4.0 );                 // Add 4.0 for shadow
-        float         cellWidth   = ( (charSize.width) + SW_METER_PAD ); // Add 8.0 for Apple suck
-        float         dataWidth   = ( cellWidth * count );
-        NSRect        dataRect    = [self setupWindowWithDataSize:NSMakeSize(dataWidth, cellHeight)];
+        float         divisor     = 1.0;
+        NSFont       *font;
+        NSDictionary *attr;
+        NSSize        charSize;
+        float         cellHeight;
+        float         cellWidth;
+        float         dataWidth;
+        NSRect        dataRect;
         NSEnumerator *cellEnum    = nil;
         id            aCell       = nil;
         int           activeCount = 0;
         NSColor      *onColor     = [NSColor whiteColor];
         NSColor      *offColor    = [NSColor colorWithCalibratedWhite:0.15 alpha:0.50];
-        NSMatrix     *volMatrix   = [[[NSMatrix alloc] initWithFrame:dataRect
-                                                                mode:NSHighlightModeMatrix
-                                                           cellClass:NSClassFromString(@"ITTextFieldCell")
-                                                        numberOfRows:1
-                                                     numberOfColumns:count] autorelease];
-
+        NSMatrix     *volMatrix;
+        
+        if ( _sizing == ITTransientStatusWindowSmall ) {
+            divisor = SMALL_DIVISOR;
+        } else if ( _sizing == ITTransientStatusWindowMini ) {
+            divisor = MINI_DIVISOR;
+        }
+        
+        font        = [NSFont fontWithName:@"AppleGothic" size:( size / divisor )];
+        attr        = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
+        charSize    = [character sizeWithAttributes:attr];
+        cellHeight  = ( charSize.height + 4.0 );  // Add 4.0 for shadow
+        cellWidth   = ( (charSize.width) + (SW_METER_PAD / divisor) );
+        dataWidth   = ( cellWidth * count );
+        dataRect    = [self setupWindowWithDataSize:NSMakeSize(dataWidth, cellHeight)];
+        volMatrix   = [[[NSMatrix alloc] initWithFrame:dataRect
+                                                  mode:NSHighlightModeMatrix
+                                             cellClass:NSClassFromString(@"ITTextFieldCell")
+                                          numberOfRows:1
+                                       numberOfColumns:count] autorelease];
+        
         [volMatrix setCellSize:NSMakeSize(cellWidth, cellHeight)];
         [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
+        [volMatrix setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
 
         cellEnum = [[volMatrix cells] objectEnumerator];
 
 {
     if ( ! _locked ) {
 
+        float         divisor       = 1.0;
         float         textWidth     = 0.0;
         float         textHeight    = 0.0;
         float         okWidth       = 0.0;
         NSArray      *lines         = [message componentsSeparatedByString:@"\n"];
         id                       oneLine       = nil;
         NSEnumerator *lineEnum      = [lines objectEnumerator];
+        float         baseFontSize  = 18.0;
         ITTextField  *textField;
         ITButton     *okButton;
         ITButton     *cancelButton;
         NSColor      *textColor     = [NSColor whiteColor];
-        NSFont       *font          = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
-        NSDictionary *attr          = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
-        NSFont       *buttonFont    = [NSFont fontWithName:@"Lucida Grande Bold" size:14];
-        NSDictionary *buttonAttr    = [NSDictionary dictionaryWithObjectsAndKeys:
+        NSFont       *font;
+        NSDictionary *attr;
+        NSFont       *buttonFont;
+        NSDictionary *buttonAttr;
+        
+        if ( _sizing == ITTransientStatusWindowSmall ) {
+            divisor = SMALL_DIVISOR;
+        } else if ( _sizing == ITTransientStatusWindowMini ) {
+            divisor = MINI_DIVISOR;
+        }
+        
+        font = [NSFont fontWithName:@"LucidaGrande-Bold" size:(baseFontSize / divisor)];
+        attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
+        buttonFont = [NSFont fontWithName:@"LucidaGrande-Bold" size:(14 / divisor)];
+        buttonAttr = [NSDictionary dictionaryWithObjectsAndKeys:
             buttonFont , NSFontAttributeName,
             textColor  , NSForegroundColorAttributeName, 
             nil];
         }
         
 //      Make the buttons, set the titles, and size them to fit their titles
-        okButton     = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
-        cancelButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
+        okButton     = [[[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)] autorelease];
+        cancelButton = [[[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)] autorelease];
         [okButton     setTarget:target];
         [cancelButton setTarget:target];
         [okButton     setAction:okAction];
                                            wideButtonW,
                                            24.0)];
         [[self contentView] addSubview:okButton];
-        [[self contentView] addSubview:cancelButton];
+        if (alternateTitle) {
+            [[self contentView] addSubview:cancelButton];
+        }
 
         [self setIgnoresMouseEvents:NO];
   
     }
 }
 
+- (void)updateTime:(NSString *)time range:(NSRange)range
+{
+       NSMutableAttributedString *string = [[_textField attributedStringValue] mutableCopy];
+       [string replaceCharactersInRange:range withString:time];
+       [_textField setAttributedStringValue:[string autorelease]];
+       [[self contentView] setNeedsDisplay:YES];
+}
+
+- (NSTimeInterval)animationResizeTime:(NSRect)newFrame
+{
+    return (NSTimeInterval)0.25;
+}
 
 @end