这并不是一个难题 :-)
你需要将你的SWT.Paint
监听器添加到所有包含的子元素中(递归地),而不仅仅是添加到Composite
。关键是要为每个控件适当地映射坐标...
为了说明,我附上了一些我在许多项目中使用的代码。是的,我知道有一些缺失的类,但你可以从这里大概了解。
package com.rcpcompany.uibindings.utils;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import com.rcpcompany.uibindings.IDisposable;
import com.rcpcompany.uibindings.internal.utils.PaintDecorationManager;
public interface IPaintDecoration {
final class Factory {
private Factory() {
}
public static void addDecoration(IPaintDecoration decoration) {
PaintDecorationManager.addDecoration(decoration);
}
public static IPaintDecoration paintRectangle(final Control c, Rectangle rect, final Color color) {
final Rectangle r;
if (rect == null) {
final Point s = c.getSize();
r = new Rectangle(0, 0, s.x, s.y);
} else {
r = rect;
}
final IPaintDecoration pd = new IPaintDecoration() {
@Override
public void paint(Event event, Rectangle area) {
final Color oldForeground = event.gc.getForeground();
event.gc.setForeground(color);
event.gc.drawRectangle(area.x, area.y, area.width - 1, area.height - 1);
event.gc.setForeground(oldForeground);
}
@Override
public Control getControl() {
return c;
}
@Override
public Rectangle getArea() {
return r;
}
};
addDecoration(pd);
return pd;
}
public static void removeDecoration(IPaintDecoration decoration) {
PaintDecorationManager.removeDecoration(decoration);
}
};
Control getControl();
Rectangle getArea();
void paint(Event event, Rectangle area);
}
并且
package com.rcpcompany.uibindings.internal.utils;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.eclipse.jface.util.Util;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import com.rcpcompany.uibindings.IDisposable;
import com.rcpcompany.uibindings.internal.Activator;
import com.rcpcompany.uibindings.utils.IPaintDecoration;
import com.rcpcompany.utils.logging.LogUtils;
public final class PaintDecorationManager implements IDisposable, Listener {
private final Shell myShell;
private static boolean MAC = Util.isMac();
private PaintDecorationManager(Shell shell) {
myShell = shell;
theManagers.put(getShell(), this);
hookControl(getShell());
}
@Override
public void dispose() {
for (final Control c : myHookedControls.toArray(new Control[myHookedControls.size()])) {
unhookControl(c);
}
theManagers.remove(getShell());
}
public static void addDecoration(IPaintDecoration decoration) {
final PaintDecorationManager mng = getManager(decoration);
if (mng != null) {
mng.addADecoration(decoration);
}
}
public static void removeDecoration(IPaintDecoration decoration) {
final PaintDecorationManager mng = getManager(decoration);
if (mng != null) {
mng.removeADecoration(decoration);
}
}
private final Map<IPaintDecoration, DecorationData> myDecorations = new HashMap<IPaintDecoration, DecorationData>();
public void addADecoration(IPaintDecoration decoration) {
DecorationData dd = myDecorations.get(decoration);
if (dd == null) {
dd = new DecorationData(decoration);
}
dd.update();
}
public void removeADecoration(IPaintDecoration decoration) {
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS) {
LogUtils.debug(this, "control: " + decoration.getControl() + "@" + decoration.getControl().hashCode() + "/"
+ decoration.getArea());
}
final DecorationData dd = myDecorations.get(decoration);
if (dd == null) return;
dd.dispose();
}
private static Map<Shell, PaintDecorationManager> theManagers = new HashMap<Shell, PaintDecorationManager>();
private Shell getShell() {
return myShell;
}
private static PaintDecorationManager getManager(IPaintDecoration decoration) {
final Control c = decoration.getControl();
if (c == null) return null;
final Shell shell = c.getShell();
PaintDecorationManager mng = theManagers.get(shell);
if (mng == null) {
mng = new PaintDecorationManager(shell);
}
return mng;
}
private final Set<Control> myHookedControls = new HashSet<Control>();
public void hookControl(Control control) {
if (myHookedControls.contains(control)) return;
myHookedControls.add(control);
control.addListener(SWT.Dispose, this);
control.addListener(SWT.Paint, this);
if (control != getShell()) {
hookControl(control.getParent());
}
}
public void unhookControl(Control control) {
if (!myHookedControls.contains(control)) return;
myHookedControls.remove(control);
if (!control.isDisposed()) {
control.removeListener(SWT.Dispose, this);
control.removeListener(SWT.Paint, this);
}
for (final DecorationData dd : myDecorations.values()
.toArray(new DecorationData[myDecorations.values().size()])) {
if (dd.getControl() == control) {
dd.dispose();
}
}
}
@Override
public void handleEvent(Event event) {
switch (event.type) {
case SWT.Dispose:
handleDispose(event);
break;
case SWT.Paint:
handlePaint(event);
break;
default:
break;
}
}
private void handleDispose(Event event) {
if (event.widget == getShell()) {
dispose();
return;
}
unhookControl((Control) event.widget);
}
private void handlePaint(Event event) {
final Control c = (Control) event.widget;
final Display display = c.getDisplay();
final Rectangle area = display.map(c, null, event.x, event.y, event.width, event.height);
for (final DecorationData dd : myDecorations.values()) {
if (dd.intersects(area)) {
dd.paint(event);
}
}
}
protected class DecorationData implements IDisposable {
private final IPaintDecoration myDecoration;
private Rectangle myPreviousArea = null;
private boolean isDisposed = false;
protected DecorationData(IPaintDecoration decoration) {
myDecoration = decoration;
myDecorations.put(getDecoration(), this);
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS) {
LogUtils.debug(this, "control: " + this);
}
getManager().hookControl(getDecoration().getControl());
}
public Control getControl() {
return getDecoration().getControl();
}
@Override
public void dispose() {
isDisposed = true;
update();
myDecorations.remove(getDecoration());
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS) {
LogUtils.debug(this, "control: " + this);
}
}
public PaintDecorationManager getManager() {
return PaintDecorationManager.this;
}
public IPaintDecoration getDecoration() {
return myDecoration;
}
public void update() {
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS) {
LogUtils.debug(this, "control: " + this);
}
final Rectangle newArea = getDecorationRectangle(getShell());
if ((newArea == null ? myPreviousArea == null : newArea.equals(myPreviousArea))) {
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS_VERBOSE) {
LogUtils.debug(this, "-- return");
}
return;
}
Rectangle r = null;
if (myPreviousArea != null) {
r = myPreviousArea;
if (newArea != null) {
r.add(newArea);
}
} else {
r = newArea;
}
myPreviousArea = newArea;
if (r != null) {
getShell().redraw(r.x, r.y, r.width, r.height, true);
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS_VERBOSE) {
LogUtils.debug(this, "redraw " + r);
}
}
}
private Rectangle getDecorationRectangle(Control c) {
final Control control = getDecoration().getControl();
final Rectangle b = getDecoration().getArea();
final Rectangle bounds = new Rectangle(b.x, b.y, b.width + 1, b.height + 1);
return getShell().getDisplay().map(control, c, bounds);
}
public void paint(Event event) {
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS_VERBOSE) {
LogUtils.debug(this, "paint: " + event.widget);
}
final Rectangle rect = getDecorationRectangle((Control) event.widget);
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS_VERBOSE) {
LogUtils.debug(this, "paint: " + event.widget + "/" + event.widget.hashCode() + ": rect=" + rect);
}
getDecoration().paint(event, rect);
}
public boolean intersects(Rectangle eventArea) {
if (isDisposed) return false;
if (!getControl().isVisible()) return false;
final Rectangle area = getDecorationRectangle(null);
if (area == null) return false;
if (!area.intersects(eventArea)) return false;
return true;
}
@Override
public String toString() {
return getControl() + "@" + getControl().hashCode() + " " + getDecoration().getArea() + " area "
+ getDecorationRectangle(null);
}
}
}
CCombo
和可能的CTabFolder
存在问题...我今晚稍后会测试您的代码。 - Tonny Madsen