class library: Spawner - don't access PriorityQueue-array
[supercollider.git] / editors / scapp / iPhone / iPhoneGUIPrimitives.mm
blob772d447b3a5c442b8552860ebdb7d42953519157
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 #import <UIKit/UIKit.h>
22 #include "PyrPrimitive.h"
23 #include "PyrObject.h"
24 #include "PyrKernel.h"
25 #include "PyrSymbol.h"
26 #include "VMGlobals.h"
27 #include "SC_RGen.h"
28 #include "SCBase.h"
30 #import "iPhoneSCGraphView.h"
31 #import "ChangeCounter.h"
32 #import "GC.h"
33 #import "iPhoneSCNSWindow.h"
34 #import "iSCLangController.h"
35 //#import "SCTextView.h"
36 //#import "SCCocoaView.h"
38 extern ChangeCounter gUIChangeCounter;
39 //extern NSTextView* gPostView;
40 PyrSymbol *s_draw;
41 PyrSymbol *s_font;
42 PyrSymbol *s_closed;
43 PyrSymbol *s_tick;
44 PyrSymbol *s_doaction;
45 PyrSymbol *s_didBecomeKey;
46 PyrSymbol *s_didResignKey;
48 extern bool docCreatedFromLang;
49 int slotColorVal(PyrSlot *slot, SCColor *sccolor);
52 int slotGetCGRect(PyrSlot* a, CGRect *r)
54         PyrSlot *slots = slotRawObject(a)->slots;
55         int err;
56         err = slotFloatVal(slots+0, &r->origin.x);
57         if (err) return err;
58         err = slotFloatVal(slots+1, &r->origin.y);
59         if (err) return err;
60         err = slotFloatVal(slots+2, &r->size.width);
61         if (err) return err;
62         err = slotFloatVal(slots+3, &r->size.height);
63         if (err) return err;
65         return errNone;
68 int slotGetQDRect(PyrSlot* a, Rect *r)
70         PyrSlot *slots = slotRawObject(a)->slots;
71         int err;
72         float x, y, width, height;
74         err = slotFloatVal(slots+0, &x);
75         if (err) return err;
76         err = slotFloatVal(slots+1, &y);
77         if (err) return err;
78         err = slotFloatVal(slots+2, &width);
79         if (err) return err;
80         err = slotFloatVal(slots+3, &height);
81         if (err) return err;
83         r->left   = (int)x;
84         r->right  = (int)(x + width);
85         r->top    = (int)(y - height);
86         r->bottom = (int)y;
88         return errNone;
91 int slotGetPoint(PyrSlot* a, CGPoint *p)
93         PyrSlot *slots = slotRawObject(a)->slots;
94         int err;
95         err = slotFloatVal(slots+0, &p->x);
96         if (err) return err;
97         err = slotFloatVal(slots+1, &p->y);
98         if (err) return err;
100         return errNone;
104 int prSCWindow_New(struct VMGlobals *g, int numArgsPushed);
105 int prSCWindow_New(struct VMGlobals *g, int numArgsPushed)
107         if (!g->canCallOS) return errCantCallOS;
109         PyrSlot *args = g->sp - 6;
110         PyrSlot *a = args + 0;
111         PyrSlot *b = args + 1; // name
112         PyrSlot *c = args + 2; // bounds
113         PyrSlot *d = args + 3; // resizable
114         PyrSlot *e = args + 4; // border
115         PyrSlot *f = args + 6; // view
116         PyrSlot *h = args + 5; // scroll
117         PyrSlot *j = args + 7; // is this app modal? (Doesn't matter for sheets as they have no close button)
119         if (!(isKindOfSlot(b, class_string))) return errWrongType;
120         if (!(isKindOfSlot(c, s_rect->u.classobj))) return errWrongType;
122         CGRect bounds;
123         int err = slotGetCGRect(c, &bounds);
124         if (err) return err;
126         PyrString *string = slotRawString(b);
127         NSString *title = [NSString stringWithCString: string->s length: string->size];
129         SCNSWindow *window = [[SCNSWindow alloc] initWithFrame: bounds];
130         [window setTitle: title];
131         [window setHasBorders: YES];
133         iSCLangController *controller = [iSCLangController sharedInstance];
134         SCGraphView* view = [[SCGraphView alloc] initWithFrame: bounds];
135         [view setSCObject: slotRawObject(a)];
136         SetPtr(slotRawObject(a)->slots + 0, view);
137         [controller insertWindow:window];
138         [window setSCGraphView: view];
140         if(IsTrue(h)) {
142                 SCScrollTopView* scrollTopView = (SCScrollTopView*)slotRawInt(slotRawObject(f)->slots);
143                 [view setSCTopView: scrollTopView];
145                 NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame: bounds];
146                 [scrollView setHasVerticalScroller:YES];
147                 [scrollView setHasHorizontalScroller:YES];
148                 [scrollView setAutohidesScrollers:YES];
149                 [[scrollView horizontalScroller] setControlSize:NSSmallControlSize];
150                 [[scrollView verticalScroller] setControlSize:NSSmallControlSize];
151                 [[scrollView horizontalScroller] setControlTint:NSGraphiteControlTint];
152                 [[scrollView verticalScroller] setControlTint:NSGraphiteControlTint];
154                 [scrollView setBackgroundColor:[NSColor clearColor]];
155                 [scrollView setDrawsBackground:NO];
156                 // configure the scroller to have no visible border
157                 [scrollView setBorderType:NSNoBorder];
158                 [scrollView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
159                 [scrollView setDocumentView:view];
161                 [scrollView setPostsFrameChangedNotifications: YES]; // we need this to resize the SCGraphView if the scroll view exceeds its bounds
162                 [[NSNotificationCenter defaultCenter] addObserver:view
163                                                                                                  selector:@selector(scrollViewResized:)
164                                                                                                          name:@"NSViewFrameDidChangeNotification"
165                                                                                                    object:scrollView];
167                 NSClipView *contentView = [scrollView contentView];
168                 [contentView setPostsBoundsChangedNotifications:YES];
169                 [[NSNotificationCenter defaultCenter] addObserver:view
170                                                                                                  selector:@selector(userScrolled:)
171                                                                                                          name:@"NSViewBoundsDidChangeNotification"
172                                                                                                    object:contentView];
174                 scrollTopView->SetNSScrollView(scrollView);
175                 [view autorelease];
177                 [window setContentView: scrollView];
178                 [scrollView autorelease];
180         } else {
181                 [view setSCTopView: (SCTopView*)slotRawPtr(slotRawObject(f)->slots)];
182                 [window addSubview: view];
183                 [view autorelease];
184         }
185         //[window makeFirstResponder: view];
186         //[window setFrameOrigin: bounds.origin];
189         return errNone;
193 int prSCWindow_Refresh(struct VMGlobals *g, int numArgsPushed);
194 int prSCWindow_Refresh(struct VMGlobals *g, int numArgsPushed)
196     if (!g->canCallOS) return errCantCallOS;
198     PyrSlot *a = g->sp;
199     SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
200     if (!view) return errNone;
202     SEL sel = @selector(setNeedsDisplay:);
203     NSMethodSignature *sig = [UIView instanceMethodSignatureForSelector: sel];
205     NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
206     iSCLangController* controller = [iSCLangController sharedInstance];
207     [anInvocation setTarget: view];
208     [anInvocation setSelector: sel];
209     BOOL flag = YES;
210     [anInvocation setArgument: &flag atIndex: 2];
211     [controller defer: anInvocation];
213     return errNone;
216 int prSCWindow_Close(struct VMGlobals *g, int numArgsPushed);
217 int prSCWindow_Close(struct VMGlobals *g, int numArgsPushed)
219    if (!g->canCallOS) return errCantCallOS;
221     PyrSlot *a = g->sp;
222     SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
223     if (!view) return errNone;
225     SCNSWindow *window = (SCNSWindow *) [view superview];
227     SEL sel = @selector(closeWindow:);
228     NSMethodSignature *sig = [iSCLangController instanceMethodSignatureForSelector: sel];
230     NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
231     iSCLangController* controller = [iSCLangController sharedInstance];
232     [anInvocation setTarget: controller];
233     [anInvocation setSelector: sel];
234     [anInvocation setArgument: &window atIndex: 2];
235     [controller defer: anInvocation];
237     return errNone;
239     PyrSlot *a = g->sp;
240     SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
241     if (!view) return errNone;
243     SEL sel = @selector(closeWindow);
244     NSMethodSignature *sig = [SCGraphView instanceMethodSignatureForSelector: sel];
246     NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
247     iSCLangController* controller = [iSCLangController sharedInstance];
248     [anInvocation setTarget: view];
249     [anInvocation setSelector: sel];
250     [controller defer: anInvocation];
252     return errNone;
257 int prSCWindow_ToFront(struct VMGlobals *g, int numArgsPushed);
258 int prSCWindow_ToFront(struct VMGlobals *g, int numArgsPushed)
260     if (!g->canCallOS) return errCantCallOS;
262     PyrSlot *a = g->sp;
263     SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
264     if (!view) return errNone;
266     SCNSWindow *window = (SCNSWindow *) [view superview];
268     SEL sel = @selector(makeWindowFront:);
269     NSMethodSignature *sig = [iSCLangController instanceMethodSignatureForSelector: sel];
271     NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
272     iSCLangController* controller = [iSCLangController sharedInstance];
273     [anInvocation setTarget: controller];
274     [anInvocation setSelector: sel];
275     [anInvocation setArgument: &window atIndex: 2];
276     [controller defer: anInvocation];
278     return errNone;
281 int prSCWindow_SetName(struct VMGlobals *g, int numArgsPushed);
282 int prSCWindow_SetName(struct VMGlobals *g, int numArgsPushed)
284     if (!g->canCallOS) return errCantCallOS;
286     PyrSlot *a = g->sp - 1;
287     PyrSlot *b = g->sp;
289     if (!(isKindOfSlot(b, class_string))) return errWrongType;
291     SCGraphView* view = (SCGraphView*)slotRawPtr(slotRawObject(a)->slots);
292     if (!view) return errNone;
293     PyrString *string = slotRawString(b);
294     NSString *title = [NSString stringWithCString: string->s length: string->size];
295     [[view window] setTitle: title];
296     return errNone;
300 int prFont_AvailableFonts(struct VMGlobals *g, int numArgsPushed);
301 int prFont_AvailableFonts(struct VMGlobals *g, int numArgsPushed)
303     if (!g->canCallOS) return errCantCallOS;
305         PyrSlot *a = g->sp;
307         NSArray *fonts = [UIFont familyNames];
309         int size = [fonts count];
310         PyrObject* array = newPyrArray(g->gc, size, 0, true);
311         SetObject(a, array);
313         for (int i=0; i<size; ++i) {
314                 NSString *name = [fonts objectAtIndex: i];
315                 //if (!name) continue;
316                 PyrString *string = newPyrString(g->gc, [name UTF8String], 0, true);
317                 SetObject(array->slots + array->size, string);
318                 array->size++;
319                 g->gc->GCWrite(array, string);
320         }
322     return errNone;
326 void initGUIPrimitives()
328         int base, index;
330         s_draw = getsym("draw");
331         s_font = getsym("SCFont");
332         s_closed = getsym("closed");
333         s_tick = getsym("tick");
334         s_doaction = getsym("doAction");
335         s_didBecomeKey = getsym("didBecomeKey");
336         s_didResignKey = getsym("didResignKey");
338         base = nextPrimitiveIndex();
339         index = 0;
341         definePrimitive(base, index++, "_SCWindow_New", prSCWindow_New, 7, 0);
342         definePrimitive(base, index++, "_SCWindow_Refresh", prSCWindow_Refresh, 1, 0);
343         definePrimitive(base, index++, "_SCWindow_Close", prSCWindow_Close, 1, 0);
344         definePrimitive(base, index++, "_SCWindow_ToFront", prSCWindow_ToFront, 1, 0);
345         definePrimitive(base, index++, "_SCWindow_SetName", prSCWindow_SetName, 2, 0);
347         definePrimitive(base, index++, "_Font_AvailableFonts", prFont_AvailableFonts, 1, 0);