-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
LWWindowPeer.java
1530 lines (1308 loc) · 53.3 KB
/
LWWindowPeer.java
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
/*
* Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.lwawt;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.KeyboardFocusManager;
import java.awt.MenuBar;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowEvent;
import java.awt.peer.ComponentPeer;
import java.awt.peer.DialogPeer;
import java.awt.peer.FramePeer;
import java.awt.peer.KeyboardFocusManagerPeer;
import java.awt.peer.WindowPeer;
import java.util.List;
import java.util.Objects;
import javax.swing.JComponent;
import sun.awt.AWTAccessor;
import sun.awt.AWTAccessor.ComponentAccessor;
import sun.awt.AppContext;
import sun.awt.CGraphicsDevice;
import sun.awt.DisplayChangedListener;
import sun.awt.ExtendedKeyCodes;
import sun.awt.FullScreenCapable;
import sun.awt.SunToolkit;
import sun.awt.TimedWindowEvent;
import sun.awt.UngrabEvent;
import sun.java2d.NullSurfaceData;
import sun.java2d.SunGraphics2D;
import sun.java2d.SunGraphicsEnvironment;
import sun.java2d.SurfaceData;
import sun.java2d.loops.Blit;
import sun.java2d.loops.CompositeType;
import sun.java2d.pipe.Region;
import sun.util.logging.PlatformLogger;
public class LWWindowPeer
extends LWContainerPeer<Window, JComponent>
implements FramePeer, DialogPeer, FullScreenCapable, DisplayChangedListener, PlatformEventNotifier
{
public enum PeerType {
SIMPLEWINDOW,
FRAME,
DIALOG,
EMBEDDED_FRAME,
VIEW_EMBEDDED_FRAME,
LW_FRAME
}
private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.lwawt.focus.LWWindowPeer");
private final PlatformWindow platformWindow;
private static final int MINIMUM_WIDTH = 1;
private static final int MINIMUM_HEIGHT = 1;
private Insets insets = new Insets(0, 0, 0, 0);
private Rectangle maximizedBounds;
private GraphicsDevice graphicsDevice;
private GraphicsConfiguration graphicsConfig;
private SurfaceData surfaceData;
private final Object surfaceDataLock = new Object();
private volatile int windowState = Frame.NORMAL;
// check that the mouse is over the window
private volatile boolean isMouseOver;
// A peer where the last mouse event came to. Used by cursor manager to
// find the component under cursor
private static volatile LWComponentPeer<?, ?> lastCommonMouseEventPeer;
// A peer where the last mouse event came to. Used to generate
// MOUSE_ENTERED/EXITED notifications
private volatile LWComponentPeer<?, ?> lastMouseEventPeer;
// Peers where all dragged/released events should come to,
// depending on what mouse button is being dragged according to Cocoa
private static final LWComponentPeer<?, ?>[] mouseDownTarget = new LWComponentPeer<?, ?>[3];
// A bitmask that indicates what mouse buttons produce MOUSE_CLICKED events
// on MOUSE_RELEASE. Click events are only generated if there were no drag
// events between MOUSE_PRESSED and MOUSE_RELEASED for particular button
private static int mouseClickButtons = 0;
private volatile boolean isOpaque = true;
private static final Font DEFAULT_FONT = new Font("Lucida Grande", Font.PLAIN, 13);
private static LWWindowPeer grabbingWindow;
private volatile boolean skipNextFocusChange;
private static final Color nonOpaqueBackground = new Color(0, 0, 0, 0);
private volatile boolean textured;
private final PeerType peerType;
private final SecurityWarningWindow warningWindow;
private volatile boolean targetFocusable;
/**
* Current modal blocker or null.
*
* Synchronization: peerTreeLock.
*/
private LWWindowPeer blocker;
public LWWindowPeer(Window target, PlatformComponent platformComponent,
PlatformWindow platformWindow, PeerType peerType)
{
super(target, platformComponent);
this.platformWindow = platformWindow;
this.peerType = peerType;
Window owner = target.getOwner();
LWWindowPeer ownerPeer = owner == null ? null :
(LWWindowPeer) AWTAccessor.getComponentAccessor().getPeer(owner);
PlatformWindow ownerDelegate = (ownerPeer != null) ? ownerPeer.getPlatformWindow() : null;
// The delegate.initialize() needs a non-null GC on X11.
GraphicsConfiguration gc = getTarget().getGraphicsConfiguration();
synchronized (getStateLock()) {
// graphicsConfig should be updated according to the real window
// bounds when the window is shown, see 4868278
this.graphicsConfig = gc;
}
if (!target.isFontSet()) {
target.setFont(DEFAULT_FONT);
}
if (!target.isBackgroundSet()) {
target.setBackground(SystemColor.window);
} else {
// first we check if user provided alpha for background. This is
// similar to what Apple's Java do.
// Since JDK7 we should rely on setOpacity() only.
// this.opacity = c.getAlpha();
}
if (!target.isForegroundSet()) {
target.setForeground(SystemColor.windowText);
// we should not call setForeground because it will call a repaint
// which the peer may not be ready to do yet.
}
platformWindow.initialize(target, this, ownerDelegate);
// Init warning window(for applets)
SecurityWarningWindow warn = null;
if (target.getWarningString() != null) {
// accessSystemTray permission allows to display TrayIcon, TrayIcon tooltip
// and TrayIcon balloon windows without a warning window.
if (!AWTAccessor.getWindowAccessor().isTrayIconWindow(target)) {
LWToolkit toolkit = (LWToolkit)Toolkit.getDefaultToolkit();
warn = toolkit.createSecurityWarning(target, this);
}
}
warningWindow = warn;
}
@Override
void initializeImpl() {
super.initializeImpl();
if (getTarget() instanceof Frame) {
Frame frame = (Frame) getTarget();
setTitle(frame.getTitle());
setState(frame.getExtendedState());
setMaximizedBounds(frame.getMaximizedBounds());
} else if (getTarget() instanceof Dialog) {
setTitle(((Dialog) getTarget()).getTitle());
}
updateAlwaysOnTopState();
updateMinimumSize();
updateFocusableWindowState();
final Shape shape = getTarget().getShape();
if (shape != null) {
applyShape(Region.getInstance(shape, null));
}
final float opacity = getTarget().getOpacity();
if (opacity < 1.0f) {
setOpacity(opacity);
}
setOpaque(getTarget().isOpaque());
updateInsets(platformWindow.getInsets());
if (getSurfaceData() == null) {
replaceSurfaceData(false);
}
activateDisplayListener();
}
// Just a helper method
@Override
public PlatformWindow getPlatformWindow() {
return platformWindow;
}
@Override
protected LWWindowPeer getWindowPeerOrSelf() {
return this;
}
// ---- PEER METHODS ---- //
@Override
protected void disposeImpl() {
deactivateDisplayListener();
SurfaceData oldData = getSurfaceData();
synchronized (surfaceDataLock){
surfaceData = null;
}
if (oldData != null) {
oldData.invalidate();
}
if (isGrabbing()) {
ungrab();
}
if (warningWindow != null) {
warningWindow.dispose();
}
platformWindow.dispose();
super.disposeImpl();
}
@Override
public void setBackground(final Color c) {
Color oldBg = getBackground();
if (Objects.equals(oldBg, c)) {
return;
}
super.setBackground(c);
updateOpaque();
}
@Override
protected void setVisibleImpl(final boolean visible) {
if (!visible && warningWindow != null) {
warningWindow.setVisible(false, false);
}
updateFocusableWindowState();
super.setVisibleImpl(visible);
// TODO: update graphicsConfig, see 4868278
platformWindow.setVisible(visible);
if (isSimpleWindow()) {
KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance();
if (visible) {
if (!getTarget().isAutoRequestFocus()) {
return;
} else {
requestWindowFocus(FocusEvent.Cause.ACTIVATION);
}
// Focus the owner in case this window is focused.
} else if (kfmPeer.getCurrentFocusedWindow() == getTarget()) {
// Transfer focus to the owner.
LWWindowPeer owner = getOwnerFrameDialog(LWWindowPeer.this);
if (owner != null) {
owner.requestWindowFocus(FocusEvent.Cause.ACTIVATION);
}
}
}
}
@Override
public final GraphicsConfiguration getGraphicsConfiguration() {
synchronized (getStateLock()) {
return graphicsConfig;
}
}
@Override
public boolean updateGraphicsData(GraphicsConfiguration gc) {
setGraphicsConfig(gc);
return false;
}
protected final Graphics getOnscreenGraphics(Color fg, Color bg, Font f) {
SurfaceData surfaceData = getSurfaceData();
if (surfaceData == null) {
return null;
}
if (fg == null) {
fg = SystemColor.windowText;
}
if (bg == null) {
bg = SystemColor.window;
}
if (f == null) {
f = DEFAULT_FONT;
}
return new SunGraphics2D(surfaceData, fg, bg, f);
}
@Override
public void setBounds(int x, int y, int w, int h, int op) {
if((op & NO_EMBEDDED_CHECK) == 0 && getPeerType() == PeerType.VIEW_EMBEDDED_FRAME) {
return;
}
if ((op & SET_CLIENT_SIZE) != 0) {
// SET_CLIENT_SIZE is only applicable to window peers, so handle it here
// instead of pulling 'insets' field up to LWComponentPeer
// no need to add insets since Window's notion of width and height includes insets.
op &= ~SET_CLIENT_SIZE;
op |= SET_SIZE;
}
// Don't post ComponentMoved/Resized and Paint events
// until we've got a notification from the delegate
Rectangle cb = constrainBounds(x, y, w, h);
Rectangle newBounds = new Rectangle(getBounds());
if ((op & (SET_LOCATION | SET_BOUNDS)) != 0) {
newBounds.x = cb.x;
newBounds.y = cb.y;
}
if ((op & (SET_SIZE | SET_BOUNDS)) != 0) {
newBounds.width = cb.width;
newBounds.height = cb.height;
}
// Native system could constraint bounds, so the peer wold be updated in the callback
platformWindow.setBounds(newBounds.x, newBounds.y, newBounds.width, newBounds.height);
}
public Rectangle constrainBounds(Rectangle bounds) {
return constrainBounds(bounds.x, bounds.y, bounds.width, bounds.height);
}
public Rectangle constrainBounds(int x, int y, int w, int h) {
if (w < MINIMUM_WIDTH) {
w = MINIMUM_WIDTH;
}
if (h < MINIMUM_HEIGHT) {
h = MINIMUM_HEIGHT;
}
final int maxW = getLWGC().getMaxTextureWidth();
final int maxH = getLWGC().getMaxTextureHeight();
if (w > maxW) {
w = maxW;
}
if (h > maxH) {
h = maxH;
}
return new Rectangle(x, y, w, h);
}
@Override
public Point getLocationOnScreen() {
return platformWindow.getLocationOnScreen();
}
/**
* Overridden from LWContainerPeer to return the correct insets.
* Insets are queried from the delegate and are kept up to date by
* requerying when needed (i.e. when the window geometry is changed).
*/
@Override
public Insets getInsets() {
synchronized (getStateLock()) {
return insets;
}
}
@Override
public FontMetrics getFontMetrics(Font f) {
// TODO: check for "use platform metrics" settings
return platformWindow.getFontMetrics(f);
}
@Override
public void toFront() {
platformWindow.toFront();
}
@Override
public void toBack() {
platformWindow.toBack();
}
@Override
public void setZOrder(ComponentPeer above) {
throw new RuntimeException("not implemented");
}
@Override
public void updateAlwaysOnTopState() {
platformWindow.setAlwaysOnTop(getTarget().isAlwaysOnTop());
}
@Override
public void updateFocusableWindowState() {
targetFocusable = getTarget().isFocusableWindow();
platformWindow.updateFocusableWindowState();
}
@Override
public void setModalBlocked(Dialog blocker, boolean blocked) {
synchronized (getPeerTreeLock()) {
ComponentPeer peer = AWTAccessor.getComponentAccessor().getPeer(blocker);
if (blocked && (peer instanceof LWWindowPeer)) {
this.blocker = (LWWindowPeer) peer;
} else {
this.blocker = null;
}
}
platformWindow.setModalBlocked(blocked);
}
@Override
public void updateMinimumSize() {
final Dimension min;
if (getTarget().isMinimumSizeSet()) {
min = getTarget().getMinimumSize();
min.width = Math.max(min.width, MINIMUM_WIDTH);
min.height = Math.max(min.height, MINIMUM_HEIGHT);
} else {
min = new Dimension(MINIMUM_WIDTH, MINIMUM_HEIGHT);
}
final Dimension max;
if (getTarget().isMaximumSizeSet()) {
max = getTarget().getMaximumSize();
max.width = Math.min(max.width, getLWGC().getMaxTextureWidth());
max.height = Math.min(max.height, getLWGC().getMaxTextureHeight());
} else {
max = new Dimension(getLWGC().getMaxTextureWidth(),
getLWGC().getMaxTextureHeight());
}
platformWindow.setSizeConstraints(min.width, min.height, max.width, max.height);
}
@Override
public void updateIconImages() {
getPlatformWindow().updateIconImages();
}
@Override
public void setOpacity(float opacity) {
getPlatformWindow().setOpacity(opacity);
repaintPeer();
}
@Override
public final void setOpaque(final boolean isOpaque) {
if (this.isOpaque != isOpaque) {
this.isOpaque = isOpaque;
updateOpaque();
}
}
private void updateOpaque() {
getPlatformWindow().setOpaque(!isTranslucent());
replaceSurfaceData(false);
repaintPeer();
}
@Override
public void updateWindow() {
}
public final boolean isTextured() {
return textured;
}
public final void setTextured(final boolean isTextured) {
if (textured != isTextured) {
textured = isTextured;
updateOpaque();
}
}
@Override
public final boolean isTranslucent() {
synchronized (getStateLock()) {
/*
* Textured window is a special case of translucent window.
* The difference is only in nswindow background. So when we set
* texture property our peer became fully translucent. It doesn't
* fill background, create non opaque backbuffers and layer etc.
*/
return !isOpaque || isShaped() || isTextured();
}
}
@Override
final void applyShapeImpl(final Region shape) {
super.applyShapeImpl(shape);
updateOpaque();
}
@Override
public void repositionSecurityWarning() {
if (warningWindow != null) {
ComponentAccessor compAccessor = AWTAccessor.getComponentAccessor();
Window target = getTarget();
int x = compAccessor.getX(target);
int y = compAccessor.getY(target);
int width = compAccessor.getWidth(target);
int height = compAccessor.getHeight(target);
warningWindow.reposition(x, y, width, height);
}
}
// ---- FRAME PEER METHODS ---- //
@Override // FramePeer and DialogPeer
public void setTitle(String title) {
platformWindow.setTitle(title == null ? "" : title);
}
@Override
public void setMenuBar(MenuBar mb) {
platformWindow.setMenuBar(mb);
}
@Override // FramePeer and DialogPeer
public void setResizable(boolean resizable) {
platformWindow.setResizable(resizable);
}
@Override
public void setState(int state) {
platformWindow.setWindowState(state);
}
@Override
public int getState() {
return windowState;
}
private boolean isMaximizedBoundsSet() {
synchronized (getStateLock()) {
return maximizedBounds != null;
}
}
private Rectangle getDefaultMaximizedBounds() {
GraphicsConfiguration config = getGraphicsConfiguration();
Insets screenInsets = ((CGraphicsDevice) config.getDevice())
.getScreenInsets();
Rectangle gcBounds = config.getBounds();
return new Rectangle(
gcBounds.x + screenInsets.left,
gcBounds.y + screenInsets.top,
gcBounds.width - screenInsets.left - screenInsets.right,
gcBounds.height - screenInsets.top - screenInsets.bottom);
}
@Override
public void setMaximizedBounds(Rectangle bounds) {
boolean isMaximizedBoundsSet;
synchronized (getStateLock()) {
this.maximizedBounds = (isMaximizedBoundsSet = (bounds != null))
? constrainBounds(bounds) : null;
}
setPlatformMaximizedBounds(isMaximizedBoundsSet ? maximizedBounds
: getDefaultMaximizedBounds());
}
public Rectangle getMaximizedBounds() {
synchronized (getStateLock()) {
return (maximizedBounds == null)
? getDefaultMaximizedBounds()
: maximizedBounds;
}
}
private void setPlatformMaximizedBounds(Rectangle bounds) {
platformWindow.setMaximizedBounds(
bounds.x, bounds.y,
bounds.width, bounds.height);
}
@Override
public void setBoundsPrivate(int x, int y, int width, int height) {
setBounds(x, y, width, height, SET_BOUNDS | NO_EMBEDDED_CHECK);
}
@Override
public Rectangle getBoundsPrivate() {
throw new RuntimeException("not implemented");
}
// ---- DIALOG PEER METHODS ---- //
@Override
public void blockWindows(List<Window> windows) {
//TODO: LWX will probably need some collectJavaToplevels to speed this up
for (Window w : windows) {
WindowPeer wp = AWTAccessor.getComponentAccessor().getPeer(w);
if (wp != null) {
wp.setModalBlocked((Dialog)getTarget(), true);
}
}
}
// ---- PEER NOTIFICATIONS ---- //
@Override
public void notifyIconify(boolean iconify) {
//The toplevel target is Frame and states are applicable to it.
//Otherwise, the target is Window and it don't have state property.
//Hopefully, no such events are posted in the queue so consider the
//target as Frame in all cases.
// REMIND: should we send it anyway if the state not changed since last
// time?
WindowEvent iconifyEvent = new WindowEvent(getTarget(),
iconify ? WindowEvent.WINDOW_ICONIFIED
: WindowEvent.WINDOW_DEICONIFIED);
postEvent(iconifyEvent);
int newWindowState = iconify ? Frame.ICONIFIED : Frame.NORMAL;
postWindowStateChangedEvent(newWindowState);
// REMIND: RepaintManager doesn't repaint iconified windows and
// hence ignores any repaint request during deiconification.
// So, we need to repaint window explicitly when it becomes normal.
if (!iconify) {
repaintPeer();
}
}
@Override
public void notifyZoom(boolean isZoomed) {
int newWindowState = isZoomed ? Frame.MAXIMIZED_BOTH : Frame.NORMAL;
postWindowStateChangedEvent(newWindowState);
}
/**
* Called by the {@code PlatformWindow} when any part of the window should
* be repainted.
*/
@Override
public void notifyExpose(final Rectangle r) {
repaintPeer(r);
}
/**
* Called by the {@code PlatformWindow} when this window is moved/resized by
* user or window insets are changed. There's no notifyReshape() in
* LWComponentPeer as the only components which could be resized by user are
* top-level windows.
* <p>
* We need to update the target and post the events, if the peer was moved
* or resized, or if the target is out of sync with this peer.
*/
@Override
public void notifyReshape(int x, int y, int w, int h) {
final Rectangle pBounds = getBounds();
final boolean invalid = updateInsets(platformWindow.getInsets());
final boolean pMoved = (x != pBounds.x) || (y != pBounds.y);
final boolean pResized = (w != pBounds.width) || (h != pBounds.height);
final ComponentAccessor accessor = AWTAccessor.getComponentAccessor();
final Rectangle tBounds = accessor.getBounds(getTarget());
final boolean tMoved = (x != tBounds.x) || (y != tBounds.y);
final boolean tResized = (w != tBounds.width) || (h != tBounds.height);
// Check if anything changed
if (!tMoved && !tResized && !pMoved && !pResized && !invalid) {
// Native window(NSWindow)/LWWindowPeer/Target are in sync
return;
}
// First, update peer's bounds
setBounds(x, y, w, h, SET_BOUNDS, false, false);
// Second, update the graphics config and surface data
final boolean isNewDevice = updateGraphicsDevice();
if (isNewDevice && !isMaximizedBoundsSet()) {
setPlatformMaximizedBounds(getDefaultMaximizedBounds());
}
if (pResized || isNewDevice || invalid) {
replaceSurfaceData();
updateMinimumSize();
}
// Third, COMPONENT_MOVED/COMPONENT_RESIZED/PAINT events
if (tMoved || pMoved || invalid) {
handleMove(x, y, true);
}
if (tResized || pResized || invalid || isNewDevice) {
handleResize(w, h, true);
repaintPeer();
}
repositionSecurityWarning();
}
private void clearBackground(final int w, final int h) {
final Graphics g = getOnscreenGraphics(getForeground(), getBackground(),
getFont());
if (g != null) {
try {
if (g instanceof Graphics2D) {
((Graphics2D) g).setComposite(AlphaComposite.Src);
}
if (isTranslucent()) {
g.setColor(nonOpaqueBackground);
g.fillRect(0, 0, w, h);
}
if (!isTextured()) {
if (g instanceof SunGraphics2D) {
((SunGraphics2D) g).constrain(0, 0, w, h, getRegion());
}
g.setColor(getBackground());
g.fillRect(0, 0, w, h);
}
} finally {
g.dispose();
}
}
}
@Override
public void notifyUpdateCursor() {
getLWToolkit().getCursorManager().updateCursorLater(this);
}
@Override
public void notifyActivation(boolean activation, LWWindowPeer opposite) {
Window oppositeWindow = (opposite == null)? null : opposite.getTarget();
changeFocusedWindow(activation, oppositeWindow);
}
// MouseDown in non-client area
@Override
public void notifyNCMouseDown() {
// Ungrab except for a click on a Dialog with the grabbing owner
if (grabbingWindow != null &&
!grabbingWindow.isOneOfOwnersOf(this))
{
grabbingWindow.ungrab();
}
}
// ---- EVENTS ---- //
/*
* Called by the delegate to dispatch the event to Java. Event
* coordinates are relative to non-client window are, i.e. the top-left
* point of the client area is (insets.top, insets.left).
*/
@Override
public void notifyMouseEvent(int id, long when, int button,
int x, int y, int absX, int absY,
int modifiers, int clickCount, boolean popupTrigger,
byte[] bdata)
{
// TODO: fill "bdata" member of AWTEvent
Rectangle r = getBounds();
// findPeerAt() expects parent coordinates
LWComponentPeer<?, ?> targetPeer = findPeerAt(r.x + x, r.y + y);
if (id == MouseEvent.MOUSE_EXITED) {
isMouseOver = false;
if (lastMouseEventPeer != null) {
if (lastMouseEventPeer.isEnabled()) {
Point lp = lastMouseEventPeer.windowToLocal(x, y,
this);
Component target = lastMouseEventPeer.getTarget();
postMouseExitedEvent(target, when, modifiers, lp,
absX, absY, clickCount, popupTrigger, button);
}
// Sometimes we may get MOUSE_EXITED after lastCommonMouseEventPeer is switched
// to a peer from another window. So we must first check if this peer is
// the same as lastWindowPeer
if (lastCommonMouseEventPeer != null && lastCommonMouseEventPeer.getWindowPeerOrSelf() == this) {
lastCommonMouseEventPeer = null;
}
lastMouseEventPeer = null;
}
} else if(id == MouseEvent.MOUSE_ENTERED) {
isMouseOver = true;
if (targetPeer != null) {
if (targetPeer.isEnabled()) {
Point lp = targetPeer.windowToLocal(x, y, this);
Component target = targetPeer.getTarget();
postMouseEnteredEvent(target, when, modifiers, lp,
absX, absY, clickCount, popupTrigger, button);
}
lastCommonMouseEventPeer = targetPeer;
lastMouseEventPeer = targetPeer;
}
} else {
PlatformWindow topmostPlatformWindow = LWToolkit.getLWToolkit().getPlatformWindowUnderMouse();
LWWindowPeer topmostWindowPeer =
topmostPlatformWindow != null ? topmostPlatformWindow.getPeer() : null;
// topmostWindowPeer == null condition is added for the backward
// compatibility with applets. It can be removed when the
// getTopmostPlatformWindowUnderMouse() method will be properly
// implemented in CPlatformEmbeddedFrame class
if (topmostWindowPeer == this || topmostWindowPeer == null) {
generateMouseEnterExitEventsForComponents(when, button, x, y,
absX, absY, modifiers, clickCount, popupTrigger,
targetPeer);
} else {
LWComponentPeer<?, ?> topmostTargetPeer = topmostWindowPeer.findPeerAt(r.x + x, r.y + y);
topmostWindowPeer.generateMouseEnterExitEventsForComponents(when, button, x, y,
absX, absY, modifiers, clickCount, popupTrigger,
topmostTargetPeer);
}
// TODO: fill "bdata" member of AWTEvent
int eventButtonMask = (button > 0)? MouseEvent.getMaskForButton(button) : 0;
int otherButtonsPressed = modifiers & ~eventButtonMask;
// For pressed/dragged/released events OS X treats other
// mouse buttons as if they were BUTTON2, so we do the same
int targetIdx = (button > 3) ? MouseEvent.BUTTON2 - 1 : button - 1;
// MOUSE_ENTERED/EXITED are generated for the components strictly under
// mouse even when dragging. That's why we first update lastMouseEventPeer
// based on initial targetPeer value and only then recalculate targetPeer
// for MOUSE_DRAGGED/RELEASED events
if (id == MouseEvent.MOUSE_PRESSED) {
// Ungrab only if this window is not an owned window of the grabbing one.
if (!isGrabbing() && grabbingWindow != null &&
!grabbingWindow.isOneOfOwnersOf(this))
{
grabbingWindow.ungrab();
}
if (otherButtonsPressed == 0) {
mouseClickButtons = eventButtonMask;
} else {
mouseClickButtons |= eventButtonMask;
}
// The window should be focused on mouse click. If it gets activated by the native platform,
// this request will be no op. It will take effect when:
// 1. A simple not focused window is clicked.
// 2. An active but not focused owner frame/dialog is clicked.
// The mouse event then will trigger a focus request "in window" to the component, so the window
// should gain focus before.
requestWindowFocus(FocusEvent.Cause.MOUSE_EVENT);
mouseDownTarget[targetIdx] = targetPeer;
} else if (id == MouseEvent.MOUSE_DRAGGED) {
// Cocoa dragged event has the information about which mouse
// button is being dragged. Use it to determine the peer that
// should receive the dragged event.
targetPeer = mouseDownTarget[targetIdx];
mouseClickButtons &= ~modifiers;
} else if (id == MouseEvent.MOUSE_RELEASED) {
// TODO: currently, mouse released event goes to the same component
// that received corresponding mouse pressed event. For most cases,
// it's OK, however, we need to make sure that our behavior is consistent
// with 1.6 for cases where component in question have been
// hidden/removed in between of mouse pressed/released events.
targetPeer = mouseDownTarget[targetIdx];
if ((modifiers & eventButtonMask) == 0) {
mouseDownTarget[targetIdx] = null;
}
// mouseClickButtons is updated below, after MOUSE_CLICK is sent
}
if (targetPeer == null) {
//TODO This can happen if this window is invisible. this is correct behavior in this case?
targetPeer = this;
}
Point lp = targetPeer.windowToLocal(x, y, this);
if (targetPeer.isEnabled()) {
MouseEvent event = new MouseEvent(targetPeer.getTarget(), id,
when, modifiers, lp.x, lp.y,
absX, absY, clickCount,
popupTrigger, button);
postEvent(event);
}
if (id == MouseEvent.MOUSE_RELEASED) {
if ((mouseClickButtons & eventButtonMask) != 0
&& targetPeer.isEnabled()) {
postEvent(new MouseEvent(targetPeer.getTarget(),
MouseEvent.MOUSE_CLICKED,
when, modifiers,
lp.x, lp.y, absX, absY,
clickCount, popupTrigger, button));
}
mouseClickButtons &= ~eventButtonMask;
}
}
notifyUpdateCursor();
}
private void generateMouseEnterExitEventsForComponents(long when,
int button, int x, int y, int screenX, int screenY,
int modifiers, int clickCount, boolean popupTrigger,
final LWComponentPeer<?, ?> targetPeer) {
if (!isMouseOver || targetPeer == lastMouseEventPeer) {
return;
}
// Generate Mouse Exit for components
if (lastMouseEventPeer != null && lastMouseEventPeer.isEnabled()) {
Point oldp = lastMouseEventPeer.windowToLocal(x, y, this);
Component target = lastMouseEventPeer.getTarget();
postMouseExitedEvent(target, when, modifiers, oldp, screenX, screenY,
clickCount, popupTrigger, button);
}
lastCommonMouseEventPeer = targetPeer;
lastMouseEventPeer = targetPeer;
// Generate Mouse Enter for components
if (targetPeer != null && targetPeer.isEnabled()) {
Point newp = targetPeer.windowToLocal(x, y, this);
Component target = targetPeer.getTarget();
postMouseEnteredEvent(target, when, modifiers, newp, screenX, screenY, clickCount, popupTrigger, button);
}
}
private void postMouseEnteredEvent(Component target, long when, int modifiers,
Point loc, int xAbs, int yAbs,
int clickCount, boolean popupTrigger, int button) {
updateSecurityWarningVisibility();
postEvent(new MouseEvent(target,
MouseEvent.MOUSE_ENTERED,
when, modifiers,
loc.x, loc.y, xAbs, yAbs,
clickCount, popupTrigger, button));