/
SCCocoaView.M
3075 lines (2655 loc) · 88 KB
/
SCCocoaView.M
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
SCCocoaView.M
Created by falkenst on Tue Feb 08 2005.
Copyright (c) 2005 jan truetzschler. All rights reserved.
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
http://www.audiosynth.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Cocoa/Cocoa.h>
#include <Carbon/Carbon.h>
#include <pthread.h>
#include "PyrPrimitive.h"
#include "PyrObject.h"
#include "PyrKernel.h"
#include "GC.h"
#include "VMGlobals.h"
#include "SC_RGen.h"
#include "SC_BoundsMacros.h"
#include "SC_InlineBinaryOp.h"
#include "PyrListPrim.h"
#include "SCCocoaView.h"
#include "QuickTime/Movies.h"
#import "HTMLRenderer.h"
#import "MyDocument.h"
#import "SCImage.h"
extern pthread_mutex_t gLangMutex;
extern bool compiledOK;
// SCImage support
extern PyrSymbol *s_scimage; // class symbol
extern PyrObject* newPyrSCImage(VMGlobals* g); // class creation func
void SyntaxColorize(NSTextView* textView);
static NSString *sSCObjType = @"SuperCollider object address";
extern PyrSymbol *s_scview;
@implementation SCCocoaTextViewResponder
- (struct PyrObject*)getSCObject
{
return mSCViewObject->GetSCObj();
}
- (void)textDidEndEditing:(NSNotification *)aNotification
{
// post("endEditing");
}
- (void)textDidBeginEditing:(NSNotification *)aNotification
{
}
- (void)setSCView: (struct SCCocoaTextView*)inObject
{
mSCViewObject = inObject;
}
- (IBAction) executeSelection: (id) sender
{
if(enterExecutesSelection)
[self sendSelection: "interpretPrintCmdLine" ];
}
- (void)sendSelection: (char*) methodName
{
if (!compiledOK) {
return;
}
NSRange selectedRange;
SCTextView * txtView = mSCViewObject->getTextView();
NSString* selection = [txtView currentlySelectedTextOrLine: &selectedRange];
const char *text = [selection UTF8String];
int textlength = strlen(text);
[[SCVirtualMachine sharedInstance] setCmdLine: text length: textlength];
NSRange newSelectedRange = NSMakeRange(selectedRange.location + selectedRange.length, 0);
[txtView setSelectedRange: newSelectedRange];
pthread_mutex_lock(&gLangMutex);
runLibrary(getsym(methodName));
pthread_mutex_unlock(&gLangMutex);
}
//- (void) keyDown: (NSEvent*) event
//- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRanges:(NSArray *)affectedRanges replacementStrings:(NSArray *)characters
- (BOOL) handleKeyDown: (NSEvent*) event;
{
// for some reason modifiers becomes 256 on my machine with no keys pressed. So need to mask against known keys.
if(usesTabToFocusNextView && ([event type] == NSKeyDown)){
NSString *characters = [event characters];
unsigned int modifiers = [event modifierFlags];
uint32 allKnownModifiers = NSAlphaShiftKeyMask | NSShiftKeyMask | NSControlKeyMask | NSCommandKeyMask
| NSAlternateKeyMask | NSHelpKeyMask | NSFunctionKeyMask;
unichar character = 0;
if([characters length] > 0) {
character = [characters characterAtIndex: 0];
}
if(character == 9 && ((modifiers & allKnownModifiers) == 0)) {
mSCViewObject->tabPrevFocus();
return YES;
} else if (character == 25 && ((modifiers & allKnownModifiers) == NSShiftKeyMask)) { // check above for tab
//NSLog(@"backtab");
/////[mSCViewObject->getTextView() resignFirstResponder];
mSCViewObject->tabNextFocus();
return YES;
} // other tab keys avail for user
}
return NO;
}
//- (BOOL) handleKeyDown: (NSEvent*) event;
//{
// //NSLog(@"keyDown");
// // for some reason modifiers becomes 256 on my machine with no keys pressed. So need to mask against known keys.
// if(usesTabToFocusNextView){
// NSString *characters = [event characters];
// unsigned int modifiers = [event modifierFlags];
// uint32 allKnownModifiers = NSAlphaShiftKeyMask | NSShiftKeyMask | NSControlKeyMask | NSCommandKeyMask
// | NSAlternateKeyMask | NSHelpKeyMask | NSFunctionKeyMask;
// unichar character = 0;
// if([characters length] > 0) {
// character = [characters characterAtIndex: 0];
// //NSLog(@"char %i", character);
// }
// if (character == 25 && ((modifiers & allKnownModifiers) == NSShiftKeyMask)) { // check above for tab
// //NSLog(@"backtab");
// /////[mSCViewObject->getTextView() resignFirstResponder];
// mSCViewObject->tabNextFocus();
// return YES;
// } // other tab keys avail for user
// }
// return NO;
//}
- (void) keyUp: (NSEvent*) event
{
}
- (void) mouseDown: (NSEvent*) event
{
mSCViewObject->makeFocus(true);
}
- (void) setUsesTabToFocusNextView: (BOOL) flag
{
usesTabToFocusNextView = flag;
}
- (void) setEnterExecutesSelection: (BOOL) flag
{
enterExecutesSelection = flag;
}
- (BOOL) textView: (NSTextView *) textView
clickedOnLink: (id) link
atIndex: (unsigned) charIndex
{
bool loadLinkInView = mSCViewObject->getLoadLinkInView();
NSDocumentController* docctl = [NSDocumentController sharedDocumentController];
if (!docctl && !loadLinkInView) return YES;
NSURL *desiredURL;
// is it a NSURL link or a NSString link?
if ([link isKindOfClass: [NSString class]])
{
if([link hasPrefix:@"SC://"] || [link hasPrefix:@"sc://"]) { // this means search immediately
NSString *helpPath = pathOfHelpFileFor([[[link substringFromIndex:5] stringByDeletingPathExtension] stringByDeletingPathExtension]);
if(!helpPath) {
post("WARNING:\nInvalid hyperlink: '%s' Please repair this.\n", [link cString]);
return YES;
}
desiredURL = [NSURL URLWithString: helpPath];
} else desiredURL = [NSURL URLWithString: [link stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding] relativeToURL: mSCViewObject->getLastURL()];
} else if ([link isKindOfClass: [NSURL class]])
{
// check for schemes which we'll handle
if([[link scheme] isEqualToString: @"SC"] || [[link scheme] isEqualToString:@"sc"]) { // this means search immediately
NSString *helpPath = pathOfHelpFileFor([[[[link relativeString] substringFromIndex:5] stringByDeletingPathExtension] stringByDeletingPathExtension]);
if(!helpPath) {
post("WARNING:\nInvalid hyperlink: '%s' Please repair this.\n", [[link relativeString] cString]);
return YES;
}
desiredURL = [NSURL fileURLWithPath: helpPath];
} else if (![link scheme]) { // NULL could be a regular file link that's been edited
if([[link relativePath] hasPrefix: @"/"]) {
desiredURL = [NSURL fileURLWithPath: [link relativeString]];
} else {
desiredURL = [NSURL URLWithString: [[link relativeString] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding] relativeToURL: mSCViewObject->getLastURL()];
}
} else if(![[link scheme] isEqualToString: @"file"]) {
return NO; // it's http, etc. so pass it on to Safari or whatever
} else {
desiredURL = link; // it's a regular file:// URL
}
} else return NO;
// try the link action first then use default
if(mSCViewObject->linkAction([desiredURL absoluteString])) return YES;
MyDocument *doc = nil;
int result = 0;
if([[NSFileManager defaultManager] fileExistsAtPath: [desiredURL path]]) {
if(!loadLinkInView) doc = (MyDocument*)[docctl documentForURL: [desiredURL absoluteURL]];
} else NSLog(@"file doesn't exist at path");
if (!doc) {
if(!loadLinkInView) {
doc = [docctl openDocumentWithContentsOfURL: desiredURL display: true];
} else {
result = mSCViewObject->open([desiredURL path]);
}
if (!doc && result) {
// it's a bad file:// URL, post a warning and search
post("WARNING:\nInvalid hyperlink: '%s' Please repair this.\nSearching help directories for alternative.\n", [[desiredURL path] cString]);
// delete extension twice in case something.help.rtf
if(!desiredURL) NSLog(@"foo");
NSLog(@"url path %@", [desiredURL path]);
NSString *desiredHelpName = [[[[desiredURL path] lastPathComponent] stringByDeletingPathExtension] stringByDeletingPathExtension];
NSString *helpPath = pathOfHelpFileFor(desiredHelpName);
if(!helpPath) {
post("Can't find Help File Document for: '%s'\n", [desiredHelpName cString]);
return YES;
}
desiredURL = [NSURL fileURLWithPath: helpPath];
if([[NSFileManager defaultManager] fileExistsAtPath: [desiredURL path]]) {
if(!loadLinkInView) {
doc = (MyDocument*)[docctl documentForURL: [desiredURL absoluteURL]];
}
} else post("file doesn't exist at path: '%s'\n", [[desiredURL path] cString]);
if (!doc) {
if(!loadLinkInView) {
doc = [docctl openDocumentWithContentsOfURL: desiredURL display: true];
} else {
result = mSCViewObject->open(helpPath);
}
if(!doc && result) {
post("Can't open Help File Document: '%s'\n", [[desiredURL path] cString]);
return YES;
}
}
}
}
if(!loadLinkInView) {
NSWindow *window = [[[doc windowControllers] objectAtIndex: 0] window];
if (!window) {
post("!! window controller returns nil ? failed to open help file window\n");
return YES;
}
[window makeKeyAndOrderFront: nil];
}
return YES;
}
- (void) loadHTMLToTextView:(NSURL *)url
{
if (!url) return;
SCTextView *textView = mSCViewObject->getTextView();
NSTextStorage* text = [textView textStorage];
[text beginEditing]; // Bracket with begin/end editing for efficiency
NSAttributedString *htmlAttributedString = [HTMLRenderer attributedStringWithURL:url];
if(htmlAttributedString)
{
[text setAttributedString:htmlAttributedString];
[textView setDefaultTabsTo: 28.0f];
}
[text endEditing];
[url release];
}
// for compatibility with Document
- (void)setActiveTextView:(SCTextView*)aTextView
{
}
@end
@implementation SCTextFieldResponder
//
//- (id)initWithFrame:(NSRect)frameRect
//{
// backGroundColor = [[NSColor whiteColor] retain];
// return [super initWithFrame:frameRect];
//}
//
//- (void)dealloc
//{
// [backGroundColor release];
// [super dealloc];
//}
//
//- (void)setBackgroundColor:(NSColor *)aColor
//{
// [aColor retain];
// [backGroundColor release];
// backGroundColor = aColor;
//}
//- (void)drawRect:(NSRect)aRect
//{
// // draw a background
// CGContextRef cgc = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
// CGContextSaveGState(cgc);
// CGContextSetRGBFillColor(cgc, 1.0, 0.0, 0.0, 0.5);
// CGContextFillRect(cgc, (*(CGRect *)&(aRect)));
// CGContextRestoreGState(cgc);
//
// [super drawRect:aRect]; // call superclass here for everything else
//}
- (struct PyrObject*)getSCObject
{
return mSCViewObject->GetSCObj();
}
//- (void)controlTextDidEndEditing:(NSNotification *)aNotification
//{
// if(textReallyChanged){
// pthread_mutex_lock (&gLangMutex);
// PyrSymbol *method = getsym("doAction");
// PyrObject *mObj;
// if (mObj = mSCViewObject->GetSCObj()) {
// VMGlobals *g = gMainVMGlobals;
// g->canCallOS = true;
// ++g->sp; SetObject(g->sp, mObj);
// runInterpreter(g, method, 1);
// g->canCallOS = false;
// }
// pthread_mutex_unlock (&gLangMutex);
// textReallyChanged = false;
// }
//}
//- (BOOL)textShouldBeginEditing:(NSText *)fieldEditor
//{
// post("foo\n");
// if(mSCViewObject->isFocus()) return YES;
//
// return NO;
//}
//- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
//{
// //[(SCFieldEditor*)fieldEditor setCurrentTextField:self];
// post("ctsbe\n");
// return YES;
//}
//
//- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
//{
// //[(SCFieldEditor*)fieldEditor setCurrentTextField:nil];
// return YES;
//}
//- (void)mouseDown:(NSEvent *)theEvent
//{
//
// mSCViewObject->makeFocus(true);
// unsigned int modifiers = [theEvent modifierFlags];
// //control tab/escape doesn't get passed here at all ?
// if(modifiers & NSCommandKeyMask) {
// [self selectText:self]; // exit editing and select all for a drag
// [self display];
// } else [super mouseDown:theEvent];
//}
//- (void)drawRect:(NSRect)aRect
//{
// [backGroundColor setFill];
// NSRectFill(aRect);
// [super drawRect:aRect];
//}
-(void)rightMouseDown:(NSEvent*)theEvent { [self mouseDown:theEvent]; }
-(void)otherMouseDown:(NSEvent*)theEvent { [self mouseDown:theEvent]; }
- (void)mouseDown:(NSEvent *)theEvent
{
//NSLog(@"SCGraphView MOUSEDOWN");
//[[self window] makeFirstResponder:self]; // there may be an active field editor
BOOL keepOn = YES;
//BOOL isInside = YES;
NSPoint mouseLoc;
//NSLog(@"Click count: %i", [theEvent clickCount]);
//if([theEvent clickCount] == 2) return;
//if (!mTopView) return;
unsigned int modifiers = [theEvent modifierFlags];
mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
SCPoint scpoint = SCMakePoint(mouseLoc.x, mouseLoc.y);
SCTopView *mTopView = mSCViewObject->getTop();
if (mSCViewObject) {
mDragStarted = NO;
bool mouseMoved = NO;
//mMenuView = 0;
mSCViewObject->makeFocus(true);
// force focus ring into drawing even if we're already in focus
SEL sel = @selector(setNeedsDisplay:);
NSMethodSignature *sig = [NSView instanceMethodSignatureForSelector: sel];
NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
[anInvocation setTarget: [self superview]];
[anInvocation setSelector: sel];
BOOL flag = YES;
[anInvocation setArgument: &flag atIndex: 2];
[scvm defer: anInvocation];
[self setEditingInactive:NO]; // in this way we know that editing was started by a mouse click rather than a key down
bool constructionmode = mTopView->ConstructionMode();
if(!constructionmode)
{
mSCViewObject->mouseDownAction(scpoint, modifiers,theEvent);
mSCViewObject->mouseBeginTrack(scpoint, modifiers,theEvent);
}else
mSCViewObject->setConstructionModeFromPoint(scpoint);
[self displayIfNeeded];
while (keepOn && !mDragStarted) {
theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask |NSRightMouseUp | NSOtherMouseUp |
NSLeftMouseDraggedMask | NSRightMouseDragged | NSOtherMouseDragged
| NSKeyDownMask | NSKeyUpMask
];
modifiers = [theEvent modifierFlags]; // added
mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
//isInside = [self mouse:mouseLoc inRect:[self bounds]];
scpoint = SCMakePoint(mouseLoc.x, mouseLoc.y);
int evtype = [theEvent type];
switch ([theEvent type]) {
case NSLeftMouseDown:
case NSRightMouseDown:
if(constructionmode)
{
mSCViewObject->doConstructionMove(scpoint);
mTopView->refresh();
}else
mSCViewObject->mouseDownAction(scpoint, modifiers,theEvent);
// post("down \n");
break;
case NSLeftMouseDragged:
case NSRightMouseDragged:
case NSOtherMouseDragged:
if(constructionmode)
{
mSCViewObject->doConstructionMove(scpoint);
mTopView->refresh();
}else
mSCViewObject->mouseTrack(scpoint, modifiers,theEvent);
mSCViewObject->mouseMoveAction(scpoint, modifiers,theEvent);
mouseMoved = YES;
// post("drag \n");
break;
case NSLeftMouseUp:
case NSRightMouseUp:
case NSOtherMouseUp:
if(constructionmode)
{
// view->doConstructionMove(scpoint);
mTopView->refresh();
}else
{
if(mouseMoved) { // check if this was just a mouse down or also a drag; if the latter end editing
[[self window] endEditingFor:self];
[self setEditingInactive:YES];
//[[self window] makeFirstResponder:[self superview]];
// trick focus ring into drawing
SEL sel = @selector(setNeedsDisplay:);
NSMethodSignature *sig = [NSView instanceMethodSignatureForSelector: sel];
NSInvocation *anInvocation = [NSInvocation invocationWithMethodSignature: sig];
SCVirtualMachine* scvm = [SCVirtualMachine sharedInstance];
[anInvocation setTarget: [self superview]];
[anInvocation setSelector: sel];
BOOL flag = YES;
[anInvocation setArgument: &flag atIndex: 2];
[scvm defer: anInvocation];
}
// if(!view.GetSCObj()) break;
mSCViewObject->mouseUpAction(scpoint, modifiers,theEvent);
mSCViewObject->mouseEndTrack(scpoint, modifiers,theEvent);
}
keepOn = NO;
break;
case NSKeyDown:
if(!constructionmode)
{
[self keyDown:theEvent];
}
break;
case NSKeyUp:
if(!constructionmode)
{
[self keyUp:theEvent];
}
break;
default:
post("evtype %d %4.4s\n", evtype, (char*)&evtype);
/* Ignore any other kind of event. */
break;
}
// display:
[self displayIfNeeded];
flushPostBuf();
}
}
//mMenuView = 0;
return;
}
-(void)mouseMoved:(NSEvent*)theEvent
{
NSPoint mouseLoc;
unsigned int modifiers = [theEvent modifierFlags];
mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
SCPoint scpoint = SCMakePoint(mouseLoc.x, mouseLoc.y);
if (mSCViewObject) {
mDragStarted = NO;
// view->makeFocus(true);
mSCViewObject->mouseOver(scpoint, modifiers, theEvent);
}
}
const int circDiam = 20;
- (NSImage*) makeDragImage: (PyrSlot*)slot label: (NSString*)label
{
if (!slot) return 0;
NSString *nsstring;
if(label) {
nsstring = label;
} else if (slot) {
PyrClass *classobj = classOfSlot(slot);
nsstring = [NSString stringWithCString: slotRawSymbol(&classobj->name)->name];
if (!nsstring) return 0;
} else {
nsstring = @"No Data!";
}
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSFont *font = [NSFont fontWithName: @"Helvetica" size: 12];
if (!font) return 0;
[dict setObject: font forKey: NSFontAttributeName ];
NSSize strSize = [nsstring sizeWithAttributes: dict];
NSRect strRect = NSMakeRect(circDiam, 0, circDiam + strSize.width, strSize.height);
NSSize size = NSMakeSize(circDiam+strSize.width, sc_max(circDiam, strSize.height));
NSImage *image = [[NSImage alloc] initWithSize: size];
if (!image) return 0;
[image autorelease];
float alpha = 0.6;
NSColor *colorClear = [NSColor colorWithCalibratedRed: 0
green: 0
blue: 0
alpha: 0];
NSColor *colorTransBlack = [NSColor colorWithCalibratedRed: 0
green: 0
blue: 0
alpha: alpha];
NSColor *colorTransBlue = [NSColor colorWithCalibratedRed: 0
green: 0
blue: 1
alpha: alpha];
/*NSColor *colorTransLtBlue = [NSColor colorWithCalibratedRed: 0.8
green: 0.8
blue: 1
alpha: alpha];*/
NSColor *colorTransWhite = [NSColor colorWithCalibratedRed: 1
green: 1
blue: 1
alpha: alpha];
NSColor *colorCaptionBackgnd = [NSColor colorWithCalibratedRed: 0
green: 0
blue: 0
alpha: 0.4];
NSColor *colorWhite = [NSColor colorWithCalibratedRed: 1
green: 1
blue: 1
alpha: 1];
[dict setObject: colorWhite forKey: NSForegroundColorAttributeName ];
[image lockFocus];
[colorClear set];
[NSBezierPath fillRect: NSMakeRect(0,0,size.width,size.height)];
NSBezierPath *path = [NSBezierPath bezierPathWithOvalInRect: NSMakeRect(1,1,circDiam-2,circDiam-2)];
[path setLineWidth: 1.5];
[colorTransBlue set];
[path fill];
NSBezierPath *hilite = [NSBezierPath bezierPathWithOvalInRect:
NSMakeRect(circDiam*0.3, circDiam*0.7, circDiam*0.4, circDiam*0.15)];
[colorTransWhite set];
[hilite fill];
[colorTransBlack set];
[path stroke];
[colorCaptionBackgnd set];
[NSBezierPath fillRect: strRect];
[nsstring drawInRect: strRect withAttributes: dict];
[image unlockFocus];
return image;
}
- (void) beginDragFrom: (NSPoint)where of: (PyrSlot*)slot string:(NSString*) string label:(NSString*) label
{
NSImage *image = [self makeDragImage: slot label: label];
NSPasteboard *pboard = [NSPasteboard pasteboardWithName: NSDragPboard];
[pboard declareTypes: [NSArray arrayWithObjects: sSCObjType, NSStringPboardType, nil] owner: self];
int fakeData;
NSData *data = [NSData dataWithBytes: &fakeData length: sizeof(int)];
[pboard setData: data forType: sSCObjType];
[pboard setString: string forType: NSStringPboardType];
NSSize imageSize = [image size];
where.x -= circDiam / 2;
where.y += circDiam / 4;
NSSize dragOffset = NSMakeSize(0.0, 0.0);
mDragStarted = YES;
[self dragImage: image at: where offset: dragOffset event: [NSApp currentEvent]
pasteboard: pboard source: self slideBack: YES];
}
//- (void)mouseDragged:(NSEvent *)theEvent
//{
// NSSize dragOffset = NSMakeSize(0.0, 0.0);
// NSPasteboard *pboard;
//
// pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
// [pboard declareTypes:[NSArray arrayWithObjects:sSCObjType, NSStringPboardType, nil] owner:self];
//
// int fakeData;
// NSData *data = [NSData dataWithBytes: &fakeData length: sizeof(int)];
//
// [pboard setData: data forType: sSCObjType];
// [pboard setString: [self stringValue] forType: NSStringPboardType];
//
// [self dragImage:[[[self window] fieldEditor:NO forObject:self] dragImageForSelectionWithEvent:NSMakePoint(0.0, 0.0)] at:[self imageLocation] offset:dragOffset
// event:theEvent pasteboard:pboard source:self slideBack:YES];
//
// return;
//}
- (void)setSCView: (struct SCTextField*)inObject
{
mSCViewObject = inObject;
}
-(BOOL) acceptsFirstResponder
{
return mAcceptsFirstResponder;
}
-(void) setAcceptsFirstResponder: (BOOL) flag
{
mAcceptsFirstResponder = flag;
}
//
// TEST: For tab-index
//- (BOOL)becomeFirstResponder
//{
// if(!mSCViewObject->isFocus()){
// [self resignFirstResponder];
// [mSCViewObject->getTop()->focusView()->focusResponder() becomeFirstResponder];
// return NO;
// } else return [super becomeFirstResponder];
//}
//-(BOOL) acceptsFirstResponder
//{
// return YES;
//}
//
//- (BOOL)resignFirstResponder
//{
// post("resignFirstResponder\n");
// [[self window] endEditingFor:self];
// return YES;
//}
// this for keyDowns. KeyUps come in keyUp below.
- (BOOL)textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
{
//post("sctir\n");
NSEvent* event = [NSApp currentEvent];
NSString *characters = [event characters];
unsigned int modifiers = [event modifierFlags];
unichar character = 0;
if([characters length] > 0) {
character = [characters characterAtIndex: 0];
}
if ([event keyCode] == 53){ //escape key breaks from modal or fullscreen windows
[[self window] keyDown:event];
}
mSCViewObject->keyDown(character, modifiers,[event keyCode]);
return YES;
}
// handle tabs and enter
- (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)aSelector
{
//post("dcbs\n");
//NSLog(@"sel: %@", NSStringFromSelector(aSelector));
if (aSelector == @selector(insertTab:)){
mSCViewObject->tabPrevFocus();
return YES;
} else if (aSelector == @selector(insertBacktab:)){
mSCViewObject->tabNextFocus();
return YES;
} else if (aSelector == @selector(insertNewline:)){
[[self window] endEditingFor:self];
// fire the action
pthread_mutex_lock (&gLangMutex);
PyrSymbol *method = getsym("doAction");
PyrObject *mObj;
if (mObj = mSCViewObject->GetSCObj()) {
VMGlobals *g = gMainVMGlobals;
g->canCallOS = true;
++g->sp; SetObject(g->sp, mObj);
runInterpreter(g, method, 1);
g->canCallOS = false;
}
pthread_mutex_unlock (&gLangMutex);
//NSLog(@"sv: %@", [self superview]);
[self setEditingInactive:YES];
[[self window] makeFirstResponder:[self superview]];
//NSLog(@"firstresp: %@", [[self window] firstResponder]);
return NO; // this will let the field editor end editing
} else if (aSelector == @selector(moveUp:) || aSelector == @selector(moveDown:)){ // stop editing and forward key event (for numbox)
NSEvent* event = [NSApp currentEvent];
NSString *characters = [event characters];
unsigned int modifiers = [event modifierFlags];
unichar character = 0;
if([characters length] > 0) {
character = [characters characterAtIndex: 0];
}
[[self window] endEditingFor:self];
[self setEditingInactive:YES];
[[self window] makeFirstResponder:[self superview]];
mSCViewObject->keyDown(character, modifiers,[event keyCode]);
return YES;
}
return NO;
}
- (NSDragOperation)draggingEntered:(id < NSDraggingInfo >)dragInfo {
NSPasteboard* pboard = [dragInfo draggingPasteboard];
if ([[pboard types] containsObject: sSCObjType]) {
return mSCViewObject->draggingEntered();
} else if ([[pboard types] containsObject: NSStringPboardType]) {
NSString *nsstring = [pboard stringForType: NSStringPboardType];
if (!nsstring) return NSDragOperationNone;
pthread_mutex_lock (&gLangMutex);
VMGlobals *g = gMainVMGlobals;
PyrString* pstrobj = newPyrString(g->gc, [nsstring UTF8String], 0, true);
int classVarIndex = slotRawInt(&getsym("SCView")->u.classobj->classVarIndex);
SetObject(&g->classvars->slots[classVarIndex+0], pstrobj);
g->gc->GCWrite(g->classvars, pstrobj);
//PyrSymbol *method = getsym("importDrag");
//g->canCallOS = true;
++g->sp; SetObject(g->sp, s_scview->u.classobj);
//runInterpreter(g, method, 1);
//g->canCallOS = false;
pthread_mutex_unlock (&gLangMutex);
int fakeData;
NSData *data = [NSData dataWithBytes: &fakeData length: sizeof(int)];
[pboard setData: data forType: sSCObjType];
} else if ([[pboard types] containsObject: NSFilenamesPboardType]) {
NSArray *files = [pboard propertyListForType: NSFilenamesPboardType];
if (!files) return NSDragOperationNone;
pthread_mutex_lock (&gLangMutex);
VMGlobals *g = gMainVMGlobals;
int size = [files count];
PyrObject* array = newPyrArray(g->gc, size, 0, true);
for (int i=0; i<size; ++i) {
NSString *path = [files objectAtIndex: i];
PyrString *string = newPyrString(g->gc, [path UTF8String], 0, true);
SetObject(array->slots + array->size, string);
array->size++;
g->gc->GCWrite(array, string);
}
int classVarIndex = slotRawInt(&getsym("SCView")->u.classobj->classVarIndex);
SetObject(&g->classvars->slots[classVarIndex+0], array);
g->gc->GCWrite(g->classvars, array);
pthread_mutex_unlock (&gLangMutex);
int fakeData;
NSData *data = [NSData dataWithBytes: &fakeData length: sizeof(int)];
[pboard setData: data forType: sSCObjType];
} else {
return NSDragOperationNone;
}
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
return mSCViewObject->performDrag();
}
- (void)keyUp:(NSEvent*)event
{
//post("doKeyUpAction\n");
NSString *characters = [event characters];
unsigned int modifiers = [event modifierFlags];
unichar character = 0;
if([characters length] > 0) {
character = [characters characterAtIndex: 0];
}
mSCViewObject->keyUp(character, modifiers,[event keyCode]);
}
void QDDrawBevelRect(CGContextRef cgc, CGRect bounds, float width, bool inout);
- (void)drawRect:(NSRect)aRect
{
[super drawRect:aRect];
CGContextRef cgc = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(cgc);
QDDrawBevelRect(cgc, (*(CGRect *)&([self bounds])), 1, true);
CGContextRestoreGState(cgc);
}
// doing this here keeps C++ classes much simpler
- (void) addNumberFormatter
{
[NSNumberFormatter setDefaultFormatterBehavior:NSNumberFormatterBehavior10_4];
mFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[mFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
[mFormatter setAlwaysShowsDecimalSeparator:NO];
[mFormatter setExponentSymbol:@"e"];
[mFormatter setMaximumFractionDigits:20];
[mFormatter setDecimalSeparator:@"."];
[mFormatter setUsesGroupingSeparator:NO];
[[self cell] setFormatter:mFormatter];
}
// with these two methods we can check in C++ keyDown methods to see if editing is active
// if not, we start it and replace the string in the field editor
- (void) setEditingInactive: (BOOL)flag
{
mEditingInactive = flag;
}
- (BOOL) editingInactive
{
return mEditingInactive;
}
@end
@implementation SCNSMenuItem
- (void)setSCObject: (struct PyrObject*)inObject
{
mMenuItemObj = inObject;
}
- (struct PyrObject*)getSCObject
{
return mMenuItemObj;
}
- (void)doAction: (id)sender
{
// post("doAction \n");
pthread_mutex_lock (&gLangMutex);
PyrObject * pobj = [self getSCObject];
if(compiledOK && pobj){
PyrSymbol *method = getsym("doAction");
VMGlobals *g = gMainVMGlobals;
g->canCallOS = true;
++g->sp; SetObject(g->sp, pobj);
++g->sp; SetInt(g->sp, 1);
runInterpreter(g, method, 2);
g->canCallOS = false;
}
pthread_mutex_unlock (&gLangMutex);
}
@end
@implementation SCNSLevelIndicator
- (void)drawRect:(NSRect)aRect
{
if(drawPeak) { // make warning and critical show for peak not value if peak is drawn
if(criticalAboveWarning) {
if(peakValue >= critical) {
[self setCriticalValue: value];
[self setWarningValue: value - 0.1];
} else if (peakValue >= warning) {
[self setWarningValue: value];
[self setCriticalValue: value + 0.1];
} else {
[self setWarningValue: 1.1];
[self setCriticalValue: 1.1];
}
} else {
if(peakValue <= critical) {
[self setCriticalValue: value];
[self setWarningValue: value + 0.1];
} else if (peakValue <= warning) {
[self setWarningValue: value];
[self setCriticalValue: value - 0.1];
} else {
[self setWarningValue: -0.1];
[self setCriticalValue: -0.1];
}
}
[super drawRect:aRect];
CGContextRef cgc = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(cgc);
CGContextSetRGBFillColor(cgc, 1.0, 1.0, 0.0, 0.6);
CGRect peakRect;
//peakRect = CGRectMake(peakLevel, aRect.origin.y + peakSubtract, 3.f, aRect.size.height - peakSubtract);
peakRect = CGRectMake(peakLevel, peakY, 3.f, peakHeight);
CGContextFillRect(cgc, peakRect);
CGContextRestoreGState(cgc);
} else {
[super drawRect:aRect];
}
}
- (id)initWithFrame:(NSRect)frameRect
{
if (![super initWithFrame:frameRect])
return nil;
drawPeak = NO;
isVertical = NO;
peakSubtract = 0.f;
peakLevel = 0.f;
value = peakValue = 0.0;
critical = warning = 1.1;
criticalAboveWarning = YES;
[self prepPeakBounds];
return self;
}
- (void)setFrame:(NSRect)frameRect
{
[super setFrame:frameRect];