1 /*
   2  * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include "awt.h"
  27 
  28 #include <windowsx.h>
  29 #include <zmouse.h>
  30 
  31 #include "jlong.h"
  32 #include "awt_AWTEvent.h"
  33 #include "awt_BitmapUtil.h"
  34 #include "awt_Component.h"
  35 #include "awt_Cursor.h"
  36 #include "awt_Dimension.h"
  37 #include "awt_Frame.h"
  38 #include "awt_InputEvent.h"
  39 #include "awt_InputTextInfor.h"
  40 #include "awt_Insets.h"
  41 #include "awt_KeyEvent.h"
  42 #include "awt_MenuItem.h"
  43 #include "awt_MouseEvent.h"
  44 #include "awt_Palette.h"
  45 #include "awt_Toolkit.h"
  46 #include "awt_Window.h"
  47 #include "awt_Win32GraphicsDevice.h"
  48 #include "Hashtable.h"
  49 #include "ComCtl32Util.h"
  50 
  51 #include <Region.h>
  52 
  53 #include <jawt.h>
  54 
  55 #include <java_awt_Toolkit.h>
  56 #include <java_awt_FontMetrics.h>
  57 #include <java_awt_Color.h>
  58 #include <java_awt_Event.h>
  59 #include <java_awt_event_KeyEvent.h>
  60 #include <java_awt_Insets.h>
  61 #include <sun_awt_windows_WPanelPeer.h>
  62 #include <java_awt_event_InputEvent.h>
  63 #include <java_awt_event_ActionEvent.h>
  64 #include <java_awt_event_InputMethodEvent.h>
  65 #include <sun_awt_windows_WInputMethod.h>
  66 #include <java_awt_event_MouseEvent.h>
  67 #include <java_awt_event_MouseWheelEvent.h>
  68 
  69 // Begin -- Win32 SDK include files
  70 #include <imm.h>
  71 #include <ime.h>
  72 // End -- Win32 SDK include files
  73 
  74 #include <awt_DnDDT.h>
  75 
  76 LPCTSTR szAwtComponentClassName = TEXT("SunAwtComponent");
  77 // register a message that no other window in the process (even in a plugin
  78 // scenario) will be using
  79 const UINT AwtComponent::WmAwtIsComponent =
  80     ::RegisterWindowMessage(szAwtComponentClassName);
  81 
  82 static HWND g_hwndDown = NULL;
  83 static DCList activeDCList;
  84 static DCList passiveDCList;
  85 
  86 extern void CheckFontSmoothingSettings(HWND);
  87 
  88 extern "C" {
  89     // Remember the input language has changed by some user's action
  90     // (Alt+Shift or through the language icon on the Taskbar) to control the
  91     // race condition between the toolkit thread and the AWT event thread.
  92     // This flag remains TRUE until the next WInputMethod.getNativeLocale() is
  93     // issued.
  94     BOOL g_bUserHasChangedInputLang = FALSE;
  95 }
  96 
  97 BOOL AwtComponent::sm_suppressFocusAndActivation = FALSE;
  98 BOOL AwtComponent::sm_restoreFocusAndActivation = FALSE;
  99 HWND AwtComponent::sm_focusOwner = NULL;
 100 HWND AwtComponent::sm_focusedWindow = NULL;
 101 BOOL AwtComponent::sm_bMenuLoop = FALSE;
 102 BOOL AwtComponent::sm_inSynthesizeFocus = FALSE;
 103 
 104 /************************************************************************/
 105 // Struct for _Reshape() and ReshapeNoCheck() methods
 106 struct ReshapeStruct {
 107     jobject component;
 108     jint x, y;
 109     jint w, h;
 110 };
 111 // Struct for _NativeHandleEvent() method
 112 struct NativeHandleEventStruct {
 113     jobject component;
 114     jobject event;
 115 };
 116 // Struct for _SetForeground() and _SetBackground() methods
 117 struct SetColorStruct {
 118     jobject component;
 119     jint rgb;
 120 };
 121 // Struct for _SetFont() method
 122 struct SetFontStruct {
 123     jobject component;
 124     jobject font;
 125 };
 126 // Struct for _CreatePrintedPixels() method
 127 struct CreatePrintedPixelsStruct {
 128     jobject component;
 129     int srcx, srcy;
 130     int srcw, srch;
 131     jint alpha;
 132 };
 133 // Struct for _SetRectangularShape() method
 134 struct SetRectangularShapeStruct {
 135     jobject component;
 136     jint x1, x2, y1, y2;
 137     jobject region;
 138 };
 139 // Struct for _GetInsets function
 140 struct GetInsetsStruct {
 141     jobject window;
 142     RECT *insets;
 143 };
 144 // Struct for _SetZOrder function
 145 struct SetZOrderStruct {
 146     jobject component;
 147     jlong above;
 148 };
 149 // Struct for _SetFocus function
 150 struct SetFocusStruct {
 151     jobject component;
 152     jboolean doSetFocus;
 153 };
 154 // Struct for _SetParent function
 155 struct SetParentStruct {
 156     jobject component;
 157     jobject parentComp;
 158 };
 159 /************************************************************************/
 160 
 161 //////////////////////////////////////////////////////////////////////////
 162 
 163 /*************************************************************************
 164  * AwtComponent fields
 165  */
 166 
 167 
 168 jfieldID AwtComponent::peerID;
 169 jfieldID AwtComponent::xID;
 170 jfieldID AwtComponent::yID;
 171 jfieldID AwtComponent::widthID;
 172 jfieldID AwtComponent::heightID;
 173 jfieldID AwtComponent::visibleID;
 174 jfieldID AwtComponent::backgroundID;
 175 jfieldID AwtComponent::foregroundID;
 176 jfieldID AwtComponent::enabledID;
 177 jfieldID AwtComponent::parentID;
 178 jfieldID AwtComponent::graphicsConfigID;
 179 jfieldID AwtComponent::peerGCID;
 180 jfieldID AwtComponent::focusableID;
 181 jfieldID AwtComponent::appContextID;
 182 jfieldID AwtComponent::cursorID;
 183 jfieldID AwtComponent::hwndID;
 184 
 185 jmethodID AwtComponent::getFontMID;
 186 jmethodID AwtComponent::getToolkitMID;
 187 jmethodID AwtComponent::isEnabledMID;
 188 jmethodID AwtComponent::getLocationOnScreenMID;
 189 jmethodID AwtComponent::replaceSurfaceDataMID;
 190 jmethodID AwtComponent::replaceSurfaceDataLaterMID;
 191 jmethodID AwtComponent::disposeLaterMID;
 192 
 193 HKL    AwtComponent::m_hkl = ::GetKeyboardLayout(0);
 194 LANGID AwtComponent::m_idLang = LOWORD(::GetKeyboardLayout(0));
 195 UINT   AwtComponent::m_CodePage
 196                        = AwtComponent::LangToCodePage(m_idLang);
 197 
 198 jint *AwtComponent::masks;
 199 
 200 static BOOL bLeftShiftIsDown = false;
 201 static BOOL bRightShiftIsDown = false;
 202 static UINT lastShiftKeyPressed = 0; // init to safe value
 203 
 204 // Added by waleed to initialize the RTL Flags
 205 BOOL AwtComponent::sm_rtl = PRIMARYLANGID(GetInputLanguage()) == LANG_ARABIC ||
 206                             PRIMARYLANGID(GetInputLanguage()) == LANG_HEBREW;
 207 BOOL AwtComponent::sm_rtlReadingOrder =
 208     PRIMARYLANGID(GetInputLanguage()) == LANG_ARABIC;
 209 
 210 BOOL AwtComponent::sm_PrimaryDynamicTableBuilt = FALSE;
 211 
 212 HWND AwtComponent::sm_cursorOn;
 213 BOOL AwtComponent::m_QueryNewPaletteCalled = FALSE;
 214 
 215 CriticalSection windowMoveLock;
 216 BOOL windowMoveLockHeld = FALSE;
 217 
 218 /************************************************************************
 219  * AwtComponent methods
 220  */
 221 
 222 AwtComponent::AwtComponent()
 223 {
 224     m_mouseButtonClickAllowed = 0;
 225     m_touchDownOccurred = FALSE;
 226     m_touchUpOccurred = FALSE;
 227     m_touchDownPoint.x = m_touchDownPoint.y = 0;
 228     m_touchUpPoint.x = m_touchUpPoint.y = 0;
 229     m_callbacksEnabled = FALSE;
 230     m_hwnd = NULL;
 231 
 232     m_colorForeground = 0;
 233     m_colorBackground = 0;
 234     m_backgroundColorSet = FALSE;
 235     m_penForeground = NULL;
 236     m_brushBackground = NULL;
 237     m_DefWindowProc = NULL;
 238     m_nextControlID = 1;
 239     m_childList = NULL;
 240     m_myControlID = 0;
 241     m_hdwp = NULL;
 242     m_validationNestCount = 0;
 243 
 244     m_dropTarget = NULL;
 245 
 246     m_InputMethod = NULL;
 247     m_useNativeCompWindow = TRUE;
 248     m_PendingLeadByte = 0;
 249     m_bitsCandType = 0;
 250 
 251     windowMoveLockPosX = 0;
 252     windowMoveLockPosY = 0;
 253     windowMoveLockPosCX = 0;
 254     windowMoveLockPosCY = 0;
 255 
 256     m_hCursorCache = NULL;
 257 
 258     m_bSubclassed = FALSE;
 259     m_bPauseDestroy = FALSE;
 260 
 261     m_MessagesProcessing = 0;
 262     m_wheelRotationAmountX = 0;
 263     m_wheelRotationAmountY = 0;
 264     if (!sm_PrimaryDynamicTableBuilt) {
 265         // do it once.
 266         AwtComponent::BuildPrimaryDynamicTable();
 267         sm_PrimaryDynamicTableBuilt = TRUE;
 268     }
 269 
 270     deadKeyActive = FALSE;
 271 }
 272 
 273 AwtComponent::~AwtComponent()
 274 {
 275     DASSERT(AwtToolkit::IsMainThread());
 276 
 277     /*
 278      * All the messages for this component are processed, native
 279      * resources are freed, and Java object is not connected to
 280      * the native one anymore. So we can safely destroy component's
 281      * handle.
 282      */
 283     DestroyHWnd();
 284 }
 285 
 286 void AwtComponent::Dispose()
 287 {
 288     DASSERT(AwtToolkit::IsMainThread());
 289 
 290     // NOTE: in case the component/toplevel was focused, Java should
 291     // have already taken care of proper transferring it or clearing.
 292 
 293     if (m_hdwp != NULL) {
 294     // end any deferred window positioning, regardless
 295     // of m_validationNestCount
 296         ::EndDeferWindowPos(m_hdwp);
 297     }
 298 
 299     // Send final message to release all DCs associated with this component
 300     SendMessage(WM_AWT_RELEASE_ALL_DCS);
 301 
 302     /* Stop message filtering. */
 303     UnsubclassHWND();
 304 
 305     /* Release global ref to input method */
 306     SetInputMethod(NULL, TRUE);
 307 
 308     if (m_childList != NULL) {
 309         delete m_childList;
 310         m_childList = NULL;
 311     }
 312 
 313     DestroyDropTarget();
 314     ReleaseDragCapture(0);
 315 
 316     if (m_myControlID != 0) {
 317         AwtComponent* parent = GetParent();
 318         if (parent != NULL)
 319             parent->RemoveChild(m_myControlID);
 320     }
 321 
 322     ::RemoveProp(GetHWnd(), DrawingStateProp);
 323 
 324     /* Release any allocated resources. */
 325     if (m_penForeground != NULL) {
 326         m_penForeground->Release();
 327         m_penForeground = NULL;
 328     }
 329     if (m_brushBackground != NULL) {
 330         m_brushBackground->Release();
 331         m_brushBackground = NULL;
 332     }
 333 
 334     /* Disconnect all links. */
 335     UnlinkObjects();
 336 
 337     if (m_bPauseDestroy) {
 338         // AwtComponent::WmNcDestroy could be released now
 339         m_bPauseDestroy = FALSE;
 340         m_hwnd = NULL;
 341     }
 342 
 343     // The component instance is deleted using AwtObject::Dispose() method
 344     AwtObject::Dispose();
 345 }
 346 
 347 /* store component pointer in window extra bytes */
 348 void AwtComponent::SetComponentInHWND() {
 349     DASSERT(::GetWindowLongPtr(GetHWnd(), GWLP_USERDATA) == NULL);
 350     ::SetWindowLongPtr(GetHWnd(), GWLP_USERDATA, (LONG_PTR)this);
 351 }
 352 
 353 /*
 354  * static function to get AwtComponent pointer from hWnd --
 355  * you don't want to call this from inside a wndproc to avoid
 356  * infinite recursion
 357  */
 358 AwtComponent* AwtComponent::GetComponent(HWND hWnd) {
 359     // Requests for Toolkit hwnd resolution happen pretty often. Check first.
 360     if (hWnd == AwtToolkit::GetInstance().GetHWnd()) {
 361         return NULL;
 362     }
 363 
 364     // check that it's an AWT component from the same toolkit as the caller
 365     if (::IsWindow(hWnd) &&
 366         AwtToolkit::MainThread() == ::GetWindowThreadProcessId(hWnd, NULL))
 367     {
 368         DASSERT(WmAwtIsComponent != 0);
 369         if (::SendMessage(hWnd, WmAwtIsComponent, 0, 0L)) {
 370             return GetComponentImpl(hWnd);
 371         }
 372     }
 373     return NULL;
 374 }
 375 
 376 /*
 377  * static function to get AwtComponent pointer from hWnd--
 378  * different from GetComponent because caller knows the
 379  * hwnd is an AWT component hwnd
 380  */
 381 AwtComponent* AwtComponent::GetComponentImpl(HWND hWnd) {
 382     AwtComponent *component =
 383         (AwtComponent *)::GetWindowLongPtr(hWnd, GWLP_USERDATA);
 384     DASSERT(!component || !IsBadReadPtr(component, sizeof(AwtComponent)) );
 385     DASSERT(!component || component->GetHWnd() == hWnd );
 386     return component;
 387 }
 388 
 389 /*
 390  * Single window proc for all the components. Delegates real work to
 391  * the component's WindowProc() member function.
 392  */
 393 LRESULT CALLBACK AwtComponent::WndProc(HWND hWnd, UINT message,
 394                                        WPARAM wParam, LPARAM lParam)
 395 {
 396     TRY;
 397 
 398     AwtComponent * self = AwtComponent::GetComponentImpl(hWnd);
 399     if (self == NULL || self->GetHWnd() != hWnd ||
 400         message == WM_UNDOCUMENTED_CLIENTSHUTDOWN) // handle log-off gracefully
 401     {
 402         return ComCtl32Util::GetInstance().DefWindowProc(NULL, hWnd, message, wParam, lParam);
 403     } else {
 404         return self->WindowProc(message, wParam, lParam);
 405     }
 406 
 407     CATCH_BAD_ALLOC_RET(0);
 408 }
 409 
 410 BOOL AwtComponent::IsFocusable() {
 411     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 412     jobject peer = GetPeer(env);
 413     jobject target = env->GetObjectField(peer, AwtObject::targetID);
 414     BOOL res = env->GetBooleanField(target, focusableID);
 415     AwtWindow *pCont = GetContainer();
 416     if (pCont) {
 417         res &= pCont->IsFocusableWindow();
 418     }
 419     env->DeleteLocalRef(target);
 420     return res;
 421 }
 422 
 423 /************************************************************************
 424  * AwtComponent dynamic methods
 425  *
 426  * Window class registration routines
 427  */
 428 
 429 /*
 430  * Fix for 4964237: Win XP: Changing theme changes java dialogs title icon
 431  */
 432 void AwtComponent::FillClassInfo(WNDCLASSEX *lpwc)
 433 {
 434     lpwc->cbSize        = sizeof(WNDCLASSEX);
 435     lpwc->style         = 0L;//CS_OWNDC;
 436     lpwc->lpfnWndProc   = (WNDPROC)::DefWindowProc;
 437     lpwc->cbClsExtra    = 0;
 438     lpwc->cbWndExtra    = 0;
 439     lpwc->hInstance     = AwtToolkit::GetInstance().GetModuleHandle(),
 440     lpwc->hIcon         = AwtToolkit::GetInstance().GetAwtIcon();
 441     lpwc->hCursor       = NULL;
 442     lpwc->hbrBackground = NULL;
 443     lpwc->lpszMenuName  = NULL;
 444     lpwc->lpszClassName = GetClassName();
 445     //Fixed 6233560: PIT: Java Cup Logo on the title bar of top-level windows look blurred, Win32
 446     lpwc->hIconSm       = AwtToolkit::GetInstance().GetAwtIconSm();
 447 }
 448 
 449 void AwtComponent::RegisterClass()
 450 {
 451     WNDCLASSEX wc;
 452     if (!::GetClassInfoEx(AwtToolkit::GetInstance().GetModuleHandle(), GetClassName(), &wc)) {
 453         FillClassInfo(&wc);
 454         ATOM ret = ::RegisterClassEx(&wc);
 455         DASSERT(ret != 0);
 456     }
 457 }
 458 
 459 void AwtComponent::UnregisterClass()
 460 {
 461     ::UnregisterClass(GetClassName(), AwtToolkit::GetInstance().GetModuleHandle());
 462 }
 463 
 464 /*
 465  * Copy the graphicsConfig reference from Component into WComponentPeer
 466  */
 467 void AwtComponent::InitPeerGraphicsConfig(JNIEnv *env, jobject peer)
 468 {
 469     jobject target = env->GetObjectField(peer, AwtObject::targetID);
 470     //Get graphicsConfig object ref from Component
 471     jobject compGC = env->GetObjectField(target,
 472                       AwtComponent::graphicsConfigID);
 473 
 474     //Set peer's graphicsConfig to Component's graphicsConfig
 475     if (compGC != NULL) {
 476         jclass win32GCCls = env->FindClass("sun/awt/Win32GraphicsConfig");
 477         DASSERT(win32GCCls != NULL);
 478         DASSERT(env->IsInstanceOf(compGC, win32GCCls));
 479         if (win32GCCls == NULL) {
 480             throw std::bad_alloc();
 481         }
 482         env->SetObjectField(peer, AwtComponent::peerGCID, compGC);
 483     }
 484 }
 485 
 486 void
 487 AwtComponent::CreateHWnd(JNIEnv *env, LPCWSTR title,
 488                          DWORD windowStyle,
 489                          DWORD windowExStyle,
 490                          int x, int y, int w, int h,
 491                          HWND hWndParent, HMENU hMenu,
 492                          COLORREF colorForeground,
 493                          COLORREF colorBackground,
 494                          jobject peer)
 495 {
 496     if (env->EnsureLocalCapacity(2) < 0) {
 497         return;
 498     }
 499 
 500     /*
 501      * The window class of multifont label must be "BUTTON" because
 502      * "STATIC" class can't get WM_DRAWITEM message, and m_peerObject
 503      * member is referred in the GetClassName method of AwtLabel class.
 504      * So m_peerObject member must be set here.
 505      */
 506     if (m_peerObject == NULL) {
 507         m_peerObject = env->NewGlobalRef(peer);
 508     } else {
 509         assert(env->IsSameObject(m_peerObject, peer));
 510     }
 511 
 512     RegisterClass();
 513 
 514     jobject target = env->GetObjectField(peer, AwtObject::targetID);
 515     jboolean visible = env->GetBooleanField(target, AwtComponent::visibleID);
 516     m_visible = visible;
 517 
 518     if (visible) {
 519         windowStyle |= WS_VISIBLE;
 520     } else {
 521         windowStyle &= ~WS_VISIBLE;
 522     }
 523 
 524     InitPeerGraphicsConfig(env, peer);
 525 
 526     SetLastError(0);
 527     HWND hwnd = ::CreateWindowEx(windowExStyle,
 528                                  GetClassName(),
 529                                  title,
 530                                  windowStyle,
 531                                  x, y, w, h,
 532                                  hWndParent,
 533                                  hMenu,
 534                                  AwtToolkit::GetInstance().GetModuleHandle(),
 535                                  NULL);
 536 
 537     // fix for 5088782
 538     // check if CreateWindowsEx() returns not null value and if it does -
 539     //   create an InternalError or OutOfMemoryError based on GetLastError().
 540     //   This error is set to createError field of WObjectPeer and then
 541     //   checked and thrown in WComponentPeer constructor. We can't throw an
 542     //   error here because this code is invoked on Toolkit thread
 543     if (hwnd == NULL)
 544     {
 545         DWORD dw = ::GetLastError();
 546         jobject createError = NULL;
 547         if (dw == ERROR_OUTOFMEMORY)
 548         {
 549             jstring errorMsg = JNU_NewStringPlatform(env, L"too many window handles");
 550             if (errorMsg == NULL || env->ExceptionCheck()) {
 551                 env->ExceptionClear();
 552                 createError = JNU_NewObjectByName(env, "java/lang/OutOfMemoryError", "()V");
 553             } else {
 554                 createError = JNU_NewObjectByName(env, "java/lang/OutOfMemoryError",
 555                                                       "(Ljava/lang/String;)V",
 556                                                       errorMsg);
 557                 env->DeleteLocalRef(errorMsg);
 558             }
 559         }
 560         else
 561         {
 562             TCHAR *buf;
 563             FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
 564                 NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
 565                 (LPTSTR)&buf, 0, NULL);
 566             jstring s = JNU_NewStringPlatform(env, buf);
 567             if (s == NULL || env->ExceptionCheck()) {
 568                 env->ExceptionClear();
 569                 createError = JNU_NewObjectByName(env, "java/lang/InternalError", "()V");
 570             } else {
 571                 createError = JNU_NewObjectByName(env, "java/lang/InternalError",
 572                                                                   "(Ljava/lang/String;)V", s);
 573                 env->DeleteLocalRef(s);
 574             }
 575             LocalFree(buf);
 576         }
 577         if (createError != NULL) {
 578             env->SetObjectField(peer, AwtObject::createErrorID, createError);
 579             env->DeleteLocalRef(createError);
 580         }
 581         env->DeleteLocalRef(target);
 582         return;
 583     }
 584 
 585     m_hwnd = hwnd;
 586 
 587     ::ImmAssociateContext(m_hwnd, NULL);
 588 
 589     SetDrawState((jint)JAWT_LOCK_SURFACE_CHANGED |
 590         (jint)JAWT_LOCK_BOUNDS_CHANGED |
 591         (jint)JAWT_LOCK_CLIP_CHANGED);
 592 
 593     LinkObjects(env, peer);
 594 
 595     /* Subclass the window now so that we can snoop on its messages */
 596     SubclassHWND();
 597 
 598     AwtToolkit& tk = AwtToolkit::GetInstance();
 599     if (tk.IsWin8OrLater() && tk.IsTouchKeyboardAutoShowEnabled()) {
 600         tk.TIRegisterTouchWindow(GetHWnd(), TWF_WANTPALM);
 601     }
 602 
 603     /*
 604       * Fix for 4046446.
 605       */
 606     SetWindowPos(GetHWnd(), 0, x, y, w, h, SWP_NOZORDER | SWP_NOCOPYBITS | SWP_NOACTIVATE);
 607 
 608     /* Set default colors. */
 609     m_colorForeground = colorForeground;
 610     m_colorBackground = colorBackground;
 611 
 612     /*
 613      * Only set background color if the color is actually set on the
 614      * target -- this avoids inheriting a parent's color unnecessarily,
 615      * and has to be done here because there isn't an API to get the
 616      * real background color from outside the AWT package.
 617      */
 618     jobject bkgrd = env->GetObjectField(target, AwtComponent::backgroundID) ;
 619     if (bkgrd != NULL) {
 620         JNU_CallMethodByName(env, NULL, peer, "setBackground",
 621                              "(Ljava/awt/Color;)V", bkgrd);
 622         DASSERT(!safe_ExceptionOccurred(env));
 623     }
 624     env->DeleteLocalRef(target);
 625     env->DeleteLocalRef(bkgrd);
 626 }
 627 
 628 /*
 629  * Destroy this window's HWND
 630  */
 631 void AwtComponent::DestroyHWnd() {
 632     if (m_hwnd != NULL) {
 633         AwtToolkit::DestroyComponentHWND(m_hwnd);
 634         //AwtToolkit::DestroyComponent(this);
 635         m_hwnd = NULL;
 636     }
 637 }
 638 
 639 /*
 640  * Returns hwnd for target on non Toolkit thread
 641  */
 642 HWND
 643 AwtComponent::GetHWnd(JNIEnv* env, jobject target) {
 644     if (JNU_IsNull(env, target)) {
 645         return 0;
 646     }
 647     jobject peer = env->GetObjectField(target, AwtComponent::peerID);
 648     if (JNU_IsNull(env, peer)) {
 649         return 0;
 650     }
 651     HWND hwnd = reinterpret_cast<HWND>(static_cast<LONG_PTR> (
 652         env->GetLongField(peer, AwtComponent::hwndID)));
 653     env->DeleteLocalRef(peer);
 654     return hwnd;
 655 }
 656 //
 657 // Propagate the background color to synchronize Java field and peer's field.
 658 // This is needed to fix 4148334
 659 //
 660 void AwtComponent::UpdateBackground(JNIEnv *env, jobject target)
 661 {
 662     if (env->EnsureLocalCapacity(1) < 0) {
 663         return;
 664     }
 665 
 666     jobject bkgrnd = env->GetObjectField(target, AwtComponent::backgroundID);
 667 
 668     if (bkgrnd == NULL) {
 669         bkgrnd = JNU_NewObjectByName(env, "java/awt/Color", "(III)V",
 670                                      GetRValue(m_colorBackground),
 671                                      GetGValue(m_colorBackground),
 672                                      GetBValue(m_colorBackground));
 673         if (bkgrnd != NULL) {
 674             env->SetObjectField(target, AwtComponent::backgroundID, bkgrnd);
 675         }
 676     }
 677     env->DeleteLocalRef(bkgrnd);
 678 }
 679 
 680 /*
 681  * Install our window proc as the proc for our HWND, and save off the
 682  * previous proc as the default
 683  */
 684 void AwtComponent::SubclassHWND()
 685 {
 686     if (m_bSubclassed) {
 687         return;
 688     }
 689     const WNDPROC wndproc = WndProc; // let compiler type check WndProc
 690     m_DefWindowProc = ComCtl32Util::GetInstance().SubclassHWND(GetHWnd(), wndproc);
 691     m_bSubclassed = TRUE;
 692 }
 693 
 694 /*
 695  * Reinstall the original window proc as the proc for our HWND
 696  */
 697 void AwtComponent::UnsubclassHWND()
 698 {
 699     if (!m_bSubclassed) {
 700         return;
 701     }
 702     ComCtl32Util::GetInstance().UnsubclassHWND(GetHWnd(), WndProc, m_DefWindowProc);
 703     m_bSubclassed = FALSE;
 704 }
 705 
 706 /////////////////////////////////////
 707 // (static method)
 708 // Determines the top-level ancestor for a given window. If the given
 709 // window is a top-level window, return itself.
 710 //
 711 // 'Top-level' includes dialogs as well.
 712 //
 713 HWND AwtComponent::GetTopLevelParentForWindow(HWND hwndDescendant) {
 714     if (hwndDescendant == NULL) {
 715         return NULL;
 716     }
 717 
 718     DASSERT(IsWindow(hwndDescendant));
 719     HWND hwnd = hwndDescendant;
 720     for(;;) {
 721         DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);
 722         // a) found a non-child window so terminate
 723         // b) found real toplevel window (e.g. EmbeddedFrame
 724         //    that is child though)
 725         if ( (style & WS_CHILD) == 0 ||
 726              AwtComponent::IsTopLevelHWnd(hwnd) )
 727         {
 728             break;
 729         }
 730         hwnd = ::GetParent(hwnd);
 731     }
 732 
 733     return hwnd;
 734 }
 735 ////////////////////
 736 
 737 jobject AwtComponent::FindHeavyweightUnderCursor(BOOL useCache) {
 738     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 739     if (env->EnsureLocalCapacity(1) < 0) {
 740         return NULL;
 741     }
 742 
 743     HWND hit = NULL;
 744     POINT p = { 0, 0 };
 745     AwtComponent *comp = NULL;
 746 
 747     if (useCache) {
 748         if (sm_cursorOn == NULL) {
 749             return NULL;
 750         }
 751 
 752 
 753         DASSERT(::IsWindow(sm_cursorOn));
 754         VERIFY(::GetCursorPos(&p));
 755         /*
 756          * Fix for BugTraq ID 4304024.
 757          * Allow a non-default cursor only for the client area.
 758          */
 759         comp = AwtComponent::GetComponent(sm_cursorOn);
 760         if (comp != NULL &&
 761             ::SendMessage(sm_cursorOn, WM_NCHITTEST, 0,
 762                           MAKELPARAM(p.x, p.y)) == HTCLIENT) {
 763             goto found;
 764         }
 765     }
 766 
 767     ::GetCursorPos(&p);
 768     hit = ::WindowFromPoint(p);
 769     while (hit != NULL) {
 770         comp = AwtComponent::GetComponent(hit);
 771 
 772         if (comp != NULL) {
 773             INT nHittest = (INT)::SendMessage(hit, WM_NCHITTEST,
 774                                           0, MAKELPARAM(p.x, p.y));
 775             /*
 776              * Fix for BugTraq ID 4304024.
 777              * Allow a non-default cursor only for the client area.
 778              */
 779             if (nHittest != HTCLIENT) {
 780                 /*
 781                  * When over the non-client area, send WM_SETCURSOR
 782                  * to revert the cursor to an arrow.
 783                  */
 784                 ::SendMessage(hit, WM_SETCURSOR, (WPARAM)hit,
 785                               MAKELPARAM(nHittest, WM_MOUSEMOVE));
 786                 return NULL;
 787             } else {
 788               sm_cursorOn = hit;
 789               goto found;
 790             }
 791         }
 792 
 793         if ((::GetWindowLong(hit, GWL_STYLE) & WS_CHILD) == 0) {
 794             return NULL;
 795         }
 796         hit = ::GetParent(hit);
 797     }
 798 
 799     return NULL;
 800 
 801 found:
 802     jobject localRef = comp->GetTarget(env);
 803     jobject globalRef = env->NewGlobalRef(localRef);
 804     env->DeleteLocalRef(localRef);
 805     return globalRef;
 806 }
 807 
 808 void AwtComponent::SetColor(COLORREF c)
 809 {
 810     int screen = AwtWin32GraphicsDevice::DeviceIndexForWindow(GetHWnd());
 811     int grayscale = AwtWin32GraphicsDevice::GetGrayness(screen);
 812     if (grayscale != GS_NOTGRAY) {
 813         int g;
 814 
 815         g = (int) (.299 * (c & 0xFF) + .587 * ((c >> 8) & 0xFF) +
 816             .114 * ((c >> 16) & 0xFF) + 0.5);
 817         // c = g | (g << 8) | (g << 16);
 818         c = PALETTERGB(g, g, g);
 819     }
 820 
 821     if (m_colorForeground == c) {
 822         return;
 823     }
 824 
 825     m_colorForeground = c;
 826     if (m_penForeground != NULL) {
 827         m_penForeground->Release();
 828         m_penForeground = NULL;
 829     }
 830     VERIFY(::InvalidateRect(GetHWnd(), NULL, FALSE));
 831 }
 832 
 833 void AwtComponent::SetBackgroundColor(COLORREF c)
 834 {
 835     int screen = AwtWin32GraphicsDevice::DeviceIndexForWindow(GetHWnd());
 836     int grayscale = AwtWin32GraphicsDevice::GetGrayness(screen);
 837     if (grayscale != GS_NOTGRAY) {
 838         int g;
 839 
 840         g = (int) (.299 * (c & 0xFF) + .587 * ((c >> 8) & 0xFF) +
 841             .114 * ((c >> 16) & 0xFF) + 0.5);
 842         // c = g | (g << 8) | (g << 16);
 843         c = PALETTERGB(g, g, g);
 844     }
 845 
 846     if (m_colorBackground == c) {
 847         return;
 848     }
 849     m_colorBackground = c;
 850     m_backgroundColorSet = TRUE;
 851     if (m_brushBackground != NULL) {
 852         m_brushBackground->Release();
 853         m_brushBackground = NULL;
 854     }
 855     VERIFY(::InvalidateRect(GetHWnd(), NULL, TRUE));
 856 }
 857 
 858 HPEN AwtComponent::GetForegroundPen()
 859 {
 860     if (m_penForeground == NULL) {
 861         m_penForeground = AwtPen::Get(m_colorForeground);
 862     }
 863     return (HPEN)m_penForeground->GetHandle();
 864 }
 865 
 866 COLORREF AwtComponent::GetBackgroundColor()
 867 {
 868     if (m_backgroundColorSet == FALSE) {
 869         AwtComponent* c = this;
 870         while ((c = c->GetParent()) != NULL) {
 871             if (c->IsBackgroundColorSet()) {
 872                 return c->GetBackgroundColor();
 873             }
 874         }
 875     }
 876     return m_colorBackground;
 877 }
 878 
 879 HBRUSH AwtComponent::GetBackgroundBrush()
 880 {
 881     if (m_backgroundColorSet == FALSE) {
 882         if (m_brushBackground != NULL) {
 883             m_brushBackground->Release();
 884             m_brushBackground = NULL;
 885         }
 886           AwtComponent* c = this;
 887           while ((c = c->GetParent()) != NULL) {
 888               if (c->IsBackgroundColorSet()) {
 889                   m_brushBackground =
 890                       AwtBrush::Get(c->GetBackgroundColor());
 891                   break;
 892               }
 893           }
 894     }
 895     if (m_brushBackground == NULL) {
 896         m_brushBackground = AwtBrush::Get(m_colorBackground);
 897     }
 898     return (HBRUSH)m_brushBackground->GetHandle();
 899 }
 900 
 901 void AwtComponent::SetFont(AwtFont* font)
 902 {
 903     DASSERT(font != NULL);
 904     if (font->GetAscent() < 0) {
 905         AwtFont::SetupAscent(font);
 906     }
 907     SendMessage(WM_SETFONT, (WPARAM)font->GetHFont(), MAKELPARAM(FALSE, 0));
 908     VERIFY(::InvalidateRect(GetHWnd(), NULL, TRUE));
 909 }
 910 
 911 AwtComponent* AwtComponent::GetParent()
 912 {
 913     HWND hwnd = ::GetParent(GetHWnd());
 914     if (hwnd == NULL) {
 915         return NULL;
 916     }
 917     return GetComponent(hwnd);
 918 }
 919 
 920 AwtWindow* AwtComponent::GetContainer()
 921 {
 922     AwtComponent* comp = this;
 923     while (comp != NULL) {
 924         if (comp->IsContainer()) {
 925             return (AwtWindow*)comp;
 926         }
 927         comp = comp->GetParent();
 928     }
 929     return NULL;
 930 }
 931 
 932 void AwtComponent::Show()
 933 {
 934     m_visible = true;
 935     ::ShowWindow(GetHWnd(), SW_SHOWNA);
 936 }
 937 
 938 void AwtComponent::Hide()
 939 {
 940     m_visible = false;
 941     ::ShowWindow(GetHWnd(), SW_HIDE);
 942 }
 943 
 944 BOOL
 945 AwtComponent::SetWindowPos(HWND wnd, HWND after,
 946                            int x, int y, int w, int h, UINT flags)
 947 {
 948     // Conditions we shouldn't handle:
 949     // z-order changes, correct window dimensions
 950     if (after != NULL || (w < 32767 && h < 32767)
 951         || ((::GetWindowLong(wnd, GWL_STYLE) & WS_CHILD) == 0))
 952     {
 953         return ::SetWindowPos(wnd, after, x, y, w, h, flags);
 954     }
 955     WINDOWPLACEMENT wp;
 956     ::ZeroMemory(&wp, sizeof(wp));
 957 
 958     wp.length = sizeof(wp);
 959     ::GetWindowPlacement(wnd, &wp);
 960     wp.rcNormalPosition.left = x;
 961     wp.rcNormalPosition.top = y;
 962     wp.rcNormalPosition.right = x + w;
 963     wp.rcNormalPosition.bottom = y + h;
 964     if ( flags & SWP_NOACTIVATE ) {
 965         wp.showCmd = SW_SHOWNOACTIVATE;
 966     }
 967     ::SetWindowPlacement(wnd, &wp);
 968     return 1;
 969 }
 970 
 971 void AwtComponent::Reshape(int x, int y, int w, int h) {
 972     ReshapeNoScale(ScaleUpX(x), ScaleUpY(y), ScaleUpX(w), ScaleUpY(h));
 973 }
 974 
 975 void AwtComponent::ReshapeNoScale(int x, int y, int w, int h)
 976 {
 977 #if defined(DEBUG)
 978     RECT        rc;
 979     ::GetWindowRect(GetHWnd(), &rc);
 980     ::MapWindowPoints(HWND_DESKTOP, ::GetParent(GetHWnd()), (LPPOINT)&rc, 2);
 981     DTRACE_PRINTLN4("AwtComponent::Reshape from %d, %d, %d, %d", rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top);
 982 #endif
 983 
 984     AwtWindow* container = GetContainer();
 985     AwtComponent* parent = GetParent();
 986     if (container != NULL && container == parent) {
 987         container->SubtractInsetPoint(x, y);
 988     }
 989     DTRACE_PRINTLN4("AwtComponent::Reshape to %d, %d, %d, %d", x, y, w, h);
 990     UINT flags = SWP_NOACTIVATE | SWP_NOZORDER;
 991 
 992     RECT        r;
 993 
 994     ::GetWindowRect(GetHWnd(), &r);
 995     // if the component size is changing , don't copy window bits
 996     if (r.right - r.left != w || r.bottom - r.top != h) {
 997         flags |= SWP_NOCOPYBITS;
 998     }
 999 
1000     if (parent && _tcscmp(parent->GetClassName(), TEXT("SunAwtScrollPane")) == 0) {
1001         if (x > 0) {
1002             x = 0;
1003         }
1004         if (y > 0) {
1005             y = 0;
1006         }
1007     }
1008     if (m_hdwp != NULL) {
1009         m_hdwp = ::DeferWindowPos(m_hdwp, GetHWnd(), 0, x, y, w, h, flags);
1010         DASSERT(m_hdwp != NULL);
1011     } else {
1012         /*
1013          * Fox for 4046446
1014          * If window has dimensions above the short int limit, ::SetWindowPos doesn't work.
1015          * We should use SetWindowPlacement instead.
1016          */
1017         SetWindowPos(GetHWnd(), 0, x, y, w, h, flags);
1018     }
1019 }
1020 
1021 void AwtComponent::SetScrollValues(UINT bar, int min, int value, int max)
1022 {
1023     int minTmp, maxTmp;
1024 
1025     ::GetScrollRange(GetHWnd(), bar, &minTmp, &maxTmp);
1026     if (min == INT_MAX) {
1027         min = minTmp;
1028     }
1029     if (value == INT_MAX) {
1030         value = ::GetScrollPos(GetHWnd(), bar);
1031     }
1032     if (max == INT_MAX) {
1033         max = maxTmp;
1034     }
1035     if (min == max) {
1036         max++;
1037     }
1038     ::SetScrollRange(GetHWnd(), bar, min, max, FALSE);
1039     ::SetScrollPos(GetHWnd(), bar, value, TRUE);
1040 }
1041 
1042 /*
1043  * Save Global Reference of sun.awt.windows.WInputMethod object
1044  */
1045 void AwtComponent::SetInputMethod(jobject im, BOOL useNativeCompWindow)
1046 {
1047     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1048 
1049     if (m_InputMethod!=NULL)
1050         env->DeleteGlobalRef(m_InputMethod);
1051 
1052     if (im!=NULL){
1053         m_InputMethod = env->NewGlobalRef(im);
1054         m_useNativeCompWindow = useNativeCompWindow;
1055     } else {
1056         m_InputMethod = NULL;
1057         m_useNativeCompWindow = TRUE;
1058     }
1059 
1060 }
1061 
1062 /*
1063  * Opportunity to process and/or eat a message before it is dispatched
1064  */
1065 MsgRouting AwtComponent::PreProcessMsg(MSG& msg)
1066 {
1067     return mrPassAlong;
1068 }
1069 
1070 static UINT lastMessage = WM_NULL;
1071 
1072 #ifndef SPY_MESSAGES
1073 #define SpyWinMessage(hwin,msg,str)
1074 #else
1075 
1076 #define FMT_MSG(x,y) case x: _stprintf(szBuf, \
1077     "0x%8.8x(%s):%s\n", hwnd, szComment, y); break;
1078 #define WIN_MSG(x) FMT_MSG(x,#x)
1079 
1080 void SpyWinMessage(HWND hwnd, UINT message, LPCTSTR szComment) {
1081 
1082     TCHAR szBuf[256];
1083 
1084     switch (message) {
1085         WIN_MSG(WM_NULL)
1086         WIN_MSG(WM_CREATE)
1087         WIN_MSG(WM_DESTROY)
1088         WIN_MSG(WM_MOVE)
1089         WIN_MSG(WM_SIZE)
1090         WIN_MSG(WM_ACTIVATE)
1091         WIN_MSG(WM_SETFOCUS)
1092         WIN_MSG(WM_KILLFOCUS)
1093         WIN_MSG(WM_ENABLE)
1094         WIN_MSG(WM_SETREDRAW)
1095         WIN_MSG(WM_SETTEXT)
1096         WIN_MSG(WM_GETTEXT)
1097         WIN_MSG(WM_GETTEXTLENGTH)
1098         WIN_MSG(WM_PAINT)
1099         WIN_MSG(WM_CLOSE)
1100         WIN_MSG(WM_QUERYENDSESSION)
1101         WIN_MSG(WM_QUIT)
1102         WIN_MSG(WM_QUERYOPEN)
1103         WIN_MSG(WM_ERASEBKGND)
1104         WIN_MSG(WM_SYSCOLORCHANGE)
1105         WIN_MSG(WM_ENDSESSION)
1106         WIN_MSG(WM_SHOWWINDOW)
1107         FMT_MSG(WM_WININICHANGE,"WM_WININICHANGE/WM_SETTINGCHANGE")
1108         WIN_MSG(WM_DEVMODECHANGE)
1109         WIN_MSG(WM_ACTIVATEAPP)
1110         WIN_MSG(WM_FONTCHANGE)
1111         WIN_MSG(WM_TIMECHANGE)
1112         WIN_MSG(WM_CANCELMODE)
1113         WIN_MSG(WM_SETCURSOR)
1114         WIN_MSG(WM_MOUSEACTIVATE)
1115         WIN_MSG(WM_CHILDACTIVATE)
1116         WIN_MSG(WM_QUEUESYNC)
1117         WIN_MSG(WM_GETMINMAXINFO)
1118         WIN_MSG(WM_PAINTICON)
1119         WIN_MSG(WM_ICONERASEBKGND)
1120         WIN_MSG(WM_NEXTDLGCTL)
1121         WIN_MSG(WM_SPOOLERSTATUS)
1122         WIN_MSG(WM_DRAWITEM)
1123         WIN_MSG(WM_MEASUREITEM)
1124         WIN_MSG(WM_DELETEITEM)
1125         WIN_MSG(WM_VKEYTOITEM)
1126         WIN_MSG(WM_CHARTOITEM)
1127         WIN_MSG(WM_SETFONT)
1128         WIN_MSG(WM_GETFONT)
1129         WIN_MSG(WM_SETHOTKEY)
1130         WIN_MSG(WM_GETHOTKEY)
1131         WIN_MSG(WM_QUERYDRAGICON)
1132         WIN_MSG(WM_COMPAREITEM)
1133         FMT_MSG(0x003D, "WM_GETOBJECT")
1134         WIN_MSG(WM_COMPACTING)
1135         WIN_MSG(WM_COMMNOTIFY)
1136         WIN_MSG(WM_WINDOWPOSCHANGING)
1137         WIN_MSG(WM_WINDOWPOSCHANGED)
1138         WIN_MSG(WM_POWER)
1139         WIN_MSG(WM_COPYDATA)
1140         WIN_MSG(WM_CANCELJOURNAL)
1141         WIN_MSG(WM_NOTIFY)
1142         WIN_MSG(WM_INPUTLANGCHANGEREQUEST)
1143         WIN_MSG(WM_INPUTLANGCHANGE)
1144         WIN_MSG(WM_TCARD)
1145         WIN_MSG(WM_HELP)
1146         WIN_MSG(WM_USERCHANGED)
1147         WIN_MSG(WM_NOTIFYFORMAT)
1148         WIN_MSG(WM_CONTEXTMENU)
1149         WIN_MSG(WM_STYLECHANGING)
1150         WIN_MSG(WM_STYLECHANGED)
1151         WIN_MSG(WM_DISPLAYCHANGE)
1152         WIN_MSG(WM_GETICON)
1153         WIN_MSG(WM_SETICON)
1154         WIN_MSG(WM_NCCREATE)
1155         WIN_MSG(WM_NCDESTROY)
1156         WIN_MSG(WM_NCCALCSIZE)
1157         WIN_MSG(WM_NCHITTEST)
1158         WIN_MSG(WM_NCPAINT)
1159         WIN_MSG(WM_NCACTIVATE)
1160         WIN_MSG(WM_GETDLGCODE)
1161         WIN_MSG(WM_SYNCPAINT)
1162         WIN_MSG(WM_NCMOUSEMOVE)
1163         WIN_MSG(WM_NCLBUTTONDOWN)
1164         WIN_MSG(WM_NCLBUTTONUP)
1165         WIN_MSG(WM_NCLBUTTONDBLCLK)
1166         WIN_MSG(WM_NCRBUTTONDOWN)
1167         WIN_MSG(WM_NCRBUTTONUP)
1168         WIN_MSG(WM_NCRBUTTONDBLCLK)
1169         WIN_MSG(WM_NCMBUTTONDOWN)
1170         WIN_MSG(WM_NCMBUTTONUP)
1171         WIN_MSG(WM_NCMBUTTONDBLCLK)
1172         WIN_MSG(WM_KEYDOWN)
1173         WIN_MSG(WM_KEYUP)
1174         WIN_MSG(WM_CHAR)
1175         WIN_MSG(WM_DEADCHAR)
1176         WIN_MSG(WM_SYSKEYDOWN)
1177         WIN_MSG(WM_SYSKEYUP)
1178         WIN_MSG(WM_SYSCHAR)
1179         WIN_MSG(WM_SYSDEADCHAR)
1180         WIN_MSG(WM_IME_STARTCOMPOSITION)
1181         WIN_MSG(WM_IME_ENDCOMPOSITION)
1182         WIN_MSG(WM_IME_COMPOSITION)
1183         WIN_MSG(WM_INITDIALOG)
1184         WIN_MSG(WM_COMMAND)
1185         WIN_MSG(WM_SYSCOMMAND)
1186         WIN_MSG(WM_TIMER)
1187         WIN_MSG(WM_HSCROLL)
1188         WIN_MSG(WM_VSCROLL)
1189         WIN_MSG(WM_INITMENU)
1190         WIN_MSG(WM_INITMENUPOPUP)
1191         WIN_MSG(WM_MENUSELECT)
1192         WIN_MSG(WM_MENUCHAR)
1193         WIN_MSG(WM_ENTERIDLE)
1194         FMT_MSG(0x0122, "WM_MENURBUTTONUP")
1195         FMT_MSG(0x0123, "WM_MENUDRAG")
1196         FMT_MSG(0x0124, "WM_MENUGETOBJECT")
1197         FMT_MSG(0x0125, "WM_UNINITMENUPOPUP")
1198         FMT_MSG(0x0126, "WM_MENUCOMMAND")
1199         WIN_MSG(WM_CTLCOLORMSGBOX)
1200         WIN_MSG(WM_CTLCOLOREDIT)
1201         WIN_MSG(WM_CTLCOLORLISTBOX)
1202         WIN_MSG(WM_CTLCOLORBTN)
1203         WIN_MSG(WM_CTLCOLORDLG)
1204         WIN_MSG(WM_CTLCOLORSCROLLBAR)
1205         WIN_MSG(WM_CTLCOLORSTATIC)
1206         WIN_MSG(WM_MOUSEMOVE)
1207         WIN_MSG(WM_LBUTTONDOWN)
1208         WIN_MSG(WM_LBUTTONUP)
1209         WIN_MSG(WM_LBUTTONDBLCLK)
1210         WIN_MSG(WM_RBUTTONDOWN)
1211         WIN_MSG(WM_RBUTTONUP)
1212         WIN_MSG(WM_RBUTTONDBLCLK)
1213         WIN_MSG(WM_MBUTTONDOWN)
1214         WIN_MSG(WM_MBUTTONUP)
1215         WIN_MSG(WM_MBUTTONDBLCLK)
1216         WIN_MSG(WM_XBUTTONDBLCLK)
1217         WIN_MSG(WM_XBUTTONDOWN)
1218         WIN_MSG(WM_XBUTTONUP)
1219         WIN_MSG(WM_MOUSEWHEEL)
1220         WIN_MSG(WM_MOUSEHWHEEL)
1221         WIN_MSG(WM_PARENTNOTIFY)
1222         WIN_MSG(WM_ENTERMENULOOP)
1223         WIN_MSG(WM_EXITMENULOOP)
1224         WIN_MSG(WM_NEXTMENU)
1225         WIN_MSG(WM_SIZING)
1226         WIN_MSG(WM_CAPTURECHANGED)
1227         WIN_MSG(WM_MOVING)
1228         WIN_MSG(WM_POWERBROADCAST)
1229         WIN_MSG(WM_DEVICECHANGE)
1230         WIN_MSG(WM_MDICREATE)
1231         WIN_MSG(WM_MDIDESTROY)
1232         WIN_MSG(WM_MDIACTIVATE)
1233         WIN_MSG(WM_MDIRESTORE)
1234         WIN_MSG(WM_MDINEXT)
1235         WIN_MSG(WM_MDIMAXIMIZE)
1236         WIN_MSG(WM_MDITILE)
1237         WIN_MSG(WM_MDICASCADE)
1238         WIN_MSG(WM_MDIICONARRANGE)
1239         WIN_MSG(WM_MDIGETACTIVE)
1240         WIN_MSG(WM_MDISETMENU)
1241         WIN_MSG(WM_ENTERSIZEMOVE)
1242         WIN_MSG(WM_EXITSIZEMOVE)
1243         WIN_MSG(WM_DROPFILES)
1244         WIN_MSG(WM_MDIREFRESHMENU)
1245         WIN_MSG(WM_IME_SETCONTEXT)
1246         WIN_MSG(WM_IME_NOTIFY)
1247         WIN_MSG(WM_IME_CONTROL)
1248         WIN_MSG(WM_IME_COMPOSITIONFULL)
1249         WIN_MSG(WM_IME_SELECT)
1250         WIN_MSG(WM_IME_CHAR)
1251         FMT_MSG(WM_IME_REQUEST)
1252         WIN_MSG(WM_IME_KEYDOWN)
1253         WIN_MSG(WM_IME_KEYUP)
1254         FMT_MSG(0x02A1, "WM_MOUSEHOVER")
1255         FMT_MSG(0x02A3, "WM_MOUSELEAVE")
1256         WIN_MSG(WM_CUT)
1257         WIN_MSG(WM_COPY)
1258         WIN_MSG(WM_PASTE)
1259         WIN_MSG(WM_CLEAR)
1260         WIN_MSG(WM_UNDO)
1261         WIN_MSG(WM_RENDERFORMAT)
1262         WIN_MSG(WM_RENDERALLFORMATS)
1263         WIN_MSG(WM_DESTROYCLIPBOARD)
1264         WIN_MSG(WM_DRAWCLIPBOARD)
1265         WIN_MSG(WM_PAINTCLIPBOARD)
1266         WIN_MSG(WM_VSCROLLCLIPBOARD)
1267         WIN_MSG(WM_SIZECLIPBOARD)
1268         WIN_MSG(WM_ASKCBFORMATNAME)
1269         WIN_MSG(WM_CHANGECBCHAIN)
1270         WIN_MSG(WM_HSCROLLCLIPBOARD)
1271         WIN_MSG(WM_QUERYNEWPALETTE)
1272         WIN_MSG(WM_PALETTEISCHANGING)
1273         WIN_MSG(WM_PALETTECHANGED)
1274         WIN_MSG(WM_HOTKEY)
1275         WIN_MSG(WM_PRINT)
1276         WIN_MSG(WM_PRINTCLIENT)
1277         WIN_MSG(WM_HANDHELDFIRST)
1278         WIN_MSG(WM_HANDHELDLAST)
1279         WIN_MSG(WM_AFXFIRST)
1280         WIN_MSG(WM_AFXLAST)
1281         WIN_MSG(WM_PENWINFIRST)
1282         WIN_MSG(WM_PENWINLAST)
1283         WIN_MSG(WM_AWT_COMPONENT_CREATE)
1284         WIN_MSG(WM_AWT_DESTROY_WINDOW)
1285         WIN_MSG(WM_AWT_MOUSEENTER)
1286         WIN_MSG(WM_AWT_MOUSEEXIT)
1287         WIN_MSG(WM_AWT_COMPONENT_SHOW)
1288         WIN_MSG(WM_AWT_COMPONENT_HIDE)
1289         WIN_MSG(WM_AWT_COMPONENT_SETFOCUS)
1290         WIN_MSG(WM_AWT_WINDOW_SETACTIVE)
1291         WIN_MSG(WM_AWT_LIST_SETMULTISELECT)
1292         WIN_MSG(WM_AWT_HANDLE_EVENT)
1293         WIN_MSG(WM_AWT_PRINT_COMPONENT)
1294         WIN_MSG(WM_AWT_RESHAPE_COMPONENT)
1295         WIN_MSG(WM_AWT_SETALWAYSONTOP)
1296         WIN_MSG(WM_AWT_BEGIN_VALIDATE)
1297         WIN_MSG(WM_AWT_END_VALIDATE)
1298         WIN_MSG(WM_AWT_FORWARD_CHAR)
1299         WIN_MSG(WM_AWT_FORWARD_BYTE)
1300         WIN_MSG(WM_AWT_SET_SCROLL_INFO)
1301         WIN_MSG(WM_AWT_CREATECONTEXT)
1302         WIN_MSG(WM_AWT_DESTROYCONTEXT)
1303         WIN_MSG(WM_AWT_ASSOCIATECONTEXT)
1304         WIN_MSG(WM_AWT_GET_DEFAULT_IME_HANDLER)
1305         WIN_MSG(WM_AWT_HANDLE_NATIVE_IME_EVENT)
1306         WIN_MSG(WM_AWT_PRE_KEYDOWN)
1307         WIN_MSG(WM_AWT_PRE_KEYUP)
1308         WIN_MSG(WM_AWT_PRE_SYSKEYDOWN)
1309         WIN_MSG(WM_AWT_PRE_SYSKEYUP)
1310         WIN_MSG(WM_AWT_ENDCOMPOSITION,)
1311         WIN_MSG(WM_AWT_DISPOSE,)
1312         WIN_MSG(WM_AWT_DELETEOBJECT,)
1313         WIN_MSG(WM_AWT_SETCONVERSIONSTATUS,)
1314         WIN_MSG(WM_AWT_GETCONVERSIONSTATUS,)
1315         WIN_MSG(WM_AWT_SETOPENSTATUS,)
1316         WIN_MSG(WM_AWT_GETOPENSTATUS)
1317         WIN_MSG(WM_AWT_ACTIVATEKEYBOARDLAYOUT)
1318         WIN_MSG(WM_AWT_OPENCANDIDATEWINDOW)
1319         WIN_MSG(WM_AWT_DLG_SHOWMODAL,)
1320         WIN_MSG(WM_AWT_DLG_ENDMODAL,)
1321         WIN_MSG(WM_AWT_SETCURSOR,)
1322         WIN_MSG(WM_AWT_WAIT_FOR_SINGLE_OBJECT,)
1323         WIN_MSG(WM_AWT_INVOKE_METHOD,)
1324         WIN_MSG(WM_AWT_INVOKE_VOID_METHOD,)
1325         WIN_MSG(WM_AWT_EXECUTE_SYNC,)
1326         WIN_MSG(WM_AWT_CURSOR_SYNC)
1327         WIN_MSG(WM_AWT_GETDC)
1328         WIN_MSG(WM_AWT_RELEASEDC)
1329         WIN_MSG(WM_AWT_RELEASE_ALL_DCS)
1330         WIN_MSG(WM_AWT_SHOWCURSOR)
1331         WIN_MSG(WM_AWT_HIDECURSOR)
1332         WIN_MSG(WM_AWT_CREATE_PRINTED_PIXELS)
1333         WIN_MSG(WM_AWT_OBJECTLISTCLEANUP)
1334         default:
1335             sprintf(szBuf, "0x%8.8x(%s):Unknown message 0x%8.8x\n",
1336                 hwnd, szComment, message);
1337             break;
1338     }
1339     printf(szBuf);
1340 }
1341 
1342 #endif /* SPY_MESSAGES */
1343 
1344 /*
1345  * Dispatch messages for this window class--general component
1346  */
1347 LRESULT AwtComponent::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
1348 {
1349     CounterHelper ch(&m_MessagesProcessing);
1350 
1351     JNILocalFrame lframe(AwtToolkit::GetEnv(), 10);
1352     SpyWinMessage(GetHWnd(), message,
1353         (message == WM_AWT_RELEASE_ALL_DCS) ? TEXT("Disposed Component") : GetClassName());
1354 
1355     LRESULT retValue = 0;
1356     MsgRouting mr = mrDoDefault;
1357     AwtToolkit::GetInstance().eventNumber++;
1358 
1359     static BOOL ignoreNextLBTNUP = FALSE; //Ignore next LBUTTONUP msg?
1360 
1361     lastMessage = message;
1362 
1363     if (message == WmAwtIsComponent) {
1364     // special message to identify AWT HWND's without using
1365     // resource hogging ::SetProp
1366         return (LRESULT)TRUE;
1367     }
1368 
1369     DWORD curPos = 0;
1370 
1371     UINT switchMessage = message;
1372     switch (switchMessage) {
1373       case WM_AWT_GETDC:
1374       {
1375             HDC hDC;
1376             // First, release the DCs scheduled for deletion
1377             ReleaseDCList(GetHWnd(), passiveDCList);
1378 
1379             GetDCReturnStruct *returnStruct = new GetDCReturnStruct;
1380             returnStruct->gdiLimitReached = FALSE;
1381             if (AwtGDIObject::IncrementIfAvailable()) {
1382                 hDC = ::GetDCEx(GetHWnd(), NULL,
1383                                 DCX_CACHE | DCX_CLIPCHILDREN |
1384                                 DCX_CLIPSIBLINGS);
1385                 if (hDC != NULL) {
1386                     // Add new DC to list of DC's associated with this Component
1387                     activeDCList.AddDC(hDC, GetHWnd());
1388                 } else {
1389                     // Creation failed; decrement counter in AwtGDIObject
1390                     AwtGDIObject::Decrement();
1391                 }
1392             } else {
1393                 hDC = NULL;
1394                 returnStruct->gdiLimitReached = TRUE;
1395             }
1396             returnStruct->hDC = hDC;
1397             retValue = (LRESULT)returnStruct;
1398             mr = mrConsume;
1399             break;
1400       }
1401       case WM_AWT_RELEASEDC:
1402       {
1403             HDC hDC = (HDC)wParam;
1404             MoveDCToPassiveList(hDC, GetHWnd());
1405             ReleaseDCList(GetHWnd(), passiveDCList);
1406             mr = mrConsume;
1407             break;
1408       }
1409       case WM_AWT_RELEASE_ALL_DCS:
1410       {
1411             // Called during Component destruction.  Gets current list of
1412             // DC's associated with Component and releases each DC.
1413             ReleaseDCList(GetHWnd(), activeDCList);
1414             ReleaseDCList(GetHWnd(), passiveDCList);
1415             mr = mrConsume;
1416             break;
1417       }
1418       case WM_AWT_SHOWCURSOR:
1419           ::ShowCursor(TRUE);
1420           break;
1421       case WM_AWT_HIDECURSOR:
1422           ::ShowCursor(FALSE);
1423           break;
1424       case WM_CREATE: mr = WmCreate(); break;
1425       case WM_CLOSE:      mr = WmClose(); break;
1426       case WM_DESTROY:    mr = WmDestroy(); break;
1427       case WM_NCDESTROY:  mr = WmNcDestroy(); break;
1428 
1429       case WM_ERASEBKGND:
1430           mr = WmEraseBkgnd((HDC)wParam, *(BOOL*)&retValue); break;
1431       case WM_PAINT:
1432           CheckFontSmoothingSettings(GetHWnd());
1433           /* Set draw state */
1434           SetDrawState(GetDrawState() | JAWT_LOCK_CLIP_CHANGED);
1435           mr = WmPaint((HDC)wParam);
1436           break;
1437 
1438       case WM_GETMINMAXINFO:
1439           mr = WmGetMinMaxInfo((LPMINMAXINFO)lParam);
1440           break;
1441 
1442       case WM_WINDOWPOSCHANGING:
1443       {
1444           // We process this message so that we can synchronize access to
1445           // a moving window.  The Scale/Blt functions in Win32BlitLoops
1446           // take the same windowMoveLock to ensure that a window is not
1447           // moving while we are trying to copy pixels into it.
1448           WINDOWPOS *lpPosInfo = (WINDOWPOS *)lParam;
1449           if ((lpPosInfo->flags & (SWP_NOMOVE | SWP_NOSIZE)) !=
1450               (SWP_NOMOVE | SWP_NOSIZE))
1451           {
1452               // Move or Size command.
1453               // Windows tends to send erroneous events that the window
1454               // is about to move when the coordinates are exactly the
1455               // same as the last time.  This can cause problems with
1456               // our windowMoveLock CriticalSection because we enter it
1457               // here and never get to WM_WINDOWPOSCHANGED to release it.
1458               // So make sure this is a real move/size event before bothering
1459               // to grab the critical section.
1460               BOOL takeLock = FALSE;
1461               if (!(lpPosInfo->flags & SWP_NOMOVE) &&
1462                   ((windowMoveLockPosX != lpPosInfo->x) ||
1463                    (windowMoveLockPosY != lpPosInfo->y)))
1464               {
1465                   // Real move event
1466                   takeLock = TRUE;
1467                   windowMoveLockPosX = lpPosInfo->x;
1468                   windowMoveLockPosY = lpPosInfo->y;
1469               }
1470               if (!(lpPosInfo->flags & SWP_NOSIZE) &&
1471                   ((windowMoveLockPosCX != lpPosInfo->cx) ||
1472                    (windowMoveLockPosCY != lpPosInfo->cy)))
1473               {
1474                   // Real size event
1475                   takeLock = TRUE;
1476                   windowMoveLockPosCX = lpPosInfo->cx;
1477                   windowMoveLockPosCY = lpPosInfo->cy;
1478               }
1479               if (takeLock) {
1480                   if (!windowMoveLockHeld) {
1481                       windowMoveLock.Enter();
1482                       windowMoveLockHeld = TRUE;
1483                   }
1484               }
1485           }
1486           mr = WmWindowPosChanging(lParam);
1487           break;
1488       }
1489       case WM_WINDOWPOSCHANGED:
1490       {
1491           // Release lock grabbed in the POSCHANGING message
1492           if (windowMoveLockHeld) {
1493               windowMoveLockHeld = FALSE;
1494               windowMoveLock.Leave();
1495           }
1496           mr = WmWindowPosChanged(lParam);
1497           break;
1498       }
1499       case WM_MOVE: {
1500           RECT r;
1501           ::GetWindowRect(GetHWnd(), &r);
1502           mr = WmMove(r.left, r.top);
1503           break;
1504       }
1505       case WM_SIZE:
1506       {
1507           RECT r;
1508           // fix 4128317 : use GetClientRect for full 32-bit int precision and
1509           // to avoid negative client area dimensions overflowing 16-bit params - robi
1510           ::GetClientRect( GetHWnd(), &r );
1511           mr = WmSize(static_cast<UINT>(wParam), r.right - r.left, r.bottom - r.top);
1512           //mr = WmSize(wParam, LOWORD(lParam), HIWORD(lParam));
1513           SetCompositionWindow(r);
1514           break;
1515       }
1516       case WM_SIZING:
1517           mr = WmSizing();
1518           break;
1519       case WM_SHOWWINDOW:
1520           mr = WmShowWindow(static_cast<BOOL>(wParam),
1521                             static_cast<UINT>(lParam)); break;
1522       case WM_SYSCOMMAND:
1523           mr = WmSysCommand(static_cast<UINT>(wParam & 0xFFF0),
1524                             GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
1525           break;
1526       case WM_ENTERSIZEMOVE:
1527           mr = WmEnterSizeMove();
1528           break;
1529       case WM_EXITSIZEMOVE:
1530           mr = WmExitSizeMove();
1531           break;
1532       // Bug #4039858 (Selecting menu item causes bogus mouse click event)
1533       case WM_ENTERMENULOOP:
1534           mr = WmEnterMenuLoop((BOOL)wParam);
1535           sm_bMenuLoop = TRUE;
1536           // we need to release grab if menu is shown
1537           if (AwtWindow::GetGrabbedWindow() != NULL) {
1538               AwtWindow::GetGrabbedWindow()->Ungrab();
1539           }
1540           break;
1541       case WM_EXITMENULOOP:
1542           mr = WmExitMenuLoop((BOOL)wParam);
1543           sm_bMenuLoop = FALSE;
1544           break;
1545 
1546       // We don't expect any focus messages on non-proxy component,
1547       // except those that came from Java.
1548       case WM_SETFOCUS:
1549           if (sm_inSynthesizeFocus) {
1550               mr = WmSetFocus((HWND)wParam);
1551           } else {
1552               mr = mrConsume;
1553           }
1554           break;
1555       case WM_KILLFOCUS:
1556           if (sm_inSynthesizeFocus) {
1557               mr = WmKillFocus((HWND)wParam);
1558           } else {
1559               mr = mrConsume;
1560           }
1561           break;
1562       case WM_ACTIVATE: {
1563           UINT nState = LOWORD(wParam);
1564           BOOL fMinimized = (BOOL)HIWORD(wParam);
1565           mr = mrConsume;
1566 
1567           if (!sm_suppressFocusAndActivation &&
1568               (!fMinimized || (nState == WA_INACTIVE)))
1569           {
1570               mr = WmActivate(nState, fMinimized, (HWND)lParam);
1571 
1572               // When the window is deactivated, send WM_IME_ENDCOMPOSITION
1573               // message to deactivate the composition window so that
1574               // it won't receive keyboard input focus.
1575               HIMC hIMC;
1576               HWND hwnd = ImmGetHWnd();
1577               if ((hIMC = ImmGetContext(hwnd)) != NULL) {
1578                   ImmReleaseContext(hwnd, hIMC);
1579                   DefWindowProc(WM_IME_ENDCOMPOSITION, 0, 0);
1580               }
1581           }
1582           break;
1583       }
1584       case WM_MOUSEACTIVATE: {
1585           AwtWindow *window = GetContainer();
1586           if (window && window->IsFocusableWindow()) {
1587               // AWT/Swing will later request focus to a proper component
1588               // on handling the Java mouse event. Anyway, we have to
1589               // activate the window here as it works both for AWT & Swing.
1590               // Do it in our own fassion,
1591               window->AwtSetActiveWindow(TRUE, LOWORD(lParam)/*hittest*/);
1592           }
1593           mr = mrConsume;
1594           retValue = MA_NOACTIVATE;
1595           break;
1596       }
1597       case WM_CTLCOLORMSGBOX:
1598       case WM_CTLCOLOREDIT:
1599       case WM_CTLCOLORLISTBOX:
1600       case WM_CTLCOLORBTN:
1601       case WM_CTLCOLORDLG:
1602       case WM_CTLCOLORSCROLLBAR:
1603       case WM_CTLCOLORSTATIC:
1604           mr = WmCtlColor((HDC)wParam, (HWND)lParam,
1605                           message-WM_CTLCOLORMSGBOX+CTLCOLOR_MSGBOX,
1606                           *(HBRUSH*)&retValue);
1607           break;
1608       case WM_HSCROLL:
1609           mr = WmHScroll(LOWORD(wParam), HIWORD(wParam), (HWND)lParam);
1610           break;
1611       case WM_VSCROLL:
1612           mr = WmVScroll(LOWORD(wParam), HIWORD(wParam), (HWND)lParam);
1613           break;
1614       // 4664415: We're seeing a WM_LBUTTONUP when the user releases the
1615       // mouse button after a WM_NCLBUTTONDBLCLK.  We want to ignore this
1616       // WM_LBUTTONUP, so we set a flag in WM_NCLBUTTONDBLCLK and look for the
1617       // flag on a WM_LBUTTONUP.  -bchristi
1618       case WM_NCLBUTTONDBLCLK:
1619           mr = WmNcMouseDown(wParam, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), LEFT_BUTTON | DBL_CLICK);
1620           if (mr == mrDoDefault) {
1621               ignoreNextLBTNUP = TRUE;
1622           }
1623           break;
1624       case WM_NCLBUTTONDOWN:
1625           mr = WmNcMouseDown(wParam, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), LEFT_BUTTON);
1626           ignoreNextLBTNUP = FALSE;
1627           break;
1628       case WM_NCLBUTTONUP:
1629           mr = WmNcMouseUp(wParam, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), LEFT_BUTTON);
1630           break;
1631       case WM_NCRBUTTONDOWN:
1632            mr = WmNcMouseDown(wParam, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), RIGHT_BUTTON);
1633            break;
1634       case WM_LBUTTONUP:
1635           if (ignoreNextLBTNUP) {
1636               ignoreNextLBTNUP = FALSE;
1637               return mrDoDefault;
1638           }
1639           //fall-through
1640       case WM_LBUTTONDOWN:
1641           ignoreNextLBTNUP = FALSE;
1642           //fall-through
1643       case WM_LBUTTONDBLCLK:
1644       case WM_RBUTTONDOWN:
1645       case WM_RBUTTONDBLCLK:
1646       case WM_RBUTTONUP:
1647       case WM_MBUTTONDOWN:
1648       case WM_MBUTTONDBLCLK:
1649       case WM_MBUTTONUP:
1650       case WM_XBUTTONDBLCLK:
1651       case WM_XBUTTONDOWN:
1652       case WM_XBUTTONUP:
1653       case WM_MOUSEMOVE:
1654       case WM_MOUSEWHEEL:
1655       case WM_MOUSEHWHEEL:
1656       case WM_AWT_MOUSEENTER:
1657       case WM_AWT_MOUSEEXIT:
1658           curPos = ::GetMessagePos();
1659           POINT myPos;
1660           myPos.x = GET_X_LPARAM(curPos);
1661           myPos.y = GET_Y_LPARAM(curPos);
1662           ::ScreenToClient(GetHWnd(), &myPos);
1663           switch(switchMessage) {
1664           case WM_AWT_MOUSEENTER:
1665               mr = WmMouseEnter(static_cast<UINT>(wParam), myPos.x, myPos.y);
1666               break;
1667           case WM_LBUTTONDOWN:
1668           case WM_LBUTTONDBLCLK:
1669               mr = WmMouseDown(static_cast<UINT>(wParam), myPos.x, myPos.y,
1670                                LEFT_BUTTON);
1671               break;
1672           case WM_LBUTTONUP:
1673               mr = WmMouseUp(static_cast<UINT>(wParam), myPos.x, myPos.y,
1674                              LEFT_BUTTON);
1675               break;
1676           case WM_MOUSEMOVE:
1677               mr = WmMouseMove(static_cast<UINT>(wParam), myPos.x, myPos.y);
1678               break;
1679           case WM_MBUTTONDOWN:
1680           case WM_MBUTTONDBLCLK:
1681               mr = WmMouseDown(static_cast<UINT>(wParam), myPos.x, myPos.y,
1682                                MIDDLE_BUTTON);
1683               break;
1684           case WM_XBUTTONDOWN:
1685           case WM_XBUTTONDBLCLK:
1686               if (AwtToolkit::GetInstance().areExtraMouseButtonsEnabled()) {
1687                   if (HIWORD(wParam) == 1) {
1688                       mr = WmMouseDown(static_cast<UINT>(wParam), myPos.x, myPos.y,
1689                                        X1_BUTTON);
1690                   }
1691                   if (HIWORD(wParam) == 2) {
1692                       mr = WmMouseDown(static_cast<UINT>(wParam), myPos.x, myPos.y,
1693                                        X2_BUTTON);
1694                   }
1695               }
1696               break;
1697           case WM_XBUTTONUP:
1698               if (AwtToolkit::GetInstance().areExtraMouseButtonsEnabled()) {
1699                   if (HIWORD(wParam) == 1) {
1700                       mr = WmMouseUp(static_cast<UINT>(wParam), myPos.x, myPos.y,
1701                                      X1_BUTTON);
1702                   }
1703                   if (HIWORD(wParam) == 2) {
1704                       mr = WmMouseUp(static_cast<UINT>(wParam), myPos.x, myPos.y,
1705                                      X2_BUTTON);
1706                   }
1707               }
1708               break;
1709           case WM_RBUTTONDOWN:
1710           case WM_RBUTTONDBLCLK:
1711               mr = WmMouseDown(static_cast<UINT>(wParam), myPos.x, myPos.y,
1712                                RIGHT_BUTTON);
1713               break;
1714           case WM_RBUTTONUP:
1715               mr = WmMouseUp(static_cast<UINT>(wParam), myPos.x, myPos.y,
1716                              RIGHT_BUTTON);
1717               break;
1718           case WM_MBUTTONUP:
1719               mr = WmMouseUp(static_cast<UINT>(wParam), myPos.x, myPos.y,
1720                              MIDDLE_BUTTON);
1721               break;
1722           case WM_AWT_MOUSEEXIT:
1723               mr = WmMouseExit(static_cast<UINT>(wParam), myPos.x, myPos.y);
1724               break;
1725           case WM_MOUSEWHEEL:
1726           case WM_MOUSEHWHEEL:
1727               mr = WmMouseWheel(GET_KEYSTATE_WPARAM(wParam),
1728                                 GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam),
1729                                 GET_WHEEL_DELTA_WPARAM(wParam),
1730                                 switchMessage == WM_MOUSEHWHEEL);
1731               break;
1732           }
1733           break;
1734       case WM_TOUCH:
1735           WmTouch(wParam, lParam);
1736           break;
1737       case WM_SETCURSOR:
1738           mr = mrDoDefault;
1739           if (LOWORD(lParam) == HTCLIENT) {
1740               if (AwtComponent* comp =
1741                                     AwtComponent::GetComponent((HWND)wParam)) {
1742                   AwtCursor::UpdateCursor(comp);
1743                   mr = mrConsume;
1744               }
1745           }
1746           break;
1747 
1748       case WM_KEYDOWN:
1749           mr = WmKeyDown(static_cast<UINT>(wParam),
1750                          LOWORD(lParam), HIWORD(lParam), FALSE);
1751           break;
1752       case WM_KEYUP:
1753           mr = WmKeyUp(static_cast<UINT>(wParam),
1754                        LOWORD(lParam), HIWORD(lParam), FALSE);
1755           break;
1756       case WM_SYSKEYDOWN:
1757           mr = WmKeyDown(static_cast<UINT>(wParam),
1758                          LOWORD(lParam), HIWORD(lParam), TRUE);
1759           break;
1760       case WM_SYSKEYUP:
1761           mr = WmKeyUp(static_cast<UINT>(wParam),
1762                        LOWORD(lParam), HIWORD(lParam), TRUE);
1763           break;
1764       case WM_IME_SETCONTEXT:
1765           // lParam is passed as pointer and it can be modified.
1766           mr = WmImeSetContext(static_cast<BOOL>(wParam), &lParam);
1767           CallProxyDefWindowProc(message, wParam, lParam, retValue, mr);
1768           break;
1769       case WM_IME_NOTIFY:
1770           mr = WmImeNotify(wParam, lParam);
1771           CallProxyDefWindowProc(message, wParam, lParam, retValue, mr);
1772           break;
1773       case WM_IME_STARTCOMPOSITION:
1774           mr = WmImeStartComposition();
1775           CallProxyDefWindowProc(message, wParam, lParam, retValue, mr);
1776           break;
1777       case WM_IME_ENDCOMPOSITION:
1778           mr = WmImeEndComposition();
1779           CallProxyDefWindowProc(message, wParam, lParam, retValue, mr);
1780           break;
1781       case WM_IME_COMPOSITION: {
1782           WORD dbcschar = static_cast<WORD>(wParam);
1783           mr = WmImeComposition(dbcschar, lParam);
1784           CallProxyDefWindowProc(message, wParam, lParam, retValue, mr);
1785           break;
1786       }
1787       case WM_IME_CONTROL:
1788       case WM_IME_COMPOSITIONFULL:
1789       case WM_IME_SELECT:
1790       case WM_IME_KEYUP:
1791       case WM_IME_KEYDOWN:
1792       case WM_IME_REQUEST:
1793           CallProxyDefWindowProc(message, wParam, lParam, retValue, mr);
1794           break;
1795       case WM_CHAR:
1796           mr = WmChar(static_cast<UINT>(wParam),
1797                       LOWORD(lParam), HIWORD(lParam), FALSE);
1798           break;
1799       case WM_SYSCHAR:
1800           mr = WmChar(static_cast<UINT>(wParam),
1801                       LOWORD(lParam), HIWORD(lParam), TRUE);
1802           break;
1803       case WM_IME_CHAR:
1804           mr = WmIMEChar(static_cast<UINT>(wParam),
1805                          LOWORD(lParam), HIWORD(lParam), FALSE);
1806           break;
1807 
1808       case WM_INPUTLANGCHANGEREQUEST: {
1809           DTRACE_PRINTLN4("WM_INPUTLANGCHANGEREQUEST: hwnd = 0x%X (%s);"//
1810                           "0x%08X -> 0x%08X",
1811                           GetHWnd(), GetClassName(),
1812                           (UINT_PTR)GetKeyboardLayout(), (UINT_PTR)lParam);
1813           // 4267428: make sure keyboard layout is turned undead.
1814           static BYTE keyboardState[AwtToolkit::KB_STATE_SIZE];
1815           AwtToolkit::GetKeyboardState(keyboardState);
1816           WORD ignored;
1817           ::ToAsciiEx(VK_SPACE, ::MapVirtualKey(VK_SPACE, 0),
1818                       keyboardState, &ignored, 0, GetKeyboardLayout());
1819 
1820           // Set this flag to block ActivateKeyboardLayout from
1821           // WInputMethod.activate()
1822           g_bUserHasChangedInputLang = TRUE;
1823           CallProxyDefWindowProc(message, wParam, lParam, retValue, mr);
1824           break;
1825       }
1826       case WM_INPUTLANGCHANGE:
1827           DTRACE_PRINTLN3("WM_INPUTLANGCHANGE: hwnd = 0x%X (%s);"//
1828                           "new = 0x%08X",
1829                           GetHWnd(), GetClassName(), (UINT)lParam);
1830           mr = WmInputLangChange(static_cast<UINT>(wParam), reinterpret_cast<HKL>(lParam));
1831           g_bUserHasChangedInputLang = TRUE;
1832           CallProxyDefWindowProc(message, wParam, lParam, retValue, mr);
1833           // should return non-zero if we process this message
1834           retValue = 1;
1835           break;
1836 
1837       case WM_AWT_FORWARD_CHAR:
1838           mr = WmForwardChar(LOWORD(wParam), lParam, HIWORD(wParam));
1839           break;
1840 
1841       case WM_AWT_FORWARD_BYTE:
1842           mr = HandleEvent( (MSG *) lParam, (BOOL) wParam);
1843           break;
1844 
1845       case WM_PASTE:
1846           mr = WmPaste();
1847           break;
1848       case WM_TIMER:
1849           mr = WmTimer(wParam);
1850           break;
1851 
1852       case WM_COMMAND:
1853           mr = WmCommand(LOWORD(wParam), (HWND)lParam, HIWORD(wParam));
1854           break;
1855       case WM_COMPAREITEM:
1856           mr = WmCompareItem(static_cast<UINT>(wParam),
1857                              *(COMPAREITEMSTRUCT*)lParam, retValue);
1858           break;
1859       case WM_DELETEITEM:
1860           mr = WmDeleteItem(static_cast<UINT>(wParam),
1861                             *(DELETEITEMSTRUCT*)lParam);
1862           break;
1863       case WM_DRAWITEM:
1864           mr = WmDrawItem(static_cast<UINT>(wParam),
1865                           *(DRAWITEMSTRUCT*)lParam);
1866           break;
1867       case WM_MEASUREITEM:
1868           mr = WmMeasureItem(static_cast<UINT>(wParam),
1869                              *(MEASUREITEMSTRUCT*)lParam);
1870           break;
1871 
1872       case WM_AWT_HANDLE_EVENT:
1873           mr = HandleEvent( (MSG *) lParam, (BOOL) wParam);
1874           break;
1875 
1876       case WM_PRINT:
1877           mr = WmPrint((HDC)wParam, lParam);
1878           break;
1879       case WM_PRINTCLIENT:
1880           mr = WmPrintClient((HDC)wParam, lParam);
1881           break;
1882 
1883       case WM_NCCALCSIZE:
1884           mr = WmNcCalcSize((BOOL)wParam, (LPNCCALCSIZE_PARAMS)lParam,
1885                             retValue);
1886           break;
1887       case WM_NCPAINT:
1888           mr = WmNcPaint((HRGN)wParam);
1889           break;
1890       case WM_NCHITTEST:
1891           mr = WmNcHitTest(LOWORD(lParam), HIWORD(lParam), retValue);
1892           break;
1893 
1894       case WM_AWT_RESHAPE_COMPONENT: {
1895           RECT* r = (RECT*)lParam;
1896           WPARAM checkEmbedded = wParam;
1897           if (checkEmbedded == CHECK_EMBEDDED && IsEmbeddedFrame()) {
1898               ::OffsetRect(r, -r->left, -r->top);
1899           }
1900           Reshape(r->left, r->top, r->right - r->left, r->bottom - r->top);
1901           delete r;
1902           mr = mrConsume;
1903           break;
1904       }
1905 
1906       case WM_AWT_SETALWAYSONTOP: {
1907         AwtWindow* w = (AwtWindow*)lParam;
1908         BOOL value = (BOOL)wParam;
1909         UINT flags = SWP_NOMOVE | SWP_NOSIZE;
1910         // transient windows shouldn't change the owner window's position in the z-order
1911         if (w->IsRetainingHierarchyZOrder()) {
1912             flags |= SWP_NOOWNERZORDER;
1913         }
1914         ::SetWindowPos(w->GetHWnd(), (value != 0 ? HWND_TOPMOST : HWND_NOTOPMOST),
1915                        0,0,0,0, flags);
1916         break;
1917       }
1918 
1919       case WM_AWT_BEGIN_VALIDATE:
1920           BeginValidate();
1921           mr = mrConsume;
1922           break;
1923       case WM_AWT_END_VALIDATE:
1924           EndValidate();
1925           mr = mrConsume;
1926           break;
1927 
1928       case WM_PALETTEISCHANGING:
1929           mr = WmPaletteIsChanging((HWND)wParam);
1930           mr = mrDoDefault;
1931           break;
1932       case WM_QUERYNEWPALETTE:
1933           mr = WmQueryNewPalette(retValue);
1934           break;
1935       case WM_PALETTECHANGED:
1936           mr = WmPaletteChanged((HWND)wParam);
1937           break;
1938       case WM_STYLECHANGED:
1939           mr = WmStyleChanged(static_cast<int>(wParam), (LPSTYLESTRUCT)lParam);
1940           break;
1941       case WM_SETTINGCHANGE:
1942           CheckFontSmoothingSettings(NULL);
1943           mr = WmSettingChange(static_cast<UINT>(wParam), (LPCTSTR)lParam);
1944           break;
1945       case WM_CONTEXTMENU:
1946           mr = WmContextMenu((HWND)wParam,
1947                              GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
1948           break;
1949 
1950           /*
1951            * These messages are used to route Win32 calls to the
1952            * creating thread, since these calls fail unless executed
1953            * there.
1954            */
1955       case WM_AWT_COMPONENT_SHOW:
1956           Show();
1957           mr = mrConsume;
1958           break;
1959       case WM_AWT_COMPONENT_HIDE:
1960           Hide();
1961           mr = mrConsume;
1962           break;
1963 
1964       case WM_AWT_COMPONENT_SETFOCUS:
1965           if ((BOOL)wParam) {
1966               retValue = SynthesizeWmSetFocus(GetHWnd(), NULL);
1967           } else {
1968               retValue = SynthesizeWmKillFocus(GetHWnd(), NULL);
1969           }
1970           mr = mrConsume;
1971           break;
1972       case WM_AWT_WINDOW_SETACTIVE:
1973           retValue = (LRESULT)((AwtWindow*)this)->AwtSetActiveWindow((BOOL)wParam);
1974           mr = mrConsume;
1975           break;
1976 
1977       case WM_AWT_SET_SCROLL_INFO: {
1978           SCROLLINFO *si = (SCROLLINFO *) lParam;
1979           ::SetScrollInfo(GetHWnd(), (int) wParam, si, TRUE);
1980           delete si;
1981           mr = mrConsume;
1982           break;
1983       }
1984       case WM_AWT_CREATE_PRINTED_PIXELS: {
1985           CreatePrintedPixelsStruct* cpps = (CreatePrintedPixelsStruct*)wParam;
1986           SIZE loc = { cpps->srcx, cpps->srcy };
1987           SIZE size = { cpps->srcw, cpps->srch };
1988           retValue = (LRESULT)CreatePrintedPixels(loc, size, cpps->alpha);
1989           mr = mrConsume;
1990           break;
1991       }
1992       case WM_UNDOCUMENTED_CLICKMENUBAR:
1993       {
1994           if (::IsWindow(AwtWindow::GetModalBlocker(GetHWnd()))) {
1995               mr = mrConsume;
1996           }
1997       }
1998     }
1999 
2000     /*
2001      * If not a specific Consume, it was a specific DoDefault, or a
2002      * PassAlong (since the default is the next in chain), then call the
2003      * default proc.
2004      */
2005     if (mr != mrConsume) {
2006         retValue = DefWindowProc(message, wParam, lParam);
2007     }
2008 
2009     return retValue;
2010 }
2011 /*
2012  * Call this instance's default window proc, or if none set, call the stock
2013  * Window's one.
2014  */
2015 LRESULT AwtComponent::DefWindowProc(UINT msg, WPARAM wParam, LPARAM lParam)
2016 {
2017     return ComCtl32Util::GetInstance().DefWindowProc(m_DefWindowProc, GetHWnd(), msg, wParam, lParam);
2018 }
2019 
2020 /*
2021  * This message should only be received when a window is destroyed by
2022  * Windows, and not Java.  Window termination has been reworked so
2023  * this method should never be called during termination.
2024  */
2025 MsgRouting AwtComponent::WmDestroy()
2026 {
2027     return mrConsume;
2028 }
2029 
2030 /*
2031  * This message should only be received when a window is destroyed by
2032  * Windows, and not Java. It is sent only after child windows were destroyed.
2033  */
2034 MsgRouting AwtComponent::WmNcDestroy()
2035 {
2036     if (m_peerObject != NULL) { // is not being terminating
2037         // Stay in this handler until AwtComponent::Dispose is called.
2038         m_bPauseDestroy = TRUE;
2039 
2040         JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
2041         // Post invocation event for WObjectPeer.dispose to EDT
2042         env->CallVoidMethod(m_peerObject, AwtComponent::disposeLaterMID);
2043         // Wait until AwtComponent::Dispose is called
2044         AwtToolkit::GetInstance().PumpToDestroy(this);
2045     }
2046 
2047     return mrConsume;
2048 }
2049 
2050 MsgRouting AwtComponent::WmGetMinMaxInfo(LPMINMAXINFO lpmmi)
2051 {
2052     return mrDoDefault;
2053 }
2054 
2055 MsgRouting AwtComponent::WmMove(int x, int y)
2056 {
2057     SetDrawState(GetDrawState() | static_cast<jint>(JAWT_LOCK_BOUNDS_CHANGED)
2058         | static_cast<jint>(JAWT_LOCK_CLIP_CHANGED));
2059     return mrDoDefault;
2060 }
2061 
2062 MsgRouting AwtComponent::WmSize(UINT type, int w, int h)
2063 {
2064     SetDrawState(GetDrawState() | static_cast<jint>(JAWT_LOCK_BOUNDS_CHANGED)
2065         | static_cast<jint>(JAWT_LOCK_CLIP_CHANGED));
2066     return mrDoDefault;
2067 }
2068 
2069 MsgRouting AwtComponent::WmSizing()
2070 {
2071     return mrDoDefault;
2072 }
2073 
2074 MsgRouting AwtComponent::WmSysCommand(UINT uCmdType, int xPos, int yPos)
2075 {
2076     return mrDoDefault;
2077 }
2078 
2079 MsgRouting AwtComponent::WmEnterSizeMove()
2080 {
2081     return mrDoDefault;
2082 }
2083 
2084 MsgRouting AwtComponent::WmExitSizeMove()
2085 {
2086     return mrDoDefault;
2087 }
2088 
2089 MsgRouting AwtComponent::WmEnterMenuLoop(BOOL isTrackPopupMenu)
2090 {
2091     return mrDoDefault;
2092 }
2093 
2094 MsgRouting AwtComponent::WmExitMenuLoop(BOOL isTrackPopupMenu)
2095 {
2096     return mrDoDefault;
2097 }
2098 
2099 MsgRouting AwtComponent::WmShowWindow(BOOL show, UINT status)
2100 {
2101     return mrDoDefault;
2102 }
2103 
2104 MsgRouting AwtComponent::WmSetFocus(HWND hWndLostFocus)
2105 {
2106     m_wheelRotationAmountX = 0;
2107     m_wheelRotationAmountY = 0;
2108     return mrDoDefault;
2109 }
2110 
2111 MsgRouting AwtComponent::WmKillFocus(HWND hWndGotFocus)
2112 {
2113     m_wheelRotationAmountX = 0;
2114     m_wheelRotationAmountY = 0;
2115     return mrDoDefault;
2116 }
2117 
2118 MsgRouting AwtComponent::WmCtlColor(HDC hDC, HWND hCtrl,
2119                                     UINT ctlColor, HBRUSH& retBrush)
2120 {
2121     AwtComponent* child = AwtComponent::GetComponent(hCtrl);
2122     if (child) {
2123         ::SetBkColor(hDC, child->GetBackgroundColor());
2124         ::SetTextColor(hDC, child->GetColor());
2125         retBrush = child->GetBackgroundBrush();
2126         return mrConsume;
2127     }
2128     return mrDoDefault;
2129 /*
2130     switch (ctlColor) {
2131         case CTLCOLOR_MSGBOX:
2132         case CTLCOLOR_EDIT:
2133         case CTLCOLOR_LISTBOX:
2134         case CTLCOLOR_BTN:
2135         case CTLCOLOR_DLG:
2136         case CTLCOLOR_SCROLLBAR:
2137         case CTLCOLOR_STATIC:
2138     }
2139 */
2140 }
2141 
2142 MsgRouting AwtComponent::WmHScroll(UINT scrollCode, UINT pos,
2143                                    HWND hScrollbar) {
2144     if (hScrollbar && hScrollbar != GetHWnd()) {
2145         /* the last test should never happen */
2146         AwtComponent* sb = GetComponent(hScrollbar);
2147         if (sb) {
2148             sb->WmHScroll(scrollCode, pos, hScrollbar);
2149         }
2150     }
2151     return mrDoDefault;
2152 }
2153 
2154 MsgRouting AwtComponent::WmVScroll(UINT scrollCode, UINT pos, HWND hScrollbar)
2155 {
2156     if (hScrollbar && hScrollbar != GetHWnd()) {
2157         /* the last test should never happen */
2158         AwtComponent* sb = GetComponent(hScrollbar);
2159         if (sb) {
2160             sb->WmVScroll(scrollCode, pos, hScrollbar);
2161         }
2162     }
2163     return mrDoDefault;
2164 }
2165 
2166 
2167 MsgRouting AwtComponent::WmPaint(HDC)
2168 {
2169     /* Get the rectangle that covers all update regions, if any exist. */
2170     RECT r;
2171     if (::GetUpdateRect(GetHWnd(), &r, FALSE)) {
2172         if ((r.right-r.left) > 0 && (r.bottom-r.top) > 0 &&
2173             m_peerObject != NULL && m_callbacksEnabled) {
2174             /*
2175              * Always call handlePaint, because the underlying control
2176              * will have painted itself (the "background") before any
2177              * paint method is called.
2178              */
2179             DoCallback("handlePaint", "(IIII)V",
2180                        r.left, r.top, r.right-r.left, r.bottom-r.top);
2181         }
2182     }
2183     return mrDoDefault;
2184 }
2185 
2186 void AwtComponent::PaintUpdateRgn(const RECT *insets)
2187 {
2188     // Fix 4530093: Don't Validate if can't actually paint
2189     if (m_peerObject == NULL || !m_callbacksEnabled) {
2190 
2191         // Fix 4745222: If we don't ValidateRgn,  windows will keep sending
2192         // WM_PAINT messages until we do. This causes java to go into
2193         // a tight loop that increases CPU to 100% and starves main
2194         // thread which needs to complete initialization, but cant.
2195         ::ValidateRgn(GetHWnd(), NULL);
2196 
2197         return;
2198     }
2199 
2200     HRGN rgn = ::CreateRectRgn(0,0,1,1);
2201     int updated = ::GetUpdateRgn(GetHWnd(), rgn, FALSE);
2202     /*
2203      * Now remove all update regions from this window -- do it
2204      * here instead of after the Java upcall, in case any new
2205      * updating is requested.
2206      */
2207     ::ValidateRgn(GetHWnd(), NULL);
2208 
2209     if (updated == COMPLEXREGION || updated == SIMPLEREGION) {
2210         if (insets != NULL) {
2211             ::OffsetRgn(rgn, insets->left, insets->top);
2212         }
2213         DWORD size = ::GetRegionData(rgn, 0, NULL);
2214         if (size == 0) {
2215             ::DeleteObject((HGDIOBJ)rgn);
2216             return;
2217         }
2218         char* buffer = new char[size]; // safe because sizeof(char)==1
2219         memset(buffer, 0, size);
2220         LPRGNDATA rgndata = (LPRGNDATA)buffer;
2221         rgndata->rdh.dwSize = sizeof(RGNDATAHEADER);
2222         rgndata->rdh.iType = RDH_RECTANGLES;
2223         int retCode = ::GetRegionData(rgn, size, rgndata);
2224         VERIFY(retCode);
2225         if (retCode == 0) {
2226             delete [] buffer;
2227             ::DeleteObject((HGDIOBJ)rgn);
2228             return;
2229         }
2230         /*
2231          * Updating rects are divided into mostly vertical and mostly horizontal
2232          * Each group is united together and if not empty painted separately
2233          */
2234         RECT* r = (RECT*)(buffer + rgndata->rdh.dwSize);
2235         RECT* un[2] = {0, 0};
2236     DWORD i;
2237     for (i = 0; i < rgndata->rdh.nCount; i++, r++) {
2238             int width = r->right-r->left;
2239             int height = r->bottom-r->top;
2240             if (width > 0 && height > 0) {
2241                 int toAdd = (width > height) ? 0: 1;
2242                 if (un[toAdd] != 0) {
2243                     ::UnionRect(un[toAdd], un[toAdd], r);
2244                 } else {
2245                     un[toAdd] = r;
2246                 }
2247             }
2248         }
2249         for(i = 0; i < 2; i++) {
2250             if (un[i] != 0) {
2251                 DoCallback("handleExpose", "(IIII)V",
2252                            ScaleDownX(un[i]->left),
2253                            ScaleDownY(un[i]->top),
2254                            ScaleDownX(un[i]->right - un[i]->left),
2255                            ScaleDownY(un[i]->bottom - un[i]->top));
2256             }
2257         }
2258         delete [] buffer;
2259     }
2260     ::DeleteObject((HGDIOBJ)rgn);
2261 }
2262 
2263 MsgRouting AwtComponent::WmMouseEnter(UINT flags, int x, int y)
2264 {
2265     SendMouseEvent(java_awt_event_MouseEvent_MOUSE_ENTERED,
2266                    ::JVM_CurrentTimeMillis(NULL, 0), x, y, GetJavaModifiers(), 0, JNI_FALSE);
2267     if ((flags & ALL_MK_BUTTONS) == 0) {
2268         AwtCursor::UpdateCursor(this);
2269     }
2270     sm_cursorOn = GetHWnd();
2271     return mrConsume;   /* Don't pass our synthetic event on! */
2272 }
2273 
2274 MSG*
2275 AwtComponent::CreateMessage(UINT message, WPARAM wParam, LPARAM lParam,
2276                             int x = 0, int y = 0)
2277 {
2278     MSG* pMsg = new MSG;
2279     InitMessage(pMsg, message, wParam, lParam, x, y);
2280     return pMsg;
2281 }
2282 
2283 
2284 jint
2285 AwtComponent::GetDrawState(HWND hwnd) {
2286     return (jint)(INT_PTR)(::GetProp(hwnd, DrawingStateProp));
2287 }
2288 
2289 void
2290 AwtComponent::SetDrawState(HWND hwnd, jint state) {
2291     ::SetProp(hwnd, DrawingStateProp, (HANDLE)(INT_PTR)state);
2292 }
2293 
2294 void
2295 AwtComponent::InitMessage(MSG* msg, UINT message, WPARAM wParam, LPARAM lParam,
2296                             int x = 0, int y = 0)
2297 {
2298     msg->message = message;
2299     msg->wParam = wParam;
2300     msg->lParam = lParam;
2301     msg->time = ::GetMessageTime();
2302     msg->pt.x = x;
2303     msg->pt.y = y;
2304 }
2305 
2306 MsgRouting AwtComponent::WmNcMouseDown(WPARAM hitTest, int x, int y, int button) {
2307     return mrDoDefault;
2308 }
2309 MsgRouting AwtComponent::WmNcMouseUp(WPARAM hitTest, int x, int y, int button) {
2310     return mrDoDefault;
2311 }
2312 
2313 MsgRouting AwtComponent::WmWindowPosChanging(LPARAM windowPos) {
2314     return mrDoDefault;
2315 }
2316 MsgRouting AwtComponent::WmWindowPosChanged(LPARAM windowPos) {
2317     return mrDoDefault;
2318 }
2319 
2320 void AwtComponent::WmTouch(WPARAM wParam, LPARAM lParam) {
2321     AwtToolkit& tk = AwtToolkit::GetInstance();
2322     if (!tk.IsWin8OrLater() || !tk.IsTouchKeyboardAutoShowEnabled()) {
2323         return;
2324     }
2325 
2326     UINT inputsCount = LOWORD(wParam);
2327     TOUCHINPUT* pInputs = new TOUCHINPUT[inputsCount];
2328     if (pInputs != NULL) {
2329         if (tk.TIGetTouchInputInfo((HTOUCHINPUT)lParam, inputsCount, pInputs,
2330                 sizeof(TOUCHINPUT)) != 0) {
2331             for (UINT i = 0; i < inputsCount; i++) {
2332                 TOUCHINPUT ti = pInputs[i];
2333                 if (ti.dwFlags & TOUCHEVENTF_PRIMARY) {
2334                     if (ti.dwFlags & TOUCHEVENTF_DOWN) {
2335                         m_touchDownPoint.x = ti.x / 100;
2336                         m_touchDownPoint.y = ti.y / 100;
2337                         ::ScreenToClient(GetHWnd(), &m_touchDownPoint);
2338                         m_touchDownOccurred = TRUE;
2339                     } else if (ti.dwFlags & TOUCHEVENTF_UP) {
2340                         m_touchUpPoint.x = ti.x / 100;
2341                         m_touchUpPoint.y = ti.y / 100;
2342                         ::ScreenToClient(GetHWnd(), &m_touchUpPoint);
2343                         m_touchUpOccurred = TRUE;
2344                     }
2345                 }
2346             }
2347         }
2348         delete[] pInputs;
2349     }
2350 }
2351 
2352 /* Double-click variables. */
2353 static jlong multiClickTime = ::GetDoubleClickTime();
2354 static int multiClickMaxX = ::GetSystemMetrics(SM_CXDOUBLECLK);
2355 static int multiClickMaxY = ::GetSystemMetrics(SM_CYDOUBLECLK);
2356 static AwtComponent* lastClickWnd = NULL;
2357 static jlong lastTime = 0;
2358 static int lastClickX = 0;
2359 static int lastClickY = 0;
2360 static int lastButton = 0;
2361 static int clickCount = 0;
2362 
2363 // A static method that makes the clickCount available in the derived classes
2364 // overriding WmMouseDown().
2365 int AwtComponent::GetClickCount()
2366 {
2367     return clickCount;
2368 }
2369 
2370 MsgRouting AwtComponent::WmMouseDown(UINT flags, int x, int y, int button)
2371 {
2372     jlong now = ::JVM_CurrentTimeMillis(NULL, 0);
2373 
2374     if (lastClickWnd == this &&
2375         lastButton == button &&
2376         (now - lastTime) <= multiClickTime &&
2377         abs(x - lastClickX) <= multiClickMaxX &&
2378         abs(y - lastClickY) <= multiClickMaxY)
2379     {
2380         clickCount++;
2381     } else {
2382         clickCount = 1;
2383         lastClickWnd = this;
2384         lastButton = button;
2385         lastClickX = x;
2386         lastClickY = y;
2387     }
2388     /*
2389      *Set appropriate bit of the mask on WM_MOUSE_DOWN message.
2390      */
2391     m_mouseButtonClickAllowed |= GetButtonMK(button);
2392     lastTime = now;
2393 
2394     BOOL causedByTouchEvent = FALSE;
2395     if (m_touchDownOccurred &&
2396         (abs(m_touchDownPoint.x - x) <= TOUCH_MOUSE_COORDS_DELTA) &&
2397         (abs(m_touchDownPoint.y - y) <= TOUCH_MOUSE_COORDS_DELTA)) {
2398         causedByTouchEvent = TRUE;
2399         m_touchDownOccurred = FALSE;
2400     }
2401 
2402     MSG msg;
2403     InitMessage(&msg, lastMessage, flags, MAKELPARAM(x, y), x, y);
2404 
2405     AwtWindow *toplevel = GetContainer();
2406     if (toplevel && !toplevel->IsSimpleWindow()) {
2407         /*
2408          * The frame should be focused by click in case it is
2409          * the active window but not the focused window. See 6886678.
2410          */
2411         if (toplevel->GetHWnd() == ::GetActiveWindow() &&
2412             toplevel->GetHWnd() != AwtComponent::GetFocusedWindow())
2413         {
2414             toplevel->AwtSetActiveWindow();
2415         }
2416     }
2417 
2418     SendMouseEvent(java_awt_event_MouseEvent_MOUSE_PRESSED, now, x, y,
2419                    GetJavaModifiers(), clickCount, JNI_FALSE,
2420                    GetButton(button), &msg, causedByTouchEvent);
2421     /*
2422      * NOTE: this call is intentionally placed after all other code,
2423      * since AwtComponent::WmMouseDown() assumes that the cached id of the
2424      * latest retrieved message (see lastMessage in awt_Component.cpp)
2425      * matches the mouse message being processed.
2426      * SetCapture() sends WM_CAPTURECHANGED and breaks that
2427      * assumption.
2428      */
2429     SetDragCapture(flags);
2430 
2431     AwtWindow * owner = (AwtWindow*)GetComponent(GetTopLevelParentForWindow(GetHWnd()));
2432     if (AwtWindow::GetGrabbedWindow() != NULL && owner != NULL) {
2433         if (!AwtWindow::GetGrabbedWindow()->IsOneOfOwnersOf(owner)) {
2434             AwtWindow::GetGrabbedWindow()->Ungrab();
2435         }
2436     }
2437     return mrConsume;
2438 }
2439 
2440 MsgRouting AwtComponent::WmMouseUp(UINT flags, int x, int y, int button)
2441 {
2442     BOOL causedByTouchEvent = FALSE;
2443     if (m_touchUpOccurred &&
2444         (abs(m_touchUpPoint.x - x) <= TOUCH_MOUSE_COORDS_DELTA) &&
2445         (abs(m_touchUpPoint.y - y) <= TOUCH_MOUSE_COORDS_DELTA)) {
2446         causedByTouchEvent = TRUE;
2447         m_touchUpOccurred = FALSE;
2448     }
2449 
2450     MSG msg;
2451     InitMessage(&msg, lastMessage, flags, MAKELPARAM(x, y), x, y);
2452 
2453     SendMouseEvent(java_awt_event_MouseEvent_MOUSE_RELEASED, ::JVM_CurrentTimeMillis(NULL, 0),
2454                    x, y, GetJavaModifiers(), clickCount,
2455                    (GetButton(button) == java_awt_event_MouseEvent_BUTTON3 ?
2456                     TRUE : FALSE), GetButton(button), &msg, causedByTouchEvent);
2457     /*
2458      * If no movement, then report a click following the button release.
2459      * When WM_MOUSEUP comes to a window without previous WM_MOUSEDOWN,
2460      * spurous MOUSE_CLICK is about to happen. See 6430553.
2461      */
2462     if ((m_mouseButtonClickAllowed & GetButtonMK(button)) != 0) { //CLICK allowed
2463         SendMouseEvent(java_awt_event_MouseEvent_MOUSE_CLICKED,
2464                        ::JVM_CurrentTimeMillis(NULL, 0), x, y, GetJavaModifiers(),
2465                        clickCount, JNI_FALSE, GetButton(button));
2466     }
2467     // Exclude button from allowed to generate CLICK messages
2468     m_mouseButtonClickAllowed &= ~GetButtonMK(button);
2469 
2470     if ((flags & ALL_MK_BUTTONS) == 0) {
2471         // only update if all buttons have been released
2472         AwtCursor::UpdateCursor(this);
2473     }
2474     /*
2475      * NOTE: this call is intentionally placed after all other code,
2476      * since AwtComponent::WmMouseUp() assumes that the cached id of the
2477      * latest retrieved message (see lastMessage in awt_Component.cpp)
2478      * matches the mouse message being processed.
2479      * ReleaseCapture() sends WM_CAPTURECHANGED and breaks that
2480      * assumption.
2481      */
2482     ReleaseDragCapture(flags);
2483 
2484     return mrConsume;
2485 }
2486 
2487 MsgRouting AwtComponent::WmMouseMove(UINT flags, int x, int y)
2488 {
2489     static AwtComponent* lastComp = NULL;
2490     static int lastX = 0;
2491     static int lastY = 0;
2492 
2493     /*
2494      * Only report mouse move and drag events if a move or drag
2495      * actually happened -- Windows sends a WM_MOUSEMOVE in case the
2496      * app wants to modify the cursor.
2497      */
2498     if (lastComp != this || x != lastX || y != lastY) {
2499         lastComp = this;
2500         lastX = x;
2501         lastY = y;
2502         BOOL extraButtonsEnabled = AwtToolkit::GetInstance().areExtraMouseButtonsEnabled();
2503         if (((flags & (ALL_MK_BUTTONS)) != 0) ||
2504             (extraButtonsEnabled && (flags & (X_BUTTONS)) != 0))
2505 //        if (( extraButtonsEnabled && ( (flags & (ALL_MK_BUTTONS | X_BUTTONS)) != 0 )) ||
2506 //            ( !extraButtonsEnabled && (((flags & (ALL_MK_BUTTONS)) != 0 )) && ((flags & (X_BUTTONS)) == 0) ))
2507         {
2508             // 6404008 : if Dragged event fired we shouldn't fire
2509             // Clicked event: m_firstDragSent set to TRUE.
2510             // This is a partial backout of 5039416 fix.
2511             MSG msg;
2512             InitMessage(&msg, lastMessage, flags, MAKELPARAM(x, y), x, y);
2513             SendMouseEvent(java_awt_event_MouseEvent_MOUSE_DRAGGED, ::JVM_CurrentTimeMillis(NULL, 0), x, y,
2514                            GetJavaModifiers(), 0, JNI_FALSE,
2515                            java_awt_event_MouseEvent_NOBUTTON, &msg);
2516             //dragging means no more CLICKs until next WM_MOUSE_DOWN/WM_MOUSE_UP message sequence
2517             m_mouseButtonClickAllowed = 0;
2518         } else {
2519             MSG msg;
2520             InitMessage(&msg, lastMessage, flags, MAKELPARAM(x, y), x, y);
2521             SendMouseEvent(java_awt_event_MouseEvent_MOUSE_MOVED, ::JVM_CurrentTimeMillis(NULL, 0), x, y,
2522                            GetJavaModifiers(), 0, JNI_FALSE,
2523                            java_awt_event_MouseEvent_NOBUTTON, &msg);
2524         }
2525     }
2526 
2527     return mrConsume;
2528 }
2529 
2530 MsgRouting AwtComponent::WmMouseExit(UINT flags, int x, int y)
2531 {
2532     SendMouseEvent(java_awt_event_MouseEvent_MOUSE_EXITED, ::JVM_CurrentTimeMillis(NULL, 0), x,
2533                    y, GetJavaModifiers(), 0, JNI_FALSE);
2534     sm_cursorOn = NULL;
2535     return mrConsume;   /* Don't pass our synthetic event on! */
2536 }
2537 
2538 MsgRouting AwtComponent::WmMouseWheel(UINT flags, int x, int y,
2539                                       int wheelRotation, BOOL isHorizontal)
2540 {
2541     // convert coordinates to be Component-relative, not screen relative
2542     // for wheeling when outside the window, this works similar to
2543     // coordinates during a drag
2544     POINT eventPt;
2545     eventPt.x = x;
2546     eventPt.y = y;
2547     DTRACE_PRINT2("  original coords: %i,%i\n", x, y);
2548     ::ScreenToClient(GetHWnd(), &eventPt);
2549     DTRACE_PRINT2("  new coords: %i,%i\n\n", eventPt.x, eventPt.y);
2550 
2551     // set some defaults
2552     jint scrollType = java_awt_event_MouseWheelEvent_WHEEL_UNIT_SCROLL;
2553     jint scrollUnits = 3;
2554 
2555     BOOL result;
2556     UINT platformUnits;
2557     jint roundedWheelRotation;
2558     jdouble preciseWheelRotation;
2559 
2560     // AWT interprets wheel rotation differently than win32, so we need to
2561     // decode wheel amount.
2562     jint modifiers = GetJavaModifiers();
2563     if (isHorizontal) {
2564         modifiers |= java_awt_event_InputEvent_SHIFT_DOWN_MASK;
2565         m_wheelRotationAmountX += wheelRotation;
2566         roundedWheelRotation = m_wheelRotationAmountX / (WHEEL_DELTA);
2567         preciseWheelRotation = (jdouble) wheelRotation / (WHEEL_DELTA);
2568         result = ::SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0,
2569                                         &platformUnits, 0);
2570     } else {
2571         m_wheelRotationAmountY += wheelRotation;
2572         roundedWheelRotation = m_wheelRotationAmountY / (-1 * WHEEL_DELTA);
2573         preciseWheelRotation = (jdouble) wheelRotation / (-1 * WHEEL_DELTA);
2574         result = ::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
2575                                         &platformUnits, 0);
2576     }
2577 
2578     MSG msg;
2579     InitMessage(&msg, lastMessage, MAKEWPARAM(flags, wheelRotation),
2580                 MAKELPARAM(x, y));
2581 
2582     if (result) {
2583         if (platformUnits == WHEEL_PAGESCROLL) {
2584             scrollType = java_awt_event_MouseWheelEvent_WHEEL_BLOCK_SCROLL;
2585             scrollUnits = 1;
2586         }
2587         else {
2588             scrollType = java_awt_event_MouseWheelEvent_WHEEL_UNIT_SCROLL;
2589             scrollUnits = platformUnits;
2590         }
2591     }
2592 
2593     DTRACE_PRINTLN("calling SendMouseWheelEvent");
2594 
2595     SendMouseWheelEvent(java_awt_event_MouseEvent_MOUSE_WHEEL, ::JVM_CurrentTimeMillis(NULL, 0),
2596                         eventPt.x, eventPt.y, modifiers, 0, 0, scrollType,
2597                         scrollUnits, roundedWheelRotation, preciseWheelRotation, &msg);
2598 
2599     m_wheelRotationAmountX %= WHEEL_DELTA;
2600     m_wheelRotationAmountY %= WHEEL_DELTA;
2601     // this message could be propagated up to the parent chain
2602     // by the mouse message post processors
2603     return mrConsume;
2604 }
2605 
2606 jint AwtComponent::GetKeyLocation(UINT wkey, UINT flags) {
2607     // Rector+Newcomer page 413
2608     // The extended keys are the Alt and Control on the right of
2609     // the space bar, the non-Numpad arrow keys, the non-Numpad
2610     // Insert, PageUp, etc. keys, and the Numpad Divide and Enter keys.
2611     // Note that neither Shift key is extended.
2612     // Although not listed in Rector+Newcomer, both Windows keys
2613     // (91 and 92) are extended keys, the Context Menu key
2614     // (property key or application key - 93) is extended,
2615     // and so is the NumLock key.
2616 
2617     // wkey is the wParam, flags is the HIWORD of the lParam
2618 
2619     // "Extended" bit is 24th in lParam, so it's 8th in flags = HIWORD(lParam)
2620     BOOL extended = ((1<<8) & flags);
2621 
2622     if (IsNumPadKey(wkey, extended)) {
2623         return java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD;
2624     }
2625 
2626     switch (wkey) {
2627       case VK_SHIFT:
2628         return AwtComponent::GetShiftKeyLocation(wkey, flags);
2629       case VK_CONTROL: // fall through
2630       case VK_MENU:
2631         if (extended) {
2632             return java_awt_event_KeyEvent_KEY_LOCATION_RIGHT;
2633         } else {
2634             return java_awt_event_KeyEvent_KEY_LOCATION_LEFT;
2635         }
2636       case VK_LWIN:
2637         return java_awt_event_KeyEvent_KEY_LOCATION_LEFT;
2638       case VK_RWIN:
2639         return java_awt_event_KeyEvent_KEY_LOCATION_RIGHT;
2640       default:
2641         break;
2642     }
2643 
2644     // REMIND: if we add keycodes for the windows keys, we'll have to
2645     // include left/right discrimination code for them.
2646 
2647     return java_awt_event_KeyEvent_KEY_LOCATION_STANDARD;
2648 }
2649 
2650 jint AwtComponent::GetShiftKeyLocation(UINT vkey, UINT flags)
2651 {
2652     // init scancodes to safe values
2653     UINT leftShiftScancode = 0;
2654     UINT rightShiftScancode = 0;
2655 
2656     // First 8 bits of flags is the scancode
2657     UINT keyScanCode = flags & 0xFF;
2658 
2659     DTRACE_PRINTLN3(
2660       "AwtComponent::GetShiftKeyLocation  vkey = %d = 0x%x  scan = %d",
2661       vkey, vkey, keyScanCode);
2662 
2663     leftShiftScancode = ::MapVirtualKey(VK_LSHIFT, 0);
2664     rightShiftScancode = ::MapVirtualKey(VK_RSHIFT, 0);
2665 
2666     if (keyScanCode == leftShiftScancode) {
2667         return java_awt_event_KeyEvent_KEY_LOCATION_LEFT;
2668     }
2669     if (keyScanCode == rightShiftScancode) {
2670         return java_awt_event_KeyEvent_KEY_LOCATION_RIGHT;
2671     }
2672 
2673     DASSERT(false);
2674     // Note: the above should not fail on NT (or 2000)
2675 
2676     // default value
2677     return java_awt_event_KeyEvent_KEY_LOCATION_LEFT;
2678 }
2679 
2680 /* Returns Java ActionEvent modifieres.
2681  * When creating ActionEvent, modifiers provided by ActionEvent
2682  * class should be set.
2683  */
2684 jint
2685 AwtComponent::GetActionModifiers()
2686 {
2687     jint modifiers = GetJavaModifiers();
2688 
2689     if (modifiers & java_awt_event_InputEvent_CTRL_DOWN_MASK) {
2690         modifiers |= java_awt_event_ActionEvent_CTRL_MASK;
2691     }
2692     if (modifiers & java_awt_event_InputEvent_SHIFT_DOWN_MASK) {
2693         modifiers |= java_awt_event_ActionEvent_SHIFT_MASK;
2694     }
2695     if (modifiers & java_awt_event_InputEvent_ALT_DOWN_MASK) {
2696         modifiers |= java_awt_event_ActionEvent_ALT_MASK;
2697     }
2698     return modifiers;
2699 }
2700 
2701 /* Returns Java extended InputEvent modifieres.
2702  * Since ::GetKeyState returns current state and Java modifiers represent
2703  * state before event, modifier on changed key are inverted.
2704  */
2705 jint
2706 AwtComponent::GetJavaModifiers()
2707 {
2708     jint modifiers = 0;
2709 
2710     if (HIBYTE(::GetKeyState(VK_CONTROL)) != 0) {
2711         modifiers |= java_awt_event_InputEvent_CTRL_DOWN_MASK;
2712     }
2713     if (HIBYTE(::GetKeyState(VK_SHIFT)) != 0) {
2714         modifiers |= java_awt_event_InputEvent_SHIFT_DOWN_MASK;
2715     }
2716     if (HIBYTE(::GetKeyState(VK_MENU)) != 0) {
2717         modifiers |= java_awt_event_InputEvent_ALT_DOWN_MASK;
2718     }
2719     if (HIBYTE(::GetKeyState(VK_RMENU)) != 0) {
2720         modifiers |= java_awt_event_InputEvent_ALT_GRAPH_DOWN_MASK;
2721     }
2722     if (HIBYTE(::GetKeyState(VK_MBUTTON)) != 0) {
2723        modifiers |= java_awt_event_InputEvent_BUTTON2_DOWN_MASK;
2724     }
2725     if (HIBYTE(::GetKeyState(VK_RBUTTON)) != 0) {
2726         modifiers |= java_awt_event_InputEvent_BUTTON3_DOWN_MASK;
2727     }
2728     if (HIBYTE(::GetKeyState(VK_LBUTTON)) != 0) {
2729         modifiers |= java_awt_event_InputEvent_BUTTON1_DOWN_MASK;
2730     }
2731 
2732     if (HIBYTE(::GetKeyState(VK_XBUTTON1)) != 0) {
2733         modifiers |= masks[3];
2734     }
2735     if (HIBYTE(::GetKeyState(VK_XBUTTON2)) != 0) {
2736         modifiers |= masks[4];
2737     }
2738     return modifiers;
2739 }
2740 
2741 jint
2742 AwtComponent::GetButton(int mouseButton)
2743 {
2744     /* Mouse buttons are already set correctly for left/right handedness */
2745     switch(mouseButton) {
2746     case LEFT_BUTTON:
2747         return java_awt_event_MouseEvent_BUTTON1;
2748     case MIDDLE_BUTTON:
2749         return java_awt_event_MouseEvent_BUTTON2;
2750     case RIGHT_BUTTON:
2751         return java_awt_event_MouseEvent_BUTTON3;
2752     case X1_BUTTON: //16 :
2753         //just assign 4 and 5 numbers because MouseEvent class doesn't contain const identifier for them now
2754         return 4;
2755     case X2_BUTTON: //32
2756         return 5;
2757     }
2758     return java_awt_event_MouseEvent_NOBUTTON;
2759 }
2760 
2761 UINT
2762 AwtComponent::GetButtonMK(int mouseButton)
2763 {
2764     switch(mouseButton) {
2765     case LEFT_BUTTON:
2766         return MK_LBUTTON;
2767     case MIDDLE_BUTTON:
2768         return MK_MBUTTON;
2769     case RIGHT_BUTTON:
2770         return MK_RBUTTON;
2771     case X1_BUTTON:
2772         return MK_XBUTTON1;
2773     case X2_BUTTON:
2774         return MK_XBUTTON2;
2775     }
2776     return 0;
2777 }
2778 
2779 // FIXME: Keyboard related stuff has grown so big and hairy that we
2780 // really need to move it into a class of its own.  And, since
2781 // keyboard is a shared resource, AwtComponent is a bad place for it.
2782 
2783 // These constants are defined in the Japanese version of VC++5.0,
2784 // but not the US version
2785 #ifndef VK_CONVERT
2786 #define VK_KANA           0x15
2787 #define VK_KANJI          0x19
2788 #define VK_CONVERT        0x1C
2789 #define VK_NONCONVERT     0x1D
2790 #endif
2791 
2792 #ifndef VK_XBUTTON1
2793 #define VK_XBUTTON1      0x05
2794 #endif
2795 
2796 #ifndef VK_XBUTTON2
2797 #define VK_XBUTTON2      0x06
2798 #endif
2799 
2800 typedef struct {
2801     UINT javaKey;
2802     UINT windowsKey;
2803 } KeyMapEntry;
2804 
2805 // Static table, arranged more or less spatially.
2806 KeyMapEntry keyMapTable[] = {
2807     // Modifier keys
2808     {java_awt_event_KeyEvent_VK_CAPS_LOCK,        VK_CAPITAL},
2809     {java_awt_event_KeyEvent_VK_SHIFT,            VK_SHIFT},
2810     {java_awt_event_KeyEvent_VK_CONTROL,          VK_CONTROL},
2811     {java_awt_event_KeyEvent_VK_ALT,              VK_MENU},
2812     {java_awt_event_KeyEvent_VK_ALT_GRAPH,        VK_RMENU},
2813     {java_awt_event_KeyEvent_VK_NUM_LOCK,         VK_NUMLOCK},
2814 
2815     // Miscellaneous Windows keys
2816     {java_awt_event_KeyEvent_VK_WINDOWS,          VK_LWIN},
2817     {java_awt_event_KeyEvent_VK_WINDOWS,          VK_RWIN},
2818     {java_awt_event_KeyEvent_VK_CONTEXT_MENU,     VK_APPS},
2819 
2820     // Alphabet
2821     {java_awt_event_KeyEvent_VK_A,                'A'},
2822     {java_awt_event_KeyEvent_VK_B,                'B'},
2823     {java_awt_event_KeyEvent_VK_C,                'C'},
2824     {java_awt_event_KeyEvent_VK_D,                'D'},
2825     {java_awt_event_KeyEvent_VK_E,                'E'},
2826     {java_awt_event_KeyEvent_VK_F,                'F'},
2827     {java_awt_event_KeyEvent_VK_G,                'G'},
2828     {java_awt_event_KeyEvent_VK_H,                'H'},
2829     {java_awt_event_KeyEvent_VK_I,                'I'},
2830     {java_awt_event_KeyEvent_VK_J,                'J'},
2831     {java_awt_event_KeyEvent_VK_K,                'K'},
2832     {java_awt_event_KeyEvent_VK_L,                'L'},
2833     {java_awt_event_KeyEvent_VK_M,                'M'},
2834     {java_awt_event_KeyEvent_VK_N,                'N'},
2835     {java_awt_event_KeyEvent_VK_O,                'O'},
2836     {java_awt_event_KeyEvent_VK_P,                'P'},
2837     {java_awt_event_KeyEvent_VK_Q,                'Q'},
2838     {java_awt_event_KeyEvent_VK_R,                'R'},
2839     {java_awt_event_KeyEvent_VK_S,                'S'},
2840     {java_awt_event_KeyEvent_VK_T,                'T'},
2841     {java_awt_event_KeyEvent_VK_U,                'U'},
2842     {java_awt_event_KeyEvent_VK_V,                'V'},
2843     {java_awt_event_KeyEvent_VK_W,                'W'},
2844     {java_awt_event_KeyEvent_VK_X,                'X'},
2845     {java_awt_event_KeyEvent_VK_Y,                'Y'},
2846     {java_awt_event_KeyEvent_VK_Z,                'Z'},
2847 
2848     // Standard numeric row
2849     {java_awt_event_KeyEvent_VK_0,                '0'},
2850     {java_awt_event_KeyEvent_VK_1,                '1'},
2851     {java_awt_event_KeyEvent_VK_2,                '2'},
2852     {java_awt_event_KeyEvent_VK_3,                '3'},
2853     {java_awt_event_KeyEvent_VK_4,                '4'},
2854     {java_awt_event_KeyEvent_VK_5,                '5'},
2855     {java_awt_event_KeyEvent_VK_6,                '6'},
2856     {java_awt_event_KeyEvent_VK_7,                '7'},
2857     {java_awt_event_KeyEvent_VK_8,                '8'},
2858     {java_awt_event_KeyEvent_VK_9,                '9'},
2859 
2860     // Misc key from main block
2861     {java_awt_event_KeyEvent_VK_ENTER,            VK_RETURN},
2862     {java_awt_event_KeyEvent_VK_SPACE,            VK_SPACE},
2863     {java_awt_event_KeyEvent_VK_BACK_SPACE,       VK_BACK},
2864     {java_awt_event_KeyEvent_VK_TAB,              VK_TAB},
2865     {java_awt_event_KeyEvent_VK_ESCAPE,           VK_ESCAPE},
2866 
2867     // NumPad with NumLock off & extended block (rectangular)
2868     {java_awt_event_KeyEvent_VK_INSERT,           VK_INSERT},
2869     {java_awt_event_KeyEvent_VK_DELETE,           VK_DELETE},
2870     {java_awt_event_KeyEvent_VK_HOME,             VK_HOME},
2871     {java_awt_event_KeyEvent_VK_END,              VK_END},
2872     {java_awt_event_KeyEvent_VK_PAGE_UP,          VK_PRIOR},
2873     {java_awt_event_KeyEvent_VK_PAGE_DOWN,        VK_NEXT},
2874     {java_awt_event_KeyEvent_VK_CLEAR,            VK_CLEAR}, // NumPad 5
2875 
2876     // NumPad with NumLock off & extended arrows block (triangular)
2877     {java_awt_event_KeyEvent_VK_LEFT,             VK_LEFT},
2878     {java_awt_event_KeyEvent_VK_RIGHT,            VK_RIGHT},
2879     {java_awt_event_KeyEvent_VK_UP,               VK_UP},
2880     {java_awt_event_KeyEvent_VK_DOWN,             VK_DOWN},
2881 
2882     // NumPad with NumLock on: numbers
2883     {java_awt_event_KeyEvent_VK_NUMPAD0,          VK_NUMPAD0},
2884     {java_awt_event_KeyEvent_VK_NUMPAD1,          VK_NUMPAD1},
2885     {java_awt_event_KeyEvent_VK_NUMPAD2,          VK_NUMPAD2},
2886     {java_awt_event_KeyEvent_VK_NUMPAD3,          VK_NUMPAD3},
2887     {java_awt_event_KeyEvent_VK_NUMPAD4,          VK_NUMPAD4},
2888     {java_awt_event_KeyEvent_VK_NUMPAD5,          VK_NUMPAD5},
2889     {java_awt_event_KeyEvent_VK_NUMPAD6,          VK_NUMPAD6},
2890     {java_awt_event_KeyEvent_VK_NUMPAD7,          VK_NUMPAD7},
2891     {java_awt_event_KeyEvent_VK_NUMPAD8,          VK_NUMPAD8},
2892     {java_awt_event_KeyEvent_VK_NUMPAD9,          VK_NUMPAD9},
2893 
2894     // NumPad with NumLock on
2895     {java_awt_event_KeyEvent_VK_MULTIPLY,         VK_MULTIPLY},
2896     {java_awt_event_KeyEvent_VK_ADD,              VK_ADD},
2897     {java_awt_event_KeyEvent_VK_SEPARATOR,        VK_SEPARATOR},
2898     {java_awt_event_KeyEvent_VK_SUBTRACT,         VK_SUBTRACT},
2899     {java_awt_event_KeyEvent_VK_DECIMAL,          VK_DECIMAL},
2900     {java_awt_event_KeyEvent_VK_DIVIDE,           VK_DIVIDE},
2901 
2902     // Functional keys
2903     {java_awt_event_KeyEvent_VK_F1,               VK_F1},
2904     {java_awt_event_KeyEvent_VK_F2,               VK_F2},
2905     {java_awt_event_KeyEvent_VK_F3,               VK_F3},
2906     {java_awt_event_KeyEvent_VK_F4,               VK_F4},
2907     {java_awt_event_KeyEvent_VK_F5,               VK_F5},
2908     {java_awt_event_KeyEvent_VK_F6,               VK_F6},
2909     {java_awt_event_KeyEvent_VK_F7,               VK_F7},
2910     {java_awt_event_KeyEvent_VK_F8,               VK_F8},
2911     {java_awt_event_KeyEvent_VK_F9,               VK_F9},
2912     {java_awt_event_KeyEvent_VK_F10,              VK_F10},
2913     {java_awt_event_KeyEvent_VK_F11,              VK_F11},
2914     {java_awt_event_KeyEvent_VK_F12,              VK_F12},
2915     {java_awt_event_KeyEvent_VK_F13,              VK_F13},
2916     {java_awt_event_KeyEvent_VK_F14,              VK_F14},
2917     {java_awt_event_KeyEvent_VK_F15,              VK_F15},
2918     {java_awt_event_KeyEvent_VK_F16,              VK_F16},
2919     {java_awt_event_KeyEvent_VK_F17,              VK_F17},
2920     {java_awt_event_KeyEvent_VK_F18,              VK_F18},
2921     {java_awt_event_KeyEvent_VK_F19,              VK_F19},
2922     {java_awt_event_KeyEvent_VK_F20,              VK_F20},
2923     {java_awt_event_KeyEvent_VK_F21,              VK_F21},
2924     {java_awt_event_KeyEvent_VK_F22,              VK_F22},
2925     {java_awt_event_KeyEvent_VK_F23,              VK_F23},
2926     {java_awt_event_KeyEvent_VK_F24,              VK_F24},
2927 
2928     {java_awt_event_KeyEvent_VK_PRINTSCREEN,      VK_SNAPSHOT},
2929     {java_awt_event_KeyEvent_VK_SCROLL_LOCK,      VK_SCROLL},
2930     {java_awt_event_KeyEvent_VK_PAUSE,            VK_PAUSE},
2931     {java_awt_event_KeyEvent_VK_CANCEL,           VK_CANCEL},
2932     {java_awt_event_KeyEvent_VK_HELP,             VK_HELP},
2933 
2934     // Japanese
2935     {java_awt_event_KeyEvent_VK_CONVERT,          VK_CONVERT},
2936     {java_awt_event_KeyEvent_VK_NONCONVERT,       VK_NONCONVERT},
2937     {java_awt_event_KeyEvent_VK_INPUT_METHOD_ON_OFF, VK_KANJI},
2938     {java_awt_event_KeyEvent_VK_ALPHANUMERIC,     VK_DBE_ALPHANUMERIC},
2939     {java_awt_event_KeyEvent_VK_KATAKANA,         VK_DBE_KATAKANA},
2940     {java_awt_event_KeyEvent_VK_HIRAGANA,         VK_DBE_HIRAGANA},
2941     {java_awt_event_KeyEvent_VK_FULL_WIDTH,       VK_DBE_DBCSCHAR},
2942     {java_awt_event_KeyEvent_VK_HALF_WIDTH,       VK_DBE_SBCSCHAR},
2943     {java_awt_event_KeyEvent_VK_ROMAN_CHARACTERS, VK_DBE_ROMAN},
2944 
2945     {java_awt_event_KeyEvent_VK_UNDEFINED,        0}
2946 };
2947 
2948 
2949 // Dynamic mapping table for OEM VK codes.  This table is refilled
2950 // by BuildDynamicKeyMapTable when keyboard layout is switched.
2951 // (see NT4 DDK src/input/inc/vkoem.h for OEM VK_ values).
2952 struct DynamicKeyMapEntry {
2953     UINT windowsKey;            // OEM VK codes known in advance
2954     UINT javaKey;               // depends on input langauge (kbd layout)
2955 };
2956 
2957 static DynamicKeyMapEntry dynamicKeyMapTable[] = {
2958     {0x00BA,  java_awt_event_KeyEvent_VK_UNDEFINED}, // VK_OEM_1
2959     {0x00BB,  java_awt_event_KeyEvent_VK_UNDEFINED}, // VK_OEM_PLUS
2960     {0x00BC,  java_awt_event_KeyEvent_VK_UNDEFINED}, // VK_OEM_COMMA
2961     {0x00BD,  java_awt_event_KeyEvent_VK_UNDEFINED}, // VK_OEM_MINUS
2962     {0x00BE,  java_awt_event_KeyEvent_VK_UNDEFINED}, // VK_OEM_PERIOD
2963     {0x00BF,  java_awt_event_KeyEvent_VK_UNDEFINED}, // VK_OEM_2
2964     {0x00C0,  java_awt_event_KeyEvent_VK_UNDEFINED}, // VK_OEM_3
2965     {0x00DB,  java_awt_event_KeyEvent_VK_UNDEFINED}, // VK_OEM_4
2966     {0x00DC,  java_awt_event_KeyEvent_VK_UNDEFINED}, // VK_OEM_5
2967     {0x00DD,  java_awt_event_KeyEvent_VK_UNDEFINED}, // VK_OEM_6
2968     {0x00DE,  java_awt_event_KeyEvent_VK_UNDEFINED}, // VK_OEM_7
2969     {0x00DF,  java_awt_event_KeyEvent_VK_UNDEFINED}, // VK_OEM_8
2970     {0x00E2,  java_awt_event_KeyEvent_VK_UNDEFINED}, // VK_OEM_102
2971     {0, 0}
2972 };
2973 
2974 
2975 
2976 // Auxiliary tables used to fill the above dynamic table.  We first
2977 // find the character for the OEM VK code using ::MapVirtualKey and
2978 // then go through these auxiliary tables to map it to Java VK code.
2979 
2980 struct CharToVKEntry {
2981     WCHAR c;
2982     UINT  javaKey;
2983 };
2984 
2985 static const CharToVKEntry charToVKTable[] = {
2986     {L'!',   java_awt_event_KeyEvent_VK_EXCLAMATION_MARK},
2987     {L'"',   java_awt_event_KeyEvent_VK_QUOTEDBL},
2988     {L'#',   java_awt_event_KeyEvent_VK_NUMBER_SIGN},
2989     {L'$',   java_awt_event_KeyEvent_VK_DOLLAR},
2990     {L'&',   java_awt_event_KeyEvent_VK_AMPERSAND},
2991     {L'\'',  java_awt_event_KeyEvent_VK_QUOTE},
2992     {L'(',   java_awt_event_KeyEvent_VK_LEFT_PARENTHESIS},
2993     {L')',   java_awt_event_KeyEvent_VK_RIGHT_PARENTHESIS},
2994     {L'*',   java_awt_event_KeyEvent_VK_ASTERISK},
2995     {L'+',   java_awt_event_KeyEvent_VK_PLUS},
2996     {L',',   java_awt_event_KeyEvent_VK_COMMA},
2997     {L'-',   java_awt_event_KeyEvent_VK_MINUS},
2998     {L'.',   java_awt_event_KeyEvent_VK_PERIOD},
2999     {L'/',   java_awt_event_KeyEvent_VK_SLASH},
3000     {L':',   java_awt_event_KeyEvent_VK_COLON},
3001     {L';',   java_awt_event_KeyEvent_VK_SEMICOLON},
3002     {L'<',   java_awt_event_KeyEvent_VK_LESS},
3003     {L'=',   java_awt_event_KeyEvent_VK_EQUALS},
3004     {L'>',   java_awt_event_KeyEvent_VK_GREATER},
3005     {L'@',   java_awt_event_KeyEvent_VK_AT},
3006     {L'[',   java_awt_event_KeyEvent_VK_OPEN_BRACKET},
3007     {L'\\',  java_awt_event_KeyEvent_VK_BACK_SLASH},
3008     {L']',   java_awt_event_KeyEvent_VK_CLOSE_BRACKET},
3009     {L'^',   java_awt_event_KeyEvent_VK_CIRCUMFLEX},
3010     {L'_',   java_awt_event_KeyEvent_VK_UNDERSCORE},
3011     {L'`',   java_awt_event_KeyEvent_VK_BACK_QUOTE},
3012     {L'{',   java_awt_event_KeyEvent_VK_BRACELEFT},
3013     {L'}',   java_awt_event_KeyEvent_VK_BRACERIGHT},
3014     {0x00A1, java_awt_event_KeyEvent_VK_INVERTED_EXCLAMATION_MARK},
3015     {0x20A0, java_awt_event_KeyEvent_VK_EURO_SIGN}, // ????
3016     {0,0}
3017 };
3018 
3019 // For dead accents some layouts return ASCII punctuation, while some
3020 // return spacing accent chars, so both should be listed.  NB: MS docs
3021 // say that conversion routings return spacing accent character, not
3022 // combining.
3023 static const CharToVKEntry charToDeadVKTable[] = {
3024     {L'`',   java_awt_event_KeyEvent_VK_DEAD_GRAVE},
3025     {L'\'',  java_awt_event_KeyEvent_VK_DEAD_ACUTE},
3026     {0x00B4, java_awt_event_KeyEvent_VK_DEAD_ACUTE},
3027     {L'^',   java_awt_event_KeyEvent_VK_DEAD_CIRCUMFLEX},
3028     {L'~',   java_awt_event_KeyEvent_VK_DEAD_TILDE},
3029     {0x02DC, java_awt_event_KeyEvent_VK_DEAD_TILDE},
3030     {0x00AF, java_awt_event_KeyEvent_VK_DEAD_MACRON},
3031     {0x02D8, java_awt_event_KeyEvent_VK_DEAD_BREVE},
3032     {0x02D9, java_awt_event_KeyEvent_VK_DEAD_ABOVEDOT},
3033     {L'"',   java_awt_event_KeyEvent_VK_DEAD_DIAERESIS},
3034     {0x00A8, java_awt_event_KeyEvent_VK_DEAD_DIAERESIS},
3035     {0x02DA, java_awt_event_KeyEvent_VK_DEAD_ABOVERING},
3036     {0x02DD, java_awt_event_KeyEvent_VK_DEAD_DOUBLEACUTE},
3037     {0x02C7, java_awt_event_KeyEvent_VK_DEAD_CARON},            // aka hacek
3038     {L',',   java_awt_event_KeyEvent_VK_DEAD_CEDILLA},
3039     {0x00B8, java_awt_event_KeyEvent_VK_DEAD_CEDILLA},
3040     {0x02DB, java_awt_event_KeyEvent_VK_DEAD_OGONEK},
3041     {0x037A, java_awt_event_KeyEvent_VK_DEAD_IOTA},             // ASCII ???
3042     {0x309B, java_awt_event_KeyEvent_VK_DEAD_VOICED_SOUND},
3043     {0x309C, java_awt_event_KeyEvent_VK_DEAD_SEMIVOICED_SOUND},
3044     {0x0004, java_awt_event_KeyEvent_VK_COMPOSE},
3045     {0,0}
3046 };
3047 
3048 // The full map of the current keyboard state including
3049 // windows virtual key, scancode, java virtual key, and unicode
3050 // for this key sans modifiers.
3051 // All but first element may be 0.
3052 // XXX in the update releases this is an addition to the unchanged existing code
3053 struct DynPrimaryKeymapEntry {
3054     UINT wkey;
3055     UINT scancode;
3056     UINT jkey;
3057     WCHAR unicode;
3058 };
3059 
3060 static DynPrimaryKeymapEntry dynPrimaryKeymap[256];
3061 
3062 void
3063 AwtComponent::InitDynamicKeyMapTable()
3064 {
3065     static BOOL kbdinited = FALSE;
3066 
3067     if (!kbdinited) {
3068         AwtComponent::BuildDynamicKeyMapTable();
3069         // We cannot build it here since JNI is not available yet:
3070         //AwtComponent::BuildPrimaryDynamicTable();
3071         kbdinited = TRUE;
3072     }
3073 }
3074 
3075 void
3076 AwtComponent::BuildDynamicKeyMapTable()
3077 {
3078     HKL hkl = GetKeyboardLayout();
3079 
3080     DTRACE_PRINTLN2("Building dynamic VK mapping tables: HKL = %08X (CP%d)",
3081                     hkl, AwtComponent::GetCodePage());
3082 
3083     // Will need this to reset layout after dead keys.
3084     UINT spaceScanCode = ::MapVirtualKeyEx(VK_SPACE, 0, hkl);
3085 
3086     // Entries in dynamic table that maps between Java VK and Windows
3087     // VK are built in three steps:
3088     //   1. Map windows VK to ANSI character (cannot map to unicode
3089     //      directly, since ::ToUnicode is not implemented on win9x)
3090     //   2. Convert ANSI char to Unicode char
3091     //   3. Map Unicode char to Java VK via two auxilary tables.
3092 
3093     for (DynamicKeyMapEntry *dynamic = dynamicKeyMapTable;
3094          dynamic->windowsKey != 0;
3095          ++dynamic)
3096     {
3097         // Defaults to VK_UNDEFINED
3098         dynamic->javaKey = java_awt_event_KeyEvent_VK_UNDEFINED;
3099 
3100         BYTE kbdState[AwtToolkit::KB_STATE_SIZE];
3101         AwtToolkit::GetKeyboardState(kbdState);
3102 
3103         kbdState[dynamic->windowsKey] |=  0x80; // Press the key.
3104 
3105         // Unpress modifiers, since they are most likely pressed as
3106         // part of the keyboard switching shortcut.
3107         kbdState[VK_CONTROL] &= ~0x80;
3108         kbdState[VK_SHIFT]   &= ~0x80;
3109         kbdState[VK_MENU]    &= ~0x80;
3110 
3111         char cbuf[2] = { '\0', '\0'};
3112         UINT scancode = ::MapVirtualKeyEx(dynamic->windowsKey, 0, hkl);
3113         int nchars = ::ToAsciiEx(dynamic->windowsKey, scancode, kbdState,
3114                                  (WORD*)cbuf, 0, hkl);
3115 
3116         // Auxiliary table used to map Unicode character to Java VK.
3117         // Will assign a different table for dead keys (below).
3118         const CharToVKEntry *charMap = charToVKTable;
3119 
3120         if (nchars < 0) { // Dead key
3121             // Use a different table for dead chars since different layouts
3122             // return different characters for the same dead key.
3123             charMap = charToDeadVKTable;
3124 
3125             // We also need to reset layout so that next translation
3126             // is unaffected by the dead status.  We do this by
3127             // translating <SPACE> key.
3128             kbdState[dynamic->windowsKey] &= ~0x80;
3129             kbdState[VK_SPACE] |= 0x80;
3130 
3131             char junkbuf[2] = { '\0', '\0'};
3132             ::ToAsciiEx(VK_SPACE, spaceScanCode, kbdState,
3133                         (WORD*)junkbuf, 0, hkl);
3134         }
3135 
3136 #ifdef DEBUG
3137         if (nchars == 0) {
3138             DTRACE_PRINTLN1("VK 0x%02X -> cannot convert to ANSI char",
3139                             dynamic->windowsKey);
3140             continue;
3141         }
3142         else if (nchars > 1) {  // can't happen, see reset code below
3143             DTRACE_PRINTLN3("VK 0x%02X -> converted to <0x%02X,0x%02X>",
3144                             dynamic->windowsKey,
3145                             (UCHAR)cbuf[0], (UCHAR)cbuf[1]);
3146             continue;
3147         }
3148 #endif
3149 
3150         WCHAR ucbuf[2] = { L'\0', L'\0' };
3151         int nconverted = ::MultiByteToWideChar(AwtComponent::GetCodePage(), 0,
3152                                                cbuf, 1, ucbuf, 2);
3153 #ifdef DEBUG
3154         if (nconverted < 0) {
3155             DTRACE_PRINTLN3("VK 0x%02X -> ANSI 0x%02X -> MultiByteToWideChar failed (0x%X)",
3156                             dynamic->windowsKey, (UCHAR)cbuf[0],
3157                             ::GetLastError());
3158             continue;
3159         }
3160 #endif
3161 
3162         WCHAR uc = ucbuf[0];
3163         for (const CharToVKEntry *map = charMap;  map->c != 0;  ++map) {
3164             if (uc == map->c) {
3165                 dynamic->javaKey = map->javaKey;
3166                 break;
3167             }
3168         }
3169 
3170         DTRACE_PRINTLN4("VK 0x%02X -> ANSI 0x%02X -> U+%04X -> Java VK 0x%X",
3171                         dynamic->windowsKey, (UCHAR)cbuf[0], (UINT)ucbuf[0],
3172                         dynamic->javaKey);
3173     } // for each VK_OEM_*
3174 }
3175 
3176 
3177 static BOOL isKanaLockAvailable()
3178 {
3179     // This method is to determine whether the Kana Lock feature is
3180     // available on the attached keyboard.  Kana Lock feature does not
3181     // necessarily require that the real KANA keytop is available on
3182     // keyboard, so using MapVirtualKey(VK_KANA) is not sufficient for testing.
3183     // Instead of that we regard it as Japanese keyboard (w/ Kana Lock) if :-
3184     //
3185     // - the keyboard layout is Japanese (VK_KANA has the same value as VK_HANGUL)
3186     // - the keyboard is Japanese keyboard (keyboard type == 7).
3187     return (LOWORD(GetKeyboardLayout(0)) == MAKELANGID(LANG_JAPANESE, SUBLANG_DEFAULT))
3188         && (GetKeyboardType(0) == 7);
3189 }
3190 
3191 void AwtComponent::JavaKeyToWindowsKey(UINT javaKey,
3192                                        UINT *windowsKey, UINT *modifiers, UINT originalWindowsKey)
3193 {
3194     // Handle the few cases where a Java VK code corresponds to a Windows
3195     // key/modifier combination or applies only to specific keyboard layouts
3196     switch (javaKey) {
3197         case java_awt_event_KeyEvent_VK_ALL_CANDIDATES:
3198             *windowsKey = VK_CONVERT;
3199             *modifiers = java_awt_event_InputEvent_ALT_DOWN_MASK;
3200             return;
3201         case java_awt_event_KeyEvent_VK_PREVIOUS_CANDIDATE:
3202             *windowsKey = VK_CONVERT;
3203             *modifiers = java_awt_event_InputEvent_SHIFT_DOWN_MASK;
3204             return;
3205         case java_awt_event_KeyEvent_VK_CODE_INPUT:
3206             *windowsKey = VK_DBE_ALPHANUMERIC;
3207             *modifiers = java_awt_event_InputEvent_ALT_DOWN_MASK;
3208             return;
3209         case java_awt_event_KeyEvent_VK_KANA_LOCK:
3210             if (isKanaLockAvailable()) {
3211                 *windowsKey = VK_KANA;
3212                 *modifiers = java_awt_event_InputEvent_CTRL_DOWN_MASK;
3213                 return;
3214             }
3215     }
3216 
3217     // for the general case, use a bi-directional table
3218     for (int i = 0; keyMapTable[i].windowsKey != 0; i++) {
3219         if (keyMapTable[i].javaKey == javaKey) {
3220             *windowsKey = keyMapTable[i].windowsKey;
3221             *modifiers = 0;
3222             return;
3223         }
3224     }
3225 
3226     // Bug 4766655
3227     // Two Windows keys could map to the same Java key, so
3228     // give preference to the originalWindowsKey if it is
3229     // specified (not IGNORE_KEY).
3230     if (originalWindowsKey == IGNORE_KEY) {
3231         for (int j = 0; dynamicKeyMapTable[j].windowsKey != 0; j++) {
3232             if (dynamicKeyMapTable[j].javaKey == javaKey) {
3233                 *windowsKey = dynamicKeyMapTable[j].windowsKey;
3234                 *modifiers = 0;
3235                 return;
3236             }
3237         }
3238     } else {
3239         BOOL found = false;
3240         for (int j = 0; dynamicKeyMapTable[j].windowsKey != 0; j++) {
3241             if (dynamicKeyMapTable[j].javaKey == javaKey) {
3242                 *windowsKey = dynamicKeyMapTable[j].windowsKey;
3243                 *modifiers = 0;
3244                 found = true;
3245                 if (*windowsKey == originalWindowsKey) {
3246                     return;   /* if ideal case found return, else keep looking */
3247                 }
3248             }
3249         }
3250         if (found) {
3251             return;
3252         }
3253     }
3254 
3255     *windowsKey = 0;
3256     *modifiers = 0;
3257     return;
3258 }
3259 
3260 UINT AwtComponent::WindowsKeyToJavaKey(UINT windowsKey, UINT modifiers, UINT character, BOOL isDeadKey)
3261 
3262 {
3263     // Handle the few cases where we need to take the modifier into
3264     // consideration for the Java VK code or where we have to take the keyboard
3265     // layout into consideration so that function keys can get
3266     // recognized in a platform-independent way.
3267     switch (windowsKey) {
3268         case VK_CONVERT:
3269             if ((modifiers & java_awt_event_InputEvent_ALT_DOWN_MASK) != 0) {
3270                 return java_awt_event_KeyEvent_VK_ALL_CANDIDATES;
3271             }
3272             if ((modifiers & java_awt_event_InputEvent_SHIFT_DOWN_MASK) != 0) {
3273                 return java_awt_event_KeyEvent_VK_PREVIOUS_CANDIDATE;
3274             }
3275             break;
3276         case VK_DBE_ALPHANUMERIC:
3277             if ((modifiers & java_awt_event_InputEvent_ALT_DOWN_MASK) != 0) {
3278                 return java_awt_event_KeyEvent_VK_CODE_INPUT;
3279             }
3280             break;
3281         case VK_KANA:
3282             if (isKanaLockAvailable()) {
3283                 return java_awt_event_KeyEvent_VK_KANA_LOCK;
3284             }
3285             break;
3286     };
3287 
3288     // check dead key
3289     if (isDeadKey) {
3290       for (int i = 0; charToDeadVKTable[i].c != 0; i++) {
3291         if (charToDeadVKTable[i].c == character) {
3292             return charToDeadVKTable[i].javaKey;
3293         }
3294       }
3295     }
3296 
3297     // for the general case, use a bi-directional table
3298     for (int i = 0; keyMapTable[i].windowsKey != 0; i++) {
3299         if (keyMapTable[i].windowsKey == windowsKey) {
3300             return keyMapTable[i].javaKey;
3301         }
3302     }
3303 
3304     for (int j = 0; dynamicKeyMapTable[j].windowsKey != 0; j++) {
3305         if (dynamicKeyMapTable[j].windowsKey == windowsKey) {
3306             if (dynamicKeyMapTable[j].javaKey != java_awt_event_KeyEvent_VK_UNDEFINED) {
3307                 return dynamicKeyMapTable[j].javaKey;
3308             }else{
3309                 break;
3310             }
3311         }
3312     }
3313 
3314     return java_awt_event_KeyEvent_VK_UNDEFINED;
3315 }
3316 
3317 BOOL AwtComponent::IsNavigationKey(UINT wkey) {
3318     switch (wkey) {
3319       case VK_END:
3320       case VK_PRIOR:  // PageUp
3321       case VK_NEXT:  // PageDown
3322       case VK_HOME:
3323       case VK_LEFT:
3324       case VK_UP:
3325       case VK_RIGHT:
3326       case VK_DOWN:
3327           return TRUE;
3328     }
3329     return FALSE;
3330 }
3331 
3332 // determine if a key is a numpad key (distinguishes the numpad
3333 // arrow keys from the non-numpad arrow keys, for example).
3334 BOOL AwtComponent::IsNumPadKey(UINT vkey, BOOL extended)
3335 {
3336     // Note: scancodes are the same for the numpad arrow keys and
3337     // the non-numpad arrow keys (also for PageUp, etc.).
3338     // The scancodes for the numpad divide and the non-numpad slash
3339     // are the same, but the wparams are different
3340 
3341     DTRACE_PRINTLN3("AwtComponent::IsNumPadKey  vkey = %d = 0x%x  extended = %d",
3342       vkey, vkey, extended);
3343 
3344     switch (vkey) {
3345       case VK_CLEAR:  // numpad 5 with numlock off
3346       case VK_NUMPAD0:
3347       case VK_NUMPAD1:
3348       case VK_NUMPAD2:
3349       case VK_NUMPAD3:
3350       case VK_NUMPAD4:
3351       case VK_NUMPAD5:
3352       case VK_NUMPAD6:
3353       case VK_NUMPAD7:
3354       case VK_NUMPAD8:
3355       case VK_NUMPAD9:
3356       case VK_MULTIPLY:
3357       case VK_ADD:
3358       case VK_SEPARATOR:  // numpad ,  not on US kbds
3359       case VK_SUBTRACT:
3360       case VK_DECIMAL:
3361       case VK_DIVIDE:
3362       case VK_NUMLOCK:
3363         return TRUE;
3364         break;
3365       case VK_END:
3366       case VK_PRIOR:  // PageUp
3367       case VK_NEXT:  // PageDown
3368       case VK_HOME:
3369       case VK_LEFT:
3370       case VK_UP:
3371       case VK_RIGHT:
3372       case VK_DOWN:
3373       case VK_INSERT:
3374       case VK_DELETE:
3375         // extended if non-numpad
3376         return (!extended);
3377         break;
3378       case VK_RETURN:  // extended if on numpad
3379         return (extended);
3380         break;
3381       default:
3382         break;
3383     }
3384 
3385     return FALSE;
3386 }
3387 static void
3388 resetKbdState( BYTE kstate[256]) {
3389     BYTE tmpState[256];
3390     WCHAR wc[2];
3391     memmove(tmpState, kstate, sizeof(kstate));
3392     tmpState[VK_SHIFT] = 0;
3393     tmpState[VK_CONTROL] = 0;
3394     tmpState[VK_MENU] = 0;
3395 
3396     ::ToUnicodeEx(VK_SPACE,::MapVirtualKey(VK_SPACE, 0), tmpState, wc, 2, 0,  GetKeyboardLayout(0));
3397 }
3398 
3399 // XXX in the update releases this is an addition to the unchanged existing code
3400 // After the call, a table will have a unicode associated with a windows virtual keycode
3401 // sans modifiers. With some further simplification, one can
3402 // derive java keycode from it, and anyway we will pass this unicode value
3403 // all the way up in a comment to a KeyEvent.
3404 void
3405 AwtComponent::BuildPrimaryDynamicTable() {
3406     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
3407     // XXX: how about that?
3408     //CriticalSection::Lock l(GetLock());
3409     //if (GetPeer(env) == NULL) {
3410     //    /* event received during termination. */
3411     //    return;
3412     //}
3413 
3414     HKL hkl = GetKeyboardLayout();
3415     UINT sc = 0;
3416     BYTE kbdState[AwtToolkit::KB_STATE_SIZE];
3417     memset(kbdState, 0, sizeof (kbdState));
3418 
3419     // Use JNI call to obtain java key code. We should keep a list
3420     // of currently available keycodes in a single place.
3421     static jclass extKeyCodesCls;
3422     if( extKeyCodesCls == NULL) {
3423         jclass extKeyCodesClsLocal = env->FindClass("sun/awt/ExtendedKeyCodes");
3424         DASSERT(extKeyCodesClsLocal);
3425         CHECK_NULL(extKeyCodesClsLocal);
3426         extKeyCodesCls = (jclass)env->NewGlobalRef(extKeyCodesClsLocal);
3427         env->DeleteLocalRef(extKeyCodesClsLocal);
3428     }
3429     static jmethodID getExtendedKeyCodeForChar;
3430     if (getExtendedKeyCodeForChar == NULL) {
3431         getExtendedKeyCodeForChar =
3432                   env->GetStaticMethodID(extKeyCodesCls, "getExtendedKeyCodeForChar", "(I)I");
3433         DASSERT(getExtendedKeyCodeForChar);
3434         CHECK_NULL(getExtendedKeyCodeForChar);
3435     }
3436     jint extJKC; //extended Java key code
3437 
3438     for (UINT i = 0; i < 256; i++) {
3439         dynPrimaryKeymap[i].wkey = i;
3440         dynPrimaryKeymap[i].jkey = java_awt_event_KeyEvent_VK_UNDEFINED;
3441         dynPrimaryKeymap[i].unicode = 0;
3442 
3443         if ((sc = MapVirtualKey (i, 0)) == 0) {
3444             dynPrimaryKeymap[i].scancode = 0;
3445             continue;
3446         }
3447         dynPrimaryKeymap[i].scancode = sc;
3448 
3449         // XXX process cases like VK_SHIFT etc.
3450         kbdState[i] = 0x80; // "key pressed".
3451         WCHAR wc[16];
3452         int k = ::ToUnicodeEx(i, sc, kbdState, wc, 16, 0, hkl);
3453         if (k == 1) {
3454             // unicode
3455             dynPrimaryKeymap[i].unicode = wc[0];
3456             if (dynPrimaryKeymap[i].jkey == java_awt_event_KeyEvent_VK_UNDEFINED) {
3457             // Convert unicode to java keycode.
3458                 //dynPrimaryKeymap[i].jkey = ((UINT)(wc[0]) + 0x01000000);
3459                 //
3460                 //XXX If this key in on the keypad, we should force a special value equal to
3461                 //XXX an old java keycode: but how to say if it is a keypad key?
3462                 //XXX We'll do it in WmKeyUp/Down.
3463                 extJKC = env->CallStaticIntMethod(extKeyCodesCls,
3464                                                   getExtendedKeyCodeForChar, (jint)(wc[0]));
3465                 dynPrimaryKeymap[i].jkey = extJKC;
3466             }
3467         }else if (k == -1) {
3468             // dead key: use charToDeadVKTable
3469             dynPrimaryKeymap[i].unicode = wc[0];
3470             resetKbdState( kbdState );
3471             for (const CharToVKEntry *map = charToDeadVKTable;  map->c != 0;  ++map) {
3472                 if (wc[0] == map->c) {
3473                     dynPrimaryKeymap[i].jkey = map->javaKey;
3474                     break;
3475                 }
3476             }
3477         } else if (k == 0) {
3478             // reset
3479             resetKbdState( kbdState );
3480         }else {
3481             // k > 1: this key does generate multiple characters. Ignore it.
3482             // An example: Arabic Lam and Alef ligature.
3483             // There will be no extended keycode and thus shortcuts for this  key.
3484             // XXX shouldn't we reset the kbd state?
3485 #ifdef DEBUG
3486             DTRACE_PRINTLN2("wkey 0x%02X (%d)", i,i);
3487 #endif
3488         }
3489         kbdState[i] = 0; // "key unpressed"
3490     }
3491 }
3492 void
3493 AwtComponent::UpdateDynPrimaryKeymap(UINT wkey, UINT jkeyLegacy, jint keyLocation, UINT modifiers)
3494 {
3495     if( wkey && wkey < 256 ) {
3496         if(keyLocation == java_awt_event_KeyEvent_KEY_LOCATION_NUMPAD) {
3497             // At the creation time,
3498             // dynPrimaryKeymap cannot distinguish between e.g. "/" and "NumPad /"
3499             dynPrimaryKeymap[wkey].jkey = jkeyLegacy;
3500         }
3501         if(dynPrimaryKeymap[wkey].jkey ==  java_awt_event_KeyEvent_VK_UNDEFINED) {
3502             // E.g. it is non-unicode key
3503             dynPrimaryKeymap[wkey].jkey = jkeyLegacy;
3504         }
3505     }
3506 }
3507 
3508 UINT AwtComponent::WindowsKeyToJavaChar(UINT wkey, UINT modifiers, TransOps ops, BOOL &isDeadKey)
3509 {
3510     static Hashtable transTable("VKEY translations");
3511     static Hashtable deadKeyFlagTable("Dead Key Flags");
3512     isDeadKey = FALSE;
3513 
3514     // Try to translate using last saved translation
3515     if (ops == LOAD) {
3516        void* deadKeyFlag = deadKeyFlagTable.remove(reinterpret_cast<void*>(static_cast<INT_PTR>(wkey)));
3517        void* value = transTable.remove(reinterpret_cast<void*>(static_cast<INT_PTR>(wkey)));
3518        if (value != NULL) {
3519            isDeadKey = static_cast<BOOL>(reinterpret_cast<INT_PTR>(deadKeyFlag));
3520            return static_cast<UINT>(reinterpret_cast<INT_PTR>(value));
3521        }
3522     }
3523 
3524     // If the windows key is a return, wkey will equal 13 ('\r')
3525     // In this case, we want to return 10 ('\n')
3526     // Since ToAscii would convert VK_RETURN to '\r', we need
3527     // to have a special case here.
3528     if (wkey == VK_RETURN)
3529         return '\n';
3530 
3531     // high order bit in keyboardState indicates whether the key is down
3532     static const BYTE KEY_STATE_DOWN = 0x80;
3533     BYTE    keyboardState[AwtToolkit::KB_STATE_SIZE];
3534     AwtToolkit::GetKeyboardState(keyboardState);
3535 
3536     // apply modifiers to keyboard state if necessary
3537     BOOL shiftIsDown = FALSE;
3538     if (modifiers) {
3539         shiftIsDown = modifiers & java_awt_event_InputEvent_SHIFT_DOWN_MASK;
3540         BOOL altIsDown = modifiers & java_awt_event_InputEvent_ALT_DOWN_MASK;
3541         BOOL ctrlIsDown = modifiers & java_awt_event_InputEvent_CTRL_DOWN_MASK;
3542 
3543         // Windows treats AltGr as Ctrl+Alt
3544         if (modifiers & java_awt_event_InputEvent_ALT_GRAPH_DOWN_MASK) {
3545             altIsDown = TRUE;
3546             ctrlIsDown = TRUE;
3547         }
3548 
3549         if (shiftIsDown) {
3550             keyboardState[VK_SHIFT] |= KEY_STATE_DOWN;
3551         }
3552 
3553         // fix for 4623376,4737679,4501485,4740906,4708221 (4173679/4122715)
3554         // Here we try to resolve a conflict with ::ToAsciiEx's translating
3555         // ALT+number key combinations. kdm@sarc.spb.su
3556         // yan: Do it for navigation keys only, otherwise some AltGr deadkeys fail.
3557         if( IsNavigationKey(wkey) ) {
3558             keyboardState[VK_MENU] &= ~KEY_STATE_DOWN;
3559         }
3560 
3561         if (ctrlIsDown)
3562         {
3563             if (altIsDown) {
3564                 // bugid 4215009: don't mess with AltGr == Ctrl + Alt
3565                 keyboardState[VK_CONTROL] |= KEY_STATE_DOWN;
3566             }
3567             else {
3568                 // bugid 4098210: old event model doesn't have KEY_TYPED
3569                 // events, so try to provide a meaningful character for
3570                 // Ctrl+<key>.  Take Ctrl into account only when we know
3571                 // that Ctrl+<key> will be an ASCII control.  Ignore by
3572                 // default.
3573                 keyboardState[VK_CONTROL] &= ~KEY_STATE_DOWN;
3574 
3575                 // Letters have Ctrl+<letter> counterparts.  According to
3576                 // <winuser.h> VK_A through VK_Z are the same as ASCII
3577                 // 'A' through 'Z'.
3578                 if (wkey >= 'A' && wkey <= 'Z') {
3579                     keyboardState[VK_CONTROL] |= KEY_STATE_DOWN;
3580                 }
3581                 else {
3582                     // Non-letter controls 033 to 037 are:
3583                     // ^[ (ESC), ^\ (FS), ^] (GS), ^^ (RS), and ^_ (US)
3584 
3585                     // Shift state bits returned by ::VkKeyScan in HIBYTE
3586                     static const UINT _VKS_SHIFT_MASK = 0x01;
3587                     static const UINT _VKS_CTRL_MASK = 0x02;
3588                     static const UINT _VKS_ALT_MASK = 0x04;
3589 
3590                     // Check to see whether there is a meaningful translation
3591                     TCHAR ch;
3592                     short vk;
3593                     for (ch = _T('\033'); ch < _T('\040'); ch++) {
3594                         vk = ::VkKeyScan(ch);
3595                         if (wkey == LOBYTE(vk)) {
3596                             UINT shiftState = HIBYTE(vk);
3597                             if ((shiftState & _VKS_CTRL_MASK) ||
3598                                 (!(shiftState & _VKS_SHIFT_MASK)
3599                                 == !shiftIsDown))
3600                             {
3601                                 keyboardState[VK_CONTROL] |= KEY_STATE_DOWN;
3602                             }
3603                             break;
3604                         }
3605                     }
3606                 }
3607             } // ctrlIsDown && altIsDown
3608         } // ctrlIsDown
3609     } // modifiers
3610 
3611     WORD wChar[2];
3612     int converted = 1;
3613     UINT ch = ::MapVirtualKeyEx(wkey, 2, GetKeyboardLayout());
3614     if (ch & 0x80000000) {
3615         // Dead key which is handled as a normal key
3616         isDeadKey = deadKeyActive = TRUE;
3617     } else if (deadKeyActive) {
3618         // We cannot use ::ToUnicodeEx if dead key is active because this will
3619         // break dead key function
3620         wChar[0] = shiftIsDown ? ch : tolower(ch);
3621     } else {
3622         UINT scancode = ::MapVirtualKey(wkey, 0);
3623         converted = ::ToUnicodeEx(wkey, scancode, keyboardState,
3624                                               wChar, 2, 0, GetKeyboardLayout());
3625     }
3626 
3627     UINT translation;
3628     BOOL deadKeyFlag = (converted == 2);
3629 
3630     // Dead Key
3631     if (converted < 0 || isDeadKey) {
3632         translation = java_awt_event_KeyEvent_CHAR_UNDEFINED;
3633     } else
3634     // No translation available -- try known conversions or else punt.
3635     if (converted == 0) {
3636         if (wkey == VK_DELETE) {
3637             translation = '\177';
3638         } else
3639         if (wkey >= VK_NUMPAD0 && wkey <= VK_NUMPAD9) {
3640             translation = '0' + wkey - VK_NUMPAD0;
3641         } else {
3642             translation = java_awt_event_KeyEvent_CHAR_UNDEFINED;
3643         }
3644     } else
3645     // the caller expects a Unicode character.
3646     if (converted > 0) {
3647         translation = wChar[0];
3648     }
3649     if (ops == SAVE) {
3650         transTable.put(reinterpret_cast<void*>(static_cast<INT_PTR>(wkey)),
3651                        reinterpret_cast<void*>(static_cast<INT_PTR>(translation)));
3652         if (deadKeyFlag) {
3653             deadKeyFlagTable.put(reinterpret_cast<void*>(static_cast<INT_PTR>(wkey)),
3654                          reinterpret_cast<void*>(static_cast<INT_PTR>(deadKeyFlag)));
3655         } else {
3656             deadKeyFlagTable.remove(reinterpret_cast<void*>(static_cast<INT_PTR>(wkey)));
3657         }
3658     }
3659 
3660     isDeadKey = deadKeyFlag;
3661     return translation;
3662 }
3663 
3664 MsgRouting AwtComponent::WmKeyDown(UINT wkey, UINT repCnt,
3665                                    UINT flags, BOOL system)
3666 {
3667     // VK_PROCESSKEY is a special value which means
3668     //          "Current IME wants to consume this KeyEvent"
3669     // Real key code is saved by IMM32.DLL and can be retrieved by
3670     // calling ImmGetVirtualKey();
3671     if (wkey == VK_PROCESSKEY) {
3672         return mrDoDefault;
3673     }
3674     MSG msg;
3675     InitMessage(&msg, (system ? WM_SYSKEYDOWN : WM_KEYDOWN),
3676                              wkey, MAKELPARAM(repCnt, flags));
3677 
3678     UINT modifiers = GetJavaModifiers();
3679     jint keyLocation = GetKeyLocation(wkey, flags);
3680     BOOL isDeadKey = FALSE;
3681     UINT character = WindowsKeyToJavaChar(wkey, modifiers, SAVE, isDeadKey);
3682     UINT jkey = WindowsKeyToJavaKey(wkey, modifiers, character, isDeadKey);
3683     UpdateDynPrimaryKeymap(wkey, jkey, keyLocation, modifiers);
3684 
3685 
3686     SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_PRESSED,
3687                              ::JVM_CurrentTimeMillis(NULL, 0), jkey, character,
3688                              modifiers, keyLocation, (jlong)wkey, &msg);
3689 
3690     // bugid 4724007: Windows does not create a WM_CHAR for the Del key
3691     // for some reason, so we need to create the KEY_TYPED event on the
3692     // WM_KEYDOWN.  Use null msg so the character doesn't get sent back
3693     // to the native window for processing (this event is synthesized
3694     // for Java - we don't want Windows trying to process it).
3695     if (jkey == java_awt_event_KeyEvent_VK_DELETE) {
3696         SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_TYPED,
3697                                  ::JVM_CurrentTimeMillis(NULL, 0),
3698                                  java_awt_event_KeyEvent_VK_UNDEFINED,
3699                                  character, modifiers,
3700                                  java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN, (jlong)0);
3701     }
3702 
3703     return mrConsume;
3704 }
3705 
3706 MsgRouting AwtComponent::WmKeyUp(UINT wkey, UINT repCnt,
3707                                  UINT flags, BOOL system)
3708 {
3709 
3710     // VK_PROCESSKEY is a special value which means
3711     //          "Current IME wants to consume this KeyEvent"
3712     // Real key code is saved by IMM32.DLL and can be retrieved by
3713     // calling ImmGetVirtualKey();
3714     if (wkey == VK_PROCESSKEY) {
3715         return mrDoDefault;
3716     }
3717     MSG msg;
3718     InitMessage(&msg, (system ? WM_SYSKEYUP : WM_KEYUP),
3719                              wkey, MAKELPARAM(repCnt, flags));
3720 
3721     UINT modifiers = GetJavaModifiers();
3722     jint keyLocation = GetKeyLocation(wkey, flags);
3723     BOOL isDeadKey = FALSE;
3724     UINT character = WindowsKeyToJavaChar(wkey, modifiers, LOAD, isDeadKey);
3725     UINT jkey = WindowsKeyToJavaKey(wkey, modifiers, character, isDeadKey);
3726     UpdateDynPrimaryKeymap(wkey, jkey, keyLocation, modifiers);
3727 
3728     SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_RELEASED,
3729                              ::JVM_CurrentTimeMillis(NULL, 0), jkey, character,
3730                              modifiers, keyLocation, (jlong)wkey, &msg);
3731     return mrConsume;
3732 }
3733 
3734 MsgRouting AwtComponent::WmInputLangChange(UINT charset, HKL hKeyboardLayout)
3735 {
3736     // Normally we would be able to use charset and TranslateCharSetInfo
3737     // to get a code page that should be associated with this keyboard
3738     // layout change. However, there seems to be an NT 4.0 bug associated
3739     // with the WM_INPUTLANGCHANGE message, which makes the charset parameter
3740     // unreliable, especially on Asian systems. Our workaround uses the
3741     // keyboard layout handle instead.
3742     m_hkl = hKeyboardLayout;
3743     m_idLang = LOWORD(hKeyboardLayout); // lower word of HKL is LANGID
3744     m_CodePage = LangToCodePage(m_idLang);
3745     BuildDynamicKeyMapTable();  // compute new mappings for VK_OEM
3746     BuildPrimaryDynamicTable();
3747     return mrConsume;           // do not propagate to children
3748 }
3749 
3750 // Convert Language ID to CodePage
3751 UINT AwtComponent::LangToCodePage(LANGID idLang)
3752 {
3753     TCHAR strCodePage[MAX_ACP_STR_LEN];
3754     // use the LANGID to create a LCID
3755     LCID idLocale = MAKELCID(idLang, SORT_DEFAULT);
3756     // get the ANSI code page associated with this locale
3757     if (GetLocaleInfo(idLocale, LOCALE_IDEFAULTANSICODEPAGE, strCodePage, sizeof(strCodePage)/sizeof(TCHAR)) > 0 )
3758         return _ttoi(strCodePage);
3759     else
3760         return GetACP();
3761 }
3762 
3763 
3764 MsgRouting AwtComponent::WmIMEChar(UINT character, UINT repCnt, UINT flags, BOOL system)
3765 {
3766     // We will simply create Java events here.
3767     WCHAR unicodeChar = character;
3768     MSG msg;
3769     InitMessage(&msg, WM_IME_CHAR, character,
3770                               MAKELPARAM(repCnt, flags));
3771 
3772     jint modifiers = GetJavaModifiers();
3773     SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_TYPED,
3774                              ::JVM_CurrentTimeMillis(NULL, 0),
3775                              java_awt_event_KeyEvent_VK_UNDEFINED,
3776                              unicodeChar, modifiers,
3777                              java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN, (jlong)0,
3778                              &msg);
3779     return mrConsume;
3780 }
3781 
3782 MsgRouting AwtComponent::WmChar(UINT character, UINT repCnt, UINT flags,
3783                                 BOOL system)
3784 {
3785     deadKeyActive = FALSE;
3786 
3787     // Will only get WmChar messages with DBCS if we create them for
3788     // an Edit class in the WmForwardChar method. These synthesized
3789     // DBCS chars are ok to pass on directly to the default window
3790     // procedure. They've already been filtered through the Java key
3791     // event queue. We will never get the trail byte since the edit
3792     // class will PeekMessage(&msg, hwnd, WM_CHAR, WM_CHAR,
3793     // PM_REMOVE).  I would like to be able to pass this character off
3794     // via WM_AWT_FORWARD_BYTE, but the Edit classes don't seem to
3795     // like that.
3796 
3797     // We will simply create Java events here.
3798     UINT message = system ? WM_SYSCHAR : WM_CHAR;
3799 
3800     // The Alt modifier is reported in the 29th bit of the lParam,
3801     // i.e., it is the 13th bit of `flags' (which is HIWORD(lParam)).
3802     bool alt_is_down = (flags & (1<<13)) != 0;
3803 
3804     // Fix for bug 4141621, corrected by fix for bug 6223726: Alt+space doesn't invoke system menu
3805     // We should not pass this particular combination to Java.
3806 
3807     if (system && alt_is_down) {
3808         if (character == VK_SPACE) {
3809             return mrDoDefault;
3810         }
3811     }
3812 
3813     // If this is a WM_CHAR (non-system) message, then the Alt flag
3814     // indicates that the character was typed using an AltGr key
3815     // (which Windows treats as Ctrl+Alt), so in this case we do NOT
3816     // pass the Ctrl and Alt modifiers to Java, but instead we
3817     // replace them with Java's AltGraph modifier.  Note: the AltGraph
3818     // modifier does not exist in 1.1.x releases.
3819     jint modifiers = GetJavaModifiers();
3820     if (!system && alt_is_down) {
3821         // character typed with AltGraph
3822         modifiers &= ~(java_awt_event_InputEvent_ALT_DOWN_MASK
3823                        | java_awt_event_InputEvent_CTRL_DOWN_MASK);
3824         modifiers |= java_awt_event_InputEvent_ALT_GRAPH_DOWN_MASK;
3825     }
3826 
3827     WCHAR unicodeChar = character;
3828 
3829     // Kludge: Combine pending single byte with this char for some Chinese IMEs
3830     if (m_PendingLeadByte != 0) {
3831         character = (m_PendingLeadByte & 0x00ff) | (character << 8);
3832         m_PendingLeadByte = 0;
3833         ::MultiByteToWideChar(GetCodePage(), 0, (CHAR*)&character, 2,
3834                           &unicodeChar, 1);
3835     }
3836 
3837     if (unicodeChar == VK_RETURN) {
3838         // Enter key generates \r in windows, but \n is required in java
3839         unicodeChar = java_awt_event_KeyEvent_VK_ENTER;
3840     }
3841     MSG msg;
3842     InitMessage(&msg, message, character,
3843                               MAKELPARAM(repCnt, flags));
3844     SendKeyEventToFocusOwner(java_awt_event_KeyEvent_KEY_TYPED,
3845                              ::JVM_CurrentTimeMillis(NULL, 0),
3846                              java_awt_event_KeyEvent_VK_UNDEFINED,
3847                              unicodeChar, modifiers,
3848                              java_awt_event_KeyEvent_KEY_LOCATION_UNKNOWN, (jlong)0,
3849                              &msg);
3850     return mrConsume;
3851 }
3852 
3853 MsgRouting AwtComponent::WmForwardChar(WCHAR character, LPARAM lParam,
3854                                        BOOL synthetic)
3855 {
3856     // just post WM_CHAR with unicode key value
3857     DefWindowProc(WM_CHAR, (WPARAM)character, lParam);
3858     return mrConsume;
3859 }
3860 
3861 MsgRouting AwtComponent::WmPaste()
3862 {
3863     return mrDoDefault;
3864 }
3865 
3866 // support IME Composition messages
3867 void AwtComponent::SetCompositionWindow(RECT& r)
3868 {
3869     HWND hwnd = ImmGetHWnd();
3870     HIMC hIMC = ImmGetContext(hwnd);
3871     if (hIMC == NULL) {
3872         return;
3873     }
3874     COMPOSITIONFORM cf = {CFS_DEFAULT, {0, 0}, {0, 0, 0, 0}};
3875     LOGFONT lf;
3876     HFONT hFont = (HFONT) GetStockObject(DEFAULT_GUI_FONT);
3877     if (GetObject(hFont, sizeof(lf), (LPVOID)&lf) == sizeof(lf)) {
3878         ImmSetCompositionFont(hIMC, &lf);
3879     }
3880     ImmSetCompositionWindow(hIMC, &cf);
3881     ImmReleaseContext(hwnd, hIMC);
3882 }
3883 
3884 void AwtComponent::OpenCandidateWindow(int x, int y)
3885 {
3886     UINT bits = 1;
3887     POINT p = {0, 0}; // upper left corner of the client area
3888     HWND hWnd = ImmGetHWnd();
3889     if (!::IsWindowVisible(hWnd)) {
3890         return;
3891     }
3892     HWND hTop = GetTopLevelParentForWindow(hWnd);
3893     ::ClientToScreen(hTop, &p);
3894     int sx = ScaleUpX(x) - p.x;
3895     int sy = ScaleUpY(y) - p.y;
3896     if (!m_bitsCandType) {
3897         SetCandidateWindow(m_bitsCandType, sx, sy);
3898         return;
3899     }
3900     for (int iCandType=0; iCandType<32; iCandType++, bits<<=1) {
3901         if ( m_bitsCandType & bits )
3902             SetCandidateWindow(iCandType, sx, sy);
3903     }
3904 }
3905 
3906 void AwtComponent::SetCandidateWindow(int iCandType, int x, int y)
3907 {
3908     HWND hwnd = ImmGetHWnd();
3909     HIMC hIMC = ImmGetContext(hwnd);
3910     if (hIMC) {
3911         CANDIDATEFORM cf;
3912         cf.dwStyle = CFS_POINT;
3913         ImmGetCandidateWindow(hIMC, 0, &cf);
3914         if (x != cf.ptCurrentPos.x || y != cf.ptCurrentPos.y) {
3915             cf.dwIndex = iCandType;
3916             cf.dwStyle = CFS_POINT;
3917             cf.ptCurrentPos = {x, y};
3918             cf.rcArea = {0, 0, 0, 0};
3919             ImmSetCandidateWindow(hIMC, &cf);
3920         }
3921         COMPOSITIONFORM cfr;
3922         cfr.dwStyle = CFS_POINT;
3923         ImmGetCompositionWindow(hIMC, &cfr);
3924         if (x != cfr.ptCurrentPos.x || y != cfr.ptCurrentPos.y) {
3925             cfr.dwStyle = CFS_POINT;
3926             cfr.ptCurrentPos = {x, y};
3927             cfr.rcArea = {0, 0, 0, 0};
3928             ImmSetCompositionWindow(hIMC, &cfr);
3929         }
3930         ImmReleaseContext(hwnd, hIMC);
3931     }
3932 }
3933 
3934 MsgRouting AwtComponent::WmImeSetContext(BOOL fSet, LPARAM *lplParam)
3935 {
3936     // If the Windows input context is disabled, do not let Windows
3937     // display any UIs.
3938     HWND hwnd = ImmGetHWnd();
3939     HIMC hIMC = ImmGetContext(hwnd);
3940     if (hIMC == NULL) {
3941         *lplParam = 0;
3942         return mrDoDefault;
3943     }
3944     ImmReleaseContext(hwnd, hIMC);
3945 
3946     if (fSet) {
3947         LPARAM lParam = *lplParam;
3948         if (!m_useNativeCompWindow) {
3949             // stop to draw native composing window.
3950             *lplParam &= ~ISC_SHOWUICOMPOSITIONWINDOW;
3951         }
3952     }
3953     return mrDoDefault;
3954 }
3955 
3956 MsgRouting AwtComponent::WmImeNotify(WPARAM subMsg, LPARAM bitsCandType)
3957 {
3958     if (!m_useNativeCompWindow) {
3959         if (subMsg == IMN_OPENCANDIDATE || subMsg == IMN_CHANGECANDIDATE) {
3960             m_bitsCandType = bitsCandType;
3961             InquireCandidatePosition();
3962         } else if (subMsg == IMN_OPENSTATUSWINDOW ||
3963                    subMsg == WM_IME_STARTCOMPOSITION ||
3964                    subMsg == IMN_SETCANDIDATEPOS) {
3965             InquireCandidatePosition();
3966         }
3967     }
3968     return mrDoDefault;
3969 }
3970 
3971 MsgRouting AwtComponent::WmImeStartComposition()
3972 {
3973     if (m_useNativeCompWindow) {
3974         RECT rc;
3975         ::GetClientRect(GetHWnd(), &rc);
3976         SetCompositionWindow(rc);
3977         return mrDoDefault;
3978     } else
3979         return mrConsume;
3980 }
3981 
3982 MsgRouting AwtComponent::WmImeEndComposition()
3983 {
3984     if (m_useNativeCompWindow)   return mrDoDefault;
3985 
3986     SendInputMethodEvent(
3987         java_awt_event_InputMethodEvent_INPUT_METHOD_TEXT_CHANGED,
3988         NULL, 0, NULL, NULL, 0, NULL, NULL, 0, 0, 0 );
3989     return mrConsume;
3990 }
3991 
3992 MsgRouting AwtComponent::WmImeComposition(WORD wChar, LPARAM flags)
3993 {
3994     if (m_useNativeCompWindow)   return mrDoDefault;
3995 
3996     int*      bndClauseW = NULL;
3997     jstring*  readingClauseW = NULL;
3998     int*      bndAttrW = NULL;
3999     BYTE*     valAttrW = NULL;
4000     int       cClauseW = 0;
4001     AwtInputTextInfor* textInfor = NULL;
4002 
4003     try {
4004         HWND hwnd = ImmGetHWnd();
4005         HIMC hIMC = ImmGetContext(hwnd);
4006         DASSERT(hIMC!=0);
4007 
4008         textInfor = new AwtInputTextInfor;
4009         textInfor->GetContextData(hIMC, flags);
4010         ImmReleaseContext(hwnd, hIMC);
4011 
4012         jstring jtextString = textInfor->GetText();
4013         /* The conditions to send the input method event to AWT EDT are:
4014            1. Whenever there is a composition message sent regarding whether
4015            the composition text is NULL or not. See details at bug 6222692.
4016            2. When there is a committed message sent, in which case, we have to
4017            check whether the committed string is NULL or not. If the committed string
4018            is NULL, there is no need to send any input method event.
4019            (Minor note: 'jtextString' returned is the merged string in the case of
4020            partial commit.)
4021         */
4022         if ((flags & GCS_RESULTSTR && jtextString != NULL) ||
4023             (flags & GCS_COMPSTR)) {
4024             int       cursorPosW = textInfor->GetCursorPosition();
4025             // In order not to delete the readingClauseW in the catch clause,
4026             // calling GetAttributeInfor before GetClauseInfor.
4027             int       cAttrW = textInfor->GetAttributeInfor(bndAttrW, valAttrW);
4028             cClauseW = textInfor->GetClauseInfor(bndClauseW, readingClauseW);
4029 
4030             /* Send INPUT_METHOD_TEXT_CHANGED event to the WInputMethod which in turn sends
4031                the event to AWT EDT.
4032 
4033                The last two paremeters are set to equal since we don't have recommendations for
4034                the visible position within the current composed text. See details at
4035                java.awt.event.InputMethodEvent.
4036             */
4037             SendInputMethodEvent(java_awt_event_InputMethodEvent_INPUT_METHOD_TEXT_CHANGED,
4038                                  jtextString,
4039                                  cClauseW, bndClauseW, readingClauseW,
4040                                  cAttrW, bndAttrW, valAttrW,
4041                                  textInfor->GetCommittedTextLength(),
4042                                  cursorPosW, cursorPosW);
4043         }
4044     } catch (...) {
4045         // since GetClauseInfor and GetAttributeInfor could throw exception, we have to release
4046         // the pointer here.
4047         delete [] bndClauseW;
4048         delete [] readingClauseW;
4049         delete [] bndAttrW;
4050         delete [] valAttrW;
4051         throw;
4052     }
4053 
4054     /* Free the storage allocated. Since jtextString won't be passed from threads
4055      *  to threads, we just use the local ref and it will be deleted within the destructor
4056      *  of AwtInputTextInfor object.
4057      */
4058     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
4059     if (cClauseW && readingClauseW) {
4060         for (int i = 0; i < cClauseW; i ++) {
4061             if (readingClauseW[i]) {
4062                 env->DeleteLocalRef(readingClauseW[i]);
4063             }
4064         }
4065     }
4066     delete [] bndClauseW;
4067     delete [] readingClauseW;
4068     delete [] bndAttrW;
4069     delete [] valAttrW;
4070     delete textInfor;
4071 
4072     return mrConsume;
4073 }
4074 
4075 //
4076 // generate and post InputMethodEvent
4077 //
4078 void AwtComponent::SendInputMethodEvent(jint id, jstring text,
4079                                         int cClause, int* rgClauseBoundary, jstring* rgClauseReading,
4080                                         int cAttrBlock, int* rgAttrBoundary, BYTE *rgAttrValue,
4081                                         int commitedTextLength, int caretPos, int visiblePos)
4082 {
4083     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
4084 
4085     // assumption for array type casting
4086     DASSERT(sizeof(int)==sizeof(jint));
4087     DASSERT(sizeof(BYTE)==sizeof(jbyte));
4088 
4089     // caluse information
4090     jintArray clauseBoundary = NULL;
4091     jobjectArray clauseReading = NULL;
4092     if (cClause && rgClauseBoundary && rgClauseReading) {
4093         // convert clause boundary offset array to java array
4094         clauseBoundary = env->NewIntArray(cClause+1);
4095         DASSERT(clauseBoundary);
4096         CHECK_NULL(clauseBoundary);
4097         env->SetIntArrayRegion(clauseBoundary, 0, cClause+1, (jint *)rgClauseBoundary);
4098         DASSERT(!safe_ExceptionOccurred(env));
4099 
4100         // convert clause reading string array to java array
4101         jclass stringCls = JNU_ClassString(env);
4102         DASSERT(stringCls);
4103         CHECK_NULL(stringCls);
4104         clauseReading = env->NewObjectArray(cClause, stringCls, NULL);
4105         DASSERT(clauseReading);
4106         CHECK_NULL(clauseReading);
4107         for (int i=0; i<cClause; i++)   env->SetObjectArrayElement(clauseReading, i, rgClauseReading[i]);
4108         DASSERT(!safe_ExceptionOccurred(env));
4109     }
4110 
4111 
4112     // attrubute value definition in WInputMethod.java must be equal to that in IMM.H
4113     DASSERT(ATTR_INPUT==sun_awt_windows_WInputMethod_ATTR_INPUT);
4114     DASSERT(ATTR_TARGET_CONVERTED==sun_awt_windows_WInputMethod_ATTR_TARGET_CONVERTED);
4115     DASSERT(ATTR_CONVERTED==sun_awt_windows_WInputMethod_ATTR_CONVERTED);
4116     DASSERT(ATTR_TARGET_NOTCONVERTED==sun_awt_windows_WInputMethod_ATTR_TARGET_NOTCONVERTED);
4117     DASSERT(ATTR_INPUT_ERROR==sun_awt_windows_WInputMethod_ATTR_INPUT_ERROR);
4118 
4119     // attribute information
4120     jintArray attrBoundary = NULL;
4121     jbyteArray attrValue = NULL;
4122     if (cAttrBlock && rgAttrBoundary && rgAttrValue) {
4123         // convert attribute boundary offset array to java array
4124         attrBoundary = env->NewIntArray(cAttrBlock+1);
4125         DASSERT(attrBoundary);
4126         CHECK_NULL(attrBoundary);
4127         env->SetIntArrayRegion(attrBoundary, 0, cAttrBlock+1, (jint *)rgAttrBoundary);
4128         DASSERT(!safe_ExceptionOccurred(env));
4129 
4130         // convert attribute value byte array to java array
4131         attrValue = env->NewByteArray(cAttrBlock);
4132         DASSERT(attrValue);
4133         CHECK_NULL(attrValue);
4134         env->SetByteArrayRegion(attrValue, 0, cAttrBlock, (jbyte *)rgAttrValue);
4135         DASSERT(!safe_ExceptionOccurred(env));
4136     }
4137 
4138 
4139     // get global reference of WInputMethod class (run only once)
4140     static jclass wInputMethodCls = NULL;
4141     if (wInputMethodCls == NULL) {
4142         jclass wInputMethodClsLocal = env->FindClass("sun/awt/windows/WInputMethod");
4143         DASSERT(wInputMethodClsLocal);
4144         CHECK_NULL(wInputMethodClsLocal);
4145         wInputMethodCls = (jclass)env->NewGlobalRef(wInputMethodClsLocal);
4146         env->DeleteLocalRef(wInputMethodClsLocal);
4147     }
4148 
4149     // get method ID of sendInputMethodEvent() (run only once)
4150     static jmethodID sendIMEventMid = 0;
4151     if (sendIMEventMid == 0) {
4152         sendIMEventMid =  env->GetMethodID(wInputMethodCls, "sendInputMethodEvent",
4153                                            "(IJLjava/lang/String;[I[Ljava/lang/String;[I[BIII)V");
4154         DASSERT(sendIMEventMid);
4155         CHECK_NULL(sendIMEventMid);
4156     }
4157 
4158     // call m_InputMethod.sendInputMethod()
4159     env->CallVoidMethod(m_InputMethod, sendIMEventMid, id, ::JVM_CurrentTimeMillis(NULL, 0),
4160                         text, clauseBoundary, clauseReading, attrBoundary,
4161                         attrValue, commitedTextLength, caretPos, visiblePos);
4162     if (safe_ExceptionOccurred(env))   env->ExceptionDescribe();
4163     DASSERT(!safe_ExceptionOccurred(env));
4164 
4165 }
4166 
4167 
4168 
4169 //
4170 // Inquires candidate position according to the composed text
4171 //
4172 void AwtComponent::InquireCandidatePosition()
4173 {
4174     if (!::IsWindowVisible(GetHWnd())) {
4175         return;
4176     }
4177     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
4178 
4179     // get global reference of WInputMethod class (run only once)
4180     static jclass wInputMethodCls = NULL;
4181     if (wInputMethodCls == NULL) {
4182         jclass wInputMethodClsLocal = env->FindClass("sun/awt/windows/WInputMethod");
4183         DASSERT(wInputMethodClsLocal);
4184         CHECK_NULL(wInputMethodClsLocal);
4185         wInputMethodCls = (jclass)env->NewGlobalRef(wInputMethodClsLocal);
4186         env->DeleteLocalRef(wInputMethodClsLocal);
4187     }
4188 
4189     // get method ID of sendInputMethodEvent() (run only once)
4190     static jmethodID inqCandPosMid = 0;
4191     if (inqCandPosMid == 0) {
4192         inqCandPosMid =  env->GetMethodID(wInputMethodCls, "inquireCandidatePosition", "()V");
4193         DASSERT(!safe_ExceptionOccurred(env));
4194         DASSERT(inqCandPosMid);
4195         CHECK_NULL(inqCandPosMid);
4196     }
4197 
4198     // call m_InputMethod.sendInputMethod()
4199     jobject candPos = env->CallObjectMethod(m_InputMethod, inqCandPosMid);
4200     DASSERT(!safe_ExceptionOccurred(env));
4201 }
4202 
4203 HWND AwtComponent::ImmGetHWnd()
4204 {
4205     HWND proxy = GetProxyFocusOwner();
4206     return (proxy != NULL) ? proxy : GetHWnd();
4207 }
4208 
4209 HIMC AwtComponent::ImmAssociateContext(HIMC himc)
4210 {
4211     return ::ImmAssociateContext(ImmGetHWnd(), himc);
4212 }
4213 
4214 HWND AwtComponent::GetProxyFocusOwner()
4215 {
4216     AwtWindow *window = GetContainer();
4217     if (window != 0) {
4218         AwtFrame *owner = window->GetOwningFrameOrDialog();
4219         if (owner != 0) {
4220             return owner->GetProxyFocusOwner();
4221         } else if (!window->IsSimpleWindow()) { // isn't an owned simple window
4222             return ((AwtFrame*)window)->GetProxyFocusOwner();
4223         }
4224     }
4225     return (HWND)NULL;
4226 }
4227 
4228 /* Redirects message to the focus proxy, if any */
4229 void AwtComponent::CallProxyDefWindowProc(UINT message, WPARAM wParam,
4230     LPARAM lParam, LRESULT &retVal, MsgRouting &mr)
4231 {
4232     if (mr != mrConsume)  {
4233         HWND proxy = GetProxyFocusOwner();
4234         if (proxy != NULL && ::IsWindowEnabled(proxy)) {
4235             retVal = ::DefWindowProc(proxy, message, wParam, lParam);
4236             mr = mrConsume;
4237         }
4238     }
4239 }
4240 
4241 MsgRouting AwtComponent::WmCommand(UINT id, HWND hWndChild, UINT notifyCode)
4242 {
4243     /* Menu/Accelerator */
4244     if (hWndChild == 0) {
4245         AwtObject* obj = AwtToolkit::GetInstance().LookupCmdID(id);
4246         if (obj == NULL) {
4247             return mrConsume;
4248         }
4249         DASSERT(((AwtMenuItem*)obj)->GetID() == id);
4250         obj->DoCommand();
4251         return mrConsume;
4252     }
4253     /* Child id notification */
4254     else {
4255         AwtComponent* child = AwtComponent::GetComponent(hWndChild);
4256         if (child) {
4257             child->WmNotify(notifyCode);
4258         }
4259     }
4260     return mrDoDefault;
4261 }
4262 
4263 MsgRouting AwtComponent::WmNotify(UINT notifyCode)
4264 {
4265     return mrDoDefault;
4266 }
4267 
4268 MsgRouting AwtComponent::WmCompareItem(UINT ctrlId,
4269                                        COMPAREITEMSTRUCT &compareInfo,
4270                                        LRESULT &result)
4271 {
4272     AwtComponent* child = AwtComponent::GetComponent(compareInfo.hwndItem);
4273     if (child == this) {
4274         /* DoCallback("handleItemDelete", */
4275     }
4276     else if (child) {
4277         return child->WmCompareItem(ctrlId, compareInfo, result);
4278     }
4279     return mrConsume;
4280 }
4281 
4282 MsgRouting AwtComponent::WmDeleteItem(UINT ctrlId,
4283                                       DELETEITEMSTRUCT &deleteInfo)
4284 {
4285     /*
4286      * Workaround for NT 4.0 bug -- if SetWindowPos is called on a AwtList
4287      * window, a WM_DELETEITEM message is sent to its parent with a window
4288      * handle of one of the list's child windows.  The property lookup
4289      * succeeds, but the HWNDs don't match.
4290      */
4291     if (deleteInfo.hwndItem == NULL) {
4292         return mrConsume;
4293     }
4294     AwtComponent* child = (AwtComponent *)AwtComponent::GetComponent(deleteInfo.hwndItem);
4295 
4296     if (child && child->GetHWnd() != deleteInfo.hwndItem) {
4297         return mrConsume;
4298     }
4299 
4300     if (child == this) {
4301         /*DoCallback("handleItemDelete", */
4302     }
4303     else if (child) {
4304         return child->WmDeleteItem(ctrlId, deleteInfo);
4305     }
4306     return mrConsume;
4307 }
4308 
4309 MsgRouting AwtComponent::WmDrawItem(UINT ctrlId, DRAWITEMSTRUCT &drawInfo)
4310 {
4311     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
4312 
4313     if (drawInfo.CtlType == ODT_MENU) {
4314         if (IsMenu((HMENU)drawInfo.hwndItem) && drawInfo.itemData != 0) {
4315             AwtMenu* menu = (AwtMenu*)(drawInfo.itemData);
4316             menu->DrawItem(drawInfo);
4317         }
4318     } else {
4319         return OwnerDrawItem(ctrlId, drawInfo);
4320     }
4321     return mrConsume;
4322 }
4323 
4324 MsgRouting AwtComponent::WmMeasureItem(UINT ctrlId,
4325                                        MEASUREITEMSTRUCT &measureInfo)
4326 {
4327     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
4328 
4329     if (measureInfo.CtlType == ODT_MENU) {
4330         if (measureInfo.itemData != 0) {
4331             AwtMenu* menu = (AwtMenu*)(measureInfo.itemData);
4332             HDC hDC = ::GetDC(GetHWnd());
4333             /* menu->MeasureItem(env, hDC, measureInfo); */
4334             menu->MeasureItem(hDC, measureInfo);
4335             ::ReleaseDC(GetHWnd(), hDC);
4336         }
4337     } else {
4338         return OwnerMeasureItem(ctrlId, measureInfo);
4339     }
4340     return mrConsume;
4341 }
4342 
4343 MsgRouting AwtComponent::OwnerDrawItem(UINT ctrlId,
4344     DRAWITEMSTRUCT &drawInfo)
4345 {
4346     AwtComponent* child = AwtComponent::GetComponent(drawInfo.hwndItem);
4347     if (child == this) {
4348         /* DoCallback("handleItemDelete", */
4349     } else if (child != NULL) {
4350         return child->WmDrawItem(ctrlId, drawInfo);
4351     }
4352     return mrConsume;
4353 }
4354 
4355 MsgRouting AwtComponent::OwnerMeasureItem(UINT ctrlId,
4356     MEASUREITEMSTRUCT &measureInfo)
4357 {
4358     HWND  hChild = ::GetDlgItem(GetHWnd(), measureInfo.CtlID);
4359     AwtComponent* child = AwtComponent::GetComponent(hChild);
4360     /*
4361      * If the parent cannot find the child's instance from its handle,
4362      * maybe the child is in its creation.  So the child must be searched
4363      * from the list linked before the child's creation.
4364      */
4365     if (child == NULL) {
4366         child = SearchChild((UINT)ctrlId);
4367     }
4368 
4369     if (child == this) {
4370     /* DoCallback("handleItemDelete",  */
4371     }
4372     else if (child) {
4373         return child->WmMeasureItem(ctrlId, measureInfo);
4374     }
4375     return mrConsume;
4376 }
4377 
4378 /* for WmDrawItem method of Label, Button and Checkbox */
4379 void AwtComponent::DrawWindowText(HDC hDC, jobject font, jstring text,
4380                                   int x, int y)
4381 {
4382     int nOldBkMode = ::SetBkMode(hDC,TRANSPARENT);
4383     DASSERT(nOldBkMode != 0);
4384     AwtFont::drawMFString(hDC, font, text, x, y, GetCodePage());
4385     VERIFY(::SetBkMode(hDC,nOldBkMode));
4386 }
4387 
4388 /*
4389  * Draw text in gray (the color being set to COLOR_GRAYTEXT) when the
4390  * component is disabled.  Used only for label, checkbox and button in
4391  * OWNER_DRAW.  It draws the text in emboss.
4392  */
4393 void AwtComponent::DrawGrayText(HDC hDC, jobject font, jstring text,
4394                                 int x, int y)
4395 {
4396     ::SetTextColor(hDC, ::GetSysColor(COLOR_BTNHILIGHT));
4397     AwtComponent::DrawWindowText(hDC, font, text, x+1, y+1);
4398     ::SetTextColor(hDC, ::GetSysColor(COLOR_BTNSHADOW));
4399     AwtComponent::DrawWindowText(hDC, font, text, x, y);
4400 }
4401 
4402 /* for WmMeasureItem method of List and Choice */
4403 jstring AwtComponent::GetItemString(JNIEnv *env, jobject target, jint index)
4404 {
4405     jstring str = (jstring)JNU_CallMethodByName(env, NULL, target, "getItemImpl",
4406                                                 "(I)Ljava/lang/String;",
4407                                                 index).l;
4408     DASSERT(!safe_ExceptionOccurred(env));
4409     return str;
4410 }
4411 
4412 /* for WmMeasureItem method of List and Choice */
4413 void AwtComponent::MeasureListItem(JNIEnv *env,
4414                                    MEASUREITEMSTRUCT &measureInfo)
4415 {
4416     if (env->EnsureLocalCapacity(1) < 0) {
4417         return;
4418     }
4419     jobject dimension = PreferredItemSize(env);
4420     DASSERT(dimension);
4421     measureInfo.itemWidth =
4422       env->GetIntField(dimension, AwtDimension::widthID);
4423     measureInfo.itemHeight =
4424       env->GetIntField(dimension, AwtDimension::heightID);
4425     env->DeleteLocalRef(dimension);
4426 }
4427 
4428 /* for WmDrawItem method of List and Choice */
4429 void AwtComponent::DrawListItem(JNIEnv *env, DRAWITEMSTRUCT &drawInfo)
4430 {
4431     if (env->EnsureLocalCapacity(3) < 0) {
4432         return;
4433     }
4434     jobject peer = GetPeer(env);
4435     jobject target = env->GetObjectField(peer, AwtObject::targetID);
4436 
4437     HDC hDC = drawInfo.hDC;
4438     RECT rect = drawInfo.rcItem;
4439 
4440     BOOL bEnabled = isEnabled();
4441     BOOL unfocusableChoice = (drawInfo.itemState & ODS_COMBOBOXEDIT) && !IsFocusable();
4442     DWORD crBack, crText;
4443     if (drawInfo.itemState & ODS_SELECTED){
4444         /* Set background and text colors for selected item */
4445         crBack = ::GetSysColor (COLOR_HIGHLIGHT);
4446         crText = ::GetSysColor (COLOR_HIGHLIGHTTEXT);
4447     } else {
4448         /* Set background and text colors for unselected item */
4449         crBack = GetBackgroundColor();
4450         crText = bEnabled ? GetColor() : ::GetSysColor(COLOR_GRAYTEXT);
4451     }
4452     if (unfocusableChoice) {
4453         //6190728. Shouldn't draw selection field (edit control) of an owner-drawn combo box.
4454         crBack = GetBackgroundColor();
4455         crText = bEnabled ? GetColor() : ::GetSysColor(COLOR_GRAYTEXT);
4456     }
4457 
4458     /* Fill item rectangle with background color */
4459     HBRUSH hbrBack = ::CreateSolidBrush (crBack);
4460     DASSERT(hbrBack);
4461     /* 6190728. Shouldn't draw any kind of rectangle around selection field
4462      * (edit control) of an owner-drawn combo box while unfocusable
4463      */
4464     if (!unfocusableChoice){
4465         VERIFY(::FillRect (hDC, &rect, hbrBack));
4466     }
4467     VERIFY(::DeleteObject (hbrBack));
4468 
4469     /* Set current background and text colors */
4470     ::SetBkColor (hDC, crBack);
4471     ::SetTextColor (hDC, crText);
4472 
4473     /*draw string (with left margin of 1 point) */
4474     if ((int) (drawInfo.itemID) >= 0) {
4475             jobject font = GET_FONT(target, peer);
4476             jstring text = GetItemString(env, target, drawInfo.itemID);
4477             if (env->ExceptionCheck()) {
4478                 env->DeleteLocalRef(font);
4479                 env->DeleteLocalRef(target);
4480                 return;
4481             }
4482             SIZE size = AwtFont::getMFStringSize(hDC, font, text);
4483             AwtFont::drawMFString(hDC, font, text,
4484                                   (GetRTL()) ? rect.right - size.cx - 1
4485                                              : rect.left + 1,
4486                                   (rect.top + rect.bottom - size.cy) / 2,
4487                                   GetCodePage());
4488             env->DeleteLocalRef(font);
4489             env->DeleteLocalRef(text);
4490     }
4491     if ((drawInfo.itemState & ODS_FOCUS)  &&
4492         (drawInfo.itemAction & (ODA_FOCUS | ODA_DRAWENTIRE))) {
4493       if (!unfocusableChoice){
4494           if(::DrawFocusRect(hDC, &rect) == 0)
4495               VERIFY(::GetLastError() == 0);
4496       }
4497     }
4498     env->DeleteLocalRef(target);
4499 }
4500 
4501 /* for MeasureListItem method and WmDrawItem method of Checkbox */
4502 jint AwtComponent::GetFontHeight(JNIEnv *env)
4503 {
4504     if (env->EnsureLocalCapacity(4) < 0) {
4505         return NULL;
4506     }
4507     jobject self = GetPeer(env);
4508     jobject target = env->GetObjectField(self, AwtObject::targetID);
4509 
4510     jobject font = GET_FONT(target, self);
4511     jobject toolkit = env->CallObjectMethod(target,
4512                                             AwtComponent::getToolkitMID);
4513 
4514     DASSERT(!safe_ExceptionOccurred(env));
4515 
4516     jobject fontMetrics =
4517         env->CallObjectMethod(toolkit, AwtToolkit::getFontMetricsMID, font);
4518 
4519     DASSERT(!safe_ExceptionOccurred(env));
4520 
4521     jint height = env->CallIntMethod(fontMetrics, AwtFont::getHeightMID);
4522     DASSERT(!safe_ExceptionOccurred(env));
4523 
4524     env->DeleteLocalRef(target);
4525     env->DeleteLocalRef(font);
4526     env->DeleteLocalRef(toolkit);
4527     env->DeleteLocalRef(fontMetrics);
4528 
4529     return height;
4530 }
4531 
4532 // If you override WmPrint, make sure to save a copy of the DC on the GDI
4533 // stack to be restored in WmPrintClient. Windows mangles the DC in
4534 // ::DefWindowProc.
4535 MsgRouting AwtComponent::WmPrint(HDC hDC, LPARAM flags)
4536 {
4537     /*
4538      * DefWindowProc for WM_PRINT changes DC parameters, so we have
4539      * to restore it ourselves. Otherwise it will cause problems
4540      * when several components are printed to the same DC.
4541      */
4542     int nOriginalDC = ::SaveDC(hDC);
4543     DASSERT(nOriginalDC != 0);
4544 
4545     if (flags & PRF_NONCLIENT) {
4546 
4547         VERIFY(::SaveDC(hDC));
4548 
4549         DefWindowProc(WM_PRINT, (WPARAM)hDC,
4550                       (flags & (PRF_NONCLIENT
4551                                 | PRF_CHECKVISIBLE | PRF_ERASEBKGND)));
4552 
4553         VERIFY(::RestoreDC(hDC, -1));
4554 
4555         // Special case for components with a sunken border. Windows does not
4556         // print the border correctly on PCL printers, so we have to do it ourselves.
4557         if (GetStyleEx() & WS_EX_CLIENTEDGE) {
4558             RECT r;
4559             VERIFY(::GetWindowRect(GetHWnd(), &r));
4560             VERIFY(::OffsetRect(&r, -r.left, -r.top));
4561             VERIFY(::DrawEdge(hDC, &r, EDGE_SUNKEN, BF_RECT));
4562         }
4563     }
4564 
4565     if (flags & PRF_CLIENT) {
4566 
4567         /*
4568          * Special case for components with a sunken border.
4569          * Windows prints a client area without offset to a border width.
4570          * We will first print the non-client area with the original offset,
4571          * then the client area with a corrected offset.
4572          */
4573         if (GetStyleEx() & WS_EX_CLIENTEDGE) {
4574 
4575             int nEdgeWidth = ::GetSystemMetrics(SM_CXEDGE);
4576             int nEdgeHeight = ::GetSystemMetrics(SM_CYEDGE);
4577 
4578             VERIFY(::OffsetWindowOrgEx(hDC, -nEdgeWidth, -nEdgeHeight, NULL));
4579 
4580             // Save a copy of the DC for WmPrintClient
4581             VERIFY(::SaveDC(hDC));
4582 
4583             DefWindowProc(WM_PRINT, (WPARAM) hDC,
4584                           (flags & (PRF_CLIENT
4585                                     | PRF_CHECKVISIBLE | PRF_ERASEBKGND)));
4586 
4587             VERIFY(::OffsetWindowOrgEx(hDC, nEdgeWidth, nEdgeHeight, NULL));
4588 
4589         } else {
4590 
4591             // Save a copy of the DC for WmPrintClient
4592             VERIFY(::SaveDC(hDC));
4593             DefWindowProc(WM_PRINT, (WPARAM) hDC,
4594                           (flags & (PRF_CLIENT
4595                                     | PRF_CHECKVISIBLE | PRF_ERASEBKGND)));
4596         }
4597     }
4598 
4599     if (flags & (PRF_CHILDREN | PRF_OWNED)) {
4600         DefWindowProc(WM_PRINT, (WPARAM) hDC,
4601                       (flags & ~PRF_CLIENT & ~PRF_NONCLIENT));
4602     }
4603 
4604     VERIFY(::RestoreDC(hDC, nOriginalDC));
4605 
4606     return mrConsume;
4607 }
4608 
4609 // If you override WmPrintClient, make sure to obtain a valid copy of
4610 // the DC from the GDI stack. The copy of the DC should have been placed
4611 // there by WmPrint. Windows mangles the DC in ::DefWindowProc.
4612 MsgRouting AwtComponent::WmPrintClient(HDC hDC, LPARAM)
4613 {
4614     // obtain valid DC from GDI stack
4615     ::RestoreDC(hDC, -1);
4616 
4617     return mrDoDefault;
4618 }
4619 
4620 MsgRouting AwtComponent::WmNcCalcSize(BOOL fCalcValidRects,
4621                                       LPNCCALCSIZE_PARAMS lpncsp,
4622                                       LRESULT &retVal)
4623 {
4624     return mrDoDefault;
4625 }
4626 
4627 MsgRouting AwtComponent::WmNcPaint(HRGN hrgn)
4628 {
4629     return mrDoDefault;
4630 }
4631 
4632 MsgRouting AwtComponent::WmNcHitTest(UINT x, UINT y, LRESULT &retVal)
4633 {
4634     return mrDoDefault;
4635 }
4636 
4637 /**
4638  * WmQueryNewPalette is called whenever our component is coming to
4639  * the foreground; this gives us an opportunity to install our
4640  * custom palette.  If this install actually changes entries in
4641  * the system palette, then we get a further call to WmPaletteChanged
4642  * (but note that we only need to realize our palette once).
4643  */
4644 MsgRouting AwtComponent::WmQueryNewPalette(LRESULT &retVal)
4645 {
4646     int screen = AwtWin32GraphicsDevice::DeviceIndexForWindow(GetHWnd());
4647     m_QueryNewPaletteCalled = TRUE;
4648     HDC hDC = ::GetDC(GetHWnd());
4649     DASSERT(hDC);
4650     AwtWin32GraphicsDevice::SelectPalette(hDC, screen);
4651     AwtWin32GraphicsDevice::RealizePalette(hDC, screen);
4652     ::ReleaseDC(GetHWnd(), hDC);
4653     // We must realize the palettes of all of our DC's
4654     // There is sometimes a problem where the realization of
4655     // our temporary hDC here does not actually do what
4656     // we want.  Not clear why, but presumably fallout from
4657     // our use of several simultaneous hDC's.
4658     activeDCList.RealizePalettes(screen);
4659     // Do not invalidate here; if the palette
4660     // has not changed we will get an extra repaint
4661     retVal = TRUE;
4662 
4663     return mrDoDefault;
4664 }
4665 
4666 /**
4667  * We should not need to track this event since we handle our
4668  * palette management effectively in the WmQueryNewPalette and
4669  * WmPaletteChanged methods.  However, there seems to be a bug
4670  * on some win32 systems (e.g., NT4) whereby the palette
4671  * immediately after a displayChange is not yet updated to its
4672  * final post-display-change values (hence we adjust our palette
4673  * using the wrong system palette entries), then the palette is
4674  * updated, but a WM_PALETTECHANGED message is never sent.
4675  * By tracking the ISCHANGING message as well (and by tracking
4676  * displayChange events in the AwtToolkit object), we can account
4677  * for this error by forcing our WmPaletteChanged method to be
4678  * called and thereby realizing our logical palette and updating
4679  * our dynamic colorModel object.
4680  */
4681 MsgRouting AwtComponent::WmPaletteIsChanging(HWND hwndPalChg)
4682 {
4683     if (AwtToolkit::GetInstance().HasDisplayChanged()) {
4684         WmPaletteChanged(hwndPalChg);
4685         AwtToolkit::GetInstance().ResetDisplayChanged();
4686     }
4687     return mrDoDefault;
4688 }
4689 
4690 MsgRouting AwtComponent::WmPaletteChanged(HWND hwndPalChg)
4691 {
4692     // We need to re-realize our palette here (unless we're the one
4693     // that was realizing it in the first place).  That will let us match the
4694     // remaining colors in the system palette as best we can.  We always
4695     // invalidate because the palette will have changed when we receive this
4696     // message.
4697 
4698     int screen = AwtWin32GraphicsDevice::DeviceIndexForWindow(GetHWnd());
4699     if (hwndPalChg != GetHWnd()) {
4700         HDC hDC = ::GetDC(GetHWnd());
4701         DASSERT(hDC);
4702         AwtWin32GraphicsDevice::SelectPalette(hDC, screen);
4703         AwtWin32GraphicsDevice::RealizePalette(hDC, screen);
4704         ::ReleaseDC(GetHWnd(), hDC);
4705         // We must realize the palettes of all of our DC's
4706         activeDCList.RealizePalettes(screen);
4707     }
4708     if (AwtWin32GraphicsDevice::UpdateSystemPalette(screen)) {
4709         AwtWin32GraphicsDevice::UpdateDynamicColorModel(screen);
4710     }
4711     Invalidate(NULL);
4712     return mrDoDefault;
4713 }
4714 
4715 MsgRouting AwtComponent::WmStyleChanged(int wStyleType, LPSTYLESTRUCT lpss)
4716 {
4717     DASSERT(!IsBadReadPtr(lpss, sizeof(STYLESTRUCT)));
4718     return mrDoDefault;
4719 }
4720 
4721 MsgRouting AwtComponent::WmSettingChange(UINT wFlag, LPCTSTR pszSection)
4722 {
4723     DASSERT(!IsBadStringPtr(pszSection, 20));
4724     DTRACE_PRINTLN2("WM_SETTINGCHANGE: wFlag=%d pszSection=%s", (int)wFlag, pszSection);
4725     return mrDoDefault;
4726 }
4727 
4728 HDC AwtComponent::GetDCFromComponent()
4729 {
4730     GetDCReturnStruct *hdcStruct =
4731         (GetDCReturnStruct*)SendMessage(WM_AWT_GETDC);
4732     HDC hdc;
4733     if (hdcStruct) {
4734         if (hdcStruct->gdiLimitReached) {
4735             if (jvm != NULL) {
4736                 JNIEnv* env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
4737                 if (env != NULL && !safe_ExceptionOccurred(env)) {
4738                     JNU_ThrowByName(env, "java/awt/AWTError",
4739                         "HDC creation failure - " \
4740                         "exceeded maximum GDI resources");
4741                 }
4742             }
4743         }
4744         hdc = hdcStruct->hDC;
4745         delete hdcStruct;
4746     } else {
4747         hdc = NULL;
4748     }
4749     return hdc;
4750 }
4751 
4752 void AwtComponent::FillBackground(HDC hMemoryDC, SIZE &size)
4753 {
4754     RECT eraseR = { 0, 0, size.cx, size.cy };
4755     VERIFY(::FillRect(hMemoryDC, &eraseR, GetBackgroundBrush()));
4756 }
4757 
4758 void AwtComponent::FillAlpha(void *bitmapBits, SIZE &size, BYTE alpha)
4759 {
4760     if (!bitmapBits) {
4761         return;
4762     }
4763 
4764     DWORD* dest = (DWORD*)bitmapBits;
4765     //XXX: might be optimized to use one loop (cy*cx -> 0)
4766     for (int i = 0; i < size.cy; i++ ) {
4767         for (int j = 0; j < size.cx; j++ ) {
4768             ((BYTE*)(dest++))[3] = alpha;
4769         }
4770     }
4771 }
4772 
4773 int AwtComponent::ScaleUpX(int x) {
4774     int screen = AwtWin32GraphicsDevice::DeviceIndexForWindow(GetHWnd());
4775     Devices::InstanceAccess devices;
4776     AwtWin32GraphicsDevice* device = devices->GetDevice(screen);
4777     return device == NULL ? x : device->ScaleUpX(x);
4778 }
4779 
4780 int AwtComponent::ScaleUpY(int y) {
4781     int screen = AwtWin32GraphicsDevice::DeviceIndexForWindow(GetHWnd());
4782     Devices::InstanceAccess devices;
4783     AwtWin32GraphicsDevice* device = devices->GetDevice(screen);
4784     return device == NULL ? y : device->ScaleUpY(y);
4785 }
4786 
4787 int AwtComponent::ScaleDownX(int x) {
4788     int screen = AwtWin32GraphicsDevice::DeviceIndexForWindow(GetHWnd());
4789     Devices::InstanceAccess devices;
4790     AwtWin32GraphicsDevice* device = devices->GetDevice(screen);
4791     return device == NULL ? x : device->ScaleDownX(x);
4792 }
4793 
4794 int AwtComponent::ScaleDownY(int y) {
4795     int screen = AwtWin32GraphicsDevice::DeviceIndexForWindow(GetHWnd());
4796     Devices::InstanceAccess devices;
4797     AwtWin32GraphicsDevice* device = devices->GetDevice(screen);
4798     return device == NULL ? y : device->ScaleDownY(y);
4799 }
4800 
4801 jintArray AwtComponent::CreatePrintedPixels(SIZE &loc, SIZE &size, int alpha) {
4802     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
4803 
4804     if (!::IsWindowVisible(GetHWnd())) {
4805         return NULL;
4806     }
4807 
4808     HDC hdc = GetDCFromComponent();
4809     if (!hdc) {
4810         return NULL;
4811     }
4812     HDC hMemoryDC = ::CreateCompatibleDC(hdc);
4813     void *bitmapBits = NULL;
4814     HBITMAP hBitmap = BitmapUtil::CreateARGBBitmap(size.cx, size.cy, &bitmapBits);
4815     HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hMemoryDC, hBitmap);
4816     SendMessage(WM_AWT_RELEASEDC, (WPARAM)hdc);
4817 
4818     FillBackground(hMemoryDC, size);
4819 
4820     VERIFY(::SetWindowOrgEx(hMemoryDC, loc.cx, loc.cy, NULL));
4821 
4822     // Don't bother with PRF_CHECKVISIBLE because we called IsWindowVisible
4823     // above.
4824     SendMessage(WM_PRINT, (WPARAM)hMemoryDC, PRF_CLIENT | PRF_NONCLIENT);
4825 
4826     // First make sure the system completed any drawing to the bitmap.
4827     ::GdiFlush();
4828 
4829     // WM_PRINT does not fill the alpha-channel of the ARGB bitmap
4830     // leaving it equal to zero. Hence we need to fill it manually. Otherwise
4831     // the pixels will be considered transparent when interpreting the data.
4832     FillAlpha(bitmapBits, size, alpha);
4833 
4834     ::SelectObject(hMemoryDC, hOldBitmap);
4835 
4836     BITMAPINFO bmi;
4837     memset(&bmi, 0, sizeof(BITMAPINFO));
4838     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
4839     bmi.bmiHeader.biWidth = size.cx;
4840     bmi.bmiHeader.biHeight = -size.cy;
4841     bmi.bmiHeader.biPlanes = 1;
4842     bmi.bmiHeader.biBitCount = 32;
4843     bmi.bmiHeader.biCompression = BI_RGB;
4844 
4845     jobject localPixelArray = env->NewIntArray(size.cx * size.cy);
4846     jintArray pixelArray = NULL;
4847     if (localPixelArray != NULL) {
4848         pixelArray = (jintArray)env->NewGlobalRef(localPixelArray);
4849         env->DeleteLocalRef(localPixelArray); localPixelArray = NULL;
4850 
4851         jboolean isCopy;
4852         jint *pixels = env->GetIntArrayElements(pixelArray, &isCopy);
4853 
4854         ::GetDIBits(hMemoryDC, hBitmap, 0, size.cy, (LPVOID)pixels, &bmi,
4855                     DIB_RGB_COLORS);
4856 
4857         env->ReleaseIntArrayElements(pixelArray, pixels, 0);
4858     }
4859 
4860     VERIFY(::DeleteObject(hBitmap));
4861     VERIFY(::DeleteDC(hMemoryDC));
4862 
4863     return pixelArray;
4864 }
4865 
4866 void* AwtComponent::SetNativeFocusOwner(void *self) {
4867     if (self == NULL) {
4868         // It means that the KFM wants to set focus to null
4869         sm_focusOwner = NULL;
4870         return NULL;
4871     }
4872 
4873     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
4874 
4875     AwtComponent *c = NULL;
4876     jobject peer = (jobject)self;
4877 
4878     PDATA pData;
4879     JNI_CHECK_NULL_GOTO(peer, "peer", ret);
4880     pData = JNI_GET_PDATA(peer);
4881     if (pData == NULL) {
4882         goto ret;
4883     }
4884     c = (AwtComponent *)pData;
4885 
4886 ret:
4887     if (c && ::IsWindow(c->GetHWnd())) {
4888         sm_focusOwner = c->GetHWnd();
4889     } else {
4890         sm_focusOwner = NULL;
4891     }
4892     env->DeleteGlobalRef(peer);
4893     return NULL;
4894 }
4895 
4896 void* AwtComponent::GetNativeFocusedWindow() {
4897     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
4898     AwtComponent *comp =
4899         AwtComponent::GetComponent(AwtComponent::GetFocusedWindow());
4900     return (comp != NULL) ? comp->GetTargetAsGlobalRef(env) : NULL;
4901 }
4902 
4903 void* AwtComponent::GetNativeFocusOwner() {
4904     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
4905     AwtComponent *comp =
4906         AwtComponent::GetComponent(AwtComponent::sm_focusOwner);
4907     return (comp != NULL) ? comp->GetTargetAsGlobalRef(env) : NULL;
4908 }
4909 
4910 AwtComponent* AwtComponent::SearchChild(UINT id) {
4911     ChildListItem* child;
4912     for (child = m_childList; child != NULL;child = child->m_next) {
4913         if (child->m_ID == id)
4914             return child->m_Component;
4915     }
4916     /*
4917      * DASSERT(FALSE);
4918      * This should not be happend if all children are recorded
4919      */
4920     return NULL;        /* make compiler happy */
4921 }
4922 
4923 void AwtComponent::RemoveChild(UINT id) {
4924     ChildListItem* child = m_childList;
4925     ChildListItem* lastChild = NULL;
4926     while (child != NULL) {
4927         if (child->m_ID == id) {
4928             if (lastChild == NULL) {
4929                 m_childList = child->m_next;
4930             } else {
4931                 lastChild->m_next = child->m_next;
4932             }
4933             child->m_next = NULL;
4934             DASSERT(child != NULL);
4935             delete child;
4936             return;
4937         }
4938         lastChild = child;
4939         child = child->m_next;
4940     }
4941 }
4942 
4943 void AwtComponent::SendKeyEvent(jint id, jlong when, jint raw, jint cooked,
4944                                 jint modifiers, jint keyLocation, jlong nativeCode, MSG *pMsg)
4945 {
4946     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
4947     CriticalSection::Lock l(GetLock());
4948     if (GetPeer(env) == NULL) {
4949         /* event received during termination. */
4950         return;
4951     }
4952 
4953     static jclass keyEventCls;
4954     if (keyEventCls == NULL) {
4955         jclass keyEventClsLocal = env->FindClass("java/awt/event/KeyEvent");
4956         DASSERT(keyEventClsLocal);
4957         if (keyEventClsLocal == NULL) {
4958             /* exception already thrown */
4959             return;
4960         }
4961         keyEventCls = (jclass)env->NewGlobalRef(keyEventClsLocal);
4962         env->DeleteLocalRef(keyEventClsLocal);
4963     }
4964 
4965     static jmethodID keyEventConst;
4966     if (keyEventConst == NULL) {
4967         keyEventConst =  env->GetMethodID(keyEventCls, "<init>",
4968                                           "(Ljava/awt/Component;IJIICI)V");
4969         DASSERT(keyEventConst);
4970         CHECK_NULL(keyEventConst);
4971     }
4972     if (env->EnsureLocalCapacity(2) < 0) {
4973         return;
4974     }
4975     jobject target = GetTarget(env);
4976     jobject keyEvent = env->NewObject(keyEventCls, keyEventConst, target,
4977                                       id, when, modifiers, raw, cooked,
4978                                       keyLocation);
4979     if (safe_ExceptionOccurred(env)) env->ExceptionDescribe();
4980     DASSERT(!safe_ExceptionOccurred(env));
4981     DASSERT(keyEvent != NULL);
4982     if (keyEvent == NULL) {
4983         env->DeleteLocalRef(target);
4984         return;
4985     }
4986     env->SetLongField(keyEvent, AwtKeyEvent::rawCodeID, nativeCode);
4987     if( nativeCode && nativeCode < 256 ) {
4988         env->SetLongField(keyEvent, AwtKeyEvent::primaryLevelUnicodeID, (jlong)(dynPrimaryKeymap[nativeCode].unicode));
4989         env->SetLongField(keyEvent, AwtKeyEvent::extendedKeyCodeID, (jlong)(dynPrimaryKeymap[nativeCode].jkey));
4990         if( nativeCode < 255 ) {
4991             env->SetLongField(keyEvent, AwtKeyEvent::scancodeID, (jlong)(dynPrimaryKeymap[nativeCode].scancode));
4992         }else if( pMsg != NULL ) {
4993             // unknown key with virtual keycode 0xFF.
4994             // Its scancode is not in the table, pickup it from the message.
4995             env->SetLongField(keyEvent, AwtKeyEvent::scancodeID, (jlong)(HIWORD(pMsg->lParam) & 0xFF));
4996         }
4997     }
4998     if (pMsg != NULL) {
4999         AwtAWTEvent::saveMSG(env, pMsg, keyEvent);
5000     }
5001     SendEvent(keyEvent);
5002 
5003     env->DeleteLocalRef(keyEvent);
5004     env->DeleteLocalRef(target);
5005 }
5006 
5007 void
5008 AwtComponent::SendKeyEventToFocusOwner(jint id, jlong when,
5009                                        jint raw, jint cooked,
5010                                        jint modifiers, jint keyLocation,
5011                                        jlong nativeCode,
5012                                        MSG *msg)
5013 {
5014     /*
5015      * if focus owner is null, but focused window isn't
5016      * we will send key event to focused window
5017      */
5018     HWND hwndTarget = ((sm_focusOwner != NULL) ? sm_focusOwner : AwtComponent::GetFocusedWindow());
5019 
5020     if (hwndTarget == GetHWnd()) {
5021         SendKeyEvent(id, when, raw, cooked, modifiers, keyLocation, nativeCode, msg);
5022     } else {
5023         AwtComponent *target = NULL;
5024         if (hwndTarget != NULL) {
5025             target = AwtComponent::GetComponent(hwndTarget);
5026             if (target == NULL) {
5027                 target = this;
5028             }
5029         }
5030         if (target != NULL) {
5031             target->SendKeyEvent(id, when, raw, cooked, modifiers,
5032               keyLocation, nativeCode, msg);
5033         }
5034     }
5035 }
5036 
5037 void AwtComponent::SetDragCapture(UINT flags)
5038 {
5039     // don't want to interfere with other controls
5040     if (::GetCapture() == NULL) {
5041         ::SetCapture(GetHWnd());
5042     }
5043 }
5044 
5045 void AwtComponent::ReleaseDragCapture(UINT flags)
5046 {
5047     if ((::GetCapture() == GetHWnd()) && ((flags & ALL_MK_BUTTONS) == 0)) {
5048         // user has released all buttons, so release the capture
5049         ::ReleaseCapture();
5050     }
5051 }
5052 
5053 void AwtComponent::SendMouseEvent(jint id, jlong when, jint x, jint y,
5054                                   jint modifiers, jint clickCount,
5055                                   jboolean popupTrigger, jint button,
5056                                   MSG *pMsg, BOOL causedByTouchEvent)
5057 {
5058     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5059     CriticalSection::Lock l(GetLock());
5060     if (GetPeer(env) == NULL) {
5061         /* event received during termination. */
5062         return;
5063     }
5064 
5065     static jclass mouseEventCls;
5066     if (mouseEventCls == NULL) {
5067         jclass mouseEventClsLocal =
5068             env->FindClass("java/awt/event/MouseEvent");
5069         CHECK_NULL(mouseEventClsLocal);
5070         mouseEventCls = (jclass)env->NewGlobalRef(mouseEventClsLocal);
5071         env->DeleteLocalRef(mouseEventClsLocal);
5072     }
5073     RECT insets;
5074     GetInsets(&insets);
5075 
5076     static jmethodID mouseEventConst;
5077     if (mouseEventConst == NULL) {
5078         mouseEventConst =
5079             env->GetMethodID(mouseEventCls, "<init>",
5080                  "(Ljava/awt/Component;IJIIIIIIZI)V");
5081         DASSERT(mouseEventConst);
5082         CHECK_NULL(mouseEventConst);
5083     }
5084     if (env->EnsureLocalCapacity(2) < 0) {
5085         return;
5086     }
5087     jobject target = GetTarget(env);
5088     DWORD curMousePos = ::GetMessagePos();
5089     int xAbs = GET_X_LPARAM(curMousePos);
5090     int yAbs = GET_Y_LPARAM(curMousePos);
5091     jobject mouseEvent = env->NewObject(mouseEventCls, mouseEventConst,
5092                                         target,
5093                                         id, when, modifiers,
5094                                         ScaleDownX(x + insets.left),
5095                                         ScaleDownY(y + insets.top),
5096                                         ScaleDownX(xAbs), ScaleDownY(yAbs),
5097                                         clickCount, popupTrigger, button);
5098 
5099     if (safe_ExceptionOccurred(env)) {
5100         env->ExceptionDescribe();
5101         env->ExceptionClear();
5102     }
5103 
5104     DASSERT(mouseEvent != NULL);
5105     CHECK_NULL(mouseEvent);
5106     if (causedByTouchEvent) {
5107         env->SetBooleanField(mouseEvent, AwtMouseEvent::causedByTouchEventID,
5108             JNI_TRUE);
5109     }
5110     if (pMsg != 0) {
5111         AwtAWTEvent::saveMSG(env, pMsg, mouseEvent);
5112     }
5113     SendEvent(mouseEvent);
5114 
5115     env->DeleteLocalRef(mouseEvent);
5116     env->DeleteLocalRef(target);
5117 }
5118 
5119 void
5120 AwtComponent::SendMouseWheelEvent(jint id, jlong when, jint x, jint y,
5121                                   jint modifiers, jint clickCount,
5122                                   jboolean popupTrigger, jint scrollType,
5123                                   jint scrollAmount, jint roundedWheelRotation,
5124                                   jdouble preciseWheelRotation, MSG *pMsg)
5125 {
5126     /* Code based not so loosely on AwtComponent::SendMouseEvent */
5127     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5128     CriticalSection::Lock l(GetLock());
5129     if (GetPeer(env) == NULL) {
5130         /* event received during termination. */
5131         return;
5132     }
5133 
5134     static jclass mouseWheelEventCls;
5135     if (mouseWheelEventCls == NULL) {
5136         jclass mouseWheelEventClsLocal =
5137             env->FindClass("java/awt/event/MouseWheelEvent");
5138         CHECK_NULL(mouseWheelEventClsLocal);
5139         mouseWheelEventCls = (jclass)env->NewGlobalRef(mouseWheelEventClsLocal);
5140         env->DeleteLocalRef(mouseWheelEventClsLocal);
5141     }
5142     RECT insets;
5143     GetInsets(&insets);
5144 
5145     static jmethodID mouseWheelEventConst;
5146     if (mouseWheelEventConst == NULL) {
5147         mouseWheelEventConst =
5148             env->GetMethodID(mouseWheelEventCls, "<init>",
5149                            "(Ljava/awt/Component;IJIIIIIIZIIID)V");
5150         DASSERT(mouseWheelEventConst);
5151         CHECK_NULL(mouseWheelEventConst);
5152     }
5153     if (env->EnsureLocalCapacity(2) < 0) {
5154         return;
5155     }
5156     jobject target = GetTarget(env);
5157     DWORD curMousePos = ::GetMessagePos();
5158     int xAbs = GET_X_LPARAM(curMousePos);
5159     int yAbs = GET_Y_LPARAM(curMousePos);
5160 
5161     DTRACE_PRINTLN("creating MWE in JNI");
5162 
5163     jobject mouseWheelEvent = env->NewObject(mouseWheelEventCls,
5164                                              mouseWheelEventConst,
5165                                              target,
5166                                              id, when, modifiers,
5167                                              ScaleDownX(x + insets.left),
5168                                              ScaleDownY(y + insets.top),
5169                                              ScaleDownX(xAbs),
5170                                              ScaleDownY(yAbs),
5171                                              clickCount, popupTrigger,
5172                                              scrollType, scrollAmount,
5173                                              roundedWheelRotation, preciseWheelRotation);
5174 
5175     DASSERT(mouseWheelEvent != NULL);
5176     if (mouseWheelEvent == NULL || safe_ExceptionOccurred(env)) {
5177         env->ExceptionDescribe();
5178         env->ExceptionClear();
5179         env->DeleteLocalRef(target);
5180         return;
5181     }
5182     if (pMsg != NULL) {
5183         AwtAWTEvent::saveMSG(env, pMsg, mouseWheelEvent);
5184     }
5185     SendEvent(mouseWheelEvent);
5186 
5187     env->DeleteLocalRef(mouseWheelEvent);
5188     env->DeleteLocalRef(target);
5189 }
5190 
5191 void AwtComponent::SendFocusEvent(jint id, HWND opposite)
5192 {
5193     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5194 
5195     CriticalSection::Lock l(GetLock());
5196     if (GetPeer(env) == NULL) {
5197         /* event received during termination. */
5198         return;
5199     }
5200 
5201     static jclass focusEventCls;
5202     if (focusEventCls == NULL) {
5203         jclass focusEventClsLocal
5204             = env->FindClass("java/awt/event/FocusEvent");
5205         DASSERT(focusEventClsLocal);
5206         CHECK_NULL(focusEventClsLocal);
5207         focusEventCls = (jclass)env->NewGlobalRef(focusEventClsLocal);
5208         env->DeleteLocalRef(focusEventClsLocal);
5209     }
5210 
5211     static jmethodID focusEventConst;
5212     if (focusEventConst == NULL) {
5213         focusEventConst =
5214             env->GetMethodID(focusEventCls, "<init>",
5215                              "(Ljava/awt/Component;IZLjava/awt/Component;)V");
5216         DASSERT(focusEventConst);
5217         CHECK_NULL(focusEventConst);
5218     }
5219 
5220     static jclass sequencedEventCls;
5221     if (sequencedEventCls == NULL) {
5222         jclass sequencedEventClsLocal =
5223             env->FindClass("java/awt/SequencedEvent");
5224         DASSERT(sequencedEventClsLocal);
5225         CHECK_NULL(sequencedEventClsLocal);
5226         sequencedEventCls =
5227             (jclass)env->NewGlobalRef(sequencedEventClsLocal);
5228         env->DeleteLocalRef(sequencedEventClsLocal);
5229     }
5230 
5231     static jmethodID sequencedEventConst;
5232     if (sequencedEventConst == NULL) {
5233         sequencedEventConst =
5234             env->GetMethodID(sequencedEventCls, "<init>",
5235                              "(Ljava/awt/AWTEvent;)V");
5236         DASSERT(sequencedEventConst);
5237         CHECK_NULL(sequencedEventConst);
5238     }
5239 
5240     if (env->EnsureLocalCapacity(3) < 0) {
5241         return;
5242     }
5243 
5244     jobject target = GetTarget(env);
5245     jobject jOpposite = NULL;
5246     if (opposite != NULL) {
5247         AwtComponent *awtOpposite = AwtComponent::GetComponent(opposite);
5248         if (awtOpposite != NULL) {
5249             jOpposite = awtOpposite->GetTarget(env);
5250         }
5251     }
5252     jobject focusEvent = env->NewObject(focusEventCls, focusEventConst,
5253                                         target, id, JNI_FALSE, jOpposite);
5254     DASSERT(!safe_ExceptionOccurred(env));
5255     DASSERT(focusEvent != NULL);
5256     if (jOpposite != NULL) {
5257         env->DeleteLocalRef(jOpposite); jOpposite = NULL;
5258     }
5259     env->DeleteLocalRef(target); target = NULL;
5260     CHECK_NULL(focusEvent);
5261 
5262     jobject sequencedEvent = env->NewObject(sequencedEventCls,
5263                                             sequencedEventConst,
5264                                             focusEvent);
5265     DASSERT(!safe_ExceptionOccurred(env));
5266     DASSERT(sequencedEvent != NULL);
5267     env->DeleteLocalRef(focusEvent); focusEvent = NULL;
5268     CHECK_NULL(sequencedEvent);
5269     SendEvent(sequencedEvent);
5270 
5271     env->DeleteLocalRef(sequencedEvent);
5272 }
5273 
5274 /*
5275  * Forward a filtered event directly to the subclassed window.
5276  * This method is needed so that DefWindowProc is invoked on the
5277  * component's owning thread.
5278  */
5279 MsgRouting AwtComponent::HandleEvent(MSG *msg, BOOL)
5280 {
5281     DefWindowProc(msg->message, msg->wParam, msg->lParam);
5282     delete msg;
5283     return mrConsume;
5284 }
5285 
5286 /* Post a WM_AWT_HANDLE_EVENT message which invokes HandleEvent
5287    on the toolkit thread. This method may pre-filter the messages. */
5288 BOOL AwtComponent::PostHandleEventMessage(MSG *msg, BOOL synthetic)
5289 {
5290     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5291     // We should cut off keyboard events to disabled components
5292     // to avoid the components responding visually to keystrokes when disabled.
5293     // we shouldn't cut off WM_SYS* messages as they aren't used for normal activity
5294     // but to activate menus, close windows, etc
5295     switch(msg->message) {
5296         case WM_KEYDOWN:
5297         case WM_KEYUP:
5298         case WM_CHAR:
5299         case WM_DEADCHAR:
5300             {
5301                 if (!isRecursivelyEnabled()) {
5302                     goto quit;
5303                 }
5304                 break;
5305             }
5306     }
5307     if (PostMessage(GetHWnd(), WM_AWT_HANDLE_EVENT,
5308         (WPARAM) synthetic, (LPARAM) msg)) {
5309             return TRUE;
5310     } else {
5311         JNU_ThrowInternalError(env, "Message not posted, native event queue may be full.");
5312     }
5313 quit:
5314     delete msg;
5315     return FALSE;
5316 }
5317 
5318 void AwtComponent::SynthesizeKeyMessage(JNIEnv *env, jobject keyEvent)
5319 {
5320     jint id = (env)->GetIntField(keyEvent, AwtAWTEvent::idID);
5321     UINT message;
5322     switch (id) {
5323       case java_awt_event_KeyEvent_KEY_PRESSED:
5324           message = WM_KEYDOWN;
5325           break;
5326       case java_awt_event_KeyEvent_KEY_RELEASED:
5327           message = WM_KEYUP;
5328           break;
5329       case java_awt_event_KeyEvent_KEY_TYPED:
5330           message = WM_CHAR;
5331           break;
5332       default:
5333           return;
5334     }
5335 
5336     /*
5337      * KeyEvent.modifiers aren't supported -- the Java apppwd must send separate
5338      * KEY_PRESSED and KEY_RELEASED events for the modifier virtual keys.
5339      */
5340     if (id == java_awt_event_KeyEvent_KEY_TYPED) {
5341         // WM_CHAR message must be posted using WM_AWT_FORWARD_CHAR
5342         // (for Edit control)
5343         jchar keyChar = (jchar)
5344           (env)->GetCharField(keyEvent, AwtKeyEvent::keyCharID);
5345 
5346         // Bugid 4724007.  If it is a Delete character, don't send the fake
5347         // KEY_TYPED we created back to the native window: Windows doesn't
5348         // expect a WM_CHAR for Delete in TextFields, so it tries to enter a
5349         // character after deleting.
5350         if (keyChar == '\177') { // the Delete character
5351             return;
5352         }
5353 
5354         // Disable forwarding WM_CHAR messages to disabled components
5355         if (isRecursivelyEnabled()) {
5356             if (!::PostMessage(GetHWnd(), WM_AWT_FORWARD_CHAR,
5357                 MAKEWPARAM(keyChar, TRUE), 0)) {
5358                 JNU_ThrowInternalError(env, "Message not posted, native event queue may be full.");
5359             }
5360         }
5361     } else {
5362         jint keyCode =
5363           (env)->GetIntField(keyEvent, AwtKeyEvent::keyCodeID);
5364         UINT key, modifiers;
5365         AwtComponent::JavaKeyToWindowsKey(keyCode, &key, &modifiers);
5366         MSG* msg = CreateMessage(message, key, 0);
5367         PostHandleEventMessage(msg, TRUE);
5368     }
5369 }
5370 
5371 void AwtComponent::SynthesizeMouseMessage(JNIEnv *env, jobject mouseEvent)
5372 {
5373     /*    DebugBreak(); */
5374     jint button = (env)->GetIntField(mouseEvent, AwtMouseEvent::buttonID);
5375     jint modifiers = (env)->GetIntField(mouseEvent, AwtInputEvent::modifiersID);
5376 
5377     WPARAM wParam = 0;
5378     WORD wLow = 0;
5379     jint wheelAmt = 0;
5380     jint id = (env)->GetIntField(mouseEvent, AwtAWTEvent::idID);
5381     UINT message;
5382     switch (id) {
5383       case java_awt_event_MouseEvent_MOUSE_PRESSED: {
5384           switch (button) {
5385             case java_awt_event_MouseEvent_BUTTON1:
5386                 message = WM_LBUTTONDOWN; break;
5387             case java_awt_event_MouseEvent_BUTTON3:
5388                 message = WM_MBUTTONDOWN; break;
5389             case java_awt_event_MouseEvent_BUTTON2:
5390                 message = WM_RBUTTONDOWN; break;
5391             default:
5392                 return;
5393           }
5394           break;
5395       }
5396       case java_awt_event_MouseEvent_MOUSE_RELEASED: {
5397           switch (button) {
5398             case java_awt_event_MouseEvent_BUTTON1:
5399                 message = WM_LBUTTONUP; break;
5400             case java_awt_event_MouseEvent_BUTTON3:
5401                 message = WM_MBUTTONUP; break;
5402             case java_awt_event_MouseEvent_BUTTON2:
5403                 message = WM_RBUTTONUP; break;
5404             default:
5405                 return;
5406           }
5407           break;
5408       }
5409       case java_awt_event_MouseEvent_MOUSE_MOVED:
5410           /* MOUSE_DRAGGED events must first have sent a MOUSE_PRESSED event. */
5411       case java_awt_event_MouseEvent_MOUSE_DRAGGED:
5412           message = WM_MOUSEMOVE;
5413           break;
5414       case java_awt_event_MouseEvent_MOUSE_WHEEL:
5415           if (modifiers & java_awt_event_InputEvent_CTRL_DOWN_MASK) {
5416               wLow |= MK_CONTROL;
5417           }
5418           if (modifiers & java_awt_event_InputEvent_SHIFT_DOWN_MASK) {
5419               wLow |= MK_SHIFT;
5420           }
5421           if (modifiers & java_awt_event_InputEvent_BUTTON1_DOWN_MASK) {
5422               wLow |= MK_LBUTTON;
5423           }
5424           if (modifiers & java_awt_event_InputEvent_BUTTON2_DOWN_MASK) {
5425               wLow |= MK_RBUTTON;
5426           }
5427           if (modifiers & java_awt_event_InputEvent_BUTTON3_DOWN_MASK) {
5428               wLow |= MK_MBUTTON;
5429           }
5430           if (modifiers & X1_BUTTON) {
5431               wLow |= GetButtonMK(X1_BUTTON);
5432           }
5433           if (modifiers & X2_BUTTON) {
5434               wLow |= GetButtonMK(X2_BUTTON);
5435           }
5436 
5437           wheelAmt = (jint)JNU_CallMethodByName(env,
5438                                                NULL,
5439                                                mouseEvent,
5440                                                "getWheelRotation",
5441                                                "()I").i;
5442           DASSERT(!safe_ExceptionOccurred(env));
5443           JNU_CHECK_EXCEPTION(env);
5444           DTRACE_PRINTLN1("wheelAmt = %i\n", wheelAmt);
5445 
5446           // convert Java wheel amount value to Win32
5447           wheelAmt *= -1 * WHEEL_DELTA;
5448 
5449           message = WM_MOUSEWHEEL;
5450           wParam = MAKEWPARAM(wLow, wheelAmt);
5451 
5452           break;
5453       default:
5454           return;
5455     }
5456     jint x = (env)->GetIntField(mouseEvent, AwtMouseEvent::xID);
5457     jint y = (env)->GetIntField(mouseEvent, AwtMouseEvent::yID);
5458     MSG* msg = CreateMessage(message, wParam, MAKELPARAM(x, y), x, y);
5459     PostHandleEventMessage(msg, TRUE);
5460 }
5461 
5462 BOOL AwtComponent::InheritsNativeMouseWheelBehavior() {return false;}
5463 
5464 void AwtComponent::Invalidate(RECT* r)
5465 {
5466     ::InvalidateRect(GetHWnd(), r, FALSE);
5467 }
5468 
5469 void AwtComponent::BeginValidate()
5470 {
5471     DASSERT(m_validationNestCount >= 0 &&
5472            m_validationNestCount < 1000); // sanity check
5473 
5474     if (m_validationNestCount == 0) {
5475     // begin deferred window positioning if we're not inside
5476     // another Begin/EndValidate pair
5477         DASSERT(m_hdwp == NULL);
5478         m_hdwp = ::BeginDeferWindowPos(32);
5479     }
5480 
5481     m_validationNestCount++;
5482 }
5483 
5484 void AwtComponent::EndValidate()
5485 {
5486     DASSERT(m_validationNestCount > 0 &&
5487            m_validationNestCount < 1000); // sanity check
5488     DASSERT(m_hdwp != NULL);
5489 
5490     m_validationNestCount--;
5491     if (m_validationNestCount == 0) {
5492     // if this call to EndValidate is not nested inside another
5493     // Begin/EndValidate pair, end deferred window positioning
5494         ::EndDeferWindowPos(m_hdwp);
5495         m_hdwp = NULL;
5496     }
5497 }
5498 
5499 /**
5500  * HWND, AwtComponent and Java Peer interaction
5501  */
5502 
5503 /*
5504  *Link the C++, Java peer, and HWNDs together.
5505  */
5506 void AwtComponent::LinkObjects(JNIEnv *env, jobject peer)
5507 {
5508     /*
5509      * Bind all three objects together thru this C++ object, two-way to each:
5510      *     JavaPeer <-> C++ <-> HWND
5511      *
5512      * C++ -> JavaPeer
5513      */
5514     if (m_peerObject == NULL) {
5515         // This may have already been set up by CreateHWnd
5516         // And we don't want to create two references so we
5517         // will leave the prior one alone
5518         m_peerObject = env->NewGlobalRef(peer);
5519     }
5520     /* JavaPeer -> HWND */
5521     env->SetLongField(peer, AwtComponent::hwndID, reinterpret_cast<jlong>(m_hwnd));
5522 
5523     /* JavaPeer -> C++ */
5524     JNI_SET_PDATA(peer, this);
5525 
5526     /* HWND -> C++ */
5527     SetComponentInHWND();
5528 }
5529 
5530 /* Cleanup above linking */
5531 void AwtComponent::UnlinkObjects()
5532 {
5533     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5534     if (m_peerObject) {
5535         env->SetLongField(m_peerObject, AwtComponent::hwndID, 0);
5536         JNI_SET_PDATA(m_peerObject, static_cast<PDATA>(NULL));
5537         JNI_SET_DESTROYED(m_peerObject);
5538         env->DeleteGlobalRef(m_peerObject);
5539         m_peerObject = NULL;
5540     }
5541 }
5542 
5543 void AwtComponent::Enable(BOOL bEnable)
5544 {
5545     if (bEnable && IsTopLevel()) {
5546         // we should not enable blocked toplevels
5547         bEnable = !::IsWindow(AwtWindow::GetModalBlocker(GetHWnd()));
5548     }
5549     // Shouldn't trigger native focus change
5550     // (only the proxy may be the native focus owner).
5551     ::EnableWindow(GetHWnd(), bEnable);
5552 
5553     CriticalSection::Lock l(GetLock());
5554     VerifyState();
5555 }
5556 
5557 /*
5558  * associate an AwtDropTarget with this AwtComponent
5559  */
5560 
5561 AwtDropTarget* AwtComponent::CreateDropTarget(JNIEnv* env) {
5562     m_dropTarget = new AwtDropTarget(env, this);
5563     m_dropTarget->RegisterTarget(TRUE);
5564     return m_dropTarget;
5565 }
5566 
5567 /*
5568  * disassociate an AwtDropTarget with this AwtComponent
5569  */
5570 
5571 void AwtComponent::DestroyDropTarget() {
5572     if (m_dropTarget != NULL) {
5573         m_dropTarget->RegisterTarget(FALSE);
5574         m_dropTarget->Release();
5575         m_dropTarget = NULL;
5576     }
5577 }
5578 
5579 BOOL AwtComponent::IsFocusingMouseMessage(MSG *pMsg) {
5580     return pMsg->message == WM_LBUTTONDOWN || pMsg->message == WM_LBUTTONDBLCLK;
5581 }
5582 
5583 BOOL AwtComponent::IsFocusingKeyMessage(MSG *pMsg) {
5584     return pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_SPACE;
5585 }
5586 
5587 void AwtComponent::_Show(void *param)
5588 {
5589     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5590 
5591     jobject self = (jobject)param;
5592 
5593     AwtComponent *p;
5594 
5595     PDATA pData;
5596     JNI_CHECK_PEER_GOTO(self, ret);
5597     p = (AwtComponent *)pData;
5598     if (::IsWindow(p->GetHWnd()))
5599     {
5600         p->SendMessage(WM_AWT_COMPONENT_SHOW);
5601     }
5602 ret:
5603     env->DeleteGlobalRef(self);
5604 }
5605 
5606 void AwtComponent::_Hide(void *param)
5607 {
5608     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5609 
5610     jobject self = (jobject)param;
5611 
5612     AwtComponent *p;
5613 
5614     PDATA pData;
5615     JNI_CHECK_PEER_GOTO(self, ret);
5616     p = (AwtComponent *)pData;
5617     if (::IsWindow(p->GetHWnd()))
5618     {
5619         p->SendMessage(WM_AWT_COMPONENT_HIDE);
5620     }
5621 ret:
5622     env->DeleteGlobalRef(self);
5623 }
5624 
5625 void AwtComponent::_Enable(void *param)
5626 {
5627     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5628 
5629     jobject self = (jobject)param;
5630 
5631     AwtComponent *p;
5632 
5633     PDATA pData;
5634     JNI_CHECK_PEER_GOTO(self, ret);
5635     p = (AwtComponent *)pData;
5636     if (::IsWindow(p->GetHWnd()))
5637     {
5638         p->Enable(TRUE);
5639     }
5640 ret:
5641     env->DeleteGlobalRef(self);
5642 }
5643 
5644 void AwtComponent::_Disable(void *param)
5645 {
5646     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5647 
5648     jobject self = (jobject)param;
5649 
5650     AwtComponent *p;
5651 
5652     PDATA pData;
5653     JNI_CHECK_PEER_GOTO(self, ret);
5654     p = (AwtComponent *)pData;
5655     if (::IsWindow(p->GetHWnd()))
5656     {
5657         p->Enable(FALSE);
5658     }
5659 ret:
5660     env->DeleteGlobalRef(self);
5661 }
5662 
5663 jobject AwtComponent::_GetLocationOnScreen(void *param)
5664 {
5665     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5666 
5667     jobject self = (jobject)param;
5668 
5669     jobject result = NULL;
5670     AwtComponent *p;
5671 
5672     PDATA pData;
5673     JNI_CHECK_PEER_GOTO(self, ret);
5674     p = (AwtComponent *)pData;
5675     if (::IsWindow(p->GetHWnd()))
5676     {
5677         RECT rect;
5678         VERIFY(::GetWindowRect(p->GetHWnd(),&rect));
5679         result = JNU_NewObjectByName(env, "java/awt/Point", "(II)V",
5680                                      p->ScaleDownX(rect.left),
5681                                      p->ScaleDownY(rect.top));
5682     }
5683 ret:
5684     env->DeleteGlobalRef(self);
5685 
5686     if (result != NULL)
5687     {
5688         jobject resultGlobalRef = env->NewGlobalRef(result);
5689         env->DeleteLocalRef(result);
5690         return resultGlobalRef;
5691     }
5692     else
5693     {
5694         return NULL;
5695     }
5696 }
5697 
5698 void AwtComponent::_Reshape(void *param)
5699 {
5700     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5701 
5702     ReshapeStruct *rs = (ReshapeStruct*)param;
5703     jobject self = rs->component;
5704     jint x = rs->x;
5705     jint y = rs->y;
5706     jint w = rs->w;
5707     jint h = rs->h;
5708 
5709     AwtComponent *p;
5710 
5711     PDATA pData;
5712     JNI_CHECK_PEER_GOTO(self, ret);
5713     p = (AwtComponent *)pData;
5714     if (::IsWindow(p->GetHWnd()))
5715     {
5716         RECT* r = new RECT;
5717         ::SetRect(r, x, y, x + w, y + h);
5718         p->SendMessage(WM_AWT_RESHAPE_COMPONENT, CHECK_EMBEDDED, (LPARAM)r);
5719     }
5720 ret:
5721     env->DeleteGlobalRef(self);
5722 
5723     delete rs;
5724 }
5725 
5726 void AwtComponent::_ReshapeNoCheck(void *param)
5727 {
5728     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5729 
5730     ReshapeStruct *rs = (ReshapeStruct*)param;
5731     jobject self = rs->component;
5732     jint x = rs->x;
5733     jint y = rs->y;
5734     jint w = rs->w;
5735     jint h = rs->h;
5736 
5737     AwtComponent *p;
5738 
5739     PDATA pData;
5740     JNI_CHECK_PEER_GOTO(self, ret);
5741     p = (AwtComponent *)pData;
5742     if (::IsWindow(p->GetHWnd()))
5743     {
5744         RECT* r = new RECT;
5745         ::SetRect(r, x, y, x + w, y + h);
5746         p->SendMessage(WM_AWT_RESHAPE_COMPONENT, DONT_CHECK_EMBEDDED, (LPARAM)r);
5747     }
5748 ret:
5749     env->DeleteGlobalRef(self);
5750 
5751     delete rs;
5752 }
5753 
5754 void AwtComponent::_NativeHandleEvent(void *param)
5755 {
5756     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5757 
5758     NativeHandleEventStruct *nhes = (NativeHandleEventStruct *)param;
5759     jobject self = nhes->component;
5760     jobject event = nhes->event;
5761 
5762     AwtComponent *p;
5763 
5764     PDATA pData;
5765     JNI_CHECK_NULL_GOTO(self, "peer", ret);
5766     pData = JNI_GET_PDATA(self);
5767     if (pData == NULL) {
5768         env->DeleteGlobalRef(self);
5769         if (event != NULL) {
5770             env->DeleteGlobalRef(event);
5771         }
5772         delete nhes;
5773         return;
5774     }
5775     JNI_CHECK_NULL_GOTO(event, "null AWTEvent", ret);
5776 
5777     p = (AwtComponent *)pData;
5778     if (::IsWindow(p->GetHWnd()))
5779     {
5780         if (env->EnsureLocalCapacity(1) < 0) {
5781             env->DeleteGlobalRef(self);
5782             env->DeleteGlobalRef(event);
5783             delete nhes;
5784             return;
5785         }
5786         jbyteArray bdata = (jbyteArray)(env)->GetObjectField(event, AwtAWTEvent::bdataID);
5787         int id = (env)->GetIntField(event, AwtAWTEvent::idID);
5788         DASSERT(!safe_ExceptionOccurred(env));
5789         if (bdata != 0) {
5790             MSG msg;
5791             (env)->GetByteArrayRegion(bdata, 0, sizeof(MSG), (jbyte *)&msg);
5792             (env)->DeleteLocalRef(bdata);
5793             static BOOL keyDownConsumed = FALSE;
5794             static BOOL bCharChanged = FALSE;
5795             static WCHAR modifiedChar;
5796             WCHAR unicodeChar;
5797 
5798             /* Remember if a KEY_PRESSED event is consumed, as an old model
5799              * program won't consume a subsequent KEY_TYPED event.
5800              */
5801             jboolean consumed =
5802                 (env)->GetBooleanField(event, AwtAWTEvent::consumedID);
5803             DASSERT(!safe_ExceptionOccurred(env));
5804 
5805             if (consumed) {
5806                 keyDownConsumed = (id == java_awt_event_KeyEvent_KEY_PRESSED);
5807                 env->DeleteGlobalRef(self);
5808                 env->DeleteGlobalRef(event);
5809                 delete nhes;
5810                 return;
5811 
5812             } else if (id == java_awt_event_KeyEvent_KEY_PRESSED) {
5813                 // Fix for 6637607: reset consuming
5814                 keyDownConsumed = FALSE;
5815             }
5816 
5817             /* Consume a KEY_TYPED event if a KEY_PRESSED had been, to support
5818              * the old model.
5819              */
5820             if ((id == java_awt_event_KeyEvent_KEY_TYPED) && keyDownConsumed) {
5821                 keyDownConsumed = FALSE;
5822                 env->DeleteGlobalRef(self);
5823                 env->DeleteGlobalRef(event);
5824                 delete nhes;
5825                 return;
5826             }
5827 
5828             /* Modify any event parameters, if necessary. */
5829             if (self && pData &&
5830                 id >= java_awt_event_KeyEvent_KEY_FIRST &&
5831                 id <= java_awt_event_KeyEvent_KEY_LAST) {
5832 
5833                     AwtComponent* p = (AwtComponent*)pData;
5834 
5835                     jint keyCode =
5836                       (env)->GetIntField(event, AwtKeyEvent::keyCodeID);
5837                     jchar keyChar =
5838                       (env)->GetCharField(event, AwtKeyEvent::keyCharID);
5839                     jint modifiers =
5840                       (env)->GetIntField(event, AwtInputEvent::modifiersID);
5841 
5842                     DASSERT(!safe_ExceptionOccurred(env));
5843 
5844                 /* Check to see whether the keyCode or modifiers were changed
5845                    on the keyPressed event, and tweak the following keyTyped
5846                    event (if any) accodingly.  */
5847                 switch (id) {
5848                 case java_awt_event_KeyEvent_KEY_PRESSED:
5849                 {
5850                     UINT winKey = (UINT)msg.wParam;
5851                     bCharChanged = FALSE;
5852 
5853                     if (winKey == VK_PROCESSKEY) {
5854                         // Leave it up to IME
5855                         break;
5856                     }
5857 
5858                     if (keyCode != java_awt_event_KeyEvent_VK_UNDEFINED) {
5859                         UINT newWinKey, ignored;
5860                         p->JavaKeyToWindowsKey(keyCode, &newWinKey, &ignored, winKey);
5861                         if (newWinKey != 0) {
5862                             winKey = newWinKey;
5863                         }
5864                     }
5865 
5866                     BOOL isDeadKey = FALSE;
5867                     modifiedChar = p->WindowsKeyToJavaChar(winKey, modifiers, AwtComponent::NONE, isDeadKey);
5868                     bCharChanged = (keyChar != modifiedChar);
5869                 }
5870                 break;
5871 
5872                 case java_awt_event_KeyEvent_KEY_RELEASED:
5873                 {
5874                     keyDownConsumed = FALSE;
5875                     bCharChanged = FALSE;
5876                 }
5877                 break;
5878 
5879                 case java_awt_event_KeyEvent_KEY_TYPED:
5880                 {
5881                     if (bCharChanged)
5882                     {
5883                         unicodeChar = modifiedChar;
5884                     }
5885                     else
5886                     {
5887                         unicodeChar = keyChar;
5888                     }
5889                     bCharChanged = FALSE;
5890 
5891                     // Disable forwarding KEY_TYPED messages to peers of
5892                     // disabled components
5893                     if (p->isRecursivelyEnabled()) {
5894                         // send the character back to the native window for
5895                         // processing. The WM_AWT_FORWARD_CHAR handler will send
5896                         // this character to DefWindowProc
5897                         if (!::PostMessage(p->GetHWnd(), WM_AWT_FORWARD_CHAR,
5898                             MAKEWPARAM(unicodeChar, FALSE), msg.lParam)) {
5899                             JNU_ThrowInternalError(env, "Message not posted, native event queue may be full.");
5900                         }
5901                     }
5902                     env->DeleteGlobalRef(self);
5903                     env->DeleteGlobalRef(event);
5904                     delete nhes;
5905                     return;
5906                 }
5907                 break;
5908 
5909                 default:
5910                     break;
5911                 }
5912             }
5913 
5914             // ignore all InputMethodEvents
5915             if (self && (pData = JNI_GET_PDATA(self)) &&
5916                 id >= java_awt_event_InputMethodEvent_INPUT_METHOD_FIRST &&
5917                 id <= java_awt_event_InputMethodEvent_INPUT_METHOD_LAST) {
5918                 env->DeleteGlobalRef(self);
5919                 env->DeleteGlobalRef(event);
5920                 delete nhes;
5921                 return;
5922             }
5923 
5924             // Create copy for local msg
5925             MSG* pCopiedMsg = new MSG;
5926             memmove(pCopiedMsg, &msg, sizeof(MSG));
5927             // Event handler deletes msg
5928             p->PostHandleEventMessage(pCopiedMsg, FALSE);
5929 
5930             env->DeleteGlobalRef(self);
5931             env->DeleteGlobalRef(event);
5932             delete nhes;
5933             return;
5934         }
5935 
5936         /* Forward any valid synthesized events.  Currently only mouse and
5937          * key events are supported.
5938          */
5939         if (self == NULL || (pData = JNI_GET_PDATA(self)) == NULL) {
5940             env->DeleteGlobalRef(self);
5941             env->DeleteGlobalRef(event);
5942             delete nhes;
5943             return;
5944         }
5945 
5946         AwtComponent* p = (AwtComponent*)pData;
5947         if (id >= java_awt_event_KeyEvent_KEY_FIRST &&
5948             id <= java_awt_event_KeyEvent_KEY_LAST) {
5949             p->SynthesizeKeyMessage(env, event);
5950         } else if (id >= java_awt_event_MouseEvent_MOUSE_FIRST &&
5951                    id <= java_awt_event_MouseEvent_MOUSE_LAST) {
5952             p->SynthesizeMouseMessage(env, event);
5953         }
5954     }
5955 
5956 ret:
5957     if (self != NULL) {
5958         env->DeleteGlobalRef(self);
5959     }
5960     if (event != NULL) {
5961         env->DeleteGlobalRef(event);
5962     }
5963 
5964     delete nhes;
5965 }
5966 
5967 void AwtComponent::_SetForeground(void *param)
5968 {
5969     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5970 
5971     SetColorStruct *scs = (SetColorStruct *)param;
5972     jobject self = scs->component;
5973     jint rgb = scs->rgb;
5974 
5975     AwtComponent *c = NULL;
5976 
5977     PDATA pData;
5978     JNI_CHECK_PEER_GOTO(self, ret);
5979     c = (AwtComponent *)pData;
5980     if (::IsWindow(c->GetHWnd()))
5981     {
5982         c->SetColor(PALETTERGB((rgb>>16)&0xff,
5983                                (rgb>>8)&0xff,
5984                                (rgb)&0xff));
5985         c->VerifyState();
5986     }
5987 ret:
5988     env->DeleteGlobalRef(self);
5989 
5990     delete scs;
5991 }
5992 
5993 void AwtComponent::_SetBackground(void *param)
5994 {
5995     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
5996 
5997     SetColorStruct *scs = (SetColorStruct *)param;
5998     jobject self = scs->component;
5999     jint rgb = scs->rgb;
6000 
6001     AwtComponent *c = NULL;
6002 
6003     PDATA pData;
6004     JNI_CHECK_PEER_GOTO(self, ret);
6005     c = (AwtComponent *)pData;
6006     if (::IsWindow(c->GetHWnd()))
6007     {
6008         c->SetBackgroundColor(PALETTERGB((rgb>>16)&0xff,
6009                                          (rgb>>8)&0xff,
6010                                          (rgb)&0xff));
6011         c->VerifyState();
6012     }
6013 ret:
6014     env->DeleteGlobalRef(self);
6015 
6016     delete scs;
6017 }
6018 
6019 void AwtComponent::_SetFont(void *param)
6020 {
6021     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6022 
6023     SetFontStruct *sfs = (SetFontStruct *)param;
6024     jobject self = sfs->component;
6025     jobject font = sfs->font;
6026 
6027     AwtComponent *c = NULL;
6028 
6029     PDATA pData;
6030     JNI_CHECK_PEER_GOTO(self, ret);
6031     JNI_CHECK_NULL_GOTO(font, "null font", ret);
6032     c = (AwtComponent *)pData;
6033     if (::IsWindow(c->GetHWnd()))
6034     {
6035         AwtFont *awtFont = (AwtFont *)env->GetLongField(font, AwtFont::pDataID);
6036         if (awtFont == NULL) {
6037             /*arguments of AwtFont::Create are changed for multifont component */
6038             awtFont = AwtFont::Create(env, font);
6039         }
6040         env->SetLongField(font, AwtFont::pDataID, (jlong)awtFont);
6041 
6042         c->SetFont(awtFont);
6043     }
6044 ret:
6045     env->DeleteGlobalRef(self);
6046     env->DeleteGlobalRef(font);
6047 
6048     delete sfs;
6049 }
6050 
6051 // Sets or kills focus for a component.
6052 void AwtComponent::_SetFocus(void *param)
6053 {
6054     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6055 
6056     SetFocusStruct *sfs = (SetFocusStruct *)param;
6057     jobject self = sfs->component;
6058     jboolean doSetFocus = sfs->doSetFocus;
6059 
6060     AwtComponent *c = NULL;
6061 
6062     PDATA pData;
6063     JNI_CHECK_NULL_GOTO(self, "peer", ret);
6064     pData = JNI_GET_PDATA(self);
6065     if (pData == NULL) {
6066         // do nothing just return false
6067         goto ret;
6068     }
6069 
6070     c = (AwtComponent *)pData;
6071     if (::IsWindow(c->GetHWnd())) {
6072         c->SendMessage(WM_AWT_COMPONENT_SETFOCUS, (WPARAM)doSetFocus, 0);
6073     }
6074 ret:
6075     env->DeleteGlobalRef(self);
6076 
6077     delete sfs;
6078 }
6079 
6080 void AwtComponent::_Start(void *param)
6081 {
6082     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6083 
6084     jobject self = (jobject)param;
6085 
6086     AwtComponent *c = NULL;
6087 
6088     PDATA pData;
6089     JNI_CHECK_PEER_GOTO(self, ret);
6090     c = (AwtComponent *)pData;
6091     if (::IsWindow(c->GetHWnd()))
6092     {
6093         jobject target = c->GetTarget(env);
6094 
6095         /* Disable window if specified -- windows are enabled by default. */
6096         jboolean enabled = (jboolean)env->GetBooleanField(target,
6097                                                           AwtComponent::enabledID);
6098         if (!enabled) {
6099             ::EnableWindow(c->GetHWnd(), FALSE);
6100         }
6101 
6102         /* The peer is now ready for callbacks, since this is the last
6103          * initialization call
6104          */
6105         c->EnableCallbacks(TRUE);
6106 
6107         // Fix 4745222: we need to invalidate region since we validated it before initialization.
6108         ::InvalidateRgn(c->GetHWnd(), NULL, FALSE);
6109 
6110         // Fix 4530093: WM_PAINT after EnableCallbacks
6111         ::UpdateWindow(c->GetHWnd());
6112 
6113         env->DeleteLocalRef(target);
6114     }
6115 ret:
6116     env->DeleteGlobalRef(self);
6117 }
6118 
6119 void AwtComponent::_BeginValidate(void *param)
6120 {
6121     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6122     if (AwtToolkit::IsMainThread()) {
6123         jobject self = (jobject)param;
6124         if (self != NULL) {
6125             PDATA pData = JNI_GET_PDATA(self);
6126             if (pData) {
6127                 AwtComponent *c = (AwtComponent *)pData;
6128                 if (::IsWindow(c->GetHWnd())) {
6129                     c->SendMessage(WM_AWT_BEGIN_VALIDATE);
6130                 }
6131             }
6132             env->DeleteGlobalRef(self);
6133         }
6134     } else {
6135         AwtToolkit::GetInstance().InvokeFunction(AwtComponent::_BeginValidate, param);
6136     }
6137 }
6138 
6139 void AwtComponent::_EndValidate(void *param)
6140 {
6141     if (AwtToolkit::IsMainThread()) {
6142         JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6143         jobject self = (jobject)param;
6144         if (self != NULL) {
6145             PDATA pData = JNI_GET_PDATA(self);
6146             if (pData) {
6147                 AwtComponent *c = (AwtComponent *)pData;
6148                 if (::IsWindow(c->GetHWnd())) {
6149                     c->SendMessage(WM_AWT_END_VALIDATE);
6150                 }
6151             }
6152             env->DeleteGlobalRef(self);
6153         }
6154     } else {
6155         AwtToolkit::GetInstance().InvokeFunction(AwtComponent::_EndValidate, param);
6156     }
6157 }
6158 
6159 void AwtComponent::_UpdateWindow(void *param)
6160 {
6161     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6162     if (AwtToolkit::IsMainThread()) {
6163         jobject self = (jobject)param;
6164         AwtComponent *c = NULL;
6165         PDATA pData;
6166         JNI_CHECK_PEER_GOTO(self, ret);
6167         c = (AwtComponent *)pData;
6168         if (::IsWindow(c->GetHWnd())) {
6169             ::UpdateWindow(c->GetHWnd());
6170         }
6171 ret:
6172         env->DeleteGlobalRef(self);
6173     } else {
6174         AwtToolkit::GetInstance().InvokeFunction(AwtComponent::_UpdateWindow, param);
6175     }
6176 }
6177 
6178 jlong AwtComponent::_AddNativeDropTarget(void *param)
6179 {
6180     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6181 
6182     jobject self = (jobject)param;
6183 
6184     jlong result = 0;
6185     AwtComponent *c = NULL;
6186 
6187     PDATA pData;
6188     JNI_CHECK_PEER_GOTO(self, ret);
6189     c = (AwtComponent *)pData;
6190     if (::IsWindow(c->GetHWnd()))
6191     {
6192         result = (jlong)(c->CreateDropTarget(env));
6193     }
6194 ret:
6195     env->DeleteGlobalRef(self);
6196 
6197     return result;
6198 }
6199 
6200 void AwtComponent::_RemoveNativeDropTarget(void *param)
6201 {
6202     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6203 
6204     jobject self = (jobject)param;
6205 
6206     AwtComponent *c = NULL;
6207 
6208     PDATA pData;
6209     JNI_CHECK_PEER_GOTO(self, ret);
6210     c = (AwtComponent *)pData;
6211     if (::IsWindow(c->GetHWnd()))
6212     {
6213         c->DestroyDropTarget();
6214     }
6215 ret:
6216     env->DeleteGlobalRef(self);
6217 }
6218 
6219 jintArray AwtComponent::_CreatePrintedPixels(void *param)
6220 {
6221     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6222 
6223     CreatePrintedPixelsStruct *cpps = (CreatePrintedPixelsStruct *)param;
6224     jobject self = cpps->component;
6225 
6226     jintArray result = NULL;
6227     AwtComponent *c = NULL;
6228 
6229     PDATA pData;
6230     JNI_CHECK_PEER_GOTO(self, ret);
6231     c = (AwtComponent *)pData;
6232     if (::IsWindow(c->GetHWnd()))
6233     {
6234         result = (jintArray)c->SendMessage(WM_AWT_CREATE_PRINTED_PIXELS, (WPARAM)cpps, 0);
6235     }
6236 ret:
6237     env->DeleteGlobalRef(self);
6238 
6239     delete cpps;
6240     return result; // this reference is global
6241 }
6242 
6243 jboolean AwtComponent::_IsObscured(void *param)
6244 {
6245     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6246 
6247     jobject self = (jobject)param;
6248 
6249     jboolean result = JNI_FALSE;
6250     AwtComponent *c = NULL;
6251 
6252     PDATA pData;
6253     JNI_CHECK_PEER_GOTO(self, ret);
6254 
6255     c = (AwtComponent *)pData;
6256 
6257     if (::IsWindow(c->GetHWnd()))
6258     {
6259         HWND hWnd = c->GetHWnd();
6260         HDC hDC = ::GetDC(hWnd);
6261         RECT clipbox;
6262         int callresult = ::GetClipBox(hDC, &clipbox);
6263         switch(callresult) {
6264             case NULLREGION :
6265                 result = JNI_FALSE;
6266                 break;
6267             case SIMPLEREGION : {
6268                 RECT windowRect;
6269                 if (!::GetClientRect(hWnd, &windowRect)) {
6270                     result = JNI_TRUE;
6271                 } else {
6272                     result  = (jboolean)((clipbox.bottom != windowRect.bottom)
6273                         || (clipbox.left != windowRect.left)
6274                         || (clipbox.right != windowRect.right)
6275                         || (clipbox.top != windowRect.top));
6276                 }
6277                 break;
6278             }
6279             case COMPLEXREGION :
6280             default :
6281                 result = JNI_TRUE;
6282                 break;
6283         }
6284         ::ReleaseDC(hWnd, hDC);
6285     }
6286 ret:
6287     env->DeleteGlobalRef(self);
6288 
6289     return result;
6290 }
6291 
6292 jboolean AwtComponent::_NativeHandlesWheelScrolling(void *param)
6293 {
6294     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6295 
6296     jobject self = (jobject)param;
6297 
6298     jboolean result = JNI_FALSE;
6299     AwtComponent *c = NULL;
6300 
6301     PDATA pData;
6302     JNI_CHECK_PEER_GOTO(self, ret);
6303     c = (AwtComponent *)pData;
6304     if (::IsWindow(c->GetHWnd()))
6305     {
6306         result = JNI_IS_TRUE(c->InheritsNativeMouseWheelBehavior());
6307     }
6308 ret:
6309     env->DeleteGlobalRef(self);
6310 
6311     return result;
6312 }
6313 
6314 void AwtComponent::_SetParent(void * param)
6315 {
6316     if (AwtToolkit::IsMainThread()) {
6317         JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6318         SetParentStruct *data = (SetParentStruct*) param;
6319         jobject self = data->component;
6320         jobject parent = data->parentComp;
6321 
6322         AwtComponent *awtComponent = NULL;
6323         AwtComponent *awtParent = NULL;
6324 
6325         PDATA pData;
6326         JNI_CHECK_PEER_GOTO(self, ret);
6327         awtComponent = (AwtComponent *)pData;
6328         JNI_CHECK_PEER_GOTO(parent, ret);
6329         awtParent = (AwtComponent *)pData;
6330 
6331         HWND selfWnd = awtComponent->GetHWnd();
6332         HWND parentWnd = awtParent->GetHWnd();
6333         if (::IsWindow(selfWnd) && ::IsWindow(parentWnd)) {
6334             // Shouldn't trigger native focus change
6335             // (only the proxy may be the native focus owner).
6336             ::SetParent(selfWnd, parentWnd);
6337         }
6338 ret:
6339         env->DeleteGlobalRef(self);
6340         env->DeleteGlobalRef(parent);
6341         delete data;
6342     } else {
6343         AwtToolkit::GetInstance().InvokeFunction(AwtComponent::_SetParent, param);
6344     }
6345 }
6346 
6347 void AwtComponent::_SetRectangularShape(void *param)
6348 {
6349     if (!AwtToolkit::IsMainThread()) {
6350         AwtToolkit::GetInstance().InvokeFunction(AwtComponent::_SetRectangularShape, param);
6351     } else {
6352         JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6353 
6354         SetRectangularShapeStruct *data = (SetRectangularShapeStruct *)param;
6355         jobject self = data->component;
6356         jint x1 = data->x1;
6357         jint x2 = data->x2;
6358         jint y1 = data->y1;
6359         jint y2 = data->y2;
6360         jobject region = data->region;
6361 
6362         AwtComponent *c = NULL;
6363 
6364         PDATA pData;
6365         JNI_CHECK_PEER_GOTO(self, ret);
6366 
6367         c = (AwtComponent *)pData;
6368         if (::IsWindow(c->GetHWnd())) {
6369             HRGN hRgn = NULL;
6370 
6371             // If all the params are zeros, the shape must be simply reset.
6372             // Otherwise, convert it into a region.
6373             if (region || x1 || x2 || y1 || y2) {
6374                 RECT_T rects[256];
6375                 RECT_T *pRect = rects;
6376 
6377                 const int numrects = RegionToYXBandedRectangles(env, x1, y1, x2, y2,
6378                         region, &pRect, sizeof(rects)/sizeof(rects[0]));
6379                 if (!pRect) {
6380                     // RegionToYXBandedRectangles doesn't use safe_Malloc(),
6381                     // so throw the exception explicitly
6382                     throw std::bad_alloc();
6383                 }
6384 
6385                 RGNDATA *pRgnData = (RGNDATA *) SAFE_SIZE_STRUCT_ALLOC(safe_Malloc,
6386                         sizeof(RGNDATAHEADER), sizeof(RECT_T), numrects);
6387                 memcpy((BYTE*)pRgnData + sizeof(RGNDATAHEADER), pRect, sizeof(RECT_T) * numrects);
6388                 if (pRect != rects) {
6389                     free(pRect);
6390                 }
6391                 pRect = NULL;
6392 
6393                 RGNDATAHEADER *pRgnHdr = (RGNDATAHEADER *) pRgnData;
6394                 pRgnHdr->dwSize = sizeof(RGNDATAHEADER);
6395                 pRgnHdr->iType = RDH_RECTANGLES;
6396                 pRgnHdr->nRgnSize = 0;
6397                 pRgnHdr->rcBound.top = 0;
6398                 pRgnHdr->rcBound.left = 0;
6399                 pRgnHdr->rcBound.bottom = LONG(y2 - y1);
6400                 pRgnHdr->rcBound.right = LONG(x2 - x1);
6401                 pRgnHdr->nCount = numrects;
6402 
6403                 hRgn = ::ExtCreateRegion(NULL,
6404                         sizeof(RGNDATAHEADER) + sizeof(RECT_T) * pRgnHdr->nCount, pRgnData);
6405 
6406                 free(pRgnData);
6407             }
6408 
6409             ::SetWindowRgn(c->GetHWnd(), hRgn, TRUE);
6410         }
6411 
6412 ret:
6413         env->DeleteGlobalRef(self);
6414         if (region) {
6415             env->DeleteGlobalRef(region);
6416         }
6417 
6418         delete data;
6419     }
6420 }
6421 
6422 void AwtComponent::_SetZOrder(void *param) {
6423     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6424 
6425     SetZOrderStruct *data = (SetZOrderStruct *)param;
6426     jobject self = data->component;
6427     HWND above = HWND_TOP;
6428     if (data->above != 0) {
6429         above = reinterpret_cast<HWND>(data->above);
6430     }
6431 
6432     AwtComponent *c = NULL;
6433 
6434     PDATA pData;
6435     JNI_CHECK_PEER_GOTO(self, ret);
6436 
6437     c = (AwtComponent *)pData;
6438     if (::IsWindow(c->GetHWnd())) {
6439         ::SetWindowPos(c->GetHWnd(), above, 0, 0, 0, 0,
6440                        SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_DEFERERASE | SWP_ASYNCWINDOWPOS);
6441     }
6442 
6443 ret:
6444     env->DeleteGlobalRef(self);
6445 
6446     delete data;
6447 }
6448 
6449 void AwtComponent::PostUngrabEvent() {
6450     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6451     jobject target = GetTarget(env);
6452     jobject event = JNU_NewObjectByName(env, "sun/awt/UngrabEvent", "(Ljava/awt/Component;)V",
6453                                         target);
6454     if (safe_ExceptionOccurred(env)) {
6455         env->ExceptionDescribe();
6456         env->ExceptionClear();
6457     }
6458     env->DeleteLocalRef(target);
6459     if (event != NULL) {
6460         SendEvent(event);
6461         env->DeleteLocalRef(event);
6462     }
6463 }
6464 
6465 void AwtComponent::SetFocusedWindow(HWND window)
6466 {
6467     HWND old = sm_focusedWindow;
6468     sm_focusedWindow = window;
6469 
6470     AwtWindow::FocusedWindowChanged(old, window);
6471 }
6472 
6473 /************************************************************************
6474  * Component native methods
6475  */
6476 
6477 extern "C" {
6478 
6479 /**
6480  * This method is called from the WGL pipeline when it needs to retrieve
6481  * the HWND associated with a ComponentPeer's C++ level object.
6482  */
6483 HWND
6484 AwtComponent_GetHWnd(JNIEnv *env, jlong pData)
6485 {
6486     AwtComponent *p = (AwtComponent *)jlong_to_ptr(pData);
6487     if (p == NULL) {
6488         return (HWND)0;
6489     }
6490     return p->GetHWnd();
6491 }
6492 
6493 static void _GetInsets(void* param)
6494 {
6495     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
6496 
6497     GetInsetsStruct *gis = (GetInsetsStruct *)param;
6498     jobject self = gis->window;
6499 
6500     gis->insets->left = gis->insets->top =
6501         gis->insets->right = gis->insets->bottom = 0;
6502 
6503     PDATA pData;
6504     JNI_CHECK_PEER_GOTO(self, ret);
6505     AwtComponent *component = (AwtComponent *)pData;
6506 
6507     component->GetInsets(gis->insets);
6508 
6509   ret:
6510     env->DeleteGlobalRef(self);
6511     delete gis;
6512 }
6513 
6514 /**
6515  * This method is called from the WGL pipeline when it needs to retrieve
6516  * the insets associated with a ComponentPeer's C++ level object.
6517  */
6518 void AwtComponent_GetInsets(JNIEnv *env, jobject peer, RECT *insets)
6519 {
6520     TRY;
6521 
6522     GetInsetsStruct *gis = new GetInsetsStruct;
6523     gis->window = env->NewGlobalRef(peer);
6524     gis->insets = insets;
6525 
6526     AwtToolkit::GetInstance().InvokeFunction(_GetInsets, gis);
6527     // global refs and mds are deleted in _UpdateWindow
6528 
6529     CATCH_BAD_ALLOC;
6530 
6531 }
6532 
6533 JNIEXPORT void JNICALL
6534 Java_java_awt_Component_initIDs(JNIEnv *env, jclass cls)
6535 {
6536     TRY;
6537     jclass inputEventClazz = env->FindClass("java/awt/event/InputEvent");
6538     CHECK_NULL(inputEventClazz);
6539     jmethodID getButtonDownMasksID = env->GetStaticMethodID(inputEventClazz, "getButtonDownMasks", "()[I");
6540     CHECK_NULL(getButtonDownMasksID);
6541     jintArray obj = (jintArray)env->CallStaticObjectMethod(inputEventClazz, getButtonDownMasksID);
6542     jint * tmp = env->GetIntArrayElements(obj, JNI_FALSE);
6543     CHECK_NULL(tmp);
6544     jsize len = env->GetArrayLength(obj);
6545     AwtComponent::masks = SAFE_SIZE_NEW_ARRAY(jint, len);
6546     for (int i = 0; i < len; i++) {
6547         AwtComponent::masks[i] = tmp[i];
6548     }
6549     env->ReleaseIntArrayElements(obj, tmp, 0);
6550     env->DeleteLocalRef(obj);
6551 
6552     /* class ids */
6553     jclass peerCls = env->FindClass("sun/awt/windows/WComponentPeer");
6554 
6555     DASSERT(peerCls);
6556     CHECK_NULL(peerCls);
6557 
6558     /* field ids */
6559     AwtComponent::peerID =
6560       env->GetFieldID(cls, "peer", "Ljava/awt/peer/ComponentPeer;");
6561     DASSERT(AwtComponent::peerID);
6562     CHECK_NULL(AwtComponent::peerID);
6563 
6564     AwtComponent::xID = env->GetFieldID(cls, "x", "I");
6565     DASSERT(AwtComponent::xID);
6566     CHECK_NULL(AwtComponent::xID);
6567 
6568     AwtComponent::yID = env->GetFieldID(cls, "y", "I");
6569     DASSERT(AwtComponent::yID);
6570     CHECK_NULL(AwtComponent::yID);
6571 
6572     AwtComponent::heightID = env->GetFieldID(cls, "height", "I");
6573     DASSERT(AwtComponent::heightID);
6574     CHECK_NULL(AwtComponent::heightID);
6575 
6576     AwtComponent::widthID = env->GetFieldID(cls, "width", "I");
6577     DASSERT(AwtComponent::widthID);
6578     CHECK_NULL(AwtComponent::widthID);
6579 
6580     AwtComponent::visibleID = env->GetFieldID(cls, "visible", "Z");
6581     DASSERT(AwtComponent::visibleID);
6582     CHECK_NULL(AwtComponent::visibleID);
6583 
6584     AwtComponent::backgroundID =
6585         env->GetFieldID(cls, "background", "Ljava/awt/Color;");
6586     DASSERT(AwtComponent::backgroundID);
6587     CHECK_NULL(AwtComponent::backgroundID);
6588 
6589     AwtComponent::foregroundID =
6590         env->GetFieldID(cls, "foreground", "Ljava/awt/Color;");
6591     DASSERT(AwtComponent::foregroundID);
6592     CHECK_NULL(AwtComponent::foregroundID);
6593 
6594     AwtComponent::enabledID = env->GetFieldID(cls, "enabled", "Z");
6595     DASSERT(AwtComponent::enabledID);
6596     CHECK_NULL(AwtComponent::enabledID);
6597 
6598     AwtComponent::parentID = env->GetFieldID(cls, "parent", "Ljava/awt/Container;");
6599     DASSERT(AwtComponent::parentID);
6600     CHECK_NULL(AwtComponent::parentID);
6601 
6602     AwtComponent::graphicsConfigID =
6603      env->GetFieldID(cls, "graphicsConfig", "Ljava/awt/GraphicsConfiguration;");
6604     DASSERT(AwtComponent::graphicsConfigID);
6605     CHECK_NULL(AwtComponent::graphicsConfigID);
6606 
6607     AwtComponent::focusableID = env->GetFieldID(cls, "focusable", "Z");
6608     DASSERT(AwtComponent::focusableID);
6609     CHECK_NULL(AwtComponent::focusableID);
6610 
6611     AwtComponent::appContextID = env->GetFieldID(cls, "appContext",
6612                                                  "Lsun/awt/AppContext;");
6613     DASSERT(AwtComponent::appContextID);
6614     CHECK_NULL(AwtComponent::appContextID);
6615 
6616     AwtComponent::peerGCID = env->GetFieldID(peerCls, "winGraphicsConfig",
6617                                         "Lsun/awt/Win32GraphicsConfig;");
6618     DASSERT(AwtComponent::peerGCID);
6619     CHECK_NULL(AwtComponent::peerGCID);
6620 
6621     AwtComponent::hwndID = env->GetFieldID(peerCls, "hwnd", "J");
6622     DASSERT(AwtComponent::hwndID);
6623     CHECK_NULL(AwtComponent::hwndID);
6624 
6625     AwtComponent::cursorID = env->GetFieldID(cls, "cursor", "Ljava/awt/Cursor;");
6626     DASSERT(AwtComponent::cursorID);
6627     CHECK_NULL(AwtComponent::cursorID);
6628 
6629     /* method ids */
6630     AwtComponent::getFontMID =
6631         env->GetMethodID(cls, "getFont_NoClientCode", "()Ljava/awt/Font;");
6632     DASSERT(AwtComponent::getFontMID);
6633     CHECK_NULL(AwtComponent::getFontMID);
6634 
6635     AwtComponent::getToolkitMID =
6636         env->GetMethodID(cls, "getToolkitImpl", "()Ljava/awt/Toolkit;");
6637     DASSERT(AwtComponent::getToolkitMID);
6638     CHECK_NULL(AwtComponent::getToolkitMID);
6639 
6640     AwtComponent::isEnabledMID = env->GetMethodID(cls, "isEnabledImpl", "()Z");
6641     DASSERT(AwtComponent::isEnabledMID);
6642     CHECK_NULL(AwtComponent::isEnabledMID);
6643 
6644     AwtComponent::getLocationOnScreenMID =
6645         env->GetMethodID(cls, "getLocationOnScreen_NoTreeLock", "()Ljava/awt/Point;");
6646     DASSERT(AwtComponent::getLocationOnScreenMID);
6647     CHECK_NULL(AwtComponent::getLocationOnScreenMID);
6648 
6649     AwtComponent::replaceSurfaceDataMID =
6650         env->GetMethodID(peerCls, "replaceSurfaceData", "()V");
6651     DASSERT(AwtComponent::replaceSurfaceDataMID);
6652     CHECK_NULL(AwtComponent::replaceSurfaceDataMID);
6653 
6654     AwtComponent::replaceSurfaceDataLaterMID =
6655         env->GetMethodID(peerCls, "replaceSurfaceDataLater", "()V");
6656     DASSERT(AwtComponent::replaceSurfaceDataLaterMID);
6657     CHECK_NULL(AwtComponent::replaceSurfaceDataLaterMID);
6658 
6659     AwtComponent::disposeLaterMID = env->GetMethodID(peerCls, "disposeLater", "()V");
6660     DASSERT(AwtComponent::disposeLaterMID);
6661     CHECK_NULL(AwtComponent::disposeLaterMID);
6662 
6663     CATCH_BAD_ALLOC;
6664 }
6665 
6666 } /* extern "C" */
6667 
6668 
6669 /************************************************************************
6670  * ComponentPeer native methods
6671  */
6672 
6673 extern "C" {
6674 
6675 /*
6676  * Class:     sun_awt_windows_WComponentPeer
6677  * Method:    pShow
6678  * Signature: ()V
6679  */
6680 JNIEXPORT void JNICALL
6681 Java_sun_awt_windows_WComponentPeer_pShow(JNIEnv *env, jobject self)
6682 {
6683     TRY;
6684 
6685     jobject selfGlobalRef = env->NewGlobalRef(self);
6686 
6687     AwtToolkit::GetInstance().SyncCall(AwtComponent::_Show, (void *)selfGlobalRef);
6688     // selfGlobalRef is deleted in _Show
6689 
6690     CATCH_BAD_ALLOC;
6691 }
6692 
6693 /*
6694  * Class:     sun_awt_windows_WComponentPeer
6695  * Method:    hide
6696  * Signature: ()V
6697  */
6698 JNIEXPORT void JNICALL
6699 Java_sun_awt_windows_WComponentPeer_hide(JNIEnv *env, jobject self)
6700 {
6701     TRY;
6702 
6703     jobject selfGlobalRef = env->NewGlobalRef(self);
6704 
6705     AwtToolkit::GetInstance().SyncCall(AwtComponent::_Hide, (void *)selfGlobalRef);
6706     // selfGlobalRef is deleted in _Hide
6707 
6708     CATCH_BAD_ALLOC;
6709 }
6710 
6711 /*
6712  * Class:     sun_awt_windows_WComponentPeer
6713  * Method:    enable
6714  * Signature: ()V
6715  */
6716 JNIEXPORT void JNICALL
6717 Java_sun_awt_windows_WComponentPeer_enable(JNIEnv *env, jobject self)
6718 {
6719     TRY;
6720 
6721     jobject selfGlobalRef = env->NewGlobalRef(self);
6722 
6723     AwtToolkit::GetInstance().SyncCall(AwtComponent::_Enable, (void *)selfGlobalRef);
6724     // selfGlobalRef is deleted in _Enable
6725 
6726     CATCH_BAD_ALLOC;
6727 }
6728 
6729 /*
6730  * Class:     sun_awt_windows_WComponentPeer
6731  * Method:    disable
6732  * Signature: ()V
6733  */
6734 JNIEXPORT void JNICALL
6735 Java_sun_awt_windows_WComponentPeer_disable(JNIEnv *env, jobject self)
6736 {
6737     TRY;
6738 
6739     jobject selfGlobalRef = env->NewGlobalRef(self);
6740 
6741     AwtToolkit::GetInstance().SyncCall(AwtComponent::_Disable, (void *)selfGlobalRef);
6742     // selfGlobalRef is deleted in _Disable
6743 
6744     CATCH_BAD_ALLOC;
6745 }
6746 
6747 /*
6748  * Class:     sun_awt_windows_WComponentPeer
6749  * Method:    getLocationOnScreen
6750  * Signature: ()Ljava/awt/Point;
6751  */
6752 JNIEXPORT jobject JNICALL
6753 Java_sun_awt_windows_WComponentPeer_getLocationOnScreen(JNIEnv *env, jobject self)
6754 {
6755     TRY;
6756 
6757     jobject selfGlobalRef = env->NewGlobalRef(self);
6758 
6759     jobject resultGlobalRef = (jobject)AwtToolkit::GetInstance().SyncCall(
6760         (void*(*)(void*))AwtComponent::_GetLocationOnScreen, (void *)selfGlobalRef);
6761     // selfGlobalRef is deleted in _GetLocationOnScreen
6762     if (resultGlobalRef != NULL)
6763     {
6764         jobject resultLocalRef = env->NewLocalRef(resultGlobalRef);
6765         env->DeleteGlobalRef(resultGlobalRef);
6766         return resultLocalRef;
6767     }
6768 
6769     return NULL;
6770 
6771     CATCH_BAD_ALLOC_RET(NULL);
6772 }
6773 
6774 /*
6775  * Class:     sun_awt_windows_WComponentPeer
6776  * Method:    reshape
6777  * Signature: (IIII)V
6778  */
6779 JNIEXPORT void JNICALL
6780 Java_sun_awt_windows_WComponentPeer_reshape(JNIEnv *env, jobject self,
6781                                             jint x, jint y, jint w, jint h)
6782 {
6783     TRY;
6784 
6785     ReshapeStruct *rs = new ReshapeStruct;
6786     rs->component = env->NewGlobalRef(self);
6787     rs->x = x;
6788     rs->y = y;
6789     rs->w = w;
6790     rs->h = h;
6791 
6792     AwtToolkit::GetInstance().SyncCall(AwtComponent::_Reshape, rs);
6793     // global ref and rs are deleted in _Reshape
6794 
6795     CATCH_BAD_ALLOC;
6796 }
6797 
6798 /*
6799  * Class:     sun_awt_windows_WComponentPeer
6800  * Method:    reshape
6801  * Signature: (IIII)V
6802  */
6803 JNIEXPORT void JNICALL
6804 Java_sun_awt_windows_WComponentPeer_reshapeNoCheck(JNIEnv *env, jobject self,
6805                                             jint x, jint y, jint w, jint h)
6806 {
6807     TRY;
6808 
6809     ReshapeStruct *rs = new ReshapeStruct;
6810     rs->component = env->NewGlobalRef(self);
6811     rs->x = x;
6812     rs->y = y;
6813     rs->w = w;
6814     rs->h = h;
6815 
6816     AwtToolkit::GetInstance().SyncCall(AwtComponent::_ReshapeNoCheck, rs);
6817     // global ref and rs are deleted in _ReshapeNoCheck
6818 
6819     CATCH_BAD_ALLOC;
6820 }
6821 
6822 
6823 /*
6824  * Class:     sun_awt_windows_WComponentPeer
6825  * Method:    nativeHandleEvent
6826  * Signature: (Ljava/awt/AWTEvent;)V
6827  */
6828 JNIEXPORT void JNICALL
6829 Java_sun_awt_windows_WComponentPeer_nativeHandleEvent(JNIEnv *env,
6830                                                       jobject self,
6831                                                       jobject event)
6832 {
6833     TRY;
6834 
6835     jobject selfGlobalRef = env->NewGlobalRef(self);
6836     jobject eventGlobalRef = env->NewGlobalRef(event);
6837 
6838     NativeHandleEventStruct *nhes = new NativeHandleEventStruct;
6839     nhes->component = selfGlobalRef;
6840     nhes->event = eventGlobalRef;
6841 
6842     AwtToolkit::GetInstance().SyncCall(AwtComponent::_NativeHandleEvent, nhes);
6843     // global refs and nhes are deleted in _NativeHandleEvent
6844 
6845     CATCH_BAD_ALLOC;
6846 }
6847 
6848 /*
6849  * Class:     sun_awt_windows_WComponentPeer
6850  * Method:    _dispose
6851  * Signature: ()V
6852  */
6853 JNIEXPORT void JNICALL
6854 Java_sun_awt_windows_WComponentPeer__1dispose(JNIEnv *env, jobject self)
6855 {
6856     TRY_NO_HANG;
6857 
6858     AwtObject::_Dispose(self);
6859 
6860     CATCH_BAD_ALLOC;
6861 }
6862 
6863 /*
6864  * Class:     sun_awt_windows_WComponentPeer
6865  * Method:    _setForeground
6866  * Signature: (I)V
6867  */
6868 JNIEXPORT void JNICALL
6869 Java_sun_awt_windows_WComponentPeer__1setForeground(JNIEnv *env, jobject self,
6870                                                     jint rgb)
6871 {
6872     TRY;
6873 
6874     jobject selfGlobalRef = env->NewGlobalRef(self);
6875 
6876     SetColorStruct *scs = new SetColorStruct;
6877     scs->component = selfGlobalRef;
6878     scs->rgb = rgb;
6879 
6880     AwtToolkit::GetInstance().SyncCall(AwtComponent::_SetForeground, scs);
6881     // selfGlobalRef and scs are deleted in _SetForeground()
6882 
6883     CATCH_BAD_ALLOC;
6884 }
6885 
6886 /*
6887  * Class:     sun_awt_windows_WComponentPeer
6888  * Method:    _setBackground
6889  * Signature: (I)V
6890  */
6891 JNIEXPORT void JNICALL
6892 Java_sun_awt_windows_WComponentPeer__1setBackground(JNIEnv *env, jobject self,
6893                                                     jint rgb)
6894 {
6895     TRY;
6896 
6897     jobject selfGlobalRef = env->NewGlobalRef(self);
6898 
6899     SetColorStruct *scs = new SetColorStruct;
6900     scs->component = selfGlobalRef;
6901     scs->rgb = rgb;
6902 
6903     AwtToolkit::GetInstance().SyncCall(AwtComponent::_SetBackground, scs);
6904     // selfGlobalRef and scs are deleted in _SetBackground()
6905 
6906     CATCH_BAD_ALLOC;
6907 }
6908 
6909 /*
6910  * Class:     sun_awt_windows_WComponentPeer
6911  * Method:    _setFont
6912  * Signature: (Ljava/awt/Font;)V
6913  */
6914 JNIEXPORT void JNICALL
6915 Java_sun_awt_windows_WComponentPeer__1setFont(JNIEnv *env, jobject self,
6916                         jobject font)
6917 {
6918     TRY;
6919 
6920     jobject selfGlobalRef = env->NewGlobalRef(self);
6921     jobject fontGlobalRef = env->NewGlobalRef(font);
6922 
6923     SetFontStruct *sfs = new SetFontStruct;
6924     sfs->component = selfGlobalRef;
6925     sfs->font = fontGlobalRef;
6926 
6927     AwtToolkit::GetInstance().SyncCall(AwtComponent::_SetFont, sfs);
6928     // global refs and sfs are deleted in _SetFont()
6929 
6930     CATCH_BAD_ALLOC;
6931 }
6932 
6933 /*
6934  * Class:     sun_awt_windows_WComponentPeer
6935  * Method:    focusGained
6936  * Signature: (Z)
6937  */
6938 JNIEXPORT void JNICALL Java_sun_awt_windows_WComponentPeer_setFocus
6939     (JNIEnv *env, jobject self, jboolean doSetFocus)
6940 {
6941     TRY;
6942 
6943     jobject selfGlobalRef = env->NewGlobalRef(self);
6944 
6945     SetFocusStruct *sfs = new SetFocusStruct;
6946     sfs->component = selfGlobalRef;
6947     sfs->doSetFocus = doSetFocus;
6948 
6949     AwtToolkit::GetInstance().SyncCall(
6950         (void*(*)(void*))AwtComponent::_SetFocus, sfs);
6951     // global refs and self are deleted in _SetFocus
6952 
6953     CATCH_BAD_ALLOC;
6954 }
6955 
6956 /*
6957  * Class:     sun_awt_windows_WComponentPeer
6958  * Method:    start
6959  * Signature: ()V
6960  */
6961 JNIEXPORT void JNICALL
6962 Java_sun_awt_windows_WComponentPeer_start(JNIEnv *env, jobject self)
6963 {
6964     TRY;
6965 
6966     jobject selfGlobalRef = env->NewGlobalRef(self);
6967 
6968     AwtToolkit::GetInstance().SyncCall(AwtComponent::_Start, (void *)selfGlobalRef);
6969     // selfGlobalRef is deleted in _Start
6970 
6971     CATCH_BAD_ALLOC;
6972 }
6973 
6974 /*
6975  * Class:     sun_awt_windows_WComponentPeer
6976  * Method:    beginValidate
6977  * Signature: ()V
6978  */
6979 JNIEXPORT void JNICALL
6980 Java_sun_awt_windows_WComponentPeer_beginValidate(JNIEnv *env, jobject self)
6981 {
6982     TRY;
6983 
6984     jobject selfGlobalRef = env->NewGlobalRef(self);
6985 
6986     AwtToolkit::GetInstance().SyncCall(AwtComponent::_BeginValidate, (void *)selfGlobalRef);
6987     // selfGlobalRef is deleted in _BeginValidate
6988 
6989     CATCH_BAD_ALLOC;
6990 }
6991 
6992 /*
6993  * Class:     sun_awt_windows_WComponentPeer
6994  * Method:    endValidate
6995  * Signature: ()V
6996  */
6997 JNIEXPORT void JNICALL
6998 Java_sun_awt_windows_WComponentPeer_endValidate(JNIEnv *env, jobject self)
6999 {
7000     TRY;
7001 
7002     jobject selfGlobalRef = env->NewGlobalRef(self);
7003 
7004     AwtToolkit::GetInstance().SyncCall(AwtComponent::_EndValidate, (void *)selfGlobalRef);
7005     // selfGlobalRef is deleted in _EndValidate
7006 
7007     CATCH_BAD_ALLOC;
7008 }
7009 
7010 JNIEXPORT void JNICALL
7011 Java_sun_awt_windows_WComponentPeer_updateWindow(JNIEnv *env, jobject self)
7012 {
7013     TRY;
7014 
7015     jobject selfGlobalRef = env->NewGlobalRef(self);
7016 
7017     AwtToolkit::GetInstance().SyncCall(AwtComponent::_UpdateWindow, (void *)selfGlobalRef);
7018     // selfGlobalRef is deleted in _UpdateWindow
7019 
7020     CATCH_BAD_ALLOC;
7021 }
7022 
7023 /*
7024  * Class:     sun_awt_windows_WComponentPeer
7025  * Method:    addNativeDropTarget
7026  * Signature: ()L
7027  */
7028 
7029 JNIEXPORT jlong JNICALL
7030 Java_sun_awt_windows_WComponentPeer_addNativeDropTarget(JNIEnv *env,
7031                                                         jobject self)
7032 {
7033     TRY;
7034 
7035     jobject selfGlobalRef = env->NewGlobalRef(self);
7036 
7037     return ptr_to_jlong(AwtToolkit::GetInstance().SyncCall(
7038         (void*(*)(void*))AwtComponent::_AddNativeDropTarget,
7039         (void *)selfGlobalRef));
7040     // selfGlobalRef is deleted in _AddNativeDropTarget
7041 
7042     CATCH_BAD_ALLOC_RET(0);
7043 }
7044 
7045 /*
7046  * Class:     sun_awt_windows_WComponentPeer
7047  * Method:    removeNativeDropTarget
7048  * Signature: ()V
7049  */
7050 
7051 JNIEXPORT void JNICALL
7052 Java_sun_awt_windows_WComponentPeer_removeNativeDropTarget(JNIEnv *env,
7053                                                            jobject self)
7054 {
7055     TRY;
7056 
7057     jobject selfGlobalRef = env->NewGlobalRef(self);
7058 
7059     AwtToolkit::GetInstance().SyncCall(
7060         AwtComponent::_RemoveNativeDropTarget, (void *)selfGlobalRef);
7061     // selfGlobalRef is deleted in _RemoveNativeDropTarget
7062 
7063     CATCH_BAD_ALLOC;
7064 }
7065 
7066 /*
7067  * Class:     sun_awt_windows_WComponentPeer
7068  * Method:    getTargetGC
7069  * Signature: ()Ljava/awt/GraphicsConfiguration;
7070  */
7071 JNIEXPORT jobject JNICALL
7072 Java_sun_awt_windows_WComponentPeer_getTargetGC(JNIEnv* env, jobject theThis)
7073 {
7074     TRY;
7075 
7076     jobject targetObj;
7077     jobject gc = 0;
7078 
7079     targetObj = env->GetObjectField(theThis, AwtObject::targetID);
7080     DASSERT(targetObj);
7081 
7082     gc = env->GetObjectField(targetObj, AwtComponent::graphicsConfigID);
7083     return gc;
7084 
7085     CATCH_BAD_ALLOC_RET(NULL);
7086 }
7087 
7088 /*
7089  * Class:     sun_awt_windows_WComponentPeer
7090  * Method:    createPrintedPixels
7091  * Signature: (IIIIII)I[
7092  */
7093 JNIEXPORT jintArray JNICALL
7094 Java_sun_awt_windows_WComponentPeer_createPrintedPixels(JNIEnv* env,
7095     jobject self, jint srcX, jint srcY, jint srcW, jint srcH, jint alpha)
7096 {
7097     TRY;
7098 
7099     jobject selfGlobalRef = env->NewGlobalRef(self);
7100 
7101     CreatePrintedPixelsStruct *cpps = new CreatePrintedPixelsStruct;
7102     cpps->component = selfGlobalRef;
7103     cpps->srcx = srcX;
7104     cpps->srcy = srcY;
7105     cpps->srcw = srcW;
7106     cpps->srch = srcH;
7107     cpps->alpha = alpha;
7108 
7109     jintArray globalRef = (jintArray)AwtToolkit::GetInstance().SyncCall(
7110         (void*(*)(void*))AwtComponent::_CreatePrintedPixels, cpps);
7111     // selfGlobalRef and cpps are deleted in _CreatePrintedPixels
7112     if (globalRef != NULL)
7113     {
7114         jintArray localRef = (jintArray)env->NewLocalRef(globalRef);
7115         env->DeleteGlobalRef(globalRef);
7116         return localRef;
7117     }
7118     else
7119     {
7120         return NULL;
7121     }
7122 
7123     CATCH_BAD_ALLOC_RET(NULL);
7124 }
7125 
7126 /*
7127  * Class:     sun_awt_windows_WComponentPeer
7128  * Method:    nativeHandlesWheelScrolling
7129  * Signature: ()Z
7130  */
7131 JNIEXPORT jboolean JNICALL
7132 Java_sun_awt_windows_WComponentPeer_nativeHandlesWheelScrolling (JNIEnv* env,
7133     jobject self)
7134 {
7135     TRY;
7136 
7137     return (jboolean)((intptr_t)AwtToolkit::GetInstance().SyncCall(
7138         (void *(*)(void *))AwtComponent::_NativeHandlesWheelScrolling,
7139         env->NewGlobalRef(self)));
7140     // global ref is deleted in _NativeHandlesWheelScrolling
7141 
7142     CATCH_BAD_ALLOC_RET(NULL);
7143 }
7144 
7145 /*
7146  * Class:     sun_awt_windows_WComponentPeer
7147  * Method:    isObscured
7148  * Signature: ()Z
7149  */
7150 JNIEXPORT jboolean JNICALL
7151 Java_sun_awt_windows_WComponentPeer_isObscured(JNIEnv* env,
7152     jobject self)
7153 {
7154     TRY;
7155 
7156     jobject selfGlobalRef = env->NewGlobalRef(self);
7157 
7158     return (jboolean)((intptr_t)AwtToolkit::GetInstance().SyncCall(
7159         (void*(*)(void*))AwtComponent::_IsObscured,
7160         (void *)selfGlobalRef));
7161     // selfGlobalRef is deleted in _IsObscured
7162 
7163     CATCH_BAD_ALLOC_RET(NULL);
7164 }
7165 
7166 JNIEXPORT void JNICALL
7167 Java_sun_awt_windows_WComponentPeer_pSetParent(JNIEnv* env, jobject self, jobject parent) {
7168     TRY;
7169 
7170     SetParentStruct * data = new SetParentStruct;
7171     data->component = env->NewGlobalRef(self);
7172     data->parentComp = env->NewGlobalRef(parent);
7173 
7174     AwtToolkit::GetInstance().SyncCall(AwtComponent::_SetParent, data);
7175     // global refs and data are deleted in SetParent
7176 
7177     CATCH_BAD_ALLOC;
7178 }
7179 
7180 JNIEXPORT void JNICALL
7181 Java_sun_awt_windows_WComponentPeer_setRectangularShape(JNIEnv* env, jobject self,
7182         jint x1, jint y1, jint x2, jint y2, jobject region)
7183 {
7184     TRY;
7185 
7186     SetRectangularShapeStruct * data = new SetRectangularShapeStruct;
7187     data->component = env->NewGlobalRef(self);
7188     data->x1 = x1;
7189     data->x2 = x2;
7190     data->y1 = y1;
7191     data->y2 = y2;
7192     if (region) {
7193         data->region = env->NewGlobalRef(region);
7194     } else {
7195         data->region = NULL;
7196     }
7197 
7198     AwtToolkit::GetInstance().SyncCall(AwtComponent::_SetRectangularShape, data);
7199     // global refs and data are deleted in _SetRectangularShape
7200 
7201     CATCH_BAD_ALLOC;
7202 }
7203 
7204 JNIEXPORT void JNICALL
7205 Java_sun_awt_windows_WComponentPeer_setZOrder(JNIEnv* env, jobject self, jlong above)
7206 {
7207     TRY;
7208 
7209     SetZOrderStruct * data = new SetZOrderStruct;
7210     data->component = env->NewGlobalRef(self);
7211     data->above = above;
7212 
7213     AwtToolkit::GetInstance().SyncCall(AwtComponent::_SetZOrder, data);
7214     // global refs and data are deleted in _SetLower
7215 
7216     CATCH_BAD_ALLOC;
7217 }
7218 
7219 } /* extern "C" */
7220 
7221 
7222 /************************************************************************
7223  * Diagnostic routines
7224  */
7225 
7226 #ifdef DEBUG
7227 
7228 void AwtComponent::VerifyState()
7229 {
7230     if (AwtToolkit::GetInstance().VerifyComponents() == FALSE) {
7231         return;
7232     }
7233 
7234     if (m_callbacksEnabled == FALSE) {
7235         /* Component is not fully setup yet. */
7236         return;
7237     }
7238 
7239     /* Get target bounds. */
7240     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
7241     if (env->PushLocalFrame(10) < 0)
7242         return;
7243 
7244     jobject target = GetTarget(env);
7245 
7246     jint x = env->GetIntField(target, AwtComponent::xID);
7247     jint y = env->GetIntField(target, AwtComponent::yID);
7248     jint width = env->GetIntField(target, AwtComponent::widthID);
7249     jint height = env->GetIntField(target, AwtComponent::heightID);
7250 
7251     /* Convert target origin to absolute coordinates */
7252     while (TRUE) {
7253 
7254         jobject parent = env->GetObjectField(target, AwtComponent::parentID);
7255         if (parent == NULL) {
7256             break;
7257         }
7258         x += env->GetIntField(parent, AwtComponent::xID);
7259         y += env->GetIntField(parent, AwtComponent::yID);
7260 
7261         /* If this component has insets, factor them in, but ignore
7262          * top-level windows.
7263          */
7264         jobject parent2 = env->GetObjectField(parent, AwtComponent::parentID);
7265         if (parent2 != NULL) {
7266             jobject peer = GetPeerForTarget(env, parent);
7267             if (peer != NULL &&
7268                 JNU_IsInstanceOfByName(env, peer,
7269                                        "sun/awt/windows/WPanelPeer") > 0) {
7270                 jobject insets =
7271                     JNU_CallMethodByName(env, NULL, peer,"insets",
7272                                          "()Ljava/awt/Insets;").l;
7273                 x += (env)->GetIntField(insets, AwtInsets::leftID);
7274                 y += (env)->GetIntField(insets, AwtInsets::topID);
7275             }
7276         }
7277         env->DeleteLocalRef(target);
7278         target = parent;
7279     }
7280 
7281     x = ScaleUpX(x);
7282     y = ScaleUpY(y);
7283     width = ScaleUpX(width);
7284     height = ScaleUpY(height);
7285 
7286     // Test whether component's bounds match the native window's
7287     RECT rect;
7288     VERIFY(::GetWindowRect(GetHWnd(), &rect));
7289 #if 0
7290     DASSERT( (x == rect.left) &&
7291             (y == rect.top) &&
7292             (width == (rect.right-rect.left)) &&
7293             (height == (rect.bottom-rect.top)) );
7294 #else
7295     BOOL fSizeValid = ( (x == rect.left) &&
7296             (y == rect.top) &&
7297             (width == (rect.right-rect.left)) &&
7298             (height == (rect.bottom-rect.top)) );
7299 #endif
7300 
7301     // See if visible state matches
7302     BOOL wndVisible = ::IsWindowVisible(GetHWnd());
7303     jboolean targetVisible;
7304     // To avoid possibly running client code on the toolkit thread, don't
7305     // do the following check if we're running on the toolkit thread.
7306     if (AwtToolkit::MainThread() != ::GetCurrentThreadId()) {
7307         targetVisible = JNU_CallMethodByName(env, NULL, GetTarget(env),
7308                                                   "isShowing", "()Z").z;
7309         DASSERT(!safe_ExceptionOccurred(env));
7310     } else {
7311         targetVisible = wndVisible ? 1 : 0;
7312     }
7313 #if 0
7314     DASSERT( (targetVisible && wndVisible) ||
7315             (!targetVisible && !wndVisible) );
7316 #else
7317     BOOL fVisibleValid = ( (targetVisible && wndVisible) ||
7318             (!targetVisible && !wndVisible) );
7319 #endif
7320 
7321     // Check enabled state
7322     BOOL wndEnabled = ::IsWindowEnabled(GetHWnd());
7323     jboolean enabled = (jboolean)env->GetBooleanField(target,
7324                                                       AwtComponent::enabledID);
7325 #if 0
7326     DASSERT( (enabled && wndEnabled) ||
7327             (!enabled && !wndEnabled) );
7328 #else
7329     BOOL fEnabledValid = ((enabled && wndEnabled) ||
7330                           (!(enabled && !wndEnabled) ));
7331 
7332     if (!fSizeValid || !fVisibleValid || !fEnabledValid) {
7333         printf("AwtComponent::ValidateState() failed:\n");
7334         // To avoid possibly running client code on the toolkit thread, don't
7335         // do the following call if we're running on the toolkit thread.
7336         if (AwtToolkit::MainThread() != ::GetCurrentThreadId()) {
7337             jstring targetStr =
7338                 (jstring)JNU_CallMethodByName(env, NULL, GetTarget(env),
7339                                               "getName",
7340                                               "()Ljava/lang/String;").l;
7341             DASSERT(!safe_ExceptionOccurred(env));
7342             LPCWSTR targetStrW = JNU_GetStringPlatformChars(env, targetStr, NULL);
7343             printf("\t%S\n", targetStrW);
7344             JNU_ReleaseStringPlatformChars(env, targetStr, targetStrW);
7345         }
7346         printf("\twas:       [%d,%d,%dx%d]\n", x, y, width, height);
7347         if (!fSizeValid) {
7348             printf("\tshould be: [%d,%d,%dx%d]\n", rect.left, rect.top,
7349                    rect.right-rect.left, rect.bottom-rect.top);
7350         }
7351         if (!fVisibleValid) {
7352             printf("\tshould be: %s\n",
7353                    (targetVisible) ? "visible" : "hidden");
7354         }
7355         if (!fEnabledValid) {
7356             printf("\tshould be: %s\n",
7357                    enabled ? "enabled" : "disabled");
7358         }
7359     }
7360 #endif
7361     env->PopLocalFrame(0);
7362 }
7363 #endif //DEBUG
7364 
7365 // Methods for globally managed DC list
7366 
7367 /**
7368  * Add a new DC to the DC list for this component.
7369  */
7370 void DCList::AddDC(HDC hDC, HWND hWnd)
7371 {
7372     DCItem *newItem = new DCItem;
7373     newItem->hDC = hDC;
7374     newItem->hWnd = hWnd;
7375     AddDCItem(newItem);
7376 }
7377 
7378 void DCList::AddDCItem(DCItem *newItem)
7379 {
7380     listLock.Enter();
7381     newItem->next = head;
7382     head = newItem;
7383     listLock.Leave();
7384 }
7385 
7386 /**
7387  * Given a DC and window handle, remove the DC from the DC list
7388  * and return TRUE if it exists on the current list.  Otherwise
7389  * return FALSE.
7390  * A DC may not exist on the list because it has already
7391  * been released elsewhere (for example, the window
7392  * destruction process may release a DC while a rendering
7393  * thread may also want to release a DC when it notices that
7394  * its DC is obsolete for the current window).
7395  */
7396 DCItem *DCList::RemoveDC(HDC hDC, HWND hWnd)
7397 {
7398     listLock.Enter();
7399     DCItem **prevPtrPtr = &head;
7400     DCItem *listPtr = head;
7401     while (listPtr) {
7402         DCItem *nextPtr = listPtr->next;
7403         if (listPtr->hDC == hDC && listPtr->hWnd == hWnd) {
7404             *prevPtrPtr = nextPtr;
7405             break;
7406         }
7407         prevPtrPtr = &listPtr->next;
7408         listPtr = nextPtr;
7409     }
7410     listLock.Leave();
7411     return listPtr;
7412 }
7413 
7414 /**
7415  * Remove all DCs from the DC list which are associated with
7416  * the same window as hWnd.  Return the list of those
7417  * DC's to the caller (which will then probably want to
7418  * call ReleaseDC() for the returned DCs).
7419  */
7420 DCItem *DCList::RemoveAllDCs(HWND hWnd)
7421 {
7422     listLock.Enter();
7423     DCItem **prevPtrPtr = &head;
7424     DCItem *listPtr = head;
7425     DCItem *newListPtr = NULL;
7426     BOOL ret = FALSE;
7427     while (listPtr) {
7428         DCItem *nextPtr = listPtr->next;
7429         if (listPtr->hWnd == hWnd) {
7430             *prevPtrPtr = nextPtr;
7431             listPtr->next = newListPtr;
7432             newListPtr = listPtr;
7433         } else {
7434             prevPtrPtr = &listPtr->next;
7435         }
7436         listPtr = nextPtr;
7437     }
7438     listLock.Leave();
7439     return newListPtr;
7440 }
7441 
7442 
7443 /**
7444  * Realize palettes of all existing HDC objects
7445  */
7446 void DCList::RealizePalettes(int screen)
7447 {
7448     listLock.Enter();
7449     DCItem *listPtr = head;
7450     while (listPtr) {
7451         AwtWin32GraphicsDevice::RealizePalette(listPtr->hDC, screen);
7452         listPtr = listPtr->next;
7453     }
7454     listLock.Leave();
7455 }
7456 
7457 void MoveDCToPassiveList(HDC hDC, HWND hWnd) {
7458     DCItem *removedDC;
7459     if ((removedDC = activeDCList.RemoveDC(hDC, hWnd)) != NULL) {
7460         passiveDCList.AddDCItem(removedDC);
7461     }
7462 }
7463 
7464 void ReleaseDCList(HWND hwnd, DCList &list) {
7465     DCItem *removedDCs = list.RemoveAllDCs(hwnd);
7466     while (removedDCs) {
7467         DCItem *tmpDCList = removedDCs;
7468         DASSERT(::GetObjectType(tmpDCList->hDC) == OBJ_DC);
7469         int retValue = ::ReleaseDC(tmpDCList->hWnd, tmpDCList->hDC);
7470         VERIFY(retValue != 0);
7471         if (retValue != 0) {
7472             // Valid ReleaseDC call; need to decrement GDI object counter
7473             AwtGDIObject::Decrement();
7474         }
7475         removedDCs = removedDCs->next;
7476         delete tmpDCList;
7477     }
7478 }