home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / tpw / owldemos / mfileapp.pas < prev    next >
Pascal/Delphi Source File  |  1991-05-20  |  7KB  |  256 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal for Windows                     }
  4. {   Demo program                                 }
  5. {   Copyright (c) 1991 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. program MDIFileEditor;
  10.  
  11. {$R MFILEAPP.RES}
  12.  
  13. uses WObjects, WinTypes, WinProcs, WinDos, StdDlgs, StdWnds, Strings;
  14.  
  15. const
  16.   cm_SaveState    = 200;
  17.   cm_RestoreState = 201;
  18.  
  19. const
  20.   DskFile = 'MFILEAPP.DSK';
  21.  
  22. type
  23.  
  24.   { Declare TMDIFileApp, a TApplication descendant }
  25.   TMDIFileApp = object(TApplication)
  26.     procedure InitMainWindow; virtual;
  27.     procedure InitInstance; virtual;
  28.   end;
  29.  
  30.   { Declare TMDIFileWindow, a TMDIWindow descendant }
  31.   PMDIFileWindow = ^TMDIFileWindow;
  32.   TMDIFileWindow = object(TMDIWindow)
  33.     procedure SetupWindow; virtual;
  34.     procedure NewFile(var Msg: TMessage);
  35.       virtual cm_First + cm_MDIFileNew;
  36.     procedure OpenFile(var Msg: TMessage);
  37.       virtual cm_First + cm_MDIFileOpen;
  38.     procedure SaveState(var Msg: TMessage);
  39.       virtual cm_First + cm_SaveState;
  40.     procedure RestoreState(var Msg: TMessage);
  41.       virtual cm_First + cm_RestoreState;
  42.   end;
  43.  
  44.   { Declare TFileEditor, a TFileWindow desendant }
  45.   PFileEditor = ^TFileEditor;
  46.   TFileEditor = object(TFileWindow)
  47.     constructor Init(AParent: PWindowsObject; AFileName: PChar);
  48.     destructor Done; virtual;
  49.     procedure GetWindowClass(var AWndClass: TWndClass); virtual;
  50.     function GetClassName: PChar; virtual;
  51.   end;
  52.  
  53. const
  54.   RFileEditor: TStreamRec = (
  55.     ObjType: 1000;
  56.     VmtLink: Ofs(TypeOf(TFileEditor)^);
  57.     Load:    @TFileEditor.Load;
  58.     Store:   @TFileEditor.Store);
  59.  
  60. { TFileEditor }
  61.  
  62. const
  63.   EditorCount: Integer = 0;
  64.  
  65. type
  66.   TMenuState = (Enable, Disable);
  67.  
  68. procedure MenuItems(State: TMenuState);
  69.  
  70. procedure ModifyCommand(Command: Word);
  71. var
  72.   NewState: Word;
  73. begin
  74.   NewState := mf_ByCommand;
  75.   if State = Enable then Inc(NewState, mf_Enabled)
  76.   else Inc(NewState, mf_Disabled + mf_Grayed);
  77.   EnableMenuItem(PWindow(Application^.MainWindow)^.Attr.Menu, Command,
  78.     NewState);
  79. end;
  80.  
  81. begin
  82.   { Bail out if the window is already closed }
  83.   if Application^.MainWindow^.HWindow = 0 then Exit;
  84.  
  85.   ModifyCommand(cm_FileSave);
  86.   ModifyCommand(cm_FileSaveAs);
  87.   ModifyCommand(cm_ArrangeIcons);
  88.   ModifyCommand(cm_TileChildren);
  89.   ModifyCommand(cm_CascadeChildren);
  90.   ModifyCommand(cm_CloseChildren);
  91.   ModifyCommand(cm_EditCut);
  92.   ModifyCommand(cm_EditCopy);
  93.   ModifyCommand(cm_EditPaste);
  94.   ModifyCommand(cm_EditDelete);
  95.   ModifyCommand(cm_EditClear);
  96.   ModifyCommand(cm_EditUndo);
  97.   ModifyCommand(cm_EditFind);
  98.   ModifyCommand(cm_EditReplace);
  99.   ModifyCommand(cm_EditFindNext);
  100. end;
  101.  
  102. procedure IncEditors;
  103. begin
  104.   if EditorCount = 0 then MenuItems(Enable);
  105.   Inc(EditorCount);
  106. end;
  107.  
  108. procedure DecEditors;
  109. begin
  110.   Dec(EditorCount);
  111.   if EditorCount = 0 then MenuItems(Disable);
  112. end;
  113.  
  114. constructor TFileEditor.Init(AParent: PWindowsObject; AFileName: PChar);
  115. begin
  116.   TFileWindow.Init(AParent, '', AFileName);
  117.   IncEditors;
  118. end;
  119.  
  120. destructor TFileEditor.Done;
  121. begin
  122.   DecEditors;
  123.   TFileWindow.Done;
  124. end;
  125.  
  126. procedure TFileEditor.GetWindowClass(var AWndClass: TWndClass);
  127. begin
  128.   TFileWindow.GetWindowClass(AWndClass);
  129.   AWndClass.hIcon := LoadIcon(HInstance, 'FILEICON');
  130. end;
  131.  
  132. function TFileEditor.GetClassName: PChar;
  133. begin
  134.   GetClassName := 'FileEditor';
  135. end;
  136.  
  137. { Respond to "New" command by constructing, creating, and setting up a
  138.   new TFileWindow MDI child }
  139. procedure TMDIFileWindow.NewFile(var Msg: TMessage);
  140. begin
  141.   Application^.MakeWindow(New(PFileEditor, Init(@Self, '')));
  142. end;
  143.  
  144. procedure TMDIFileWindow.SetupWindow;
  145. begin
  146.   TMDIWindow.SetupWindow;
  147.   MenuItems(Disable);
  148. end;
  149.  
  150. { Respond to "Open" command by constructing, creating, and setting up a
  151.   new TFileWindow MDI child }
  152. procedure TMDIFileWindow.OpenFile(var Msg: TMessage);
  153. var
  154.   FileName: array[0..fsPathName] of Char;
  155. begin
  156.   if Application^.ExecDialog(New(PFileDialog, Init(@Self, PChar(sd_FileOpen),
  157.       StrCopy(FileName, '*.*')))) = id_Ok then
  158.     Application^.MakeWindow(New(PFileEditor, Init(@Self, FileName)));
  159. end;
  160.  
  161. { Save the the position and contents of the windows to the
  162.   "desk top" file. }
  163. procedure TMDIFileWindow.SaveState(var Msg: TMessage);
  164. var
  165.   S: PStream;
  166.  
  167. function FileDelete(Name: PChar): Integer; assembler;
  168. asm
  169.     PUSH    DS
  170.     LDS    DX,Name
  171.     MOV    AH,41H
  172.     INT    21H
  173.     JC    @@1
  174.     XOR    AX,AX
  175. @@1:    NEG    AX
  176.     POP    DS
  177. end;
  178.  
  179. begin
  180.   S := New(PBufStream, Init(DskFile, stCreate, 1024));
  181.   PutChildren(S^);
  182.   if S^.Status <> stOk then
  183.   begin
  184.     Dispose(S, Done);
  185.     FileDelete(DskFile);
  186.     MessageBox(HWindow, 'Unable to write desktop file.', 'Disk error',
  187.       mb_Ok or mb_IconExclamation);
  188.   end
  189.   else Dispose(S, Done);
  190. end;
  191.  
  192. { Read windows positions and contents from the "desk top" file. }
  193. procedure TMDIFileWindow.RestoreState(var Msg: TMessage);
  194. var
  195.   S: PStream;
  196.   ErrorMsg: PChar;
  197. begin
  198.   ErrorMsg := nil;
  199.   S := New(PBufStream, Init(DskFile, stOpenRead, 1024));
  200.   if S^.Status <> stOk then
  201.     ErrorMsg := 'Unable to open desktop file.'
  202.   else
  203.   begin
  204.     CloseChildren;
  205.     GetChildren(S^);
  206.     if S^.Status <> stOk then
  207.       ErrorMsg := 'Error reading desktop file.';
  208.     if LowMemory then
  209.     begin
  210.       CloseChildren;
  211.       ErrorMsg := 'Not enough memory to open file.'
  212.     end
  213.     else CreateChildren;
  214.   end;
  215.   if ErrorMsg <> nil then
  216.     MessageBox(HWindow, ErrorMsg, 'Disk error', mb_Ok or mb_IconExclamation);
  217. end;
  218.  
  219. { Construct the TMDIFileApp's MainWindow of type TMDIFileWindow,
  220.   loading its menu }
  221. procedure TMDIFileApp.InitMainWindow;
  222. begin
  223.   MainWindow := New(PMDIFileWindow, Init('MDI Files',
  224.     LoadMenu(HInstance, 'Commands')));
  225.   PMDIFileWindow(MainWindow)^.ChildMenuPos := 3;
  226.  
  227.   { Register types to be written to stream }
  228.   RegisterType(RWindow);
  229.   RegisterType(REdit);
  230.   RegisterType(RFileEditor);
  231. end;
  232.  
  233. { Initialize each MS-Windows application instance, loading an
  234.   accelerator table }
  235. procedure TMDIFileApp.InitInstance;
  236. begin
  237.   TApplication.InitInstance;
  238.   if Status = 0 then
  239.   begin
  240.     HAccTable := LoadAccelerators(HInstance, 'FileCommands');
  241.     if HAccTable = 0 then
  242.       Status := em_InvalidWindow;
  243.   end;
  244. end;
  245.  
  246. { Declare a variable of type TFileApp }
  247. var
  248.   MDIFileApp : TMDIFileApp;
  249.  
  250. { Run the FileApp }
  251. begin
  252.   MDIFileApp.Init('MDIFileApp');
  253.   MDIFileApp.Run;
  254.   MDIFileApp.Done;
  255. end.
  256.