forked from drdrang/jumpcut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBezelWindow.m
179 lines (158 loc) · 4.95 KB
/
BezelWindow.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// BezelWindow.m
// Jumpcut
//
// Created by Steve Cook on 4/3/06.
// Copyright 2006 Steve Cook. All rights reserved.
//
// This code is open-source software subject to the MIT License; see the homepage
// at <http://jumpcut.sourceforge.net/> for details.
#import "BezelWindow.h"
@implementation BezelWindow
- (id)initWithContentRect:(NSRect)contentRect
styleMask:(unsigned int)aStyle
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag
{
self = [super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
if ( self )
{
[self setOpaque:NO];
[self setAlphaValue:1.0];
[self setOpaque:NO];
[self setHasShadow:NO];
[self setMovableByWindowBackground:NO];
[self setBackgroundColor:[self sizedBezelBackgroundWithRadius:25.0 withAlpha:[[NSUserDefaults standardUserDefaults] floatForKey:@"bezelAlpha"]]];
float lineHeight = 16;
float nLineHeight = 24;
NSRect textFrame = NSMakeRect(12, 12, [self frame].size.width - 24, 13 * lineHeight);
textField = [[RoundRecTextField alloc] initWithFrame:textFrame];
[[self contentView] addSubview:textField];
[textField setEditable:NO];
[textField setTextColor:[NSColor whiteColor]];
[textField setBackgroundColor:[NSColor colorWithCalibratedWhite:0.33 alpha:0.0]];
[textField setDrawsBackground:YES];
[textField setBordered:NO];
[textField setAlignment:NSLeftTextAlignment];
NSRect charFrame = NSMakeRect(450, 250, 1.75 * nLineHeight, 1.25 * nLineHeight);
charField = [[RoundRecTextField alloc] initWithFrame:charFrame];
[[self contentView] addSubview:charField];
[charField setEditable:NO];
[charField setTextColor:[NSColor whiteColor]];
[charField setFont: [NSFont systemFontOfSize: 18]];
[charField setBackgroundColor:[NSColor colorWithCalibratedWhite:0.33 alpha:0.0]];
[charField setDrawsBackground:YES];
[charField setBordered:NO];
[charField setAlignment:NSCenterTextAlignment];
[self setInitialFirstResponder:textField];
icon = [NSImage imageNamed:@"net.sf.jumpcut.ghost_scissors_vsmall.png"];
if ( [icon isValid] ) {
NSRect iconFrame = NSMakeRect( ([self frame].size.width - [icon size].width) / 2, [self frame].size.height - [icon size].height - 24, [icon size].width, [icon size].height);
iconView = [[NSImageView alloc] initWithFrame:iconFrame];
[iconView setImage:icon];
[[self contentView] addSubview:iconView];
}
return self;
}
return nil;
}
- (void) setAlpha:(float)newValue
{
[self setBackgroundColor:[self sizedBezelBackgroundWithRadius:25.0 withAlpha:[[NSUserDefaults standardUserDefaults] floatForKey:@"bezelAlpha"]]];
[[self contentView] setNeedsDisplay:YES];
}
- (NSString *)title
{
return title;
}
- (void)setTitle:(NSString *)newTitle
{
[newTitle retain];
[title release];
title = newTitle;
}
- (NSString *)text
{
return bezelText;
}
- (void)setCharString:(NSString *)newChar
{
[newChar retain];
[charString release];
charString = newChar;
[charField setStringValue:charString];
}
- (void)setText:(NSString *)newText
{
[newText retain];
[bezelText release];
bezelText = newText;
[textField setStringValue:bezelText];
}
- (void)setFrame:(NSRect)frameRect display:(BOOL)displayFlag animate:(BOOL)animationFlag
{
[super setFrame:frameRect display:displayFlag animate:animationFlag];
}
- (NSColor *)roundedBackgroundWithRect:(NSRect)bgRect withRadius:(float)radius withAlpha:(float)alpha
{
NSImage *bg = [[NSImage alloc] initWithSize:bgRect.size];
[bg lockFocus];
// I'm not at all clear why this seems to work
NSRect dummyRect = NSMakeRect(0, 0, [bg size].width, [bg size].height);
NSBezierPath *roundedRec = [NSBezierPath bezierPathWithRoundRectInRect:dummyRect radius:radius];
[[NSColor colorWithCalibratedWhite:0.1 alpha:alpha] set];
[roundedRec fill];
[bg unlockFocus];
return [NSColor colorWithPatternImage:[bg autorelease]];
}
- (NSColor *)sizedBezelBackgroundWithRadius:(float)radius withAlpha:(float)alpha
{
return [self roundedBackgroundWithRect:[self frame] withRadius:radius withAlpha:alpha];
}
-(BOOL)canBecomeKeyWindow
{
return YES;
}
- (void)dealloc
{
[textField release];
[charField release];
[iconView release];
[super dealloc];
}
- (BOOL)performKeyEquivalent:(NSEvent*) theEvent
{
if ( [self delegate] )
{
[delegate performSelector:@selector(processBezelKeyDown:) withObject:theEvent];
return YES;
}
return NO;
}
- (void)keyDown:(NSEvent *)theEvent {
if ( [self delegate] )
{
[delegate performSelector:@selector(processBezelKeyDown:) withObject:theEvent];
}
}
- (void)flagsChanged:(NSEvent *)theEvent {
if ( ! ( [theEvent modifierFlags] & NSCommandKeyMask )
&& ! ( [theEvent modifierFlags] & NSAlternateKeyMask )
&& ! ( [theEvent modifierFlags] & NSControlKeyMask )
&& ! ( [theEvent modifierFlags] & NSShiftKeyMask )
&& [ self delegate ]
)
{
[delegate performSelector:@selector(metaKeysReleased)];
}
}
- (id)delegate {
return delegate;
}
- (void)setDelegate:(id)newDelegate {
delegate = newDelegate;
}
@end