home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 10.ddi / MFILEAPP.ZIP / MFILEAPP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.1 KB  |  155 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. #include <owl.h>
  4. #include <filewnd.h>
  5. #include <mdi.h>
  6. #include <string.h>
  7. #include <io.h>
  8. #include "mfileapp.h"
  9.  
  10. const char DskFile[] = "MFILEAPP.SAV";
  11.  
  12. _CLASSDEF(TMDIFileApp)
  13. _CLASSDEF(TMDIFileWindow)
  14.  
  15. // Declare TMDIFileApp, a TApplication descendant
  16. class _CLASSTYPE TMDIFileApp : public TApplication {
  17. public:
  18.     TMDIFileApp(LPSTR name, HINSTANCE hInstance,
  19.           HINSTANCE hPrevInstance, LPSTR lpCmd,
  20.           int nCmdShow)
  21.             : TApplication(name, hInstance,
  22.                    hPrevInstance, lpCmd, nCmdShow) {};
  23.     virtual void InitMainWindow();
  24.     virtual void InitInstance();
  25. };
  26.  
  27. // Declare TMDIFileWindow, a TMDIFrame descendant
  28. class _CLASSTYPE TMDIFileWindow : public TMDIFrame {
  29. public:
  30.     WORD ChildNum;
  31.  
  32.     TMDIFileWindow(LPSTR ATitle, LPSTR MenuName);
  33.     virtual void NewFile(RTMessage Msg) =
  34.                  [CM_FIRST + CM_MDIFILENEW];
  35.     virtual void OpenFile(RTMessage Msg) =
  36.                  [CM_FIRST + CM_MDIFILEOPEN];
  37.     virtual void SaveState(RTMessage Msg) =
  38.                  [CM_FIRST + CM_SAVESTATE];
  39.     virtual void RestoreState(RTMessage Msg) =
  40.                  [CM_FIRST + CM_RESTORESTATE];
  41. };
  42.  
  43. TMDIFileWindow::TMDIFileWindow(LPSTR ATitle, LPSTR MenuName)
  44.                    : TMDIFrame(ATitle, MenuName)
  45. {
  46.   ChildNum = 1;
  47. }
  48.  
  49. /* Respond to "New" command by constructing, creating, and setting up a
  50.   new TFileWindow MDI child */
  51. void TMDIFileWindow::NewFile(RTMessage)
  52. {
  53.   char ChildName[14];
  54.  
  55.   wsprintf(ChildName,"MDI Child %d", ChildNum++);
  56.   GetApplication()->MakeWindow(new TFileWindow(this, ChildName, ""));
  57. }
  58.  
  59. /* Respond to "Open" command by constructing, creating, and setting up a
  60.   new TFileWindow MDI child */
  61. void TMDIFileWindow::OpenFile(RTMessage)
  62. {
  63.   char FileName[MAXPATH];
  64.   char ChildName[14];
  65.  
  66.   wsprintf(ChildName,"MDI Child %d", ChildNum++);
  67.   if ( GetApplication()->ExecDialog(new TFileDialog(this, SD_FILEOPEN,
  68.                              _fstrcpy(FileName, "*.*"))) == IDOK )
  69.     GetApplication()->MakeWindow(new TFileWindow(this, ChildName, FileName));
  70. }
  71.  
  72. /* Save the the position and contents of the windows to the
  73.   "desk top" file. */
  74. void TMDIFileWindow::SaveState(RTMessage)
  75. {
  76.   ofpstream os(DskFile);
  77.  
  78.   PutChildren(os);
  79.   os.close();
  80.  
  81.   if ( os.bad() )
  82.   {
  83.     unlink(DskFile);
  84.     MessageBox(HWindow, "Unable to write desktop file.", "Disk error",
  85.       MB_OK | MB_ICONEXCLAMATION);
  86.   }
  87. }
  88.  
  89. void DoCreateChild(Pvoid P, Pvoid)
  90. {
  91.   if ( ((PTWindowsObject)P)->IsFlagSet(WB_AUTOCREATE) )
  92.     ((PTWindowsObject)P)->Create();
  93. }
  94.  
  95. /* Read windows positions and contents from the "desk top" file. */
  96. void TMDIFileWindow::RestoreState(RTMessage)
  97. {
  98.   LPSTR ErrorMsg = NULL;
  99.  
  100.   ifpstream is(DskFile);
  101.   if ( is.bad() )
  102.     ErrorMsg = _fstrdup("Unable to open desktop file.");
  103.   else
  104.   {
  105.     if ( CloseChildren() )
  106.     {
  107.       GetChildren(is);
  108.       if ( is.bad() )
  109.         ErrorMsg = _fstrdup("Error reading desktop file.");
  110.       if ( GetApplication()->LowMemory() )
  111.       {
  112.         CloseChildren();
  113.         ErrorMsg = _fstrdup("Not enough memory to open file.");
  114.       }
  115.       else
  116.         ForEach(DoCreateChild, NULL);
  117.     }
  118.   }
  119.   if ( ErrorMsg )
  120.     MessageBox(HWindow, ErrorMsg, "Disk error",
  121.                                    MB_OK | MB_ICONEXCLAMATION);
  122. }
  123.  
  124.  
  125. /* Construct the TMDIFileApp's MainWindow of type TMDIFileWindow,
  126.   loading its menu */
  127. void TMDIFileApp::InitMainWindow()
  128. {
  129.   MainWindow = new TMDIFileWindow("MDI Files", "Commands");
  130.   ((PTMDIFileWindow)MainWindow)->ChildMenuPos = 3;
  131. }
  132.  
  133. /* Initialize each MS-Windows application instance, loading an
  134.    accelerator table */
  135. void TMDIFileApp::InitInstance()
  136. {
  137.   TApplication::InitInstance();
  138.   if ( Status == 0 )
  139.   {
  140.     HAccTable = LoadAccelerators(hInstance, "FileCommands");
  141.     if ( HAccTable == 0 )
  142.       Status = EM_INVALIDWINDOW;
  143.   }
  144. }
  145.  
  146. // Run the MDIFileApp
  147. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  148.            LPSTR lpCmd, int nCmdShow)
  149. {
  150.     TMDIFileApp MDIFileApp("MDIFileApp", hInstance, hPrevInstance,
  151.         lpCmd, nCmdShow);
  152.     MDIFileApp.Run();
  153.     return MDIFileApp.Status;
  154. }
  155.