5 // Created by Matt L. Judy on Sat Feb 22 2003.
6 // Copyright (c) 2003 NibFile.com. All rights reserved.
9 #import "StatusWindow.h"
12 @interface StatusWindow (Private)
13 - (void)buildStatusWindow;
17 @implementation StatusWindow
19 - (id)initWithContentView:(NSView *)contentView
20 exitMode:(ITTransientStatusWindowExitMode)exitMode
21 backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType
23 if ( ( self = [super initWithContentView:contentView
25 backgroundType:backgroundType]) ) {
26 // Set default values.
27 windowMode = StatusWindowTextMode;
28 image = [NSImage imageNamed:@"NSApplicationIcon"];
29 text = @"No string set yet.";
32 [self buildStatusWindow];
38 - (void)buildStatusWindow
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"];
51 NSEnumerator *lineEnum = [lines objectEnumerator];
52 NSFont *font = [NSFont fontWithName:@"Lucida Grande Bold" size:18];
53 NSDictionary *attr = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
55 // Get image width and height.
56 imageWidth = [image size].width;
57 imageHeight = [image size].height;
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 );
70 // Add 4.0 to the final dataHeight to accomodate the shadow.
77 // Set the content height to the greater of the text and image heights.
78 contentHeight = ( ( imageHeight > dataHeight ) ? imageHeight : dataHeight );
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)];
86 // Setup, position, fill, and add the image view to the content view.
87 imageRect = NSMakeRect( SW_PAD,
88 (SW_PAD + ((contentHeight - imageHeight) / 2)),
91 imageView = [[[NSImageView alloc] initWithFrame:imageRect] autorelease];
92 [imageView setImage:image];
93 [[self contentView] addSubview:imageView];
95 dataRect = NSMakeRect( (SW_PAD + imageWidth + SW_SPACE),
96 (SW_PAD + ((contentHeight - dataHeight) / 2)),
100 if ( windowMode == StatusWindowTextMode ) {
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];
114 } else if ( windowMode == StatusWindowVolumeMode ) {
116 NSEnumerator *cellEnum;
118 int lights = ( ceil(volumeLevel * 100) / 10 );
121 volMatrix = [[[NSMatrix alloc] initWithFrame:dataRect
122 mode:NSHighlightModeMatrix
123 cellClass:NSClassFromString(@"ITTextFieldCell")
125 numberOfColumns:10] autorelease];
127 [volMatrix setCellSize:NSMakeSize(20, 24)];
128 [volMatrix setIntercellSpacing:NSMakeSize(0, 0)];
130 cellEnum = [[volMatrix cells] objectEnumerator];
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:"▊"]];
142 // NSLog(@"%f, %i, %i", volumeLevel, lights, lightCount);
144 if ( lights >= lightCount ) {
145 [aCell setCastsShadow:YES];
146 [aCell setTextColor:[NSColor whiteColor]];
148 [aCell setCastsShadow:NO];
149 [aCell setTextColor:[NSColor darkGrayColor]];
154 [[self contentView] addSubview:volMatrix];
157 [[self contentView] setNeedsDisplay:YES];
160 - (void)setImage:(NSImage *)newImage
163 image = [newImage copy];
164 [self buildStatusWindow];
167 - (void)setText:(NSString *)newText
170 text = [newText copy];
171 windowMode = StatusWindowTextMode;
172 [self buildStatusWindow];
175 - (void)setVolume:(float)level
178 windowMode = StatusWindowVolumeMode;
179 [self buildStatusWindow];