📄 cmenu.java
字号:
}
protected void handleMouseMove(Event e) {
if(e.x >= BORDER + LEFT_MARGIN && e.x < size.x - BORDER - RIGHT_MARGIN) {
CMenuItem item = getItemAtY(e.y);
if(item != activeItem) {
// 隐藏前面的子菜单
if(activeChild != null)
activeChild.hide(false);
activeChild = null;
// 设置当前item,重画菜单
activeItem = item;
// 看看是不是要显示新的子菜单
if(item != null && item.isEnabled() && item.getStyle() == SWT.CASCADE) {
CMenu child = activeItem.getChild();
if(child != null) {
Point shellLoc = menuShell.getLocation();
int childX, childY;
Point childSize = child.getSize();
Rectangle displayBound = menuShell.getDisplay().getBounds();
int rightExceed = shellLoc.x + size.x - BORDER - RIGHT_MARGIN + childSize.x - displayBound.width;
int leftExceed = 0 - (shellLoc.x - childSize.x + BORDER + LEFT_MARGIN);
if(rightExceed <= 0 || rightExceed <= leftExceed)
childX = shellLoc.x + size.x - BORDER - RIGHT_MARGIN;
else
childX = -leftExceed;
int itemY = getYAtItem(activeItem);
int topExceed = 0 - (shellLoc.y + itemY + getItemHeight() - childSize.y);
int bottomExceed = shellLoc.y + itemY + childSize.y - displayBound.height;
if(bottomExceed <= 0 || bottomExceed <= topExceed)
childY = shellLoc.y + itemY;
else
childY = -topExceed;
child.setLocation(childX, childY);
child.setParent(this);
activeChild = child;
child.setVisible(true);
}
}
menuShell.redraw();
}
}
}
protected void handlePaint(Event e) {
if(e.width == 0 || e.height == 0)
return;
int x = BORDER + LEFT_MARGIN;
int y = Math.max(TOP_MARGIN + BORDER, e.y);
CMenuItem item = getItemAtY(y);
if(item == null)
return;
y = getYAtItem(item);
GC gc;
Image bufferImage = null;
int startX, startY;
if(DOUBLE_BUFFERED) {
bufferImage = new Image(menuShell.getDisplay(), e.width, e.height);
gc = new GC(bufferImage);
startX = startY = 0;
y -= e.y;
x -= e.x;
} else {
gc = e.gc;
startX = e.x;
startY = e.y;
}
// 画背景
gc.setBackground(background);
gc.fillRectangle(startX, startY, e.width, e.height);
// 画边框
gc.setForeground(borderColor);
if(e.x == 0)
gc.drawLine(0, startY, 0, startY + e.height);
if(e.y == 0)
gc.drawLine(startX, 0, startX + e.width, 0);
if(e.x + e.width == size.x)
gc.drawLine(startX + e.width - 1, startY, startX + e.width - 1, startY + e.height);
if(e.y + e.height == size.y)
gc.drawLine(startX, startY + e.height - 1, startX + e.width, startY + e.height - 1);
// 画item
gc.setForeground(textColor);
int itemHeight = getItemHeight();
int itemIndex = items.indexOf(item);
int itemCount = items.size();
int pEnd = startY + e.height;
while(y < pEnd && itemIndex < itemCount) {
// get item
item = items.get(itemIndex);
// 检查可见性
if(!item.isVisible()) {
itemIndex++;
continue;
}
// set foreground
if(item == activeItem)
gc.setForeground(textHoverColor);
if(!item.isEnabled())
gc.setForeground(disabledTextColor);
// draw item
if(item.getStyle() == SWT.SEPARATOR) {
int quater = (size.x - (BORDER << 1) - LEFT_MARGIN - RIGHT_MARGIN - ITEM_LEFT_MARGIN - ITEM_RIGHT_MARGIN) >> 2;
int sX = x + ITEM_LEFT_MARGIN;
int sY = y + ((SEPARATOR_HEIGHT - 1) >> 1);
gc.setForeground(LIGHT_SEPARATOR);
gc.setBackground(DARK_SEPARATOR);
gc.fillGradientRectangle(sX, sY, quater, 1, false);
sX += quater;
gc.setForeground(DARK_SEPARATOR);
gc.drawLine(sX, sY, sX + quater + quater, sY);
sX += quater + quater;
gc.setBackground(LIGHT_SEPARATOR);
gc.fillGradientRectangle(sX, sY, quater, 1, false);
y += SEPARATOR_HEIGHT + VERTICAL_SPACING;
} else {
// 画背景
if(item == activeItem) {
gc.setBackground(activeBackground);
gc.fillRectangle(x, y, size.x - (BORDER << 1) - LEFT_MARGIN - RIGHT_MARGIN, itemHeight);
}
if(item.getStyle() == SWT.PUSH || item.getStyle() == SWT.CASCADE) {
// 画图片
Image image = item.getImage();
if(image != null) {
Rectangle bound = image.getBounds();
gc.drawImage(image,
0,
0,
bound.width,
bound.height,
x + ITEM_LEFT_MARGIN,
y + ITEM_TOP_MARGIN,
imageSize,
imageSize
);
}
} else if(item.getStyle() == SWT.CHECK && item.isSelected()) {
int cX = x + ITEM_LEFT_MARGIN + ((imageSize - 7) >> 1);
int cY = y + ((itemHeight - 7) >> 1);
gc.drawLine(cX, cY + 2, cX, cY + 4);
gc.drawLine(cX + 1, cY + 3, cX + 1, cY + 5);
gc.drawLine(cX + 2, cY + 4, cX + 2, cY + 6);
gc.drawLine(cX + 3, cY + 3, cX + 3, cY + 5);
gc.drawLine(cX + 4, cY + 2, cX + 4, cY + 4);
gc.drawLine(cX + 5, cY + 1, cX + 5, cY + 3);
gc.drawLine(cX + 6, cY, cX + 6, cY + 2);
} else if(item.getStyle() == SWT.RADIO && item.isSelected()) {
int rX = x + ITEM_LEFT_MARGIN + ((imageSize - 6) >> 1);
int rY = y + ((itemHeight - 6) >> 1);
gc.setBackground((item == activeItem) ? textHoverColor : textColor);
gc.fillRectangle(rX, rY + 1, 6, 4);
gc.fillRectangle(rX + 1, rY, 4, 6);
}
// 画文字
gc.drawText(item.getText(),
x + ITEM_LEFT_MARGIN + imageSize + IMAGE_TEXT_SPACING,
y + ((itemHeight - fontHeight) >> 1),
true);
if(item.getStyle() == SWT.CASCADE) {
int arrowX = x + size.x - BORDER - ITEM_RIGHT_MARGIN - ARROW;
int arrowY = (itemHeight - 7) >> 1;
gc.drawLine(arrowX, y + arrowY, arrowX, y + arrowY + 6);
gc.drawLine(arrowX + 1, y + arrowY + 1, arrowX + 1, y + arrowY + 5);
gc.drawLine(arrowX + 2, y + arrowY + 2, arrowX + 2, y + arrowY + 4);
gc.drawLine(arrowX + 3, y + arrowY + 3, arrowX + 3, y + arrowY + 3);
}
y += itemHeight + VERTICAL_SPACING;
}
itemIndex++;
gc.setForeground(textColor);
}
if(DOUBLE_BUFFERED) {
e.gc.drawImage(bufferImage, e.x, e.y);
bufferImage.dispose();
gc.dispose();
}
}
CMenuItem getItemAtY(int y) {
y -= TOP_MARGIN + BORDER;
if(y < 0)
return null;
int itemHeight = getItemHeight();
for(CMenuItem item : items) {
if(!item.isVisible())
continue;
if(item.getStyle() == SWT.SEPARATOR)
y -= SEPARATOR_HEIGHT + VERTICAL_SPACING;
else
y -= itemHeight + VERTICAL_SPACING;
if(y <= 0)
return item;
}
return null;
}
int getYAtItem(CMenuItem i) {
int y = TOP_MARGIN + BORDER;
int itemHeight = getItemHeight();
for(CMenuItem item : items) {
if(!item.isVisible())
continue;
if(item == i)
return y;
if(item.getStyle() == SWT.SEPARATOR)
y += SEPARATOR_HEIGHT + VERTICAL_SPACING;
else
y += itemHeight + VERTICAL_SPACING;
}
return -1;
}
private int getItemHeight() {
return Math.max(fontHeight, imageSize) + ITEM_TOP_MARGIN + ITEM_BOTTOM_MARGIN;
}
/**
* @return Returns the location.
*/
public Point getLocation() {
return location;
}
/**
* @param location The location to set.
*/
public void setLocation(Point location) {
this.location = location;
}
/**
* @return Returns the visible.
*/
public boolean isVisible() {
return visible;
}
/**
* @param visible The visible to set.
*/
public void setVisible(boolean visible) {
this.visible = visible;
if(visible)
show();
else
hide();
}
/**
* @return Returns the activeBackground.
*/
public Color getActiveBackground() {
return activeBackground;
}
/**
* @param activeBackground The activeBackground to set.
*/
public void setActiveBackground(Color activeBackground) {
this.activeBackground = activeBackground;
}
/**
* @return Returns the background.
*/
public Color getBackground() {
return background;
}
/**
* @param background The background to set.
*/
public void setBackground(Color background) {
this.background = background;
}
/**
* @return Returns the textColor.
*/
public Color getTextColor() {
return textColor;
}
/**
* @param textColor The textColor to set.
*/
public void setTextColor(Color textColor) {
this.textColor = textColor;
}
/**
* @return Returns the textHoverColor.
*/
public Color getTextHoverColor() {
return textHoverColor;
}
/**
* @param textHoverColor The textHoverColor to set.
*/
public void setTextHoverColor(Color textHoverColor) {
this.textHoverColor = textHoverColor;
}
/**
* @return Returns the borderColor.
*/
public Color getBorderColor() {
return borderColor;
}
/**
* @param borderColor The borderColor to set.
*/
public void setBorderColor(Color borderColor) {
this.borderColor = borderColor;
}
public void setLocation(int x, int y) {
if(location == null)
location = new Point(x, y);
location.x = x;
location.y = y;
}
/**
* @return Returns the parent.
*/
CMenu getParent() {
return parent;
}
/**
* @param parent The parent to set.
*/
void setParent(CMenu parent) {
this.parent = parent;
}
/**
* @return Returns the sizeDirty.
*/
boolean isSizeDirty() {
return sizeDirty;
}
/**
* @param sizeDirty The sizeDirty to set.
*/
void setSizeDirty(boolean sizeDirty) {
this.sizeDirty = sizeDirty;
}
/**
* @return the imageSize
*/
public int getImageSize() {
return imageSize;
}
/**
* @param imageSize the imageSize to set
*/
public void setImageSize(int imageSize) {
this.imageSize = imageSize;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -