1 /*
   2  * Copyright (c) 1996, 2016, 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_Toolkit.h"
  27 #include "awt_Frame.h"
  28 #include "awt_MenuBar.h"
  29 #include "awt_Dialog.h"
  30 #include "awt_IconCursor.h"
  31 #include "awt_Win32GraphicsDevice.h"
  32 #include "ComCtl32Util.h"
  33 
  34 #include <windowsx.h>
  35 
  36 #include <java_lang_Integer.h>
  37 #include <sun_awt_windows_WEmbeddedFrame.h>
  38 #include <sun_awt_windows_WEmbeddedFramePeer.h>
  39 
  40 
  41 /* IMPORTANT! Read the README.JNI file for notes on JNI converted AWT code.
  42  */
  43 
  44 /***********************************************************************/
  45 // Struct for _SetState() method
  46 struct SetStateStruct {
  47     jobject frame;
  48     jint state;
  49 };
  50 // Struct for _SetMaximizedBounds() method
  51 struct SetMaximizedBoundsStruct {
  52     jobject frame;
  53     jint x, y;
  54     jint width, height;
  55 };
  56 // Struct for _SetMenuBar() method
  57 struct SetMenuBarStruct {
  58     jobject frame;
  59     jobject menubar;
  60 };
  61 
  62 // Struct for _SetIMMOption() method
  63 struct SetIMMOptionStruct {
  64     jobject frame;
  65     jstring option;
  66 };
  67 // Struct for _SynthesizeWmActivate() method
  68 struct SynthesizeWmActivateStruct {
  69     jobject frame;
  70     jboolean doActivate;
  71 };
  72 // Struct for _NotifyModalBlocked() method
  73 struct NotifyModalBlockedStruct {
  74     jobject frame;
  75     jobject peer;
  76     jobject blockerPeer;
  77     jboolean blocked;
  78 };
  79 // Information about thread containing modal blocked embedded frames
  80 struct BlockedThreadStruct {
  81     int framesCount;
  82     HHOOK mouseHook;
  83     HHOOK modalHook;
  84 };
  85 
  86 
  87 // Communication with plugin control
  88 
  89 // The value must be the same as in AxControl.h
  90 #define WM_AX_REQUEST_FOCUS_TO_EMBEDDER (WM_USER + 197)
  91 
  92 static bool SetFocusToPluginControl(HWND hwndPlugin);
  93 
  94 /************************************************************************
  95  * AwtFrame fields
  96  */
  97 
  98 jfieldID AwtFrame::handleID;
  99 
 100 jfieldID AwtFrame::undecoratedID;
 101 jmethodID AwtFrame::getExtendedStateMID;
 102 jmethodID AwtFrame::setExtendedStateMID;
 103 
 104 jmethodID AwtFrame::activateEmbeddingTopLevelMID;
 105 jfieldID AwtFrame::isEmbeddedInIEID;
 106 
 107 Hashtable AwtFrame::sm_BlockedThreads("AWTBlockedThreads");
 108 
 109 /************************************************************************
 110  * AwtFrame methods
 111  */
 112 
 113 AwtFrame::AwtFrame() {
 114     m_parentWnd = NULL;
 115     menuBar = NULL;
 116     m_isEmbedded = FALSE;
 117     m_isEmbeddedInIE = FALSE;
 118     m_isLightweight = FALSE;
 119     m_ignoreWmSize = FALSE;
 120     m_isMenuDropped = FALSE;
 121     m_isInputMethodWindow = FALSE;
 122     m_isUndecorated = FALSE;
 123     m_imeTargetComponent = NULL;
 124     m_actualFocusedWindow = NULL;
 125     m_iconic = FALSE;
 126     m_zoomed = FALSE;
 127     m_maxBoundsSet = FALSE;
 128     m_forceResetZoomed = FALSE;
 129 
 130     isInManualMoveOrSize = FALSE;
 131     grabbedHitTest = 0;
 132 }
 133 
 134 AwtFrame::~AwtFrame()
 135 {
 136 }
 137 
 138 void AwtFrame::Dispose()
 139 {
 140     AwtWindow::Dispose();
 141 }
 142 
 143 LPCTSTR AwtFrame::GetClassName() {
 144     return AWT_FRAME_WINDOW_CLASS_NAME;
 145 }
 146 
 147 /*
 148  * Create a new AwtFrame object and window.
 149  */
 150 AwtFrame* AwtFrame::Create(jobject self, jobject parent)
 151 {
 152     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 153     if (env->EnsureLocalCapacity(1) < 0) {
 154         return NULL;
 155     }
 156 
 157     PDATA pData;
 158     HWND hwndParent = NULL;
 159     AwtFrame* frame = NULL;
 160     jclass cls = NULL;
 161     jclass inputMethodWindowCls = NULL;
 162     jobject target = NULL;
 163 
 164     try {
 165         target = env->GetObjectField(self, AwtObject::targetID);
 166         JNI_CHECK_NULL_GOTO(target, "target", done);
 167 
 168         if (parent != NULL) {
 169             JNI_CHECK_PEER_GOTO(parent, done);
 170             {
 171                 AwtFrame* parent = (AwtFrame *)pData;
 172                 HWND oHWnd = parent->GetOverriddenHWnd();
 173                 hwndParent = oHWnd ? oHWnd : parent->GetHWnd();
 174             }
 175         }
 176 
 177         frame = new AwtFrame();
 178 
 179         {
 180             /*
 181              * A variation on Netscape's hack for embedded frames: the client
 182              * area of the browser is a Java Frame for parenting purposes, but
 183              * really a Windows child window
 184              */
 185             BOOL isEmbeddedInstance = FALSE;
 186             BOOL isEmbedded = FALSE;
 187             cls = env->FindClass("sun/awt/EmbeddedFrame");
 188 
 189             if (cls) {
 190                 isEmbeddedInstance = env->IsInstanceOf(target, cls);
 191             } else {
 192                 throw std::bad_alloc();
 193             }
 194             INT_PTR handle;
 195             if (isEmbeddedInstance) {
 196                 handle = static_cast<INT_PTR>(env->GetLongField(target, AwtFrame::handleID));
 197                 if (handle != 0) {
 198                     isEmbedded = TRUE;
 199                 }
 200             }
 201             frame->m_isEmbedded = isEmbedded;
 202 
 203             BOOL isLightweight = FALSE;
 204             cls = env->FindClass("sun/awt/LightweightFrame");
 205             if (cls) {
 206                 isLightweight = env->IsInstanceOf(target, cls);
 207             } else {
 208                 throw std::bad_alloc();
 209             }
 210             frame->m_isLightweight = isLightweight;
 211 
 212             if (isEmbedded) {
 213                 hwndParent = (HWND)handle;
 214 
 215                 // JDK-8056915: Handle focus communication between plugin and frame
 216                 frame->m_isEmbeddedInIE = IsEmbeddedInIE(hwndParent);
 217                 if (frame->m_isEmbeddedInIE) {
 218                     env->SetBooleanField(target, isEmbeddedInIEID, JNI_TRUE);
 219                 }
 220 
 221                 RECT rect;
 222                 ::GetClientRect(hwndParent, &rect);
 223                 //Fix for 6328675: SWT_AWT.new_Frame doesn't occupy client area under JDK6
 224                 frame->m_isUndecorated = true;
 225                 /*
 226                  * Fix for BugTraq ID 4337754.
 227                  * Initialize m_peerObject before the first call
 228                  * to AwtFrame::GetClassName().
 229                  */
 230                 frame->m_peerObject = env->NewGlobalRef(self);
 231                 frame->RegisterClass();
 232                 DWORD exStyle = WS_EX_NOPARENTNOTIFY;
 233 
 234                 if (GetRTL()) {
 235                     exStyle |= WS_EX_RIGHT | WS_EX_LEFTSCROLLBAR;
 236                     if (GetRTLReadingOrder())
 237                         exStyle |= WS_EX_RTLREADING;
 238                 }
 239 
 240                 frame->m_hwnd = ::CreateWindowEx(exStyle,
 241                                                  frame->GetClassName(), TEXT(""),
 242                                                  WS_CHILD | WS_CLIPCHILDREN,
 243                                                  0, 0,
 244                                                  rect.right, rect.bottom,
 245                                                  hwndParent, NULL,
 246                                                  AwtToolkit::GetInstance().GetModuleHandle(),
 247                                                  NULL);
 248                 frame->LinkObjects(env, self);
 249                 frame->SubclassHWND();
 250 
 251                 // Update target's dimensions to reflect this embedded window.
 252                 ::GetClientRect(frame->m_hwnd, &rect);
 253                 ::MapWindowPoints(frame->m_hwnd, hwndParent, (LPPOINT)&rect, 2);
 254 
 255                 env->SetIntField(target, AwtComponent::xID, rect.left);
 256                 env->SetIntField(target, AwtComponent::yID, rect.top);
 257                 env->SetIntField(target, AwtComponent::widthID,
 258                                  rect.right-rect.left);
 259                 env->SetIntField(target, AwtComponent::heightID,
 260                                  rect.bottom-rect.top);
 261                 frame->InitPeerGraphicsConfig(env, self);
 262                 AwtToolkit::GetInstance().RegisterEmbedderProcessId(hwndParent);
 263             } else if (isLightweight) {
 264                 frame->m_isUndecorated = true;
 265                 frame->m_peerObject = env->NewGlobalRef(self);
 266                 frame->RegisterClass();
 267 
 268                 DWORD exStyle = 0;
 269                 DWORD style = WS_POPUP;
 270 
 271                 frame->CreateHWnd(env, L"",
 272                                   style,
 273                                   exStyle,
 274                                   0, 0, 0, 0,
 275                                   0,
 276                                   NULL,
 277                                   ::GetSysColor(COLOR_WINDOWTEXT),
 278                                   ::GetSysColor(COLOR_WINDOWFRAME),
 279                                   self);
 280             } else {
 281                 jint state = env->CallIntMethod(self, AwtFrame::getExtendedStateMID);
 282                 DWORD exStyle;
 283                 DWORD style;
 284 
 285                // for input method windows, use minimal decorations
 286                inputMethodWindowCls = env->FindClass("sun/awt/im/InputMethodWindow");
 287                if (inputMethodWindowCls == NULL) {
 288                    throw std::bad_alloc();
 289                }
 290 
 291                if (env->IsInstanceOf(target, inputMethodWindowCls)) {
 292                    //for below-the-spot composition window, use no decoration
 293                    if (env->GetBooleanField(target, AwtFrame::undecoratedID) == JNI_TRUE){
 294                         exStyle = 0;
 295                         style = WS_POPUP|WS_CLIPCHILDREN;
 296                         frame->m_isUndecorated = TRUE;
 297                    } else {
 298                         exStyle = WS_EX_PALETTEWINDOW;
 299                         style = WS_CLIPCHILDREN;
 300                    }
 301                    frame->m_isInputMethodWindow = TRUE;
 302                 } else if (env->GetBooleanField(target, AwtFrame::undecoratedID) == JNI_TRUE) {
 303                     exStyle = 0;
 304                     style = WS_POPUP | WS_SYSMENU | WS_CLIPCHILDREN |
 305                         WS_MAXIMIZEBOX | WS_MINIMIZEBOX;
 306                   if (state & java_awt_Frame_ICONIFIED) {
 307                       frame->setIconic(TRUE);
 308                   }
 309                     frame->m_isUndecorated = TRUE;
 310                 } else {
 311                     exStyle = WS_EX_WINDOWEDGE;
 312                     style = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN;
 313                   if (state & java_awt_Frame_ICONIFIED) {
 314                       frame->setIconic(TRUE);
 315                   }
 316                 }
 317 
 318                 if (GetRTL()) {
 319                     exStyle |= WS_EX_RIGHT | WS_EX_LEFTSCROLLBAR;
 320                     if (GetRTLReadingOrder())
 321                         exStyle |= WS_EX_RTLREADING;
 322                 }
 323 
 324                 jint x = env->GetIntField(target, AwtComponent::xID);
 325                 jint y = env->GetIntField(target, AwtComponent::yID);
 326                 jint width = env->GetIntField(target, AwtComponent::widthID);
 327                 jint height = env->GetIntField(target, AwtComponent::heightID);
 328 
 329                 frame->CreateHWnd(env, L"",
 330                                   style,
 331                                   exStyle,
 332                                   0, 0, 0, 0,
 333                                   hwndParent,
 334                                   NULL,
 335                                   ::GetSysColor(COLOR_WINDOWTEXT),
 336                                   ::GetSysColor(COLOR_WINDOWFRAME),
 337                                   self);
 338                 /*
 339                  * Reshape here instead of during create, so that a
 340                  * WM_NCCALCSIZE is sent.
 341                  */
 342                 frame->Reshape(x, y, width, height);
 343             }
 344         }
 345     } catch (...) {
 346         env->DeleteLocalRef(target);
 347         env->DeleteLocalRef(cls);
 348         env->DeleteLocalRef(inputMethodWindowCls);
 349         throw;
 350     }
 351 
 352 done:
 353     env->DeleteLocalRef(target);
 354     env->DeleteLocalRef(cls);
 355     env->DeleteLocalRef(inputMethodWindowCls);
 356 
 357     return frame;
 358 }
 359 
 360 /*
 361  * Returns true if the frame is embedded into Internet Explorer.
 362  * The function checks the class name of the parent window of the embedded frame.
 363  */
 364 BOOL AwtFrame::IsEmbeddedInIE(HWND hwndParent)
 365 {
 366     const char *pluginClass = "Java Plug-in Control Window";
 367     #define PARENT_CLASS_BUFFER_SIZE 64
 368     char parentClass[PARENT_CLASS_BUFFER_SIZE];
 369 
 370     return (::GetClassNameA(hwndParent, parentClass, PARENT_CLASS_BUFFER_SIZE) > 0)
 371            && (strncmp(parentClass, pluginClass, PARENT_CLASS_BUFFER_SIZE) == 0);
 372 }
 373 
 374 
 375 LRESULT AwtFrame::ProxyWindowProc(UINT message, WPARAM wParam, LPARAM lParam, MsgRouting &mr)
 376 {
 377     LRESULT retValue = 0L;
 378 
 379     AwtComponent *focusOwner = NULL;
 380     AwtComponent *imeTargetComponent = NULL;
 381 
 382     // IME and input language related messages need to be sent to a window
 383     // which has the Java input focus
 384     switch (message) {
 385         case WM_IME_STARTCOMPOSITION:
 386         case WM_IME_ENDCOMPOSITION:
 387         case WM_IME_COMPOSITION:
 388         case WM_IME_SETCONTEXT:
 389         case WM_IME_NOTIFY:
 390         case WM_IME_CONTROL:
 391         case WM_IME_COMPOSITIONFULL:
 392         case WM_IME_SELECT:
 393         case WM_IME_CHAR:
 394         case WM_IME_REQUEST:
 395         case WM_IME_KEYDOWN:
 396         case WM_IME_KEYUP:
 397         case WM_INPUTLANGCHANGEREQUEST:
 398         case WM_INPUTLANGCHANGE:
 399             if (message == WM_IME_STARTCOMPOSITION) {
 400                 SetImeTargetComponent(sm_focusOwner);
 401             }
 402             imeTargetComponent = AwtComponent::GetComponent(GetImeTargetComponent());
 403             if (imeTargetComponent != NULL &&
 404                 imeTargetComponent != this) // avoid recursive calls
 405             {
 406                 retValue = imeTargetComponent->WindowProc(message, wParam, lParam);
 407                 mr = mrConsume;
 408             }
 409             if (message == WM_IME_ENDCOMPOSITION) {
 410                 SetImeTargetComponent(NULL);
 411             }
 412             break;
 413         case WM_SETFOCUS:
 414             if (sm_inSynthesizeFocus) break; // pass it up the WindowProc chain
 415 
 416             if (!sm_suppressFocusAndActivation) {
 417                 if (IsLightweightFrame() || IsEmbeddedFrame()) {
 418                     AwtSetActiveWindow();
 419                 }
 420             }
 421             mr = mrConsume;
 422             break;
 423         case WM_KILLFOCUS:
 424             if (sm_inSynthesizeFocus) break; // pass it up the WindowProc chain
 425 
 426             if (!sm_suppressFocusAndActivation) {
 427                 if (IsLightweightFrame() || IsEmbeddedFrame()) {
 428                     HWND oppositeToplevelHWnd = AwtComponent::GetTopLevelParentForWindow((HWND)wParam);
 429                     if (oppositeToplevelHWnd != AwtComponent::GetFocusedWindow()) {
 430                         AwtWindow::SynthesizeWmActivate(FALSE, GetHWnd(), NULL);
 431                     }
 432                 }
 433             } else if (sm_restoreFocusAndActivation) {
 434                 if (AwtComponent::GetFocusedWindow() != NULL) {
 435                     AwtWindow *focusedWindow = (AwtWindow*)GetComponent(AwtComponent::GetFocusedWindow());
 436                     if (focusedWindow != NULL) {
 437                         // Will just silently restore native focus & activation.
 438                         focusedWindow->AwtSetActiveWindow();
 439                     }
 440                 }
 441             }
 442             mr = mrConsume;
 443             break;
 444         case 0x0127: // WM_CHANGEUISTATE
 445         case 0x0128: // WM_UPDATEUISTATE
 446             mr = mrConsume;
 447             break;
 448     }
 449 
 450     return retValue;
 451 }
 452 
 453 LRESULT AwtFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
 454 {
 455     MsgRouting mr = mrDoDefault;
 456     LRESULT retValue = 0L;
 457 
 458     retValue = ProxyWindowProc(message, wParam, lParam, mr);
 459 
 460     if (mr != mrConsume) {
 461         retValue = AwtWindow::WindowProc(message, wParam, lParam);
 462     }
 463     return retValue;
 464 }
 465 
 466 MsgRouting AwtFrame::WmShowWindow(BOOL show, UINT status)
 467 {
 468     /*
 469      * Fix for 6492970.
 470      * When a non-focusable toplevel is shown alone the Java process
 471      * is not foreground. If one shows another (focusable) toplevel
 472      * the native platform not always makes it foreground (see the CR).
 473      * Even worse, sometimes it sends the newly shown toplevel WM_ACTIVATE
 474      * message. This breaks Java focus. To workaround the problem we
 475      * set the toplevel being shown foreground programmatically.
 476      * The fix is localized to non-foreground process case only.
 477      * (See also: 6599270)
 478      */
 479     if (!IsEmbeddedFrame() && show == TRUE && status == 0) {
 480         HWND fgHWnd = ::GetForegroundWindow();
 481         if (fgHWnd != NULL) {
 482             DWORD fgProcessID;
 483             ::GetWindowThreadProcessId(fgHWnd, &fgProcessID);
 484 
 485             if (fgProcessID != ::GetCurrentProcessId()) {
 486                 AwtWindow* window = (AwtWindow*)GetComponent(GetHWnd());
 487 
 488                 if (window != NULL &&
 489                     window->IsFocusableWindow() &&
 490                     window->IsAutoRequestFocus() &&
 491                     !::IsWindowVisible(GetHWnd()) && // the window is really showing
 492                     !::IsWindow(GetModalBlocker(GetHWnd())))
 493                 {
 494                     // When the Java process is not allowed to set the foreground window
 495                     // (see MSDN) the request below will just have no effect.
 496                     ::SetForegroundWindow(GetHWnd());
 497                 }
 498             }
 499         }
 500     }
 501     return AwtWindow::WmShowWindow(show, status);
 502 }
 503 
 504 MsgRouting AwtFrame::WmMouseUp(UINT flags, int x, int y, int button) {
 505     if (isInManualMoveOrSize) {
 506         isInManualMoveOrSize = FALSE;
 507         ::ReleaseCapture();
 508         return mrConsume;
 509     }
 510     return AwtWindow::WmMouseUp(flags, x, y, button);
 511 }
 512 
 513 MsgRouting AwtFrame::WmMouseMove(UINT flags, int x, int y) {
 514     /**
 515      * If this Frame is non-focusable then we should implement move and size operation for it by
 516      * ourselfves because we don't dispatch appropriate mouse messages to default window procedure.
 517      */
 518     if (!IsFocusableWindow() && isInManualMoveOrSize) {
 519         DWORD curPos = ::GetMessagePos();
 520         x = GET_X_LPARAM(curPos);
 521         y = GET_Y_LPARAM(curPos);
 522         RECT r;
 523         ::GetWindowRect(GetHWnd(), &r);
 524         POINT mouseLoc = {x, y};
 525         mouseLoc.x -= savedMousePos.x;
 526         mouseLoc.y -= savedMousePos.y;
 527         savedMousePos.x = x;
 528         savedMousePos.y = y;
 529         if (grabbedHitTest == HTCAPTION) {
 530             ::SetWindowPos(GetHWnd(), NULL, r.left+mouseLoc.x, r.top+mouseLoc.y,
 531                            r.right-r.left, r.bottom-r.top,
 532                            SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
 533         } else {
 534             switch (grabbedHitTest) {
 535             case HTTOP:
 536                 r.top += mouseLoc.y;
 537                 break;
 538             case HTBOTTOM:
 539                 r.bottom += mouseLoc.y;
 540                 break;
 541             case HTRIGHT:
 542                 r.right += mouseLoc.x;
 543                 break;
 544             case HTLEFT:
 545                 r.left += mouseLoc.x;
 546                 break;
 547             case HTTOPLEFT:
 548                 r.left += mouseLoc.x;
 549                 r.top += mouseLoc.y;
 550                 break;
 551             case HTTOPRIGHT:
 552                 r.top += mouseLoc.y;
 553                 r.right += mouseLoc.x;
 554                 break;
 555             case HTBOTTOMLEFT:
 556                 r.left += mouseLoc.x;
 557                 r.bottom += mouseLoc.y;
 558                 break;
 559             case HTBOTTOMRIGHT:
 560             case HTSIZE:
 561                 r.right += mouseLoc.x;
 562                 r.bottom += mouseLoc.y;
 563                 break;
 564             }
 565 
 566             ::SetWindowPos(GetHWnd(), NULL, r.left, r.top,
 567                            r.right-r.left, r.bottom-r.top,
 568                            SWP_NOACTIVATE | SWP_NOZORDER |
 569                            SWP_NOCOPYBITS | SWP_DEFERERASE);
 570         }
 571         return mrConsume;
 572     } else {
 573         return AwtWindow::WmMouseMove(flags, x, y);
 574     }
 575 }
 576 
 577 MsgRouting AwtFrame::WmNcMouseUp(WPARAM hitTest, int x, int y, int button) {
 578     if (!IsFocusableWindow() && (button & LEFT_BUTTON)) {
 579         /*
 580          * Fix for 6399659.
 581          * The native system shouldn't activate the next window in z-order
 582          * when minimizing non-focusable window.
 583          */
 584         if (hitTest == HTMINBUTTON) {
 585             ::ShowWindow(GetHWnd(), SW_SHOWMINNOACTIVE);
 586             return mrConsume;
 587         }
 588         /**
 589          * If this Frame is non-focusable then we should implement move and size operation for it by
 590          * ourselfves because we don't dispatch appropriate mouse messages to default window procedure.
 591          */
 592         if ((button & DBL_CLICK) && hitTest == HTCAPTION) {
 593             // Double click on caption - maximize or restore Frame.
 594             if (IsResizable()) {
 595                 if (::IsZoomed(GetHWnd())) {
 596                     ::ShowWindow(GetHWnd(), SW_SHOWNOACTIVATE);
 597                 } else {
 598                     ::ShowWindow(GetHWnd(), SW_MAXIMIZE);
 599                 }
 600             }
 601             return mrConsume;
 602         }
 603         switch (hitTest) {
 604         case HTMAXBUTTON:
 605             if (IsResizable()) {
 606                 if (::IsZoomed(GetHWnd())) {
 607                     ::ShowWindow(GetHWnd(), SW_SHOWNOACTIVATE);
 608                 } else {
 609                     ::ShowWindow(GetHWnd(), SW_MAXIMIZE);
 610                 }
 611             }
 612             return mrConsume;
 613         default:
 614             return mrDoDefault;
 615         }
 616     }
 617     return AwtWindow::WmNcMouseUp(hitTest, x, y, button);
 618 }
 619 
 620 MsgRouting AwtFrame::WmNcMouseDown(WPARAM hitTest, int x, int y, int button) {
 621     // By Swing request, click on the Frame's decorations (even on
 622     // grabbed Frame) should generate UngrabEvent
 623     if (m_grabbedWindow != NULL/* && !m_grabbedWindow->IsOneOfOwnersOf(this)*/) {
 624         m_grabbedWindow->Ungrab();
 625     }
 626     if (!IsFocusableWindow() && (button & LEFT_BUTTON)) {
 627         switch (hitTest) {
 628         case HTTOP:
 629         case HTBOTTOM:
 630         case HTLEFT:
 631         case HTRIGHT:
 632         case HTTOPLEFT:
 633         case HTTOPRIGHT:
 634         case HTBOTTOMLEFT:
 635         case HTBOTTOMRIGHT:
 636         case HTSIZE:
 637             // Zoomed or non-resizable unfocusable frames should not be resizable.
 638             if (isZoomed() || !IsResizable()) {
 639                 return mrConsume;
 640             }
 641         case HTCAPTION:
 642             // We are going to perform default mouse action on non-client area of this window
 643             // Grab mouse for this purpose and store coordinates for motion vector calculation
 644             savedMousePos.x = x;
 645             savedMousePos.y = y;
 646             ::SetCapture(GetHWnd());
 647             isInManualMoveOrSize = TRUE;
 648             grabbedHitTest = hitTest;
 649             return mrConsume;
 650         default:
 651             return mrDoDefault;
 652         }
 653     }
 654     return AwtWindow::WmNcMouseDown(hitTest, x, y, button);
 655 }
 656 
 657 // Override AwtWindow::Reshape() to handle minimized/maximized
 658 // frames (see 6525850, 4065534)
 659 void AwtFrame::Reshape(int x, int y, int width, int height)
 660 {
 661     if (isIconic()) {
 662     // normal AwtComponent::Reshape will not work for iconified windows so...
 663         WINDOWPLACEMENT wp;
 664         POINT       ptMinPosition = {x,y};
 665         POINT       ptMaxPosition = {0,0};
 666         RECT        rcNormalPosition = {x,y,x+width,y+height};
 667         RECT        rcWorkspace;
 668         HWND        hWndDesktop = GetDesktopWindow();
 669         HWND        hWndSelf = GetHWnd();
 670 
 671         // SetWindowPlacement takes workspace coordinates, but
 672         // if taskbar is at top of screen, workspace coords !=
 673         // screen coords, so offset by workspace origin
 674         VERIFY(::SystemParametersInfo(SPI_GETWORKAREA, 0, (PVOID)&rcWorkspace, 0));
 675         ::OffsetRect(&rcNormalPosition, -rcWorkspace.left, -rcWorkspace.top);
 676 
 677         // set the window size for when it is not-iconified
 678         wp.length = sizeof(wp);
 679         wp.flags = WPF_SETMINPOSITION;
 680         wp.showCmd = IsVisible() ? SW_SHOWMINIMIZED : SW_HIDE;
 681         wp.ptMinPosition = ptMinPosition;
 682         wp.ptMaxPosition = ptMaxPosition;
 683         wp.rcNormalPosition = rcNormalPosition;
 684 
 685         // If the call is not guarded with ignoreWmSize,
 686         // a regression for bug 4851435 appears.
 687         // Having this call guarded also prevents
 688         // changing the iconified state of the frame
 689         // while calling the Frame.setBounds() method.
 690         m_ignoreWmSize = TRUE;
 691         ::SetWindowPlacement(hWndSelf, &wp);
 692         m_ignoreWmSize = FALSE;
 693 
 694         return;
 695     }
 696 
 697     if (isZoomed()) {
 698     // setting size of maximized window, we remove the
 699     // maximized state bit (matches Motif behaviour)
 700     // (calling ShowWindow(SW_RESTORE) would fire an
 701     //  activation event which we don't want)
 702         LONG    style = GetStyle();
 703         DASSERT(style & WS_MAXIMIZE);
 704         style ^= WS_MAXIMIZE;
 705         SetStyle(style);
 706     }
 707 
 708     AwtWindow::Reshape(x, y, width, height);
 709 }
 710 
 711 
 712 /* Show the frame in it's current state */
 713 void
 714 AwtFrame::Show()
 715 {
 716     m_visible = true;
 717     HWND hwnd = GetHWnd();
 718     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 719 
 720     if (IsLightweightFrame()) {
 721         return;
 722     }
 723 
 724     DTRACE_PRINTLN3("AwtFrame::Show:%s%s%s",
 725                   m_iconic ? " iconic" : "",
 726                   m_zoomed ? " zoomed" : "",
 727                   m_iconic || m_zoomed ? "" : " normal");
 728 
 729     BOOL locationByPlatform = env->GetBooleanField(GetTarget(env), AwtWindow::locationByPlatformID);
 730 
 731     if (locationByPlatform) {
 732          moveToDefaultLocation();
 733     }
 734     EnableTranslucency(TRUE);
 735 
 736     BOOL autoRequestFocus = IsAutoRequestFocus();
 737 
 738     if (m_iconic) {
 739         if (m_zoomed) {
 740             // This whole function could probably be rewritten to use
 741             // ::SetWindowPlacement but MS docs doesn't tell if
 742             // ::SetWindowPlacement is a proper superset of
 743             // ::ShowWindow.  So let's be conservative and only use it
 744             // here, where we really do need it.
 745             DTRACE_PRINTLN("AwtFrame::Show(SW_SHOWMINIMIZED, WPF_RESTORETOMAXIMIZED");
 746             WINDOWPLACEMENT wp;
 747             ::ZeroMemory(&wp, sizeof(WINDOWPLACEMENT));
 748             wp.length = sizeof(WINDOWPLACEMENT);
 749             ::GetWindowPlacement(hwnd, &wp);
 750             if (!IsFocusableWindow() || !autoRequestFocus) {
 751                 wp.showCmd = SW_SHOWMINNOACTIVE;
 752             } else {
 753                 wp.showCmd = SW_SHOWMINIMIZED;
 754             }
 755             wp.flags |= WPF_RESTORETOMAXIMIZED;
 756             ::SetWindowPlacement(hwnd, &wp);
 757         }
 758         else {
 759             DTRACE_PRINTLN("AwtFrame::Show(SW_SHOWMINIMIZED)");
 760             if (!IsFocusableWindow() || !autoRequestFocus) {
 761                 ::ShowWindow(hwnd, SW_SHOWMINNOACTIVE);
 762             } else {
 763                 ::ShowWindow(hwnd, SW_SHOWMINIMIZED);
 764             }
 765         }
 766     }
 767     else if (m_zoomed) {
 768         DTRACE_PRINTLN("AwtFrame::Show(SW_SHOWMAXIMIZED)");
 769         if (!autoRequestFocus) {
 770 
 771             m_filterFocusAndActivation = TRUE;
 772             ::ShowWindow(hwnd, SW_MAXIMIZE);
 773             m_filterFocusAndActivation = FALSE;
 774 
 775         } else if (!IsFocusableWindow()) {
 776             ::ShowWindow(hwnd, SW_MAXIMIZE);
 777         } else {
 778             ::ShowWindow(hwnd, SW_SHOWMAXIMIZED);
 779         }
 780     }
 781     else if (m_isInputMethodWindow) {
 782         // Don't activate input methow window
 783         DTRACE_PRINTLN("AwtFrame::Show(SW_SHOWNA)");
 784         ::ShowWindow(hwnd, SW_SHOWNA);
 785 
 786         // After the input method window shown, we have to adjust the
 787         // IME candidate window position. Here is why.
 788         // Usually, when IMM opens the candidate window, it sends WM_IME_NOTIFY w/
 789         // IMN_OPENCANDIDATE message to the awt component window. The
 790         // awt component makes a Java call to acquire the text position
 791         // in order to show the candidate window just below the input method window.
 792         // However, by the time it acquires the position, the input method window
 793         // hasn't been displayed yet, the position returned is just below
 794         // the composed text and when the input method window is shown, it
 795         // will hide part of the candidate list. To fix this, we have to
 796         // adjust the candidate window position after the input method window
 797         // is shown. See bug 5012944.
 798         AdjustCandidateWindowPos();
 799     }
 800     else {
 801         // Nor iconic, nor zoomed (handled above) - so use SW_RESTORE
 802         // to show in "normal" state regardless of whatever stale
 803         // state might the invisible window still has.
 804         DTRACE_PRINTLN("AwtFrame::Show(SW_RESTORE)");
 805         if (!IsFocusableWindow() || !autoRequestFocus) {
 806             ::ShowWindow(hwnd, SW_SHOWNOACTIVATE);
 807         } else {
 808             ::ShowWindow(hwnd, SW_RESTORE);
 809         }
 810     }
 811 }
 812 
 813 void
 814 AwtFrame::SendWindowStateEvent(int oldState, int newState)
 815 {
 816     SendWindowEvent(java_awt_event_WindowEvent_WINDOW_STATE_CHANGED,
 817                     NULL, oldState, newState);
 818 }
 819 
 820 void
 821 AwtFrame::ClearMaximizedBounds()
 822 {
 823     m_maxBoundsSet = FALSE;
 824 }
 825 
 826 void AwtFrame::AdjustCandidateWindowPos()
 827 {
 828     // This method should only be called if the current frame
 829     // is the input method window frame.
 830     if (!m_isInputMethodWindow) {
 831         return;
 832     }
 833 
 834     RECT inputWinRec, focusWinRec;
 835     AwtComponent *comp = AwtComponent::GetComponent(AwtComponent::sm_focusOwner);
 836     if (comp == NULL) {
 837         return;
 838     }
 839 
 840     ::GetWindowRect(GetHWnd(), &inputWinRec);
 841     ::GetWindowRect(sm_focusOwner, &focusWinRec);
 842 
 843     LPARAM candType = comp->GetCandidateType();
 844     HWND defaultIMEWnd = ::ImmGetDefaultIMEWnd(GetHWnd());
 845     if (defaultIMEWnd == NULL) {
 846         return;
 847     }
 848     UINT bits = 1;
 849     // adjusts the candidate window position
 850     for (int iCandType = 0; iCandType < 32; iCandType++, bits<<=1) {
 851         if (candType & bits) {
 852             CANDIDATEFORM cf;
 853             cf.dwIndex = iCandType;
 854             cf.dwStyle = CFS_CANDIDATEPOS;
 855             // Since the coordinates are relative to the containing window,
 856             // we have to calculate the coordinates as below.
 857             cf.ptCurrentPos.x = inputWinRec.left - focusWinRec.left;
 858             cf.ptCurrentPos.y = inputWinRec.bottom - focusWinRec.top;
 859 
 860             // sends IMC_SETCANDIDATEPOS to IMM to move the candidate window.
 861             ::SendMessage(defaultIMEWnd, WM_IME_CONTROL, IMC_SETCANDIDATEPOS, (LPARAM)&cf);
 862         }
 863     }
 864 }
 865 
 866 void
 867 AwtFrame::SetMaximizedBounds(int x, int y, int w, int h)
 868 {
 869     m_maxPos.x  = x;
 870     m_maxPos.y  = y;
 871     m_maxSize.x = w;
 872     m_maxSize.y = h;
 873     m_maxBoundsSet = TRUE;
 874 }
 875 
 876 MsgRouting AwtFrame::WmGetMinMaxInfo(LPMINMAXINFO lpmmi)
 877 {
 878     //Firstly call AwtWindow's function
 879     MsgRouting r = AwtWindow::WmGetMinMaxInfo(lpmmi);
 880 
 881     //Then replace maxPos & maxSize if necessary
 882     if (!m_maxBoundsSet) {
 883         return r;
 884     }
 885 
 886     if (m_maxPos.x != java_lang_Integer_MAX_VALUE)
 887         lpmmi->ptMaxPosition.x = m_maxPos.x;
 888     if (m_maxPos.y != java_lang_Integer_MAX_VALUE)
 889         lpmmi->ptMaxPosition.y = m_maxPos.y;
 890     if (m_maxSize.x != java_lang_Integer_MAX_VALUE)
 891         lpmmi->ptMaxSize.x = m_maxSize.x;
 892     if (m_maxSize.y != java_lang_Integer_MAX_VALUE)
 893         lpmmi->ptMaxSize.y = m_maxSize.y;
 894     return mrConsume;
 895 }
 896 
 897 MsgRouting AwtFrame::WmSize(UINT type, int w, int h)
 898 {
 899     currentWmSizeState = type;
 900     if (currentWmSizeState == SIZE_MINIMIZED) {
 901         UpdateSecurityWarningVisibility();
 902     }
 903 
 904     if (m_ignoreWmSize) {
 905         return mrDoDefault;
 906     }
 907 
 908     DTRACE_PRINTLN6("AwtFrame::WmSize: %dx%d,%s visible, state%s%s%s",
 909                   w, h,
 910                   ::IsWindowVisible(GetHWnd()) ? "" : " not",
 911                   m_iconic ? " iconic" : "",
 912                   m_zoomed ? " zoomed" : "",
 913                   m_iconic || m_zoomed ? "" : " normal");
 914 
 915     BOOL iconify = type == SIZE_MINIMIZED;
 916 
 917     // Note that zoom may be set to TRUE in several cases:
 918     //    1. type == SIZE_MAXIMIZED means that either the user or
 919     //       the developer (via setExtendedState(MAXIMIZED_BOTH)
 920     //       maximizes the frame.
 921     //    2. type == SIZE_MINIMIZED && isZoomed() means that a maximized
 922     //       frame is to be minimized. If the user minimizes a maximized
 923     //       frame, we need to keep the zoomed property TRUE. However,
 924     //       if the developer calls setExtendedState(ICONIFIED), i.e.
 925     //       w/o combining the ICONIFIED state with the MAXIMIZED state,
 926     //       we MUST RESET the zoomed property.
 927     //       The flag m_forceResetZoomed identifies the latter case.
 928     BOOL zoom =
 929         (
 930          type == SIZE_MAXIMIZED
 931          ||
 932          (type == SIZE_MINIMIZED && isZoomed())
 933         )
 934         && !m_forceResetZoomed;
 935 
 936     // Set the new state and send appropriate Java event
 937     jint oldState = java_awt_Frame_NORMAL;
 938     if (isIconic()) {
 939         oldState |= java_awt_Frame_ICONIFIED;
 940     }
 941     if (isZoomed()) {
 942         oldState |= java_awt_Frame_MAXIMIZED_BOTH;
 943     }
 944 
 945     jint newState = java_awt_Frame_NORMAL;
 946     if (iconify) {
 947         newState |= java_awt_Frame_ICONIFIED;
 948     }
 949     if (zoom) {
 950         newState |= java_awt_Frame_MAXIMIZED_BOTH;
 951     }
 952 
 953     setIconic(iconify);
 954     setZoomed(zoom);
 955 
 956     jint changed = oldState ^ newState;
 957     if (changed != 0) {
 958         DTRACE_PRINTLN2("AwtFrame::WmSize: reporting state change %x -> %x",
 959                 oldState, newState);
 960 
 961         // sync target with peer
 962         JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 963         env->CallVoidMethod(GetPeer(env), AwtFrame::setExtendedStateMID, newState);
 964 
 965         // report (de)iconification to old clients
 966         if (changed & java_awt_Frame_ICONIFIED) {
 967             if (newState & java_awt_Frame_ICONIFIED) {
 968                 SendWindowEvent(java_awt_event_WindowEvent_WINDOW_ICONIFIED);
 969             } else {
 970                 SendWindowEvent(java_awt_event_WindowEvent_WINDOW_DEICONIFIED);
 971             }
 972         }
 973 
 974         // New (since 1.4) state change event
 975         SendWindowStateEvent(oldState, newState);
 976     }
 977 
 978     // If window is in iconic state, do not send COMPONENT_RESIZED event
 979     if (isIconic()) {
 980         return mrDoDefault;
 981     }
 982 
 983     return AwtWindow::WmSize(type, w, h);
 984 }
 985 
 986 MsgRouting AwtFrame::WmActivate(UINT nState, BOOL fMinimized, HWND opposite)
 987 {
 988     jint type;
 989 
 990     if (nState != WA_INACTIVE) {
 991         if (::IsWindow(AwtWindow::GetModalBlocker(GetHWnd())) ||
 992             CheckActivateActualFocusedWindow(opposite))
 993         {
 994             return mrConsume;
 995         }
 996         type = java_awt_event_WindowEvent_WINDOW_GAINED_FOCUS;
 997         AwtComponent::SetFocusedWindow(GetHWnd());
 998 
 999     } else {
1000         if (::IsWindow(AwtWindow::GetModalBlocker(opposite))) {
1001             return mrConsume;
1002         } else {
1003             // If deactivation happens because of press on grabbing
1004             // window - this is nonsense, since grabbing window is
1005             // assumed to have focus and watch for deactivation.  But
1006             // this can happen - if grabbing window is proxied Window,
1007             // with Frame keeping real focus for it.
1008             if (m_grabbedWindow != NULL) {
1009                 if (m_grabbedWindow->GetHWnd() == opposite) {
1010                     // Do nothing
1011                 } else {
1012                     // Normally, we would rather check that this ==
1013                     // grabbed window, and focus is leaving it -
1014                     // ungrab.  But since we know about proxied
1015                     // windows, we simply assume this is one of the
1016                     // known cases.
1017                     if (!m_grabbedWindow->IsOneOfOwnersOf((AwtWindow*)AwtComponent::GetComponent(opposite))) {
1018                         m_grabbedWindow->Ungrab();
1019                     }
1020                 }
1021             }
1022             CheckRetainActualFocusedWindow(opposite);
1023 
1024             type = java_awt_event_WindowEvent_WINDOW_LOST_FOCUS;
1025             AwtComponent::SetFocusedWindow(NULL);
1026             sm_focusOwner = NULL;
1027         }
1028     }
1029 
1030     SendWindowEvent(type, opposite);
1031     return mrConsume;
1032 }
1033 
1034 BOOL AwtFrame::CheckActivateActualFocusedWindow(HWND deactivatedOpositeHWnd)
1035 {
1036     if (m_actualFocusedWindow != NULL) {
1037         HWND hwnd = m_actualFocusedWindow->GetHWnd();
1038         if (hwnd != NULL && ::IsWindowVisible(hwnd)) {
1039             SynthesizeWmActivate(TRUE, hwnd, deactivatedOpositeHWnd);
1040             return TRUE;
1041         }
1042         m_actualFocusedWindow = NULL;
1043     }
1044     return FALSE;
1045 }
1046 
1047 void AwtFrame::CheckRetainActualFocusedWindow(HWND activatedOpositeHWnd)
1048 {
1049     // If actual focused window is not this Frame
1050     if (AwtComponent::GetFocusedWindow() != GetHWnd()) {
1051         // Make sure the actual focused window is an owned window of this frame
1052         AwtWindow *focusedWindow = (AwtWindow *)AwtComponent::GetComponent(AwtComponent::GetFocusedWindow());
1053         if (focusedWindow != NULL && focusedWindow->GetOwningFrameOrDialog() == this) {
1054 
1055             // Check that the opposite window is not this frame, nor an owned window of this frame
1056             if (activatedOpositeHWnd != NULL) {
1057                 AwtWindow *oppositeWindow = (AwtWindow *)AwtComponent::GetComponent(activatedOpositeHWnd);
1058                 if (oppositeWindow && oppositeWindow != this &&
1059                     oppositeWindow->GetOwningFrameOrDialog() != this)
1060                 {
1061                     m_actualFocusedWindow = focusedWindow;
1062                 }
1063             } else {
1064                  m_actualFocusedWindow = focusedWindow;
1065             }
1066         }
1067     }
1068 }
1069 
1070 BOOL AwtFrame::AwtSetActiveWindow(BOOL isMouseEventCause, UINT hittest)
1071 {
1072     if (hittest == HTCLIENT) {
1073         // Don't let the actualFocusedWindow to steal focus if:
1074         // a) the frame is clicked in its client area;
1075         // b) focus is requested to some of the frame's child.
1076         m_actualFocusedWindow = NULL;
1077     }
1078     if (IsLightweightFrame()) {
1079         return TRUE;
1080     }
1081     if (isMouseEventCause && IsEmbeddedFrame() && m_isEmbeddedInIE) {
1082         HWND hwndProxy = GetProxyFocusOwner();
1083         // Do nothing if this frame is focused already
1084         if (::GetFocus() != hwndProxy) {
1085             // Fix for JDK-8056915:
1086             // If window activated with mouse, set focus to plugin control window
1087             // first to preserve focus owner inside browser window
1088             if (SetFocusToPluginControl(::GetParent(GetHWnd()))) {
1089                 return TRUE;
1090             }
1091             // Plugin control window is already focused, so do normal processing
1092         }
1093     }
1094     return AwtWindow::AwtSetActiveWindow(isMouseEventCause);
1095 }
1096 
1097 MsgRouting AwtFrame::WmEnterMenuLoop(BOOL isTrackPopupMenu)
1098 {
1099     if ( !isTrackPopupMenu ) {
1100         m_isMenuDropped = TRUE;
1101     }
1102     return mrDoDefault;
1103 }
1104 
1105 MsgRouting AwtFrame::WmExitMenuLoop(BOOL isTrackPopupMenu)
1106 {
1107     if ( !isTrackPopupMenu ) {
1108         m_isMenuDropped = FALSE;
1109     }
1110     return mrDoDefault;
1111 }
1112 
1113 AwtMenuBar* AwtFrame::GetMenuBar()
1114 {
1115     return menuBar;
1116 }
1117 
1118 void AwtFrame::SetMenuBar(AwtMenuBar* mb)
1119 {
1120     if (menuBar) {
1121         menuBar->SetFrame(NULL);
1122     }
1123     menuBar = mb;
1124     if (mb == NULL) {
1125         // Remove existing menu bar, if any.
1126         ::SetMenu(GetHWnd(), NULL);
1127     } else {
1128         AwtFrame* oldFrame = menuBar->GetFrame();
1129         if (oldFrame && oldFrame != this) {
1130             oldFrame->SetMenuBar(NULL);
1131         }
1132         menuBar->SetFrame(this);
1133         if (menuBar->GetHMenu() != NULL) {
1134             ::SetMenu(GetHWnd(), menuBar->GetHMenu());
1135         }
1136     }
1137 }
1138 
1139 MsgRouting AwtFrame::WmDrawItem(UINT ctrlId, DRAWITEMSTRUCT& drawInfo)
1140 {
1141     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1142 
1143     // if the item to be redrawn is the menu bar, then do it
1144     AwtMenuBar* awtMenubar = GetMenuBar();
1145     if (drawInfo.CtlType == ODT_MENU && (awtMenubar != NULL) &&
1146         (::GetMenu( GetHWnd() ) == (HMENU)drawInfo.hwndItem) )
1147         {
1148                 awtMenubar->DrawItem(drawInfo);
1149                 return mrConsume;
1150     }
1151 
1152         return AwtComponent::WmDrawItem(ctrlId, drawInfo);
1153 }
1154 
1155 MsgRouting AwtFrame::WmMeasureItem(UINT ctrlId, MEASUREITEMSTRUCT& measureInfo)
1156 {
1157         JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1158         AwtMenuBar* awtMenubar = GetMenuBar();
1159         if ((measureInfo.CtlType == ODT_MENU) && (awtMenubar != NULL))
1160         {
1161                 // AwtMenu instance is stored in itemData. Use it to check if this
1162                 // menu is the menu bar.
1163                 AwtMenu * pMenu = (AwtMenu *) measureInfo.itemData;
1164                 DASSERT(pMenu != NULL);
1165                 if ( pMenu == awtMenubar )
1166                 {
1167                         HWND hWnd = GetHWnd();
1168                         HDC hDC = ::GetDC(hWnd);
1169                         DASSERT(hDC != NULL);
1170                         awtMenubar->MeasureItem(hDC, measureInfo);
1171                         VERIFY(::ReleaseDC(hWnd, hDC));
1172                         return mrConsume;
1173                 }
1174         }
1175 
1176         return AwtComponent::WmMeasureItem(ctrlId, measureInfo);
1177 }
1178 
1179 MsgRouting AwtFrame::WmGetIcon(WPARAM iconType, LRESULT& retVal)
1180 {
1181     //Workaround windows bug:
1182     //when reseting from specific icon to class icon
1183     //taskbar is not updated
1184     if (iconType <= 2 /*ICON_SMALL2*/) {
1185         retVal = (LRESULT)GetEffectiveIcon(iconType);
1186         return mrConsume;
1187     } else {
1188         return mrDoDefault;
1189     }
1190 }
1191 
1192 void AwtFrame::DoUpdateIcon()
1193 {
1194     //Workaround windows bug:
1195     //when reseting from specific icon to class icon
1196     //taskbar is not updated
1197     HICON hIcon = GetEffectiveIcon(ICON_BIG);
1198     HICON hIconSm = GetEffectiveIcon(ICON_SMALL);
1199     SendMessage(WM_SETICON, ICON_BIG,   (LPARAM)hIcon);
1200     SendMessage(WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
1201 }
1202 
1203 HICON AwtFrame::GetEffectiveIcon(int iconType)
1204 {
1205     BOOL smallIcon = ((iconType == ICON_SMALL) || (iconType == 2/*ICON_SMALL2*/));
1206     HICON hIcon = (smallIcon) ? GetHIconSm() : GetHIcon();
1207     if (hIcon == NULL) {
1208         hIcon = (smallIcon) ? AwtToolkit::GetInstance().GetAwtIconSm() :
1209             AwtToolkit::GetInstance().GetAwtIcon();
1210     }
1211     return hIcon;
1212 }
1213 
1214 static BOOL keepOnMinimize(jobject peer) {
1215     static BOOL checked = FALSE;
1216     static BOOL keep = FALSE;
1217     if (!checked) {
1218         keep = (JNU_GetStaticFieldByName(AwtToolkit::GetEnv(), NULL,
1219             "sun/awt/windows/WFramePeer", "keepOnMinimize", "Z").z) == JNI_TRUE;
1220         checked = TRUE;
1221     }
1222     return keep;
1223 }
1224 
1225 MsgRouting AwtFrame::WmSysCommand(UINT uCmdType, int xPos, int yPos)
1226 {
1227     // ignore any WM_SYSCOMMAND if this window is blocked by modal dialog
1228     if (::IsWindow(AwtWindow::GetModalBlocker(GetHWnd()))) {
1229         return mrConsume;
1230     }
1231 
1232     if (uCmdType == (SYSCOMMAND_IMM & 0xFFF0)){
1233         JNIEnv* env = AwtToolkit::GetEnv();
1234         JNU_CallMethodByName(env, NULL, m_peerObject,
1235             "notifyIMMOptionChange", "()V");
1236         DASSERT(!safe_ExceptionOccurred(env));
1237         return mrConsume;
1238     }
1239     if ((uCmdType == SC_MINIMIZE) && keepOnMinimize(m_peerObject)) {
1240         ::ShowWindow(GetHWnd(),SW_SHOWMINIMIZED);
1241         return mrConsume;
1242     }
1243     return AwtWindow::WmSysCommand(uCmdType, xPos, yPos);
1244 }
1245 
1246 LRESULT AwtFrame::WinThreadExecProc(ExecuteArgs * args)
1247 {
1248     switch( args->cmdId ) {
1249         case FRAME_SETMENUBAR:
1250         {
1251             jobject  mbPeer = (jobject)args->param1;
1252 
1253             // cancel any currently dropped down menus
1254             if (m_isMenuDropped) {
1255                 SendMessage(WM_CANCELMODE);
1256             }
1257 
1258             if (mbPeer == NULL) {
1259                 // Remove existing menu bar, if any
1260                 SetMenuBar(NULL);
1261             } else {
1262                 JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1263                 AwtMenuBar* menuBar = (AwtMenuBar *)JNI_GET_PDATA(mbPeer);
1264                 SetMenuBar(menuBar);
1265             }
1266             DrawMenuBar();
1267             break;
1268         }
1269 
1270         default:
1271             AwtWindow::WinThreadExecProc(args);
1272             break;
1273     }
1274 
1275     return 0L;
1276 }
1277 
1278 void AwtFrame::_SynthesizeWmActivate(void *param)
1279 {
1280     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1281 
1282     SynthesizeWmActivateStruct *sas = (SynthesizeWmActivateStruct *)param;
1283     jobject self = sas->frame;
1284     jboolean doActivate = sas->doActivate;
1285 
1286     AwtFrame *frame = NULL;
1287 
1288     PDATA pData;
1289     JNI_CHECK_PEER_GOTO(self, ret);
1290     frame = (AwtFrame *)pData;
1291 
1292     SynthesizeWmActivate(doActivate, frame->GetHWnd(), NULL);
1293 ret:
1294     env->DeleteGlobalRef(self);
1295 
1296     delete sas;
1297 }
1298 
1299 jobject AwtFrame::_GetBoundsPrivate(void *param)
1300 {
1301     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1302 
1303     jobject self = (jobject)param;
1304 
1305     jobject result = NULL;
1306     AwtFrame *f = NULL;
1307 
1308     PDATA pData;
1309     JNI_CHECK_PEER_GOTO(self, ret);
1310     f = (AwtFrame *)pData;
1311     if (::IsWindow(f->GetHWnd()))
1312     {
1313         RECT rect;
1314         ::GetWindowRect(f->GetHWnd(), &rect);
1315         HWND parent = ::GetParent(f->GetHWnd());
1316         if (::IsWindow(parent))
1317         {
1318             POINT zero;
1319             zero.x = 0;
1320             zero.y = 0;
1321             ::ClientToScreen(parent, &zero);
1322             ::OffsetRect(&rect, -zero.x, -zero.y);
1323         }
1324 
1325         result = JNU_NewObjectByName(env, "java/awt/Rectangle", "(IIII)V",
1326             rect.left, rect.top, rect.bottom-rect.top, rect.right-rect.left);
1327     }
1328 ret:
1329     env->DeleteGlobalRef(self);
1330 
1331     if (result != NULL)
1332     {
1333         jobject resultGlobalRef = env->NewGlobalRef(result);
1334         env->DeleteLocalRef(result);
1335         return resultGlobalRef;
1336     }
1337     else
1338     {
1339         return NULL;
1340     }
1341 }
1342 
1343 void AwtFrame::_SetState(void *param)
1344 {
1345     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1346 
1347     SetStateStruct *sss = (SetStateStruct *)param;
1348     jobject self = sss->frame;
1349     jint state = sss->state;
1350 
1351     AwtFrame *f = NULL;
1352 
1353     PDATA pData;
1354     JNI_CHECK_PEER_GOTO(self, ret);
1355     f = (AwtFrame *)pData;
1356     HWND hwnd = f->GetHWnd();
1357     if (::IsWindow(hwnd))
1358     {
1359         DASSERT(!IsBadReadPtr(f, sizeof(AwtFrame)));
1360 
1361         BOOL iconify = (state & java_awt_Frame_ICONIFIED) != 0;
1362         BOOL zoom = (state & java_awt_Frame_MAXIMIZED_BOTH)
1363                         == java_awt_Frame_MAXIMIZED_BOTH;
1364 
1365         DTRACE_PRINTLN4("WFramePeer.setState:%s%s ->%s%s",
1366                   f->isIconic() ? " iconic" : "",
1367                   f->isZoomed() ? " zoomed" : "",
1368                   iconify       ? " iconic" : "",
1369                   zoom          ? " zoomed" : "");
1370 
1371         if (::IsWindowVisible(hwnd)) {
1372             BOOL focusable = f->IsFocusableWindow();
1373 
1374             WINDOWPLACEMENT wp;
1375             ::ZeroMemory(&wp, sizeof(wp));
1376             wp.length = sizeof(wp);
1377             ::GetWindowPlacement(hwnd, &wp);
1378 
1379             // Iconify first.
1380             // If both iconify & zoom are TRUE, handle this case
1381             // with wp.flags field below.
1382             if (iconify) {
1383                 wp.showCmd = focusable ? SW_MINIMIZE : SW_SHOWMINNOACTIVE;
1384             } else if (zoom) {
1385                 wp.showCmd = focusable ? SW_SHOWMAXIMIZED : SW_MAXIMIZE;
1386             } else { // zoom == iconify == FALSE
1387                 wp.showCmd = focusable ? SW_RESTORE : SW_SHOWNOACTIVATE;
1388             }
1389             if (zoom && iconify) {
1390                 wp.flags |= WPF_RESTORETOMAXIMIZED;
1391             } else {
1392                 wp.flags &= ~WPF_RESTORETOMAXIMIZED;
1393             }
1394 
1395             if (!zoom) {
1396                 f->m_forceResetZoomed = TRUE;
1397             }
1398 
1399             // The SetWindowPlacement() causes the WmSize() invocation
1400             //  which, in turn, actually updates the m_iconic & m_zoomed flags
1401             //  as well as sends Java event (WINDOW_STATE_CHANGED.)
1402             ::SetWindowPlacement(hwnd, &wp);
1403 
1404             f->m_forceResetZoomed = FALSE;
1405         } else {
1406             DTRACE_PRINTLN("  not visible, just recording the requested state");
1407 
1408             f->setIconic(iconify);
1409             f->setZoomed(zoom);
1410         }
1411     }
1412 ret:
1413     env->DeleteGlobalRef(self);
1414 
1415     delete sss;
1416 }
1417 
1418 jint AwtFrame::_GetState(void *param)
1419 {
1420     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1421 
1422     jobject self = (jobject)param;
1423 
1424     jint result = java_awt_Frame_NORMAL;
1425     AwtFrame *f = NULL;
1426 
1427     PDATA pData;
1428     JNI_CHECK_PEER_GOTO(self, ret);
1429     f = (AwtFrame *)pData;
1430     if (::IsWindow(f->GetHWnd()))
1431     {
1432         DASSERT(!::IsBadReadPtr(f, sizeof(AwtFrame)));
1433         if (f->isIconic()) {
1434             result |= java_awt_Frame_ICONIFIED;
1435         }
1436         if (f->isZoomed()) {
1437             result |= java_awt_Frame_MAXIMIZED_BOTH;
1438         }
1439 
1440         DTRACE_PRINTLN2("WFramePeer.getState:%s%s",
1441                   f->isIconic() ? " iconic" : "",
1442                   f->isZoomed() ? " zoomed" : "");
1443     }
1444 ret:
1445     env->DeleteGlobalRef(self);
1446 
1447     return result;
1448 }
1449 
1450 void AwtFrame::_SetMaximizedBounds(void *param)
1451 {
1452     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1453 
1454     SetMaximizedBoundsStruct *smbs = (SetMaximizedBoundsStruct *)param;
1455     jobject self = smbs->frame;
1456     int x = smbs->x;
1457     int y = smbs->y;
1458     int width = smbs->width;
1459     int height = smbs->height;
1460 
1461     AwtFrame *f = NULL;
1462 
1463     PDATA pData;
1464     JNI_CHECK_PEER_GOTO(self, ret);
1465     f = (AwtFrame *)pData;
1466     if (::IsWindow(f->GetHWnd()))
1467     {
1468         DASSERT(!::IsBadReadPtr(f, sizeof(AwtFrame)));
1469         f->SetMaximizedBounds(x, y, width, height);
1470     }
1471 ret:
1472     env->DeleteGlobalRef(self);
1473 
1474     delete smbs;
1475 }
1476 
1477 void AwtFrame::_ClearMaximizedBounds(void *param)
1478 {
1479     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1480 
1481     jobject self = (jobject)param;
1482 
1483     AwtFrame *f = NULL;
1484 
1485     PDATA pData;
1486     JNI_CHECK_PEER_GOTO(self, ret);
1487     f = (AwtFrame *)pData;
1488     if (::IsWindow(f->GetHWnd()))
1489     {
1490         DASSERT(!::IsBadReadPtr(f, sizeof(AwtFrame)));
1491         f->ClearMaximizedBounds();
1492     }
1493 ret:
1494     env->DeleteGlobalRef(self);
1495 }
1496 
1497 void AwtFrame::_SetMenuBar(void *param)
1498 {
1499     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1500 
1501     SetMenuBarStruct *smbs = (SetMenuBarStruct *)param;
1502     jobject self = smbs->frame;
1503     jobject menubar = smbs->menubar;
1504 
1505     AwtFrame *f = NULL;
1506 
1507     PDATA pData;
1508     JNI_CHECK_PEER_GOTO(self, ret);
1509     f = (AwtFrame *)pData;
1510     if (::IsWindow(f->GetHWnd()))
1511     {
1512         ExecuteArgs args;
1513         args.cmdId = FRAME_SETMENUBAR;
1514         args.param1 = (LPARAM)menubar;
1515         f->WinThreadExecProc(&args);
1516     }
1517 ret:
1518     env->DeleteGlobalRef(self);
1519     env->DeleteGlobalRef(menubar);
1520 
1521     delete smbs;
1522 }
1523 
1524 void AwtFrame::_SetIMMOption(void *param)
1525 {
1526     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1527 
1528     SetIMMOptionStruct *sios = (SetIMMOptionStruct *)param;
1529     jobject self = sios->frame;
1530     jstring option = sios->option;
1531 
1532     int badAlloc = 0;
1533     LPCTSTR coption;
1534     LPCTSTR empty = TEXT("InputMethod");
1535     AwtFrame *f = NULL;
1536 
1537     PDATA pData;
1538     JNI_CHECK_PEER_GOTO(self, ret);
1539     JNI_CHECK_NULL_GOTO(option, "IMMOption argument", ret);
1540 
1541     f = (AwtFrame *)pData;
1542     if (::IsWindow(f->GetHWnd()))
1543     {
1544         coption = JNU_GetStringPlatformChars(env, option, NULL);
1545         if (coption == NULL)
1546         {
1547             badAlloc = 1;
1548         }
1549         if (!badAlloc)
1550         {
1551             HMENU hSysMenu = ::GetSystemMenu(f->GetHWnd(), FALSE);
1552             ::AppendMenu(hSysMenu,  MF_STRING, SYSCOMMAND_IMM, coption);
1553 
1554             if (coption != empty)
1555             {
1556                 JNU_ReleaseStringPlatformChars(env, option, coption);
1557             }
1558         }
1559     }
1560 ret:
1561     env->DeleteGlobalRef(self);
1562     env->DeleteGlobalRef(option);
1563 
1564     delete sios;
1565 
1566     if (badAlloc)
1567     {
1568         throw std::bad_alloc();
1569     }
1570 }
1571 
1572 void AwtFrame::_NotifyModalBlocked(void *param)
1573 {
1574     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1575 
1576     NotifyModalBlockedStruct *nmbs = (NotifyModalBlockedStruct *)param;
1577     jobject self = nmbs->frame;
1578     jobject peer = nmbs->peer;
1579     jobject blockerPeer = nmbs->blockerPeer;
1580     jboolean blocked = nmbs->blocked;
1581 
1582     PDATA pData;
1583 
1584     JNI_CHECK_PEER_GOTO(peer, ret);
1585     AwtFrame *f = (AwtFrame *)pData;
1586 
1587     // dialog here may be NULL, for example, if the blocker is a native dialog
1588     // however, we need to install/unistall modal hooks anyway
1589     JNI_CHECK_PEER_GOTO(blockerPeer, ret);
1590     AwtDialog *d = (AwtDialog *)pData;
1591 
1592     if ((f != NULL) && ::IsWindow(f->GetHWnd()))
1593     {
1594         // get an HWND of the toplevel window this embedded frame is within
1595         HWND fHWnd = f->GetHWnd();
1596         while (::GetParent(fHWnd) != NULL) {
1597             fHWnd = ::GetParent(fHWnd);
1598         }
1599         // we must get a toplevel hwnd here, however due to some strange
1600         // behaviour of Java Plugin (a bug?) when running in IE at
1601         // this moment the embedded frame hasn't been placed into the
1602         // browser yet and fHWnd is not a toplevel, so we shouldn't install
1603         // the hook here
1604         if ((::GetWindowLong(fHWnd, GWL_STYLE) & WS_CHILD) == 0) {
1605             // if this toplevel is created in another thread, we should install
1606             // the modal hook into it to track window activation and mouse events
1607             DWORD fThread = ::GetWindowThreadProcessId(fHWnd, NULL);
1608             if (fThread != AwtToolkit::GetInstance().MainThread()) {
1609                 // check if this thread has been already blocked
1610                 BlockedThreadStruct *blockedThread = (BlockedThreadStruct *)sm_BlockedThreads.get((void *)((intptr_t)fThread));
1611                 if (blocked) {
1612                     if (blockedThread == NULL) {
1613                         blockedThread = new BlockedThreadStruct;
1614                         blockedThread->framesCount = 1;
1615                         blockedThread->modalHook = ::SetWindowsHookEx(WH_CBT, (HOOKPROC)AwtDialog::ModalFilterProc,
1616                                                                       0, fThread);
1617                         blockedThread->mouseHook = ::SetWindowsHookEx(WH_MOUSE, (HOOKPROC)AwtDialog::MouseHookProc_NonTT,
1618                                                                       0, fThread);
1619                         sm_BlockedThreads.put((void *)((intptr_t)fThread), blockedThread);
1620                     } else {
1621                         blockedThread->framesCount++;
1622                     }
1623                 } else {
1624                     // see the comment above: if Java Plugin behaviour when running in IE
1625                     // was right, blockedThread would be always not NULL here
1626                     if (blockedThread != NULL) {
1627                         DASSERT(blockedThread->framesCount > 0);
1628                         if ((blockedThread->framesCount) == 1) {
1629                             ::UnhookWindowsHookEx(blockedThread->modalHook);
1630                             ::UnhookWindowsHookEx(blockedThread->mouseHook);
1631                             sm_BlockedThreads.remove((void *)((intptr_t)fThread));
1632                             delete blockedThread;
1633                         } else {
1634                             blockedThread->framesCount--;
1635                         }
1636                     }
1637                 }
1638             }
1639         }
1640     }
1641 ret:
1642     env->DeleteGlobalRef(self);
1643     env->DeleteGlobalRef(peer);
1644     env->DeleteGlobalRef(blockerPeer);
1645 
1646     delete nmbs;
1647 }
1648 
1649 /************************************************************************
1650  * WFramePeer native methods
1651  */
1652 
1653 extern "C" {
1654 
1655 /*
1656  * Class:     java_awt_Frame
1657  * Method:    initIDs
1658  * Signature: ()V
1659  */
1660 JNIEXPORT void JNICALL
1661 Java_java_awt_Frame_initIDs(JNIEnv *env, jclass cls)
1662 {
1663     TRY;
1664 
1665     AwtFrame::undecoratedID = env->GetFieldID(cls,"undecorated","Z");
1666     DASSERT(AwtFrame::undecoratedID != NULL);
1667 
1668     CATCH_BAD_ALLOC;
1669 }
1670 
1671 /*
1672  * Class:     sun_awt_windows_WFramePeer
1673  * Method:    initIDs
1674  * Signature: ()V
1675  */
1676 JNIEXPORT void JNICALL
1677 Java_sun_awt_windows_WFramePeer_initIDs(JNIEnv *env, jclass cls)
1678 {
1679     TRY;
1680 
1681     AwtFrame::setExtendedStateMID = env->GetMethodID(cls, "setExtendedState", "(I)V");
1682     DASSERT(AwtFrame::setExtendedStateMID);
1683     CHECK_NULL(AwtFrame::setExtendedStateMID);
1684 
1685     AwtFrame::getExtendedStateMID = env->GetMethodID(cls, "getExtendedState", "()I");
1686     DASSERT(AwtFrame::getExtendedStateMID);
1687 
1688     CATCH_BAD_ALLOC;
1689 }
1690 
1691 /*
1692  * Class:     sun_awt_windows_WFramePeer
1693  * Method:    setState
1694  * Signature: (I)V
1695  */
1696 JNIEXPORT void JNICALL
1697 Java_sun_awt_windows_WFramePeer_setState(JNIEnv *env, jobject self,
1698     jint state)
1699 {
1700     TRY;
1701 
1702     SetStateStruct *sss = new SetStateStruct;
1703     sss->frame = env->NewGlobalRef(self);
1704     sss->state = state;
1705 
1706     AwtToolkit::GetInstance().SyncCall(AwtFrame::_SetState, sss);
1707     // global ref and sss are deleted in _SetState()
1708 
1709     CATCH_BAD_ALLOC;
1710 }
1711 
1712 /*
1713  * Class:     sun_awt_windows_WFramePeer
1714  * Method:    getState
1715  * Signature: ()I
1716  */
1717 JNIEXPORT jint JNICALL
1718 Java_sun_awt_windows_WFramePeer_getState(JNIEnv *env, jobject self)
1719 {
1720     TRY;
1721 
1722     jobject selfGlobalRef = env->NewGlobalRef(self);
1723 
1724     return static_cast<jint>(reinterpret_cast<INT_PTR>(AwtToolkit::GetInstance().SyncCall(
1725         (void*(*)(void*))AwtFrame::_GetState,
1726         (void *)selfGlobalRef)));
1727     // selfGlobalRef is deleted in _GetState()
1728 
1729     CATCH_BAD_ALLOC_RET(java_awt_Frame_NORMAL);
1730 }
1731 
1732 
1733 /*
1734  * Class:     sun_awt_windows_WFramePeer
1735  * Method:    setMaximizedBounds
1736  * Signature: (IIII)V
1737  */
1738 JNIEXPORT void JNICALL
1739 Java_sun_awt_windows_WFramePeer_setMaximizedBounds(JNIEnv *env, jobject self,
1740     jint x, jint y, jint width, jint height)
1741 {
1742     TRY;
1743 
1744     SetMaximizedBoundsStruct *smbs = new SetMaximizedBoundsStruct;
1745     smbs->frame = env->NewGlobalRef(self);
1746     smbs->x = x;
1747     smbs->y = y;
1748     smbs->width = width;
1749     smbs->height = height;
1750 
1751     AwtToolkit::GetInstance().SyncCall(AwtFrame::_SetMaximizedBounds, smbs);
1752     // global ref and smbs are deleted in _SetMaximizedBounds()
1753 
1754     CATCH_BAD_ALLOC;
1755 }
1756 
1757 
1758 /*
1759  * Class:     sun_awt_windows_WFramePeer
1760  * Method:    clearMaximizedBounds
1761  * Signature: ()V
1762  */
1763 JNIEXPORT void JNICALL
1764 Java_sun_awt_windows_WFramePeer_clearMaximizedBounds(JNIEnv *env, jobject self)
1765 {
1766     TRY;
1767 
1768     jobject selfGlobalRef = env->NewGlobalRef(self);
1769 
1770     AwtToolkit::GetInstance().SyncCall(AwtFrame::_ClearMaximizedBounds,
1771         (void *)selfGlobalRef);
1772     // selfGlobalRef is deleted in _ClearMaximizedBounds()
1773 
1774     CATCH_BAD_ALLOC;
1775 }
1776 
1777 
1778 /*
1779  * Class:     sun_awt_windows_WFramePeer
1780  * Method:    setMenuBar0
1781  * Signature: (Lsun/awt/windows/WMenuBarPeer;)V
1782  */
1783 JNIEXPORT void JNICALL
1784 Java_sun_awt_windows_WFramePeer_setMenuBar0(JNIEnv *env, jobject self,
1785                                             jobject mbPeer)
1786 {
1787     TRY;
1788 
1789     SetMenuBarStruct *smbs = new SetMenuBarStruct;
1790     smbs->frame = env->NewGlobalRef(self);
1791     smbs->menubar = env->NewGlobalRef(mbPeer);
1792 
1793     AwtToolkit::GetInstance().SyncCall(AwtFrame::_SetMenuBar, smbs);
1794     // global refs ans smbs are deleted in _SetMenuBar()
1795 
1796     CATCH_BAD_ALLOC;
1797 }
1798 
1799 /*
1800  * Class:     sun_awt_windows_WFramePeer
1801  * Method:    create
1802  * Signature: (Lsun/awt/windows/WComponentPeer;)V
1803  */
1804 JNIEXPORT void JNICALL
1805 Java_sun_awt_windows_WFramePeer_createAwtFrame(JNIEnv *env, jobject self,
1806                                                jobject parent)
1807 {
1808     TRY;
1809 
1810     AwtToolkit::CreateComponent(self, parent,
1811                                 (AwtToolkit::ComponentFactory)
1812                                 AwtFrame::Create);
1813 
1814     CATCH_BAD_ALLOC;
1815 }
1816 
1817 /*
1818  * Class:     sun_awt_windows_WFramePeer
1819  * Method:    getSysMenuHeight
1820  * Signature: ()I
1821  */
1822 JNIEXPORT jint JNICALL
1823 Java_sun_awt_windows_WFramePeer_getSysMenuHeight(JNIEnv *env, jclass self)
1824 {
1825     TRY;
1826 
1827     return ::GetSystemMetrics(SM_CYMENUSIZE);
1828 
1829     CATCH_BAD_ALLOC_RET(0);
1830 }
1831 
1832 /*
1833  * Class:     sun_awt_windows_WFramePeer
1834  * Method:    pSetIMMOption
1835  * Signature: (Ljava/lang/String;)V
1836  */
1837 JNIEXPORT void JNICALL
1838 Java_sun_awt_windows_WFramePeer_pSetIMMOption(JNIEnv *env, jobject self,
1839                                                jstring option)
1840 {
1841     TRY;
1842 
1843     SetIMMOptionStruct *sios = new SetIMMOptionStruct;
1844     sios->frame = env->NewGlobalRef(self);
1845     sios->option = (jstring)env->NewGlobalRef(option);
1846 
1847     AwtToolkit::GetInstance().SyncCall(AwtFrame::_SetIMMOption, sios);
1848     // global refs and sios are deleted in _SetIMMOption()
1849 
1850     CATCH_BAD_ALLOC;
1851 }
1852 
1853 } /* extern "C" */
1854 
1855 
1856 /************************************************************************
1857  * WEmbeddedFrame native methods
1858  */
1859 
1860 extern "C" {
1861 
1862 /*
1863  * Class:     sun_awt_windows_WFramePeer
1864  * Method:    initIDs
1865  * Signature: (Lsun/awt/windows/WMenuBarPeer;)V
1866  */
1867 JNIEXPORT void JNICALL
1868 Java_sun_awt_windows_WEmbeddedFrame_initIDs(JNIEnv *env, jclass cls)
1869 {
1870     TRY;
1871 
1872     AwtFrame::handleID = env->GetFieldID(cls, "handle", "J");
1873     DASSERT(AwtFrame::handleID != NULL);
1874     CHECK_NULL(AwtFrame::handleID);
1875 
1876     AwtFrame::activateEmbeddingTopLevelMID = env->GetMethodID(cls, "activateEmbeddingTopLevel", "()V");
1877     DASSERT(AwtFrame::activateEmbeddingTopLevelMID != NULL);
1878     CHECK_NULL(AwtFrame::activateEmbeddingTopLevelMID);
1879 
1880     AwtFrame::isEmbeddedInIEID = env->GetFieldID(cls, "isEmbeddedInIE", "Z");
1881     DASSERT(AwtFrame::isEmbeddedInIEID != NULL);
1882 
1883     CATCH_BAD_ALLOC;
1884 }
1885 
1886 JNIEXPORT void JNICALL
1887 Java_sun_awt_windows_WEmbeddedFrame_notifyModalBlockedImpl(JNIEnv *env,
1888                                                            jobject self,
1889                                                            jobject peer,
1890                                                            jobject blockerPeer,
1891                                                            jboolean blocked)
1892 {
1893     TRY;
1894 
1895     NotifyModalBlockedStruct *nmbs = new NotifyModalBlockedStruct;
1896     nmbs->frame = env->NewGlobalRef(self);
1897     nmbs->peer = env->NewGlobalRef(peer);
1898     nmbs->blockerPeer = env->NewGlobalRef(blockerPeer);
1899     nmbs->blocked = blocked;
1900 
1901     AwtToolkit::GetInstance().SyncCall(AwtFrame::_NotifyModalBlocked, nmbs);
1902     // global refs and nmbs are deleted in _NotifyModalBlocked()
1903 
1904     CATCH_BAD_ALLOC;
1905 }
1906 
1907 } /* extern "C" */
1908 
1909 
1910 /************************************************************************
1911  * WEmbeddedFramePeer native methods
1912  */
1913 
1914 extern "C" {
1915 
1916 JNIEXPORT void JNICALL
1917 Java_sun_awt_windows_WEmbeddedFramePeer_create(JNIEnv *env, jobject self,
1918                                                jobject parent)
1919 {
1920     TRY;
1921 
1922     JNI_CHECK_NULL_RETURN(self, "peer");
1923     AwtToolkit::CreateComponent(self, parent,
1924                                 (AwtToolkit::ComponentFactory)
1925                                 AwtFrame::Create);
1926 
1927     CATCH_BAD_ALLOC;
1928 }
1929 
1930 JNIEXPORT jobject JNICALL
1931 Java_sun_awt_windows_WEmbeddedFramePeer_getBoundsPrivate(JNIEnv *env, jobject self)
1932 {
1933     TRY;
1934 
1935     jobject result = (jobject)AwtToolkit::GetInstance().SyncCall(
1936         (void *(*)(void *))AwtFrame::_GetBoundsPrivate,
1937         env->NewGlobalRef(self));
1938     // global ref is deleted in _GetBoundsPrivate
1939 
1940     if (result != NULL)
1941     {
1942         jobject resultLocalRef = env->NewLocalRef(result);
1943         env->DeleteGlobalRef(result);
1944         return resultLocalRef;
1945     }
1946     else
1947     {
1948         return NULL;
1949     }
1950 
1951     CATCH_BAD_ALLOC_RET(NULL);
1952 }
1953 
1954 JNIEXPORT void JNICALL
1955 Java_sun_awt_windows_WFramePeer_synthesizeWmActivate(JNIEnv *env, jobject self, jboolean doActivate)
1956 {
1957     TRY;
1958 
1959     SynthesizeWmActivateStruct *sas = new SynthesizeWmActivateStruct;
1960     sas->frame = env->NewGlobalRef(self);
1961     sas->doActivate = doActivate;
1962 
1963     /*
1964      * WARNING: invoking this function without synchronization by m_Sync CriticalSection.
1965      * Taking this lock results in a deadlock.
1966      */
1967     AwtToolkit::GetInstance().InvokeFunction(AwtFrame::_SynthesizeWmActivate, sas);
1968     // global ref and sas are deleted in _SynthesizeWmActivate()
1969 
1970     CATCH_BAD_ALLOC;
1971 }
1972 
1973 } /* extern "C" */
1974 
1975 static bool SetFocusToPluginControl(HWND hwndPlugin)
1976 {
1977     HWND hwndFocus = ::GetFocus();
1978 
1979     if (hwndFocus == hwndPlugin) {
1980         return false;
1981     }
1982 
1983     ::SetFocus(hwndPlugin);
1984     DWORD dwError = ::GetLastError();
1985     if (dwError != ERROR_SUCCESS) {
1986         // If direct call failed, use a special message to set focus
1987         return (::SendMessage(hwndPlugin, WM_AX_REQUEST_FOCUS_TO_EMBEDDER, 0, 0) == 0);
1988     }
1989     return true;
1990 }