Added notes to the TODO. Matt, please check and modify TODO as required :)
[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      // Default images and text.
27         image = [NSImage imageNamed:@"NSApplicationIcon"];
28         text  = @"No string set yet.";
29         [self buildStatusWindow];
30     }
31     return self;
32 }
33
34 - (void)buildStatusWindow
35 {
36     NSRect        imageRect;
37     NSRect        textRect;
38     float         imageWidth    = 0.0;
39     float         imageHeight   = 0.0;
40     float         textWidth     = 0.0;
41     float         textHeight    = 0.0;
42     float         contentHeight = 0.0;
43     float         windowWidth   = 0.0;
44     float         windowHeight  = 0.0;
45     NSArray      *lines         = [text componentsSeparatedByString:@"\n"];
46     id                    oneLine       = nil;
47     NSEnumerator *lineEnum      = [lines objectEnumerator];
48     NSFont       *font          = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
49     NSDictionary *attr          = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
50     
51      // Get image width and height.
52     imageWidth  = [image size].width;
53     imageHeight = [image size].height;
54     
55      // Iterate over each line to get text width and height
56     while ( (oneLine = [lineEnum nextObject]) ) {
57          // Get the width of one line, adding 8.0 because Apple sucks donkey rectum.
58         float oneLineWidth = ( [oneLine sizeWithAttributes:attr].width + 8.0 );
59          // Add the height of this line to the total text height
60         textHeight += [oneLine sizeWithAttributes:attr].height;
61          // If this line wider than the last one, set it as the text width.
62         textWidth = ( ( textWidth > oneLineWidth ) ? textWidth : oneLineWidth );
63     }
64     
65      // Add 4.0 to the final textHeight to accomodate the shadow.
66     textHeight += 4.0;
67     
68      // Set the content height to the greater of the text and image heights.
69     contentHeight = ( ( imageHeight > textHeight ) ? imageHeight : textHeight );
70     
71      // Setup the Window, and remove all its contentview's subviews.
72     windowWidth  = ( SW_PAD + imageWidth + SW_SPACE + textWidth + SW_PAD );
73     windowHeight = ( SW_PAD + contentHeight + SW_PAD );
74     [self setFrame:NSMakeRect(SW_BORDER, SW_BORDER, windowWidth, windowHeight) display:YES];
75     [[[self contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
76     
77      // Setup, position, fill, and add the image view to the content view.
78     imageRect = NSMakeRect( SW_PAD,
79                             (SW_PAD + ((contentHeight - imageHeight) / 2)),
80                             imageWidth,
81                             imageHeight );
82     imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
83     [imageView setImage:image];
84     [[self contentView] addSubview:imageView];
85     
86      // Setup, position, fill, and add the text view to the content view.
87     textRect = NSMakeRect( (SW_PAD + imageWidth + SW_SPACE),
88                            (SW_PAD + ((contentHeight - textHeight) / 2)),
89                            textWidth,
90                            textHeight);
91     textField = [[[ITTextField alloc] initWithFrame:textRect] autorelease];
92     [textField setEditable:NO];
93     [textField setSelectable:NO];
94     [textField setBordered:NO];
95     [textField setDrawsBackground:NO];
96     [textField setFont:[NSFont fontWithName:@"Lucida Grande Bold" size:18]];
97     [textField setTextColor:[NSColor whiteColor]];
98     [textField setCastsShadow:YES];
99     [textField setStringValue:text];
100     [[self contentView] addSubview:textField];
101
102     [[self contentView] setNeedsDisplay:YES];
103     
104 }
105
106 - (void)setImage:(NSImage *)newImage
107 {
108     [image autorelease];
109     image = [newImage copy];
110     [self buildStatusWindow];
111 }
112
113 - (void)setText:(NSString *)newText
114 {
115     [text autorelease];
116     text = [newText copy];
117     [self buildStatusWindow];
118 }
119
120
121
122 @end