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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1994 by Borland International
  3. //   Tutorial application -- step16.cpp
  4. //   Embed/Linking/Automation Server 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 "step16.rc"
  22. #include "step16dv.h"
  23. #include "step16.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,      "{5E4BD330-8ABC-101B-A23B-CE4E85D07ED2}")
  34.   REGDATA(progid,     "DrawPad.Application.16")
  35.   REGDATA(description,"DrawPad AutoServer Application")
  36.   REGDATA(appname,    "DrawPad AutoServer")
  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.   TOleMDIFrame* frame;
  113.   frame = new TOleMDIFrame(GetName(), 0, *(Client = new TMDIClient(this)), true, this);
  114.  
  115.   // Construct a status bar
  116.   TStatusBar* sb = new TStatusBar(frame, TGadget::Recessed);
  117.  
  118.   // Construct a control bar
  119.   TControlBar* cb = new TControlBar(frame);
  120.   cb->Insert(*new TButtonGadget(CM_FILENEW, CM_FILENEW, TButtonGadget::Command));
  121.   cb->Insert(*new TButtonGadget(CM_FILEOPEN, CM_FILEOPEN, TButtonGadget::Command));
  122.   cb->Insert(*new TButtonGadget(CM_FILESAVE, CM_FILESAVE, TButtonGadget::Command));
  123.   cb->Insert(*new TButtonGadget(CM_FILESAVEAS, CM_FILESAVEAS, TButtonGadget::Command));
  124.   cb->Insert(*new TSeparatorGadget);
  125.   cb->Insert(*new TButtonGadget(CM_PENSIZE, CM_PENSIZE, TButtonGadget::Command));
  126.   cb->Insert(*new TButtonGadget(CM_PENCOLOR, CM_PENCOLOR, TButtonGadget::Command));
  127.   cb->Insert(*new TSeparatorGadget);
  128.   cb->Insert(*new TButtonGadget(CM_ABOUT, CM_ABOUT, TButtonGadget::Command));
  129.   cb->SetHintMode(TGadgetWindow::EnterHints);
  130.  
  131.   cb->Attr.Id = IDW_TOOLBAR;
  132.  
  133.   // Insert the status bar and control bar into the frame
  134.   frame->Insert(*sb, TDecoratedFrame::Bottom);
  135.   frame->Insert(*cb, TDecoratedFrame::Top);
  136.  
  137.   // Set the main window and its menu
  138.   SetMainWindow(frame);
  139.   GetMainWindow()->SetMenuDescr(TMenuDescr(IDM_MDICMNDS));
  140.  
  141.   // Install the document manager
  142.   SetDocManager(new TDocManager(dmMDI, this));
  143. }
  144.  
  145. void
  146. TDrawApp::InitInstance()
  147. {
  148.   TApplication::InitInstance();
  149.   GetMainWindow()->DragAcceptFiles(true);
  150. }
  151.  
  152. void
  153. TDrawApp::EvDropFiles(TDropInfo dropInfo)
  154. {
  155.   int fileCount = dropInfo.DragQueryFileCount();
  156.   for (int index = 0; index < fileCount; index++) {
  157.     int fileLength = dropInfo.DragQueryFileNameLen(index)+1;
  158.     char* filePath = new char [fileLength];
  159.     dropInfo.DragQueryFile(index, filePath, fileLength);
  160.     TDocTemplate* tpl = GetDocManager()->MatchTemplate(filePath);
  161.     GetDocManager()->CreateDoc(tpl, filePath);
  162.     delete filePath;
  163.   }
  164.   dropInfo.DragFinish();
  165. }
  166.  
  167. void
  168. TDrawApp::EvNewView(TView& view)
  169. {
  170.   TOleView* ov = TYPESAFE_DOWNCAST(&view, TOleView);
  171.   if (view.GetDocument().IsEmbedded() && ov && !ov->IsOpenEditing()) {
  172.     TWindow* vw = view.GetWindow();
  173.     vw->SetParent(TYPESAFE_DOWNCAST(GetMainWindow(), TOleFrame)->GetRemViewBucket());
  174.     vw->Create();
  175.   }
  176.   else {
  177.     TMDIChild* child = new TMDIChild(*Client, 0);
  178.     if (view.GetViewMenu())
  179.       child->SetMenuDescr(*view.GetViewMenu());
  180.     child->Create();
  181.     child->SetClientWindow(view.GetWindow());
  182.   }
  183.  
  184.   if (!ov || !ov->GetOcRemView())
  185.     OcApp->SetOption(amEmbedding, false);
  186. }
  187.  
  188. void
  189. TDrawApp::EvCloseView(TView& /*view*/)
  190. {
  191.   // nothing needs to be done here for MDI
  192. }
  193.  
  194. void
  195. TDrawApp::CmAbout()
  196. {
  197.   TDialog(&TWindow(::GetFocus(), this), IDD_ABOUT).Execute();
  198. }
  199.  
  200. int
  201. OwlMain(int /*argc*/, char* /*argv*/ [])
  202. {
  203.   try {
  204.     Registrar = new TOcRegistrar(AppReg, TOleDocViewAutoFactory<TDrawApp>(),
  205.                                  TApplication::GetCmdLine(), ::DocTemplateStaticHead);
  206.     if (Registrar->IsOptionSet(amAnyRegOption))
  207.       return 0;
  208.  
  209.     return Registrar->Run();
  210.   }
  211.   catch (xmsg& x) {
  212.     ::MessageBox(0, x.why().c_str(), "Exception", MB_OK);
  213.   }
  214.   return -1;
  215. }
  216.