-
Notifications
You must be signed in to change notification settings - Fork 0
/
SFDCanvas_LinkBack.m
142 lines (100 loc) · 4.11 KB
/
SFDCanvas_LinkBack.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
//
// BGCanvas_LinkBack.m
// K2LinkBackSupport
//
// Created by King Chung Huang on 3/12/05.
// Copyright 2005, 2007 King Chung Huang. All rights reserved.
//
#import "SFDCanvas_LinkBack.h"
#import "SFDDrawableInfo_Linkback.h"
#import "SFDSelectionController_LinkBack.h"
#import "SFDAffineGeometry.h"
static NSMutableDictionary *_activeLinksByCanvas = nil;
@implementation SFDCanvas (LinkBack)
+ (NSMutableDictionary *)_activeLinksByCanvas {
if (_activeLinksByCanvas == nil) {
_activeLinksByCanvas = [[NSMutableDictionary alloc] initWithCapacity:128];
}
return _activeLinksByCanvas;
}
#pragma mark LinkBackClientDelegate protocol
- (void)linkBackDidClose:(LinkBack *)link {
[self removeActiveLink:link];
}
- (void)linkBackServerDidSendEdit:(LinkBack *)link {
NSPasteboard *pasteboard = [link pasteboard];
id newInfo, oldInfo;
oldInfo = [link representedObject];
newInfo = [self makeInfoFromPasteboard:pasteboard withStyle:[oldInfo style]];
id lbd = [newInfo linkBackData];
[newInfo setLinkBackData:nil];
[newInfo setLinkBackKey:[oldInfo linkBackKey]];
[newInfo setLinkBackData:lbd];
NSEnumerator *properties = [[oldInfo styleProperties] objectEnumerator];
id property, value;
while (property = [properties nextObject]) {
if ([oldInfo canInspectProperty:property]) {
value = [oldInfo valueForProperty:property];
if (value != nil && [newInfo canMutateProperty:property]) {
[newInfo setValue:value forProperty:property];
}
}
}
SFDAffineGeometry *oldGeometry = [oldInfo geometry];
NSSize oldNaturalSize = [oldInfo naturalSize];
NSSize oldSize = [oldInfo size];
NSSize newNaturalSize = [newInfo naturalSize];
NSSize newSize = NSMakeSize(newNaturalSize.width * oldSize.width / oldNaturalSize.width, newNaturalSize.height * oldSize.height / oldNaturalSize.height);
SFDAffineGeometry *newGeometry = [[SFDAffineGeometry alloc] initWithNaturalSize:newNaturalSize size:newSize sizesLocked:[oldGeometry sizesLocked] aspectRatioLocked:[oldGeometry aspectRatioLocked] position:[oldGeometry position] angleInDegrees:[oldGeometry angleInDegrees] horizontalFlip:[oldGeometry horizontalFlip] verticalFlip:[oldGeometry verticalFlip] shearXAngle:[oldGeometry shearXAngle] shearYAngle:[oldGeometry shearYAngle]];
[newInfo setGeometry:newGeometry];
id slide = [self performSelector:@selector(activeSlide)];
[slide performSelector:@selector(removeDrawable:) withObject:oldInfo];
[slide performSelector:@selector(addDrawable:) withObject:newInfo];
[link setRepresentedObject:newInfo];
}
#pragma mark Managing LinkBacks
- (void)addActiveLink:(LinkBack *)link {
if (link != nil)
[[self activeLinks] addObject:link];
}
- (void)removeActiveLink:(LinkBack *)link {
[[self activeLinks] removeObject:link];
}
- (NSMutableArray *)activeLinks {
NSMutableArray *links = [[[self class] _activeLinksByCanvas] objectForKey:[self canvasKey]];
if (links == nil) {
links = [NSMutableArray arrayWithCapacity:8];
[[[self class] _activeLinksByCanvas] setObject:links forKey:[self canvasKey]];
}
return links;
}
- (void)deallocLinks {
NSMutableArray *links = [self activeLinks];
if ([links count] > 0) {
[links makeObjectsPerformSelector:@selector(closeLink)];
}
[[[self class] _activeLinksByCanvas] removeObjectForKey:[self canvasKey]];
}
- (NSNumber *)canvasKey {
return [NSNumber numberWithUnsignedInt:(unsigned)self];
}
#pragma mark Starting a LinkBack
- (void)beginLinkBackForSelection:(id)sender {
NSArray *infos = [[self selectionController] selectedLinkBackInfos];
int idx = [infos count];
while (--idx >= 0) {
[self beginLinkBackForInfo:[infos objectAtIndex:idx] canvas:self];
}
}
- (void)beginLinkBackForInfo:(SFDDrawableInfo *)info canvas:(SFDCanvas *)canvas {
if ([info respondsToSelector:@selector(hasLinkBackData)] && [info performSelector:@selector(hasLinkBackData)]) {
id data = [info linkBackData];
NSString *sourceName = [[[info storage] document] displayName];
LinkBack *link = [LinkBack editLinkBackData:data sourceName:sourceName delegate:canvas itemKey:[info linkBackKey]];
if (link) {
[link setRepresentedObject:info];
[canvas addActiveLink:link];
}
}
}
@end