0N/A/*
6441N/A * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.awt.X11;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.peer.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.awt.image.ColorModel;
0N/A
0N/Aimport sun.awt.*;
0N/A
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Vector;
1696N/Aimport sun.util.logging.PlatformLogger;
0N/Aimport sun.java2d.SurfaceData;
0N/Aimport sun.java2d.SunGraphics2D;
0N/A
0N/A/**
0N/A * The abstract class XBaseMenuWindow is the superclass
0N/A * of all menu windows.
0N/A */
0N/Aabstract public class XBaseMenuWindow extends XWindow {
0N/A
0N/A /************************************************
0N/A *
0N/A * Data members
0N/A *
0N/A ************************************************/
0N/A
1696N/A private static PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XBaseMenuWindow");
0N/A
0N/A /*
0N/A * Colors are calculated using MotifColorUtilities class
0N/A * from backgroundColor and are contained in these vars.
0N/A */
0N/A private Color backgroundColor;
0N/A private Color foregroundColor;
0N/A private Color lightShadowColor;
0N/A private Color darkShadowColor;
0N/A private Color selectedColor;
0N/A private Color disabledColor;
0N/A
0N/A /**
0N/A * Array of items.
0N/A */
0N/A private ArrayList<XMenuItemPeer> items;
0N/A
0N/A /**
0N/A * Index of selected item in array of items
0N/A */
0N/A private int selectedIndex = -1;
0N/A
0N/A /**
0N/A * Specifies currently showing submenu.
0N/A */
0N/A private XMenuPeer showingSubmenu = null;
0N/A
0N/A /**
0N/A * Static synchronizational object.
0N/A * Following operations should be synchronized
0N/A * using this object:
0N/A * 1. Access to items vector
0N/A * 2. Access to selection
0N/A * 3. Access to showing menu window member
0N/A *
0N/A * This is lowest level lock,
0N/A * no other locks should be taken when
0N/A * thread own this lock.
0N/A */
0N/A static private Object menuTreeLock = new Object();
0N/A
0N/A /************************************************
0N/A *
0N/A * Event processing
0N/A *
0N/A ************************************************/
0N/A
0N/A /**
0N/A * If mouse button is clicked on item showing submenu
0N/A * we have to hide its submenu.
0N/A * And if mouse button is pressed on such item and
0N/A * dragged to another, getShowingSubmenu() is changed.
0N/A * So this member saves the item that the user
0N/A * presses mouse button on _only_ if it's showing submenu.
0N/A */
0N/A private XMenuPeer showingMousePressedSubmenu = null;
0N/A
0N/A /**
0N/A * If the PopupMenu is invoked as a result of right button click
0N/A * first mouse event after grabInput would be MouseReleased.
0N/A * We need to check if the user has moved mouse after input grab.
0N/A * If yes - hide the PopupMenu. If no - do nothing
0N/A */
0N/A protected Point grabInputPoint = null;
0N/A protected boolean hasPointerMoved = false;
0N/A
0N/A /************************************************
0N/A *
0N/A * Mapping data
0N/A *
0N/A ************************************************/
0N/A
0N/A /**
0N/A * Mapping data that is filled in getMappedItems function
0N/A * and reset in resetSize function. It contains array of
0N/A * items in order that they appear on screen and may contain
0N/A * additional data defined by descendants.
0N/A */
0N/A private MappingData mappingData;
0N/A
0N/A static class MappingData implements Cloneable {
0N/A
0N/A /**
0N/A * Array of item in order that they appear on screen
0N/A */
0N/A private XMenuItemPeer[] items;
0N/A
0N/A /**
0N/A * Constructs MappingData object with list
0N/A * of menu items
0N/A */
0N/A MappingData(XMenuItemPeer[] items) {
0N/A this.items = items;
0N/A }
0N/A
0N/A /**
0N/A * Constructs MappingData without items
0N/A * This constructor should be used in case of errors
0N/A */
0N/A MappingData() {
0N/A this.items = new XMenuItemPeer[0];
0N/A }
0N/A
0N/A public Object clone() {
0N/A try {
0N/A return super.clone();
0N/A } catch (CloneNotSupportedException ex) {
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A
0N/A public XMenuItemPeer[] getItems() {
0N/A return this.items;
0N/A }
0N/A }
0N/A
0N/A /************************************************
0N/A *
0N/A * Construction
0N/A *
0N/A ************************************************/
0N/A XBaseMenuWindow() {
0N/A super(new XCreateWindowParams(new Object[] {
0N/A DELAYED, Boolean.TRUE}));
0N/A }
0N/A
0N/A /************************************************
0N/A *
0N/A * Abstract methods
0N/A *
0N/A ************************************************/
0N/A
0N/A /**
0N/A * Returns parent menu window (not the X-heirarchy parent window)
0N/A */
0N/A protected abstract XBaseMenuWindow getParentMenuWindow();
0N/A
0N/A /**
0N/A * Performs mapping of items in window.
0N/A * This function creates and fills specific
0N/A * descendant of MappingData
0N/A * and sets mapping coordinates of items
0N/A * This function should return default menu data
0N/A * if errors occur
0N/A */
0N/A protected abstract MappingData map();
0N/A
0N/A /**
0N/A * Calculates placement of submenu window
0N/A * given bounds of item with submenu and
0N/A * size of submenu window. Returns suggested
0N/A * rectangle for submenu window in global coordinates
0N/A * @param itemBounds the bounding rectangle of item
0N/A * in local coordinates
0N/A * @param windowSize the desired size of submenu's window
0N/A */
0N/A protected abstract Rectangle getSubmenuBounds(Rectangle itemBounds, Dimension windowSize);
0N/A
0N/A
0N/A /**
0N/A * This function is to be called if it's likely that size
0N/A * of items was changed. It can be called from any thread
0N/A * in any locked state, so it should not take locks
0N/A */
0N/A protected abstract void updateSize();
0N/A
0N/A /************************************************
0N/A *
0N/A * Initialization
0N/A *
0N/A ************************************************/
0N/A
0N/A /**
0N/A * Overrides XBaseWindow.instantPreInit
0N/A */
0N/A void instantPreInit(XCreateWindowParams params) {
0N/A super.instantPreInit(params);
0N/A items = new ArrayList();
0N/A }
0N/A
0N/A /************************************************
0N/A *
0N/A * General-purpose functions
0N/A *
0N/A ************************************************/
0N/A
0N/A /**
0N/A * Returns static lock used for menus
0N/A */
0N/A static Object getMenuTreeLock() {
0N/A return menuTreeLock;
0N/A }
0N/A
0N/A /**
0N/A * This function is called to clear all saved
0N/A * size data.
0N/A */
0N/A protected void resetMapping() {
0N/A mappingData = null;
0N/A }
0N/A
0N/A /**
0N/A * Invokes repaint procedure on eventHandlerThread
0N/A */
0N/A void postPaintEvent() {
0N/A if (isShowing()) {
0N/A PaintEvent pe = new PaintEvent(target, PaintEvent.PAINT,
0N/A new Rectangle(0, 0, width, height));
0N/A postEvent(pe);
0N/A }
0N/A }
0N/A
0N/A /************************************************
0N/A *
0N/A * Utility functions for manipulating items
0N/A *
0N/A ************************************************/
0N/A
0N/A /**
0N/A * Thread-safely returns item at specified index
0N/A * @param index the position of the item to be returned.
0N/A */
0N/A XMenuItemPeer getItem(int index) {
0N/A if (index >= 0) {
0N/A synchronized(getMenuTreeLock()) {
0N/A if (items.size() > index) {
0N/A return items.get(index);
0N/A }
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Thread-safely creates a copy of the items vector
0N/A */
0N/A XMenuItemPeer[] copyItems() {
0N/A synchronized(getMenuTreeLock()) {
0N/A return (XMenuItemPeer[])items.toArray(new XMenuItemPeer[] {});
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Thread-safely returns selected item
0N/A */
0N/A XMenuItemPeer getSelectedItem() {
0N/A synchronized(getMenuTreeLock()) {
0N/A if (selectedIndex >= 0) {
0N/A if (items.size() > selectedIndex) {
0N/A return items.get(selectedIndex);
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns showing submenu, if any
0N/A */
0N/A XMenuPeer getShowingSubmenu() {
0N/A synchronized(getMenuTreeLock()) {
0N/A return showingSubmenu;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adds item to end of items vector.
0N/A * Note that this function does not perform
0N/A * check for adding duplicate items
0N/A * @param item item to add
0N/A */
0N/A public void addItem(MenuItem item) {
0N/A XMenuItemPeer mp = (XMenuItemPeer)item.getPeer();
0N/A if (mp != null) {
0N/A mp.setContainer(this);
0N/A synchronized(getMenuTreeLock()) {
0N/A items.add(mp);
0N/A }
0N/A } else {
1696N/A if (log.isLoggable(PlatformLogger.FINE)) {
0N/A log.fine("WARNING: Attempt to add menu item without a peer");
0N/A }
0N/A }
0N/A updateSize();
0N/A }
0N/A
0N/A /**
0N/A * Removes item at the specified index from items vector.
0N/A * @param index the position of the item to be removed
0N/A */
0N/A public void delItem(int index) {
0N/A synchronized(getMenuTreeLock()) {
0N/A if (selectedIndex == index) {
0N/A selectItem(null, false);
0N/A } else if (selectedIndex > index) {
0N/A selectedIndex--;
0N/A }
0N/A if (index < items.size()) {
0N/A items.remove(index);
0N/A } else {
1696N/A if (log.isLoggable(PlatformLogger.FINE)) {
0N/A log.fine("WARNING: Attempt to remove non-existing menu item, index : " + index + ", item count : " + items.size());
0N/A }
0N/A }
0N/A }
0N/A updateSize();
0N/A }
0N/A
0N/A /**
0N/A * Clears items vector and loads specified vector
0N/A * @param items vector to be loaded
0N/A */
0N/A public void reloadItems(Vector items) {
0N/A synchronized(getMenuTreeLock()) {
0N/A this.items.clear();
0N/A MenuItem[] itemArray = (MenuItem[])items.toArray(new MenuItem[] {});
0N/A int itemCnt = itemArray.length;
0N/A for(int i = 0; i < itemCnt; i++) {
0N/A addItem(itemArray[i]);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Select specified item and shows/hides submenus if necessary
0N/A * We can not select by index, so we need to select by ref.
0N/A * @param item the item to be selected, null to clear selection
0N/A * @param showWindowIfMenu if the item is XMenuPeer then its
0N/A * window is shown/hidden according to this param.
0N/A */
0N/A void selectItem(XMenuItemPeer item, boolean showWindowIfMenu) {
0N/A synchronized(getMenuTreeLock()) {
0N/A XMenuPeer showingSubmenu = getShowingSubmenu();
0N/A int newSelectedIndex = (item != null) ? items.indexOf(item) : -1;
0N/A if (this.selectedIndex != newSelectedIndex) {
1696N/A if (log.isLoggable(PlatformLogger.FINEST)) {
0N/A log.finest("Selected index changed, was : " + this.selectedIndex + ", new : " + newSelectedIndex);
0N/A }
0N/A this.selectedIndex = newSelectedIndex;
0N/A postPaintEvent();
0N/A }
0N/A final XMenuPeer submenuToShow = (showWindowIfMenu && (item instanceof XMenuPeer)) ? (XMenuPeer)item : null;
0N/A if (submenuToShow != showingSubmenu) {
0N/A XToolkit.executeOnEventHandlerThread(target, new Runnable() {
0N/A public void run() {
0N/A doShowSubmenu(submenuToShow);
0N/A }
0N/A });
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Performs hiding of currently showing submenu
0N/A * and showing of submenuToShow.
0N/A * This function should be executed on eventHandlerThread
0N/A * @param submenuToShow submenu to be shown or null
0N/A * to hide currently showing submenu
0N/A */
0N/A private void doShowSubmenu(XMenuPeer submenuToShow) {
0N/A XMenuWindow menuWindowToShow = (submenuToShow != null) ? submenuToShow.getMenuWindow() : null;
0N/A Dimension dim = null;
0N/A Rectangle bounds = null;
0N/A //ensureCreated can invoke XWindowPeer.init() ->
0N/A //XWindowPeer.initGraphicsConfiguration() ->
0N/A //Window.getGraphicsConfiguration()
0N/A //that tries to obtain Component.AWTTreeLock.
0N/A //So it should be called outside awtLock()
0N/A if (menuWindowToShow != null) {
0N/A menuWindowToShow.ensureCreated();
0N/A }
0N/A XToolkit.awtLock();
0N/A try {
0N/A synchronized(getMenuTreeLock()) {
0N/A if (showingSubmenu != submenuToShow) {
1696N/A if (log.isLoggable(PlatformLogger.FINER)) {
0N/A log.finest("Changing showing submenu");
0N/A }
0N/A if (showingSubmenu != null) {
0N/A XMenuWindow showingSubmenuWindow = showingSubmenu.getMenuWindow();
0N/A if (showingSubmenuWindow != null) {
0N/A showingSubmenuWindow.hide();
0N/A }
0N/A }
0N/A if (submenuToShow != null) {
0N/A dim = menuWindowToShow.getDesiredSize();
0N/A bounds = menuWindowToShow.getParentMenuWindow().getSubmenuBounds(submenuToShow.getBounds(), dim);
0N/A menuWindowToShow.show(bounds);
0N/A }
0N/A showingSubmenu = submenuToShow;
0N/A }
0N/A }
0N/A } finally {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A }
0N/A
0N/A final void setItemsFont( Font font ) {
0N/A XMenuItemPeer[] items = copyItems();
0N/A int itemCnt = items.length;
0N/A for (int i = 0; i < itemCnt; i++) {
0N/A items[i].setFont(font);
0N/A }
0N/A }
0N/A
0N/A /************************************************
0N/A *
0N/A * Utility functions for manipulating mapped items
0N/A *
0N/A ************************************************/
0N/A
0N/A /**
0N/A * Returns array of mapped items, null if error
0N/A * This function has to be not synchronized
0N/A * and we have to guarantee that we return
0N/A * some MappingData to user. It's OK if
0N/A * this.mappingData is replaced meanwhile
0N/A */
0N/A MappingData getMappingData() {
0N/A MappingData mappingData = this.mappingData;
0N/A if (mappingData == null) {
0N/A mappingData = map();
0N/A this.mappingData = mappingData;
0N/A }
0N/A return (MappingData)mappingData.clone();
0N/A }
0N/A
0N/A /**
0N/A * returns item thats mapped coordinates contain
0N/A * specified point, null of none.
0N/A * @param pt the point in this window's coordinate system
0N/A */
0N/A XMenuItemPeer getItemFromPoint(Point pt) {
0N/A XMenuItemPeer[] items = getMappingData().getItems();
0N/A int cnt = items.length;
0N/A for (int i = 0; i < cnt; i++) {
0N/A if (items[i].getBounds().contains(pt)) {
0N/A return items[i];
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns first item after currently selected
0N/A * item that can be selected according to mapping array.
0N/A * (no separators and no disabled items).
0N/A * Currently selected item if it's only selectable,
0N/A * null if no item can be selected
0N/A */
0N/A XMenuItemPeer getNextSelectableItem() {
0N/A XMenuItemPeer[] mappedItems = getMappingData().getItems();
0N/A XMenuItemPeer selectedItem = getSelectedItem();
0N/A int cnt = mappedItems.length;
0N/A //Find index of selected item
0N/A int selIdx = -1;
0N/A for (int i = 0; i < cnt; i++) {
0N/A if (mappedItems[i] == selectedItem) {
0N/A selIdx = i;
0N/A break;
0N/A }
0N/A }
0N/A int idx = (selIdx == cnt - 1) ? 0 : selIdx + 1;
0N/A //cycle through mappedItems to find selectable item
0N/A //beginning from the next item and moving to the
0N/A //beginning of array when end is reached.
0N/A //Cycle is finished on selected item itself
0N/A for (int i = 0; i < cnt; i++) {
0N/A XMenuItemPeer item = mappedItems[idx];
0N/A if (!item.isSeparator() && item.isTargetItemEnabled()) {
0N/A return item;
0N/A }
0N/A idx++;
0N/A if (idx >= cnt) {
0N/A idx = 0;
0N/A }
0N/A }
0N/A //return null if no selectable item was found
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns first item before currently selected
0N/A * see getNextSelectableItem() for comments
0N/A */
0N/A XMenuItemPeer getPrevSelectableItem() {
0N/A XMenuItemPeer[] mappedItems = getMappingData().getItems();
0N/A XMenuItemPeer selectedItem = getSelectedItem();
0N/A int cnt = mappedItems.length;
0N/A //Find index of selected item
0N/A int selIdx = -1;
0N/A for (int i = 0; i < cnt; i++) {
0N/A if (mappedItems[i] == selectedItem) {
0N/A selIdx = i;
0N/A break;
0N/A }
0N/A }
0N/A int idx = (selIdx <= 0) ? cnt - 1 : selIdx - 1;
0N/A //cycle through mappedItems to find selectable item
0N/A for (int i = 0; i < cnt; i++) {
0N/A XMenuItemPeer item = mappedItems[idx];
0N/A if (!item.isSeparator() && item.isTargetItemEnabled()) {
0N/A return item;
0N/A }
0N/A idx--;
0N/A if (idx < 0) {
0N/A idx = cnt - 1;
0N/A }
0N/A }
0N/A //return null if no selectable item was found
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns first selectable item
0N/A * This function is intended for clearing selection
0N/A */
0N/A XMenuItemPeer getFirstSelectableItem() {
0N/A XMenuItemPeer[] mappedItems = getMappingData().getItems();
0N/A int cnt = mappedItems.length;
0N/A for (int i = 0; i < cnt; i++) {
0N/A XMenuItemPeer item = mappedItems[i];
0N/A if (!item.isSeparator() && item.isTargetItemEnabled()) {
0N/A return item;
0N/A }
0N/A }
0N/A
0N/A return null;
0N/A }
0N/A
0N/A /************************************************
0N/A *
0N/A * Utility functions for manipulating
0N/A * hierarchy of windows
0N/A *
0N/A ************************************************/
0N/A
0N/A /**
0N/A * returns leaf menu window or
0N/A * this if no children are showing
0N/A */
0N/A XBaseMenuWindow getShowingLeaf() {
0N/A synchronized(getMenuTreeLock()) {
0N/A XBaseMenuWindow leaf = this;
0N/A XMenuPeer leafchild = leaf.getShowingSubmenu();
0N/A while (leafchild != null) {
0N/A leaf = leafchild.getMenuWindow();
0N/A leafchild = leaf.getShowingSubmenu();
0N/A }
0N/A return leaf;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * returns root menu window
0N/A * or this if this window is topmost
0N/A */
0N/A XBaseMenuWindow getRootMenuWindow() {
0N/A synchronized(getMenuTreeLock()) {
0N/A XBaseMenuWindow t = this;
0N/A XBaseMenuWindow tparent = t.getParentMenuWindow();
0N/A while (tparent != null) {
0N/A t = tparent;
0N/A tparent = t.getParentMenuWindow();
0N/A }
0N/A return t;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns window that contains pt.
0N/A * search is started from leaf window
0N/A * to return first window in Z-order
0N/A * @param pt point in global coordinates
0N/A */
0N/A XBaseMenuWindow getMenuWindowFromPoint(Point pt) {
0N/A synchronized(getMenuTreeLock()) {
0N/A XBaseMenuWindow t = getShowingLeaf();
0N/A while (t != null) {
0N/A Rectangle r = new Rectangle(t.toGlobal(new Point(0, 0)), t.getSize());
0N/A if (r.contains(pt)) {
0N/A return t;
0N/A }
0N/A t = t.getParentMenuWindow();
0N/A }
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /************************************************
0N/A *
0N/A * Primitives for getSubmenuBounds
0N/A *
0N/A * These functions are invoked from getSubmenuBounds
0N/A * implementations in different order. They check if window
0N/A * of size windowSize fits to the specified edge of
0N/A * rectangle itemBounds on the screen of screenSize.
0N/A * Return rectangle that occupies the window if it fits or null.
0N/A *
0N/A ************************************************/
0N/A
0N/A /**
0N/A * Checks if window fits below specified item
0N/A * returns rectangle that the window fits to or null.
0N/A * @param itemBounds rectangle of item in global coordinates
0N/A * @param windowSize size of submenu window to fit
0N/A * @param screenSize size of screen
0N/A */
0N/A Rectangle fitWindowBelow(Rectangle itemBounds, Dimension windowSize, Dimension screenSize) {
0N/A int width = windowSize.width;
0N/A int height = windowSize.height;
0N/A //Fix for 6267162: PIT: Popup Menu gets hidden below the screen when opened
0N/A //near the periphery of the screen, XToolkit
0N/A //Window should be moved if it's outside top-left screen bounds
0N/A int x = (itemBounds.x > 0) ? itemBounds.x : 0;
0N/A int y = (itemBounds.y + itemBounds.height > 0) ? itemBounds.y + itemBounds.height : 0;
0N/A if (y + height <= screenSize.height) {
0N/A //move it to the left if needed
0N/A if (width > screenSize.width) {
0N/A width = screenSize.width;
0N/A }
0N/A if (x + width > screenSize.width) {
0N/A x = screenSize.width - width;
0N/A }
0N/A return new Rectangle(x, y, width, height);
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Checks if window fits above specified item
0N/A * returns rectangle that the window fits to or null.
0N/A * @param itemBounds rectangle of item in global coordinates
0N/A * @param windowSize size of submenu window to fit
0N/A * @param screenSize size of screen
0N/A */
0N/A Rectangle fitWindowAbove(Rectangle itemBounds, Dimension windowSize, Dimension screenSize) {
0N/A int width = windowSize.width;
0N/A int height = windowSize.height;
0N/A //Fix for 6267162: PIT: Popup Menu gets hidden below the screen when opened
0N/A //near the periphery of the screen, XToolkit
0N/A //Window should be moved if it's outside bottom-left screen bounds
0N/A int x = (itemBounds.x > 0) ? itemBounds.x : 0;
0N/A int y = (itemBounds.y > screenSize.height) ? screenSize.height - height : itemBounds.y - height;
0N/A if (y >= 0) {
0N/A //move it to the left if needed
0N/A if (width > screenSize.width) {
0N/A width = screenSize.width;
0N/A }
0N/A if (x + width > screenSize.width) {
0N/A x = screenSize.width - width;
0N/A }
0N/A return new Rectangle(x, y, width, height);
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Checks if window fits to the right specified item
0N/A * returns rectangle that the window fits to or null.
0N/A * @param itemBounds rectangle of item in global coordinates
0N/A * @param windowSize size of submenu window to fit
0N/A * @param screenSize size of screen
0N/A */
0N/A Rectangle fitWindowRight(Rectangle itemBounds, Dimension windowSize, Dimension screenSize) {
0N/A int width = windowSize.width;
0N/A int height = windowSize.height;
0N/A //Fix for 6267162: PIT: Popup Menu gets hidden below the screen when opened
0N/A //near the periphery of the screen, XToolkit
0N/A //Window should be moved if it's outside top-left screen bounds
0N/A int x = (itemBounds.x + itemBounds.width > 0) ? itemBounds.x + itemBounds.width : 0;
0N/A int y = (itemBounds.y > 0) ? itemBounds.y : 0;
0N/A if (x + width <= screenSize.width) {
0N/A //move it to the top if needed
0N/A if (height > screenSize.height) {
0N/A height = screenSize.height;
0N/A }
0N/A if (y + height > screenSize.height) {
0N/A y = screenSize.height - height;
0N/A }
0N/A return new Rectangle(x, y, width, height);
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Checks if window fits to the left specified item
0N/A * returns rectangle that the window fits to or null.
0N/A * @param itemBounds rectangle of item in global coordinates
0N/A * @param windowSize size of submenu window to fit
0N/A * @param screenSize size of screen
0N/A */
0N/A Rectangle fitWindowLeft(Rectangle itemBounds, Dimension windowSize, Dimension screenSize) {
0N/A int width = windowSize.width;
0N/A int height = windowSize.height;
0N/A //Fix for 6267162: PIT: Popup Menu gets hidden below the screen when opened
0N/A //near the periphery of the screen, XToolkit
0N/A //Window should be moved if it's outside top-right screen bounds
0N/A int x = (itemBounds.x < screenSize.width) ? itemBounds.x - width : screenSize.width - width;
0N/A int y = (itemBounds.y > 0) ? itemBounds.y : 0;
0N/A if (x >= 0) {
0N/A //move it to the top if needed
0N/A if (height > screenSize.height) {
0N/A height = screenSize.height;
0N/A }
0N/A if (y + height > screenSize.height) {
0N/A y = screenSize.height - height;
0N/A }
0N/A return new Rectangle(x, y, width, height);
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The last thing we can do with the window
0N/A * to fit it on screen - move it to the
0N/A * top-left edge and cut by screen dimensions
0N/A * @param windowSize size of submenu window to fit
0N/A * @param screenSize size of screen
0N/A */
0N/A Rectangle fitWindowToScreen(Dimension windowSize, Dimension screenSize) {
0N/A int width = (windowSize.width < screenSize.width) ? windowSize.width : screenSize.width;
0N/A int height = (windowSize.height < screenSize.height) ? windowSize.height : screenSize.height;
0N/A return new Rectangle(0, 0, width, height);
0N/A }
0N/A
0N/A
0N/A /************************************************
0N/A *
0N/A * Utility functions for manipulating colors
0N/A *
0N/A ************************************************/
0N/A
0N/A /**
0N/A * This function is called before every painting.
0N/A * TODO:It would be better to add PropertyChangeListener
0N/A * to target component
0N/A * TODO:It would be better to access background color
0N/A * not invoking user-overridable function
0N/A */
0N/A void resetColors() {
0N/A replaceColors((target == null) ? SystemColor.window : target.getBackground());
0N/A }
0N/A
0N/A /**
0N/A * Calculates colors of various elements given
0N/A * background color. Uses MotifColorUtilities
0N/A * @param backgroundColor the color of menu window's
0N/A * background.
0N/A */
0N/A void replaceColors(Color backgroundColor) {
0N/A if (backgroundColor != this.backgroundColor) {
0N/A this.backgroundColor = backgroundColor;
0N/A
0N/A int red = backgroundColor.getRed();
0N/A int green = backgroundColor.getGreen();
0N/A int blue = backgroundColor.getBlue();
0N/A
0N/A foregroundColor = new Color(MotifColorUtilities.calculateForegroundFromBackground(red,green,blue));
0N/A lightShadowColor = new Color(MotifColorUtilities.calculateTopShadowFromBackground(red,green,blue));
0N/A darkShadowColor = new Color(MotifColorUtilities.calculateBottomShadowFromBackground(red,green,blue));
0N/A selectedColor = new Color(MotifColorUtilities.calculateSelectFromBackground(red,green,blue));
0N/A disabledColor = (backgroundColor.equals(Color.BLACK)) ? foregroundColor.darker() : backgroundColor.darker();
0N/A }
0N/A }
0N/A
0N/A Color getBackgroundColor() {
0N/A return backgroundColor;
0N/A }
0N/A
0N/A Color getForegroundColor() {
0N/A return foregroundColor;
0N/A }
0N/A
0N/A Color getLightShadowColor() {
0N/A return lightShadowColor;
0N/A }
0N/A
0N/A Color getDarkShadowColor() {
0N/A return darkShadowColor;
0N/A }
0N/A
0N/A Color getSelectedColor() {
0N/A return selectedColor;
0N/A }
0N/A
0N/A Color getDisabledColor() {
0N/A return disabledColor;
0N/A }
0N/A
0N/A /************************************************
0N/A *
0N/A * Painting utility functions
0N/A *
0N/A ************************************************/
0N/A
0N/A /**
0N/A * Draws raised or sunken rectangle on specified graphics
0N/A * @param g the graphics on which to draw
0N/A * @param x the coordinate of left edge in coordinates of graphics
0N/A * @param y the coordinate of top edge in coordinates of graphics
0N/A * @param width the width of rectangle
0N/A * @param height the height of rectangle
0N/A * @param raised true to draw raised rectangle, false to draw sunken
0N/A */
0N/A void draw3DRect(Graphics g, int x, int y, int width, int height, boolean raised) {
0N/A if ((width <= 0) || (height <= 0)) {
0N/A return;
0N/A }
0N/A Color c = g.getColor();
0N/A g.setColor(raised ? getLightShadowColor() : getDarkShadowColor());
0N/A g.drawLine(x, y, x, y + height - 1);
0N/A g.drawLine(x + 1, y, x + width - 1, y);
0N/A g.setColor(raised ? getDarkShadowColor() : getLightShadowColor());
0N/A g.drawLine(x + 1, y + height - 1, x + width - 1, y + height - 1);
0N/A g.drawLine(x + width - 1, y + 1, x + width - 1, y + height - 1);
0N/A g.setColor(c);
0N/A }
0N/A
0N/A /************************************************
0N/A *
0N/A * Overriden utility functions of XWindow
0N/A *
0N/A ************************************************/
0N/A
0N/A /**
0N/A * Filters X events
0N/A */
0N/A protected boolean isEventDisabled(XEvent e) {
0N/A switch (e.get_type()) {
216N/A case XConstants.Expose :
216N/A case XConstants.GraphicsExpose :
216N/A case XConstants.ButtonPress:
216N/A case XConstants.ButtonRelease:
216N/A case XConstants.MotionNotify:
216N/A case XConstants.KeyPress:
216N/A case XConstants.KeyRelease:
216N/A case XConstants.DestroyNotify:
0N/A return super.isEventDisabled(e);
0N/A default:
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Invokes disposal procedure on eventHandlerThread
0N/A */
0N/A public void dispose() {
0N/A setDisposed(true);
6441N/A InvocationEvent ev = new InvocationEvent(target, new Runnable() {
0N/A public void run() {
0N/A doDispose();
0N/A }
0N/A });
6441N/A super.postEvent(ev);
0N/A }
0N/A
0N/A /**
0N/A * Performs disposal of menu window.
0N/A * Should be called only on eventHandlerThread
0N/A */
0N/A protected void doDispose() {
0N/A xSetVisible(false);
0N/A SurfaceData oldData = surfaceData;
0N/A surfaceData = null;
0N/A if (oldData != null) {
0N/A oldData.invalidate();
0N/A }
0N/A XToolkit.targetDisposedPeer(target, this);
0N/A destroy();
0N/A }
0N/A
0N/A /**
0N/A * Invokes event processing on eventHandlerThread
0N/A * This function needs to be overriden since
0N/A * XBaseMenuWindow has no corresponding component
0N/A * so events can not be processed using standart means
0N/A */
0N/A void postEvent(final AWTEvent event) {
6441N/A InvocationEvent ev = new InvocationEvent(event.getSource(), new Runnable() {
6441N/A public void run() {
6441N/A handleEvent(event);
6441N/A }
6441N/A });
6441N/A super.postEvent(ev);
0N/A }
0N/A
0N/A /**
0N/A * The implementation of base window performs processing
0N/A * of paint events only. This behaviour is changed in
0N/A * descendants.
0N/A */
0N/A protected void handleEvent(AWTEvent event) {
0N/A switch(event.getID()) {
0N/A case PaintEvent.PAINT:
0N/A doHandleJavaPaintEvent((PaintEvent)event);
0N/A break;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Save location of pointer for further use
0N/A * then invoke superclass
0N/A */
0N/A public boolean grabInput() {
0N/A int rootX;
0N/A int rootY;
0N/A boolean res;
0N/A XToolkit.awtLock();
0N/A try {
0N/A long root = XlibWrapper.RootWindow(XToolkit.getDisplay(),
0N/A getScreenNumber());
0N/A res = XlibWrapper.XQueryPointer(XToolkit.getDisplay(), root,
0N/A XlibWrapper.larg1, //root
0N/A XlibWrapper.larg2, //child
0N/A XlibWrapper.larg3, //root_x
0N/A XlibWrapper.larg4, //root_y
0N/A XlibWrapper.larg5, //child_x
0N/A XlibWrapper.larg6, //child_y
0N/A XlibWrapper.larg7);//mask
0N/A rootX = Native.getInt(XlibWrapper.larg3);
0N/A rootY = Native.getInt(XlibWrapper.larg4);
0N/A res &= super.grabInput();
0N/A } finally {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A if (res) {
0N/A //Mouse pointer is on the same display
0N/A this.grabInputPoint = new Point(rootX, rootY);
0N/A this.hasPointerMoved = false;
0N/A } else {
0N/A this.grabInputPoint = null;
0N/A this.hasPointerMoved = true;
0N/A }
0N/A return res;
0N/A }
0N/A /************************************************
0N/A *
0N/A * Overridable event processing functions
0N/A *
0N/A ************************************************/
0N/A
0N/A /**
0N/A * Performs repainting
0N/A */
0N/A void doHandleJavaPaintEvent(PaintEvent event) {
0N/A Rectangle rect = event.getUpdateRect();
0N/A repaint(rect.x, rect.y, rect.width, rect.height);
0N/A }
0N/A
0N/A /************************************************
0N/A *
0N/A * User input handling utility functions
0N/A *
0N/A ************************************************/
0N/A
0N/A /**
0N/A * Performs handling of java mouse event
0N/A * Note that this function should be invoked
0N/A * only from root of menu window's hierarchy
0N/A * that grabs input focus
0N/A */
0N/A void doHandleJavaMouseEvent( MouseEvent mouseEvent ) {
0N/A if (!XToolkit.isLeftMouseButton(mouseEvent) && !XToolkit.isRightMouseButton(mouseEvent)) {
0N/A return;
0N/A }
0N/A //Window that owns input
0N/A XBaseWindow grabWindow = XAwtState.getGrabWindow();
0N/A //Point of mouse event in global coordinates
0N/A Point ptGlobal = mouseEvent.getLocationOnScreen();
0N/A if (!hasPointerMoved) {
0N/A //Fix for 6301307: NullPointerException while dispatching mouse events, XToolkit
0N/A if (grabInputPoint == null ||
0N/A (Math.abs(ptGlobal.x - grabInputPoint.x) > getMouseMovementSmudge()) ||
0N/A (Math.abs(ptGlobal.y - grabInputPoint.y) > getMouseMovementSmudge())) {
0N/A hasPointerMoved = true;
0N/A }
0N/A }
0N/A //Z-order first descendant of current menu window
0N/A //hierarchy that contain mouse point
0N/A XBaseMenuWindow wnd = getMenuWindowFromPoint(ptGlobal);
0N/A //Item in wnd that contains mouse point, if any
0N/A XMenuItemPeer item = (wnd != null) ? wnd.getItemFromPoint(wnd.toLocal(ptGlobal)) : null;
0N/A //Currently showing leaf window
0N/A XBaseMenuWindow cwnd = getShowingLeaf();
0N/A switch (mouseEvent.getID()) {
0N/A case MouseEvent.MOUSE_PRESSED:
0N/A //This line is to get rid of possible problems
0N/A //That may occur if mouse events are lost
0N/A showingMousePressedSubmenu = null;
0N/A if ((grabWindow == this) && (wnd == null)) {
0N/A //Menus grab input and the user
0N/A //presses mouse button outside
0N/A ungrabInput();
0N/A } else {
0N/A //Menus grab input OR mouse is pressed on menu window
0N/A grabInput();
0N/A if (item != null && !item.isSeparator() && item.isTargetItemEnabled()) {
0N/A //Button is pressed on enabled item
0N/A if (wnd.getShowingSubmenu() == item) {
0N/A //Button is pressed on item that shows
0N/A //submenu. We have to hide its submenu
0N/A //if user clicks on it
0N/A showingMousePressedSubmenu = (XMenuPeer)item;
0N/A }
0N/A wnd.selectItem(item, true);
0N/A } else {
0N/A //Button is pressed on disabled item or empty space
0N/A if (wnd != null) {
0N/A wnd.selectItem(null, false);
0N/A }
0N/A }
0N/A }
0N/A break;
0N/A case MouseEvent.MOUSE_RELEASED:
0N/A //Note that if item is not null, wnd has to be not null
0N/A if (item != null && !item.isSeparator() && item.isTargetItemEnabled()) {
0N/A if (item instanceof XMenuPeer) {
0N/A if (showingMousePressedSubmenu == item) {
0N/A //User clicks on item that shows submenu.
0N/A //Hide the submenu
0N/A if (wnd instanceof XMenuBarPeer) {
0N/A ungrabInput();
0N/A } else {
0N/A wnd.selectItem(item, false);
0N/A }
0N/A }
0N/A } else {
0N/A //Invoke action event
0N/A item.action(mouseEvent.getWhen());
0N/A ungrabInput();
0N/A }
0N/A } else {
0N/A //Mouse is released outside menu items
0N/A if (hasPointerMoved || (wnd instanceof XMenuBarPeer)) {
0N/A ungrabInput();
0N/A }
0N/A }
0N/A showingMousePressedSubmenu = null;
0N/A break;
0N/A case MouseEvent.MOUSE_DRAGGED:
0N/A if (wnd != null) {
0N/A //Mouse is dragged over menu window
0N/A //Move selection to item under cursor
0N/A if (item != null && !item.isSeparator() && item.isTargetItemEnabled()) {
0N/A if (grabWindow == this){
0N/A wnd.selectItem(item, true);
0N/A }
0N/A } else {
0N/A wnd.selectItem(null, false);
0N/A }
0N/A } else {
0N/A //Mouse is dragged outside menu windows
0N/A //clear selection in leaf to reflect it
0N/A if (cwnd != null) {
0N/A cwnd.selectItem(null, false);
0N/A }
0N/A }
0N/A break;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Performs handling of java keyboard event
0N/A * Note that this function should be invoked
0N/A * only from root of menu window's hierarchy
0N/A * that grabs input focus
0N/A */
0N/A void doHandleJavaKeyEvent(KeyEvent event) {
1696N/A if (log.isLoggable(PlatformLogger.FINER)) log.finer(event.toString());
0N/A if (event.getID() != KeyEvent.KEY_PRESSED) {
0N/A return;
0N/A }
0N/A final int keyCode = event.getKeyCode();
0N/A XBaseMenuWindow cwnd = getShowingLeaf();
0N/A XMenuItemPeer citem = cwnd.getSelectedItem();
0N/A switch(keyCode) {
0N/A case KeyEvent.VK_UP:
0N/A case KeyEvent.VK_KP_UP:
0N/A if (!(cwnd instanceof XMenuBarPeer)) {
0N/A //If active window is not menu bar,
0N/A //move selection up
0N/A cwnd.selectItem(cwnd.getPrevSelectableItem(), false);
0N/A }
0N/A break;
0N/A case KeyEvent.VK_DOWN:
0N/A case KeyEvent.VK_KP_DOWN:
0N/A if (cwnd instanceof XMenuBarPeer) {
0N/A //If active window is menu bar show current submenu
0N/A selectItem(getSelectedItem(), true);
0N/A } else {
0N/A //move selection down
0N/A cwnd.selectItem(cwnd.getNextSelectableItem(), false);
0N/A }
0N/A break;
0N/A case KeyEvent.VK_LEFT:
0N/A case KeyEvent.VK_KP_LEFT:
0N/A if (cwnd instanceof XMenuBarPeer) {
0N/A //leaf window is menu bar
0N/A //select previous item
0N/A selectItem(getPrevSelectableItem(), false);
0N/A } else if (cwnd.getParentMenuWindow() instanceof XMenuBarPeer) {
0N/A //leaf window is direct child of menu bar
0N/A //select previous item of menu bar
0N/A //and show its submenu
0N/A selectItem(getPrevSelectableItem(), true);
0N/A } else {
0N/A //hide leaf moving focus to its parent
0N/A //(equvivalent of pressing ESC)
0N/A XBaseMenuWindow pwnd = cwnd.getParentMenuWindow();
0N/A //Fix for 6272952: PIT: Pressing LEFT ARROW on a popup menu throws NullPointerException, XToolkit
0N/A if (pwnd != null) {
0N/A pwnd.selectItem(pwnd.getSelectedItem(), false);
0N/A }
0N/A }
0N/A break;
0N/A case KeyEvent.VK_RIGHT:
0N/A case KeyEvent.VK_KP_RIGHT:
0N/A if (cwnd instanceof XMenuBarPeer) {
0N/A //leaf window is menu bar
0N/A //select next item
0N/A selectItem(getNextSelectableItem(), false);
0N/A } else if (citem instanceof XMenuPeer) {
0N/A //current item is menu, show its window
0N/A //(equivalent of ENTER)
0N/A cwnd.selectItem(citem, true);
0N/A } else if (this instanceof XMenuBarPeer) {
0N/A //if this is menu bar (not popup menu)
0N/A //and the user presses RIGHT on item (not submenu)
0N/A //select next top-level menu
0N/A selectItem(getNextSelectableItem(), true);
0N/A }
0N/A break;
0N/A case KeyEvent.VK_SPACE:
0N/A case KeyEvent.VK_ENTER:
0N/A //If the current item has submenu show it
0N/A //Perform action otherwise
0N/A if (citem instanceof XMenuPeer) {
0N/A cwnd.selectItem(citem, true);
0N/A } else if (citem != null) {
0N/A citem.action(event.getWhen());
0N/A ungrabInput();
0N/A }
0N/A break;
0N/A case KeyEvent.VK_ESCAPE:
0N/A //If current window is menu bar or its child - close it
0N/A //If current window is popup menu - close it
0N/A //go one level up otherwise
0N/A
0N/A //Fixed 6266513: Incorrect key handling in XAWT popup menu
0N/A //Popup menu should be closed on 'ESC'
0N/A if ((cwnd instanceof XMenuBarPeer) || (cwnd.getParentMenuWindow() instanceof XMenuBarPeer)) {
0N/A ungrabInput();
0N/A } else if (cwnd instanceof XPopupMenuPeer) {
0N/A ungrabInput();
0N/A } else {
0N/A XBaseMenuWindow pwnd = cwnd.getParentMenuWindow();
0N/A pwnd.selectItem(pwnd.getSelectedItem(), false);
0N/A }
0N/A break;
0N/A case KeyEvent.VK_F10:
0N/A //Fixed 6266513: Incorrect key handling in XAWT popup menu
0N/A //All menus should be closed on 'F10'
0N/A ungrabInput();
0N/A break;
0N/A default:
0N/A break;
0N/A }
0N/A }
0N/A
0N/A} //class XBaseMenuWindow