Whoops, didn't remove something cleanly. Now I did. Also, the inactive bits of...
[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 @interface StatusWindow (Private)
12 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize;
13 @end
14
15 @implementation StatusWindow
16
17 /*************************************************************************/
18 #pragma mark -
19 #pragma mark INITIALIZATION / DEALLOCATION METHODS
20 /*************************************************************************/
21
22 - (id)initWithContentView:(NSView *)contentView
23                  exitMode:(ITTransientStatusWindowExitMode)exitMode
24            backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType
25 {
26     if ( ( self = [super initWithContentView:contentView
27                                exitMode:exitMode
28                          backgroundType:backgroundType] ) ) {
29      // Set default values.
30         _image       = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
31         _locked      = NO;
32     }
33     
34     return self;
35 }
36
37 - (void)dealloc
38 {
39     [_image     release];
40     [super dealloc];
41 }
42
43
44 /*************************************************************************/
45 #pragma mark -
46 #pragma mark ACCESSOR METHODS
47 /*************************************************************************/
48
49 - (void)setImage:(NSImage *)newImage
50 {
51     [_image autorelease];
52     _image = [newImage copy];
53 }
54
55 - (void)setLocked:(BOOL)flag
56 {
57     _locked = flag;
58 }
59
60
61 /*************************************************************************/
62 #pragma mark -
63 #pragma mark INSTANCE METHODS
64 /*************************************************************************/
65
66 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize
67 {
68     NSRect       imageRect;
69     float        imageWidth    = 0.0;
70     float        imageHeight   = 0.0;
71     float        dataWidth     = dataSize.width;
72     float        dataHeight    = dataSize.height;
73     float        contentHeight = 0.0;
74     float        windowWidth   = 0.0;
75     float        windowHeight  = 0.0;
76     NSImageView *imageView;
77
78 //  Get image width and height.
79     imageWidth  = [_image size].width;
80     imageHeight = [_image size].height;
81     
82 //  Set the content height to the greater of the text and image heights.
83     contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight );
84
85 //  Setup the Window, and remove all its contentview's subviews.
86     windowWidth  = ( SW_PAD + imageWidth + SW_SPACE + dataWidth + SW_PAD );
87     windowHeight = ( SW_PAD + contentHeight + SW_PAD );
88     [self setFrame:NSMakeRect(SW_BORDER, SW_BORDER, windowWidth, windowHeight) display:YES];
89     [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
90
91 //  Setup, position, fill, and add the image view to the content view.
92     imageRect = NSMakeRect( SW_PAD,
93                             (SW_PAD + ((contentHeight - imageHeight) / 2)),
94                             imageWidth,
95                             imageHeight );
96     imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
97     [imageView setImage:_image];
98     [[self contentView] addSubview:imageView];
99
100     return NSMakeRect( (SW_PAD + imageWidth + SW_SPACE),
101                        (SW_PAD + ((contentHeight - dataHeight) / 2)),
102                        dataWidth,
103                        dataHeight);
104 }
105
106 - (void)buildTextWindowWithString:(NSString *)text
107 {
108     float         dataWidth     = 0.0;
109     float         dataHeight    = 0.0;
110     NSRect        dataRect;
111     NSArray      *lines         = [text componentsSeparatedByString:@"\n"];
112     id                    oneLine       = nil;
113     NSEnumerator *lineEnum      = [lines objectEnumerator];
114     NSFont       *font          = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
115     NSDictionary *attr          = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
116     ITTextField  *textField;
117     
118 //  Iterate over each line to get text width and height
119     while ( (oneLine = [lineEnum nextObject]) ) {
120 //      Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
121         float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
122 //      Add the height of this line to the total text height
123         dataHeight += [oneLine sizeWithAttributes:attr].height;
124 //      If this line wider than the last one, set it as the text width.
125         dataWidth = ( ( dataWidth > oneLineWidth ) ? dataWidth : oneLineWidth );
126     }
127         
128 //  Add 4.0 to the final dataHeight to accomodate the shadow.
129     dataHeight += 4.0;
130     
131     dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
132     
133 //  Create, position, setup, fill, and add the text view to the content view.
134     textField = [[[ITTextField alloc] initWithFrame:dataRect] autorelease];
135     [textField setEditable:NO];
136     [textField setSelectable:NO];
137     [textField setBordered:NO];
138     [textField setDrawsBackground:NO];
139     [textField setFont:font];
140     [textField setTextColor:[NSColor whiteColor]];
141     [textField setCastsShadow:YES];
142     [textField setStringValue:text];
143     [[self contentView] addSubview:textField];
144     
145 //  Display the window.
146     [[self contentView] setNeedsDisplay:YES];
147 }
148
149 - (void)buildMeterWindowWithCharacter:(NSString *)character
150                                 count:(int)count
151                                active:(int)active
152 {
153     NSFont       *font        = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
154     NSDictionary *attr        = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
155     NSSize        charSize    = [character sizeWithAttributes:attr];
156     float         cellHeight  = ( charSize.height + 4.0 );                 // Add 4.0 for shadow
157     float         cellWidth   = ( (charSize.width) + SW_METER_PAD ); // Add 8.0 for Apple suck
158     float         dataWidth   = ( cellWidth * count );
159     NSRect        dataRect    = [self setupWindowWithDataSize:NSMakeSize(dataWidth, cellHeight)];
160     NSEnumerator *cellEnum    = nil;
161     id            aCell       = nil;
162     int           activeCount = 0;
163     NSColor      *onColor     = [NSColor whiteColor];
164     NSColor      *offColor    = [NSColor colorWithCalibratedWhite:0.0 alpha:0.50];
165     NSMatrix     *volMatrix   = [[[NSMatrix alloc] initWithFrame:dataRect
166                                                             mode:NSHighlightModeMatrix
167                                                        cellClass:NSClassFromString(@"ITTextFieldCell")
168                                                     numberOfRows:1
169                                                  numberOfColumns:count] autorelease];
170
171     [volMatrix setCellSize:NSMakeSize(cellWidth, cellHeight)];
172     [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
173
174     cellEnum = [[volMatrix cells] objectEnumerator];
175
176     while ( (aCell = [cellEnum nextObject]) ) {
177         [aCell setEditable:NO];
178         [aCell setSelectable:NO];
179         [aCell setBordered:NO];
180         [aCell setDrawsBackground:NO];
181         [aCell setAlignment:NSCenterTextAlignment];
182         [aCell setFont:font];
183         [aCell setStringValue:character];
184
185         activeCount ++;
186         
187         if ( active >= activeCount ) {
188             [aCell setCastsShadow:YES];
189             [aCell setTextColor:onColor];
190         } else {
191             [aCell setCastsShadow:NO];
192             [aCell setTextColor:offColor];
193         }
194         
195     }
196
197     [[self contentView] addSubview:volMatrix];
198     [[self contentView] setNeedsDisplay:YES];
199 }
200
201 - (void)buildDialogWindowWithMessage:(NSString *)message
202                        defaultButton:(NSString *)defaultTitle
203                      alternateButton:(NSString *)alternateTitle
204                               target:(id)target
205                        defaultAction:(SEL)okAction
206                      alternateAction:(SEL)alternateAction
207 {
208
209 }
210
211
212 @end