J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1995-2003 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | package sun.awt.motif; |
| 26 | |
| 27 | import java.awt.*; |
| 28 | import java.awt.peer.*; |
| 29 | import sun.awt.*; |
| 30 | |
| 31 | public class MMenuBarPeer implements MenuBarPeer { |
| 32 | long pData; |
| 33 | MenuBar target; |
| 34 | private X11GraphicsConfig graphicsConfig=null; |
| 35 | |
| 36 | private boolean disposed = false; |
| 37 | |
| 38 | static { |
| 39 | initIDs(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Initialize JNI field and method IDs for fields that may be accessed |
| 44 | from C. |
| 45 | */ |
| 46 | private static native void initIDs(); |
| 47 | |
| 48 | native void create(MFramePeer f); |
| 49 | |
| 50 | public MMenuBarPeer(MenuBar target) { |
| 51 | this.target = target; |
| 52 | MFramePeer parent = (MFramePeer) MToolkit.targetToPeer(MMenuItemPeer.getParent_NoClientCode(target)); |
| 53 | create(parent); |
| 54 | } |
| 55 | |
| 56 | protected void finalize() throws Throwable { |
| 57 | dispose(); |
| 58 | super.finalize(); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Subclasses should override disposeImpl() instead of dispose(). Client |
| 63 | * code should always invoke dispose(), never disposeImpl(). |
| 64 | */ |
| 65 | private native void pDispose(); |
| 66 | protected void disposeImpl() { |
| 67 | MToolkit.targetDisposedPeer(target, this); |
| 68 | pDispose(); |
| 69 | } |
| 70 | public final void dispose() { |
| 71 | boolean call_disposeImpl = false; |
| 72 | |
| 73 | if (!disposed) { |
| 74 | synchronized (this) { |
| 75 | if (!disposed) { |
| 76 | disposed = call_disposeImpl = true; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (call_disposeImpl) { |
| 82 | disposeImpl(); |
| 83 | } |
| 84 | } |
| 85 | public void addMenu(Menu m) { |
| 86 | } |
| 87 | public void delMenu(int index) { |
| 88 | } |
| 89 | public void addHelpMenu(Menu m) { |
| 90 | } |
| 91 | |
| 92 | static final int GAP = 10; |
| 93 | static final int W_DIFF = (MFramePeer.CROSSHAIR_INSET + 1) * 2; |
| 94 | static final int H_DIFF = MFramePeer.BUTTON_Y + MFramePeer.BUTTON_H; |
| 95 | |
| 96 | /* |
| 97 | * Print the native component by rendering the Motif look ourselves. |
| 98 | * ToDo(aim): needs to query native motif for more appropriate size and |
| 99 | * color information. |
| 100 | */ |
| 101 | void print(Graphics g) { |
| 102 | MenuBar mb = (MenuBar)target; |
| 103 | Frame f = (Frame)MMenuItemPeer.getParent_NoClientCode(target); |
| 104 | Dimension fd = f.size(); |
| 105 | Insets insets = f.insets(); |
| 106 | |
| 107 | /* Calculate menubar dimension. */ |
| 108 | int width = fd.width; |
| 109 | int height = insets.top; |
| 110 | if (f.getPeer() instanceof MFramePeer) { |
| 111 | MFramePeer fpeer = (MFramePeer)f.getPeer(); |
| 112 | if (fpeer.hasDecorations(MWindowAttributes.AWT_DECOR_BORDER)) { |
| 113 | width -= W_DIFF; |
| 114 | height -= MFramePeer.BUTTON_Y; |
| 115 | } |
| 116 | if (fpeer.hasDecorations(MWindowAttributes.AWT_DECOR_MENU)) { |
| 117 | height -= MFramePeer.BUTTON_H; |
| 118 | } |
| 119 | } |
| 120 | Dimension d = new Dimension(width, height); |
| 121 | |
| 122 | Shape oldClipArea = g.getClip(); |
| 123 | g.clipRect(0, 0, d.width, d.height); |
| 124 | |
| 125 | Color bg = f.getBackground(); |
| 126 | Color fg = f.getForeground(); |
| 127 | Color highlight = bg.brighter(); |
| 128 | Color shadow = bg.darker(); |
| 129 | |
| 130 | // because we'll most likely be drawing on white paper, |
| 131 | // for aesthetic reasons, don't make any part of the outer border |
| 132 | // pure white |
| 133 | if (highlight.equals(Color.white)) { |
| 134 | g.setColor(new Color(230, 230, 230)); |
| 135 | } |
| 136 | else { |
| 137 | g.setColor(highlight); |
| 138 | } |
| 139 | g.drawLine(0, 0, d.width, 0); |
| 140 | g.drawLine(1, 1, d.width - 1, 1); |
| 141 | g.drawLine(0, 0, 0, d.height); |
| 142 | g.drawLine(1, 1, 1, d.height - 1); |
| 143 | g.setColor(shadow); |
| 144 | g.drawLine(d.width, 1, d.width, d.height); |
| 145 | g.drawLine(d.width - 1, 2, d.width - 1, d.height); |
| 146 | g.drawLine(1, d.height, d.width, d.height); |
| 147 | g.drawLine(2, d.height - 1, d.width, d.height - 1); |
| 148 | |
| 149 | int x = GAP; |
| 150 | int nitems = mb.countMenus(); |
| 151 | |
| 152 | Menu helpMenu = target.getHelpMenu(); |
| 153 | |
| 154 | for (int i = 0 ; i < nitems ; i++) { |
| 155 | Menu mn = target.getMenu(i); |
| 156 | String item = mn.getLabel(); |
| 157 | if (item == null) { |
| 158 | item = ""; |
| 159 | } |
| 160 | Font menuFont = mn.getFont(); |
| 161 | g.setFont(menuFont); |
| 162 | FontMetrics menuMetrics = g.getFontMetrics(); |
| 163 | int y = (d.height / 2) + menuMetrics.getMaxDescent(); |
| 164 | int w = menuMetrics.stringWidth(item) + GAP * 2; |
| 165 | |
| 166 | if (x >= d.width) { |
| 167 | break; |
| 168 | } |
| 169 | if (mn.isEnabled()) { |
| 170 | g.setColor(fg); |
| 171 | } |
| 172 | else { |
| 173 | // draw text as grayed out |
| 174 | g.setColor(shadow); |
| 175 | } |
| 176 | |
| 177 | if (helpMenu == mn) { |
| 178 | g.drawString(item, d.width - w + GAP, y); |
| 179 | } |
| 180 | else { |
| 181 | g.drawString(item, x, y); |
| 182 | x += w; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | g.setClip(oldClipArea); |
| 187 | } |
| 188 | |
| 189 | // Needed for MenuComponentPeer. |
| 190 | public void setFont(Font f) { |
| 191 | } |
| 192 | } |