OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
metafile.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of TMetaFilePict class
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/metafile.h>
10#include <owl/gdiobjec.h>
11#include <owl/clipboar.h>
12#include <owl/except.h>
13#include <owl/file.h>
14
15#if defined(__BORLANDC__)
16# pragma option -w-ccc // Disable "Condition is always true/false"
17#endif
18
19using namespace std;
20
21namespace owl {
22
24DIAG_DECLARE_GROUP(OwlGDI); // General GDI diagnostic group
25
26
27/// Constant for checking validity of a METAFILEHEADER
28//
29const uint32 TMetaFilePict::MFHeaderKey = 0x9AC6CDD7L;
30
31// header description
32static TBinField MetaFileHeaderField[]={
33 {varLONG, 4, 1}, /* DWORD key */
34 {varSHORT,2, 1}, /* WORD hmf unused (0) */
35 {varSHORT,2, 4}, /* RECT16 bbox; // bounding rectangle */
36 {varSHORT,2, 1}, /* WORD inch; // units per inch */
37 {varLONG, 4, 1}, /* DWORD reserved; // unused (0) */
38 {varSHORT,2, 1}, /* WORD checksum; // XOR of previous fields */
39 {varEnd,0,0}
40};
41
42//
43/// Creates a TMetaFilePict object using the given handle argument.
44//
53
54//
55/// Creates a TMetaFilePict object for the metafile stored in the named file.
56//
58:
59 Extent(0,0),
60 Placeable(false)
61{
62 TFile file;
63
65 file.ReadStruct(&MFHeader, MetaFileHeaderField, boLittle_Endian);
66
67 // Determine whether this file begins with a valid METAFILEHEADER
68 // struct and load the file accordingly.
69 //
70 if ((MFHeader.key == MFHeaderKey)
71 && (MFHeader.checksum == CalcHeaderChecksum(MFHeader)))
72 {
73 // Determine size of metafile data
74 //
75 TFileStatus status;
76 file.GetStatus(status);
77 long size = status.size - sizeof(METAFILEHEADER);
78
79 // Convert the data to a metafile
80 //
82 if (!hMem)
85 file.Read((void *)pMem, size);
86
88
91
92 // Remember that this metafile comes with a placeable header
93 // (in other words, MFHeader holds valid data)
94 //
95 if (Handle)
96 Placeable = true;
97
98 // Placeable metafiles are meant to be played back in
99 // the unconstrained mapping mode. The header tells
100 // the desired size and aspect ratio.
101 //
102 Mm = MM_ANISOTROPIC;
103
104 // Set metafile dimensions according to info in header
105 //
106 Extent.cx = MFHeader.bbox.right - MFHeader.bbox.left;
107 Extent.cy = MFHeader.bbox.bottom - MFHeader.bbox.top;
108
109 // Convert dimensions to HIMETRIC units (.001 cm)
110 //
111 Extent.cx = MulDiv(Extent.cx, 2540, MFHeader.inch);
112 Extent.cy = MulDiv(Extent.cy, 2540, MFHeader.inch);
113 }
114 else {
115 Mm = MM_ANISOTROPIC;
116 Handle = ::GetMetaFile(filename.c_str());
117 }
118 ShouldDelete = true;
119 file.Close();
120 CheckValid();
121}
122
123//
124/// Constructs a TMetaFilePict that represents the metafilepict on the clipboard.
125/// Should be copied if metafile needs to be kept.
126//
128:
129 Extent(0,0),
130 Placeable(false)
131{
132 HGLOBAL hmfp = ::GetClipboardData(CF_METAFILEPICT);
134
135 if (mfp) {
136 Mm = mfp->mm;
137 Extent = TSize(mfp->xExt, mfp->yExt);
138 Handle = mfp->hMF;
140 }
141 else
142 Handle = nullptr;
143
144 ShouldDelete = false;
145 CheckValid();
146}
147
148
149//
150/// Creates a TMetaFilePict object for the memory-based metafile specified by data.
151/// The data buffer must hold a metafile of length size bytes.
152//
154:
156 Extent(0,0),
157 Placeable(false)
158{
159 CheckValid();
160 Mm = MM_ANISOTROPIC;
161}
162
163
164//
165/// Copies the metafile src to the named file. If filename is 0 (the default), the
166/// metafile is copied to a memory-based metafile.
167//
169:
171 Extent(src.Extent),
172 Placeable(false)
173{
174 CheckValid();
175 Mm = src.Mm;
176}
177
178//
179/// String-aware overload
180/// If the filename is empty, the metafile is copied to a memory-based metafile.
181//
183:
185 Extent(src.Extent),
186 Placeable(false)
187{
188 CheckValid();
189 Mm = src.Mm;
190}
191
192//
193/// Deletes the metafile.
194//
200
201//
202/// Reads the fields in the file header structure of a placeable
203/// metafile and computes a checksum value for verifying the
204/// header's integrity.
205//
206uint16
208{
209 LPWORD lpw; // pointer to words in header
210 uint16 checksum = 0;
211
212 for (lpw = (LPWORD)&header;
213 lpw < (LPWORD)&header.checksum;
214 ++lpw)
215 {
216 // XOR current checksum with next field
217 //
218 checksum ^= *lpw;
219 }
220
221 return checksum;
222}
223
224
225//
226/// Retrieves the contents of the metafile associated with this object
227/// and copies them (up to size bytes) to the data buffer. If data is nonzero and
228/// the call succeeds, the actual number of bytes copied is returned. If data is 0,
229/// a successful call returns the number of bytes required by the buffer. A return
230/// value of 0 always indicates a failure.
231//
232uint32
234{
235 return ::GetMetaFileBitsEx(HMETAFILE(Handle), size, (LPBYTE)data);
236}
237
238
239//
240/// Calculates target play size based on info from the metafilepict (if any)
241/// and default target size as necessary
242//
243TSize
245{
246 // Given a fixed mapping mode, return precalculated extents
247 //
248 if (Mm != MM_ISOTROPIC && Mm != MM_ANISOTROPIC) {
249 WARNX(OwlGDI, (Extent.X() == 0) || (Extent.Y() == 0), 0, "Constrained metafile extent == 0");
250 return Extent; // Assumes extents were calculated correctly.
251 }
252
253 //
254 // Metafile uses partially constrained or unconstrained mapping mode
255 //
256
257 // If no extent info given, then use defaults
258 //
259 if (Extent.X() == 0) {
260 WARNX(OwlGDI, (defSize.X() == 0) || (defSize.Y() == 0), 0, "Metafile using default extent == 0");
261 return defSize;
262 }
263
264 // Use positive extents scaled to 0.01mm units
265 //
266 else if (Extent.cx > 0) {
267 return TSize(
268 int(long(Extent.cx)*dc.GetDeviceCaps(HORZRES)/dc.GetDeviceCaps(HORZSIZE)/100),
269 int(long(Extent.cy)*dc.GetDeviceCaps(VERTRES)/dc.GetDeviceCaps(VERTSIZE)/100)
270 );
271 }
272 // Use negative extents scaled to 0.01mm units w/ aspect ratio scaling
273 //
274 else {
275 long xscale = 100L * defSize.cx *
276 dc.GetDeviceCaps(HORZSIZE)/dc.GetDeviceCaps(HORZRES) / -Extent.cx;
277 long yscale = 100L * defSize.cy *
278 dc.GetDeviceCaps(VERTSIZE)/dc.GetDeviceCaps(VERTRES) / -Extent.cy;
279 long scale = std::min(xscale, yscale);
280 return TSize(
281 int(long(-Extent.cx)*scale*dc.GetDeviceCaps(HORZRES)/dc.GetDeviceCaps(HORZSIZE) / 100),
282 int(long(-Extent.cy)*scale*dc.GetDeviceCaps(VERTRES)/dc.GetDeviceCaps(VERTSIZE) / 100)
283 );
284 }
285}
286
287//
288/// Plays this metafile onto a dc, possibly using a default size if this
289/// metafile doesn't have one. Does not save dc state.
290//
291bool
293{
294 // Set target dc's mapping mode to this metafile's if there is one
295 //
296 if (Mm)
297 dc.SetMapMode(Mm);
298
299 // Set the viewport extent to the area that the metafile will fill
300 //
301 if ((Mm == MM_ISOTROPIC || Mm == MM_ANISOTROPIC) && Extent.cx && Extent.cy)
302// if (Mm == MM_ISOTROPIC || Mm == MM_ANISOTROPIC)
304
305 return ::PlayMetaFile(dc, *this);
306}
307
308//
309/// Moves this metafile to the clipboard inside of a metafilepict struct.
310/// Ownership of the metafilepict as well as the metafile is passed to the
311/// clipboard.
312//
313void
315 const TSize& extent)
316{
317
319 if (!hmfp)
322 mfp->mm = mapMode;
323 mfp->xExt = extent.cx;
324 mfp->yExt = extent.cy;
325 mfp->hMF = (HMETAFILE)Handle;
327
329 ShouldDelete = false;
330}
331
332
333//
334/// Alias for an existing enhanced metafile handle.
335//
341
342//
343/// Creates an enhanced metafile from an external file.
344//
350
351//
352/// Copies a metafile.
353/// If the filename is 0 (the default), then the metafile is copied to a memory-based metafile.
354//
361
362//
363/// String-aware overload
364/// If the filename is empty, the metafile is copied to a memory-based metafile.
365//
369
370//
371/// Creates metafile from buffer.
372//
374:
376{
377}
378
379//
380/// Destroys the enhanced metafile picture.
381//
387
388//
389/// Plays the metafile onto a device context.
390//
391bool
393{
394 return ::PlayEnhMetaFile(dc, *this, rect);
395}
396
397
398
399} // OWL namespace
400/* ========================================================================== */
401
#define WARNX(group, condition, level, message)
Definition checks.h:277
#define DIAG_DECLARE_GROUP(group)
Definition checks.h:404
The clipboard class encapsulates the methods for the clipboard object of Windows.
Definition clipboar.h:32
HANDLE SetClipboardData(uint format, HANDLE handle)
Copy the data onto the clipboard in the format.
Definition clipboar.h:282
TDC is the root class for GDI DC wrappers.
Definition dc.h:64
virtual int GetDeviceCaps(int index) const
Used under WIN3.1 or later, GetDeviceCaps returns capability information about this DC.
Definition dc.cpp:373
virtual int SetMapMode(int mode)
Sets the current window mapping mode of this DC to mode.
Definition dc.cpp:431
virtual bool SetViewportExt(const TSize &extent, TSize *oldExtent=nullptr)
Sets this DC's viewport x- and y-extents to the given extent values.
Definition dc.cpp:474
TEnhMetaFilePict is a class that encapsulates the enhanced metafile.
Definition metafile.h:148
TEnhMetaFilePict(HENHMETAFILE handle, TAutoDelete autoDelete)
Alias for an existing enhanced metafile handle.
Definition metafile.cpp:336
bool PlayOnto(TDC &dc, const TRect *rect) const
Plays the metafile onto a device context.
Definition metafile.cpp:392
~TEnhMetaFilePict()
Destroys the enhanced metafile picture.
Definition metafile.cpp:382
The TFile class encapsulates standard file characteristics and operations.
Definition file.h:120
@ ReadOnly
Definition file.h:130
@ OpenExisting
Opens the file. The function fails if the file does not exist.
Definition file.h:151
@ PermRead
Subsequent open operations on the object will succeed only if read access is requested.
Definition file.h:138
Root and abstract class for Windows object wrappers.
Definition gdibase.h:79
void CheckValid(uint resId=IDS_GDIFAILURE)
Definition gdibase.cpp:49
bool ShouldDelete
Should object delete GDI handle in dtor?
Definition gdibase.h:82
HANDLE Handle
GDI handle of this object.
Definition gdibase.h:81
TMetaFilePict is a support class used with TMetaFileDC to simplify metafile operations,...
Definition metafile.h:80
uint16 CalcHeaderChecksum(const METAFILEHEADER &mfHeader)
BGM consider adding these, as in TDib: BGM Write(ostream& os, bool writeFileHeader = false); BGM Writ...
Definition metafile.cpp:207
TMetaFilePict(HMETAFILE handle, TAutoDelete autoDelete)
Creates a TMetaFilePict object using the given handle argument.
Definition metafile.cpp:45
~TMetaFilePict()
Deletes the metafile.
Definition metafile.cpp:195
void ToClipboard(TClipboard &clipboard, uint mapMode=MM_ANISOTROPIC, const TSize &extent=TSize(0, 0))
Moves this metafile to the clipboard inside of a metafilepict struct.
Definition metafile.cpp:314
bool PlayOnto(TDC &dc, const TSize &defSize) const
Plays this metafile onto a dc, possibly using a default size if this metafile doesn't have one.
Definition metafile.cpp:292
TSize CalcPlaySize(TDC &dc, const TSize &defSize) const
Calculates target play size based on info from the metafilepict (if any) and default target size as n...
Definition metafile.cpp:244
uint32 GetMetaFileBitsEx(uint size, void *data)
Retrieves the contents of the metafile associated with this object and copies them (up to size bytes)...
Definition metafile.cpp:233
TRect is a mathematical class derived from tagRect.
Definition geometry.h:308
The tagSIZE struct is defined as.
Definition geometry.h:234
int Y() const
Returns the height.
Definition geometry.h:1478
int X() const
Returns the width.
Definition geometry.h:1469
Definition of classes for clipboard Encapsulation.
Definition of abstract GDI object class and derived classes.
TAutoDelete
Flag for Handle ctors to control Handle deletion in dtor.
Definition gdibase.h:70
@ AutoDelete
Definition gdibase.h:70
@ NoAutoDelete
Definition gdibase.h:70
static void Raise()
Construct a TXOutOfMemory exception from scratch, and throw it.
Definition except.cpp:301
@ boLittle_Endian
LSB at lowest address: Intel //.
Definition file.h:49
@ varLONG
Definition file.h:74
@ varEnd
Definition file.h:69
@ varSHORT
Definition file.h:73
Definition of TMetafilePict, a MetaFile wrapper class.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
unsigned long uint32
Definition number.h:34
OWL_DIAGINFO
Definition animctrl.cpp:14
unsigned short uint16
Definition number.h:33
std::string tstring
Definition defs.h:79
unsigned int uint
Definition number.h:25
ObjectWindows exception class & function definitions.
WORD checksum
XOR of previous fields.
Definition metafile.h:63
RECT16 bbox
bounding rectangle
Definition metafile.h:60
WORD inch
units per inch
Definition metafile.h:61
DWORD key
identifies file type
Definition metafile.h:55
int16 left
Definition metafile.h:48
int16 top
Definition metafile.h:49
int16 right
Definition metafile.h:50
int16 bottom
Definition metafile.h:51
uint32 size
Definition file.h:39