home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OCFSRC.PAK / OCLINK.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-29  |  10.0 KB  |  485 lines

  1. //
  2. //----------------------------------------------------------------------------
  3. // ObjectComponents
  4. // (C) Copyright 1994 by Borland International, All Rights Reserved
  5. //
  6. //   Implementation of TOcLinkView Class
  7. //----------------------------------------------------------------------------
  8. #include <ocf/ocfpch.h>
  9. #include <ocf/oclink.h>
  10. #include <ocf/ocapp.h>
  11. #include <ocf/ocremvie.h>
  12.  
  13. TOcLinkView::TOcLinkView(TOcView* ocView, TRegList* regList, IUnknown* outer)
  14. :
  15.   BSiteI(0), OcView(ocView),
  16. #if defined(BI_DATA_NEAR)
  17.   Moniker(*new TString),
  18.   Origin(*new TPoint(0,0)),
  19.   Extent(*new TSize(0,0))
  20. #else
  21.   Origin(0,0),
  22.   Extent(0,0)
  23. #endif
  24. {
  25.   PRECONDITION(OcView);
  26.   SetOuter(outer);
  27.   AddRef();    // TUnknown defaults to 0, we need 1
  28.  
  29.   // Create a site for this remote view
  30.   //
  31.   if (SUCCEEDED(OcView->OcApp.BOleComponentCreate(&BSite, (IUnknown*)(IBPart*)this,
  32.       OcView->OcApp.IsOptionSet(amExeModule)? cidBOleSite : cidBOleInProcSite))) {
  33.  
  34.     if (SUCCEEDED(BSite->QueryInterface(IID_IBSite, &(LPVOID)BSiteI)))
  35.       Release();
  36.  
  37.     // Connect the part and the site
  38.     //
  39.     if (BSiteI) {
  40.       const char* progid = regList->Lookup(OcView->OcApp.IsOptionSet(amDebug) ?
  41.                                            "debugprogid" : "progid");
  42.       BSiteI->Init(this, this, OleStr(progid), true);
  43.     }
  44.  
  45.     if (SUCCEEDED(BSite->QueryInterface(IID_IBApplication, &(LPVOID)BAppI)))
  46.       BAppI->Release();     // avoid deadlock
  47.   }
  48. }
  49.  
  50. TOcLinkView::~TOcLinkView()
  51. {
  52.   // Detach the link view
  53.   //
  54.   Detach();
  55.   if (BSite) {
  56.     BSite->Release();
  57.   }
  58. #if defined(BI_DATA_NEAR)
  59.   delete &Moniker;
  60. #endif
  61. }
  62.  
  63. //
  64. // Remove this link view from the document
  65. //
  66. int
  67. TOcLinkView::Detach()
  68. {
  69.   return OcView->OcDocument.GetViews().Detach(this);
  70. }
  71.  
  72. //
  73. //
  74. //
  75. HRESULT
  76. TOcLinkView::QueryObject(const IID far& iid, void far* far* iface)
  77. {
  78.   PRECONDITION(iface);
  79.   HRESULT hr;
  80.  
  81.   // interfaces
  82.   //
  83.      SUCCEEDED(hr = IBPart_QueryInterface(this, iid, iface))
  84.   || SUCCEEDED(hr = IBDataProvider_QueryInterface(this, iid, iface))
  85.  
  86.   // helpers
  87.   //
  88.   || (BSite && SUCCEEDED(hr = BSite->QueryInterface(iid, iface)))
  89.   ;
  90.  
  91.   return hr;
  92. }
  93.  
  94. //----------------------------------------------------------------------------
  95. // IBSite pass-thrus
  96.  
  97. //
  98. // Invalidate the site corresponding to this view
  99. //
  100. void
  101. TOcLinkView::Invalidate(TOcInvalidate invalid)
  102. {
  103.   if (BSiteI)
  104.     BSiteI->Invalidate(invalid);
  105. }
  106.  
  107. //
  108. // Disconnect from the client site
  109. //
  110. void
  111. TOcLinkView::Disconnect()
  112. {
  113.   if (BSiteI)
  114.     BSiteI->Disconnect();
  115. }
  116.  
  117. //
  118. // Remember the name of the moniker
  119. //
  120. void
  121. TOcLinkView::SetMoniker(const char far* name)
  122. {
  123.   Moniker  = const_cast<char far*>(name);  // force TString to copy
  124. }
  125.  
  126.  
  127. //----------------------------------------------------------------------------
  128. // IDataNegotiator implementation
  129.  
  130. uint _IFUNC
  131. TOcLinkView::CountFormats()
  132. {
  133.   return OcView->CountFormats();
  134. }
  135.  
  136. HRESULT _IFUNC
  137. TOcLinkView::GetFormat(uint index, TOcFormatInfo far* fmt)
  138. {
  139.   PRECONDITION(fmt);
  140.  
  141.   return OcView->GetFormat(index, fmt);
  142. }
  143.  
  144.  
  145. //----------------------------------------------------------------------------
  146. // IBDataNegotiator implementation
  147.  
  148. //
  149. // Request native data for pasting into client application.
  150. // This is only called at paste time (not at copy time).
  151. //
  152. HANDLE _IFUNC
  153. TOcLinkView::GetFormatData(TOcFormatInfo far* fmt)
  154. {
  155.   PRECONDITION(fmt);
  156.  
  157.   TOcFormat* format = OcView->FormatList.Find(fmt->Id);
  158.   if (!format)
  159.     return 0;
  160.  
  161.   TOcFormatData formatData(*format);
  162.   if (OcView->ForwardEvent(OC_VIEWCLIPDATA, &formatData))
  163.     return formatData.Handle;
  164.   else
  165.     return 0;
  166. }
  167.  
  168. //
  169. //  Get the initial size and position from the app
  170. //
  171. void
  172. TOcLinkView::GetLinkRect()
  173. {
  174.   TOcPartSize ps(true, &Moniker);
  175.  
  176.   // Ask the app for initial server extent
  177.   //
  178.   if (!OcView->ForwardEvent(OC_VIEWPARTSIZE, &ps)) {
  179.     // An empty rect as default means that the container
  180.     // decides the size for this server
  181.     //
  182.     ps.PartRect.SetNull();
  183.   }
  184.  
  185.   Extent = ps.PartRect.Size();
  186.   Origin = ps.PartRect.TopLeft();
  187. }
  188.  
  189. //
  190. // Render the view in the DC provided. Should be a MetaFile
  191. // Packup all the args & forward message to real view to paint
  192. //
  193. HRESULT _IFUNC
  194. TOcLinkView::Draw(HDC dc, const RECTL far*  pos, const RECTL far* clip,
  195.                  TOcAspect aspect, TOcDraw bd)
  196. {
  197.   PRECONDITION(dc);
  198.  
  199.   // Rely on the bolero shading
  200.   //
  201.   if (bd == drShadingOnly)
  202.     return HR_NOERROR;
  203.  
  204.   TRect p((int)pos->left, (int)pos->top, (int)pos->right, (int)pos->bottom);
  205.   TRect c((int)clip->left, (int)clip->top, (int)clip->right, (int)clip->bottom);
  206.  
  207.   p.SetEmpty();
  208.   ::SetMapMode(dc, MM_ANISOTROPIC);
  209.  
  210.   ::SetWindowExtEx(dc, Extent.cx, Extent.cy, 0);
  211.   ::SetWindowOrgEx(dc, 0, 0, 0);
  212.  
  213.   p.Normalize();
  214.   c.Normalize();
  215.  
  216.   // Find out where the TOleLinkView is
  217.   //
  218.   GetLinkRect();
  219.   *(TPoint*)&p = Origin;
  220.  
  221.   TOcViewPaint vp = { dc, &p, &c, (TOcAspect)aspect, false, &Moniker, 0 };
  222.   bool result = (bool)OcView->ForwardEvent(OC_VIEWPAINT, &vp);
  223.  
  224.   return HRFailIfZero(result);
  225. }
  226.  
  227. //
  228. // Return the 'size' of the document that this view in on
  229. //
  230. HRESULT _IFUNC
  231. TOcLinkView::GetPartSize(TSize far* size)
  232. {
  233.   *size = Extent;
  234.   return HR_NOERROR;
  235. }
  236.  
  237. //
  238. // Save the document that we are a view on
  239. //
  240. HRESULT _IFUNC
  241. TOcLinkView::Save(IStorage* storage, BOOL sameAsLoad, BOOL remember)
  242. {
  243.   PRECONDITION(storage);
  244.  
  245.   TOcSaveLoad ocSave(storage, static_cast<bool>(sameAsLoad), static_cast<bool>(remember));
  246.   return HRFailIfZero((bool)OcView->ForwardEvent(OC_VIEWSAVEPART, &ocSave));
  247. }
  248.  
  249. //----------------------------------------------------------------------------
  250. // IBPart implementation
  251.  
  252. //
  253. // Load the associated document and activate the remote view
  254. //
  255. HRESULT _IFUNC
  256. TOcLinkView::Init(IBSite far*, TOcInitInfo far* /*initInfo*/)
  257. {
  258.   return HR_NOERROR;
  259. }
  260.  
  261. //
  262. // Close the remote view window, & if canShutDown is true, try to close the server
  263. // app too
  264. //
  265. HRESULT _IFUNC
  266. TOcLinkView::Close()
  267. {
  268.   OcView->ForwardEvent(OC_VIEWBREAKLINK, this);
  269.  
  270.   return HR_NOERROR;
  271. }
  272.  
  273. //
  274. // Query to determin if this view can open in place
  275. //
  276. HRESULT _IFUNC
  277. TOcLinkView::CanOpenInPlace()
  278. {
  279.   return HR_FAIL;
  280. }
  281.  
  282. //
  283. // Set a new position for our document within its container
  284. //
  285. HRESULT _IFUNC
  286. TOcLinkView::SetPartPos(TRect far* r)
  287. {
  288.   Origin = *(POINT*)&r->left;
  289.  
  290.   return HR_NOERROR;
  291. }
  292.  
  293. HRESULT _IFUNC
  294. TOcLinkView::SetPartSize(TSize far* size)
  295. {
  296.   Extent = *size;
  297.   return HR_NOERROR;
  298. }
  299.  
  300. //
  301. // Activate this view
  302. //
  303. HRESULT _IFUNC
  304. TOcLinkView::Activate(bool /*activate*/)
  305. {
  306.   return HR_NOERROR;
  307. }
  308.  
  309. //
  310. // Show/Hide the server view window
  311. //
  312. HRESULT _IFUNC
  313. TOcLinkView::Show(bool /*show*/)
  314. {
  315.   return HR_NOERROR;
  316. }
  317.  
  318. //
  319. // Start or end open editing
  320. // Work with the window Z-order and parenting
  321. //
  322. HRESULT _IFUNC
  323. TOcLinkView::Open(bool open)
  324. {
  325.   if (open) {
  326.     TOcRemView* ocRemView = TYPESAFE_DOWNCAST(OcView, TOcRemView);
  327.     if (ocRemView)
  328.       ocRemView->SetOpenEditing();
  329.  
  330.     OcView->ForwardEvent(OC_VIEWATTACHWINDOW, true);
  331.     OcView->BringToFront();
  332.   }
  333.  
  334.   return HR_NOERROR;
  335. }
  336.  
  337. //
  338. // Enumerate the verbs for our document
  339. //
  340. HRESULT _IFUNC
  341. TOcLinkView::EnumVerbs(TOcVerb far*)
  342. {
  343.   return HR_FAIL;  // Not called on BOle parts
  344. }
  345.  
  346. //
  347. // Perform a verb on our document
  348. //
  349. HRESULT _IFUNC
  350. TOcLinkView::DoVerb(uint)
  351. {
  352.   return HR_FAIL;  // Not called on BOle parts
  353. }
  354.  
  355. //
  356. // Open or close this view as an in-place edit session. If hWndParent is 0, then
  357. // in-place is closing
  358. //
  359. HWND _IFUNC
  360. TOcLinkView::OpenInPlace(HWND /*hWndParent*/)
  361. {
  362.   return 0;
  363. }
  364.  
  365. //
  366. // Insert the server's menus into the shared menubar
  367. //
  368. HRESULT _IFUNC
  369. TOcLinkView::InsertMenus(HMENU /*hMenu*/, TOcMenuWidths far* /*omw*/)
  370. {
  371.   return HR_NOERROR;
  372. }
  373.  
  374. //
  375. // Show or hide the tool windows used by our view
  376. //
  377. HRESULT _IFUNC
  378. TOcLinkView::ShowTools(bool /*show*/)
  379. {
  380.   return HR_NOERROR;
  381. }
  382.  
  383. //
  384. // A container window has resized. Perform any necessary adjustment of our
  385. // tools.
  386. //
  387. void _IFUNC
  388. TOcLinkView::FrameResized(const TRect far* /*contFrameR*/, bool /*isMainFrame*/)
  389. {
  390. }
  391.  
  392. //
  393. // Let the server provide drag feedback
  394. //
  395. HRESULT _IFUNC
  396. TOcLinkView::DragFeedback(TPoint far* where, bool /*nearScroll*/)
  397. {
  398.   TPoint awhere(*where);
  399.   TOcDragDrop dd = { 0, &awhere, 0 };
  400.   return HRFailIfZero((bool)OcView->ForwardEvent(OC_VIEWDRAG, &dd));
  401. }
  402.  
  403. //
  404. // Optional palette query for
  405. //
  406. HRESULT _IFUNC
  407. TOcLinkView::GetPalette(LOGPALETTE far* far* palette)
  408. {
  409.   PRECONDITION(palette);
  410.  
  411.   return HRFailIfZero((bool)OcView->ForwardEvent(OC_VIEWGETPALETTE, palette));
  412. }
  413.  
  414. HRESULT _IFUNC
  415. TOcLinkView::SetHost(IBContainer far* /*objContainer*/)
  416. {
  417.   return HR_FAIL;  // Not called on BOle parts.
  418. }
  419.  
  420. HRESULT _IFUNC
  421. TOcLinkView::DoQueryInterface(const IID far& iid, void far* far* iface)
  422. {
  423.   PRECONDITION(iface);
  424.  
  425.   return OcView->QueryInterface(iid, iface);  // Unused on server side
  426. }
  427.  
  428. LPOLESTR _IFUNC
  429. TOcLinkView::GetName(TOcPartName /*name*/)
  430. {
  431.   return 0;  // Not called on BOle parts.
  432. }
  433.  
  434.  
  435. //----------------------------------------------------------------------------
  436. // TOcViewCollection
  437. //
  438.  
  439. TOcViewCollection::TOcViewCollection()
  440. :
  441.   TCVectorImp<TOcLinkView*>(0, 10)
  442. {
  443. }
  444.  
  445. TOcViewCollection::~TOcViewCollection()
  446. {
  447.   Clear();
  448. }
  449.  
  450. //
  451. // Release Views in the collection
  452. //
  453. void
  454. TOcViewCollection::Clear()
  455. {
  456.   for (int i = Count() - 1; i >= 0; i--) {
  457.     TOcLinkView* View = (TOcLinkView*)(*this)[i];
  458.     View->Release();
  459.   }
  460. }
  461.  
  462. int
  463. TOcViewCollection::Detach(TOcLinkView* const& view, int del)
  464. {
  465.   int ret = Base::Detach(Find(view));
  466.   if (ret && del)
  467.     const_cast<TOcLinkView*>(view)->Release();
  468.   return ret;
  469. }
  470.  
  471. TOcLinkView*
  472. TOcViewCollection::Find(TString const moniker) const
  473. {
  474.   for (TOcViewCollectionIter j(*this); j; j++) {
  475.     TOcLinkView* view = (TOcLinkView*)j.Current();
  476.     if (view && strcmp(view->GetMoniker(), moniker) == 0) {
  477.       return view;
  478.     }
  479.   }
  480.  
  481.   return 0;
  482. }
  483.  
  484.  
  485.