home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 11.ddi / OWLSRC.PAK / METAFILE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  4.7 KB  |  179 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1992, 1993 by Borland International
  3. //   source\owl\metafile.cpp
  4. //   Implementation of TMetaFilePict class
  5. //----------------------------------------------------------------------------
  6. #pragma hdrignore SECTION
  7. #include <owl\owlpch.h>
  8. #include <owl\metafile.h>
  9. #include <owl\gdiobjec.h>
  10. #include <owl\clipboar.h>
  11. #include <owl\except.h>
  12.  
  13.  
  14. TMetaFilePict::TMetaFilePict(HMETAFILE handle, TAutoDelete autoDelete)
  15.   : TGdiBase(handle, autoDelete),
  16.     Extent(0,0)
  17. {
  18.   Mm = MM_ANISOTROPIC;
  19. }
  20.  
  21. TMetaFilePict::TMetaFilePict(const char* filename)
  22.   : TGdiBase(::GetMetaFile(filename), AutoDelete),
  23.     Extent(0,0)
  24. {
  25.   Mm = MM_ANISOTROPIC;
  26. }
  27.  
  28. //
  29. // Construct a TMetaFilePict that represents the metafilepict on the clipboard.
  30. // Should be copied if metafile needs to be kept.
  31. //
  32. TMetaFilePict::TMetaFilePict(const TClipboard&)
  33.   : Extent(0,0)
  34. {
  35.   HGLOBAL hmfp = ::GetClipboardData(CF_METAFILEPICT);
  36.   METAFILEPICT far* mfp = (METAFILEPICT far*)::GlobalLock(hmfp);
  37.  
  38.   if (mfp) {
  39.     Mm = mfp->mm;
  40.     Extent = TSize(mfp->xExt, mfp->yExt);
  41.     Handle = mfp->hMF;
  42.     ::GlobalUnlock(hmfp);
  43.   } else
  44.     Handle = 0;
  45.  
  46.   ShouldDelete = FALSE;
  47.   CheckValid();
  48. }
  49.  
  50. #if defined(__WIN32__)
  51.  
  52. TMetaFilePict::TMetaFilePict(UINT size, void* data)
  53.   : TGdiBase(::SetMetaFileBitsEx(size, (LPBYTE)data), NoAutoDelete),
  54.     Extent(0,0)
  55. {
  56.   CheckValid();
  57.   Mm = MM_ANISOTROPIC;
  58. }
  59.  
  60. #else
  61.  
  62. TMetaFilePict::TMetaFilePict(HGLOBAL data)
  63.   : TGdiBase(::SetMetaFileBitsBetter(data), NoAutoDelete),
  64.     Extent(0,0)
  65. {
  66.   CheckValid();
  67.   Mm = MM_ANISOTROPIC;
  68. }
  69.  
  70. #endif
  71.  
  72. TMetaFilePict::TMetaFilePict(const TMetaFilePict& src, const char far* fileName)
  73.   : TGdiBase(::CopyMetaFile(src, fileName), AutoDelete),
  74.     Extent(src.Extent)
  75. {
  76.   CheckValid();
  77.   Mm = src.Mm;
  78. }
  79.  
  80. TMetaFilePict::~TMetaFilePict()
  81. {
  82.   if (ShouldDelete && Handle)
  83.     ::DeleteMetaFile(HMETAFILE(Handle));
  84. }
  85.  
  86. #if defined(__WIN32__)
  87.  
  88. DWORD
  89. TMetaFilePict::GetMetaFileBitsEx(UINT size, void* data)
  90. {
  91.   return ::GetMetaFileBitsEx(HMETAFILE(Handle), size, (LPBYTE)data);
  92. }
  93.  
  94. #endif
  95.  
  96. //
  97. // Calculates target play size based on info from the metafilepict (if any)
  98. // and default target size as necessary
  99. //
  100. TSize
  101. TMetaFilePict::CalcPlaySize(TDC& dc, const TSize& defSize) const
  102. {
  103.   // Given a fixed mapping mode, return precalculated extents
  104.   //
  105.   if (Mm != MM_ISOTROPIC && Mm != MM_ANISOTROPIC)
  106.     return Extent;    // Assumes extents were calculated correctly.
  107.  
  108.   // If no extent info given, then use defaults
  109.   //
  110.   if (!Extent.cx) {
  111.     return defSize;
  112.  
  113.   // Use positive extents scaled to 0.01mm units
  114.   //
  115.   } else if (Extent.cx > 0) {
  116.     return TSize(
  117.         int(long(Extent.cx)*dc.GetDeviceCaps(HORZRES)/dc.GetDeviceCaps(HORZSIZE)/100),
  118.         int(long(Extent.cy)*dc.GetDeviceCaps(VERTRES)/dc.GetDeviceCaps(VERTSIZE)/100)
  119.       );
  120.  
  121.   // Use negative extents scaled to 0.01mm units w/ aspect ratio scaling
  122.   //
  123.   } else {
  124.     long xscale = 100L * defSize.cx *
  125.       dc.GetDeviceCaps(HORZSIZE)/dc.GetDeviceCaps(HORZRES) / -Extent.cx;
  126.     long yscale = 100L * defSize.cy *
  127.       dc.GetDeviceCaps(VERTSIZE)/dc.GetDeviceCaps(VERTRES) / -Extent.cy;
  128.     long scale = min(xscale, yscale);
  129.     return TSize(
  130.       int(long(-Extent.cx)*scale*dc.GetDeviceCaps(HORZRES)/dc.GetDeviceCaps(HORZSIZE) / 100),
  131.       int(long(-Extent.cy)*scale*dc.GetDeviceCaps(VERTRES)/dc.GetDeviceCaps(VERTSIZE) / 100)
  132.     );
  133.   }
  134. }
  135.  
  136. //
  137. // Play this metafile onto a dc, possibly using a default size if this 
  138. // metafile doesn't have one. Does not save dc state.
  139. //
  140. BOOL
  141. TMetaFilePict::PlayOnto(TDC& dc, const TSize& defSize) const
  142. {
  143.   // Set target dc's mapping mode to this metafile's if there is one
  144.   //
  145.   if (Mm)        
  146.     dc.SetMapMode(Mm);
  147.  
  148.   // Set the viewport extent to the size that the metafile wil play to
  149.   //
  150.   if ((Mm == MM_ISOTROPIC || Mm == MM_ANISOTROPIC) && Extent.cx && Extent.cy)
  151.     dc.SetViewportExt(CalcPlaySize(dc, defSize));
  152.  
  153.   return ::PlayMetaFile(dc, *this);
  154. }
  155.  
  156. //
  157. // Move this metafile to the clipboard inside of a metafilepict struct.
  158. // Ownership of the metafilepict as well as the metafile is passed to the
  159. // clipboard.
  160. //
  161. void
  162. TMetaFilePict::ToClipboard(TClipboard& clipboard, unsigned mapMode,
  163.                            const TSize& extent)
  164. {
  165.  
  166.   HGLOBAL hmfp = ::GlobalAlloc(GMEM_MOVEABLE, sizeof(METAFILEPICT));
  167.   if (!hmfp)
  168.     THROW( TXOutOfMemory() );
  169.   METAFILEPICT far* mfp = (METAFILEPICT far*)::GlobalLock(hmfp);
  170.   mfp->mm = mapMode;
  171.   mfp->xExt = extent.cx;
  172.   mfp->yExt = extent.cy;
  173.   mfp->hMF = (HMETAFILE)Handle;
  174.   ::GlobalUnlock(hmfp);
  175.   
  176.   clipboard.SetClipboardData(CF_METAFILEPICT, hmfp);
  177.   ShouldDelete = FALSE;
  178. }
  179.