1 /* SwingMenuBarPeer.java -- A Swing based peer for AWT menu bars
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.java.awt.peer.swing;
40 import java.awt.Container;
42 import java.awt.Graphics;
44 import java.awt.MenuBar;
45 import java.awt.Point;
46 import java.awt.event.MouseEvent;
47 import java.awt.peer.MenuBarPeer;
49 import javax.swing.JMenuBar;
52 * A Swing based peer for the AWT menu bar. This is a little bit different from
53 * the other peers, since the AWT MenuBar is not derived from the AWT
56 * @author Roman Kennke (kennke@aicas.com)
58 public class SwingMenuBarPeer
59 implements MenuBarPeer
73 * The peer of the frame that contains this menu bar.
75 SwingFramePeer framePeer;
78 * A specialized JMenuBar that can be used as 'backend' for AWT MenuBars.
80 * @author Roman Kennke (kennke@aicas.com)
82 private class SwingMenuBar
86 * Overridden in order to provide a parent frame for this menu bar. The
87 * menu bar still is not inside the component hierarchy, we are faking
90 public Container getParent()
92 Container result = null;
93 if (framePeer != null)
94 result = (Container) framePeer.awtComponent;
99 * Unconditionally returns <code>true</code>, since we assume that when the
100 * menubar has a peer, it must be showing.
102 * @return <code>true</code>
104 public boolean isShowing()
106 // FIXME: This might be wrong. Maybe find a better way to do that.
111 * Handles mouse events by forwarding it to
112 * <code>processMouseEvent()</code>.
114 * @param ev the mouse event
116 public void handleMouseEvent(MouseEvent ev)
119 processMouseEvent(ev);
123 * Determines the menubar's screen location by asking the SwingFramePeer
126 * @return the screen location of the menu bar
128 public Point getLocationOnScreen()
130 return framePeer.getMenuLocationOnScreen();
135 * Creates a new <code>SwingMenuBarPeer</code> instance.
137 * @param awtMenuBar the AWT menu bar
139 public SwingMenuBarPeer(MenuBar awtMenuBar)
141 this.awtMenuBar = awtMenuBar;
142 menuBar = new SwingMenuBar();
143 menuBar.setDoubleBuffered(false);
144 // Add all the menus that are already in the MenuBar.
145 for (int i = 0; i < awtMenuBar.getMenuCount(); i++)
147 Menu menu = awtMenuBar.getMenu(i);
149 addMenu(awtMenuBar.getMenu(i));
154 * Sets the <code>SwingFramePeer</code> of the frame that holds this menu.
156 * @param peer the <code>SwingFramePeer</code> to set
158 public void setFramePeer(SwingFramePeer peer)
164 * Adds a menu to the menu bar.
166 * @param m the menu to add
168 public void addMenu(Menu m)
170 SwingMenuPeer menuPeer = (SwingMenuPeer) m.getPeer();
171 menuBar.add(menuPeer.menu);
175 * Adds a help menu to the menu bar.
177 * @param m the menu to add
179 public void addHelpMenu(Menu menu)
181 // FIXME: We should manage the help menu differently, so that it always
182 // appears at the rightmost position.
183 SwingMenuPeer menuPeer = (SwingMenuPeer) menu.getPeer();
184 menuBar.add(menuPeer.menu);
188 * Removes the menu with the specified index.
190 * @param index the index of the menu to remove
192 public void delMenu(int index)
194 menuBar.remove(index);
198 * Disposes this peer. This releases any reference to the AWT and Swing
201 public void dispose()
208 * Sets a font for the menu bar.
210 * @param font the font to set
212 public void setFont(Font font)
214 menuBar.setFont(font);
218 * Sets the width of the menu bar. This is called from the top level
219 * component peers to adjust the width of the menubar when their sizes
222 * @param w the width to set
224 public void setWidth(int w)
226 menuBar.setSize(w, menuBar.getPreferredSize().height);
231 * Paints the menu bar.
233 * @param g the graphics context to use for painting
235 public void peerPaint(Graphics g)
241 * Determines the height of the menubar.
243 * @return the height of the menu bar
245 public int getHeight()
247 return menuBar.getPreferredSize().height;
251 * Handles mouse events.
253 * @param ev the mouse event
255 public void handleMouseEvent(MouseEvent ev)
257 Point point = ev.getPoint();
258 for (int i = 0; i < awtMenuBar.getMenuCount(); i++)
260 Menu menu = awtMenuBar.getMenu(i);
261 SwingMenuPeer peer = (SwingMenuPeer) menu.getPeer();
262 int x1 = peer.getX();
263 int x2 = x1 + peer.getWidth();
264 if (point.x >= x1 && point.x <= x2)
266 ev.translatePoint(peer.getX(), peer.getY());
267 peer.handleMouseEvent(ev);
274 * Handles mouse motion events.
276 * @param ev the mouse motion event
278 public void handleMouseMotionEvent(MouseEvent ev)
280 Point point = ev.getPoint();
281 for (int i = 0; i < awtMenuBar.getMenuCount(); i++)
283 Menu menu = awtMenuBar.getMenu(i);
284 SwingMenuPeer peer = (SwingMenuPeer) menu.getPeer();
285 int x1 = peer.getX();
286 int x2 = x1 + peer.getWidth();
287 if (point.x >= x1 && point.x <= x2)
289 ev.translatePoint(peer.getX(), peer.getY());
290 peer.handleMouseMotionEvent(ev);