Tuesday, December 15, 2009

Using Scintilla in Delphi 7

Scintilla is a cool open source text editing control which supports syntax highlighting, code folding, auto-indentation, line numbers and many more features.

A list of projects using scintilla can be found on their website (http://www.scintilla.org/ScintillaRelated.html).
To use Scintilla in Delphi you must install Delphi Scintilla Interface Components
 a.k.a delphisci which is no longer under development but since Delphi 7 itself is older, this shouldn’t be a problem.

Step 0
I’ll use the following names in this tutorial:
 - DELPHI_INSTALLATION_FOLDER: The folder where your Delphi is installed
 - SYSTEM32: C:\Windows\System32
 - DELPHI_SCI_FOLDER: The folder where you unzip delphisci

Step 1 (Getting ready)
Get a copy of delphisci from sourceforge, unzip it and inside the unzipped folder, there should be a zip file called scilexer_precompiled_171CVS which contains scilexer.dll (the only external dependency) and put this scilexer.dll in SYSTEM32 if there isn’t one yet.

Step 2 (Installation)
In DELPHI_SCI_FOLDER, there's file names INSTALL which has the instructions for installing delphisci. Follow those instructions. If you struggle with that make sure you:
  1. Compiled the right project group for the version of Delphi you are using.
  2. Installed the package into Delphi (Components->Install Package-> [choose scitD7.bpk from DELPHI_INSTALLATION_FOLDER\Projects\Bpl or go to Tools->Environment Options->Library and ckeck where your Bpl Output Directory is])
Step 3 (Successfully running it)
If you successfully followed those instructions you should have a Scintilla tab on the components pallette but if you try to run any project using these Scintilla components you get a “[Fatal Error] … File not found: SciLexer.dcu”. That happens because you haven’t added the DELPHI_SCI_FOLDER \d7dcu folder to the Environment yet so Delphi is unable to locate these.

Step 3.14
To add the DELPHI_SCI_FOLDER \d7dcu folder to the Environment I’d normally create a folder in DELPHI_INSTALLATION_FOLDER\Scintilla and copy all the files from DELPHI_SCI_FOLDER \d7dcu into this new folder.

Step 3.5
Now on Delphi, go to Tools->Environment Options->Library and add DELPHI_INSTALLATION_FOLDER\Scintilla to the Library Path

Step 4 (Testing)
Now to test the installation, create a new application, drop some Scintilla controls to the form and Run it.


Step 5 (Using syntax highlighting)
When you first drop a TScintilla components on a form, it has no language set and getting the syntax highlighting to work took some working until I realised that you have to Right click on the control -> Select Prefered Languages -> Choose a language and press OK. Then on the property editor, expand the LanguageManager and on the SelectedLanguage drop-down pick the language you chose earlier.
And that's it!


Proceed to Let’s cook a simple yet powerful HTML Editor with Delphi for some action.

FIN

No comments:

Post a Comment