001: /*
002: * Copyright 1995-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package sun.awt.motif;
026:
027: import java.awt.*;
028: import java.awt.peer.*;
029: import sun.awt.*;
030:
031: public class MMenuBarPeer implements MenuBarPeer {
032: long pData;
033: MenuBar target;
034: private X11GraphicsConfig graphicsConfig = null;
035:
036: private boolean disposed = false;
037:
038: static {
039: initIDs();
040: }
041:
042: /**
043: * Initialize JNI field and method IDs for fields that may be accessed
044: from C.
045: */
046: private static native void initIDs();
047:
048: native void create(MFramePeer f);
049:
050: public MMenuBarPeer(MenuBar target) {
051: this .target = target;
052: MFramePeer parent = (MFramePeer) MToolkit
053: .targetToPeer(MMenuItemPeer
054: .getParent_NoClientCode(target));
055: create(parent);
056: }
057:
058: protected void finalize() throws Throwable {
059: dispose();
060: super .finalize();
061: }
062:
063: /*
064: * Subclasses should override disposeImpl() instead of dispose(). Client
065: * code should always invoke dispose(), never disposeImpl().
066: */
067: private native void pDispose();
068:
069: protected void disposeImpl() {
070: MToolkit.targetDisposedPeer(target, this );
071: pDispose();
072: }
073:
074: public final void dispose() {
075: boolean call_disposeImpl = false;
076:
077: if (!disposed) {
078: synchronized (this ) {
079: if (!disposed) {
080: disposed = call_disposeImpl = true;
081: }
082: }
083: }
084:
085: if (call_disposeImpl) {
086: disposeImpl();
087: }
088: }
089:
090: public void addMenu(Menu m) {
091: }
092:
093: public void delMenu(int index) {
094: }
095:
096: public void addHelpMenu(Menu m) {
097: }
098:
099: static final int GAP = 10;
100: static final int W_DIFF = (MFramePeer.CROSSHAIR_INSET + 1) * 2;
101: static final int H_DIFF = MFramePeer.BUTTON_Y + MFramePeer.BUTTON_H;
102:
103: /*
104: * Print the native component by rendering the Motif look ourselves.
105: * ToDo(aim): needs to query native motif for more appropriate size and
106: * color information.
107: */
108: void print(Graphics g) {
109: MenuBar mb = (MenuBar) target;
110: Frame f = (Frame) MMenuItemPeer.getParent_NoClientCode(target);
111: Dimension fd = f.size();
112: Insets insets = f.insets();
113:
114: /* Calculate menubar dimension. */
115: int width = fd.width;
116: int height = insets.top;
117: if (f.getPeer() instanceof MFramePeer) {
118: MFramePeer fpeer = (MFramePeer) f.getPeer();
119: if (fpeer
120: .hasDecorations(MWindowAttributes.AWT_DECOR_BORDER)) {
121: width -= W_DIFF;
122: height -= MFramePeer.BUTTON_Y;
123: }
124: if (fpeer.hasDecorations(MWindowAttributes.AWT_DECOR_MENU)) {
125: height -= MFramePeer.BUTTON_H;
126: }
127: }
128: Dimension d = new Dimension(width, height);
129:
130: Shape oldClipArea = g.getClip();
131: g.clipRect(0, 0, d.width, d.height);
132:
133: Color bg = f.getBackground();
134: Color fg = f.getForeground();
135: Color highlight = bg.brighter();
136: Color shadow = bg.darker();
137:
138: // because we'll most likely be drawing on white paper,
139: // for aesthetic reasons, don't make any part of the outer border
140: // pure white
141: if (highlight.equals(Color.white)) {
142: g.setColor(new Color(230, 230, 230));
143: } else {
144: g.setColor(highlight);
145: }
146: g.drawLine(0, 0, d.width, 0);
147: g.drawLine(1, 1, d.width - 1, 1);
148: g.drawLine(0, 0, 0, d.height);
149: g.drawLine(1, 1, 1, d.height - 1);
150: g.setColor(shadow);
151: g.drawLine(d.width, 1, d.width, d.height);
152: g.drawLine(d.width - 1, 2, d.width - 1, d.height);
153: g.drawLine(1, d.height, d.width, d.height);
154: g.drawLine(2, d.height - 1, d.width, d.height - 1);
155:
156: int x = GAP;
157: int nitems = mb.countMenus();
158:
159: Menu helpMenu = target.getHelpMenu();
160:
161: for (int i = 0; i < nitems; i++) {
162: Menu mn = target.getMenu(i);
163: String item = mn.getLabel();
164: if (item == null) {
165: item = "";
166: }
167: Font menuFont = mn.getFont();
168: g.setFont(menuFont);
169: FontMetrics menuMetrics = g.getFontMetrics();
170: int y = (d.height / 2) + menuMetrics.getMaxDescent();
171: int w = menuMetrics.stringWidth(item) + GAP * 2;
172:
173: if (x >= d.width) {
174: break;
175: }
176: if (mn.isEnabled()) {
177: g.setColor(fg);
178: } else {
179: // draw text as grayed out
180: g.setColor(shadow);
181: }
182:
183: if (helpMenu == mn) {
184: g.drawString(item, d.width - w + GAP, y);
185: } else {
186: g.drawString(item, x, y);
187: x += w;
188: }
189: }
190:
191: g.setClip(oldClipArea);
192: }
193:
194: // Needed for MenuComponentPeer.
195: public void setFont(Font f) {
196: }
197: }
|