/
HandMorph.class.st
1486 lines (1246 loc) · 44.9 KB
/
HandMorph.class.st
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
"
The cursor may be thought of as the HandMorph. The hand's submorphs hold anything being carried by dragging.
There is some minimal support for multiple hands in the same world.
"
Class {
#name : #HandMorph,
#superclass : #Morph,
#instVars : [
'mouseFocus',
'keyboardFocus',
'eventListeners',
'mouseListeners',
'keyboardListeners',
'mouseClickState',
'mouseOverHandler',
'lastMouseEvent',
'targetOffset',
'damageRecorder',
'cacheCanvas',
'cachedCanvasHasHoles',
'temporaryCursor',
'temporaryCursorOffset',
'hardwareCursor',
'hasChanged',
'savedPatch',
'lastEventBuffer',
'lastKeyScanCode',
'combinedChar',
'eventSource',
'lastSystemEvent',
'captureBlock',
'recentModifiers'
],
#classVars : [
'DoubleClickTime',
'EventSource',
'EventStats',
'NormalCursor',
'PasteBuffer',
'ShowEvents',
'UpperHandLimit'
],
#pools : [
'EventSensorConstants'
],
#category : #'Morphic-Core-Kernel'
}
{ #category : #utilities }
HandMorph class >> attach: aMorph [
"Attach aMorph the current world's primary hand."
self currentWorld primaryHand attachMorph: aMorph
]
{ #category : #cleanup }
HandMorph class >> cleanUp [
"Called from ImageCleaner>>cleanUpForRelease --> SmalltalkImage>>cleanUp:except:confirming:"
self logEventStatsStop.
ActiveHand resetClickState.
]
{ #category : #accessing }
HandMorph class >> doubleClickTime [
^ DoubleClickTime
]
{ #category : #accessing }
HandMorph class >> doubleClickTime: milliseconds [
DoubleClickTime := milliseconds.
]
{ #category : #'events-processing' }
HandMorph class >> eventStats [
^ EventStats ifNil: [ 'EventStats are disabled' ].
]
{ #category : #'class initialization' }
HandMorph class >> initialize [
"HandMorph initialize"
PasteBuffer := nil.
DoubleClickTime := 350.
NormalCursor := CursorWithMask normal asCursorForm.
]
{ #category : #'events-processing' }
HandMorph class >> logEventStats: evt [
EventStats ifNotNil: [
EventStats at: #count put: (EventStats at: #count ifAbsent:[0]) + 1.
EventStats at: evt type put: (EventStats at: evt type ifAbsent:[0]) + 1.
].
]
{ #category : #settings }
HandMorph class >> logEventStatsEnabled [
"self logEventStatsEnabled"
^ EventStats isNotNil
]
{ #category : #settings }
HandMorph class >> logEventStatsEnabled: aBoolean [
aBoolean
ifTrue: [ self logEventStatsStart ]
ifFalse: [ self logEventStatsStop ].
]
{ #category : #'events-processing' }
HandMorph class >> logEventStatsStart [
EventStats ifNil:[EventStats := IdentityDictionary new].
]
{ #category : #'events-processing' }
HandMorph class >> logEventStatsStop [
EventStats := nil.
]
{ #category : #settings }
HandMorph class >> settingsOn: aBuilder [
<systemsettings>
(aBuilder setting: #handMorphsLogEventStats)
parent: #pharoSystem;
label: 'HandMorph event statistics';
target: self;
selector: #logEventStatsEnabled;
description: 'Enable/disable gathering global statistics of events handled by HandMorphs.
"HandMorph eventStats inspect"'
]
{ #category : #utilities }
HandMorph class >> showEvents: aBool [
"HandMorph showEvents: true"
"HandMorph showEvents: false"
ShowEvents := aBool.
aBool ifFalse: [ self currentWorld invalidRect: (0@0 extent: 500@120) ].
]
{ #category : #accessing }
HandMorph class >> upperHandLimit [
^ UpperHandLimit ifNil: [ UpperHandLimit := 0 ]
]
{ #category : #accessing }
HandMorph class >> upperHandLimit: anInteger [
UpperHandLimit := anInteger
]
{ #category : #listeners }
HandMorph >> addEventListener: anObject [
"Make anObject a listener for all events. All events will be reported to the object."
self eventListeners: (self addListener: anObject to: self eventListeners)
]
{ #category : #listeners }
HandMorph >> addListener: anObject to: aListenerGroup [
"Add anObject to the given listener group. Return the new group."
| listeners |
listeners := aListenerGroup.
(listeners notNil and:[listeners includes: anObject]) ifFalse:[
listeners
ifNil:[listeners := WeakArray with: anObject]
ifNotNil:[listeners := listeners copyWith: anObject]].
listeners := listeners copyWithout: nil. "obsolete entries"
^listeners
]
{ #category : #listeners }
HandMorph >> addMouseListener: anObject [
"Make anObject a listener for mouse events. All mouse events will be reported to the object."
self mouseListeners: (self addListener: anObject to: self mouseListeners)
]
{ #category : #accessing }
HandMorph >> anyButtonPressed [
^lastMouseEvent anyButtonPressed
]
{ #category : #accessing }
HandMorph >> anyModifierKeyPressed [
^recentModifiers anyMask: 16r0E "cmd | opt | ctrl"
]
{ #category : #'grabbing/dropping' }
HandMorph >> attachMorph: m [
"Position the center of the given morph under this hand, then grab it.
This method is used to grab far away or newly created morphs."
| delta |
self releaseMouseFocus. "Break focus"
delta := m bounds extent // 2.
m position: (self position - delta).
m formerPosition: m position.
targetOffset := m position - self position.
self addMorphBack: m.
]
{ #category : #'balloon help' }
HandMorph >> balloonHelp [
"Return the balloon morph associated with this hand"
^self valueOfProperty: #balloonHelpMorph
]
{ #category : #'balloon help' }
HandMorph >> balloonHelp: aBalloonMorph [
"Return the balloon morph associated with this hand"
self balloonHelp ifNotNil:[:oldHelp |oldHelp delete].
aBalloonMorph
ifNil:[self removeProperty: #balloonHelpMorph]
ifNotNil:[self setProperty: #balloonHelpMorph toValue: aBalloonMorph]
]
{ #category : #'events-processing' }
HandMorph >> captureEventsUntil: aBlock [
"
Capture all input events, bypassing normal processing flow and redirect all events into block instead.
Repeat until block will answer true.
World activeHand captureEventsUntil: [:evt |
evt isKeyboard and: [ evt keyCharacter = $a ] ]
"
| release |
release := false.
captureBlock := [:evt | release := aBlock value: evt ].
[
[ self world doOneCycle. release ] whileFalse.
] ensure: [
captureBlock := nil.
]
]
{ #category : #'events-processing' }
HandMorph >> captureEventsWhile: aBlock [
"
Capture all input events, bypassing normal processing flow and redirect all events into block instead.
Repeat until block will answer false.
"
^ self captureEventsUntil: [:evt | (aBlock value: evt) not ]
]
{ #category : #updating }
HandMorph >> changed [
hasChanged := true.
]
{ #category : #accessing }
HandMorph >> colorForInsets [
"Morphs being dragged by the hand use the world's color"
^ owner colorForInsets
]
{ #category : #'meta-actions' }
HandMorph >> copyToPasteBuffer: aMorph [
"Save this morph in the paste buffer. This is mostly useful for copying morphs between projects."
aMorph ifNil:[^PasteBuffer := nil].
Cursor wait showWhile:[
PasteBuffer := aMorph topRendererOrSelf veryDeepCopy.
PasteBuffer privateOwner: nil].
]
{ #category : #accessing }
HandMorph >> cursorBounds [
temporaryCursor
ifNil: [^ self position extent: NormalCursor extent]
ifNotNil: [^ self position + temporaryCursorOffset
extent: temporaryCursor extent]
]
{ #category : #'event handling' }
HandMorph >> cursorPoint [
"Implemented for allowing embedded worlds in an event cycle to query a hand's position and get it in its coordinates. The same can be achieved by #point:from: but this is simply much more convenient since it will look as if the hand is in the lower world."
| pos |
pos := self position.
(self currentWorld isNil or: [self currentWorld == owner]) ifTrue: [^pos].
^self currentWorld point: pos from: owner
]
{ #category : #'balloon help' }
HandMorph >> deleteBalloonTarget: aMorph [
"Delete any existing balloon help. This is now done unconditionally, whether or not the morph supplied is the same as the current balloon target"
self balloonHelp: nil
" | h |
h := self balloonHelp ifNil: [^ self].
h balloonOwner == aMorph ifTrue: [self balloonHelp: nil]"
]
{ #category : #drawing }
HandMorph >> drawOn: aCanvas [
"Draw the hand itself (i.e., the cursor)."
temporaryCursor
ifNil: [aCanvas paintImage: NormalCursor at: bounds topLeft]
ifNotNil: [aCanvas paintImage: temporaryCursor at: bounds topLeft].
]
{ #category : #'grabbing/dropping' }
HandMorph >> dropMorph: aMorph event: anEvent [
"Drop the given morph which was carried by the hand"
| event dropped |
(anEvent isMouseUp and:[aMorph shouldDropOnMouseUp not]) ifTrue:[^self].
"Note: For robustness in drag and drop handling we remove the morph BEFORE we drop him, but we keep his owner set to the hand. This prevents system lockups when there is a problem in drop handling (for example if there's an error in #wantsToBeDroppedInto:). THIS TECHNIQUE IS NOT RECOMMENDED FOR CASUAL USE."
self privateRemove: aMorph.
aMorph privateOwner: self.
dropped := aMorph.
(dropped hasProperty: #addedFlexAtGrab)
ifTrue:[dropped := aMorph removeFlexShell].
event := DropEvent new setPosition: self position contents: dropped hand: self.
self sendEvent: event focus: nil.
event wasHandled ifFalse:[aMorph rejectDropMorphEvent: event].
aMorph owner == self ifTrue:[aMorph delete].
self mouseOverHandler processMouseOver: anEvent.
]
{ #category : #'grabbing/dropping' }
HandMorph >> dropMorphs [
"Drop the morphs at the hands position"
self dropMorphs: lastMouseEvent.
]
{ #category : #'grabbing/dropping' }
HandMorph >> dropMorphs: anEvent [
"Drop the morphs at the hands position"
self submorphsReverseDo:[:m|
"Drop back to front to maintain z-order"
self dropMorph: m event: anEvent.
].
]
{ #category : #listeners }
HandMorph >> eventListeners [
^eventListeners
]
{ #category : #listeners }
HandMorph >> eventListeners: anArrayOrNil [
eventListeners := anArrayOrNil
]
{ #category : #'private events' }
HandMorph >> eventQueue [
^ Sensor
]
{ #category : #layout }
HandMorph >> fullBounds [
"Extend my bounds by the shadow offset when carrying morphs."
| bnds |
bnds := super fullBounds.
submorphs isEmpty
ifTrue: [^ bnds ]
ifFalse: [^ bnds topLeft corner: bnds bottomRight + self shadowOffset].
]
{ #category : #drawing }
HandMorph >> fullDrawOn: aCanvas [
"A HandMorph has unusual drawing requirements:
1. the hand itself (i.e., the cursor) appears in front of its submorphs
2. morphs being held by the hand cast a shadow on the world/morphs below
The illusion is that the hand plucks up morphs and carries them above the world."
"Note: This version caches an image of the morphs being held by the hand for
better performance. This cache is invalidated if one of those morphs changes."
| disableCaching subBnds roundCorners rounded |
self visible ifFalse: [^self].
(aCanvas isVisible: self fullBounds) ifFalse: [^self].
disableCaching := false.
disableCaching
ifTrue:
[self nonCachingFullDrawOn: aCanvas.
^self].
submorphs isEmpty
ifTrue:
[cacheCanvas := nil.
^self drawOn: aCanvas]. "just draw the hand itself"
subBnds := Rectangle merging: (submorphs collect: [:m | m fullBounds]).
self updateCacheCanvas: aCanvas.
(cacheCanvas isNil
or: [cachedCanvasHasHoles and: [cacheCanvas depth = 1]]
)ifTrue: ["could not use caching due to translucency; do full draw"
self nonCachingFullDrawOn: aCanvas.
^self
].
"--> begin rounded corners hack <---"
roundCorners := cachedCanvasHasHoles == false
and: [submorphs size = 1 and: [submorphs first wantsRoundedCorners]].
roundCorners
ifTrue:
[rounded := submorphs first.
aCanvas asShadowDrawingCanvas translateBy: self shadowOffset
during:
[:shadowCanvas |
shadowCanvas roundCornersOf: rounded
during:
[(subBnds areasOutside: (rounded boundsWithinCorners
translateBy: self shadowOffset negated))
do: [:r | shadowCanvas fillRectangle: r color: Color black]]].
aCanvas roundCornersOf: rounded
during:
[aCanvas
drawImage: cacheCanvas form
at: subBnds origin
sourceRect: cacheCanvas form boundingBox].
^self drawOn: aCanvas "draw the hand itself in front of morphs"].
"--> end rounded corners hack <---"
"draw the shadow"
(submorphs anySatisfy: [:m | m handlesDropShadowInHand not]) ifTrue: [
aCanvas asShadowDrawingCanvas translateBy: self shadowOffset
during:
[:shadowCanvas |
cachedCanvasHasHoles
ifTrue:
["Have to draw the real shadow of the form"
shadowCanvas paintImage: cacheCanvas form at: subBnds origin]
ifFalse:
["Much faster if only have to shade the edge of a solid rectangle"
(subBnds areasOutside: (subBnds translateBy: self shadowOffset negated))
do: [:r | shadowCanvas fillRectangle: r color: Color black]]]].
"draw morphs in front of the shadow using the cached Form"
cachedCanvasHasHoles
ifTrue: [aCanvas paintImage: cacheCanvas form at: subBnds origin]
ifFalse: [
aCanvas
drawImage: cacheCanvas form
at: subBnds origin
sourceRect: cacheCanvas form boundingBox
].
self drawOn: aCanvas "draw the hand itself in front of morphs"
]
{ #category : #'private events' }
HandMorph >> generateKeyboardEvent: evtBuf [
"Generate the appropriate mouse event for the given raw event buffer"
| buttons modifiers type pressType stamp charCode keyValue keyEvent |
stamp := evtBuf second.
stamp = 0 ifTrue: [stamp := Time millisecondClockValue].
pressType := evtBuf fourth.
pressType = EventKeyDown
ifTrue: [
type := #keyDown.
lastKeyScanCode := evtBuf third].
pressType = EventKeyUp ifTrue: [type := #keyUp].
pressType = EventKeyChar ifTrue: [
type := #keystroke].
modifiers := evtBuf fifth.
buttons := modifiers bitShift: 3.
keyValue := evtBuf third.
charCode := evtBuf sixth.
type = #keystroke
ifTrue: [combinedChar
ifNil: [
| peekedEvent |
peekedEvent := Sensor peekEvent.
(peekedEvent notNil
and: [peekedEvent fourth = EventKeyDown])
ifTrue: [
(CombinedChar isCompositionCharacter: charCode)
ifTrue: [
combinedChar := CombinedChar new.
combinedChar simpleAdd: charCode asCharacter.
(combinedChar combinesWith: peekedEvent third asCharacter)
ifTrue: [^nil].
]]]
ifNotNil: [
(combinedChar simpleAdd: charCode asCharacter)
ifTrue: [charCode := combinedChar combined charCode].
combinedChar := nil]].
"This piece of code handles the creation of scrolling events. When a scroll is done by the user, the VM forwards a keystroke event with the up/down key. So we reconvert it to a MouseWheelEvent in that case."
(type = #keystroke
and: [(buttons anyMask: 16)
and: [{Character arrowUp. Character arrowDown} includes: (charCode asCharacter)]])
ifTrue: [^ MouseWheelEvent
fromCharacter: charCode asCharacter
position: lastMouseEvent cursorPoint
buttons: buttons
hand: self
stamp: stamp].
"If charCode is not single-byte, we definately have Unicode input. Zero keyValue to avoid garbage values from som VMs."
self flag: #pharoFixMe. "I need to look at the windows vm side, to see why this keyvalue is generated in the first place".
(charCode notNil and:[charCode > 255]) ifTrue: [keyValue:=0].
keyEvent := KeyboardEvent new
setType: type
buttons: buttons
position: self position
keyValue: keyValue
charCode: charCode
hand: self
stamp: stamp.
keyEvent scanCode: lastKeyScanCode.
^keyEvent
]
{ #category : #'private events' }
HandMorph >> generateMouseEvent: evtBuf [
"Generate the appropriate mouse event for the given raw event buffer"
| position buttons modifiers type trail stamp oldButtons evtChanged |
self class environment at: #KMBuffer ifPresent: [ :kmBuffer | kmBuffer uniqueInstance clearBuffer].
evtBuf first = lastEventBuffer first
ifTrue:
["Workaround for Mac VM bug, *always* generating 3 events on clicks"
evtChanged := false.
3 to: evtBuf size
do: [:i | (lastEventBuffer at: i) = (evtBuf at: i) ifFalse: [evtChanged := true]].
evtChanged ifFalse: [^nil]].
stamp := evtBuf second.
stamp = 0 ifTrue: [stamp := Time millisecondClockValue].
position := evtBuf third @ evtBuf fourth.
buttons := evtBuf fifth.
modifiers := evtBuf sixth.
type := buttons = 0
ifTrue:
[lastEventBuffer fifth = 0 ifTrue: [#mouseMove] ifFalse: [#mouseUp]]
ifFalse:
[lastEventBuffer fifth = 0
ifTrue: [#mouseDown]
ifFalse: [#mouseMove]].
buttons := buttons bitOr: (modifiers bitShift: 3).
oldButtons := lastEventBuffer fifth
bitOr: (lastEventBuffer sixth bitShift: 3).
lastEventBuffer := evtBuf.
type == #mouseMove
ifTrue:
[trail := self mouseTrailFrom: evtBuf.
^MouseMoveEvent basicNew
setType: type
startPoint: (self position)
endPoint: trail last
trail: trail
buttons: buttons
hand: self
stamp: stamp].
^MouseButtonEvent basicNew
setType: type
position: position
which: (oldButtons bitXor: buttons)
buttons: buttons
hand: self
stamp: stamp
]
{ #category : #'private events' }
HandMorph >> generateWindowEvent: evtBuf [
"Generate the appropriate window event for the given raw event buffer"
| evt |
evt := WindowEvent new.
evt setTimeStamp: evtBuf second.
evt timeStamp = 0 ifTrue: [evt setTimeStamp: Time millisecondClockValue].
evt action: evtBuf third.
evt rectangle: (Rectangle origin: evtBuf fourth @ evtBuf fifth corner: evtBuf sixth @ evtBuf seventh ).
^evt
]
{ #category : #'meta-actions' }
HandMorph >> grabMorph: aMorph [
"Grab the given morph (i.e., add it to this hand and remove it from its current owner) without changing its position. This is used to pick up a morph under the hand's current position, versus attachMorph: which is used to pick up a morph that may not be near this hand."
| grabbed |
aMorph = self currentWorld ifTrue: [^ self].
self releaseMouseFocus.
grabbed := aMorph aboutToBeGrabbedBy: self.
grabbed ifNil: [^self].
grabbed := grabbed topRendererOrSelf.
^self grabMorph: grabbed from: grabbed owner
]
{ #category : #'grabbing/dropping' }
HandMorph >> grabMorph: aMorph from: formerOwner [
"Grab the given morph (i.e., add it to this hand and remove it from its
current owner) without changing its position. This is used to pick up a
morph under the hand's current position, versus attachMorph: which
is used to pick up a morph that may not be near this hand."
| grabbed offset targetPoint grabTransform fullTransform |
self releaseMouseFocus.
"Break focus"
grabbed := aMorph.
aMorph keepsTransform
ifTrue: [grabTransform := fullTransform := IdentityTransform new]
ifFalse: ["Compute the transform to apply to the grabbed morph"
grabTransform := formerOwner
ifNil: [IdentityTransform new]
ifNotNil: [formerOwner grabTransform].
fullTransform := formerOwner
ifNil: [IdentityTransform new]
ifNotNil: [formerOwner transformFrom: owner]].
"targetPoint is point in aMorphs reference frame"
targetPoint := fullTransform globalPointToLocal: self position.
"but current position will be determined by grabTransform, so
compute offset"
offset := targetPoint
- (grabTransform globalPointToLocal: self position).
"apply the transform that should be used after grabbing"
grabbed := grabbed transformedBy: grabTransform.
grabbed == aMorph
ifFalse: [grabbed setProperty: #addedFlexAtGrab toValue: true].
"offset target to compensate for differences in transforms"
grabbed position: grabbed position - offset asIntegerPoint.
"And compute distance from hand's position"
targetOffset := grabbed position - self position.
self addMorphBack: grabbed.
grabbed justGrabbedFrom: formerOwner
]
{ #category : #'halos and balloon help' }
HandMorph >> halo [
"Return the halo associated with this hand, if any"
^self valueOfProperty: #halo
]
{ #category : #'halo handling' }
HandMorph >> halo: newHalo [
"Set halo associated with this hand"
| oldHalo |
oldHalo := self halo.
(oldHalo isNil or:[oldHalo == newHalo]) ifFalse:[oldHalo delete].
newHalo
ifNil:[self removeProperty: #halo]
ifNotNil:[self setProperty: #halo toValue: newHalo]
]
{ #category : #'events-processing' }
HandMorph >> handleEvent: anEvent [
| evt |
owner ifNil:[^self].
evt := anEvent.
"nil test here is for efficiency in the normal case"
EventStats ifNotNil: [ self class logEventStats: evt ].
evt isMouse ifTrue:[
"just for record, to be used by capture block"
lastMouseEvent := evt].
captureBlock ifNotNil: [ ^ captureBlock value: anEvent ].
evt isMouseOver ifTrue:[^self sendMouseEvent: evt].
self showDebugEvent: evt.
"Notify listeners"
self sendListenEvent: evt to: self eventListeners.
evt isWindowEvent ifTrue: [
self sendEvent: evt focus: nil.
^self mouseOverHandler processMouseOver: lastMouseEvent].
evt isKeyboard ifTrue:[
self sendListenEvent: evt to: self keyboardListeners.
self sendKeyboardEvent: evt.
^self mouseOverHandler processMouseOver: lastMouseEvent].
evt isDropEvent ifTrue:[
self sendEvent: evt focus: nil.
^self mouseOverHandler processMouseOver: lastMouseEvent].
evt isMouse ifTrue:[
self sendListenEvent: evt to: self mouseListeners.
lastMouseEvent := evt].
"Check for pending drag or double click operations."
mouseClickState ifNotNil:[
(mouseClickState handleEvent: evt from: self) ifFalse:[
"Possibly dispatched #click: or something and will not re-establish otherwise"
^self mouseOverHandler processMouseOver: lastMouseEvent]].
evt isMove ifTrue:[
| pos |
pos := evt position.
evt isDraggingEvent ifTrue: [
| treshold |
treshold := 0.
(self submorphs at: 1 ifAbsent: [ nil ])
ifNotNil: [ :first | treshold := self top-first top ].
pos y < (self class upperHandLimit+treshold) ifTrue: [ pos := pos x @ (self class upperHandLimit + treshold)] ].
self position: pos.
self sendMouseEvent: evt.
] ifFalse:[
"Issue a synthetic move event if we're not at the position of the event"
(evt position = self position) ifFalse:[self moveToEvent: evt].
"Drop submorphs on button events"
(self hasSubmorphs)
ifTrue:[self dropMorphs: evt]
ifFalse:[self sendMouseEvent: evt].
].
self showMouseFocusEvent: evt.
self mouseOverHandler processMouseOver: lastMouseEvent.
]
{ #category : #drawing }
HandMorph >> hasChanged [
"Return true if this hand has changed, either because it has moved or because some morph it is holding has changed."
^ hasChanged ifNil: [ true ]
]
{ #category : #initialization }
HandMorph >> initForEvents [
mouseOverHandler := nil.
lastMouseEvent := MouseEvent basicNew setType: #mouseMove position: 0@0 buttons: 0 hand: self.
lastEventBuffer := {1. 0. 0. 0. 0. 0. nil. nil}.
recentModifiers := 0.
self resetClickState.
]
{ #category : #initialization }
HandMorph >> initialize [
super initialize.
self initForEvents.
keyboardFocus := nil.
mouseFocus := nil.
bounds := 0@0 extent: Cursor normal extent.
damageRecorder := DamageRecorder new.
cachedCanvasHasHoles := false.
temporaryCursor := temporaryCursorOffset := nil.
self initForEvents.
]
{ #category : #initialization }
HandMorph >> interrupted [
"Something went wrong - we're about to bring up a debugger.
Release some stuff that could be problematic."
self releaseAllFocus. "or else debugger might not handle clicks"
]
{ #category : #'change reporting' }
HandMorph >> invalidRect: damageRect from: aMorph [
"Note that a change has occurred and record the given damage rectangle relative to the origin this hand's cache."
hasChanged := true.
aMorph == self ifTrue:[^self].
damageRecorder recordInvalidRect: damageRect.
]
{ #category : #classification }
HandMorph >> isHandMorph [
^ true
]
{ #category : #'focus handling' }
HandMorph >> keyboardFocus [
^ keyboardFocus
]
{ #category : #'focus handling' }
HandMorph >> keyboardFocus: aMorphOrNil [
self newKeyboardFocus: aMorphOrNil
]
{ #category : #listeners }
HandMorph >> keyboardListeners [
^nil
]
{ #category : #accessing }
HandMorph >> lastEvent [
^ lastMouseEvent
]
{ #category : #'focus handling' }
HandMorph >> mouseFocus [
^mouseFocus
]
{ #category : #'focus handling' }
HandMorph >> mouseFocus: aMorphOrNil [
mouseFocus := aMorphOrNil
]
{ #category : #listeners }
HandMorph >> mouseListeners [
^mouseListeners
]
{ #category : #listeners }
HandMorph >> mouseListeners: anArrayOrNil [
mouseListeners := anArrayOrNil
]
{ #category : #accessing }
HandMorph >> mouseOverHandler [
^mouseOverHandler ifNil:[mouseOverHandler := MouseOverHandler new].
]
{ #category : #'private events' }
HandMorph >> mouseTrailFrom: currentBuf [
"Current event, a mouse event buffer, is about to be processed. If there are other similar mouse events queued up, then drop them from the queue, and report the positions inbetween."
| nextEvent trail |
trail := (Array new: 1) writeStream.
trail nextPut: currentBuf third @ currentBuf fourth.
[(nextEvent := Sensor peekEvent) isNil] whileFalse:
[nextEvent first = currentBuf first
ifFalse: [^trail contents "different event type"].
nextEvent fifth = currentBuf fifth
ifFalse: [^trail contents "buttons changed"].
nextEvent sixth = currentBuf sixth
ifFalse: [^trail contents "modifiers changed"].
"nextEvent is similar. Remove it from the queue, and check the next."
nextEvent := Sensor nextEvent.
nextEvent
ifNotNil: [ trail nextPut: nextEvent third @ nextEvent fourth ]].
^trail contents
]
{ #category : #'private events' }
HandMorph >> moveToEvent: anEvent [
"Issue a mouse move event to make the receiver appear at the given position"
self handleEvent: (MouseMoveEvent basicNew
setType: #mouseMove
startPoint: self position
endPoint: anEvent position
trail: (Array with: self position with: anEvent position)
buttons: anEvent buttons
hand: self
stamp: anEvent timeStamp)
]
{ #category : #drawing }
HandMorph >> needsToBeDrawn [
"Return true if this hand must be drawn explicitely instead of being drawn via the hardware cursor. This is the case if it (a) it is a remote hand, (b) it is showing a temporary cursor, or (c) it is not empty and there are any visible submorphs. If using the software cursor, ensure that the hardware cursor is hidden."
"Details: Return true if this hand has a saved patch to ensure that is is processed by the world. This saved patch will be deleted after one final display pass when it becomes possible to start using the hardware cursor again. This trick gives us one last display cycle to allow us to remove the software cursor and shadow from the display."
| cursor |
(savedPatch notNil
or: [ (submorphs anySatisfy: [ :ea | ea visible ])
or: [ (temporaryCursor notNil and: [hardwareCursor isNil])
]])
ifTrue: [
"using the software cursor; hide the hardware one"
Cursor blank isCurrent ifFalse: [Cursor blank show].
^ true].
"Switch from one hardware cursor to another, if needed."
cursor := hardwareCursor ifNil: [Cursor normal].
cursor isCurrent ifFalse: [cursor show].
^ false
]
{ #category : #'focus handling' }
HandMorph >> newKeyboardFocus: aMorphOrNil [
"Make the given morph the new keyboard focus, canceling the previous keyboard focus if any. If the argument is nil, the current keyboard focus is cancelled."
| oldFocus |
keyboardFocus == aMorphOrNil ifTrue: [ ^self ].
oldFocus := keyboardFocus.
keyboardFocus := aMorphOrNil.
oldFocus ifNotNil: [oldFocus keyboardFocusChange: false].
aMorphOrNil ifNotNil: [ aMorphOrNil keyboardFocusChange: true ].
]
{ #category : #'focus handling' }
HandMorph >> newMouseFocus: aMorphOrNil [
"Make the given morph the new mouse focus, canceling the previous mouse focus if any. If the argument is nil, the current mouse focus is cancelled."
self mouseFocus: aMorphOrNil.
]
{ #category : #'focus handling' }
HandMorph >> newMouseFocus: aMorph event: event [
aMorph ifNotNil: [targetOffset := event cursorPoint - aMorph position].
^self newMouseFocus: aMorph
]
{ #category : #accessing }
HandMorph >> noButtonPressed [
"Answer whether any mouse button is not being pressed."
^self anyButtonPressed not
]
{ #category : #drawing }
HandMorph >> nonCachingFullDrawOn: aCanvas [
"A HandMorph has unusual drawing requirements:
1. the hand itself (i.e., the cursor) appears in front of its submorphs
2. morphs being held by the hand cast a shadow on the world/morphs below
The illusion is that the hand plucks up morphs and carries them above the world."
"Note: This version does not cache an image of the morphs being held by the hand.
Thus, it is slower for complex morphs, but consumes less space."
submorphs isEmpty ifTrue: [^ self drawOn: aCanvas]. "just draw the hand itself"
aCanvas asShadowDrawingCanvas
translateBy: self shadowOffset during:[:shadowCanvas| | shadowForm |
"Note: We use a shadow form here to prevent drawing
overlapping morphs multiple times using the transparent
shadow color."
shadowForm := self shadowForm.
"
shadowForm displayAt: shadowForm offset negated. Display forceToScreen: (0@0 extent: shadowForm extent).
"
shadowCanvas paintImage: shadowForm at: shadowForm offset. "draw shadows"
].
"draw morphs in front of shadows"
self drawSubmorphsOn: aCanvas.
self drawOn: aCanvas. "draw the hand itself in front of morphs"
]
{ #category : #'event handling' }
HandMorph >> noticeMouseOver: aMorph event: anEvent [
mouseOverHandler ifNil:[^self].
mouseOverHandler noticeMouseOver: aMorph event: anEvent.
]
{ #category : #'paste buffer' }
HandMorph >> objectToPaste [
"It may need to be sent #startRunning by the client"
^ Cursor wait showWhile: [PasteBuffer veryDeepCopy]
"PasteBuffer usableDuplicateIn: self world"
]
{ #category : #'halo handling' }
HandMorph >> obtainHalo: aHalo [
"Used for transfering halos between hands"
self halo == aHalo
ifTrue: [ ^ self ].
"Find former owner"
self world hands detect: [ :hand | hand halo == aHalo ] ifFound: [ :formerOwner | formerOwner releaseHalo: aHalo ].
self halo: aHalo
]
{ #category : #'paste buffer' }
HandMorph >> pasteBuffer [
"Return the paste buffer associated with this hand"
^ PasteBuffer
]
{ #category : #'paste buffer' }
HandMorph >> pasteBuffer: aMorphOrNil [
"Set the contents of the paste buffer."
PasteBuffer := aMorphOrNil.
]
{ #category : #geometry }
HandMorph >> position [
^temporaryCursor
ifNil: [bounds topLeft]
ifNotNil: [bounds topLeft - temporaryCursorOffset]
]
{ #category : #geometry }
HandMorph >> position: aPoint [
"Overridden to align submorph origins to the grid if gridding is on."
| adjustedPosition delta box |
adjustedPosition := aPoint.
temporaryCursor ifNotNil: [adjustedPosition := adjustedPosition + temporaryCursorOffset].
"Copied from Morph to avoid owner layoutChanged"