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

  1. //----------------------------------------------------------------------------
  2. //  Project ApxSdiDv
  3. //  Borland International
  4. //  Copyright ⌐ 1996. All Rights Reserved.
  5. //
  6. //  SUBSYSTEM:    ApxSdiDv Application
  7. //  FILE:         apxsddva.cpp
  8. //  AUTHOR:
  9. //
  10. //  OVERVIEW
  11. //  ~~~~~~~~
  12. //  Source file for implementation of TApxSdiDvApp (TApplication).
  13. //
  14. //----------------------------------------------------------------------------
  15.  
  16. #include <owl/pch.h>
  17.  
  18. #include <owl/buttonga.h>
  19. #include <owl/docmanag.h>
  20. #include <owl/statusba.h>
  21. #include <owl/filedoc.h>
  22.  
  23. #include <classlib/cmdline.h>
  24. #include <classlib/filename.h>
  25.  
  26. #include "apxsddva.h"
  27. #include "apxsddev.h"                        // Definition of client class.
  28. #include "apxsddad.h"                        // Definition of about dialog.
  29.  
  30.  
  31. //{{TApxSdiDvApp Implementation}}
  32.  
  33.  
  34.  
  35. //{{DOC_VIEW}}
  36. DEFINE_DOC_TEMPLATE_CLASS(TFileDocument, TApxSdiDvEditView, DocType1);
  37. //{{DOC_VIEW_END}}
  38.  
  39. //{{DOC_MANAGER}}
  40. DocType1 __dvt1("All Files", "*.*", 0, "txt", dtAutoDelete | dtUpdateDir | dtOverwritePrompt);
  41. //{{DOC_MANAGER_END}}
  42.  
  43.  
  44. //
  45. // Build a response table for all messages/commands handled by the application.
  46. //
  47. DEFINE_RESPONSE_TABLE2(TApxSdiDvApp, TRecentFiles, TApplication)
  48. //{{TApxSdiDvAppRSP_TBL_BEGIN}}
  49.   EV_OWLVIEW(dnCreate, EvNewView),
  50.   EV_OWLVIEW(dnClose,  EvCloseView),
  51.   EV_COMMAND(CM_FILESEND, CmFileSend),
  52.   EV_COMMAND_ENABLE(CM_FILESEND, CeFileSend),
  53.   EV_COMMAND(CM_HELPABOUT, CmHelpAbout),
  54.   EV_COMMAND(CM_FILEPRINT, CmFilePrint),
  55.   EV_COMMAND(CM_FILEPRINTERSETUP, CmFilePrintSetup),
  56.   EV_COMMAND(CM_FILEPRINTPREVIEW, CmFilePrintPreview),
  57.   EV_COMMAND_ENABLE(CM_FILEPRINT, CmPrintEnable),
  58.   EV_COMMAND_ENABLE(CM_FILEPRINTERSETUP, CmPrintEnable),
  59.   EV_COMMAND_ENABLE(CM_FILEPRINTPREVIEW, CmPrintEnable),
  60.   EV_WM_DROPFILES,
  61.   EV_WM_WININICHANGE,
  62.   EV_OWLDOCUMENT(dnCreate, EvOwlDocument),
  63.   EV_OWLDOCUMENT(dnRename, EvOwlDocument),
  64.   EV_REGISTERED(MruFileMessage, CmFileSelected),
  65. //{{TApxSdiDvAppRSP_TBL_END}}
  66. END_RESPONSE_TABLE;
  67.  
  68.  
  69. //--------------------------------------------------------
  70. // TApxSdiDvApp
  71. // ~~~~~
  72. //
  73. TApxSdiDvApp::TApxSdiDvApp() : TApplication("AppExpert SDI DocView Example"), TRecentFiles(".\\ApxSdiDv.ini", 4)
  74. {
  75.   Printer = 0;
  76.   Printing = 0;
  77.  
  78.   SetDocManager(new TDocManager(dmSDI, this));
  79.  
  80.   ApxMail = new TMailer();
  81.  
  82.   // INSERT>> Your constructor code here.
  83. }
  84.  
  85.  
  86. TApxSdiDvApp::~TApxSdiDvApp()
  87. {
  88.   delete Printer;
  89.  
  90.   delete ApxMail;
  91.  
  92.   // INSERT>> Your destructor code here.
  93. }
  94.  
  95.  
  96. void TApxSdiDvApp::CreateGadgets(TDockableControlBar* cb, bool server)
  97. {
  98.   if (!server) {
  99.     cb->Insert(*new TButtonGadget(CM_FILENEW, CM_FILENEW));
  100.     cb->Insert(*new TButtonGadget(CM_FILEOPEN, CM_FILEOPEN));
  101.     cb->Insert(*new TButtonGadget(CM_FILESAVE, CM_FILESAVE));
  102.     cb->Insert(*new TSeparatorGadget(6));
  103.   }
  104.  
  105.   cb->Insert(*new TButtonGadget(CM_EDITCUT, CM_EDITCUT));
  106.   cb->Insert(*new TButtonGadget(CM_EDITCOPY, CM_EDITCOPY));
  107.   cb->Insert(*new TButtonGadget(CM_EDITPASTE, CM_EDITPASTE));
  108.   cb->Insert(*new TSeparatorGadget(6));
  109.   cb->Insert(*new TButtonGadget(CM_EDITUNDO, CM_EDITUNDO));
  110.   cb->Insert(*new TSeparatorGadget(6));
  111.   cb->Insert(*new TButtonGadget(CM_EDITFIND, CM_EDITFIND));
  112.   cb->Insert(*new TButtonGadget(CM_EDITFINDNEXT, CM_EDITFINDNEXT));
  113.  
  114.   if (!server) {
  115.     cb->Insert(*new TSeparatorGadget(6));
  116.     cb->Insert(*new TButtonGadget(CM_FILEPRINT, CM_FILEPRINT));
  117.     cb->Insert(*new TButtonGadget(CM_FILEPRINTPREVIEW, CM_FILEPRINTPREVIEW));
  118.   }
  119.  
  120.   // Add caption and fly-over help hints.
  121.   //
  122.   cb->SetCaption("Toolbar");
  123.   cb->SetHintMode(TGadgetWindow::EnterHints);
  124. }
  125.  
  126.  
  127. void TApxSdiDvApp::SetupSpeedBar(TDecoratedFrame* frame)
  128. {
  129.   ApxHarbor = new THarbor(*frame);
  130.  
  131.   // Create default toolbar New and associate toolbar buttons with commands.
  132.   //
  133.   TDockableControlBar* cb = new TDockableControlBar(frame);
  134.   CreateGadgets(cb);
  135.  
  136.   // Setup the toolbar ID used by OLE 2 for toolbar negotiation.
  137.   //
  138.   cb->Attr.Id = IDW_TOOLBAR;
  139.  
  140.   ApxHarbor->Insert(*cb, alTop);
  141. }
  142.  
  143.  
  144. //--------------------------------------------------------
  145. // TApxSdiDvApp
  146. // ~~~~~
  147. // Application main window construction & intialization.
  148. //
  149. void TApxSdiDvApp::InitMainWindow()
  150. {
  151.   if (nCmdShow != SW_HIDE)
  152.     nCmdShow = (nCmdShow != SW_SHOWMINNOACTIVE) ? SW_SHOWNORMAL : nCmdShow;
  153.  
  154.   TSDIDecFrame* frame = new TSDIDecFrame(0, GetName(), 0, true, this);
  155.  
  156.   // Enable acceptance of dropped files
  157.   //
  158.   frame->Attr.ExStyle |= WS_EX_ACCEPTFILES;
  159.  
  160.   // Assign icons for this application.
  161.   //
  162.   frame->SetIcon(this, IDI_SDIAPPLICATION);
  163.   frame->SetIconSm(this, IDI_SDIAPPLICATION);
  164.  
  165.   // Menu associated with window and accelerator table associated with table.
  166.   //
  167.   frame->AssignMenu(IDM_SDI);
  168.  
  169.   // Associate with the accelerator table.
  170.   //
  171.   frame->Attr.AccelTable = IDM_SDI;
  172.  
  173.   TStatusBar* sb = new TStatusBar(frame, TGadget::Recessed,
  174.                                   TStatusBar::CapsLock        |
  175.                                   TStatusBar::NumLock         |
  176.                                   TStatusBar::ScrollLock);
  177.   frame->Insert(*sb, TDecoratedFrame::Bottom);
  178.  
  179.   SetupSpeedBar(frame);
  180.  
  181.   SetMainWindow(frame);
  182.  
  183.   frame->SetMenuDescr(TMenuDescr(IDM_SDI));
  184.  
  185. }
  186.  
  187.  
  188. //--------------------------------------------------------
  189. // TApxSdiDvApp
  190. // ~~~~~
  191. // Application instance initialization.
  192. //
  193. void TApxSdiDvApp::InitInstance()
  194. {
  195.   TApplication::InitInstance();
  196.   GetDocManager()->CreateAnyDoc(0, dtNewDoc);
  197.   ProcessCmdLine(lpCmdLine);
  198. }
  199.  
  200.  
  201. //--------------------------------------------------------
  202. // TApxSdiDvApp
  203. // ~~~~~
  204. // Process command line parameters.
  205. //
  206. void TApxSdiDvApp::ProcessCmdLine(char * CmdLine)
  207. {
  208.   TCmdLine cmd(CmdLine);
  209.  
  210.   while (cmd.Kind != TCmdLine::Done) {
  211.     if (cmd.Kind == TCmdLine::Option) {
  212.       if (strnicmp(cmd.Token, "unregister", cmd.TokenLen) == 0) {
  213.         UnRegisterInfo();
  214.         return;
  215.       }
  216.     }
  217.     cmd.NextToken();
  218.   }
  219.  
  220.   RegisterInfo();
  221. }
  222.  
  223.  
  224. //--------------------------------------------------------
  225. // TApxSdiDvApp
  226. // ~~~~~
  227. // Register application info.
  228. //
  229. void TApxSdiDvApp::RegisterInfo()
  230. {
  231.   TAPointer<char> buffer = new char[_MAX_PATH];
  232.  
  233.   GetModuleFileName(buffer, _MAX_PATH);
  234.  
  235.   TRegKey(TRegKey::ClassesRoot, "ApxSdiDv.Application\\DefaultIcon").SetDefValue(0, REG_SZ, buffer, strlen(buffer));
  236.   strcat(buffer, ",1");
  237.   TRegKey(TRegKey::ClassesRoot, "ApxSdiDv.Document.1\\DefaultIcon").SetDefValue(0, REG_SZ, buffer, strlen(buffer));
  238.   strcpy(buffer, "ApxSdiDv.Document.1");
  239.   TRegKey(TRegKey::ClassesRoot, ".txt").SetDefValue(0, REG_SZ, buffer, strlen(buffer));
  240. }
  241.  
  242.  
  243. //--------------------------------------------------------
  244. // TApxSdiDvApp
  245. // ~~~~~
  246. // Unregister application info.
  247. //
  248. void TApxSdiDvApp::UnRegisterInfo()
  249. {
  250.   TAPointer<char> buffer = new char[_MAX_PATH];
  251.  
  252.   GetModuleFileName(buffer, _MAX_PATH);
  253.  
  254.   TRegKey(TRegKey::ClassesRoot, "ApxSdiDv.Application").DeleteKey("DefaultIcon");
  255.   TRegKey(TRegKey::ClassesRoot, "ApxSdiDv.Document.1").DeleteKey("DefaultIcon");
  256.  
  257.   TRegKey::ClassesRoot.DeleteKey("ApxSdiDv.Application");
  258.   TRegKey::ClassesRoot.DeleteKey("ApxSdiDv.Document.1");
  259.   TRegKey::ClassesRoot.DeleteKey(".txt");
  260. }
  261.  
  262.  
  263.  
  264. //--------------------------------------------------------
  265. // TApxSdiDvApp
  266. // ~~~~~
  267. // Response Table handlers:
  268. //
  269. void TApxSdiDvApp::EvNewView(TView& view)
  270. {
  271.   GetMainWindow()->SetClientWindow(view.GetWindow());
  272.   if (!view.IsOK())
  273.     GetMainWindow()->SetClientWindow(0);
  274.   else if (view.GetViewMenu())
  275.     GetMainWindow()->MergeMenu(*view.GetViewMenu());
  276. }
  277.  
  278.  
  279. void TApxSdiDvApp::EvCloseView(TView&)
  280. {
  281.   GetMainWindow()->SetClientWindow(0);
  282.   GetMainWindow()->SetCaption("AppExpert SDI DocView Example");
  283. }
  284.  
  285.  
  286. //{{TSDIDecFrame Implementation}}
  287.  
  288.  
  289. TSDIDecFrame::TSDIDecFrame(TWindow* parent, const char far* title, TWindow* clientWnd, bool trackMenuSelection, TModule* module)
  290. :
  291.   TDecoratedFrame(parent, title, clientWnd, trackMenuSelection, module)
  292. {
  293.   // INSERT>> Your constructor code here.
  294.  
  295. }
  296.  
  297.  
  298. TSDIDecFrame::~TSDIDecFrame()
  299. {
  300.   // INSERT>> Your destructor code here.
  301.  
  302. }
  303.  
  304.  
  305. //--------------------------------------------------------
  306. // TApxSdiDvApp
  307. // ~~~~~~~~~~
  308. // Menu File Print command
  309. //
  310. void TApxSdiDvApp::CmFilePrint()
  311. {
  312.   // Create Printer object if not already created.
  313.   //
  314.   if (!Printer)
  315.     Printer = new TPrinter(this);
  316.  
  317.   TDocument* currentDoc = GetDocManager()->GetCurrentDoc();
  318.  
  319.   TAPointer<char> docName = new char[_MAX_PATH];
  320.  
  321.   if (currentDoc->GetTitle())
  322.     strcpy(docName, currentDoc->GetTitle());
  323.   else
  324.     strcpy(docName, "Document");
  325.  
  326.   // Create Printout window and set characteristics.
  327.   //
  328.   TApxPrintout printout(Printer, docName, GetMainWindow()->GetClientWindow());
  329.   printout.SetBanding(true);
  330.  
  331.   Printing++;
  332.  
  333.   // Bring up the Print dialog and print the document.
  334.   //
  335.   Printer->Print(GetWindowPtr(GetActiveWindow()), printout, true);
  336.  
  337.   Printing--;
  338. }
  339.  
  340.  
  341. //--------------------------------------------------------
  342. // TApxSdiDvApp
  343. // ~~~~~~~~~~
  344. // Menu File Print Setup command
  345. //
  346. void TApxSdiDvApp::CmFilePrintSetup()
  347. {
  348.   if (!Printer)
  349.     Printer = new TPrinter(this);
  350.  
  351.   // Bring up the Print Setup dialog.
  352.   //
  353.   Printer->Setup(GetMainWindow());
  354. }
  355.  
  356.  
  357. //--------------------------------------------------------
  358. // TApxSdiDvApp
  359. // ~~~~~~~~~~
  360. // Menu File Print Preview command
  361. //
  362. void TApxSdiDvApp::CmFilePrintPreview()
  363. {
  364.   TSDIDecFrame* sdiFrame = TYPESAFE_DOWNCAST(GetMainWindow(), TSDIDecFrame);
  365.   if (sdiFrame) {
  366.     if (!Printer)
  367.       Printer = new TPrinter(this);
  368.  
  369.     Printing++;
  370.  
  371.     TApxPreviewWin* prevW = new TApxPreviewWin(sdiFrame, Printer, sdiFrame->GetClientWindow(), "Print Preview", new TLayoutWindow(0));
  372.     prevW->Create();
  373.  
  374.     // Here we resize the preview window to take the size of the MainWindow, then
  375.     // hide the MainWindow.
  376.     //
  377.     TFrameWindow * mainWindow =  GetMainWindow();
  378.     TRect r = mainWindow->GetWindowRect();
  379.     prevW->MoveWindow(r);
  380.     prevW->ShowWindow(SW_SHOWNORMAL);
  381.     mainWindow->ShowWindow(SW_HIDE);
  382.  
  383.     BeginModal(GetMainWindow());
  384.  
  385.     // After the user closes the preview Window, we take its size and use it
  386.     // to size the MainWindow, then show the MainWindow again.
  387.     //
  388.     r = prevW->GetWindowRect();
  389.     mainWindow->MoveWindow(r);
  390.     mainWindow->ShowWindow(SW_SHOWNORMAL);
  391.  
  392.     Printing--;
  393.  
  394.     // Now that printing is off we can invalidate because the edit window to repaint.
  395.     //
  396.     GetMainWindow()->SetRedraw(true);
  397.     GetMainWindow()->Invalidate();
  398.  
  399.     // We must destroy the preview window explicitly.  Otherwise, the window will
  400.     // not be destroyed until it's parent the MainWindow is destroyed.
  401.     //
  402.     prevW->Destroy();
  403.     delete prevW;
  404.   }
  405. }
  406.  
  407.  
  408. //--------------------------------------------------------
  409. // TApxSdiDvApp
  410. // ~~~~~~~~~~
  411. // Menu enabler used by Print, Print Setup and Print Preview.
  412. //
  413. void TApxSdiDvApp::CmPrintEnable(TCommandEnabler& tce)
  414. {
  415.   // If we have a Printer already created just test if all is okay.
  416.   // Otherwise create a Printer object and make sure the printer really exists
  417.   // and then delete the Printer object.
  418.   //
  419.   if (!Printer) {
  420.     Printer = new TPrinter(this);
  421.       tce.Enable(!Printer->GetSetup().Error);
  422.   }
  423.   else
  424.     tce.Enable(!Printer->GetSetup().Error);
  425. }
  426.  
  427.  
  428. void TApxSdiDvApp::CeFileSend(TCommandEnabler& ce)
  429. {
  430.   ce.Enable((GetDocManager()->GetCurrentDoc() != 0)
  431.             && ApxMail->IsMAPIAvailable());
  432. }
  433.  
  434.  
  435. void TApxSdiDvApp::CmFileSend ()
  436. {
  437.   // Check to see if a document exists
  438.   //
  439.   TDocument* currentDoc = GetDocManager()->GetCurrentDoc();
  440.  
  441.   if (currentDoc) {
  442.     TAPointer<char> savedPath = new char[_MAX_PATH];
  443.     TAPointer<char> docName = new char[_MAX_PATH];
  444.  
  445.     bool dirtyState = currentDoc->IsDirty();
  446.  
  447.     if (currentDoc->GetDocPath())
  448.       strcpy(savedPath, currentDoc->GetDocPath());
  449.     else
  450.       strcpy(savedPath, "");
  451.  
  452.     if (currentDoc->GetTitle())
  453.       strcpy(docName, currentDoc->GetTitle());
  454.     else
  455.       strcpy(docName, "Document");
  456.  
  457.     TFileName tempFile(TFileName::TempFile);
  458.  
  459.     currentDoc->SetDocPath(tempFile.Canonical().c_str());
  460.     currentDoc->Commit(true);
  461.  
  462.     currentDoc->SetDocPath(savedPath);
  463.     currentDoc->SetDirty(dirtyState);
  464.  
  465.     ApxMail->SendDocuments(GetMainWindow(), tempFile.Canonical().c_str(), docName, false);
  466.  
  467.     tempFile.Remove();
  468.   }
  469. }
  470.  
  471.  
  472. //--------------------------------------------------------
  473. // TApxSdiDvApp
  474. // ~~~~~~~~~~~
  475. //
  476. // Menu Help About ApxSdiDv command
  477. void TApxSdiDvApp::CmHelpAbout()
  478. {
  479.   // Show the modal dialog.
  480.   //
  481.   TApxSdiDvAboutDlg(GetMainWindow()).Execute();
  482. }
  483.  
  484.  
  485. void TApxSdiDvApp::EvDropFiles(TDropInfo drop)
  486. {
  487.   // Tell DragQueryFile the file interested in (0) and the length of your buffer.
  488.   //
  489.   int    nameLength = drop.DragQueryFileNameLen(0) + 1;
  490.   char*  fileName = new char[nameLength];
  491.  
  492.   drop.DragQueryFile(0, fileName, nameLength);
  493.  
  494.   // Open the file that was dropped.
  495.   //
  496.   AddFile(fileName);
  497.  
  498.   // Release the memory allocated for this handle with DragFinish.
  499.   //
  500.   drop.DragFinish();
  501.   delete[] fileName;
  502. }
  503.  
  504.  
  505. void TApxSdiDvApp::AddFile(const char* FileName)
  506. {
  507.   TDocTemplate* tpl = GetDocManager()->MatchTemplate(FileName);
  508.   if (tpl)
  509.     GetDocManager()->CreateDoc(tpl, FileName);
  510. }
  511.  
  512.  
  513. void TApxSdiDvApp::EvOwlDocument(TDocument& doc)
  514. {
  515.   if (doc.GetDocPath())
  516.     SaveMenuChoice(doc.GetDocPath());
  517. }
  518.  
  519.  
  520. int32 TApxSdiDvApp::CmFileSelected(uint wp, int32)
  521. {
  522.   TAPointer<char> text = new char[_MAX_PATH];
  523.  
  524.   GetMenuText(wp, text, _MAX_PATH);
  525.   TDocTemplate* tpl = GetDocManager()->MatchTemplate(text);
  526.   if (tpl)
  527.     GetDocManager()->CreateDoc(tpl, text);
  528.   return 0;
  529. }
  530.  
  531. void TApxSdiDvApp::EvWinIniChange(char far* section)
  532. {
  533.   if (strcmp(section, "windows") == 0) {
  534.     // If the device changed in the WIN.INI file then the printer
  535.     // might have changed.  If we have a TPrinter(Printer) then
  536.     // check and make sure it's identical to the current device
  537.     // entry in WIN.INI.
  538.     //
  539.     if (Printer) {
  540.       const int bufferSize = 255;
  541.       char printDBuffer[bufferSize];
  542.       LPSTR printDevice = printDBuffer;
  543.       LPSTR devName;
  544.       LPSTR driverName = 0;
  545.       LPSTR outputName = 0;
  546.       if (::GetProfileString("windows", "device", "", printDevice, bufferSize)) {
  547.         // The string which should come back is something like:
  548.         //
  549.         //      HP LaserJet III,hppcl5a,LPT1:
  550.         //
  551.         // Where the format is:
  552.         //
  553.         //      devName,driverName,outputName
  554.         //
  555.         devName = printDevice;
  556.         while (*printDevice) {
  557.           if (*printDevice == ',') {
  558.             *printDevice++ = 0;
  559.             if (!driverName)
  560.               driverName = printDevice;
  561.             else
  562.               outputName = printDevice;
  563.           }
  564.           else
  565.             printDevice = ::AnsiNext(printDevice);
  566.         }
  567.  
  568.         if (Printer->GetSetup().Error != 0                ||
  569.             strcmp(devName, Printer->GetSetup().GetDeviceName()) != 0  ||
  570.             strcmp(driverName, Printer->GetSetup().GetDriverName()) != 0 ||
  571.             strcmp(outputName, Printer->GetSetup().GetOutputName()) != 0 ) {
  572.           // New printer installed so get the new printer device now.
  573.           //
  574.           delete Printer;
  575.           Printer = new TPrinter(this);
  576.         }
  577.       }
  578.       else {
  579.         // No printer installed(GetProfileString failed).
  580.         //
  581.         delete Printer;
  582.         Printer = new TPrinter(this);
  583.       }
  584.     }
  585.   }
  586. }
  587.  
  588.  
  589. int OwlMain(int , char* [])
  590. {
  591.   TApxSdiDvApp   app;
  592.   return app.Run();
  593. }
  594.