/
nsviewframe.mm
1551 lines (1388 loc) · 54.2 KB
/
nsviewframe.mm
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
// This file is part of VSTGUI. It is subject to the license terms
// in the LICENSE file found in the top-level directory of this
// distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
#import "nsviewframe.h"
#if MAC_COCOA
#import "cocoahelpers.h"
#import "cocoatextedit.h"
#import "nsviewoptionmenu.h"
#import "cocoaopenglview.h"
#import "autoreleasepool.h"
#import "../macclipboard.h"
#import "../macfactory.h"
#import "../cgdrawcontext.h"
#import "../cgbitmap.h"
#import "../quartzgraphicspath.h"
#import "../caviewlayer.h"
#import "../../../cvstguitimer.h"
#import "../../common/genericoptionmenu.h"
#import "../../../cframe.h"
#if MAC_CARBON
#import "../carbon/hiviewframe.h"
#import <Carbon/Carbon.h>
#endif
#include <QuartzCore/QuartzCore.h>
#ifndef MAC_OS_X_VERSION_10_14
#define MAC_OS_X_VERSION_10_14 101400
#endif
using namespace VSTGUI;
#if DEBUG
//------------------------------------------------------------------------
@interface DebugRedrawAnimDelegate : NSObject<CAAnimationDelegate>
@property (retain, readwrite) CALayer* layer;
@end
@implementation DebugRedrawAnimDelegate
//------------------------------------------------------------------------
- (void)dealloc
{
self.layer = nil;
[super dealloc];
}
//------------------------------------------------------------------------
- (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag
{
if (flag)
{
[self.layer removeFromSuperlayer];
self.layer = nil;
}
}
@end
#endif // DEBUG
//------------------------------------------------------------------------------------
HIDDEN inline IPlatformFrameCallback* getFrame (id obj)
{
NSViewFrame* nsViewFrame = (NSViewFrame*)OBJC_GET_VALUE(obj, _nsViewFrame);
if (nsViewFrame)
return nsViewFrame->getFrame ();
return nullptr;
}
//------------------------------------------------------------------------------------
HIDDEN inline NSViewFrame* getNSViewFrame (id obj)
{
return (NSViewFrame*)OBJC_GET_VALUE(obj, _nsViewFrame);
}
static Class viewClass = nullptr;
//------------------------------------------------------------------------------------
@interface NSObject (VSTGUI_NSView)
- (id) initWithNSViewFrame: (NSViewFrame*) frame parent: (NSView*) parent andSize: (const CRect*) size;
- (BOOL) onMouseDown: (NSEvent*) event;
- (BOOL) onMouseUp: (NSEvent*) event;
- (BOOL) onMouseMoved: (NSEvent*) event;
@end
//------------------------------------------------------------------------------------
static void mapModifiers (NSUInteger nsEventModifiers, CButtonState& buttonState)
{
if (nsEventModifiers & MacEventModifier::ShiftKeyMask)
buttonState |= kShift;
if (nsEventModifiers & MacEventModifier::CommandKeyMask)
buttonState |= kControl;
if (nsEventModifiers & MacEventModifier::AlternateKeyMask)
buttonState |= kAlt;
if (nsEventModifiers & MacEventModifier::ControlKeyMask)
buttonState |= kApple;
}
//------------------------------------------------------------------------------------
static bool nsViewGetCurrentMouseLocation (void* nsView, CPoint& where)
{
NSView* view = (NSView*)nsView;
NSPoint p = [[view window] mouseLocationOutsideOfEventStream];
p = [view convertPoint:p fromView:nil];
where = pointFromNSPoint (p);
return true;
}
//------------------------------------------------------------------------------------
static id VSTGUI_NSView_Init (id self, SEL _cmd, void* _frame, NSView* parentView, const void* _size)
{
const CRect* size = (const CRect*)_size;
NSViewFrame* frame = (NSViewFrame*)_frame;
NSRect nsSize = nsRectFromCRect (*size);
__OBJC_SUPER(self)
self = SuperInitWithFrame (SUPER, @selector(initWithFrame:), nsSize); // self = [super initWithFrame: nsSize];
if (self)
{
OBJC_SET_VALUE(self, _nsViewFrame, frame); // _vstguiframe = frame;
[parentView addSubview: self];
[self
registerForDraggedTypes:
[NSArray
arrayWithObjects:NSPasteboardTypeString,
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_14
NSPasteboardTypeFileURL,
#else
NSFilenamesPboardType,
#endif
NSPasteboardTypeColor,
[NSString
stringWithCString:MacClipboard::getPasteboardBinaryType ()
encoding:NSASCIIStringEncoding],
nil]];
[self setFocusRingType:NSFocusRingTypeNone];
}
return self;
}
//------------------------------------------------------------------------------------
static BOOL VSTGUI_NSView_isFlipped (id self, SEL _cmd) { return YES; }
static BOOL VSTGUI_NSView_acceptsFirstResponder (id self, SEL _cmd) { return YES; }
static BOOL VSTGUI_NSView_canBecomeKeyView (id self, SEL _cmd) { return YES; }
static BOOL VSTGUI_NSView_wantsDefaultClipping (id self, SEL _cmd) { return NO; }
static NSFocusRingType VSTGUI_NSView_focusRingType (id self) { return NSFocusRingTypeNone; }
static BOOL VSTGUI_NSView_shouldBeTreatedAsInkEvent (id self, SEL _cmd, NSEvent *event) { return NO; }
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_makeSubViewFirstResponder (id self, SEL _cmd, NSResponder* newFirstResponder)
{
NSViewFrame* nsFrame = getNSViewFrame (self);
if (nsFrame)
{
nsFrame->setIgnoreNextResignFirstResponder (true);
[[self window] makeFirstResponder:newFirstResponder];
nsFrame->setIgnoreNextResignFirstResponder (false);
}
}
//------------------------------------------------------------------------------------
static BOOL VSTGUI_NSView_becomeFirstResponder (id self, SEL _cmd)
{
if ([[self window] isKeyWindow])
{
IPlatformFrameCallback* frame = getFrame (self);
if (frame)
frame->platformOnActivate (true);
}
return YES;
}
//------------------------------------------------------------------------------------
static BOOL VSTGUI_NSView_resignFirstResponder (id self, SEL _cmd)
{
NSView* firstResponder = (NSView*)[[self window] firstResponder];
if (![firstResponder isKindOfClass:[NSView class]])
firstResponder = nil;
if (firstResponder)
{
NSViewFrame* nsFrame = getNSViewFrame (self);
if (nsFrame && nsFrame->getIgnoreNextResignFirstResponder ())
{
while (firstResponder != self && firstResponder != nil)
firstResponder = [firstResponder superview];
if (firstResponder == self && [[self window] isKeyWindow])
{
return YES;
}
}
IPlatformFrameCallback* frame = getFrame (self);
if (frame)
frame->platformOnActivate (false);
}
return YES;
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_updateTrackingAreas (id self, SEL _cmd)
{
__OBJC_SUPER(self)
NSViewFrame* viewFrame = getNSViewFrame (self);
if (viewFrame)
viewFrame->initTrackingArea ();
SuperUpdateTrackingAreas (SUPER, @selector(updateTrackingAreas));
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_windowDidChangeBackingProperties (id self, SEL _cmd, NSNotification* notification)
{
double scaleFactor = 1.;
if (auto window = [self window])
scaleFactor = [window backingScaleFactor];
NSViewFrame* viewFrame = getNSViewFrame (self);
if (viewFrame)
viewFrame->scaleFactorChanged (scaleFactor);
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_viewDidMoveToWindow (id self, SEL _cmd)
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
NSWindow* window = [self window];
if (window)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowKeyStateChanged:) name:NSWindowDidBecomeKeyNotification object:window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowKeyStateChanged:) name:NSWindowDidResignKeyNotification object:window];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidChangeBackingProperties:) name:NSWindowDidChangeBackingPropertiesNotification object:window];
IPlatformFrameCallback* frame = getFrame (self);
if (frame)
frame->platformOnActivate ([window isKeyWindow] ? true : false);
VSTGUI_NSView_windowDidChangeBackingProperties (self, _cmd, nil);
}
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_windowKeyStateChanged (id self, SEL _cmd, NSNotification* notification)
{
IPlatformFrameCallback* frame = getFrame (self);
auto active = [[notification name] isEqualToString:NSWindowDidBecomeKeyNotification] ? true : false;
NSView* firstResponder = (NSView*)[[self window] firstResponder];
if (![firstResponder isKindOfClass:[NSView class]])
firstResponder = nil;
if (firstResponder)
{
while (firstResponder != self && firstResponder != nil)
firstResponder = [firstResponder superview];
if (firstResponder == self)
{
if (frame)
frame->platformOnActivate (active);
}
}
frame->platformOnWindowActivate (active);
}
//------------------------------------------------------------------------------------
static BOOL VSTGUI_NSView_isOpaque (id self, SEL _cmd)
{
return NO;
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_drawRect (id self, SEL _cmd, NSRect rect)
{
NSViewFrame* frame = getNSViewFrame (self);
if (frame)
frame->drawRect (&rect);
}
//------------------------------------------------------------------------
static void VSTGUI_NSView_viewWillDraw (id self, SEL _cmd)
{
if (@available (macOS 10.12, *))
{
if (auto layer = [self layer])
{
layer.contentsFormat = kCAContentsFormatRGBA8Uint;
}
}
__OBJC_SUPER (self)
SuperViewWillRedraw (SUPER, _cmd);
}
//------------------------------------------------------------------------
static void VSTGUI_NSView_setNeedsDisplayInRect (id self, SEL _cmd, NSRect rect)
{
NSViewFrame* frame = getNSViewFrame (self);
if (frame)
frame->setNeedsDisplayInRect (rect);
__OBJC_SUPER (self)
SuperSetNeedsDisplayInRect (SUPER, _cmd, rect);
}
//------------------------------------------------------------------------------------
static BOOL VSTGUI_NSView_onMouseDown (id self, SEL _cmd, NSEvent* theEvent)
{
NSViewFrame* frame = getNSViewFrame (self);
if (frame)
return frame->onMouseDown (theEvent) ? YES : NO;
return NO;
}
//------------------------------------------------------------------------------------
static BOOL VSTGUI_NSView_onMouseUp (id self, SEL _cmd, NSEvent* theEvent)
{
NSViewFrame* frame = getNSViewFrame (self);
if (frame)
return frame->onMouseUp (theEvent) ? YES : NO;
return NO;
}
//------------------------------------------------------------------------------------
static BOOL VSTGUI_NSView_onMouseMoved (id self, SEL _cmd, NSEvent* theEvent)
{
NSViewFrame* frame = getNSViewFrame (self);
if (frame)
return frame->onMouseMoved (theEvent) ? YES : NO;
return NO;
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_mouseDown (id self, SEL _cmd, NSEvent* theEvent)
{
if (![self onMouseDown: theEvent])
{
__OBJC_SUPER(self)
SuperEventMsg (SUPER, _cmd, theEvent);
}
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_mouseUp (id self, SEL _cmd, NSEvent* theEvent)
{
if (![self onMouseUp: theEvent])
{
__OBJC_SUPER(self)
SuperEventMsg (SUPER, _cmd, theEvent);
}
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_mouseMoved (id self, SEL _cmd, NSEvent* theEvent)
{
if (![self onMouseMoved: theEvent])
{
__OBJC_SUPER(self)
SuperEventMsg (SUPER, _cmd, theEvent);
}
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_scrollWheel (id self, SEL _cmd, NSEvent* theEvent)
{
IPlatformFrameCallback* _vstguiframe = getFrame (self);
if (!_vstguiframe)
return;
CButtonState buttons = 0;
NSUInteger modifiers = [theEvent modifierFlags];
NSPoint nsPoint = [theEvent locationInWindow];
nsPoint = [self convertPoint:nsPoint fromView:nil];
mapModifiers (modifiers, buttons);
auto distanceX = [theEvent scrollingDeltaX];
auto distanceY = [theEvent scrollingDeltaY];
if ([theEvent hasPreciseScrollingDeltas])
{
distanceX *= 0.1;
distanceY *= 0.1;
}
if ([theEvent isDirectionInvertedFromDevice])
{
distanceX *= -1;
distanceY *= -1;
buttons |= kMouseWheelInverted;
}
CPoint p = pointFromNSPoint (nsPoint);
if (distanceX != 0.)
_vstguiframe->platformOnMouseWheel (p, kMouseWheelAxisX, static_cast<float> (distanceX), buttons);
if (distanceY != 0.)
_vstguiframe->platformOnMouseWheel (p, kMouseWheelAxisY, static_cast<float> (distanceY), buttons);
}
//------------------------------------------------------------------------------------
static NSPoint getGlobalMouseLocation (NSView* view)
{
NSRect r = {};
r.origin = [NSEvent mouseLocation];
r = [[view window] convertRectFromScreen:r];
return [view convertPoint:r.origin fromView:nil];
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_mouseEntered (id self, SEL _cmd, NSEvent* theEvent)
{
IPlatformFrameCallback* _vstguiframe = getFrame (self);
if (!_vstguiframe)
return;
CButtonState buttons = 0; //eventButton (theEvent);
NSUInteger modifiers = [theEvent modifierFlags];
CPoint p = pointFromNSPoint (getGlobalMouseLocation (self));
mapModifiers (modifiers, buttons);
_vstguiframe->platformOnMouseMoved (p, buttons);
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_mouseExited (id self, SEL _cmd, NSEvent* theEvent)
{
IPlatformFrameCallback* _vstguiframe = getFrame (self);
if (!_vstguiframe)
return;
CButtonState buttons = 0; //eventButton (theEvent);
NSUInteger modifiers = [theEvent modifierFlags];
mapModifiers (modifiers, buttons);
CPoint p = pointFromNSPoint (getGlobalMouseLocation (self));
_vstguiframe->platformOnMouseExited (p, buttons);
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_cursorUpdate (id self, SEL _cmd, NSEvent* theEvent)
{
NSViewFrame* frame = getNSViewFrame(self);
if (frame)
{
frame->cursorUpdate ();
}
}
//------------------------------------------------------------------------------------
static BOOL VSTGUI_NSView_acceptsFirstMouse (id self, SEL _cmd, NSEvent* event)
{
return YES; // click through
}
//------------------------------------------------------------------------------------
static BOOL VSTGUI_NSView_performKeyEquivalent (id self, SEL _cmd, NSEvent* theEvent)
{
NSView* firstResponder = (NSView*)[[self window] firstResponder];
if (![firstResponder isKindOfClass:[NSView class]])
firstResponder = nil;
if (firstResponder)
{
while (firstResponder != self && firstResponder != nil)
firstResponder = [firstResponder superview];
if (firstResponder == self)
{
IPlatformFrameCallback* frame = getFrame (self);
if (frame)
{
VstKeyCode keyCode = CreateVstKeyCodeFromNSEvent (theEvent);
if (frame->platformOnKeyDown (keyCode))
return YES;
}
}
}
return NO;
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_keyDown (id self, SEL _cmd, NSEvent* theEvent)
{
IPlatformFrameCallback* _vstguiframe = getFrame (self);
if (!_vstguiframe)
return;
VstKeyCode keyCode = CreateVstKeyCodeFromNSEvent (theEvent);
bool res = _vstguiframe->platformOnKeyDown (keyCode);
if (!res&& keyCode.virt == VKEY_TAB)
{
if (keyCode.modifier & kShift)
[[self window] selectKeyViewPrecedingView:self];
else
[[self window] selectKeyViewFollowingView:self];
}
else if (!res)
[[self nextResponder] keyDown:theEvent];
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_keyUp (id self, SEL _cmd, NSEvent* theEvent)
{
IPlatformFrameCallback* _vstguiframe = getFrame (self);
if (!_vstguiframe)
return;
VstKeyCode keyCode = CreateVstKeyCodeFromNSEvent (theEvent);
bool res = _vstguiframe->platformOnKeyUp (keyCode);
if (!res)
[[self nextResponder] keyUp:theEvent];
}
//------------------------------------------------------------------------------------
static NSDragOperation VSTGUI_NSView_draggingEntered (id self, SEL _cmd, id sender)
{
NSViewFrame* frame = getNSViewFrame (self);
if (!frame)
return NSDragOperationNone;
NSPasteboard *pboard = [sender draggingPasteboard];
frame->setDragDataPackage (MacClipboard::createDragDataPackage (pboard));
CPoint where;
nsViewGetCurrentMouseLocation (self, where);
NSUInteger modifiers = [NSEvent modifierFlags];
CButtonState buttons = 0;
mapModifiers (modifiers, buttons);
DragEventData data {frame->getDragDataPackage (), where, buttons};
auto result = frame->getFrame ()->platformOnDragEnter (data);
if (result == DragOperation::Copy)
return NSDragOperationCopy;
if (result == DragOperation::Move)
return NSDragOperationMove;
return NSDragOperationGeneric;
}
//------------------------------------------------------------------------------------
static NSDragOperation VSTGUI_NSView_draggingUpdated (id self, SEL _cmd, id sender)
{
NSViewFrame* frame = getNSViewFrame (self);
if (!frame)
return NSDragOperationNone;
CPoint where;
nsViewGetCurrentMouseLocation (self, where);
NSUInteger modifiers = [NSEvent modifierFlags];
CButtonState buttons = 0;
mapModifiers (modifiers, buttons);
DragEventData data {frame->getDragDataPackage (), where, buttons};
auto result = frame->getFrame ()->platformOnDragMove (data);
if (result == DragOperation::Copy)
return NSDragOperationCopy;
if (result == DragOperation::Move)
return NSDragOperationMove;
return NSDragOperationNone;
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_draggingExited (id self, SEL _cmd, id sender)
{
NSViewFrame* frame = getNSViewFrame (self);
if (!frame || !frame->getDragDataPackage ())
return;
CPoint where;
nsViewGetCurrentMouseLocation (self, where);
NSUInteger modifiers = [NSEvent modifierFlags];
CButtonState buttons = 0;
mapModifiers (modifiers, buttons);
DragEventData data {frame->getDragDataPackage (), where, buttons};
frame->getFrame ()->platformOnDragLeave (data);
frame->setDragDataPackage (nullptr);
[[NSCursor arrowCursor] set]; // we may should remember the cursor via [NSCursor currentCursor]
}
//------------------------------------------------------------------------------------
static BOOL VSTGUI_NSView_performDragOperation (id self, SEL _cmd, id sender)
{
NSViewFrame* frame = getNSViewFrame (self);
if (!frame || !frame->getDragDataPackage ())
return NO;
CPoint where;
nsViewGetCurrentMouseLocation (self, where);
NSUInteger modifiers = [NSEvent modifierFlags];
CButtonState buttons = 0;
mapModifiers (modifiers, buttons);
DragEventData data {frame->getDragDataPackage (), where, buttons};
bool result = frame->getFrame ()->platformOnDrop (data);
frame->setMouseCursor (kCursorDefault);
frame->setDragDataPackage (nullptr);
return result;
}
//------------------------------------------------------------------------------------
static NSDragOperation VSTGUI_NSView_draggingSessionSourceOperationMaskForDraggingContext (id self, SEL _cmd, NSDraggingSession* session, NSDraggingContext context)
{
if (context == NSDraggingContextOutsideApplication)
{
if (auto dataPackage = MacClipboard::createDragDataPackage (session.draggingPasteboard))
{
for (auto index = 0u, count = dataPackage->getCount (); index < count; ++index)
{
if (dataPackage->getDataType (index) == IDataPackage::kBinary)
return NSDragOperationPrivate;
}
}
return NSDragOperationCopy;
}
return NSDragOperationGeneric;
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_draggingSessionWillBeginAtPoint (id self, SEL _cmd, NSDraggingSession* session, NSPoint position)
{
NSViewFrame* frame = getNSViewFrame (self);
if (!frame)
return;
if (auto dragSession = frame->getDraggingSession ())
{
auto r = [[self window] convertRectFromScreen:{position, NSMakeSize (0, 0)}];
auto pos = pointFromNSPoint ([self convertPoint:r.origin fromView:nil]);
dragSession->dragWillBegin (pos);
}
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_draggingSessionMovedToPoint (id self, SEL _cmd, NSDraggingSession* session, NSPoint position)
{
NSViewFrame* frame = getNSViewFrame (self);
if (!frame)
return;
if (auto dragSession = frame->getDraggingSession ())
{
auto r = [[self window] convertRectFromScreen:{position, NSMakeSize (0, 0)}];
auto pos = pointFromNSPoint ([self convertPoint:r.origin fromView:nil]);
dragSession->dragMoved (pos);
}
}
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_draggingSessionEndedAtPoint (id self, SEL _cmd, NSDraggingSession* session, NSPoint position, NSDragOperation operation)
{
NSViewFrame* frame = getNSViewFrame (self);
if (!frame)
return;
if (auto dragSession = frame->getDraggingSession ())
{
DragOperation result;
switch (operation)
{
case NSDragOperationNone: result = DragOperation::None; break;
case NSDragOperationMove: result = DragOperation::Move; break;
default: result = DragOperation::Copy; break;
}
auto r = [[self window] convertRectFromScreen:{position, NSMakeSize (0, 0)}];
auto pos = pointFromNSPoint ([self convertPoint:r.origin fromView:nil]);
dragSession->dragEnded (pos, result);
frame->clearDraggingSession ();
}
}
#if VSTGUI_ENABLE_DEPRECATED_METHODS
//------------------------------------------------------------------------------------
static void VSTGUI_NSView_draggedImageEndedAtOperation (id self, SEL _cmd, NSImage* image, NSPoint aPoint, NSDragOperation operation)
{
NSViewFrame* frame = getNSViewFrame (self);
if (frame)
{
if (operation == NSDragOperationNone)
{
frame->setLastDragOperationResult (kDragRefused);
}
else if (operation == NSDragOperationMove)
{
frame->setLastDragOperationResult (kDragMoved);
}
else
frame->setLastDragOperationResult (kDragCopied);
}
}
#endif
//------------------------------------------------------------------------------------
static id VSTGUI_NSView_makeTouchbar (id self)
{
NSViewFrame* frame = getNSViewFrame (self);
if (frame)
return reinterpret_cast<id> (frame->makeTouchBar ());
return nil;
}
namespace VSTGUI {
//------------------------------------------------------------------------------------
class CocoaTooltipWindow : public NonAtomicReferenceCounted
{
public:
CocoaTooltipWindow () = default;
~CocoaTooltipWindow () noexcept override;
void set (NSViewFrame* nsViewFrame, const CRect& rect, const char* tooltip);
void hide ();
void onTimer ();
protected:
SharedPointer<CVSTGUITimer> timer;
CFrame* frame {nullptr};
NSWindow* window {nullptr};
NSTextField* textfield {nullptr};
};
//-----------------------------------------------------------------------------
__attribute__((__destructor__)) static void cleanup_VSTGUI_NSView ()
{
if (viewClass)
objc_disposeClassPair (viewClass);
}
//-----------------------------------------------------------------------------
void NSViewFrame::initClass ()
{
if (viewClass == nullptr)
{
AutoreleasePool ap;
const char* nsUIntegerEncoded = @encode(NSUInteger);
const char* nsRectEncoded = @encode(NSRect);
char funcSig[100];
NSMutableString* viewClassName = [[[NSMutableString alloc] initWithString:@"VSTGUI_NSView"] autorelease];
viewClass = generateUniqueClass (viewClassName, [NSView class]);
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(initWithNSViewFrame:parent:andSize:), IMP (VSTGUI_NSView_Init), "@@:@:^:^:^:"))
// VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(dealloc), IMP (VSTGUI_NSView_Dealloc), "v@:@:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(updateTrackingAreas), IMP (VSTGUI_NSView_updateTrackingAreas), "v@:@:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(viewDidMoveToWindow), IMP (VSTGUI_NSView_viewDidMoveToWindow), "v@:@:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(windowKeyStateChanged:), IMP (VSTGUI_NSView_windowKeyStateChanged), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(windowDidChangeBackingProperties:), IMP (VSTGUI_NSView_windowDidChangeBackingProperties), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(isFlipped), IMP (VSTGUI_NSView_isFlipped), "B@:@:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(acceptsFirstResponder), IMP (VSTGUI_NSView_acceptsFirstResponder), "B@:@:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(becomeFirstResponder), IMP (VSTGUI_NSView_becomeFirstResponder), "B@:@:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(resignFirstResponder), IMP (VSTGUI_NSView_resignFirstResponder), "B@:@:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(canBecomeKeyView), IMP (VSTGUI_NSView_canBecomeKeyView), "B@:@:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(wantsDefaultClipping), IMP (VSTGUI_NSView_wantsDefaultClipping), "B@:@:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(isOpaque), IMP (VSTGUI_NSView_isOpaque), "B@:@:"))
sprintf (funcSig, "v@:@:%s:", nsRectEncoded);
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(drawRect:), IMP (VSTGUI_NSView_drawRect), funcSig))
VSTGUI_CHECK_YES (class_addMethod (viewClass, @selector (setNeedsDisplayInRect:),
IMP (VSTGUI_NSView_setNeedsDisplayInRect), funcSig))
VSTGUI_CHECK_YES (class_addMethod (viewClass, @selector (viewWillDraw),
IMP (VSTGUI_NSView_viewWillDraw), "v@:@:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(shouldBeTreatedAsInkEvent:), IMP(VSTGUI_NSView_shouldBeTreatedAsInkEvent), "B@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(onMouseDown:), IMP (VSTGUI_NSView_onMouseDown), "B@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(onMouseUp:), IMP (VSTGUI_NSView_onMouseUp), "B@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(onMouseMoved:), IMP (VSTGUI_NSView_onMouseMoved), "B@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(mouseDown:), IMP (VSTGUI_NSView_mouseDown), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(rightMouseDown:), IMP (VSTGUI_NSView_mouseDown), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(otherMouseDown:), IMP (VSTGUI_NSView_mouseDown), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(mouseUp:), IMP (VSTGUI_NSView_mouseUp), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(rightMouseUp:), IMP (VSTGUI_NSView_mouseUp), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(otherMouseUp:), IMP (VSTGUI_NSView_mouseUp), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(mouseMoved:), IMP (VSTGUI_NSView_mouseMoved), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(mouseDragged:), IMP (VSTGUI_NSView_mouseMoved), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(rightMouseDragged:), IMP (VSTGUI_NSView_mouseMoved), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(otherMouseDragged:), IMP (VSTGUI_NSView_mouseMoved), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(scrollWheel:), IMP (VSTGUI_NSView_scrollWheel), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(mouseEntered:), IMP (VSTGUI_NSView_mouseEntered), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(mouseExited:), IMP (VSTGUI_NSView_mouseExited), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(cursorUpdate:), IMP (VSTGUI_NSView_cursorUpdate), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(acceptsFirstMouse:), IMP (VSTGUI_NSView_acceptsFirstMouse), "B@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(performKeyEquivalent:), IMP (VSTGUI_NSView_performKeyEquivalent), "B@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(keyDown:), IMP (VSTGUI_NSView_keyDown), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(keyUp:), IMP (VSTGUI_NSView_keyUp), "v@:@:^:"))
sprintf (funcSig, "%s@:@:", @encode(NSFocusRingType));
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(focusRingType), IMP (VSTGUI_NSView_focusRingType), funcSig))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(makeSubViewFirstResponder:), IMP (VSTGUI_NSView_makeSubViewFirstResponder), "v@:@:^:"))
sprintf (funcSig, "%s@:@:^:", nsUIntegerEncoded);
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(draggingEntered:), IMP (VSTGUI_NSView_draggingEntered), funcSig))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(draggingUpdated:), IMP (VSTGUI_NSView_draggingUpdated), funcSig))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(draggingExited:), IMP (VSTGUI_NSView_draggingExited), "v@:@:^:"))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(performDragOperation:), IMP (VSTGUI_NSView_performDragOperation), "B@:@:^:"))
if (auto nsDraggingSourceProtocol = objc_getProtocol ("NSDraggingSource"))
{
sprintf (funcSig, "%s@:@:^:%s", @encode(NSDragOperation), @encode(NSDraggingSession));
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(draggingSession:sourceOperationMaskForDraggingContext:), IMP (VSTGUI_NSView_draggingSessionSourceOperationMaskForDraggingContext), funcSig))
sprintf (funcSig, "%s@:@:^:%s:%s", @encode(void), @encode(NSDraggingSession), @encode(NSPoint));
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(draggingSession:willBeginAtPoint:), IMP (VSTGUI_NSView_draggingSessionWillBeginAtPoint), funcSig))
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(draggingSession:movedToPoint:), IMP (VSTGUI_NSView_draggingSessionMovedToPoint), funcSig))
sprintf (funcSig, "%s@:@:^:%s:%s:%s", @encode(void), @encode(NSDraggingSession), @encode(NSPoint), @encode(NSDragOperation));
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(draggingSession:endedAtPoint:operation:), IMP (VSTGUI_NSView_draggingSessionEndedAtPoint), funcSig))
class_addProtocol (viewClass, nsDraggingSourceProtocol);
}
#if VSTGUI_ENABLE_DEPRECATED_METHODS
sprintf (funcSig, "v@:@:^:%s:%s", @encode(NSPoint), nsUIntegerEncoded);
VSTGUI_CHECK_YES(class_addMethod (viewClass, @selector(draggedImage:endedAt:operation:), IMP (VSTGUI_NSView_draggedImageEndedAtOperation), funcSig))
#endif
// optional touchbar support
if (auto protocol = objc_getProtocol ("NSTouchBarProvider"))
{
class_addProtocol (viewClass, protocol);
sprintf (funcSig, "%s@:@:", @encode(NSObject*));
class_addMethod (viewClass, @selector(makeTouchBar), IMP (VSTGUI_NSView_makeTouchbar), funcSig);
}
VSTGUI_CHECK_YES(class_addIvar (viewClass, "_nsViewFrame", sizeof (void*), (uint8_t)log2(sizeof(void*)), @encode(void*)))
objc_registerClassPair (viewClass);
}
}
//-----------------------------------------------------------------------------
NSViewFrame::NSViewFrame (IPlatformFrameCallback* frame, const CRect& size, NSView* parent, IPlatformFrameConfig* config)
: IPlatformFrame (frame)
, nsView (nullptr)
, tooltipWindow (nullptr)
, ignoreNextResignFirstResponder (false)
, trackingAreaInitialized (false)
, inDraw (false)
, cursor (kCursorDefault)
{
auto cocoaConfig = dynamic_cast<CocoaFrameConfig*> (config);
initClass ();
nsView = [[viewClass alloc] initWithNSViewFrame: this parent: parent andSize: &size];
if (cocoaConfig && cocoaConfig->flags & CocoaFrameConfig::kNoCALayer)
return;
auto processInfo = [NSProcessInfo processInfo];
if ([processInfo respondsToSelector:@selector(operatingSystemVersion)])
{
// on Mac OS X 10.11 we activate layer drawing as this fixes a few issues like that only a
// few parts of a window are updated permanently when scrolling or manipulating a control
// while other parts are only updated when the malipulation ended, or CNinePartTiledBitmap
// are drawn incorrectly when scaled.
if (@available (macOS 10.11, *))
{
[nsView setWantsLayer:YES];
if (@available (macOS 10.13, *))
{
nsView.layer.contentsFormat = kCAContentsFormatRGBA8Uint;
// asynchronous layer drawing or drawing only dirty rectangles are exclusive as
// the CoreGraphics engineers decided to be clever and join dirty rectangles without
// letting us know
if (getPlatformFactory ().asMacFactory ()->getUseAsynchronousLayerDrawing ())
nsView.layer.drawsAsynchronously = YES;
else
useInvalidRects = true;
}
}
}
}
//-----------------------------------------------------------------------------
NSViewFrame::~NSViewFrame () noexcept
{
if (tooltipWindow)
tooltipWindow->forget ();
[nsView unregisterDraggedTypes]; // this is neccessary otherwise AppKit will crash if the plug-in is unloaded from the process
[nsView removeFromSuperview];
[nsView release];
}
//------------------------------------------------------------------------------------
void NSViewFrame::scaleFactorChanged (double newScaleFactor)
{
if (nsView.wantsLayer)
nsView.layer.contentsScale = newScaleFactor;
if (frame)
frame->platformScaleFactorChanged (newScaleFactor);
}
//-----------------------------------------------------------------------------
void NSViewFrame::initTrackingArea ()
{
if (trackingAreaInitialized == false)
{
NSPoint p = getGlobalMouseLocation (nsView);
NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited|NSTrackingMouseMoved|NSTrackingActiveInActiveApp|NSTrackingInVisibleRect;
if ([nsView hitTest:p])
{
options |= NSTrackingAssumeInside;
CPoint cp = pointFromNSPoint (p);
frame->platformOnMouseMoved (cp, 0);
}
NSTrackingArea* trackingArea = [[[NSTrackingArea alloc] initWithRect:[nsView frame] options:options owner:nsView userInfo:nil] autorelease];
[nsView addTrackingArea: trackingArea];
trackingAreaInitialized = true;
}
}
//-----------------------------------------------------------------------------
void NSViewFrame::cursorUpdate ()
{
setMouseCursor (cursor);
}
//------------------------------------------------------------------------
void NSViewFrame::setNeedsDisplayInRect (NSRect r)
{
if (useInvalidRects)
invalidRectList.add (rectFromNSRect (r));
}
//-----------------------------------------------------------------------------
void NSViewFrame::addDebugRedrawRect (CRect r, bool isClipBoundingBox)
{
#if DEBUG
if (getPlatformFactory ().asMacFactory ()->enableVisualizeRedrawAreas () && nsView.layer)
{
auto delegate = [[DebugRedrawAnimDelegate new] autorelease];
auto anim = [CABasicAnimation animation];
anim.fromValue = [NSNumber numberWithDouble:isClipBoundingBox ? 0.2 : 0.8];
anim.toValue = [NSNumber numberWithDouble:0.];
anim.keyPath = @"opacity";
anim.delegate = delegate;
anim.duration = isClipBoundingBox ? 1. : 1.;
auto rect = nsRectFromCRect (r);
for (CALayer* layer in nsView.layer.sublayers)
{
if (![layer.name isEqualToString:@"DebugLayer"])
continue;
if (CGRectEqualToRect (rect, layer.frame))
{
[layer removeAnimationForKey:@"opacity"];
[layer addAnimation:anim forKey:@"opacity"];
return;
}
}
auto layer = [[CALayer new] autorelease];
layer.name = @"DebugLayer";
layer.backgroundColor = CGColorCreateGenericRGB (isClipBoundingBox ? 0. : 1., 1., 0., 1.);
layer.opacity = 0.f;
layer.zPosition = isClipBoundingBox ? 10 : 11;
layer.frame = nsRectFromCRect (r);
[nsView.layer addSublayer:layer];
delegate.layer = layer;
[layer addAnimation:anim forKey:@"opacity"];
}
#endif
}
//-----------------------------------------------------------------------------
void NSViewFrame::drawRect (NSRect* rect)
{
inDraw = true;
NSGraphicsContext* nsContext = [NSGraphicsContext currentContext];
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAX_OS_X_VERSION_10_10
auto cgContext = static_cast<CGContextRef> ([nsContext graphicsPort]);
#else
auto cgContext = static_cast<CGContextRef> ([nsContext CGContext]);
#endif
addDebugRedrawRect (rectFromNSRect (*rect), true);
CGDrawContext drawContext (cgContext, rectFromNSRect ([nsView bounds]));
drawContext.beginDraw ();
if (useInvalidRects)
{
joinNearbyInvalidRects (invalidRectList, 24.);
for (auto r : invalidRectList)
{
frame->platformDrawRect (&drawContext, r);
addDebugRedrawRect (r, false);
}
invalidRectList.clear ();
}
else
{
const NSRect* dirtyRects;
NSInteger numDirtyRects;
[nsView getRectsBeingDrawn:&dirtyRects count:&numDirtyRects];
for (NSInteger i = 0; i < numDirtyRects; i++)
{
auto r = rectFromNSRect (dirtyRects[i]);
frame->platformDrawRect (&drawContext, r);
addDebugRedrawRect (r, false);
}
}
drawContext.endDraw ();
inDraw = false;
}
//-----------------------------------------------------------------------------
bool NSViewFrame::onMouseDown (NSEvent* theEvent)
{
CButtonState buttons = eventButton (theEvent);