The File Menu


The File menu should contain commands that enable the user to terminate the application and create, load, and save files as shown in Figure 16-4. We are not going to implement the Page Setup and Print commands in this chapter.

image from book
Figure 16-4: The File menu

To create the File menu commands, add a TActionList component to the Designer Surface and then double-click it to display the Action List editor.

First, we need to create the File ® Exit command. Click the New Action button to create a new action and then do the following:

  1. Set the Caption property of the new action to E&xit.

  2. Set the Hint property to "Exit the application."

  3. Rename the action ExitAction.

  4. Finally, assign the action to an unnamed menu item in the File menu.

To close the application, you only have to call the Close method of the form in the action's OnExecute event handler:

procedure TMainForm.ExitActionExecute(Sender: TObject); begin   Close; end;

Loading Documents

Now we have to implement the File ® Open command to enable the user to open existing documents. First, we have to declare a private variable that will hold the file name of the opened document. This variable is required in the File ® Save command and in several other places, as you'll see in a moment.

type   TMainForm = class(TForm)   private   FOpenedFile: string; public end; 

To allow the user to select an existing document, you'll need to use the TOpenDialog component. Drop a TOpenDialog component on the Designer Surface and modify its Filter property to have the dialog box only display plain text files that have the .txt extension.

image from book
Figure 16-5: TOpenDialog Filter settings

It's always a good idea to add the All File Types filter because the user might have plain text documents that aren't saved under the .txt extension.

Now, add a new action to the action list and modify its settings as follows:

  1. Set the Caption property of the new action to &Open…

  2. Set the Hint property to "Open an existing document."

  3. Rename the action OpenAction.

  4. Set the Shortcut property to Ctrl+O.

To load a text document to a TMemo component, you have to do several things. First, you have to call the Execute method of the TOpenDialog component to allow the user to select a document. If the user clicks OK in the dialog box, the selected file name is written to the FileName property of the TOpenDialog component and the Execute method returns True. When the Execute method returns True, you have to copy the selected file name to the FOpenedFile variable and then call the LoadFromFile method to load the selected text document into the Lines property of the TMemo component (see Listing 16-1). Again, write this code in the action's OnExecute event handler.

Listing 16-1: Loading a text document

image from book
procedure TMainForm.OpenActionExecute(Sender: TObject); begin   if OpenDialog1.Execute then   begin     { save the selected file name }     FOpenedFile := OpenDialog1.FileName;     { load the selected document }     Editor.Lines.LoadFromFile(FOpenedFile);   end; end;
image from book

The last thing that you have to do is create the Open command in the File menu. Here's how you do that:

  1. Double-click the TMainMenu component to display the Menu Designer.

  2. Select the Exit menu item in the File menu.

  3. Press Insert on the keyboard to insert a new menu item before the Exit item.

  4. Assign the OpenAction action to the Action property of the new menu item.

    image from book
    Figure 16-6: Inserting a menu item

Saving Documents

The majority of applications have two separate commands that allow the user to save a document: Save and Save As. The Save command is used to save changes to an existing document, and the Save As command is used to save a document that doesn't exist on disk or to save a copy of the existing document under a new name.

We'll first implement the File ® Save As command. The Save As command has to enable the user to select the directory and the file name for the document using the TSaveDialog component, so add a TSaveDialog component to the Designer Surface and then modify the following properties:

  1. Set the DefaultExt property to "txt" (without the quotes) to have the dialog box automatically append the .txt extension to the file name.

  2. Create the same filter as you did for the TOpenDialog component.

  3. Expand the Options property and set the ofOverwritePrompt item to True.

When you include ofOverwritePrompt in the Options set, the TSaveDialog component automatically generates the following warning message if the user tries to use a file name that is already being used.

image from book
Figure 16-7: Result of the ofOverwritePrompt value

Now open the Action List editor and create the Save As action:

  1. Set the Caption property to Save &As…

  2. Rename the action SaveAsAction.

  3. Set the Hint property to "Save the active document with a new name."

Finally, place the following code in the OnExecute event handler and then create the Save As command in the File menu.

Listing 16-2: Saving the document using the Save As dialog box

image from book
procedure TMainForm.SaveAsActionExecute(Sender: TObject); begin   if SaveDialog1.Execute then   begin     { remember the new file name }     FOpenedFile := SaveDialog1.FileName;     { save the document }     Editor.Lines.SaveToFile(FOpenedFile);     Editor.Modified := False;   end; end;
image from book

The Save action has to do two things — save changes made to the active document if the document exists or display the Save As dialog box (execute the Save As action) if the document doesn't exist.

To create the Save action, follow these steps:

  1. Create a new action in the Action List editor.

  2. Set the Caption property of the new action to &Save.

  3. Set the Hint property to "Save the active document."

  4. Rename the action SaveAction.

  5. Set the ShortCut property to Ctrl+S.

Finally, here's the Save action's OnExecute event handler:

procedure TMainForm.SaveActionExecute(Sender: TObject); begin   { if the active document exists, save changes }   if FOpenedFile <> '' then   begin     Editor.Lines.SaveToFile(FOpenedFile);     Editor.Modified := False;   end else     { if the document doesn't exist, display the Save As dialog }     SaveAsAction.Execute; end;

Creating New Documents

The File ® New command is actually very simple because we only have to do two things:

  • Clear the FOpenedFile variable to let the application know that the active document doesn't exist.

  • Remove the existing text from the Editor component by calling Clear.

To create the New action, follow these steps:

  1. Create a new action in the Action List editor.

  2. Set the Caption property of the new action to &New.

  3. Set the Hint property to "Create a new document."

  4. Rename the action NewAction.

  5. Set the ShortCut property to Ctrl+N.

  6. Add the code in Listing 16-3 to the OnExecute event handler.

Listing 16-3: Creating a new document

image from book
procedure TMainForm.NewActionExecute(Sender: TObject); begin   Editor.Lines.Clear;   FOpenedFile := ''; end;
image from book

Protecting User Data

Now that the entire File menu is finished, as shown in Figure 16-8, we have to write a bit more code that will protect the contents of the active document.

image from book
Figure 16-8: The File menu

A well-designed application must check whether the user modified the contents of the active document and ask the user if he or she wants to save the document before allowing the user to:

  • Create a new document

  • Open an existing document

  • Close the entire application

For instance, if you type some text in Notepad and then try to close it, Note- pad automatically notifies you that you haven't saved the most recent changes to the document and allows you to either save or discard the changes in the active document.

image from book
Figure 16-9: Notepad properly protects user data.

The best way to implement this behavior is to create a function that will check whether the user modified the text in the editor and ask the user if he or she wants to discard or save the changes.

Listing 16-4: The function that enables the user to save/discard changes

image from book
type   TMainForm = class(TForm)   private     FOpenedFile: string;     function CloseCurrentDocument: Boolean;   public     { Public declarations }   end; function TMainForm.CloseCurrentDocument: Boolean; begin   Result := True; { enable the user to close the current document }   if Editor.Modified then   begin     case MessageDlg('Save changes to the current document?',       mtWarning, mbYesNoCancel, 0) of         mrYes: SaveAction.Execute;         mrCancel: Result := False;     end;      // case   end;        // if Editor.Modified end;
image from book

If the text displayed in the editor isn't modified, the function returns True, which allows us to either close the entire application or replace the text in the editor with an empty or an existing document. If the text in the editor is modified, the MessageDlg function is called to allow the user to save or discard the text displayed in the editor. If the user clicks Yes, the function first fires the Save action to save the document and returns True to notify us that we can now close the document. If the user clicks the No button, the function returns True because the user wants to discard the changes. Finally, if the user clicks the Cancel button, the function returns False to notify us that we can neither close the application nor replace the active document with another one.

Now, we have to call the CloseCurrentDocument function in the New and Open actions to actually ask the user what to do if there are changes in the text.

Listing 16-5: The updated New and Open actions

image from book
procedure TMainForm.NewActionExecute(Sender: TObject); begin   if CloseCurrentDocument then   begin     Editor.Clear;     FOpenedFile := '';   end; end; procedure TMainForm.OpenActionExecute(Sender: TObject); begin   if CloseCurrentDocument and OpenDialog1.Execute then   begin     FOpenedFile := OpenDialog1.FileName;     Editor.Lines.LoadFromFile(FOpenedFile);   end; end;
image from book

Figure 16-10 shows what happens when you modify the text in the editor and then try to open another document.

image from book
Figure 16-10: The CloseCurrentDocument function at work

The last thing we have to do now is write the handler for the form's OnCloseQuery event. This event occurs when the user tries to close the form. The CanClose parameter of the OnCloseQuery event enables us to specify whether or not the form can be closed.

The OnCloseQuery event handler is actually really simple. We only have to assign the result of the CloseCurrentDocument function to the CanClose parameter. This way, if the CloseCurrentDocument function returns True, the user can close the application. The only case when the closing of the form is stopped is when the text in the editor is modified and the user clicks Cancel in the MessageDlg dialog box.

Here's the OnCloseQuery event handler:

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin   CanClose := CloseCurrentDocument; end;



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net