Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Chapter 8 Files and Devices

By Felipe Monteiro de Carvalho and Jrg Braun

All programs have to deal with files in one way or another. Lazarus offers many ways for handling files. Which one we use depends on the result we want to achieve. A simple example is the need most programs have to save configuration data in a file for later use. The TlniFile class offers a convenient encapsulation of file routines for doing exactly that. There may also be cases where a user will need to choose the required settings from a list of alternative configurations.

Lazarus provides platform-independent ways of opening and saving files, by making use of controls from each platform's widget-set.
Property DefaultExt FileName Filter Filterindex Function

8.1 File Dialogs in Lazarus

The predefined file name extension, a string. The predefined or selected file name after the dialog is closed. A list of file type descriptions and their associated extensions. Determines which description will be show when the dialog is opened. Usually set to 1. InitialDir Initial directory on opening the dialog. Normally empty, in which case the current directory will be used. Options A set of options indicating whether hidden files and directories should be displayed, whether creating new files is allowed, etc. Title Title line shown at the top of the dialog. Width, Height Height and width of the dialog. These should normally be left at 0 to get the predefined values. Tag The component tag.

Table 8.1: The properties of file and directory dialog components The Component Palette's Dialogs tab contains the following file selection controls: TOpenDialog, TSaveDialog, TSelectDirectoryDialog, TOpenPictureDialog and TSavePictureDialog. Each name describes its purpose. TSelectDirectoryDialog differs from the other four components, in that it uses a completely different control in Windows compared to Gtk+/Gtk2 and Qt. The properties shared by the file dialogs are listed in Table 8.1. File dialog events are the same in all five dialogs (see Table 8.2). 514
Lazarus - the complete guide

8.1 File Dialogs in Lazarus Event OnCanClose OnClose OnFolderChange OnHelpClicked OnSelectionChange OnShow OnTypeChange The event is triggered When the user tries to close the dialog. Can be disabled by setting CanClose to False in the event procedure. When the dialog closes. When the directory changes. When the Help button is pressed. When the file selection changes. When the dialog opens. When the filter type changes.

Table 8.2: The event handler for file dialogs Usually there is no need to react to these events. All dialogs save the file name (including the path) into the property FileName when the Execute method is called. TSelectDirectoryDialog loads a target path only. This is only true if the method completes successfully, meaning that the dialog was closed with the OK button and not with Cancel. (The keys [Enter] and [Esc] can be used instead of these buttons). This is a code example of using a TOpenDialog:
if OpenDialog.Execute then Memo1.Lines.LoadFromFile(OpenDialog1.FileName);

That is all there is to selecting a file for reading. When we want to save the file it will look as follows:
if OpenDialog.Execute then Memo1.Lines.SaveToFile(OpenDialog1.FileName);

For loading and saving pictures the two xxxPictureDialogs should be used. They differ from the normal dialogs in that they provide previews and they have a different default for the filter property. This list comes already filled with the graphical formats supported by Lazarus:
Graphic (*.bmp;*.xpm;*.png;*.pbm;*.pgm;*.ppm;*.ico;*.icns;*.cur;*.jpg;*.jpeg; *.jpe;*.jfif;*.tif;*.tiff;*.gif) |*.bmp;*.xpm;*.png;*.pbm;*.pgm;*.ppm;*.ico;*.icns;*.cur;*.jpg;*.jpeg; *.jpe;*.jfif;*.tif;*.tiff;*.gif| Bitmaps (*.bmp)|*.bmp| Pixmap (*.xpm)|*.xpm| Portable Network Graphic (*.png)|*.png| Portable PixMap (*.pbm;*.pgm;*.ppm)|*.pbm;*.pgm;*.ppm| Icon (*.ico)|*.ico| Mac OS X Icon (*.icns)|*.icns| Cursor (*.cur)|*.cur| Joint Picture Expert Group (*.jpg;*.jpeg;*.jpe;*.jfif)|*.jpg;*.jpeg;*.jpe;*.jfif| Tagged Image File Format (*.tif;*.tiff)|*.tif;*.tiff| Graphics Interchange Format (*.gif)|*.gif| All files (*.*)|*.*|

Lazarus - the complete guide

515

You might also like