Delphi Tutorial Creating A Text Editor Using Delphi PDF
Delphi Tutorial Creating A Text Editor Using Delphi PDF
Borland
Kylix 3
COPYRIGHT 20012002 Borland Software Corporation. All rights reserved. All Borland brand and product names
are trademarks or registered trademarks of Borland Software Corporation in the United States and other countries.
All other marks are the property of their respective owners.
K3Delphi_TE0702
Contents
Creating a text editor using
the Delphi IDE
Starting a new application . . . . .
Setting property values . . . . . .
Adding components to the form .
Adding support for a menu
and a toolbar . . . . . . . . . . .
Adding actions to the action list
Adding standard
actions to the action list . . . .
Adding images to the image list
Adding a menu . . . . . . . . . . .
Clearing the text area . . . . . . . .
Adding a toolbar . . . . . . . . . .
. . . . . . . . 1
. . . . . . . . 3
. . . . . . . . 3
. . . . . . . . 6
. . . . . . . . 7
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. 9
11
13
15
16
Index
iii
. . . . . . . . 17
. . . . . . . . 17
. . . . . . . . 19
. . . . . . . . 21
. . . . . . . . 22
. . . . . . . . 23
. . . . . . . . 24
. . . . . . . . 26
iv
Chapter0
This tutorial guides you through the creation of a text editor complete with menus, a
toolbar, and a status bar. You will use the Delphi IDE to create the text editor.
This tutorial assumes you are familiar with Linux and have read the introduction to
Kylix programming and the IDE in the Quick Start.
Note
3 Choose File|Save All to save your files to disk. When the Save dialog box appears:
Navigate to your TextEditor folder.
Save Unit1 using the default name Unit1.pas.
Save the project using the name TextEditor.dpr. (The executable will be named
the same as the project name without an extension.)
Later, you can resave your work by choosing File|Save All.
When you save your project, Kylix creates additional files in your project
directory. Do not delete these files.
When you open a new project, Kylix displays the projects main form, named
Form1 by default. Youll create the user interface and other parts of your
application by placing components on this form.
Run the form now by pressing F9, even though there are no components on it.
To return to the design-time view of Form1, either:
Click the X in the upper right corner of the title bar of your application
(the runtime view of the form);
Click the Exit application button in the upper left corner of the title bar and
select Close from the menu;
Choose Run|Program Reset; or
Choose View|Forms, select Form1, and click OK.
Tutorial
1 To create a text area, first add a Memo component. To find the Memo component,
on the Standard tab of the Component palette, point to an icon on the palette for a
moment; Kylix displays a Help tooltip showing the name of the component.
The Memo component now fills the entire form so you have a large text editing
area. By choosing the alClient value for the Align property, the size of the Memo
control will vary to fill whatever size window is displayed even if the form is
resized.
Tutorial
4 Next you want to create a place on the status bar to display the path and file name
of the file being edited by your text editor. The easiest way is to provide one status
panel.
Change the SimpleText property to untitled.txt. If the file being edited is not yet
saved, the name will be untitled.txt.
Set Simple Panel to True.
Editing area
Status bar
Click the ellipse of the Panels property to open the Editing StatusBar1.Panels
dialog box. You can stretch the dialog box to make it larger.
Right-click the dialog box and click Add to add a panel to the status bar.
Tip
You can also access the Editing StatusBar1 Panels dialog box by double-clicking
the status bar.
5 Click the X in the upper right corner to close the Editing StatusBar1.Panels dialog
box.
Now the main editing area of the user interface for the text editor is set up.
File
Yes
Open
File
Yes
Save
File
Yes
Save As
File
No
Stores a file using a new name (also lets you store a new file
using a specified name).
Exit
File
Yes
Cut
Edit
Yes
Copy
Edit
Yes
Paste
Edit
Yes
About
Help No
You can also centralize images to use for your toolbar and menus in an image list.
To add an ActionList and an ImageList component to your form:
Tutorial
By convention, well name actions that are connected to menu items with the name of
the top-level menu and the item name. For example, the FileExit action refers to the
Exit command on the File menu.
3 In the Object Inspector, set the following properties for the action:
After Caption, type &New. Note that typing an ampersand before one of the letters
makes that letter a shortcut to accessing the command.
After Category, type File (this organizes the File commands in one place).
After Hint, type Create file (this will be the Help tooltip).
After ImageIndex, type 0 (this will associate image number 0 in your ImageList
with this action).
After Name, type FileNew (for the File|New command) and press Enter to save
the change.
With the new action
selected in the Action
List editor, change its
properties in the Object
Inspector.
Caption is used in the
menu, Category is the
type of action, Hint is a
Help tooltip, ImageIndex
lets you refer to a
graphic in the ImageList,
and Name is what its
called in the code.
Tutorial
12 Right-click on the Action List editor and choose New Action to create a Help|
About command.
13 In the Object Inspector, set the following properties:
The Action List editor should still be displayed. If its not, double-click the ActionList
icon on the form.
To add standard actions to the action list:
1 Right-click on the Action List editor and choose New Standard Action.
The Standard Actions dialog box appears.
Right-click on the Action List editor and
choose New Standard Action.
The available
standard actions
are then
displayed. To
pick one, doubleclick an action.
2 Right-click on the Action List editor and choose New Standard Action.
Double-click TEditCopy.
In the Object Inspector, set the ImageIndex property to 5.
3 Right-click on the Action List editor and choose New Standard Action.
Double-click TEditPaste.
In the Object Inspector, set the ImageIndex property to 6.
4 Now youve got all the actions that youll need for the menus and toolbar. If you
click the category All Actions, you can see all the actions in the list:
Click the down arrow next to the Images property. Select ImageList1. This associates the
images that youll add to the image list with the actions in the action list.
10
Tutorial
Command Images
Command
ImageIndex property
File|New
Filenew.bmp
File|Open
Fileopen.bmp
File|Save
Filesave.bmp
File|Exit
Doorshut.bmp
Edit|Cut
Cut.bmp
Edit|Copy
Copy.bmp
Edit|Paste
Paste.bmp
1 Double-click the ImageList object on the form to display the Image List editor.
2 Click the Add button and navigate to the Buttons directory provided with the
product. The default location is {install directory}/images/buttons. For example,
if Kylix is installed in your /usr/local/kylix directory, look in /usr/local/kylix/
images/buttons.
3 Double-click fileopen.bmp.
4 When a message asks if you want to separate the bitmap into two separate ones,
click Yes each time. Each of the icons includes an active and a grayed out version
of the image. Youll see both images. Delete the grayed out (second) image.
Click Add. Double-click filenew.bmp. Delete the grayed out image.
Click Add. Double-click fileopen.bmp. Delete the grayed out image.
Click Add. Double-click filesave.bmp. Delete the grayed out image.
Click Add. Double-click doorshut.bmp. Delete the grayed out image.
Click Add. Double-click cut.bmp. Delete the grayed out image.
Click Add. Double-click copy.bmp. Delete the grayed out image.
11
Note
6 To see the associated icons on the action list, double-click the ActionList object
then select the All Actions category.
When youre done close the Action List editor. Now youre ready to add the menu
and toolbar.
12
Tutorial
Adding a menu
Adding a menu
In this section, youll add a main menu bar with three drop-down menusFile, Edit,
and Helpand youll add menu items to each one using the actions in the action list.
1 From the Standard tab of the Component palette, drop a MainMenu component
onto the form. It doesnt matter where you place it.
2 Set the main menus Images property to ImageList1 so you can add the bitmaps to
the menu items.
3 Double-click the main menu component to display the Menu Designer.
4 In the Object Inspector, after Caption, type &File, and press Enter to set the first toplevel menu item.
13
Adding a menu
5 In the Menu Designer, select the File item you just created. Youll notice an empty
item under it: select the empty item. In the Object Inspector, choose the Action
property. The Actions from the action list are all listed there. Select FileNew.
When you select
FileNew from the
Action property list,
the New command
appears with the
correct Caption
and ImageIndex.
Select the item under New and set its Action property to FileOpen.
Select the item under Open and set its Action property to FileSave.
Select the item under Save and set its Action property to FileSaveAs.
Select the item under Save As, type a hyphen after its Caption property, and
press Enter. This creates a separator bar on the menu.
Select the item under the separator bar and set its Action property to FileExit.
14
Tutorial
You can also run the project by clicking the Run button on the Debug toolbar or
choosing Run|Run.
When you run your project, Kylix opens the program in a window like the one
you designed on the runtime form. The menus all work although most of the
commands are grayed out. The images appear next to the menu items with which
we associated the icons.
Though your program already has a great deal of functionality, theres still more
to do to activate the commands. And we want to add a toolbar to provide easy
access to the commands.
15
Adding a toolbar
Adding a toolbar
Since youve set up actions in an action list, you can add some of those actions to a
toolbar.
1 On the Common Controls tab of the Component palette, double-click the ToolBar
component to add it to the form.
A blank toolbar is added under the main menu. With the toolbar still selected,
change the following properties in the Object Inspector:
Set the Indent property to 4. (This indents the icons 4 pixels from the left of the
toolbar.)
Set the Images property to ImageList1.
Set the ShowHint property to True. (Tip: Double-click False to change it to True.)
Note
3 Assign actions from the action list to the first set of buttons.
16
Tutorial
Select the first button and set its Action property to FileExit.
Select the second button and set its Action property to FileNew.
Select the third button and set its Action property to FileOpen.
Select the fourth button and set its Action property to FileSave.
6 Click the X in the upper right corner to close the application and return to the
design-time view.
1 Choose View|Units and select Unit1 to display the code associated with Form1.
2 You need to declare a file name that will be used in the event handler. Add a
custom property for the file name to make it globally accessible. Early in the
Unit1.pas file, locate the public declarations section for the class TForm1 and on
the line after:
{ Public declarations }
type:
FileName: String;
17
Tip
4 Double-click the ActionList icon on the form to display the Action List editor.
5 In the Action List editor, select the File category and then double-click the FileNew
action.
The Code editor opens with the cursor inside the event handler.
First, double-click the Action List object to display
the Action List editor.
18
Tutorial
6 Right where the cursor is positioned in the Code editor (between begin and end),
type the following lines:
Memo1.Clear;
FileName := 'untitled.txt';
StatusBar1.Panels[0].Text := FileName;
Your event handler should look like this when youre done:
You can resize the code portion of the window to reduce horizontal scrolling.
19
type *.txt. In the second row of the Filter Name column, type All files (*) and
in the Filter column, type *. Click OK.
4 The Action List editor should still be displayed. If its not, double-click the
ActionList icon on the form.
5 In the Action List editor, double-click the FileOpen action.
The Code editor opens with the cursor inside the event handler.
6 Right where the cursor is positioned in the Code editor (between begin and end),
type the following lines:
if OpenDialog1.Execute then
begin
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
FileName := OpenDialog1.FileName;
StatusBar1.Panels[0].Text := FileName;
end;
Your FileOpen event handler should look like this when youre done:
Thats it for the File|Open command and the Open dialog box.
20
Tutorial
1 The Action List editor should still be displayed. If its not, double-click the
ActionList icon on the form.
2 On the Action List editor, double-click the FileSave action.
The Code editor opens with the cursor inside the event handler.
3 Right where the cursor is positioned in the Code editor (between begin and end),
type the following lines:
if (FileName = 'untitled.txt') then
FileSaveAsExecute(nil)
else
Memo1.Lines.SaveToFile(FileName);
This code tells the text editor to display the SaveAs dialog box if the file isnt
named yet so the user can assign a name to it. Otherwise, it saves the file using its
current name. The SaveAs dialog box is defined in the event handler for the Save
As command on page -22. FileSaveAsExecute is the automatically generated name
for the Save As command.
Your event handler should look like this when youre done:
21
1 From the Dialogs tab of the Component palette, drop a SaveDialog component onto
the form. This is a nonvisual component, so it doesnt matter where you place it.
Kylix names it SaveDialog1 by default. (When SaveDialogs Execute method is
called, it invokes a standard dialog box for saving files.)
2 In the Object Inspector, set the following properties of SaveDialog1:
Set DefaultExt to txt.
Double-click the text area next to Filter to display the Filter Editor. In the first
row under the Filter Name column, type Text files (*.txt). In the Filter
column, type *.txt. In the second row under the Filter Name column, type All
files (*) and in the Filter column, type *. Click OK.
Set Title to Save As.
The Action List editor should still be displayed. If its not, double-click the
ActionList icon on the form.
Note
4 Right where the cursor is positioned in the Code editor, type the following lines:
SaveDialog1.FileName := FileName;
SaveDialog1.InitialDir := ExtractFilePath(Filename);
if SaveDialog1.Execute then
begin
Memo1.Lines.SaveToFile(SaveDialog1.FileName);
FileName := SaveDialog1.FileName;
StatusBar1.Panels[0].Text := FileName;
end;
Your FileSaveAs event handler should look like this when youre done:
This sets the SaveAs dialog
boxs FileName property to
the main forms FileName
property value.
The default directory is set
to the last one accessed.
This line saves the text to
the specified file.
This sets the main forms
FileName to the name
specified in the SaveAs
dialog box.
This puts the file name in
the text panel of the status
bar.
22
Tutorial
1 The Action List editor should still be displayed. If its not, double-click the
ActionList icon on the form.
2 On the Action List editor, double-click the FileExit action.
The Code editor opens with the cursor inside the event handler.
3 Right where the cursor is positioned in the Code editor, type the following line:
Close;
This calls the close method of the main form. Thats all you need to do for the File|
Exit command.
Most of the buttons and toolbar buttons work but were not finished yet.
5 To return to design mode, close the text editor application by clicking the X.
If you receive any error messages, click them to locate the error. Make sure youve
followed the steps as described in the tutorial.
23
1 Choose File|New|Other to display the New Items dialog box and click the Forms
tab.
2 On the Forms tab, double-click About Box.
3 Select the grid itself (click the grid portion) and in the Object Inspector, click the
Properties tab and change the Caption property to About Text Editor.
4 Select the following TLabel items on the About box and in the Object Inspector,
change their Caption properties:
Change Product Name to Text Editor.
Change Version to Version 1.0.
24
Tutorial
5 Save the About box form by choosing File|Save As and saving it as About.pas.
6 In the Code editor, you should have these files displayed: Unit1 and About. Click
the Unit1 tab to display Unit1.pas.
7 Add the new About unit to the uses section of Unit1 by typing the word About at
the end of the list of included units in the uses clause.
Click the tab to display a file associated with a unit. If you open
other files while working on a project, additional tabs appear on the
Code editor.
When you create a new form for your application, you need to add it to
the uses clause of the main form. Here were adding the About box.
8 On the action list, double-click the HelpAbout action to create an event handler.
9 Right where the cursor is positioned in the Code editor, type the following line:
AboutBox.ShowModal;
25
This code opens the About box when the user clicks Help|About. ShowModal
opens the form in a modal state, a runtime state in which the user cant do
anything until the form is closed.
3 In the Events tab, double-click OnCreate and choose FormCreate from the dropdown list to create an event handler that describes what happens when the form is
created (that is, when you open the application).
4 Right where the cursor is positioned in the Code editor, type the following lines:
FileName := 'untitled.txt';
StatusBar1.Panels[0].Text := FileName;
Memo1.Clear;
26
Tutorial
Index
A
header files 1
Help tooltips 3
code, writing 17 to 23
compiling programs 15
components
adding to a form 3
setting properties 3
D
design-time view 2
E
error messages 23, 26
event handlers, creating 17 to 23
events 17
F
files
form 1
project 1
saving 2
types 1
unit 1
form files, defined 1
forms
adding components to 3
closing 2
main 2
I
images, adding to an application 11
M
menus, adding to an application 13
messages, error 23, 26
P
programs, compiling and debugging 15
project files 2
projects
default files 1
saving 2
properties, setting 3, 7
R
running applications 15
S
saving projects 2
setting properties 3, 7
source code, files 1
standard actions, adding to an application 9
T
toolbars, adding to an application 16
tooltips, viewing 3
Index
unit files 1
unit header files 1
user interface, designing 3
.xfm files 1
vi
Tutorial