CMenuBar.m 14.1 KB
Newer Older
1
/*
2
 * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

#import <AppKit/AppKit.h>
#import <JavaNativeFoundation/JavaNativeFoundation.h>
#import <JavaRuntimeSupport/JavaRuntimeSupport.h>


#import "CMenuBar.h"
#import "CMenu.h"
#import "ThreadUtilities.h"

#import "sun_lwawt_macosx_CMenuBar.h"

__attribute__((visibility("default")))
NSString *CMenuBarDidReuseItemNotification =
    @"CMenuBarDidReuseItemNotification";

static CMenuBar *sActiveMenuBar = nil;
static NSMenu *sDefaultHelpMenu = nil;
static BOOL sSetupHelpMenu = NO;

@interface CMenuBar (CMenuBar_Private)
+ (void) addDefaultHelpMenu;
@end

@implementation CMenuBar

+ (void)clearMenuBarExcludingAppleMenu_OnAppKitThread:(BOOL) excludingAppleMenu {
    AWT_ASSERT_APPKIT_THREAD;
    // Remove all Java menus from the main bar.
    NSMenu *theMainMenu = [NSApp mainMenu];
    NSUInteger i, menuCount = [theMainMenu numberOfItems];

    for (i = menuCount; i > 1; i--) {
        NSUInteger index = i-1;

        NSMenuItem *currItem = [theMainMenu itemAtIndex:index];
        NSMenu *currMenu = [currItem submenu];

        if (excludingAppleMenu && ![currMenu isJavaMenu]) {
            continue;
        }
66
        [currItem setSubmenu:nil];
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        [theMainMenu removeItemAtIndex:index];
    }

    [CMenuBar addDefaultHelpMenu];
}

+ (BOOL) isActiveMenuBar:(CMenuBar *)inMenuBar {
    return (sActiveMenuBar == inMenuBar);
}

- (id) initWithPeer:(jobject)peer {
    AWT_ASSERT_APPKIT_THREAD;
    self = [super initWithPeer: peer];
    if (self) {
        fMenuList = [[NSMutableArray alloc] init];
    }
    return self;
}

-(void) dealloc {
    [fMenuList release];
    fMenuList = nil;

    [fHelpMenu release];
    fHelpMenu = nil;

    [super dealloc];
}

+ (void) activate:(CMenuBar *)menubar modallyDisabled:(BOOL)modallyDisabled {
    AWT_ASSERT_APPKIT_THREAD;

    if (!menubar) {
        [CMenuBar clearMenuBarExcludingAppleMenu_OnAppKitThread:YES];
        return;
    }

    @synchronized([CMenuBar class]) {
        sActiveMenuBar = menubar;
    }

    @synchronized(menubar) {
        menubar->fModallyDisabled = modallyDisabled;
    }

    NSUInteger i = 0, newMenuListSize = [menubar->fMenuList count];

    NSMenu *theMainMenu = [NSApp mainMenu];
    NSUInteger menuIndex, menuCount = [theMainMenu numberOfItems];

    NSUInteger cmenuIndex = 0, cmenuCount = newMenuListSize;
    NSMutableArray *removedMenuArray = [NSMutableArray array];

    for (menuIndex = 0; menuIndex < menuCount; menuIndex++) {
        NSMenuItem *currItem = [theMainMenu itemAtIndex:menuIndex];
        NSMenu *currMenu = [currItem submenu];

        if ([currMenu isJavaMenu]) {
            // Ready to replace, find next candidate
            CMenu *newMenu = nil;
            if (cmenuIndex < cmenuCount) {
                newMenu = (CMenu *)[menubar->fMenuList objectAtIndex:cmenuIndex];
                if (newMenu == menubar->fHelpMenu) {
                    cmenuIndex++;
                    if (cmenuIndex < cmenuCount) {
                        newMenu = (CMenu *)[menubar->fMenuList objectAtIndex:cmenuIndex];
                    }
                }
            }
            if (newMenu) {
                NSMenu *menuToAdd = [newMenu menu];
                if ([theMainMenu indexOfItemWithSubmenu:menuToAdd] == -1) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:CMenuBarDidReuseItemNotification object:theMainMenu];

                    [currItem setSubmenu:menuToAdd];
                    [currItem setTitle:[menuToAdd title]];
                    cmenuIndex++;
                }

                BOOL newEnabledState = [newMenu isEnabled] && !menubar->fModallyDisabled;
                [currItem setEnabled:newEnabledState];
            } else {
                [removedMenuArray addObject:[NSNumber numberWithInteger:menuIndex]];
            }
        }
    }

    // Clean up extra items
    NSUInteger removedIndex, removedCount = [removedMenuArray count];
    for (removedIndex=removedCount; removedIndex > 0; removedIndex--) {
157 158 159 160
        NSUInteger index = [[removedMenuArray objectAtIndex:(removedIndex-1)] integerValue];
        NSMenuItem *currItem = [theMainMenu itemAtIndex:index];
        [currItem setSubmenu:nil];
        [theMainMenu removeItemAtIndex:index];
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    }

    i = cmenuIndex;

    // Add all of the menus in the menu list.
    for (; i < newMenuListSize; i++) {
        CMenu *newMenu = (CMenu *)[menubar->fMenuList objectAtIndex:i];

        if (newMenu != menubar->fHelpMenu) {
            NSArray *args = [NSArray arrayWithObjects:newMenu, [NSNumber numberWithInt:-1], nil];
            [menubar nativeAddMenuAtIndex_OnAppKitThread:args];
        }
    }

    // Add the help menu last.
    if (menubar->fHelpMenu) {
        NSArray *args = [NSArray arrayWithObjects:menubar->fHelpMenu, [NSNumber numberWithInt:-1], nil];
        [menubar nativeAddMenuAtIndex_OnAppKitThread:args];
    } else {
        [CMenuBar addDefaultHelpMenu];
    }
}

-(void) deactivate {
    AWT_ASSERT_APPKIT_THREAD;

    @synchronized([CMenuBar class]) {
        sActiveMenuBar = nil;
    }

    @synchronized(self) {
        fModallyDisabled = NO;
    }
}

-(void) javaAddMenu: (CMenu *)theMenu {
    @synchronized(self) {
        [fMenuList addObject: theMenu];
    }

    if (self == sActiveMenuBar) {
        NSArray *args = [[NSArray alloc] initWithObjects:theMenu, [NSNumber numberWithInt:-1], nil];
203
        [ThreadUtilities performOnMainThread:@selector(nativeAddMenuAtIndex_OnAppKitThread:) on:self withObject:args waitUntilDone:YES];
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        [args release];
    }
}

// This method is a special case for use by the screen menu bar.
// See ScreenMenuBar.java -- used to implement setVisible(boolean) by
// removing or adding the menu from the current menu bar's list.
-(void) javaAddMenu: (CMenu *)theMenu atIndex:(jint)index {
    @synchronized(self) {
        if (index == -1){
            [fMenuList addObject:theMenu];
        }else{
            [fMenuList insertObject:theMenu atIndex:index];
        }
    }

    if (self == sActiveMenuBar) {
        NSArray *args = [[NSArray alloc] initWithObjects:theMenu, [NSNumber numberWithInt:index], nil];
222
        [ThreadUtilities performOnMainThread:@selector(nativeAddMenuAtIndex_OnAppKitThread:) on:self withObject:args waitUntilDone:YES];
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        [args release];
    }
}

- (NSInteger) javaIndexToNSMenuIndex_OnAppKitThread:(jint)javaIndex {
    AWT_ASSERT_APPKIT_THREAD;
    NSInteger returnValue = -1;
    NSMenu *theMainMenu = [NSApp mainMenu];

    if (javaIndex == -1) {
        if (fHelpMenu) {
            returnValue = [theMainMenu indexOfItemWithSubmenu:[fHelpMenu menu]];
        }
    } else {
        CMenu *requestedMenu = [fMenuList objectAtIndex:javaIndex];

        if (requestedMenu == fHelpMenu) {
            returnValue = [theMainMenu indexOfItemWithSubmenu:[fHelpMenu menu]];
        } else {
            NSUInteger i, menuCount = [theMainMenu numberOfItems];
            jint currJavaMenuIndex = 0;
            for (i = 0; i < menuCount; i++) {
                NSMenuItem *currItem = [theMainMenu itemAtIndex:i];
                NSMenu *currMenu = [currItem submenu];

                if ([currMenu isJavaMenu]) {
                    if (javaIndex == currJavaMenuIndex) {
                        returnValue = i;
                        break;
                    }

                    currJavaMenuIndex++;
                }
            }
        }
    }

    return returnValue;
}

- (void) nativeAddMenuAtIndex_OnAppKitThread:(NSArray *)args {
    AWT_ASSERT_APPKIT_THREAD;
    CMenu *theNewMenu = (CMenu*)[args objectAtIndex:0];
    jint index = [(NSNumber*)[args objectAtIndex:1] intValue];
    NSApplication *theApp = [NSApplication sharedApplication];
    NSMenu *theMainMenu = [theApp mainMenu];
    NSMenu *menuToAdd = [theNewMenu menu];

    if ([theMainMenu indexOfItemWithSubmenu:menuToAdd] == -1) {
        NSMenuItem *newItem = [[NSMenuItem alloc] init];
        [newItem setSubmenu:[theNewMenu menu]];
        [newItem setTitle:[[theNewMenu menu] title]];

        NSInteger nsMenuIndex = [self javaIndexToNSMenuIndex_OnAppKitThread:index];

        if (nsMenuIndex == -1) {
            [theMainMenu addItem:newItem];
        } else {
            [theMainMenu insertItem:newItem atIndex:nsMenuIndex];
        }

        BOOL newEnabledState = [theNewMenu isEnabled] && !fModallyDisabled;
        [newItem setEnabled:newEnabledState];
        [newItem release];
    }
}

- (void) javaDeleteMenu: (jint)index {
    if (self == sActiveMenuBar) {
292
        [ThreadUtilities performOnMainThread:@selector(nativeDeleteMenu_OnAppKitThread:) on:self withObject:[NSNumber numberWithInt:index] waitUntilDone:YES];
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    }

    @synchronized(self) {
        CMenu *menuToRemove = [fMenuList objectAtIndex:index];

        if (menuToRemove == fHelpMenu) {
            [fHelpMenu release];
            fHelpMenu = nil;
        }

        [fMenuList removeObjectAtIndex:index];
    }
}

- (void) nativeDeleteMenu_OnAppKitThread:(id)indexObj {
    AWT_ASSERT_APPKIT_THREAD;
    NSApplication *theApp = [NSApplication sharedApplication];
    NSMenu *theMainMenu = [theApp mainMenu];
    jint menuToRemove = [(NSNumber *)indexObj intValue];
    NSInteger nsMenuToRemove = [self javaIndexToNSMenuIndex_OnAppKitThread:menuToRemove];

    if (nsMenuToRemove != -1) {
        [theMainMenu removeItemAtIndex:nsMenuToRemove];
    }
}

- (void) javaSetHelpMenu:(CMenu *)theMenu {
    @synchronized(self) {
        [theMenu retain];
        [fHelpMenu release];
        fHelpMenu = theMenu;
    }
}

+ (void) addDefaultHelpMenu {
    AWT_ASSERT_APPKIT_THREAD;

    // Look for a help book tag. If it's there, add the help menu.
    @synchronized ([CMenuBar class]) {
        if (!sSetupHelpMenu) {
            if (sDefaultHelpMenu == nil) {
                // If we are embedded, don't make a help menu.
                // TODO(cpc): we don't have NSApplicationAWT yet...
                //if (![NSApp isKindOfClass:[NSApplicationAWT class]]) {
                //    sSetupHelpMenu = YES;
                //    return;
                //}

                // If the developer specified a NIB, don't make a help menu.
                // TODO(cpc): usingDefaultNib only defined on NSApplicationAWT
                //if (![NSApp usingDefaultNib]) {
                //    sSetupHelpMenu = YES;
                //    return;
                //}

            // TODO: not implemented
            }

            sSetupHelpMenu = YES;
        }
    }

    if (sDefaultHelpMenu) {
        NSMenu *theMainMenu = [NSApp mainMenu];

        if ([theMainMenu indexOfItemWithSubmenu:sDefaultHelpMenu] == -1) {
            // Since we're re-using this NSMenu, we need to clear its parent before
            // adding it to a new menu item, or else AppKit will complain.
            [sDefaultHelpMenu setSupermenu:nil];

            // Add the help menu to the main menu.
            NSMenuItem *newItem = [[NSMenuItem alloc] init];
            [newItem setSubmenu:sDefaultHelpMenu];
            [newItem setTitle:[sDefaultHelpMenu title]];
            [theMainMenu addItem:newItem];

            // Release it so the main menu owns it.
            [newItem release];
        }
    }
}

@end

/*
 * Class:     sun_lwawt_macosx_CMenuBar
 * Method:    nativeCreateMenuBar
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL
Java_sun_lwawt_macosx_CMenuBar_nativeCreateMenuBar
    (JNIEnv *env, jobject peer)
{
386
    __block CMenuBar *aCMenuBar = nil;
387 388 389 390
    JNF_COCOA_ENTER(env);

    jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer);

391
    [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
392

393 394 395
        aCMenuBar = [[CMenuBar alloc] initWithPeer:cPeerObjGlobal];
        // the aCMenuBar is released in CMenuComponent.dispose()
    }];
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    if (aCMenuBar == nil) {
        return 0L;
    }

    JNF_COCOA_EXIT(env);
    if (aCMenuBar) {
        CFRetain(aCMenuBar); // GC
        [aCMenuBar release];
    }
    return ptr_to_jlong(aCMenuBar);
}

/*
 * Class:     sun_lwawt_macosx_CMenuBar
 * Method:    nativeAddAtIndex
 * Signature: (JJI)V
 */
JNIEXPORT void JNICALL
Java_sun_lwawt_macosx_CMenuBar_nativeAddAtIndex
    (JNIEnv *env, jobject peer,
     jlong menuBarObject, jlong menuObject, jint index)
{
    JNF_COCOA_ENTER(env);
    // Remove the specified item.
    [((CMenuBar *) jlong_to_ptr(menuBarObject)) javaAddMenu:(CMenu *) jlong_to_ptr(menuObject) atIndex:index];
    JNF_COCOA_EXIT(env);
}

/*
 * Class:     sun_lwawt_macosx_CMenuBar
 * Method:    nativeDelMenu
 * Signature: (JI)V
 */
JNIEXPORT void JNICALL
Java_sun_lwawt_macosx_CMenuBar_nativeDelMenu
    (JNIEnv *env, jobject peer, jlong menuBarObject, jint index)
{
    JNF_COCOA_ENTER(env);
    // Remove the specified item.
    [((CMenuBar *) jlong_to_ptr(menuBarObject)) javaDeleteMenu: index];
    JNF_COCOA_EXIT(env);
}

/*
 * Class:     sun_lwawt_macosx_CMenuBar
 * Method:    nativeSetHelpMenu
 * Signature: (JJ)V
 */
JNIEXPORT void JNICALL
Java_sun_lwawt_macosx_CMenuBar_nativeSetHelpMenu
    (JNIEnv *env, jobject peer, jlong menuBarObject, jlong menuObject)
{
    JNF_COCOA_ENTER(env);
    // Remove the specified item.
    [((CMenuBar *) jlong_to_ptr(menuBarObject)) javaSetHelpMenu: ((CMenu *)jlong_to_ptr(menuObject))];
    JNF_COCOA_EXIT(env);
}