-
Notifications
You must be signed in to change notification settings - Fork 232
/
AWTWindow.m
1685 lines (1405 loc) · 60.1 KB
/
AWTWindow.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2011, 2018, 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.
*/
#import <Cocoa/Cocoa.h>
#import "sun_lwawt_macosx_CPlatformWindow.h"
#import "com_apple_eawt_event_GestureHandler.h"
#import "com_apple_eawt_FullScreenHandler.h"
#import "ApplicationDelegate.h"
#import "AWTWindow.h"
#import "AWTView.h"
#import "GeomUtilities.h"
#import "ThreadUtilities.h"
#import "JNIUtilities.h"
#define MASK(KEY) \
(sun_lwawt_macosx_CPlatformWindow_ ## KEY)
#define IS(BITS, KEY) \
((BITS & MASK(KEY)) != 0)
#define SET(BITS, KEY, VALUE) \
BITS = VALUE ? BITS | MASK(KEY) : BITS & ~MASK(KEY)
static jclass jc_CPlatformWindow = NULL;
#define GET_CPLATFORM_WINDOW_CLASS() \
GET_CLASS(jc_CPlatformWindow, "sun/lwawt/macosx/CPlatformWindow");
#define GET_CPLATFORM_WINDOW_CLASS_RETURN(ret) \
GET_CLASS_RETURN(jc_CPlatformWindow, "sun/lwawt/macosx/CPlatformWindow", ret);
// Cocoa windowDidBecomeKey/windowDidResignKey notifications
// doesn't provide information about "opposite" window, so we
// have to do a bit of tracking. This variable points to a window
// which had been the key window just before a new key window
// was set. It would be nil if the new key window isn't an AWT
// window or the app currently has no key window.
static AWTWindow* lastKeyWindow = nil;
// This variable contains coordinates of a window's top left
// which was positioned via java.awt.Window.setLocationByPlatform.
// It would be NSZeroPoint if 'Location by Platform' is not used.
static NSPoint lastTopLeftPoint;
// --------------------------------------------------------------
// NSWindow/NSPanel descendants implementation
#define AWT_NS_WINDOW_IMPLEMENTATION \
- (id) initWithDelegate:(AWTWindow *)delegate \
frameRect:(NSRect)contectRect \
styleMask:(NSUInteger)styleMask \
contentView:(NSView *)view \
{ \
self = [super initWithContentRect:contectRect \
styleMask:styleMask \
backing:NSBackingStoreBuffered \
defer:NO]; \
\
if (self == nil) return nil; \
\
[self setDelegate:delegate]; \
[self setContentView:view]; \
[self setInitialFirstResponder:view]; \
[self setReleasedWhenClosed:NO]; \
[self setPreservesContentDuringLiveResize:YES]; \
\
return self; \
} \
\
/* NSWindow overrides */ \
- (BOOL) canBecomeKeyWindow { \
return [(AWTWindow*)[self delegate] canBecomeKeyWindow]; \
} \
\
- (BOOL) canBecomeMainWindow { \
return [(AWTWindow*)[self delegate] canBecomeMainWindow]; \
} \
\
- (BOOL) worksWhenModal { \
return [(AWTWindow*)[self delegate] worksWhenModal]; \
} \
\
- (void)sendEvent:(NSEvent *)event { \
[(AWTWindow*)[self delegate] sendEvent:event]; \
[super sendEvent:event]; \
}
@implementation AWTWindow_Normal
AWT_NS_WINDOW_IMPLEMENTATION
// Gesture support
- (void)postGesture:(NSEvent *)event as:(jint)type a:(jdouble)a b:(jdouble)b {
AWT_ASSERT_APPKIT_THREAD;
JNIEnv *env = [ThreadUtilities getJNIEnv];
jobject platformWindow = (*env)->NewLocalRef(env, ((AWTWindow *)self.delegate).javaPlatformWindow);
if (platformWindow != NULL) {
// extract the target AWT Window object out of the CPlatformWindow
GET_CPLATFORM_WINDOW_CLASS();
DECLARE_FIELD(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;");
jobject awtWindow = (*env)->GetObjectField(env, platformWindow, jf_target);
if (awtWindow != NULL) {
// translate the point into Java coordinates
NSPoint loc = [event locationInWindow];
loc.y = [self frame].size.height - loc.y;
// send up to the GestureHandler to recursively dispatch on the AWT event thread
DECLARE_CLASS(jc_GestureHandler, "com/apple/eawt/event/GestureHandler");
DECLARE_STATIC_METHOD(sjm_handleGestureFromNative, jc_GestureHandler,
"handleGestureFromNative", "(Ljava/awt/Window;IDDDD)V");
(*env)->CallStaticVoidMethod(env, jc_GestureHandler, sjm_handleGestureFromNative,
awtWindow, type, (jdouble)loc.x, (jdouble)loc.y, (jdouble)a, (jdouble)b);
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, awtWindow);
}
(*env)->DeleteLocalRef(env, platformWindow);
}
}
- (void)beginGestureWithEvent:(NSEvent *)event {
[self postGesture:event
as:com_apple_eawt_event_GestureHandler_PHASE
a:-1.0
b:0.0];
}
- (void)endGestureWithEvent:(NSEvent *)event {
[self postGesture:event
as:com_apple_eawt_event_GestureHandler_PHASE
a:1.0
b:0.0];
}
- (void)magnifyWithEvent:(NSEvent *)event {
[self postGesture:event
as:com_apple_eawt_event_GestureHandler_MAGNIFY
a:[event magnification]
b:0.0];
}
- (void)rotateWithEvent:(NSEvent *)event {
[self postGesture:event
as:com_apple_eawt_event_GestureHandler_ROTATE
a:[event rotation]
b:0.0];
}
- (void)swipeWithEvent:(NSEvent *)event {
[self postGesture:event
as:com_apple_eawt_event_GestureHandler_SWIPE
a:[event deltaX]
b:[event deltaY]];
}
@end
@implementation AWTWindow_Panel
AWT_NS_WINDOW_IMPLEMENTATION
@end
// END of NSWindow/NSPanel descendants implementation
// --------------------------------------------------------------
@implementation AWTWindow
@synthesize nsWindow;
@synthesize javaPlatformWindow;
@synthesize javaMenuBar;
@synthesize javaMinSize;
@synthesize javaMaxSize;
@synthesize styleBits;
@synthesize isEnabled;
@synthesize ownerWindow;
@synthesize preFullScreenLevel;
@synthesize standardFrame;
@synthesize isMinimizing;
- (void) updateMinMaxSize:(BOOL)resizable {
if (resizable) {
[self.nsWindow setMinSize:self.javaMinSize];
[self.nsWindow setMaxSize:self.javaMaxSize];
} else {
NSRect currentFrame = [self.nsWindow frame];
[self.nsWindow setMinSize:currentFrame.size];
[self.nsWindow setMaxSize:currentFrame.size];
}
}
// creates a new NSWindow style mask based on the _STYLE_PROP_BITMASK bits
+ (NSUInteger) styleMaskForStyleBits:(jint)styleBits {
NSUInteger type = 0;
if (IS(styleBits, DECORATED)) {
type |= NSTitledWindowMask;
if (IS(styleBits, CLOSEABLE)) type |= NSClosableWindowMask;
if (IS(styleBits, RESIZABLE)) type |= NSResizableWindowMask;
if (IS(styleBits, FULL_WINDOW_CONTENT)) type |= NSFullSizeContentViewWindowMask;
} else {
type |= NSBorderlessWindowMask;
}
if (IS(styleBits, MINIMIZABLE)) type |= NSMiniaturizableWindowMask;
if (IS(styleBits, TEXTURED)) type |= NSTexturedBackgroundWindowMask;
if (IS(styleBits, UNIFIED)) type |= NSUnifiedTitleAndToolbarWindowMask;
if (IS(styleBits, UTILITY)) type |= NSUtilityWindowMask;
if (IS(styleBits, HUD)) type |= NSHUDWindowMask;
if (IS(styleBits, SHEET)) type |= NSDocModalWindowMask;
if (IS(styleBits, NONACTIVATING)) type |= NSNonactivatingPanelMask;
return type;
}
// updates _METHOD_PROP_BITMASK based properties on the window
- (void) setPropertiesForStyleBits:(jint)bits mask:(jint)mask {
if (IS(mask, RESIZABLE)) {
BOOL resizable = IS(bits, RESIZABLE);
[self updateMinMaxSize:resizable];
[self.nsWindow setShowsResizeIndicator:resizable];
// Zoom button should be disabled, if the window is not resizable,
// otherwise button should be restored to initial state.
BOOL zoom = resizable && IS(bits, ZOOMABLE);
[[self.nsWindow standardWindowButton:NSWindowZoomButton] setEnabled:zoom];
}
if (IS(mask, HAS_SHADOW)) {
[self.nsWindow setHasShadow:IS(bits, HAS_SHADOW)];
}
if (IS(mask, ZOOMABLE)) {
[[self.nsWindow standardWindowButton:NSWindowZoomButton] setEnabled:IS(bits, ZOOMABLE)];
}
if (IS(mask, ALWAYS_ON_TOP)) {
[self.nsWindow setLevel:IS(bits, ALWAYS_ON_TOP) ? NSFloatingWindowLevel : NSNormalWindowLevel];
}
if (IS(mask, HIDES_ON_DEACTIVATE)) {
[self.nsWindow setHidesOnDeactivate:IS(bits, HIDES_ON_DEACTIVATE)];
}
if (IS(mask, DRAGGABLE_BACKGROUND)) {
[self.nsWindow setMovableByWindowBackground:IS(bits, DRAGGABLE_BACKGROUND)];
}
if (IS(mask, DOCUMENT_MODIFIED)) {
[self.nsWindow setDocumentEdited:IS(bits, DOCUMENT_MODIFIED)];
}
if (IS(mask, FULLSCREENABLE) && [self.nsWindow respondsToSelector:@selector(toggleFullScreen:)]) {
if (IS(bits, FULLSCREENABLE)) {
[self.nsWindow setCollectionBehavior:(1 << 7) /*NSWindowCollectionBehaviorFullScreenPrimary*/];
} else {
[self.nsWindow setCollectionBehavior:NSWindowCollectionBehaviorDefault];
}
}
if (IS(mask, TRANSPARENT_TITLE_BAR) && [self.nsWindow respondsToSelector:@selector(setTitlebarAppearsTransparent:)]) {
[self.nsWindow setTitlebarAppearsTransparent:IS(bits, TRANSPARENT_TITLE_BAR)];
}
}
- (id) initWithPlatformWindow:(jobject)platformWindow
ownerWindow:owner
styleBits:(jint)bits
frameRect:(NSRect)rect
contentView:(NSView *)view
{
AWT_ASSERT_APPKIT_THREAD;
NSUInteger styleMask = [AWTWindow styleMaskForStyleBits:bits];
NSRect contentRect = rect; //[NSWindow contentRectForFrameRect:rect styleMask:styleMask];
if (contentRect.size.width <= 0.0) {
contentRect.size.width = 1.0;
}
if (contentRect.size.height <= 0.0) {
contentRect.size.height = 1.0;
}
self = [super init];
if (self == nil) return nil; // no hope
if (IS(bits, UTILITY) ||
IS(bits, NONACTIVATING) ||
IS(bits, HUD) ||
IS(bits, HIDES_ON_DEACTIVATE))
{
self.nsWindow = [[AWTWindow_Panel alloc] initWithDelegate:self
frameRect:contentRect
styleMask:styleMask
contentView:view];
}
else
{
// These windows will appear in the window list in the dock icon menu
self.nsWindow = [[AWTWindow_Normal alloc] initWithDelegate:self
frameRect:contentRect
styleMask:styleMask
contentView:view];
}
if (self.nsWindow == nil) return nil; // no hope either
[self.nsWindow release]; // the property retains the object already
self.isEnabled = YES;
self.isMinimizing = NO;
self.javaPlatformWindow = platformWindow;
self.styleBits = bits;
self.ownerWindow = owner;
[self setPropertiesForStyleBits:styleBits mask:MASK(_METHOD_PROP_BITMASK)];
if (IS(self.styleBits, IS_POPUP)) {
[self.nsWindow setCollectionBehavior:(1 << 8) /*NSWindowCollectionBehaviorFullScreenAuxiliary*/];
}
return self;
}
+ (BOOL) isAWTWindow:(NSWindow *)window {
return [window isKindOfClass: [AWTWindow_Panel class]] || [window isKindOfClass: [AWTWindow_Normal class]];
}
// Retrieves the list of possible window layers (levels)
+ (NSArray*) getWindowLayers {
static NSArray *windowLayers;
static dispatch_once_t token;
// Initialize the list of possible window layers
dispatch_once(&token, ^{
// The layers are ordered from front to back, (i.e. the toppest one is the first)
windowLayers = [NSArray arrayWithObjects:
[NSNumber numberWithInt:CGWindowLevelForKey(kCGPopUpMenuWindowLevelKey)],
[NSNumber numberWithInt:CGWindowLevelForKey(kCGFloatingWindowLevelKey)],
[NSNumber numberWithInt:CGWindowLevelForKey(kCGNormalWindowLevelKey)],
nil
];
[windowLayers retain];
});
return windowLayers;
}
// returns id for the topmost window under mouse
+ (NSInteger) getTopmostWindowUnderMouseID {
NSInteger result = -1;
NSArray *windowLayers = [AWTWindow getWindowLayers];
// Looking for the window under mouse starting from the toppest layer
for (NSNumber *layer in windowLayers) {
result = [AWTWindow getTopmostWindowUnderMouseIDImpl:[layer integerValue]];
if (result != -1) {
break;
}
}
return result;
}
+ (NSInteger) getTopmostWindowUnderMouseIDImpl:(NSInteger)windowLayer {
NSInteger result = -1;
NSRect screenRect = [[NSScreen mainScreen] frame];
NSPoint nsMouseLocation = [NSEvent mouseLocation];
CGPoint cgMouseLocation = CGPointMake(nsMouseLocation.x, screenRect.size.height - nsMouseLocation.y);
NSMutableArray *windows = (NSMutableArray *)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
for (NSDictionary *window in windows) {
NSInteger layer = [[window objectForKey:(id)kCGWindowLayer] integerValue];
if (layer == windowLayer) {
CGRect rect;
CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[window objectForKey:(id)kCGWindowBounds], &rect);
if (CGRectContainsPoint(rect, cgMouseLocation)) {
result = [[window objectForKey:(id)kCGWindowNumber] integerValue];
break;
}
}
}
[windows release];
return result;
}
// checks that this window is under the mouse cursor and this point is not overlapped by others windows
- (BOOL) isTopmostWindowUnderMouse {
return [self.nsWindow windowNumber] == [AWTWindow getTopmostWindowUnderMouseID];
}
+ (AWTWindow *) getTopmostWindowUnderMouse {
NSEnumerator *windowEnumerator = [[NSApp windows] objectEnumerator];
NSWindow *window;
NSInteger topmostWindowUnderMouseID = [AWTWindow getTopmostWindowUnderMouseID];
while ((window = [windowEnumerator nextObject]) != nil) {
if ([window windowNumber] == topmostWindowUnderMouseID) {
BOOL isAWTWindow = [AWTWindow isAWTWindow: window];
return isAWTWindow ? (AWTWindow *) [window delegate] : nil;
}
}
return nil;
}
+ (void) synthesizeMouseEnteredExitedEvents:(NSWindow*)window withType:(NSEventType)eventType {
NSPoint screenLocation = [NSEvent mouseLocation];
NSPoint windowLocation = [window convertScreenToBase: screenLocation];
int modifierFlags = (eventType == NSMouseEntered) ? NSMouseEnteredMask : NSMouseExitedMask;
NSEvent *mouseEvent = [NSEvent enterExitEventWithType: eventType
location: windowLocation
modifierFlags: modifierFlags
timestamp: 0
windowNumber: [window windowNumber]
context: nil
eventNumber: 0
trackingNumber: 0
userData: nil
];
[[window contentView] deliverJavaMouseEvent: mouseEvent];
}
+ (void) synthesizeMouseEnteredExitedEventsForAllWindows {
NSInteger topmostWindowUnderMouseID = [AWTWindow getTopmostWindowUnderMouseID];
NSArray *windows = [NSApp windows];
NSWindow *window;
NSEnumerator *windowEnumerator = [windows objectEnumerator];
while ((window = [windowEnumerator nextObject]) != nil) {
if ([AWTWindow isAWTWindow: window]) {
BOOL isUnderMouse = ([window windowNumber] == topmostWindowUnderMouseID);
BOOL mouseIsOver = [[window contentView] mouseIsOver];
if (isUnderMouse && !mouseIsOver) {
[AWTWindow synthesizeMouseEnteredExitedEvents:window withType:NSMouseEntered];
} else if (!isUnderMouse && mouseIsOver) {
[AWTWindow synthesizeMouseEnteredExitedEvents:window withType:NSMouseExited];
}
}
}
}
+ (NSNumber *) getNSWindowDisplayID_AppKitThread:(NSWindow *)window {
AWT_ASSERT_APPKIT_THREAD;
NSScreen *screen = [window screen];
NSDictionary *deviceDescription = [screen deviceDescription];
return [deviceDescription objectForKey:@"NSScreenNumber"];
}
- (void) dealloc {
AWT_ASSERT_APPKIT_THREAD;
JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
(*env)->DeleteWeakGlobalRef(env, self.javaPlatformWindow);
self.javaPlatformWindow = nil;
self.nsWindow = nil;
self.ownerWindow = nil;
[super dealloc];
}
// Tests whether window is blocked by modal dialog/window
- (BOOL) isBlocked {
BOOL isBlocked = NO;
JNIEnv *env = [ThreadUtilities getJNIEnv];
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
GET_CPLATFORM_WINDOW_CLASS_RETURN(isBlocked);
DECLARE_METHOD_RETURN(jm_isBlocked, jc_CPlatformWindow, "isBlocked", "()Z", isBlocked);
isBlocked = (*env)->CallBooleanMethod(env, platformWindow, jm_isBlocked) == JNI_TRUE ? YES : NO;
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, platformWindow);
}
return isBlocked;
}
// Test whether window is simple window and owned by embedded frame
- (BOOL) isSimpleWindowOwnedByEmbeddedFrame {
BOOL isSimpleWindowOwnedByEmbeddedFrame = NO;
JNIEnv *env = [ThreadUtilities getJNIEnv];
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
GET_CPLATFORM_WINDOW_CLASS_RETURN(NO);
DECLARE_METHOD_RETURN(jm_isBlocked, jc_CPlatformWindow, "isSimpleWindowOwnedByEmbeddedFrame", "()Z", NO);
isSimpleWindowOwnedByEmbeddedFrame = (*env)->CallBooleanMethod(env, platformWindow, jm_isBlocked) == JNI_TRUE ? YES : NO;
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, platformWindow);
}
return isSimpleWindowOwnedByEmbeddedFrame;
}
// Tests whether the corresponding Java platform window is visible or not
+ (BOOL) isJavaPlatformWindowVisible:(NSWindow *)window {
BOOL isVisible = NO;
if ([AWTWindow isAWTWindow:window] && [window delegate] != nil) {
AWTWindow *awtWindow = (AWTWindow *)[window delegate];
[AWTToolkit eventCountPlusPlus];
JNIEnv *env = [ThreadUtilities getJNIEnv];
jobject platformWindow = (*env)->NewLocalRef(env, awtWindow.javaPlatformWindow);
if (platformWindow != NULL) {
GET_CPLATFORM_WINDOW_CLASS_RETURN(isVisible);
DECLARE_METHOD_RETURN(jm_isVisible, jc_CPlatformWindow, "isVisible", "()Z", isVisible)
isVisible = (*env)->CallBooleanMethod(env, platformWindow, jm_isVisible) == JNI_TRUE ? YES : NO;
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, platformWindow);
}
}
return isVisible;
}
// Orders window's childs based on the current focus state
- (void) orderChildWindows:(BOOL)focus {
AWT_ASSERT_APPKIT_THREAD;
if (self.isMinimizing || [self isBlocked]) {
// Do not perform any ordering, if iconify is in progress
// or the window is blocked by a modal window
return;
}
NSEnumerator *windowEnumerator = [[NSApp windows]objectEnumerator];
NSWindow *window;
while ((window = [windowEnumerator nextObject]) != nil) {
if ([AWTWindow isJavaPlatformWindowVisible:window]) {
AWTWindow *awtWindow = (AWTWindow *)[window delegate];
AWTWindow *owner = awtWindow.ownerWindow;
if (IS(awtWindow.styleBits, ALWAYS_ON_TOP)) {
// Do not order 'always on top' windows
continue;
}
while (awtWindow.ownerWindow != nil) {
if (awtWindow.ownerWindow == self) {
if (focus) {
// Move the childWindow to floating level
// so it will appear in front of its
// parent which owns the focus
[window setLevel:NSFloatingWindowLevel];
} else {
// Focus owner has changed, move the childWindow
// back to normal window level
[window setLevel:NSNormalWindowLevel];
}
// The childWindow should be displayed in front of
// its nearest parentWindow
[window orderWindow:NSWindowAbove relativeTo:[owner.nsWindow windowNumber]];
break;
}
awtWindow = awtWindow.ownerWindow;
}
}
}
}
// NSWindow overrides
- (BOOL) canBecomeKeyWindow {
AWT_ASSERT_APPKIT_THREAD;
return self.isEnabled && (IS(self.styleBits, SHOULD_BECOME_KEY) || [self isSimpleWindowOwnedByEmbeddedFrame]);
}
- (BOOL) canBecomeMainWindow {
AWT_ASSERT_APPKIT_THREAD;
if (!self.isEnabled) {
// Native system can bring up the NSWindow to
// the top even if the window is not main.
// We should bring up the modal dialog manually
[AWTToolkit eventCountPlusPlus];
JNIEnv *env = [ThreadUtilities getJNIEnv];
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
GET_CPLATFORM_WINDOW_CLASS_RETURN(NO);
DECLARE_METHOD_RETURN(jm_checkBlockingAndOrder, jc_CPlatformWindow, "checkBlockingAndOrder", "()Z", NO);
(*env)->CallBooleanMethod(env, platformWindow, jm_checkBlockingAndOrder);
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, platformWindow);
}
}
return self.isEnabled && IS(self.styleBits, SHOULD_BECOME_MAIN);
}
- (BOOL) worksWhenModal {
AWT_ASSERT_APPKIT_THREAD;
return IS(self.styleBits, MODAL_EXCLUDED);
}
// NSWindowDelegate methods
- (void) _deliverMoveResizeEvent {
AWT_ASSERT_APPKIT_THREAD;
// deliver the event if this is a user-initiated live resize or as a side-effect
// of a Java initiated resize, because AppKit can override the bounds and force
// the bounds of the window to avoid the Dock or remain on screen.
[AWTToolkit eventCountPlusPlus];
JNIEnv *env = [ThreadUtilities getJNIEnv];
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow == NULL) {
// TODO: create generic AWT assert
}
NSRect frame = ConvertNSScreenRect(env, [self.nsWindow frame]);
GET_CPLATFORM_WINDOW_CLASS();
DECLARE_METHOD(jm_deliverMoveResizeEvent, jc_CPlatformWindow, "deliverMoveResizeEvent", "(IIIIZ)V");
(*env)->CallVoidMethod(env, platformWindow, jm_deliverMoveResizeEvent,
(jint)frame.origin.x,
(jint)frame.origin.y,
(jint)frame.size.width,
(jint)frame.size.height,
(jboolean)[self.nsWindow inLiveResize]);
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, platformWindow);
[AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
}
- (void)windowDidMove:(NSNotification *)notification {
AWT_ASSERT_APPKIT_THREAD;
[self _deliverMoveResizeEvent];
}
- (void)windowDidResize:(NSNotification *)notification {
AWT_ASSERT_APPKIT_THREAD;
[self _deliverMoveResizeEvent];
}
- (void)windowDidExpose:(NSNotification *)notification {
AWT_ASSERT_APPKIT_THREAD;
[AWTToolkit eventCountPlusPlus];
// TODO: don't see this callback invoked anytime so we track
// window exposing in _setVisible:(BOOL)
}
- (NSRect)windowWillUseStandardFrame:(NSWindow *)window
defaultFrame:(NSRect)newFrame {
return NSEqualSizes(NSZeroSize, [self standardFrame].size)
? newFrame
: [self standardFrame];
}
// Hides/shows window's childs during iconify/de-iconify operation
- (void) iconifyChildWindows:(BOOL)iconify {
AWT_ASSERT_APPKIT_THREAD;
NSEnumerator *windowEnumerator = [[NSApp windows]objectEnumerator];
NSWindow *window;
while ((window = [windowEnumerator nextObject]) != nil) {
if ([AWTWindow isJavaPlatformWindowVisible:window]) {
AWTWindow *awtWindow = (AWTWindow *)[window delegate];
while (awtWindow.ownerWindow != nil) {
if (awtWindow.ownerWindow == self) {
if (iconify) {
[window orderOut:window];
} else {
[window orderFront:window];
}
break;
}
awtWindow = awtWindow.ownerWindow;
}
}
}
}
- (void) _deliverIconify:(BOOL)iconify {
AWT_ASSERT_APPKIT_THREAD;
[AWTToolkit eventCountPlusPlus];
JNIEnv *env = [ThreadUtilities getJNIEnv];
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
GET_CPLATFORM_WINDOW_CLASS();
DECLARE_METHOD(jm_deliverIconify, jc_CPlatformWindow, "deliverIconify", "(Z)V");
(*env)->CallVoidMethod(env, platformWindow, jm_deliverIconify, iconify);
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, platformWindow);
}
}
- (void)windowWillMiniaturize:(NSNotification *)notification {
AWT_ASSERT_APPKIT_THREAD;
self.isMinimizing = YES;
JNIEnv *env = [ThreadUtilities getJNIEnv];
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
GET_CPLATFORM_WINDOW_CLASS();
DECLARE_METHOD(jm_windowWillMiniaturize, jc_CPlatformWindow, "windowWillMiniaturize", "()V");
(*env)->CallVoidMethod(env, platformWindow, jm_windowWillMiniaturize);
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, platformWindow);
}
// Explicitly make myself a key window to avoid possible
// negative visual effects during iconify operation
[self.nsWindow makeKeyAndOrderFront:self.nsWindow];
[self iconifyChildWindows:YES];
}
- (void)windowDidMiniaturize:(NSNotification *)notification {
AWT_ASSERT_APPKIT_THREAD;
[self _deliverIconify:JNI_TRUE];
self.isMinimizing = NO;
}
- (void)windowDidDeminiaturize:(NSNotification *)notification {
AWT_ASSERT_APPKIT_THREAD;
[self _deliverIconify:JNI_FALSE];
[self iconifyChildWindows:NO];
}
- (void) _deliverWindowFocusEvent:(BOOL)focused oppositeWindow:(AWTWindow *)opposite {
//AWT_ASSERT_APPKIT_THREAD;
JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
jobject oppositeWindow = (*env)->NewLocalRef(env, opposite.javaPlatformWindow);
GET_CPLATFORM_WINDOW_CLASS();
DECLARE_METHOD(jm_deliverWindowFocusEvent, jc_CPlatformWindow, "deliverWindowFocusEvent", "(ZLsun/lwawt/macosx/CPlatformWindow;)V");
(*env)->CallVoidMethod(env, platformWindow, jm_deliverWindowFocusEvent, (jboolean)focused, oppositeWindow);
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, platformWindow);
(*env)->DeleteLocalRef(env, oppositeWindow);
}
}
- (void) windowDidBecomeMain: (NSNotification *) notification {
AWT_ASSERT_APPKIT_THREAD;
[AWTToolkit eventCountPlusPlus];
#ifdef DEBUG
NSLog(@"became main: %d %@ %@", [self.nsWindow isKeyWindow], [self.nsWindow title], [self menuBarForWindow]);
#endif
if (![self.nsWindow isKeyWindow]) {
[self activateWindowMenuBar];
}
JNIEnv *env = [ThreadUtilities getJNIEnv];
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
GET_CPLATFORM_WINDOW_CLASS();
DECLARE_METHOD(jm_windowDidBecomeMain, jc_CPlatformWindow, "windowDidBecomeMain", "()V");
(*env)->CallVoidMethod(env, platformWindow, jm_windowDidBecomeMain);
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, platformWindow);
}
}
- (void) windowDidBecomeKey: (NSNotification *) notification {
AWT_ASSERT_APPKIT_THREAD;
[AWTToolkit eventCountPlusPlus];
#ifdef DEBUG
NSLog(@"became key: %d %@ %@", [self.nsWindow isMainWindow], [self.nsWindow title], [self menuBarForWindow]);
#endif
AWTWindow *opposite = [AWTWindow lastKeyWindow];
if (![self.nsWindow isMainWindow]) {
[self activateWindowMenuBar];
}
[AWTWindow setLastKeyWindow:nil];
[self _deliverWindowFocusEvent:YES oppositeWindow: opposite];
[self orderChildWindows:YES];
}
- (void) activateWindowMenuBar {
AWT_ASSERT_APPKIT_THREAD;
// Finds appropriate menubar in our hierarchy
AWTWindow *awtWindow = self;
while (awtWindow.ownerWindow != nil) {
awtWindow = awtWindow.ownerWindow;
}
CMenuBar *menuBar = nil;
BOOL isDisabled = NO;
if ([awtWindow.nsWindow isVisible]){
menuBar = awtWindow.javaMenuBar;
isDisabled = !awtWindow.isEnabled;
}
if (menuBar == nil) {
menuBar = [[ApplicationDelegate sharedDelegate] defaultMenuBar];
isDisabled = NO;
}
[CMenuBar activate:menuBar modallyDisabled:isDisabled];
}
#ifdef DEBUG
- (CMenuBar *) menuBarForWindow {
AWT_ASSERT_APPKIT_THREAD;
AWTWindow *awtWindow = self;
while (awtWindow.ownerWindow != nil) {
awtWindow = awtWindow.ownerWindow;
}
return awtWindow.javaMenuBar;
}
#endif
- (void) windowDidResignKey: (NSNotification *) notification {
// TODO: check why sometimes at start is invoked *not* on AppKit main thread.
AWT_ASSERT_APPKIT_THREAD;
[AWTToolkit eventCountPlusPlus];
#ifdef DEBUG
NSLog(@"resigned key: %d %@ %@", [self.nsWindow isMainWindow], [self.nsWindow title], [self menuBarForWindow]);
#endif
if (![self.nsWindow isMainWindow]) {
[self deactivateWindow];
}
}
- (void) windowDidResignMain: (NSNotification *) notification {
AWT_ASSERT_APPKIT_THREAD;
[AWTToolkit eventCountPlusPlus];
#ifdef DEBUG
NSLog(@"resigned main: %d %@ %@", [self.nsWindow isKeyWindow], [self.nsWindow title], [self menuBarForWindow]);
#endif
if (![self.nsWindow isKeyWindow]) {
[self deactivateWindow];
}
}
- (void) deactivateWindow {
AWT_ASSERT_APPKIT_THREAD;
#ifdef DEBUG
NSLog(@"deactivating window: %@", [self.nsWindow title]);
#endif
[self.javaMenuBar deactivate];
// the new key window
NSWindow *keyWindow = [NSApp keyWindow];
AWTWindow *opposite = nil;
if ([AWTWindow isAWTWindow: keyWindow]) {
opposite = (AWTWindow *)[keyWindow delegate];
[AWTWindow setLastKeyWindow: self];
} else {
[AWTWindow setLastKeyWindow: nil];
}
[self _deliverWindowFocusEvent:NO oppositeWindow: opposite];
[self orderChildWindows:NO];
}
- (BOOL)windowShouldClose:(id)sender {
AWT_ASSERT_APPKIT_THREAD;
[AWTToolkit eventCountPlusPlus];
JNIEnv *env = [ThreadUtilities getJNIEnv];
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
GET_CPLATFORM_WINDOW_CLASS_RETURN(NO);
DECLARE_METHOD_RETURN(jm_deliverWindowClosingEvent, jc_CPlatformWindow, "deliverWindowClosingEvent", "()V", NO);
(*env)->CallVoidMethod(env, platformWindow, jm_deliverWindowClosingEvent);
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, platformWindow);
}
// The window will be closed (if allowed) as result of sending Java event
return NO;
}
- (void)_notifyFullScreenOp:(jint)op withEnv:(JNIEnv *)env {
DECLARE_CLASS(jc_FullScreenHandler, "com/apple/eawt/FullScreenHandler");
DECLARE_STATIC_METHOD(jm_notifyFullScreenOperation, jc_FullScreenHandler,
"handleFullScreenEventFromNative", "(Ljava/awt/Window;I)V");
GET_CPLATFORM_WINDOW_CLASS();
DECLARE_FIELD(jf_target, jc_CPlatformWindow, "target", "Ljava/awt/Window;");
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
jobject awtWindow = (*env)->GetObjectField(env, platformWindow, jf_target);
if (awtWindow != NULL) {
(*env)->CallStaticVoidMethod(env, jc_FullScreenHandler, jm_notifyFullScreenOperation, awtWindow, op);
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, awtWindow);
}
(*env)->DeleteLocalRef(env, platformWindow);
}
}
- (void)windowWillEnterFullScreen:(NSNotification *)notification {
JNIEnv *env = [ThreadUtilities getJNIEnv];
GET_CPLATFORM_WINDOW_CLASS();
DECLARE_METHOD(jm_windowWillEnterFullScreen, jc_CPlatformWindow, "windowWillEnterFullScreen", "()V");
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
(*env)->CallVoidMethod(env, platformWindow, jm_windowWillEnterFullScreen);
CHECK_EXCEPTION();
[self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_WILL_ENTER withEnv:env];
(*env)->DeleteLocalRef(env, platformWindow);
}
}
- (void)windowDidEnterFullScreen:(NSNotification *)notification {
JNIEnv *env = [ThreadUtilities getJNIEnv];
GET_CPLATFORM_WINDOW_CLASS();
DECLARE_METHOD(jm_windowDidEnterFullScreen, jc_CPlatformWindow, "windowDidEnterFullScreen", "()V");
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
(*env)->CallVoidMethod(env, platformWindow, jm_windowDidEnterFullScreen);
CHECK_EXCEPTION();
[self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_ENTER withEnv:env];
(*env)->DeleteLocalRef(env, platformWindow);
}
[AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
}
- (void)windowWillExitFullScreen:(NSNotification *)notification {
JNIEnv *env = [ThreadUtilities getJNIEnv];
GET_CPLATFORM_WINDOW_CLASS();
DECLARE_METHOD(jm_windowWillExitFullScreen, jc_CPlatformWindow, "windowWillExitFullScreen", "()V");
if (jm_windowWillExitFullScreen == NULL) {
GET_CPLATFORM_WINDOW_CLASS();
jm_windowWillExitFullScreen = (*env)->GetMethodID(env, jc_CPlatformWindow, "windowWillExitFullScreen", "()V");
}
CHECK_NULL(jm_windowWillExitFullScreen);
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
(*env)->CallVoidMethod(env, platformWindow, jm_windowWillExitFullScreen);
CHECK_EXCEPTION();
[self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_WILL_EXIT withEnv:env];
(*env)->DeleteLocalRef(env, platformWindow);
}
}
- (void)windowDidExitFullScreen:(NSNotification *)notification {
JNIEnv *env = [ThreadUtilities getJNIEnv];
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
GET_CPLATFORM_WINDOW_CLASS();
DECLARE_METHOD(jm_windowDidExitFullScreen, jc_CPlatformWindow, "windowDidExitFullScreen", "()V");
(*env)->CallVoidMethod(env, platformWindow, jm_windowDidExitFullScreen);
CHECK_EXCEPTION();
[self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_EXIT withEnv:env];
(*env)->DeleteLocalRef(env, platformWindow);
}
[AWTWindow synthesizeMouseEnteredExitedEventsForAllWindows];
}
- (void)sendEvent:(NSEvent *)event {
if ([event type] == NSLeftMouseDown || [event type] == NSRightMouseDown || [event type] == NSOtherMouseDown) {
if ([self isBlocked]) {
// Move parent windows to front and make sure that a child window is displayed
// in front of its nearest parent.
if (self.ownerWindow != nil) {
JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
GET_CPLATFORM_WINDOW_CLASS();
DECLARE_METHOD(jm_orderAboveSiblings, jc_CPlatformWindow, "orderAboveSiblings", "()V");
(*env)->CallVoidMethod(env,platformWindow, jm_orderAboveSiblings);
CHECK_EXCEPTION();
(*env)->DeleteLocalRef(env, platformWindow);
}
}
[self orderChildWindows:YES];
}
NSPoint p = [NSEvent mouseLocation];
NSRect frame = [self.nsWindow frame];
NSRect contentRect = [self.nsWindow contentRectForFrameRect:frame];
// Check if the click happened in the non-client area (title bar)
if (p.y >= (frame.origin.y + contentRect.size.height)) {
JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
jobject platformWindow = (*env)->NewLocalRef(env, self.javaPlatformWindow);
if (platformWindow != NULL) {
// Currently, no need to deliver the whole NSEvent.
GET_CPLATFORM_WINDOW_CLASS();