OSDN Git Service

20021009版ソース
[seraphyscrtools/SeraphyScriptTools.git] / Layer.cpp
1 // Layer.cpp : CLayer \82Ì\83C\83\93\83v\83\8a\83\81\83\93\83e\81[\83V\83\87\83\93
2 #include "stdafx.h"
3 #include "SeraphyScriptTools.h"
4 #include "Layer.h"
5 #include "generic.h"
6
7 /////////////////////////////////////////////////////////////////////////////
8 // CLayer
9
10 STDMETHODIMP CLayer::InterfaceSupportsErrorInfo(REFIID riid)
11 {
12         static const IID* arr[] = 
13         {
14                 &IID_ILayer
15         };
16         for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
17         {
18                 if (IsEqualGUID(*arr[i],riid))
19                         return S_OK;
20         }
21         return S_FALSE;
22 }
23
24 void CLayer::Draw(CDC dc)
25 {
26         // \83h\83\8d\81[\83C\83\93\83O\83f\81[\83^\81[\82Ì\95`\89æ
27         if(m_bVisible){
28                 // \83y\83\93\81A\83u\83\89\83V\82Ì\90Ý\92è
29                 HPEN hPen = CreatePen(PS_SOLID,0,COLORREF(m_dwColor));
30                 HBRUSH hBrush = CreateSolidBrush(COLORREF(m_dwFillColor));
31                 HPEN hOldPen = (HPEN)SelectObject(dc.m_hDC,hPen);
32                 HBRUSH hOldBrush = (HBRUSH)SelectObject(dc.m_hDC,hBrush);
33                 COLORREF oldColor = SetTextColor(dc.m_hDC,COLORREF(m_dwFontColor));
34                 // \83t\83H\83\93\83g\82Ì\91I\91ð
35                 int fntsiz = m_nFontSize;
36                 if(m_bFontTextMappingMode){
37                         //DPtoLP(hdc,&fz,1);
38                         HWND hWnd = GetDesktopWindow();
39                         HDC hdc = GetDC(hWnd);
40                         long height_mm = GetDeviceCaps(hdc,VERTSIZE) * 10;
41                         long height_px = GetDeviceCaps(hdc,VERTRES);
42                         fntsiz = MulDiv(fntsiz,height_mm,height_px);
43                         ReleaseDC(hWnd,hdc);
44                 }
45                 HFONT hFont = CreateFont(fntsiz,0,0,0,0,false,false,false,SHIFTJIS_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FF_DONTCARE,m_szFontName);
46                 HFONT hOldFont = NULL;
47                 if(hFont){
48                         hOldFont = (HFONT)SelectObject(dc.m_hDC,hFont);
49                 }
50                 // \95`\89æ\83f\81[\83^
51                 EnterCriticalSection(&m_objDrawingDataProtection);
52                 list<drawdata*>::iterator p = m_lstDraw.begin();
53                 while(p != m_lstDraw.end()){
54                         (*p)->Draw(dc);
55                         p++;
56                 }
57                 LeaveCriticalSection(&m_objDrawingDataProtection);
58                 // \83I\83u\83W\83F\83N\83g\82Ì\89ð\95ú
59                 SelectObject(dc.m_hDC,hOldPen);
60                 SelectObject(dc.m_hDC,hOldBrush);
61                 DeleteObject(hPen);
62                 DeleteObject(hBrush);
63                 SetTextColor(dc.m_hDC,oldColor);
64                 if(hOldFont){
65                         SelectObject(dc.m_hDC,hOldFont);
66                         DeleteObject(hFont);
67                 }
68         }
69 }
70
71 void CLayer::FinalRelease()
72 {
73         ATLTRACE("CLeayer::FinalRelease\r\n");
74         // \95`\89æ\83f\81[\83^\81[\82Ì\94j\8aü
75         Clear();
76         // \83N\83\8a\83e\83B\83J\83\8b\83Z\83N\83V\83\87\83\93\82Ì\89ð\95ú
77         DeleteCriticalSection(&m_objDrawingDataProtection);
78 }
79
80 STDMETHODIMP CLayer::Text(VARIANT x,VARIANT y,VARIANT text)
81 {
82         CComVariant str;
83         if(str.ChangeType(VT_BSTR,&text) != S_OK){
84                 return DISP_E_TYPEMISMATCH;
85         }
86         long lx = GetMappedValue(x);
87         long ly = GetMappedValue(y);
88         int len = SysStringByteLen(str.bstrVal);
89         LPSTR pBuf = new CHAR[len+1];
90         WideCharToMultiByte(GetACP(),0,str.bstrVal,len,pBuf,len,0,0);
91         pBuf[len] = 0;
92         AddDrawData(new textdata(lx,ly,pBuf,m_bTextMappingMode));
93         delete[]pBuf;
94         return S_OK;
95 }
96
97 STDMETHODIMP CLayer::TextBox(VARIANT sx, VARIANT sy, VARIANT ex, VARIANT ey, VARIANT text, VARIANT fmt)
98 {
99         CComVariant str,cfmt;
100         long lsx = GetMappedValue(sx);
101         long lsy = GetMappedValue(sy);
102         long lex = GetMappedValue(ex);
103         long ley = GetMappedValue(ey);
104         UINT ufmt = 0;
105         if(cfmt.ChangeType(VT_I4,&fmt) == S_OK){
106                 ufmt = cfmt.lVal;
107         }
108         if(str.ChangeType(VT_BSTR,&text) != S_OK){
109                 return DISP_E_TYPEMISMATCH;
110         }
111         int len = SysStringByteLen(str.bstrVal);
112         LPSTR pBuf = new CHAR[len+1];
113         int cnt = WideCharToMultiByte(GetACP(),0,str.bstrVal,-1,pBuf,len,0,0);
114         pBuf[cnt-1] = 0;
115         AddDrawData(new textboxdata(lsx,lsy,lex,ley,pBuf,ufmt,m_bTextMappingMode));
116         delete[]pBuf;
117         return S_OK;
118 }
119
120 STDMETHODIMP CLayer::Circle(VARIANT x, VARIANT y, VARIANT radius)
121 {
122         long lx = GetMappedValue(x);
123         long ly = GetMappedValue(y);
124         long lr = GetMappedValue(radius);
125         AddDrawData(new circledata(lx,ly,lr,m_bTextMappingMode));
126         return S_OK;
127 }
128
129 STDMETHODIMP CLayer::Line(VARIANT sx, VARIANT sy, VARIANT ex, VARIANT ey)
130 {
131         long lsx = GetMappedValue(sx);
132         long lsy = GetMappedValue(sy);
133         long lex = GetMappedValue(ex);
134         long ley = GetMappedValue(ey);
135         AddDrawData(new linedata(lsx,lsy,lex,ley,m_bTextMappingMode));
136         return S_OK;
137 }
138
139 STDMETHODIMP CLayer::Box(VARIANT sx, VARIANT sy, VARIANT ex, VARIANT ey)
140 {
141         long lsx = GetMappedValue(sx);
142         long lsy = GetMappedValue(sy);
143         long lex = GetMappedValue(ex);
144         long ley = GetMappedValue(ey);
145         AddDrawData(new boxdata(lsx,lsy,lex,ley,m_bTextMappingMode));
146         return S_OK;
147 }
148
149 STDMETHODIMP CLayer::Arc(VARIANT x1, VARIANT y1, VARIANT x2, VARIANT y2, VARIANT sx, VARIANT sy, VARIANT ex, VARIANT ey)
150 {
151         long lx1 = GetMappedValue(x1);
152         long ly1 = GetMappedValue(y1);
153         long lx2 = GetMappedValue(x2);
154         long ly2 = GetMappedValue(y2);
155         long lsx = GetMappedValue(sx);
156         long lsy = GetMappedValue(sy);
157         long lex = GetMappedValue(ex);
158         long ley = GetMappedValue(ey);
159         AddDrawData(new arcdata(lx1,ly1,lx2,ly2,lsx,lsy,lex,ley,m_bTextMappingMode));
160         return S_OK;
161 }
162
163 STDMETHODIMP CLayer::Picture(VARIANT unk, VARIANT x, VARIANT y, VARIANT w, VARIANT h)
164 {
165         CComVariant cUnk;
166         IPicture* pPicture = NULL;
167         if( cUnk.ChangeType(VT_UNKNOWN,&unk) != S_OK ||
168                 cUnk.punkVal->QueryInterface(IID_IPicture,(void**)&pPicture) != S_OK){
169                 return DISP_E_TYPEMISMATCH;
170         }
171         long lx = GetMappedValue(x);
172         long ly = GetMappedValue(y);
173         int width = -1;
174         if(w.vt != VT_NULL && w.vt != VT_ERROR && w.vt != VT_EMPTY){
175                 width = GetMappedValue(w,-1);
176         }
177         int height = -1;
178         if(h.vt != VT_NULL && h.vt != VT_ERROR && h.vt != VT_EMPTY){
179                 height = GetMappedValue(h,-1);
180         }
181         AddDrawData(new picturedata(pPicture,lx,ly,width,height,m_bTextMappingMode));
182         pPicture->Release();
183         return S_OK;
184 }
185
186 STDMETHODIMP CLayer::FillBox(VARIANT sx, VARIANT sy, VARIANT ex, VARIANT ey)
187 {
188         long lsx = GetMappedValue(sx);
189         long lsy = GetMappedValue(sy);
190         long lex = GetMappedValue(ex);
191         long ley = GetMappedValue(ey);
192         AddDrawData(new fillboxdata(lsx,lsy,lex,ley,m_bTextMappingMode));
193         return S_OK;
194 }
195
196 STDMETHODIMP CLayer::FillCircle(VARIANT x, VARIANT y, VARIANT radius)
197 {
198         long lx = GetMappedValue(x);
199         long ly = GetMappedValue(y);
200         long lr = GetMappedValue(radius);
201         AddDrawData(new fillcircledata(lx,ly,lr,m_bTextMappingMode));
202         return S_OK;
203 }
204
205 STDMETHODIMP CLayer::FillArc(VARIANT x1, VARIANT y1, VARIANT x2, VARIANT y2, VARIANT sx, VARIANT sy, VARIANT ex, VARIANT ey)
206 {
207         long lx1 = GetMappedValue(x1);
208         long ly1 = GetMappedValue(y1);
209         long lx2 = GetMappedValue(x2);
210         long ly2 = GetMappedValue(y2);
211         long lsx = GetMappedValue(sx);
212         long lsy = GetMappedValue(sy);
213         long lex = GetMappedValue(ex);
214         long ley = GetMappedValue(ey);
215         AddDrawData(new fillarcdata(lx1,ly1,lx2,ly2,lsx,lsy,lex,ley,m_bTextMappingMode));
216         return S_OK;
217 }
218
219 STDMETHODIMP CLayer::FillRBox(VARIANT sx, VARIANT sy, VARIANT ex, VARIANT ey, VARIANT w, VARIANT h)
220 {
221         long lsx = GetMappedValue(sx);
222         long lsy = GetMappedValue(sy);
223         long lex = GetMappedValue(ex);
224         long ley = GetMappedValue(ey);
225         long lw  = GetMappedValue(w);
226         long lh  = GetMappedValue(h);
227         AddDrawData(new fillrboxdata(lsx,lsy,lex,ley,lw,lh,m_bTextMappingMode));
228         return S_OK;
229 }
230
231 STDMETHODIMP CLayer::Polygon(VARIANT cx, VARIANT cy, VARIANT arrayPt)
232 {
233         long lx = GetMappedValue(cx);
234         long ly = GetMappedValue(cy);
235         VARTYPE vt = VT_EMPTY;
236         SAFEARRAY* pArray = GetArrayFromVariant(arrayPt,&vt);
237         if(!pArray || vt != VT_VARIANT){
238                 return DISP_E_TYPEMISMATCH;
239         }
240         POINT* pPt = NULL;
241         long lb = 0;
242         long ub = 0;
243         long lb0 = 0;
244         long ub0 = 0;
245         int dm = SafeArrayGetDim(pArray);
246         SafeArrayGetLBound(pArray,1,&lb0); // \8d\91¤\82Ì\93Y\82¦\8e\9a
247         SafeArrayGetUBound(pArray,1,&ub0);
248         SafeArrayGetLBound(pArray,2,&lb);  // \89E\91¤\82Ì\93Y\82¦\8e\9a
249         SafeArrayGetUBound(pArray,2,&ub);
250         if(dm == 2 && lb == 0 && lb0 == 0 && ub0 == 2 && ub > lb && ub > 2){
251                 // \94z\97ñ\82Ì\8e\9f\8c³\81A\94Í\88Í\82ª\97L\8cø\82Å\82 \82é\81B
252                 // \91½\8ap\8c`\82ð\8dì\90¬\82·\82é\82½\82ß\82É\82Í\92¸\93_\82Í3\82Â\88È\8fã\95K\97v\81B
253                 pPt = new POINT[ub + 1];
254                 long idx[2];
255                 for(int cnt = 0 ; cnt <= ub ; cnt++){
256                         // VARIANT\82Ì\94z\97ñ\82Å\82 \82é
257                         CComVariant tmpX,tmpY;
258                         idx[1] = cnt;
259                         idx[0] = 0;
260                         SafeArrayGetElement(pArray,idx,&tmpX);
261                         idx[0] = 1;
262                         SafeArrayGetElement(pArray,idx,&tmpY);
263                         pPt[cnt].x = GetMappedValue(tmpX) + lx;
264                         pPt[cnt].y = GetMappedValue(tmpY) + ly;
265                 }
266         }
267         if(!pPt){
268                 // \94z\97ñ\82Ì\8eí\97Þ\82ª\82È\82ñ\82¾\82©\95ª\82©\82ç\82È\82¢\81B
269                 ErrorInfo(IDS_ERR_NEED2DIM);
270                 return DISP_E_EXCEPTION;
271         }
272         AddDrawData(new polygondata(pPt,ub,m_bTextMappingMode));
273         return S_OK;
274 }
275
276 STDMETHODIMP CLayer::Clear()
277 {
278         // \83h\83\8d\81[\83C\83\93\83O\83f\81[\83^\81[\82Ì\94j\8aü
279         EnterCriticalSection(&m_objDrawingDataProtection);
280         list<drawdata*>::iterator p = m_lstDraw.begin();
281         while(p != m_lstDraw.end()){
282                 delete *p;
283                 p = m_lstDraw.erase(p);
284         }
285         LeaveCriticalSection(&m_objDrawingDataProtection);
286         return S_OK;
287 }
288
289 void CLayer::AddDrawData(drawdata *pDrawData)
290 {
291         // \83f\81[\83^\81[\82Ì\92Ç\89Á
292         EnterCriticalSection(&m_objDrawingDataProtection);
293         m_lstDraw.push_back(pDrawData);
294         LeaveCriticalSection(&m_objDrawingDataProtection);
295 }
296
297 STDMETHODIMP CLayer::get_Color(long *pVal)
298 {
299         *pVal = m_dwColor;
300         return S_OK;
301 }
302
303 STDMETHODIMP CLayer::put_Color(long newVal)
304 {
305         m_dwColor = newVal;
306         return S_OK;
307 }
308
309
310 STDMETHODIMP CLayer::get_FontColor(long *pVal)
311 {
312         *pVal = m_dwFontColor;
313         return S_OK;
314 }
315
316 STDMETHODIMP CLayer::put_FontColor(long newVal)
317 {
318         m_dwFontColor = newVal;
319         return S_OK;
320 }
321
322 STDMETHODIMP CLayer::get_Visible(BOOL *pVal)
323 {
324         *pVal = m_bVisible;
325         return S_OK;
326 }
327
328 STDMETHODIMP CLayer::put_Visible(BOOL newVal)
329 {
330         m_bVisible = newVal;
331         return S_OK;
332 }
333
334 STDMETHODIMP CLayer::get_FontName(BSTR *pVal)
335 {
336         WCHAR wmes[MAX_PATH];
337         MultiByteToWideChar(GetACP(),0,m_szFontName,-1,wmes,MAX_PATH);
338         *pVal = SysAllocString(wmes);
339         return S_OK;
340 }
341
342 STDMETHODIMP CLayer::put_FontName(BSTR newVal)
343 {
344         WideCharToMultiByte(GetACP(),0,newVal,-1,m_szFontName,MAX_PATH,NULL,NULL);
345         return S_OK;
346 }
347
348 STDMETHODIMP CLayer::get_FillColor(long *pVal)
349 {
350         *pVal = m_dwFillColor;
351         return S_OK;
352 }
353
354 STDMETHODIMP CLayer::put_FillColor(long newVal)
355 {
356         m_dwFillColor = newVal;
357         return S_OK;
358 }
359
360 STDMETHODIMP CLayer::get_FontSize(short *pVal)
361 {
362         *pVal = (short)m_nFontSize;
363         return S_OK;
364 }
365
366 STDMETHODIMP CLayer::put_FontSize(short newVal)
367 {
368         m_nFontSize = (int)newVal;
369         m_bFontTextMappingMode = m_bTextMappingMode;
370         return S_OK;
371 }
372
373
374 STDMETHODIMP CLayer::SetMappingMode(VARIANT mode)
375 {
376         CComVariant varMode;
377         if(varMode.ChangeType(VT_I2,&mode) == S_OK){
378                 m_bTextMappingMode = varMode.iVal;
379         }
380         return S_OK;
381 }
382
383 long CLayer::GetMappedValue(VARIANT var, long def)
384 {
385         CComVariant varTmp;
386         if(m_bTextMappingMode){
387                 // \83s\83N\83Z\83\8b\92P\88Ê\82Ì\8dÀ\95W\8ew\92è
388                 if(varTmp.ChangeType(VT_I4,&var) == S_OK){
389                         return varTmp.lVal;
390                 }
391         }
392         else{
393                 // \83~\83\8a\92P\88Ê\82Ì\8dÀ\95W\8ew\92è (1\92P\88Ê 0.1mm)
394                 if(varTmp.ChangeType(VT_R8,&var) == S_OK){
395                         return (long)(varTmp.dblVal * 10);
396                 }
397         }
398         return def;
399 }