1 /* 2 * Copyright (c) 2011, 2019, 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 package com.apple.laf; 27 28 import java.awt.*; 29 import java.security.AccessController; 30 31 import javax.swing.*; 32 import javax.swing.plaf.ComponentUI; 33 import javax.swing.plaf.basic.BasicMenuBarUI; 34 35 import sun.lwawt.macosx.LWCToolkit; 36 import sun.security.action.GetBooleanAction; 37 38 // MenuBar implementation for Mac L&F 39 public class AquaMenuBarUI extends BasicMenuBarUI implements ScreenMenuBarProvider { 40 41 static { 42 jdk.internal.access.SharedSecrets.getJavaLangAccess().loadLibrary("osxui"); 43 } 44 45 // Utilities 46 public void uninstallUI(final JComponent c) { 47 if (fScreenMenuBar != null) { 48 final JFrame frame = (JFrame)(c.getTopLevelAncestor()); 49 if (frame != null && frame.getMenuBar() == fScreenMenuBar) { 50 frame.setMenuBar((MenuBar)null); 51 } 52 fScreenMenuBar = null; 53 } 54 super.uninstallUI(c); 55 } 56 57 // Create PLAF 58 public static ComponentUI createUI(final JComponent c) { 59 return new AquaMenuBarUI(); 60 } 61 62 // [3320390] -- If the screen menu bar is in use, don't register keyboard actions that 63 // show the menus when F10 is pressed. 64 protected void installKeyboardActions() { 65 if (!useScreenMenuBar) { 66 super.installKeyboardActions(); 67 } 68 } 69 70 protected void uninstallKeyboardActions() { 71 if (!useScreenMenuBar) { 72 super.uninstallKeyboardActions(); 73 } 74 } 75 76 // Paint Methods 77 public void paint(final Graphics g, final JComponent c) { 78 AquaMenuPainter.instance().paintMenuBarBackground(g, c.getWidth(), c.getHeight(), c); 79 } 80 81 public Dimension getPreferredSize(final JComponent c) { 82 if (isScreenMenuBar((JMenuBar)c)) { 83 if (setScreenMenuBar((JFrame)(c.getTopLevelAncestor()))) { 84 return new Dimension(0, 0); 85 } 86 } 87 return null; 88 } 89 90 void clearScreenMenuBar(final JFrame frame) { 91 if (useScreenMenuBar) { 92 frame.setMenuBar(null); 93 } 94 } 95 96 boolean setScreenMenuBar(final JFrame frame) { 97 if (useScreenMenuBar) { 98 try { 99 getScreenMenuBar(); 100 } catch(final Throwable t) { 101 return false; 102 } 103 104 frame.setMenuBar(fScreenMenuBar); 105 } 106 107 return true; 108 } 109 110 public ScreenMenuBar getScreenMenuBar() { 111 // Lazy init of member variables means we should use a synchronized block. 112 synchronized(this) { 113 if (fScreenMenuBar == null) fScreenMenuBar = new ScreenMenuBar(this.menuBar); 114 } 115 return fScreenMenuBar; 116 } 117 118 // JMenuBars are in frame unless we're using ScreenMenuBars *and* it's 119 // been set by JFrame.setJMenuBar 120 // unless the JFrame has a normal java.awt.MenuBar (it's possible!) 121 // Other JMenuBars appear where the programmer puts them - top of window or elsewhere 122 public static final boolean isScreenMenuBar(final JMenuBar c) { 123 final javax.swing.plaf.ComponentUI ui = c.getUI(); 124 if (ui instanceof AquaMenuBarUI) { 125 if (!((AquaMenuBarUI)ui).useScreenMenuBar) return false; 126 127 final Component parent = c.getTopLevelAncestor(); 128 if (parent instanceof JFrame) { 129 final MenuBar mb = ((JFrame)parent).getMenuBar(); 130 final boolean thisIsTheJMenuBar = (((JFrame)parent).getJMenuBar() == c); 131 if (mb == null) return thisIsTheJMenuBar; 132 return (mb instanceof ScreenMenuBar && thisIsTheJMenuBar); 133 } 134 } 135 return false; 136 } 137 138 ScreenMenuBar fScreenMenuBar; 139 boolean useScreenMenuBar = getScreenMenuBarProperty(); 140 141 public static boolean getScreenMenuBarProperty() { 142 // Do not allow AWT to set the screen menu bar if it's embedded in another UI toolkit 143 if (LWCToolkit.isEmbedded()) return false; 144 if (AccessController.doPrivileged( 145 new GetBooleanAction(AquaLookAndFeel.sPropertyPrefix + "useScreenMenuBar"))) { 146 return true; 147 } 148 if (AccessController.doPrivileged( 149 new GetBooleanAction(AquaLookAndFeel.sOldPropertyPrefix + "useScreenMenuBar"))) { 150 System.err.println(AquaLookAndFeel.sOldPropertyPrefix + 151 "useScreenMenuBar has been deprecated. Please switch to " + 152 AquaLookAndFeel.sPropertyPrefix + "useScreenMenuBar"); 153 return true; 154 } 155 return false; 156 } 157 }