views:

581

answers:

3

When we have a RichEdit control and send it an EM_AUTOURLDETECT message with WPARAM set to TRUE, it nicely hightlights the detected URLs and sends the EN_LINK notifications. But it does this only for text that is entered into the control. I haven't found the way to do it for text that's loaded into the control with SetWindowText or EM_STREAMIN. Please help! Thanks

Upd: I've created a test application from scratch and it works fine there. I think the problem might be that I have superclassed the control, that is, created a new window class and just use the window procedure of the original class. I'm gonna try subclassing the control instead..

A: 

Without knowing the format of the text you are trying to add to the control with SetWindowText and EM_STREAMIN I'm going to take a guess and say this might have something to do with the control's text mode. After setting the contents of the control try sending it a EM_GETTEXTMODE message and see if the TM_PLAINTEXT bit is set. If this is the case then try sending a EM_SETTEXTMODE message followed by EM_AUTOURLDETECT. Your code should look something like this:

UINT textmode = (UINT)::SendMessage(handle_to_control, EM_GETTEXTMODE, 0, 0);
if(textmode & TM_PLAINTEXT) {
    textmode &= ~TM_PLAINTEXT;    // Clear the TM_PLAINTEXT bit
    textmode |= TM_RICHTEXT;      // Set the TM_RICHTEXT bit
    if(::SendMessage(handle_to_control, EM_SETTEXTMODE, textmode, 0) != 0) {
     // Failed to set the text mode
    }
}
::SendMessage(handle_to_control, EM_AUTOURLDETECT, TRUE, 0);
Kevin Loney
Unfortunately this didn't work :( Also this is what MSDN says about the EM_SETTEXTMODE: "The message fails if the control contains any text."
Alex Jenter
@Alex so the code snippet gets to if(::SendMessage(handle_to_control, EM_SETTEXTMODE, textmode, 0) != 0) and then fails?
Kevin Loney
First of all it didn't get to EM_SETTEXTMODE because textmode was already TM_RICHTEXT. Then I put the control into the TM_PLAINTEXT mode manually, EM_SETTEXTMODE still failed. Tried clearing the text and settings it afterwards - didn't help either: SETTEXTMODE succeeded, but no links detected((
Alex Jenter
A: 

You might just have to rewrite the text to the control to get it to re-parse.

veefu
What do you mean exactly by "rewrite"? I'm setting the control's text after sending EM_AUTOURLDETECT, and it's not working.
Alex Jenter
I ran across an example in VB here: http://www.vbforums.com/archive/index.php/t-59959.html in that example, they rewrote the .Text property of the control. I'm not sure how this translate to C++, but if it was doable in VB, it should be doable in C++
veefu
+1  A: 

I just knocked up a basic WTL dialog based app containing a riched20 control and the following works fine:

CRichEditCtrl richedit = GetDlgItem(IDC_RICHEDIT);
richedit.SetAutoURLDetect(TRUE);
richedit.SetWindowText(_T("http://www.stackoverflow.com"));

I have some old MFC code that does something similar, albeit with ES_STREAM, and it works OK too.

FWIW the WTL CRichEditCtrl wrapper is pretty thin. SetAutoURLDetect simply calls SendMessage passing it EM_AUTOURLDETECT.

I am compiling with _RICHEDIT_VER set to 0x0200 FWIW.

Rob
I'm not sure Alex is still monitoring the question, but I think the problem wasn't just setting url detection, but applying it to text that was in the control before you set it.
veefu
Yes, that's exactly the case. With EM_AUTOURLDETECT, the control just detects the URLs which are entered by hand.
Alex Jenter
But in my example above the text isn't entered by hand (SetWindowText) and it works as expected. I'm a bit confused. Do you have some sample code?
Rob
Sample code would involve a lot, since I've built a mini-OO-Framework on top of WinApi. I guess the problem is that I've superclassed the control (it has it's own window class and just uses the window procedure of the original control), but I'm not sure..
Alex Jenter