Logo
Top sites
Gaming world
Gamefaqs
Navigation
Home
Tutorials
Forum
Chat
Demos
Links

Lets build a functional text editor with full syntax highlighting


You can find an older tutorial covering some of this forother versions of Delphi in Borland Delphi 6 PE's help system.
This tutorial is a rewrite of Borland's tutorial which has been updated and added to.
Instead of using the richedit component we will start off using the memo component and eventually (If you want) we will change that to a synmemo.

You will need:
Delphi 6 PE (www.borland.com)(works in verion 7 but synedit components don't).
Synedit components (Optional) which can be found on sourceforge.net.
about half an hour or so if you want to get all nitty gritty...
A basic understanding of how to get around the Delphi IDE...


First we need a form...
If you have been trying out Delphi you will need to start a new project By doing: file => new => Aplication...
You now have a big empty window that's covered in black dots...
This is a form... A form is the foundation of all WIN32 programs made in Delphi...
When you place components on a form Delphi will attempt to build the code needed and any extra code used to connect the component to ther objects...

Before we do anything we may as well make a folder and save what we have...
Locate your projects folder and create a new folder in it called texteditor...
Click save all and go to your new folder...
Save unit1 as unit1.pas and save the project as texteditor.dpr(you can probably change this but remember that the .exe file is named the same as this file).
If you need to save everything at any time just click save all...
You may find that Delphi has added some other files to your your project folder...
You don't need to edit them but DO NOT delete them...

You should see the object inspector...
Make sure you have Form1 selected and find the caption box in the Object Inspector...
You can change the text that apears in the title bar of your editor here if you wish...
You have a program! (Not a useful one though...)
Press F9 to run it or click the run button...
Exit using the small X button on your program and click view form to show Form1.

Once you know what you want your GUI to look like you can start adding components...
Add a MEMO component from the standard menu (Double click).
Make sure you have the new memo selected and go to the align property in the object inspector...
Select alclient from the dropdown... now, whenever you resize your form your memo will always fill it...
Go to the WIN32 pallet and double click the status bar component to add one...
Select it and change the Simple Text property to untitled.txt in the Object Inspector.
Double click the status bar and add a new panel (in the small dialog that pops up).
Close the panels dialog and you should now have a very basic text editor...

Now we will add menu's and some working commands (Cut, paste etc.).
Add an acionlist from the standard component page...(it's non-visual so put it anywhere...)
Double click on the actionlist to show a dialog where you can add actions or remove them...
Right click in the right box and add a new action (Action1) and select it (Single left click).
Change it's caption property to New (putting an & before it will add a shortcut like Ctrl+N).
Change it's category property to File...
Change it's Hint to something like "Create new file".
Rename it to FileNew...
Add another new action and select it...
Caption = &Save
Category = File
Hint = "Saves current file"
Name = FileSave
Add another and input these:
Caption = &Index
Category = Help
Name = HelpIndex
Do it again with these details:
Caption = &About
Category = Help
Name = HelpAbout
Right click in the right window and sellect New Standard Action...
Ctrl+Left click all the options under Edit and click OK.
You now have a new "Edit" category.
Do the same but select: SaveAs, Open and Exit.
Do it once more but select HelpContents...
Close the actionlist editor and save your work...

Now... We don't want our menu's to be just text so we need an imagelist...
It's under WIN32 and it's non-visual like the actionlist...
Set actionlist1's images property to imagelist1.
Double click the imagelist to add images and click add...
We may as well use Delphi's images for the tut so click add and locate the images folder (May be under borland shared in the common files folder)
Go into the Buttons folder and select the filenew image... If asked about spliting click yes and delete the greyed out one only and click OK.
Open the actionlist and select FileNew, locate the imageindex property and change it to your chosen image... Do this for the rest of them (add images and set them up).

Add a MainMenu component from the standard tab and double click it...
Right click and select Insert... then click on the new box that apears...
Now you can start building your menu's by changing the action of each option.
You can also change the caption of the button you click to drop the menu down (File, Edit etc.).
When your done close the mainmenu editor and add a toolbar from the WIN32 tab.
Right click it and add a button... then set it's action to New...
Do this for the rest of the actions you want on your toolbar... and don't forget to set the images property to imagelist1 or you won't see any images on the buttons...
Save your work and run it to see what you have so far...
Some stuff won't work and it will say Memo1 in the memo box...
To get rid of that we need to do our first bit of PASCAL coding (Not hard at all).
Double click the Form1 from the tree view and type:
memo1.clear;
After begin and before end...
This just empties memo1 when the form is created...

Switch to the code for Unit1 and locate {public declerations}
On the line below type:
FileName: String;
^Like a Variable that doesn't start with Var.^
Open the actionlist and double click the FileNew option.
Put the following between begine and end:
Memo1.clear;
FileName := 'untitled.txt';
statusbar1.panels[0].text := FileName;
Save your stuff and you have a working New command.

Select open in the actionlist and go to the Dialog section in the inspector.
Extend it and set the defaultext to txt.
Double click the filter box and under filter name input: Text files(*.txt)
Under Filter input: *.txt
Do the same but put All files and *.* instead and then click Ok.
Change the Title property to Open File.
Change to the Events tab and double click OnAccept... input the following:
Memo1.Lines.LoadFromFile(FileOpen1.Dialog.FileName);
FileName := FileOpen1.Dialog.FileName;
StatusBar1.Panels[0].Text := FileName;

Double click on FileSave in the actionlist and add this code:
if(FileName = 'untitled.txt') then
FileSaveAs1.Execute
else
Memo1.Lines.SaveToFile(FileName);
If the file hasn't been saved before then open the saveas box so you can name it.

Open the actionlist and select SaveAs.
Change it's dialog properties to:
Defaultext = txt
Filter name = Text Files(*.txt) Filter = *.txt
Filter name = All files(*.*) Filter = *.*
Title = Save As
Go to the event tab and double click beforeexecute and type:
FileSaveAs1.Dialog.InitialDir := ExtractFilePath(Filename);
Then double click on OnAccept and type:
Memo1.Lines.SaveToFile(FileSaveAs1.Dialog.FileName);
FileName := FileSaveAs1.Dialog.FileName;
StatusBar1.Panels[0].Text := FileName;
You now have a save as command...
Save your files and try running it... (F9) (If you get errors then make sure you entered everything right and try again)

You should create a Help file for your program but I won't cover that here...
To create the event handler for the help contents you need to select helpcontents
from the actionlist and double click on it and then you enter the following before begin:
const
HELP_TAB = 15;
CONTENTS_ACTIVE = -3;
And this after:
Application.HelpCommand(HELP_TAB, CONTENTS_ACTIVE);

Double click on the helpindex object and enter the following before begin:
const
HELP_TAB = 15;
INDEX_ACTIVE = -2;
And the following after begin:
Application.HelpCommand(HELP_TAB, INDEX_ACTIVE);

To add an about box go to: File => New => Other => Forms => About box.
Modify this as you wish and then save it as About.pas.
Go to unit1's uses section and add About to it...
Double click on helpabout and type:
AboutBox.ShowModal;
Showmodal stops everything else untill you close the about box. (Make sure you have
helpabout on your menu as a selection or button or you won't be able to view it)

Select Form1 and go to the events tab...
Double click on OnCreate and type this under begin:
Application.HelpFile := ExtractFilePath(Application.ExeName) + 'Helpfilename.hlp';
FileNew.Execute
Save it and you have a (hopefully) fully functional text editor ready for Compiling...
The next part is going to be the better part though as it shows you how to add Syntax
highlighting and other neat stuff...
See ya in part 2!


This tutorial (series) was made for TSM and / or the website(s) it has been submited to by it's creator.
Using other people's material is theft and theft is wrong.
There is no need to steal this as it's free to link to and free to read so please don't.
(TSM 15 Jan 2003.)
All origional content is property of TSM