Some minor stuff
[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 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize;
14 @end
15
16
17 @implementation StatusWindow
18
19
20 /*************************************************************************/
21 #pragma mark -
22 #pragma mark INITIALIZATION / DEALLOCATION METHODS
23 /*************************************************************************/
24
25 - (id)initWithContentView:(NSView *)contentView
26                  exitMode:(ITTransientStatusWindowExitMode)exitMode
27            backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType
28 {
29     if ( ( self = [super initWithContentView:contentView
30                                exitMode:exitMode
31                          backgroundType:backgroundType] ) ) {
32      // Set default values.
33         _image       = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
34         _locked      = NO;
35     }
36     
37     return self;
38 }
39
40 - (void)dealloc
41 {
42     [_image     release];
43     [super dealloc];
44 }
45
46
47 /*************************************************************************/
48 #pragma mark -
49 #pragma mark ACCESSOR METHODS
50 /*************************************************************************/
51
52 - (void)setImage:(NSImage *)newImage
53 {
54     [_image autorelease];
55     _image = [newImage copy];
56 }
57
58 - (void)setLocked:(BOOL)flag
59 {
60     _locked = flag;
61     [self setExitMode:(flag ? ITTransientStatusWindowExitOnCommand : ITTransientStatusWindowExitAfterDelay)];
62 }
63
64
65 /*************************************************************************/
66 #pragma mark -
67 #pragma mark INSTANCE METHODS
68 /*************************************************************************/
69
70 - (void)appear:(id)sender
71 {
72     if ( ! _locked ) {
73         [super appear:sender];
74     }
75 }
76
77 - (void)vanish:(id)sender
78 {
79     if ( ! _locked ) {
80         [super vanish:sender];
81     }
82 }
83
84 - (NSRect)setupWindowWithDataSize:(NSSize)dataSize
85 {
86     NSRect       imageRect;
87     float        imageWidth    = 0.0;
88     float        imageHeight   = 0.0;
89     float        dataWidth     = dataSize.width;
90     float        dataHeight    = dataSize.height;
91     float        contentHeight = 0.0;
92     float        windowWidth   = 0.0;
93     float        windowHeight  = 0.0;
94     NSImageView *imageView;
95
96 //  Get image width and height.
97     imageWidth  = [_image size].width;
98     imageHeight = [_image size].height;
99     
100 //  Set the content height to the greater of the text and image heights.
101     contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight );
102
103 //  Setup the Window, and remove all its contentview's subviews.
104     windowWidth  = ( SW_PAD + imageWidth + SW_SPACE + dataWidth + SW_PAD );
105     windowHeight = ( SW_PAD + contentHeight + SW_PAD );
106     [self setFrame:NSMakeRect(SW_BORDER, SW_BORDER, windowWidth, windowHeight) display:YES];
107     [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
108
109 //  Setup, position, fill, and add the image view to the content view.
110     imageRect = NSMakeRect( SW_PAD,
111                             (SW_PAD + ((contentHeight - imageHeight) / 2)),
112                             imageWidth,
113                             imageHeight );
114     imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
115     [imageView setImage:_image];
116     [[self contentView] addSubview:imageView];
117
118     return NSMakeRect( (SW_PAD + imageWidth + SW_SPACE),
119                        (SW_PAD + ((contentHeight - dataHeight) / 2)),
120                        dataWidth,
121                        dataHeight);
122 }
123
124 - (void)buildTextWindowWithString:(NSString *)text
125 {
126     if ( ! _locked ) {
127
128         float         dataWidth     = 0.0;
129         float         dataHeight    = 0.0;
130         NSRect        dataRect;
131         NSArray      *lines         = [text componentsSeparatedByString:@"\n"];
132         id                        oneLine       = nil;
133         NSEnumerator *lineEnum      = [lines objectEnumerator];
134         NSFont       *font          = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
135         NSDictionary *attr          = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
136         ITTextField  *textField;
137         
138 //      Iterate over each line to get text width and height
139         while ( (oneLine = [lineEnum nextObject]) ) {
140 //          Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
141             float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
142 //          Add the height of this line to the total text height
143             dataHeight += [oneLine sizeWithAttributes:attr].height;
144 //          If this line wider than the last one, set it as the text width.
145             dataWidth = ( ( dataWidth > oneLineWidth ) ? dataWidth : oneLineWidth );
146         }
147         
148 //      Add 4.0 to the final dataHeight to accomodate the shadow.
149         dataHeight += 4.0;
150
151         dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
152         
153 //      Create, position, setup, fill, and add the text view to the content view.
154         textField = [[[ITTextField alloc] initWithFrame:dataRect] autorelease];
155         [textField setEditable:NO];
156         [textField setSelectable:NO];
157         [textField setBordered:NO];
158         [textField setDrawsBackground:NO];
159         [textField setFont:font];
160         [textField setTextColor:[NSColor whiteColor]];
161         [textField setCastsShadow:YES];
162         [textField setStringValue:text];
163         [textField setShadowSaturation:SW_SHADOW_SAT];
164         [[self contentView] addSubview:textField];
165         
166 //      Display the window.
167         [[self contentView] setNeedsDisplay:YES];
168
169     }
170 }
171
172 - (void)buildMeterWindowWithCharacter:(NSString *)character
173                                  size:(float)size
174                                 count:(int)count
175                                active:(int)active
176 {
177     if ( ! _locked ) {
178
179         NSFont       *font        = [NSFont fontWithName:@"Lucida Grande Bold" size:size];
180         NSDictionary *attr        = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
181         NSSize        charSize    = [character sizeWithAttributes:attr];
182         float         cellHeight  = ( charSize.height + 4.0 );                 // Add 4.0 for shadow
183         float         cellWidth   = ( (charSize.width) + SW_METER_PAD ); // Add 8.0 for Apple suck
184         float         dataWidth   = ( cellWidth * count );
185         NSRect        dataRect    = [self setupWindowWithDataSize:NSMakeSize(dataWidth, cellHeight)];
186         NSEnumerator *cellEnum    = nil;
187         id            aCell       = nil;
188         int           activeCount = 0;
189         NSColor      *onColor     = [NSColor whiteColor];
190         NSColor      *offColor    = [NSColor colorWithCalibratedWhite:0.15 alpha:0.50];
191         NSMatrix     *volMatrix   = [[[NSMatrix alloc] initWithFrame:dataRect
192                                                                 mode:NSHighlightModeMatrix
193                                                            cellClass:NSClassFromString(@"ITTextFieldCell")
194                                                         numberOfRows:1
195                                                      numberOfColumns:count] autorelease];
196
197         [volMatrix setCellSize:NSMakeSize(cellWidth, cellHeight)];
198         [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
199
200         cellEnum = [[volMatrix cells] objectEnumerator];
201
202         while ( (aCell = [cellEnum nextObject]) ) {
203             [aCell setEditable:NO];
204             [aCell setSelectable:NO];
205             [aCell setBordered:NO];
206             [aCell setDrawsBackground:NO];
207             [aCell setAlignment:NSCenterTextAlignment];
208             [aCell setFont:font];
209             [aCell setStringValue:character];
210             [aCell setShadowSaturation:SW_SHADOW_SAT];
211
212             activeCount ++;
213
214             if ( active >= activeCount ) {
215                 [aCell setCastsShadow:YES];
216                 [aCell setTextColor:onColor];
217             } else {
218                 [aCell setCastsShadow:NO];
219                 [aCell setTextColor:offColor];
220             }
221
222         }
223
224         [[self contentView] addSubview:volMatrix];
225         [[self contentView] setNeedsDisplay:YES];
226         
227     }
228 }
229
230 - (void)buildDialogWindowWithMessage:(NSString *)message
231                        defaultButton:(NSString *)defaultTitle
232                      alternateButton:(NSString *)alternateTitle
233                               target:(id)target
234                        defaultAction:(SEL)okAction
235                      alternateAction:(SEL)alternateAction
236 {
237     if ( ! _locked ) {
238
239         float         textWidth     = 0.0;
240         float         textHeight    = 0.0;
241         float         okWidth       = 0.0;
242         float         cancelWidth   = 0.0;
243         float         wideButtonW   = 0.0;
244         float         buttonWidth   = 0.0;
245         float         dataHeight    = 0.0;
246         float         dataWidth     = 0.0;
247         NSRect        dataRect;
248         float         textY         = 0.0;
249         NSRect        textRect;
250         float         textAddBelow  = 32.0;
251         float         dataMinH      = 92.0;
252         float         textMinH      = 48.0;
253         NSArray      *lines         = [message componentsSeparatedByString:@"\n"];
254         id                        oneLine       = nil;
255         NSEnumerator *lineEnum      = [lines objectEnumerator];
256         ITTextField  *textField;
257         ITButton     *okButton;
258         ITButton     *cancelButton;
259         NSColor      *textColor     = [NSColor whiteColor];
260         NSFont       *font          = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
261         NSDictionary *attr          = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
262         NSFont       *buttonFont    = [NSFont fontWithName:@"Lucida Grande Bold" size:14];
263         NSDictionary *buttonAttr    = [NSDictionary dictionaryWithObjectsAndKeys:
264             buttonFont , NSFontAttributeName,
265             textColor  , NSForegroundColorAttributeName, 
266             nil];
267         
268 //      Iterate over each line to get text width and height
269         while ( (oneLine = [lineEnum nextObject]) ) {
270 //          Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
271             float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
272 //          Add the height of this line to the total text height
273             textHeight += [oneLine sizeWithAttributes:attr].height;
274 //          If this line wider than the last one, set it as the text width.
275             textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
276         }
277         
278 //      Add 4.0 to the final dataHeight to accomodate the shadow.
279         textHeight += 4.0;
280         
281 //      Add extra padding below the text
282         dataHeight = (textHeight + textAddBelow);
283         
284 //      Test to see if data height is tall enough
285         if ( dataHeight < dataMinH ) {
286             dataHeight = dataMinH;
287         }
288         
289 //      Make the buttons, set the titles, and size them to fit their titles
290         okButton     = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
291         cancelButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
292         [okButton     setBezelStyle:ITGrayRoundedBezelStyle];
293         [cancelButton setBezelStyle:ITGrayRoundedBezelStyle];
294         [okButton     setAlignment:NSRightTextAlignment];
295         [cancelButton setAlignment:NSCenterTextAlignment];
296         [okButton     setImagePosition:NSNoImage];
297         [cancelButton setImagePosition:NSNoImage];
298         [okButton     setAttributedTitle:[[[NSAttributedString alloc] initWithString:defaultTitle
299                                                                           attributes:buttonAttr] autorelease]];
300         [cancelButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:alternateTitle
301                                                                           attributes:buttonAttr] autorelease]];
302         [okButton     sizeToFit];
303         [cancelButton sizeToFit];
304         
305 //      Get the button widths.  Add any extra width here.
306         okWidth     = ([okButton     frame].size.width + SW_BUTTON_EXTRA_W);
307         cancelWidth = ([cancelButton frame].size.width + SW_BUTTON_EXTRA_W);
308         
309 //      Figure out which button is wider.
310         wideButtonW = ( (okWidth > cancelWidth) ? okWidth : cancelWidth );
311
312 //      Get the total width of the buttons. Add the divider space.
313         buttonWidth = ( (wideButtonW * 2) + SW_BUTTON_DIV );
314
315 //      Set the dataWidth to whichever is greater: text width or button width.
316         dataWidth = ( (textWidth > buttonWidth) ? textWidth : buttonWidth);
317         
318 //      Setup the window
319         dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
320         
321 //      Set an initial vertical point for the textRect's origin.
322         textY = dataRect.origin.y + textAddBelow;
323         
324 //      Move that point up if the minimimum height of the text area is not occupied.
325         if ( textHeight < textMinH ) {
326             textY += ( (textMinH - textHeight) / 2 );
327         }
328         
329 //      Build the text rect.
330         textRect = NSMakeRect(dataRect.origin.x,
331                               textY,
332                               textWidth,
333                               textHeight);
334         
335 //      Create, position, setup, fill, and add the text view to the content view.
336         textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
337         [textField setEditable:NO];
338         [textField setSelectable:NO];
339         [textField setBordered:NO];
340         [textField setDrawsBackground:NO];
341         [textField setFont:font];
342         [textField setTextColor:textColor];
343         [textField setCastsShadow:YES];
344         [textField setStringValue:message];
345         [textField setShadowSaturation:SW_SHADOW_SAT];
346         [[self contentView] addSubview:textField];
347         
348 //      Set the button frames, and add them to the content view.
349         [okButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - (wideButtonW + SW_BUTTON_PAD_R) ),
350                                        SW_BUTTON_PAD_B,
351                                        wideButtonW,
352                                        24.0)];
353         [cancelButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - ((wideButtonW * 2) + SW_BUTTON_DIV + SW_BUTTON_PAD_R) ),
354                                            SW_BUTTON_PAD_B,
355                                            wideButtonW,
356                                            24.0)];
357         [[self contentView] addSubview:okButton];
358         [[self contentView] addSubview:cancelButton];
359         NSLog(@"%@", [[self contentView] description]);
360
361         [self setIgnoresMouseEvents:NO];
362   
363 //      Display the window.
364         [[self contentView] setNeedsDisplay:YES];
365     }
366 }
367
368
369 @end