Volume.tiff
[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 @interface StatusWindow (Private)
13 - (void)buildStatusWindow;
14 @end
15
16
17 @implementation StatusWindow
18
19 - (id)initWithContentView:(NSView *)contentView
20                  exitMode:(ITTransientStatusWindowExitMode)exitMode
21            backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType
22 {
23     if ( ( self = [super initWithContentView:contentView
24                                exitMode:exitMode
25                          backgroundType:backgroundType]) ) {
26      // Set default values.
27         windowMode  = StatusWindowTextMode;
28         image       = [NSImage imageNamed:@"NSApplicationIcon"];
29         text        = @"No string set yet.";
30         volumeLevel = 0.0;
31         
32         [self buildStatusWindow];
33     }
34     
35     return self;
36 }
37
38 - (void)buildStatusWindow
39 {
40     NSRect        imageRect;
41     NSRect        dataRect;
42     float         imageWidth    = 0.0;
43     float         imageHeight   = 0.0;
44     float         dataWidth     = 0.0;
45     float         dataHeight    = 0.0;
46     float         contentHeight = 0.0;
47     float         windowWidth   = 0.0;
48     float         windowHeight  = 0.0;
49     NSArray      *lines         = [text componentsSeparatedByString:@"\n"];
50     id                    oneLine       = nil;
51     NSEnumerator *lineEnum      = [lines objectEnumerator];
52     NSFont       *font          = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
53     NSDictionary *attr          = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
54     
55      // Get image width and height.
56     imageWidth  = [image size].width;
57     imageHeight = [image size].height;
58
59     if ( windowMode == StatusWindowTextMode ) {
60      // Iterate over each line to get text width and height
61         while ( (oneLine = [lineEnum nextObject]) ) {
62          // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
63             float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
64          // Add the height of this line to the total text height
65             dataHeight += [oneLine sizeWithAttributes:attr].height;
66          // If this line wider than the last one, set it as the text width.
67             dataWidth = ( ( dataWidth > oneLineWidth ) ? dataWidth : oneLineWidth );
68         }
69         
70      // Add 4.0 to the final dataHeight to accomodate the shadow.
71         dataHeight += 4.0;
72     } else {
73         dataHeight = 24.0;
74         dataWidth  = 200.0;
75     }
76     
77      // Set the content height to the greater of the text and image heights.
78     contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight );
79     
80      // Setup the Window, and remove all its contentview's subviews.
81     windowWidth  = ( SW_PAD + imageWidth + SW_SPACE + dataWidth + SW_PAD );
82     windowHeight = ( SW_PAD + contentHeight + SW_PAD );
83     [self setFrame:NSMakeRect(SW_BORDER, SW_BORDER, windowWidth, windowHeight) display:YES];
84     [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
85     
86      // Setup, position, fill, and add the image view to the content view.
87     imageRect = NSMakeRect( SW_PAD,
88                             (SW_PAD + ((contentHeight - imageHeight) / 2)),
89                             imageWidth,
90                             imageHeight );
91     imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
92     [imageView setImage:image];
93     [[self contentView] addSubview:imageView];
94
95     dataRect = NSMakeRect( (SW_PAD + imageWidth + SW_SPACE),
96                            (SW_PAD + ((contentHeight - dataHeight) / 2)),
97                            dataWidth,
98                            dataHeight);
99
100     if ( windowMode == StatusWindowTextMode ) {
101     
102      // Setup, position, fill, and add the text view to the content view.
103         textField = [[[ITTextField alloc] initWithFrame:dataRect] autorelease];
104         [textField setEditable:NO];
105         [textField setSelectable:NO];
106         [textField setBordered:NO];
107         [textField setDrawsBackground:NO];
108         [textField setFont:[NSFont fontWithName:@"Lucida Grande Bold" size:18]];
109         [textField setTextColor:[NSColor whiteColor]];
110         [textField setCastsShadow:YES];
111         [textField setStringValue:text];
112         [[self contentView] addSubview:textField];
113         
114     } else if ( windowMode == StatusWindowVolumeMode ) {
115
116         NSEnumerator *cellEnum;
117         id            aCell;
118         int           lights     = ( ceil(volumeLevel * 100) / 10 );
119         int           lightCount = 0;
120
121         volMatrix = [[[NSMatrix alloc] initWithFrame:dataRect
122                                                 mode:NSHighlightModeMatrix
123                                            cellClass:NSClassFromString(@"ITTextFieldCell")
124                                         numberOfRows:1
125                                      numberOfColumns:10] autorelease];
126
127         [volMatrix setCellSize:NSMakeSize(20, 24)];
128         [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
129
130         cellEnum = [[volMatrix cells] objectEnumerator];
131         
132         while ( (aCell = [cellEnum nextObject]) ) {
133             [aCell setEditable:NO];
134             [aCell setSelectable:NO];
135             [aCell setBordered:NO];
136             [aCell setDrawsBackground:NO];
137             [aCell setFont:[NSFont fontWithName:@"Lucida Grande Bold" size:18]];
138             [aCell setStringValue:[NSString stringWithUTF8String:"▊"]];
139
140             lightCount ++;
141
142          // NSLog(@"%f, %i, %i", volumeLevel, lights, lightCount);
143
144             if ( lights >= lightCount ) {
145                 [aCell setCastsShadow:YES];
146                 [aCell setTextColor:[NSColor whiteColor]];
147             } else {
148                 [aCell setCastsShadow:NO];
149                 [aCell setTextColor:[NSColor darkGrayColor]];
150             }
151
152         }
153         
154         [[self contentView] addSubview:volMatrix];
155     }
156
157     [[self contentView] setNeedsDisplay:YES];
158 }
159
160 - (void)setImage:(NSImage *)newImage
161 {
162     [image autorelease];
163     image = [newImage copy];
164     [self buildStatusWindow];
165 }
166
167 - (void)setText:(NSString *)newText
168 {
169     [text autorelease];
170     text = [newText copy];
171     windowMode = StatusWindowTextMode;
172     [self buildStatusWindow];
173 }
174
175 - (void)setVolume:(float)level
176 {
177     volumeLevel = level;
178     windowMode = StatusWindowVolumeMode;
179     [self buildStatusWindow];
180 }
181
182 @end