1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        wx/msw/enhmeta.h
3 // Purpose:     wxEnhMetaFile class for Win32
4 // Author:      Vadim Zeitlin
5 // Modified by:
6 // Created:     13.01.00
7 // Copyright:   (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence:     wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef _WX_MSW_ENHMETA_H_
12 #define _WX_MSW_ENHMETA_H_
13 
14 #include "wx/defs.h"
15 
16 #if wxUSE_ENH_METAFILE
17 
18 #include "wx/dc.h"
19 #include "wx/gdiobj.h"
20 
21 #if wxUSE_DATAOBJ
22     #include "wx/dataobj.h"
23 #endif
24 
25 // ----------------------------------------------------------------------------
26 // wxEnhMetaFile: encapsulation of Win32 HENHMETAFILE
27 // ----------------------------------------------------------------------------
28 
29 class WXDLLIMPEXP_CORE wxEnhMetaFile : public wxGDIObject
30 {
31 public:
m_filename(file)32     wxEnhMetaFile(const wxString& file = wxEmptyString) : m_filename(file)
33         { Init(); }
wxEnhMetaFile(const wxEnhMetaFile & metafile)34     wxEnhMetaFile(const wxEnhMetaFile& metafile) : wxGDIObject()
35         { Init(); Assign(metafile); }
36     wxEnhMetaFile& operator=(const wxEnhMetaFile& metafile)
37         { Free(); Assign(metafile); return *this; }
38 
~wxEnhMetaFile()39     virtual ~wxEnhMetaFile()
40         { Free(); }
41 
42     // display the picture stored in the metafile on the given DC
43     bool Play(wxDC *dc, wxRect *rectBound = NULL);
44 
45     // accessors
IsOk()46     virtual bool IsOk() const wxOVERRIDE { return m_hMF != NULL; }
47 
48     wxSize GetSize() const;
GetWidth()49     int GetWidth() const { return GetSize().x; }
GetHeight()50     int GetHeight() const { return GetSize().y; }
51 
GetFileName()52     const wxString& GetFileName() const { return m_filename; }
53 
54     // copy the metafile to the clipboard: the width and height parameters are
55     // for backwards compatibility (with wxMetaFile) only, they are ignored by
56     // this method
57     bool SetClipboard(int width = 0, int height = 0);
58 
59     // Detach the HENHMETAFILE from this object, i.e. don't delete the handle
60     // in the dtor -- the caller is now responsible for doing this, e.g. using
61     // Free() method below.
Detach()62     WXHANDLE Detach() { WXHANDLE h = m_hMF; m_hMF = NULL; return h; }
63 
64     // Destroy the given HENHMETAFILE object.
65     static void Free(WXHANDLE handle);
66 
67     // implementation
GetHENHMETAFILE()68     WXHANDLE GetHENHMETAFILE() const { return m_hMF; }
SetHENHMETAFILE(WXHANDLE hMF)69     void SetHENHMETAFILE(WXHANDLE hMF) { Free(); m_hMF = hMF; }
70 
71 protected:
72     void Init();
Free()73     void Free() { Free(m_hMF); }
74     void Assign(const wxEnhMetaFile& mf);
75 
76     // we don't use these functions (but probably should) but have to implement
77     // them as they're pure virtual in the base class
78     virtual wxGDIRefData *CreateGDIRefData() const wxOVERRIDE;
79     virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const wxOVERRIDE;
80 
81 private:
82     wxString m_filename;
83     WXHANDLE m_hMF;
84 
85     wxDECLARE_DYNAMIC_CLASS(wxEnhMetaFile);
86 };
87 
88 // ----------------------------------------------------------------------------
89 // wxEnhMetaFileDC: allows to create a wxEnhMetaFile
90 // ----------------------------------------------------------------------------
91 
92 class WXDLLIMPEXP_CORE wxEnhMetaFileDC : public wxDC
93 {
94 public:
95     // the ctor parameters specify the filename (empty for memory metafiles),
96     // the metafile picture size and the optional description/comment
97     wxEnhMetaFileDC(const wxString& filename = wxEmptyString,
98                     int width = 0, int height = 0,
99                     const wxString& description = wxEmptyString);
100 
101     // as above, but takes reference DC as first argument to take resolution,
102     // size, font metrics etc. from
103     explicit
104     wxEnhMetaFileDC(const wxDC& referenceDC,
105                     const wxString& filename = wxEmptyString,
106                     int width = 0, int height = 0,
107                     const wxString& description = wxEmptyString);
108 
109     // obtain a pointer to the new metafile (caller should delete it)
110     wxEnhMetaFile *Close();
111 
112 private:
113     wxDECLARE_DYNAMIC_CLASS_NO_COPY(wxEnhMetaFileDC);
114 };
115 
116 #if wxUSE_DATAOBJ
117 
118 // ----------------------------------------------------------------------------
119 // wxEnhMetaFileDataObject is a specialization of wxDataObject for enh metafile
120 // ----------------------------------------------------------------------------
121 
122 // notice that we want to support both CF_METAFILEPICT and CF_ENHMETAFILE and
123 // so we derive from wxDataObject and not from wxDataObjectSimple
124 class WXDLLIMPEXP_CORE wxEnhMetaFileDataObject : public wxDataObject
125 {
126 public:
127     // ctors
wxEnhMetaFileDataObject()128     wxEnhMetaFileDataObject() { }
wxEnhMetaFileDataObject(const wxEnhMetaFile & metafile)129     wxEnhMetaFileDataObject(const wxEnhMetaFile& metafile)
130         : m_metafile(metafile) { }
131 
132     // virtual functions which you may override if you want to provide data on
133     // demand only - otherwise, the trivial default versions will be used
SetMetafile(const wxEnhMetaFile & metafile)134     virtual void SetMetafile(const wxEnhMetaFile& metafile)
135         { m_metafile = metafile; }
GetMetafile()136     virtual wxEnhMetaFile GetMetafile() const
137         { return m_metafile; }
138 
139     // implement base class pure virtuals
140     virtual wxDataFormat GetPreferredFormat(Direction dir) const wxOVERRIDE;
141     virtual size_t GetFormatCount(Direction dir) const wxOVERRIDE;
142     virtual void GetAllFormats(wxDataFormat *formats, Direction dir) const wxOVERRIDE;
143     virtual size_t GetDataSize(const wxDataFormat& format) const wxOVERRIDE;
144     virtual bool GetDataHere(const wxDataFormat& format, void *buf) const wxOVERRIDE;
145     virtual bool SetData(const wxDataFormat& format, size_t len,
146                          const void *buf) wxOVERRIDE;
147 
148 protected:
149     wxEnhMetaFile m_metafile;
150 
151     wxDECLARE_NO_COPY_CLASS(wxEnhMetaFileDataObject);
152 };
153 
154 
155 // ----------------------------------------------------------------------------
156 // wxEnhMetaFileSimpleDataObject does derive from wxDataObjectSimple which
157 // makes it more convenient to use (it can be used with wxDataObjectComposite)
158 // at the price of not supoprting any more CF_METAFILEPICT but only
159 // CF_ENHMETAFILE
160 // ----------------------------------------------------------------------------
161 
162 class WXDLLIMPEXP_CORE wxEnhMetaFileSimpleDataObject : public wxDataObjectSimple
163 {
164 public:
165     // ctors
wxEnhMetaFileSimpleDataObject()166     wxEnhMetaFileSimpleDataObject() : wxDataObjectSimple(wxDF_ENHMETAFILE) { }
wxEnhMetaFileSimpleDataObject(const wxEnhMetaFile & metafile)167     wxEnhMetaFileSimpleDataObject(const wxEnhMetaFile& metafile)
168         : wxDataObjectSimple(wxDF_ENHMETAFILE), m_metafile(metafile) { }
169 
170     // virtual functions which you may override if you want to provide data on
171     // demand only - otherwise, the trivial default versions will be used
SetEnhMetafile(const wxEnhMetaFile & metafile)172     virtual void SetEnhMetafile(const wxEnhMetaFile& metafile)
173         { m_metafile = metafile; }
GetEnhMetafile()174     virtual wxEnhMetaFile GetEnhMetafile() const
175         { return m_metafile; }
176 
177     // implement base class pure virtuals
178     virtual size_t GetDataSize() const wxOVERRIDE;
179     virtual bool GetDataHere(void *buf) const wxOVERRIDE;
180     virtual bool SetData(size_t len, const void *buf) wxOVERRIDE;
181 
GetDataSize(const wxDataFormat & WXUNUSED (format))182     virtual size_t GetDataSize(const wxDataFormat& WXUNUSED(format)) const wxOVERRIDE
183         { return GetDataSize(); }
GetDataHere(const wxDataFormat & WXUNUSED (format),void * buf)184     virtual bool GetDataHere(const wxDataFormat& WXUNUSED(format),
185                              void *buf) const wxOVERRIDE
186         { return GetDataHere(buf); }
SetData(const wxDataFormat & WXUNUSED (format),size_t len,const void * buf)187     virtual bool SetData(const wxDataFormat& WXUNUSED(format),
188                          size_t len, const void *buf) wxOVERRIDE
189         { return SetData(len, buf); }
190 
191 protected:
192     wxEnhMetaFile m_metafile;
193 
194     wxDECLARE_NO_COPY_CLASS(wxEnhMetaFileSimpleDataObject);
195 };
196 
197 #endif // wxUSE_DATAOBJ
198 
199 #endif // wxUSE_ENH_METAFILE
200 
201 #endif // _WX_MSW_ENHMETA_H_
202