Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / editors / scapp / UserPanel.M
blobeb99fbaaf5af570115ae3e4a1271a729974215c0
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 #import "UserPanel.h"
22 #import <objc/objc-class.h>
23 //#import <objc/runtime.h>
25 #include "SCBase.h"
26 #include "PyrSymbol.h"
27 #include "PyrObject.h"
28 #include "PyrKernel.h"
29 #include "GC.h"
30 #include "VMGlobals.h"
31 #include <pthread.h>
33 #if (MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4)
34 #define class_getName(a) ((a)->name)
35 #endif
37 NSMutableArray *gUserPanels = nil;
38 extern PyrSymbol *s_doaction;
39 extern PyrSymbol *s_closed;
40 extern pthread_mutex_t gLangMutex;
42 @implementation UserPanel
44 + (void)closeAll
46 if (gUserPanels && [gUserPanels count]) {
47 NSMutableArray *copy = [gUserPanels copy];
48 [gUserPanels removeAllObjects];
49 [copy makeObjectsPerformSelector: @selector(close)];
50 [copy release];
54 - (id)init
56 [super init];
57 if (!gUserPanels) {
58 gUserPanels = [NSMutableArray arrayWithCapacity: 8];
59 [gUserPanels retain];
61 [gUserPanels addObject: self];
62 return self;
65 - (NSWindow*)window { return window; }
67 - (void)close
69 [window close];
72 - (void)windowWillClose:(NSNotification *)aNotification
74 [gUserPanels removeObject: self];
76 pthread_mutex_lock (&gLangMutex);
77 if (scobject) {
78 VMGlobals *g = gMainVMGlobals;
79 g->canCallOS = true;
80 ++g->sp; SetObject(g->sp, scobject); // push window obj
81 runInterpreter(g, s_closed, 1);
82 g->canCallOS = false;
84 pthread_mutex_unlock (&gLangMutex);
87 - (void)setSCObject: (struct PyrObject*)inObject { scobject = inObject; }
89 - (struct PyrObject*) getSCObject { return scobject; }
91 void post(const char *fmt, ...);
93 - (IBAction) panelAction: (id) sender
95 id hitThing = sender;
96 post("sender '%s' %d\n", class_getName(object_getClass(sender)), [sender tag]);
97 if ([sender respondsToSelector: @selector(selectedCell)]) {
98 id cell = [sender selectedCell];
99 if (cell) {
100 hitThing = cell;
101 } else {
102 post(" cell nil\n");
105 int tag = [hitThing tag];
106 post("hitThing '%s' %d\n", class_getName(object_getClass(hitThing)), [sender tag]);
107 // what kind of value is appropriate?
108 double controlValue = 0.;
109 if ([hitThing respondsToSelector: @selector(doubleValue)]) {
110 controlValue = [hitThing doubleValue];
112 char *cstring = 0;
113 if ([hitThing respondsToSelector: @selector(stringValue)]) {
114 cstring = (char*)[[hitThing stringValue] cStringUsingEncoding:[NSString defaultCStringEncoding]];
117 pthread_mutex_lock (&gLangMutex);
118 if (scobject) {
119 VMGlobals *g = gMainVMGlobals;
120 g->canCallOS = true;
121 ++g->sp; SetObject(g->sp, scobject); // push window obj
122 ++g->sp; SetInt(g->sp, tag); // push tag
123 ++g->sp; SetFloat(g->sp, controlValue);
124 if (cstring) {
125 PyrString *scstring = newPyrString(g->gc, cstring, 0, false);
126 ++g->sp; SetObject(g->sp, scstring); // push tag
127 } else {
128 ++g->sp; SetNil(g->sp); // push tag
130 runInterpreter(g, s_doaction, 4);
131 g->canCallOS = false;
133 pthread_mutex_unlock (&gLangMutex);
137 @end