1 /*
   2  * Copyright (c) 1996, 2012, 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 /*
  27  * This file has been modified by Azul Systems, Inc. in 2014. These
  28  * modifications are Copyright (c) 2014 Azul Systems, Inc., and are made
  29  * available on the same license terms set forth above. 
  30  */
  31 
  32 #include <windowsx.h>
  33 
  34 #include <jlong.h>
  35 
  36 #include "awt_Component.h"
  37 #include "awt_Container.h"
  38 #include "awt_Frame.h"
  39 #include "awt_Dialog.h"
  40 #include "awt_Insets.h"
  41 #include "awt_Panel.h"
  42 #include "awt_Toolkit.h"
  43 #include "awt_Window.h"
  44 #include "awt_dlls.h"
  45 #include "awt_Win32GraphicsDevice.h"
  46 #include "awt_BitmapUtil.h"
  47 #include "awt_IconCursor.h"
  48 #include "ComCtl32Util.h"
  49 
  50 #include "java_awt_Insets.h"
  51 #include <java_awt_Container.h>
  52 #include <java_awt_event_ComponentEvent.h>
  53 #include "sun_awt_windows_WCanvasPeer.h"
  54 
  55 #if !defined(__int3264)
  56 typedef __int32 LONG_PTR;
  57 #endif // __int3264
  58 
  59 // Used for Swing's Menu/Tooltip animation Support
  60 const int UNSPECIFIED = 0;
  61 const int TOOLTIP = 1;
  62 const int MENU = 2;
  63 const int SUBMENU = 3;
  64 const int POPUPMENU = 4;
  65 const int COMBOBOX_POPUP = 5;
  66 const int TYPES_COUNT = 6;
  67 jint windowTYPES[TYPES_COUNT];
  68 
  69 
  70 /* IMPORTANT! Read the README.JNI file for notes on JNI converted AWT code.
  71  */
  72 
  73 /***********************************************************************/
  74 // struct for _SetAlwaysOnTop() method
  75 struct SetAlwaysOnTopStruct {
  76     jobject window;
  77     jboolean value;
  78 };
  79 // struct for _SetTitle() method
  80 struct SetTitleStruct {
  81     jobject window;
  82     jstring title;
  83 };
  84 // struct for _SetResizable() method
  85 struct SetResizableStruct {
  86     jobject window;
  87     jboolean resizable;
  88 };
  89 // struct for _UpdateInsets() method
  90 struct UpdateInsetsStruct {
  91     jobject window;
  92     jobject insets;
  93 };
  94 // struct for _ReshapeFrame() method
  95 struct ReshapeFrameStruct {
  96     jobject frame;
  97     jint x, y;
  98     jint w, h;
  99 };
 100 // struct for _SetIconImagesData
 101 struct SetIconImagesDataStruct {
 102     jobject window;
 103     jintArray iconRaster;
 104     jint w, h;
 105     jintArray smallIconRaster;
 106     jint smw, smh;
 107 };
 108 // struct for _SetMinSize() method
 109 // and other methods setting sizes
 110 struct SizeStruct {
 111     jobject window;
 112     jint w, h;
 113 };
 114 // struct for _SetFocusableWindow() method
 115 struct SetFocusableWindowStruct {
 116     jobject window;
 117     jboolean isFocusableWindow;
 118 };
 119 // struct for _ModalDisable() method
 120 struct ModalDisableStruct {
 121     jobject window;
 122     jlong blockerHWnd;
 123 };
 124 // struct for _SetOpacity() method
 125 struct OpacityStruct {
 126     jobject window;
 127     jint iOpacity;
 128 };
 129 // struct for _SetOpaque() method
 130 struct OpaqueStruct {
 131     jobject window;
 132     jboolean isOpaque;
 133 };
 134 // struct for _UpdateWindow() method
 135 struct UpdateWindowStruct {
 136     jobject window;
 137     jintArray data;
 138     HBITMAP hBitmap;
 139     jint width, height;
 140 };
 141 // struct for _RepositionSecurityWarning() method
 142 struct RepositionSecurityWarningStruct {
 143     jobject window;
 144 };
 145 
 146 
 147 /************************************************************************
 148  * AwtWindow fields
 149  */
 150 
 151 jfieldID AwtWindow::warningStringID;
 152 jfieldID AwtWindow::locationByPlatformID;
 153 jfieldID AwtWindow::autoRequestFocusID;
 154 jfieldID AwtWindow::securityWarningWidthID;
 155 jfieldID AwtWindow::securityWarningHeightID;
 156 
 157 jfieldID AwtWindow::sysXID;
 158 jfieldID AwtWindow::sysYID;
 159 jfieldID AwtWindow::sysWID;
 160 jfieldID AwtWindow::sysHID;
 161 
 162 jmethodID AwtWindow::getWarningStringMID;
 163 jmethodID AwtWindow::calculateSecurityWarningPositionMID;
 164 
 165 int AwtWindow::ms_instanceCounter = 0;
 166 HHOOK AwtWindow::ms_hCBTFilter;
 167 AwtWindow * AwtWindow::m_grabbedWindow = NULL;
 168 HWND AwtWindow::sm_retainingHierarchyZOrderInShow = NULL;
 169 BOOL AwtWindow::sm_resizing = FALSE;
 170 UINT AwtWindow::untrustedWindowsCounter = 0;
 171 
 172 /************************************************************************
 173  * AwtWindow class methods
 174  */
 175 
 176 AwtWindow::AwtWindow() {
 177     m_sizePt.x = m_sizePt.y = 0;
 178     m_owningFrameDialog = NULL;
 179     m_isResizable = FALSE;//Default value is replaced after construction
 180     m_minSize.x = m_minSize.y = 0;
 181     m_hIcon = NULL;
 182     m_hIconSm = NULL;
 183     m_iconInherited = FALSE;
 184     VERIFY(::SetRectEmpty(&m_insets));
 185     VERIFY(::SetRectEmpty(&m_old_insets));
 186     VERIFY(::SetRectEmpty(&m_warningRect));
 187 
 188     // what's the best initial value?
 189     m_screenNum = -1;
 190     ms_instanceCounter++;
 191     m_grabbed = FALSE;
 192     m_isFocusableWindow = TRUE;
 193     m_isRetainingHierarchyZOrder = FALSE;
 194     m_filterFocusAndActivation = FALSE;
 195 
 196     if (AwtWindow::ms_instanceCounter == 1) {
 197         AwtWindow::ms_hCBTFilter =
 198             ::SetWindowsHookEx(WH_CBT, (HOOKPROC)AwtWindow::CBTFilter,
 199                                0, AwtToolkit::MainThread());
 200     }
 201 
 202     m_opaque = TRUE;
 203     m_opacity = 0xff;
 204 
 205 
 206     warningString = NULL;
 207     warningWindow = NULL;
 208     securityTooltipWindow = NULL;
 209     securityWarningAnimationStage = 0;
 210     currentWmSizeState = SIZE_RESTORED;
 211 
 212     hContentBitmap = NULL;
 213 
 214     ::InitializeCriticalSection(&contentBitmapCS);
 215 }
 216 
 217 AwtWindow::~AwtWindow()
 218 {
 219     if (warningString != NULL) {
 220         delete [] warningString;
 221     }
 222     ::EnterCriticalSection(&contentBitmapCS);
 223     if (hContentBitmap != NULL) {
 224         ::DeleteObject(hContentBitmap);
 225         hContentBitmap = NULL;
 226     }
 227     ::LeaveCriticalSection(&contentBitmapCS);
 228     ::DeleteCriticalSection(&contentBitmapCS);
 229 }
 230 
 231 void AwtWindow::Dispose()
 232 {
 233     // Fix 4745575 GDI Resource Leak
 234     // MSDN
 235     // Before a window is destroyed (that is, before it returns from processing
 236     // the WM_NCDESTROY message), an application must remove all entries it has
 237     // added to the property list. The application must use the RemoveProp function
 238     // to remove the entries.
 239 
 240     if (--AwtWindow::ms_instanceCounter == 0) {
 241         ::UnhookWindowsHookEx(AwtWindow::ms_hCBTFilter);
 242     }
 243 
 244     ::RemoveProp(GetHWnd(), ModalBlockerProp);
 245 
 246     if (m_grabbedWindow == this) {
 247         Ungrab();
 248     }
 249     if ((m_hIcon != NULL) && !m_iconInherited) {
 250         ::DestroyIcon(m_hIcon);
 251     }
 252     if ((m_hIconSm != NULL) && !m_iconInherited) {
 253         ::DestroyIcon(m_hIconSm);
 254     }
 255 
 256     AwtCanvas::Dispose();
 257 }
 258 
 259 void
 260 AwtWindow::Grab() {
 261     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 262     if (m_grabbedWindow != NULL) {
 263         m_grabbedWindow->Ungrab();
 264     }
 265     m_grabbed = TRUE;
 266     m_grabbedWindow = this;
 267     if (AwtComponent::GetFocusedWindow() == NULL) {
 268         // we shouldn't perform grab in this case (see 4841881)
 269         Ungrab();
 270     } else if (GetHWnd() != AwtComponent::GetFocusedWindow()) {
 271         _ToFront(env->NewGlobalRef(GetPeer(env)));
 272         // Global ref was deleted in _ToFront
 273     }
 274 }
 275 
 276 void
 277 AwtWindow::Ungrab(BOOL doPost) {
 278     if (m_grabbed && m_grabbedWindow == this) {
 279         if (doPost) {
 280             PostUngrabEvent();
 281         }
 282         m_grabbedWindow = NULL;
 283         m_grabbed = FALSE;
 284     }
 285 }
 286 
 287 void
 288 AwtWindow::Ungrab() {
 289     Ungrab(TRUE);
 290 }
 291 
 292 void AwtWindow::_Grab(void * param) {
 293     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 294 
 295     jobject self = (jobject)param;
 296 
 297     if (env->EnsureLocalCapacity(1) < 0)
 298     {
 299         env->DeleteGlobalRef(self);
 300         return;
 301     }
 302 
 303     AwtWindow *p = NULL;
 304 
 305     PDATA pData;
 306     JNI_CHECK_PEER_GOTO(self, ret);
 307     p = (AwtWindow *)pData;
 308     p->Grab();
 309 
 310   ret:
 311     env->DeleteGlobalRef(self);
 312 }
 313 
 314 void AwtWindow::_Ungrab(void * param) {
 315     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 316 
 317     jobject self = (jobject)param;
 318 
 319     if (env->EnsureLocalCapacity(1) < 0)
 320     {
 321         env->DeleteGlobalRef(self);
 322         return;
 323     }
 324 
 325     AwtWindow *p = NULL;
 326 
 327     PDATA pData;
 328     JNI_CHECK_PEER_GOTO(self, ret);
 329     p = (AwtWindow *)pData;
 330     p->Ungrab(FALSE);
 331 
 332   ret:
 333     env->DeleteGlobalRef(self);
 334 }
 335 
 336 MsgRouting AwtWindow::WmNcMouseDown(WPARAM hitTest, int x, int y, int button) {
 337     if (m_grabbedWindow != NULL && !m_grabbedWindow->IsOneOfOwnersOf(this)) {
 338         m_grabbedWindow->Ungrab();
 339     }
 340     return AwtCanvas::WmNcMouseDown(hitTest, x, y, button);
 341 }
 342 
 343 MsgRouting AwtWindow::WmWindowPosChanging(LPARAM windowPos) {
 344     /*
 345      * See 6178004.
 346      * Some windows shouldn't trigger a change in z-order of
 347      * any window from the hierarchy.
 348      */
 349     if (IsRetainingHierarchyZOrder()) {
 350         if (((WINDOWPOS *)windowPos)->flags & SWP_SHOWWINDOW) {
 351             sm_retainingHierarchyZOrderInShow = GetHWnd();
 352         }
 353     } else if (sm_retainingHierarchyZOrderInShow != NULL) {
 354         HWND ancestor = ::GetAncestor(sm_retainingHierarchyZOrderInShow, GA_ROOTOWNER);
 355         HWND windowAncestor = ::GetAncestor(GetHWnd(), GA_ROOTOWNER);
 356 
 357         if (windowAncestor == ancestor) {
 358             ((WINDOWPOS *)windowPos)->flags |= SWP_NOZORDER;
 359         }
 360     }
 361     return mrDoDefault;
 362 }
 363 
 364 void AwtWindow::RepositionSecurityWarning(JNIEnv *env)
 365 {
 366     RECT rect;
 367     CalculateWarningWindowBounds(env, &rect);
 368 
 369     ::SetWindowPos(warningWindow, HWND_NOTOPMOST,
 370             rect.left, rect.top,
 371             rect.right - rect.left, rect.bottom - rect.top,
 372             SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOZORDER
 373             );
 374 }
 375 
 376 MsgRouting AwtWindow::WmWindowPosChanged(LPARAM windowPos) {
 377     WINDOWPOS * wp = (WINDOWPOS *)windowPos;
 378 
 379     if (IsRetainingHierarchyZOrder() && wp->flags & SWP_SHOWWINDOW) {
 380         // By this time all the windows from the hierarchy are already notified about z-order change.
 381         // Thus we may and we should reset the trigger in order not to affect other changes.
 382         sm_retainingHierarchyZOrderInShow = NULL;
 383     }
 384 
 385     // Reposition the warning window
 386     if (IsUntrusted() && warningWindow != NULL) {
 387         if (wp->flags & SWP_HIDEWINDOW) {
 388             UpdateSecurityWarningVisibility();
 389         }
 390 
 391         RepositionSecurityWarning((JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2));
 392 
 393         if (wp->flags & SWP_SHOWWINDOW) {
 394             UpdateSecurityWarningVisibility();
 395         }
 396     }
 397 
 398     return mrDoDefault;
 399 }
 400 
 401 LPCTSTR AwtWindow::GetClassName() {
 402   return TEXT("SunAwtWindow");
 403 }
 404 
 405 void AwtWindow::FillClassInfo(WNDCLASSEX *lpwc)
 406 {
 407     AwtComponent::FillClassInfo(lpwc);
 408     /*
 409      * This line causes bug #4189244 (Swing Popup menu is not being refreshed (cleared) under a Dialog)
 410      * so it's comment out (son@sparc.spb.su)
 411      *
 412      * lpwc->style     |= CS_SAVEBITS; // improve pull-down menu performance
 413      */
 414     lpwc->cbWndExtra = DLGWINDOWEXTRA;
 415 }
 416 
 417 bool AwtWindow::IsWarningWindow(HWND hWnd)
 418 {
 419     const UINT len = 128;
 420     TCHAR windowClassName[len];
 421 
 422     ::RealGetWindowClass(hWnd, windowClassName, len);
 423     return 0 == _tcsncmp(windowClassName,
 424             AwtWindow::GetWarningWindowClassName(), len);
 425 }
 426 
 427 LRESULT CALLBACK AwtWindow::CBTFilter(int nCode, WPARAM wParam, LPARAM lParam)
 428 {
 429     if (nCode == HCBT_ACTIVATE || nCode == HCBT_SETFOCUS) {
 430         HWND hWnd = (HWND)wParam;
 431         AwtComponent *comp = AwtComponent::GetComponent(hWnd);
 432 
 433         if (comp == NULL) {
 434             // Check if it's a security warning icon
 435             // See: 5091224, 6181725, 6732583
 436             if (AwtWindow::IsWarningWindow(hWnd)) {
 437                 return 1;
 438             }
 439         } else {
 440             if (comp->IsTopLevel() && !((AwtWindow*)comp)->IsFocusableWindow()) {
 441                 return 1; // Don't change focus/activation.
 442             }
 443         }
 444     }
 445     return ::CallNextHookEx(AwtWindow::ms_hCBTFilter, nCode, wParam, lParam);
 446 }
 447 
 448 void AwtWindow::InitSecurityWarningSize(JNIEnv *env)
 449 {
 450     warningWindowWidth = ::GetSystemMetrics(SM_CXSMICON);
 451     warningWindowHeight = ::GetSystemMetrics(SM_CYSMICON);
 452 
 453     jobject target = GetTarget(env);
 454 
 455     env->SetIntField(target, AwtWindow::securityWarningWidthID,
 456             warningWindowWidth);
 457     env->SetIntField(target, AwtWindow::securityWarningHeightID,
 458             warningWindowHeight);
 459 
 460     env->DeleteLocalRef(target);
 461 }
 462 
 463 void AwtWindow::CreateHWnd(JNIEnv *env, LPCWSTR title,
 464         DWORD windowStyle,
 465         DWORD windowExStyle,
 466         int x, int y, int w, int h,
 467         HWND hWndParent, HMENU hMenu,
 468         COLORREF colorForeground,
 469         COLORREF colorBackground,
 470         jobject peer)
 471 {
 472     // Retrieve the warning string
 473     // Note: we need to get it before CreateHWnd() happens because
 474     // the isUntrusted() method may be invoked while the HWND
 475     // is being created in response to some window messages.
 476     jobject target = env->GetObjectField(peer, AwtObject::targetID);
 477     jstring javaWarningString =
 478         (jstring)env->CallObjectMethod(target, AwtWindow::getWarningStringMID);
 479 
 480     if (javaWarningString != NULL) {
 481         size_t length = env->GetStringLength(javaWarningString) + 1;
 482         warningString = new WCHAR[length];
 483         env->GetStringRegion(javaWarningString, 0,
 484                 static_cast<jsize>(length - 1), reinterpret_cast<jchar*>(warningString));
 485         warningString[length-1] = L'\0';
 486 
 487         env->DeleteLocalRef(javaWarningString);
 488     }
 489     env->DeleteLocalRef(target);
 490 
 491     AwtCanvas::CreateHWnd(env, title,
 492             windowStyle,
 493             windowExStyle,
 494             x, y, w, h,
 495             hWndParent, hMenu,
 496             colorForeground,
 497             colorBackground,
 498             peer);
 499 
 500     // Now we need to create the warning window.
 501     CreateWarningWindow(env);
 502 }
 503 
 504 void AwtWindow::CreateWarningWindow(JNIEnv *env)
 505 {
 506     if (!IsUntrusted()) {
 507         return;
 508     }
 509 
 510     if (++AwtWindow::untrustedWindowsCounter == 1) {
 511         AwtToolkit::GetInstance().InstallMouseLowLevelHook();
 512     }
 513 
 514     InitSecurityWarningSize(env);
 515 
 516     RECT rect;
 517     CalculateWarningWindowBounds(env, &rect);
 518 
 519     RegisterWarningWindowClass();
 520     warningWindow = ::CreateWindowEx(
 521             WS_EX_NOACTIVATE | WS_EX_LAYERED,
 522             GetWarningWindowClassName(),
 523             warningString,
 524             WS_POPUP,
 525             rect.left, rect.top,
 526             rect.right - rect.left, rect.bottom - rect.top,
 527             GetHWnd(), // owner
 528             NULL, // menu
 529             AwtToolkit::GetInstance().GetModuleHandle(),
 530             NULL // lParam
 531             );
 532     if (warningWindow == NULL) {
 533         //XXX: actually this is bad... We didn't manage to create the widow.
 534         return;
 535     }
 536 
 537     HICON hIcon = GetSecurityWarningIcon();
 538 
 539     ICONINFO ii;
 540     ::GetIconInfo(hIcon, &ii);
 541 
 542     //Note: we assume that every security icon has exactly the same shape.
 543     HRGN rgn = BitmapUtil::BitmapToRgn(ii.hbmColor);
 544     if (rgn) {
 545         ::SetWindowRgn(warningWindow, rgn, TRUE);
 546     }
 547 
 548     // Now we need to create the tooltip control for this window.
 549     if (!ComCtl32Util::GetInstance().IsToolTipControlInitialized()) {
 550         return;
 551     }
 552 
 553     securityTooltipWindow = ::CreateWindowEx(
 554             WS_EX_TOPMOST,
 555             TOOLTIPS_CLASS,
 556             NULL,
 557             WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
 558             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
 559             warningWindow,
 560             NULL,
 561             AwtToolkit::GetInstance().GetModuleHandle(),
 562             NULL
 563             );
 564 
 565     ::SetWindowPos(securityTooltipWindow,
 566             HWND_TOPMOST, 0, 0, 0, 0,
 567             SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
 568 
 569 
 570     // We currently don't expect changing the size of the window,
 571     // hence we may not care of updating the TOOL position/size.
 572     ::GetClientRect(warningWindow, &rect);
 573 
 574     TOOLINFO ti;
 575 
 576     ti.cbSize = sizeof(ti);
 577     ti.uFlags = TTF_SUBCLASS;
 578     ti.hwnd = warningWindow;
 579     ti.hinst = AwtToolkit::GetInstance().GetModuleHandle();
 580     ti.uId = 0;
 581     ti.lpszText = warningString;
 582     ti.rect.left = rect.left;
 583     ti.rect.top = rect.top;
 584     ti.rect.right = rect.right;
 585     ti.rect.bottom = rect.bottom;
 586 
 587     ::SendMessage(securityTooltipWindow, TTM_ADDTOOL,
 588             0, (LPARAM) (LPTOOLINFO) &ti);
 589 }
 590 
 591 void AwtWindow::DestroyWarningWindow()
 592 {
 593     if (!IsUntrusted()) {
 594         return;
 595     }
 596     if (--AwtWindow::untrustedWindowsCounter == 0) {
 597         AwtToolkit::GetInstance().UninstallMouseLowLevelHook();
 598     }
 599     if (warningWindow != NULL) {
 600         // Note that the warningWindow is an owned window, and hence
 601         // it would be destroyed automatically. However, the window
 602         // class may only be unregistered if there's no any single
 603         // window left using this class. Thus, we're destroying the
 604         // warning window manually. Note that the tooltip window
 605         // will be destroyed automatically because it's an owned
 606         // window as well.
 607         ::DestroyWindow(warningWindow);
 608         warningWindow = NULL;
 609         securityTooltipWindow = NULL;
 610         UnregisterWarningWindowClass();
 611     }
 612 }
 613 
 614 void AwtWindow::DestroyHWnd()
 615 {
 616     DestroyWarningWindow();
 617     AwtCanvas::DestroyHWnd();
 618 }
 619 
 620 LPCTSTR AwtWindow::GetWarningWindowClassName()
 621 {
 622     return TEXT("SunAwtWarningWindow");
 623 }
 624 
 625 void AwtWindow::FillWarningWindowClassInfo(WNDCLASS *lpwc)
 626 {
 627     lpwc->style         = 0L;
 628     lpwc->lpfnWndProc   = (WNDPROC)WarningWindowProc;
 629     lpwc->cbClsExtra    = 0;
 630     lpwc->cbWndExtra    = 0;
 631     lpwc->hInstance     = AwtToolkit::GetInstance().GetModuleHandle(),
 632     lpwc->hIcon         = AwtToolkit::GetInstance().GetAwtIcon();
 633     lpwc->hCursor       = ::LoadCursor(NULL, IDC_ARROW);
 634     lpwc->hbrBackground = NULL;
 635     lpwc->lpszMenuName  = NULL;
 636     lpwc->lpszClassName = AwtWindow::GetWarningWindowClassName();
 637 }
 638 
 639 void AwtWindow::RegisterWarningWindowClass()
 640 {
 641     WNDCLASS  wc;
 642 
 643     ::ZeroMemory(&wc, sizeof(wc));
 644 
 645     if (!::GetClassInfo(AwtToolkit::GetInstance().GetModuleHandle(),
 646                         AwtWindow::GetWarningWindowClassName(), &wc))
 647     {
 648         AwtWindow::FillWarningWindowClassInfo(&wc);
 649         ATOM atom = ::RegisterClass(&wc);
 650         DASSERT(atom != 0);
 651     }
 652 }
 653 
 654 void AwtWindow::UnregisterWarningWindowClass()
 655 {
 656     ::UnregisterClass(AwtWindow::GetWarningWindowClassName(), AwtToolkit::GetInstance().GetModuleHandle());
 657 }
 658 
 659 HICON AwtWindow::GetSecurityWarningIcon()
 660 {
 661     HICON ico = AwtToolkit::GetInstance().GetSecurityWarningIcon(securityWarningAnimationStage,
 662             warningWindowWidth, warningWindowHeight);
 663     return ico;
 664 }
 665 
 666 // This function calculates the bounds of the warning window and stores them
 667 // into the RECT structure pointed by the argument rect.
 668 void AwtWindow::CalculateWarningWindowBounds(JNIEnv *env, LPRECT rect)
 669 {
 670     RECT windowBounds;
 671     AwtToolkit::GetWindowRect(GetHWnd(), &windowBounds);
 672 
 673     jobject target = GetTarget(env);
 674     jobject point2D = env->CallObjectMethod(target,
 675             calculateSecurityWarningPositionMID,
 676             (jdouble)windowBounds.left, (jdouble)windowBounds.top,
 677             (jdouble)(windowBounds.right - windowBounds.left),
 678             (jdouble)(windowBounds.bottom - windowBounds.top));
 679     env->DeleteLocalRef(target);
 680 
 681     static jclass point2DClassID = NULL;
 682     static jmethodID point2DGetXMID = NULL;
 683     static jmethodID point2DGetYMID = NULL;
 684 
 685     if (point2DClassID == NULL) {
 686         jclass point2DClassIDLocal = env->FindClass("java/awt/geom/Point2D");
 687         point2DClassID = (jclass)env->NewGlobalRef(point2DClassIDLocal);
 688         env->DeleteLocalRef(point2DClassIDLocal);
 689     }
 690 
 691     if (point2DGetXMID == NULL) {
 692         point2DGetXMID = env->GetMethodID(point2DClassID, "getX", "()D");
 693     }
 694     if (point2DGetYMID == NULL) {
 695         point2DGetYMID = env->GetMethodID(point2DClassID, "getY", "()D");
 696     }
 697 
 698 
 699     int x = (int)env->CallDoubleMethod(point2D, point2DGetXMID);
 700     int y = (int)env->CallDoubleMethod(point2D, point2DGetYMID);
 701 
 702     env->DeleteLocalRef(point2D);
 703 
 704     //Make sure the warning is not far from the window bounds
 705     x = max(x, windowBounds.left - (int)warningWindowWidth - 2);
 706     x = min(x, windowBounds.right + (int)warningWindowWidth + 2);
 707 
 708     y = max(y, windowBounds.top - (int)warningWindowHeight - 2);
 709     y = min(y, windowBounds.bottom + (int)warningWindowHeight + 2);
 710 
 711     // Now make sure the warning window is visible on the screen
 712     HMONITOR hmon = (HMONITOR)MonitorFromWindow(GetHWnd(), MONITOR_DEFAULTTOPRIMARY);
 713     DASSERT(hmon != NULL);
 714 
 715     RECT monitorBounds;
 716     RECT monitorInsets;
 717 
 718     MonitorBounds(hmon, &monitorBounds);
 719     if (!AwtToolkit::GetScreenInsets(m_screenNum, &monitorInsets)) {
 720         ::ZeroMemory(&monitorInsets, sizeof(monitorInsets));
 721     }
 722 
 723     x = max(x, monitorBounds.left + monitorInsets.left);
 724     x = min(x, monitorBounds.right - monitorInsets.right - (int)warningWindowWidth);
 725 
 726     y = max(y, monitorBounds.top + monitorInsets.top);
 727     y = min(y, monitorBounds.bottom - monitorInsets.bottom - (int)warningWindowHeight);
 728 
 729     rect->left = x;
 730     rect->top = y;
 731     rect->right = rect->left + warningWindowWidth;
 732     rect->bottom = rect->top + warningWindowHeight;
 733 }
 734 
 735 LRESULT CALLBACK AwtWindow::WarningWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 736 {
 737     switch (uMsg) {
 738         case WM_PAINT:
 739             PaintWarningWindow(hwnd);
 740             return 0;
 741 
 742         case WM_MOUSEACTIVATE:
 743             {
 744                 // Retrive the owner of the warning window.
 745                 HWND javaWindow = ::GetParent(hwnd);
 746                 if (javaWindow) {
 747                     // If the window is blocked by a modal dialog, substitute
 748                     // its handle with the topmost blocker.
 749                     HWND topmostBlocker = GetTopmostModalBlocker(javaWindow);
 750                     if (::IsWindow(topmostBlocker)) {
 751                         javaWindow = topmostBlocker;
 752                     }
 753 
 754                     ::BringWindowToTop(javaWindow);
 755 
 756                     AwtWindow * window =
 757                         (AwtWindow*)AwtComponent::GetComponent(javaWindow);
 758                     if (window == NULL) {
 759                         // Quite unlikely to go into here, but it's way better
 760                         // than getting a crash.
 761                         ::SetForegroundWindow(javaWindow);
 762                     } else {
 763                         // Activate the window if it is focusable and inactive
 764                         if (window->IsFocusableWindow() &&
 765                                 javaWindow != ::GetActiveWindow()) {
 766                             ::SetForegroundWindow(javaWindow);
 767                         } else {
 768                             // ...otherwise just start the animation.
 769                             window->StartSecurityAnimation(akShow);
 770                         }
 771                     }
 772 
 773                     // In every case if there's a top-most blocker, we need to
 774                     // enable modal animation.
 775                     if (::IsWindow(topmostBlocker)) {
 776                         AwtDialog::AnimateModalBlocker(topmostBlocker);
 777                     }
 778                 }
 779                 return MA_NOACTIVATEANDEAT;
 780             }
 781     }
 782     return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
 783 }
 784 
 785 void AwtWindow::PaintWarningWindow(HWND warningWindow)
 786 {
 787     RECT updateRect;
 788 
 789     if (!::GetUpdateRect(warningWindow, &updateRect, FALSE)) {
 790         // got nothing to update
 791         return;
 792     }
 793 
 794     PAINTSTRUCT ps;
 795     HDC hdc = ::BeginPaint(warningWindow, &ps);
 796     if (hdc == NULL) {
 797         // indicates an error
 798         return;
 799     }
 800 
 801     PaintWarningWindow(warningWindow, hdc);
 802 
 803     ::EndPaint(warningWindow, &ps);
 804 }
 805 
 806 void AwtWindow::PaintWarningWindow(HWND warningWindow, HDC hdc)
 807 {
 808     HWND javaWindow = ::GetParent(warningWindow);
 809 
 810     AwtWindow * window = (AwtWindow*)AwtComponent::GetComponent(javaWindow);
 811     if (window == NULL) {
 812         return;
 813     }
 814 
 815     ::DrawIconEx(hdc, 0, 0, window->GetSecurityWarningIcon(),
 816             window->warningWindowWidth, window->warningWindowHeight,
 817             0, NULL, DI_NORMAL);
 818 }
 819 
 820 static const UINT_PTR IDT_AWT_SECURITYANIMATION = 0x102;
 821 
 822 // Approximately 6 times a second. 0.75 seconds total.
 823 static const UINT securityAnimationTimerElapse = 150;
 824 static const UINT securityAnimationMaxIterations = 5;
 825 
 826 void AwtWindow::RepaintWarningWindow()
 827 {
 828     HDC hdc = ::GetDC(warningWindow);
 829     PaintWarningWindow(warningWindow, hdc);
 830     ::ReleaseDC(warningWindow, hdc);
 831 }
 832 
 833 void AwtWindow::StartSecurityAnimation(AnimationKind kind)
 834 {
 835     if (!IsUntrusted()) {
 836         return;
 837     }
 838     if (warningWindow == NULL) {
 839         return;
 840     }
 841 
 842     securityAnimationKind = kind;
 843 
 844     securityWarningAnimationStage = 1;
 845     ::SetTimer(GetHWnd(), IDT_AWT_SECURITYANIMATION,
 846             securityAnimationTimerElapse, NULL);
 847 
 848     if (securityAnimationKind == akShow) {
 849         ::SetWindowPos(warningWindow, HWND_NOTOPMOST, 0, 0, 0, 0,
 850                 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE |
 851                 SWP_SHOWWINDOW);
 852 
 853         ::SetLayeredWindowAttributes(warningWindow, RGB(0, 0, 0),
 854                 0xFF, LWA_ALPHA);
 855         ::RedrawWindow(warningWindow, NULL, NULL,
 856                 RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);
 857     }
 858 }
 859 
 860 void AwtWindow::StopSecurityAnimation()
 861 {
 862     if (!IsUntrusted()) {
 863         return;
 864     }
 865     if (warningWindow == NULL) {
 866         return;
 867     }
 868 
 869     securityWarningAnimationStage = 0;
 870     ::KillTimer(GetHWnd(), IDT_AWT_SECURITYANIMATION);
 871 
 872     switch (securityAnimationKind) {
 873         case akHide:
 874         case akPreHide:
 875             ::SetWindowPos(warningWindow, HWND_NOTOPMOST, 0, 0, 0, 0,
 876                     SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE |
 877                     SWP_HIDEWINDOW);
 878             break;
 879         case akShow:
 880             RepaintWarningWindow();
 881             break;
 882     }
 883 
 884     securityAnimationKind = akNone;
 885 }
 886 
 887 MsgRouting AwtWindow::WmTimer(UINT_PTR timerID)
 888 {
 889     if (timerID != IDT_AWT_SECURITYANIMATION) {
 890         return mrPassAlong;
 891     }
 892 
 893     if (securityWarningAnimationStage == 0) {
 894         return mrConsume;
 895     }
 896 
 897     securityWarningAnimationStage++;
 898     if (securityWarningAnimationStage >= securityAnimationMaxIterations) {
 899         if (securityAnimationKind == akPreHide) {
 900             // chain real hiding
 901             StartSecurityAnimation(akHide);
 902         } else {
 903             StopSecurityAnimation();
 904         }
 905     } else {
 906         switch (securityAnimationKind) {
 907             case akHide:
 908                 {
 909                     BYTE opacity = ((int)0xFF *
 910                             (securityAnimationMaxIterations -
 911                              securityWarningAnimationStage)) /
 912                         securityAnimationMaxIterations;
 913                     ::SetLayeredWindowAttributes(warningWindow,
 914                             RGB(0, 0, 0), opacity, LWA_ALPHA);
 915                 }
 916                 break;
 917             case akShow:
 918             case akNone: // quite unlikely, but quite safe
 919                 RepaintWarningWindow();
 920                 break;
 921         }
 922     }
 923 
 924     return mrConsume;
 925 }
 926 
 927 // The security warning is visible if:
 928 //    1. The window has the keyboard window focus, OR
 929 //    2. The mouse pointer is located within the window bounds,
 930 //       or within the security warning icon.
 931 void AwtWindow::UpdateSecurityWarningVisibility()
 932 {
 933     if (!IsUntrusted()) {
 934         return;
 935     }
 936     if (warningWindow == NULL) {
 937         return;
 938     }
 939 
 940     bool show = false;
 941 
 942     if (IsVisible() && currentWmSizeState != SIZE_MINIMIZED) {
 943         if (AwtComponent::GetFocusedWindow() == GetHWnd()) {
 944             show = true;
 945         }
 946 
 947         HWND hwnd = AwtToolkit::GetInstance().GetWindowUnderMouse();
 948         if (hwnd == GetHWnd()) {
 949             show = true;
 950         }
 951         if (hwnd == warningWindow) {
 952             show = true;
 953         }
 954     }
 955 
 956     if (show && (!::IsWindowVisible(warningWindow) ||
 957                 securityAnimationKind == akHide ||
 958                 securityAnimationKind == akPreHide)) {
 959         StartSecurityAnimation(akShow);
 960     }
 961     if (!show && ::IsWindowVisible(warningWindow)) {
 962         StartSecurityAnimation(akPreHide);
 963     }
 964 }
 965 
 966 void AwtWindow::FocusedWindowChanged(HWND from, HWND to)
 967 {
 968     AwtWindow * fw = (AwtWindow *)AwtComponent::GetComponent(from);
 969     AwtWindow * tw = (AwtWindow *)AwtComponent::GetComponent(to);
 970 
 971     if (fw != NULL) {
 972         fw->UpdateSecurityWarningVisibility();
 973     }
 974     if (tw != NULL) {
 975         tw->UpdateSecurityWarningVisibility();
 976 
 977         // Flash on receiving the keyboard focus even if the warning
 978         // has already been shown (e.g. by hovering with the mouse)
 979         tw->StartSecurityAnimation(akShow);
 980     }
 981 }
 982 
 983 void AwtWindow::_RepositionSecurityWarning(void* param)
 984 {
 985     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 986 
 987     RepositionSecurityWarningStruct *rsws =
 988         (RepositionSecurityWarningStruct *)param;
 989     jobject self = rsws->window;
 990 
 991     PDATA pData;
 992     JNI_CHECK_PEER_GOTO(self, ret);
 993     AwtWindow *window = (AwtWindow *)pData;
 994 
 995     window->RepositionSecurityWarning(env);
 996 
 997   ret:
 998     env->DeleteGlobalRef(self);
 999     delete rsws;
1000 }
1001 
1002 /* Create a new AwtWindow object and window.   */
1003 AwtWindow* AwtWindow::Create(jobject self, jobject parent)
1004 {
1005     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1006 
1007     jobject target = NULL;
1008     AwtWindow* window = NULL;
1009 
1010     try {
1011         if (env->EnsureLocalCapacity(1) < 0) {
1012             return NULL;
1013         }
1014 
1015         AwtWindow* awtParent = NULL;
1016 
1017         PDATA pData;
1018         if (parent != NULL) {
1019             JNI_CHECK_PEER_GOTO(parent, done);
1020             awtParent = (AwtWindow *)pData;
1021         }
1022 
1023         target = env->GetObjectField(self, AwtObject::targetID);
1024         JNI_CHECK_NULL_GOTO(target, "null target", done);
1025 
1026         window = new AwtWindow();
1027 
1028         {
1029             if (JNU_IsInstanceOfByName(env, target, "javax/swing/Popup$HeavyWeightWindow") > 0) {
1030                 window->m_isRetainingHierarchyZOrder = TRUE;
1031             }
1032             DWORD style = WS_CLIPCHILDREN | WS_POPUP;
1033             DWORD exStyle = 0;
1034             if (GetRTL()) {
1035                 exStyle |= WS_EX_RIGHT | WS_EX_LEFTSCROLLBAR;
1036                 if (GetRTLReadingOrder())
1037                     exStyle |= WS_EX_RTLREADING;
1038             }
1039             if (awtParent != NULL) {
1040                 window->InitOwner(awtParent);
1041             } else {
1042                 // specify WS_EX_TOOLWINDOW to remove parentless windows from taskbar
1043                 exStyle |= WS_EX_TOOLWINDOW;
1044             }
1045             window->CreateHWnd(env, L"",
1046                                style, exStyle,
1047                                0, 0, 0, 0,
1048                                (awtParent != NULL) ? awtParent->GetHWnd() : NULL,
1049                                NULL,
1050                                ::GetSysColor(COLOR_WINDOWTEXT),
1051                                ::GetSysColor(COLOR_WINDOW),
1052                                self);
1053 
1054             jint x = env->GetIntField(target, AwtComponent::xID);
1055             jint y = env->GetIntField(target, AwtComponent::yID);
1056             jint width = env->GetIntField(target, AwtComponent::widthID);
1057             jint height = env->GetIntField(target, AwtComponent::heightID);
1058 
1059             /*
1060              * Initialize icon as inherited from parent if it exists
1061              */
1062             if (parent != NULL) {
1063                 window->m_hIcon = awtParent->GetHIcon();
1064                 window->m_hIconSm = awtParent->GetHIconSm();
1065                 window->m_iconInherited = TRUE;
1066             }
1067             window->DoUpdateIcon();
1068 
1069 
1070             /*
1071              * Reshape here instead of during create, so that a WM_NCCALCSIZE
1072              * is sent.
1073              */
1074             window->Reshape(x, y, width, height);
1075         }
1076     } catch (...) {
1077         env->DeleteLocalRef(target);
1078         throw;
1079     }
1080 
1081 done:
1082     env->DeleteLocalRef(target);
1083     return window;
1084 }
1085 
1086 BOOL AwtWindow::IsOneOfOwnersOf(AwtWindow * wnd) {
1087     while (wnd != NULL) {
1088         if (wnd == this || wnd->GetOwningFrameOrDialog() == this) return TRUE;
1089         wnd = (AwtWindow*)GetComponent(::GetWindow(wnd->GetHWnd(), GW_OWNER));
1090     }
1091     return FALSE;
1092 }
1093 
1094 void AwtWindow::InitOwner(AwtWindow *owner)
1095 {
1096     DASSERT(owner != NULL);
1097     while (owner != NULL && owner->IsSimpleWindow()) {
1098 
1099         HWND ownerOwnerHWND = ::GetWindow(owner->GetHWnd(), GW_OWNER);
1100         if (ownerOwnerHWND == NULL) {
1101             owner = NULL;
1102             break;
1103         }
1104         owner = (AwtWindow *)AwtComponent::GetComponent(ownerOwnerHWND);
1105     }
1106     m_owningFrameDialog = (AwtFrame *)owner;
1107 }
1108 
1109 void AwtWindow::moveToDefaultLocation() {
1110     HWND boggy = ::CreateWindow(GetClassName(), L"BOGGY", WS_OVERLAPPED, CW_USEDEFAULT, 0 ,0, 0,
1111         NULL, NULL, NULL, NULL);
1112     RECT defLoc;
1113 
1114     // Fixed 6477497: Windows drawn off-screen on Win98, even when java.awt.Window.locationByPlatform is set
1115     //    Win9x does not position a window until the window is shown.
1116     //    The behavior is slightly opposite to the WinNT (and up), where
1117     //    Windows will position the window upon creation of the window.
1118     //    That's why we have to manually set the left & top values of
1119     //    the defLoc to 0 if the GetWindowRect function returns FALSE.
1120     BOOL result = ::GetWindowRect(boggy, &defLoc);
1121     if (!result) {
1122         defLoc.left = defLoc.top = 0;
1123     }
1124     VERIFY(::DestroyWindow(boggy));
1125     VERIFY(::SetWindowPos(GetHWnd(), NULL, defLoc.left, defLoc.top, 0, 0, SWP_NOSIZE | SWP_NOZORDER));
1126 }
1127 
1128 void AwtWindow::Show()
1129 {
1130     m_visible = true;
1131     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1132     BOOL  done = false;
1133     HWND hWnd = GetHWnd();
1134 
1135     if (env->EnsureLocalCapacity(2) < 0) {
1136         return;
1137     }
1138     jobject target = GetTarget(env);
1139     INT nCmdShow;
1140 
1141     AwtFrame* owningFrame = GetOwningFrameOrDialog();
1142     if (IsFocusableWindow() && IsAutoRequestFocus() && owningFrame != NULL &&
1143         ::GetForegroundWindow() == owningFrame->GetHWnd())
1144     {
1145         nCmdShow = SW_SHOW;
1146     } else {
1147         nCmdShow = SW_SHOWNA;
1148     }
1149 
1150     BOOL locationByPlatform = env->GetBooleanField(GetTarget(env), AwtWindow::locationByPlatformID);
1151 
1152     if (locationByPlatform) {
1153          moveToDefaultLocation();
1154     }
1155 
1156     // The following block exists to support Menu/Tooltip animation for
1157     // Swing programs in a way which avoids introducing any new public api into
1158     // AWT or Swing.
1159     // This code should eventually be replaced by a better longterm solution
1160     // which might involve tagging java.awt.Window instances with a semantic
1161     // property so platforms can animate/decorate/etc accordingly.
1162     //
1163     if ((IS_WIN98 || IS_WIN2000) &&
1164         JNU_IsInstanceOfByName(env, target, "com/sun/java/swing/plaf/windows/WindowsPopupWindow") > 0)
1165     {
1166         // need this global ref to make the class unloadable (see 6500204)
1167         static jclass windowsPopupWindowCls;
1168         static jfieldID windowTypeFID = NULL;
1169         jint windowType = 0;
1170         BOOL  animateflag = FALSE;
1171         BOOL  fadeflag = FALSE;
1172         DWORD animateStyle = 0;
1173 
1174         if (windowTypeFID == NULL) {
1175             // Initialize Window type constants ONCE...
1176 
1177             jfieldID windowTYPESFID[TYPES_COUNT];
1178             jclass cls = env->GetObjectClass(target);
1179             windowTypeFID = env->GetFieldID(cls, "windowType", "I");
1180 
1181             windowTYPESFID[UNSPECIFIED] = env->GetStaticFieldID(cls, "UNDEFINED_WINDOW_TYPE", "I");
1182             windowTYPESFID[TOOLTIP] = env->GetStaticFieldID(cls, "TOOLTIP_WINDOW_TYPE", "I");
1183             windowTYPESFID[MENU] = env->GetStaticFieldID(cls, "MENU_WINDOW_TYPE", "I");
1184             windowTYPESFID[SUBMENU] = env->GetStaticFieldID(cls, "SUBMENU_WINDOW_TYPE", "I");
1185             windowTYPESFID[POPUPMENU] = env->GetStaticFieldID(cls, "POPUPMENU_WINDOW_TYPE", "I");
1186             windowTYPESFID[COMBOBOX_POPUP] = env->GetStaticFieldID(cls, "COMBOBOX_POPUP_WINDOW_TYPE", "I");
1187 
1188             for (int i=0; i < 6; i++) {
1189                 windowTYPES[i] = env->GetStaticIntField(cls, windowTYPESFID[i]);
1190             }
1191             windowsPopupWindowCls = (jclass) env->NewGlobalRef(cls);
1192             env->DeleteLocalRef(cls);
1193         }
1194         windowType = env->GetIntField(target, windowTypeFID);
1195 
1196         if (windowType == windowTYPES[TOOLTIP]) {
1197             if (IS_WIN2000) {
1198                 SystemParametersInfo(SPI_GETTOOLTIPANIMATION, 0, &animateflag, 0);
1199                 SystemParametersInfo(SPI_GETTOOLTIPFADE, 0, &fadeflag, 0);
1200             } else {
1201                 // use same setting as menus
1202                 SystemParametersInfo(SPI_GETMENUANIMATION, 0, &animateflag, 0);
1203             }
1204             if (animateflag) {
1205               // AW_BLEND currently produces runtime parameter error
1206               // animateStyle = fadeflag? AW_BLEND : AW_SLIDE | AW_VER_POSITIVE;
1207                  animateStyle = fadeflag? 0 : AW_SLIDE | AW_VER_POSITIVE;
1208             }
1209         } else if (windowType == windowTYPES[MENU] || windowType == windowTYPES[SUBMENU] ||
1210                    windowType == windowTYPES[POPUPMENU]) {
1211             SystemParametersInfo(SPI_GETMENUANIMATION, 0, &animateflag, 0);
1212             if (animateflag) {
1213 
1214                 if (IS_WIN2000) {
1215                     SystemParametersInfo(SPI_GETMENUFADE, 0, &fadeflag, 0);
1216                     if (fadeflag) {
1217                       // AW_BLEND currently produces runtime parameter error
1218                       //animateStyle = AW_BLEND;
1219                     }
1220                 }
1221                 if (animateStyle == 0 && !fadeflag) {
1222                     animateStyle = AW_SLIDE;
1223                     if (windowType == windowTYPES[MENU]) {
1224                       animateStyle |= AW_VER_POSITIVE;
1225                     } else if (windowType == windowTYPES[SUBMENU]) {
1226                       animateStyle |= AW_HOR_POSITIVE;
1227                     } else { /* POPUPMENU */
1228                       animateStyle |= (AW_VER_POSITIVE | AW_HOR_POSITIVE);
1229                     }
1230                 }
1231             }
1232         } else if (windowType == windowTYPES[COMBOBOX_POPUP]) {
1233             SystemParametersInfo(SPI_GETCOMBOBOXANIMATION, 0, &animateflag, 0);
1234             if (animateflag) {
1235                  animateStyle = AW_SLIDE | AW_VER_POSITIVE;
1236             }
1237         }
1238 
1239         if (animateStyle != 0) {
1240             load_user_procs();
1241 
1242             if (fn_animate_window != NULL) {
1243                 BOOL result = (*fn_animate_window)(hWnd, (DWORD)200, animateStyle);
1244                 if (result == 0) {
1245                     LPTSTR      msgBuffer = NULL;
1246                     FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1247                       FORMAT_MESSAGE_FROM_SYSTEM |
1248                       FORMAT_MESSAGE_IGNORE_INSERTS,
1249                       NULL,
1250                       GetLastError(),
1251                       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
1252                       (LPTSTR)&msgBuffer, // it's an output parameter when allocate buffer is used
1253                       0,
1254                       NULL);
1255 
1256                     if (msgBuffer == NULL) {
1257                         msgBuffer = TEXT("<Could not get GetLastError() message text>");
1258                     }
1259                     _ftprintf(stderr,TEXT("AwtWindow::Show: AnimateWindow: "));
1260                     _ftprintf(stderr,msgBuffer);
1261                     LocalFree(msgBuffer);
1262                 } else {
1263                   // WM_PAINT is not automatically sent when invoking AnimateWindow,
1264                   // so force an expose event
1265                     RECT rect;
1266                     ::GetWindowRect(hWnd,&rect);
1267                     ::ScreenToClient(hWnd, (LPPOINT)&rect);
1268                     ::InvalidateRect(hWnd,&rect,TRUE);
1269                     ::UpdateWindow(hWnd);
1270                     done = TRUE;
1271                 }
1272             }
1273         }
1274     }
1275     if (!done) {
1276         ::ShowWindow(GetHWnd(), nCmdShow);
1277     }
1278     env->DeleteLocalRef(target);
1279 }
1280 
1281 /*
1282  * Get and return the insets for this window (container, really).
1283  * Calculate & cache them while we're at it, for use by AwtGraphics
1284  */
1285 BOOL AwtWindow::UpdateInsets(jobject insets)
1286 {
1287     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1288     DASSERT(GetPeer(env) != NULL);
1289     if (env->EnsureLocalCapacity(2) < 0) {
1290         return FALSE;
1291     }
1292 
1293     // fix 4167248 : don't update insets when frame is iconified
1294     // to avoid bizarre window/client rectangles
1295     if (::IsIconic(GetHWnd())) {
1296         return FALSE;
1297     }
1298 
1299     /*
1300      * Code to calculate insets. Stores results in frame's data
1301      * members, and in the peer's Inset object.
1302      */
1303     RECT outside;
1304     RECT inside;
1305     int extraBottomInsets = 0;
1306 
1307     ::GetClientRect(GetHWnd(), &inside);
1308     ::GetWindowRect(GetHWnd(), &outside);
1309 
1310     /* Update our inset member */
1311     if (outside.right - outside.left > 0 && outside.bottom - outside.top > 0) {
1312         ::MapWindowPoints(GetHWnd(), 0, (LPPOINT)&inside, 2);
1313         m_insets.top = inside.top - outside.top;
1314         m_insets.bottom = outside.bottom - inside.bottom + extraBottomInsets;
1315         m_insets.left = inside.left - outside.left;
1316         m_insets.right = outside.right - inside.right;
1317     } else {
1318         m_insets.top = -1;
1319     }
1320     if (m_insets.left < 0 || m_insets.top < 0 ||
1321         m_insets.right < 0 || m_insets.bottom < 0)
1322     {
1323         /* This window hasn't been sized yet -- use system metrics. */
1324         jobject target = GetTarget(env);
1325         if (IsUndecorated() == FALSE) {
1326             /* Get outer frame sizes. */
1327             LONG style = GetStyle();
1328             if (style & WS_THICKFRAME) {
1329                 m_insets.left = m_insets.right =
1330                     ::GetSystemMetrics(SM_CXSIZEFRAME);
1331                 m_insets.top = m_insets.bottom =
1332                     ::GetSystemMetrics(SM_CYSIZEFRAME);
1333             } else {
1334                 m_insets.left = m_insets.right =
1335                     ::GetSystemMetrics(SM_CXDLGFRAME);
1336                 m_insets.top = m_insets.bottom =
1337                     ::GetSystemMetrics(SM_CYDLGFRAME);
1338             }
1339 
1340 
1341             /* Add in title. */
1342             m_insets.top += ::GetSystemMetrics(SM_CYCAPTION);
1343         }
1344         else {
1345             /* fix for 4418125: Undecorated frames are off by one */
1346             /* undo the -1 set above */
1347             /* Additional fix for 5059656 */
1348                 /* Also, 5089312: Window insets should be 0. */
1349             ::memset(&m_insets, 0, sizeof(m_insets));
1350         }
1351 
1352         /* Add in menuBar, if any. */
1353         if (JNU_IsInstanceOfByName(env, target, "java/awt/Frame") > 0 &&
1354             ((AwtFrame*)this)->GetMenuBar()) {
1355             m_insets.top += ::GetSystemMetrics(SM_CYMENU);
1356         }
1357         m_insets.bottom += extraBottomInsets;
1358         env->DeleteLocalRef(target);
1359     }
1360 
1361     BOOL insetsChanged = FALSE;
1362 
1363     jobject peer = GetPeer(env);
1364     /* Get insets into our peer directly */
1365     jobject peerInsets = (env)->GetObjectField(peer, AwtPanel::insets_ID);
1366     DASSERT(!safe_ExceptionOccurred(env));
1367     if (peerInsets != NULL) { // may have been called during creation
1368         (env)->SetIntField(peerInsets, AwtInsets::topID, m_insets.top);
1369         (env)->SetIntField(peerInsets, AwtInsets::bottomID,
1370                            m_insets.bottom);
1371         (env)->SetIntField(peerInsets, AwtInsets::leftID, m_insets.left);
1372         (env)->SetIntField(peerInsets, AwtInsets::rightID, m_insets.right);
1373     }
1374     /* Get insets into the Inset object (if any) that was passed */
1375     if (insets != NULL) {
1376         (env)->SetIntField(insets, AwtInsets::topID, m_insets.top);
1377         (env)->SetIntField(insets, AwtInsets::bottomID, m_insets.bottom);
1378         (env)->SetIntField(insets, AwtInsets::leftID, m_insets.left);
1379         (env)->SetIntField(insets, AwtInsets::rightID, m_insets.right);
1380     }
1381     env->DeleteLocalRef(peerInsets);
1382 
1383     insetsChanged = !::EqualRect( &m_old_insets, &m_insets );
1384     ::CopyRect( &m_old_insets, &m_insets );
1385 
1386     if (insetsChanged) {
1387         // Since insets are changed we need to update the surfaceData object
1388         // to reflect that change
1389         env->CallVoidMethod(peer, AwtComponent::replaceSurfaceDataLaterMID);
1390     }
1391 
1392     return insetsChanged;
1393 }
1394 
1395 /**
1396  * Sometimes we need the hWnd that actually owns this Window's hWnd (if
1397  * there is an owner).
1398  */
1399 HWND AwtWindow::GetTopLevelHWnd()
1400 {
1401     return m_owningFrameDialog ? m_owningFrameDialog->GetHWnd() :
1402                                  GetHWnd();
1403 }
1404 
1405 /*
1406  * Although this function sends ComponentEvents, it needs to be defined
1407  * here because only top-level windows need to have move and resize
1408  * events fired from native code.  All contained windows have these events
1409  * fired from common Java code.
1410  */
1411 void AwtWindow::SendComponentEvent(jint eventId)
1412 {
1413     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1414 
1415     static jclass classEvent = NULL;
1416     if (classEvent == NULL) {
1417         if (env->PushLocalFrame(1) < 0)
1418             return;
1419         classEvent = env->FindClass("java/awt/event/ComponentEvent");
1420         if (classEvent != NULL) {
1421             classEvent = (jclass)env->NewGlobalRef(classEvent);
1422         }
1423         env->PopLocalFrame(0);
1424     }
1425     static jmethodID eventInitMID = NULL;
1426     if (eventInitMID == NULL) {
1427         eventInitMID = env->GetMethodID(classEvent, "<init>",
1428                                         "(Ljava/awt/Component;I)V");
1429         if (eventInitMID == NULL) {
1430             return;
1431         }
1432     }
1433     if (env->EnsureLocalCapacity(2) < 0) {
1434         return;
1435     }
1436     jobject target = GetTarget(env);
1437     jobject event = env->NewObject(classEvent, eventInitMID,
1438                                    target, eventId);
1439     DASSERT(!safe_ExceptionOccurred(env));
1440     DASSERT(event != NULL);
1441     SendEvent(event);
1442 
1443     env->DeleteLocalRef(target);
1444     env->DeleteLocalRef(event);
1445 }
1446 
1447 void AwtWindow::SendWindowEvent(jint id, HWND opposite,
1448                                 jint oldState, jint newState)
1449 {
1450     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1451 
1452     static jclass wClassEvent;
1453     if (wClassEvent == NULL) {
1454         if (env->PushLocalFrame(1) < 0)
1455             return;
1456         wClassEvent = env->FindClass("java/awt/event/WindowEvent");
1457         if (wClassEvent != NULL) {
1458             wClassEvent = (jclass)env->NewGlobalRef(wClassEvent);
1459         }
1460         env->PopLocalFrame(0);
1461         if (wClassEvent == NULL) {
1462             return;
1463         }
1464     }
1465 
1466     static jmethodID wEventInitMID;
1467     if (wEventInitMID == NULL) {
1468         wEventInitMID =
1469             env->GetMethodID(wClassEvent, "<init>",
1470                              "(Ljava/awt/Window;ILjava/awt/Window;II)V");
1471         DASSERT(wEventInitMID);
1472         if (wEventInitMID == NULL) {
1473             return;
1474         }
1475     }
1476 
1477     static jclass sequencedEventCls;
1478     if (sequencedEventCls == NULL) {
1479         jclass sequencedEventClsLocal
1480             = env->FindClass("java/awt/SequencedEvent");
1481         DASSERT(sequencedEventClsLocal);
1482         if (sequencedEventClsLocal == NULL) {
1483             /* exception already thrown */
1484             return;
1485         }
1486         sequencedEventCls =
1487             (jclass)env->NewGlobalRef(sequencedEventClsLocal);
1488         env->DeleteLocalRef(sequencedEventClsLocal);
1489     }
1490 
1491     static jmethodID sequencedEventConst;
1492     if (sequencedEventConst == NULL) {
1493         sequencedEventConst =
1494             env->GetMethodID(sequencedEventCls, "<init>",
1495                              "(Ljava/awt/AWTEvent;)V");
1496     }
1497 
1498     if (env->EnsureLocalCapacity(3) < 0) {
1499         return;
1500     }
1501 
1502     jobject target = GetTarget(env);
1503     jobject jOpposite = NULL;
1504     if (opposite != NULL) {
1505         AwtComponent *awtOpposite = AwtComponent::GetComponent(opposite);
1506         if (awtOpposite != NULL) {
1507             jOpposite = awtOpposite->GetTarget(env);
1508         }
1509     }
1510     jobject event = env->NewObject(wClassEvent, wEventInitMID, target, id,
1511                                    jOpposite, oldState, newState);
1512     DASSERT(!safe_ExceptionOccurred(env));
1513     DASSERT(event != NULL);
1514     if (jOpposite != NULL) {
1515         env->DeleteLocalRef(jOpposite); jOpposite = NULL;
1516     }
1517     env->DeleteLocalRef(target); target = NULL;
1518 
1519     if (id == java_awt_event_WindowEvent_WINDOW_GAINED_FOCUS ||
1520         id == java_awt_event_WindowEvent_WINDOW_LOST_FOCUS)
1521     {
1522         jobject sequencedEvent = env->NewObject(sequencedEventCls,
1523                                                 sequencedEventConst,
1524                                                 event);
1525         DASSERT(!safe_ExceptionOccurred(env));
1526         DASSERT(sequencedEvent != NULL);
1527         env->DeleteLocalRef(event);
1528         event = sequencedEvent;
1529     }
1530 
1531     SendEvent(event);
1532 
1533     env->DeleteLocalRef(event);
1534 }
1535 
1536 MsgRouting AwtWindow::WmActivate(UINT nState, BOOL fMinimized, HWND opposite)
1537 {
1538     jint type;
1539 
1540     if (nState != WA_INACTIVE) {
1541         ::SetFocus((sm_focusOwner == NULL ||
1542                     AwtComponent::GetTopLevelParentForWindow(sm_focusOwner) !=
1543                     GetHWnd()) ? NULL : sm_focusOwner);
1544         type = java_awt_event_WindowEvent_WINDOW_GAINED_FOCUS;
1545         AwtToolkit::GetInstance().
1546             InvokeFunctionLater(BounceActivation, this);
1547         AwtComponent::SetFocusedWindow(GetHWnd());
1548     } else {
1549         if (m_grabbedWindow != NULL && !m_grabbedWindow->IsOneOfOwnersOf(this)) {
1550             m_grabbedWindow->Ungrab();
1551         }
1552         type = java_awt_event_WindowEvent_WINDOW_LOST_FOCUS;
1553         AwtComponent::SetFocusedWindow(NULL);
1554     }
1555 
1556     SendWindowEvent(type, opposite);
1557     return mrConsume;
1558 }
1559 
1560 void AwtWindow::BounceActivation(void *self) {
1561     AwtWindow *wSelf = (AwtWindow *)self;
1562 
1563     if (::GetActiveWindow() == wSelf->GetHWnd()) {
1564         AwtFrame *owner = wSelf->GetOwningFrameOrDialog();
1565 
1566         if (owner != NULL) {
1567             sm_suppressFocusAndActivation = TRUE;
1568             ::SetActiveWindow(owner->GetHWnd());
1569             ::SetFocus(owner->GetProxyFocusOwner());
1570             sm_suppressFocusAndActivation = FALSE;
1571         }
1572     }
1573 }
1574 
1575 MsgRouting AwtWindow::WmCreate()
1576 {
1577     return mrDoDefault;
1578 }
1579 
1580 MsgRouting AwtWindow::WmClose()
1581 {
1582     SendWindowEvent(java_awt_event_WindowEvent_WINDOW_CLOSING);
1583 
1584     /* Rely on above notification to handle quitting as needed */
1585     return mrConsume;
1586 }
1587 
1588 MsgRouting AwtWindow::WmDestroy()
1589 {
1590     SendWindowEvent(java_awt_event_WindowEvent_WINDOW_CLOSED);
1591     return AwtComponent::WmDestroy();
1592 }
1593 
1594 MsgRouting AwtWindow::WmShowWindow(BOOL show, UINT status)
1595 {
1596     /*
1597      * Original fix for 4810575. Modified for 6386592.
1598      * If an owned window (not frame/dialog) gets disposed we should synthesize
1599      * WM_ACTIVATE for its nearest owner. This is not performed by default because
1600      * the owner frame/dialog is natively active.
1601      */
1602     HWND hwndSelf = GetHWnd();
1603     HWND hwndParent = ::GetParent(hwndSelf);
1604 
1605     if (!show && IsSimpleWindow() && hwndSelf == AwtComponent::GetFocusedWindow() &&
1606         hwndParent != NULL && ::IsWindowVisible(hwndParent))
1607     {
1608         ::PostMessage(hwndParent, WM_ACTIVATE, (WPARAM)WA_ACTIVE, (LPARAM)hwndSelf);
1609     }
1610 
1611     //Fixed 4842599: REGRESSION: JPopupMenu not Hidden Properly After Iconified and Deiconified
1612     if (show && (status == SW_PARENTOPENING)) {
1613         if (!IsVisible()) {
1614             return mrConsume;
1615         }
1616     }
1617     return AwtCanvas::WmShowWindow(show, status);
1618 }
1619 
1620 /*
1621  * Override AwtComponent's move handling to first update the
1622  * java AWT target's position fields directly, since Windows
1623  * and below can be resized from outside of java (by user)
1624  */
1625 MsgRouting AwtWindow::WmMove(int x, int y)
1626 {
1627     if ( ::IsIconic(GetHWnd()) ) {
1628     // fixes 4065534 (robi.khan@eng)
1629     // if a window is iconified we don't want to update
1630     // it's target's position since minimized Win32 windows
1631     // move to -32000, -32000 for whatever reason
1632     // NOTE: See also AwtWindow::Reshape
1633         return mrDoDefault;
1634     }
1635 
1636     if (m_screenNum == -1) {
1637     // Set initial value
1638         m_screenNum = GetScreenImOn();
1639     }
1640     else {
1641         CheckIfOnNewScreen();
1642     }
1643 
1644     /* Update the java AWT target component's fields directly */
1645     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1646     if (env->EnsureLocalCapacity(1) < 0) {
1647         return mrConsume;
1648     }
1649     jobject peer = GetPeer(env);
1650     jobject target = env->GetObjectField(peer, AwtObject::targetID);
1651 
1652     RECT rect;
1653     ::GetWindowRect(GetHWnd(), &rect);
1654 
1655     (env)->SetIntField(target, AwtComponent::xID, rect.left);
1656     (env)->SetIntField(target, AwtComponent::yID, rect.top);
1657     (env)->SetIntField(peer, AwtWindow::sysXID, rect.left);
1658     (env)->SetIntField(peer, AwtWindow::sysYID, rect.top);
1659     SendComponentEvent(java_awt_event_ComponentEvent_COMPONENT_MOVED);
1660 
1661     env->DeleteLocalRef(target);
1662     return AwtComponent::WmMove(x, y);
1663 }
1664 
1665 MsgRouting AwtWindow::WmGetMinMaxInfo(LPMINMAXINFO lpmmi)
1666 {
1667     MsgRouting r = AwtCanvas::WmGetMinMaxInfo(lpmmi);
1668     if ((m_minSize.x == 0) && (m_minSize.y == 0)) {
1669         return r;
1670     }
1671     lpmmi->ptMinTrackSize.x = m_minSize.x;
1672     lpmmi->ptMinTrackSize.y = m_minSize.y;
1673     return mrConsume;
1674 }
1675 
1676 MsgRouting AwtWindow::WmSizing()
1677 {
1678     if (!AwtToolkit::GetInstance().IsDynamicLayoutActive()) {
1679         return mrDoDefault;
1680     }
1681 
1682     DTRACE_PRINTLN("AwtWindow::WmSizing  fullWindowDragEnabled");
1683 
1684     SendComponentEvent(java_awt_event_ComponentEvent_COMPONENT_RESIZED);
1685 
1686     HWND thisHwnd = GetHWnd();
1687     if (thisHwnd == NULL) {
1688         return mrDoDefault;
1689     }
1690 
1691     // Call WComponentPeer::dynamicallyLayoutContainer()
1692     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1693     jobject peer = GetPeer(env);
1694     JNU_CallMethodByName(env, NULL, peer, "dynamicallyLayoutContainer", "()V");
1695     DASSERT(!safe_ExceptionOccurred(env));
1696 
1697     return mrDoDefault;
1698 }
1699 
1700 /*
1701  * Override AwtComponent's size handling to first update the
1702  * java AWT target's dimension fields directly, since Windows
1703  * and below can be resized from outside of java (by user)
1704  */
1705 MsgRouting AwtWindow::WmSize(UINT type, int w, int h)
1706 {
1707     currentWmSizeState = type;
1708 
1709     if (type == SIZE_MINIMIZED) {
1710         UpdateSecurityWarningVisibility();
1711         return mrDoDefault;
1712     }
1713 
1714     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1715     if (env->EnsureLocalCapacity(1) < 0)
1716         return mrDoDefault;
1717     jobject target = GetTarget(env);
1718     // fix 4167248 : ensure the insets are up-to-date before using
1719     BOOL insetsChanged = UpdateInsets(NULL);
1720     int newWidth = w + m_insets.left + m_insets.right;
1721     int newHeight = h + m_insets.top + m_insets.bottom;
1722 
1723     (env)->SetIntField(target, AwtComponent::widthID, newWidth);
1724     (env)->SetIntField(target, AwtComponent::heightID, newHeight);
1725 
1726     jobject peer = GetPeer(env);
1727     (env)->SetIntField(peer, AwtWindow::sysWID, newWidth);
1728     (env)->SetIntField(peer, AwtWindow::sysHID, newHeight);
1729 
1730     if (!AwtWindow::IsResizing()) {
1731         WindowResized();
1732     }
1733 
1734     env->DeleteLocalRef(target);
1735     return AwtComponent::WmSize(type, w, h);
1736 }
1737 
1738 MsgRouting AwtWindow::WmPaint(HDC)
1739 {
1740     PaintUpdateRgn(&m_insets);
1741     return mrConsume;
1742 }
1743 
1744 MsgRouting AwtWindow::WmSettingChange(UINT wFlag, LPCTSTR pszSection)
1745 {
1746     if (wFlag == SPI_SETNONCLIENTMETRICS) {
1747     // user changed window metrics in
1748     // Control Panel->Display->Appearance
1749     // which may cause window insets to change
1750         UpdateInsets(NULL);
1751 
1752     // [rray] fix for 4407329 - Changing Active Window Border width in display
1753     //  settings causes problems
1754         WindowResized();
1755         Invalidate(NULL);
1756 
1757         return mrConsume;
1758     }
1759     return mrDoDefault;
1760 }
1761 
1762 MsgRouting AwtWindow::WmNcCalcSize(BOOL fCalcValidRects,
1763                                    LPNCCALCSIZE_PARAMS lpncsp, LRESULT& retVal)
1764 {
1765     MsgRouting mrRetVal = mrDoDefault;
1766 
1767     if (fCalcValidRects == FALSE) {
1768         return mrDoDefault;
1769     }
1770     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1771     if (env->EnsureLocalCapacity(2) < 0) {
1772         return mrConsume;
1773     }
1774     // WM_NCCALCSIZE is usually in response to a resize, but
1775     // also can be triggered by SetWindowPos(SWP_FRAMECHANGED),
1776     // which means the insets will have changed - rnk 4/7/1998
1777     retVal = static_cast<UINT>(DefWindowProc(
1778                 WM_NCCALCSIZE, fCalcValidRects, reinterpret_cast<LPARAM>(lpncsp)));
1779     if (HasValidRect()) {
1780         UpdateInsets(NULL);
1781     }
1782     mrRetVal = mrConsume;
1783     return mrRetVal;
1784 }
1785 
1786 MsgRouting AwtWindow::WmNcHitTest(UINT x, UINT y, LRESULT& retVal)
1787 {
1788     // If this window is blocked by modal dialog, return HTCLIENT for any point of it.
1789     // That prevents it to be moved or resized using the mouse. Disabling these
1790     // actions to be launched from sysmenu is implemented by ignoring WM_SYSCOMMAND
1791     if (::IsWindow(GetModalBlocker(GetHWnd()))) {
1792         retVal = HTCLIENT;
1793     } else {
1794         retVal = DefWindowProc(WM_NCHITTEST, 0, MAKELPARAM(x, y));
1795     }
1796     return mrConsume;
1797 }
1798 
1799 MsgRouting AwtWindow::WmGetIcon(WPARAM iconType, LRESULT& retValue)
1800 {
1801     return mrDoDefault;
1802 }
1803 
1804 LRESULT AwtWindow::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
1805 {
1806     MsgRouting mr = mrDoDefault;
1807     LRESULT retValue = 0L;
1808 
1809     switch(message) {
1810         case WM_GETICON:
1811             mr = WmGetIcon(wParam, retValue);
1812             break;
1813         case WM_SYSCOMMAND:
1814             //Fixed 6355340: Contents of frame are not layed out properly on maximize
1815             if ((wParam & 0xFFF0) == SC_SIZE) {
1816                 AwtWindow::sm_resizing = TRUE;
1817                 mr = WmSysCommand(wParam, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
1818                 if (mr != mrConsume) {
1819                     AwtWindow::DefWindowProc(message, wParam, lParam);
1820                 }
1821                 AwtWindow::sm_resizing = FALSE;
1822                 if (!AwtToolkit::GetInstance().IsDynamicLayoutActive()) {
1823                     WindowResized();
1824                 }
1825                 mr = mrConsume;
1826             }
1827             break;
1828     }
1829 
1830     if (mr != mrConsume) {
1831         retValue = AwtCanvas::WindowProc(message, wParam, lParam);
1832     }
1833     return retValue;
1834 }
1835 
1836 /*
1837  * Fix for BugTraq ID 4041703: keyDown not being invoked.
1838  * This method overrides AwtCanvas::HandleEvent() since
1839  * an empty Window always receives the focus on the activation
1840  * so we don't have to modify the behavior.
1841  */
1842 MsgRouting AwtWindow::HandleEvent(MSG *msg, BOOL synthetic)
1843 {
1844     return AwtComponent::HandleEvent(msg, synthetic);
1845 }
1846 
1847 void AwtWindow::WindowResized()
1848 {
1849     SendComponentEvent(java_awt_event_ComponentEvent_COMPONENT_RESIZED);
1850     // Need to replace surfaceData on resize to catch changes to
1851     // various component-related values, such as insets
1852     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1853     env->CallVoidMethod(m_peerObject, AwtComponent::replaceSurfaceDataLaterMID);
1854 }
1855 
1856 BOOL CALLBACK InvalidateChildRect(HWND hWnd, LPARAM)
1857 {
1858     TRY;
1859 
1860     ::InvalidateRect(hWnd, NULL, TRUE);
1861     return TRUE;
1862 
1863     CATCH_BAD_ALLOC_RET(FALSE);
1864 }
1865 
1866 void AwtWindow::Invalidate(RECT* r)
1867 {
1868     ::InvalidateRect(GetHWnd(), NULL, TRUE);
1869     ::EnumChildWindows(GetHWnd(), (WNDENUMPROC)InvalidateChildRect, 0);
1870 }
1871 
1872 BOOL AwtWindow::IsResizable() {
1873     return m_isResizable;
1874 }
1875 
1876 void AwtWindow::SetResizable(BOOL isResizable)
1877 {
1878     m_isResizable = isResizable;
1879     if (IsEmbeddedFrame()) {
1880         return;
1881     }
1882     LONG style = GetStyle();
1883     LONG resizeStyle = WS_MAXIMIZEBOX;
1884 
1885     if (IsUndecorated() == FALSE) {
1886         resizeStyle |= WS_THICKFRAME;
1887     }
1888 
1889     if (isResizable) {
1890         style |= resizeStyle;
1891     } else {
1892         style &= ~resizeStyle;
1893     }
1894     SetStyle(style);
1895     RedrawNonClient();
1896 }
1897 
1898 // SetWindowPos flags to cause frame edge to be recalculated
1899 static const UINT SwpFrameChangeFlags =
1900     SWP_FRAMECHANGED | /* causes WM_NCCALCSIZE to be called */
1901     SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
1902     SWP_NOACTIVATE | SWP_NOCOPYBITS |
1903     SWP_NOREPOSITION | SWP_NOSENDCHANGING;
1904 
1905 //
1906 // Forces WM_NCCALCSIZE to be called to recalculate
1907 // window border (updates insets) without redrawing it
1908 //
1909 void AwtWindow::RecalcNonClient()
1910 {
1911     ::SetWindowPos(GetHWnd(), (HWND) NULL, 0, 0, 0, 0, SwpFrameChangeFlags|SWP_NOREDRAW);
1912 }
1913 
1914 //
1915 // Forces WM_NCCALCSIZE to be called to recalculate
1916 // window border (updates insets) and redraws border to match
1917 //
1918 void AwtWindow::RedrawNonClient()
1919 {
1920     ::SetWindowPos(GetHWnd(), (HWND) NULL, 0, 0, 0, 0, SwpFrameChangeFlags|SWP_ASYNCWINDOWPOS);
1921 }
1922 
1923 int AwtWindow::GetScreenImOn() {
1924     MHND hmon;
1925     int scrnNum;
1926 
1927     hmon = ::MonitorFromWindow(GetHWnd(), MONITOR_DEFAULT_TO_PRIMARY);
1928     DASSERT(hmon != NULL);
1929 
1930     scrnNum = AwtWin32GraphicsDevice::GetScreenFromMHND(hmon);
1931     DASSERT(scrnNum > -1);
1932 
1933     return scrnNum;
1934 }
1935 
1936 /* Check to see if we've been moved onto another screen.
1937  * If so, update internal data, surfaces, etc.
1938  */
1939 
1940 void AwtWindow::CheckIfOnNewScreen() {
1941     int curScrn = GetScreenImOn();
1942 
1943     if (curScrn != m_screenNum) {  // we've been moved
1944         JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1945 
1946         jclass peerCls = env->GetObjectClass(m_peerObject);
1947         DASSERT(peerCls);
1948 
1949         jmethodID draggedID = env->GetMethodID(peerCls, "draggedToNewScreen",
1950                                                "()V");
1951         DASSERT(draggedID);
1952 
1953         env->CallVoidMethod(m_peerObject, draggedID);
1954         m_screenNum = curScrn;
1955 
1956         env->DeleteLocalRef(peerCls);
1957     }
1958 }
1959 
1960 BOOL AwtWindow::IsFocusableWindow() {
1961     /*
1962      * For Window/Frame/Dialog to accept focus it should:
1963      * - be focusable;
1964      * - be not blocked by any modal blocker.
1965      */
1966     BOOL focusable = m_isFocusableWindow && !::IsWindow(AwtWindow::GetModalBlocker(GetHWnd()));
1967     AwtFrame *owner = GetOwningFrameOrDialog(); // NULL for Frame and Dialog
1968 
1969     if (owner != NULL) {
1970         /*
1971          * Also for Window (not Frame/Dialog) to accept focus:
1972          * - its decorated parent should accept focus;
1973          */
1974         focusable = focusable && owner->IsFocusableWindow();
1975     }
1976     return focusable;
1977 }
1978 
1979 void AwtWindow::SetModalBlocker(HWND window, HWND blocker) {
1980     if (!::IsWindow(window)) {
1981         return;
1982     }
1983 
1984     if (::IsWindow(blocker)) {
1985         ::SetProp(window, ModalBlockerProp, reinterpret_cast<HANDLE>(blocker));
1986         ::EnableWindow(window, FALSE);
1987     } else {
1988         ::RemoveProp(window, ModalBlockerProp);
1989          AwtComponent *comp = AwtComponent::GetComponent(window);
1990          // we don't expect to be called with non-java HWNDs
1991          DASSERT(comp && comp->IsTopLevel());
1992          // we should not unblock disabled toplevels
1993          ::EnableWindow(window, comp->isEnabled());
1994     }
1995 }
1996 
1997 void AwtWindow::SetAndActivateModalBlocker(HWND window, HWND blocker) {
1998     if (!::IsWindow(window)) {
1999         return;
2000     }
2001     AwtWindow::SetModalBlocker(window, blocker);
2002     if (::IsWindow(blocker)) {
2003         // We must check for visibility. Otherwise invisible dialog will receive WM_ACTIVATE.
2004         if (::IsWindowVisible(blocker)) {
2005             ::BringWindowToTop(blocker);
2006             ::SetForegroundWindow(blocker);
2007         }
2008     }
2009 }
2010 
2011 HWND AwtWindow::GetTopmostModalBlocker(HWND window)
2012 {
2013     HWND ret, blocker = NULL;
2014 
2015     do {
2016         ret = blocker;
2017         blocker = AwtWindow::GetModalBlocker(window);
2018         window = blocker;
2019     } while (::IsWindow(blocker));
2020 
2021     return ret;
2022 }
2023 
2024 void AwtWindow::FlashWindowEx(HWND hWnd, UINT count, DWORD timeout, DWORD flags) {
2025     FLASHWINFO fi;
2026     fi.cbSize = sizeof(fi);
2027     fi.hwnd = hWnd;
2028     fi.dwFlags = flags;
2029     fi.uCount = count;
2030     fi.dwTimeout = timeout;
2031     ::FlashWindowEx(&fi);
2032 }
2033 
2034 void AwtWindow::_ToFront(void *param)
2035 {
2036     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2037 
2038     jobject self = (jobject)param;
2039 
2040     AwtWindow *w = NULL;
2041 
2042     PDATA pData;
2043     JNI_CHECK_PEER_GOTO(self, ret);
2044     w = (AwtWindow *)pData;
2045     if (::IsWindow(w->GetHWnd()))
2046     {
2047         UINT flags = SWP_NOMOVE|SWP_NOSIZE;
2048         BOOL focusable = w->IsFocusableWindow();
2049         BOOL autoRequestFocus = w->IsAutoRequestFocus();
2050 
2051         if (!focusable || !autoRequestFocus)
2052         {
2053             flags = flags|SWP_NOACTIVATE;
2054         }
2055         ::SetWindowPos(w->GetHWnd(), HWND_TOP, 0, 0, 0, 0, flags);
2056         if (focusable && autoRequestFocus)
2057         {
2058             ::SetForegroundWindow(w->GetHWnd());
2059         }
2060     }
2061 ret:
2062     env->DeleteGlobalRef(self);
2063 }
2064 
2065 void AwtWindow::_ToBack(void *param)
2066 {
2067     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2068 
2069     jobject self = (jobject)param;
2070 
2071     AwtWindow *w = NULL;
2072 
2073     PDATA pData;
2074     JNI_CHECK_PEER_GOTO(self, ret);
2075     w = (AwtWindow *)pData;
2076     if (::IsWindow(w->GetHWnd()))
2077     {
2078         HWND hwnd = w->GetHWnd();
2079 //        if (AwtComponent::GetComponent(hwnd) == NULL) {
2080 //            // Window was disposed. Don't bother.
2081 //            return;
2082 //        }
2083 
2084         ::SetWindowPos(hwnd, HWND_BOTTOM, 0, 0 ,0, 0,
2085             SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
2086 
2087         // If hwnd is the foreground window or if *any* of its owners are, then
2088         // we have to reset the foreground window. The reason is that when we send
2089         // hwnd to back, all of its owners are sent to back as well. If any one of
2090         // them is the foreground window, then it's possible that we could end up
2091         // with a foreground window behind a window of another application.
2092         HWND foregroundWindow = ::GetForegroundWindow();
2093         BOOL adjustForegroundWindow;
2094         HWND toTest = hwnd;
2095         do
2096         {
2097             adjustForegroundWindow = (toTest == foregroundWindow);
2098             if (adjustForegroundWindow)
2099             {
2100                 break;
2101             }
2102             toTest = ::GetWindow(toTest, GW_OWNER);
2103         }
2104         while (toTest != NULL);
2105 
2106         if (adjustForegroundWindow)
2107         {
2108             HWND foregroundSearch = hwnd, newForegroundWindow = NULL;
2109                 while (1)
2110                 {
2111                 foregroundSearch = ::GetNextWindow(foregroundSearch, GW_HWNDPREV);
2112                 if (foregroundSearch == NULL)
2113                 {
2114                     break;
2115                 }
2116                 LONG style = static_cast<LONG>(::GetWindowLongPtr(foregroundSearch, GWL_STYLE));
2117                 if ((style & WS_CHILD) || !(style & WS_VISIBLE))
2118                 {
2119                     continue;
2120                 }
2121 
2122                 AwtComponent *c = AwtComponent::GetComponent(foregroundSearch);
2123                 if ((c != NULL) && !::IsWindow(AwtWindow::GetModalBlocker(c->GetHWnd())))
2124                 {
2125                     newForegroundWindow = foregroundSearch;
2126                 }
2127             }
2128             if (newForegroundWindow != NULL)
2129             {
2130                 ::SetWindowPos(newForegroundWindow, HWND_TOP, 0, 0, 0, 0,
2131                     SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
2132                 if (((AwtWindow*)AwtComponent::GetComponent(newForegroundWindow))->IsFocusableWindow())
2133                 {
2134                     ::SetForegroundWindow(newForegroundWindow);
2135                 }
2136             }
2137             else
2138             {
2139                 // We *have* to set the active HWND to something new. We simply
2140                 // cannot risk having an active Java HWND which is behind an HWND
2141                 // of a native application. This really violates the Windows user
2142                 // experience.
2143                 //
2144                 // Windows won't allow us to set the foreground window to NULL,
2145                 // so we use the desktop window instead. To the user, it appears
2146                 // that there is no foreground window system-wide.
2147                 ::SetForegroundWindow(::GetDesktopWindow());
2148             }
2149         }
2150     }
2151 ret:
2152     env->DeleteGlobalRef(self);
2153 }
2154 
2155 void AwtWindow::_SetAlwaysOnTop(void *param)
2156 {
2157     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2158 
2159     SetAlwaysOnTopStruct *sas = (SetAlwaysOnTopStruct *)param;
2160     jobject self = sas->window;
2161     jboolean value = sas->value;
2162 
2163     AwtWindow *w = NULL;
2164 
2165     PDATA pData;
2166     JNI_CHECK_PEER_GOTO(self, ret);
2167     w = (AwtWindow *)pData;
2168     if (::IsWindow(w->GetHWnd()))
2169     {
2170         w->SendMessage(WM_AWT_SETALWAYSONTOP, (WPARAM)value, (LPARAM)w);
2171     }
2172 ret:
2173     env->DeleteGlobalRef(self);
2174 
2175     delete sas;
2176 }
2177 
2178 void AwtWindow::_SetTitle(void *param)
2179 {
2180     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2181 
2182     SetTitleStruct *sts = (SetTitleStruct *)param;
2183     jobject self = sts->window;
2184     jstring title = sts->title;
2185 
2186     AwtWindow *w = NULL;
2187 
2188     PDATA pData;
2189     JNI_CHECK_PEER_GOTO(self, ret);
2190     JNI_CHECK_NULL_GOTO(title, "null title", ret);
2191 
2192     w = (AwtWindow *)pData;
2193     if (::IsWindow(w->GetHWnd()))
2194     {
2195         int length = env->GetStringLength(title);
2196         WCHAR *buffer = new WCHAR[length + 1];
2197         env->GetStringRegion(title, 0, length, buffer);
2198         buffer[length] = L'\0';
2199         VERIFY(::SetWindowTextW(w->GetHWnd(), buffer));
2200         delete[] buffer;
2201     }
2202 ret:
2203     env->DeleteGlobalRef(self);
2204     if (title != NULL) {
2205         env->DeleteGlobalRef(title);
2206     }
2207 
2208     delete sts;
2209 }
2210 
2211 void AwtWindow::_SetResizable(void *param)
2212 {
2213     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2214 
2215     SetResizableStruct *srs = (SetResizableStruct *)param;
2216     jobject self = srs->window;
2217     jboolean resizable = srs->resizable;
2218 
2219     AwtWindow *w = NULL;
2220 
2221     PDATA pData;
2222     JNI_CHECK_PEER_GOTO(self, ret);
2223     w = (AwtWindow *)pData;
2224     if (::IsWindow(w->GetHWnd()))
2225     {
2226         DASSERT(!IsBadReadPtr(w, sizeof(AwtWindow)));
2227         w->SetResizable(resizable != 0);
2228     }
2229 ret:
2230     env->DeleteGlobalRef(self);
2231 
2232     delete srs;
2233 }
2234 
2235 void AwtWindow::_UpdateInsets(void *param)
2236 {
2237     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2238 
2239     UpdateInsetsStruct *uis = (UpdateInsetsStruct *)param;
2240     jobject self = uis->window;
2241     jobject insets = uis->insets;
2242 
2243     AwtWindow *w = NULL;
2244 
2245     PDATA pData;
2246     JNI_CHECK_PEER_GOTO(self, ret);
2247     JNI_CHECK_NULL_GOTO(insets, "null insets", ret);
2248     w = (AwtWindow *)pData;
2249     if (::IsWindow(w->GetHWnd()))
2250     {
2251         w->UpdateInsets(insets);
2252     }
2253 ret:
2254     env->DeleteGlobalRef(self);
2255     env->DeleteGlobalRef(insets);
2256 
2257     delete uis;
2258 }
2259 
2260 void AwtWindow::_ReshapeFrame(void *param)
2261 {
2262     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2263 
2264     ReshapeFrameStruct *rfs = (ReshapeFrameStruct *)param;
2265     jobject self = rfs->frame;
2266     jint x = rfs->x;
2267     jint y = rfs->y;
2268     jint w = rfs->w;
2269     jint h = rfs->h;
2270 
2271     if (env->EnsureLocalCapacity(1) < 0)
2272     {
2273         env->DeleteGlobalRef(self);
2274         delete rfs;
2275         return;
2276     }
2277 
2278     AwtFrame *p = NULL;
2279 
2280     PDATA pData;
2281     JNI_CHECK_PEER_GOTO(self, ret);
2282     p = (AwtFrame *)pData;
2283     if (::IsWindow(p->GetHWnd()))
2284     {
2285         jobject target = env->GetObjectField(self, AwtObject::targetID);
2286         if (target != NULL)
2287         {
2288             // enforce tresholds before sending the event
2289             // Fix for 4459064 : do not enforce thresholds for embedded frames
2290             if (!p->IsEmbeddedFrame())
2291             {
2292                 jobject peer = p->GetPeer(env);
2293                 int minWidth = ::GetSystemMetrics(SM_CXMIN);
2294                 int minHeight = ::GetSystemMetrics(SM_CYMIN);
2295                 if (w < minWidth)
2296                 {
2297                     env->SetIntField(target, AwtComponent::widthID,
2298                         w = minWidth);
2299                     env->SetIntField(peer, AwtWindow::sysWID,
2300                         w);
2301                 }
2302                 if (h < minHeight)
2303                 {
2304                     env->SetIntField(target, AwtComponent::heightID,
2305                         h = minHeight);
2306                     env->SetIntField(peer, AwtWindow::sysHID,
2307                         h);
2308                 }
2309             }
2310             env->DeleteLocalRef(target);
2311 
2312             RECT *r = new RECT;
2313             ::SetRect(r, x, y, x + w, y + h);
2314             p->SendMessage(WM_AWT_RESHAPE_COMPONENT, 0, (LPARAM)r);
2315             // r is deleted in message handler
2316 
2317             // After the input method window shown, the dimension & position may not
2318             // be valid until this method is called. So we need to adjust the
2319             // IME candidate window position for the same reason as commented on
2320             // awt_Frame.cpp Show() method.
2321             if (p->isInputMethodWindow() && ::IsWindowVisible(p->GetHWnd())) {
2322               p->AdjustCandidateWindowPos();
2323             }
2324         }
2325         else
2326         {
2327             JNU_ThrowNullPointerException(env, "null target");
2328         }
2329     }
2330 ret:
2331    env->DeleteGlobalRef(self);
2332 
2333    delete rfs;
2334 }
2335 
2336 /*
2337  * This is AwtWindow-specific function that is not intended for reusing
2338  */
2339 HICON CreateIconFromRaster(JNIEnv* env, jintArray iconRaster, jint w, jint h)
2340 {
2341     HBITMAP mask = NULL;
2342     HBITMAP image = NULL;
2343     HICON icon = NULL;
2344     if (iconRaster != NULL) {
2345         int* iconRasterBuffer = NULL;
2346         try {
2347             iconRasterBuffer = (int *)env->GetPrimitiveArrayCritical(iconRaster, 0);
2348 
2349             JNI_CHECK_NULL_GOTO(iconRasterBuffer, "iconRaster data", done);
2350 
2351             mask = BitmapUtil::CreateTransparencyMaskFromARGB(w, h, iconRasterBuffer);
2352             image = BitmapUtil::CreateV4BitmapFromARGB(w, h, iconRasterBuffer);
2353         } catch (...) {
2354             if (iconRasterBuffer != NULL) {
2355                 env->ReleasePrimitiveArrayCritical(iconRaster, iconRasterBuffer, 0);
2356             }
2357             throw;
2358         }
2359         if (iconRasterBuffer != NULL) {
2360             env->ReleasePrimitiveArrayCritical(iconRaster, iconRasterBuffer, 0);
2361         }
2362     }
2363     if (mask && image) {
2364         ICONINFO icnInfo;
2365         memset(&icnInfo, 0, sizeof(ICONINFO));
2366         icnInfo.hbmMask = mask;
2367         icnInfo.hbmColor = image;
2368         icnInfo.fIcon = TRUE;
2369         icon = ::CreateIconIndirect(&icnInfo);
2370     }
2371     if (image) {
2372         destroy_BMP(image);
2373     }
2374     if (mask) {
2375         destroy_BMP(mask);
2376     }
2377 done:
2378     return icon;
2379 }
2380 
2381 void AwtWindow::SetIconData(JNIEnv* env, jintArray iconRaster, jint w, jint h,
2382                              jintArray smallIconRaster, jint smw, jint smh)
2383 {
2384     HICON hOldIcon = NULL;
2385     HICON hOldIconSm = NULL;
2386     //Destroy previous icon if it isn't inherited
2387     if ((m_hIcon != NULL) && !m_iconInherited) {
2388         hOldIcon = m_hIcon;
2389     }
2390     m_hIcon = NULL;
2391     if ((m_hIconSm != NULL) && !m_iconInherited) {
2392         hOldIconSm = m_hIconSm;
2393     }
2394     m_hIconSm = NULL;
2395     m_hIcon = CreateIconFromRaster(env, iconRaster, w, h);
2396     m_hIconSm = CreateIconFromRaster(env, smallIconRaster, smw, smh);
2397 
2398     m_iconInherited = (m_hIcon == NULL);
2399     if (m_iconInherited) {
2400         HWND hOwner = ::GetWindow(GetHWnd(), GW_OWNER);
2401         AwtWindow* owner = (AwtWindow *)AwtComponent::GetComponent(hOwner);
2402         if (owner != NULL) {
2403             m_hIcon = owner->GetHIcon();
2404             m_hIconSm = owner->GetHIconSm();
2405         } else {
2406             m_iconInherited = FALSE;
2407         }
2408     }
2409     DoUpdateIcon();
2410     EnumThreadWindows(AwtToolkit::MainThread(), UpdateOwnedIconCallback, (LPARAM)this);
2411     if (hOldIcon != NULL) {
2412         DestroyIcon(hOldIcon);
2413     }
2414     if (hOldIconSm != NULL) {
2415         DestroyIcon(hOldIconSm);
2416     }
2417 }
2418 
2419 BOOL AwtWindow::UpdateOwnedIconCallback(HWND hWndOwned, LPARAM lParam)
2420 {
2421     HWND hWndOwner = ::GetWindow(hWndOwned, GW_OWNER);
2422     AwtWindow* owner = (AwtWindow*)lParam;
2423     if (hWndOwner == owner->GetHWnd()) {
2424         AwtComponent* comp = AwtComponent::GetComponent(hWndOwned);
2425         if (comp != NULL && comp->IsTopLevel()) {
2426             AwtWindow* owned = (AwtWindow *)comp;
2427             if (owned->m_iconInherited) {
2428                 owned->m_hIcon = owner->m_hIcon;
2429                 owned->m_hIconSm = owner->m_hIconSm;
2430                 owned->DoUpdateIcon();
2431                 EnumThreadWindows(AwtToolkit::MainThread(), UpdateOwnedIconCallback, (LPARAM)owned);
2432             }
2433         }
2434     }
2435     return TRUE;
2436 }
2437 
2438 void AwtWindow::DoUpdateIcon()
2439 {
2440     //Does nothing for windows, is overriden for frames and dialogs
2441 }
2442 
2443 void AwtWindow::RedrawWindow()
2444 {
2445     if (isOpaque()) {
2446         ::RedrawWindow(GetHWnd(), NULL, NULL,
2447                 RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);
2448     } else {
2449         ::EnterCriticalSection(&contentBitmapCS);
2450         if (hContentBitmap != NULL) {
2451             UpdateWindowImpl(contentWidth, contentHeight, hContentBitmap);
2452         }
2453         ::LeaveCriticalSection(&contentBitmapCS);
2454     }
2455 }
2456 
2457 void AwtWindow::SetTranslucency(BYTE opacity, BOOL opaque)
2458 {
2459     BYTE old_opacity = getOpacity();
2460     BOOL old_opaque = isOpaque();
2461 
2462     if (opacity == old_opacity && opaque == old_opaque) {
2463         return;
2464     }
2465 
2466     setOpacity(opacity);
2467     setOpaque(opaque);
2468 
2469     HWND hwnd = GetHWnd();
2470 
2471     LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
2472 
2473     if (opaque != old_opaque) {
2474         ::EnterCriticalSection(&contentBitmapCS);
2475         if (hContentBitmap != NULL) {
2476             ::DeleteObject(hContentBitmap);
2477             hContentBitmap = NULL;
2478         }
2479         ::LeaveCriticalSection(&contentBitmapCS);
2480     }
2481 
2482     if (opaque && opacity == 0xff) {
2483         // Turn off all the effects
2484         ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style & ~WS_EX_LAYERED);
2485         // Ask the window to repaint itself and all the children
2486         RedrawWindow();
2487     } else {
2488         // We're going to enable some effects
2489         if (!(ex_style & WS_EX_LAYERED)) {
2490             ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style | WS_EX_LAYERED);
2491         } else {
2492             if ((opaque && opacity < 0xff) ^ (old_opaque && old_opacity < 0xff)) {
2493                 // _One_ of the modes uses the SetLayeredWindowAttributes.
2494                 // Need to reset the style in this case.
2495                 // If both modes are simple (i.e. just changing the opacity level),
2496                 // no need to reset the style.
2497                 ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style & ~WS_EX_LAYERED);
2498                 ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style | WS_EX_LAYERED);
2499             }
2500         }
2501 
2502         if (opaque) {
2503             // Simple opacity mode
2504             ::SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), opacity, LWA_ALPHA);
2505         }
2506     }
2507 }
2508 
2509 static HBITMAP CreateBitmapFromRaster(JNIEnv* env, jintArray raster, jint w, jint h)
2510 {
2511     HBITMAP image = NULL;
2512     if (raster != NULL) {
2513         int* rasterBuffer = NULL;
2514         try {
2515             rasterBuffer = (int *)env->GetPrimitiveArrayCritical(raster, 0);
2516             JNI_CHECK_NULL_GOTO(rasterBuffer, "raster data", done);
2517             image = BitmapUtil::CreateBitmapFromARGBPre(w, h, w*4, rasterBuffer);
2518         } catch (...) {
2519             if (rasterBuffer != NULL) {
2520                 env->ReleasePrimitiveArrayCritical(raster, rasterBuffer, 0);
2521             }
2522             throw;
2523         }
2524         if (rasterBuffer != NULL) {
2525             env->ReleasePrimitiveArrayCritical(raster, rasterBuffer, 0);
2526         }
2527     }
2528 done:
2529     return image;
2530 }
2531 
2532 void AwtWindow::UpdateWindowImpl(int width, int height, HBITMAP hBitmap)
2533 {
2534     if (isOpaque()) {
2535         return;
2536     }
2537 
2538     HWND hWnd = GetHWnd();
2539     HDC hdcDst = ::GetDC(NULL);
2540     HDC hdcSrc = ::CreateCompatibleDC(NULL);
2541     HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hdcSrc, hBitmap);
2542 
2543     //XXX: this code doesn't paint the children (say, the java.awt.Button)!
2544     //So, if we ever want to support HWs here, we need to repaint them
2545     //in some other way...
2546     //::SendMessage(hWnd, WM_PRINT, (WPARAM)hdcSrc, /*PRF_CHECKVISIBLE |*/
2547     //      PRF_CHILDREN /*| PRF_CLIENT | PRF_NONCLIENT*/);
2548 
2549     POINT ptSrc;
2550     ptSrc.x = ptSrc.y = 0;
2551 
2552     RECT rect;
2553     POINT ptDst;
2554     SIZE size;
2555 
2556     ::GetWindowRect(hWnd, &rect);
2557     ptDst.x = rect.left;
2558     ptDst.y = rect.top;
2559     size.cx = width;
2560     size.cy = height;
2561 
2562     BLENDFUNCTION bf;
2563 
2564     bf.SourceConstantAlpha = getOpacity();
2565     bf.AlphaFormat = AC_SRC_ALPHA;
2566     bf.BlendOp = AC_SRC_OVER;
2567     bf.BlendFlags = 0;
2568 
2569     ::UpdateLayeredWindow(hWnd, hdcDst, &ptDst, &size, hdcSrc, &ptSrc,
2570             RGB(0, 0, 0), &bf, ULW_ALPHA);
2571 
2572     ::ReleaseDC(NULL, hdcDst);
2573     ::SelectObject(hdcSrc, hOldBitmap);
2574     ::DeleteDC(hdcSrc);
2575 }
2576 
2577 void AwtWindow::UpdateWindow(JNIEnv* env, jintArray data, int width, int height,
2578                              HBITMAP hNewBitmap)
2579 {
2580     if (isOpaque()) {
2581         return;
2582     }
2583 
2584     HBITMAP hBitmap;
2585     if (hNewBitmap == NULL) {
2586         if (data == NULL) {
2587             return;
2588         }
2589         hBitmap = CreateBitmapFromRaster(env, data, width, height);
2590         if (hBitmap == NULL) {
2591             return;
2592         }
2593     } else {
2594         hBitmap = hNewBitmap;
2595     }
2596 
2597     ::EnterCriticalSection(&contentBitmapCS);
2598     if (hContentBitmap != NULL) {
2599         ::DeleteObject(hContentBitmap);
2600     }
2601     hContentBitmap = hBitmap;
2602     contentWidth = width;
2603     contentHeight = height;
2604     UpdateWindowImpl(width, height, hBitmap);
2605     ::LeaveCriticalSection(&contentBitmapCS);
2606 }
2607 
2608 /*
2609  * Fixed 6353381: it's improved fix for 4792958
2610  * which was backed-out to avoid 5059656
2611  */
2612 BOOL AwtWindow::HasValidRect()
2613 {
2614     RECT inside;
2615     RECT outside;
2616 
2617     if (::IsIconic(GetHWnd())) {
2618         return FALSE;
2619     }
2620 
2621     ::GetClientRect(GetHWnd(), &inside);
2622     ::GetWindowRect(GetHWnd(), &outside);
2623 
2624     BOOL isZeroClientArea = (inside.right == 0 && inside.bottom == 0);
2625     BOOL isInvalidLocation = ((outside.left == -32000 && outside.top == -32000) || // Win2k && WinXP
2626                               (outside.left == 32000 && outside.top == 32000) || // Win95 && Win98
2627                               (outside.left == 3000 && outside.top == 3000)); // Win95 && Win98
2628 
2629     // the bounds correspond to iconic state
2630     if (isZeroClientArea && isInvalidLocation)
2631     {
2632         return FALSE;
2633     }
2634 
2635     return TRUE;
2636 }
2637 
2638 
2639 void AwtWindow::_SetIconImagesData(void * param)
2640 {
2641     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2642 
2643     SetIconImagesDataStruct* s = (SetIconImagesDataStruct*)param;
2644     jobject self = s->window;
2645 
2646     jintArray iconRaster = s->iconRaster;
2647     jintArray smallIconRaster = s->smallIconRaster;
2648 
2649     AwtWindow *window = NULL;
2650 
2651     PDATA pData;
2652     JNI_CHECK_PEER_GOTO(self, ret);
2653     // ok to pass null raster: default AWT icon
2654 
2655     window = (AwtWindow*)pData;
2656     if (::IsWindow(window->GetHWnd()))
2657     {
2658         window->SetIconData(env, iconRaster, s->w, s->h, smallIconRaster, s->smw, s->smh);
2659 
2660     }
2661 
2662 ret:
2663     env->DeleteGlobalRef(self);
2664     env->DeleteGlobalRef(iconRaster);
2665     env->DeleteGlobalRef(smallIconRaster);
2666     delete s;
2667 }
2668 
2669 void AwtWindow::_SetMinSize(void* param)
2670 {
2671     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2672 
2673     SizeStruct *ss = (SizeStruct *)param;
2674     jobject self = ss->window;
2675     jint w = ss->w;
2676     jint h = ss->h;
2677     //Perform size setting
2678     AwtWindow *window = NULL;
2679 
2680     PDATA pData;
2681     JNI_CHECK_PEER_GOTO(self, ret);
2682     window = (AwtWindow *)pData;
2683     window->m_minSize.x = w;
2684     window->m_minSize.y = h;
2685   ret:
2686     env->DeleteGlobalRef(self);
2687     delete ss;
2688 }
2689 
2690 jint AwtWindow::_GetScreenImOn(void *param)
2691 {
2692     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2693 
2694     jobject self = (jobject)param;
2695 
2696     // It's entirely possible that our native resources have been destroyed
2697     // before our java peer - if we're dispose()d, for instance.
2698     // Alert caller w/ IllegalComponentStateException.
2699     if (self == NULL) {
2700         JNU_ThrowByName(env, "java/awt/IllegalComponentStateException",
2701                         "Peer null in JNI");
2702         return 0;
2703     }
2704     PDATA pData = JNI_GET_PDATA(self);
2705     if (pData == NULL) {
2706         JNU_ThrowByName(env, "java/awt/IllegalComponentStateException",
2707                         "Native resources unavailable");
2708         env->DeleteGlobalRef(self);
2709         return 0;
2710     }
2711 
2712     jint result = 0;
2713     AwtWindow *w = (AwtWindow *)pData;
2714     if (::IsWindow(w->GetHWnd()))
2715     {
2716         result = (jint)w->GetScreenImOn();
2717     }
2718 
2719     env->DeleteGlobalRef(self);
2720 
2721     return result;
2722 }
2723 
2724 void AwtWindow::_SetFocusableWindow(void *param)
2725 {
2726     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2727 
2728     SetFocusableWindowStruct *sfws = (SetFocusableWindowStruct *)param;
2729     jobject self = sfws->window;
2730     jboolean isFocusableWindow = sfws->isFocusableWindow;
2731 
2732     AwtWindow *window = NULL;
2733 
2734     PDATA pData;
2735     JNI_CHECK_PEER_GOTO(self, ret);
2736     window = (AwtWindow *)pData;
2737 
2738     window->m_isFocusableWindow = isFocusableWindow;
2739 
2740     if (IS_WIN2000) {
2741         if (!window->m_isFocusableWindow) {
2742             LONG isPopup = window->GetStyle() & WS_POPUP;
2743             window->SetStyleEx(window->GetStyleEx() | (isPopup ? 0 : WS_EX_APPWINDOW) | AWT_WS_EX_NOACTIVATE);
2744         } else {
2745             window->SetStyleEx(window->GetStyleEx() & ~WS_EX_APPWINDOW & ~AWT_WS_EX_NOACTIVATE);
2746         }
2747     }
2748 
2749   ret:
2750     env->DeleteGlobalRef(self);
2751     delete sfws;
2752 }
2753 
2754 void AwtWindow::_ModalDisable(void *param)
2755 {
2756     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2757 
2758     ModalDisableStruct *mds = (ModalDisableStruct *)param;
2759     jobject self = mds->window;
2760     HWND blockerHWnd = (HWND)mds->blockerHWnd;
2761 
2762     AwtWindow *window = NULL;
2763     HWND windowHWnd = 0;
2764 
2765     JNI_CHECK_NULL_GOTO(self, "peer", ret);
2766     PDATA pData = JNI_GET_PDATA(self);
2767     if (pData == NULL) {
2768         env->DeleteGlobalRef(self);
2769         delete mds;
2770         return;
2771     }
2772 
2773     window = (AwtWindow *)pData;
2774     windowHWnd = window->GetHWnd();
2775     if (::IsWindow(windowHWnd)) {
2776         AwtWindow::SetAndActivateModalBlocker(windowHWnd, blockerHWnd);
2777     }
2778 
2779 ret:
2780     env->DeleteGlobalRef(self);
2781 
2782     delete mds;
2783 }
2784 
2785 void AwtWindow::_ModalEnable(void *param)
2786 {
2787     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2788 
2789     jobject self = (jobject)param;
2790 
2791     AwtWindow *window = NULL;
2792     HWND windowHWnd = 0;
2793 
2794     JNI_CHECK_NULL_GOTO(self, "peer", ret);
2795     PDATA pData = JNI_GET_PDATA(self);
2796     if (pData == NULL) {
2797         env->DeleteGlobalRef(self);
2798         return;
2799     }
2800 
2801     window = (AwtWindow *)pData;
2802     windowHWnd = window->GetHWnd();
2803     if (::IsWindow(windowHWnd)) {
2804         AwtWindow::SetModalBlocker(windowHWnd, NULL);
2805     }
2806 
2807   ret:
2808     env->DeleteGlobalRef(self);
2809 }
2810 
2811 void AwtWindow::_SetOpacity(void* param)
2812 {
2813     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2814 
2815     OpacityStruct *os = (OpacityStruct *)param;
2816     jobject self = os->window;
2817     BYTE iOpacity = (BYTE)os->iOpacity;
2818 
2819     PDATA pData;
2820     JNI_CHECK_PEER_GOTO(self, ret);
2821     AwtWindow *window = (AwtWindow *)pData;
2822 
2823     window->SetTranslucency(iOpacity, window->isOpaque());
2824 
2825   ret:
2826     env->DeleteGlobalRef(self);
2827     delete os;
2828 }
2829 
2830 void AwtWindow::_SetOpaque(void* param)
2831 {
2832     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2833 
2834     OpaqueStruct *os = (OpaqueStruct *)param;
2835     jobject self = os->window;
2836     BOOL isOpaque = (BOOL)os->isOpaque;
2837 
2838     PDATA pData;
2839     JNI_CHECK_PEER_GOTO(self, ret);
2840     AwtWindow *window = (AwtWindow *)pData;
2841 
2842     window->SetTranslucency(window->getOpacity(), isOpaque);
2843 
2844   ret:
2845     env->DeleteGlobalRef(self);
2846     delete os;
2847 }
2848 
2849 void AwtWindow::_UpdateWindow(void* param)
2850 {
2851     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2852 
2853     UpdateWindowStruct *uws = (UpdateWindowStruct *)param;
2854     jobject self = uws->window;
2855     jintArray data = uws->data;
2856 
2857     PDATA pData;
2858     JNI_CHECK_PEER_GOTO(self, ret);
2859     AwtWindow *window = (AwtWindow *)pData;
2860 
2861     window->UpdateWindow(env, data, (int)uws->width, (int)uws->height,
2862                          uws->hBitmap);
2863 
2864   ret:
2865     env->DeleteGlobalRef(self);
2866     if (data != NULL) {
2867         env->DeleteGlobalRef(data);
2868     }
2869     delete uws;
2870 }
2871 
2872 
2873 extern "C" {
2874 
2875 /*
2876  * Class:     java_awt_Window
2877  * Method:    initIDs
2878  * Signature: ()V
2879  */
2880 JNIEXPORT void JNICALL
2881 Java_java_awt_Window_initIDs(JNIEnv *env, jclass cls)
2882 {
2883     TRY;
2884 
2885     AwtWindow::warningStringID =
2886         env->GetFieldID(cls, "warningString", "Ljava/lang/String;");
2887     AwtWindow::locationByPlatformID =
2888         env->GetFieldID(cls, "locationByPlatform", "Z");
2889     AwtWindow::securityWarningWidthID =
2890         env->GetFieldID(cls, "securityWarningWidth", "I");
2891     AwtWindow::securityWarningHeightID =
2892         env->GetFieldID(cls, "securityWarningHeight", "I");
2893     AwtWindow::getWarningStringMID =
2894         env->GetMethodID(cls, "getWarningString", "()Ljava/lang/String;");
2895     AwtWindow::autoRequestFocusID =
2896         env->GetFieldID(cls, "autoRequestFocus", "Z");
2897     AwtWindow::calculateSecurityWarningPositionMID =
2898         env->GetMethodID(cls, "calculateSecurityWarningPosition", "(DDDD)Ljava/awt/geom/Point2D;");
2899 
2900     CATCH_BAD_ALLOC;
2901 }
2902 
2903 } /* extern "C" */
2904 
2905 
2906 /************************************************************************
2907  * WindowPeer native methods
2908  */
2909 
2910 extern "C" {
2911 
2912 /*
2913  * Class:     sun_awt_windows_WWindowPeer
2914  * Method:    initIDs
2915  * Signature: ()V
2916  */
2917 JNIEXPORT void JNICALL
2918 Java_sun_awt_windows_WWindowPeer_initIDs(JNIEnv *env, jclass cls)
2919 {
2920     TRY;
2921 
2922     AwtWindow::sysXID = env->GetFieldID(cls, "sysX", "I");
2923     AwtWindow::sysYID = env->GetFieldID(cls, "sysY", "I");
2924     AwtWindow::sysWID = env->GetFieldID(cls, "sysW", "I");
2925     AwtWindow::sysHID = env->GetFieldID(cls, "sysH", "I");
2926 
2927     CATCH_BAD_ALLOC;
2928 }
2929 
2930 /*
2931  * Class:     sun_awt_windows_WWindowPeer
2932  * Method:    toFront
2933  * Signature: ()V
2934  */
2935 JNIEXPORT void JNICALL
2936 Java_sun_awt_windows_WWindowPeer__1toFront(JNIEnv *env, jobject self)
2937 {
2938     TRY;
2939 
2940     AwtToolkit::GetInstance().SyncCall(AwtWindow::_ToFront,
2941         env->NewGlobalRef(self));
2942     // global ref is deleted in _ToFront()
2943 
2944     CATCH_BAD_ALLOC;
2945 }
2946 
2947 /*
2948  * Class:     sun_awt_windows_WWindowPeer
2949  * Method:    toBack
2950  * Signature: ()V
2951  */
2952 JNIEXPORT void JNICALL
2953 Java_sun_awt_windows_WWindowPeer_toBack(JNIEnv *env, jobject self)
2954 {
2955     TRY;
2956 
2957     AwtToolkit::GetInstance().SyncCall(AwtWindow::_ToBack,
2958         env->NewGlobalRef(self));
2959     // global ref is deleted in _ToBack()
2960 
2961     CATCH_BAD_ALLOC;
2962 }
2963 
2964 /*
2965  * Class:     sun_awt_windows_WWindowPeer
2966  * Method:    setAlwaysOnTop
2967  * Signature: (Z)V
2968  */
2969 JNIEXPORT void JNICALL
2970 Java_sun_awt_windows_WWindowPeer_setAlwaysOnTopNative(JNIEnv *env, jobject self,
2971                                                 jboolean value)
2972 {
2973     TRY;
2974 
2975     SetAlwaysOnTopStruct *sas = new SetAlwaysOnTopStruct;
2976     sas->window = env->NewGlobalRef(self);
2977     sas->value = value;
2978 
2979     AwtToolkit::GetInstance().SyncCall(AwtWindow::_SetAlwaysOnTop, sas);
2980     // global ref and sas are deleted in _SetAlwaysOnTop
2981 
2982     CATCH_BAD_ALLOC;
2983 }
2984 
2985 /*
2986  * Class:     sun_awt_windows_WWindowPeer
2987  * Method:    _setTitle
2988  * Signature: (Ljava/lang/String;)V
2989  */
2990 JNIEXPORT void JNICALL
2991 Java_sun_awt_windows_WWindowPeer__1setTitle(JNIEnv *env, jobject self,
2992                                             jstring title)
2993 {
2994     TRY;
2995 
2996     SetTitleStruct *sts = new SetTitleStruct;
2997     sts->window = env->NewGlobalRef(self);
2998     sts->title = (jstring)env->NewGlobalRef(title);
2999 
3000     AwtToolkit::GetInstance().SyncCall(AwtWindow::_SetTitle, sts);
3001     /// global refs and sts are deleted in _SetTitle()
3002 
3003     CATCH_BAD_ALLOC;
3004 }
3005 
3006 /*
3007  * Class:     sun_awt_windows_WWindowPeer
3008  * Method:    _setResizable
3009  * Signature: (Z)V
3010  */
3011 JNIEXPORT void JNICALL
3012 Java_sun_awt_windows_WWindowPeer__1setResizable(JNIEnv *env, jobject self,
3013                                                 jboolean resizable)
3014 {
3015     TRY;
3016 
3017     SetResizableStruct *srs = new SetResizableStruct;
3018     srs->window = env->NewGlobalRef(self);
3019     srs->resizable = resizable;
3020 
3021     AwtToolkit::GetInstance().SyncCall(AwtWindow::_SetResizable, srs);
3022     // global ref and srs are deleted in _SetResizable
3023 
3024     CATCH_BAD_ALLOC;
3025 }
3026 
3027 /*
3028  * Class:     sun_awt_windows_WWindowPeer
3029  * Method:    create
3030  * Signature: (Lsun/awt/windows/WComponentPeer;)V
3031  */
3032 JNIEXPORT void JNICALL
3033 Java_sun_awt_windows_WWindowPeer_createAwtWindow(JNIEnv *env, jobject self,
3034                                                  jobject parent)
3035 {
3036     TRY;
3037 
3038     PDATA pData;
3039 //    JNI_CHECK_PEER_RETURN(parent);
3040     AwtToolkit::CreateComponent(self, parent,
3041                                 (AwtToolkit::ComponentFactory)
3042                                 AwtWindow::Create);
3043     JNI_CHECK_PEER_CREATION_RETURN(self);
3044 
3045     CATCH_BAD_ALLOC;
3046 }
3047 
3048 /*
3049  * Class:     sun_awt_windows_WWindowPeer
3050  * Method:    updateInsets
3051  * Signature: (Ljava/awt/Insets;)V
3052  */
3053 JNIEXPORT void JNICALL
3054 Java_sun_awt_windows_WWindowPeer_updateInsets(JNIEnv *env, jobject self,
3055                                               jobject insets)
3056 {
3057     TRY;
3058 
3059     UpdateInsetsStruct *uis = new UpdateInsetsStruct;
3060     uis->window = env->NewGlobalRef(self);
3061     uis->insets = env->NewGlobalRef(insets);
3062 
3063     AwtToolkit::GetInstance().SyncCall(AwtWindow::_UpdateInsets, uis);
3064     // global refs and uis are deleted in _UpdateInsets()
3065 
3066     CATCH_BAD_ALLOC;
3067 }
3068 
3069 /*
3070  * Class:     sun_awt_windows_WWindowPeer
3071  * Method:    reshapeFrame
3072  * Signature: (IIII)V
3073  */
3074 JNIEXPORT void JNICALL
3075 Java_sun_awt_windows_WWindowPeer_reshapeFrame(JNIEnv *env, jobject self,
3076                                         jint x, jint y, jint w, jint h)
3077 {
3078     TRY;
3079 
3080     ReshapeFrameStruct *rfs = new ReshapeFrameStruct;
3081     rfs->frame = env->NewGlobalRef(self);
3082     rfs->x = x;
3083     rfs->y = y;
3084     rfs->w = w;
3085     rfs->h = h;
3086 
3087     AwtToolkit::GetInstance().SyncCall(AwtWindow::_ReshapeFrame, rfs);
3088     // global ref and rfs are deleted in _ReshapeFrame()
3089 
3090     CATCH_BAD_ALLOC;
3091 }
3092 
3093 /*
3094  * Class:     sun_awt_windows_WWindowPeer
3095  * Method:    getSysMinWidth
3096  * Signature: ()I
3097  */
3098 JNIEXPORT jint JNICALL
3099 Java_sun_awt_windows_WWindowPeer_getSysMinWidth(JNIEnv *env, jclass self)
3100 {
3101     TRY;
3102 
3103     return ::GetSystemMetrics(SM_CXMIN);
3104 
3105     CATCH_BAD_ALLOC_RET(0);
3106 }
3107 
3108 /*
3109  * Class:     sun_awt_windows_WWindowPeer
3110  * Method:    getSysMinHeight
3111  * Signature: ()I
3112  */
3113 JNIEXPORT jint JNICALL
3114 Java_sun_awt_windows_WWindowPeer_getSysMinHeight(JNIEnv *env, jclass self)
3115 {
3116     TRY;
3117 
3118     return ::GetSystemMetrics(SM_CYMIN);
3119 
3120     CATCH_BAD_ALLOC_RET(0);
3121 }
3122 
3123 /*
3124  * Class:     sun_awt_windows_WWindowPeer
3125  * Method:    getSysIconHeight
3126  * Signature: ()I
3127  */
3128 JNIEXPORT jint JNICALL
3129 Java_sun_awt_windows_WWindowPeer_getSysIconHeight(JNIEnv *env, jclass self)
3130 {
3131     TRY;
3132 
3133     return ::GetSystemMetrics(SM_CYICON);
3134 
3135     CATCH_BAD_ALLOC_RET(0);
3136 }
3137 
3138 /*
3139  * Class:     sun_awt_windows_WWindowPeer
3140  * Method:    getSysIconWidth
3141  * Signature: ()I
3142  */
3143 JNIEXPORT jint JNICALL
3144 Java_sun_awt_windows_WWindowPeer_getSysIconWidth(JNIEnv *env, jclass self)
3145 {
3146     TRY;
3147 
3148     return ::GetSystemMetrics(SM_CXICON);
3149 
3150     CATCH_BAD_ALLOC_RET(0);
3151 }
3152 
3153 /*
3154  * Class:     sun_awt_windows_WWindowPeer
3155  * Method:    getSysSmIconHeight
3156  * Signature: ()I
3157  */
3158 JNIEXPORT jint JNICALL
3159 Java_sun_awt_windows_WWindowPeer_getSysSmIconHeight(JNIEnv *env, jclass self)
3160 {
3161     TRY;
3162 
3163     return ::GetSystemMetrics(SM_CYSMICON);
3164 
3165     CATCH_BAD_ALLOC_RET(0);
3166 }
3167 
3168 /*
3169  * Class:     sun_awt_windows_WWindowPeer
3170  * Method:    getSysSmIconWidth
3171  * Signature: ()I
3172  */
3173 JNIEXPORT jint JNICALL
3174 Java_sun_awt_windows_WWindowPeer_getSysSmIconWidth(JNIEnv *env, jclass self)
3175 {
3176     TRY;
3177 
3178     return ::GetSystemMetrics(SM_CXSMICON);
3179 
3180     CATCH_BAD_ALLOC_RET(0);
3181 }
3182 
3183 /*
3184  * Class:     sun_awt_windows_WWindowPeer
3185  * Method:    setIconImagesData
3186  * Signature: ([I)V
3187  */
3188 JNIEXPORT void JNICALL
3189 Java_sun_awt_windows_WWindowPeer_setIconImagesData(JNIEnv *env, jobject self,
3190     jintArray iconRaster, jint w, jint h,
3191     jintArray smallIconRaster, jint smw, jint smh)
3192 {
3193     TRY;
3194 
3195     SetIconImagesDataStruct *sims = new SetIconImagesDataStruct;
3196 
3197     sims->window = env->NewGlobalRef(self);
3198     sims->iconRaster = (jintArray)env->NewGlobalRef(iconRaster);
3199     sims->w = w;
3200     sims->h = h;
3201     sims->smallIconRaster = (jintArray)env->NewGlobalRef(smallIconRaster);
3202     sims->smw = smw;
3203     sims->smh = smh;
3204 
3205     AwtToolkit::GetInstance().SyncCall(AwtWindow::_SetIconImagesData, sims);
3206     // global refs and sims are deleted in _SetIconImagesData()
3207 
3208     CATCH_BAD_ALLOC;
3209 }
3210 
3211 /*
3212  * Class:     sun_awt_windows_WWindowPeer
3213  * Method:    setMinSize
3214  * Signature: (Lsun/awt/windows/WWindowPeer;)V
3215  */
3216 JNIEXPORT void JNICALL
3217 Java_sun_awt_windows_WWindowPeer_setMinSize(JNIEnv *env, jobject self,
3218                                               jint w, jint h)
3219 {
3220     TRY;
3221 
3222     SizeStruct *ss = new SizeStruct;
3223     ss->window = env->NewGlobalRef(self);
3224     ss->w = w;
3225     ss->h = h;
3226 
3227     AwtToolkit::GetInstance().SyncCall(AwtWindow::_SetMinSize, ss);
3228     // global refs and mds are deleted in _SetMinSize
3229 
3230     CATCH_BAD_ALLOC;
3231 }
3232 
3233 /*
3234  * Class:     sun_awt_windows_WWindowPeer
3235  * Method:    getScreenImOn
3236  * Signature: ()I
3237  */
3238 JNIEXPORT jint JNICALL
3239 Java_sun_awt_windows_WWindowPeer_getScreenImOn(JNIEnv *env, jobject self)
3240 {
3241     TRY;
3242 
3243     return static_cast<jint>(reinterpret_cast<INT_PTR>(AwtToolkit::GetInstance().SyncCall(
3244         (void *(*)(void *))AwtWindow::_GetScreenImOn,
3245         env->NewGlobalRef(self))));
3246     // global ref is deleted in _GetScreenImOn()
3247 
3248     CATCH_BAD_ALLOC_RET(-1);
3249 }
3250 
3251 /*
3252  * Class:     sun_awt_windows_WWindowPeer
3253  * Method:    modalDisable
3254  * Signature: (J)V
3255  */
3256 JNIEXPORT void JNICALL
3257 Java_sun_awt_windows_WWindowPeer_modalDisable(JNIEnv *env, jobject self,
3258                                               jobject blocker, jlong blockerHWnd)
3259 {
3260     TRY;
3261 
3262     ModalDisableStruct *mds = new ModalDisableStruct;
3263     mds->window = env->NewGlobalRef(self);
3264     mds->blockerHWnd = blockerHWnd;
3265 
3266     AwtToolkit::GetInstance().SyncCall(AwtWindow::_ModalDisable, mds);
3267     // global ref and mds are deleted in _ModalDisable
3268 
3269     CATCH_BAD_ALLOC;
3270 }
3271 
3272 /*
3273  * Class:     sun_awt_windows_WWindowPeer
3274  * Method:    modalEnable
3275  * Signature: ()V
3276  */
3277 JNIEXPORT void JNICALL
3278 Java_sun_awt_windows_WWindowPeer_modalEnable(JNIEnv *env, jobject self, jobject blocker)
3279 {
3280     TRY;
3281 
3282     AwtToolkit::GetInstance().SyncCall(AwtWindow::_ModalEnable,
3283         env->NewGlobalRef(self));
3284     // global ref is deleted in _ModalEnable
3285 
3286     CATCH_BAD_ALLOC;
3287 }
3288 
3289 /*
3290  * Class:     sun_awt_windows_WWindowPeer
3291  * Method:    setFocusableWindow
3292  * Signature: (Z)V
3293  */
3294 JNIEXPORT void JNICALL
3295 Java_sun_awt_windows_WWindowPeer_setFocusableWindow(JNIEnv *env, jobject self, jboolean isFocusableWindow)
3296 {
3297     TRY;
3298 
3299     SetFocusableWindowStruct *sfws = new SetFocusableWindowStruct;
3300     sfws->window = env->NewGlobalRef(self);
3301     sfws->isFocusableWindow = isFocusableWindow;
3302 
3303     AwtToolkit::GetInstance().SyncCall(AwtWindow::_SetFocusableWindow, sfws);
3304     // global ref and sfws are deleted in _SetFocusableWindow()
3305 
3306     CATCH_BAD_ALLOC;
3307 }
3308 
3309 JNIEXPORT void JNICALL
3310 Java_sun_awt_windows_WWindowPeer_nativeGrab(JNIEnv *env, jobject self)
3311 {
3312     TRY;
3313 
3314     AwtToolkit::GetInstance().SyncCall(AwtWindow::_Grab, env->NewGlobalRef(self));
3315     // global ref is deleted in _Grab()
3316 
3317     CATCH_BAD_ALLOC;
3318 }
3319 
3320 JNIEXPORT void JNICALL
3321 Java_sun_awt_windows_WWindowPeer_nativeUngrab(JNIEnv *env, jobject self)
3322 {
3323     TRY;
3324 
3325     AwtToolkit::GetInstance().SyncCall(AwtWindow::_Ungrab, env->NewGlobalRef(self));
3326     // global ref is deleted in _Ungrab()
3327 
3328     CATCH_BAD_ALLOC;
3329 }
3330 
3331 /*
3332  * Class:     sun_awt_windows_WWindowPeer
3333  * Method:    setOpacity
3334  * Signature: (I)V
3335  */
3336 JNIEXPORT void JNICALL
3337 Java_sun_awt_windows_WWindowPeer_setOpacity(JNIEnv *env, jobject self,
3338                                               jint iOpacity)
3339 {
3340     TRY;
3341 
3342     OpacityStruct *os = new OpacityStruct;
3343     os->window = env->NewGlobalRef(self);
3344     os->iOpacity = iOpacity;
3345 
3346     AwtToolkit::GetInstance().SyncCall(AwtWindow::_SetOpacity, os);
3347     // global refs and mds are deleted in _SetMinSize
3348 
3349     CATCH_BAD_ALLOC;
3350 }
3351 
3352 /*
3353  * Class:     sun_awt_windows_WWindowPeer
3354  * Method:    setOpaqueImpl
3355  * Signature: (Z)V
3356  */
3357 JNIEXPORT void JNICALL
3358 Java_sun_awt_windows_WWindowPeer_setOpaqueImpl(JNIEnv *env, jobject self,
3359                                               jboolean isOpaque)
3360 {
3361     TRY;
3362 
3363     OpaqueStruct *os = new OpaqueStruct;
3364     os->window = env->NewGlobalRef(self);
3365     os->isOpaque = isOpaque;
3366 
3367     AwtToolkit::GetInstance().SyncCall(AwtWindow::_SetOpaque, os);
3368     // global refs and mds are deleted in _SetMinSize
3369 
3370     CATCH_BAD_ALLOC;
3371 }
3372 
3373 /*
3374  * Class:     sun_awt_windows_WWindowPeer
3375  * Method:    updateWindowImpl
3376  * Signature: ([III)V
3377  */
3378 JNIEXPORT void JNICALL
3379 Java_sun_awt_windows_WWindowPeer_updateWindowImpl(JNIEnv *env, jobject self,
3380                                                   jintArray data,
3381                                                   jint width, jint height)
3382 {
3383     TRY;
3384 
3385     UpdateWindowStruct *uws = new UpdateWindowStruct;
3386     uws->window = env->NewGlobalRef(self);
3387     uws->data = (jintArray)env->NewGlobalRef(data);
3388     uws->hBitmap = NULL;
3389     uws->width = width;
3390     uws->height = height;
3391 
3392     AwtToolkit::GetInstance().InvokeFunction(AwtWindow::_UpdateWindow, uws);
3393     // global refs and mds are deleted in _UpdateWindow
3394 
3395     CATCH_BAD_ALLOC;
3396 }
3397 
3398 /**
3399  * This method is called from the WGL pipeline when it needs to update
3400  * the layered window WindowPeer's C++ level object.
3401  */
3402 void AwtWindow_UpdateWindow(JNIEnv *env, jobject peer,
3403                             jint width, jint height, HBITMAP hBitmap)
3404 {
3405     TRY;
3406 
3407     UpdateWindowStruct *uws = new UpdateWindowStruct;
3408     uws->window = env->NewGlobalRef(peer);
3409     uws->data = NULL;
3410     uws->hBitmap = hBitmap;
3411     uws->width = width;
3412     uws->height = height;
3413 
3414     AwtToolkit::GetInstance().InvokeFunction(AwtWindow::_UpdateWindow, uws);
3415     // global refs and mds are deleted in _UpdateWindow
3416 
3417     CATCH_BAD_ALLOC;
3418 }
3419 
3420 /*
3421  * Class:     sun_awt_windows_WWindowPeer
3422  * Method:    repositionSecurityWarning
3423  * Signature: ()V
3424  */
3425 JNIEXPORT void JNICALL
3426 Java_sun_awt_windows_WWindowPeer_repositionSecurityWarning(JNIEnv *env,
3427         jobject self)
3428 {
3429     TRY;
3430 
3431     RepositionSecurityWarningStruct *rsws =
3432         new RepositionSecurityWarningStruct;
3433     rsws->window = env->NewGlobalRef(self);
3434 
3435     AwtToolkit::GetInstance().InvokeFunction(
3436             AwtWindow::_RepositionSecurityWarning, rsws);
3437     // global refs and mds are deleted in _RepositionSecurityWarning
3438 
3439     CATCH_BAD_ALLOC;
3440 }
3441 
3442 } /* extern "C" */