1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #import "common.h"
  27 #import "com_sun_glass_events_ViewEvent.h"
  28 #import "com_sun_glass_events_WindowEvent.h"
  29 #import "com_sun_glass_ui_Window.h"
  30 #import "com_sun_glass_ui_Window_Level.h"
  31 #import "com_sun_glass_ui_mac_MacWindow.h"
  32 
  33 #import "GlassMacros.h"
  34 #import "GlassWindow.h"
  35 #import "GlassWindow+Java.h"
  36 #import "GlassWindow+Overrides.h"
  37 #import "GlassEmbeddedWindow+Overrides.h"
  38 #import "GlassView.h"
  39 #import "GlassScreen.h"
  40 #import "GlassTouches.h"
  41 #import "GlassApplication.h"
  42 #import "GlassLayer3D.h"
  43 #import "GlassHelper.h"
  44 
  45 //#include <stdio.h>
  46 //#include <stdarg.h>
  47 //
  48 //void glass_logf(char *fmt, ...) {
  49 //    va_list argp;
  50 //    va_start(argp, fmt);
  51 //    FILE *f = fopen("/tmp/glass.log", "a");
  52 //    vfprintf(f, fmt, argp);
  53 //    fclose(f);
  54 //}
  55 //
  56 //void Sys_out_sprintf(JNIEnv *env, char *fmt, ...) {
  57 //    va_list argp;
  58 //    va_start(argp, fmt);
  59 //    char buffer[4096];
  60 //    vsprintf(buffer, fmt, argp);
  61 ////    glass_logf("str = %s", buffer);
  62 ////    return;
  63 //    jclass sysClass = (*env)->FindClass(env, "java/lang/System");
  64 //    if (!sysClass) { glass_logf("Null finding System class\n"); return; }
  65 //    jclass psClass = (*env)->FindClass(env, "java/io/PrintStream");
  66 //    if (!psClass) { glass_logf("Null finding PrintStream class\n"); return; }
  67 //    jfieldID outField = (*env)->GetStaticFieldID(env, sysClass, "out", "Ljava/io/PrintStream;");
  68 //    if (!outField) { glass_logf("Null finding System.out field\n"); return; }
  69 //    jmethodID printMethod = (*env)->GetMethodID(env, psClass, "print", "(Ljava/lang/String;)V");
  70 //    if (!printMethod) { glass_logf("Null finding print method\n"); return; }
  71 //    jobject outStream = (*env)->GetStaticObjectField(env, sysClass, outField);
  72 //    if (!outStream) { glass_logf("Null getting System.out object\n"); return; }
  73 //    jstring theString = (*env)->NewStringUTF(env, buffer);
  74 //    if (!theString) { glass_logf("Null creating String object\n"); return; }
  75 //    (*env)->CallVoidMethod(env, outStream, printMethod, theString);
  76 //    (*env)->DeleteLocalRef(env, theString);
  77 //}
  78 
  79 //#define VERBOSE
  80 #ifndef VERBOSE
  81     #define LOG(MSG, ...)
  82 #else
  83     #define LOG(MSG, ...) GLASS_LOG(MSG, ## __VA_ARGS__);
  84 #endif
  85 
  86 #define BROWSER_PARENT_ID -1L
  87 
  88 #pragma mark --- Internal utilities
  89 
  90 static inline GlassWindow *getGlassWindow(JNIEnv *env, jlong jPtr)
  91 {
  92     assert(jPtr != 0L);
  93 
  94     NSWindow * nsWindow = (NSWindow*)jlong_to_ptr(jPtr);
  95     return (GlassWindow*)[nsWindow delegate];
  96 }
  97 
  98 static inline NSView<GlassView> *getMacView(JNIEnv *env, jobject jview)
  99 {
 100     if (jview != NULL)
 101     {
 102         jfieldID jfID = (*env)->GetFieldID(env, jViewClass, "ptr", "J");
 103         GLASS_CHECK_EXCEPTION(env);
 104         return (NSView<GlassView>*)jlong_to_ptr((*env)->GetLongField(env, jview, jfID));
 105     }
 106     else
 107     {
 108         return nil;
 109     }
 110 }
 111 
 112 // --------------------------------------------------------------------------------------
 113 // NSWindow/NSPanel descendants implementation
 114 #define GLASS_NS_WINDOW_IMPLEMENTATION                                                  \
 115 - (id)initWithDelegate:(GlassWindow*)delegate                                           \
 116              frameRect:(NSRect)rect                                                     \
 117              styleMask:(NSUInteger)styleMask                                            \
 118                 screen:(NSScreen*)screen                                                \
 119 {                                                                                       \
 120     self->gWindow = delegate; /* must be done before calling [super init...] */         \
 121     self = [super initWithContentRect:rect                                              \
 122                             styleMask:styleMask                                         \
 123                               backing:NSBackingStoreBuffered                            \
 124                                 defer:NO                                                \
 125                                screen:screen];                                          \
 126                                                                                         \
 127     if (self == nil) {                                                                  \
 128         return nil;                                                                     \
 129     }                                                                                   \
 130                                                                                         \
 131     [self setDelegate:delegate];                                                        \
 132     [self setAcceptsMouseMovedEvents:NO];                                               \
 133     [self setShowsResizeIndicator:NO];                                                  \
 134     [self setAllowsConcurrentViewDrawing:YES];                                          \
 135                                                                                         \
 136     [self setReleasedWhenClosed:YES];                                                   \
 137                                                                                         \
 138     return self;                                                                        \
 139 }                                                                                       \
 140                                                                                         \
 141 - (void)dealloc                                                                         \
 142 {                                                                                       \
 143     id window = self->gWindow;                                                          \
 144     LOG("dealloc window: %p", window);                                                  \
 145     [super dealloc];                                                                    \
 146     [window release];                                                                   \
 147 }                                                                                       \
 148                                                                                         \
 149 - (void)close                                                                           \
 150 {                                                                                       \
 151     self->gWindow->isClosed = YES;                                                      \
 152     [self->gWindow close];                                                              \
 153     LOG("gWindow close: %p", self->gWindow);                                            \
 154     [super close];                                                                      \
 155     LOG("super close");                                                                 \
 156 }                                                                                       \
 157 /* super calls NSWindow on the next run-loop pass when NSWindow could be released */    \
 158 - (BOOL)performKeyEquivalent:(NSEvent *)theEvent                                        \
 159 {                                                                                       \
 160     [self retain];                                                                      \
 161     BOOL result = [super performKeyEquivalent:theEvent];                                \
 162     result = result || self->gWindow->isClosed;                                         \
 163     [self release];                                                                     \
 164     return result;                                                                      \
 165 }                                                                                       \
 166 - (BOOL)canBecomeMainWindow                                                             \
 167 {                                                                                       \
 168     return [self->gWindow canBecomeMainWindow];                                         \
 169 }                                                                                       \
 170 - (BOOL)canBecomeKeyWindow                                                              \
 171 {                                                                                       \
 172     return [self->gWindow canBecomeKeyWindow];                                          \
 173 }                                                                                       \
 174 - (void)setHidesOnDeactivate:(BOOL)hideOnDeactivate                                     \
 175 {                                                                                       \
 176     [super setHidesOnDeactivate:NO];                                                    \
 177 }                                                                                       \
 178 - (BOOL)hidesOnDeactivate                                                               \
 179 {                                                                                       \
 180     return [self->gWindow hidesOnDeactivate];                                           \
 181 }                                                                                       \
 182 - (BOOL)worksWhenModal                                                                  \
 183 {                                                                                       \
 184     return [self->gWindow worksWhenModal];                                              \
 185 }                                                                                       \
 186 - (void)setBackgroundColor:(NSColor *)color                                             \
 187 {                                                                                       \
 188     [super setBackgroundColor:[self->gWindow setBackgroundColor:color]];                \
 189 }                                                                                       \
 190 - (NSButton *)standardWindowButton:(NSWindowButton)type                                 \
 191 {                                                                                       \
 192     NSButton* button = [super standardWindowButton:type];                               \
 193     switch (type)                                                                       \
 194     {                                                                                   \
 195         case NSWindowDocumentIconButton:                                                \
 196             [button setAcceptsTouchEvents:NO];                                          \
 197             [button setAction:nil];                                                     \
 198             [button setEnabled:NO];                                                     \
 199             break;                                                                      \
 200     }                                                                                   \
 201     return button;                                                                      \
 202 }                                                                                       \
 203 - (void)sendEvent:(NSEvent *)event                                                      \
 204 {                                                                                       \
 205     /* Local copy of the id keeps the retain/release calls balanced. */                 \
 206     id view = [self->gWindow->view retain];                                             \
 207     [self->gWindow sendEvent:event];                                                    \
 208     [super sendEvent:event];                                                            \
 209     [view release];                                                                     \
 210 }
 211 
 212 @implementation GlassWindow_Normal
 213 GLASS_NS_WINDOW_IMPLEMENTATION
 214 @end
 215 
 216 @implementation GlassWindow_Panel
 217 GLASS_NS_WINDOW_IMPLEMENTATION
 218 
 219 - (void)setWorksWhenModal:(BOOL)worksWhenModal
 220 {
 221     [super setWorksWhenModal:NO];
 222 }
 223 
 224 - (BOOL)accessibilityIsIgnored
 225 {
 226     /* In JavaFX NSPanels are used to implement PopupWindows,
 227      * which are used by ContextMenu.  In Accessibility, for a
 228      * menu to work as expected, the window has to be ignored.
 229      * Note that asking the children of the window is
 230      * very important in this context. It ensures that all
 231      * descendants created.  Without it, the menu  will
 232      * not be seen by the assistive technology.
 233      */
 234     __block BOOL ignored = [super accessibilityIsIgnored];
 235     NSArray* children = [self accessibilityAttributeValue: NSAccessibilityChildrenAttribute];
 236     if (children) {
 237         [children enumerateObjectsUsingBlock: ^(id child, NSUInteger index, BOOL *stop) {
 238             NSString* role = [child accessibilityAttributeValue: NSAccessibilityRoleAttribute];
 239             if ([NSAccessibilityMenuRole isEqualToString: role]) {
 240                 ignored = YES;
 241                 *stop = YES;
 242             }
 243             /* Tooltips are exposed by AXHelp attribute and there is no API in Mac
 244              * to represent the tooltip window.
 245              * Nonetheless, the window must be ignored to prevent interfering with
 246              * VoiceOver focus.
 247              */
 248             if ([@"AXJFXTOOLTIP" isEqualToString: role]) {
 249                 ignored = YES;
 250                 *stop = YES;
 251             }
 252         }];
 253     }
 254     return ignored;
 255 }
 256 
 257 - (id)accessibilityAttributeValue:(NSString *)attribute
 258 {
 259     /*
 260     * The default value of AXRoleDescription for a NSPanel is 'system dialog'.
 261     * While this is correct for an average cocoa application it is not appropriate
 262     * for JFX, where all NSPanels are decoration-less windows used to implement
 263     * tooltip, context menus, combo boxes, etc.
 264     */
 265     if ([NSAccessibilityRoleDescriptionAttribute isEqualToString: attribute]) {
 266         return @"";
 267     }
 268     return [super accessibilityAttributeValue: attribute];
 269 }
 270 
 271 @end
 272 // --------------------------------------------------------------------------------------
 273 
 274 
 275 @implementation GlassWindow
 276 
 277 - (void)setFullscreenWindow:(NSWindow*)fsWindow
 278 {
 279     if (self->fullscreenWindow == fsWindow) {
 280         return;
 281     }
 282 
 283     [self _ungrabFocus];
 284 
 285     NSWindow *from, *to;
 286     from = self->fullscreenWindow ? self->fullscreenWindow : self->nsWindow;
 287     to = fsWindow ? fsWindow : self->nsWindow;
 288 
 289     NSArray * children = [from childWindows];
 290     for (NSUInteger i=0; i<[children count]; i++)
 291     {
 292         NSWindow *child = (NSWindow*)[children objectAtIndex:i];
 293         if ([[child delegate] isKindOfClass: [GlassWindow class]]) {
 294             [from removeChildWindow: child];
 295             [to addChildWindow:child ordered:NSWindowAbove];
 296         }
 297     }
 298 
 299     self->fullscreenWindow = fsWindow;
 300 
 301     GET_MAIN_JENV;
 302     (*env)->CallVoidMethod(env, self->jWindow, jWindowNotifyDelegatePtr, ptr_to_jlong(fsWindow));
 303     GLASS_CHECK_EXCEPTION(env);
 304 }
 305 
 306 - (void)close
 307 {
 308     [self _ungrabFocus];
 309 }
 310 
 311 - (void)sendEvent:(NSEvent *)event
 312 {
 313     if ([event type] == NSLeftMouseDown || [event type] == NSRightMouseDown || [event type] == NSOtherMouseDown)
 314     {
 315         NSPoint p = [NSEvent mouseLocation];
 316         NSRect frame = [self->nsWindow frame];
 317         NSRect contentRect = [self->nsWindow contentRectForFrameRect:frame];
 318 
 319         if (p.y >= (frame.origin.y + contentRect.size.height))
 320         {
 321             // Click to the titlebar
 322             [self _ungrabFocus];
 323         }
 324 
 325         [self _checkUngrab];
 326     }
 327 }
 328 
 329 // Window vs Panel API
 330 - (BOOL)canBecomeMainWindow
 331 {
 332     if (!self->isEnabled)
 333     {
 334         // We'll send FOCUS_DISABLED
 335         return YES;
 336     }
 337     return self->isFocusable;
 338 }
 339 
 340 // Window vs Panel API
 341 - (BOOL)hidesOnDeactivate
 342 {
 343     return NO;
 344 }
 345 
 346 // Window vs Panel API
 347 - (BOOL)worksWhenModal
 348 {
 349     return NO;
 350 }
 351 
 352 - (BOOL)canBecomeKeyWindow
 353 {
 354     if (!self->isEnabled)
 355     {
 356         // We'll send FOCUS_DISABLED
 357         return YES;
 358     }
 359     return self->isFocusable;
 360 }
 361 
 362 - (NSColor*)setBackgroundColor:(NSColor *)color
 363 {
 364     if (self->isTransparent == NO)
 365     {
 366         // allow color if we're opaque
 367         return color;
 368     }
 369     else
 370     {
 371         // for transparent window, ignore and set to clear color
 372         // FIXME: do we want to store the background color in case we switch to non-transparent mode?
 373         return [NSColor clearColor];
 374     }
 375 }
 376 
 377 
 378 @end
 379 
 380 #pragma mark --- GlassEmbeddedWindow
 381 
 382 static NSMutableArray * embeddedWindowsList = nil;
 383 
 384 @implementation GlassEmbeddedWindow
 385 
 386 - (id)initWithDelegate:(GlassWindow*)delegate
 387              frameRect:(NSRect)rect
 388              styleMask:(NSUInteger)styleMask
 389                 screen:(NSScreen*)screen
 390 {
 391     self = [super initWithDelegate:delegate frameRect:rect styleMask:styleMask screen:screen];
 392     if (self == nil) {
 393         return nil;
 394     }
 395 
 396     if (embeddedWindowsList == nil) {
 397         embeddedWindowsList = [[NSMutableArray alloc] initWithCapacity: 4];
 398     }
 399     [embeddedWindowsList addObject:self]; // retains 'self'
 400 
 401     return self;
 402 }
 403 
 404 - (void)close
 405 {
 406     if (embeddedWindowsList) {
 407         [embeddedWindowsList removeObject:self]; // releases 'self'
 408         if ([embeddedWindowsList count] == 0) {
 409             [embeddedWindowsList release];
 410             embeddedWindowsList = nil;
 411         }
 412     }
 413     [super close];
 414 }
 415 
 416 + (BOOL)exists:(GlassEmbeddedWindow*)window
 417 {
 418     if (embeddedWindowsList && window) {
 419         return [embeddedWindowsList indexOfObjectIdenticalTo:window] != NSNotFound;
 420     }
 421     return NO;
 422 }
 423 
 424 - (void)setFullscreenWindow:(NSWindow*)fsWindow
 425 {
 426     if (self->parent != nil)
 427     {
 428         BOOL fullscreen = (fsWindow != nil);
 429 
 430         CALayer *layer = [self->gWindow->view layer];
 431         if ([layer isKindOfClass:[GlassLayer3D class]] == YES)
 432         {
 433             [((CAOpenGLLayer*)layer) setAsynchronous:fullscreen];
 434 
 435             layer = [self->parent->gWindow->view layer];
 436             if ([layer isKindOfClass:[GlassLayer3D class]] == YES)
 437             {
 438                 [((CAOpenGLLayer*)layer) setAsynchronous:!fullscreen];
 439             }
 440         }
 441 
 442         self->fullscreenWindow = fsWindow;
 443     }
 444 }
 445 
 446 - (void)sendEvent:(NSEvent *)theEvent
 447 {
 448     BOOL fullscreen = (self->fullscreenWindow != nil);
 449     if (fullscreen == NO)
 450     {
 451         [super sendEvent:theEvent];
 452     }
 453     else
 454     {
 455         [self->fullscreenWindow sendEvent:theEvent];
 456     }
 457 }
 458 
 459 
 460 @end
 461 
 462 #pragma mark --- Dispatcher
 463 
 464 // TODO: re-implement using Obj-C blocks ?
 465 static jlong _createWindowCommonDo(JNIEnv *env, jobject jWindow, jlong jOwnerPtr, jlong jScreenPtr, jint jStyleMask, jboolean jIsChild)
 466 {
 467     GlassWindow *window = nil;
 468 
 469     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 470     {
 471         NSUInteger styleMask = NSBorderlessWindowMask;
 472         // only titled windows get title
 473         if ((jStyleMask&com_sun_glass_ui_Window_TITLED) != 0)
 474         {
 475             styleMask = styleMask|NSTitledWindowMask;
 476         }
 477         // only nontransparent windows get decorations
 478         if ((jStyleMask&com_sun_glass_ui_Window_TRANSPARENT) == 0)
 479         {
 480             if ((jStyleMask&com_sun_glass_ui_Window_CLOSABLE) != 0)
 481             {
 482                 styleMask = styleMask|NSClosableWindowMask;
 483             }
 484 
 485             if (((jStyleMask&com_sun_glass_ui_Window_MINIMIZABLE) != 0) ||
 486                 ((jStyleMask&com_sun_glass_ui_Window_MAXIMIZABLE) != 0))
 487             {
 488                 // on Mac OS X there is one set for min/max buttons,
 489                 // so if clients requests either one, we turn them both on
 490                 styleMask = styleMask|NSMiniaturizableWindowMask;
 491             }
 492 
 493             if ((jStyleMask&com_sun_glass_ui_Window_UNIFIED) != 0) {
 494                 styleMask = styleMask|NSTexturedBackgroundWindowMask;
 495             }
 496 
 497             if ((jStyleMask&com_sun_glass_ui_Window_UTILITY) != 0)
 498             {
 499                 styleMask = styleMask | NSUtilityWindowMask | NSNonactivatingPanelMask;
 500             }
 501         }
 502 
 503         if ((jStyleMask&com_sun_glass_ui_Window_POPUP) != 0)
 504         {
 505             // can receive keyboard input without activating the owning application
 506             styleMask = styleMask|NSNonactivatingPanelMask;
 507         }
 508 
 509         // initial size must be 0x0 otherwise we don't get resize update if the initial size happens to be the exact same size as the later programatical one!
 510         CGFloat x = 0.0f;
 511         CGFloat y = 0.0f;
 512         CGFloat w = 0.0f;
 513         CGFloat h = 0.0f;
 514 
 515         NSScreen *screen = (NSScreen*)jlong_to_ptr(jScreenPtr);
 516         window = [[GlassWindow alloc] _initWithContentRect:NSMakeRect(x, y, w, h) styleMask:styleMask screen:screen jwindow:jWindow jIsChild:jIsChild];
 517 
 518         if ((jStyleMask & com_sun_glass_ui_Window_UNIFIED) != 0) {
 519             //Prevent the textured effect from disappearing on border thickness recalculation
 520             [window->nsWindow setAutorecalculatesContentBorderThickness:NO forEdge:NSMaxYEdge];
 521         }
 522 
 523         if ((jStyleMask & com_sun_glass_ui_Window_UTILITY) != 0) {
 524             [[window->nsWindow standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES];
 525             [[window->nsWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
 526         }
 527 
 528         if (jIsChild == JNI_FALSE)
 529         {
 530             if (jOwnerPtr != 0L)
 531             {
 532                 window->owner = getGlassWindow(env, jOwnerPtr)->nsWindow; // not retained (use weak reference?)
 533             }
 534         }
 535         else
 536         {
 537             if ((jOwnerPtr != 0L) && (jOwnerPtr != BROWSER_PARENT_ID))
 538             {
 539                 GlassEmbeddedWindow *parent = getGlassEmbeddedWindow(env, jOwnerPtr);
 540                 GlassEmbeddedWindow *ewindow = (GlassEmbeddedWindow*)window->nsWindow;
 541                 parent->child = ewindow; // not retained (use weak reference?)
 542 
 543                 ewindow->parent = parent; // not retained (use weak reference?)
 544             }
 545         }
 546         window->isResizable = NO;
 547         window->isDecorated = (jStyleMask&com_sun_glass_ui_Window_TITLED) != 0;
 548         /* 10.7 full screen window support */
 549         if ([NSWindow instancesRespondToSelector:@selector(toggleFullScreen:)]) {
 550             NSWindowCollectionBehavior behavior = [window->nsWindow collectionBehavior];
 551             if ((jStyleMask&com_sun_glass_ui_Window_POPUP) == 0 && !window->owner)
 552             {
 553                 // Only ownerless windows should have the Full Screen Toggle control
 554                 behavior |= (1 << 7) /* NSWindowCollectionBehaviorFullScreenPrimary */;
 555             }
 556             else
 557             {
 558                 // Other windows are only allowed to be shown together with a primary
 559                 // full screen window
 560                 behavior |= (1 << 8) /* NSWindowCollectionBehaviorFullScreenAuxiliary */;
 561             }
 562             [window->nsWindow setCollectionBehavior: behavior];
 563         }
 564 
 565         window->isTransparent = (jStyleMask & com_sun_glass_ui_Window_TRANSPARENT) != 0;
 566         if (window->isTransparent == YES)
 567         {
 568             [window->nsWindow setBackgroundColor:[NSColor clearColor]];
 569             [window->nsWindow setHasShadow:NO];
 570             [window->nsWindow setOpaque:NO];
 571         }
 572         else
 573         {
 574             [window->nsWindow setHasShadow:YES];
 575             [window->nsWindow setOpaque:YES];
 576         }
 577 
 578         window->fullscreenWindow = nil;
 579 
 580         window->isSizeAssigned = NO;
 581         window->isLocationAssigned = NO;
 582 
 583         if (jIsChild == JNI_TRUE && jOwnerPtr != 0L && jOwnerPtr != BROWSER_PARENT_ID
 584             && [window->nsWindow isKindOfClass:[GlassEmbeddedWindow class]])
 585         {
 586             GlassEmbeddedWindow* parent = ((GlassEmbeddedWindow*)window->nsWindow)->parent;
 587             if ([GlassEmbeddedWindow exists:parent])
 588             {
 589                 window->isLocationAssigned = YES;
 590                 NSScreen *pscreen = [[NSScreen screens] objectAtIndex:0];
 591                 NSRect screenFrame = pscreen.frame;
 592                 NSRect frameRect = parent.frame;
 593                 int invy = (int)round(screenFrame.size.height - frameRect.size.height - frameRect.origin.y);
 594                 [window _setBounds:(int)round(frameRect.origin.x)
 595                                  y:invy
 596                               xSet:YES ySet:YES w:0 h:0 cw:0 ch:0];
 597             }
 598         }
 599     }
 600     [pool drain];
 601 
 602     GLASS_CHECK_EXCEPTION(env);
 603 
 604     return ptr_to_jlong(window->nsWindow);
 605 }
 606 
 607 static jlong _createWindowCommon
 608 (JNIEnv *env, jobject jWindow, jlong jOwnerPtr, jlong jScreenPtr, jint jStyleMask, jboolean jIsChild)
 609 {
 610     LOG("_createWindowCommon");
 611 
 612     jlong value = 0L;
 613 
 614     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 615     GLASS_POOL_ENTER;
 616     {
 617         jobject jWindowRef = (*env)->NewGlobalRef(env, jWindow);
 618         value = _createWindowCommonDo(env, jWindowRef, jOwnerPtr, jScreenPtr, jStyleMask, jIsChild);
 619     }
 620     GLASS_POOL_EXIT;
 621     GLASS_CHECK_EXCEPTION(env);
 622 
 623     LOG("   window: %p", value);
 624     return value;
 625 }
 626 
 627 #pragma mark --- JNI
 628 
 629 /*
 630  * Class:     com_sun_glass_ui_mac_MacWindow
 631  * Method:    _initIDs
 632  * Signature: ()V
 633  */
 634 JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacWindow__1initIDs
 635 (JNIEnv *env, jclass jClass)
 636 {
 637     LOG("Java_com_sun_glass_ui_mac_MacWindow__1initIDs");
 638 
 639     if (jWindowClass == NULL)
 640     {
 641         jWindowClass = (*env)->NewGlobalRef(env, jClass);
 642     }
 643 
 644     if (jMenuBarDelegateClass == NULL)
 645     {
 646         jMenuBarDelegateClass = (*env)->NewGlobalRef(env, [GlassHelper ClassForName:"com.sun.glass.ui.mac.MacMenuBarDelegate" withEnv:env]);
 647     }
 648 
 649     if (jViewClass == NULL)
 650     {
 651         jViewClass = (*env)->NewGlobalRef(env, [GlassHelper ClassForName:"com.sun.glass.ui.View" withEnv:env]);
 652     }
 653 
 654     if (jScreenClass == NULL)
 655     {
 656         jScreenClass = (*env)->NewGlobalRef(env, [GlassHelper ClassForName:"com.sun.glass.ui.Screen" withEnv:env]);
 657     }
 658 
 659     if (jWindowNotifyMove == NULL)
 660     {
 661         jWindowNotifyMove = (*env)->GetMethodID(env, jWindowClass, "notifyMove", "(IIZ)V");
 662         if ((*env)->ExceptionCheck(env)) return;
 663     }
 664 
 665     if (jWindowNotifyResize == NULL)
 666     {
 667         jWindowNotifyResize = (*env)->GetMethodID(env, jWindowClass, "notifyResize", "(III)V");
 668         if ((*env)->ExceptionCheck(env)) return;
 669     }
 670 
 671     if (jWindowNotifyMoveToAnotherScreen == NULL)
 672     {
 673         jWindowNotifyMoveToAnotherScreen = (*env)->GetMethodID(env, jWindowClass, "notifyMoveToAnotherScreen", "(Lcom/sun/glass/ui/Screen;)V");
 674         if ((*env)->ExceptionCheck(env)) return;
 675     }
 676 
 677     if (jWindowNotifyScaleChanged == NULL)
 678     {
 679         jWindowNotifyScaleChanged = (*env)->GetMethodID(env, jWindowClass, "notifyScaleChanged", "(FFFF)V");
 680         if ((*env)->ExceptionCheck(env)) return;
 681     }
 682 
 683     if (jWindowNotifyClose == NULL)
 684     {
 685         jWindowNotifyClose = (*env)->GetMethodID(env, jWindowClass, "notifyClose", "()V");
 686         if ((*env)->ExceptionCheck(env)) return;
 687     }
 688 
 689     if (jWindowNotifyFocus == NULL)
 690     {
 691         jWindowNotifyFocus = (*env)->GetMethodID(env, jWindowClass, "notifyFocus", "(I)V");
 692         if ((*env)->ExceptionCheck(env)) return;
 693     }
 694 
 695     if (jWindowNotifyFocusUngrab == NULL)
 696     {
 697         jWindowNotifyFocusUngrab = (*env)->GetMethodID(env, jWindowClass, "notifyFocusUngrab", "()V");
 698         if ((*env)->ExceptionCheck(env)) return;
 699     }
 700 
 701     if (jWindowNotifyFocusDisabled == NULL)
 702     {
 703         jWindowNotifyFocusDisabled = (*env)->GetMethodID(env, jWindowClass, "notifyFocusDisabled", "()V");
 704         if ((*env)->ExceptionCheck(env)) return;
 705     }
 706 
 707     if (jWindowNotifyDestroy == NULL)
 708     {
 709         jWindowNotifyDestroy = (*env)->GetMethodID(env, jWindowClass, "notifyDestroy", "()V");
 710         if ((*env)->ExceptionCheck(env)) return;
 711     }
 712 
 713     if (jWindowNotifyDelegatePtr == NULL)
 714     {
 715         jWindowNotifyDelegatePtr = (*env)->GetMethodID(env, jWindowClass, "notifyDelegatePtr", "(J)V");
 716     }
 717 }
 718 
 719 /*
 720  * Class:     com_sun_glass_ui_mac_MacWindow
 721  * Method:    _createWindow
 722  * Signature: (JJI)J
 723  */
 724 JNIEXPORT jlong JNICALL Java_com_sun_glass_ui_mac_MacWindow__1createWindow
 725 (JNIEnv *env, jobject jWindow, jlong jOwnerPtr, jlong jScreenPtr, jint jStyleMask)
 726 {
 727     LOG("Java_com_sun_glass_ui_mac_MacWindow__1createWindow");
 728 
 729     return _createWindowCommon(env, jWindow, jOwnerPtr, jScreenPtr, jStyleMask, JNI_FALSE);
 730 }
 731 
 732 /*
 733  * Class:     com_sun_glass_ui_mac_MacWindow
 734  * Method:    _createChildWindow
 735  * Signature: (J)J
 736  */
 737 JNIEXPORT jlong JNICALL Java_com_sun_glass_ui_mac_MacWindow__1createChildWindow
 738 (JNIEnv *env, jobject jWindow, jlong jOwnerPtr)
 739 {
 740     LOG("Java_com_sun_glass_ui_mac_MacWindow__1createChildWindow");
 741     LOG("   owner: %p", jOwnerPtr);
 742 
 743     jlong jScreenPtr = 0L;
 744     jint jStyleMask = NSBorderlessWindowMask;
 745     if (jOwnerPtr == BROWSER_PARENT_ID)
 746     {
 747         LOG("       case PARENT (PLUGIN)");
 748         // special case: embedded window for plugin (the container which will hold the child window)
 749     }
 750     else
 751     {
 752         LOG("       case CHILD (EMBEDDED)");
 753         // special case: embedded window for plugin (the actual plugin window with remote layer)
 754         // jOwnerPtr must be a valid GlassEmbeddedWindow instance
 755         if (![GlassEmbeddedWindow exists:(GlassEmbeddedWindow*)jlong_to_ptr(jOwnerPtr)]) {
 756             return (jlong)0;
 757         }
 758     }
 759 
 760     return _createWindowCommon(env, jWindow, jOwnerPtr, jScreenPtr, jStyleMask, JNI_TRUE);
 761 }
 762 
 763 /*
 764  * Class:     com_sun_glass_ui_mac_MacWindow
 765  * Method:    _setLevel
 766  * Signature: (JI)V
 767  */
 768 JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacWindow__1setLevel
 769 (JNIEnv *env, jobject jWindow, jlong jPtr, jint jLevel)
 770 {
 771     LOG("Java_com_sun_glass_ui_mac_MacWindow__1setLevel");
 772     if (!jPtr) return;
 773 
 774     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 775     GLASS_POOL_ENTER;
 776     {
 777         GlassWindow *window = getGlassWindow(env, jPtr);
 778         NSInteger level = NSNormalWindowLevel;
 779         switch (jLevel)
 780         {
 781             case com_sun_glass_ui_Window_Level_FLOATING:
 782                 level = NSFloatingWindowLevel;
 783                 break;
 784             case com_sun_glass_ui_Window_Level_TOPMOST:
 785                 level = NSScreenSaverWindowLevel;
 786                 break;
 787         }
 788         [window->nsWindow setLevel:level];
 789     }
 790     GLASS_POOL_EXIT;
 791     GLASS_CHECK_EXCEPTION(env);
 792 }
 793 
 794 /*
 795  * Class:     com_sun_glass_ui_mac_MacWindow
 796  * Method:    _setFocusable
 797  * Signature: (Z)V
 798  */
 799 JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacWindow__1setFocusable
 800 (JNIEnv *env, jobject jWindow, jlong jPtr, jboolean isFocusable)
 801 {
 802     LOG("Java_com_sun_glass_ui_mac_MacWindow__1setCanBecomeActive");
 803     if (!jPtr) return;
 804 
 805     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 806     GLASS_POOL_ENTER;
 807     {
 808         GlassWindow *window = getGlassWindow(env, jPtr);
 809         window->isFocusable = (isFocusable==JNI_TRUE);
 810     }
 811     GLASS_POOL_EXIT;
 812     GLASS_CHECK_EXCEPTION(env);
 813 }
 814 
 815 /*
 816  * Class:     com_sun_glass_ui_mac_MacWindow
 817  * Method:    _setEnabled
 818  * Signature: (JZ)V
 819  */
 820 JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacWindow__1setEnabled
 821 (JNIEnv *env, jobject jwindow, jlong jPtr, jboolean isEnabled)
 822 {
 823     LOG("Java_com_sun_glass_ui_mac_MacWindow__1setEnabled");
 824     if (!jPtr) return;
 825 
 826     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 827     GLASS_POOL_ENTER;
 828     {
 829         GlassWindow *window = getGlassWindow(env, jPtr);
 830         window->isEnabled = (BOOL)isEnabled;
 831         NSButton *zoomButton = [window->nsWindow standardWindowButton:NSWindowZoomButton];
 832         if ((window->isEnabled) && (window->isResizable)){
 833             [window->nsWindow setStyleMask: window->enabledStyleMask];
 834             if (window->enabledStyleMask & NSResizableWindowMask) {
 835                 [zoomButton setEnabled:YES];
 836             }
 837         }
 838         else if((window->isEnabled) && (!window->isResizable)){
 839             [window->nsWindow setStyleMask:
 840                 (window->enabledStyleMask & ~(NSUInteger) NSResizableWindowMask)];
 841             [zoomButton setEnabled:NO];
 842         }
 843         else{
 844             window->enabledStyleMask = [window->nsWindow styleMask];
 845             [window->nsWindow setStyleMask:
 846                 (window->enabledStyleMask & ~(NSUInteger)(NSMiniaturizableWindowMask | NSResizableWindowMask))];
 847             [zoomButton setEnabled:NO];
 848         }
 849     }
 850     GLASS_POOL_EXIT;
 851     GLASS_CHECK_EXCEPTION(env);
 852 }
 853 
 854 /*
 855  * Class:     com_sun_glass_ui_mac_MacWindow
 856  * Method:    _setAlpha
 857  * Signature: (F)V
 858  */
 859 JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacWindow__1setAlpha
 860 (JNIEnv *env, jobject jWindow, jlong jPtr, jfloat jAlpha)
 861 {
 862     LOG("Java_com_sun_glass_ui_mac_MacWindow__1setAlpha");
 863     if (!jPtr) return;
 864 
 865     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 866     GLASS_POOL_ENTER;
 867     {
 868         GlassWindow *window = getGlassWindow(env, jPtr);
 869         [window->nsWindow setAlphaValue:jAlpha];
 870     }
 871     GLASS_POOL_EXIT;
 872     GLASS_CHECK_EXCEPTION(env);
 873 }
 874 
 875 /*
 876  * Class:     com_sun_glass_ui_mac_MacWindow
 877  * Method:    _setBackground
 878  * Signature: (JFFF)Z
 879  */
 880 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_mac_MacWindow__1setBackground
 881 (JNIEnv *env, jobject jWindow, jlong jPtr, jfloat r, jfloat g, jfloat b)
 882 {
 883     LOG("Java_com_sun_glass_ui_mac_MacWindow__1setBackground");
 884     if (!jPtr) return JNI_FALSE;
 885 
 886     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 887     GLASS_POOL_ENTER;
 888     {
 889         GlassWindow *window = getGlassWindow(env, jPtr);
 890         [window->nsWindow setBackgroundColor:[NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0f]];
 891     }
 892     GLASS_POOL_EXIT;
 893     GLASS_CHECK_EXCEPTION(env);
 894 
 895     return JNI_TRUE; // gznote: remove this return value if unused
 896 }
 897 
 898 /*
 899  * Class:     com_sun_glass_ui_mac_MacWindow
 900  * Method:    _setView
 901  * Signature: (J)Z
 902  */
 903 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_mac_MacWindow__1setView
 904 (JNIEnv *env, jobject jWindow, jlong jPtr, jobject jview)
 905 {
 906     LOG("Java_com_sun_glass_ui_mac_MacWindow__1setView");
 907     LOG("   window: %p", jPtr);
 908     LOG("   view: %p", getMacView(env, jview));
 909     if (!jPtr) return JNI_FALSE;
 910 
 911     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 912     GLASS_POOL_ENTER;
 913     {
 914         GlassWindow *window = getGlassWindow(env, jPtr);
 915 
 916         // We don't support changing views in the FS mode because
 917         // by Glass design the FS functionality belongs to the View.
 918         // Also, this leads to a crash on the Mac
 919         if ([window->nsWindow styleMask] & (1 << 14)/*NSFullScreenWindowMask*/) {
 920             [window->nsWindow performSelector:@selector(toggleFullScreen:) withObject:nil];
 921 
 922             // Wait until the FS mode has really exited
 923             [GlassApplication enterFullScreenExitingLoop];
 924         }
 925 
 926         NSView<GlassView> *oldView = window->view;
 927         window->view = getMacView(env, jview);
 928         //NSLog(@"        window: %@", window);
 929         //NSLog(@"                frame: %.2f,%.2f %.2fx%.2f", [window frame].origin.x, [window frame].origin.y, [window frame].size.width, [window frame].size.height);
 930         //NSLog(@"        view: %@", window->view);
 931         //NSLog(@"                frame: %.2f,%.2f %.2fx%.2f", [window->view frame].origin.x, [window->view frame].origin.y, [window->view frame].size.width, [window->view frame].size.height);
 932 
 933         // Make sure we synchronize scale factors to the new view as any
 934         // dynamic updates might have happened when we had the old view
 935         // and/or we may have been set visible before we had a view and
 936         // missed the initial notification.
 937         if ([window->nsWindow screen]) {
 938             [window->view notifyScaleFactorChanged:GetScreenScaleFactor([window->nsWindow screen])];
 939         }
 940 
 941         if (oldView && oldView != window->view) {
 942             [[oldView delegate] resetMouseTracking];
 943         }
 944 
 945         if (window->view != nil)
 946         {
 947             CALayer *layer = [window->view layer];
 948             if (([layer isKindOfClass:[CAOpenGLLayer class]] == YES) &&
 949                 (([window->nsWindow styleMask] & NSTexturedBackgroundWindowMask) == NO))
 950             {
 951                 [((CAOpenGLLayer*)layer) setOpaque:[window->nsWindow isOpaque]];
 952             }
 953 
 954             window->suppressWindowMoveEvent = YES; // RT-11215
 955             {
 956                 NSRect viewFrame = [window->view frame];
 957                 if ((viewFrame.size.width != 0.0f) && (viewFrame.size.height != 0.0f))
 958                 {
 959                     NSRect windowFrame = [window->nsWindow frameRectForContentRect:viewFrame];
 960                     windowFrame.origin.x = [window->nsWindow frame].origin.x;
 961                     windowFrame.origin.y = [window->nsWindow frame].origin.y;
 962                     [window _setWindowFrameWithRect:NSMakeRect(windowFrame.origin.x, windowFrame.origin.y, windowFrame.size.width, windowFrame.size.height) withDisplay:JNI_TRUE withAnimate:JNI_FALSE];
 963                 }
 964 
 965                 [window->nsWindow setContentView:[window->view superview]]; // use our superview not ourselves!
 966                 [window->nsWindow setInitialFirstResponder:window->view];
 967                 [window->nsWindow makeFirstResponder:window->view];
 968             }
 969             window->suppressWindowMoveEvent = NO;
 970         }
 971         else
 972         {
 973             [window->nsWindow performSelectorOnMainThread:@selector(setContentView:) withObject:nil waitUntilDone:YES];
 974         }
 975     }
 976     GLASS_POOL_EXIT;
 977     GLASS_CHECK_EXCEPTION(env);
 978 
 979     return JNI_TRUE; // gznote: remove this return value if unused
 980 }
 981 
 982 /*
 983  * Class:     com_sun_glass_ui_mac_MacWindow
 984  * Method:    _setMenubar
 985  * Signature: (Lcom/sun/glass/ui/mac/MacMenubarDelegate;)V
 986  */
 987 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_mac_MacWindow__1setMenubar
 988 (JNIEnv *env, jobject jWindow, jlong jPtr, jlong jMenubarPtr)
 989 {
 990     LOG("Java_com_sun_glass_ui_mac_MacWindow__1setMenubar");
 991     if (!jPtr) return JNI_FALSE;
 992 
 993     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
 994     GLASS_POOL_ENTER;
 995     {
 996         GlassWindow *window = getGlassWindow(env, jPtr);
 997         window->menubar = (GlassMenubar*)jlong_to_ptr(jMenubarPtr);
 998         [NSApp setMainMenu:window->menubar->menu];
 999         [[NSApp mainMenu] update];
1000     }
1001     GLASS_POOL_EXIT;
1002     GLASS_CHECK_EXCEPTION(env);
1003 
1004     return JNI_TRUE; // gznote: remove this return value if unused
1005 }
1006 
1007 /*
1008  * Class:     com_sun_glass_ui_mac_MacWindow
1009  * Method:    _close
1010  * Signature: ()V
1011  */
1012 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_mac_MacWindow__1close
1013 (JNIEnv *env, jclass cls, jlong jPtr)
1014 {
1015     LOG("Java_com_sun_glass_ui_mac_MacWindow__1close");
1016     if (!jPtr) return JNI_FALSE;
1017 
1018     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1019     GLASS_POOL_ENTER;
1020     {
1021         GlassWindow *window = getGlassWindow(env, jPtr);
1022         // this call will always close the window
1023         // without calling the windowShouldClose
1024 
1025         // RT-39813 When closing a window as the result of a global right-click
1026         //          mouse event outside the bounds of the window, using an immediate
1027         //          [window->nsWindow close] crashes the JDK as the AppKit at this
1028         //          point still has another [NSWindow _resignKeyFocus] from the
1029         //          right-click handling in [NSApplication sendEvent].  This defers
1030         //          the close until the [NSWindow _resignKeyFocus] can be performed.
1031 
1032         [window->nsWindow performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:NO];
1033 
1034         // The NSWindow will be automatically released after closing
1035         // The GlassWindow is released in the [NSWindow dealloc] override
1036     }
1037     GLASS_POOL_EXIT;
1038     GLASS_CHECK_EXCEPTION(env);
1039 
1040     return JNI_TRUE; // gznote: remove this return value if unused
1041 }
1042 
1043 /*
1044  * Class:     com_sun_glass_ui_mac_MacWindow
1045  * Method:    _requestFocus
1046  * Signature: (J)Z
1047  */
1048 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_mac_MacWindow__1requestFocus
1049 (JNIEnv *env, jobject jWindow, jlong jPtr)
1050 {
1051     LOG("Java_com_sun_glass_ui_mac_MacWindow__1requestFocus");
1052     if (!jPtr) return JNI_FALSE;
1053 
1054     jboolean focused = JNI_FALSE;
1055 
1056     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1057     GLASS_POOL_ENTER;
1058     {
1059         GlassWindow *window = getGlassWindow(env, jPtr);
1060 
1061         if ([window->nsWindow isVisible])
1062         {
1063             [window->nsWindow makeMainWindow];
1064             [window->nsWindow makeKeyAndOrderFront:window->nsWindow];
1065             [window->nsWindow orderFrontRegardless];
1066         }
1067 
1068         focused = [window->nsWindow isKeyWindow] ? JNI_TRUE : JNI_FALSE;
1069     }
1070     GLASS_POOL_EXIT;
1071     GLASS_CHECK_EXCEPTION(env);
1072 
1073     return focused;
1074 }
1075 
1076 /*
1077  * Class:     com_sun_glass_ui_mac_MacWindow
1078  * Method:    _grabFocus
1079  * Signature: (J)Z
1080  */
1081 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_mac_MacWindow__1grabFocus
1082 (JNIEnv *env, jobject jThis, jlong jPtr)
1083 {
1084     LOG("Java_com_sun_glass_ui_mac_MacWindow__1grabFocus");
1085     if (!jPtr) return JNI_FALSE;
1086 
1087     jboolean ret = JNI_FALSE;
1088 
1089     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1090     GLASS_POOL_ENTER;
1091     {
1092         GlassWindow * window = getGlassWindow(env, jPtr);
1093         //TODO: full screen
1094         [window _grabFocus];
1095         ret = JNI_TRUE;
1096     }
1097     GLASS_POOL_EXIT;
1098     GLASS_CHECK_EXCEPTION(env);
1099 
1100     return ret;
1101 }
1102 
1103 /*
1104  * Class:     com_sun_glass_ui_mac_MacWindow
1105  * Method:    _ungrabFocus
1106  * Signature: (J)V
1107  */
1108 JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacWindow__1ungrabFocus
1109 (JNIEnv *env, jobject jThis, jlong jPtr)
1110 {
1111     LOG("Java_com_sun_glass_ui_mac_MacWindow__1ungrabFocus");
1112     if (!jPtr) return;
1113 
1114     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1115     GLASS_POOL_ENTER;
1116     {
1117         GlassWindow * window = getGlassWindow(env, jPtr);
1118         //TODO; full screen
1119         [window _ungrabFocus];
1120     }
1121     GLASS_POOL_EXIT;
1122     GLASS_CHECK_EXCEPTION(env);
1123 }
1124 
1125 /*
1126  * Class:     com_sun_glass_ui_mac_MacWindow
1127  * Method:    _maximize
1128  * Signature: (JZZ)V
1129  */
1130 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_mac_MacWindow__1maximize
1131 (JNIEnv *env, jobject jWindow, jlong jPtr, jboolean maximize, jboolean isZoomed)
1132 {
1133     LOG("Java_com_sun_glass_ui_mac_MacWindow__1maximize");
1134     if (!jPtr) return JNI_FALSE;
1135 
1136     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1137     GLASS_POOL_ENTER;
1138     {
1139         GlassWindow *window = getGlassWindow(env, jPtr);
1140         window->suppressWindowResizeEvent = YES;
1141 
1142         if ((maximize == JNI_TRUE) && (isZoomed == JNI_FALSE))
1143         {
1144             window->preZoomedRect = [window->nsWindow frame];
1145 
1146             if ([window->nsWindow styleMask] != NSBorderlessWindowMask)
1147             {
1148                 [window->nsWindow zoom:nil];
1149                 // windowShouldZoom will be called automatically in this case
1150             }
1151             else
1152             {
1153                 NSRect visibleRect = [[window _getScreen] visibleFrame];
1154                 [window _setWindowFrameWithRect:NSMakeRect(visibleRect.origin.x, visibleRect.origin.y, visibleRect.size.width, visibleRect.size.height) withDisplay:JNI_TRUE withAnimate:JNI_TRUE];
1155 
1156                 // calling windowShouldZoom will send Java maximize event
1157                 [window windowShouldZoom:window->nsWindow toFrame:[window->nsWindow frame]];
1158             }
1159         }
1160         else if ((maximize == JNI_FALSE) && (isZoomed == JNI_TRUE))
1161         {
1162             [window _restorePreZoomedRect];
1163         }
1164 
1165         window->suppressWindowResizeEvent = NO;
1166     }
1167     GLASS_POOL_EXIT;
1168     GLASS_CHECK_EXCEPTION(env);
1169 
1170     return JNI_TRUE; // gznote: remove this return value if unused
1171 }
1172 
1173 /*
1174  * Class:     com_sun_glass_ui_mac_MacWindow
1175  * Method:    _setBounds
1176  * Signature: (JIIZZIIIIFF)V
1177  */
1178 JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacWindow__1setBounds2
1179 (JNIEnv *env, jobject jWindow, jlong jPtr,
1180  jint x, jint y, jboolean xSet, jboolean ySet,
1181  jint w, jint h, jint cw, jint ch, jfloat xGravity, jfloat yGravity)
1182 {
1183     LOG("Java_com_sun_glass_ui_mac_MacWindow__1setBounds");
1184     LOG("   x,y: %d,%d", x, y);
1185     LOG("   xSet,ySet: %d,%d", xSet, ySet);
1186     LOG("   xGravity,yGravity: %.2f,%.2f", xGravity, yGravity);
1187     LOG("   w x h: %dx%d", w, h);
1188     LOG("   cw x ch: %dx%d", cw, ch);
1189     if (!jPtr) return;
1190 
1191     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1192     GLASS_POOL_ENTER;
1193     {
1194         GlassWindow *window = getGlassWindow(env, jPtr);
1195         if (xSet || ySet) window->isLocationAssigned = YES;
1196         if (w > 0 || h > 0 || cw > 0 || ch > 0) window->isSizeAssigned = YES;
1197         [window _setBounds:x y:y xSet:xSet ySet:ySet w:w h:h cw:cw ch:ch];
1198     }
1199     GLASS_POOL_EXIT;
1200     GLASS_CHECK_EXCEPTION(env);
1201 }
1202 
1203 /*
1204  * Class:     com_sun_glass_ui_mac_MacWindow
1205  * Method:    _setMinimumSize
1206  * Signature: (JII)Z
1207  */
1208 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_mac_MacWindow__1setMinimumSize
1209 (JNIEnv *env, jobject jWindow, jlong jPtr, jint jW, jint jH)
1210 {
1211     LOG("Java_com_sun_glass_ui_mac_MacWindow__1setMinimumSize");
1212     if (!jPtr) return JNI_FALSE;
1213 
1214     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1215     GLASS_POOL_ENTER;
1216     {
1217         GlassWindow *window = getGlassWindow(env, jPtr);
1218         [window->nsWindow setMinSize:NSMakeSize(jW, jH)];
1219     }
1220     GLASS_POOL_EXIT;
1221     GLASS_CHECK_EXCEPTION(env);
1222 
1223     return JNI_TRUE; // gznote: remove this return value if unused
1224 }
1225 
1226 /*
1227  * Class:     com_sun_glass_ui_mac_MacWindow
1228  * Method:    _setMaximumSize
1229  * Signature: (JII)Z
1230  */
1231 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_mac_MacWindow__1setMaximumSize
1232 (JNIEnv *env, jobject jWindow, jlong jPtr, jint jW, jint jH)
1233 {
1234     LOG("Java_com_sun_glass_ui_mac_MacWindow__1setMaximumSize");
1235     if (!jPtr) return JNI_FALSE;
1236 
1237     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1238     GLASS_POOL_ENTER;
1239     {
1240         GlassWindow *window = getGlassWindow(env, jPtr);
1241         [window->nsWindow setMaxSize:NSMakeSize(jW == -1 ? FLT_MAX : (CGFloat)jW,
1242                                                 jH == -1 ? FLT_MAX : (CGFloat)jH)];
1243     }
1244     GLASS_POOL_EXIT;
1245     GLASS_CHECK_EXCEPTION(env);
1246 
1247     return JNI_TRUE; // gznote: remove this return value if unused
1248 }
1249 
1250 /*
1251  * Class:     com_sun_glass_ui_mac_MacWindow
1252  * Method:    _setResizable
1253  * Signature: (Z)Z
1254  */
1255 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_mac_MacWindow__1setResizable
1256 (JNIEnv *env, jobject jWindow, jlong jPtr, jboolean jResizable)
1257 {
1258     LOG("Java_com_sun_glass_ui_mac_MacWindow__1setResizable");
1259     if (!jPtr) return JNI_FALSE;
1260 
1261     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1262     GLASS_POOL_ENTER;
1263     {
1264         GlassWindow *window = getGlassWindow(env, jPtr);
1265         if (window->isResizable != jResizable)
1266         {
1267             [window performSelectorOnMainThread:@selector(_setResizable) withObject:nil waitUntilDone:YES];
1268         }
1269     }
1270     GLASS_POOL_EXIT;
1271     GLASS_CHECK_EXCEPTION(env);
1272 
1273     return JNI_TRUE;
1274 }
1275 
1276 /*
1277  * Class:     com_sun_glass_ui_mac_MacWindow
1278  * Method:    _setVisible
1279  * Signature: (Z)Z
1280  */
1281 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_mac_MacWindow__1setVisible
1282 (JNIEnv *env, jobject jWindow, jlong jPtr, jboolean jVisible)
1283 {
1284     LOG("Java_com_sun_glass_ui_mac_MacWindow__1setVisible: %d", jVisible);
1285     LOG("   window: %p", jPtr);
1286     if (!jPtr) return JNI_FALSE;
1287 
1288     jboolean now = JNI_FALSE;
1289 
1290     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1291     GLASS_POOL_ENTER;
1292     {
1293         GlassWindow *window = getGlassWindow(env, jPtr);
1294         if (jVisible == JNI_TRUE)
1295         {
1296             if (!window->isLocationAssigned) {
1297                 [window _setBounds:0 y:0 xSet:JNI_TRUE ySet:JNI_TRUE w:-1 h:-1 cw:-1 ch:-1];
1298             }
1299             if (!window->isSizeAssigned) {
1300                 [window _setBounds:0 y:0 xSet:JNI_FALSE ySet:JNI_FALSE w:320 h:200 cw:-1 ch:-1];
1301             }
1302             [window _setVisible];
1303         }
1304         else
1305         {
1306             [window _ungrabFocus];
1307             if (window->owner != nil)
1308             {
1309                 LOG("   removeChildWindow: %p", window);
1310                 [window->owner removeChildWindow:window->nsWindow];
1311             }
1312             [window->nsWindow orderOut:window->nsWindow];
1313         }
1314         now = [window->nsWindow isVisible] ? JNI_TRUE : JNI_FALSE;
1315 
1316         // RT-22502 temp workaround: bring plugin window in front of a browser
1317         if (now == YES)
1318         {
1319             static BOOL isBackgroundOnlyAppChecked = NO;
1320             static BOOL isBackgroundOnlyApp = NO;
1321             if (isBackgroundOnlyAppChecked == NO)
1322             {
1323                 isBackgroundOnlyAppChecked = YES;
1324 
1325                 ProcessSerialNumber psn;
1326                 if (GetCurrentProcess(&psn) == noErr)
1327                 {
1328                     ProcessInfoRec info;
1329                     memset(&info, 0x00, sizeof(ProcessInfoRec));
1330                     GetProcessInformation(&psn, &info);
1331                     isBackgroundOnlyApp = ((modeOnlyBackground&info.processMode) == modeOnlyBackground);
1332                 }
1333             }
1334             if (isBackgroundOnlyApp == YES)
1335             {
1336                 [window->nsWindow performSelectorOnMainThread:@selector(orderFrontRegardless) withObject:nil waitUntilDone:YES];
1337             }
1338         }
1339     }
1340     GLASS_POOL_EXIT;
1341     GLASS_CHECK_EXCEPTION(env);
1342 
1343     return now;
1344 }
1345 
1346 /*
1347  * Class:     com_sun_glass_ui_mac_MacWindow
1348  * Method:    _setTitle
1349  * Signature: (Ljava/lang/String;)Z
1350  */
1351 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_mac_MacWindow__1setTitle
1352 (JNIEnv *env, jobject jWindow, jlong jPtr, jstring jTitle)
1353 {
1354     LOG("Java_com_sun_glass_ui_mac_MacWindow__1setTitle");
1355     LOG("   window: %p", jPtr);
1356     if (!jPtr) return JNI_FALSE;
1357 
1358     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1359     GLASS_POOL_ENTER;
1360     {
1361         GlassWindow *window = getGlassWindow(env, jPtr);
1362 
1363         NSString *title = [GlassHelper nsStringWithJavaString:jTitle withEnv:env];
1364         LOG("   title: %s", [title UTF8String]);
1365         [window->nsWindow setTitle:title];
1366     }
1367     GLASS_POOL_EXIT;
1368     GLASS_CHECK_EXCEPTION(env);
1369 
1370     return JNI_TRUE; // gnote: remove this return value if unused
1371 }
1372 
1373 /*
1374  * Class:     com_sun_glass_ui_mac_MacWindow
1375  * Method:    _minimize
1376  * Signature: (Z)V
1377  */
1378 JNIEXPORT jboolean JNICALL Java_com_sun_glass_ui_mac_MacWindow__1minimize
1379 (JNIEnv *env, jobject jWindow, jlong jPtr, jboolean jMiniaturize)
1380 {
1381     LOG("Java_com_sun_glass_ui_mac_MacWindow__1minimize");
1382     if (!jPtr) return JNI_FALSE;
1383 
1384     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1385     GLASS_POOL_ENTER;
1386     {
1387         GlassWindow *window = getGlassWindow(env, jPtr);
1388 
1389         if (jMiniaturize == JNI_TRUE)
1390         {
1391             [window->nsWindow miniaturize:nil];
1392         }
1393         else
1394         {
1395             [window->nsWindow deminiaturize:nil];
1396         }
1397     }
1398     GLASS_POOL_EXIT;
1399     GLASS_CHECK_EXCEPTION(env);
1400 
1401     return JNI_TRUE; // gnote: remove this return value if unused
1402 }
1403 
1404 /*
1405  * Class:     com_sun_glass_ui_mac_MacWindow
1406  * Method:    _setIcon
1407  * Signature: (JLjava/nio/ByteBuffer;II)V
1408  */
1409 JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacWindow__1setIcon
1410 (JNIEnv *env, jobject jWindow, jlong jPtr, jobject iconBuffer, jint jWidth, jint jHeight)
1411 {
1412     LOG("Java_com_sun_glass_ui_mac_MacWindow__1setIcon");
1413     if (!jPtr) return;
1414 
1415     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1416     GLASS_POOL_ENTER;
1417     {
1418         GlassWindow *window = getGlassWindow(env, jPtr);
1419 
1420         if (iconBuffer)
1421         {
1422             NSImage *image = nil;
1423 
1424             image = getImage(
1425                         (*env)->GetDirectBufferAddress(env, iconBuffer),
1426                         jHeight, jWidth, 0);
1427 
1428             if (image != nil) {
1429                 // need an explicit window title for the rest of the code to work
1430                 if ([window->nsWindow title] == nil)
1431                 {
1432                     [window->nsWindow setTitle:@"Untitled"];
1433                 }
1434 
1435                 // http://www.cocoabuilder.com/archive/cocoa/199554-nswindow-title-bar-icon-without-representedurl.html
1436                 [window->nsWindow setRepresentedURL:[NSURL fileURLWithPath:[window->nsWindow title]]];
1437                 [[window->nsWindow standardWindowButton:NSWindowDocumentIconButton] setImage:image];
1438                 [image release];
1439             } else {
1440                 [[window->nsWindow standardWindowButton:NSWindowDocumentIconButton] setImage:nil];
1441             }
1442         } else {
1443             [[window->nsWindow standardWindowButton:NSWindowDocumentIconButton] setImage:nil];
1444         }
1445     }
1446     GLASS_POOL_EXIT;
1447     GLASS_CHECK_EXCEPTION(env);
1448 }
1449 
1450 /*
1451  * Class:     com_sun_glass_ui_mac_MacWindow
1452  * Method:    _toFront
1453  * Signature: (J)V
1454  */
1455 JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacWindow__1toFront
1456 (JNIEnv *env, jobject jWindow, jlong jPtr)
1457 {
1458     LOG("Java_com_sun_glass_ui_mac_MacWindow__1toFront");
1459     LOG("   window: %p", jPtr);
1460     if (!jPtr) return;
1461 
1462     GLASS_POOL_ENTER;
1463     {
1464         GlassWindow *window = getGlassWindow(env, jPtr);
1465         [window->nsWindow orderFrontRegardless];
1466     }
1467     GLASS_POOL_EXIT;
1468     GLASS_CHECK_EXCEPTION(env);
1469 }
1470 
1471 /*
1472  * Class:     com_sun_glass_ui_mac_MacWindow
1473  * Method:    _toBack
1474  * Signature: (J)V
1475  */
1476 JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacWindow__1toBack
1477 (JNIEnv *env, jobject jWindow, jlong jPtr)
1478 {
1479     LOG("Java_com_sun_glass_ui_mac_MacWindow__1toBack");
1480     if (!jPtr) return;
1481 
1482     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1483     GLASS_POOL_ENTER;
1484     {
1485         GlassWindow *window = getGlassWindow(env, jPtr);
1486         [window->nsWindow orderBack:nil];
1487     }
1488     GLASS_POOL_EXIT;
1489     GLASS_CHECK_EXCEPTION(env);
1490 }
1491 
1492 
1493 /*
1494  * Class:     com_sun_glass_ui_mac_MacWindow
1495  * Method:    _enterModal
1496  * Signature: (J)V
1497  */
1498 JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacWindow__1enterModal
1499 (JNIEnv *env, jobject jWindow, jlong jPtr)
1500 {
1501     LOG("Java_com_sun_glass_ui_mac_MacWindow__1enterModal");
1502     if (!jPtr) return;
1503 
1504     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1505     GLASS_POOL_ENTER;
1506     {
1507         GlassWindow *window = getGlassWindow(env, jPtr);
1508         [NSApp runModalForWindow:window->nsWindow];
1509     }
1510     GLASS_POOL_EXIT;
1511     GLASS_CHECK_EXCEPTION(env);
1512 }
1513 
1514 /*
1515  * Class:     com_sun_glass_ui_mac_MacWindow
1516  * Method:    _enterModalWithWindow
1517  * Signature: (JJ)V
1518  */
1519 JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacWindow__1enterModalWithWindow
1520 (JNIEnv *env, jobject jWindow, jlong jDialogPtr, jlong jWindowPtr)
1521 {
1522     LOG("Java_com_sun_glass_ui_mac_MacWindow__1enterModalWithWindow");
1523 
1524     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1525     GLASS_POOL_ENTER;
1526     {
1527         //GlassWindow *window = getGlassWindow(env, jDialogPtr);
1528         // TODO: implement _enterModalWithWindow
1529     }
1530     GLASS_POOL_EXIT;
1531     GLASS_CHECK_EXCEPTION(env);
1532 }
1533 
1534 /*
1535  * Class:     com_sun_glass_ui_mac_MacWindow
1536  * Method:    _exitModal
1537  * Signature: (J)V
1538  */
1539 JNIEXPORT void JNICALL Java_com_sun_glass_ui_mac_MacWindow__1exitModal
1540 (JNIEnv *env, jobject jWindow, jlong jPtr)
1541 {
1542     LOG("Java_com_sun_glass_ui_mac_MacWindow__1exitModal");
1543     if (!jPtr) return;
1544 
1545     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1546     GLASS_POOL_ENTER;
1547     {
1548         GlassWindow *window = getGlassWindow(env, jPtr);
1549         [NSApp stop:window->nsWindow];
1550     }
1551     GLASS_POOL_EXIT;
1552     GLASS_CHECK_EXCEPTION(env);
1553 }
1554 
1555 /*
1556  * Class:     com_sun_glass_ui_mac_MacWindow
1557  * Method:    _getEmbeddedX
1558  * Signature: (J)I
1559  */
1560 JNIEXPORT jint JNICALL Java_com_sun_glass_ui_mac_MacWindow__1getEmbeddedX
1561 (JNIEnv *env, jobject jWindow, jlong jPtr)
1562 {
1563     LOG("Java_com_sun_glass_ui_mac_MacWindow__1getEmbeddedX");
1564     if (!jPtr) return 0;
1565 
1566     jint x = 0;
1567 
1568     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1569     GLASS_POOL_ENTER;
1570     {
1571         GlassEmbeddedWindow *window = getGlassEmbeddedWindow(env, jPtr);
1572         x = (int)round([window frame].origin.x);
1573     }
1574     GLASS_POOL_EXIT;
1575     GLASS_CHECK_EXCEPTION(env);
1576 
1577     return x;
1578 }
1579 
1580 /*
1581  * Class:     com_sun_glass_ui_mac_MacWindow
1582  * Method:    _getEmbeddedY
1583  * Signature: (J)I
1584  */
1585 JNIEXPORT jint JNICALL Java_com_sun_glass_ui_mac_MacWindow__1getEmbeddedY
1586 (JNIEnv *env, jobject jWindow, jlong jPtr)
1587 {
1588     LOG("Java_com_sun_glass_ui_mac_MacWindow__1getEmbeddedY");
1589     if (!jPtr) return 0;
1590 
1591     jint y = 0;
1592 
1593     GLASS_ASSERT_MAIN_JAVA_THREAD(env);
1594     GLASS_POOL_ENTER;
1595     {
1596         GlassEmbeddedWindow *window = getGlassEmbeddedWindow(env, jPtr);
1597         NSRect frameRect = [window frame];
1598 
1599         // flip y coordinate
1600         NSScreen *screen = [[NSScreen screens] objectAtIndex:0];
1601         NSRect screenFrame = screen.frame;
1602         y = (int)round(screenFrame.size.height - frameRect.size.height - frameRect.origin.y);
1603     }
1604     GLASS_POOL_EXIT;
1605     GLASS_CHECK_EXCEPTION(env);
1606 
1607     return y;
1608 }