Hooked up the setup query window
[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 + [[self screen] visibleFrame].origin.x),
107                                (SW_BORDER + [[self screen] visibleFrame].origin.y),
108                                windowWidth,
109                                windowHeight) display:YES];
110     [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
111
112 //  Setup, position, fill, and add the image view to the content view.
113     imageRect = NSMakeRect( SW_PAD,
114                             (SW_PAD + ((contentHeight - imageHeight) / 2)),
115                             imageWidth,
116                             imageHeight );
117     imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
118     [imageView setImage:_image];
119     [[self contentView] addSubview:imageView];
120
121     return NSMakeRect( (SW_PAD + imageWidth + SW_SPACE),
122                        (SW_PAD + ((contentHeight - dataHeight) / 2)),
123                        dataWidth,
124                        dataHeight);
125 }
126
127 - (void)buildTextWindowWithString:(NSString *)text
128 {
129     if ( ! _locked ) {
130
131         float         dataWidth     = 0.0;
132         float         dataHeight    = 0.0;
133         NSRect        dataRect;
134         NSArray      *lines         = [text componentsSeparatedByString:@"\n"];
135         id                        oneLine       = nil;
136         NSEnumerator *lineEnum      = [lines objectEnumerator];
137         NSFont       *font          = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
138         NSDictionary *attr          = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
139         ITTextField  *textField;
140         
141 //      Iterate over each line to get text width and height
142         while ( (oneLine = [lineEnum nextObject]) ) {
143 //          Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
144             float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
145 //          Add the height of this line to the total text height
146             dataHeight += [oneLine sizeWithAttributes:attr].height;
147 //          If this line wider than the last one, set it as the text width.
148             dataWidth = ( ( dataWidth > oneLineWidth ) ? dataWidth : oneLineWidth );
149         }
150         
151 //      Add 4.0 to the final dataHeight to accomodate the shadow.
152         dataHeight += 4.0;
153
154         dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
155         
156 //      Create, position, setup, fill, and add the text view to the content view.
157         textField = [[[ITTextField alloc] initWithFrame:dataRect] autorelease];
158         [textField setEditable:NO];
159         [textField setSelectable:NO];
160         [textField setBordered:NO];
161         [textField setDrawsBackground:NO];
162         [textField setFont:font];
163         [textField setTextColor:[NSColor whiteColor]];
164         [textField setCastsShadow:YES];
165         [textField setStringValue:text];
166         [textField setShadowSaturation:SW_SHADOW_SAT];
167         [[self contentView] addSubview:textField];
168         
169 //      Display the window.
170         [[self contentView] setNeedsDisplay:YES];
171
172     }
173 }
174
175 - (void)buildMeterWindowWithCharacter:(NSString *)character
176                                  size:(float)size
177                                 count:(int)count
178                                active:(int)active
179 {
180     if ( ! _locked ) {
181
182         NSFont       *font        = [NSFont fontWithName:@"Lucida Grande Bold" size:size];
183         NSDictionary *attr        = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
184         NSSize        charSize    = [character sizeWithAttributes:attr];
185         float         cellHeight  = ( charSize.height + 4.0 );                 // Add 4.0 for shadow
186         float         cellWidth   = ( (charSize.width) + SW_METER_PAD ); // Add 8.0 for Apple suck
187         float         dataWidth   = ( cellWidth * count );
188         NSRect        dataRect    = [self setupWindowWithDataSize:NSMakeSize(dataWidth, cellHeight)];
189         NSEnumerator *cellEnum    = nil;
190         id            aCell       = nil;
191         int           activeCount = 0;
192         NSColor      *onColor     = [NSColor whiteColor];
193         NSColor      *offColor    = [NSColor colorWithCalibratedWhite:0.15 alpha:0.50];
194         NSMatrix     *volMatrix   = [[[NSMatrix alloc] initWithFrame:dataRect
195                                                                 mode:NSHighlightModeMatrix
196                                                            cellClass:NSClassFromString(@"ITTextFieldCell")
197                                                         numberOfRows:1
198                                                      numberOfColumns:count] autorelease];
199
200         [volMatrix setCellSize:NSMakeSize(cellWidth, cellHeight)];
201         [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
202
203         cellEnum = [[volMatrix cells] objectEnumerator];
204
205         while ( (aCell = [cellEnum nextObject]) ) {
206             [aCell setEditable:NO];
207             [aCell setSelectable:NO];
208             [aCell setBordered:NO];
209             [aCell setDrawsBackground:NO];
210             [aCell setAlignment:NSCenterTextAlignment];
211             [aCell setFont:font];
212             [aCell setStringValue:character];
213             [aCell setShadowSaturation:SW_SHADOW_SAT];
214
215             activeCount ++;
216
217             if ( active >= activeCount ) {
218                 [aCell setCastsShadow:YES];
219                 [aCell setTextColor:onColor];
220             } else {
221                 [aCell setCastsShadow:NO];
222                 [aCell setTextColor:offColor];
223             }
224
225         }
226
227         [[self contentView] addSubview:volMatrix];
228         [[self contentView] setNeedsDisplay:YES];
229         
230     }
231 }
232
233 - (void)buildDialogWindowWithMessage:(NSString *)message
234                        defaultButton:(NSString *)defaultTitle
235                      alternateButton:(NSString *)alternateTitle
236                               target:(id)target
237                        defaultAction:(SEL)okAction
238                      alternateAction:(SEL)alternateAction
239 {
240     if ( ! _locked ) {
241
242         float         textWidth     = 0.0;
243         float         textHeight    = 0.0;
244         float         okWidth       = 0.0;
245         float         cancelWidth   = 0.0;
246         float         wideButtonW   = 0.0;
247         float         buttonWidth   = 0.0;
248         float         dataHeight    = 0.0;
249         float         dataWidth     = 0.0;
250         NSRect        dataRect;
251         float         textY         = 0.0;
252         NSRect        textRect;
253         float         textAddBelow  = 32.0;
254         float         dataMinH      = 92.0;
255         float         textMinH      = 48.0;
256         NSArray      *lines         = [message componentsSeparatedByString:@"\n"];
257         id                        oneLine       = nil;
258         NSEnumerator *lineEnum      = [lines objectEnumerator];
259         ITTextField  *textField;
260         ITButton     *okButton;
261         ITButton     *cancelButton;
262         NSColor      *textColor     = [NSColor whiteColor];
263         NSFont       *font          = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
264         NSDictionary *attr          = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
265         NSFont       *buttonFont    = [NSFont fontWithName:@"Lucida Grande Bold" size:14];
266         NSDictionary *buttonAttr    = [NSDictionary dictionaryWithObjectsAndKeys:
267             buttonFont , NSFontAttributeName,
268             textColor  , NSForegroundColorAttributeName, 
269             nil];
270         
271 //      Iterate over each line to get text width and height
272         while ( (oneLine = [lineEnum nextObject]) ) {
273 //          Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
274             float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
275 //          Add the height of this line to the total text height
276             textHeight += [oneLine sizeWithAttributes:attr].height;
277 //          If this line wider than the last one, set it as the text width.
278             textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
279         }
280         
281 //      Add 4.0 to the final dataHeight to accomodate the shadow.
282         textHeight += 4.0;
283         
284 //      Add extra padding below the text
285         dataHeight = (textHeight + textAddBelow);
286         
287 //      Test to see if data height is tall enough
288         if ( dataHeight < dataMinH ) {
289             dataHeight = dataMinH;
290         }
291         
292 //      Make the buttons, set the titles, and size them to fit their titles
293         okButton     = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
294         cancelButton = [[ITButton alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
295         [okButton     setTarget:target];
296         [cancelButton setTarget:target];
297         [okButton     setAction:okAction];
298         [cancelButton setAction:alternateAction];
299         [okButton     setBezelStyle:ITGrayRoundedBezelStyle];
300         [cancelButton setBezelStyle:ITGrayRoundedBezelStyle];
301         [okButton     setAlignment:NSRightTextAlignment];
302         [cancelButton setAlignment:NSCenterTextAlignment];
303         [okButton     setImagePosition:NSNoImage];
304         [cancelButton setImagePosition:NSNoImage];
305         [okButton     setAttributedTitle:[[[NSAttributedString alloc] initWithString:defaultTitle
306                                                                           attributes:buttonAttr] autorelease]];
307         [cancelButton setAttributedTitle:[[[NSAttributedString alloc] initWithString:alternateTitle
308                                                                           attributes:buttonAttr] autorelease]];
309         [okButton     sizeToFit];
310         [cancelButton sizeToFit];
311         
312 //      Get the button widths.  Add any extra width here.
313         okWidth     = ([okButton     frame].size.width + SW_BUTTON_EXTRA_W);
314         cancelWidth = ([cancelButton frame].size.width + SW_BUTTON_EXTRA_W);
315         
316 //      Figure out which button is wider.
317         wideButtonW = ( (okWidth > cancelWidth) ? okWidth : cancelWidth );
318
319 //      Get the total width of the buttons. Add the divider space.
320         buttonWidth = ( (wideButtonW * 2) + SW_BUTTON_DIV );
321
322 //      Set the dataWidth to whichever is greater: text width or button width.
323         dataWidth = ( (textWidth > buttonWidth) ? textWidth : buttonWidth);
324         
325 //      Setup the window
326         dataRect = [self setupWindowWithDataSize:NSMakeSize(dataWidth, dataHeight)];
327         
328 //      Set an initial vertical point for the textRect's origin.
329         textY = dataRect.origin.y + textAddBelow;
330         
331 //      Move that point up if the minimimum height of the text area is not occupied.
332         if ( textHeight < textMinH ) {
333             textY += ( (textMinH - textHeight) / 2 );
334         }
335         
336 //      Build the text rect.
337         textRect = NSMakeRect(dataRect.origin.x,
338                               textY,
339                               textWidth,
340                               textHeight);
341         
342 //      Create, position, setup, fill, and add the text view to the content view.
343         textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
344         [textField setEditable:NO];
345         [textField setSelectable:NO];
346         [textField setBordered:NO];
347         [textField setDrawsBackground:NO];
348         [textField setFont:font];
349         [textField setTextColor:textColor];
350         [textField setCastsShadow:YES];
351         [textField setStringValue:message];
352         [textField setShadowSaturation:SW_SHADOW_SAT];
353         [[self contentView] addSubview:textField];
354         
355 //      Set the button frames, and add them to the content view.
356         [okButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - (wideButtonW + SW_BUTTON_PAD_R) ),
357                                        SW_BUTTON_PAD_B,
358                                        wideButtonW,
359                                        24.0)];
360         [cancelButton setFrame:NSMakeRect( ([[self contentView] frame].size.width - ((wideButtonW * 2) + SW_BUTTON_DIV + SW_BUTTON_PAD_R) ),
361                                            SW_BUTTON_PAD_B,
362                                            wideButtonW,
363                                            24.0)];
364         [[self contentView] addSubview:okButton];
365         [[self contentView] addSubview:cancelButton];
366
367         [self setIgnoresMouseEvents:NO];
368   
369 //      Display the window.
370         [[self contentView] setNeedsDisplay:YES];
371     }
372 }
373
374
375 @end