How to determine the correct position of an image inside a CTabItem

I add CTabItem

with Image

to CTabFolder

:

CTabFolder tabFolder = new CTabFolder(someSection, SWT.BORDER);
ImageDescriptor deleteImageDesc = sharedImages.getImageDescriptor(ISharedImages.IMG_ETOOL_DELETE);
Image deleteImage = deleteImageDesc.createImage();
CTabItem tabItem = new CTabItem(tabFolder, SWT.NONE);
tabItem.setImage(deleteImage);
// add more tabs...

      

tabItems

Then I want to create ToolTip

one that appears if the user moves the mouse over deleteImage

.

ToolTip deleteToolTip = new ToolTip(getShell(), SWT.BALOON);
deleteToolTip.setMessage("Delete");
tabFolder.addMouseTrackListener(new MouseTrackAdapter()
{
    @Override
    public void mouseHover(MouseEvent e)
    {
        toolTip.setLocation(tabFolder.toDisplay(e.x, e.y));
        toolTip.setVisible(doesAnyOfTabImagesContainPoint(mousePosition));
    }
});

      

To implement the method doesAnyOfTabImagesContainPoint

I need to determine the position of each deleteImage

. Since CTabItem

it is not Control

, I cannot use the method toDisplay

. I am trying to solve this by manually defining the deleteImage

relative position tabFolder

. This would help as the position of the mouse occupied MouseEvent

was also relative to tabFolder

.

private boolean doesAnyOfTabImagesContainPoint(Point p)
{
    for (CTabItem tabItem : tabFolder.getItems())
    {
        Image i = tabItem.getImage();
        Rectangle tabItemBounds = tabItem.getBounds();
        Rectangle imageBounds = i.getBounds();
        imageBounds.x += tabItemBounds.x;
        imageBounds.y += tabItemBounds.y;
        if (imageBounds.contains(p))
            return true;
    }
    return false;
}

      

The requirement for correct operation is that the Rectangle

returned item i.getBounds()

has the correct position relative to tabItem

. However, it returns (0, 0, 16, 16)

which cannot be right.

The dirty way to fix this would be to just add some constants:

imageBounds.x += bsTabBounds.x + 4;
imageBounds.y += bsTabBounds.y + 3;

      

But I'm wondering if there is a better way. I have been trying to research how the CTabFolder

tab images are positioned, but with no success. Any help would be greatly appreciated. Thanks in advance.

edit: For testing purposes, here's the extracted image I am getting from ISharedImages

is modified to see its border:delete.png

+3


source to share


1 answer


Give the code below. This is based on my answer here .

public static void main(String[] args)
{
    Display display = Display.getDefault();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final TabFolder folder = new TabFolder(shell, SWT.NONE);

    TabItem item = new TabItem(folder, SWT.NONE);
    item.setImage(display.getSystemImage(SWT.ICON_ERROR));
    item.setText("Text");

    folder.addListener(SWT.MouseMove, event -> {
        TabFolder curFolder = (TabFolder) event.widget;
        Point eventLocation = new Point(event.x, event.y);
        TabItem theItem = curFolder.getItem(eventLocation);

        if (theItem == null)
            return;

        Image image = theItem.getImage();

        if (eventLocation.x >= curFolder.getClientArea().x + theItem.getBounds().x + image.getBounds().x
                && eventLocation.x <= curFolder.getClientArea().x + theItem.getBounds().x + image.getBounds().x + image.getBounds().width
                && eventLocation.y >= theItem.getBounds().y + image.getBounds().y
                && eventLocation.y <= theItem.getBounds().y + image.getBounds().y + image.getBounds().height)
        {
            System.out.println("show tooltip");
        }
    });


    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
}

      



I tested it on Windows 7 and it seems to work fine.

+1


source







All Articles