home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Runimage / Delphi50 / Demos / Activex / Oleauto / Autoserv / EDITFRM.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-11  |  6.2 KB  |  261 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 "MemoApp.Application" OLE class. Since
  28.   instances of TMemoDoc cannot be created through OLE, there is no need to
  29.   register the class. }
  30.  
  31. interface
  32.  
  33. uses
  34.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  35.   StdCtrls, Menus, ComObj, Memo_TLB;
  36.  
  37. type
  38.   TMemoDoc = class;
  39.  
  40.   TEditForm = class(TForm)
  41.     Memo: TMemo;
  42.     MainMenu: TMainMenu;
  43.     FileMenu: TMenuItem;
  44.     FileNewItem: TMenuItem;
  45.     FileOpenItem: TMenuItem;
  46.     FileSaveItem: TMenuItem;
  47.     FileSaveAsItem: TMenuItem;
  48.     FileCloseItem: TMenuItem;
  49.     FileExitItem: TMenuItem;
  50.     procedure FormCreate(Sender: TObject);
  51.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  52.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  53.     procedure FileNewItemClick(Sender: TObject);
  54.     procedure FileOpenItemClick(Sender: TObject);
  55.     procedure FileSaveItemClick(Sender: TObject);
  56.     procedure FileSaveAsItemClick(Sender: TObject);
  57.     procedure FileCloseItemClick(Sender: TObject);
  58.     procedure FileExitItemClick(Sender: TObject);
  59.     procedure MemoChange(Sender: TObject);
  60.     procedure FormDestroy(Sender: TObject);
  61.   private
  62.     FMemoDoc: TMemoDoc;
  63.     FFileName: string;
  64.     FModified: Boolean;
  65.     FUnnamed: Boolean;
  66.     function GetOleObject: Variant;
  67.     procedure Rename(const NewName: string);
  68.     function Save(ChangeName, ForceSave: Boolean): Boolean;
  69.     procedure SaveToFile;
  70.   public
  71.     property OleObject: Variant read GetOleObject;
  72.   end;
  73.  
  74.   TMemoDoc = class(TAutoObject, IMemoDoc)
  75.   private
  76.     FEditForm: TEditForm;
  77.   protected
  78.     function Get_FileName: WideString; safecall;
  79.     function Get_Modified: WordBool; safecall;
  80.     procedure Clear; safecall;
  81.     procedure Close; safecall;
  82.     procedure Insert(const Text: WideString); safecall;
  83.     procedure Save; safecall;
  84.     procedure Set_FileName(const Value: WideString); safecall;
  85.   end;
  86.  
  87. implementation
  88.  
  89. {$R *.DFM}
  90.  
  91. uses MainFrm, ComServ;
  92.  
  93. { TEditForm }
  94.  
  95. procedure TEditForm.FormCreate(Sender: TObject);
  96. begin
  97.   if not MainForm.Local then
  98.   begin
  99.     FMemoDoc := TMemoDoc.Create;
  100.     FMemoDoc.FEditForm := Self;
  101.   end;
  102.   if MainForm.NewFileName = '' then
  103.   begin
  104.     FFileName := 'Untitled.txt';
  105.     FUnnamed := True;
  106.   end else
  107.   begin
  108.     FFileName := MainForm.NewFileName;
  109.     Memo.Lines.LoadFromFile(FFileName);
  110.     FUnnamed := False;
  111.   end;
  112.   Caption := FFileName;
  113.   FModified := False;
  114.   MainForm.Local := False;
  115. end;
  116.  
  117. procedure TEditForm.FormDestroy(Sender: TObject);
  118. begin
  119.   if FMemoDoc <> nil then
  120.   begin
  121.     FMemoDoc.FEditForm := nil;
  122.     FMemoDoc := nil;
  123.   end;
  124. end;
  125.  
  126. procedure TEditForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  127. begin
  128.   CanClose := Save(False, False);
  129. end;
  130.  
  131. procedure TEditForm.FormClose(Sender: TObject; var Action: TCloseAction);
  132. begin
  133.   Action := caFree;
  134. end;
  135.  
  136. procedure TEditForm.FileNewItemClick(Sender: TObject);
  137. begin
  138.   MainForm.FileNewItemClick(Sender);
  139. end;
  140.  
  141. procedure TEditForm.FileOpenItemClick(Sender: TObject);
  142. begin
  143.   MainForm.FileOpenItemClick(Sender);
  144. end;
  145.  
  146. procedure TEditForm.FileSaveItemClick(Sender: TObject);
  147. begin
  148.   Save(False, True);
  149. end;
  150.  
  151. procedure TEditForm.FileSaveAsItemClick(Sender: TObject);
  152. begin
  153.   Save(True, True);
  154. end;
  155.  
  156. procedure TEditForm.FileCloseItemClick(Sender: TObject);
  157. begin
  158.   Close;
  159. end;
  160.  
  161. procedure TEditForm.FileExitItemClick(Sender: TObject);
  162. begin
  163.   MainForm.Close;
  164. end;
  165.  
  166. procedure TEditForm.MemoChange(Sender: TObject);
  167. begin
  168.   FModified := True;
  169. end;
  170.  
  171. function TEditForm.GetOleObject: Variant;
  172. begin
  173.   Result := FMemoDoc as IDispatch;
  174. end;
  175.  
  176. procedure TEditForm.Rename(const NewName: string);
  177. begin
  178.   FFileName := ExpandFileName(NewName);
  179.   FUnnamed := False;
  180.   Caption := FFileName;
  181. end;
  182.  
  183. function TEditForm.Save(ChangeName, ForceSave: Boolean): Boolean;
  184. begin
  185.   Result := False;
  186.   if not ForceSave and FModified then
  187.     case MessageDlg(Format('Save changes to %s?',
  188.       [ExtractFileName(FFileName)]), mtConfirmation, mbYesNoCancel, 0) of
  189.       mrYes: ForceSave := True;
  190.       mrCancel: Exit;
  191.     end;
  192.   if ForceSave then
  193.   begin
  194.     if ChangeName or FUnnamed then
  195.       with MainForm.SaveDialog do
  196.       begin
  197.         FileName := FFileName;
  198.         DefaultExt := #0;
  199.         if not Execute then Exit;
  200.         Rename(FileName);
  201.       end;
  202.     SaveToFile;
  203.   end;
  204.   Result := True;
  205. end;
  206.  
  207. procedure TEditForm.SaveToFile;
  208. begin
  209.   Memo.Lines.SaveToFile(FFileName);
  210.   FModified := False;
  211. end;
  212.  
  213. { TMemoDoc }
  214.  
  215. procedure TMemoDoc.Clear;
  216. begin
  217.   if FEditForm <> nil then
  218.   begin
  219.     FEditForm.Memo.Clear;
  220.     FEditForm.FModified := True;
  221.   end;
  222. end;
  223.  
  224. procedure TMemoDoc.Close;
  225. begin
  226.   FEditForm.Free;
  227. end;
  228.  
  229. function TMemoDoc.Get_FileName: WideString;
  230. begin
  231.   if FEditForm <> nil then
  232.     Result := FEditForm.FFileName else
  233.     Result := '';
  234. end;
  235.  
  236. function TMemoDoc.Get_Modified: WordBool;
  237. begin
  238.   if FEditForm <> nil then
  239.     Result := FEditForm.FModified else
  240.     Result := False;
  241. end;
  242.  
  243. procedure TMemoDoc.Insert(const Text: WideString);
  244. begin
  245.   if FEditForm <> nil then FEditForm.Memo.SelText := Text;
  246. end;
  247.  
  248. procedure TMemoDoc.Save;
  249. begin
  250.   if FEditForm <> nil then FEditForm.SaveToFile;
  251. end;
  252.  
  253. procedure TMemoDoc.Set_FileName(const Value: WideString);
  254. begin
  255.   if FEditForm <> nil then FEditForm.Rename(Value);
  256. end;
  257.  
  258. initialization
  259.   TAutoObjectFactory.Create(ComServer, TMemoDoc, Class_MemoDoc, ciInternal);
  260. end.
  261.