Wednesday, November 30, 2016

RTF Keyword Highlighting

In 2009 I started playing with an RTF control and I created one organizer for my own need to store articles and any other useful info I can find in the web for later offline reading and reference.  I decided to do that because most of the time we don't have good internet here.

Well I was reworking the previous days on that old DevOrg app which consisted of a treeview and an RTF activeX controls as I decided to finally give it a filtering capability, insertion of image with rescaling and quality reduction, reworking the icon panel to look like a ribbon, etc.; and along the way, I thought of allowing it to highlight the words I typed to filter the threads and nodes of treeview.  So I can see outright where those are, like this:




The trick is to work behind the scene on its RTF codes. Here is the method I created for that:



*_HighLight Method

Lparameters cWord
LOCAL lcTable
lcTable = thisform._currenttable
SELECT(m.lcTable)
Try
   * Insert highlight color
   lcRTFSource = Strtran(rtftext,'}}','}}'+Chr(13)+'{\colortbl ;\red0\green255\blue0;}')
   * Add new highlightings.  Check proper, small, all caps
   lcRTFSource = Strtran(m.lcRTFSource,m.cWord,'\highlight1 '+m.cWord+'\highlight0 ')
   lcRTFSource = Strtran(m.lcRTFSource,Upper(m.cWord),'\highlight1 '+Upper(m.cWord)+'\highlight0 ')
   lcRTFSource = Strtran(m.lcRTFSource,Lower(m.cWord),'\highlight1 '+Lower(m.cWord)+'\highlight0 ')
   lcRTFSource = Strtran(m.lcRTFSource,Proper(m.cWord),'\highlight1 '+Proper(m.cWord)+'\highlight0 ')
   Thisform.corTF.olerTF.textRTF=m.lcRTFSource
Catch
   Thisform.corTF.olerTF.textRTF = rtftext
Endtry


If you notice, I have used three (3) possible cases, i.e., UPPER(), LOWER() and PROPER().  The problem is I can't force the RTF codes to any of these cases alone to ensure I can highlight everything as that will also affect the output.  So say I filter threads for entries with the word "javascript", then it can find and highlight words with javascript, JAVASCRIPT and Javascript.  But not JavaScript.

While I can instruct it with another function/method to look for any other transmutation of the word like JavaScript, JAvascript, jaVaScript, etc., I think that is an overkill for this simple need of mine.

So anyway, I am just showing here the trick on how to achieve that in case you use and RTF tool on your end and wanted to have the same on your end.  Cheers!

No comments:

Post a Comment