scide: DocManager - report warnings via the status bar
[supercollider.git] / editors / scapp / GUIPrimitives.M
blob4ace1edb0f37f79552c4254de2b5fe944e0b1467
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <Cocoa/Cocoa.h>
22 #include <Carbon/Carbon.h>
23 #import "SCBase.h"
24 #import "PyrSymbol.h"
25 #include "PyrPrimitive.h"
26 #include "PyrObject.h"
27 #include "PyrKernel.h"
28 #include "VMGlobals.h"
29 #include "SC_RGen.h"
30 #import "MyDocument.h"
31 #import "SCGraphView.h"
32 #import "SCVirtualMachine.h"
33 #import "ChangeCounter.h"
34 #import "GC.h"
35 #import "SCTextView.h"
36 #import "SCNSWindow.h"
37 #import "SCCocoaView.h"
39 #if (MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4)
40 typedef float ColorData;
41 #else
42 typedef CGFloat ColorData;
43 #endif
46 extern ChangeCounter gUIChangeCounter;
47 extern NSTextView* gPostView;
48 PyrSymbol *s_draw;
49 PyrSymbol *s_font;
50 PyrSymbol *s_closed;
51 PyrSymbol *s_tick;
52 PyrSymbol *s_doaction;
53 PyrSymbol *s_didBecomeKey;
54 PyrSymbol *s_didResignKey;
56 extern bool docCreatedFromLang;
57 extern bool defaultDocumentUseAutoInOutDent;
58 int slotColorVal(PyrSlot *slot, SCColor *sccolor);
60 int prNumberOfOpenTextWindows(struct VMGlobals *g, int numArgsPushed);
61 int prNumberOfOpenTextWindows(struct VMGlobals *g, int numArgsPushed)
63 if (!g->canCallOS) return errCantCallOS;
65 PyrSlot *a = g->sp;
67 NSDocumentController *docctl = [NSDocumentController sharedDocumentController];
68 if (!docctl) return errFailed;
69 int num = [[docctl documents] count];
70 if (!num) return errFailed;
71 SetInt(a, num);
72 return errNone;
76 int prTextWindow_IsEdited(struct VMGlobals *g, int numArgsPushed);
77 int prTextWindow_IsEdited(struct VMGlobals *g, int numArgsPushed)
79 if (!g->canCallOS) return errCantCallOS;
81 PyrSlot *a = g->sp;
83 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
84 if (!doc) return errFailed;
85 bool edited = (bool) [doc isDocumentEdited];
86 if(edited) SetTrue(a);
87 else SetFalse(a);
88 return errNone;
91 int prTextWindow_SetEditable(struct VMGlobals *g, int numArgsPushed);
92 int prTextWindow_SetEditable(struct VMGlobals *g, int numArgsPushed)
94 if (!g->canCallOS) return errCantCallOS;
96 PyrSlot *a = g->sp-1;
97 PyrSlot *b = g->sp;
99 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
100 if (!doc) return errFailed;
101 if(IsFalse(b)){
102 [[doc textView] setEditable: NO];
103 [[doc textView2] setEditable: NO];
104 } else {
105 [[doc textView] setEditable: YES];
106 [[doc textView2] setEditable: YES];
108 return errNone;
111 int slotGetNSRect(PyrSlot* a, NSRect *r);
113 int prTextWindow_SetBounds(struct VMGlobals *g, int numArgsPushed);
114 int prTextWindow_SetBounds(struct VMGlobals *g, int numArgsPushed)
116 if (!g->canCallOS) return errCantCallOS;
118 PyrSlot *a = g->sp - 1;
119 PyrSlot *b = g->sp;
121 if (!(isKindOfSlot(b, s_rect->u.classobj))) return errWrongType;
123 NSRect bounds;
124 int err = slotGetNSRect(b, &bounds);
125 if (err) return err;
127 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
128 if (!doc) return errFailed;
130 NSWindow *window = [[doc textView] window];
131 //[window setFrame: bounds display: YES];
133 SEL sel = @selector(setFrame:display:);
134 NSMethodSignature *sig = [NSWindow instanceMethodSignatureForSelector: sel];
135 BOOL flag = YES;
137 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
138 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
139 [anInvocation setTarget: window];
140 [anInvocation setSelector: sel];
141 [anInvocation setArgument: &bounds atIndex: 2];
142 [anInvocation setArgument: &flag atIndex: 3];
143 [scvm defer: anInvocation];
145 return errNone;
148 int prTextWindow_GetBounds(struct VMGlobals *g, int numArgsPushed);
149 int prTextWindow_GetBounds(struct VMGlobals *g, int numArgsPushed)
151 if (!g->canCallOS) return errCantCallOS;
153 PyrSlot *a = g->sp - 1;
154 PyrSlot *b = g->sp;
156 if (!(isKindOfSlot(b, s_rect->u.classobj))) return errWrongType;
158 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
159 if (!doc) return errFailed;
161 NSWindow *window = [[doc textView] window];
163 NSRect bounds = [window frame];
164 PyrSlot *slots = slotRawObject(b)->slots;
165 SetFloat(slots+0, bounds.origin.x);
166 SetFloat(slots+1, bounds.origin.y);
167 SetFloat(slots+2, bounds.size.width);
168 SetFloat(slots+3, bounds.size.height);
169 slotCopy(a, b);
171 return errNone;
174 int prTextWindow_SetName(struct VMGlobals *g, int numArgsPushed);
175 int prTextWindow_SetName(struct VMGlobals *g, int numArgsPushed)
177 if (!g->canCallOS) return errCantCallOS;
179 PyrSlot *a = g->sp - 1;
180 PyrSlot *b = g->sp;
182 if (!(isKindOfSlot(b, class_string))) return errWrongType;
184 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
185 if (!doc) return errFailed;
187 NSWindow *window = [[doc textView] window];
188 PyrString *string = slotRawString(b);
189 NSString *title = [NSString stringWithCString: string->s encoding:[NSString defaultCStringEncoding]];
190 title = [title substringToIndex: string->size];
191 [window setTitle: title];
192 return errNone;
195 int prTextWindow_SetBackgroundColor(struct VMGlobals *g, int numArgsPushed);
196 int prTextWindow_SetBackgroundColor(struct VMGlobals *g, int numArgsPushed)
198 if (!g->canCallOS) return errCantCallOS;
200 PyrSlot *a = g->sp-1;
201 PyrSlot *b = g->sp;
202 if (IsNil(b)) return errNone;
203 SCColor rgb;
204 int err = slotColorVal(b, &rgb);
205 if (err) return err;
206 NSColor *color = [NSColor colorWithCalibratedRed: rgb.red
207 green: rgb.green
208 blue: rgb.blue
209 alpha: rgb.alpha];
211 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
212 if (!doc) return errFailed;
213 [doc setBackgroundColor: color];
214 //[[doc textView] didChangeText];
216 return errNone;
219 int prTextWindow_GetBackgroundColor(struct VMGlobals *g, int numArgsPushed);
220 int prTextWindow_GetBackgroundColor(struct VMGlobals *g, int numArgsPushed)
222 if (!g->canCallOS) return errCantCallOS;
223 // let the lang allocate the Color
224 // rtf seems not to save the backgroundcolor. set it in the lang first!
225 PyrSlot *a = g->sp-1;
226 PyrSlot *b = g->sp;
227 if (IsNil(b)) return errNone;
228 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
229 if (!doc) return errFailed;
230 NSTextView * view = [doc initTextView];
231 if(!view) view = [doc textView];
232 if(!view) return errNone;
233 NSColor * color = [view backgroundColor];
234 if(!color) return errNone;
235 ColorData red, green, blue, alpha;
236 [color getRed:&red green:&green blue:&blue alpha:&alpha];
237 PyrSlot *slots = slotRawObject(b)->slots;
238 SetFloat(slots+0, red);
239 SetFloat(slots+1, green);
240 SetFloat(slots+2, blue);
241 SetFloat(slots+3, alpha);
242 return errNone;
245 extern SCColor synColText, synColClass, synColString, synColSymbol, synColComment, synColNumber, synColSpecialVals, synColSpecialVars, synColDecl, synColPunc, synColEnv;
247 int prSetSyntaxColorTheme(struct VMGlobals *g, int numArgsPushed);
248 int prSetSyntaxColorTheme(struct VMGlobals *g, int numArgsPushed)
250 PyrSlot *colText;
251 PyrSlot *colClass;
252 PyrSlot *colString;
253 PyrSlot *colSymbol;
254 PyrSlot *colComment;
255 PyrSlot *colNumber;
256 PyrSlot *colVals;
257 PyrSlot *colVars;
258 PyrSlot *colDecl;
259 PyrSlot *colPunc;
260 PyrSlot *colEnviron;
262 colText = g->sp - numArgsPushed + 2;
263 colClass = colText + 1;
264 colString = colClass + 1;
265 colSymbol = colString + 1;
266 colComment = colSymbol + 1;
267 colNumber = colComment + 1;
268 colVals = colNumber + 1;
269 colVars = colVals + 1;
270 colDecl = colVars + 1;
271 colPunc = colDecl + 1;
272 colEnviron = colPunc + 1;
274 if (isKindOfSlot(colText, s_color->u.classobj)) {
275 int err = slotColorVal(colText, &synColText);
276 if(err) return err;
279 if (isKindOfSlot(colClass, s_color->u.classobj)) {
280 int err = slotColorVal(colClass, &synColClass);
281 if(err) return err;
284 if (isKindOfSlot(colString, s_color->u.classobj)) {
285 int err = slotColorVal(colString, &synColString);
286 if(err) return err;
289 if (isKindOfSlot(colSymbol, s_color->u.classobj)) {
290 int err = slotColorVal(colSymbol, &synColSymbol);
291 if(err) return err;
294 if (isKindOfSlot(colComment, s_color->u.classobj)) {
295 int err = slotColorVal(colComment, &synColComment);
296 if(err) return err;
299 if (isKindOfSlot(colNumber, s_color->u.classobj)) {
300 int err = slotColorVal(colNumber, &synColNumber);
301 if(err) return err;
304 if (isKindOfSlot(colVals, s_color->u.classobj)) {
305 int err = slotColorVal(colVals, &synColSpecialVals);
306 if(err) return err;
309 if (isKindOfSlot(colVars, s_color->u.classobj)) {
310 int err = slotColorVal(colVars, &synColSpecialVars);
311 if(err) return err;
314 if (isKindOfSlot(colDecl, s_color->u.classobj)) {
315 int err = slotColorVal(colDecl, &synColDecl);
316 if(err) return err;
319 if (isKindOfSlot(colPunc, s_color->u.classobj)) {
320 int err = slotColorVal(colPunc, &synColPunc);
321 if(err) return err;
324 if (isKindOfSlot(colPunc, s_color->u.classobj)) {
325 int err = slotColorVal(colEnviron, &synColEnv);
326 if(err) return err;
329 return errNone;
333 int prTextWindow_SetSelectedBackgroundColor(struct VMGlobals *g, int numArgsPushed);
334 int prTextWindow_SetSelectedBackgroundColor(struct VMGlobals *g, int numArgsPushed)
336 if (!g->canCallOS) return errCantCallOS;
337 PyrSlot *a = g->sp-1;
338 PyrSlot *b = g->sp;
339 if (IsNil(b)) return errNone;
340 SCColor rgb;
341 int err = slotColorVal(b, &rgb);
342 if (err) return err;
343 NSColor *color = [NSColor colorWithCalibratedRed: rgb.red
344 green: rgb.green
345 blue: rgb.blue
346 alpha: rgb.alpha];
347 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
348 if (!doc) return errFailed;
349 [doc setSelectedBackgroundColor: color];
350 return errNone;
353 int prTextWindow_GetSelectedBackgroundColor(struct VMGlobals *g, int numArgsPushed);
354 int prTextWindow_GetSelectedBackgroundColor(struct VMGlobals *g, int numArgsPushed)
356 if (!g->canCallOS) return errCantCallOS;
358 PyrSlot *a = g->sp-1;
359 PyrSlot *b = g->sp;
360 if (IsNil(b)) return errNone;
361 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
362 if (!doc) return errFailed;
363 NSTextView * view = [doc initTextView];
364 if(!view) view = [doc textView];
365 if(!view) return errNone;
366 NSColor * color = [[[view selectedTextAttributes] objectForKey: NSBackgroundColorAttributeName] colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
367 if(!color) return errNone;
368 ColorData red, green, blue, alpha;
369 [color getRed:&red green:&green blue:&blue alpha:&alpha];
370 PyrSlot *slots = slotRawObject(b)->slots;
371 SetFloat(slots+0, red);
372 SetFloat(slots+1, green);
373 SetFloat(slots+2, blue);
374 SetFloat(slots+3, alpha);
375 return errNone;
379 int prTextWindow_SetTextColor(struct VMGlobals *g, int numArgsPushed);
380 int prTextWindow_SetTextColor(struct VMGlobals *g, int numArgsPushed)
382 if (!g->canCallOS) return errCantCallOS;
384 PyrSlot *a = g->sp-3;
385 PyrSlot *b = g->sp-2;
386 PyrSlot *c = g->sp-1;
387 PyrSlot *d = g->sp;
389 int rangeStart, rangeSize;
390 if (IsNil(b)) return errNone;
392 SCColor rgb;
393 int err = slotColorVal(b, &rgb);
394 if (err) return err;
395 err = slotIntVal(c, &rangeStart); //if -1 do not use range
396 if (err) return err;
397 err = slotIntVal(d, &rangeSize);
398 if (err) return err;
401 NSColor *color = [NSColor colorWithCalibratedRed: rgb.red
402 green: rgb.green
403 blue: rgb.blue
404 alpha: rgb.alpha];
406 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
407 if (!doc) return errFailed;
408 //[[doc textView] setBackgroundColor: color];
410 if(rangeStart < 0){
411 [[doc activeTextView] setTextColor: color];
412 [[doc activeTextView] didChangeText];
413 return errNone;
415 int length = [[[doc activeTextView] string] length];
416 if(rangeStart >= length) rangeStart = length - 1 ;
417 if(rangeStart + rangeSize >= length) rangeSize = length - rangeStart;
418 NSRange selectedRange = NSMakeRange(rangeStart, rangeSize);
421 [[doc activeTextView] setTextColor: color range: selectedRange];
422 [[doc activeTextView] didChangeText];
427 return errNone;
430 int prTextWindow_SetFont(struct VMGlobals *g, int numArgsPushed);
431 int prTextWindow_SetFont(struct VMGlobals *g, int numArgsPushed)
433 if (!g->canCallOS) return errCantCallOS;
434 PyrSlot *a = g->sp - 3;
435 PyrSlot *fontSlot = g->sp - 2; //fontsize
437 PyrSlot *d = g->sp - 1; //rangestart
438 PyrSlot *e = g->sp; //rangesize
439 if (IsNil(fontSlot)) return errNone; // use default font
440 if (!(isKindOfSlot(fontSlot, s_font->u.classobj))) return errWrongType;
442 PyrSlot *nameSlot = slotRawObject(fontSlot)->slots+0;
443 PyrSlot *sizeSlot = slotRawObject(fontSlot)->slots+1;
444 float size;
445 int err = slotFloatVal(sizeSlot, &size);
446 if (err) return err;
448 PyrString *pstring = slotRawString(nameSlot);
449 NSString *fontName = [NSString stringWithCString: pstring->s encoding:[NSString defaultCStringEncoding]];
450 fontName = [fontName substringToIndex: pstring->size];
451 if (!fontName) return errFailed;
452 NSFont *font = [NSFont fontWithName: fontName size: size];
453 if (!font) return errFailed;
455 int rangeStart, rangeSize;
456 err = slotIntVal(d, &rangeStart); //if -1 do not use range
457 if (err) return err;
458 err = slotIntVal(e, &rangeSize);
459 if (err) return err;
461 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
462 if (!doc) return errFailed;
463 if(rangeStart < 0){
464 [[doc activeTextView] setFont: font];
465 return errNone;
467 NSString* string = [[doc activeTextView] string];
468 int length = [string length];
469 if(length < 1) return errFailed;
470 if(rangeStart >= length) rangeStart = length - 1 ;
471 if(rangeStart + rangeSize >= length) rangeSize = length - rangeStart;
472 NSRange selectedRange = NSMakeRange(rangeStart, rangeSize);
474 [[doc activeTextView] setFont: font range: selectedRange];
475 return errNone;
477 int prTextWindow_UnderlineSelection(struct VMGlobals *g, int numArgsPushed);
478 int prTextWindow_UnderlineSelection(struct VMGlobals *g, int numArgsPushed)
480 if (!g->canCallOS) return errCantCallOS;
482 PyrSlot *a = g->sp;
484 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
485 if (!doc) return errFailed;
486 [[doc activeTextView] underline: NULL];
487 return errNone;
491 int prTextWindow_Close(struct VMGlobals *g, int numArgsPushed);
492 int prTextWindow_Close(struct VMGlobals *g, int numArgsPushed)
494 if (!g->canCallOS) return errCantCallOS;
496 PyrSlot *a = g->sp;
498 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
499 if (!doc) return errFailed;
500 SEL sel = @selector(closeWindow);
501 NSMethodSignature *sig = [MyDocument instanceMethodSignatureForSelector: sel];
503 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
504 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
505 [anInvocation setTarget: doc];
506 [anInvocation setSelector: sel];
507 [scvm defer: anInvocation];
510 return errNone;
513 int prTextWindow_SelectLine(struct VMGlobals *g, int numArgsPushed);
514 int prTextWindow_SelectLine(struct VMGlobals *g, int numArgsPushed)
516 if (!g->canCallOS) return errCantCallOS;
518 PyrSlot *a = g->sp - 1;
519 PyrSlot *b = g->sp; //the win number
521 int err, linenum;
522 err = slotIntVal(b, &linenum);
523 if (err) return errWrongType;
524 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
525 if (!doc) return errFailed;
526 //[doc selectLine: linenum];
527 SEL sel = @selector(selectLine:);
528 NSMethodSignature *sig = [MyDocument instanceMethodSignatureForSelector: sel];
530 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
531 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
532 [anInvocation setTarget: doc];
533 [anInvocation setSelector: sel];
534 [anInvocation setArgument: &linenum atIndex: 2];
535 [scvm defer: anInvocation];
537 return errNone;
540 int prTextWindow_SelectRange(struct VMGlobals *g, int numArgsPushed);
541 int prTextWindow_SelectRange(struct VMGlobals *g, int numArgsPushed)
543 if (!g->canCallOS) return errCantCallOS;
544 PyrSlot *a = g->sp - 2;
545 PyrSlot *b = g->sp - 1; //the win number
546 PyrSlot *c = g->sp;
548 int rangeStart, rangeSize;
549 int err = slotIntVal(b, &rangeStart);
550 if (err) return err;
551 err = slotIntVal(c, &rangeSize);
552 if (err) return err;
553 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
554 if (!doc) return errFailed;
555 [doc selectRangeStart: rangeStart size: rangeSize];
556 return errNone;
559 int prTextWindow_GetByIndex(struct VMGlobals *g, int numArgsPushed);
560 int prTextWindow_GetByIndex(struct VMGlobals *g, int numArgsPushed)
562 if (!g->canCallOS) return errCantCallOS;
564 PyrSlot *a = g->sp - 1;
565 PyrSlot *b = g->sp; //the win number
567 int err, inputIndex;
568 err = slotIntVal(b, &inputIndex);
569 if (err) return errWrongType;
571 NSDocumentController *docctl = [NSDocumentController sharedDocumentController];
572 if (!docctl) return errFailed;
573 MyDocument* doc = [[docctl documents] objectAtIndex: inputIndex];
574 if (!doc) return errFailed;
575 PyrObject * obj = [doc getSCObject];
576 //if there is a scobject already don't store i
577 if(obj){
578 //post("docuemnt has scObject already\n");
579 SetNil(a);
580 return errNone;
582 [doc setSCObject: slotRawObject(a)];
583 SetPtr(slotRawObject(a)->slots + 0, doc);
584 // if([doc getSCKeyDownAction])
585 // SetObject(slotRawObject(a)->slots + 1, [doc getSCKeyDownAction]);
586 return errNone;
589 int prTextWindow_GetLastIndex(struct VMGlobals *g, int numArgsPushed);
590 int prTextWindow_GetLastIndex(struct VMGlobals *g, int numArgsPushed)
592 if (!g->canCallOS) return errCantCallOS;
594 PyrSlot *a = g->sp;
595 NSDocumentController *docctl = [NSDocumentController sharedDocumentController];
596 if (!docctl) return errFailed;
597 MyDocument* doc = [[docctl documents] lastObject];
598 if (!doc) return errFailed;
599 PyrObject * obj = [doc getSCObject];
600 if(obj){
601 //post("docuemnt has scObject already\n");
602 SetNil(a);
603 return errNone;
605 [doc setSCObject: slotRawObject(a)];
606 SetPtr(slotRawObject(a)->slots + 0, doc);
607 return errNone;
610 int prTextWindow_ToFront(struct VMGlobals *g, int numArgsPushed);
611 int prTextWindow_ToFront(struct VMGlobals *g, int numArgsPushed)
613 if (!g->canCallOS) return errCantCallOS;
615 PyrSlot *b = g->sp; //the win number
617 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(b)->slots);
618 if (!doc) return errFailed;
619 docCreatedFromLang = true;
620 [[[doc textView] window] makeKeyAndOrderFront: nil];
621 docCreatedFromLang = false;
622 return errNone;
625 int prTextWindow_SyntaxColorize(struct VMGlobals *g, int numArgsPushed);
626 int prTextWindow_SyntaxColorize(struct VMGlobals *g, int numArgsPushed)
628 if (!g->canCallOS) return errCantCallOS;
630 PyrSlot *b = g->sp; //the win number
632 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(b)->slots);
633 if (!doc) return errFailed;
634 [doc syntaxColorize: nil];
635 return errNone;
638 int prTextWindow_InsertTextInRange(struct VMGlobals *g, int numArgsPushed);
639 int prTextWindow_InsertTextInRange(struct VMGlobals *g, int numArgsPushed)
641 if (!g->canCallOS) return errCantCallOS;
642 PyrSlot *a = g->sp - 3;
643 PyrSlot *b = g->sp - 2; //the text
644 PyrSlot *c = g->sp - 1;
645 PyrSlot *d = g->sp;
647 if (!(isKindOfSlot(b, class_string))) return errWrongType;
649 int rangeStart, rangeSize;
650 int err = slotIntVal(c, &rangeStart); //if -1 do not use range
651 if (err) return err;
652 err = slotIntVal(d, &rangeSize);
653 if (err) return err;
655 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
656 if (!doc) return errFailed;
657 PyrString* pstring = slotRawString(b);
658 NSRange selectedRange;
659 int length = [[[doc activeTextView] string] length];
661 if(rangeSize < 0) rangeSize = length - 1;
662 if(rangeStart >= length) rangeStart = length - 1 ;
663 if(rangeStart + rangeSize >= length) rangeSize = length - rangeStart;
665 if(rangeStart<0) selectedRange = NSMakeRange(0, length);
666 else selectedRange = NSMakeRange(rangeStart, rangeSize);
668 NSString *string = [NSString stringWithCString: pstring->s encoding:[NSString defaultCStringEncoding]];
669 string = [string substringToIndex:pstring->size];
670 if ([[doc activeTextView] shouldChangeTextInRange: selectedRange replacementString: string]) {
671 [[doc activeTextView] replaceCharactersInRange: selectedRange withString: string];
672 [[doc activeTextView] didChangeText];
675 return errNone;
678 //insert in current position
679 int prTextWindow_InsertText(struct VMGlobals *g, int numArgsPushed);
680 int prTextWindow_InsertText(struct VMGlobals *g, int numArgsPushed)
682 if (!g->canCallOS) return errCantCallOS;
683 PyrSlot *b = g->sp - 1;
684 PyrSlot *c = g->sp; //the text
685 if (!(isKindOfSlot(c, class_string))) return errWrongType;
687 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(b)->slots);
688 if (!doc) return errFailed;
689 PyrString* string = slotRawString(c);
690 [doc insertText: string->s length: string->size];
691 return errNone;
694 int prTextWindow_GetName(struct VMGlobals *g, int numArgsPushed);
695 int prTextWindow_GetName(struct VMGlobals *g, int numArgsPushed)
697 if (!g->canCallOS) return errCantCallOS;
698 PyrSlot *a = g->sp;
699 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
700 if (!doc) return errFailed;
701 // const char * cstr = [[doc displayName] UTF8String];
702 const char * cstr = [[[[doc textView] window] title] UTF8String];
703 PyrString *string = newPyrString(g->gc, cstr, 0, true);
704 SetObject(a, string);
705 return errNone;
708 int prTextWindow_GetFileName(struct VMGlobals *g, int numArgsPushed);
709 int prTextWindow_GetFileName(struct VMGlobals *g, int numArgsPushed)
711 if (!g->canCallOS) return errCantCallOS;
712 PyrSlot *a = g->sp;
713 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
714 if (!doc) return errFailed;
715 const char * cstr = [[[doc fileURL] path] UTF8String];
716 if(!cstr){
717 SetNil(a);
718 return errNone;
720 PyrString *string = newPyrString(g->gc, cstr, 0, true);
721 SetObject(a, string);
722 return errNone;
724 int prTextWindow_SetFileName(struct VMGlobals *g, int numArgsPushed);
725 int prTextWindow_SetFileName(struct VMGlobals *g, int numArgsPushed)
727 if (!g->canCallOS) return errCantCallOS;
728 PyrSlot *a = g->sp-1;
729 PyrSlot *b = g->sp;
730 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
731 if (!doc) return errFailed;
732 PyrString* filename = slotRawString(b);
733 NSString *nsname = [NSString stringWithCString: filename->s encoding:[NSString defaultCStringEncoding]];
734 nsname = [nsname substringToIndex:filename->size];
735 if(!nsname) return errFailed;
736 NSURL *urlname = [NSURL URLWithString:nsname];
737 [doc setFileURL: urlname];
738 return errNone;
740 int prTextWindow_GetSelectedRangeLocation(struct VMGlobals *g, int numArgsPushed);
741 int prTextWindow_GetSelectedRangeLocation(struct VMGlobals *g, int numArgsPushed)
743 if (!g->canCallOS) return errCantCallOS;
745 PyrSlot *a = g->sp;
746 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
747 if (!doc) return errFailed;
748 NSRange range = [[doc activeTextView] selectedRange];
749 SetInt(a, range.location);
750 return errNone;
753 #if 0
754 int prTextWindow_SelectToken(struct VMGlobals *g, int numArgsPushed);
755 int prTextWindow_SelectToken(struct VMGlobals *g, int numArgsPushed)
757 if (!g->canCallOS) return errCantCallOS;
759 PyrSlot *a = g->sp;
760 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
761 if (!doc) return errFailed;
763 NSString* string = [[doc activeTextView] string];
764 NSRange range = [[doc activeTextView] selectedRange];
765 int clickLoc = range.location;
766 range.location = range.location < 64 ? 0 : range.location - 64;
767 range.length = 64;
768 NSRange wordRange = range;
769 for (int i=clickLoc; i>=(int)range.location; --i) {
770 if (isspace([string characterAtIndex: i])) {
771 wordRange.location = i;
772 wordRange.length = range.location + range.length - i;
773 break;
776 for (int i=clickLoc; i<(int)(range.location + range.length); ++i) {
777 if (isspace([string characterAtIndex: i])) {
778 wordRange.length = i - wordRange.location;
779 break;
783 [doc selectRangeStart: wordRange.location size: wordRange.length];
785 return errNone;
787 #endif
789 int prTextWindow_SelectUnderlinedText(struct VMGlobals *g, int numArgsPushed);
790 int prTextWindow_SelectUnderlinedText(struct VMGlobals *g, int numArgsPushed)
792 if (!g->canCallOS) return errCantCallOS;
794 PyrSlot *a = g->sp - 1;
795 PyrSlot *b = g->sp;
797 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
798 if (!doc) return errFailed;
800 NSTextView *textView = [doc activeTextView];
802 int clickLoc;
803 int err = slotIntVal(b, &clickLoc);
804 if (err) {
805 clickLoc = [textView selectedRange].location;
807 NSRange range = NSMakeRange(clickLoc, 0);
809 NSTextStorage *textStorage = [textView textStorage];
810 NSRange limitRange = NSMakeRange(0, [textStorage length]);
811 NSRange underlineRange;
812 if (range.location < limitRange.length) {
814 id isUnderlined = [textStorage attribute: NSUnderlineStyleAttributeName
815 atIndex: range.location
816 longestEffectiveRange: &underlineRange
817 inRange: limitRange];
819 if (isUnderlined) {
821 NSString* string = [[doc activeTextView] string];
823 // underlined range should not cross newlines.
824 for (int i=clickLoc; i>=(int)underlineRange.location; --i) {
825 int c = [string characterAtIndex: i];
826 if (c == '\n') {
827 underlineRange.length -= i + 1 - underlineRange.location;
828 underlineRange.location = i + 1;
829 break;
832 for (int i=clickLoc; i<(int)(underlineRange.location + underlineRange.length); ++i) {
833 int c = [string characterAtIndex: i];
834 if (c == '\n') {
835 underlineRange.length = i - underlineRange.location;
836 break;
840 // trim whitespace
841 int start = underlineRange.location;
842 int end = underlineRange.location + underlineRange.length;
843 for (int i=start; i<end; ++i) {
844 int c = [string characterAtIndex: i];
845 if (!isspace(c)) break;
846 underlineRange.location ++;
847 underlineRange.length --;
850 start = underlineRange.location + underlineRange.length - 1;
851 end = underlineRange.location;
852 for (int i=start; i>=end; --i) {
853 int c = [string characterAtIndex: i];
854 if (!isspace(c)) break;
855 underlineRange.length --;
858 // include square brackets in selection
859 start = underlineRange.location - 1;
860 end = underlineRange.location + underlineRange.length;
861 if (start >= 0 && [string characterAtIndex: start] == '['
862 && end < (int)limitRange.length && [string characterAtIndex: end] == ']') {
863 underlineRange.location --;
864 underlineRange.length += 2;
866 // include parentheses in selection
867 if (start >= 0 && [string characterAtIndex: start] == '('
868 && end < (int)limitRange.length && [string characterAtIndex: end] == ')') {
869 underlineRange.location --;
870 underlineRange.length += 2;
873 if ((int)underlineRange.length > 0 && (int)underlineRange.location <= clickLoc && clickLoc < (int)(underlineRange.location + underlineRange.length)) {
874 [doc selectRangeStart: underlineRange.location size: underlineRange.length];
875 SetTrue(a);
876 } else {
877 SetFalse(a);
879 } else {
880 SetFalse(a);
882 } else {
883 SetFalse(a);
885 return errNone;
888 int prTextWindow_LinkAtClickPos(struct VMGlobals *g, int numArgsPushed);
889 int prTextWindow_LinkAtClickPos(struct VMGlobals *g, int numArgsPushed)
891 if (!g->canCallOS) return errCantCallOS;
893 PyrSlot *a = g->sp - 1;
894 PyrSlot *b = g->sp;
896 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
897 if (!doc) return errFailed;
899 NSTextView *textView = [doc activeTextView];
901 int clickLoc;
902 int err = slotIntVal(b, &clickLoc);
903 if (err) {
904 clickLoc = [textView selectedRange].location;
906 NSRange range = NSMakeRange(clickLoc, 0);
908 NSTextStorage *textStorage = [textView textStorage];
909 NSRange limitRange = NSMakeRange(0, [textStorage length]);
910 if (range.location < limitRange.length) {
912 id isLink = [textStorage attribute: NSLinkAttributeName atIndex: range.location effectiveRange: NULL];
914 if (isLink) { SetTrue(a);} else { SetFalse(a);}
916 } else {
917 SetFalse(a);
919 return errNone;
923 int prTextWindow_GetSelectedRangeLength(struct VMGlobals *g, int numArgsPushed);
924 int prTextWindow_GetSelectedRangeLength(struct VMGlobals *g, int numArgsPushed)
926 if (!g->canCallOS) return errCantCallOS;
927 PyrSlot *a = g->sp;
928 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
929 if (!doc) return errFailed;
930 NSRange range = [[doc activeTextView] selectedRange];
931 SetInt(a, range.length);
932 return errNone;
935 int prTextWindow_SelectedText(struct VMGlobals *g, int numArgsPushed);
936 int prTextWindow_SelectedText(struct VMGlobals *g, int numArgsPushed)
938 if (!g->canCallOS) return errCantCallOS;
940 PyrSlot *a = g->sp;
942 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
943 if (!doc) return errFailed;
945 NSString* str = [[doc activeTextView] currentlySelectedTextOrLine:NULL];
946 const char * cstr = [str UTF8String];
947 PyrString *string = newPyrString(g->gc, cstr, 0, true);
948 SetObject(a, string);
949 return errNone;
952 int prTextWindow_TextWithRange(struct VMGlobals *g, int numArgsPushed);
953 int prTextWindow_TextWithRange(struct VMGlobals *g, int numArgsPushed)
956 if (!g->canCallOS) return errCantCallOS;
957 PyrSlot *a = g->sp - 2;
958 PyrSlot *b = g->sp - 1; //from
959 PyrSlot *c = g->sp;//length
961 int rangeStart, rangeSize;
962 int err = slotIntVal(b, &rangeStart);
963 if (err) return err;
964 err = slotIntVal(c, &rangeSize);
965 if (err) return err;
966 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
967 if (!doc) return errFailed;
968 NSString* string = [[doc activeTextView] string];
969 int length = [string length];
970 if(length < 1)
972 SetNil(a);
973 return errNone;
975 if(rangeStart >= length) rangeStart = length - 1 ;
976 if(rangeStart + rangeSize >= length) rangeSize = length - rangeStart;
978 NSRange selectedRange = NSMakeRange(rangeStart, rangeSize);
979 NSString* str = [string substringWithRange: selectedRange];
980 const char * cstr = [str UTF8String];
981 PyrString *pstring = newPyrString(g->gc, cstr, 0, true);
982 SetObject(a, pstring);
983 return errNone;
986 int prTextWindow_Text(struct VMGlobals *g, int numArgsPushed);
987 int prTextWindow_Text(struct VMGlobals *g, int numArgsPushed)
989 if (!g->canCallOS) return errCantCallOS;
991 PyrSlot *a = g->sp;
993 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
994 if (!doc) return errFailed;
996 NSString* str = [[doc activeTextView] string];
997 const char * cstr = [str UTF8String];
998 PyrString *string = newPyrString(g->gc, cstr, 0, true);
999 SetObject(a, string);
1000 return errNone;
1002 int prTextWindow_RemoveUndo(struct VMGlobals *g, int numArgsPushed);
1003 int prTextWindow_RemoveUndo(struct VMGlobals *g, int numArgsPushed)
1005 if (!g->canCallOS) return errCantCallOS;
1007 PyrSlot *a = g->sp;
1009 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
1010 if (!doc) return errFailed;
1011 [doc updateChangeCount: NSChangeCleared];
1012 return errNone;
1015 int prTextWindow_UnfocusedFront(struct VMGlobals *g, int numArgsPushed);
1016 int prTextWindow_UnfocusedFront(struct VMGlobals *g, int numArgsPushed)
1018 if (!g->canCallOS) return errCantCallOS;
1020 PyrSlot *a = g->sp;
1022 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
1023 if (!doc) return errFailed;
1024 [[[doc textView] window] orderFrontRegardless];
1025 return errNone;
1028 int prTextWindow_AlwaysOnTop(struct VMGlobals *g, int numArgsPushed);
1029 int prTextWindow_AlwaysOnTop(struct VMGlobals *g, int numArgsPushed)
1031 if (!g->canCallOS) return errCantCallOS;
1033 PyrSlot *a = g->sp-1;
1034 PyrSlot *b = g->sp;
1036 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
1037 if (!doc) return errFailed;
1038 if(IsTrue(b))
1039 [[[doc textView] window] setLevel:NSFloatingWindowLevel];
1040 else
1041 [[[doc textView] window] setLevel:NSNormalWindowLevel];
1042 return errNone;
1045 int prTextWindow_IsAlwaysOnTop(struct VMGlobals *g, int numArgsPushed);
1046 int prTextWindow_IsAlwaysOnTop(struct VMGlobals *g, int numArgsPushed)
1048 if (!g->canCallOS) return errCantCallOS;
1049 PyrSlot *a = g->sp;
1050 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
1051 if (!doc) return errFailed;
1052 int level = [[[doc textView] window] level];
1053 if(level>0) SetTrue(a);
1054 else SetFalse(a);
1055 return errNone;
1058 int prTextWindow_SetPromptToSave(struct VMGlobals *g, int numArgsPushed);
1059 int prTextWindow_SetPromptToSave(struct VMGlobals *g, int numArgsPushed)
1061 if (!g->canCallOS) return errCantCallOS;
1063 PyrSlot *a = g->sp-1;
1064 PyrSlot *b = g->sp;
1066 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
1067 if (!doc) return errFailed;
1068 if(IsTrue(b))
1069 [doc setPromptToSave:YES];
1070 else
1071 [doc setPromptToSave:NO];
1072 return errNone;
1075 int prTextWindow_PromptToSave(struct VMGlobals *g, int numArgsPushed);
1076 int prTextWindow_PromptToSave(struct VMGlobals *g, int numArgsPushed)
1078 if (!g->canCallOS) return errCantCallOS;
1079 PyrSlot *a = g->sp;
1080 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
1081 if (!doc) return errFailed;
1082 bool flag = [doc promptToSave];
1083 if(flag) SetTrue(a);
1084 else SetFalse(a);
1085 return errNone;
1088 extern NSTextView *gPostView;
1090 int prTextWindow_GetIndexOfListener(struct VMGlobals *g, int numArgsPushed);
1091 int prTextWindow_GetIndexOfListener(struct VMGlobals *g, int numArgsPushed)
1093 if (!g->canCallOS) return errCantCallOS;
1095 PyrSlot *a = g->sp;
1096 NSDocumentController *docctl = [NSDocumentController sharedDocumentController];
1097 if (!docctl) return errFailed;
1098 MyDocument* doc = (MyDocument*)[gPostView delegate];
1099 if (!doc) return errFailed;
1100 int index = [[docctl documents] indexOfObject: doc];
1101 SetInt(a, index);
1102 return errNone;
1105 int prTextWindow_UsesAutoInOutdent(struct VMGlobals *g, int numArgsPushed);
1106 int prTextWindow_UsesAutoInOutdent(struct VMGlobals *g, int numArgsPushed)
1108 if (!g->canCallOS) return errCantCallOS;
1110 PyrSlot *a = g->sp - 1;
1111 PyrSlot *b = g->sp; //bool
1113 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
1114 if (!doc) return errFailed;
1115 [doc setUsesAutoInOutdent:IsTrue(b)];
1116 return errNone;
1119 int prTextWindow_DefaultUsesAutoInOutdent(struct VMGlobals *g, int numArgsPushed);
1120 int prTextWindow_DefaultUsesAutoInOutdent(struct VMGlobals *g, int numArgsPushed)
1122 if (!g->canCallOS) return errCantCallOS;
1124 //PyrSlot *a = g->sp - 1;
1125 PyrSlot *b = g->sp; //bool
1127 defaultDocumentUseAutoInOutDent = IsTrue(b);
1128 return errNone;
1132 ///////
1135 int prNewTextWindow(struct VMGlobals *g, int numArgsPushed);
1136 int prNewTextWindow(struct VMGlobals *g, int numArgsPushed)
1138 if (!g->canCallOS) return errCantCallOS;
1140 PyrSlot *d = g->sp - 3;
1141 PyrSlot *titleSlot = g->sp - 2;
1142 PyrSlot *stringSlot = g->sp - 1;
1143 PyrSlot *c = g->sp;
1145 if (!(isKindOfSlot(stringSlot, class_string))) return errWrongType;
1147 PyrString* string = slotRawString(stringSlot);
1148 NSDocumentController *docctl = [NSDocumentController sharedDocumentController];
1149 if (!docctl) return errFailed;
1150 docCreatedFromLang = true;
1151 NSError * outError;
1152 MyDocument* doc = [docctl openUntitledDocumentAndDisplay:YES error: &outError];
1153 // MyDocument* doc = [docctl currentDocument];
1154 if (!doc) {
1155 docCreatedFromLang = false;
1156 return errFailed;
1159 [doc insertText: string->s length: string->size];
1160 NSWindow *window = [[[doc windowControllers] objectAtIndex: 0] window];
1161 if (!window) {
1162 docCreatedFromLang = false;
1163 return errFailed;
1165 if (isKindOfSlot(titleSlot, class_string)) {
1166 PyrString* title = slotRawString(titleSlot);
1167 NSString *nstitle = [NSString stringWithCString: title->s encoding:[NSString defaultCStringEncoding]];
1168 nstitle = [nstitle substringToIndex: title->size];
1169 [window setTitle: nstitle];
1171 [[doc undoManager] removeAllActions];
1173 if (IsTrue(c)) {
1174 gPostView = [doc textView];
1176 PyrObject * obj = [doc getSCObject];
1177 if(obj){
1178 //post("docuemnt has scObject already\n");
1179 docCreatedFromLang = false;
1180 SetNil(d);
1181 return errNone;
1183 [doc setSCObject: slotRawObject(d)];
1184 SetPtr(slotRawObject(d)->slots + 0, doc);
1185 docCreatedFromLang = false;
1186 return errNone;
1190 int prOpenTextFile(struct VMGlobals *g, int numArgsPushed);
1191 int prOpenTextFile(struct VMGlobals *g, int numArgsPushed)
1193 if (!g->canCallOS) return errCantCallOS;
1194 PyrSlot *d = g->sp - 3;
1196 PyrSlot *a = g->sp - 2;
1197 PyrSlot *b = g->sp - 1;
1198 PyrSlot *c = g->sp;
1200 if (!(isKindOfSlot(a, class_string))) return errWrongType;
1201 PyrString* string = slotRawString(a);
1202 if(string->size == 0) return errFailed;
1203 int rangeStart, rangeSize;
1204 int err = slotIntVal(b, &rangeStart);
1205 if (err) return err;
1206 err = slotIntVal(c, &rangeSize);
1207 if (err) return err;
1209 NSDocumentController *docctl = [NSDocumentController sharedDocumentController];
1210 if (!docctl) {
1211 post("No NSDocumentController\n");
1212 return errFailed;
1215 NSString *nsstring = [NSString stringWithCString: string->s encoding:[NSString defaultCStringEncoding]];
1216 nsstring = [nsstring substringToIndex:string->size];
1217 NSURL *url = [NSURL fileURLWithPath: nsstring];
1218 NSString *nspath = [url path];
1220 docCreatedFromLang = true;
1221 MyDocument *doc = [docctl openDocumentWithContentsOfFile: nspath display: true];
1222 if (!doc) {
1223 post("Can't open Document '%s'\n", [nspath cStringUsingEncoding:[NSString defaultCStringEncoding]]);
1224 docCreatedFromLang = false;
1225 return errFailed;
1227 NSWindow *window = [[[doc windowControllers] objectAtIndex: 0] window];
1228 if (!window) {
1229 post("window controller returns nil\n");
1230 docCreatedFromLang = false;
1231 return errFailed;
1233 [window makeKeyAndOrderFront: nil];
1235 // select
1236 [doc selectRangeStart: rangeStart size: rangeSize];
1238 PyrObject * obj = [doc getSCObject];
1239 //if there is a scobject already don't store i
1240 if(obj){
1241 //post("docuemnt has scObject already\n");
1242 SetNil(d);
1243 docCreatedFromLang = false;
1244 return errNone;
1246 [doc setSCObject: slotRawObject(d)];
1247 SetPtr(slotRawObject(d)->slots + 0, doc);
1248 docCreatedFromLang = false;
1249 return errNone;
1252 int prGetStringFromUser(struct VMGlobals *g, int numArgsPushed);
1253 int prGetStringFromUser(struct VMGlobals *g, int numArgsPushed)
1255 if (!g->canCallOS) return errCantCallOS;
1257 //PyrSlot *a = g->sp - 2;
1258 //PyrSlot *b = g->sp - 1;
1259 //PyrSlot *c = g->sp;
1261 return errNone;
1264 extern NSFont *defaultFont;
1266 int prTextWindow_SetDefaultFont(struct VMGlobals *g, int numArgsPushed);
1267 int prTextWindow_SetDefaultFont(struct VMGlobals *g, int numArgsPushed)
1269 NSFont *newDefault, *oldDefault;
1270 if (!g->canCallOS) return errCantCallOS;
1271 PyrSlot *fontSlot = g->sp; //font
1272 if (IsNil(fontSlot)) return errNone; // use default font
1273 if (!(isKindOfSlot(fontSlot, s_font->u.classobj))) return errWrongType;
1275 PyrSlot *nameSlot = slotRawObject(fontSlot)->slots+0;
1276 PyrSlot *sizeSlot = slotRawObject(fontSlot)->slots+1;
1277 float size;
1278 int err = slotFloatVal(sizeSlot, &size);
1279 if (err) return err;
1281 PyrString *pstring = slotRawString(nameSlot);
1282 NSString *fontName = [NSString stringWithCString:pstring->s encoding:[NSString defaultCStringEncoding]];
1283 fontName = [fontName substringToIndex:pstring->size];
1284 if (!fontName) return errFailed;
1285 newDefault = [NSFont fontWithName: fontName size: size];
1286 if (!newDefault) {
1287 return errFailed;
1288 } else {
1289 oldDefault = defaultFont;
1290 [newDefault retain];
1291 defaultFont = newDefault;
1292 [oldDefault release];
1294 return errNone;
1298 extern SCColor postTextColor;
1300 int prSetPostTextColor(struct VMGlobals *g, int numArgsPushed);
1301 int prSetPostTextColor(struct VMGlobals *g, int numArgsPushed)
1303 PyrSlot *a;
1304 PyrSlot *c;
1306 a = g->sp - 1;
1307 c = a + 1;
1309 if(!(isKindOfSlot(c, s_color->u.classobj))) return errWrongType;
1311 int err = slotColorVal(c, &postTextColor);
1312 if(err) return err;
1314 return errNone;
1318 int prBalanceParens(struct VMGlobals *g, int numArgsPushed);
1319 int prBalanceParens(struct VMGlobals *g, int numArgsPushed)
1321 if (!g->canCallOS) return errCantCallOS;
1323 PyrSlot *a = g->sp;
1325 MyDocument* doc = (MyDocument*) slotRawPtr(slotRawObject(a)->slots);
1326 if (!doc) return errFailed;
1327 [[doc activeTextView] balanceParens: nil];
1329 return errNone;
1333 int slotGetNSRect(PyrSlot* a, NSRect *r)
1335 PyrSlot *slots = slotRawObject(a)->slots;
1336 int err;
1337 err = slotVal(slots+0, &r->origin.x);
1338 if (err) return err;
1339 err = slotVal(slots+1, &r->origin.y);
1340 if (err) return err;
1341 err = slotVal(slots+2, &r->size.width);
1342 if (err) return err;
1343 err = slotVal(slots+3, &r->size.height);
1344 if (err) return err;
1346 return errNone;
1350 int slotGetCGRect(PyrSlot* a, CGRect *r)
1352 PyrSlot *slots = slotRawObject(a)->slots;
1353 int err;
1354 err = slotVal(slots+0, &r->origin.x);
1355 if (err) return err;
1356 err = slotVal(slots+1, &r->origin.y);
1357 if (err) return err;
1358 err = slotVal(slots+2, &r->size.width);
1359 if (err) return err;
1360 err = slotVal(slots+3, &r->size.height);
1361 if (err) return err;
1363 return errNone;
1367 int slotGetPoint(PyrSlot* a, NSPoint *p)
1369 PyrSlot *slots = slotRawObject(a)->slots;
1370 int err;
1371 err = slotVal(slots+0, &p->x);
1372 if (err) return err;
1373 err = slotVal(slots+1, &p->y);
1374 if (err) return err;
1376 return errNone;
1379 int prSCWindow_New(struct VMGlobals *g, int numArgsPushed);
1380 int prSCWindow_New(struct VMGlobals *g, int numArgsPushed)
1382 if (!g->canCallOS) return errCantCallOS;
1384 PyrSlot *args = g->sp - 6;
1385 PyrSlot *a = args + 0;
1386 PyrSlot *b = args + 1; // name
1387 PyrSlot *c = args + 2; // bounds
1388 PyrSlot *d = args + 3; // resizable
1389 PyrSlot *e = args + 4; // border
1390 PyrSlot *f = args + 6; // view
1391 PyrSlot *h = args + 5; // scroll
1392 PyrSlot *j = args + 7; // is this app modal? (Doesn't matter for sheets as they have no close button)
1394 if (!(isKindOfSlot(b, class_string))) return errWrongType;
1395 if (!(isKindOfSlot(c, s_rect->u.classobj))) return errWrongType;
1397 bool eIsTrue = IsTrue(e);
1398 unsigned int mask = IsTrue(d) && eIsTrue ? NSResizableWindowMask : 0;
1399 if (IsFalse(j) && eIsTrue){
1400 mask |= NSMiniaturizableWindowMask | NSClosableWindowMask;
1402 mask |= eIsTrue ? NSTitledWindowMask | NSTexturedBackgroundWindowMask : NSBorderlessWindowMask;
1404 NSRect bounds;
1405 int err = slotGetNSRect(c, &bounds);
1406 if (err) return err;
1408 PyrString *string = slotRawString(b);
1409 // NSString *title = [NSString stringWithCString: string->s length: string->size];
1410 NSString *title = [NSString stringWithCString:string->s encoding:[NSString defaultCStringEncoding]];
1411 title = [title substringToIndex:string->size];
1413 SCNSWindow *window = [[SCNSWindow alloc]
1414 initWithContentRect: bounds
1415 styleMask: mask
1416 backing: NSBackingStoreBuffered
1417 defer: NO];
1418 [window setTitle: title];
1419 [window setHasBorders: YES];
1421 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
1422 SCGraphView* view = [[SCGraphView alloc] initWithFrame: bounds];
1423 [view setSCObject: slotRawObject(a)];
1424 SetPtr(slotRawObject(a)->slots + 0, view);
1425 [window setDelegate: scvm];
1426 [window setSCGraphView: view];
1428 if(IsTrue(h)) {
1429 SCScrollTopView* scrollTopView = (SCScrollTopView*)slotRawPtr(slotRawObject(f)->slots);
1430 [view setSCTopView: scrollTopView];
1432 NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame: bounds];
1433 [scrollView setHasVerticalScroller:YES];
1434 [scrollView setHasHorizontalScroller:YES];
1435 [scrollView setAutohidesScrollers:YES];
1436 [[scrollView horizontalScroller] setControlSize:NSSmallControlSize];
1437 [[scrollView verticalScroller] setControlSize:NSSmallControlSize];
1438 [[scrollView horizontalScroller] setControlTint:NSGraphiteControlTint];
1439 [[scrollView verticalScroller] setControlTint:NSGraphiteControlTint];
1441 [scrollView setBackgroundColor:[NSColor clearColor]];
1442 [scrollView setDrawsBackground:NO];
1443 // configure the scroller to have no visible border
1444 [scrollView setBorderType:NSNoBorder];
1445 [scrollView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
1446 [scrollView setDocumentView:view];
1448 [scrollView setPostsFrameChangedNotifications: YES]; // we need this to resize the SCGraphView if the scroll view exceeds its bounds
1450 NSClipView *contentView = [scrollView contentView];
1451 [contentView setPostsBoundsChangedNotifications:YES];
1453 scrollTopView->SetNSScrollView(scrollView);
1454 [view autorelease];
1456 [window setContentView: scrollView];
1457 [scrollView autorelease];
1459 [[NSNotificationCenter defaultCenter] addObserver:view
1460 selector:@selector(scrollViewResized:)
1461 name:@"NSViewFrameDidChangeNotification"
1462 object:scrollView];
1463 [[NSNotificationCenter defaultCenter] addObserver:view
1464 selector:@selector(userScrolled:)
1465 name:@"NSViewBoundsDidChangeNotification"
1466 object:contentView];
1468 } else {
1469 [view setSCTopView: (SCTopView*)slotRawPtr(slotRawObject(f)->slots)];
1470 [window setContentView: view];
1471 [view autorelease];
1474 NSRect matrect = NSMakeRect(40,40,400,400);
1475 NSSize cellsize = NSMakeSize(200,100);
1476 id mx = [[NSMatrix alloc]
1477 initWithFrame: matrect
1478 mode: NSTrackModeMatrix
1479 cellClass: [NSSliderCell class]
1480 numberOfRows: 4
1481 numberOfColumns: 2 ];
1482 [mx setCellSize: cellsize];
1483 [view addSubview: mx];
1486 [window makeFirstResponder: view];
1487 [window setFrameOrigin: bounds.origin];
1488 [scvm addWindow: window];
1491 id sender = nil;
1493 SEL sel = @selector(makeKeyAndOrderFront:);
1494 NSMethodSignature *sig = [NSWindow instanceMethodSignatureForSelector: sel];
1496 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
1497 [anInvocation setTarget: window];
1498 [anInvocation setSelector: sel];
1499 [anInvocation setArgument: &sender atIndex: 2];
1500 [scvm defer: anInvocation];
1503 SEL sel = @selector(newGuiWindowTitle:bounds:SCObject:styleMask:);
1504 NSMethodSignature *sig = [SCVirtualMachine instanceMethodSignatureForSelector: sel];
1506 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
1507 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
1508 [anInvocation setTarget: scvm];
1509 [anInvocation setSelector: sel];
1510 [anInvocation setArgument: &title atIndex: 2];
1511 [anInvocation setArgument: &bounds atIndex: 3];
1512 [anInvocation setArgument: &slotRawObject(a) atIndex: 4];
1513 [anInvocation setArgument: &flags atIndex: 5];
1514 [scvm defer: anInvocation];
1516 return errNone;
1519 int prSCWindow_Refresh(struct VMGlobals *g, int numArgsPushed);
1520 int prSCWindow_Refresh(struct VMGlobals *g, int numArgsPushed)
1522 if (!g->canCallOS) return errCantCallOS;
1524 PyrSlot *a = g->sp;
1525 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1526 if (!view) return errNone;
1528 SEL sel = @selector(setNeedsDisplay:);
1529 NSMethodSignature *sig = [NSView instanceMethodSignatureForSelector: sel];
1531 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
1532 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
1533 [anInvocation setTarget: view];
1534 [anInvocation setSelector: sel];
1535 BOOL flag = YES;
1536 [anInvocation setArgument: &flag atIndex: 2];
1537 [scvm defer: anInvocation];
1539 return errNone;
1542 int prSCWindow_Close(struct VMGlobals *g, int numArgsPushed);
1543 int prSCWindow_Close(struct VMGlobals *g, int numArgsPushed)
1545 PyrSlot *a = g->sp;
1546 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1547 if (!view) return errNone;
1548 // [[view window] close];
1550 // NSWindow *window = [view window];
1552 SEL sel = @selector(closeWindow);
1553 NSMethodSignature *sig = [SCGraphView instanceMethodSignatureForSelector: sel];
1555 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
1556 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
1557 [anInvocation setTarget: view];
1558 [anInvocation setSelector: sel];
1559 [scvm defer: anInvocation];
1561 return errNone;
1564 int prSCWindow_Minimize(struct VMGlobals *g, int numArgsPushed);
1565 int prSCWindow_Minimize(struct VMGlobals *g, int numArgsPushed)
1567 if (!g->canCallOS) return errCantCallOS;
1569 PyrSlot *a = g->sp;
1570 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1571 if (!view) return errNone;
1572 //[[view window] miniaturize: nil];
1574 NSWindow *window = [view window];
1575 id sender = nil;
1577 SEL sel = @selector(miniaturize:);
1578 NSMethodSignature *sig = [NSWindow instanceMethodSignatureForSelector: sel];
1580 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
1581 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
1582 [anInvocation setTarget: window];
1583 [anInvocation setSelector: sel];
1584 [anInvocation setArgument: &sender atIndex: 2];
1585 [scvm defer: anInvocation];
1587 return errNone;
1590 int prSCWindow_ToFront(struct VMGlobals *g, int numArgsPushed);
1591 int prSCWindow_ToFront(struct VMGlobals *g, int numArgsPushed)
1593 if (!g->canCallOS) return errCantCallOS;
1595 PyrSlot *a = g->sp;
1596 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1597 if (!view) return errNone;
1598 //[[view window] makeKeyAndOrderFront: nil];
1600 NSWindow *window = [view window];
1601 id sender = nil;
1603 SEL sel = @selector(makeKeyAndOrderFront:);
1604 NSMethodSignature *sig = [NSWindow instanceMethodSignatureForSelector: sel];
1606 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
1607 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
1608 [anInvocation setTarget: window];
1609 [anInvocation setSelector: sel];
1610 [anInvocation setArgument: &sender atIndex: 2];
1611 [scvm defer: anInvocation];
1613 return errNone;
1617 int prSCWindow_FullScreen(struct VMGlobals *g, int numArgsPushed);
1618 int prSCWindow_FullScreen(struct VMGlobals *g, int numArgsPushed)
1620 if (!g->canCallOS) return errCantCallOS;
1622 PyrSlot *a = g->sp;
1623 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1624 if (!view) return errNone;
1626 NSWindow *window = [view window];
1628 SEL sel = @selector(becomeFullScreen:);
1629 NSMethodSignature *sig = [SCVirtualMachine instanceMethodSignatureForSelector: sel];
1631 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
1632 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
1633 [anInvocation setTarget: scvm];
1634 [anInvocation setSelector: sel];
1635 [anInvocation setArgument: &window atIndex: 2];
1636 [scvm defer: anInvocation];
1638 return errNone;
1641 int prSCWindow_EndFullScreen(struct VMGlobals *g, int numArgsPushed);
1642 int prSCWindow_EndFullScreen(struct VMGlobals *g, int numArgsPushed)
1644 if (!g->canCallOS) return errCantCallOS;
1646 PyrSlot *a = g->sp;
1647 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1648 if (!view) return errNone;
1650 NSWindow *window = [view window];
1652 SEL sel = @selector(endFullScreen:);
1653 NSMethodSignature *sig = [SCVirtualMachine instanceMethodSignatureForSelector: sel];
1655 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
1656 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
1657 [anInvocation setTarget: scvm];
1658 [anInvocation setSelector: sel];
1659 [anInvocation setArgument: &window atIndex: 2];
1660 [scvm defer: anInvocation];
1662 return errNone;
1665 int prSCWindow_SetShouldClose(struct VMGlobals *g, int numArgsPushed);
1666 int prSCWindow_SetShouldClose(struct VMGlobals *g, int numArgsPushed)
1668 PyrSlot *a = g->sp - 1;
1669 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1670 if (!view) return errNone;
1671 PyrSlot *boo = g->sp;
1673 // no need to defer, right ?
1674 [view setWindowShouldClose: IsTrue(boo)];
1676 return errNone;
1679 int prSCWindow_AlwaysOnTop(struct VMGlobals *g, int numArgsPushed);
1680 int prSCWindow_AlwaysOnTop(struct VMGlobals *g, int numArgsPushed)
1682 if (!g->canCallOS) return errCantCallOS;
1684 PyrSlot *a = g->sp - 1;
1685 PyrSlot *b = g->sp;
1687 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1688 if (!view) return errNone;
1689 NSWindow *window = [view window];
1690 if(IsTrue(b))
1691 [window setLevel:NSFloatingWindowLevel];
1692 else
1693 [window setLevel:NSNormalWindowLevel];
1694 return errNone;
1697 int prSCWindow_AcceptsClickThrough(struct VMGlobals *g, int numArgsPushed);
1698 int prSCWindow_AcceptsClickThrough(struct VMGlobals *g, int numArgsPushed)
1700 if (!g->canCallOS) return errCantCallOS;
1702 PyrSlot *a = g->sp - 1;
1703 PyrSlot *b = g->sp;
1705 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1706 if (!view) return errNone;
1707 if(IsTrue(b))
1708 [view setAcceptsClickThrough:YES];
1709 else
1710 [view setAcceptsClickThrough:NO];
1711 return errNone;
1714 int prSCWindow_SetName(struct VMGlobals *g, int numArgsPushed);
1715 int prSCWindow_SetName(struct VMGlobals *g, int numArgsPushed)
1717 if (!g->canCallOS) return errCantCallOS;
1719 PyrSlot *a = g->sp - 1;
1720 PyrSlot *b = g->sp;
1722 if (!(isKindOfSlot(b, class_string))) return errWrongType;
1724 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1725 if (!view) return errNone;
1726 PyrString *string = slotRawString(b);
1727 // NSString *title = [NSString stringWithCString: string->s length: string->size];
1728 NSString *title = [NSString stringWithCString: string->s encoding:[NSString defaultCStringEncoding]];
1729 // title = [title substringToIndex:string->size];
1730 [[view window] setTitle: title];
1731 return errNone;
1734 int prSCWindow_SetBounds(struct VMGlobals *g, int numArgsPushed);
1735 int prSCWindow_SetBounds(struct VMGlobals *g, int numArgsPushed)
1737 if (!g->canCallOS) return errCantCallOS;
1739 PyrSlot *a = g->sp - 1;
1740 PyrSlot *b = g->sp;
1742 if (!(isKindOfSlot(b, s_rect->u.classobj))) return errWrongType;
1744 NSRect bounds;
1745 int err = slotGetNSRect(b, &bounds);
1746 if (err) return err;
1748 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1749 if (!view) return errNone;
1751 NSWindow *window = [view window];
1753 bounds = [NSWindow frameRectForContentRect: bounds styleMask: [window styleMask]];
1755 //[window setFrame: bounds display: YES];
1757 SEL sel = @selector(setFrame:display:);
1758 NSMethodSignature *sig = [NSWindow instanceMethodSignatureForSelector: sel];
1759 BOOL flag = YES;
1761 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
1762 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
1763 [anInvocation setTarget: window];
1764 [anInvocation setSelector: sel];
1765 [anInvocation setArgument: &bounds atIndex: 2];
1766 [anInvocation setArgument: &flag atIndex: 3];
1767 [scvm defer: anInvocation];
1769 return errNone;
1772 int prSCWindow_GetBounds(struct VMGlobals *g, int numArgsPushed);
1773 int prSCWindow_GetBounds(struct VMGlobals *g, int numArgsPushed)
1775 if (!g->canCallOS) return errCantCallOS;
1777 PyrSlot *a = g->sp - 1;
1778 PyrSlot *b = g->sp;
1780 if (!(isKindOfSlot(b, s_rect->u.classobj))) return errWrongType;
1782 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1783 if (!view) return errNone;
1785 NSWindow *window = [view window];
1786 NSRect bounds = [window frame];
1787 bounds = [NSWindow contentRectForFrameRect: bounds styleMask: [window styleMask]];
1789 PyrSlot *slots = slotRawObject(b)->slots;
1790 SetFloat(slots+0, bounds.origin.x);
1791 SetFloat(slots+1, bounds.origin.y);
1792 SetFloat(slots+2, bounds.size.width);
1793 SetFloat(slots+3, bounds.size.height);
1794 slotCopy(a, b);
1796 return errNone;
1799 int prSCWindow_GetScreenBounds(struct VMGlobals *g, int numArgsPushed);
1800 int prSCWindow_GetScreenBounds(struct VMGlobals *g, int numArgsPushed)
1802 if (!g->canCallOS) return errCantCallOS;
1804 PyrSlot *a = g->sp - 1;
1805 PyrSlot *b = g->sp;
1807 if (!(isKindOfSlot(b, s_rect->u.classobj))) return errWrongType;
1809 NSRect bounds = [[NSScreen mainScreen] frame];
1810 PyrSlot *slots = slotRawObject(b)->slots;
1811 SetFloat(slots+0, bounds.origin.x);
1812 SetFloat(slots+1, bounds.origin.y);
1813 SetFloat(slots+2, bounds.size.width);
1814 SetFloat(slots+3, bounds.size.height);
1815 slotCopy(a, b);
1817 return errNone;
1823 //doesn't work as intended
1824 int prSCWindow_Show(struct VMGlobals *g, int numArgsPushed);
1825 int prSCWindow_Show(struct VMGlobals *g, int numArgsPushed)
1827 if (!g->canCallOS) return errCantCallOS;
1829 PyrSlot *a = g->sp;
1830 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1832 NSWindow *window = [view window];
1833 NSRect frame = [window frame];
1835 SEL sel = @selector(setFrame:display:);
1836 NSMethodSignature *sig = [NSWindow instanceMethodSignatureForSelector: sel];
1837 BOOL flag = YES;
1839 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
1840 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
1841 [anInvocation setTarget: window];
1842 [anInvocation setSelector: sel];
1843 [anInvocation setArgument: &frame atIndex: 2];
1844 [anInvocation setArgument: &flag atIndex: 3];
1845 [scvm defer: anInvocation];
1847 return errNone;
1849 //doesn't work as intended
1850 int prSCWindow_Hide(struct VMGlobals *g, int numArgsPushed);
1851 int prSCWindow_Hide(struct VMGlobals *g, int numArgsPushed)
1853 if (!g->canCallOS) return errCantCallOS;
1855 PyrSlot *a = g->sp;
1856 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1857 NSWindow *window = [view window];
1858 NSRect frame = [window frame];
1860 SEL sel = @selector(setFrame:display:);
1861 NSMethodSignature *sig = [NSWindow instanceMethodSignatureForSelector: sel];
1862 BOOL flag = NO;
1864 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
1865 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
1866 [anInvocation setTarget: window];
1867 [anInvocation setSelector: sel];
1868 [anInvocation setArgument: &frame atIndex: 2];
1869 [anInvocation setArgument: &flag atIndex: 3];
1870 [scvm defer: anInvocation];
1872 return errNone;
1875 int prSCWindow_SetAlpha(struct VMGlobals *g, int numArgsPushed);
1876 int prSCWindow_SetAlpha(struct VMGlobals *g, int numArgsPushed)
1878 if (!g->canCallOS) return errCantCallOS;
1880 PyrSlot *a = g->sp - 1;
1881 PyrSlot *b = g->sp;
1883 float alpha;
1884 int err = slotFloatVal(b, &alpha);
1885 CGFloat cgalpha = (CGFloat)alpha;
1886 if (err) return err;
1888 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1889 if (!view) return errNone;
1891 //[[view window] setAlphaValue: alpha];
1892 NSWindow *window = [view window];
1894 SEL sel = @selector(setAlphaValue:);
1895 NSMethodSignature *sig = [NSWindow instanceMethodSignatureForSelector: sel];
1897 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
1898 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
1899 [anInvocation setTarget: window];
1900 [anInvocation setSelector: sel];
1901 [anInvocation setArgument: &cgalpha atIndex: 2];
1902 [scvm defer: anInvocation];
1904 return errNone;
1907 int prSCWindow_SetAcceptMouseOver(struct VMGlobals *g, int numArgsPushed);
1908 int prSCWindow_SetAcceptMouseOver(struct VMGlobals *g, int numArgsPushed)
1910 if (!g->canCallOS) return errCantCallOS;
1912 PyrSlot *a = g->sp - 1;
1913 PyrSlot *b = g->sp;
1915 BOOL accept;
1916 accept = (BOOL) IsTrue(b);
1918 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1919 if (!view) return errNone;
1921 NSWindow *window = [view window];
1922 [window setAcceptsMouseMovedEvents:accept];
1923 return errNone;
1926 int prSCWindow_RunModal(struct VMGlobals *g, int numArgsPushed);
1927 int prSCWindow_RunModal(struct VMGlobals *g, int numArgsPushed)
1929 if (!g->canCallOS) return errCantCallOS;
1931 PyrSlot *a = g->sp;
1933 SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1934 if (!view) return errNone;
1936 NSWindow *window = [view window];
1938 SEL sel = @selector(runAsModal:);
1939 NSMethodSignature *sig = [SCVirtualMachine instanceMethodSignatureForSelector: sel];
1941 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
1942 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
1943 [anInvocation setTarget: scvm];
1944 [anInvocation setSelector: sel];
1945 [anInvocation setArgument: &window atIndex: 2];
1946 [scvm defer: anInvocation];
1948 return errNone;
1951 int prSCWindow_StopModal(struct VMGlobals *g, int numArgsPushed);
1952 int prSCWindow_StopModal(struct VMGlobals *g, int numArgsPushed)
1954 if (!g->canCallOS) return errCantCallOS;
1956 [NSApp abortModal];
1958 return errNone;
1961 int prSCWindow_RunModalSheet(struct VMGlobals *g, int numArgsPushed);
1962 int prSCWindow_RunModalSheet(struct VMGlobals *g, int numArgsPushed)
1964 if (!g->canCallOS) return errCantCallOS;
1966 PyrSlot *a = g->sp - 1;
1967 PyrSlot *b = g->sp;
1969 SCGraphView* sheetView = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
1970 if (!sheetView) return errNone;
1972 SCNSWindow *sheet = (SCNSWindow*)[sheetView window];
1974 SCGraphView* windowView = (SCGraphView*)slotRawPtr(slotRawObject(b)->slots);
1975 if (!windowView) return errNone;
1977 NSWindow *window = [windowView window];
1979 SEL sel = @selector(runAsModalSheet:forWindow:);
1980 NSMethodSignature *sig = [SCVirtualMachine instanceMethodSignatureForSelector: sel];
1982 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
1983 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
1984 [anInvocation setTarget: scvm];
1985 [anInvocation setSelector: sel];
1986 [anInvocation setArgument: &sheet atIndex: 2];
1987 [anInvocation setArgument: &window atIndex: 3];
1988 [scvm defer: anInvocation];
1990 return errNone;
1993 int prSCWindow_StopModalSheet(struct VMGlobals *g, int numArgsPushed);
1994 int prSCWindow_StopModalSheet(struct VMGlobals *g, int numArgsPushed)
1996 if (!g->canCallOS) return errCantCallOS;
1998 PyrSlot *a = g->sp;
2000 SCGraphView* sheetView = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
2001 if (!sheetView) return errNone;
2003 NSWindow *sheet = [sheetView window];
2005 [NSApp endSheet:sheet];
2007 return errNone;
2010 int prShowCursor(struct VMGlobals *g, int numArgsPushed);
2011 int prShowCursor(struct VMGlobals *g, int numArgsPushed)
2013 if (!g->canCallOS) return errCantCallOS;
2015 PyrSlot *b = g->sp;
2017 BOOL show;
2018 show = (BOOL) IsTrue(b);
2019 if(show) [NSCursor unhide];
2020 else [NSCursor hide];
2021 return errNone;
2024 int addFontAttribute(NSMutableDictionary *dict, PyrSlot *fontSlot);
2025 int addFontAttribute(NSMutableDictionary *dict, PyrSlot *fontSlot)
2027 if (IsNil(fontSlot)) return errNone; // use default font
2028 if (!(isKindOfSlot(fontSlot, s_font->u.classobj))) return errWrongType;
2030 PyrSlot *nameSlot = slotRawObject(fontSlot)->slots+0;
2031 PyrSlot *sizeSlot = slotRawObject(fontSlot)->slots+1;
2033 if (!(isKindOfSlot(nameSlot, class_string))) return errWrongType;
2035 float size;
2036 int err = slotFloatVal(sizeSlot, &size);
2037 if (err) return err;
2039 PyrString *string = slotRawString(nameSlot);
2040 NSString *fontName = [NSString stringWithCString:string->s encoding:[NSString defaultCStringEncoding]];
2041 fontName = [fontName substringToIndex:string->size];
2042 if (!fontName) return errFailed;
2043 NSFont *font = [NSFont fontWithName: fontName size: size];
2044 if (!font) return errFailed;
2045 [dict setObject: font forKey: NSFontAttributeName ];
2047 return errNone;
2052 int slotRGBColorVal(PyrSlot *slot, RGBColor *rgbcolor)
2054 if (!(isKindOfSlot(slot, s_color->u.classobj))) return errWrongType;
2056 PyrSlot *slots = slotRawObject(slot)->slots;
2058 int err;
2059 float red, green, blue, alpha;
2060 err = slotFloatVal(slots+0, &red);
2061 if (err) return err;
2062 err = slotFloatVal(slots+1, &green);
2063 if (err) return err;
2064 err = slotFloatVal(slots+2, &blue);
2065 if (err) return err;
2066 err = slotFloatVal(slots+3, &alpha);
2067 if (err) return err;
2069 rgbcolor->red = (unsigned short)(red * 65535.);
2070 rgbcolor->green = (unsigned short)(green * 65535.);
2071 rgbcolor->blue = (unsigned short)(blue * 65535.);
2072 //rgbcolor->alpha = (unsigned short)(alpha * 65535.);
2074 return errNone;
2077 int addFontColorAttribute(NSMutableDictionary *dict, PyrSlot *colorSlot);
2078 int addFontColorAttribute(NSMutableDictionary *dict, PyrSlot *colorSlot)
2080 if (IsNil(colorSlot)) return errNone; // use default color
2081 SCColor rgb;
2082 int err = slotColorVal(colorSlot, &rgb);
2083 if (err) return err;
2084 NSColor *color = [NSColor colorWithCalibratedRed: rgb.red
2085 green: rgb.green
2086 blue: rgb.blue
2087 alpha: rgb.alpha];
2088 [dict setObject: color forKey: NSForegroundColorAttributeName ];
2090 return errNone;
2093 int prFont_AvailableFonts(struct VMGlobals *g, int numArgsPushed);
2094 int prFont_AvailableFonts(struct VMGlobals *g, int numArgsPushed)
2096 if (!g->canCallOS) return errCantCallOS;
2098 PyrSlot *a = g->sp;
2100 NSFontManager *fontManager = [NSFontManager sharedFontManager];
2101 NSArray *fonts = [fontManager availableFonts];
2103 int size = [fonts count];
2104 PyrObject* array = newPyrArray(g->gc, size, 0, true);
2105 SetObject(a, array);
2107 for (int i=0; i<size; ++i) {
2108 NSString *name = [fonts objectAtIndex: i];
2109 //if (!name) continue;
2110 PyrString *string = newPyrString(g->gc, [name UTF8String], 0, true);
2111 SetObject(array->slots + array->size, string);
2112 array->size++;
2113 g->gc->GCWrite(array, string);
2116 return errNone;
2119 int prFont_SetAntiAliasing(struct VMGlobals *g, int numArgsPushed);
2120 int prFont_SetAntiAliasing(struct VMGlobals *g, int numArgsPushed)
2122 if (!g->canCallOS) return errCantCallOS;
2124 //PyrSlot *a = g->sp - 1;
2125 PyrSlot *b = g->sp;
2127 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2128 if (!gctx) return errFailed;
2129 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2130 CGContextSetShouldAntialias(cgc, IsTrue(b));
2132 return errNone;
2135 int prFont_SetSmoothing(struct VMGlobals *g, int numArgsPushed);
2136 int prFont_SetSmoothing(struct VMGlobals *g, int numArgsPushed)
2138 if (!g->canCallOS) return errCantCallOS;
2140 //PyrSlot *a = g->sp - 1;
2141 PyrSlot *b = g->sp;
2143 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2144 if (!gctx) return errFailed;
2145 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2146 CGContextSetShouldSmoothFonts(cgc, IsTrue(b));
2148 return errNone;
2151 int prPen_SetSmoothing(struct VMGlobals *g, int numArgsPushed);
2152 int prPen_SetSmoothing(struct VMGlobals *g, int numArgsPushed)
2154 if (!g->canCallOS) return errCantCallOS;
2156 //PyrSlot *a = g->sp - 1;
2157 PyrSlot *b = g->sp;
2159 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2160 if (!gctx) return errFailed;
2161 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2162 CGContextSetShouldAntialias(cgc, IsTrue(b));
2164 return errNone;
2168 int prString_GetBounds(struct VMGlobals *g, int numArgsPushed);
2169 int prString_GetBounds(struct VMGlobals *g, int numArgsPushed)
2171 if (!g->canCallOS) return errCantCallOS;
2173 PyrSlot *a = g->sp - 2;
2174 PyrSlot *b = g->sp - 1;
2175 PyrSlot *c = g->sp;
2177 if (!isKindOfSlot(b, s_rect->u.classobj)) return errWrongType;
2179 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
2180 int err = addFontAttribute(dict, c);
2181 if (err) return err;
2183 PyrString* string = slotRawString(a);
2184 NSString *nsstring = [NSString stringWithCString:string->s encoding:[NSString defaultCStringEncoding]];
2185 nsstring = [nsstring substringToIndex:string->size];
2186 if (!nsstring) return errFailed;
2187 NSSize size = [nsstring sizeWithAttributes: dict];
2189 PyrSlot* rectSlots = slotRawObject(b)->slots;
2190 SetFloat(rectSlots+0, 0.);
2191 SetFloat(rectSlots+1, 0.);
2192 SetFloat(rectSlots+2, size.width);
2193 SetFloat(rectSlots+3, size.height);
2194 slotCopy(a, b);
2196 return errNone;
2199 int prString_DrawAtPoint(struct VMGlobals *g, int numArgsPushed);
2200 int prString_DrawAtPoint(struct VMGlobals *g, int numArgsPushed)
2202 if (!g->canCallOS) return errCantCallOS;
2204 PyrSlot *a = g->sp - 3;
2205 PyrSlot *b = g->sp - 2;
2206 PyrSlot *c = g->sp - 1;
2207 PyrSlot *d = g->sp;
2209 if (!(isKindOfSlot(b, s_point->u.classobj))) return errWrongType;
2211 NSPoint p;
2212 int err = slotGetPoint(b, &p);
2213 if (err) return err;
2215 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
2216 err = addFontAttribute(dict, c);
2217 if (err) return err;
2218 err = addFontColorAttribute(dict, d);
2219 if (err) return err;
2221 PyrString* string = slotRawString(a);
2222 // NSString *nsstring = [NSString stringWithCString: string->s length: string->size];
2223 NSString *nsstring = [NSString stringWithCString:string->s encoding:[NSString defaultCStringEncoding]];
2224 nsstring = [nsstring substringToIndex:string->size];
2225 if (!nsstring) return errFailed;
2227 [nsstring drawAtPoint: p withAttributes: dict];
2229 return errNone;
2232 int prString_DrawInRect(struct VMGlobals *g, int numArgsPushed);
2233 int prString_DrawInRect(struct VMGlobals *g, int numArgsPushed)
2235 if (!g->canCallOS) return errCantCallOS;
2237 PyrSlot *a = g->sp - 3;
2238 PyrSlot *b = g->sp - 2;
2239 PyrSlot *c = g->sp - 1;
2240 PyrSlot *d = g->sp;
2242 if (!(isKindOfSlot(b, s_rect->u.classobj))) return errWrongType;
2244 NSRect rect;
2245 int err = slotGetNSRect(b, &rect);
2246 if (err) return err;
2248 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
2249 err = addFontAttribute(dict, c);
2250 if (err) return err;
2251 err = addFontColorAttribute(dict, d);
2252 if (err) return err;
2254 PyrString* string = slotRawString(a);
2255 //NSString *nsstring = [NSString stringWithCString: string->s length: string->size];
2256 NSString *nsstring = [NSString stringWithCString:string->s encoding:[NSString defaultCStringEncoding]];
2257 if (!nsstring) return errFailed;
2258 nsstring = [nsstring substringToIndex:string->size];
2260 [nsstring drawInRect: rect withAttributes: dict];
2262 return errNone;
2265 int prColor_SetStroke(struct VMGlobals *g, int numArgsPushed);
2266 int prColor_SetStroke(struct VMGlobals *g, int numArgsPushed)
2268 if (!g->canCallOS) return errCantCallOS;
2270 PyrSlot *a = g->sp;
2271 PyrSlot *slots = slotRawObject(a)->slots;
2272 int err;
2273 float red, green, blue, alpha;
2275 err = slotFloatVal(slots+0, &red);
2276 if (err) return err;
2277 err = slotFloatVal(slots+1, &green);
2278 if (err) return err;
2279 err = slotFloatVal(slots+2, &blue);
2280 if (err) return err;
2281 err = slotFloatVal(slots+3, &alpha);
2282 if (err) return err;
2284 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2285 if (!gctx) return errFailed;
2286 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2287 CGContextSetRGBStrokeColor(cgc, red, green, blue, alpha);
2288 // CGContextSetRGBFillColor(cgc, red, green, blue, alpha);
2289 /* NSColor *color = [NSColor colorWithCalibratedRed: red
2290 green: green
2291 blue: blue
2292 alpha: alpha];
2293 if (color) {
2294 [color set];
2297 return errNone;
2300 int prColor_SetFill(struct VMGlobals *g, int numArgsPushed);
2301 int prColor_SetFill(struct VMGlobals *g, int numArgsPushed)
2303 if (!g->canCallOS) return errCantCallOS;
2305 PyrSlot *a = g->sp;
2306 PyrSlot *slots = slotRawObject(a)->slots;
2307 int err;
2308 float red, green, blue, alpha;
2310 err = slotFloatVal(slots+0, &red);
2311 if (err) return err;
2312 err = slotFloatVal(slots+1, &green);
2313 if (err) return err;
2314 err = slotFloatVal(slots+2, &blue);
2315 if (err) return err;
2316 err = slotFloatVal(slots+3, &alpha);
2317 if (err) return err;
2319 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2320 if (!gctx) return errFailed;
2321 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2322 CGContextSetRGBFillColor(cgc, red, green, blue, alpha);
2323 // NSColor *color = [NSColor colorWithCalibratedRed: red
2324 // green: green
2325 // blue: blue
2326 // alpha: alpha];
2327 // if (color) {
2328 // [color setFill];
2329 // }
2331 return errNone;
2334 int prPen_Push(struct VMGlobals *g, int numArgsPushed);
2335 int prPen_Push(struct VMGlobals *g, int numArgsPushed)
2337 if (!g->canCallOS) return errCantCallOS;
2339 [NSGraphicsContext saveGraphicsState];
2340 return errNone;
2343 int prPen_Pop(struct VMGlobals *g, int numArgsPushed);
2344 int prPen_Pop(struct VMGlobals *g, int numArgsPushed)
2346 if (!g->canCallOS) return errCantCallOS;
2348 [NSGraphicsContext restoreGraphicsState];
2349 return errNone;
2352 int prPen_AddRect(struct VMGlobals *g, int numArgsPushed);
2353 int prPen_AddRect(struct VMGlobals *g, int numArgsPushed)
2355 if (!g->canCallOS) return errCantCallOS;
2357 //PyrSlot *a = g->sp - 1;
2358 PyrSlot *b = g->sp;
2360 if (!(isKindOfSlot(b, s_rect->u.classobj))) return errWrongType;
2362 CGRect rect;
2363 int err = slotGetCGRect(b, &rect);
2364 if (err) return err;
2366 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2367 if (!gctx) return errFailed;
2368 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2370 CGContextAddRect(cgc, rect);
2371 return errNone;
2374 int prPen_StrokeRect(struct VMGlobals *g, int numArgsPushed);
2375 int prPen_StrokeRect(struct VMGlobals *g, int numArgsPushed)
2377 if (!g->canCallOS) return errCantCallOS;
2379 //PyrSlot *a = g->sp - 1;
2380 PyrSlot *b = g->sp;
2382 if (!(isKindOfSlot(b, s_rect->u.classobj))) return errWrongType;
2384 CGRect rect;
2385 int err = slotGetCGRect(b, &rect);
2386 if (err)
2387 return err;
2389 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2390 if (!gctx)
2391 return errFailed;
2393 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2394 CGContextBeginPath(cgc);
2395 CGContextAddRect(cgc, rect);
2396 CGContextDrawPath(cgc, kCGPathStroke);
2398 return errNone;
2401 int prPen_FillRect(struct VMGlobals *g, int numArgsPushed);
2402 int prPen_FillRect(struct VMGlobals *g, int numArgsPushed)
2404 if (!g->canCallOS) return errCantCallOS;
2406 //PyrSlot *a = g->sp - 1;
2407 PyrSlot *b = g->sp;
2409 if (!(isKindOfSlot(b, s_rect->u.classobj))) return errWrongType;
2411 CGRect rect;
2412 int err = slotGetCGRect(b, &rect);
2413 if (err)
2414 return err;
2416 CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
2417 if( !ctx )
2418 return errFailed;
2420 CGContextFillRect( ctx, rect );
2422 return errNone;
2425 int prPen_StrokeOval(struct VMGlobals *g, int numArgsPushed);
2426 int prPen_StrokeOval(struct VMGlobals *g, int numArgsPushed)
2428 if (!g->canCallOS) return errCantCallOS;
2430 CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
2431 if( !ctx )
2432 return errFailed;
2434 //PyrSlot *a = g->sp - 1;
2435 PyrSlot *b = g->sp;
2437 if (!(isKindOfSlot(b, s_rect->u.classobj))) return errWrongType;
2439 CGRect rect;
2440 int err = slotGetCGRect(b, &rect);
2441 if (err)
2442 return err;
2444 CGContextAddEllipseInRect( ctx, rect );
2445 CGContextStrokePath( ctx );
2447 return errNone;
2450 int prPen_FillOval(struct VMGlobals *g, int numArgsPushed);
2451 int prPen_FillOval(struct VMGlobals *g, int numArgsPushed)
2453 if (!g->canCallOS) return errCantCallOS;
2455 CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
2456 if( !ctx )
2457 return errFailed;
2459 //PyrSlot *a = g->sp - 1;
2460 PyrSlot *b = g->sp;
2462 if (!(isKindOfSlot(b, s_rect->u.classobj)))
2463 return errWrongType;
2465 NSRect rect;
2466 int err = slotGetNSRect(b, &rect);
2467 if (err) return err;
2469 CGContextAddEllipseInRect( ctx, *(CGRect*)&rect );
2470 CGContextFillPath( ctx );
2472 return errNone;
2475 int prPen_StrokeLine(struct VMGlobals *g, int numArgsPushed);
2476 int prPen_StrokeLine(struct VMGlobals *g, int numArgsPushed)
2478 if (!g->canCallOS) return errCantCallOS;
2480 CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
2481 if( !ctx )
2482 return errFailed;
2484 //PyrSlot *a = g->sp - 2;
2485 PyrSlot *b = g->sp - 1;
2486 PyrSlot *c = g->sp;
2488 if (!(isKindOfSlot(b, s_point->u.classobj))) return errWrongType;
2489 if (!(isKindOfSlot(c, s_point->u.classobj))) return errWrongType;
2491 NSPoint p1, p2;
2492 int err = slotGetPoint(b, &p1);
2493 if (err) return err;
2494 err = slotGetPoint(c, &p2);
2495 if (err) return err;
2497 CGContextMoveToPoint( ctx, p1.x, p2.x );
2498 CGContextAddLineToPoint( ctx, p1.x, p2.x );
2499 CGContextStrokePath( ctx );
2500 return errNone;
2503 int prPen_MoveTo(struct VMGlobals *g, int numArgsPushed);
2504 int prPen_MoveTo(struct VMGlobals *g, int numArgsPushed)
2506 if (!g->canCallOS) return errCantCallOS;
2508 //PyrSlot *a = g->sp - 1;
2509 PyrSlot *b = g->sp;
2511 if (!(isKindOfSlot(b, s_point->u.classobj))) return errWrongType;
2513 NSPoint p1;
2514 int err = slotGetPoint(b, &p1);
2515 if (err) return err;
2517 CGContextRef cgc = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
2518 if( !cgc )
2519 return errFailed;
2521 CGContextMoveToPoint(cgc, p1.x, p1.y);
2522 return errNone;
2525 int prPen_LineTo(struct VMGlobals *g, int numArgsPushed);
2526 int prPen_LineTo(struct VMGlobals *g, int numArgsPushed)
2528 if (!g->canCallOS) return errCantCallOS;
2530 //PyrSlot *a = g->sp - 1;
2531 PyrSlot *b = g->sp;
2533 if (!(isKindOfSlot(b, s_point->u.classobj))) return errWrongType;
2535 NSPoint p1;
2536 int err = slotGetPoint(b, &p1);
2537 if (err) return err;
2539 CGContextRef cgc = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
2540 if( !cgc )
2541 return errFailed;
2543 CGContextAddLineToPoint(cgc, p1.x, p1.y);
2545 return errNone;
2548 int prPen_AddArc(struct VMGlobals *g, int numArgsPushed);
2549 int prPen_AddArc(struct VMGlobals *g, int numArgsPushed)
2551 if (!g->canCallOS) return errCantCallOS;
2553 //PyrSlot *a = g->sp - 4;
2554 PyrSlot *b = g->sp - 3;
2555 PyrSlot *c = g->sp - 2; // radius
2556 PyrSlot *d = g->sp - 1; // start angle
2557 PyrSlot *e = g->sp; // angle
2559 if (!(isKindOfSlot(b, s_point->u.classobj))) return errWrongType;
2561 NSPoint p1;
2562 int err = slotGetPoint(b, &p1);
2563 if (err) return err;
2565 float radius;
2566 err = slotFloatVal(c, &radius);
2567 if (err) return err;
2569 float startAngle;
2570 err = slotFloatVal(d, &startAngle);
2571 if (err) return err;
2573 float angle;
2574 err = slotFloatVal(e, &angle);
2575 if (err) return err;
2577 int clockwise = angle > 0. ? 0 : 1;
2578 float endAngle = startAngle + angle;
2580 NSPoint p2;
2581 p2.x = p1.x + radius * cos(startAngle);
2582 p2.y = p1.y + radius * sin(startAngle);
2584 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2585 if (!gctx) return errFailed;
2586 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2587 //post("start %g %g end %g %g arc %g %g\n", startAngle, startAngle * 180./3.14156, endAngle, endAngle * 180./3.14156, angle, angle * 180./3.14156);
2588 CGContextMoveToPoint(cgc, p2.x, p2.y);
2589 CGContextAddArc(cgc, p1.x, p1.y, radius, startAngle, endAngle, clockwise);
2591 return errNone;
2594 int prPen_AddWedge(struct VMGlobals *g, int numArgsPushed);
2595 int prPen_AddWedge(struct VMGlobals *g, int numArgsPushed)
2597 if (!g->canCallOS) return errCantCallOS;
2599 //PyrSlot *a = g->sp - 4;
2600 PyrSlot *b = g->sp - 3;
2601 PyrSlot *c = g->sp - 2; // radius
2602 PyrSlot *d = g->sp - 1; // start angle
2603 PyrSlot *e = g->sp; // angle
2605 if (!(isKindOfSlot(b, s_point->u.classobj))) return errWrongType;
2607 NSPoint p1;
2608 int err = slotGetPoint(b, &p1);
2609 if (err) return err;
2611 float radius;
2612 err = slotFloatVal(c, &radius);
2613 if (err) return err;
2615 float startAngle;
2616 err = slotFloatVal(d, &startAngle);
2617 if (err) return err;
2619 float angle;
2620 err = slotFloatVal(e, &angle);
2621 if (err) return err;
2623 int clockwise = angle > 0. ? 0 : 1;
2624 float endAngle = startAngle + angle;
2626 NSPoint p2;
2627 p2.x = p1.x + radius * cos(startAngle);
2628 p2.y = p1.y + radius * sin(startAngle);
2630 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2631 if (!gctx) return errFailed;
2632 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2633 CGContextMoveToPoint(cgc, p1.x, p1.y);
2634 CGContextAddLineToPoint(cgc, p2.x, p2.y);
2635 CGContextAddArc(cgc, p1.x, p1.y, radius, startAngle, endAngle, clockwise);
2636 CGContextAddLineToPoint(cgc, p1.x, p1.y);
2638 return errNone;
2641 int prPen_AddAnnularWedge(struct VMGlobals *g, int numArgsPushed);
2642 int prPen_AddAnnularWedge(struct VMGlobals *g, int numArgsPushed)
2644 if (!g->canCallOS) return errCantCallOS;
2646 //PyrSlot *a = g->sp - 5;
2647 PyrSlot *b = g->sp - 4;
2648 PyrSlot *c = g->sp - 3; // innerRadius
2649 PyrSlot *d = g->sp - 2; // outerRadius
2650 PyrSlot *e = g->sp - 1; // start angle
2651 PyrSlot *f = g->sp; // angle
2653 if (!(isKindOfSlot(b, s_point->u.classobj))) return errWrongType;
2655 NSPoint p1;
2656 int err = slotGetPoint(b, &p1);
2657 if (err) return err;
2659 float innerRadius;
2660 err = slotFloatVal(c, &innerRadius);
2661 if (err) return err;
2663 float outerRadius;
2664 err = slotFloatVal(d, &outerRadius);
2665 if (err) return err;
2667 float startAngle;
2668 err = slotFloatVal(e, &startAngle);
2669 if (err) return err;
2671 float angle;
2672 err = slotFloatVal(f, &angle);
2673 if (err) return err;
2675 int clockwise = angle > 0. ? 0 : 1;
2676 float endAngle = startAngle + angle;
2678 NSPoint p2, p3, p4;
2679 p2.x = p1.x + innerRadius * cos(startAngle);
2680 p2.y = p1.y + innerRadius * sin(startAngle);
2681 p3.x = p1.x + outerRadius * cos(startAngle);
2682 p3.y = p1.y + outerRadius * sin(startAngle);
2683 p4.x = p1.x + innerRadius * cos(endAngle);
2684 p4.y = p1.y + innerRadius * sin(endAngle);
2686 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2687 if (!gctx) return errFailed;
2688 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2689 CGContextMoveToPoint(cgc, p2.x, p2.y);
2690 CGContextAddLineToPoint(cgc, p3.x, p3.y);
2691 CGContextAddArc(cgc, p1.x, p1.y, outerRadius, startAngle, endAngle, clockwise);
2692 CGContextAddLineToPoint(cgc, p4.x, p4.y);
2693 CGContextAddArc(cgc, p1.x, p1.y, innerRadius, endAngle, startAngle, 1-clockwise);
2695 return errNone;
2698 int prPen_BeginPath(struct VMGlobals *g, int numArgsPushed);
2699 int prPen_BeginPath(struct VMGlobals *g, int numArgsPushed)
2701 if (!g->canCallOS) return errCantCallOS;
2703 //PyrSlot *a = g->sp;
2705 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2706 if (!gctx) return errFailed;
2707 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2708 CGContextBeginPath(cgc);
2710 return errNone;
2713 int prPen_ClosePath(struct VMGlobals *g, int numArgsPushed);
2714 int prPen_ClosePath(struct VMGlobals *g, int numArgsPushed)
2716 if (!g->canCallOS) return errCantCallOS;
2718 //PyrSlot *a = g->sp;
2720 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2721 if (!gctx) return errFailed;
2722 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2723 CGContextClosePath(cgc);
2725 return errNone;
2728 int prPen_StrokePath(struct VMGlobals *g, int numArgsPushed);
2729 int prPen_StrokePath(struct VMGlobals *g, int numArgsPushed)
2731 if (!g->canCallOS) return errCantCallOS;
2733 //PyrSlot *a = g->sp;
2735 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2736 if (!gctx) return errFailed;
2737 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2738 CGContextDrawPath(cgc, kCGPathStroke);
2740 return errNone;
2743 int prPen_FillPath(struct VMGlobals *g, int numArgsPushed);
2744 int prPen_FillPath(struct VMGlobals *g, int numArgsPushed)
2746 if (!g->canCallOS) return errCantCallOS;
2748 //PyrSlot *a = g->sp;
2750 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2751 if (!gctx) return errFailed;
2752 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2753 CGContextFillPath(cgc);
2755 return errNone;
2759 int prPen_ClipPath(struct VMGlobals *g, int numArgsPushed);
2760 int prPen_ClipPath(struct VMGlobals *g, int numArgsPushed)
2762 if (!g->canCallOS) return errCantCallOS;
2764 //PyrSlot *a = g->sp;
2766 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2767 if (!gctx) return errFailed;
2768 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2769 CGContextClip(cgc);
2771 return errNone;
2776 int prPen_Translate(struct VMGlobals *g, int numArgsPushed);
2777 int prPen_Translate(struct VMGlobals *g, int numArgsPushed)
2779 if (!g->canCallOS) return errCantCallOS;
2781 //PyrSlot *a = g->sp - 2;
2782 PyrSlot *b = g->sp - 1;
2783 PyrSlot *c = g->sp;
2785 float x, y;
2786 int err = slotFloatVal(b, &x);
2787 if (err) return err;
2788 err = slotFloatVal(c, &y);
2789 if (err) return err;
2791 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2792 if (!gctx) return errFailed;
2793 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2794 CGContextTranslateCTM(cgc, x, y);
2796 return errNone;
2799 int prPen_Scale(struct VMGlobals *g, int numArgsPushed);
2800 int prPen_Scale(struct VMGlobals *g, int numArgsPushed)
2802 if (!g->canCallOS) return errCantCallOS;
2804 //PyrSlot *a = g->sp - 2;
2805 PyrSlot *b = g->sp - 1;
2806 PyrSlot *c = g->sp;
2808 float x, y;
2809 int err = slotFloatVal(b, &x);
2810 if (err) return err;
2811 err = slotFloatVal(c, &y);
2812 if (err) return err;
2814 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2815 if (!gctx) return errFailed;
2816 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2817 CGContextScaleCTM(cgc, x, y);
2819 return errNone;
2822 int prPen_Rotate(struct VMGlobals *g, int numArgsPushed);
2823 int prPen_Rotate(struct VMGlobals *g, int numArgsPushed)
2825 if (!g->canCallOS) return errCantCallOS;
2827 //PyrSlot *a = g->sp - 3;
2828 PyrSlot *b = g->sp - 2;
2829 PyrSlot *c = g->sp - 1;
2830 PyrSlot *d = g->sp;
2832 float angle, x, y;
2833 int err = slotFloatVal(b, &angle);
2834 if (err) return err;
2835 err = slotFloatVal(c, &x);
2836 if (err) return err;
2837 err = slotFloatVal(d, &y);
2838 if (err) return err;
2840 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2841 if (!gctx) return errFailed;
2842 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2843 if (x == 0. && y == 0.) {
2844 CGContextRotateCTM(cgc, angle);
2845 } else {
2846 CGContextTranslateCTM(cgc, x, y);
2847 CGContextRotateCTM(cgc, angle);
2848 CGContextTranslateCTM(cgc, -x, -y);
2850 return errNone;
2853 int prPen_SetWidth(struct VMGlobals *g, int numArgsPushed);
2854 int prPen_SetWidth(struct VMGlobals *g, int numArgsPushed)
2856 if (!g->canCallOS) return errCantCallOS;
2858 //PyrSlot *a = g->sp - 1;
2859 PyrSlot *b = g->sp;
2861 float width;
2862 int err = slotFloatVal(b, &width);
2863 if (err) return err;
2865 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2866 if (!gctx) return errFailed;
2867 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2868 CGContextSetLineWidth(cgc, width);
2869 return errNone;
2872 int prPen_SetMatrix(struct VMGlobals *g, int numArgsPushed);
2873 int prPen_SetMatrix(struct VMGlobals *g, int numArgsPushed)
2875 if (!g->canCallOS) return errCantCallOS;
2877 //PyrSlot *a = g->sp - 1;
2878 PyrSlot *b = g->sp;
2880 if (!isKindOfSlot(b, class_arrayed_collection)) return errWrongType;
2882 if (slotRawObject(b)->size < 6) return errFailed;
2883 PyrObject *mobj = slotRawObject(b);
2885 CGAffineTransform afx;
2887 int err = getIndexedVal(mobj, 0, &afx.a);
2888 if (err) return err;
2889 err = getIndexedVal(mobj, 1, &afx.b);
2890 if (err) return err;
2891 err = getIndexedVal(mobj, 2, &afx.c);
2892 if (err) return err;
2893 err = getIndexedVal(mobj, 3, &afx.d);
2894 if (err) return err;
2895 err = getIndexedVal(mobj, 4, &afx.tx);
2896 if (err) return err;
2897 err = getIndexedVal(mobj, 5, &afx.ty);
2898 if (err) return err;
2900 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2901 if (!gctx) return errFailed;
2902 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2903 CGContextConcatCTM(cgc, afx);
2905 return errNone;
2908 int prPen_Skew(struct VMGlobals *g, int numArgsPushed);
2909 int prPen_Skew(struct VMGlobals *g, int numArgsPushed)
2911 if (!g->canCallOS) return errCantCallOS;
2913 //PyrSlot *a = g->sp - 1;
2914 PyrSlot *b = g->sp - 1;
2915 PyrSlot *c = g->sp;
2917 float x;
2918 int err = slotFloatVal(b, &x);
2919 if (err) return err;
2921 float y;
2922 err = slotFloatVal(c, &y);
2923 if (err) return err;
2925 CGAffineTransform afx;
2926 afx.a = 1.0;
2927 afx.b = y;
2928 afx.c = x;
2929 afx.d = 1.0;
2930 afx.tx = 0.0;
2931 afx.ty = 0.0;
2933 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2934 if (!gctx) return errFailed;
2935 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2936 CGContextConcatCTM(cgc, afx);
2938 return errNone;
2942 int prPen_SetBlend(struct VMGlobals *g, int numArgsPushed);
2943 int prPen_SetBlend(struct VMGlobals *g, int numArgsPushed)
2945 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4
2946 if (!g->canCallOS) return errCantCallOS;
2947 PyrSlot *c = g->sp; // blendmode
2948 int mode = 0, err = errFailed;
2950 err = slotIntVal(c, &mode);
2951 if( err != errNone )
2952 return err;
2954 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2955 if (!gctx)
2956 return errFailed;
2958 CGContextRef cgc = (CGContextRef)[gctx graphicsPort];
2959 CGContextSetBlendMode(cgc, (CGBlendMode)mode);
2960 #endif
2961 return errNone;
2964 int prPen_SetShadow(struct VMGlobals *g, int numArgsPushed);
2965 int prPen_SetShadow(struct VMGlobals *g, int numArgsPushed)
2967 if (!g->canCallOS) return errCantCallOS;
2969 int err;
2971 PyrSlot *a = g->sp - 2; // offset
2972 PyrSlot *b = g->sp - 1; // blur
2973 PyrSlot *c = g->sp; // color
2975 NSPoint offset;
2976 float blur;
2977 SCColor scColor;
2979 err = slotColorVal(c, &scColor);
2980 if( err != errNone )
2981 return err;
2983 err = slotGetPoint(a, &offset);
2984 if( err != errNone )
2985 return err;
2987 err = slotFloatVal(b, &blur);
2988 if( err != errNone )
2989 return err;
2991 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
2992 if (!gctx)
2993 return errFailed;
2995 CGContextRef _ctx = (CGContextRef)[gctx graphicsPort];
2996 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
2997 ColorData colors[4];
2998 colors[0] = scColor.red;
2999 colors[1] = scColor.green;
3000 colors[2] = scColor.blue;
3001 colors[3] = scColor.alpha;
3002 CGColorRef col = CGColorCreate(colorSpace, colors);
3003 if(!col){
3004 CGColorSpaceRelease(colorSpace);
3005 return errFailed;
3008 CGContextSetShadowWithColor(_ctx, (CGSize){offset.x, offset.y}, blur, col);
3010 CGColorSpaceRelease(colorSpace);
3011 CGColorRelease(col);
3013 return errNone;
3016 int prPen_AddOval(struct VMGlobals *g, int numArgsPushed);
3017 int prPen_AddOval(struct VMGlobals *g, int numArgsPushed)
3019 if (!g->canCallOS) return errCantCallOS;
3021 //PyrSlot *a = g->sp - 1;
3022 PyrSlot *b = g->sp;
3024 if (!(isKindOfSlot(b, s_rect->u.classobj))) return errWrongType;
3026 CGRect rect;
3027 int err = slotGetCGRect(b, &rect);
3028 if (err) return err;
3030 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
3031 if (!gctx)
3032 return errFailed;
3034 CGContextAddEllipseInRect((CGContextRef)[gctx graphicsPort], rect);
3036 return errNone;
3039 int prPen_AddBezierQuadCurve(struct VMGlobals *g, int numArgsPushed);
3040 int prPen_AddBezierQuadCurve(struct VMGlobals *g, int numArgsPushed)
3042 if (!g->canCallOS) return errCantCallOS;
3044 int err;
3045 NSPoint points[2];
3047 PyrSlot *a = g->sp - 1; // point
3048 PyrSlot *b = g->sp; // control point 1
3050 if (!(isKindOfSlot(b, s_point->u.classobj))) return errWrongType;
3052 err = slotGetPoint(a, &points[0]);
3053 if( err != errNone )
3054 return err;
3056 err = slotGetPoint(b, &points[1]);
3057 if( err != errNone )
3058 return err;
3060 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
3061 if (!gctx)
3062 return errFailed;
3064 CGContextAddQuadCurveToPoint((CGContextRef)[gctx graphicsPort],
3065 points[1].x, points[1].y,
3066 points[0].x, points[0].y
3069 return errNone;
3072 int prPen_AddBezierCubicCurve(struct VMGlobals *g, int numArgsPushed);
3073 int prPen_AddBezierCubicCurve(struct VMGlobals *g, int numArgsPushed)
3075 if (!g->canCallOS) return errCantCallOS;
3077 int err;
3078 NSPoint points[3];
3080 PyrSlot *a = g->sp - 2; // point
3081 PyrSlot *b = g->sp - 1; // control point 1
3082 PyrSlot *c = g->sp; // control point 2
3084 if (!(isKindOfSlot(b, s_point->u.classobj))) return errWrongType;
3085 if (!(isKindOfSlot(c, s_point->u.classobj))) return errWrongType;
3087 err = slotGetPoint(a, &points[0]);
3088 if( err != errNone )
3089 return err;
3091 err = slotGetPoint(b, &points[1]);
3092 if( err != errNone )
3093 return err;
3095 err = slotGetPoint(c, &points[2]);
3096 if( err != errNone )
3097 return err;
3099 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
3100 if ( !gctx )
3101 return errFailed;
3103 CGContextAddCurveToPoint((CGContextRef)[gctx graphicsPort],
3104 points[1].x, points[1].y,
3105 points[2].x, points[2].y,
3106 points[0].x, points[0].y
3109 return errNone;
3112 int prPen_AddArcToPoint(struct VMGlobals *g, int numArgsPushed);
3113 int prPen_AddArcToPoint(struct VMGlobals *g, int numArgsPushed)
3115 if (!g->canCallOS) return errCantCallOS;
3117 PyrSlot *a = g->sp - 2; // point1
3118 PyrSlot *b = g->sp - 1; // point2
3119 PyrSlot *c = g->sp; // radius
3121 if (!(isKindOfSlot(b, s_point->u.classobj))) return errWrongType;
3123 NSPoint p1;
3124 NSPoint p2;
3126 int err = slotGetPoint(a, &p1);
3127 if (err) return err;
3129 err = slotGetPoint(b, &p2);
3130 if (err) return err;
3132 float radius;
3133 err = slotFloatVal(c, &radius);
3134 if (err) return err;
3136 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
3137 if ( !gctx )
3138 return errFailed;
3140 CGContextAddArcToPoint((CGContextRef)[gctx graphicsPort], p1.x, p1.y, p2.x, p2.y, radius);
3142 return errNone;
3145 int prPen_ClearRect(struct VMGlobals *g, int numArgsPushed);
3146 int prPen_ClearRect(struct VMGlobals *g, int numArgsPushed)
3148 if (!g->canCallOS) return errCantCallOS;
3150 PyrSlot *b = g->sp;
3152 if (!(isKindOfSlot(b, s_rect->u.classobj))) return errWrongType;
3154 CGRect rect;
3155 int err = slotGetCGRect(b, &rect);
3156 if (err) return err;
3159 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
3160 if ( !gctx )
3161 return errFailed;
3163 CGContextClearRect((CGContextRef)[gctx graphicsPort], rect);
3165 return errNone;
3168 int prPen_DrawPath(struct VMGlobals *g, int numArgsPushed);
3169 int prPen_DrawPath(struct VMGlobals *g, int numArgsPushed)
3171 if (!g->canCallOS) return errCantCallOS;
3173 PyrSlot *b = g->sp;
3174 int err, mode=0;
3176 if( (err = slotIntVal(b, &mode)) != noErr )
3177 return err;
3179 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
3180 if ( !gctx )
3181 return errFailed;
3183 //kCGPathFill=0, kCGPathEOFill=1, kCGPathStroke=2, kCGPathFillStroke=3, or kCGPathEOFillStroke=4
3184 CGContextDrawPath(
3185 (CGContextRef)[gctx graphicsPort],
3186 (CGPathDrawingMode)sc_clip( mode, kCGPathFill, kCGPathEOFillStroke )
3188 return errNone;
3191 int prPen_BeginTransparencyLayer(struct VMGlobals *g, int numArgsPushed);
3192 int prPen_BeginTransparencyLayer(struct VMGlobals *g, int numArgsPushed)
3195 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_3
3197 if (!g->canCallOS) return errCantCallOS;
3198 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
3199 if (!gctx)
3200 return errFailed;
3203 #if 1/*MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5*/
3205 CGContextBeginTransparencyLayer((CGContextRef)[gctx graphicsPort], NULL);
3207 #else /*MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5*/
3209 PyrSlot *b = g->sp;
3210 if (!(isKindOfSlot(b, s_rect->u.classobj)))
3211 return errWrongType;
3213 CGRect rect;
3214 int err = slotGetCGRect(b, &rect);
3215 if (err)
3216 return err;
3218 if(rect.size.width == 0 || rect.size.height == 0) // NULL in either side should take all current clipped region
3219 CGContextBeginTransparencyLayer((CGContextRef)[gctx graphicsPort], NULL);
3220 else
3221 CGContextBeginTransparencyLayerWithRect((CGContextRef)[gctx graphicsPort], rect, NULL);
3223 #endif
3225 #endif
3226 return errNone;
3229 int prPen_EndTransparencyLayer(struct VMGlobals *g, int numArgsPushed);
3230 int prPen_EndTransparencyLayer(struct VMGlobals *g, int numArgsPushed)
3233 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_3
3235 if (!g->canCallOS)
3236 return errCantCallOS;
3238 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
3239 if (!gctx)
3240 return errFailed;
3242 CGContextEndTransparencyLayer((CGContextRef)[gctx graphicsPort]);
3244 #endif
3246 return errNone;
3249 int prPen_LineJoin(struct VMGlobals *g, int numArgsPushed);
3250 int prPen_LineJoin(struct VMGlobals *g, int numArgsPushed)
3252 int mode=0, err;
3254 if (!g->canCallOS)
3255 return errCantCallOS;
3257 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
3258 if (!gctx)
3259 return errFailed;
3261 PyrSlot *b = g->sp; // mode
3262 if((err = slotIntVal(b, &mode)) != noErr)
3263 return err;
3265 //kCGLineJoinMiter=0, kCGLineJoinRound=1,kCGLineJoinBevel=2
3266 CGContextSetLineJoin(
3267 (CGContextRef)[gctx graphicsPort],
3268 (CGLineJoin)sc_clip( mode, kCGLineJoinMiter, kCGLineJoinBevel )
3271 return errNone;
3274 int prPen_LineDash(struct VMGlobals *g, int numArgsPushed);
3275 int prPen_LineDash(struct VMGlobals *g, int numArgsPushed)
3277 if ( !g->canCallOS )
3278 return errCantCallOS;
3280 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
3281 if ( !gctx )
3282 return errFailed;
3284 PyrSlot *a = g->sp; // array of floats
3285 PyrObject* pyrarray = slotRawObject(a);
3286 PyrClass* pyrclass = pyrarray->classptr;
3288 if( pyrclass == class_floatarray ) // float array only
3290 ColorData *colors = (ColorData *) malloc(pyrarray->size*sizeof(ColorData));
3291 int i;
3292 float *src = ((PyrFloatArray *)pyrarray)->f;
3293 for (i=0; i<pyrarray->size; i++)
3295 colors[i] = (ColorData) *(src++);
3297 CGContextSetLineDash(
3298 (CGContextRef)[gctx graphicsPort],
3300 colors,
3301 pyrarray->size
3303 free(colors);
3304 return errNone;
3306 else
3308 post("Pen line dash needs a FloatArray as argument !\n");
3311 return errFailed;
3314 int prPen_LineCap(struct VMGlobals *g, int numArgsPushed);
3315 int prPen_LineCap(struct VMGlobals *g, int numArgsPushed)
3317 if ( !g->canCallOS )
3318 return errCantCallOS;
3320 int err=noErr, mode = 0;
3321 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
3322 if ( !gctx )
3323 return errFailed;
3325 PyrSlot *b = g->sp; // mode
3326 if( (err = slotIntVal(b, &mode)) != noErr )
3327 return err;
3329 //kCGLineCapButt (default - 0), kCGLineCapRound - 1, or kCGLineCapSquare - 2
3330 CGContextSetLineCap(
3331 (CGContextRef)[gctx graphicsPort],
3332 (CGLineCap)sc_clip(mode, kCGLineCapButt, kCGLineCapSquare)
3334 return errNone;
3337 int prPen_SetAlpha(struct VMGlobals *g, int numArgsPushed);
3338 int prPen_SetAlpha(struct VMGlobals *g, int numArgsPushed)
3340 if (!g->canCallOS)
3341 return errCantCallOS;
3343 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
3344 if (!gctx)
3345 return errFailed;
3347 int err = errNone; float value;
3348 PyrSlot *a = g->sp; // float
3350 err = slotFloatVal(a, &value);
3351 if(err != errNone)
3352 return err;
3354 CGContextSetAlpha((CGContextRef)[gctx graphicsPort], value);
3356 return errNone;
3361 * Gradient Functions
3362 * Valid only for MAC OS X 10.2 -) use the CGShading object
3365 #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_2
3367 typedef struct GradientInfo {
3368 SCColor color0;
3369 SCColor color1;
3370 int scCallback; // future plan for custom gradient calc functions
3371 } GradientInfo, GradientInfoRef;
3373 static GradientInfo gGradientInfo;
3375 static void GradientCalcLinear(void *info, const ColorData *in, ColorData *out)
3377 size_t k;
3379 if(gGradientInfo.scCallback) {
3380 for (k = 0; k < 4; k++) {
3381 *out++ = // result of Pen.gradientCalcFunc call;
3383 }else{
3385 for (k = 0; k < 4; k++) {
3386 *out++ = (ColorData)(((float *)(&gGradientInfo.color1))[k]) * (*in) + (ColorData)(((float *)(&gGradientInfo.color0))[k]) * (1-(*in));
3391 static CGFunctionRef CreateGradientFunctionRef()
3393 static const ColorData input_value_range [2] = { 0, 1 };
3394 static const ColorData output_value_ranges [8] = { 0, 1, 0, 1, 0, 1, 0, 1 };
3395 static const CGFunctionCallbacks callbacks = { 0,
3396 &GradientCalcLinear,
3397 NULL};
3401 Note: (colorspace = RGBA == 4)
3402 -------------------------
3403 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
3404 components = 1 + CGColorSpaceGetNumberOfComponents (colorSpace);
3405 CGColorSpaceRelease(colorSpace);
3408 // initialize gradient info
3409 gGradientInfo.color0 = SCMakeColor(1.f,1.f,1.f,1.f);
3410 gGradientInfo.color1 = SCMakeColor(0.f,0.f,0.f,1.f);
3411 gGradientInfo.scCallback = false;
3413 return CGFunctionCreate ((void *) &gGradientInfo,
3415 input_value_range,
3416 4, /* RGBA */
3417 output_value_ranges,
3418 &callbacks);
3422 static CGFunctionRef gGradientFunctionRef = NULL;
3423 static CGFunctionRef GetGradientFunction()
3425 if(!gGradientFunctionRef) {
3426 post("creating gradient function !\n");
3427 gGradientFunctionRef = CreateGradientFunctionRef();
3429 return gGradientFunctionRef;
3433 int prPen_DrawAxialGradient(struct VMGlobals *g, int numArgsPushed);
3434 int prPen_DrawAxialGradient(struct VMGlobals *g, int numArgsPushed)
3436 if (!g->canCallOS) return errCantCallOS;
3438 int err;
3439 NSPoint points[2];
3441 PyrSlot *a = g->sp - 3; // point 1
3442 PyrSlot *b = g->sp - 2; // point 2
3443 PyrSlot *c0= g->sp - 1; // color 1
3444 PyrSlot *c1= g->sp; // color 2
3446 if (!(isKindOfSlot(a, s_point->u.classobj))) return errWrongType;
3447 if (!(isKindOfSlot(b, s_point->u.classobj))) return errWrongType;
3448 if (!(isKindOfSlot(c0, s_color->u.classobj))) return errWrongType;
3449 if (!(isKindOfSlot(c1, s_color->u.classobj))) return errWrongType;
3451 CGFunctionRef gradFunc = CreateGradientFunctionRef(); // we need to create a new function ref since Quartz cache it
3452 if(!gradFunc) {
3453 post("Error: Failed creating gradient function !\n");
3454 return errFailed;
3457 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
3458 if (!gctx)
3459 return errFailed;
3461 err = slotColorVal(c0, &(gGradientInfo.color0));
3462 if( err != errNone )
3463 return err;
3465 err = slotColorVal(c1, &(gGradientInfo.color1));
3466 if( err != errNone )
3467 return err;
3469 err = slotGetPoint(a, &points[0]);
3470 if( err != errNone )
3471 return err;
3473 err = slotGetPoint(b, &points[1]);
3474 if( err != errNone )
3475 return err;
3477 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
3478 CGShadingRef shadingRef = CGShadingCreateAxial(
3479 colorSpace,
3480 *(CGPoint*)(&points[0]), *(CGPoint*)(&points[1]),
3481 gradFunc, true, true
3484 CGContextSaveGState((CGContextRef)[gctx graphicsPort]);
3485 CGContextClip((CGContextRef)[gctx graphicsPort]);
3486 CGContextDrawShading(
3487 (CGContextRef)[gctx graphicsPort], shadingRef
3489 CGContextRestoreGState((CGContextRef)[gctx graphicsPort]);
3491 CGColorSpaceRelease(colorSpace);
3492 CGShadingRelease(shadingRef);
3493 CGFunctionRelease(gradFunc);
3495 return errNone;
3498 int prPen_DrawRadialGradient(struct VMGlobals *g, int numArgsPushed);
3499 int prPen_DrawRadialGradient(struct VMGlobals *g, int numArgsPushed)
3501 if (!g->canCallOS) return errCantCallOS;
3503 int err;
3504 NSPoint points[2];
3505 float radius[2] = {0.f, 0.f};
3507 PyrSlot *a = g->sp - 5; // point 1
3508 PyrSlot *b = g->sp - 4; // point 2
3509 PyrSlot *srad = g->sp - 3; // start radius
3510 PyrSlot *erad = g->sp - 2; // end radius
3511 PyrSlot *c0= g->sp - 1; // color 1
3512 PyrSlot *c1= g->sp; // color 2
3515 if (!(isKindOfSlot(a, s_point->u.classobj))) return errWrongType;
3516 if (!(isKindOfSlot(b, s_point->u.classobj))) return errWrongType;
3517 if (!(isKindOfSlot(c0, s_color->u.classobj))) return errWrongType;
3518 if (!(isKindOfSlot(c1, s_color->u.classobj))) return errWrongType;
3520 CGFunctionRef gradFunc = CreateGradientFunctionRef();
3521 if(!gradFunc) {
3522 NSLog(@"Error: prPen_DrawRadialGradient Failed creating gradient function !\n");
3523 return errFailed;
3526 NSGraphicsContext *gctx = [NSGraphicsContext currentContext];
3527 if (!gctx)
3528 return errFailed;
3530 err = slotColorVal(c0, &(gGradientInfo.color0));
3531 if( err != errNone )
3532 return err;
3534 err = slotColorVal(c1, &(gGradientInfo.color1));
3535 if( err != errNone )
3536 return err;
3538 err = slotGetPoint(a, &points[0]);
3539 if( err != errNone )
3540 return err;
3542 err = slotGetPoint(b, &points[1]);
3543 if( err != errNone )
3544 return err;
3546 err = slotFloatVal(srad, &radius[0]);
3547 if( err != errNone )
3548 return err;
3550 err = slotFloatVal(erad, &radius[1]);
3551 if( err != errNone )
3552 return err;
3554 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
3555 CGShadingRef shadingRef = CGShadingCreateRadial(
3556 colorSpace,
3557 *(CGPoint*)(&points[0]), radius[0],
3558 *(CGPoint*)(&points[1]), radius[1],
3559 gradFunc, true, true
3562 CGContextSaveGState( (CGContextRef)[gctx graphicsPort] );
3563 CGContextClip( (CGContextRef)[gctx graphicsPort] );
3564 CGContextDrawShading(
3565 (CGContextRef)[gctx graphicsPort], shadingRef
3567 CGContextRestoreGState( (CGContextRef)[gctx graphicsPort] );
3569 CGColorSpaceRelease(colorSpace);
3570 CGShadingRelease(shadingRef);
3571 CGFunctionRelease(gradFunc);
3572 return errNone;
3574 #else
3575 // Dummy primitives if mac os x < 10.2 // should never be the case now... but
3576 int prPen_DrawAxialGradient(struct VMGlobals *g, int numArgsPushed);
3577 int prPen_DrawAxialGradient(struct VMGlobals *g, int numArgsPushed) {return errNone;}
3578 int prPen_DrawRadialGradient(struct VMGlobals *g, int numArgsPushed);
3579 int prPen_DrawRadialGradient(struct VMGlobals *g, int numArgsPushed) {return errNone;}
3580 #endif
3583 * End Of Gradient Functions
3586 int prLoadUserPanel(struct VMGlobals *g, int numArgsPushed);
3587 int prLoadUserPanel(struct VMGlobals *g, int numArgsPushed)
3589 if (!g->canCallOS) return errCantCallOS;
3591 PyrSlot *a = g->sp - 1;
3592 PyrSlot *b = g->sp;
3594 if (!isKindOfSlot(b, class_string)) return errWrongType;
3595 PyrString* string = slotRawString(b);
3597 //NSString *nsstring = [NSString stringWithCString: string->s length: string->size];
3598 NSString *nsstring = [NSString stringWithCString:string->s encoding:[NSString defaultCStringEncoding]];
3599 nsstring = [nsstring substringToIndex:string->size];
3600 // why is this retained?
3601 [nsstring retain];
3603 void *scobj = slotRawObject(a);
3604 SEL sel = @selector(loadUserPanel:SCObject:);
3605 NSMethodSignature *sig = [SCVirtualMachine instanceMethodSignatureForSelector: sel];
3607 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
3608 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
3609 [anInvocation setTarget: scvm];
3610 [anInvocation setSelector: sel];
3611 [anInvocation setArgument: &nsstring atIndex: 2];
3612 [anInvocation setArgument: &scobj atIndex: 3];
3613 [scvm defer: anInvocation];
3615 return errNone;
3618 int prHasDeferred(struct VMGlobals *g, int numArgsPushed);
3619 int prHasDeferred(struct VMGlobals *g, int numArgsPushed)
3621 gUIChangeCounter.Change();
3622 return errNone;
3626 int prNewMenuItem(struct VMGlobals *g, int numArgsPushed);
3627 int prNewMenuItem(struct VMGlobals *g, int numArgsPushed)
3629 if (!g->canCallOS) return errCantCallOS;
3631 PyrSlot *caller = g->sp - 4; // the SC Object
3632 PyrSlot *parentObj = g->sp - 3; // the parent menu or nil for app menu
3633 PyrSlot *ind = g->sp - 2; // the index
3634 PyrSlot *stringSlot = g->sp - 1; // name of the menu item
3635 PyrSlot *submenbool = g->sp; // submenu bool
3637 bool nilIndex;
3638 int menIndex, err;
3640 nilIndex = IsNil(ind);
3641 if(!nilIndex){
3642 err = slotIntVal(ind, &menIndex);
3643 if (err) return err;
3646 //NSLog(@"menIndex: %i", menIndex);
3648 if (!(isKindOfSlot(stringSlot, class_string))) return errWrongType;
3650 PyrString* string = slotRawString(stringSlot);
3652 NSString *label = [NSString stringWithCString:string->s encoding:[NSString defaultCStringEncoding]];
3653 label = [label substringToIndex:string->size];
3654 //NSLog(@"Label: %@", label);
3655 // make a menu item
3656 SCNSMenuItem *menuItem = [[SCNSMenuItem alloc] initWithTitle:label action:@selector(doAction:) keyEquivalent:@""];
3657 [menuItem setTarget:menuItem];
3658 [menuItem setSCObject: slotRawObject(caller)];
3659 SetPtr(slotRawObject(caller)->slots + 0, menuItem); // set the dataptr
3661 if (IsTrue(submenbool)) {
3662 // make a submenu if needed
3663 //NSLog(@"Is SubMenu");
3664 [menuItem setSubmenu:[[NSMenu alloc] initWithTitle:label]];
3665 [[menuItem submenu] setAutoenablesItems:NO];
3668 NSMenu *parentMenu;
3669 if (IsNil(parentObj)) {
3670 // make a submenu if needed
3671 //NSLog(@"Top Level");
3672 parentMenu = [NSApp mainMenu];
3673 } else if (IsSym(parentObj)) {
3674 PyrSymbol *parentSymbol;
3675 int err = slotSymbolVal(parentObj, &parentSymbol);
3676 if (err) return err;
3677 if (strcmp(parentSymbol->name, "Help")==0) {
3678 parentMenu = [[[NSApp mainMenu] itemWithTitle:NSLocalizedString(@"Help", @"Help Menu Name")] submenu];
3680 } else if (isKindOfSlot(parentObj, getsym("CocoaMenuItem")->u.classobj)){
3681 SCNSMenuItem *parentItem = (SCNSMenuItem*)(slotRawPtr(slotRawObject(parentObj)->slots));
3682 if([parentItem hasSubmenu]) {
3683 //NSLog(@"Parent has submenu");
3684 parentMenu = [parentItem submenu];
3688 if(!parentMenu) {
3689 [label release];
3690 return errWrongType;
3693 // protect against bad indices
3694 int parentSize = [parentMenu numberOfItems];
3695 //NSLog(@"parentSize: %i", parentSize);
3696 if(menIndex > parentSize || nilIndex) {
3697 //NSLog(@"Index > parentSize");
3698 menIndex = parentSize;
3700 if(menIndex < 0) {
3701 //NSLog(@"Index < 0");
3702 menIndex = 0;
3704 //NSLog(@"Final Index: %i", menIndex);
3706 [parentMenu insertItem:menuItem atIndex:menIndex];
3707 [menuItem retain];
3708 if([menuItem hasSubmenu]) { [[menuItem submenu] release]; } // this has been retained
3709 [label release];
3710 return errNone;
3713 int prNewMenuSeparator(struct VMGlobals *g, int numArgsPushed);
3714 int prNewMenuSeparator(struct VMGlobals *g, int numArgsPushed)
3716 if (!g->canCallOS) return errCantCallOS;
3718 PyrSlot *caller = g->sp - 2; // the SC Object
3719 PyrSlot *parentObj = g->sp - 1; // the parent menu or nil for app menu
3720 PyrSlot *ind = g->sp; // the index
3722 bool nilIndex;
3723 int menIndex, err;
3725 nilIndex = IsNil(ind);
3726 if(!nilIndex){
3727 err = slotIntVal(ind, &menIndex);
3728 if (err) return err;
3731 // make a menu item
3732 NSMenuItem *menuItem = [NSMenuItem separatorItem];
3733 SetPtr(slotRawObject(caller)->slots + 0, menuItem); // set the dataptr
3735 NSMenu *parentMenu;
3736 if (IsNil(parentObj)) {
3737 parentMenu = [NSApp mainMenu];
3738 } else if (IsSym(parentObj)) {
3739 PyrSymbol *parentSymbol;
3740 int err = slotSymbolVal(parentObj, &parentSymbol);
3741 if (err) return err;
3742 if (strcmp(parentSymbol->name, "Help")==0) {
3743 parentMenu = [[[NSApp mainMenu] itemWithTitle:NSLocalizedString(@"Help", @"Help Menu Name")] submenu];
3745 } else if (isKindOfSlot(parentObj, getsym("CocoaMenuItem")->u.classobj)){
3746 SCNSMenuItem *parentItem = (SCNSMenuItem*)(slotRawPtr(slotRawObject(parentObj)->slots));
3747 if([parentItem hasSubmenu]) {
3748 //NSLog(@"Parent has submenu");
3749 parentMenu = [parentItem submenu];
3753 if(!parentMenu) {
3754 return errWrongType;
3757 // protect against bad indices
3758 int parentSize = [parentMenu numberOfItems];
3759 //NSLog(@"parentSize: %i", parentSize);
3760 if(menIndex > parentSize || nilIndex) {
3761 //NSLog(@"Index > parentSize");
3762 menIndex = parentSize;
3764 if(menIndex < 0) {
3765 //NSLog(@"Index < 0");
3766 menIndex = 0;
3768 //NSLog(@"Final Index: %i", menIndex);
3770 [parentMenu insertItem:menuItem atIndex:menIndex];
3771 [menuItem retain];
3772 return errNone;
3776 int prRemoveMenuItem(struct VMGlobals *g, int numArgsPushed);
3777 int prRemoveMenuItem(struct VMGlobals *g, int numArgsPushed)
3780 PyrSlot *caller = g->sp;
3782 NSMenuItem *menuItem = (NSMenuItem*)(slotRawPtr(slotRawObject(caller)->slots));
3783 if(!menuItem) return errFailed;
3784 [[menuItem menu] removeItem:menuItem];
3785 SetPtr(slotRawObject(caller)->slots, NULL); // we're now a lame duck
3786 //if([menuItem hasSubmenu]) { [[menuItem submenu] release] };
3787 [menuItem release];
3788 return errNone;
3791 int prEnableMenuItem(struct VMGlobals *g, int numArgsPushed);
3792 int prEnableMenuItem(struct VMGlobals *g, int numArgsPushed)
3794 if (!g->canCallOS) return errCantCallOS;
3796 PyrSlot *caller = g->sp - 1;
3797 PyrSlot *boolean = g->sp;
3799 SCNSMenuItem *menuItem = (SCNSMenuItem*)(slotRawPtr(slotRawObject(caller)->slots));
3800 if(!menuItem) return errFailed;
3801 if(IsTrue(boolean)) {
3802 [menuItem setEnabled:YES];
3803 } else {
3804 [menuItem setEnabled:NO];
3806 return errNone;
3809 int prSetMenuItemState(struct VMGlobals *g, int numArgsPushed);
3810 int prSetMenuItemState(struct VMGlobals *g, int numArgsPushed)
3812 if (!g->canCallOS) return errCantCallOS;
3814 PyrSlot *caller = g->sp - 1;
3815 PyrSlot *boolean = g->sp;
3817 SCNSMenuItem *menuItem = (SCNSMenuItem*)(slotRawPtr(slotRawObject(caller)->slots));
3818 if(!menuItem) return errFailed;
3819 if(IsTrue(boolean)) {
3820 [menuItem setState:NSOnState];
3821 } else {
3822 [menuItem setState:NSOffState];
3824 return errNone;
3827 int prSetMenuItemKeyboardEquivalent(struct VMGlobals *g, int numArgsPushed);
3828 int prSetMenuItemKeyboardEquivalent(struct VMGlobals *g, int numArgsPushed)
3830 if (!g->canCallOS) return errCantCallOS;
3831 PyrSlot *caller = g->sp - 3;
3832 PyrSlot *stringSlot = g->sp - 2;
3833 PyrSlot *alt = g->sp - 1;
3834 PyrSlot *ctrl = g->sp;
3836 SCNSMenuItem *menuItem = (SCNSMenuItem*)(slotRawPtr(slotRawObject(caller)->slots));
3837 if(!menuItem) return errFailed;
3839 if (!(isKindOfSlot(stringSlot, class_string))) return errWrongType;
3841 PyrString* string = slotRawString(stringSlot);
3843 NSString *kbEquiv = [NSString stringWithCString:string->s encoding:[NSString defaultCStringEncoding] ];
3844 kbEquiv = [kbEquiv substringToIndex:string->size];
3845 [menuItem setKeyEquivalent:kbEquiv];
3847 int mod = NSCommandKeyMask;
3848 if(IsTrue(alt)) {
3849 mod = mod | NSAlternateKeyMask;
3852 if(IsTrue(ctrl)) {
3853 mod = mod | NSControlKeyMask;
3856 [menuItem setKeyEquivalentModifierMask:mod];
3857 return errNone;
3860 int prMouse_getCoords(struct VMGlobals *g, int numArgsPushed);
3861 int prMouse_getCoords(struct VMGlobals *g, int numArgsPushed)
3863 if (!g->canCallOS) return errCantCallOS;
3865 PyrSlot *a = g->sp - 1;
3866 PyrSlot *b = g->sp;
3868 NSPoint mouse_location = [NSEvent mouseLocation];
3869 PyrSlot *slots = slotRawObject(b)->slots;
3870 SetFloat( slots+0, mouse_location.x );
3871 SetFloat( slots+1, mouse_location.y );
3872 slotCopy(a, b);
3875 return errNone;
3878 int prRecompile(struct VMGlobals *g, int numArgsPushed);
3879 int prRecompile(struct VMGlobals *g, int numArgsPushed)
3881 SEL sel = @selector(compileLibrary:);
3882 NSMethodSignature *sig = [SCVirtualMachine instanceMethodSignatureForSelector: sel];
3884 NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
3885 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
3886 [anInvocation setTarget: scvm];
3887 [anInvocation setSelector: sel];
3888 [scvm defer: anInvocation];
3890 return errNone;
3893 int prSetDeferredTaskInterval(struct VMGlobals *g, int numArgsPushed);
3894 int prSetDeferredTaskInterval(struct VMGlobals *g, int numArgsPushed)
3896 if (!g->canCallOS) return errCantCallOS;
3898 PyrSlot *intv = g->sp;
3900 int err;
3901 float interval;
3903 err = slotFloatVal(intv, &interval);
3904 if (err) return err;
3905 SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
3906 [scvm setDeferredTaskInterval: interval];
3908 return errNone;
3911 void initSCImagePrimitives();
3913 void initGUIPrimitives()
3915 int base, index;
3917 s_draw = getsym("draw");
3918 s_font = getsym("SCFont");
3919 s_closed = getsym("closed");
3920 s_tick = getsym("tick");
3921 s_doaction = getsym("doAction");
3922 s_didBecomeKey = getsym("didBecomeKey");
3923 s_didResignKey = getsym("didResignKey");
3925 base = nextPrimitiveIndex();
3926 index = 0;
3929 //reset scobjects in mydocument
3930 //_TextWindow_ primitves by jan.t
3931 NSDocumentController *docctl = [NSDocumentController sharedDocumentController];
3932 if (docctl) {
3933 int num = [[docctl documents] count];
3934 for(int i=0; i<num; i++){
3935 MyDocument * doc = [[docctl documents] objectAtIndex: i];
3936 [doc setSCObject: nil];
3940 definePrimitive(base, index++, "_TextWindow_ToFront", prTextWindow_ToFront, 1, 0);
3941 definePrimitive(base, index++, "_TextWindow_SelectedText", prTextWindow_SelectedText, 1, 0);
3942 definePrimitive(base, index++, "_TextWindow_InsertText", prTextWindow_InsertText, 2, 0);
3943 definePrimitive(base, index++, "_TextWindow_InsertTextInRange", prTextWindow_InsertTextInRange, 4, 0);
3944 definePrimitive(base, index++, "_TextWindow_GetByIndex", prTextWindow_GetByIndex, 2, 0);
3945 definePrimitive(base, index++, "_NumberOfOpenTextWindows", prNumberOfOpenTextWindows, 1, 0);
3946 definePrimitive(base, index++, "_TextWindow_GetLastIndex", prTextWindow_GetLastIndex, 1, 0);
3947 definePrimitive(base, index++, "_TextWindow_GetName", prTextWindow_GetName, 1, 0);
3948 definePrimitive(base, index++, "_TextWindow_IsEdited", prTextWindow_IsEdited, 1, 0);
3949 definePrimitive(base, index++, "_TextWindow_Close", prTextWindow_Close, 1, 0);
3950 definePrimitive(base, index++, "_TextWindow_Text", prTextWindow_Text, 1, 0);
3951 definePrimitive(base, index++, "_TextWindow_SetBackgroundColor", prTextWindow_SetBackgroundColor, 2, 0);
3952 definePrimitive(base, index++, "_TextWindow_GetBackgroundColor", prTextWindow_GetBackgroundColor, 2, 0);
3953 definePrimitive(base, index++, "_TextWindow_SetSelectedBackgroundColor", prTextWindow_SetSelectedBackgroundColor, 2, 0);
3954 definePrimitive(base, index++, "_TextWindow_GetSelectedBackgroundColor", prTextWindow_GetSelectedBackgroundColor, 2, 0);
3955 definePrimitive(base, index++, "_TextWindow_SetTextColor", prTextWindow_SetTextColor, 4, 0);
3956 definePrimitive(base, index++, "_TextWindow_SyntaxColorize", prTextWindow_SyntaxColorize, 1, 0);
3957 definePrimitive(base, index++, "_TextWindow_SelectLine", prTextWindow_SelectLine, 2, 0);
3958 definePrimitive(base, index++, "_TextWindow_SelectRange", prTextWindow_SelectRange, 3, 0);
3959 definePrimitive(base, index++, "_TextWindow_TextWithRange", prTextWindow_TextWithRange, 3, 0);
3960 definePrimitive(base, index++, "_TextWindow_SetFont", prTextWindow_SetFont, 4, 0);
3961 definePrimitive(base, index++, "_TextWindow_GetIndexOfListener", prTextWindow_GetIndexOfListener, 1, 0);
3962 definePrimitive(base, index++, "_TextWindow_GetSelectedRangeLocation", prTextWindow_GetSelectedRangeLocation, 1, 0);
3963 definePrimitive(base, index++, "_TextWindow_GetSelectedRangeLength", prTextWindow_GetSelectedRangeLength, 1, 0);
3964 definePrimitive(base, index++, "_TextWindow_UnfocusedFront", prTextWindow_UnfocusedFront, 1, 0);
3965 definePrimitive(base, index++, "_TextWindow_SetBounds", prTextWindow_SetBounds, 2, 0);
3966 definePrimitive(base, index++, "_TextWindow_GetBounds", prTextWindow_GetBounds, 2, 0);
3967 definePrimitive(base, index++, "_TextWindow_SetName", prTextWindow_SetName, 2, 0);
3968 definePrimitive(base, index++, "_TextWindow_SetEditable", prTextWindow_SetEditable, 2, 0);
3969 definePrimitive(base, index++, "_TextWindow_RemoveUndo", prTextWindow_RemoveUndo, 1, 0);
3970 // definePrimitive(base, index++, "_TextWindow_SelectToken", prTextWindow_SelectToken, 1, 0);
3971 definePrimitive(base, index++, "_TextWindow_SelectUnderlinedText", prTextWindow_SelectUnderlinedText, 2, 0);
3972 definePrimitive(base, index++, "_TextWindow_GetFileName", prTextWindow_GetFileName, 1, 0);
3973 definePrimitive(base, index++, "_TextWindow_SetFileName", prTextWindow_SetFileName, 2, 0);
3975 definePrimitive(base, index++, "_TextWindow_UnderlineSelection", prTextWindow_UnderlineSelection, 1, 0);
3976 definePrimitive(base, index++, "_TextWindow_AlwaysOnTop", prTextWindow_AlwaysOnTop, 2, 0);
3977 definePrimitive(base, index++, "_TextWindow_IsAlwaysOnTop", prTextWindow_IsAlwaysOnTop, 1, 0);
3978 definePrimitive(base, index++, "_TextWindow_SetPromptToSave", prTextWindow_SetPromptToSave, 2, 0);
3979 definePrimitive(base, index++, "_TextWindow_PromptToSave", prTextWindow_PromptToSave, 1, 0);
3980 definePrimitive(base, index++, "_TextWindow_SetDefaultFont", prTextWindow_SetDefaultFont, 2, 0);
3981 definePrimitive(base, index++, "_PostWindow_SetTextColor", prSetPostTextColor, 2, 0);
3982 definePrimitive(base, index++, "_TextWindow_SetSyntaxColorTheme", prSetSyntaxColorTheme, 11, 0);
3983 definePrimitive(base, index++, "_TextWindow_BalanceParens", prBalanceParens, 1, 0);
3984 definePrimitive(base, index++, "_TextWindow_UsesAutoInOutdent", prTextWindow_UsesAutoInOutdent, 2, 0);
3985 definePrimitive(base, index++, "_TextWindow_DefaultUsesAutoInOutdent", prTextWindow_DefaultUsesAutoInOutdent, 2, 0);
3989 definePrimitive(base, index++, "_NewTextWindow", prNewTextWindow, 4, 0);
3990 definePrimitive(base, index++, "_OpenTextFile", prOpenTextFile, 4, 0);
3992 definePrimitive(base, index++, "_GetStringFromUser", prGetStringFromUser, 3, 0);
3993 definePrimitive(base, index++, "_SCWindow_New", prSCWindow_New, 7, 0);
3994 definePrimitive(base, index++, "_SCWindow_Refresh", prSCWindow_Refresh, 1, 0);
3995 definePrimitive(base, index++, "_SCWindow_Close", prSCWindow_Close, 1, 0);
3996 definePrimitive(base, index++, "_SCWindow_Minimize", prSCWindow_Minimize, 1, 0);
3997 definePrimitive(base, index++, "_SCWindow_ToFront", prSCWindow_ToFront, 1, 0);
3998 definePrimitive(base, index++, "_SCWindow_BeginFullScreen", prSCWindow_FullScreen, 1, 0);
3999 definePrimitive(base, index++, "_SCWindow_EndFullScreen", prSCWindow_EndFullScreen, 1, 0);
4000 definePrimitive(base, index++, "_SCWindow_SetShouldClose", prSCWindow_SetShouldClose, 2, 0);
4002 definePrimitive(base, index++, "_SCWindow_Show", prSCWindow_Show, 1, 0);
4003 definePrimitive(base, index++, "_SCWindow_Hide", prSCWindow_Hide, 1, 0);
4004 definePrimitive(base, index++, "_SCWindow_AlwaysOnTop", prSCWindow_AlwaysOnTop, 2, 0);
4005 definePrimitive(base, index++, "_SCWindow_AcceptsClickThrough", prSCWindow_AcceptsClickThrough, 2, 0);
4007 definePrimitive(base, index++, "_SCWindow_SetName", prSCWindow_SetName, 2, 0);
4008 definePrimitive(base, index++, "_SCWindow_SetAlpha", prSCWindow_SetAlpha, 2, 0);
4009 definePrimitive(base, index++, "_SCWindow_SetBounds", prSCWindow_SetBounds, 2, 0);
4010 definePrimitive(base, index++, "_SCWindow_GetBounds", prSCWindow_GetBounds, 2, 0);
4011 definePrimitive(base, index++, "_SCWindow_GetScreenBounds", prSCWindow_GetScreenBounds, 2, 0);
4012 definePrimitive(base, index++, "_SCWindow_SetAcceptMouseOver", prSCWindow_SetAcceptMouseOver, 2, 0);
4013 definePrimitive(base, index++, "_SCWindow_RunModal", prSCWindow_RunModal, 1, 0);
4014 definePrimitive(base, index++, "_SCWindow_StopModal", prSCWindow_StopModal, 1, 0);
4015 definePrimitive(base, index++, "_SCWindow_RunModalSheet", prSCWindow_RunModalSheet, 2, 0);
4016 definePrimitive(base, index++, "_SCWindow_StopModalSheet", prSCWindow_StopModalSheet, 1, 0);
4017 definePrimitive(base, index++, "_ShowCursor", prShowCursor, 2, 0);
4019 definePrimitive(base, index++, "_String_GetBounds", prString_GetBounds, 3, 0);
4020 definePrimitive(base, index++, "_String_DrawAtPoint", prString_DrawAtPoint, 4, 0);
4021 definePrimitive(base, index++, "_String_DrawInRect", prString_DrawInRect, 4, 0);
4022 definePrimitive(base, index++, "_Color_SetStroke", prColor_SetStroke, 1, 0);
4023 definePrimitive(base, index++, "_Color_SetFill", prColor_SetFill, 1, 0);
4025 definePrimitive(base, index++, "_Pen_StrokeRect", prPen_StrokeRect, 2, 0);
4026 definePrimitive(base, index++, "_Pen_FillRect", prPen_FillRect, 2, 0);
4027 definePrimitive(base, index++, "_Pen_StrokeOval", prPen_StrokeOval, 2, 0);
4028 definePrimitive(base, index++, "_Pen_FillOval", prPen_FillOval, 2, 0);
4029 definePrimitive(base, index++, "_Pen_StrokeLine", prPen_StrokeLine, 3, 0);
4030 definePrimitive(base, index++, "_Pen_Push", prPen_Push, 1, 0);
4031 definePrimitive(base, index++, "_Pen_Pop", prPen_Pop, 1, 0);
4032 definePrimitive(base, index++, "_Pen_Translate", prPen_Translate, 3, 0);
4033 definePrimitive(base, index++, "_Pen_Skew", prPen_Skew, 3, 0);
4034 definePrimitive(base, index++, "_Pen_Scale", prPen_Scale, 3, 0);
4035 definePrimitive(base, index++, "_Pen_Rotate", prPen_Rotate, 4, 0);
4036 definePrimitive(base, index++, "_Pen_SetWidth", prPen_SetWidth, 2, 0);
4037 definePrimitive(base, index++, "_Pen_SetMatrix", prPen_SetMatrix, 2, 0);
4038 definePrimitive(base, index++, "_Pen_AddRect", prPen_AddRect, 2, 0);
4039 definePrimitive(base, index++, "_Pen_AddArc", prPen_AddArc, 5, 0);
4040 definePrimitive(base, index++, "_Pen_AddWedge", prPen_AddWedge, 5, 0);
4041 definePrimitive(base, index++, "_Pen_AddAnnularWedge", prPen_AddAnnularWedge, 6, 0);
4043 definePrimitive(base, index++, "_Pen_BeginPath", prPen_BeginPath, 1, 0);
4044 definePrimitive(base, index++, "_Pen_ClosePath", prPen_ClosePath, 1, 0);
4045 definePrimitive(base, index++, "_Pen_MoveTo", prPen_MoveTo, 2, 0);
4046 definePrimitive(base, index++, "_Pen_LineTo", prPen_LineTo, 2, 0);
4047 definePrimitive(base, index++, "_Pen_StrokePath", prPen_StrokePath, 1, 0);
4048 definePrimitive(base, index++, "_Pen_FillPath", prPen_FillPath, 1, 0);
4049 definePrimitive(base, index++, "_Pen_ClipPath", prPen_ClipPath, 1, 0);
4051 definePrimitive(base, index++, "_Pen_AddQuadCurve", prPen_AddBezierQuadCurve, 3, 0);
4052 definePrimitive(base, index++, "_Pen_AddCubicCurve", prPen_AddBezierCubicCurve, 4, 0);
4053 definePrimitive(base, index++, "_Pen_AddArcToPoint", prPen_AddArcToPoint, 4, 0);
4054 definePrimitive(base, index++, "_Pen_ClearRect", prPen_ClearRect, 2, 0);
4055 definePrimitive(base, index++, "_Pen_SetBlendMode", prPen_SetBlend, 2, 0);
4056 definePrimitive(base, index++, "_Pen_SetShadow", prPen_SetShadow, 4, 0);
4057 definePrimitive(base, index++, "_Pen_SetAlpha", prPen_SetAlpha, 2, 0);
4058 definePrimitive(base, index++, "_Pen_BeginTLayer", prPen_BeginTransparencyLayer, 1, 0);
4059 definePrimitive(base, index++, "_Pen_EndTLayer", prPen_EndTransparencyLayer, 1, 0);
4060 definePrimitive(base, index++, "_Pen_DrawPath", prPen_DrawPath, 2, 0);
4061 definePrimitive(base, index++, "_Pen_LineJoin", prPen_LineJoin, 2, 0);
4062 definePrimitive(base, index++, "_Pen_LineDash", prPen_LineDash, 2, 0);
4063 definePrimitive(base, index++, "_Pen_LineCap", prPen_LineCap, 2, 0);
4064 definePrimitive(base, index++, "_Pen_AddOval", prPen_AddOval, 2, 0);
4065 definePrimitive(base, index++, "_Pen_DrawAxialGradient", prPen_DrawAxialGradient, 5, 0);
4066 definePrimitive(base, index++, "_Pen_DrawRadialGradient", prPen_DrawRadialGradient, 7, 0);
4068 definePrimitive(base, index++, "_HasDeferred", prHasDeferred, 1, 0);
4069 definePrimitive(base, index++, "_LoadUserPanel", prLoadUserPanel, 2, 0);
4071 definePrimitive(base, index++, "_Font_AvailableFonts", prFont_AvailableFonts, 1, 0);
4072 definePrimitive(base, index++, "_Font_SetAntiAliasing", prFont_SetAntiAliasing, 2, 0);
4073 definePrimitive(base, index++, "_Font_SetSmoothing", prFont_SetSmoothing, 2, 0);
4075 definePrimitive(base, index++, "_TextWindow_LinkAtClickPos", prTextWindow_LinkAtClickPos, 2, 0);
4076 definePrimitive(base, index++, "_Pen_SetSmoothing", prPen_SetSmoothing, 2, 0);
4078 definePrimitive(base, index++, "_NewMenuItem", prNewMenuItem, 5, 0);
4079 definePrimitive(base, index++, "_NewMenuSeparator", prNewMenuSeparator, 3, 0);
4080 definePrimitive(base, index++, "_RemoveMenuItem", prRemoveMenuItem, 1, 0);
4081 definePrimitive(base, index++, "_EnableMenuItem", prEnableMenuItem, 2, 0);
4082 definePrimitive(base, index++, "_SetMenuItemState", prSetMenuItemState, 2, 0);
4083 definePrimitive(base, index++, "_SetMenuItemKeyboardEquivalent", prSetMenuItemKeyboardEquivalent, 4, 0);
4084 definePrimitive(base, index++, "_Mouse_getCoords", prMouse_getCoords, 2, 0);
4086 definePrimitive(base, index++, "_Recompile", prRecompile, 1, 0);
4087 definePrimitive(base, index++, "_SetDeferredTaskInterval", prSetDeferredTaskInterval, 2, 0);
4089 initSCImagePrimitives();