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