home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / APXMDI.PAK / APXMMDIC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  8.6 KB  |  324 lines

  1. //----------------------------------------------------------------------------
  2. //  Project ApxMdi
  3. //  Borland International
  4. //  Copyright ⌐ 1996. All Rights Reserved.
  5. //
  6. //  SUBSYSTEM:    ApxMdi Application
  7. //  FILE:         apxmmdic.cpp
  8. //  AUTHOR:       
  9. //
  10. //  OVERVIEW
  11. //  ~~~~~~~~
  12. //  Source file for implementation of TApxMdiMDIClient (TMDIClient).
  13. //
  14. //----------------------------------------------------------------------------
  15.  
  16. #include <owl/pch.h>
  17.  
  18. #include <owl/listbox.h>
  19. #include <stdio.h>
  20.  
  21. #include "apxmdapp.h"
  22. #include "apxmdedf.h"
  23. #include "apxmmdi1.h"
  24. #include "apxmmdic.h"
  25. #include "apxprint.h"
  26. #include "apxprev.h"
  27.  
  28.  
  29. //{{TApxMdiMDIClient Implementation}}
  30.  
  31.  
  32. //
  33. // Build a response table for all messages/commands handled
  34. // by TApxMdiMDIClient derived from TMDIClient.
  35. //
  36. DEFINE_RESPONSE_TABLE1(TApxMdiMDIClient, TMDIClient)
  37. //{{TApxMdiMDIClientRSP_TBL_BEGIN}}
  38.   EV_COMMAND(CM_MDIFILENEW, CmFileNew),
  39.   EV_COMMAND(CM_MDIFILEOPEN, CmFileOpen),
  40.   EV_COMMAND(CM_FILECLOSE, CmFileClose),
  41.   EV_COMMAND(CM_FILEPRINT, CmFilePrint),
  42.   EV_COMMAND(CM_FILEPRINTERSETUP, CmFilePrintSetup),
  43.   EV_COMMAND(CM_FILEPRINTPREVIEW, CmFilePrintPreview),
  44.   EV_COMMAND_ENABLE(CM_FILEPRINT, CmPrintEnable),
  45.   EV_COMMAND_ENABLE(CM_FILEPRINTERSETUP, CmPrintEnable),
  46.   EV_COMMAND_ENABLE(CM_FILEPRINTPREVIEW, CmPrintEnable),
  47.   EV_WM_DROPFILES,
  48. //{{TApxMdiMDIClientRSP_TBL_END}}
  49. END_RESPONSE_TABLE;
  50.  
  51.  
  52. //--------------------------------------------------------
  53. // TApxMdiMDIClient
  54. // ~~~~~~~~~~~
  55. // Construction/Destruction handling.
  56. //
  57. TApxMdiMDIClient::TApxMdiMDIClient(TModule* module)
  58. :
  59.   TMDIClient(module)
  60. {
  61.   ChildCount = 0;
  62.  
  63.   // INSERT>> Your constructor code here.
  64.  
  65. }
  66.  
  67.  
  68. TApxMdiMDIClient::~TApxMdiMDIClient()
  69. {
  70.   Destroy();
  71.  
  72.   // INSERT>> Your destructor code here.
  73.  
  74. }
  75.  
  76.  
  77. //--------------------------------------------------------
  78. // TApxMdiMDIClient
  79. // ~~~~~~~~~~~
  80. // MDIClient site initialization.
  81. //
  82. void TApxMdiMDIClient::SetupWindow()
  83. {
  84.   // Default SetUpWindow processing.
  85.   //
  86.   TMDIClient::SetupWindow();
  87.  
  88.   TApxMdiApp* theApp = TYPESAFE_DOWNCAST(GetApplication(), TApxMdiApp);
  89.  
  90.   // Common file file flags and filters for Open/Save As dialogs.  Filename and directory are
  91.   // computed in the member functions CmFileOpen, and CmFileSaveAs.
  92.   //
  93.   theApp->FileData.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
  94.   theApp->FileData.SetFilter("All Files (*.*)|*.*|");
  95.   theApp->FileData.DefExt = "txt";
  96.  
  97.   // Accept files via drag/drop in the client window.
  98.   //
  99.   DragAcceptFiles(true);
  100. }
  101.  
  102.  
  103. //--------------------------------------------------------
  104. // TApxMdiMDIClient
  105. // ~~~~~~~~~~~
  106. // Menu File New command
  107. //
  108. void TApxMdiMDIClient::CmFileNew()
  109. {
  110.   TAPointer<char> title = new char[255];
  111.  
  112.   // Generate a title for the MDI child window.
  113.   //
  114.   sprintf(title, "%d", ++ChildCount);
  115.  
  116.   TApxMdiMDIChild* child = new TApxMdiMDIChild(*this, title, 0);
  117.  
  118.   // Assign icons for this child window.
  119.   //
  120.   child->SetIcon(GetApplication(), IDI_DOC);
  121.   child->SetIconSm(GetApplication(), IDI_DOC);
  122.  
  123.   // If the current active MDI child is maximize then this one should be also.
  124.   //
  125.   TApxMdiMDIChild* curChild = (TApxMdiMDIChild *)GetActiveMDIChild();
  126.   if (curChild && (curChild->GetWindowLong(GWL_STYLE) & WS_MAXIMIZE))
  127.     child->Attr.Style |= WS_MAXIMIZE;
  128.  
  129.   child->Create();
  130. }
  131.  
  132.  
  133. void TApxMdiMDIClient::OpenFile(const char* fileName)
  134. {
  135.   TApxMdiApp* theApp = TYPESAFE_DOWNCAST(GetApplication(), TApxMdiApp);
  136.  
  137.   if (fileName)
  138.     strcpy(theApp->FileData.FileName, fileName);
  139.  
  140.   // Create a MDIChild window whose client is TApxMdiEditFile.
  141.   //
  142.   TApxMdiMDIChild* child = new TApxMdiMDIChild(*this, "", new TApxMdiEditFile(0, 0, 0, 0, 0, 0, 0, theApp->FileData.FileName));
  143.  
  144.   // Assign icons for this child window.
  145.   //
  146.   child->SetIcon(GetApplication(), IDI_DOC);
  147.   child->SetIconSm(GetApplication(), IDI_DOC);
  148.  
  149.   // If the current active MDI child is maximize then this one should be also.
  150.   //
  151.   TApxMdiMDIChild* curChild = (TApxMdiMDIChild *)GetActiveMDIChild();
  152.   if (curChild && (curChild->GetWindowLong(GWL_STYLE) & WS_MAXIMIZE))
  153.     child->Attr.Style |= WS_MAXIMIZE;
  154.  
  155.   child->Create();
  156.  
  157.  
  158.   // Add the file to the MRU list.
  159.   //
  160.   theApp->SaveMenuChoice(theApp->FileData.FileName);
  161. }
  162.  
  163.  
  164. //--------------------------------------------------------
  165. // TApxMdiMDIClient
  166. // ~~~~~~~~~~~
  167. // Menu File Open command
  168. //
  169. void TApxMdiMDIClient::CmFileOpen()
  170. {
  171.   TApxMdiApp* theApp = TYPESAFE_DOWNCAST(GetApplication(), TApxMdiApp);
  172.  
  173.   // Display standard Open dialog box to select a file name.
  174.   //
  175.   *(theApp->FileData.FileName) = 0;
  176.   if (TFileOpenDialog(this, theApp->FileData).Execute() == IDOK)
  177.     OpenFile();
  178. }
  179.  
  180.  
  181. //--------------------------------------------------------
  182. // TApxMdiMDIClient
  183. // ~~~~~~~~~~~
  184. // Menu File Close command
  185. //
  186. void TApxMdiMDIClient::CmFileClose()
  187. {
  188.   TApxMdiMDIChild* curChild = (TApxMdiMDIChild *)GetActiveMDIChild();
  189.  
  190.   if (curChild) {
  191.     curChild->CloseWindow();
  192.   }
  193. }
  194.  
  195. //--------------------------------------------------------
  196. // TApxMdiMDIClient
  197. // ~~~~~~~~~~
  198. // Menu File Print command
  199. //
  200. void TApxMdiMDIClient::CmFilePrint()
  201. {
  202.   // Create Printer object if not already created.
  203.   //
  204.   TApxMdiApp* theApp = TYPESAFE_DOWNCAST(GetApplication(), TApxMdiApp);
  205.   if (theApp) {
  206.     if (!theApp->Printer)
  207.       theApp->Printer = new TPrinter(theApp);
  208.  
  209.     TAPointer<char> docName = new char[_MAX_PATH];
  210.  
  211.     GetActiveMDIChild()->GetWindowText(docName, _MAX_PATH);
  212.  
  213.     // Create Printout window and set characteristics.
  214.     //
  215.     TApxPrintout printout(theApp->Printer, docName, GetActiveMDIChild()->GetClientWindow(), true);
  216.  
  217.     theApp->Printing++;
  218.  
  219.     // Bring up the Print dialog and print the document.
  220.     //
  221.     theApp->Printer->Print(GetWindowPtr(GetActiveWindow()), printout, true);
  222.  
  223.     theApp->Printing--;
  224.   }
  225. }
  226.  
  227.  
  228. //--------------------------------------------------------
  229. // TApxMdiMDIClient
  230. // ~~~~~~~~~~
  231. // Menu File Print Setup command
  232. //
  233. void TApxMdiMDIClient::CmFilePrintSetup()
  234. {
  235.   TApxMdiApp* theApp = TYPESAFE_DOWNCAST(GetApplication(), TApxMdiApp);
  236.   if (theApp) {
  237.     if (!theApp->Printer)
  238.       theApp->Printer = new TPrinter(theApp);
  239.  
  240.     // Bring up the Print Setup dialog.
  241.     //
  242.     theApp->Printer->Setup(this);
  243.   }
  244. }
  245.  
  246.  
  247. //--------------------------------------------------------
  248. // TApxMdiMDIClient
  249. // ~~~~~~~~~~
  250. // Menu File Print Preview command
  251. //
  252. void TApxMdiMDIClient::CmFilePrintPreview()
  253. {
  254.   TApxMdiApp* theApp = TYPESAFE_DOWNCAST(GetApplication(), TApxMdiApp);
  255.   if (theApp) {
  256.     if (!theApp->Printer)
  257.       theApp->Printer = new TPrinter(GetApplication());
  258.  
  259.     theApp->Printing++;
  260.  
  261.     TApxPreviewWin* prevW = new TApxPreviewWin(Parent, theApp->Printer, GetActiveMDIChild()->GetClientWindow(), "Print Preview", new TLayoutWindow(0));
  262.     prevW->Create();
  263.  
  264.     // Here we resize the preview window to take the size of the MainWindow, then
  265.     // hide the MainWindow.
  266.     //
  267.     TFrameWindow * mainWindow =  GetApplication()->GetMainWindow();
  268.     TRect r = mainWindow->GetWindowRect();
  269.     prevW->MoveWindow(r);
  270.     prevW->ShowWindow(SW_SHOWNORMAL);
  271.     mainWindow->ShowWindow(SW_HIDE);
  272.  
  273.     GetApplication()->BeginModal(GetApplication()->GetMainWindow());
  274.  
  275.     // After the user closes the preview Window, we take its size and use it
  276.     // to size the MainWindow, then show the MainWindow again.
  277.     //
  278.     r = prevW->GetWindowRect();
  279.     mainWindow->MoveWindow(r);
  280.     mainWindow->ShowWindow(SW_SHOWNORMAL);
  281.  
  282.     // We must destroy the preview window explicitly.  Otherwise, the window will
  283.     // not be destroyed until it's parent the MainWindow is destroyed.
  284.     //
  285.     prevW->Destroy();
  286.     delete prevW;
  287.  
  288.     theApp->Printing--;
  289.   }
  290. }
  291.  
  292.  
  293. //--------------------------------------------------------
  294. // TApxMdiMDIClient
  295. // ~~~~~~~~~~
  296. // Menu enabler used by Print, Print Setup and Print Preview.
  297. //
  298. void TApxMdiMDIClient::CmPrintEnable(TCommandEnabler& tce)
  299. {
  300.   if (GetActiveMDIChild()) {
  301.     TApxMdiApp* theApp = TYPESAFE_DOWNCAST(GetApplication(), TApxMdiApp);
  302.     if (theApp) {
  303.       // If we have a Printer already created just test if all is okay.
  304.       // Otherwise create a Printer object and make sure the printer really
  305.       // exists and then delete the Printer object.
  306.       //
  307.       if (!theApp->Printer) {
  308.         theApp->Printer = new TPrinter(theApp);
  309.         tce.Enable(!theApp->Printer->GetSetup().Error);
  310.       }
  311.       else
  312.         tce.Enable(!theApp->Printer->GetSetup().Error);
  313.     }
  314.   }
  315.   else
  316.     tce.Enable(false);
  317. }
  318.  
  319.  
  320. void TApxMdiMDIClient::EvDropFiles(TDropInfo)
  321. {
  322.   Parent->ForwardMessage();
  323. }
  324.