home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / borllexp / blx_1_3 / mdiedit / editfram.cpp next >
C/C++ Source or Header  |  1991-10-14  |  5KB  |  201 lines

  1. // editfram.cpp
  2.  
  3. #include <owl.h>
  4. #include <filewnd.h>
  5. #include <alloc.h>
  6. #include <dos.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "editfram.h"
  10. #include "mfilewnd.h"
  11.  
  12. static char DskFile[MAXPATH];
  13.  
  14. char DefaultMenuName[] = "InitMenu";
  15. char MainMenuName[] = "MainMenu";
  16.  
  17. TMDIEditFrame::TMDIEditFrame(LPSTR ATitle, LPSTR AMenu, PTModule AModule)
  18.     : TMDIFrame(ATitle, AMenu, AModule),
  19.     WindowMenuPosition(DEFAULTWINDOWMENUPOS) {
  20.     MDISetMenu(DefaultMenuName, INITMENUPOS);
  21.  
  22.     GetModuleFileName(GetApplication()->hInstance, DskFile, MAXPATH);
  23.     char *index = strrchr(DskFile, '\\');
  24.     *(index + 1) = '\0';
  25.     strcat(DskFile, "MDIEDIT.DTP");
  26.     }
  27.  
  28. void TMDIEditFrame::SetupWindow(void) {
  29.     TMDIFrame::SetupWindow();
  30.  
  31.     ffblk findfirstblock;
  32.     if(!findfirst(DskFile, &findfirstblock, FA_NORMAL))
  33.         SendMessage(HWindow, WM_COMMAND, CM_RESTORESTATE, 0L);
  34.     }
  35.  
  36. void TMDIEditFrame::WMClose(RTMessage Msg) {
  37.     int response = MessageBox(HWindow,
  38.         "Do you wish to save the desktop?",
  39.         "MDI Edit",
  40.         MB_YESNOCANCEL | MB_ICONINFORMATION);
  41.     if(response == IDYES)
  42.         SendMessage(HWindow, WM_COMMAND, CM_SAVESTATE, 0L);
  43.     else if(response == IDCANCEL)
  44.         return;
  45.     TMDIFrame::WMClose(Msg);
  46.     }
  47.  
  48. void TMDIEditFrame::OpenFile(RTMessage) {
  49.     char FileName[MAXPATH];
  50.  
  51.     if(GetApplication()->ExecDialog(
  52.         new TFileDialog(this,
  53.             SD_FILEOPEN,
  54.             strcpy(FileName, "*.*")))
  55.             == IDOK) {
  56.         GetApplication()->MakeWindow(
  57.             new TMyFileWindow(this, "", FileName, MainMenuName,
  58.         MAINMENUPOS));
  59.         }
  60.     }
  61.  
  62. void TMDIEditFrame::NewFile(RTMessage) {
  63.     GetApplication()->MakeWindow(new TMyFileWindow(this, "", "",
  64.         MainMenuName, MAINMENUPOS));
  65.     }
  66.  
  67. static void SaveChild(void *AChild, void *) {
  68.     TMyFileWindow *pWindow = (TMyFileWindow *) AChild;
  69.  
  70.     if(pWindow->IsFlagSet(WB_MDICHILD))
  71.         SendMessage(pWindow->HWindow, WM_COMMAND, CM_FILESAVE, 0L);
  72.     }
  73.  
  74. void TMDIEditFrame::CMSaveAll(RTMessage) {
  75.     ForEach(SaveChild, NULL);
  76.     }
  77.  
  78. void TMDIEditFrame::CMDOSShell(RTMessage) {
  79.     if(WinExec("COMMAND.COM", SW_SHOWNORMAL) <= 32)
  80.         MessageBox(HWindow, "DOS Shell could not be created.",
  81.             "Error:", MB_OK);
  82.     }
  83.  
  84. void TMDIEditFrame::CMExit(RTMessage Msg) {
  85.         TMDIEditFrame::WMClose(Msg);
  86.     }
  87.  
  88. void TMDIEditFrame::CMShowClipboard(RTMessage) {
  89.     if(WinExec("CLIPBRD.EXE", SW_SHOWNORMAL) <= 32)
  90.         MessageBox(HWindow,
  91.         "Cannot find CLIPBRD.EXE to open the Windows Clipboard.",
  92.             "Error:", MB_OK);
  93.     }
  94.  
  95. BOOL TMDIEditFrame::CloseChildren(void) {
  96.     int ReturnValue = TMDIFrame::CloseChildren();
  97.     if( (ReturnValue) && (!GetLastChild()) ) {
  98.         MDISetMenu(0,DEFAULTWINDOWMENUPOS);    // Set the menu to the default.
  99.         }
  100.     return ReturnValue;
  101.     }
  102.  
  103. void TMDIEditFrame::SaveState(RTMessage) {
  104.     ofpstream os(DskFile);
  105.  
  106.     PutChildren(os);
  107.  
  108.     os.close();
  109.     if(os.bad())    {
  110.         unlink(DskFile);
  111.         MessageBox(HWindow, "Unable to write desktop file.",
  112.             "Disk error",
  113.             MB_OK | MB_ICONEXCLAMATION);
  114.         }
  115.     }
  116.  
  117. static void _FAR DoCreateChild(Pvoid P, Pvoid) {
  118.     PTWindowsObject p = (PTWindowsObject) P;
  119.     if(p->IsFlagSet(WB_AUTOCREATE))
  120.         p->GetApplication()->MakeWindow(p);
  121.     }
  122.  
  123. void TMDIEditFrame::RestoreState(RTMessage) {
  124.     LPSTR ErrorMsg = NULL;
  125.  
  126.     ifpstream is(DskFile);
  127.     if(is.bad())
  128.         ErrorMsg = _fstrdup("Unable to open desktop file.");
  129.     else     {
  130.         if ( CloseChildren() ) {
  131.             GetChildren(is);
  132.  
  133.             is.close();
  134.             if(is.bad())
  135.                 ErrorMsg = _fstrdup("Error reading desktop file.");
  136.             else if(GetApplication()->LowMemory()) {
  137.                 CloseChildren();
  138.                 ErrorMsg = _fstrdup("Not enough memory to open file.");
  139.                 }
  140.             else         {
  141.                 ForEach(DoCreateChild, NULL);
  142.                 MDISetMenu(Attr.Menu,WindowMenuPosition);
  143.                 }
  144.             }
  145.         }
  146.     if(ErrorMsg)
  147.         MessageBox(HWindow, ErrorMsg, "Disk error",
  148.             MB_OK | MB_ICONEXCLAMATION);
  149.     }
  150.  
  151. void TMDIEditFrame::CMAbout(RTMessage) {
  152.     GetApplication()->ExecDialog(new TDialog(this, "About"));
  153.     }
  154.  
  155. BOOL TMDIEditFrame::AssignMenu(LPSTR MenuName) {
  156.     BOOL ReturnVal = TRUE;
  157.     HMENU NewHMenu;
  158.     HMENU OldHMenu;
  159.     HMENU PopupSubMenu;
  160.     LPSTR TempMenuName = NULL;  // handle case where (MenuName == Attr.Menu)
  161.  
  162.     if ( HIWORD(Attr.Menu) )
  163.         TempMenuName = Attr.Menu;
  164.  
  165.     if ( WindowMenuPosition == DEFAULTWINDOWMENUPOS ) {
  166.         // Use default menu
  167.         Attr.Menu = _fstrdup(DefaultMenuName);
  168.         WindowMenuPosition = INITMENUPOS;
  169.     }
  170.     else if (HIWORD(MenuName))   // Not NULL and not an int in disguise
  171.         Attr.Menu = _fstrdup(MenuName);
  172.     else
  173.         Attr.Menu = MenuName;
  174.  
  175.     if (TempMenuName)
  176.         farfree(TempMenuName);
  177.  
  178.     if (HWindow)
  179.     {
  180.         NewHMenu = LoadMenu(GetModule()->hInstance, Attr.Menu);
  181.         PopupSubMenu = GetSubMenu(NewHMenu, WindowMenuPosition);
  182.         // SendMessage returns with handle of frame window menu
  183.         // that was replaced.
  184.         OldHMenu = (HMENU)
  185.             SendMessage(ClientWnd->HWindow, WM_MDISETMENU, 0,
  186.                 MAKELONG(NewHMenu, PopupSubMenu));
  187.         ReturnVal = (BOOL) OldHMenu;
  188.         DrawMenuBar(HWindow);
  189.         if ( OldHMenu )
  190.             DestroyMenu(OldHMenu);
  191.     }
  192.     return ReturnVal;
  193.     }
  194.  
  195.  
  196. void TMDIEditFrame::MDISetMenu(LPSTR MenuName, int WindowMenuPos) {
  197.     WindowMenuPosition = WindowMenuPos;
  198.     AssignMenu(MenuName);
  199.     }
  200.  
  201.