Bug 300147 - [widgets] TreeItem.getTextBounds() returns width of longest visible item
Summary: [widgets] TreeItem.getTextBounds() returns width of longest visible item
Status: REOPENED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.5.1   Edit
Hardware: Macintosh Mac OS X
: P3 normal with 2 votes (vote)
Target Milestone: ---   Edit
Assignee: Lakshmi P Shanmugam CLA
QA Contact: Silenio Quarti CLA
URL:
Whiteboard: M2
Keywords:
Depends on:
Blocks:
 
Reported: 2010-01-20 03:36 EST by Francis Upton IV CLA
Modified: 2020-08-31 05:40 EDT (History)
7 users (show)

See Also:


Attachments
Fix (10.26 KB, patch)
2010-07-08 17:01 EDT, Scott Kovatch CLA
no flags Details | Diff
Correct fix (10.48 KB, text/plain)
2010-07-08 17:08 EDT, Scott Kovatch CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Francis Upton IV CLA 2010-01-20 03:36:42 EST
I call this on selection on different TreeItems and the only time the width changes is on expansion or contraction if the expansion or contraction makes a different in the total width of all TreeItems (which are in the viewport or not).

I'm using TreeItem.getTextBounds() to determine the end location of the text (to draw a graphic).

Previously I used TreeItem.getBounds() which on Linux and Windows corresponded to the actual text width, however on Cocoa it's the entire width of the tree.

Let me know if you need a snippet and I can make one.

I suppose a workaround (and probably even a better implementation for what I'm trying to do) for my problem is to listen to the Paint event and draw the line there based on the data in the event (like Snippet220).
Comment 1 Scott Kovatch CLA 2010-04-05 22:44:46 EDT
Yes, please add a snippet. I'm not sure what you're trying to do that makes this a problem. Are you drawing something after the item's text?
Comment 2 Francis Upton IV CLA 2010-04-06 00:22:56 EDT
I will get a snippet soon, and yes, I'm trying to draw something after the end of the text (I'm drawing a line).
Comment 3 Scott Kovatch CLA 2010-04-06 20:26:04 EDT
Holding on to this for now until Francis provides a test.
Comment 4 Marcel CLA 2010-07-05 07:44:17 EDT
Maybe that's related:
What I can see (version 3.5.2) is that I can not paint further in the tree than the width of the longest visible item. What I want to do is paint a background color on an entry which spans the tree's width. However this does not work on Mac [carbon] (it actually does on GTK and Win).
I am using a tree without a column.

TreeItem item= (TreeItem) event.item;
Rectangle bounds = item.getParent().getBounds();
event.gc.fillGradientRectangle(bounds.x, event.y, bounds.width, event.height, true); <-- this does not fill the tree's width, event when increasing bounds.width, it does not get painted

If this is not related to this bug, drop me a note, I will then create a new bug.
Comment 5 Scott Kovatch CLA 2010-07-08 15:11:14 EDT
I cc'd Grant, but I was able to modify Snippet283 to demonstrate the bug. We do calculate the right size for the text when sending a MeasureItem or a PaintItem, so I think we should be doing a similar calculation for getTextBounds().
Comment 6 Scott Kovatch CLA 2010-07-08 16:46:54 EDT
(In reply to comment #4)

> If this is not related to this bug, drop me a note, I will then create a new
> bug.

Marcel, this is a different bug. This bug is on Cocoa.
Comment 7 Scott Kovatch CLA 2010-07-08 17:01:05 EDT
Created attachment 173818 [details]
Fix

minor refactoring to remove repeated code, and have getTextBounds call new method for determining width & height of string.
Comment 8 Scott Kovatch CLA 2010-07-08 17:08:50 EDT
Created attachment 173819 [details]
Correct fix

Using wrong string in calculateWidth.
Comment 9 Scott Kovatch CLA 2010-07-08 17:09:32 EDT
Grant, please review for 3.6.1.
Comment 10 Grant Gayed CLA 2010-07-12 10:42:01 EDT
I see this on gtk and carbon as well.  While I think this behaviour is wrong, I don't think a cocoa fix for it should go into 3.6.1.  A better approach would be to fix it for all three (or all two if carbon is discontinued) for 3.7.  Also, I don't think it's new, this has probably been wrong on gtk and carbon since 3.3.

The test snippet I used:

public static void main(String[] args) {
    String text = "asdf";
    Display display = Display.getDefault();
    Shell shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setLayout(new FillLayout());
    Tree tree = new Tree(shell, SWT.NONE);
    new TreeItem(tree, SWT.NONE).setText(text);
    text += text;
    new TreeItem(tree, SWT.NONE).setText(text);
    text += text;
    new TreeItem(tree, SWT.NONE).setText(text);
    text += text;
    new TreeItem(tree, SWT.NONE).setText(text);
    text += text;
    TreeItem[] items = tree.getItems();
    for (int i = 0; i < items.length; i++) {
        System.out.println(items[i].getTextBounds(0));
    }
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) display.sleep();
    }
}
Comment 11 Scott Kovatch CLA 2010-07-12 17:35:27 EDT
Okay, so we agree it's a bug in any event. I may or may not be able to fix it in GTK, but we'll see.
Comment 12 Eclipse Webmaster CLA 2019-09-06 15:29:42 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.
Comment 13 Eclipse Genie CLA 2020-07-07 02:33:11 EDT
New Gerrit change created: https://git.eclipse.org/r/c/platform/eclipse.platform.swt/+/165944
Comment 14 Francis Upton IV CLA 2020-07-07 09:57:54 EDT
While I have moved on from Eclipse, I appreciate a fix to this 10 year old bug!
Comment 16 Lars Vogel CLA 2020-08-14 02:11:55 EDT
This may have caused Bug 566043.
Comment 17 Sravan Kumar Lakkimsetti CLA 2020-08-19 09:08:09 EDT
Verified in 
Eclipse SDK
Version: 2020-09 (4.17)
Build id: I20200819-0600
OS: Mac OS X, v.10.16, x86_64 / cocoa
Java version: 14.0.2
Comment 18 Sravan Kumar Lakkimsetti CLA 2020-08-31 05:25:00 EDT
this has caused Bug 566508. My suggestion is to revert the fix
Comment 19 Niraj Modi CLA 2020-08-31 05:40:37 EDT
(In reply to Sravan Kumar Lakkimsetti from comment #18)
> this has caused Bug 566508. My suggestion is to revert the fix
Fix for this bug is reverted, resetting the target.