java - How to determine the correct position of an Image inside of a CTabItem -
i add ctabitem
s image
s 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...
then want create tooltip
appears if user moves mouse on 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 method doesanyoftabimagescontainpoint
need determine position of every deleteimage
. since ctabitem
isn't control
cannot use method todisplay
. try solve manually determining position of deleteimage
relative tabfolder
. because mouse position held mouseevent
relative tabfolder
.
private boolean doesanyoftabimagescontainpoint(point p) { (ctabitem tabitem : tabfolder.getitems()) { image = 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 working rectangle
returned i.getbounds()
has correct location relative tabitem
. returns (0, 0, 16, 16)
cannot right.
a dirty way fix add constants:
imagebounds.x += bstabbounds.x + 4; imagebounds.y += bstabbounds.y + 3;
but wondering if there's better way. i'm trying investigate how ctabfolder
positions images of tabs without success now. appreciated. in advance.
edit: testing purposes here's extracted image isharedimages
modified see border:
give code below shot. it's based on 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've tested on windows 7 , seems working fine.
Comments
Post a Comment