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

  1. //----------------------------------------------------------------------------
  2. //  Project ApxOle
  3. //  Borland International
  4. //  Copyright ⌐ 1996. All Rights Reserved.
  5. //
  6. //  SUBSYSTEM:    ApxOle Application
  7. //  FILE:         apxolapp.cpp
  8. //  AUTHOR:
  9. //
  10. //  OVERVIEW
  11. //  ~~~~~~~~
  12. //  Source file for implementation of TApxOleApp (TApplication).
  13. //
  14. //----------------------------------------------------------------------------
  15.  
  16. #include <owl/pch.h>
  17.  
  18. #include <owl/buttonga.h>
  19. #include <owl/statusba.h>
  20. #include <owl/docmanag.h>
  21. #include <owl/oledoc.h>
  22.  
  23. #include <classlib/cmdline.h>
  24. #include <classlib/filename.h>
  25.  
  26. #include "apxolapp.h"
  27. #include "apxomdic.h"
  28. #include "apxomdi1.h"
  29. #include "apxololv.h"
  30. #include "apxolabd.h"                        // Definition of about dialog.
  31.  
  32. //{{TApxOleApp Implementation}}
  33.  
  34.  
  35. DEFINE_APP_DICTIONARY(AppDictionary);
  36.  
  37. static TPointer<TOcRegistrar> Registrar;
  38.  
  39. REGISTRATION_FORMAT_BUFFER(200)
  40.  
  41. BEGIN_REGISTRATION(ApplicationReg)
  42.   REGDATA(clsid, "{7D270638-64DB-11CF-87A2-0020AF062334}")
  43.   REGDATA(appname, "ApxOle Server")
  44.   REGDATA(description, "ApxOle Server Application")
  45.   REGDATA(cmdline, "")
  46.   REGDATA(usage, ocrMultipleUse)
  47. END_REGISTRATION
  48.  
  49. //{{DOC_VIEW}}
  50. DEFINE_DOC_TEMPLATE_CLASS(TOleDocument, TApxOleOleView, DocType1);
  51. //{{DOC_VIEW_END}}
  52.  
  53. //{{DOC_MANAGER}}
  54. BEGIN_REGISTRATION(__dvReg1)
  55.   REGDATA(progid, "ApxOle.Document.1")
  56.   REGDATA(description, "ApxOle Document Version 1")
  57.   REGDATA(extension, "olv")
  58.   REGDATA(docfilter, "*.olv")
  59.   REGDOCFLAGS(dtAutoDelete | dtUpdateDir | dtOverwritePrompt | dtAutoOpen | dtRegisterExt)
  60. //  REGDATA(debugger, "TDW")
  61.   REGDATA(menuname, "TApxOleOleView")
  62.   REGDATA(insertable, "")
  63.   REGDATA(verb0, "&Edit")
  64.   REGDATA(verb1, "&Open")
  65.   REGFORMAT(0, ocrEmbedSource,  ocrContent,  ocrIStorage, ocrGet)
  66.   REGFORMAT(1, ocrMetafilePict, ocrContent,  ocrMfPict | ocrStaticMed, ocrGet)
  67.   REGFORMAT(2, ocrBitmap, ocrContent,  ocrGDI | ocrStaticMed, ocrGet)
  68.   REGFORMAT(3, ocrDib, ocrContent,  ocrHGlobal | ocrStaticMed, ocrGet)
  69.   REGFORMAT(4, ocrLinkSource, ocrContent,  ocrIStream, ocrGet)
  70. END_REGISTRATION
  71. DocType1 __dvt1(__dvReg1);
  72. //{{DOC_MANAGER_END}}
  73.  
  74.  
  75. //
  76. // Build a response table for all messages/commands handled by the application.
  77. //
  78. DEFINE_RESPONSE_TABLE2(TApxOleApp, TRecentFiles, TApplication)
  79. //{{TApxOleAppRSP_TBL_BEGIN}}
  80.   EV_OWLVIEW(dnCreate, EvNewView),
  81.   EV_OWLVIEW(dnClose,  EvCloseView),
  82.   EV_COMMAND(CM_FILESEND, CmFileSend),
  83.   EV_COMMAND_ENABLE(CM_FILESEND, CeFileSend),
  84.   EV_COMMAND(CM_HELPABOUT, CmHelpAbout),
  85.   EV_WM_DROPFILES,
  86.   EV_WM_WININICHANGE,
  87.   EV_OWLDOCUMENT(dnCreate, EvOwlDocument),
  88.   EV_OWLDOCUMENT(dnRename, EvOwlDocument),
  89.   EV_REGISTERED(MruFileMessage, CmFileSelected),
  90. //{{TApxOleAppRSP_TBL_END}}
  91. END_RESPONSE_TABLE;
  92.  
  93.  
  94. //--------------------------------------------------------
  95. // TApxOleApp
  96. // ~~~~~
  97. //
  98. TApxOleApp::TApxOleApp() : TApplication(::ApplicationReg["appname"], ::Module, &::AppDictionary), TRecentFiles(".\\ApxOle.ini", 4)
  99. {
  100.   Printer = 0;
  101.   Printing = 0;
  102.  
  103.   SetDocManager(new TDocManager(dmMDI, this));
  104.  
  105.   ApxMail = new TMailer();
  106.  
  107.   // INSERT>> Your constructor code here.
  108. }
  109.  
  110.  
  111. TApxOleApp::~TApxOleApp()
  112. {
  113.   delete Printer;
  114.  
  115.   delete ApxMail;
  116.  
  117.   // INSERT>> Your destructor code here.
  118. }
  119.  
  120.  
  121. void TApxOleApp::CreateGadgets(TControlBar* cb, bool server)
  122. {
  123.   if (!server) {
  124.     cb->Insert(*new TButtonGadget(CM_MDIFILENEW, CM_MDIFILENEW));
  125.     cb->Insert(*new TButtonGadget(CM_MDIFILEOPEN, CM_MDIFILEOPEN));
  126.     cb->Insert(*new TButtonGadget(CM_FILESAVE, CM_FILESAVE));
  127.     cb->Insert(*new TSeparatorGadget(6));
  128.   }
  129.  
  130.   cb->Insert(*new TButtonGadget(CM_EDITCUT, CM_EDITCUT));
  131.   cb->Insert(*new TButtonGadget(CM_EDITCOPY, CM_EDITCOPY));
  132.   cb->Insert(*new TButtonGadget(CM_EDITPASTE, CM_EDITPASTE));
  133.   cb->Insert(*new TSeparatorGadget(6));
  134.   cb->Insert(*new TButtonGadget(CM_EDITUNDO, CM_EDITUNDO));
  135.   cb->Insert(*new TSeparatorGadget(6));
  136.   cb->Insert(*new TButtonGadget(CM_EDITFIND, CM_EDITFIND));
  137.   cb->Insert(*new TButtonGadget(CM_EDITFINDNEXT, CM_EDITFINDNEXT));
  138.  
  139.   if (!server) {
  140.     cb->Insert(*new TSeparatorGadget(6));
  141.     cb->Insert(*new TButtonGadget(CM_FILEPRINT, CM_FILEPRINT));
  142.     cb->Insert(*new TButtonGadget(CM_FILEPRINTPREVIEW, CM_FILEPRINTPREVIEW));
  143.   }
  144.  
  145.   // Add caption and fly-over help hints.
  146.   //
  147.   cb->SetCaption("Toolbar");
  148.   cb->SetHintMode(TGadgetWindow::EnterHints);
  149. }
  150.  
  151.  
  152. void TApxOleApp::SetupSpeedBar(TOleMDIFrame* frame)
  153. {
  154.   // Create default toolbar New and associate toolbar buttons with commands.
  155.   //
  156.   TControlBar* cb = new TControlBar(frame);
  157.   CreateGadgets(cb);
  158.  
  159.   // Setup the toolbar ID used by OLE 2 for toolbar negotiation.
  160.   //
  161.   cb->Attr.Id = IDW_TOOLBAR;
  162.  
  163.   frame->Insert(*cb, TDecoratedFrame::Top);
  164. }
  165.  
  166.  
  167. //--------------------------------------------------------
  168. // TApxOleApp
  169. // ~~~~~
  170. // Application main window construction & intialization.
  171. //
  172. void TApxOleApp::InitMainWindow()
  173. {
  174.   if (nCmdShow != SW_HIDE)
  175.     nCmdShow = (nCmdShow != SW_SHOWMINNOACTIVE) ? SW_SHOWNORMAL : nCmdShow;
  176.  
  177.   MdiClient = new TApxOleMDIClient(this);
  178.   TOleMDIFrame* frame = new TOleMDIFrame(Name, IDM_MDI, *MdiClient, true, this);
  179.  
  180.   // Enable acceptance of dropped files
  181.   //
  182.   frame->Attr.ExStyle |= WS_EX_ACCEPTFILES;
  183.  
  184.   // Assign icons for this application.
  185.   //
  186.   frame->SetIcon(this, IDI_MDIAPPLICATION);
  187.   frame->SetIconSm(this, IDI_MDIAPPLICATION);
  188.  
  189.   // Associate with the accelerator table.
  190.   //
  191.   frame->Attr.AccelTable = IDM_MDI;
  192.  
  193.   TStatusBar* sb = new TStatusBar(frame, TGadget::Recessed,
  194.                                   TStatusBar::CapsLock        |
  195.                                   TStatusBar::NumLock         |
  196.                                   TStatusBar::ScrollLock);
  197.   frame->Insert(*sb, TDecoratedFrame::Bottom);
  198.  
  199.   SetupSpeedBar(frame);
  200.  
  201.   SetMainWindow(frame);
  202.  
  203.   frame->SetMenuDescr(TMenuDescr(IDM_MDI));
  204.  
  205. }
  206.  
  207.  
  208. //--------------------------------------------------------
  209. // TApxOleApp
  210. // ~~~~~
  211. // Application instance initialization.
  212. //
  213. void TApxOleApp::InitInstance()
  214. {
  215.   TApplication::InitInstance();
  216.  
  217.   ProcessCmdLine(lpCmdLine);
  218. }
  219.  
  220.  
  221. //--------------------------------------------------------
  222. // TApxOleApp
  223. // ~~~~~
  224. // Process command line parameters.
  225. //
  226. void TApxOleApp::ProcessCmdLine(char * CmdLine)
  227. {
  228.   TCmdLine cmd(CmdLine);
  229.  
  230.   while (cmd.Kind != TCmdLine::Done) {
  231.     if (cmd.Kind == TCmdLine::Option) {
  232.       if (strnicmp(cmd.Token, "unregister", cmd.TokenLen) == 0) {
  233.         UnRegisterInfo();
  234.         return;
  235.       }
  236.     }
  237.     cmd.NextToken();
  238.   }
  239.  
  240.   RegisterInfo();
  241. }
  242.  
  243.  
  244. //--------------------------------------------------------
  245. // TApxOleApp
  246. // ~~~~~
  247. // Register application info.
  248. //
  249. void TApxOleApp::RegisterInfo()
  250. {
  251.   TAPointer<char> buffer = new char[_MAX_PATH];
  252.  
  253.   GetModuleFileName(buffer, _MAX_PATH);
  254.  
  255.   TRegKey(TRegKey::ClassesRoot, "ApxOle.Application\\DefaultIcon").SetDefValue(0, REG_SZ, buffer, strlen(buffer));
  256.   strcat(buffer, ",1");
  257.   TRegKey(TRegKey::ClassesRoot, "ApxOle.Document.1\\DefaultIcon").SetDefValue(0, REG_SZ, buffer, strlen(buffer));
  258.   strcpy(buffer, "ApxOle.Document.1");
  259.   TRegKey(TRegKey::ClassesRoot, ".olv").SetDefValue(0, REG_SZ, buffer, strlen(buffer));
  260. }
  261.  
  262.  
  263. //--------------------------------------------------------
  264. // TApxOleApp
  265. // ~~~~~
  266. // Unregister application info.
  267. //
  268. void TApxOleApp::UnRegisterInfo()
  269. {
  270.   TAPointer<char> buffer = new char[_MAX_PATH];
  271.  
  272.   GetModuleFileName(buffer, _MAX_PATH);
  273.  
  274.   TRegKey(TRegKey::ClassesRoot, "ApxOle.Application").DeleteKey("DefaultIcon");
  275.   TRegKey(TRegKey::ClassesRoot, "ApxOle.Document.1").DeleteKey("DefaultIcon");
  276.  
  277.   TRegKey::ClassesRoot.DeleteKey("ApxOle.Application");
  278.   TRegKey::ClassesRoot.DeleteKey("ApxOle.Document.1");
  279.   TRegKey::ClassesRoot.DeleteKey(".olv");
  280. }
  281.  
  282.  
  283.  
  284. //--------------------------------------------------------
  285. // TApxOleApp
  286. // ~~~~~
  287. // Response Table handlers:
  288. //
  289. void TApxOleApp::EvNewView(TView& view)
  290. {
  291.   TOleView* ov = TYPESAFE_DOWNCAST(&view, TOleView);
  292.  
  293.   // If  we're embedded and we're opening out of place then don't create the child window.
  294.   //
  295.   if (view.GetDocument().IsEmbedded() && ov && !ov->IsOpenEditing()) {
  296.     TWindow* vw = view.GetWindow();
  297.     vw->SetParent(TYPESAFE_DOWNCAST(GetMainWindow(), TOleFrame)->GetRemViewBucket());
  298.     vw->Create();
  299.   }
  300.   else {
  301.     TMDIClient* mdiClient = TYPESAFE_DOWNCAST(GetMainWindow()->GetClientWindow(), TMDIClient);
  302.     if (mdiClient) {
  303.       TApxOleMDIChild* child = new TApxOleMDIChild(*mdiClient, 0, 0);
  304.  
  305.       // Assign icons for this child window.
  306.       //
  307.       child->SetIcon(this, IDI_DOC);
  308.       child->SetIconSm(this, IDI_DOC);
  309.  
  310.       if (view.GetViewMenu())
  311.         child->SetMenuDescr(*view.GetViewMenu());
  312.  
  313.       child->Create();
  314.       child->SetClientWindow(view.GetWindow());
  315.     }
  316.   }
  317.  
  318.   if (!ov || !ov->GetOcRemView())
  319.     OcApp->SetOption(amEmbedding, false);
  320. }
  321.  
  322.  
  323. void TApxOleApp::EvCloseView(TView&)
  324. {
  325. }
  326.  
  327.  
  328. void TApxOleApp::CeFileSend(TCommandEnabler& ce)
  329. {
  330.   ce.Enable((GetDocManager()->GetCurrentDoc() != 0)
  331.             && ApxMail->IsMAPIAvailable());
  332. }
  333.  
  334.  
  335. void TApxOleApp::CmFileSend ()
  336. {
  337.   // Check to see if a document exists
  338.   //
  339.   TDocument* currentDoc = GetDocManager()->GetCurrentDoc();
  340.  
  341.   if (currentDoc) {
  342.     TAPointer<char> savedPath = new char[_MAX_PATH];
  343.     TAPointer<char> docName = new char[_MAX_PATH];
  344.  
  345.     bool dirtyState = currentDoc->IsDirty();
  346.  
  347.     if (currentDoc->GetDocPath())
  348.       strcpy(savedPath, currentDoc->GetDocPath());
  349.     else
  350.       strcpy(savedPath, "");
  351.  
  352.     if (currentDoc->GetTitle())
  353.       strcpy(docName, currentDoc->GetTitle());
  354.     else
  355.       strcpy(docName, "Document");
  356.  
  357.     TFileName tempFile(TFileName::TempFile);
  358.  
  359.     currentDoc->SetDocPath(tempFile.Canonical().c_str());
  360.     currentDoc->Commit(true);
  361.  
  362.     currentDoc->SetDocPath(savedPath);
  363.     currentDoc->SetDirty(dirtyState);
  364.  
  365.     ApxMail->SendDocuments(GetMainWindow(), tempFile.Canonical().c_str(), docName, false);
  366.  
  367.     tempFile.Remove();
  368.   }
  369. }
  370.  
  371.  
  372. //--------------------------------------------------------
  373. // TApxOleApp
  374. // ~~~~~~~~~~~
  375. // Menu Help About ApxOle command
  376. void TApxOleApp::CmHelpAbout()
  377. {
  378.   // Show the modal dialog.
  379.   //
  380.   TApxOleAboutDlg(&TWindow(::GetFocus(), this)).Execute();
  381. }
  382.  
  383.  
  384. void TApxOleApp::EvDropFiles(TDropInfo drop)
  385. {
  386.   TFileDropletList files;
  387.  
  388.   // Iterate thru the entries in drop and create FileDrops objects for each
  389.   // one.
  390.   //
  391.   int fileCount = drop.DragQueryFileCount();  // Number of files dropped.
  392.   for (int i = 0; i < fileCount; i++)
  393.     files.Add(new TFileDroplet(drop, i));
  394.  
  395.   // Open the files that were dropped.
  396.   //
  397.   AddFiles(files);
  398.  
  399.   // Release the memory allocated for this handle with DragFinish.
  400.   //
  401.   drop.DragFinish();
  402. }
  403.  
  404.  
  405. void TApxOleApp::AddFiles(TFileDropletList& files)
  406. {
  407.   // Open all files dragged in.
  408.   //
  409.   for (TFileDropletListIter fileIter(files); fileIter; fileIter++) {
  410.     TDocTemplate* tpl = GetDocManager()->MatchTemplate(fileIter.Current()->GetName());
  411.     if (tpl)
  412.       GetDocManager()->CreateDoc(tpl, fileIter.Current()->GetName());
  413.   }
  414. }
  415.  
  416.  
  417. void TApxOleApp::EvOwlDocument(TDocument& doc)
  418. {
  419.   if (doc.GetDocPath())
  420.     SaveMenuChoice(doc.GetDocPath());
  421. }
  422.  
  423.  
  424. int32 TApxOleApp::CmFileSelected(uint wp, int32)
  425. {
  426.   TAPointer<char> text = new char[_MAX_PATH];
  427.  
  428.   GetMenuText(wp, text, _MAX_PATH);
  429.   TDocTemplate* tpl = GetDocManager()->MatchTemplate(text);
  430.   if (tpl)
  431.     GetDocManager()->CreateDoc(tpl, text);
  432.   return 0;
  433. }
  434.  
  435.  
  436. void TApxOleApp::EvWinIniChange(char far* section)
  437. {
  438.   if (strcmp(section, "windows") == 0) {
  439.     // If the device changed in the WIN.INI file then the printer
  440.     // might have changed.  If we have a TPrinter(Printer) then
  441.     // check and make sure it's identical to the current device
  442.     // entry in WIN.INI.
  443.     //
  444.     if (Printer) {
  445.       const int bufferSize = 255;
  446.       char printDBuffer[bufferSize];
  447.       LPSTR printDevice = printDBuffer;
  448.       LPSTR devName;
  449.       LPSTR driverName = 0;
  450.       LPSTR outputName = 0;
  451.       if (::GetProfileString("windows", "device", "", printDevice, bufferSize)) {
  452.         // The string which should come back is something like:
  453.         //
  454.         //      HP LaserJet III,hppcl5a,LPT1:
  455.         //
  456.         // Where the format is:
  457.         //
  458.         //      devName,driverName,outputName
  459.         //
  460.         devName = printDevice;
  461.         while (*printDevice) {
  462.           if (*printDevice == ',') {
  463.             *printDevice++ = 0;
  464.             if (!driverName)
  465.               driverName = printDevice;
  466.             else
  467.               outputName = printDevice;
  468.           }
  469.           else
  470.             printDevice = ::AnsiNext(printDevice);
  471.         }
  472.  
  473.         if (Printer->GetSetup().Error != 0                ||
  474.             strcmp(devName, Printer->GetSetup().GetDeviceName()) != 0  ||
  475.             strcmp(driverName, Printer->GetSetup().GetDriverName()) != 0 ||
  476.             strcmp(outputName, Printer->GetSetup().GetOutputName()) != 0 ) {
  477.           // New printer installed so get the new printer device now.
  478.           //
  479.           delete Printer;
  480.           Printer = new TPrinter(this);
  481.         }
  482.       }
  483.       else {
  484.         // No printer installed(GetProfileString failed).
  485.         //
  486.         delete Printer;
  487.         Printer = new TPrinter(this);
  488.       }
  489.     }
  490.   }
  491. }
  492.  
  493.  
  494. int OwlMain(int , char* [])
  495. {
  496.   ::Registrar = new TOcRegistrar(::ApplicationReg, TOleDocViewFactory<TApxOleApp>(), TApplication::GetCmdLine(), ::DocTemplateStaticHead);
  497.   if (::Registrar->IsOptionSet(amAnyRegOption))
  498.     return 0;
  499.  
  500.   // If this is an exe server normal run, run the app now. Otherwise, wait
  501.   // until our factory gets a call.
  502.   //
  503.   return ::Registrar->Run();
  504. }
  505.