2 #import "NSData+RSerialize.h"
4 CGContextRef RDevice_GetCGContext(QuartzDesc_t,void*);
5 int RDevice_LocatePoint(QuartzDesc_t,void*,double*,double*);
6 void RDevice_GetSize(QuartzDesc_t,void*,double*,double*);
7 void RDevice_Close(QuartzDesc_t,void*);
8 void RDevice_NewPage(QuartzDesc_t desc,void*ui);
9 void RDevice_Activate(QuartzDesc_t desc,void*ui,int num);
10 void RDevice_Deactivate(QuartzDesc_t desc,void*ui,int num);
11 static NSMutableDictionary *deviceDict = nil;
13 @implementation RDevice
15 + (Class)deviceForDisplay:(NSString*)display {
16 if(nil == deviceDict) deviceDict = [[NSMutableDictionary alloc] init];
17 return [deviceDict objectForKey:display];
20 + (void)registerDevice:(Class)deviceClass forDisplay:(NSString*)display {
21 if(nil == deviceDict) deviceDict = [[NSMutableDictionary alloc] init];
22 [deviceDict setObject:deviceClass forKey:display];
24 - (double)defaultScale { return 1.0; }
26 - (id)initWithDevice:(void*)dd size:(NSSize)size pointSize:(double)ps display:(NSString*)aDisplay target:(NSString*)aTarget
27 background:(int)bg antialias:(BOOL)antialias {
29 if(nil == [super init]) return nil;
30 desc = RDevice_Create(dd,[self defaultScale],ps,size.width,size.height,bg,antialias,
32 ([self canLocate] == YES) ? RDevice_LocatePoint : NULL,
38 display = [aDisplay retain];
39 target = [aTarget retain];
45 if(nil != target) [target release];
49 - (NSString*)display { return display; }
50 - (NSString*)target { return target; }
52 - (BOOL)canResize { return NO; }
53 - (BOOL)canLocate { return NO; }
54 - (BOOL)canDrawInView { return NO; }
56 - (CGContextRef)context { return NULL; }
57 - (BOOL)locatePointAtX:(double*)x y:(double*)y { *x = *y = 0.0;return NO; }
60 [delegate didEndPageForDevice:self];
61 [delegate willCloseDevice:self];
64 [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"RDeviceClosed" object:self]];
68 [delegate didEndPageForDevice:self];
69 [delegate willBeginPageForDevice:self];
72 - (void)activate:(int)devNum {
73 if(nil != delegate) [delegate didActivateDevice:self withNumber:devNum];
75 - (void)deactivate:(int)devNum {
76 if(nil != delegate) [delegate didDeactivateDevice:self withNumber:devNum];
80 double sc = 72.0*RDevice_GetScale(desc);
81 return NSMakeSize(sc*RDevice_GetWidth(desc),sc*RDevice_GetHeight(desc));
86 - (void)setSize:(NSSize)newSize {
87 double sc = 72.0*RDevice_GetScale(desc);
88 if(abs(newSize.width/sc - RDevice_GetWidth(desc)) > .0001 || abs(newSize.height/sc - RDevice_GetHeight(desc)) > .0001) {
89 RDevice_SetWidth(desc,newSize.width/sc);
90 RDevice_SetHeight(desc,newSize.height/sc);
95 - (id<RDeviceDelegate>)delegate { return delegate; }
96 - (void)setDelegate:(id<RDeviceDelegate>)aDelegate {
98 [delegate willChangeDelegateForDevice:self];
100 if(nil != delegate) {
101 [delegate didChangeDelegateForDevice:self];
102 [delegate didUpdateDevice:self];
106 - (void)drawInRect:(NSRect)aRect { }
108 - (void)finishOpening { }
109 - (void)flushDrawing {
110 if(RDevice_GetDirty(desc)) {
112 [delegate didUpdateDevice:self];
113 RDevice_SetDirty(desc,0);
117 - (void)redraw { RDevice_ReplayDisplayList(desc);[delegate didUpdateDevice:self]; }
119 - (NSData*)deviceRepresentationOfType:(NSString*)aType {
123 - (bycopy NSData*)snapshot {
124 return [NSData dataWithSEXP:RDevice_GetSnapshot(desc)];
127 - (void)setSnapshot:(NSData*)aData {
128 NSLog(@"Got snapshot data with length:%d",[aData length]);
129 RDevice_RestoreSnapshot(desc, [aData unserialize]);
131 [delegate didUpdateDevice:self];
138 CGContextRef RDevice_GetCGContext(QuartzDesc_t desc,void*ui) {
139 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
140 CGContextRef ctx = [(RDevice*)ui context];
144 int RDevice_LocatePoint(QuartzDesc_t desc,void*ui,double*x,double *y) {
145 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
146 int ret = [(RDevice*)ui locatePointAtX:x y:y];
150 void RDevice_Close(QuartzDesc_t desc,void*ui) {
151 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
152 [(RDevice*)ui close];
156 void RDevice_NewPage(QuartzDesc_t desc,void*ui) {
157 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
158 [(RDevice*)ui newPage];
162 void RDevice_Activate(QuartzDesc_t desc,void*ui,int num) {
163 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
164 [(RDevice*)ui activate:num];
168 void RDevice_Deactivate(QuartzDesc_t desc,void*ui,int num) {
169 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
170 [(RDevice*)ui deactivate:num];