home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1996 August / VPR9608A.BIN / del20try / install / data.z / EDITFRM.PAS < prev    next >
Pascal/Delphi Source File  |  1996-05-08  |  7KB  |  271 lines

  1. unit EditFrm;
  2.  
  3. { This unit implements TEditForm, the MDI child form class, and TMemoDoc,
  4.   the automation object for a memo editor form. TMemoDoc implements the
  5.   following automated methods and properties:
  6.  
  7.   procedure Clear;
  8.     Clears the contents of the memo.
  9.  
  10.   procedure Insert(const Text: string);
  11.     Inserts the given string at the current cursor position.
  12.  
  13.   procedure Save;
  14.     Saves the contents of the memo.
  15.  
  16.   procedure Close;
  17.     Closes the memo.
  18.  
  19.   property FileName: string;
  20.     The name of the file associated with the memo. The memo can be renamed
  21.     by assigning to this property.
  22.  
  23.   property Modified: WordBool;
  24.     True if the memo has been modified since it was loaded or last saved.
  25.  
  26.   OLE Automation controllers obtain instances of TMemoDoc using the NewMemo
  27.   and OpenMemo methods of the "MemoEdit.Application" OLE class. Since
  28.   instances of TMemoDoc cannot be created through OLE, there is no need to
  29.   register the class.
  30.  
  31.   Notice how TEditForm creates an instance of TMemoDoc in the OnCreate
  32.   event handler, and releases the instance in the OnDestroy event handler.
  33.   This way, the automation object for a given form is always readily
  34.   available. Also notice how the Release method is used to release the
  35.   TMemoDoc instance, instead of the Free method normally used for other
  36.   objects. This ensures that the TMemoDoc instance is not actually disposed
  37.   until all references to it go away. In particular, if an OLE Automation
  38.   controller obtains an instance of TMemoDoc and calls its Close method,
  39.   the corresponding form is destroyed, but the TMemoDoc instance does not
  40.   go away until the OLE Automation controller releases its reference. This
  41.   also explains the NIL tests performed in each of the TMemoDoc methods to
  42.   ensure that the form has not been destroyed. }
  43.  
  44. interface
  45.  
  46. uses
  47.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  48.   StdCtrls, Menus, OleAuto;
  49.  
  50. type
  51.  
  52.   TMemoDoc = class;
  53.  
  54.   TEditForm = class(TForm)
  55.     Memo: TMemo;
  56.     MainMenu: TMainMenu;
  57.     FileMenu: TMenuItem;
  58.     FileNewItem: TMenuItem;
  59.     FileOpenItem: TMenuItem;
  60.     FileSaveItem: TMenuItem;
  61.     FileSaveAsItem: TMenuItem;
  62.     FileCloseItem: TMenuItem;
  63.     FileExitItem: TMenuItem;
  64.     procedure FormCreate(Sender: TObject);
  65.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  66.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  67.     procedure FileNewItemClick(Sender: TObject);
  68.     procedure FileOpenItemClick(Sender: TObject);
  69.     procedure FileSaveItemClick(Sender: TObject);
  70.     procedure FileSaveAsItemClick(Sender: TObject);
  71.     procedure FileCloseItemClick(Sender: TObject);
  72.     procedure FileExitItemClick(Sender: TObject);
  73.     procedure MemoChange(Sender: TObject);
  74.     procedure FormDestroy(Sender: TObject);
  75.   private
  76.     FMemoDoc: TMemoDoc;
  77.     FFileName: string;
  78.     FModified: Boolean;
  79.     FUnnamed: Boolean;
  80.     function GetOleObject: Variant;
  81.     procedure Rename(const NewName: string);
  82.     function Save(ChangeName, ForceSave: Boolean): Boolean;
  83.     procedure SaveToFile;
  84.   public
  85.     property OleObject: Variant read GetOleObject;
  86.   end;
  87.  
  88.   TMemoDoc = class(TAutoObject)
  89.   private
  90.     FEditForm: TEditForm;
  91.     function GetFileName: string;
  92.     function GetModified: WordBool;
  93.     procedure SetFileName(const Value: string);
  94.   automated
  95.     procedure Clear;
  96.     procedure Insert(const Text: string);
  97.     procedure Save;
  98.     procedure Close;
  99.     property FileName: string read GetFileName write SetFileName;
  100.     property Modified: WordBool read GetModified;
  101.   end;
  102.  
  103. implementation
  104.  
  105. {$R *.DFM}
  106.  
  107. uses MainFrm;
  108.  
  109. { TEditForm }
  110.  
  111. procedure TEditForm.FormCreate(Sender: TObject);
  112. begin
  113.   FMemoDoc := TMemoDoc.Create;
  114.   FMemoDoc.FEditForm := Self;
  115.   if MainForm.NewFileName = '' then
  116.   begin
  117.     FFileName := 'Untitled.txt';
  118.     FUnnamed := True;
  119.   end else
  120.   begin
  121.     FFileName := MainForm.NewFileName;
  122.     Memo.Lines.LoadFromFile(FFileName);
  123.     FUnnamed := False;
  124.   end;
  125.   Caption := FFileName;
  126.   FModified := False;
  127. end;
  128.  
  129. procedure TEditForm.FormDestroy(Sender: TObject);
  130. begin
  131.   if FMemoDoc <> nil then
  132.   begin
  133.     FMemoDoc.FEditForm := nil;
  134.     FMemoDoc.Release;
  135.   end;
  136. end;
  137.  
  138. procedure TEditForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  139. begin
  140.   CanClose := Save(False, False);
  141. end;
  142.  
  143. procedure TEditForm.FormClose(Sender: TObject; var Action: TCloseAction);
  144. begin
  145.   Action := caFree;
  146. end;
  147.  
  148. procedure TEditForm.FileNewItemClick(Sender: TObject);
  149. begin
  150.   MainForm.FileNewItemClick(Sender);
  151. end;
  152.  
  153. procedure TEditForm.FileOpenItemClick(Sender: TObject);
  154. begin
  155.   MainForm.FileOpenItemClick(Sender);
  156. end;
  157.  
  158. procedure TEditForm.FileSaveItemClick(Sender: TObject);
  159. begin
  160.   Save(False, True);
  161. end;
  162.  
  163. procedure TEditForm.FileSaveAsItemClick(Sender: TObject);
  164. begin
  165.   Save(True, True);
  166. end;
  167.  
  168. procedure TEditForm.FileCloseItemClick(Sender: TObject);
  169. begin
  170.   Close;
  171. end;
  172.  
  173. procedure TEditForm.FileExitItemClick(Sender: TObject);
  174. begin
  175.   MainForm.Close;
  176. end;
  177.  
  178. procedure TEditForm.MemoChange(Sender: TObject);
  179. begin
  180.   FModified := True;
  181. end;
  182.  
  183. function TEditForm.GetOleObject: Variant;
  184. begin
  185.   Result := FMemoDoc.OleObject;
  186. end;
  187.  
  188. procedure TEditForm.Rename(const NewName: string);
  189. begin
  190.   FFileName := ExpandFileName(NewName);
  191.   FUnnamed := False;
  192.   Caption := FFileName;
  193. end;
  194.  
  195. function TEditForm.Save(ChangeName, ForceSave: Boolean): Boolean;
  196. begin
  197.   Result := False;
  198.   if not ForceSave and FModified then
  199.     case MessageDlg(Format('Save changes to %s?',
  200.       [ExtractFileName(FFileName)]), mtConfirmation, mbYesNoCancel, 0) of
  201.       mrYes: ForceSave := True;
  202.       mrCancel: Exit;
  203.     end;
  204.   if ForceSave then
  205.   begin
  206.     if ChangeName or FUnnamed then
  207.       with MainForm.SaveDialog do
  208.       begin
  209.         FileName := FFileName;
  210.         DefaultExt := #0;
  211.         if not Execute then Exit;
  212.         Rename(FileName);
  213.       end;
  214.     SaveToFile;
  215.   end;
  216.   Result := True;
  217. end;
  218.  
  219. procedure TEditForm.SaveToFile;
  220. begin
  221.   Memo.Lines.SaveToFile(FFileName);
  222.   FModified := False;
  223. end;
  224.  
  225. { TMemoDoc }
  226.  
  227. procedure TMemoDoc.Clear;
  228. begin
  229.   if FEditForm <> nil then
  230.   begin
  231.     FEditForm.Memo.Clear;
  232.     FEditForm.FModified := True;
  233.   end;
  234. end;
  235.  
  236. procedure TMemoDoc.Close;
  237. begin
  238.   FEditForm.Free;
  239. end;
  240.  
  241. function TMemoDoc.GetFileName: string;
  242. begin
  243.   if FEditForm <> nil then
  244.     Result := FEditForm.FFileName else
  245.     Result := '';
  246. end;
  247.  
  248. function TMemoDoc.GetModified: WordBool;
  249. begin
  250.   if FEditForm <> nil then
  251.     Result := FEditForm.FModified else
  252.     Result := False;
  253. end;
  254.  
  255. procedure TMemoDoc.Insert(const Text: string);
  256. begin
  257.   if FEditForm <> nil then FEditForm.Memo.SelText := Text;
  258. end;
  259.  
  260. procedure TMemoDoc.Save;
  261. begin
  262.   if FEditForm <> nil then FEditForm.SaveToFile;
  263. end;
  264.  
  265. procedure TMemoDoc.SetFileName(const Value: string);
  266. begin
  267.   if FEditForm <> nil then FEditForm.Rename(Value);
  268. end;
  269.  
  270. end.
  271.