/
GLUTClipboardController.m
310 lines (255 loc) · 9.09 KB
/
GLUTClipboardController.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* Copyright (c) Dietmar Planitzer, 1998. */
/* This program is freely distributable without licensing fees
and is provided without guarantee or warrantee expressed or
implied. This program is -not- in the public domain. */
#import "GLUTClipboardController.h"
#import "macx_glut.h"
#import "GLUTApplication.h"
#if defined(__LP64__)
#import <QTKit/QTMovie.h>
#import <QTKit/QTMovieView.h>
#endif // #if defined(__LP64__)
@interface GLUTUnknownView : NSView
- (void)drawRect: (NSRect)rect;
@end
@implementation GLUTUnknownView
- (void)drawRect: (NSRect)rect
{
[[NSColor clearColor] set];
NSRectFill(rect);
}
@end
/////////////////////////////////////////////
#pragma mark -
#pragma mark Pasteboard Controller
#pragma mark -
@implementation GLUTClipboardController
- (id)init
{
if((self = [self initWithWindowNibName: @"GLUTClipboard"]) != nil) {
[self setWindowFrameAutosaveName: @""];
[self setShouldCascadeWindows: NO];
_lastChangeCount = 0;
_firstTime = YES;
return self;
}
return nil;
}
- (void)dealloc
{
if(_updateTimer) {
[_updateTimer invalidate];
[_updateTimer release];
}
[super dealloc];
}
- (void)finalize
{
if(_updateTimer) {
[_updateTimer invalidate];
}
[super finalize];
}
- (void)_installAutoupdateTimer
{
_updateTimer = [[NSTimer scheduledTimerWithTimeInterval: 0.5
target: self
selector: @selector(_checkPasteboard)
userInfo: nil
repeats: YES] retain];
}
- (void)_clearAutoupdateTimer
{
[_updateTimer invalidate];
[_updateTimer release];
_updateTimer = nil;
}
- (void)windowDidLoad
{
[super windowDidLoad];
[[self window] setDelegate: self];
}
/* ******* Actions ******** */
- (IBAction)toggleWindow: (id)sender
{
NSWindow * window = [self window];
if(![self isClipboardWindowVisible]) {
/* if Pboard contents changed, then update our document view */
if(_firstTime || (_lastChangeCount != [[NSPasteboard generalPasteboard] changeCount])) {
[self updateClipboardWindow];
_firstTime = NO;
}
/* Install the auto-update timer */
[self _installAutoupdateTimer];
[window center];
[super showWindow: sender];
} else {
[window performClose: nil];
}
}
- (BOOL)validateMenuItem:(NSMenuItem *)anItem
{
if([self isClipboardWindowVisible]) {
[anItem setTitle: NSLocalizedStringFromTableInBundle(@"Hide Clipboard", @"GLUTUI",
__glutGetFrameworkBundle(),
@"Hide Clipboard")];
} else {
[anItem setTitle: NSLocalizedStringFromTableInBundle(@"Show Clipboard", @"GLUTUI",
__glutGetFrameworkBundle(),
@"Show Clipboard")];
}
return YES;
}
- (void)_checkPasteboard
{
if(_lastChangeCount != [[NSPasteboard generalPasteboard] changeCount])
[self updateClipboardWindow];
}
- (void)windowWillClose:(NSNotification *)notification
{
[self _clearAutoupdateTimer];
}
- (void)windowWillMiniaturize:(NSNotification *)notification
{
[self _clearAutoupdateTimer];
}
- (void)windowDidDeminiaturize:(NSNotification *)notification
{
[self _installAutoupdateTimer];
}
/* ******* UI ******* */
- (BOOL)isClipboardWindowVisible
{
if([self isWindowLoaded]) {
return ([self window] && [[self window] isVisible]);
} else {
return NO;
}
}
/* Creates and returns an autoreleased view representing the current
contents on the pasteboard. */
- (void)_getTextView: (NSView **)aTextView description: (NSString **)aDesc
fromPasteboard: (NSPasteboard *)pboard type: (NSString *)type
{
NSTextView * myTextView = [[[NSTextView alloc] initWithFrame: NSMakeRect(0.0, 0.0, 1.0, 1.0)] autorelease];
[myTextView setHorizontallyResizable: YES];
[myTextView setVerticallyResizable: YES];
[myTextView setImportsGraphics: YES];
[myTextView selectAll: nil];
[myTextView paste: nil];
[myTextView setEditable: NO];
[myTextView setSelectable: NO];
[myTextView setUsesFontPanel: NO];
*aTextView = myTextView;
if([type isEqualToString: NSRTFDPboardType] ||
[type isEqualToString: NSRTFPboardType] ||
[type isEqualToString: NSHTMLPboardType]) {
/* RTFD, RTF */
*aDesc = NSLocalizedStringFromTableInBundle(@"Rich Text", @"GLUTUI",
__glutGetFrameworkBundle(),
@"Rich Text");
} else {
/* Plain text */
*aDesc = NSLocalizedStringFromTableInBundle(@"Plain Text", @"GLUTUI",
__glutGetFrameworkBundle(),
@"Plain Text");
}
}
- (void)_getImageView: (NSView **)anImageView description: (NSString **)aDesc
fromPasteboard: (NSPasteboard *)pboard type: (NSString *)type
{
NSImage * myImage = [[[NSImage alloc] initWithPasteboard: pboard] autorelease];
NSSize imageSize = [myImage size];
NSImageView * myImageView = nil;
myImageView = [[[NSImageView alloc] initWithFrame: NSMakeRect(0.0, 0.0, imageSize.width, imageSize.height)] autorelease];
[myImageView setImage: myImage];
[myImageView setEditable: NO];
[myImageView setImageAlignment: NSImageAlignCenter];
*anImageView = myImageView;
*aDesc = [NSString stringWithFormat: NSLocalizedStringFromTableInBundle(@"\"%@\" image", @"GLUTUI",
__glutGetFrameworkBundle(),
@"\"%@\" image"), type];
}
- (void)_getMovieView: (NSView **)aMovieView description: (NSString **)aDesc
fromPasteboard: (NSPasteboard *)pboard type: (NSString *)type
{
#if defined(__LP64__)
QTMovie *myMovie = [QTMovie movieWithPasteboard: pboard error: nil];
QTMovieView *myMovieView = nil;
myMovieView = [[[QTMovieView alloc] initWithFrame: NSMakeRect(0.0, 0.0, 10.0, 10.0)] autorelease];
[myMovieView setMovie: myMovie];
[myMovieView setEditable: NO];
#else
NSMovie * myMovie = [[[NSMovie alloc] initWithPasteboard: pboard] autorelease];
NSMovieView * myMovieView = nil;
myMovieView = [[[NSMovieView alloc] initWithFrame: NSMakeRect(0.0, 0.0, 10.0, 10.0)] autorelease];
[myMovieView setMovie: myMovie];
[myMovieView setEditable: NO];
#endif // #if defined(__LP64__)
*aMovieView = myMovieView;
*aDesc = NSLocalizedStringFromTableInBundle(@"Movie", @"GLUTUI",
__glutGetFrameworkBundle(),
@"Movie");
}
- (void)_getUnknownView: (NSView **)aView description: (NSString **)aDesc
fromPasteboard: (NSPasteboard *)pboard type: (NSString *)type
{
GLUTUnknownView * myView = [[[GLUTUnknownView alloc] initWithFrame: NSMakeRect(0.0, 0.0, 10.0, 10.0)] autorelease];
*aView = myView;
*aDesc = NSLocalizedStringFromTableInBundle(@"Unknown contents", @"GLUTUI",
__glutGetFrameworkBundle(),
@"Unknown contents");
}
- (void)updateClipboardWindow
{
NSPasteboard * pboard = [NSPasteboard generalPasteboard];
NSString * type = nil;
NSString * pboardDesc = nil;
NSView * pboardView = nil;
NSPoint pinPoint = NSZeroPoint;
_lastChangeCount = [pboard changeCount];
/* Knock, knock, text there ? */
type = [pboard availableTypeFromArray: [NSAttributedString textPasteboardTypes]];
if(type) {
[self _getTextView: &pboardView description: &pboardDesc fromPasteboard: pboard type: type];
}
if(pboardView == nil) {
/* Knock, knock, QT movie there ? */
#if defined(__LP64__)
type = [pboard availableTypeFromArray: [QTMovie movieUnfilteredPasteboardTypes]];
#else
type = [pboard availableTypeFromArray: [NSMovie movieUnfilteredPasteboardTypes]];
#endif // #if defined(__LP64__)
if(type) {
[self _getMovieView: &pboardView description: &pboardDesc fromPasteboard: pboard type: type];
}
}
if(pboardView == nil) {
/* Knock, knock, image there ? */
type = [pboard availableTypeFromArray: [NSImage imagePasteboardTypes]];
if(type) {
[self _getImageView: &pboardView description: &pboardDesc fromPasteboard: pboard type: type];
pinPoint = NSMakePoint(0.0, NSHeight([pboardView bounds]));
}
}
if(pboardView == nil) {
/* Knock, knock, stranger there ? */
if(type) {
[self _getUnknownView: &pboardView description: &pboardDesc fromPasteboard: pboard type: type];
} else {
/* there is nothing on the pasteboard */
[self _getUnknownView: &pboardView description: &pboardDesc fromPasteboard: pboard type: type];
pboardDesc = NSLocalizedStringFromTableInBundle(@"Empty", @"GLUTUI",
__glutGetFrameworkBundle(),
@"Empty");
}
}
/* Update window contents */
[_scrollView setDocumentView: pboardView];
[pboardView scrollPoint: pinPoint];
[_infoText setStringValue: [NSString stringWithFormat: NSLocalizedStringFromTableInBundle(@"Clipboard contents: %@.",
@"GLUTUI",
__glutGetFrameworkBundle(),
@"Clipboard contents: %@."), pboardDesc]];
}
@end