1 /*
   2  * Copyright (c) 1996, 2006, 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     ::DrawMenuBar(GetOwnerHWnd());
 202 }
 203 
 204 LRESULT AwtMenuBar::WinThreadExecProc(ExecuteArgs * args)
 205 {
 206     switch( args->cmdId ) {
 207         case MENUBAR_DELITEM:
 208             this->DeleteItem(static_cast<UINT>(args->param1));
 209             break;
 210 
 211         default:
 212             AwtMenu::WinThreadExecProc(args);
 213             break;
 214     }
 215     return 0L;
 216 }
 217 
 218 void AwtMenuBar::_AddMenu(void *param)
 219 {
 220     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
 221 
 222     AddMenuStruct *ams = (AddMenuStruct *)param;
 223     jobject self = ams->menubar;
 224     jobject menu = ams->menu;
 225 
 226     AwtMenuBar *m = NULL;
 227 
 228     PDATA pData;
 229     JNI_CHECK_PEER_GOTO(self, ret);
 230     JNI_CHECK_NULL_GOTO(menu, "null menu", ret);
 231     m = (AwtMenuBar *)pData;
 232     if (::IsWindow(m->GetOwnerHWnd()))
 233     {
 234         /* The menu was already created and added during peer creation -- redraw */
 235         ::DrawMenuBar(m->GetOwnerHWnd());
 236     }
 237 ret:
 238     env->DeleteGlobalRef(self);
 239     if (menu != NULL) {
 240         env->DeleteGlobalRef(menu);
 241     }
 242 
 243     delete ams;
 244 }
 245 
 246 /************************************************************************
 247  * MenuBar native methods
 248  */
 249 
 250 extern "C" {
 251 
 252 /*
 253  * Class:     java_awt_MenuBar
 254  * Method:    initIDs
 255  * Signature: ()V
 256  */
 257 JNIEXPORT void JNICALL
 258 Java_java_awt_MenuBar_initIDs(JNIEnv *env, jclass cls)
 259 {
 260     TRY;
 261 
 262     AwtMenuBar::getMenuCountMID = env->GetMethodID(cls, "getMenuCountImpl", "()I");
 263     AwtMenuBar::getMenuMID = env->GetMethodID(cls, "getMenuImpl",
 264                                               "(I)Ljava/awt/Menu;");
 265     DASSERT(AwtMenuBar::getMenuCountMID != NULL);
 266     DASSERT(AwtMenuBar::getMenuMID != NULL);
 267 
 268     CATCH_BAD_ALLOC;
 269 }
 270 
 271 } /* extern "C" */
 272 
 273 
 274 /************************************************************************
 275  * WMenuBarPeer native methods
 276  */
 277 
 278 extern "C" {
 279 
 280 /*
 281  * Class:     sun_awt_windows_WMenuBarPeer
 282  * Method:    addMenu
 283  * Signature: (Ljava/awt/Menu;)V
 284  */
 285 JNIEXPORT void JNICALL
 286 Java_sun_awt_windows_WMenuBarPeer_addMenu(JNIEnv *env, jobject self,
 287                                           jobject menu)
 288 {
 289     TRY;
 290 
 291     AddMenuStruct *ams = new AddMenuStruct;
 292     ams->menubar = env->NewGlobalRef(self);
 293     ams->menu = env->NewGlobalRef(menu);
 294 
 295     AwtToolkit::GetInstance().SyncCall(AwtMenuBar::_AddMenu, ams);
 296     // global refs and ams are deleted in _AddMenu()
 297 
 298     CATCH_BAD_ALLOC;
 299 }
 300 
 301 /*
 302  * Class:     sun_awt_windows_WMenuBarPeer
 303  * Method:    delMenu
 304  * Signature: (I)V
 305  */
 306 JNIEXPORT void JNICALL
 307 Java_sun_awt_windows_WMenuBarPeer_delMenu(JNIEnv *env, jobject self,
 308                                           jint index)
 309 {
 310     TRY;
 311 
 312     PDATA pData;
 313     JNI_CHECK_PEER_RETURN(self);
 314     AwtObject::WinThreadExec(self, AwtMenuBar::MENUBAR_DELITEM, (LPARAM)index);
 315 
 316     CATCH_BAD_ALLOC;
 317 }
 318 
 319 /*
 320  * Class:     sun_awt_windows_WMenuBarPeer
 321  * Method:    create
 322  * Signature: (Lsun/awt/windows/WFramePeer;)V
 323  */
 324 JNIEXPORT void JNICALL
 325 Java_sun_awt_windows_WMenuBarPeer_create(JNIEnv *env, jobject self,
 326                                          jobject frame)
 327 {
 328     TRY;
 329 
 330     AwtToolkit::CreateComponent(self, frame,
 331                                 (AwtToolkit::ComponentFactory)
 332                                 AwtMenuBar::Create);
 333     PDATA pData;
 334     JNI_CHECK_PEER_CREATION_RETURN(self);
 335 
 336     CATCH_BAD_ALLOC;
 337 }
 338 
 339 } /* extern "C" */