]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/metafile.cpp
354e4e5b1f471455b334026bce0fb9c4a9595752
[wxWidgets.git] / src / mac / carbon / metafile.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: metafile.cpp
3 // Purpose: wxMetaFile, wxMetaFileDC etc. These classes are optional.
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "metafile.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_METAFILE
24
25 #ifndef WX_PRECOMP
26 #include "wx/utils.h"
27 #include "wx/app.h"
28 #endif
29
30 #include "wx/metafile.h"
31 #include "wx/clipbrd.h"
32
33 #include "wx/mac/private.h"
34
35 #include <stdio.h>
36 #include <string.h>
37
38 extern bool wxClipboardIsOpen;
39
40 #if !USE_SHARED_LIBRARY
41 IMPLEMENT_DYNAMIC_CLASS(wxMetafile, wxObject)
42 IMPLEMENT_ABSTRACT_CLASS(wxMetafileDC, wxDC)
43 #endif
44
45 /*
46 * Metafiles
47 * Currently, the only purpose for making a metafile is to put
48 * it on the clipboard.
49 */
50
51 wxMetafileRefData::wxMetafileRefData(void)
52 {
53 m_metafile = 0;
54 }
55
56 wxMetafileRefData::~wxMetafileRefData(void)
57 {
58 if (m_metafile)
59 {
60 KillPicture( (PicHandle) m_metafile ) ;
61 m_metafile = 0;
62 }
63 }
64
65 wxMetaFile::wxMetaFile(const wxString& file)
66 {
67 m_refData = new wxMetafileRefData;
68
69
70 M_METAFILEDATA->m_metafile = 0;
71 wxASSERT_MSG( file.IsEmpty() , wxT("no file based metafile support yet") ) ;
72 /*
73 if (!file.IsNull() && (file.Cmp("") == 0))
74 M_METAFILEDATA->m_metafile = (WXHANDLE) GetMetaFile(file);
75 */
76 }
77
78 wxMetaFile::~wxMetaFile()
79 {
80 }
81
82 bool wxMetaFile::SetClipboard(int width, int height)
83 {
84 #if wxUSE_DRAG_AND_DROP
85 //TODO finishi this port , we need the data obj first
86 if (!m_refData)
87 return FALSE;
88
89 bool alreadyOpen=wxTheClipboard->IsOpened() ;
90 if (!alreadyOpen)
91 {
92 wxTheClipboard->Open();
93 wxTheClipboard->Clear();
94 }
95 wxDataObject *data =
96 new wxMetafileDataObject( *this) ;
97 bool success = wxTheClipboard->SetData(data);
98 if (!alreadyOpen)
99 wxTheClipboard->Close();
100 return (bool) success;
101 #endif
102 return TRUE ;
103 }
104
105 void wxMetafile::SetHMETAFILE(WXHMETAFILE mf)
106 {
107 if (!m_refData)
108 m_refData = new wxMetafileRefData;
109 if ( M_METAFILEDATA->m_metafile )
110 KillPicture( (PicHandle) M_METAFILEDATA->m_metafile ) ;
111
112 M_METAFILEDATA->m_metafile = mf;
113 }
114
115 bool wxMetaFile::Play(wxDC *dc)
116 {
117 if (!m_refData)
118 return FALSE;
119
120 if (!dc->Ok() )
121 return FALSE;
122
123 {
124 #if wxMAC_USE_CORE_GRAPHICS
125 #else
126 wxMacPortSetter helper( dc ) ;
127 PicHandle pict = (PicHandle) GetHMETAFILE() ;
128 DrawPicture( pict , &(**pict).picFrame ) ;
129 #endif
130 }
131 return TRUE;
132 }
133
134 wxSize wxMetaFile::GetSize() const
135 {
136 wxSize size = wxDefaultSize ;
137 if ( Ok() )
138 {
139 PicHandle pict = (PicHandle) GetHMETAFILE() ;
140 Rect &r = (**pict).picFrame ;
141 size.x = r.right - r.left ;
142 size.y = r.bottom - r.top ;
143 }
144
145 return size;
146 }
147
148 /*
149 * Metafile device context
150 *
151 */
152
153 // New constructor that takes origin and extent. If you use this, don't
154 // give origin/extent arguments to wxMakeMetaFilePlaceable.
155
156 wxMetaFileDC::wxMetaFileDC(const wxString& filename ,
157 int width , int height ,
158 const wxString& WXUNUSED(description) )
159 {
160 wxASSERT_MSG( width == 0 || height == 0 , _T("no arbitration of metafilesize supported") ) ;
161 wxASSERT_MSG( filename.IsEmpty() , _T("no file based metafile support yet")) ;
162
163 m_metaFile = new wxMetaFile(filename) ;
164 #if wxMAC_USE_CORE_GRAPHICS
165 #else
166 Rect r={0,0,height,width} ;
167
168 RectRgn( (RgnHandle) m_macBoundaryClipRgn , &r ) ;
169 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
170
171 m_metaFile->SetHMETAFILE( (WXHMETAFILE) OpenPicture( &r ) ) ;
172 ::GetPort( (GrafPtr*) &m_macPort ) ;
173 m_ok = TRUE ;
174 #endif
175 SetMapMode(wxMM_TEXT);
176 }
177
178 wxMetaFileDC::~wxMetaFileDC()
179 {
180 }
181
182 void wxMetaFileDC::DoGetSize(int *width, int *height) const
183 {
184 wxCHECK_RET( m_metaFile , _T("GetSize() doesn't work without a metafile") );
185
186 wxSize sz = m_metaFile->GetSize() ;
187 if (width) (*width) = sz.x;
188 if (height) (*height) = sz.y;
189 }
190
191 wxMetaFile *wxMetaFileDC::Close()
192 {
193 ClosePicture() ;
194 return m_metaFile;
195 }
196
197 #if wxUSE_DATAOBJ
198 size_t wxMetafileDataObject::GetDataSize() const
199 {
200 return GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() ) ;
201 }
202
203 bool wxMetafileDataObject::GetDataHere(void *buf) const
204 {
205 memcpy( buf , (*(PicHandle)(*((wxMetafile*)&m_metafile)).GetHMETAFILE()) ,
206 GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() ) ) ;
207 return true ;
208 }
209
210 bool wxMetafileDataObject::SetData(size_t len, const void *buf)
211 {
212 Handle handle = NewHandle( len ) ;
213 SetHandleSize( handle , len ) ;
214 memcpy( *handle , buf , len ) ;
215 m_metafile.SetHMETAFILE( (WXHMETAFILE) handle ) ;
216 return true ;
217 }
218 #endif
219
220 #endif