2976N/A/*
2976N/A * Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved.
2976N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2976N/A *
2976N/A * This code is free software; you can redistribute it and/or modify it
2976N/A * under the terms of the GNU General Public License version 2 only, as
2976N/A * published by the Free Software Foundation. Oracle designates this
2976N/A * particular file as subject to the "Classpath" exception as provided
2976N/A * by Oracle in the LICENSE file that accompanied this code.
6983N/A *
6983N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2976N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2976N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2976N/A * version 2 for more details (a copy is included in the LICENSE file that
2976N/A * accompanied this code).
6983N/A *
6983N/A * You should have received a copy of the GNU General Public License version
6983N/A * 2 along with this work; if not, write to the Free Software Foundation,
6983N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2976N/A *
2976N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2976N/A * or visit www.oracle.com if you need additional information or have any
2976N/A * questions.
2976N/A */
5236N/Apackage sun.awt.X11;
5728N/A
2976N/Aimport java.awt.*;
2976N/Aimport java.awt.peer.*;
2976N/Aimport java.awt.event.*;
2976N/A
2976N/Aimport java.util.Vector;
2976N/Aimport sun.awt.AWTAccessor;
3824N/Aimport sun.util.logging.PlatformLogger;
3824N/A
3824N/Apublic class XPopupMenuPeer extends XMenuWindow implements PopupMenuPeer {
6606N/A
6606N/A /************************************************
6606N/A *
6606N/A * Data members
3824N/A *
5728N/A ************************************************/
3842N/A private static PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XBaseMenuWindow");
3842N/A
3824N/A /*
3842N/A * Primary members
2976N/A */
5636N/A private XComponentPeer componentPeer;
2976N/A private PopupMenu popupMenuTarget;
2976N/A
2976N/A /*
2976N/A * If mouse button is clicked on item showing submenu
3262N/A * we have to hide its submenu.
5636N/A * This member saves the submenu under cursor
5636N/A * Only if it's showing
5636N/A */
3262N/A private XMenuPeer showingMousePressedSubmenu = null;
2976N/A
5173N/A /*
2976N/A * Painting constants
2976N/A */
2976N/A private final static int CAPTION_MARGIN_TOP = 4;
2976N/A private final static int CAPTION_SEPARATOR_HEIGHT = 6;
2976N/A
5636N/A /************************************************
2976N/A *
2976N/A * Construction
2976N/A *
2976N/A ************************************************/
2976N/A XPopupMenuPeer(PopupMenu target) {
5636N/A super(null);
2976N/A this.popupMenuTarget = target;
2976N/A }
2976N/A
2976N/A /************************************************
2976N/A *
5636N/A * Implementation of interface methods
2976N/A *
2976N/A ************************************************/
2976N/A /*
2976N/A * From MenuComponentPeer
2976N/A */
2976N/A public void setFont(Font f) {
3262N/A resetMapping();
3262N/A setItemsFont(f);
3262N/A postPaintEvent();
5636N/A }
5636N/A
5636N/A /*
3262N/A * From MenuItemPeer
2976N/A */
5173N/A public void setLabel(String label) {
2976N/A resetMapping();
2976N/A postPaintEvent();
2976N/A }
5636N/A
2976N/A
2976N/A public void setEnabled(boolean enabled) {
2976N/A postPaintEvent();
2976N/A }
2976N/A
/**
* DEPRECATED: Replaced by setEnabled(boolean).
* @see java.awt.peer.MenuItemPeer
*/
public void enable() {
setEnabled( true );
}
/**
* DEPRECATED: Replaced by setEnabled(boolean).
* @see java.awt.peer.MenuItemPeer
*/
public void disable() {
setEnabled( false );
}
/*
* From MenuPeer
*/
/**
* addSeparator routines are not used
* in peers. Shared code invokes addItem("-")
* for adding separators
*/
public void addSeparator() {
if (log.isLoggable(PlatformLogger.FINER)) log.finer("addSeparator is not implemented");
}
/*
* From PopupMenuPeer
*/
public void show(Event e) {
target = (Component)e.target;
// Get menus from the target.
Vector targetItemVector = getMenuTargetItems();
if (targetItemVector != null) {
reloadItems(targetItemVector);
//Fix for 6287092: JCK15a: api/java_awt/interactive/event/EventTests.html#EventTest0015 fails, mustang
Point tl = target.getLocationOnScreen();
Point pt = new Point(tl.x + e.x, tl.y + e.y);
//Fixed 6266513: Incorrect key handling in XAWT popup menu
//No item should be selected when showing popup menu
if (!ensureCreated()) {
return;
}
Dimension dim = getDesiredSize();
//Fix for 6267162: PIT: Popup Menu gets hidden below the screen when opened
//near the periphery of the screen, XToolkit
Rectangle bounds = getWindowBounds(pt, dim);
reshape(bounds);
xSetVisible(true);
toFront();
selectItem(null, false);
grabInput();
}
}
/************************************************
*
* Access to target's fields
*
************************************************/
//Fix for 6267144: PIT: Popup menu label is not shown, XToolkit
Font getTargetFont() {
if (popupMenuTarget == null) {
return XWindow.getDefaultFont();
}
return AWTAccessor.getMenuComponentAccessor()
.getFont_NoClientCode(popupMenuTarget);
}
//Fix for 6267144: PIT: Popup menu label is not shown, XToolkit
String getTargetLabel() {
if (target == null) {
return "";
}
return AWTAccessor.getMenuItemAccessor().getLabel(popupMenuTarget);
}
//Fix for 6184485: Popup menu is not disabled on XToolkit even when calling setEnabled (false)
boolean isTargetEnabled() {
if (popupMenuTarget == null) {
return false;
}
return AWTAccessor.getMenuItemAccessor().isEnabled(popupMenuTarget);
}
Vector getMenuTargetItems() {
if (popupMenuTarget == null) {
return null;
}
return AWTAccessor.getMenuAccessor().getItems(popupMenuTarget);
}
/************************************************
*
* Utility functions
*
************************************************/
//Fix for 6267162: PIT: Popup Menu gets hidden below the screen when opened
//near the periphery of the screen, XToolkit
/**
* Calculates placement of popup menu window
* given origin in global coordinates and
* size of menu window. Returns suggested
* rectangle for menu window in global coordinates
* @param origin the origin point specified in show()
* function converted to global coordinates
* @param windowSize the desired size of menu's window
*/
protected Rectangle getWindowBounds(Point origin, Dimension windowSize) {
Rectangle globalBounds = new Rectangle(origin.x, origin.y, 0, 0);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle res;
res = fitWindowRight(globalBounds, windowSize, screenSize);
if (res != null) {
return res;
}
res = fitWindowLeft(globalBounds, windowSize, screenSize);
if (res != null) {
return res;
}
res = fitWindowBelow(globalBounds, windowSize, screenSize);
if (res != null) {
return res;
}
res = fitWindowAbove(globalBounds, windowSize, screenSize);
if (res != null) {
return res;
}
return fitWindowToScreen(windowSize, screenSize);
}
/************************************************
*
* Overriden XMenuWindow caption-painting functions
* Necessary to fix 6267144: PIT: Popup menu label is not shown, XToolkit
*
************************************************/
/**
* Returns height of menu window's caption.
* Can be overriden for popup menus and tear-off menus
*/
protected Dimension getCaptionSize() {
String s = getTargetLabel();
if (s.equals("")) {
return null;
}
Graphics g = getGraphics();
if (g == null) {
return null;
}
try {
g.setFont(getTargetFont());
FontMetrics fm = g.getFontMetrics();
String str = getTargetLabel();
int width = fm.stringWidth(str);
int height = CAPTION_MARGIN_TOP + fm.getHeight() + CAPTION_SEPARATOR_HEIGHT;
Dimension textDimension = new Dimension(width, height);
return textDimension;
} finally {
g.dispose();
}
}
/**
* Paints menu window's caption.
* Can be overriden for popup menus and tear-off menus.
* Default implementation does nothing
*/
protected void paintCaption(Graphics g, Rectangle rect) {
String s = getTargetLabel();
if (s.equals("")) {
return;
}
g.setFont(getTargetFont());
FontMetrics fm = g.getFontMetrics();
String str = getTargetLabel();
int width = fm.stringWidth(str);
int textx = rect.x + (rect.width - width) / 2;
int texty = rect.y + CAPTION_MARGIN_TOP + fm.getAscent();
int sepy = rect.y + rect.height - CAPTION_SEPARATOR_HEIGHT / 2;
g.setColor(isTargetEnabled() ? getForegroundColor() : getDisabledColor());
g.drawString(s, textx, texty);
draw3DRect(g, rect.x, sepy, rect.width, 2, false);
}
/************************************************
*
* Overriden XBaseMenuWindow functions
*
************************************************/
protected void doDispose() {
super.doDispose();
XToolkit.targetDisposedPeer(popupMenuTarget, this);
}
protected void handleEvent(AWTEvent event) {
switch(event.getID()) {
case MouseEvent.MOUSE_PRESSED:
case MouseEvent.MOUSE_RELEASED:
case MouseEvent.MOUSE_CLICKED:
case MouseEvent.MOUSE_MOVED:
case MouseEvent.MOUSE_ENTERED:
case MouseEvent.MOUSE_EXITED:
case MouseEvent.MOUSE_DRAGGED:
doHandleJavaMouseEvent((MouseEvent)event);
break;
case KeyEvent.KEY_PRESSED:
case KeyEvent.KEY_RELEASED:
doHandleJavaKeyEvent((KeyEvent)event);
break;
default:
super.handleEvent(event);
break;
}
}
/************************************************
*
* Overriden XWindow general-purpose functions
*
************************************************/
void ungrabInputImpl() {
hide();
}
/************************************************
*
* Overriden XWindow keyboard processing
*
************************************************/
/*
* In previous version keys were handled in handleKeyPress.
* Now we override this function do disable F10 explicit
* processing. All processing is done using KeyEvent.
*/
public void handleKeyPress(XEvent xev) {
XKeyEvent xkey = xev.get_xkey();
if (log.isLoggable(PlatformLogger.FINE)) {
log.fine(xkey.toString());
}
if (isEventDisabled(xev)) {
return;
}
final Component currentSource = (Component)getEventSource();
handleKeyPress(xkey);
}
}