-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
TPLayoutLocalHostView.m
341 lines (281 loc) · 9.2 KB
/
TPLayoutLocalHostView.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//
// TPLayoutLocalHost.m
// teleport
//
// Created by JuL on Fri Feb 27 2004.
// Copyright (c) 2003-2005 abyssoft. All rights reserved.
//
#import "TPLayoutLocalHostView.h"
#import "TPLocalHost.h"
#import "TPPreferencesManager.h"
#define MENU_HEIGHT 5.0
//static NSImage * _shareScreenImage = nil;
//static NSImage * _shareScreenOnImage = nil;
@interface TPLayoutLocalScreenView : TPLayoutScreenView
@end
@implementation TPLayoutLocalScreenView
- (void)drawRect:(NSRect)rect
{
TPLocalHost * localHost = (TPLocalHost*)[_hostView host];
NSRect drawRect = [self bounds];
NSScreen * screen = [self screen];
NSImage * backgroundImage = [localHost backgroundImageForScreen:screen];
if(backgroundImage != nil) {
#if FLIPPED_VIEW
[backgroundImage setFlipped:YES];
#endif
[backgroundImage drawInRect:drawRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
#if FLIPPED_VIEW
[backgroundImage setFlipped:NO];
#endif
}
else {
[[NSColor colorWithCalibratedRed:30.0/255.0 green:70.0/255.0 blue:140.0/255.0 alpha:1.0] set];
[[NSBezierPath bezierPathWithRect:drawRect] fill];
}
if([self isMainScreen]) { // main screen
NSRect menuRect = drawRect;
#if FLIPPEDVIEW
#else
menuRect.origin.y += (menuRect.size.height - MENU_HEIGHT);
#endif
menuRect.size.height = MENU_HEIGHT;
[[NSGraphicsContext currentContext] saveGraphicsState];
CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
CGRect cgRect = *(CGRect*)(&drawRect);
CGContextClipToRect(ctx, cgRect);
NSShadow * shadow = [[NSShadow alloc] init];
[shadow setShadowBlurRadius:2.0];
[shadow setShadowColor:[NSColor blackColor]];
[shadow setShadowOffset:NSMakeSize(0,-1)];
[shadow set];
float alpha;
if([[_hostView host] osVersion] >= TPHostOSVersion(5)) {
alpha = 0.8;
}
else {
alpha = 1.0;
}
[[NSColor colorWithCalibratedWhite:1.0 alpha:alpha] set];
[[NSBezierPath bezierPathWithRect:menuRect] fill];
[[NSGraphicsContext currentContext] restoreGraphicsState];
NSRect titleRect = NSInsetRect(drawRect, 2.0, 2.0);
[self drawHostTitleInRect:titleRect dimmed:NO/*(drawMode == DISABLED_MODE)*/];
}
[[NSColor colorWithCalibratedWhite:0.2 alpha:1.0] set];
NSFrameRect(drawRect);
}
@end
@implementation TPLayoutLocalHostView
+ (Class)screenViewClass
{
return [TPLayoutLocalScreenView class];
}
- (TPHost*)host
{
return [TPLocalHost localHost];
}
- (unsigned)nearestScreenIndexForFrame:(NSRect)frame distance:(float*)distance position:(NSPoint*)hostPosition side:(TPSide*)side
{
unsigned nearestScreenIndex = 0;
float minDist = INFINITY;
NSRect minGluedRect = NSZeroRect;
TPSide minSide = TPUndefSide;
unsigned screenIndex = 0;
NSArray * screenViews = [self screenViews];
NSEnumerator * screenViewsEnum = [screenViews objectEnumerator];
TPLayoutLocalScreenView * localScreenView;
while((localScreenView = [screenViewsEnum nextObject]) != nil) {
NSRect screenFrame = [[localScreenView screen] frame];
NSRect localRect = [self convertRect:[localScreenView frame] toView:[self superview]];
// Calculate excluded sides
TPSide excludedSides = TPUndefSide;
if(NSIntersectsRect(frame, localRect)) {
NSEnumerator * screenViewsEnum2 = [screenViews objectEnumerator];
TPLayoutLocalScreenView * localScreenView2;
while((localScreenView2 = [screenViewsEnum2 nextObject]) != nil) {
NSRect screenFrame2 = [[localScreenView2 screen] frame];
if(NSMinX(screenFrame2) == NSMaxX(screenFrame)) {
excludedSides |= TPRightSide;
}
if(NSMaxX(screenFrame2) == NSMinX(screenFrame)) {
excludedSides |= TPLeftSide;
}
if(NSMinY(screenFrame2) == NSMaxY(screenFrame)) {
excludedSides |= TPTopSide;
}
if(NSMaxY(screenFrame2) == NSMinY(screenFrame)) {
excludedSides |= TPBottomSide;
}
}
}
NSRect gluedRect;
TPSide side;
float dist = TPGluedRect(&gluedRect, &side, localRect, frame, excludedSides);
if(dist < minDist) {
minDist = dist;
nearestScreenIndex = screenIndex;
minGluedRect = gluedRect;
minSide = side;
}
screenIndex++;
}
if(distance != NULL) {
*distance = minDist;
*hostPosition = minGluedRect.origin;
*side = minSide;
}
return nearestScreenIndex;
}
#if 0
+ (void)initialize
{
if(self == [TPLayoutLocalHostView class]) {
NSString * imagePath = [[NSBundle bundleForClass:[self class]] pathForImageResource:@"sharedscreen.tiff"];
_shareScreenImage = [[NSImage alloc] initWithContentsOfFile:imagePath];
imagePath = [[NSBundle bundleForClass:[self class]] pathForImageResource:@"sharedscreen-on.tiff"];
_shareScreenOnImage = [[NSImage alloc] initWithContentsOfFile:imagePath];
}
}
- (BOOL)_canChooseSharedScreen
{
NSArray * screens = [[TPLocalHost localHost] screens];
return ([_layoutView isActive] && [screens count] > 1 && [[TPPreferencesManager sharedPreferencesManager] boolForPref:ALLOW_CONTROL]);
}
- (void)setScaleFactor:(float)inScaleFactor
{
scaleFactor = inScaleFactor;
}
- (NSRect)rectForScreen:(NSScreen*)screen
{
if(screen == nil)
return NSZeroRect;
NSRect screenRect = [screen frame];
NSRect totalRect = [self totalRect];
/* Recenter */
screenRect.origin.x += (_origin.x - totalRect.origin.x);
screenRect.origin.y += (_origin.y - totalRect.origin.y);
return screenRect;
}
- (NSRect)drawRectForScreen:(NSScreen*)screen
{
if(screen == nil)
return NSZeroRect;
NSRect screenRect = [self rectForScreen:screen];
screenRect.origin.x = FORDRAW_ORIGIN(screenRect.origin.x*scaleFactor);
screenRect.origin.y = FORDRAW_ORIGIN(screenRect.origin.y*scaleFactor);
screenRect.size.width = FORDRAW_SIZE(screenRect.size.width*scaleFactor);
screenRect.size.height = FORDRAW_SIZE(screenRect.size.height*scaleFactor);
return screenRect;
}
- (NSRect)shareScreenRectFromDrawRect:(NSRect)drawRect
{
return NSMakeRect(NSMaxX(drawRect) - 14.0, NSMaxY(drawRect) - 14.0, 12.0, 12.0);
}
- (NSRect)totalRect
{
TPLocalHost * localHost = (TPLocalHost*)[self host];
NSRect enclosingRect = NSZeroRect;
unsigned screenIndex = 0;
NSScreen * screen;
while(screen = [localHost screenAtIndex:screenIndex++]) {
NSRect screenRect = [screen frame];
enclosingRect = NSUnionRect(enclosingRect, screenRect);
}
return enclosingRect;
}
- (NSSize)totalSize
{
return [self totalRect].size;
}
- (BOOL)isPointInside:(NSPoint)point
{
unsigned screenIndex = 0;
NSScreen * screen;
while(screen = [[self host] screenAtIndex:screenIndex++]) {
NSRect drawRect = [self drawRectForScreen:screen];
if(NSPointInRect(point, drawRect)) {
return YES;
}
}
return NO;
}
- (BOOL)handleMouseDownAtPoint:(NSPoint)point
{
if([self _canChooseSharedScreen]) {
_insideScreen = [self screenAtPoint:point];
if(_insideScreen != nil) {
NSRect drawRect = [self drawRectForScreen:_insideScreen];
NSRect shareScreenRect = [self shareScreenRectFromDrawRect:drawRect];
if(NSPointInRect(point, shareScreenRect)) {
_insideButton = YES;
return YES;
}
}
}
return NO;
}
- (void)handleMouseDraggedAtPoint:(NSPoint)point
{
if(_insideScreen != nil) {
NSRect drawRect = [self drawRectForScreen:_insideScreen];
NSRect shareScreenRect = [self shareScreenRectFromDrawRect:drawRect];
_insideButton = NSPointInRect(point, shareScreenRect);
}
}
- (void)handleMouseUpAtPoint:(NSPoint)point
{
if(_insideScreen != nil) {
NSRect drawRect = [self drawRectForScreen:_insideScreen];
NSRect shareScreenRect = [self shareScreenRectFromDrawRect:drawRect];
if(NSPointInRect(point, shareScreenRect)) {
[(TPLocalHost*)[self host] setSharedScreenIndex:[[[TPLocalHost localHost] screens] indexOfObject:_insideScreen]];
}
}
_insideButton = NO;
}
- (NSScreen*)screenAtPoint:(NSPoint)point
{
TPLocalHost * localHost = (TPLocalHost*)[self host];
unsigned screenIndex = 0;
NSScreen * screen = nil;
while((screen = [localHost screenAtIndex:screenIndex++]) != nil) {
NSRect drawRect = [self drawRectForScreen:screen];
if(NSPointInRect(point, drawRect)) {
break;
}
}
return screen;
}
- (NSMenu*)menuForEvent:(NSEvent*)event
{
if([self _canChooseSharedScreen]) {
TPLocalHost * localHost = [TPLocalHost localHost];
NSMenu * menu = [[NSMenu alloc] init];
NSString * title = NSLocalizedStringFromTableInBundle(@"Screen to share", nil, [NSBundle bundleForClass:[self class]], nil);
NSMenuItem * titleMenuItem = [menu addItemWithTitle:title action:nil keyEquivalent:@""];
[titleMenuItem setEnabled:NO];
NSArray * screens = [localHost screens];
unsigned sharedScreenIndex = [(TPLocalHost*)[self host] sharedScreenIndex];
unsigned i;
for(i=0; i<[screens count]; i++) {
NSScreen * screen = [screens objectAtIndex:i];
NSSize screenSize = [screen frame].size;
NSString * title = [NSString stringWithFormat:NSLocalizedStringFromTableInBundle(@"Screen %d - %.0fx%.0f", nil, [NSBundle bundleForClass:[self class]], nil), i+1, screenSize.width, screenSize.height];
NSMenuItem * screenMenuItem = [menu addItemWithTitle:title action:@selector(changeSharedScreen:) keyEquivalent:@""];
[screenMenuItem setTag:i];
[screenMenuItem setState:(i==sharedScreenIndex)?NSOnState:NSOffState];
[screenMenuItem setTarget:self];
}
return [menu autorelease];
}
else {
return [super menuForEvent:event];
}
}
- (void)changeSharedScreen:(NSMenuItem*)sender
{
[(TPLocalHost*)[self host] setSharedScreenIndex:[sender tag]];
}
#endif
@end