public
Description: OS X: displays recent CPU usage in the Dock or a floating window
Homepage: http://cbowns.com/cpuhistory/
Clone URL: git://github.com/cbowns/cpu-history.git
cpu-history / TranslucentView.m
100644 116 lines (94 sloc) 3.254 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// TranslucentView.m
//
// Created by Takashi T. Hamada on Thu Nov 01 2000.
// Copyright (c) 2000,2001 Takashi T. Hamada. All rights reserved.
//
// Modifications:
// bb 25.06.2002 - added acceptsFirstMouse method for click-through
// bb 26.06.2002 - removed the isOpaque method
//
 
#import "TranslucentView.h"
 
 
@implementation TranslucentView
 
//-------------------------------------------------------------
// initialization
//-------------------------------------------------------------
- (id)initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];
    calBGViewRectTag = [self addTrackingRect:[self frame] owner:self userData:&ImageOpacity assumeInside:YES];
    theContentDrawer = nil;
 
    return self;
}
 
 
//-------------------------------------------------------------
// draw translucent rectangle
//-------------------------------------------------------------
- (void)drawRect:(NSRect)rect
{
    [[NSColor clearColor] set];
    NSRectFill( rect );
 
    // draw the content
    if (theContentDrawer != nil)
[theContentDrawer performSelector:theDrawingMethod];
}
 
 
//-------------------------------------------------------------
// is this necessary?
//-------------------------------------------------------------
//- (BOOL)isOpaque { return NO; }
 
 
//-------------------------------------------------------------
// move the window
//-------------------------------------------------------------
- (void)mouseDown:(NSEvent *)theEvent
{
    BOOL keepOn = YES;
    NSPoint mouseLoc;
    NSPoint globalMouseLoc;
    NSPoint offsetMouseLoc;
    NSPoint tempWindowLoc;
    NSRect origFrame;
 
    offsetMouseLoc.x = offsetMouseLoc.y = 0; // avoid uninitialized warning
    origFrame = [[self window] frame];
    while( 1 ) { // gee! entering into the infinity loop...
mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
 
switch ([theEvent type]) {
case NSLeftMouseDown:
// save the initial mouse location
offsetMouseLoc = mouseLoc;
break;
case NSLeftMouseDragged:
// get the mouse location in the global coordinates
globalMouseLoc = [[self window] convertBaseToScreen:mouseLoc];
// calculate the new origin of the window
tempWindowLoc.x = (globalMouseLoc.x - offsetMouseLoc.x);
tempWindowLoc.y = (globalMouseLoc.y - offsetMouseLoc.y);
// get the window's location and size in the global coodinate system
[[self window] setFrameOrigin:tempWindowLoc]; // move and resize the window
break;
case NSLeftMouseUp:
keepOn = NO;
break;
default:
break;
        }
if (keepOn == NO)
break;
 
theEvent = [[self window] nextEventMatchingMask:(NSLeftMouseUpMask | NSLeftMouseDraggedMask) ];
     };
 
    return;
}
 
 
//-------------------------------------------------------------
// set the transparency of this view
//-------------------------------------------------------------
- (void)setContentDrawer:(id)theDrawer method:(SEL)theMethod
{
    theContentDrawer = theDrawer;
    theDrawingMethod = theMethod;
}
 
 
//-------------------------------------------------------------
// allow click-through
//-------------------------------------------------------------
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
    return (YES);
}
 
@end