home *** CD-ROM | disk | FTP | other *** search
/ Delphi 4 Bible / Delphi_4_Bible_Tom_Swan_IDG_Books_1998.iso / source / Mdidemo2 / main.pas < prev    next >
Pascal/Delphi Source File  |  1998-03-12  |  5KB  |  202 lines

  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls,
  7.   Forms, Dialogs, Menus, Child, ChildBmp;
  8.  
  9. type
  10.   TMainForm = class(TForm)
  11.     MainMenu1: TMainMenu;
  12.     FileMenu: TMenuItem;
  13.     FileOpen: TMenuItem;
  14.     FileSave: TMenuItem;
  15.     FileSaveAs: TMenuItem;
  16.     FileNew: TMenuItem;
  17.     N1: TMenuItem;
  18.     FileExit: TMenuItem;
  19.     WindowMenu: TMenuItem;
  20.     WindowCascade: TMenuItem;
  21.     WindowTile: TMenuItem;
  22.     WindowArrangeIcons: TMenuItem;
  23.     N2: TMenuItem;
  24.     WindowCloseAll: TMenuItem;
  25.     WindowMinimizeAll: TMenuItem;
  26.     HelpMenu: TMenuItem;
  27.     HelpAbout: TMenuItem;
  28.     OpenDialog: TOpenDialog;
  29.     FileClose: TMenuItem;
  30.     N3: TMenuItem;
  31.     SaveDialog: TSaveDialog;
  32.     procedure FileNewClick(Sender: TObject);
  33.     procedure FileOpenClick(Sender: TObject);
  34.     procedure FileCloseClick(Sender: TObject);
  35.     procedure FileSaveClick(Sender: TObject);
  36.     procedure FileSaveAsClick(Sender: TObject);
  37.     procedure FileExitClick(Sender: TObject);
  38.     procedure WindowCascadeClick(Sender: TObject);
  39.     procedure WindowTileClick(Sender: TObject);
  40.     procedure WindowArrangeIconsClick(Sender: TObject);
  41.     procedure WindowMinimizeAllClick(Sender: TObject);
  42.     procedure WindowCloseAllClick(Sender: TObject);
  43.     procedure HelpAboutClick(Sender: TObject);
  44.     procedure FileMenuClick(Sender: TObject);
  45.     procedure WindowMenuClick(Sender: TObject);
  46.   private
  47.   {- Private declarations }
  48.     procedure CreateChild(const Name: string);
  49.   public
  50.   {- Public declarations }
  51.   end;
  52.  
  53. var
  54.   MainForm: TMainForm;
  55.  
  56. implementation
  57.  
  58. uses About;
  59.  
  60. {$R *.DFM}
  61.  
  62. const
  63.   maxChildren = 10;  { Optional: No maximum required }
  64.  
  65. (* This procedure is replaced by the new CreateChild following,
  66.    which opens text or bitmap files depending on the filename
  67.    extension.
  68. procedure TMainForm.CreateChild(const Name: String);
  69. var
  70.   Child: TChildForm;
  71. begin
  72.   Child := TChildForm.Create(Application);
  73.   Child.Caption := Name;
  74. end;
  75. *)
  76.  
  77. { The new CreateChild creates a child window object based
  78.   on the filename extension. All files that don't end with
  79.   the extension .bmp are assumed to be text files. In practice,
  80.   you'll probably need other code to ensure that users open
  81.   the correct types of files. }
  82. procedure TMainForm.CreateChild(const Name: String);
  83. var
  84.   Child: TChildForm;
  85.   FExt: String[4];
  86. begin
  87.   FExt := ExtractFileExt(Name);
  88.   if FExt = '.bmp' then
  89.     Child := TChildBmpForm.Create(Application)
  90.   else
  91.     Child := TChildForm.Create(Application);
  92.   Child.Caption := Name;
  93. end;
  94.  
  95. procedure TMainForm.FileNewClick(Sender: TObject);
  96. begin
  97.   CreateChild('Untitled' + IntToStr(MDIChildCount + 1));
  98. end;
  99.  
  100. procedure TMainForm.FileOpenClick(Sender: TObject);
  101. begin
  102.   if OpenDialog.Execute then
  103.   begin
  104.     CreateChild(Lowercase(OpenDialog.FileName));
  105.     with ActiveMDIChild as TChildForm do
  106.       LoadData(OpenDialog.FileName);
  107.   end;
  108. end;
  109.  
  110. procedure TMainForm.FileCloseClick(Sender: TObject);
  111. begin
  112.   if ActiveMDIChild <> nil then
  113.     ActiveMDIChild.Close;
  114. end;
  115.  
  116. procedure TMainForm.FileSaveClick(Sender: TObject);
  117. begin
  118.   if Pos('Untitled', ActiveMDIChild.Caption) = 1 then
  119.     FileSaveAsClick(Sender)
  120.   else with ActiveMDIChild as TChildForm do
  121.     SaveData(Caption);
  122. end;
  123.  
  124. procedure TMainForm.FileSaveAsClick(Sender: TObject);
  125. var
  126.   FExt: String[4];
  127. begin
  128.   with SaveDialog do
  129.   begin
  130.     FileName := ActiveMDIChild.Caption;
  131.     FExt := ExtractFileExt(FileName);
  132.     if Length(FExt) = 0 then
  133.       FExt := '.*';
  134.     Filter := 'Files (*' + FExt + ')|*' + FExt;
  135.     if Execute then
  136.     with ActiveMDIChild as TChildForm do
  137.       SaveData(FileName);
  138.   end;
  139. end;
  140.  
  141. procedure TMainForm.FileExitClick(Sender: TObject);
  142. begin
  143.   Close;
  144. end;
  145.  
  146. procedure TMainForm.WindowCascadeClick(Sender: TObject);
  147. begin
  148.   Cascade;
  149. end;
  150.  
  151. procedure TMainForm.WindowTileClick(Sender: TObject);
  152. begin
  153.   Tile;
  154. end;
  155.  
  156. procedure TMainForm.WindowArrangeIconsClick(Sender: TObject);
  157. begin
  158.   ArrangeIcons;
  159. end;
  160.  
  161. procedure TMainForm.WindowMinimizeAllClick(Sender: TObject);
  162. var
  163.   I: Integer;
  164. begin
  165.   for I := MDIChildCount - 1 downto 0 do
  166.     MDIChildren[I].WindowState := wsMinimized;
  167. end;
  168.  
  169. procedure TMainForm.WindowCloseAllClick(Sender: TObject);
  170. var
  171.   I: Integer;
  172. begin
  173.   for I := MDIChildCount - 1 downto 0 do
  174.     MDIChildren[I].Close;
  175. end;
  176.  
  177. procedure TMainForm.HelpAboutClick(Sender: TObject);
  178. begin
  179.   AboutForm.ShowModal;
  180. end;
  181.  
  182. procedure TMainForm.FileMenuClick(Sender: TObject);
  183. begin
  184.   FileNew.Enabled := MDIChildCount < maxChildren;
  185.   FileOpen.Enabled := FileNew.Enabled;
  186.   FileClose.Enabled := MDIChildCount > 0;
  187.   FileSave.Enabled := FileClose.Enabled;
  188.   FileSaveAs.Enabled := FileClose.Enabled;
  189. end;
  190.  
  191. procedure TMainForm.WindowMenuClick(Sender: TObject);
  192. var
  193.   I: Integer;
  194. begin
  195.   with WindowMenu do
  196.   for I := 0 to Count - 1 do
  197.     with Items[I] as TMenuItem do
  198.       Enabled := MDIChildCount > 0;
  199. end;
  200.  
  201. end.
  202.