1 /*
   2  * Copyright (c) 2011, 2018, 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 <Carbon/Carbon.h>
  27 #import "CMenuItem.h"
  28 #import "CMenu.h"
  29 #import "AWTEvent.h"
  30 #import "AWTWindow.h"
  31 #import "ThreadUtilities.h"
  32 #import "JNIUtilities.h"
  33 
  34 #import "java_awt_Event.h"
  35 #import "java_awt_event_KeyEvent.h"
  36 #import "sun_lwawt_macosx_CMenuItem.h"
  37 
  38 #define NOT_A_CHECKBOXMENU -2
  39 
  40 
  41 @implementation CMenuItem
  42 
  43 - (id) initWithPeer:(jobject)peer asSeparator: (BOOL) asSeparator{
  44     AWT_ASSERT_APPKIT_THREAD;
  45     self = [super initWithPeer:peer];
  46     if (self) {
  47         if (asSeparator) {
  48             fMenuItem = (NSMenuItem*)[NSMenuItem separatorItem];
  49             [fMenuItem retain];
  50         } else {
  51             fMenuItem = [[NSMenuItem alloc] init];
  52             [fMenuItem setAction:@selector(handleAction:)];
  53             [fMenuItem setTarget:self];
  54         }
  55         fIsCheckbox = NO;
  56         fIsEnabled = YES;
  57     }
  58     return self;
  59 }
  60 
  61 // This is because NSApplication doesn't check the target's window when sending
  62 // actions; they only check the target itself.  We always return YES,
  63 // since we shouldn't even be installed unless our window is active.
  64 - (BOOL) worksWhenModal {
  65     return YES;
  66 }
  67 
  68 // Events
  69 - (void)handleAction:(NSMenuItem *)sender {
  70     AWT_ASSERT_APPKIT_THREAD;
  71     JNIEnv *env = [ThreadUtilities getJNIEnv];
  72     JNI_COCOA_ENTER(env);
  73     
  74     // If we are called as a result of user pressing a shortcut, do nothing,
  75     // because AVTView has already sent corresponding key event to the Java
  76     // layer from performKeyEquivalent.
  77     // There is an exception from the rule above, though: if a window with
  78     // a menu gets minimized by user and there are no other windows to take
  79     // focus, the window's menu won't be removed from the global menu bar.
  80     // However, the Java layer won't handle invocation by a shortcut coming
  81     // from this "frameless" menu, because there are no active windows. This
  82     // means we have to handle it here.
  83     NSEvent *currEvent = [[NSApplication sharedApplication] currentEvent];
  84     if (fIsCheckbox) {
  85         DECLARE_CLASS(jc_CCheckboxMenuItem, "sun/lwawt/macosx/CCheckboxMenuItem");
  86         DECLARE_METHOD(jm_ckHandleAction, jc_CCheckboxMenuItem, "handleAction", "(Z)V");
  87         
  88         // Send the opposite of what's currently checked -- the action
  89         // indicates what state we're going to.
  90         NSInteger state = [sender state];
  91         jboolean newState = (state == NSOnState ? JNI_FALSE : JNI_TRUE);
  92         (*env)->CallVoidMethod(env, fPeer, jm_ckHandleAction, newState);
  93     }
  94     else {
  95         if ([currEvent type] == NSKeyDown) {
  96             // Event available through sender variable hence NSApplication
  97             // not needed for checking the keyboard input sans the modifier keys
  98             // Also, the method used to fetch eventKey earlier would be locale dependent
  99             // With earlier implementation, if MenuKey: e EventKey: ा ; if input method
 100             // is not U.S. (Devanagari in this case)
 101             // With current implementation, EventKey = MenuKey = e irrespective of
 102             // input method
 103             NSString *eventKey = [sender keyEquivalent];
 104             // Apple uses characters from private Unicode range for some of the
 105             // keys, so we need to do the same translation here that we do
 106             // for the regular key down events
 107             if ([eventKey length] == 1) {
 108                 unichar origChar = [eventKey characterAtIndex:0];
 109                 unichar newChar =  NsCharToJavaChar(origChar, 0);
 110                 if (newChar == java_awt_event_KeyEvent_CHAR_UNDEFINED) {
 111                     newChar = origChar;
 112                 }
 113                 eventKey = [NSString stringWithCharacters: &newChar length: 1];
 114             }
 115             // The action event can be ignored only if the key window is an AWT window.
 116             // Otherwise, the action event is the only notification and must be processed.
 117             NSWindow *keyWindow = [NSApp keyWindow];
 118             if (keyWindow != nil && [AWTWindow isAWTWindow: keyWindow]) {
 119                 return;
 120             }
 121         }
 122         
 123         DECLARE_CLASS(jc_CMenuItem, "sun/lwawt/macosx/CMenuItem");
 124         DECLARE_METHOD(jm_handleAction, jc_CMenuItem, "handleAction", "(JI)V"); // AWT_THREADING Safe (event)
 125 
 126         NSUInteger modifiers = [currEvent modifierFlags];
 127         jint javaModifiers = NsKeyModifiersToJavaModifiers(modifiers, NO);
 128 
 129         (*env)->CallVoidMethod(env, fPeer, jm_handleAction, UTC(currEvent), javaModifiers); // AWT_THREADING Safe (event)
 130     }
 131     CHECK_EXCEPTION();
 132     JNI_COCOA_EXIT(env);
 133     
 134 }
 135 
 136 - (void) setJavaLabel:(NSString *)theLabel shortcut:(NSString *)theKeyEquivalent modifierMask:(jint)modifiers {
 137 
 138     NSUInteger modifierMask = 0;
 139 
 140     if (![theKeyEquivalent isEqualToString:@""]) {
 141         // Force the key equivalent to lower case if not using the shift key.
 142         // Otherwise AppKit will draw a Shift glyph in the menu.
 143         if ((modifiers & java_awt_event_KeyEvent_SHIFT_MASK) == 0) {
 144             theKeyEquivalent = [theKeyEquivalent lowercaseString];
 145         }
 146 
 147         // Hack for the question mark -- SHIFT and / means use the question mark.
 148         if ((modifiers & java_awt_event_KeyEvent_SHIFT_MASK) != 0 &&
 149             [theKeyEquivalent isEqualToString:@"/"])
 150         {
 151             theKeyEquivalent = @"?";
 152             modifiers &= ~java_awt_event_KeyEvent_SHIFT_MASK;
 153         }
 154 
 155         modifierMask = JavaModifiersToNsKeyModifiers(modifiers, NO);
 156     }
 157 
 158     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 159         [fMenuItem setKeyEquivalent:theKeyEquivalent];
 160         [fMenuItem setKeyEquivalentModifierMask:modifierMask];
 161         [fMenuItem setTitle:theLabel];
 162     }];
 163 }
 164 
 165 - (void) setJavaImage:(NSImage *)theImage {
 166 
 167     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
 168         [fMenuItem setImage:theImage];
 169     }];
 170 }
 171 
 172 - (void) setJavaToolTipText:(NSString *)theText {
 173 
 174     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
 175         [fMenuItem setToolTip:theText];
 176     }];
 177 }
 178 
 179 
 180 - (void)setJavaEnabled:(BOOL) enabled {
 181 
 182     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
 183         @synchronized(self) {
 184             fIsEnabled = enabled;
 185 
 186             // Warning:  This won't work if the parent menu is disabled.
 187             // See [CMenu syncFromJava]. We still need to call it here so
 188             // the NSMenuItem itself gets properly updated.
 189             [fMenuItem setEnabled:fIsEnabled];
 190         }
 191     }];
 192 }
 193 
 194 - (BOOL)isEnabled {
 195 
 196     BOOL enabled = NO;
 197     @synchronized(self) {
 198         enabled = fIsEnabled;
 199     }
 200     return enabled;
 201 }
 202 
 203 
 204 - (void)setJavaState:(BOOL)newState {
 205 
 206     [ThreadUtilities performOnMainThreadWaiting:NO block:^(){
 207         [fMenuItem setState:(newState ? NSOnState : NSOffState)];
 208     }];
 209 }
 210 
 211 - (void)dealloc {
 212     [fMenuItem setAction:NULL];
 213     [fMenuItem setTarget:nil];
 214     [fMenuItem release];
 215     fMenuItem = nil;
 216 
 217     [super dealloc];
 218 }
 219 
 220 - (void)addNSMenuItemToMenu:(NSMenu *)inMenu {
 221     [inMenu addItem:fMenuItem];
 222 }
 223 
 224 - (NSMenuItem *)menuItem {
 225     return [[fMenuItem retain] autorelease];
 226 }
 227 
 228 - (void)setIsCheckbox {
 229     fIsCheckbox = YES;
 230 }
 231 
 232 - (NSString *)description {
 233     return [NSString stringWithFormat:@"CMenuItem[ %@ ]", fMenuItem];
 234 }
 235 
 236 @end
 237 
 238 /** Convert a Java keycode for SetMenuItemCmd */
 239 static unichar AWTKeyToMacShortcut(jint awtKey, BOOL doShift) {
 240     unichar macKey = 0;
 241 
 242     if ((awtKey >= java_awt_event_KeyEvent_VK_0 && awtKey <= java_awt_event_KeyEvent_VK_9) ||
 243         (awtKey >= java_awt_event_KeyEvent_VK_A && awtKey <= java_awt_event_KeyEvent_VK_Z))
 244     {
 245         // These ranges are the same in ASCII
 246         macKey = awtKey;
 247     } else if (awtKey >= java_awt_event_KeyEvent_VK_F1 && awtKey <= java_awt_event_KeyEvent_VK_F12) {
 248         // Support for F1 - F12 has been around since Java 1.0 and fall into a lower range.
 249         macKey = awtKey - java_awt_event_KeyEvent_VK_F1 + NSF1FunctionKey;
 250     } else if (awtKey >= java_awt_event_KeyEvent_VK_F13 && awtKey <= java_awt_event_KeyEvent_VK_F24) {
 251         // Support for F13-F24 came in Java 1.2 and are at a different range.
 252         macKey = awtKey - java_awt_event_KeyEvent_VK_F13 + NSF13FunctionKey;
 253     } else {
 254         // Special characters
 255         switch (awtKey) {
 256             case java_awt_event_KeyEvent_VK_BACK_QUOTE      : macKey = '`'; break;
 257             case java_awt_event_KeyEvent_VK_QUOTE           : macKey = '\''; break;
 258 
 259             case java_awt_event_KeyEvent_VK_ESCAPE          : macKey = 0x1B; break;
 260             case java_awt_event_KeyEvent_VK_SPACE           : macKey = ' '; break;
 261             case java_awt_event_KeyEvent_VK_PAGE_UP         : macKey = NSPageUpFunctionKey; break;
 262             case java_awt_event_KeyEvent_VK_PAGE_DOWN       : macKey = NSPageDownFunctionKey; break;
 263             case java_awt_event_KeyEvent_VK_END             : macKey = NSEndFunctionKey; break;
 264             case java_awt_event_KeyEvent_VK_HOME            : macKey = NSHomeFunctionKey; break;
 265 
 266             case java_awt_event_KeyEvent_VK_LEFT            : macKey = NSLeftArrowFunctionKey; break;
 267             case java_awt_event_KeyEvent_VK_UP              : macKey = NSUpArrowFunctionKey; break;
 268             case java_awt_event_KeyEvent_VK_RIGHT           : macKey = NSRightArrowFunctionKey; break;
 269             case java_awt_event_KeyEvent_VK_DOWN            : macKey = NSDownArrowFunctionKey; break;
 270 
 271             case java_awt_event_KeyEvent_VK_COMMA           : macKey = ','; break;
 272 
 273                 // Mac OS doesn't distinguish between the two '-' keys...
 274             case java_awt_event_KeyEvent_VK_MINUS           :
 275             case java_awt_event_KeyEvent_VK_SUBTRACT        : macKey = '-'; break;
 276 
 277                 // or the two '.' keys...
 278             case java_awt_event_KeyEvent_VK_DECIMAL         :
 279             case java_awt_event_KeyEvent_VK_PERIOD          : macKey = '.'; break;
 280 
 281                 // or the two '/' keys.
 282             case java_awt_event_KeyEvent_VK_DIVIDE          :
 283             case java_awt_event_KeyEvent_VK_SLASH           : macKey = '/'; break;
 284 
 285             case java_awt_event_KeyEvent_VK_SEMICOLON       : macKey = ';'; break;
 286             case java_awt_event_KeyEvent_VK_EQUALS          : macKey = '='; break;
 287 
 288             case java_awt_event_KeyEvent_VK_OPEN_BRACKET    : macKey = '['; break;
 289             case java_awt_event_KeyEvent_VK_BACK_SLASH      : macKey = '\\'; break;
 290             case java_awt_event_KeyEvent_VK_CLOSE_BRACKET   : macKey = ']'; break;
 291 
 292             case java_awt_event_KeyEvent_VK_MULTIPLY        : macKey = '*'; break;
 293             case java_awt_event_KeyEvent_VK_ADD             : macKey = '+'; break;
 294 
 295             case java_awt_event_KeyEvent_VK_HELP            : macKey = NSHelpFunctionKey; break;
 296             case java_awt_event_KeyEvent_VK_TAB             : macKey = NSTabCharacter; break;
 297             case java_awt_event_KeyEvent_VK_ENTER           : macKey = NSNewlineCharacter; break;
 298             case java_awt_event_KeyEvent_VK_BACK_SPACE      : macKey = NSBackspaceCharacter; break;
 299             case java_awt_event_KeyEvent_VK_DELETE          : macKey = NSDeleteCharacter; break;
 300             case java_awt_event_KeyEvent_VK_CLEAR           : macKey = NSClearDisplayFunctionKey; break;
 301             case java_awt_event_KeyEvent_VK_AMPERSAND       : macKey = '&'; break;
 302             case java_awt_event_KeyEvent_VK_ASTERISK        : macKey = '*'; break;
 303             case java_awt_event_KeyEvent_VK_QUOTEDBL        : macKey = '\"'; break;
 304             case java_awt_event_KeyEvent_VK_LESS            : macKey = '<'; break;
 305             case java_awt_event_KeyEvent_VK_GREATER         : macKey = '>'; break;
 306             case java_awt_event_KeyEvent_VK_BRACELEFT       : macKey = '{'; break;
 307             case java_awt_event_KeyEvent_VK_BRACERIGHT      : macKey = '}'; break;
 308             case java_awt_event_KeyEvent_VK_AT              : macKey = '@'; break;
 309             case java_awt_event_KeyEvent_VK_COLON           : macKey = ':'; break;
 310             case java_awt_event_KeyEvent_VK_CIRCUMFLEX      : macKey = '^'; break;
 311             case java_awt_event_KeyEvent_VK_DOLLAR          : macKey = '$'; break;
 312             case java_awt_event_KeyEvent_VK_EXCLAMATION_MARK : macKey = '!'; break;
 313             case java_awt_event_KeyEvent_VK_LEFT_PARENTHESIS : macKey = '('; break;
 314             case java_awt_event_KeyEvent_VK_NUMBER_SIGN     : macKey = '#'; break;
 315             case java_awt_event_KeyEvent_VK_PLUS            : macKey = '+'; break;
 316             case java_awt_event_KeyEvent_VK_RIGHT_PARENTHESIS: macKey = ')'; break;
 317             case java_awt_event_KeyEvent_VK_UNDERSCORE      : macKey = '_'; break;
 318         }
 319     }
 320     return macKey;
 321 }
 322 
 323 /*
 324  * Class:     sun_lwawt_macosx_CMenuItem
 325  * Method:    nativeSetLabel
 326  * Signature: (JLjava/lang/String;CII)V
 327  */
 328 JNIEXPORT void JNICALL
 329 Java_sun_lwawt_macosx_CMenuItem_nativeSetLabel
 330 (JNIEnv *env, jobject peer,
 331  jlong menuItemObj, jstring label,
 332  jchar shortcutKey, jint shortcutKeyCode, jint mods)
 333 {
 334     JNI_COCOA_ENTER(env);
 335     NSString *theLabel = JavaStringToNSString(env, label);
 336     NSString *theKeyEquivalent = nil;
 337     unichar macKey = shortcutKey;
 338 
 339     if (macKey == 0) {
 340         macKey = AWTKeyToMacShortcut(shortcutKeyCode, (mods & java_awt_event_KeyEvent_SHIFT_MASK) != 0);
 341     }
 342 
 343     if (macKey != 0) {
 344         unichar equivalent[1] = {macKey};
 345         theKeyEquivalent = [NSString stringWithCharacters:equivalent length:1];
 346     } else {
 347         theKeyEquivalent = @"";
 348     }
 349 
 350     [((CMenuItem *)jlong_to_ptr(menuItemObj)) setJavaLabel:theLabel shortcut:theKeyEquivalent modifierMask:mods];
 351     JNI_COCOA_EXIT(env);
 352 }
 353 
 354 /*
 355  * Class:     sun_lwawt_macosx_CMenuItem
 356  * Method:    nativeSetTooltip
 357  * Signature: (JLjava/lang/String;)V
 358  */
 359 JNIEXPORT void JNICALL
 360 Java_sun_lwawt_macosx_CMenuItem_nativeSetTooltip
 361 (JNIEnv *env, jobject peer, jlong menuItemObj, jstring tooltip)
 362 {
 363     JNI_COCOA_ENTER(env);
 364     NSString *theTooltip = JavaStringToNSString(env, tooltip);
 365     [((CMenuItem *)jlong_to_ptr(menuItemObj)) setJavaToolTipText:theTooltip];
 366     JNI_COCOA_EXIT(env);
 367 }
 368 
 369 /*
 370  * Class:     sun_lwawt_macosx_CMenuItem
 371  * Method:    nativeSetImage
 372  * Signature: (JJ)V
 373  */
 374 JNIEXPORT void JNICALL
 375 Java_sun_lwawt_macosx_CMenuItem_nativeSetImage
 376 (JNIEnv *env, jobject peer, jlong menuItemObj, jlong image)
 377 {
 378     JNI_COCOA_ENTER(env);
 379     [((CMenuItem *)jlong_to_ptr(menuItemObj)) setJavaImage:(NSImage*)jlong_to_ptr(image)];
 380     JNI_COCOA_EXIT(env);
 381 }
 382 
 383 /*
 384  * Class:     sun_lwawt_macosx_CMenuItem
 385  * Method:    nativeCreate
 386  * Signature: (JZ)J
 387  */
 388 JNIEXPORT jlong JNICALL
 389 Java_sun_lwawt_macosx_CMenuItem_nativeCreate
 390 (JNIEnv *env, jobject peer, jlong parentCMenuObj, jboolean isSeparator)
 391 {
 392 
 393     __block CMenuItem *aCMenuItem = nil;
 394     BOOL asSeparator = (isSeparator == JNI_TRUE) ? YES: NO;
 395     CMenu *parentCMenu = (CMenu *)jlong_to_ptr(parentCMenuObj);
 396     JNI_COCOA_ENTER(env);
 397 
 398     jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer);
 399 
 400     [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
 401         aCMenuItem = [[CMenuItem alloc] initWithPeer: cPeerObjGlobal
 402                                          asSeparator: asSeparator];
 403         // the CMenuItem is released in CMenuComponent.dispose()
 404     }];
 405 
 406     if (aCMenuItem == nil) {
 407         return 0L;
 408     }
 409 
 410     // and add it to the parent item.
 411     [parentCMenu addJavaMenuItem: aCMenuItem];
 412 
 413     // setLabel will be called after creation completes.
 414 
 415     JNI_COCOA_EXIT(env);
 416     return ptr_to_jlong(aCMenuItem);
 417 }
 418 
 419 /*
 420  * Class:     sun_lwawt_macosx_CMenuItem
 421  * Method:    nativeSetEnabled
 422  * Signature: (JZ)V
 423  */
 424 JNIEXPORT void JNICALL
 425 Java_sun_lwawt_macosx_CMenuItem_nativeSetEnabled
 426 (JNIEnv *env, jobject peer, jlong menuItemObj, jboolean enable)
 427 {
 428     JNI_COCOA_ENTER(env);
 429     CMenuItem *item = (CMenuItem *)jlong_to_ptr(menuItemObj);
 430     [item setJavaEnabled: (enable == JNI_TRUE)];
 431     JNI_COCOA_EXIT(env);
 432 }
 433 
 434 /*
 435  * Class:     sun_lwawt_macosx_CCheckboxMenuItem
 436  * Method:    nativeSetState
 437  * Signature: (IZ)V
 438  */
 439 JNIEXPORT void JNICALL
 440 Java_sun_lwawt_macosx_CCheckboxMenuItem_nativeSetState
 441 (JNIEnv *env, jobject peer, jlong menuItemObj, jboolean state)
 442 {
 443     JNI_COCOA_ENTER(env);
 444     CMenuItem *item = (CMenuItem *)jlong_to_ptr(menuItemObj);
 445     [item setJavaState: (state == JNI_TRUE)];
 446     JNI_COCOA_EXIT(env);
 447 }
 448 
 449 /*
 450  * Class:     sun_lwawt_macosx_CCheckboxMenuItem
 451  * Method:    nativeSetState
 452  * Signature: (IZ)V
 453  */
 454 JNIEXPORT void JNICALL
 455 Java_sun_lwawt_macosx_CCheckboxMenuItem_nativeSetIsCheckbox
 456 (JNIEnv *env, jobject peer, jlong menuItemObj)
 457 {
 458     JNI_COCOA_ENTER(env);
 459     CMenuItem *item = (CMenuItem *)jlong_to_ptr(menuItemObj);
 460     [item setIsCheckbox];
 461     JNI_COCOA_EXIT(env);
 462 }