summaryrefslogtreecommitdiff
path: root/cd/src/gdk/cdgdkclp.c
blob: 3eabc5419a3262bcc45a630ac6d63a8955a56bb1 (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
/** \file
 * \brief Gdk Clipboard Driver
 *
 * See Copyright Notice in cd.h
 */

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

#include <gtk/gtk.h>

#include "cd.h"
#include "cd_private.h"
#include "cdclipbd.h"
#include "cdmf.h"
#include "cdmf_private.h"


static void cdkillcanvas(cdCtxCanvas *ctxcanvas)
{
  char* buffer;
  long dwSize;
  FILE* file;
  char filename[10240];
  cdCanvasMF* mfcanvas = (cdCanvasMF*)ctxcanvas;
  GtkClipboard *clipboard = (GtkClipboard*)mfcanvas->data;

  /* guardar antes de remover o canvas */
  strcpy(filename, mfcanvas->filename);

  cdkillcanvasMF(mfcanvas);

  file = fopen(filename, "r");
  fseek(file, 0, SEEK_END);
  dwSize = ftell(file); 
  fseek(file, 0, SEEK_SET);

  buffer = (char*)malloc(dwSize);
  fread(buffer, dwSize, 1, file);

  fclose(file);

  remove(filename);

  gtk_clipboard_store(clipboard);
}

static int cdplay(cdCanvas* canvas, int xmin, int xmax, int ymin, int ymax, void *data)
{
  char filename[1024]; 
  int dwSize;
  FILE* file;
  GdkAtom* buffer;

  gtk_clipboard_wait_for_targets((GtkClipboard*)data, &buffer, &dwSize);
  if(!buffer)
    return CD_ERROR;

  tmpnam(filename);
  file = fopen(filename, "w");
  fwrite(buffer, dwSize, 1, file);
  fclose(file);

  cdCanvasPlay(canvas, CD_METAFILE, xmin, xmax, ymin, ymax, filename);

  remove(filename);

  g_free(buffer);

  return CD_OK;
}

static void cdcreatecanvas(cdCanvas* canvas, void *data)
{
  char tmpPath[512];
  char* str = (char*)data;
  GtkClipboard* clp = NULL;

  /* Init parameters */
  if (str == NULL) 
    return;

  sscanf(str, "%p", &clp); 

  if (!clp)
    return;

  str = strstr(str, " ");
  if (!str)
    return;

  str++;
  tmpnam(tmpPath);

  strcat(tmpPath, " ");
  strcat(tmpPath, str);

  cdcreatecanvasMF(canvas, str);
  if (!canvas->ctxcanvas)
    return;

  {
    cdCanvasMF* mfcanvas = (cdCanvasMF*)canvas->ctxcanvas;
    mfcanvas->data = clp;
  }
}

static void cdinittable(cdCanvas* canvas)
{
  cdinittableMF(canvas);
  canvas->cxKillCanvas = cdkillcanvas;
}


static cdContext cdClipboardContext =
{
  CD_CAP_ALL & ~(CD_CAP_GETIMAGERGB | CD_CAP_IMAGESRV | CD_CAP_FONTDIM | CD_CAP_TEXTSIZE ),  /* same as CD_MF */
  0,
  cdcreatecanvas,  
  cdinittable,
  cdplay,          
  NULL
};

cdContext* cdContextClipboard(void)
{
  return &cdClipboardContext;
}