summaryrefslogtreecommitdiff
path: root/src/gdiplus/cdwclpp.cpp
blob: 2d1c7e670bafe21e8881f02f195946fb97e5e3c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/** \file
 * \brief Windows GDI+ Clipboard Driver
 *
 * See Copyright Notice in cd.h
 */

#include "cdwinp.h"
#include "cdclipbd.h"
#include "cdmf.h"
#include "cdemf.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "cdmf_private.h"


static void cdkillcanvasCLIPBDMF (cdCtxCanvas* ctxcanvas)
{
  HANDLE Handle, hFile;
  char* buffer;
  DWORD dwSize, nBytesRead;
  char filename[10240];
  cdCanvasMF* mfcanvas = (cdCanvasMF*)ctxcanvas;

  /* guardar antes de remover o canvas */
  strcpy(filename, mfcanvas->filename);
  
  OpenClipboard(NULL);
  EmptyClipboard();        
  
  cdkillcanvasMF(mfcanvas); /* this will close the file */
  
  hFile = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, NULL);
  dwSize = GetFileSize (hFile, NULL) ; 
  
  Handle = GlobalAlloc(GMEM_MOVEABLE, dwSize+1);
  buffer = (char*)GlobalLock(Handle);
  ReadFile(hFile, buffer, dwSize, &nBytesRead, NULL);
  buffer[dwSize] = 0;
  GlobalUnlock(Handle);
  
  CloseHandle(hFile);
  
  SetClipboardData(CF_TEXT, Handle);
  
  CloseClipboard();
}

static void cdkillcanvas (cdCtxCanvas* ctxcanvas)
{
  cdwpKillCanvas(ctxcanvas);
  
  OpenClipboard(NULL);
  EmptyClipboard();
  
  if (ctxcanvas->wtype == CDW_EMF)
  {
    HENHMETAFILE hEmf = ctxcanvas->metafile->GetHENHMETAFILE();
    SetClipboardData(CF_ENHMETAFILE, hEmf);
    delete ctxcanvas->metafile;
  }
  else
  {
    HBITMAP hBitmap;
    ctxcanvas->bitmap->GetHBITMAP(ctxcanvas->bg, &hBitmap);
    SetClipboardData(CF_BITMAP, hBitmap);
    delete ctxcanvas->bitmap;
  }
  
  CloseClipboard();
  
  delete ctxcanvas;
}

/*
%F cdCreateCanvas para Clipboard
O DC pode ser um WMF ou um BITMAP.
*/
static void cdcreatecanvas(cdCanvas* canvas, void *data)
{
  char* strsize = (char*)data;
  int w = 0, h = 0, wtype = CDW_EMF; /* default clipboard type */
  double res = 0; /* used only for BMP */
  Metafile* metafile = NULL;
  Bitmap* bitmap = NULL;
  Graphics* graphics;
  
  /* Inicializa parametros */
  if (strsize == NULL) 
    return;
  
  if (strstr(strsize, "-b") != NULL)
    wtype = CDW_BMP;
  else if (strstr(strsize, "-m") != NULL)
    wtype = -1; /* CD METAFILE */
  
  if (wtype != -1)
  {
    sscanf(strsize,"%dx%d %g",&w, &h, &res); 
    if (w == 0 || h == 0)
      return;
  }

  if (wtype == CDW_EMF)
  {
    HDC ScreenDC = GetDC(NULL);
    /* LOGPIXELS can not be used for EMF */
    canvas->xres = (double)GetDeviceCaps(ScreenDC, HORZRES) / (double)GetDeviceCaps(ScreenDC, HORZSIZE);
    canvas->yres = (double)GetDeviceCaps(ScreenDC, VERTRES) / (double)GetDeviceCaps(ScreenDC, VERTSIZE);

    Rect frameRect(0, 0, (int)(100 * w / canvas->xres), (int)(100 * h / canvas->yres));

    metafile = new Metafile(ScreenDC, frameRect, MetafileFrameUnitGdi, EmfTypeEmfPlusDual, NULL);
    ReleaseDC(NULL, ScreenDC);

    if (!metafile)
      return;

    graphics = new Graphics(metafile);
  }
  else if (wtype == CDW_BMP)
  {
    if (!res)
    {
      HDC ScreenDC = GetDC(NULL);
      canvas->xres = (double)GetDeviceCaps(ScreenDC, HORZRES) / (double)GetDeviceCaps(ScreenDC, HORZSIZE);
      canvas->yres = (double)GetDeviceCaps(ScreenDC, VERTRES) / (double)GetDeviceCaps(ScreenDC, VERTSIZE);
      ReleaseDC(NULL, ScreenDC);
    }
    else
    {
      canvas->xres = res;
      canvas->yres = res;
    }
    
    bitmap = new Bitmap(w, h, PixelFormat24bppRGB);
    if (!bitmap)
      return;
    
    bitmap->SetResolution((REAL)(canvas->xres*25.4), (REAL)(canvas->xres*25.4));

    graphics = new Graphics(bitmap);
  }
  
  if (wtype == -1)
  {
    char filename[10240]; 
    char str[10240];
    
    if (!cdStrTmpFileName(filename))
      return;
    
    sprintf(str, "%s %s", filename, strsize);
    cdcreatecanvasMF(canvas, str);
  }                    
  else
  {
    cdCtxCanvas* ctxcanvas = cdwpCreateCanvas(canvas, graphics, wtype);

    canvas->w = w;
    canvas->h = h;
    canvas->bpp = 24;

    if (wtype == CDW_BMP)
      ctxcanvas->bitmap = bitmap;
    else
      ctxcanvas->metafile = metafile;
  }
}

static void cdinittable(cdCanvas* canvas)
{
  if (canvas->invert_yaxis == 0) /* a simple way to distinguish MF from WIN */
  {
    cdinittableMF(canvas);
    canvas->cxKillCanvas = cdkillcanvasCLIPBDMF;
  }
  else
  {
    cdwpInitTable(canvas);
    canvas->cxKillCanvas = cdkillcanvas;
  }
}

static cdContext cdClipboardContext =
{
  CD_CAP_ALL & ~(CD_CAP_CLEAR | CD_CAP_FLUSH | CD_CAP_YAXIS | CD_CAP_PLAY |
                 CD_CAP_IMAGERGBA | CD_CAP_GETIMAGERGB | CD_CAP_IMAGESRV ),
  1,
  cdcreatecanvas,  
  cdinittable,
  NULL,          
  NULL
};

extern "C" {
cdContext* cdContextClipboardPlus(void)
{
  return &cdClipboardContext;
}
}