-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAsyncDrawOperation.m
More file actions
205 lines (178 loc) · 6.18 KB
/
AsyncDrawOperation.m
File metadata and controls
205 lines (178 loc) · 6.18 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
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
//
// NewAsyncDrawOperation.m
// Textmate-Minimap
//
// Created by Julian Eberius on 19.03.10.
// Copyright 2010 Julian Eberius. All rights reserved.
//
#import "AsyncDrawOperation.h"
#import "NSView+Minimap.h"
#import "MinimapView.h"
@interface NSView (TextMate_OakTextView_Only)
- (id)currentStyleSheet;
@end
@interface AsyncDrawOperation (Private_NewAsyncDrawOperation)
- (NSBitmapImageRep*)cropImageRep:(NSBitmapImageRep*)rep ToRect:(NSRect)rect;
- (void)initializeFillColorFromTextView;
- (void)makeCompleteSnapshot;
- (void)makePartialSnapshot;
- (void)partialBackgroundDraw;
- (BOOL)checkCancelled;
- (NSRect)scaleRect:(NSRect)rect withFactor:(float)factor;
@end
@implementation AsyncDrawOperation
- (id)initWithMinimapView:(MinimapView*)mv andMode:(int)md
{
self = [super init];
if (self) {
minimapView = mv;
mode = md;
fillColor = nil;
}
return self;
}
- (id)initWithMinimapView:(MinimapView*)mv andMode:(int)md andUpdater:(BackgroundUpdater*)upd
{
self = [self initWithMinimapView:mv andMode:md];
if (self) {
updater = upd;
}
return self;
}
- (void) dealloc
{
[super dealloc];
// [minimapView release];
}
- (void)setPartToDraw:(NSRect)part
{
partToDraw = part;
}
- (void)main
{
if ([self isCancelled])
return;
switch (mode) {
case MM_COMPLETE_IMAGE:
[self makeCompleteSnapshot];
break;
case MM_PARTIAL_IMAGE:
[self makePartialSnapshot];
break;
default:
break;
}
}
- (void)makePartialSnapshot
{
//[[minimapView drawLock] lock];
NSImage* old_image = [minimapView theImage];
int gutterSize = [minimapView gutterSize];
NSRect tvBounds = NSMakeRect(gutterSize, 0,
[[minimapView textView] croppedBounds].size.width-gutterSize,
[[minimapView textView] croppedBounds].size.height);
NSRect bounds = [minimapView bounds];
float scaleFactor = bounds.size.width / tvBounds.size.width;
int h = tvBounds.size.height*scaleFactor;
NSRect newImageRect = NSMakeRect(0, 0, bounds.size.width, h);
NSImage* image = [[[NSImage alloc] initWithSize:newImageRect.size] autorelease];
if ([self checkCancelled])
return;
NSRect rectToRedraw = NSMakeRect(tvBounds.origin.x,
partToDraw.origin.y/scaleFactor,
tvBounds.size.width,
partToDraw.size.height/scaleFactor);
NSImage* drawnPart = [[minimapView textView] snapshotByDrawingInRect:rectToRedraw];
if ([self checkCancelled])
return;
[image lockFocus];
[fillColor set];
NSRectFill(NSMakeRect(0, 0, [image size].width, [image size].height));
// new image is longer or equal
if (h >= [old_image size].height) {
[old_image drawInRect:NSMakeRect(0, 0, [image size].width, [old_image size].height)
fromRect:NSZeroRect
operation:NSCompositeSourceOver fraction:1.0];
}
else {
[old_image drawInRect:newImageRect
fromRect:NSMakeRect(0,0, [old_image size].width, [image size].height)
operation:NSCompositeSourceOver fraction:1.0];
}
[drawnPart drawInRect:partToDraw
fromRect:NSZeroRect
operation:NSCompositeSourceOver fraction:1.0];
[image unlockFocus];
if ([self checkCancelled])
return;
[minimapView performSelectorOnMainThread:@selector(asyncDrawFinished:) withObject:image waitUntilDone:YES];
//[[minimapView drawLock] unlock];
}
- (BOOL)checkCancelled
{
if ([self isCancelled]) {
//[[minimapView drawLock] unlock];
return YES;
}
return NO;
}
- (NSRect)scaleRect:(NSRect)rect withFactor:(float)factor
{
return NSMakeRect(rect.origin.x*factor, rect.origin.y*factor, rect.size.width*factor, rect.size.height*factor);
}
- (void)makeCompleteSnapshot
{
// [[minimapView drawLock] lock];
if ([self checkCancelled])
return;
NSView* textView = [minimapView textView];
NSBitmapImageRep* snapshot = [textView snapshot];
int gutterSize = [minimapView gutterSize];
NSBitmapImageRep* croppedSnapshot = [self cropImageRep:snapshot
ToRect:NSMakeRect(gutterSize, 0, [snapshot size].width-gutterSize, [snapshot size].height)];
if ([self checkCancelled])
return;
NSRect bounds = [minimapView bounds];
float scaleFactor = bounds.size.width / [croppedSnapshot size].width;
int h = croppedSnapshot.size.height*scaleFactor;
NSImage* image = [[[NSImage alloc] initWithSize:NSMakeRect(0, 0, bounds.size.width, h).size] autorelease];
if ([self checkCancelled])
return;
[self initializeFillColorFromTextView];
[image setFlipped:YES];
[image lockFocus];
NSRect imgRect = NSMakeRect(0, 0, bounds.size.width, h);
[croppedSnapshot drawInRect:imgRect];
[image unlockFocus];
[image setFlipped:NO];
if ([self checkCancelled])
return;
[minimapView performSelectorOnMainThread:@selector(asyncDrawFinished:) withObject:image waitUntilDone:YES];
// [[minimapView drawLock] unlock];
}
/*
Copy&Pasted from the interwebs for cropping NSBitmapImageReps (used for cropping the gutter from the TextView Snapshots)
*/
- (NSBitmapImageRep*)cropImageRep:(NSBitmapImageRep*)rep ToRect:(NSRect)rect {
CGImageRef cgImg = CGImageCreateWithImageInRect([rep CGImage], NSRectToCGRect(rect)); NSBitmapImageRep *result = [[NSBitmapImageRep alloc] initWithCGImage:cgImg];
CGImageRelease(cgImg);
return [result autorelease];
}
- (void)initializeFillColorFromTextView
{
id stylesheet = [[minimapView textView] currentStyleSheet];
NSDictionary* firstEntry = [(NSArray*)stylesheet objectAtIndex:0]; // the first entry seems to always contain the main settings
NSString* bgColor = [[firstEntry objectForKey:@"settings"] objectForKey:@"background"];
unsigned aValue = strtoul([[bgColor substringWithRange:NSMakeRange(1, 6)] UTF8String], NULL, 16);
CGFloat red = ((CGFloat)((aValue & 0xFF0000) >> 16)) / 255.0f;
CGFloat green = ((CGFloat)((aValue & 0xFF00) >> 8)) / 255.0f;
CGFloat blue = (CGFloat)(aValue & 0xFF) / 255.0f;
CGFloat alpha = 1.0;
if ([bgColor length] == 9) // if a alpha was given
{
unsigned alphaValue = strtoul([[bgColor substringWithRange:NSMakeRange(7, 2)] UTF8String], NULL, 16);
alpha = (CGFloat)(alphaValue & 0xFF) / 255.0f;
}
fillColor = [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha];
}
@end