Need custom label for hyperlink in wxTextCtrl

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Nathaniell
Earned a small fee
Earned a small fee
Posts: 14
Joined: Thu Sep 03, 2020 9:10 am

Need custom label for hyperlink in wxTextCtrl

Post by Nathaniell »

Hi,

I have a wxTextCtrl with multiple words separated by comma. I would like to change these words to hyperlinks and react when user clicks on them.
So far I have found out that if I change the style of wxTextCtrl to wxTE_AUTO_URL and wxTE_RICH and change my words to start with http:// (or ftp:// etc.. to be recognized as link) I get them as links and can react to the user clicking on them.. so far so good.

I would now like to know if its possible to hide the http:// from the shown text (basically to act like label for wxHyperlinkCtrl) or if there is some other way how to tell the wxTextCtrl that the words I am providing should be treated as hyperlinks without the need of adding http:// to the string.

I am guessing that it is going to be possible with right use of wxRichTextCtrl... but it would be so much easier if I could do it without it (we don't properly support wxRichTextCtrl atm.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Need custom label for hyperlink in wxTextCtrl

Post by PB »

I don't think it is possible with wxTextCtrl, at least not on Windows. The URI must be present for the hyperlink to work:
https://learn.microsoft.com/en-us/windo ... ct#remarks
New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 469
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: Need custom label for hyperlink in wxTextCtrl

Post by New Pagodi »

With wxStyledTextCtrl, you can set a style to be a hot spot style. When text with that style, is clicked a wxEVT_STC_HOTSPOT_CLICK event will be generated.
Nathaniell
Earned a small fee
Earned a small fee
Posts: 14
Joined: Thu Sep 03, 2020 9:10 am

Re: Need custom label for hyperlink in wxTextCtrl

Post by Nathaniell »

Well, if TextCtrl can't do it... :/

Thanks for suggestion of StyledTextCtrl - I will look into it (hopefully shortly)

I also just realized that it could also be possible to get what I want with just generating multiple wxHypelinkCtrl in a row... with wxWrapSizer it could be pretty identical to wxTextCtrl with multiple words.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Need custom label for hyperlink in wxTextCtrl

Post by PB »

Nathaniell wrote: Mon Oct 03, 2022 8:52 pm I also just realized that it could also be possible to get what I want with just generating multiple wxHypelinkCtrl in a row... with wxWrapSizer it could be pretty identical to wxTextCtrl with multiple words.
If the control is going to be read-only, did you try wxHTMLWindow?
Nathaniell
Earned a small fee
Earned a small fee
Posts: 14
Joined: Thu Sep 03, 2020 9:10 am

Re: Need custom label for hyperlink in wxTextCtrl

Post by Nathaniell »

In case someone stumbles on this in later google search...

In the end, I have styled the text with SetStyle (you need RICH flag) -> set the blue color and underline, and I am reacting on wxEVT_LEFT_UP (wxEVT_COMMAND_LEFT_CLICK didn't work). After the click, I extract the clicked position with HitTest and test that we clicked on the text, not on the empty space in the textCtrl with wxTE_HT_ON_TEXT... and from there, you just extract the word on that position and you are done (and can react to it).
Post Reply