1 /*
   2  * Copyright (c) 1996, 2014, 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_MenuBar.h"
  27 #include "awt_Frame.h"
  28 
  29 /* IMPORTANT! Read the README.JNI file for notes on JNI converted AWT code.
  30  */
  31 
  32 /***********************************************************************/
  33 // struct for _AddMenu() method
  34 struct AddMenuStruct {
  35     jobject menubar;
  36     jobject menu;
  37 };
  38 /************************************************************************
  39  * AwtMenuBar fields
  40  */
  41 
  42 jmethodID AwtMenuBar::getMenuMID;
  43 jmethodID AwtMenuBar::getMenuCountMID;
  44 
  45 
  46 /************************************************************************
  47  * AwtMenuBar methods
  48  */
  49 
  50 
  51 AwtMenuBar::AwtMenuBar() {
  52     m_frame = NULL;
  53 }
  54 
  55 AwtMenuBar::~AwtMenuBar()
  56 {
  57 }
  58 
  59 void AwtMenuBar::Dispose()
  60 {
  61     m_frame = NULL;
  62 
  63     AwtMenu::Dispose();
  64 }
  65 
  66 LPCTSTR AwtMenuBar::GetClassName() {
  67   return TEXT("SunAwtMenuBar");
  68 }
  69 
  70 /* Create a new AwtMenuBar object and menu.   */
  71 AwtMenuBar* AwtMenuBar::Create(jobject self, jobject framePeer)
  72 {
  73     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
  74 
  75     jobject target = NULL;
  76     AwtMenuBar* menuBar = NULL;
  77 
  78     try {
  79         if (env->EnsureLocalCapacity(1) < 0) {
  80             return NULL;
  81         }
  82 
  83         /* target is a java.awt.MenuBar */
  84         target = env->GetObjectField(self, AwtObject::targetID);
  85         JNI_CHECK_NULL_GOTO(target, "null target", done);
  86 
  87         menuBar = new AwtMenuBar();
  88 
  89         SetLastError(0);
  90         HMENU hMenu = ::CreateMenu();
  91         // fix for 5088782
  92         if (!CheckMenuCreation(env, self, hMenu))
  93         {
  94             env->DeleteLocalRef(target);
  95             return NULL;
  96         }
  97 
  98         menuBar->SetHMenu(hMenu);
  99 
 100         menuBar->LinkObjects(env, self);
 101         if (framePeer != NULL) {
 102             PDATA pData;
 103             JNI_CHECK_PEER_GOTO(framePeer, done);
 104             menuBar->m_frame = (AwtFrame *)pData;
 105         } else {
 106             menuBar->m_frame = NULL;
 107         }
 108     } catch (...) {
 109         env->DeleteLocalRef(target);
 110         throw;
 111     }
 112 
 113 done:
 114     if (target != NULL) {
 115         env->DeleteLocalRef(target);
 116     }
 117 
 118     return menuBar;
 119 }
 120 
 121 HWND AwtMenuBar::GetOwnerHWnd()
 122 {
 123     AwtFrame *myFrame = m_frame;
 124     if (myFrame == NULL)
 125         return NULL;
 126     else
 127         return myFrame->GetHWnd();
 128 }
 129 
 130 void AwtMenuBar::SendDrawItem(AwtMenuItem* awtMenuItem,
 131                               DRAWITEMSTRUCT& drawInfo)
 132 {
 133     awtMenuItem->DrawItem(drawInfo);
 134 }
 135 
 136 void AwtMenuBar::SendMeasureItem(AwtMenuItem* awtMenuItem,
 137                                  HDC hDC, MEASUREITEMSTRUCT& measureInfo)
 138 {
 139     awtMenuItem->MeasureItem(hDC, measureInfo);
 140 }
 141 
 142 int AwtMenuBar::CountItem(jobject menuBar)
 143 {
 144     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 145     jint nCount = env->CallIntMethod(menuBar, AwtMenuBar::getMenuCountMID);
 146     DASSERT(!safe_ExceptionOccurred(env));
 147 
 148     return nCount;
 149 }
 150 
 151 AwtMenuItem* AwtMenuBar::GetItem(jobject target, long index)
 152 {
 153     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 154     if (env->EnsureLocalCapacity(2) < 0) {
 155         return NULL;
 156     }
 157 
 158     jobject menu = env->CallObjectMethod(target, AwtMenuBar::getMenuMID,index);
 159     DASSERT(!safe_ExceptionOccurred(env));
 160 
 161     jobject menuItemPeer = GetPeerForTarget(env, menu);
 162     PDATA pData;
 163     JNI_CHECK_PEER_RETURN_NULL(menuItemPeer);
 164     AwtMenuItem* awtMenuItem = (AwtMenuItem*)pData;
 165 
 166     env->DeleteLocalRef(menu);
 167     env->DeleteLocalRef(menuItemPeer);
 168 
 169     return awtMenuItem;
 170 }
 171 
 172 void AwtMenuBar::DrawItem(DRAWITEMSTRUCT& drawInfo)
 173 {
 174     DASSERT(drawInfo.CtlType == ODT_MENU);
 175     AwtMenu::DrawItems(drawInfo);
 176 }
 177 
 178 void AwtMenuBar::MeasureItem(HDC hDC,
 179                              MEASUREITEMSTRUCT& measureInfo)
 180 {
 181     DASSERT(measureInfo.CtlType == ODT_MENU);
 182     AwtMenu::MeasureItem(hDC, measureInfo);
 183 }
 184 
 185 void AwtMenuBar::AddItem(AwtMenuItem* item)
 186 {
 187     AwtMenu::AddItem(item);
 188     HWND hOwnerWnd = GetOwnerHWnd();
 189     if (hOwnerWnd != NULL) {
 190         VERIFY(::InvalidateRect(hOwnerWnd,0,TRUE));
 191     }
 192 }
 193 
 194 void AwtMenuBar::DeleteItem(UINT index)
 195 {
 196     AwtMenu::DeleteItem(index);
 197     HWND hOwnerWnd = GetOwnerHWnd();
 198     if (hOwnerWnd != NULL) {
 199         VERIFY(::InvalidateRect(hOwnerWnd,0,TRUE));
 200     }
 201     RedrawMenuBar();
 202 }
 203 
 204 /**
 205  * If the menu changes after the system has created the window,
 206  * this function must be called to draw the changed menu bar.
 207  */
 208 void AwtMenuBar::RedrawMenuBar() {
 209     VERIFY(::DrawMenuBar(GetOwnerHWnd()));
 210 }
 211 
 212 LRESULT AwtMenuBar::WinThreadExecProc(ExecuteArgs * args)
 213 {
 214     switch( args->cmdId ) {
 215         case MENUBAR_DELITEM:
 216             this->DeleteItem(static_cast<UINT>(args->param1));
 217             break;
 218 
 219         default:
 220             AwtMenu::WinThreadExecProc(args);
 221             break;
 222     }
 223     return 0L;
 224 }
 225 
 226 void AwtMenuBar::_AddMenu(void *param)
 227 {
 228     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 229 
 230     AddMenuStruct *ams = (AddMenuStruct *)param;
 231     jobject self = ams->menubar;
 232     jobject menu = ams->menu;
 233 
 234     AwtMenuBar *m = NULL;
 235 
 236     PDATA pData;
 237     JNI_CHECK_PEER_GOTO(self, ret);
 238     JNI_CHECK_NULL_GOTO(menu, "null menu", ret);
 239     m = (AwtMenuBar *)pData;
 240     if (::IsWindow(m->GetOwnerHWnd()))
 241     {
 242         /* The menu was already created and added during peer creation -- redraw */
 243         m->RedrawMenuBar();
 244     }
 245 ret:
 246     env->DeleteGlobalRef(self);
 247     if (menu != NULL) {
 248         env->DeleteGlobalRef(menu);
 249     }
 250 
 251     delete ams;
 252 }
 253 
 254 /************************************************************************
 255  * MenuBar native methods
 256  */
 257 
 258 extern "C" {
 259 
 260 /*
 261  * Class:     java_awt_MenuBar
 262  * Method:    initIDs
 263  * Signature: ()V
 264  */
 265 JNIEXPORT void JNICALL
 266 Java_java_awt_MenuBar_initIDs(JNIEnv *env, jclass cls)
 267 {
 268     TRY;
 269 
 270     AwtMenuBar::getMenuCountMID = env->GetMethodID(cls, "getMenuCountImpl", "()I");
 271     DASSERT(AwtMenuBar::getMenuCountMID != NULL);
 272     CHECK_NULL(AwtMenuBar::getMenuCountMID);
 273 
 274     AwtMenuBar::getMenuMID = env->GetMethodID(cls, "getMenuImpl",
 275                                               "(I)Ljava/awt/Menu;");
 276     DASSERT(AwtMenuBar::getMenuMID != NULL);
 277 
 278     CATCH_BAD_ALLOC;
 279 }
 280 
 281 } /* extern "C" */
 282 
 283 
 284 /************************************************************************
 285  * WMenuBarPeer native methods
 286  */
 287 
 288 extern "C" {
 289 
 290 /*
 291  * Class:     sun_awt_windows_WMenuBarPeer
 292  * Method:    addMenu
 293  * Signature: (Ljava/awt/Menu;)V
 294  */
 295 JNIEXPORT void JNICALL
 296 Java_sun_awt_windows_WMenuBarPeer_addMenu(JNIEnv *env, jobject self,
 297                                           jobject menu)
 298 {
 299     TRY;
 300 
 301     AddMenuStruct *ams = new AddMenuStruct;
 302     ams->menubar = env->NewGlobalRef(self);
 303     ams->menu = env->NewGlobalRef(menu);
 304 
 305     AwtToolkit::GetInstance().SyncCall(AwtMenuBar::_AddMenu, ams);
 306     // global refs and ams are deleted in _AddMenu()
 307 
 308     CATCH_BAD_ALLOC;
 309 }
 310 
 311 /*
 312  * Class:     sun_awt_windows_WMenuBarPeer
 313  * Method:    delMenu
 314  * Signature: (I)V
 315  */
 316 JNIEXPORT void JNICALL
 317 Java_sun_awt_windows_WMenuBarPeer_delMenu(JNIEnv *env, jobject self,
 318                                           jint index)
 319 {
 320     TRY;
 321 
 322     PDATA pData;
 323     JNI_CHECK_PEER_RETURN(self);
 324     AwtObject::WinThreadExec(self, AwtMenuBar::MENUBAR_DELITEM, (LPARAM)index);
 325 
 326     CATCH_BAD_ALLOC;
 327 }
 328 
 329 /*
 330  * Class:     sun_awt_windows_WMenuBarPeer
 331  * Method:    create
 332  * Signature: (Lsun/awt/windows/WFramePeer;)V
 333  */
 334 JNIEXPORT void JNICALL
 335 Java_sun_awt_windows_WMenuBarPeer_create(JNIEnv *env, jobject self,
 336                                          jobject frame)
 337 {
 338     TRY;
 339 
 340     AwtToolkit::CreateComponent(self, frame,
 341                                 (AwtToolkit::ComponentFactory)
 342                                 AwtMenuBar::Create);
 343     PDATA pData;
 344     JNI_CHECK_PEER_CREATION_RETURN(self);
 345 
 346     CATCH_BAD_ALLOC;
 347 }
 348 
 349 } /* extern "C" */