001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Pavel Dolgov
019: * @version $Revision$
020: */package org.apache.harmony.awt.theme;
021:
022: import java.awt.Font;
023: import java.awt.FontMetrics;
024: import java.awt.Graphics;
025: import java.awt.Point;
026: import java.awt.Rectangle;
027: import java.awt.SystemColor;
028:
029: import org.apache.harmony.awt.state.MenuBarState;
030: import org.apache.harmony.awt.state.MenuItemState;
031:
032: /**
033: * Implementation of MenuBar's default visual style
034: */
035:
036: public class DefaultMenuBar {
037:
038: static final int vSpacing = 2;
039: static final int hSpacing = 5;
040:
041: public static void layoutMenuBar(MenuBarState s, int width) {
042:
043: Font f = s.getFont();
044: FontMetrics fm = s.getFontMetrics(f);
045: int lineHeight = getItemHeight(fm);
046: int lines = 0;
047: int lineWidth = 0;
048: for (int i = 0; i < s.getItemCount(); i++) {
049: MenuItemState is = s.getItem(i);
050: int itemWidth = getItemWidth(fm, is);
051: if (itemWidth >= width) {
052: lines += (lineWidth == 0 ? 1 : 2);
053: lineWidth = 0;
054: is.setItemBounds(0, lines * lineHeight, itemWidth,
055: lineHeight);
056: continue;
057: }
058: if (lineWidth + itemWidth > width) {
059: lines++;
060: lineWidth = itemWidth;
061: is.setItemBounds(0, lines * lineHeight, itemWidth,
062: lineHeight);
063: continue;
064: }
065: is.setItemBounds(lineWidth, lines * lineHeight, itemWidth,
066: lineHeight);
067: lineWidth += itemWidth;
068: }
069: if (lineWidth > 0) {
070: lines++;
071: }
072:
073: s.setSize(width, lines * lineHeight + 1);
074: }
075:
076: public static int getPreferredWidth(MenuBarState s) {
077: int width = 0;
078: Font f = s.getFont();
079: FontMetrics fm = s.getFontMetrics(f);
080: for (int i = 0; i < s.getItemCount(); i++) {
081: width += getItemWidth(fm, s.getItem(i));
082: }
083: return width;
084: }
085:
086: public static void drawMenuBar(MenuBarState s, Graphics g) {
087: int width = s.getWidth(), height = s.getHeight();
088: g.setColor(SystemColor.control);
089: g.fillRect(0, 0, width, height);
090: g.setColor(SystemColor.menu);
091: g.drawLine(0, height - 1, width - 1, height - 1);
092:
093: g.setFont(s.getFont());
094: g.setColor(SystemColor.menuText);
095: int selected = s.getSelectedItemIndex();
096: for (int i = 0; i < s.getItemCount(); i++) {
097: MenuItemState is = s.getItem(i);
098: Rectangle item = is.getItemBounds();
099: Rectangle text = is.getTextBounds();
100:
101: if (!is.isEnabled()) {
102: g.setColor(SystemColor.textInactiveText);
103: } else if (i == selected) {
104: g.setColor(SystemColor.textHighlight);
105: g.fillRect(item.x, item.y, item.width, item.height);
106: g.setColor(SystemColor.textHighlightText);
107: } else {
108: g.setColor(SystemColor.menuText);
109: }
110: g
111: .drawString(is.getText(), item.x + text.x, item.y
112: + text.y);
113: }
114: }
115:
116: public static int getItemIndex(MenuBarState s, Point p) {
117:
118: for (int i = 0; i < s.getItemCount(); i++) {
119: MenuItemState is = s.getItem(i);
120: Rectangle bounds = is.getItemBounds();
121: if (bounds.contains(p)) {
122: return i;
123: }
124: }
125:
126: return -1;
127: }
128:
129: public static Point getItemLocation(MenuBarState s, int index) {
130: if (index < 0 || index >= s.getItemCount()) {
131: return new Point(-1, -1);
132: }
133:
134: MenuItemState is = s.getItem(index);
135: Rectangle bounds = is.getItemBounds();
136: Point where = new Point(bounds.x, bounds.y + bounds.height);
137: Point screenPos = s.getLocationOnScreen();
138: where.translate(screenPos.x, screenPos.y);
139: return where;
140: }
141:
142: private static int getItemWidth(FontMetrics fm, MenuItemState is) {
143: Rectangle r = is.getTextBounds();
144: if (r == null) {
145: int h = getItemHeight(fm);
146: is.setTextBounds(hSpacing, h + -vSpacing - fm.getDescent(),
147: fm.stringWidth(is.getText()) + 2 * hSpacing, h);
148: r = is.getTextBounds();
149: }
150: return r.width;
151: }
152:
153: private static int getItemHeight(FontMetrics fm) {
154: return fm.getHeight() + 2 * vSpacing;
155: }
156: }
|