Many changes! Snapshots now working properly. Programmatic environment interface...
[RExecServer.git] / RDevice.m
blobc03d362be282f2e8ec0ec69c8319fb34604cbdfa
1 #import "RDevice.h"
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,
31                 RDevice_GetCGContext,
32                 ([self canLocate] == YES) ? RDevice_LocatePoint : NULL,
33                 RDevice_Close,
34                 RDevice_NewPage,
35                 RDevice_Activate,
36                 RDevice_Deactivate,
37         (void*)self);
38         display = [aDisplay retain];
39         target  = [aTarget retain];
40         return self;
43 - (void)dealloc {
44         [display release];
45         if(nil != target) [target release];
46         [super dealloc];
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; }
58 - (void)close  { 
59         if(nil != delegate) {
60                 [delegate didEndPageForDevice:self];
61                 [delegate willCloseDevice:self];
62         }
63         desc = NULL; 
64         [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"RDeviceClosed" object:self]];
66 - (void)newPage { 
67         if(nil != delegate) {
68                 [delegate didEndPageForDevice:self];
69                 [delegate willBeginPageForDevice:self];
70         }
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];
79 - (NSSize)size { 
80         double sc = 72.0*RDevice_GetScale(desc);
81         return NSMakeSize(sc*RDevice_GetWidth(desc),sc*RDevice_GetHeight(desc));
84 - (void)didResize { }
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);
91                 [self didResize];
92         }
95 - (id<RDeviceDelegate>)delegate { return delegate; }
96 - (void)setDelegate:(id<RDeviceDelegate>)aDelegate {
97         if(nil != delegate)
98                 [delegate willChangeDelegateForDevice:self];
99         delegate = aDelegate;
100         if(nil != delegate) {
101                 [delegate didChangeDelegateForDevice:self];
102                 [delegate didUpdateDevice:self];
103         }
106 - (void)drawInRect:(NSRect)aRect { }
108 - (void)finishOpening { }
109 - (void)flushDrawing  { 
110         if(RDevice_GetDirty(desc)) {
111                 if(nil != delegate)
112                         [delegate didUpdateDevice:self];
113                 RDevice_SetDirty(desc,0);
114         }
117 - (void)redraw { RDevice_ReplayDisplayList(desc);[delegate didUpdateDevice:self]; }
119 - (NSData*)deviceRepresentationOfType:(NSString*)aType {
120         return nil;
123 - (bycopy NSData*)snapshot {
124         return [NSData dataWithSEXP:RDevice_GetSnapshot(desc)];
125         
127 - (void)setSnapshot:(NSData*)aData {
128         NSLog(@"Got snapshot data with length:%d",[aData length]);
129         RDevice_RestoreSnapshot(desc, [aData unserialize]);
130         if(nil != delegate)
131                 [delegate didUpdateDevice:self];
136 @end
138 CGContextRef RDevice_GetCGContext(QuartzDesc_t desc,void*ui) { 
139         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
140         CGContextRef ctx = [(RDevice*)ui context];
141         [pool release];
142         return ctx;
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]; 
147         [pool release];
148         return ret;
150 void         RDevice_Close(QuartzDesc_t desc,void*ui) { 
151         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];     
152         [(RDevice*)ui close]; 
153         [pool release];
156 void RDevice_NewPage(QuartzDesc_t desc,void*ui) {
157         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];     
158         [(RDevice*)ui newPage]; 
159         [pool release];
162 void RDevice_Activate(QuartzDesc_t desc,void*ui,int num) {
163         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];     
164         [(RDevice*)ui activate:num]; 
165         [pool release];
168 void RDevice_Deactivate(QuartzDesc_t desc,void*ui,int num) {
169         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];     
170         [(RDevice*)ui deactivate:num]; 
171         [pool release];