]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/metafile.cpp
pragma and prec-header patch applied
[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 wxMacPortSetter helper( dc ) ;
125 PicHandle pict = (PicHandle) GetHMETAFILE() ;
126 DrawPicture( pict , &(**pict).picFrame ) ;
127 }
128 return TRUE;
129 }
130
131 wxSize wxMetaFile::GetSize() const
132 {
133 wxSize size = wxDefaultSize ;
134 if ( Ok() )
135 {
136 PicHandle pict = (PicHandle) GetHMETAFILE() ;
137 Rect &r = (**pict).picFrame ;
138 size.x = r.right - r.left ;
139 size.y = r.bottom - r.top ;
140 }
141
142 return size;
143 }
144
145 /*
146 * Metafile device context
147 *
148 */
149
150 // New constructor that takes origin and extent. If you use this, don't
151 // give origin/extent arguments to wxMakeMetaFilePlaceable.
152
153 wxMetaFileDC::wxMetaFileDC(const wxString& filename ,
154 int width , int height ,
155 const wxString& WXUNUSED(description) )
156 {
157 wxASSERT_MSG( width == 0 || height == 0 , _T("no arbitration of metafilesize supported") ) ;
158 wxASSERT_MSG( filename.IsEmpty() , _T("no file based metafile support yet")) ;
159
160 m_metaFile = new wxMetaFile(filename) ;
161 Rect r={0,0,height,width} ;
162
163 RectRgn( (RgnHandle) m_macBoundaryClipRgn , &r ) ;
164 CopyRgn( (RgnHandle) m_macBoundaryClipRgn , (RgnHandle) m_macCurrentClipRgn ) ;
165
166 m_metaFile->SetHMETAFILE( OpenPicture( &r ) ) ;
167 ::GetPort( (GrafPtr*) &m_macPort ) ;
168 m_ok = TRUE ;
169
170 SetMapMode(wxMM_TEXT);
171 }
172
173 wxMetaFileDC::~wxMetaFileDC()
174 {
175 }
176
177 void wxMetaFileDC::DoGetSize(int *width, int *height) const
178 {
179 wxCHECK_RET( m_metaFile , _T("GetSize() doesn't work without a metafile") );
180
181 wxSize sz = m_metaFile->GetSize() ;
182 if (width) (*width) = sz.x;
183 if (height) (*height) = sz.y;
184 }
185
186 wxMetaFile *wxMetaFileDC::Close()
187 {
188 ClosePicture() ;
189 return m_metaFile;
190 }
191
192 #if wxUSE_DATAOBJ
193 size_t wxMetafileDataObject::GetDataSize() const
194 {
195 return GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() ) ;
196 }
197
198 bool wxMetafileDataObject::GetDataHere(void *buf) const
199 {
200 memcpy( buf , (*(PicHandle)(*((wxMetafile*)&m_metafile)).GetHMETAFILE()) ,
201 GetHandleSize( (Handle) (*((wxMetafile*)&m_metafile)).GetHMETAFILE() ) ) ;
202 return true ;
203 }
204
205 bool wxMetafileDataObject::SetData(size_t len, const void *buf)
206 {
207 Handle handle = NewHandle( len ) ;
208 SetHandleSize( handle , len ) ;
209 memcpy( *handle , buf , len ) ;
210 m_metafile.SetHMETAFILE( handle ) ;
211 return true ;
212 }
213 #endif
214
215 #endif