home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / BCPPOWL3.ZIP / MFILEAPP.ZIP / MFILEAPP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  4.3 KB  |  161 lines

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