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

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