/*
* Copyright (c) 2002, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.awt.X11;
import java.awt.*;
import java.awt.peer.*;
import java.awt.event.*;
import java.util.Vector;
import sun.util.logging.PlatformLogger;
import sun.awt.AWTAccessor;
public class XMenuBarPeer extends XBaseMenuWindow implements MenuBarPeer {
/************************************************
*
* Data members
*
************************************************/
private static PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XMenuBarPeer");
/*
* Primary members
*/
private XFramePeer framePeer;
private MenuBar menuBarTarget;
/*
* Index of help menu
*/
private XMenuPeer helpMenu = null;
/*
* dimension constants
*/
private final static int BAR_SPACING_TOP = 3;
private final static int BAR_SPACING_BOTTOM = 3;
private final static int BAR_SPACING_LEFT = 3;
private final static int BAR_SPACING_RIGHT = 3;
private final static int BAR_ITEM_SPACING = 2;
private final static int BAR_ITEM_MARGIN_LEFT = 10;
private final static int BAR_ITEM_MARGIN_RIGHT = 10;
private final static int BAR_ITEM_MARGIN_TOP = 2;
private final static int BAR_ITEM_MARGIN_BOTTOM = 2;
/************************************************
*
* Mapping data
*
************************************************/
/**
* XBaseMenuWindow's mappingData is extended with
* desired height of menu bar
*/
static class MappingData extends XBaseMenuWindow.MappingData {
int desiredHeight;
MappingData(XMenuItemPeer[] items, int desiredHeight) {
super(items);
this.desiredHeight = desiredHeight;
}
/**
* Constructs MappingData without items
* This constructor should be used in case of errors
*/
MappingData() {
this.desiredHeight = 0;
}
public int getDesiredHeight() {
return this.desiredHeight;
}
}
/************************************************
*
* Construction
*
************************************************/
XMenuBarPeer(MenuBar menuBarTarget) {
this.menuBarTarget = menuBarTarget;
}
/************************************************
*
* Implementaion of interface methods
*
************************************************/
/*
* From MenuComponentPeer
*/
public void setFont(Font f) {
resetMapping();
setItemsFont(f);
postPaintEvent();
}
/*
* From MenuBarPeer
*/
/*
* Functions addMenu, delMenu, addHelpMenu
* need to have somewhat strange behaivour
* deduced from java.awt.MenuBar.
* We can not get index of particular item in
* MenuBar.menus array, because MenuBar firstly
* performs array operations and then calls peer.
* So we need to synchronize indicies in 'items'
* array with MenuBar.menus. We have to follow
* these rules:
* 1. Menus are always added to the end of array,
* even when helpMenu is present
* 2. Removal of any menu item acts as casual
* remove from array
* 3. MenuBar.setHelpMenu _firstly_ removes
* previous helpMenu by calling delMenu() if
* necessary, then it performs addMenu(),
* and then - addHelpMenu().
*
* Note that these functions don't perform
* type checks and checks for nulls or duplicates
*/
public void addMenu(Menu m) {
addItem(m);
postPaintEvent();
}
public void delMenu(int index) {
synchronized(getMenuTreeLock()) {
XMenuItemPeer item = getItem(index);
if (item != null && item == helpMenu) {
helpMenu = null;
}
delItem(index);
}
postPaintEvent();
}
public void addHelpMenu(Menu m) {
XMenuPeer mp = AWTAccessor.getMenuComponentAccessor().getPeer(m);
synchronized(getMenuTreeLock()) {
helpMenu = mp;
}
postPaintEvent();
}
/************************************************
*
* Initialization
*
************************************************/
/**
* called from XFramePeer.setMenuBar
*/
public void init(Frame frame) {
this.target = frame;
this.framePeer = AWTAccessor.getComponentAccessor().getPeer(frame);
XCreateWindowParams params = getDelayedParams();
params.remove(DELAYED);
params.add(PARENT_WINDOW, framePeer.getShell());
params.add(TARGET, frame);
init(params);
}
/**
* Overriden initialization
*/
void postInit(XCreateWindowParams params) {
super.postInit(params);
// Get menus from the target.
Vector