forked from jerrykrinock/ClassesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSMoveableToolTip.m
120 lines (90 loc) · 3.23 KB
/
SSMoveableToolTip.m
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
117
118
119
120
#import "SSMoveableToolTip.h"
static SSMoveableToolTip *sharedToolTip = nil;
@interface ToolTipTextField : NSTextField
@end
@implementation ToolTipTextField
- (void) drawRect:(NSRect)aRect
{
[super drawRect:aRect];
[[NSColor colorWithCalibratedWhite:0.925 alpha:1.0] set];
NSFrameRect(aRect);
}
@end
@implementation SSMoveableToolTip
- (id) init
{
id retVal = [super init];
if (retVal != nil) {
// These size are not really import, just the relation between the two...
NSRect contentRect = { { 100, 100 }, { 100, 20 } };
NSRect textFieldFrame = { { 0, 0 }, { 100, 20 } };
_window = [[NSWindow alloc]
initWithContentRect: contentRect
styleMask: NSBorderlessWindowMask
backing: NSBackingStoreBuffered
defer: YES];
[_window setOpaque:NO];
[_window setAlphaValue:0.80];
[_window setBackgroundColor:[NSColor colorWithDeviceRed:1.0 green:0.96 blue:0.76 alpha:1.0]];
[_window setHasShadow:YES];
[_window setLevel:NSStatusWindowLevel];
[_window setReleasedWhenClosed:YES];
[_window orderFront:nil];
_textField = [[ToolTipTextField alloc] initWithFrame:textFieldFrame];
[_textField setEditable:NO];
[_textField setSelectable:NO];
[_textField setBezeled:NO];
[_textField setBordered:NO];
[_textField setDrawsBackground:NO];
[_textField setAlignment:NSLeftTextAlignment];
[_textField setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[_textField setFont:[NSFont toolTipsFontOfSize:[NSFont smallSystemFontSize]]];
[[_window contentView] addSubview:_textField];
[_textField setStringValue:@" "]; // Just having at least 1 char to allow the next message...
_textAttributes = [[[_textField attributedStringValue] attributesAtIndex:0 effectiveRange:nil] retain];
}
return retVal;
}
- (void) dealloc {
[_window release];
[_textAttributes release];
[super dealloc];
}
- (void) setString:(NSString *)string
origin:(NSPoint)origin
inWindow:(NSWindow*)hostWindow
{
origin.x += _offset.x ;
origin.y += _offset.y ;
NSSize size = [string sizeWithAttributes:_textAttributes];
NSSize windowSize = NSMakeSize(size.width + 10, size.height + 1) ;
NSPoint cursorScreenPosition = [hostWindow convertBaseToScreen:origin];
[_window setFrameTopLeftPoint:NSMakePoint(cursorScreenPosition.x + 10, cursorScreenPosition.y + 28)];
[_window setContentSize:windowSize] ;
if (string) {
[_textField setStringValue:string] ;
}
}
- (void)setOffset:(NSPoint)offset {
_offset = offset ;
}
+ (void) setString:(NSString *)string
origin:(NSPoint)origin
inWindow:(NSWindow*)hostWindow
{
if (sharedToolTip == nil) {
sharedToolTip = [[SSMoveableToolTip alloc] init];
}
[sharedToolTip setString:string
origin:origin
inWindow:hostWindow];
}
+ (void) goAway
{
[sharedToolTip release];
sharedToolTip = nil;
}
+ (void)setOffset:(NSPoint)offset {
[sharedToolTip setOffset:offset] ;
}
@end