home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l038 / 1.img / OWLDEMOS.PAK / MFILEAPP.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1991-03-14  |  6.4 KB  |  253 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.   ModifyCommand(cm_FileSave);
  83.   ModifyCommand(cm_FileSaveAs);
  84.   ModifyCommand(cm_ArrangeIcons);
  85.   ModifyCommand(cm_TileChildren);
  86.   ModifyCommand(cm_CascadeChildren);
  87.   ModifyCommand(cm_CloseChildren);
  88.   ModifyCommand(cm_EditCut);
  89.   ModifyCommand(cm_EditCopy);
  90.   ModifyCommand(cm_EditPaste);
  91.   ModifyCommand(cm_EditDelete);
  92.   ModifyCommand(cm_EditClear);
  93.   ModifyCommand(cm_EditUndo);
  94.   ModifyCommand(cm_EditFind);
  95.   ModifyCommand(cm_EditReplace);
  96.   ModifyCommand(cm_EditFindNext);
  97. end;
  98.  
  99. procedure IncEditors;
  100. begin
  101.   if EditorCount = 0 then MenuItems(Enable);
  102.   Inc(EditorCount);
  103. end;
  104.  
  105. procedure DecEditors;
  106. begin
  107.   Dec(EditorCount);
  108.   if EditorCount = 0 then MenuItems(Disable);
  109. end;
  110.  
  111. constructor TFileEditor.Init(AParent: PWindowsObject; AFileName: PChar);
  112. begin
  113.   TFileWindow.Init(AParent, '', AFileName);
  114.   IncEditors;
  115. end;
  116.  
  117. destructor TFileEditor.Done;
  118. begin
  119.   TFileWindow.Done;
  120.   DecEditors;
  121. end;
  122.  
  123. procedure TFileEditor.GetWindowClass(var AWndClass: TWndClass);
  124. begin
  125.   TFileWindow.GetWindowClass(AWndClass);
  126.   AWndClass.hIcon := LoadIcon(HInstance, 'FILEICON');
  127. end;
  128.  
  129. function TFileEditor.GetClassName: PChar;
  130. begin
  131.   GetClassName := 'FileEditor';
  132. end;
  133.  
  134. { Respond to "New" command by constructing, creating, and setting up a
  135.   new TFileWindow MDI child }
  136. procedure TMDIFileWindow.NewFile(var Msg: TMessage);
  137. begin
  138.   Application^.MakeWindow(New(PFileEditor, Init(@Self, '')));
  139. end;
  140.  
  141. procedure TMDIFileWindow.SetupWindow;
  142. begin
  143.   TMDIWindow.SetupWindow;
  144.   MenuItems(Disable);
  145. end;
  146.  
  147. { Respond to "Open" command by constructing, creating, and setting up a
  148.   new TFileWindow MDI child }
  149. procedure TMDIFileWindow.OpenFile(var Msg: TMessage);
  150. var
  151.   FileName: array[0..fsPathName] of Char;
  152. begin
  153.   if Application^.ExecDialog(New(PFileDialog, Init(@Self, PChar(sd_FileOpen),
  154.       StrCopy(FileName, '*.*')))) = id_Ok then
  155.     Application^.MakeWindow(New(PFileEditor, Init(@Self, FileName)));
  156. end;
  157.  
  158. { Save the the position and contents of the windows to the
  159.   "desk top" file. }
  160. procedure TMDIFileWindow.SaveState(var Msg: TMessage);
  161. var
  162.   S: PStream;
  163.  
  164. function FileDelete(Name: PChar): Integer; assembler;
  165. asm
  166.     PUSH    DS
  167.     LDS    DX,Name
  168.     MOV    AH,41H
  169.     INT    21H
  170.     JC    @@1
  171.     XOR    AX,AX
  172. @@1:    NEG    AX
  173.     POP    DS
  174. end;
  175.  
  176. begin
  177.   S := New(PBufStream, Init(DskFile, stCreate, 1024));
  178.   PutChildren(S^);
  179.   if S^.Status <> stOk then
  180.   begin
  181.     Dispose(S, Done);
  182.     FileDelete(DskFile);
  183.     MessageBox(HWindow, 'Unable to write desktop file.', 'Disk error',
  184.       mb_Ok or mb_IconExclamation);
  185.   end
  186.   else Dispose(S, Done);
  187. end;
  188.  
  189. { Read windows positions and contents from the "desk top" file. }
  190. procedure TMDIFileWindow.RestoreState(var Msg: TMessage);
  191. var
  192.   S: PStream;
  193.   ErrorMsg: PChar;
  194. begin
  195.   ErrorMsg := nil;
  196.   S := New(PBufStream, Init(DskFile, stOpenRead, 1024));
  197.   if S^.Status <> stOk then
  198.     ErrorMsg := 'Unable to open desktop file.'
  199.   else
  200.   begin
  201.     CloseChildren;
  202.     GetChildren(S^);
  203.     if S^.Status <> stOk then
  204.       ErrorMsg := 'Error reading desktop file.';
  205.     if LowMemory then
  206.     begin
  207.       CloseChildren;
  208.       ErrorMsg := 'Not enough memory to open file.'
  209.     end
  210.     else CreateChildren;
  211.   end;
  212.   if ErrorMsg <> nil then
  213.     MessageBox(HWindow, ErrorMsg, 'Disk error', mb_Ok or mb_IconExclamation);
  214. end;
  215.  
  216. { Construct the TMDIFileApp's MainWindow of type TMDIFileWindow,
  217.   loading its menu }
  218. procedure TMDIFileApp.InitMainWindow;
  219. begin
  220.   MainWindow := New(PMDIFileWindow, Init('MDI Files',
  221.     LoadMenu(HInstance, 'Commands')));
  222.   PMDIFileWindow(MainWindow)^.ChildMenuPos := 3;
  223.  
  224.   { Register types to be written to stream }
  225.   RegisterType(RWindow);
  226.   RegisterType(REdit);
  227.   RegisterType(RFileEditor);
  228. end;
  229.  
  230. { Initialize each MS-Windows application instance, loading an
  231.   accelerator table }
  232. procedure TMDIFileApp.InitInstance;
  233. begin
  234.   TApplication.InitInstance;
  235.   if Status = 0 then
  236.   begin
  237.     HAccTable := LoadAccelerators(HInstance, 'FileCommands');
  238.     if HAccTable = 0 then
  239.       Status := em_InvalidWindow;
  240.   end;
  241. end;
  242.  
  243. { Declare a variable of type TFileApp }
  244. var
  245.   MDIFileApp : TMDIFileApp;
  246.  
  247. { Run the FileApp }
  248. begin
  249.   MDIFileApp.Init('MDIFileApp');
  250.   MDIFileApp.Run;
  251.   MDIFileApp.Done;
  252. end.
  253.