-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaquaterm.m
368 lines (309 loc) · 9.01 KB
/
aquaterm.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//
// aquaterm.m
// AquaTerm
//
// Created by Per Persson on Sat Jul 12 2003.
// Copyright (c) 2003 AquaTerm. All rights reserved.
//
#include "aquaterm.h"
#import <stdint.h>
#import <Foundation/Foundation.h>
#import "AQTAdapter.h"
static void (*_aqtEventHandlerPtr)(int32_t, const char *);
static NSAutoreleasePool *_pool;
static AQTAdapter *_adapter;
static BOOL _mayCleanPool = YES;
void _aqtCleanPool(void)
{
// NSLog(@"#arpool=%d", [NSAutoreleasePool autoreleasedObjectCount]);
if (_mayCleanPool)
{
[_pool release];
_pool = [[NSAutoreleasePool alloc] init];
}
else
{
// NSLog(@"Cleaning disabled");
}
}
/*" Class initialization etc."*/
int32_t aqtInit(void) // FIXME: retval?
{
if (!_pool)
{
_pool = [[NSAutoreleasePool alloc] init];
}
if (!_adapter)
{
_adapter = [[AQTAdapter alloc] init];
}
return (_adapter==nil)?1:0;
}
void aqtTerminate(void)
{
[_adapter release];
_adapter = nil;
[_pool release];
_pool = nil;
}
void _aqtEventTranslator(int32_t index, NSString *event)
{
// NSLog(@"_aqtEventTranslator --- %@ from %d", event, index);
_mayCleanPool = NO;
_aqtEventHandlerPtr(index, [event UTF8String]);
_mayCleanPool = YES;
}
void aqtSetEventHandler(void (*func)(int32_t ref, const char *event))
{
_aqtEventHandlerPtr = func;
[_adapter setEventHandler:_aqtEventTranslator];
}
/*" Control operations "*/
void aqtOpenPlot(int32_t refNum) // FIXME: retval?
{
[_adapter openPlotWithIndex:refNum];
}
int32_t aqtSelectPlot(int32_t refNum) // FIXME: retval?
{
return [_adapter selectPlotWithIndex:refNum]?1:0;
}
void aqtSetPlotSize(float width, float height)
{
[_adapter setPlotSize:NSMakeSize(width, height)];
}
void aqtSetPlotTitle(const char *title)
{
[_adapter setPlotTitle:title?[NSString stringWithCString:title encoding: NSISOLatin1StringEncoding]:@"Untitled"];
}
void aqtRenderPlot(void)
{
[_adapter renderPlot];
_aqtCleanPool();
}
void aqtClearPlot(void)
{
[_adapter clearPlot];
}
void aqtClosePlot(void)
{
[_adapter closePlot];
}
/*" Event handling "*/
void aqtSetAcceptingEvents(int32_t flag)
{
[_adapter setAcceptingEvents:flag?YES:NO];
}
int32_t aqtGetLastEvent(char *buffer) // FIXME: retval?
{
NSString *event = [_adapter lastEvent];
// Perform a lossy conversion from UTF8 to ASCII
NSString *eventStr = [[NSString alloc] initWithData:[event dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]
encoding:NSASCIIStringEncoding];
const char *cStr = [eventStr cStringUsingEncoding:NSASCIIStringEncoding];
int copyLen = MIN(AQT_EVENTBUF_SIZE - 1, strlen(cStr));
strncpy(buffer, cStr, copyLen);
buffer[copyLen] = '\0';
[eventStr release];
return 0;
}
int32_t aqtWaitNextEvent(char *buffer) // FIXME: retval?
{
NSString *event = [_adapter waitNextEvent];
// Perform a lossy conversion from UTF8 to ASCII
NSString *eventStr = [[NSString alloc] initWithData:[event dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]
encoding:NSASCIIStringEncoding];
const char *cStr = [eventStr cStringUsingEncoding:NSASCIIStringEncoding];
int copyLen = MIN(AQT_EVENTBUF_SIZE - 1, strlen(cStr));
strncpy(buffer, cStr, copyLen);
buffer[copyLen] = '\0';
[eventStr release];
return 0;
}
void aqtEventProcessingMode()
{
// FIXME: Add this to adapter?
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
_aqtCleanPool();
}
/*" Plotting related commands "*/
/*" Clip rect, applies to all objects "*/
void aqtSetClipRect(float originX, float originY, float width, float height)
{
[_adapter setClipRect:NSMakeRect(originX, originY, width, height)];
}
void aqtSetDefaultClipRect(void)
{
[_adapter setDefaultClipRect];
}
/*" Colormap (utility "*/
int32_t aqtColormapSize(void)
{
return [_adapter colormapSize];
}
void aqtSetColormapEntryRGBA(int32_t entryIndex, float r, float g, float b, float a)
{
[_adapter setColormapEntry:entryIndex red:r green:g blue:b alpha:a];
}
void aqtGetColormapEntryRGBA(int32_t entryIndex, float *r, float *g, float *b, float *a)
{
[_adapter getColormapEntry:entryIndex red:r green:g blue:b alpha:a];
}
void aqtSetColormapEntry(int32_t entryIndex, float r, float g, float b)
{
[_adapter setColormapEntry:entryIndex red:r green:g blue:b];
}
void aqtGetColormapEntry(int32_t entryIndex, float *r, float *g, float *b)
{
[_adapter getColormapEntry:entryIndex red:r green:g blue:b];
}
void aqtTakeColorFromColormapEntry(int32_t index)
{
[_adapter takeColorFromColormapEntry:index];
}
void aqtTakeBackgroundColorFromColormapEntry(int32_t index)
{
[_adapter takeBackgroundColorFromColormapEntry:index];
}
/*" Color handling "*/
void aqtSetColorRGBA(float r, float g, float b, float a)
{
[_adapter setColorRed:r green:g blue:b alpha:a];
}
void aqtSetBackgroundColorRGBA(float r, float g, float b, float a)
{
[_adapter setBackgroundColorRed:r green:g blue:b alpha:a];
}
void aqtGetColorRGBA(float *r, float *g, float *b, float *a)
{
[_adapter getColorRed:r green:g blue:b alpha:a];
}
void aqtGetBackgroundColorRGBA(float *r, float *g, float *b, float *a)
{
[_adapter getBackgroundColorRed:r green:g blue:b alpha:a];
}
void aqtSetColor(float r, float g, float b)
{
[_adapter setColorRed:r green:g blue:b];
}
void aqtSetBackgroundColor(float r, float g, float b)
{
[_adapter setBackgroundColorRed:r green:g blue:b];
}
void aqtGetColor(float *r, float *g, float *b)
{
[_adapter getColorRed:r green:g blue:b];
}
void aqtGetBackgroundColor(float *r, float *g, float *b)
{
[_adapter getBackgroundColorRed:r green:g blue:b];
}
/*" Text handling "*/
void aqtSetFontname(const char *newFontname)
{
if (newFontname != nil)
{
[_adapter setFontname:[NSString stringWithCString:newFontname encoding: NSISOLatin1StringEncoding]];
}
}
void aqtSetFontsize(float newFontsize)
{
[_adapter setFontsize:newFontsize];
}
void aqtAddLabel(const char *text, float x, float y, float angle, int32_t align)
{
if (text != nil)
{
[_adapter addLabel:[NSString stringWithCString:text encoding: NSISOLatin1StringEncoding] atPoint:NSMakePoint(x,y) angle:angle align:align];
}
}
void aqtAddShearedLabel(const char *text, float x, float y, float angle, float shearAngle, int32_t align)
{
if (text != nil)
{
[_adapter addLabel:[NSString stringWithCString:text encoding: NSISOLatin1StringEncoding] atPoint:NSMakePoint(x,y) angle:angle shearAngle:shearAngle align:align];
}
}
/*" Line handling "*/
void aqtSetLinewidth(float newLinewidth)
{
[_adapter setLinewidth:newLinewidth];
}
void aqtSetLineCapStyle(int32_t capStyle)
{
[_adapter setLineCapStyle:capStyle];
}
void aqtMoveTo(float x, float y)
{
[_adapter moveToPoint:NSMakePoint(x, y)];
}
void aqtAddLineTo(float x, float y)
{
[_adapter addLineToPoint:NSMakePoint(x, y)];
}
void aqtAddPolyline(float *x, float *y, int32_t pc)
{
int32_t i;
if (pc > 1)
{
[_adapter moveToPoint:NSMakePoint(x[0], y[0])];
for (i=1; i<pc; i++)
{
[_adapter addLineToPoint:NSMakePoint(x[i], y[i])];
}
}
}
void aqtSetLinestylePattern(float *newPattern, int32_t newCount, float newPhase)
{
[_adapter setLinestylePattern:newPattern count:newCount phase:newPhase];
}
void aqtSetLinestyleSolid(void)
{
[_adapter setLinestyleSolid];
}
/*" Rect and polygon handling"*/
void aqtMoveToVertex(float x, float y)
{
[_adapter moveToVertexPoint:NSMakePoint(x,y)];
}
void aqtAddEdgeToVertex(float x, float y)
{
[_adapter addEdgeToVertexPoint:NSMakePoint(x,y)];
}
void aqtAddPolygon(float *x, float *y, int32_t pc)
{
int32_t i;
if (pc > 1)
{
[_adapter moveToVertexPoint:NSMakePoint(x[0], y[0])];
for (i=1; i<pc; i++)
{
[_adapter addEdgeToVertexPoint:NSMakePoint(x[i], y[i])];
}
}
}
void aqtAddFilledRect(float originX, float originY, float width, float height)
{
[_adapter addFilledRect:NSMakeRect(originX, originY, width, height)];
}
void aqtEraseRect(float originX, float originY, float width, float height)
{
[_adapter eraseRect:NSMakeRect(originX, originY, width, height)];
}
/*" Image handling "*/
void aqtSetImageTransform(float m11, float m12, float m21, float m22, float tX, float tY)
{
[_adapter setImageTransformM11:m11 m12:m12 m21:m21 m22:m22 tX:tX tY:tY];
}
void aqtResetImageTransform(void)
{
[_adapter resetImageTransform];
}
void aqtAddImageWithBitmap(const void *bitmap, int32_t pixWide, int32_t pixHigh, float originX, float originY, float width, float height)
{
[_adapter addImageWithBitmap:bitmap size:NSMakeSize(pixWide, pixHigh) bounds:NSMakeRect(originX, originY, width, height)];
}
// FIXME: This function maps to a deprecated method in AQTAdapter. It overrides the global clipRect setting.
void aqtAddTransformedImageWithBitmap(const void *bitmap, int32_t pixWide, int32_t pixHigh, float originX, float originY, float width, float height)
{
[_adapter addTransformedImageWithBitmap:bitmap size:NSMakeSize(pixWide, pixHigh) clipRect:NSMakeRect(originX, originY, width, height)];
}