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