Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 83408

Summary: Support links in StyledText
Product: [Eclipse Project] Platform Reporter: Veronika Irvine <veronika_irvine>
Component: SWTAssignee: Felipe Heidrich <eclipse.felipe>
Status: RESOLVED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: bpasero, bradleyjames, c.hauser, daniel_megert, dejan, eclipse, heath.borders, hjbingo19921216, michaelvanmeekeren, mlists, ovidr, scottf, snorthov, susan
Version: 3.1   
Target Milestone: 3.5 M4   
Hardware: PC   
OS: All   
Whiteboard:

Description Veronika Irvine CLA 2005-01-21 11:19:52 EST
StyledText should allow hyperlinks to be specified.  The links must become 
active (cursor changes, underlined, colour change) when the mouse is over the 
link or when the user tabs to the link.  Links must be traversable by 
tabbing.  There needs to be an event when the link is selected and when the 
link is activated (e.g. in IE the status bar shows detailed info about the 
link when the user hovers over it).  The link should support a tooltip.  Must 
be able to specify whether the link is always underlined or only when active.
Comment 1 Veronika Irvine CLA 2005-01-21 13:17:29 EST
*** Bug 83429 has been marked as a duplicate of this bug. ***
Comment 2 Steve Northover CLA 2005-04-04 15:22:53 EDT
Post 3.1.
Comment 3 Felipe Heidrich CLA 2005-11-22 14:27:49 EST
[document from bug 83406] - an image can be a link: 

"A nice enhancement is support of images as links. This means:

1) Image links participate in tabbing order
2) Focus indication is drown around them when in focus
3) Mouse changes when over the image link
4) Image can be changed when mouse is over ('hover' image)
5) The same event is fired as for textual links for enter, exit and activation.

Also, image should be selectable with the rest of the text. It should be 
ignored in simple text clipboard content type but added in RTF content type."
Comment 4 Dani Megert CLA 2008-09-17 06:08:53 EDT
In the textual editors we mimic similar support but don't show the links per default (too many and too expensive to compute) but only when the 'Ctrl' key is pressed and the mouse is over a detected link. For us this new feature would be interesting if existing simple text inside StyledText can be turned into a link on the fly.
Comment 5 Felipe Heidrich CLA 2008-09-24 12:01:50 EDT
For the first cut, the thinking is to implement link as a regular html link (always blue, underline, cursor hover, click action), the same sort that you find in a browser and email rich text editor.
Comment 6 Felipe Heidrich CLA 2008-11-11 17:40:54 EST
Fixed in HEAD > 20081111

use TextStyle.underlineStyle = SWT.UNDERLINE_LINK
that sets single underline and sets the foreground to the native link color.
note that the foreground color can be overwritten by TextStyle.foreground, to overwrite the color only of the underline use TextStyle.underlineColor.

StyledText automatically sets the cursor during mouse move. But it doesn't provide an event for click on link.

One can use TextStyle.data field to store the href, for example:

public void handleMouseDown(Event event) {
 int offset = styledText.getOffsetAtLocation(event.x, event.y);
 StyleRange range styledText.getStyleRangeAtOffset(offset);
 if (range != null && range.underlineStyle == SWT.UNDERLINE_LINK) {
  String href = (String)range.data;
  //do whatever you have to do
 }
}
Comment 7 Dani Megert CLA 2008-11-14 06:47:07 EST
Tried to use it and it almost works ;-)
The problem is that the native underline color isn't used if the text is already colored.

To test
1. start the SWT Text Editor sample
2. add some text
3. give it a foreground color
4. mark it as link
==> native link color is not applied

If you agree that this should work I'll open a bug. For me this is a hard requirement because otherwise I cannot apply the native link color as I don't have a handle on it.
Comment 8 Dani Megert CLA 2008-11-14 06:57:23 EST
Just to clarify: I need the SWT.UNDERLINE_LINK together with these three variants:
- color == null ==> use text color / don't apply a color
- color != null ==> use color
- NEW: some flag (or color) for native link coloring

So either I need
- a handle on the native link color or RGB (preferred)
- a new style SWT.UNDERLINE_LINK_NATIVE which will always use native colors and
  ignore any given colors (underlineColor, foreground) and overwrites the existing
  text color
Comment 9 Felipe Heidrich CLA 2008-11-14 11:16:53 EST
Not sure I understood the problem but in my understanding you can implement all three cases with the current support:

1) link + default text color
style.underline = true;
style.underlineStyle = SWT.UNDERLINE_LINK;
style.underlineColor = null;
style.foreground = styledText.getForeground();

2) link + native link color
style.underline = true;
style.underlineStyle = SWT.UNDERLINE_LINK;
style.underlineColor = null;
style.foreground = null;

3) link + custom color
style.underline = true;
style.underlineStyle = SWT.UNDERLINE_LINK;
style.underlineColor = null;
style.foreground = customColor;

Is it possible for you to use link like this ?
Comment 10 Dani Megert CLA 2008-11-14 11:44:19 EST
Problem is that the text is already colored and the editor framework simply merges the styles it collects from it clients, hence 'null' leave the text colored as is.

What I need is bug 181592.
Comment 11 Felipe Heidrich CLA 2008-11-14 11:46:42 EST
(In reply to comment #8)
> - a handle on the native link color or RGB (preferred)

I like this option better. We have problem report for this: Bug 181592
Comment 12 Dani Megert CLA 2008-11-18 07:18:34 EST
OK, I have reworked my code a bit so that it behaves better when there's already an error/warning at the same place. This rework also removes the need for a separate link color. However, I find that the current native link color is too dark (I filed bug 255630) to track this.
Comment 13 JIABIN HU CLA 2013-04-15 04:40:36 EDT
Recently I met a problem.I test my code with the following example

The contents of styledtext are 

www.yahoo.com
www.yahoo.com
www.yahoo.com

which are in separated lines.

Thus, some of my offsets are duplicated and something is wrong with this sentense "m_text.setStyleRanges(ranges, styles);", where m_text is type of styledtext.

How can I solve this problem? Any suggestion is welcome!