| 1 |
/* $Id$ */ |
|---|
| 2 |
|
|---|
| 3 |
/* |
|---|
| 4 |
* Copyright (c) 2005 Axel Andersson |
|---|
| 5 |
* All rights reserved. |
|---|
| 6 |
* |
|---|
| 7 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 8 |
* modification, are permitted provided that the following conditions |
|---|
| 9 |
* are met: |
|---|
| 10 |
* 1. Redistributions of source code must retain the above copyright |
|---|
| 11 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 13 |
* notice, this list of conditions and the following disclaimer in the |
|---|
| 14 |
* documentation and/or other materials provided with the distribution. |
|---|
| 15 |
* |
|---|
| 16 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|---|
| 17 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 18 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|---|
| 19 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
|---|
| 20 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|---|
| 21 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|---|
| 22 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 23 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|---|
| 24 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|---|
| 25 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|---|
| 26 |
* POSSIBILITY OF SUCH DAMAGE. |
|---|
| 27 |
*/ |
|---|
| 28 |
|
|---|
| 29 |
#import "CPUDataSource.h" |
|---|
| 30 |
#import "CPUFloatingView.h" |
|---|
| 31 |
#import "CPUSettings.h" |
|---|
| 32 |
|
|---|
| 33 |
@implementation CPUFloatingView |
|---|
| 34 |
|
|---|
| 35 |
- (id)initWithFrame:(NSRect)frame isHorizontal:(BOOL)horizontal { |
|---|
| 36 |
self = [super initWithFrame:frame]; |
|---|
| 37 |
|
|---|
| 38 |
[self setIsHorizontal:horizontal]; |
|---|
| 39 |
|
|---|
| 40 |
return self; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
- (void)dealloc { |
|---|
| 46 |
[_backgroundImage release]; |
|---|
| 47 |
[_color release]; |
|---|
| 48 |
|
|---|
| 49 |
[super dealloc]; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
#pragma mark - |
|---|
| 55 |
|
|---|
| 56 |
- (void)setIsHorizontal:(BOOL)horizontal { |
|---|
| 57 |
NSRect frame; |
|---|
| 58 |
unsigned int numberOfCPUs; |
|---|
| 59 |
|
|---|
| 60 |
frame = [self frame]; |
|---|
| 61 |
numberOfCPUs = [CPUDataSource dataSource]->_numberOfCPUs; |
|---|
| 62 |
|
|---|
| 63 |
if(horizontal) { |
|---|
| 64 |
frame.size.width = CPUFloatingViewLength; |
|---|
| 65 |
frame.size.height = (numberOfCPUs * CPUFloatingViewThickness) - (numberOfCPUs - 1); |
|---|
| 66 |
} else { |
|---|
| 67 |
frame.size.width = (numberOfCPUs * CPUFloatingViewThickness) - (numberOfCPUs - 1); |
|---|
| 68 |
frame.size.height = CPUFloatingViewLength; |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
_horizontal = horizontal; |
|---|
| 72 |
|
|---|
| 73 |
[self invalidate]; |
|---|
| 74 |
[self setFrame:frame]; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
- (BOOL)isHorizontal { |
|---|
| 80 |
return _horizontal; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
#pragma mark - |
|---|
| 86 |
|
|---|
| 87 |
- (void)invalidate { |
|---|
| 88 |
[_backgroundImage release]; |
|---|
| 89 |
_backgroundImage = NULL; |
|---|
| 90 |
|
|---|
| 91 |
[_foregroundImage release]; |
|---|
| 92 |
_foregroundImage = NULL; |
|---|
| 93 |
|
|---|
| 94 |
[_color release]; |
|---|
| 95 |
_color = NULL; |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
#pragma mark - |
|---|
| 101 |
|
|---|
| 102 |
- (void)drawRect:(NSRect)frame { |
|---|
| 103 |
CPUDataSource *dataSource; |
|---|
| 104 |
CPUData *data; |
|---|
| 105 |
NSBezierPath *path; |
|---|
| 106 |
NSPoint start, stop; |
|---|
| 107 |
float x, y, width, height; |
|---|
| 108 |
unsigned int i, numberOfCPUs; |
|---|
| 109 |
|
|---|
| 110 |
dataSource = [CPUDataSource dataSource]; |
|---|
| 111 |
numberOfCPUs = dataSource->_numberOfCPUs; |
|---|
| 112 |
|
|---|
| 113 |
// --- draw background image |
|---|
| 114 |
if(!_backgroundImage) { |
|---|
| 115 |
_backgroundImage = [[NSImage alloc] initWithSize:frame.size]; |
|---|
| 116 |
[_backgroundImage lockFocus]; |
|---|
| 117 |
|
|---|
| 118 |
[[NSColor colorWithCalibratedWhite:0.67 alpha:1.0] set]; |
|---|
| 119 |
NSRectFill(frame); |
|---|
| 120 |
|
|---|
| 121 |
[[NSColor blackColor] set]; |
|---|
| 122 |
|
|---|
| 123 |
for(i = 0; i < numberOfCPUs; i++) { |
|---|
| 124 |
if(_horizontal) { |
|---|
| 125 |
x = frame.origin.x; |
|---|
| 126 |
y = frame.origin.y + (i * CPUFloatingViewThickness) - (i > 0 ? i : 0); |
|---|
| 127 |
width = CPUFloatingViewLength; |
|---|
| 128 |
height = CPUFloatingViewThickness; |
|---|
| 129 |
} else { |
|---|
| 130 |
x = frame.origin.y + (i * CPUFloatingViewThickness) - (i > 0 ? i : 0); |
|---|
| 131 |
y = frame.origin.x; |
|---|
| 132 |
width = CPUFloatingViewThickness; |
|---|
| 133 |
height = CPUFloatingViewLength; |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
NSFrameRect(NSMakeRect(x, y, width, height)); |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
[_backgroundImage unlockFocus]; |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
[(NSImageRep *) [[_backgroundImage representations] objectAtIndex:0] drawAtPoint:frame.origin]; |
|---|
| 143 |
|
|---|
| 144 |
// --- cache color |
|---|
| 145 |
if(!_color) |
|---|
| 146 |
_color = [[CPUSettings objectForKey:CPUFloatingViewColor] retain]; |
|---|
| 147 |
|
|---|
| 148 |
// --- draw CPU bar |
|---|
| 149 |
[_color set]; |
|---|
| 150 |
|
|---|
| 151 |
for(i = 0; i < numberOfCPUs; i++) { |
|---|
| 152 |
data = dataSource->_data[i]; |
|---|
| 153 |
|
|---|
| 154 |
if(_horizontal) { |
|---|
| 155 |
width = (data->_user + data->_system + data->_nice) * (CPUFloatingViewLength - 2.0f); |
|---|
| 156 |
height = CPUFloatingViewThickness - 2.0f; |
|---|
| 157 |
x = frame.origin.x + 1.0f; |
|---|
| 158 |
y = frame.origin.y + 1.0f + (i * height) + i; |
|---|
| 159 |
} else { |
|---|
| 160 |
width = CPUFloatingViewThickness - 2.0f; |
|---|
| 161 |
height = (data->_user + data->_system + data->_nice) * (CPUFloatingViewLength - 2.0f); |
|---|
| 162 |
x = frame.origin.y + 1.0f + (i * width) + i; |
|---|
| 163 |
y = frame.origin.x + 1.0f; |
|---|
| 164 |
} |
|---|
| 165 |
|
|---|
| 166 |
NSRectFill(NSMakeRect(x, y, width, height)); |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
// --- draw foreground image |
|---|
| 170 |
if(!_foregroundImage) { |
|---|
| 171 |
_foregroundImage = [[NSImage alloc] initWithSize:frame.size]; |
|---|
| 172 |
[_foregroundImage lockFocus]; |
|---|
| 173 |
path = [[NSBezierPath alloc] init]; |
|---|
| 174 |
|
|---|
| 175 |
[[NSColor blackColor] set]; |
|---|
| 176 |
|
|---|
| 177 |
for(i = 0; i < CPUFloatingViewLines; i++) { |
|---|
| 178 |
if(_horizontal) { |
|---|
| 179 |
x = frame.origin.x + 0.5f + ((i + 1) * CPUFloatingViewLineInterval); |
|---|
| 180 |
start = NSMakePoint(x, frame.origin.y + frame.size.height); |
|---|
| 181 |
stop = NSMakePoint(x, frame.origin.y); |
|---|
| 182 |
} else { |
|---|
| 183 |
y = frame.origin.y + 0.5f + ((i + 1) * CPUFloatingViewLineInterval); |
|---|
| 184 |
start = NSMakePoint(frame.origin.x + frame.size.height, y); |
|---|
| 185 |
stop = NSMakePoint(frame.origin.x, y); |
|---|
| 186 |
} |
|---|
| 187 |
|
|---|
| 188 |
[path moveToPoint:start]; |
|---|
| 189 |
[path lineToPoint:stop]; |
|---|
| 190 |
[path stroke]; |
|---|
| 191 |
[path removeAllPoints]; |
|---|
| 192 |
} |
|---|
| 193 |
|
|---|
| 194 |
[path release]; |
|---|
| 195 |
[_foregroundImage unlockFocus]; |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
[(NSImageRep *) [[_foregroundImage representations] objectAtIndex:0] drawAtPoint:frame.origin]; |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
@end |
|---|