home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / step17.pak / STEP17.CPP < prev    next >
C/C++ Source or Header  |  1997-07-23  |  8KB  |  260 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1994 by Borland International
  3. //   Tutorial application -- step17.cpp
  4. //   Enhanced Embed/Linking/Automation Server/Container example
  5. //----------------------------------------------------------------------------
  6. #include <owl/owlpch.h>
  7. #include <owl/applicat.h>
  8. #include <owl/dialog.h>
  9. #include <owl/controlb.h>
  10. #include <owl/buttonga.h>
  11. #include <owl/listbox.h>
  12. #include <owl/statusba.h>
  13. #include <owl/docmanag.h>
  14. #include <owl/olemdifr.h>
  15. #include <owl/oledoc.h>
  16. #include <owl/oleview.h>
  17. #include <classlib/arrays.h>
  18. #include <ocf/automacr.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "step17.rc"
  22. #include "step17dv.h"
  23. #include "step17.h"
  24.  
  25. DEFINE_APP_DICTIONARY(AppDictionary);
  26. static TPointer<TOcRegistrar> Registrar;
  27.  
  28. // Registration
  29. //
  30. REGISTRATION_FORMAT_BUFFER(100)
  31.  
  32. BEGIN_REGISTRATION(AppReg)
  33.   REGDATA(clsid,      "{5E4BD340-8ABC-101B-A23B-CE4E85D07ED2}")
  34.   REGDATA(progid,     "DrawPad.Application.17")
  35.   REGDATA(description,"DrawPad AutoContServer Application")
  36.   REGDATA(appname,    "DrawPad AutoContServer")
  37. //REGDATA(debugger,   "tdw")
  38.   REGDATA(cmdline,    "/automation")
  39.   REGDATA(usage,      ocrMultipleUse)
  40. END_REGISTRATION
  41.  
  42. //
  43. // TDrawApp
  44. //
  45. DEFINE_RESPONSE_TABLE1(TDrawApp, TApplication)
  46.   EV_OWLVIEW(dnCreate, EvNewView),
  47.   EV_OWLVIEW(dnClose,  EvCloseView),
  48.   EV_WM_DROPFILES,
  49.   EV_COMMAND(CM_ABOUT, CmAbout),
  50. END_RESPONSE_TABLE;
  51.  
  52. DEFINE_AUTOAGGREGATE(TDrawApp, OcApp->Aggregate)
  53.   EXPOSE_PROPRW(Visible, TAutoBool,    "Visible",     "Main window shown", 0)
  54.   EXPOSE_METHOD(NewDoc,  TDrawDocument,"NewDocument", "Create new document", 0)
  55.   EXPOSE_METHOD(OpenDoc, TDrawDocument,"OpenDocument","Open existing document", 0)
  56.    REQUIRED_ARG(         TAutoString,  "Name")
  57.   EXPOSE_PROPRO(AppName, TAutoString,  "Name",        "Application name", 0)
  58.   EXPOSE_PROPRO(FullName,TAutoString,  "FullName",    "Complete path to application", 0)
  59.   EXPOSE_APPLICATION(TDrawApp,         "Application", "Application object", 0)
  60.   EXPOSE_QUIT(                         "Quit",        "Shutdown application", 0)
  61. END_AUTOAGGREGATE(TDrawApp,tfAppObject|tfCanCreate,"TDrawApp","Application class", 0)
  62.  
  63. const char far*
  64. TDrawApp::GetPath()
  65. {
  66.   static char buf[_MAX_PATH];
  67.   GetModuleFileName(buf, sizeof(buf)-1);
  68.   return buf;
  69. }
  70.  
  71. bool
  72. TDrawApp::GetShow()
  73. {
  74.   TFrameWindow* frame = GetMainWindow();
  75.   return (frame && frame->IsWindow() && frame->IsWindowVisible());
  76. }
  77.  
  78. void
  79. TDrawApp::SetShow(bool visible)
  80. {
  81.   TFrameWindow* frame = GetMainWindow();
  82.   if (frame && frame->IsWindow()) {
  83.     unsigned flags = visible ? SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_SHOWWINDOW
  84.                 : SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_HIDEWINDOW;
  85.     frame->SetWindowPos(HWND_TOP, 0,0,0,0, flags);
  86.   }
  87. }
  88.  
  89. extern TDocTemplate drawTpl;
  90.  
  91. TDrawDocument*
  92. TDrawApp::OpenDoc(const char far* name)
  93. {
  94.   long flags = name ? 0 : dtNewDoc;
  95.   TDocManager* docManager = GetDocManager();
  96.   if (!docManager)
  97.     return 0;
  98.   HWND hWnd = ::GetFocus();
  99.   TDocument* doc = GetDocManager()->CreateDoc(&drawTpl, name, 0, flags);
  100.   ::SetFocus(hWnd);
  101.   return dynamic_cast<TDrawDocument*>(doc);
  102. }
  103.  
  104. TDrawApp::TDrawApp() :
  105.   TApplication(::AppReg["appname"], ::Module, &::AppDictionary)
  106. {
  107. }
  108.  
  109. void
  110. TDrawApp::InitMainWindow()
  111. {
  112.   View = 0;
  113.  
  114.   // Determine whether it's MDI or SDI app
  115.   // If single use or DLL server, run multiple instances as SDI apps
  116.   DocMode = (IsOptionSet(amSingleUse) ||
  117.             !IsOptionSet(amExeMode)) ? dmSDI : dmMDI;
  118.  
  119.   TOleFrame* frame;
  120.   if (DocMode == dmMDI)
  121.     // Construct the TOleMDIFrame frame window
  122.     frame = new TOleMDIFrame(GetName(), 0, *(Client = new TMDIClient), true);
  123.   else
  124.     // Construct the TOleFrame frame window
  125.     frame = new TOleFrame(GetName(), 0, true);
  126.  
  127.   // Construct a status bar
  128.   TStatusBar* sb = new TStatusBar(frame, TGadget::Recessed);
  129.  
  130.   // Construct a control bar
  131.   TControlBar* cb = new TControlBar(frame);
  132.   cb->Insert(*new TButtonGadget(CM_FILENEW, CM_FILENEW, TButtonGadget::Command));
  133.   cb->Insert(*new TButtonGadget(CM_FILEOPEN, CM_FILEOPEN, TButtonGadget::Command));
  134.   cb->Insert(*new TButtonGadget(CM_FILESAVE, CM_FILESAVE, TButtonGadget::Command));
  135.   cb->Insert(*new TButtonGadget(CM_FILESAVEAS, CM_FILESAVEAS, TButtonGadget::Command));
  136.   cb->Insert(*new TSeparatorGadget);
  137.   cb->Insert(*new TButtonGadget(CM_PENSIZE, CM_PENSIZE, TButtonGadget::Command));
  138.   cb->Insert(*new TButtonGadget(CM_PENCOLOR, CM_PENCOLOR, TButtonGadget::Command));
  139.   cb->Insert(*new TSeparatorGadget);
  140.   cb->Insert(*new TButtonGadget(CM_PEN, CM_PEN, TButtonGadget::Exclusive));
  141.   cb->Insert(*new TButtonGadget(CM_SELECT, CM_SELECT, TButtonGadget::Exclusive));
  142.   cb->Insert(*new TSeparatorGadget);
  143.   cb->Insert(*new TButtonGadget(CM_ABOUT, CM_ABOUT, TButtonGadget::Command));
  144.   cb->SetHintMode(TGadgetWindow::EnterHints);
  145.  
  146.   cb->Attr.Id = IDW_TOOLBAR;
  147.  
  148.   // Insert the status bar and control bar into the frame
  149.   frame->Insert(*sb, TDecoratedFrame::Bottom);
  150.   frame->Insert(*cb, TDecoratedFrame::Top);
  151.  
  152.   // Set the main window and its menu
  153.   SetMainWindow(frame);
  154.   if (DocMode == dmMDI)
  155.     GetMainWindow()->SetMenuDescr(TMenuDescr(IDM_MDICMNDS));
  156.   else
  157.     GetMainWindow()->SetMenuDescr(TMenuDescr(IDM_SDICMNDS));
  158.  
  159.   // Install the document manager
  160.   SetDocManager(new TDocManager(DocMode));
  161. }
  162.  
  163. void
  164. TDrawApp::InitInstance()
  165. {
  166.   TApplication::InitInstance();
  167.   GetMainWindow()->DragAcceptFiles(true);
  168. }
  169.  
  170. void
  171. TDrawApp::EvDropFiles(TDropInfo dropInfo)
  172. {
  173.   int fileCount = dropInfo.DragQueryFileCount();
  174.   for (int index = 0; index < fileCount; index++) {
  175.     int fileLength = dropInfo.DragQueryFileNameLen(index)+1;
  176.     char* filePath = new char [fileLength];
  177.     dropInfo.DragQueryFile(index, filePath, fileLength);
  178.     TDocTemplate* tpl = GetDocManager()->MatchTemplate(filePath);
  179.     if (tpl)
  180.       GetDocManager()->CreateDoc(tpl, filePath);
  181.     else { // Embedding from file
  182.       TOleWindow* oleWin = TYPESAFE_DOWNCAST(View, TOleWindow);
  183.  
  184.       // Let the TOleWindow handle the dropped file
  185.       //
  186.       if (oleWin)
  187.         oleWin->ForwardMessage();
  188.     }
  189.  
  190.     delete filePath;
  191.   }
  192.   dropInfo.DragFinish();
  193. }
  194.  
  195. void
  196. TDrawApp::EvNewView(TView& view)
  197. {
  198.   View = &view;
  199.   TOleView* ov = TYPESAFE_DOWNCAST(&view, TOleView);
  200.   if (DocMode == dmMDI) {
  201.     if (view.GetDocument().IsEmbedded() && ov && !ov->IsOpenEditing()) {
  202.       TWindow* vw = view.GetWindow();
  203.       TOleFrame* mw = TYPESAFE_DOWNCAST(GetMainWindow(), TOleFrame);
  204.       TWindow* rvb = mw->GetRemViewBucket();
  205.       vw->SetParent(rvb);
  206.       vw->Create();
  207.     }
  208.     else {
  209.       TMDIChild* child = new TMDIChild(*Client, 0);
  210.       if (view.GetViewMenu())
  211.         child->SetMenuDescr(*view.GetViewMenu());
  212.       child->Create();
  213.       child->SetClientWindow(view.GetWindow());
  214.     }
  215.   }
  216.   else {
  217.     TFrameWindow* frame = GetMainWindow();
  218.     frame->SetClientWindow(view.GetWindow());
  219.     if (!view.IsOK())
  220.       frame->SetClientWindow(0);
  221.     else if (view.GetViewMenu())
  222.       frame->MergeMenu(*view.GetViewMenu());
  223.   }
  224.  
  225.   if (!ov || !ov->GetOcRemView())
  226.     OcApp->SetOption(amEmbedding, false);
  227. }
  228.  
  229. void
  230. TDrawApp::EvCloseView(TView& /*view*/)
  231. {
  232.   // nothing needs to be done here for MDI
  233.   if (DocMode == dmSDI) {
  234.     GetMainWindow()->SetClientWindow(0);
  235.   }
  236. }
  237.  
  238. void
  239. TDrawApp::CmAbout()
  240. {
  241.   TDialog(&TWindow(::GetFocus(), this), IDD_ABOUT).Execute();
  242. }
  243.  
  244. int
  245. OwlMain(int /*argc*/, char* /*argv*/ [])
  246. {
  247.   try {
  248.     Registrar = new TOcRegistrar(AppReg, TOleDocViewAutoFactory<TDrawApp>(),
  249.                                  TApplication::GetCmdLine(), ::DocTemplateStaticHead);
  250.     if (Registrar->IsOptionSet(amAnyRegOption))
  251.       return 0;
  252.  
  253.     return Registrar->Run();
  254.   }
  255.   catch (xmsg& x) {
  256.     ::MessageBox(0, x.why().c_str(), "Exception", MB_OK);
  257.   }
  258.   return -1;
  259. }
  260.