root/CPUMonitor/trunk/CPUExpandedView.m

Revision 2747, 8.5 kB (checked in by morris, 3 years ago)

Set background color from prefs

  • Property svn:keywords set to Id Rev
Line 
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 "CPUExpandedView.h"
31 #import "CPUSettings.h"
32
33 @implementation CPUExpandedView
34
35 - (void)dealloc {
36         if(_userRects)
37                 free(_userRects);
38        
39         if(_systemRects)
40                 free(_systemRects);
41        
42         if(_niceRects)
43                 free(_niceRects);
44        
45         if(_userIconRects)
46                 free(_userIconRects);
47        
48         if(_systemIconRects)
49                 free(_systemIconRects);
50        
51         if(_niceIconRects)
52                 free(_niceIconRects);
53        
54         [super dealloc];
55 }
56
57
58
59
60 #pragma mark -
61
62 - (void)invalidate {
63         if(_userRects) {
64                 free(_userRects);
65                 _userRects = NULL;
66         }
67        
68         if(_systemRects) {
69                 free(_systemRects);
70                 _systemRects = NULL;
71         }
72        
73         if(_niceRects) {
74                 free(_niceRects);
75                 _niceRects = NULL;
76         }
77        
78         _drawn = NO;
79 }
80
81
82
83 #pragma mark -
84
85 - (void)resizeWithOldSuperviewSize:(NSSize)oldSize {
86         [self invalidate];
87        
88         [super resizeWithOldSuperviewSize:oldSize];
89 }
90
91
92
93 - (void)refresh {
94         NSRect          bounds, rect;
95        
96         if(_drawn) {
97                 bounds = [self bounds];
98                
99                 [self scrollRect:bounds by:NSMakeSize(-CPUExpandedViewLineWidth, 0.0)];
100
101                 rect = NSMakeRect(bounds.origin.x + bounds.size.width - CPUExpandedViewLineWidth,
102                                                   bounds.origin.y,
103                                                   CPUExpandedViewLineWidth,
104                                                   bounds.size.height);
105                
106                 [self setNeedsDisplayInRect:rect];
107         } else {
108                 [self setNeedsDisplay:YES];
109
110                 _drawn = YES;
111         }
112 }
113
114
115
116 - (void)drawRect:(NSRect)frame {
117         CPUDataSource   *dataSource;
118         CPUData                 *data;
119         NSBezierPath    *path;
120         NSPoint                 start, stop;
121         NSRect                  rect;
122         float                   x, y, height, offset;
123         unsigned int    i, j, count, vlines, hlines, cells, numberOfCPUs;
124         unsigned int    userRects, systemRects, niceRects;
125        
126         dataSource              = [CPUDataSource dataSource];
127         numberOfCPUs    = dataSource->_numberOfCPUs;
128        
129         height = floor((frame.size.height - ((numberOfCPUs - 1) * CPUExpandedViewDividerHeight)) / numberOfCPUs);
130         vlines = frame.size.width / CPUExpandedViewLineWidth;
131         hlines = height / CPUExpandedViewLineHeight;
132        
133         [[NSGraphicsContext currentContext] setShouldAntialias:NO];
134        
135         // --- draw background
136         [[CPUSettings objectForKey:CPUExpandedViewBackgroundColor] set];
137        
138         for(i = 0; i < numberOfCPUs; i++) {
139                 x = frame.origin.x;
140                 y = frame.origin.y + (i * height) + (i * CPUExpandedViewDividerHeight);
141                
142                 NSRectFill(NSMakeRect(x, y, frame.size.width, height));
143         }
144        
145         // --- cache rects
146         if(!_userRects) {
147                 _userRects              = (NSRect *) malloc(sizeof(NSRect) * vlines * numberOfCPUs);
148                 _systemRects    = (NSRect *) malloc(sizeof(NSRect) * vlines * numberOfCPUs);
149                 _niceRects              = (NSRect *) malloc(sizeof(NSRect) * vlines * numberOfCPUs);
150         }
151        
152         // --- draw CPU graph
153         userRects = systemRects = niceRects = 0;
154        
155         for(i = 0; i < numberOfCPUs; i++) {
156                 data    = dataSource->_data[i];
157                 offset  = (i * height) + (i * CPUExpandedViewDividerHeight);
158                
159                 for(j = data->_historyPosition - 1, count = 0; count < vlines; j--, count++) {
160                         if(j > CPUDataHistorySize)
161                                 j = CPUDataHistorySize - 1;
162                        
163                         x = frame.origin.x + frame.size.width - (CPUExpandedViewBarWidth * (count + 1)) - count - 1.0;
164                         y = frame.origin.y + offset;
165                        
166                         if(data->_niceHistory[j] > 0) {
167                                 cells = data->_niceHistory[j] * hlines;
168                                 rect = NSMakeRect(x, y, CPUExpandedViewBarWidth, cells * CPUExpandedViewLineHeight);
169                                 y += rect.size.height;
170                                 _niceRects[niceRects++] = rect;
171                         }
172                        
173                         if(data->_systemHistory[j] > 0) {
174                                 cells = data->_systemHistory[j] * hlines;
175                                 rect = NSMakeRect(x, y, CPUExpandedViewBarWidth, cells * CPUExpandedViewLineHeight);
176                                 y += rect.size.height;
177                                 _systemRects[systemRects++] = rect;
178                         }
179                        
180                         if(data->_userHistory[j] > 0) {
181                                 cells = data->_userHistory[j] * hlines;
182                                 rect = NSMakeRect(x, y, CPUExpandedViewBarWidth, cells * CPUExpandedViewLineHeight);
183                                 _userRects[userRects++] = rect;
184                         }
185                 }
186         }
187        
188         if(userRects > 0) {
189                 [[CPUSettings objectForKey:CPUExpandedViewUserColor] set];
190                 NSRectFillList(_userRects, userRects);
191         }
192        
193         if(systemRects > 0) {
194                 [[CPUSettings objectForKey:CPUExpandedViewSystemColor] set];
195                 NSRectFillList(_systemRects, systemRects);
196         }
197        
198         if(niceRects > 0) {
199                 [[CPUSettings objectForKey:CPUExpandedViewNiceColor] set];
200                 NSRectFillList(_niceRects, niceRects);
201         }
202        
203         // --- draw horizontal grid image
204         path = [[NSBezierPath alloc] init];
205        
206         [[NSColor blackColor] set];
207        
208         for(i = 0; i < numberOfCPUs; i++) {
209                 offset = (i * height) + (i * CPUExpandedViewDividerHeight);
210                
211                 for(j = 0; j < hlines + 1; j++) {
212                         y               = frame.origin.y + (CPUExpandedViewLineHeight * j) + offset + 0.5;
213                         start   = NSMakePoint(frame.origin.x + frame.size.width, y);
214                         stop    = NSMakePoint(frame.origin.x, y);
215                        
216                         [path moveToPoint:start];
217                         [path lineToPoint:stop];
218                         [path stroke];
219                         [path removeAllPoints];
220                 }
221                
222                 for(j = 0; j < vlines + 1; j++) {
223                         x               = frame.origin.x + frame.size.width - (CPUExpandedViewLineWidth * j) - 0.5;
224                         start   = NSMakePoint(x, frame.origin.y + offset + height);
225                         stop    = NSMakePoint(x, frame.origin.y + offset);
226                        
227                         [path moveToPoint:start];
228                         [path lineToPoint:stop];
229                         [path stroke];
230                         [path removeAllPoints];
231                 }
232         }
233        
234         [path release];
235 }
236
237
238
239 - (void)drawIconInRect:(NSRect)frame {
240         CPUDataSource   *dataSource;
241         CPUData                 *data;
242         NSRect                  rect;
243         float                   x, y, height, user, system, nice;
244         unsigned int    i, j, count, bars, numberOfCPUs;
245         unsigned int    userRects, systemRects, niceRects;
246        
247         dataSource              = [CPUDataSource dataSource];
248         numberOfCPUs    = dataSource->_numberOfCPUs;
249         bars                    = (frame.size.width - 2.0f) / CPUExpandedViewIconBarWidth;
250         height                  = frame.size.height - 4.0f;
251        
252         [[NSGraphicsContext currentContext] setShouldAntialias:NO];
253        
254         // --- draw background
255         [[CPUSettings objectForKey:CPUExpandedViewBackgroundColor] set];
256         NSRectFill(frame);
257        
258         [[NSColor blackColor] set];
259         NSFrameRect(frame);
260
261         // --- cache rects
262         if(!_userIconRects) {
263                 _userIconRects          = (NSRect *) malloc(sizeof(NSRect) * bars * numberOfCPUs);
264                 _systemIconRects        = (NSRect *) malloc(sizeof(NSRect) * bars * numberOfCPUs);
265                 _niceIconRects          = (NSRect *) malloc(sizeof(NSRect) * bars * numberOfCPUs);
266         }
267        
268         // --- draw CPU graph
269         userRects = systemRects = niceRects = 0;
270        
271         for(j = dataSource->_data[0]->_historyPosition - 1, count = 0; count < bars; j--, count++) {
272                 if(j > CPUDataHistorySize)
273                         j = CPUDataHistorySize - 1;
274                        
275                 user = system = nice = 0.0;
276                
277                 for(i = 0; i < numberOfCPUs; i++) {
278                         data    = dataSource->_data[i];
279                         user    += data->_userHistory[j];
280                         system  += data->_systemHistory[j];
281                         nice    += data->_niceHistory[j];
282                 }
283                
284                 user    /= numberOfCPUs;
285                 system  /= numberOfCPUs;
286                 nice    /= numberOfCPUs;
287                
288                 x = frame.origin.x + frame.size.width - 1.0 - (CPUExpandedViewIconBarWidth * (count + 1));
289                 y = frame.origin.y + 1.0;
290                
291                 if(nice > 0.0) {
292                         rect = NSMakeRect(x, y, CPUExpandedViewIconBarWidth, nice * height);
293                         y += rect.size.height;
294                         _niceIconRects[niceRects++] = rect;
295                 }
296                
297                 if(system > 0.0) {
298                         rect = NSMakeRect(x, y, CPUExpandedViewIconBarWidth, system * height);
299                         y += rect.size.height;
300                         _systemIconRects[systemRects++] = rect;
301                 }
302                
303                 if(user > 0.0) {
304                         rect = NSMakeRect(x, y, CPUExpandedViewIconBarWidth, user * height);
305                         _userIconRects[userRects++] = rect;
306                 }
307         }
308        
309         if(userRects > 0) {
310                 [[CPUSettings objectForKey:CPUExpandedViewUserColor] set];
311                 NSRectFillList(_userIconRects, userRects);
312         }
313        
314         if(systemRects > 0) {
315                 [[CPUSettings objectForKey:CPUExpandedViewSystemColor] set];
316                 NSRectFillList(_systemIconRects, systemRects);
317         }
318        
319         if(niceRects > 0) {
320                 [[CPUSettings objectForKey:CPUExpandedViewNiceColor] set];
321                 NSRectFillList(_niceIconRects, niceRects);
322         }
323 }
324
325 @end
Note: See TracBrowser for help on using the browser.