Session 2

Actions, menus, toolbars, image lists, file dialogs

Session #2

This session's main topic was to present Actions and see how they are connected to user-interface elements (e.g. menu items). I built a small text editor (like Notepad) to demonstrate this topic. The name of the project is TextEdit and you can download all of the project/source files here. There's a project file included (TextEdit.bdsproj). Just double-click on that and Turbo C++ should fire up and load the project. You can then build and run the application.

  1. TextEdit.cpp - This is where WinMain resides (This file will usually look very similar to all VCL applications.)
  2. MainForm.h - The definition of the main window.
  3. MainForm.cpp - The implementation of the main window.
  4. MainForm.dfm - The resource file for the main window. This file was built and managed by the IDE. You should browse through it for a few minutes to see how the components' information is stored in the file.
The project contains more code than I demonstrated because I didn't have enough time to implement all of it. I don't necessarily want to show every single line of code during the session, since most of it is repetitive. I also don't have the toolbar or images (menus/tool buttons) implemented in this project. (That's an exercise for you.) I'm including a folder with the bitmaps that I used for the images. You can download the executable and try it yourself.

You should load the project into Turbo C++ and run it. The code should be easy to follow. Most of the added code pertains to making sure that changes made to a file are not accidentally discarded. This can happen, for example, if the user changes a file and then chooses File | New from the menu. The application must ask the user if the modified file should be saved before discarding the current file. There is a variable named Dirty_ which keeps track of the state of the text. This is how the rest of the code knows that the file has been modified. There are still things that need to be fixed before moving on (and are exercises for the reader).

Exercises

  1. Add a toolbar (TToolBar) to the application with buttons for each of the 5 menu items.
  2. Create an image list (TImageList) and add the 5 images. Associate the image list with the actions, toolbar, and main menu.
  3. If the file has been modified and the user exits the application, no prompting is done and the changes are lost. (Hint: TForm has an event called OnCloseQuery that gives the form a chance before closing.)
  4. The File Open/Save dialog boxes allow the user to type in an invalid filename. This should be prevented. (Hint: this is an option in the dialogs)
  5. The position of the caret is only updated when a key is pressed. It doesn't get updated if you click the mouse in the TMemo or load a new file.
  6. Other things as well...


Some screenshots of the project at design time:

The Form at Design TimeActionList EditorMenu DesignerPanels Editor


The application showing some informationSome components used to build the application

The main menu with imagesA toolbar with images

Additional Information: Project Settings

By default, Turbo C++ builds applications to use runtime libraries (RTL) and packages. Since most people don't have Turbo C++ installed on their computers, they won't be able to run the executables without these additional dependencies. The preferred way to build applications is statically, so that all necessary components and libraries are included in the executable. It will make the executable larger, but that's no longer an issue these days. To build static executables, you have to change two settings in the project options. From the menu in Turbo C++, go to Project | Options... or press Ctrl-Shift-F11 to open the Project Options dialog box. You may want to collapse the "C++ Compiler (bcc32)" options, as it takes up all of the dialog box's list space.

Disable "Use dynamic RTL" under the Linker optionsDisable "Build with runtime packages" under the Packages option
File size when using the RTL and packagesFile size without the RTL and packages

Yes, it's larger, but it will work on any computer now.