Today you learn more about the Delphi IDE and how it all works together to create real-world working programs. Specifically you learn about
It will be a long day, but a rewarding one.
On Day 4, "The Delphi IDE Exp lored," you were introduced to Delphi projects and found out a little about how projects work. In this section, I cover projects in detail. Projects are a fact of life with Delphi. You cannot create a program without a project. The project makes sure that everything works together to create a working application. The following sections talk about
At some point, every project needs some management. Maybe you must add a new source unit to the project, or maybe you must remove a source unit. You might need to add another type of file to the project, such as a binary resource file. You add and remove units and other project files through the Project Manager.
On Day 4, I said that a project is a collection of files that work together to create a stand-alone executable file or DLL. That's the definition of a project as far as the Delphi IDE is concerned. In the real world, you might have a different kind of project, such as a job that you have to complete.
A large project might include one or more executable files and several DLLs. Because some projects consist of more than a single executable program, Delphi enables you to group several Delphi projects together and deal with them as a single unit. This unit is called a project group.
You might be wondering what advantage project groups provide. Project groups give you the following:
A projec t that creates a single executable file doesn't need a project group. A single project can hardly be considered a group, right? In the case of a single project, the concept of a project group is out of place.
But imagine for a moment a program that includes an EXE and a single supporting DLL. Both the DLL and the EXE go together. Usually, if you are working on the DLL, you will want the EXE present so that you can immediately test any changes you make to the DLL. In this scenario, a project group makes perfect sense because the EXE and DLL go everywhere together.
You can create a project group that contains these two individual projects and save it. When you want to work on either the application or the DLL, you can open the project group rather than an individual project. When you open the project group, both the EXE project and the DLL project are displayed. You can work on either the DLL or the EXE in the Code Editor and switch back and forth between them any time you want. Figure 9.1 shows the Project Manager window with this type of project group open.
FIGURE 9.1. The Project Manager window showing a project group.
Another reason to have a project group is so that you can group related projects. That probably sounds like it doesn't make much sense, so let me explain. Here at TurboPower Software we have a product called Async Professional, which is a collection of serial communications components. These components include three main categories: basic serial communications, faxing, and TAPI. We include dozens of sample programs with Async Professional covering each of these three categories.
Given that scenario, we could create a project group for all of our faxing examples, one for all of our TAPI examples, and one for all of our basic serial communications examples. Our users could then open the TAPI Examples project group and have all the TAPI examples in one neat package. The entire project group could b e built at one time, thereby saving the time and aggravation of opening and building each project individually. In this case, the projects don't work together like a DLL and EXE do, but the projects are related, so the concept of a project group makes just as much sense.
In any project group there is always an active project. The active project is displayed in the Project Manager in bold type. In Figure 9.1, the active project is the project called TestDll. The active project is the project that will be built when you choose Make or Build from the Project menu on the Delphi main menu.
These menu items are modified each time a project is made the active project. For example, if the active project is called Project1, the menu items will be called Make Project1 and Build Project1. If a project called PictView is made the active project, these two menu items will be called Make PictView and Build PictView.
The active project also has significance when a new form or a new unit is added using the Project Manager. When you create a new form using the Project Manager, the new form will be added to the active project regardless of which node in the Project Manager is currently selected. The active project is also the project to which new forms or units are added if you add new elements via the Delphi main menu or the Delphi toolbar.
You can make a project the active project in one of several ways. One way is to select any item in the project node that you want to make the active project and click the Activate Selected Project button on the top of the Project Manager. Another way is to simply double-click the project node itself. Finally, you can choose Activate from the project node context menu to activate a particular project.
The Project Manager in Delphi 4 is brand-new. The Project Manager concept is not new to Delphi 4, but the current implementation is much better than the previous Projec t Manager. The Project Manager is the central controller for all of your projects and your program groups. It enables you to add files to a project, delete files from a project, view a unit or form, add projects to the project group, change the order of projects, and more. To display the Project Manager, choose View|Project Manager from the main menu or press Ctrl+Alt+F11.
The Project Manager window contains a tree view control that displays up to four levels. Those levels are as follows:
Naturally, the individual nodes can be collapsed or expanded as with any tree view control. The Project Manager nodes have icons that indicate whether the node contains a project, an individual file, a form, or a form/unit pair. Refer to Figure 9.1 to see the different icons and levels that the Project Manager displays.
NOTE: In previous versions of Delphi, the Project Manager showed the path to any units as well as the unit filename. The Delphi 4 Project Manager does not show the path and filename in the same way. To see the path and filename, click on a unit in the Project Manager window and the Project Manager status bar will show the full path and filename for the unit (refer to Figure 9.1).
Most of the Project Manager's work is done through the Manager's context menus. There are four separate context menus for the Project Manager. The following sections describe each of the context menus.
The Project Group Context Menu
The project group context menu is the context menu you see when you right-click the project group node at the top of the Project Manager tree. Table 9.1 lists the project group context menu items that appear on this menu.
Item | Description |
Add New Project | Opens the Object Repository so that you can choose a new target. Targets include applications, DLLs, forms, data modules, components, or any other object available from the Object Repository. |
Add Existing Project | Opens a project file from disk and adds it to the project group. |
Save Project Group | Saves the project group. Project groups have a .bpg extension. |
Save Project Group As | Saves the project group with a new name. |
View Project Group Source | Displays the project group source. The project group source is a special file (a makefile) that contains references to all projects within the project group. |
Toolbar | Toggles the Project Manager toolbar on and off. |
Status Bar | Toggles the Project Manager status bar on and off. |
Dockable | Specifies whether the Project Manager is dockable. |
NOTE: The Toolbar, Status Bar, and Dockable menu items appear on each of the Project Manager context menus. I won't mention them again when I discuss the other Project Manager context menus.
The Project Context Menu
The project context menu is displayed when you right-click a proje ct node in the Project Manager. Table 9.2 lists the context menu items specific to the project context menu.
Item | Description |
Add | Opens the Add to Project dialog box so that you can add a file to the project. The same as choosing Project|Add to Project from the main menu or from the Delphi toolbar. |
Remove File | Opens the Remove From Project dialog box so that you can remove a file from the project. The same as choosing Project|Remove from Project from the main menu or from the Delphi toolbar. |
Save | Saves the project. The same as choosing File|Save from the Delphi main menu. |
Options | Displays the Project Options dialog box for this project. The same as choosing Project|Options from the Delphi main menu. |
Activate | Makes this project the active project. |
Compile | Compiles this project. The difference between compile and build was discussed on Day 4. |
Build | Builds this project. |
View Source | Displays the project source file. The same as choosing Project|View Source from the Delphi main menu. |
Close | Closes this project and all its files. If the project is part of a saved pr oject group, the project node icon will be grayed out. The project is still part of the group but is not open in the IDE. If the project is part of the default project group, the project is closed and removed from the default group. |
Remove Project | Removes this project from the project group. The project is not deleted from your hard drive, just removed from the project group. Same as clicking the Remove Selected Project button on the Project Manager toolbar. |
Build Sooner | Moves the project up in the project tree. Projects are built from the top of the Project Manager down. |
Build Later | Moves the project down in the project tree. |
NOTE: The Project Manager context menus are even more diverse than represented in this section. The project context menus contain additional items if the project is a Batch File or a Package project. The differences are not significant, so I won't go over those additional menu items.
The Unit Context Menu
The unit context menu is displayed when you right-click a unit node in the Project Manager. Table 9.3 lists the unit context menu items.
Item | Description |
Open | Displays the unit in the Code Editor (for standalone units) or Form Designer (if the unit has an associated form). |
Remove From Project removes the unit from the project. The Project Manager does not prompt you to remove the unit and there is no undo for this action. You an always add the unit back to the project if you remove a unit by accident.
Save | Saves the unit. The same as choosing File|Save from the Delphi main menu. |
Save As | Opens the Save As dialog box so that you can save the unit with a new name. The same as choosing File|Save As from the Delphi main menu. |
The File Context Menu
The file context menu is displayed when you right-click a node other than the project group node, a project node, or a unit node (usually a .pas or .dfm file). This context menu has only one item. The Open menu item displays the selected node in either the Code Editor or the Form Designer, depending on the type of the selected node.
In addition to the Project Manager context menus, the Project Manager has a toolbar to make working with the Project Manager easier. The Project Manager toolbar contains three buttons:
Keyboard commands include the Delete key and the Insert key. When you press Delete, the selected node is removed. If a project node is selected, the project is removed from the project group. If a unit node is selected, the unit is removed from the project to which the unit belongs. The Insert key behaves exactly the same as choosing Add to Project from the pr oject context menu.
TIP: The Project Manager toolbar buttons can be either large or small. By default, the Project Manager toolbar buttons are small. You can change the toolbar button size by dragging the bottom of the toolbar either up (to show the small buttons) or down (to show the large buttons).
Project groups are a great benefit for complex projects, but using project groups is not mandatory. You don't have to use project groups with every project. The Project Manager has a default project group called ProjectGroup1 that is used when you don't specifically open or create a project group. Try this:
FIGURE 9.2. The Project Manager showing the default project group.
The project group called ProjectGroup1 is a temporary project group. When you choose Save All from the File menu, you are prompted to save the project, but not the project group. If you want to save the project group, you must explicitly save it using the Save Project Group or Save Project Group As menu item from the Project Manager context menu.
Adding existing units to your projects is as simple as clicking the Add To Project button on the Project Manager toolbar or choosing Add To Project from the Project Manager context menu.
NOTE: You cannot add a unit to a project if a form with the same name already exists in the project. For example, if you have a form called MainForm and try to add a unit from anot her project that also has a form named MainForm, you get an error message from Delphi even if the filenames are different.
You use the Remove From Project option to remove files from the project. Alternatively, you can select the unit you want to remove and press the Delete key on the keyboard. Files removed from the project are not deleted from your hard drive, but are just removed from the project compile/link process.
CAUTION: Be careful when removing units from your projects. You must take care not to remove units that are referenced by other units in the project. If you remove units that are required by your project, a compiler error will result. Before removing a unit, be sure it is not used in your project. If you accidentally remove a needed unit from your project, you can add it back to the project with the Add To Project option as explained in the preceding section.
The Remove From Project dialog box enables multiple selection, so you can remove several units from a project at one time if you want.
To view a unit, form, or other file, just double-click the node representing the form or unit you want to view. You can also choose Open from the Project Manager context menu. The form or unit will be displayed in either the Form Designer or Code Editor, depending on the type of node you are viewing.
To build a particular project, you can do one of the following:
To build an entire project group, choose Project|Build Al l Projects from the Delphi main menu. All projects in the project group will be built starting with the first project in the group (the project at the top of the Project Manager tree) and proceeding down through the last project in the group.
Project options are another of those features that are easy to ignore. For one, the defaults are usually good enough when you are just starting out. After all, who has time to worry about all those compiler/linker options when you are simply struggling to learn a new programming environment? At some point, though, you will start to become more interested in what all those options do, and it's good to have some reference when the time comes.
This section looks at the Project Options dialog box. You can invoke this dialog box by choosing Project|Options from the main menu. The Project Options dialog box is a tabbed dialog box with several pages:
I won't discuss every page of the Project Options dialog box, but you'll take a look at the most important pages so that you can understand more about what each of these pages does. I'll start you out easy by discussing the Forms and Application pages. After that I'll move on to the more complicated pages.
NOTE: At the bottom of each page of the Project Options dialog box is a check box labeled Default. If you want the current settings to become the default settings for all new projects created, check the Default box. When you click OK, the current settings become the new default settings.
The Forms page of the Project Options dialog box is where you control how your application handles its forms. You saw this dialog box on Day 4 when you created the Picture Viewer program. Figure 9.3 shows the Forms page of the Project Options dialog box for the ScratchPad program.
FIGURE 9.3. The Forms page of the Project Options dialog box.
At the top of the Forms page is the Main form combo box. This is where you tell Delphi which form to display when the application starts. By default, the first form you create will be the main form. If you change your project around in such a way that a different form becomes the main form, you must change this setting so that the new form becomes the application's main form.
In the middle of the dialog box are two list boxes. The list box on the left is labeled Auto-create forms; the one on the right is labeled Available forms. Before I talk about how to use these two list boxes, let's take a moment to go over auto-creation of forms.
Auto-creation means that Delphi will construct the form during the application startup process.
Each time you create a form, Delphi places that form in the auto-create list for the application. Forms that are auto-created display more quickly than forms that are not auto-created. The disadvantage to auto-creation of forms is that your application will use more memory than it would if your forms were not auto-created. Another disadvantage, although probably insignificant, is that your application will take slightly longer to load if you are auto-creating a lot of forms.
NOTE: The first form in the Auto-create forms list box is always the main form. If you change the main form, the new form selected will move to the top of the Auto-create forms list box. Another way to set the main form is to drag and drop any of the forms in the Auto-create forms list box to the top of the list.
The nice thing about auto-creation is that displaying an auto-created form is easy. All you do is call that form's Show or ShowModal function:
AboutB ox.ShowModal;
If you do not have your forms auto-created by Delphi, you must take the responsibility of creating the form before you use it:
procedure TForm1.Button1Click(Sender: TObject); var About : TAboutBox; begin About := TAboutBox.Create(Self); About.ShowModal; About.Free; end;
This example does not use the Delphi-generated pointer to the About box. It creates a local pointer, displays the form, and then deletes the pointer as soon as the form is no longer needed. As is often the case in Object Pascal programming, there are several ways to perform this particular task. Because Delphi always creates a pointer to the form object, you could have written the previous code like this:
if not Assigned(AboutBox) then AboutBox := TAboutBox.Create(Self); AboutBox.ShowModal;
This code checks to see whether the form has already been created. If it has not, the object is created, and then the ShowModal method is called. Deciding which method of form creation you will use is up to you, but I prefer the former because it handles everything locally.
NOTE: Each time you create a form in the Form Designer, Delphi creates a pointer to the form. If you enable Delphi to auto-create a form, you don't have to worry about the pointer being valid. If you choose not to have a form auto-created, the pointer to the form will be nil until you explicitly create the form and initialize the pointer. If you forget and use the pointer before it is initialized, Windows will generate an access-violation error.
Okay, now turn your attention back to the Project Options dialog box. The Auto-create forms list box contains a list of the forms that will be auto-created. If you do not want a form to be auto-created, drag the form from the Auto-create forms list box to the Available forms list box. To move several forms at one time, simply select the forms you want to move (both list boxes support multiple se lection) and drag and drop them all at once. It's that easy.
NOTE: You can use the buttons between the two list boxes to move forms from one list box to the other, but it's usually easier to use drag and drop.
The Application page of the Project Options dialog box is very simple (see Figure 9.4).
FIGURE 9.4. The Application page.
The Title field on this page is used to set the title of the application. The title is the text that will appear on the Windows taskbar when your application is minimized.
NOTE: The application's title and the caption of the main form are two separate items. If you want your program's name to show up when you minimize your program, you must be sure that you set the title for the application in the Project Options dialog box. If you do not provide an application title, the name of the project file is used by default.
The Help file field of the Application page is used to set the help file that your application will use. This is the help file that the program will load when you press F1 while your application is running. You can use the Browse button to locate the help file if you can't remember the name or location of the help file. If you do not supply a help file, pressing F1 in your application will have no effect.
The Icon option enables you to choose an icon for your application. This is the icon that will be displayed in the Windows taskbar when your application runs and when it is minimized. In addition, this icon will be displayed on your main form's title bar unless you have explicitly set an icon for the main form. To choose an icon, click the Load Icon button and locate the icon file (.ico) using the Application Icon dialog box.
You use the Target file extension field to spec ify the filename extension of the project when the project is built. For example, if you were creating a screen saver, you would change this field to scr so that your screen saver would be created with an extension of .scr rather than .exe. Control Panel applets are another example. These are special programs saved with a .cpl extension. The project filename extension is automatically set to .exe for executable projects (console applications and GUI applications) and to .dll for DLLs, so you don't have to provide a value for this field for normal projects.
The Compiler page of the Project Options dialog box is where you set the options that the compiler uses to build your project. Figure 9.5 shows this page of the Project Options dialog box.
The Compiler page is divided into five sections. I'll take each section and examine it so that you can better understand the different options on this page.
FIGURE 9.5. The Compiler page of the Project Options dialog box.
The compiler can be configured to perform optimizations on your code. When optimizations are turned off (the Optimization check box is not selected), the compiler makes no attempts to optimize code in any way. If you turn the Optimization option on, the compiler will generate the fastest code possible without regard to code size. In most cases, you should leave this option on the default setting. Sometimes, however, it is better to turn optimization off while debugging your application. I'll discuss optimization and debugging in more detail on Day 10, "Debugging Your Applications."
The Aligned Record Fields option is used to control how records are aligned in memory. When this option is on, records are aligned on 4-byte boundaries. When this option is off, records are byte-aligned.
You might want to turn the Stack Frames option on when debugging. Whe n you are finished debugging, you can turn off this option to have the compiler generate smaller and faster code, but compile times will be slightly longer with the Stack Frames option off.
The Pentium-Safe FDIV option causes the compiler to generate code that detects a faulty floating-point division instruction.
These two sections affect how the compiler generates code for a project. The Delphi help for the Compiler page explains what each of these options is for, so I won't repeat that information here. To display the help page for the compiler options, click the Help button when the Compiler page is displayed or press the F1 key on the keyboard.
The Debugging section of the Compiler page of the Project Options dialog box controls how the compiler generates code for debugging sessions. When the Debug information option is enabled, Delphi generates debug information for the project. If you do not generate debug information, you will not be able to stop on breakpoints and inspect variables during debugging. Put another way, you can't debug your program unless you tell Delphi to generate debug information.
NOTE: If you change any of the options on the Compiler page, you should do a build immediately following the change. This will ensure that all units are rebuilt using the same compiler settings.
The Messages section determines whether you want the compiler to report hints and warnings after compiling. I always leave both the Show Hints and Show Warnings options on. Hints and warnings should not be ignored in the long term (in the short term, you can ignore warnings that you know are due to temporary conditions in your code). Usually, compiler warnings can, and should, be resolved. Learn to treat hints and warnings as errors. Quality code compiles without warnings.
The Linke r page of the Project Options dialog box is where you set options that specify how you want the linker to operate. Until you become very familiar with Delphi, you can leave this page alone and accept the default settings. Figure 9.6 shows the Linker page of the Project Options dialog box. The options available on this page are explained in the following sections.
FIGURE 9.6. Project Linker options.
The Map File settings control whether a map file is generated and how much detail is included in it. A map file is an advanced debugging tool and is something that you will not likely use until you get further into Delphi. For that reason, I won't go into more detail on the Map File options.
The EXE and DLL Options section determines the type of executable file produced by Delphi for an application project. When the Generate Console Application check box is checked, Delphi produces a console application as opposed to a GUI application.
The Include TD32 Debug Info option causes the linker to link the debug information into the EXE or DLL. ("TD32" refers to the 32-bit version of the venerable old Turbo Debugger. TD32 is an advanced standalone debugger that used to ship with Borland C++ products and with some versions of Delphi.) Some debugging utilities use debug information in TD32 format. TurboPower's Memory Sleuth, for example, requires TD32 debug information to be present in the EXE. Turn this option on when using programs like Memory Sleuth.
The Include Remote Debug Symbols option generates the debug symbols necessary for remote debugging of Web broker applications.
The Linker Output section determines what type of compiled binary file the linker should produce. Normally the linker produces DCU files (the default for Delphi applications). You might, however, want to generate C or C++ object files (OBJs) rather than DCUs, thereby allowing your Pascal units to be used with a C or C++ program built with Borland C++Builder.
The Memory Sizes section can be ignored by all but the most advanced users. The default values are acceptable for all applications. In some cases, you might change the Image base address when building a DLL, but doing so is rarely necessary.
The EXE Description field is used to specify a string that will be embedded in the application. This field is sometimes used to add copyright information to an EXE or DLL. Most of the time, you will use version info to store copyright information in a file rather than using the EXE Description field on this page. Version info is described later in the section "The Version Info Page."
The Directories/Conditionals page of the Project Options dialog box is where you set the directories that your project uses to find library files. Figure 9.7 shows the Directories/ Conditionals page. The fields on this page are described in the following sections.
FIGURE 9.7. The Directories/ Conditionals page.
The fields in this section determine where Delphi can find various source files during compilation of the project. It also contains fields that determine where certain files created by the compiler and linker will be placed during a build of the project.
The Output Directory field is used to specify the directory where the final executable file or DLL will be placed. The Unit Output Directory determines where the DCU files will be placed for each unit compiled. The Search Path field is used to specify the directories where any additional libraries needed to build the project can be found. The Debug Source Path field is used to specify the path to any source units that you want to debug, but which are not in t he current project's directory. For example, if you want to step into a DLL while debugging, you have to enter the path to the DLL's source code in this field. The BPL Output Directory and the DCP Output Directory fields specify where the BPL and DCP files should be placed when building a package.
Note that a small button with an ellipsis on it appears next to several of the fields on the Directories/Conditionals page. Clicking this button displays an editor that enables you to add, remove, or rearrange the items in a particular field. Figure 9.8 shows this dialog box while editing the Search Path field.
FIGURE 9.8. The Directories editor dialog box.
You use the Conditional Defines field to specify any defines that you want to add at the project level. For example, let's say you have code in your project that will be compiled only if the symbol TRIALRUN is defined. In that case, you would add TRIALRUN to the Conditional Defines field. If you have more than one symbol to define, make sure that each symbol is separated by a semicolon.
The Unit Aliases field is used to specify any unit aliases. For example, Delphi 1 used units called WinTypes.pas and WinProcs.pas for all Windows-specific code. Delphi 2, 3, and 4 use a unit called Windows.pas in place of WinTypes and WinProcs. The Unit Aliases field aliases WinTypes and WinProcs to the Windows unit. In this case, aliasing enables programs written in Delphi 1 to compile in Delphi 4 without modification of the uses list.
The Version Info page enables you to set the version information for your applications. Version information is stored in your program's EXE file or in a DLL or ActiveX file. It is used by installation programs to determine whether a file being installed is older or newer than the file it is replacing.
Version info has other uses as well. You can view the version information for a file from Windows Explorer. Just right-click the file and choose Properties from the Explorer context menu. When the Properties dialog box comes up, click the Version tab to view the version information for the file. Figure 9.9 shows the Properties dialog box displaying version information for the Database Desktop utility that comes with Delphi.
FIGURE 9.9. The Properties dialog box showing version information for DBD32.EXE.
Figure 9.10 shows the Version Info page of the Project Options dialog box. At the top of the page is a check box labeled Include Version Information in Project. When this check box is checked, version information will be included in the project's executable file. When this check box is not checked, version information is not included in the project, and the rest of the page is disabled.
FIGURE 9.10. Version information for your projects can be supplied on the Version Info page.
The remaining fields on the Version Info page are used to specify the various pieces of version information. The Major Version, Minor Version, Release, and Build fields work together to form the file version number. The version number of the file in Figure 9.10 is version 2.0, build 0. If you check the Auto-Increment Build Number option, the build number will automatically increment by one each time you perform a build.
The Module Attributes section can be used to determine any special attributes you want specified for the file. The Language section enables you to select a locale identifier for the file. For more information on the possible values for the Locale ID field, see the Windows API online help under the topic "Language Identifiers and Locales."
The table at the bottom of the Version Info page can be used to set a variety of information. This information includes your co mpany name, the file description, the internal name of the file, the legal copyright or trademark information, the product name, product version, and any comments you want to add.
You can provide information for any of these fields or none at all (the FileVersion field is set based on the fields in the Module Version Number section). You can even add your own custom version information fields. To add a custom version info field, simply click in the version info table and scroll to the end of the table using the down-arrow key. Type the down-arrow key once more and a dialog box will be displayed asking you for the version key to add. Supply a key name and that key is added to the version info for the project. Adding version information to a project has never been so easy!
The Packages page is where you determine the type of linking that your projects will use. The top part of the page enables you to add or remove design packages, but that really doesn't have anything to do with the current project. The only option that pertains to the current project is the Build with Runtime Packages check box.
When this option is selected, your application will use dynamic linking of the VCL and any third-party components. This means that your executable file will be smaller, but you will have to be sure to ship the correct packages with your application. When this check box is off, your application uses static linking. Static linking means that any VCL code and the code from any third-party components that your application uses is linked directly into your executable file. Packages were discussed in detail yesterday in the section "Using Packages." Figure 9.11 shows the Packages page of the Project Options dialog box.
FIGURE 9.11. The Packages page.
There is no question that Delphi is high ly visual in nature--that's one of the great benefits of programming with Delphi. Still, any program of any significance will have a great deal of code that must be written by hand. After you get the UI part of your application written with Delphi's impressive visual tools, you'll likely spend a long stretch with the Delphi Code Editor. The Code Editor has some features you'll learn to appreciate when you discover them.
In this section, you learn about the following:
NOTE: The Delphi Code Editor enables you to choose from four keyboard-mapping configurations: Default, IDE Classic, BRIEF, and Epsilon. The rest of this chapter assumes Default keyboard mapping. If you are already familiar with one of the other keyboard mapping configurations, you can ignore any references to specific keystrokes.
I'm going to assume that you know enough to be able to enter and delete text, highlight text with the mouse, cut, copy, paste, and so on. I won't spend any time going over functions at that level.
When it comes right down to it, the Delphi Code Editor is a typical code editor. It features syntax highlighting, which makes identifying keywords, strings, numeric constants, and comments at a glance easy. You'll look at setting the editor preferences a little later.
The Code Editor is a tabbed window. You can open as many editor windows as you like; each window will be represented by a tab along the top of the editor window that will display the name of the file. To switch to a source file, simply click the tab corresponding to the file you want to view. If more tabs exist than can be displayed at one time, scroll buttons will appear so that you can scroll among the tabs.
The status bar at the bottom of the Code Edito r gives status information (obviously). The current line number and the cursor position on the line are reported in the left panel of the status bar. If the file has changed since it was last saved, the status bar will say Modified in the center panel of the status bar. The right panel of the status bar shows the current mode, either Insert or Overwrite. If the file has been set to read-only, this panel will say Read Only.
The editor window has a gray strip in the left margin that is called the gutter. The gutter is used to display icons at different stages of the development process. For example, when you set a debugger breakpoint (discussed tomorrow), a red dot is placed in the gutter. When you set a bookmark (discussed in just a bit), an icon representing the bookmark is placed in the gutter.
NOTE: If you accidentally click on the gutter when trying to select text or place the cursor, you will find that a breakpoint is set on that line. Click the gutter again to clear the breakpoint.
Nothing is very mysterious about opening and saving files in the Code Editor. It should be pointed out, though, that there is a difference between opening a project and opening a source file. When you choose File|Open Project from the main menu, you are prompted for the name of a project file to open. When you choose File|Open from the main menu, you can open an individual Delphi source file or form file. Actually, you can open any type of text file (including file types not listed in the Open dialog box's Files of Type field). This includes .pas, .rc, .txt, and even C++ source and header files (.cpp and .h). Both the Open and Open Project menu items have corresponding toolbar buttons.
NOTE: If you open a unit file (.pas) that contains a form, Delphi will open the source file in the Code Editor and will also open the form in the Form Designer.
You can also open multiple files at one time. To open multiple files, choose the files you want to open in the Open dialog box and click OK. Each file selected will be loaded, and a tab for each file will be placed at the top of the editor window.
TIP: You can also use drag-and-drop to open files. For example, you can choose a file (or a group of files) in Explorer, drag it onto the Code Editor, and drop it. The file will be opened in the Code Editor.
To save a file, choose File|Save or File|Save As from the main menu or type Ctrl+S on the keyboard. If the file has not been previously saved, the Save As dialog box will appear, and you can enter a filename at that time.
Although text highlighting is basic text-editor stuff, I thought it wouldn't hurt to remind you of a few basic highlighting techniques you can use in the Delphi Code Editor.
To highlight a short block of text, you can use the mouse to drag across any text you want to highlight. After you've selected the text, you can cut, copy, or paste as needed. To highlight longer blocks of code, you can use the Click+Shift+Click method. First, click at the beginning of the block you want to highlight. Next, hold the Shift key on the keyboard and then click again at the end of the block. All text between the starting point and the ending point is highlighted.
Another useful feature is the capability to quickly select an individual word. To select a keyword, function name, or variable, just double-click on the word. Now you can perform any editing operations you want with the highlighted word.
TIP: To select a single line of code with the mouse, click at the beginning of the line and drag straight down to the beginning of the next line. To highlight a single line of code with the keyboard, first press the Home key to move to the beginning of the line and then us e Shift+down-arrow key to highlight the line.
Dozens of keyboard combinations can be used to highlight text and do other editing chores. For a complete list of all the keyboard shortcuts available, consult the Delphi online help.
TIP: As you program you often add, delete, or move blocks of text. Sometimes you will need to indent an entire block of code. At other times you will need to un-indent (outdent?) an entire block of code. To indent a block of code, highlight the lines that you want to indent and then press Ctrl+Shift+I on the keyboard. The entire block will be indented. To un-indent a block of code, press Ctrl+Shift+U on the keyboard.
The Code Editor also supports drag-and-drop editing. To move a section of code, first highlight it. Next, place the mouse cursor over the highlighted text and drag. Drag until the cursor reaches the location where you want the code to be placed. Release the mouse button and the code will be moved to the new location. To copy text rather than move it, repeat the preceding steps but hold down the Ctrl key before you drop the text.
The Code Editor has a virtually limitless number of undo levels (32,767 by default). Normally, you can only undo commands up to the last time you saved a file. By changing the editor options, you will be able to undo past commands even after saving the file. I'll talk about editor options and preferences later in the chapter in the section titled "Changing the Editor Options."
In general, it pays to remember this simple maxim: "Undo is your friend."
Find and Replace are used fairly heavily in programming. You might use Find to find a specific piece of code or a specific variable in your code. You might use Replace to change a variable's name or to change the name of a method. The possibilities are endless.
The Delphi Find Text and Replace Text dialog boxe s implement more or less standard find-and-replace operations. To bring up the Find Text dialog box, choose Search|Find from the main menu or press Ctrl+F. Enter text in the Text to find field and click OK or press Enter. If the text is found, it will be highlighted.
NOTE: Text highlighted by the Find Text dialog box is not the same as text highlighted with the mouse. You will notice that searched text is highlighted in black whereas text selected with the mouse is highlighted in blue (assuming you haven't changed the editor defaults). Text highlighted after a search operation is not selected for editing, it's just marked so that you can see it better.
To invoke the Replace Text dialog box, choose Search|Replace from the menu or press Ctrl+R. Figure 9.12 shows the Delphi Replace Text dialog box. With a few obvious exceptions, the Find Text dialog box contains the same options.
For the most part, the options on the Find Text and Replace Text dialog boxes do exactly what they indicate. If you choose the Case Sensitive option, you must type the search text exactly as it appears in the source file.
FIGURE 9.12. The Replace Text dialog box.
Use the Whole Words Only option when you want to be sure that text for which you are searching is not part of a longer word or variable name. For example, let's say you want to replace Form with MyForm. In this case, you would want to search for whole words only because the word Form might be used in other variable names (such as TForm).
The Regular Expressions option requires explanation as well. When this option is on, you can use special and wildcard characters when doing searches. The special characters enable you to find elements such as the beginning of a line or the end of a line in your search strings. Wildcard characters work much like they do in directory operations. For a complete description of regul ar expressions, see the Delphi online help under the topic "Regular Expressions."
When replacing text, it is safest to leave the Prompt on Replace option on. When you do a Replace All operation with this option on, the editor will highlight each found word and prompt you whether to replace it. It is easy to miscalculate the results of a Replace All operation, so always use Replace All with care. Even then, it still pays to remember that maxim: "Undo is your friend."
The rest of the Find and Replace options are self-explanatory and therefore don't need additional mention.
Find in Files is a great tool for searching for text in multiple files. I frequently use Find in Files to search the VCL source code for particular methods, variables, or classes. This tool is useful and convenient, and you should learn how to use it.
To display the Find Text dialog box, you can choose Search|Find in Files from the main menu. Perhaps an easier way is to type Ctrl+F on the keyboard to bring up the Find Text dialog box and then click the Find in Files tab. Figure 9.13 shows the Find Text dialog box with the Find in Files tab showing.
FIGURE 9.13. The Find Text dialog box.
Find in Files uses some of the same search options as the regular Find operation (case-sensitive, whole word only, and regular expressions). In addition, you have the option to search all files in the project, all open files, or files in a particular directory, including subdirectories.
When you start Find in Files, a small window with a title of Searching appears in the lower-right portion of your screen. This window shows the status of the Find in Files operation. It will show you the current file being searched and the number of matches up to this point. To cancel the search, just close the Searching window.
Any matches are reported in the Code Editor's message window. The message window show s the filename of the file in which the text was found, the line number where the text was found, and the line containing the search text with the search text displayed in bold. To view the file that contains a match, just double-click a line in the message window. Delphi will open the appropriate file and display the exact line containing the text for which you are searching. Figure 9.14 shows Delphi searching a set of files.
When specifying the file mask, all the usual wildcard characters apply. For example, if you want to search all text files in a directory, you would enter the following in the File masks field:
c:\MyStuff\*.txt
Find in Files is an indispensable tool. I find myself using it all the time. Learning to use Find in Files will save you a lot of time.
FIGURE 9.14. Delphi searching for text.
One of the most useful features of the Code Editor is its integration with the Delphi help system. Just place the editor cursor over an Object Pascal keyword, a VCL property or method, or any other Delphi-specific text and press F1. If a help topic for the text under the cursor exists in the Delphi help files, Windows Help will run with the appropriate page showing. If no help topic exists for the selected text, an error message will be displayed.
This feature is extremely useful when you can't remember how to use a particular aspect of Delphi, Object Pascal, or VCL. Help, as they say, is just a keystroke away.
The Delphi Code Editor has a few features that are extremely useful when you are writing a lot of code. They are explained in the following sections.
Code templates are another nice feature of the Delphi Code Editor. Code templates enable you to insert any predefined code (or any text, for that matter) in your source units. To use code templates, just type Ctrl+J on the keyboard while editing in the Code Editor. When you do, a list box will pop up giving you a list of templates from which to choose. Choose a template from the list, press Enter, and the text corresponding to that code template will be inserted into your source code. Figure 9.15 shows the code template list box as it appears when you type Ctrl+J.
FIGURE 9.15. The Delphi code template list box.
You can add new templates or edit existing templates via the Code Insight page of the Environment Options dialog box. Or, if you prefer, you can open the code template file with any text editor (such as the Delphi Code Editor) and edit the code templates there. The code template file is called DELPHI32.DCI and is located in the Delphi 4\Bin directory.
Feel free to modify the code templates in any way you see fit. For example, I have modified my code template representing the for statement to look like this:
for I := 0 to Pred(|) do begin end;
Notice the pipe character (|) in the code snippet. The pipe in a code template entry is a placeholder that determines where the cursor will be placed after the text is inserted into your source code.
TIP: If you make a lot of modifications to the code template file, be sure to keep a backup of the file in a safe location. You will need a backup because the Delphi installation program will overwrite your modified DELPHI32.DCI file when you update or reinstall Delphi.
You can use code templates for more than just code. Here at TurboPower Software our source files always have a header at the top of the unit. It looks something like this:
{*********************************************************}; {* Filename and version *}; {* Copyright (c) TurboPower Software 1998 *}; {* All rights reserved. *}; {********* ************************************************};
Because most of this text stays constant, I have a template that quickly inserts the header in any new source units I create. Use code templates for any text that you use frequently in your day-to-day programming.
The code parameters feature of the Code Editor displays a tooltip that prompts you with the needed parameters of a VCL method or Windows API function. With hundreds of VCL methods and Windows API functions, there is virtually no way to remember every function's parameters. The code parameters feature saves you time by showing you the parameters of a method as you type. For example, let's say you are calling the SetBounds method. When you type the opening parentheses, a hint window pops up as shown in Figure 9.16.
FIGURE 9.16. Code parameters in action.
As you can see in Figure 9.16, each parameter is listed in the tooltip. The parameter you need to type next is displayed in bold. As you type a parameter, the next parameter is displayed in bold. This continues until you type all the method's parameters. After you have typed all the required parameters, the code parameters tooltip disappears. The code parameters options are set on the Code Insight page of the Environment Options dialog boxes. I'll discuss that page later in the section "The Code Insight Page."
Code completion is another Code Editor feature that can save you development time. Type a class variable name followed by the dot operator, and the Code Editor will display a list box with all of that class's properties and methods. If, for example, you have a memo component called Memo, you will type
Memo.
and pause for a moment. A list box pops up as shown in Figure 9.17.
FIGURE 9.17. Code completion displaying the properties and m ethods of TMemo.
When you see the list box, you can select an item from the list in one of two ways. One way is to locate a property or method in the list box using the mouse or the keyboard and press Enter. The property or method will be inserted into your code. Alternatively, you can type the first few letters of the property or method you want to insert into your code. As you type, Delphi searches the list of properties and methods for a match and highlights the item in the list that most closely matches the text you are typing.
When you see the property or method you want, press the Enter key and the property or method will be inserted into your code. If you don't want to use the code completion list box, you can press the Esc key on the keyboard and the code completion list box will disappear.
Code completion saves you time by providing a list of properties and methods from which to choose. It has the added benefit of providing perfect spelling and capitalization of property and method names. Just choose the property or method you want, press the Enter key, and Delphi inserts the identifier in your code.
Tooltip symbol insight pops up a tooltip when you place your mouse cursor over just about any identifier in your source code. For example, a standard new project will have this line in the interface section:
TForm1 = class(TForm)
When you place your mouse cursor over the identifier TForm, a tooltip pops up that says:
type Forms.TForm = class(TCustomForm) - Forms.pas (584)
This line shows you the TForm class declaration and tells you that TForm is declared on line 584 of the Forms.pas unit.
Tooltip symbol insight will tell you information about any variable in your program. This is particularly convenient when you forget the type of a variable (was X declared as a Byte, a Word, or an Integer?).
Class completion is a new feature in Delphi 4. Type any pro perties or method declarations in the interface section and type Ctrl+Shift+C on the keyboard. Delphi will add the necessary code to the implementation section to complete the class. This is best illustrated by performing an exercise. Perform these steps:
procedure Test; function GetSomething : Integer;
When you perform step 4, Delphi automatically adds the bodies of the Test and GetSomething methods to the implementation section and places the editing cursor in the first method. The added code looks like this:
function TForm1.GetSomething: Integer; begin end; procedure TForm1.Test; begin end;
Keep this unit handy because you will use it again in the next section when I discuss module navigation.
Code completion works with property declarations as well as methods. Type a property declaration, type Ctrl+Shift+C, and Delphi completes the property declaration. It even adds a write method for your property. I don't cover writing properties until Day 20, "Creating Components," so this might not make much sense to you right now. It will be more clear when you begin writing your own components.
For me, class completion is one of the best of Delphi 4's new features. This feature alone has saved me a lot of time when writing components. I wouldn't be without it now that I have been using it for a period of time.
Module navigation enables you to quickly move from a method in the implementation section to that method's declaration in the interface section, and vice versa. Again, an exercise is worth a thousand words. Do this:
As you can see, module navigation makes it easy to quickly move between the implementation and interface sections.
TIP: It doesn't matter whether you use the up arrow button on the keyboard or the down arrow button. Either button acts as a toggle between the interface and implementation sections.
Module browsing is another module navigation tool. In the Code Editor, hold down the Ctrl key and place the mouse cursor over an identifier name. The identifier will be highlighted in blue and underlined. Click on the identifier and Delphi will take you to the place in the source where that identifier is declared.
In this regard, the module browser is like the module navigation feature. It goes beyond that, however. With the module browser, you can click on a VCL identifier as well as your own identifiers. Let me illustrate with an example. Before starting, however, I should point out that this exercise will work only if you have the Professional or Client/ Server version of Delphi. It requires the Delphi source code to work. Perform these steps:
$(DELPHI)\source\vcl;$(DELPHI)\source\rtl\win
Memo1.Clear;
procedure TCustomEdit.Clear; begin SetWindowText(Handle, `'); end;
function SetWindowText; external user32 name `SetWindowTextA';
The module browser is a great tool for navigating not only your own code, but also the VCL source code and the source code of any third-party component libraries you have installed. Remember, reading the VCL source code can teach you a lot, so don't be afraid to browse through the source.
You can set bookmarks in your code to temporarily mark your place in a source file. For example, you often must temporarily leave a block of code on which you are working to review previously written code or to copy code from another location. By dropping a bookmark at that point in your code before running off to do your other work, you can return to that section of code with a simple keystroke. You can have up to 10 bookmarks set at any one time.
To set a bookmark at a particular location, press Ctrl+Shift and the number of the bookmark to set. For example, to set bookmark 0 (the first bookmark), place the editor cursor at the location you want to mark and then press Ctrl+Shift+0 (Ctrl+K+0 will work as well). When you set a bookmark, an icon is placed in the Code Editor gutter to indicate that a bookmark exists on that line. The icon shows the number of the bookmark. Figure 9.18 shows the Code Editor with a bookmark dropped on a line.
FIGURE 9.18. The Code Editor with a bookmark set.
To return to the bookmark, press Ctrl plus the number of the bookmark to which you want to return. Using the same example, you would type Ctrl+0 to go back to the bookmark. (You can also set bookmarks and go to bookmarks using the Code Editor context menu.) To clear a bookmark, place the editor cursor anywhere on the line containing the bookmark and again press Ctrl+Shift+0.
NOTE: Bookmarks can be set for each file you have open in the Code Editor. For example, you can have bookmark #0 set in one source file and another bookmark #0 set in another source file. This means that bookmarks cannot be found across source files. If you set bookmark #0 in Unit1.pas, you cannot press Ctrl+0 in Unit2.pas and expect to be taken to the bookmark in Unit1.pas.
To illustrate the use of bookmarks, do the following:
Note that bookmarks are temporary. When you close the source file and reopen it, the bookmark is not preserved. Note also that to set and remove bookmarks you must type the number keys on the main keypad. The numeric keypad is ignored when setting and removing bookmarks.
You can use the incremental search option to quickly find a short series of characters. To start an incremental search, choose Search|Incremental Search from the main menu or press Ctrl+E on the keyboard. To unders tand how the incremental search works, it is easiest to do an exercise. Do the following:
Learning to write Windows programs a bit at a time is not so bad. Isn't it time you got back to work?
That's all there is to it. The incremental search is handy when you are searching for short amounts of text.
TIP: If you make a mistake when typing the characters while doing an incremental search, you can use the Backspace key to remove from the search string the last character typed.
The Code Editor has a feature to help you find a bracket or parenthesis that matches the bracket or parenthesis the cursor is currently on. To find a matching bracket, p lace the cursor before a bracket (it doesn't matter whether it's the opening or closing bracket). Now press Alt+[ on the keyboard. The cursor jumps to the bracket that matches the bracket on which you started.
Press Alt+[ again, and the cursor jumps back to where you started. The same keystrokes work for parentheses as well as brackets. Getting lost in the maze of parentheses and brackets is still a possibility, but at least now you know how to find your way out again.
Like most of the different windows you encounter in Delphi, the Code Editor has its own context menu. The Code Editor context menu can essentially be divided into two parts: editor items and debugger items. I will leave the debugger items of the context menu for tomorrow when I discuss debugging, but I'll go over the editor items on the context menu now. Table 9.4 contains a list of the context menu items that pertain to the editor, along with a description of each.
Item | Description |
Close Page | Closes the active page in the edit window. If the file on the page has been modified since it was last saved, you are prompted to save the file. |
Open File At Cursor | Opens the file under the cursor. This option has an effect only when the text under the cursor represents a source code file. For example, if you have a file in your uses list called MyUnit2, you could click on the filename and choose this menu item to open the file. The file will be placed in a new editor window and focus will be set to the window. |
New Edit Window | Opens a ne w copy of the Code Editor. This is convenient if you want to compare two source files side by side. |
Browse Symbol At Cursor | Starts the browser with the current symbol as the browse target. |
Topic Search | Displays the help topic for the item under the cursor (if it can be found). Same as pressing F1 on the keyboard. |
Add to Interface | Adds a property, function, or procedure to an ActiveX component. This item is disabled if the project is not an ActiveX project. |
Toggle Bookmarks | Toggles a bookmark (0 through 9) on or off. The bookmark is set on the source code line containing the editing cursor. |
Goto Bookmarks | Moves the source code editor to a bookmark. |
View As Form | If the active file in the Code Editor is displaying a form's contents as text, choosing this option will again display the form in the Form Designer. |
Read Only | Toggles the currently active file between read-only and read/write mode. When set to read-only, the file cannot be modified, although text can be selected and copied to the Clipboard. The status bar displays Read Only to indicate the file is read-only. When the file is closed and reopened, it is again in read/write mode. |
Message View | Displays or hides the Delphi message window. The message window automatically appears when there are compiler or linker errors or warnings, but can be specifica lly shown or hidden with this command. |
View Explorer | Sets focus to the Code Explorer window. If the Code Explorer is not docked, this command will bring it to the top of all other windows. |
Properties | Displays the Environment Options dialog box so that the editor options can be set. |
Depending on the current state of the Code Editor and the particular type of file open, some of the items in Table 9.4 can be disabled at any given time.
The editor options occupy three pages of the Environment Options dialog box. To view this dialog box, choose Tools|Environment Options from the main menu.
TIP: You can also choose Properties from the Code Editor context menu to view the editor options. The difference with this method is that only the four pages pertaining to the editor options will be displayed. When displayed this way, the dialog box is titled Editor Properties rather than Environment Options. The figures in the following sections were taken using this method.
The four pages of the Environment Options that are specific to the Code Editor are as follows:
Let's examine these pages in the following sections.
The Editor page of the Editor Properties dialog box enables you to control how the editor works for you. As you can see from Figure 9.19, a lot of options are available on this page.
FIGURE 9.19. The Editor page of the Editor Properties dialog box.
At the top of the page is a combo box labeled Editor SpeedSetting. You can choose Default Keymapping, IDE Clas sic, BRIEF Emulation, or Epsilon Emulation from the combo box. If you change the setting in this combo box, the Editor Options will change to reflect the defaults for the type you chose.
NOTE: If you are new to programming or if you have been using other Borland compilers using the Default keymapping, you don't have to worry about what you are missing. If you are accustomed to years of using a particular type of editor, you will be glad to know that you can still use the keyboard shortcuts and editor options that you know and love simply by changing the Editor SpeedSetting field on this page and on the Display page.
Toward the bottom of the screen, you will see the Block Indent and Tab Stops fields. You can use these two fields to change the amount by which code is indented when you block indent or when you tab to the next tab stop. Block indenting is discussed earlier in this chapter in the section "Highlighting Text."
NOTE: Real programmers use tab stops of either two or three characters. (I use two-character tabs.)
The Undo Limit of 32,767 is probably sufficient for most needs (I hope!), so I doubt you'll feel the need to modify that setting. The Syntax Extensions field enables you to select the types of files for which syntax highlighting will be applied. For example, you probably don't want syntax highlighting applied to regular text files (.txt) that you open in the Code Editor, so that file type is not listed by default.
In the middle of the Editor page, you will find a whole gaggle of editor options from which to choose. Because so many options are available, and because determining exactly which of the available options are the most important is difficult, I'll refer you to the Delphi online help. Simply press F1 while on this page or click the Help button and you will have explanations of each of the editor options you see on this p age. As with some of the other options you looked at today, you can probably feel comfortable in accepting the Delphi defaults.
One thing I will add is that I always turn on the Find Text at Cursor option (contrary to what Figure 9.19 might indicate). When this option is on, the text at the editing cursor is automatically placed in the Text to Find field of the Find Text dialog box when the dialog box is invoked. For me, searching for text is much quicker when I don't have to type the search text. This is particularly convenient when using Find in Files.
The Display page of the Environment Options dialog box has additional options from which you can choose. These options pertain to the actual display of the text in the Code Editor window (see Figure 9.20).
FIGURE 9.20. The Display page.
In the Display and File Options section, you will find the BRIEF Cursor Shapes option. Turn on this option if you want the horizontal cursor in the editor window rather than the vertical cursor. Check the Create Backup File option if you want Delphi to create a backup file every time you save your file or your project. Backup file extensions begin with a tilde (~). For example, the backup file for a source file called MyApp.pas would be MyApp.~pa.
The Zoom to Full Screen option controls how the Code Editor acts when maximized. If this option is on, the Code Editor will fill the entire screen when maximized. When this option is off (the default), the top of the Code Editor window will stop at the bottom of the Delphi main window when maximized. In other words, the Delphi main window will always be visible when the Code Editor is maximized if this option is off.
You can also choose whether your editor windows have a visible right margin. The right margin is not binding--you can still type text beyond it--but it gives you a visual cue that your lines might be getting too long. This section also enables you to determine whether you want a visible gutter and how wide the gutter should be (in pixels).
You can also change the Code Editor font and point size. A combo box is provided for you to choose these options. Only fixed-space screen fonts are listed; proportional and printer fonts are not. Choose the typeface and point size that best suit your needs. A preview window is provided so that you can see how the font you have chosen will look.
NOTE: I usually get fed up with all those backup files cluttering up my project directories and turn off file backups. Suit yourself.
The Color page of the Environment Options dialog box enables you to fully customize the Code Editor's window and syntax highlighting options (see Figure 9.21).
FIGURE 9.21. The Color page in the Editor Properties dialog box.
At the top of the page is the Color SpeedSetting combo box. This combo box gives
you four predefined color schemes from which to choose. You can choose one of these
color schemes or use one of them as a base for creating your own color scheme.
The Color page is very easy to use. At the bottom of the page is a text window that contains sample code. If you click one of the key elements of the code, that element will be selected in the Element list box, and its current settings will be displayed on the Color grid.
To change the foreground, background, and text attributes for that element, simply choose the settings you like. For example, keywords are displayed in bold text with a black foreground and a white background (assuming the default color scheme). To change the keywords to green, bold text, click the procedure keyword in the sample code window and then change the foreground color to green. The text colors in the sample window change to reflect the new color you have chosen. Continue changing colors until you have the sample window just the way you want it. When you click OK, the Code Editor will change to the new colors you have chosen.
The Code Insight page is used to enable or disable Code Completion, Code Parameters, Tooltip Expression Evaluation (a debugging tool), and Tooltip Symbol Insight. The scroller labeled Delay is used to set the delay time before the code insight features engage. This page also enables you to add, edit, or delete code templates. Figure 9.22 shows the Code Insight page of the Environment Options dialog box.
FIGURE 9.22. The Code Insight page of the Editor Properties dialog box.
The Add button enables you to add a new code template. When you click the Add button, the Add Code Template dialog box is displayed. Type a template name and a description and click OK. Now type the code for the template in the Code memo field. You can also paste code from the Clipboard. You can add as many templates as you want. When you click the OK button on the Environment Options dialog box, the new templates are added to the template file. To abandon changes to the code template file, click the Cancel button.
TIP: You can edit the code template file with the Delphi Code Editor if you find the Code Insight dialog box too small. The code template file is called DELPHI32.DCI and is located in the Delphi 4\Bin directory.
To delete a code template, first select the template in the Templates table and then click the Delete button. If you delete a template by mistake, just click the Cancel button on the Environment Options dialog box and your changes to the code template file will not be saved.
The Edit button enables you to change the name and description of a code template. When you click the Edit button, the Edit Code Template dialog box is displayed. This dialog box is identical to the Add Code Template dialog bo x. Type in a new name or description for the template and click OK. As with adding or deleting templates, the changes you make to the templates are not saved unless you click the OK button on the Environment Options dialog box. To edit the code for a template, modify the code in the Code memo field.
The Code Explorer is a new feature that is a much-welcomed addition to the Delphi IDE. The Code Explorer, as its name indicates, is used to quickly explore your source code units. The Code Explorer is normally docked on the left side of the Code Editor. When you first start Delphi, the Code Editor and Code Explorer appear as shown in Figure 9.23.
FIGURE 9.23. The Code Explorer for a new project.
The Code Explorer shows all of a unit's classes, functions, procedures, variables, and the uses list for the unit. The class node expands to show all of the properties, variables, fields, and methods of a particular class.
The Code Explorer shows the structure of the unit in the active Code Editor window. When you switch units in the Code Editor, the Code Explorer changes to show the new unit.
Like most Delphi windows, the Code Explorer window has a context menu. Table 9.5 lists the items on the Code Explorer context menu.
Item | Description |
New | Adds a new variable, method, function, or procedure to the unit. Also used to add a unit to the uses list. You can also use the Insert key on the keyboard to insert a new item. |
Rename | Renames an identifier (var iable, method, function, procedure, and so on). You can also use in-place editing to change an identifier's name. |
View Editor | Brings the Code Editor to the top when the Code Explorer is not docked to the Code Editor window. |
Dockable | Specifies whether the Code Explorer window is dockable. |
The Insert menu item is a powerful feature. Insert enables you to add a procedure, function, method of a class, or variable to either the unit or to a class in the unit. I'll talk more about that in just a bit.
To navigate to a particular method, function, or procedure, simply double-click the identifier name in the Code Explorer. The Code Editor jumps to the location in the source where that method is located. To locate a data field in a class or a unit variable's declaration, locate that variable's identifier in the Code Explorer window and double-click the identifier. The Code Editor displays the variable's declaration.
The Code Explorer can be used to add method and variable declarations to your source. To add a field variable to a class, for example, choose Insert from the Code Explorer context menu and type the declaration for the variable you want to add. For example, to add an Integer variable called X you would type the following:
X : Integer;
When you press the Enter key on the keyboard, the variable is added to the class.
You can add methods to a class just as easily. To see exactly how this works, perform the following exercise:
procedure Test;
function GetSomething : Byte;
AVariable : Integer;
You probably didn't notice, but while you were adding items, Delphi was busy making changes to the unit. Listing 9.1 shows the unit after performing this exercise.
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) private { Private declarations } public AVariable: Integer; procedure Test; function GetSomething: Byte; { Public declarations } end;
var
Form1: TForm1; implementation {$R *.DFM} function TForm1.GetSomething: Byte; begin end; procedure TForm1.Test; begin end; end.
Notice that not only are the declarations for the items you added present in the class declaration, but also the bodies of the methods are completed in the implementation section. The Code Explorer works together with class completion to make your job as a programmer easier. Figure 9.24 shows the Code Editor and Code Explorer after performing this exercise.
FIGURE 9.24. The Code Explorer showing the new methods and data field.
Th e Code Explorer is both a unit browser and a production tool. You can use it to navigate your units as you write code. I find it extremely useful for this purpose alone. You can also use the Code Explorer to add code to your units. The Code Explorer is a great productivity booster. A major bonus is that it is easy to use, too.
The Code Explorer display options are shown on the Explorer page of the Environment Options dialog box (Tools|Environment Options from the main menu). Figure 9.25 shows the Explorer page.
The various settings on this page control how the Code Explorer acts and how much or how little information it displays. See the Delphi help for the Explorer page for complete details on these settings.
FIGURE 9.25. The Explorer page of the Environment Options dialog box.
Today was one of those days when you learned a lot about the features that are often overlooked. I hope you picked up some tips that you can use as you work with Delphi projects and the Delphi Code Editor. You also got an explanation of what some of the project and editor options are for. Even if it didn't make much sense to you now, this chapter is something you can refer back to at a later date. Finally, you ended the day with a look at the Code Explorer. The Code Explorer is a tremendous tool, so be sure you spend some time learning how to use it. After you have used the Code Explorer, you won't be able to work without it.
The Workshop contains quiz questions to help you solidify your understanding of the material covered and exercises to provide you with experience in using what you have learned. You can find answers to the quiz questions in Appendix A, "Answers to the Quiz Questions."
© Copyright, Macmillan Computer Publishing. All rights reserved.