summaryrefslogtreecommitdiff
path: root/src/Fl_Copy_Surface.cxx
blob: f05c6edc81cb6bf5281bd2cac91643530adb1dc2 (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
204
205
206
207
208
209
210
211
//
// "$Id$"
//
// Copy-to-clipboard code for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2016 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file.  If this
// file is missing or damaged, see the license at:
//
//     http://www.fltk.org/COPYING.php
//
// Please report all bugs and problems on the following page:
//
//     http://www.fltk.org/str.php
//

#include "config_lib.h"
#include <FL/Fl_Copy_Surface.H>
#include <FL/Fl.H>
#ifdef FL_CFG_GFX_QUARTZ
#include "drivers/Quartz/Fl_Quartz_Graphics_Driver.h"
#endif
#ifdef FL_CFG_GFX_XLIB
#include "drivers/Xlib/Fl_Translated_Xlib_Graphics_Driver.H"
#endif
#ifdef FL_CFG_GFX_GDI
#include "drivers/GDI/Fl_GDI_Graphics_Driver.h"
#endif


#if defined(__APPLE__) // PORTME: Fl_Surface_Driver - platform copy surface

#elif defined(WIN32)


#else

#endif


/** Constructor.
 \param w and \param h are the width and height of the clipboard surface
 in pixels where drawing will occur.
 */
Fl_Copy_Surface::Fl_Copy_Surface(int w, int h) :  Fl_Widget_Surface(NULL)
{
  width = w;
  height = h;
#ifdef __APPLE__ // PORTME: Fl_Surface_Driver - platform copy surface
  driver(new Fl_Quartz_Graphics_Driver);
  prepare_copy_pdf_and_tiff(w, h);
#elif defined(WIN32)
  driver(new Fl_Translated_GDI_Graphics_Driver);
  oldgc = (HDC)Fl_Surface_Device::surface()->driver()->gc();
  // exact computation of factor from screen units to EnhMetaFile units (0.01 mm)
  HDC hdc = GetDC(NULL);
  int hmm = GetDeviceCaps(hdc, HORZSIZE);
  int hdots = GetDeviceCaps(hdc, HORZRES);
  int vmm = GetDeviceCaps(hdc, VERTSIZE);
  int vdots = GetDeviceCaps(hdc, VERTRES);
  ReleaseDC(NULL, hdc);
  float factorw = (100.f * hmm) / hdots;
  float factorh = (100.f * vmm) / vdots;

  RECT rect; rect.left = 0; rect.top = 0; rect.right = (LONG)(w * factorw); rect.bottom = (LONG)(h * factorh);
  gc = CreateEnhMetaFile (NULL, NULL, &rect, NULL);
  if (gc != NULL) {
    SetTextAlign(gc, TA_BASELINE|TA_LEFT);
    SetBkMode(gc, TRANSPARENT);
  }
#elif defined(FL_PORTING)
#  pragma message "FL_PORTING: initialize members of Fl_Copy_Surface"
#else // Xlib
  driver(new Fl_Translated_Xlib_Graphics_Driver());
  Fl::first_window()->make_current();
  oldwindow = fl_xid(Fl::first_window());
  xid = fl_create_offscreen(w,h);
  Fl_Surface_Device *present_surface = Fl_Surface_Device::surface();
  set_current();
  fl_color(FL_WHITE);
  fl_rectf(0, 0, w, h);
  present_surface->set_current();
#endif
}

/** Destructor.
 */
Fl_Copy_Surface::~Fl_Copy_Surface()
{
#ifdef __APPLE__ // PORTME: Fl_Surface_Driver - platform copy surface
  complete_copy_pdf_and_tiff();
#elif defined(WIN32)
  if (oldgc == (HDC)Fl_Surface_Device::surface()->driver()->gc()) oldgc = NULL;
  HENHMETAFILE hmf = CloseEnhMetaFile (gc);
  if ( hmf != NULL ) {
    if ( OpenClipboard (NULL) ){
      EmptyClipboard ();
      SetClipboardData (CF_ENHMETAFILE, hmf);
      CloseClipboard ();
    }
    DeleteEnhMetaFile(hmf);
  }
  DeleteDC(gc);
  Fl_Surface_Device::surface()->driver()->gc(oldgc);
#elif defined(FL_PORTING)
#  pragma message "FL_PORTING: free resources in destructor of Fl_Copy_Surface"
#else // Xlib
  fl_pop_clip();
  unsigned char *data = fl_read_image(NULL,0,0,width,height,0);
  fl_window = oldwindow;
  _ss->set_current();
  Fl::copy_image(data,width,height,1);
  delete[] data;
  fl_delete_offscreen(xid);
#endif
}


void Fl_Copy_Surface::set_current()
{
#if defined(__APPLE__) || defined(WIN32) // PORTME: Fl_Surface_Driver - platform copy surface
  driver()->gc(gc);
  fl_window = (Window)1;
  Fl_Surface_Device::set_current();
#elif defined(FL_PORTING)
#  pragma message "FL_PORTING: implement Fl_Copy_Surface::set_current"
#else
  fl_window=xid;
  _ss = Fl_Surface_Device::surface();
  Fl_Surface_Device::set_current();
  fl_push_no_clip();
#endif
}


#if defined(__APPLE__) // PORTME: Fl_Surface_Driver - platform copy surface

size_t Fl_Copy_Surface::MyPutBytes(void* info, const void* buffer, size_t count)
  {
  CFDataAppendBytes ((CFMutableDataRef) info, (const UInt8 *)buffer, count);
  return count;
}

void Fl_Copy_Surface::init_PDF_context(int w, int h)
{
  CGRect bounds = CGRectMake(0, 0, w, h );
  pdfdata = CFDataCreateMutable(NULL, 0);
  CGDataConsumerRef myconsumer;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
  if (&CGDataConsumerCreateWithCFData != NULL) {
    myconsumer = CGDataConsumerCreateWithCFData(pdfdata); // 10.4
  }
  else
#endif
  {
    static CGDataConsumerCallbacks callbacks = { Fl_Copy_Surface::MyPutBytes, NULL };
    myconsumer = CGDataConsumerCreate ((void*) pdfdata, &callbacks);
  }
  gc = CGPDFContextCreate (myconsumer, &bounds, NULL);
  CGDataConsumerRelease (myconsumer);
}

void Fl_Copy_Surface::prepare_copy_pdf_and_tiff(int w, int h)
{
  init_PDF_context(w, h);
  if (gc == NULL) return;
  CGRect bounds = CGRectMake(0, 0, w, h );
  CGContextBeginPage (gc, &bounds);
  CGContextTranslateCTM(gc, 0, h);
  CGContextScaleCTM(gc, 1.0f, -1.0f);
  CGContextSaveGState(gc);
}

void Fl_Copy_Surface::translate(int x, int y) {
  CGContextRef gc = (CGContextRef)driver()->gc();
  CGContextRestoreGState(gc);
  CGContextSaveGState(gc);
  CGContextTranslateCTM(gc, x, y);
  CGContextSaveGState(gc);
}

void Fl_Copy_Surface::untranslate() {
  CGContextRestoreGState((CGContextRef)driver()->gc());
}

#elif defined(WIN32)

void Fl_Copy_Surface::translate(int x, int y) {
  ((Fl_Translated_GDI_Graphics_Driver*)driver())->translate_all(x, y);
}

void Fl_Copy_Surface::untranslate() {
  ((Fl_Translated_GDI_Graphics_Driver*)driver())->untranslate_all();
}

#else
void Fl_Copy_Surface::translate(int x, int y) {
  ((Fl_Translated_Xlib_Graphics_Driver*)driver())->translate_all(x, y);
}

void Fl_Copy_Surface::untranslate() {
  ((Fl_Translated_Xlib_Graphics_Driver*)driver())->untranslate_all();
}

#endif  // __APPLE__ // PORTME: Fl_Surface_Driver - platform copy surface

//
// End of "$Id$".
//