Thursday, May 22, 2008

Embedding link to file in textbox contents

I just found a newgroup entry asking how to add "links" to Textboxes. So it's time again to give some examples to a goodie I wrote.

Welcome to "US RichTextEdit Extensions". This goodie enables some capabilites in the Windows RTF control. Surprisingly enough the RTF control already supports Links and is also able to automatically detect URLs and turn them into links. If the link you want is not a URL you have to make them linkable on your own - but thats not very hard.

Let's see some code:


presenter := RichTextPresenter

            showOn: 'This is my E-Mail Address: mailto:Udo.Schneider@homeaddress.de .
Just drop me a mail or visit me here: http://readthesourceluke.blogspot.com/'


                    asRichText.

(presenter view)

    autoUrlDetect: true;

    link: true;

    enableMailtoLinks.

presenter

    when: #linkClicked:

    send: #value:

    to:

        [:link |

        MessageBox notify: link

            caption: 'Link clicked']


So if you have a RichTextPresenter view you can use #autoUrlDetect: to enable/disable the automatic linking of URLs. #link: is used to enable linking in general. So even if #autoUrlDetect is true you won't see any links until you set #link: to true.

The package also contains a method to make mailto: links linkable.
#enableMailtoLinks parses the text and for mailto: links and hides the mailto: prefix so that only the email address remains. However the link itself still contains the mailto: + email combinations. This is a good example how to use non-visible pre- and postfixes for link.

This is i.e. useful if you want to present a "simple link" to the user but need a more descriptive link for your program.

To capture clicked links you simply have to hook up the the #linkClicked: event in your #createSchemanticWiring method. The event parameter is the complete link - not only the user visible part. So in the mailto: link above you will get a "mailto:Udo.Schneider@homeaddress.de" although only "Udo.Schneider@homeaddress.de" is visible to the user.

No comments: