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