summaryrefslogtreecommitdiff
path: root/test/cdtest/drivers.c
blob: dd233311395c9d8bf8cd885492750d41c6b3d648 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*=========================================================================*/
/* DRIVERS.C - 10/02/95                                                    */
/* Suporte para os drivers do CD.                                          */
/*=========================================================================*/

/*- Bibliotecas padrao usadas: --------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*- Inclusao das bibliotecas IUP e CD: ------------------------------------*/
#include <iup.h>
#include <cd.h>
#include <cdiup.h>

/*- Prototypes e declaracoes do CDTest: -----------------------------------*/
#include "cdtest.h"

/*- Contexto do CDTest (declarado em CDTEST.C): ---------------------------*/
extern tCTC ctgc;

#ifdef CDTEST_WIN32
#define CLIPBOARD_WIN32
#define WMF
#define PRINTER
#endif

#define CLIPBOARD 
#define CGM
#define MF
#define PS
#define DXF
#define DGN
#define PDF
#define CDDBG
#define SVG

static int LoadCanvas(char* ctx_name, cdContext* ctx, char *filename)
{
  if (IupGetFile(filename) == 0) 
  { 
    newmetafile(filename, ctx);
    cdActivate(ctgc.iup_canvas);
    cdWriteMode(CD_REPLACE);
    cdLineStyle(CD_CONTINUOUS);
    cdLineWidth(1);
    cdBackground(CD_WHITE);
    cdBackOpacity(CD_TRANSPARENT);
    cdForeground(CD_BLACK);
    cdInteriorStyle(CD_SOLID);
    if (ctgc.stretch_play)
    {
      cdPlay(ctx, 0, ctgc.w-1, 0, ctgc.h-1, filename);
      sprintf(ctgc.status_line, "cdPlay(%s, 0, %d, 0, %d, %s)", ctx_name, ctgc.w-1, ctgc.h-1, filename);
    }
    else
    {
      cdPlay(ctx, 0, 0, 0, 0, filename);
      sprintf(ctgc.status_line, "cdPlay(%s, 0, 0, 0, 0, %s)", ctx_name, filename);
    }
    set_status();
  }

  return IUP_DEFAULT;
}

static int SaveCanvas(char* ctx_name, cdContext* ctx, char *data)
{
  cdCanvas *canvas;

  canvas = cdCreateCanvas(ctx, data);
  if (!canvas)
  {
    IupMessagef("Error!", "Could not create canvas of driver %s.", ctx_name);
    return IUP_DEFAULT;
  }

  cdActivate(canvas);

  cdPattern(10, 10, ctgc.pattern);
  cdStipple(10, 10, ctgc.stipple);
  cdInteriorStyle(CD_SOLID);

  if (ctgc.sim == 1)
    cdSimulate(CD_SIM_ALL);
  else
    cdSimulate(CD_SIM_NONE);

  putlist(canvas);

  cdKillCanvas(canvas);

  return IUP_DEFAULT;
}


/*-------------------------------------------------------------------------*/
/* Copia o conteudo do canvas para o clipboard do Windows, vetorial.       */
/*-------------------------------------------------------------------------*/
#ifdef CLIPBOARD
#include <cdclipbd.h>

static int fClipBoard(void)
{
  char data[1000];
  sprintf(data, "%dx%d %g", ctgc.w, ctgc.h, ctgc.res);
  return SaveCanvas("CD_CLIPBOARD", CD_CLIPBOARD, data);
}

/*-------------------------------------------------------------------------*/
/* Copia o conteudo do canvas para o clipboard do Windows, BitMap.         */
/*-------------------------------------------------------------------------*/
static int fClipBoardBitmap(void)
{
  char data[1000];
  sprintf(data, "%dx%d %g -b", ctgc.w, ctgc.h, ctgc.res);
  return SaveCanvas("CD_CLIPBOARD", CD_CLIPBOARD, data);
}

static int fClipBoardMetafile(void)
{
  char data[1000];
#ifdef WIN32
  sprintf(data, "%gx%g %g -m", ((double)ctgc.w)/ctgc.res, ((double)ctgc.h)/ctgc.res, ctgc.res);
#else
  sprintf(data, "%p %gx%g %g", IupGetAttribute(IupGetHandle("cnvMain"), "XDISPLAY"), ((double)ctgc.w)/ctgc.res, ((double)ctgc.h)/ctgc.res, ctgc.res);
#endif
  return SaveCanvas("CD_CLIPBOARD", CD_CLIPBOARD, data);
}

static int fClipBoardPaste(void)
{
  char* data;
  newmetafile("", CD_CLIPBOARD);
  cdActivate(ctgc.iup_canvas);
  cdWriteMode(CD_REPLACE);
  cdLineStyle(CD_CONTINUOUS);
  cdLineWidth(1);
  cdBackground(CD_WHITE);
  cdBackOpacity(CD_TRANSPARENT);
  cdForeground(CD_BLACK);
  cdInteriorStyle(CD_SOLID);
  
#ifdef WIN32
  data = "";
#else
  data = IupGetAttribute(IupGetHandle("cnvMain"), "XDISPLAY");
#endif
  
  if (ctgc.stretch_play)
  {
    cdPlay(CD_CLIPBOARD, 0, ctgc.w-1, 0, ctgc.h-1, data);
    sprintf(ctgc.status_line, "cdPlay(CD_CLIPBOARD, 0, %d, 0, %d, \"\")", ctgc.w-1, ctgc.h-1);
  }
  else
  {
    cdPlay(CD_CLIPBOARD, 0, 0, 0, 0, data);
    sprintf(ctgc.status_line, "cdPlay(CD_CLIPBOARD, 0, 0, 0, 0, \"\")");
  }
  set_status();
  return IUP_DEFAULT;
}

#endif

/*-------------------------------------------------------------------------*/
/* Copia o conteudo do canvas para um arquivo PostScript.                  */
/*-------------------------------------------------------------------------*/
#ifdef PS
#include <cdps.h>

static int fPS(void)
{
  char filename[1024]="*.ps";
  char data[1024];

  if (IupGetFile(filename)>=0) 
  { 
    sprintf(data, "%s -s%d", filename, (int)(ctgc.res * 25.4));
    return SaveCanvas("CD_PS", CD_PS, data);
  }

  return IUP_DEFAULT;
}

static int fEPS(void)
{
  char filename[1024]="*.eps";
  char data[1024];

  if (IupGetFile(filename)>=0) 
  { 
    sprintf(data, "%s -s%d -e -l0 -r0 -t0 -b0", filename, (int)(ctgc.res * 25.4));
    return SaveCanvas("CD_PS", CD_PS, data);
  }

  return IUP_DEFAULT;
}
#endif

/*-------------------------------------------------------------------------*/
/* Copia o conteudo do canvas para um arquivo SVG.                  */
/*-------------------------------------------------------------------------*/
#ifdef SVG
#include <cdsvg.h>

static int fSVG(void)
{
  char filename[1024]="*.svg";
  char data[1024];

  if (IupGetFile(filename)>=0) 
  { 
    sprintf(data, "%s -s%d", filename, (int)(ctgc.res * 25.4));
    return SaveCanvas("CD_SVG", CD_SVG, data);
  }

  return IUP_DEFAULT;
}
#endif

#ifdef PDF
#include <cdpdf.h>

static int fPDF(void)
{
  char filename[1024]="*.pdf";
  char data[1024];

  if (IupGetFile(filename)>=0) 
  { 
    sprintf(data, "%s -s%d", filename, (int)(ctgc.res * 25.4));
    return SaveCanvas("CD_PDF", CD_PDF, data);
  }

  return IUP_DEFAULT;
}
#endif

/*-------------------------------------------------------------------------*/
/* Copia o conteudo do canvas para um arquivo CGM.                         */
/*-------------------------------------------------------------------------*/
#ifdef CGM
#include <cdcgm.h>

static int fCGMb(void)
{
  char filename[1024]="*.cgm";
  char data[1000];

  if (IupGetFile(filename)>=0) 
  { 
    sprintf(data, "%s %gx%g %g", filename, ((double)ctgc.w)/ctgc.res, ((double)ctgc.h)/ctgc.res, ctgc.res);
    return SaveCanvas("CD_CGM", CD_CGM, data);
  }

  return IUP_DEFAULT;
}

static int fCGMt(void)
{
  char filename[1024]="*.cgm";
  char data[1000];

  if (IupGetFile(filename)>=0) 
  { 
    sprintf(data, "%s %gx%g %g -t", filename, ((double)ctgc.w)/ctgc.res, ((double)ctgc.h)/ctgc.res, ctgc.res);
    return SaveCanvas("CD_CGM", CD_CGM, data);
  }

  return IUP_DEFAULT;
}

static int fPlayCGM(void)
{
  char filename[1024]="*.cgm";
  return LoadCanvas("CD_CGM", CD_CGM, filename);
}

#endif

/*-------------------------------------------------------------------------*/
/* Copia o conteudo do canvas para um arquivo DXF.                         */
/*-------------------------------------------------------------------------*/
#ifdef DXF
#include <cddxf.h>

static int fDXF(void)
{
  char filename[1024]="*.dxf";
  char data[1000];

  if (IupGetFile(filename)>=0) 
  { 
    sprintf(data, "%s %gx%g %g", filename, ((double)ctgc.w)/ctgc.res, ((double)ctgc.h)/ctgc.res, ctgc.res);
    return SaveCanvas("CD_DXF", CD_DXF, data);
  }

  return IUP_DEFAULT;
}
#endif

/*-------------------------------------------------------------------------*/
/* Copia o conteudo do canvas para um arquivo DGN.                         */
/*-------------------------------------------------------------------------*/
#ifdef DGN
#include <cddgn.h>

static int fDGN(void)
{
  char filename[1024]="*.dgn";
  char data[1000];

  if (IupGetFile(filename)>=0) 
  { 
    sprintf(data, "%s %gx%g %g -sseed2d.dgn", filename, ((double)ctgc.w)/ctgc.res, ((double)ctgc.h)/ctgc.res, ctgc.res);
    return SaveCanvas("CD_DGN", CD_DGN, data);
  }

  return IUP_DEFAULT;
}
#endif

#ifdef MF
#include <cdmf.h>
/*-------------------------------------------------------------------------*/
/* Copia o conteudo do canvas para um arquivo metafile do CD.              */
/*-------------------------------------------------------------------------*/
static int fMF(void)
{
  char filename[1024]="*.mf";
  char data[1000];

  if (IupGetFile(filename)>=0) 
  { 
    sprintf(data, "%s %gx%g %g", filename, ((double)ctgc.w)/ctgc.res, ((double)ctgc.h)/ctgc.res, ctgc.res);
    return SaveCanvas("CD_METAFILE", CD_METAFILE, data);
  }

  return IUP_DEFAULT;
}

/*-------------------------------------------------------------------------*/
/* Copia o conteudo do canvas para um arquivo metafile do CD.              */
/*-------------------------------------------------------------------------*/
static int fPlayMF(void)
{
  char filename[1024]="*.mf";
  return LoadCanvas("CD_METAFILE", CD_METAFILE, filename);
}
#endif

#ifdef CDDBG
#include <cddebug.h>
/*-------------------------------------------------------------------------*/
/* Copia o conteudo do canvas para um arquivo metafile do CD.              */
/*-------------------------------------------------------------------------*/
static int fDebug(void)
{
  char filename[1024]="*.mf";
  char data[1000];

  if (IupGetFile(filename)>=0) 
  { 
    sprintf(data, "%s %gx%g %g", filename, ((double)ctgc.w)/ctgc.res, ((double)ctgc.h)/ctgc.res, ctgc.res);
    return SaveCanvas("CD_DEBUG", CD_DEBUG, data);
  }

  return IUP_DEFAULT;
}
#endif

/*-------------------------------------------------------------------------*/
/* Copia o conteudo do canvas para um arquivo WMF.                         */
/*-------------------------------------------------------------------------*/
#ifdef WMF
#include <cdwmf.h>

static int fWMF(void)
{
  char filename[1024]="*.wmf";
  char data[1000];

  if (IupGetFile(filename)>=0) 
  { 
    sprintf(data, "%s %dx%d %g", filename, ctgc.w, ctgc.h, ctgc.res);
    return SaveCanvas("CD_WMF", CD_WMF, data);
  }

  return IUP_DEFAULT;
}

static int fPlayWMF(void)
{
  char filename[1024]="*.wmf";
  return LoadCanvas("CD_WMF", CD_WMF, filename);
}

/*-------------------------------------------------------------------------*/
/* Copia o conteudo do canvas para um arquivo EMF.                         */
/*-------------------------------------------------------------------------*/
#include <cdemf.h>

static int fEMF(void)
{
  char filename[1024]="*.emf";
  char data[1000];

  if (IupGetFile(filename)>=0) 
  { 
    sprintf(data, "%s %dx%d %g", filename, ctgc.w, ctgc.h, ctgc.res);
    return SaveCanvas("CD_EMF", CD_EMF, data);
  }

  return IUP_DEFAULT;
}

static int fPlayEMF(void)
{
  char filename[1024]="*.emf";
  return LoadCanvas("CD_EMF", CD_EMF, filename);
}
#endif

/*-------------------------------------------------------------------------*/
/* Copia o conteudo do canvas para a impressora.                           */
/*-------------------------------------------------------------------------*/
#ifdef PRINTER
#include <cdprint.h>

static int fPrint(void)
{
  char *data = "CDTEST.PRN -d";
  return SaveCanvas("CD_PRINTER", CD_PRINTER, data);
}
#endif

/*-------------------------------------------------------------------------*/
/* Inicializa os menus de Save e Open.                                     */
/*-------------------------------------------------------------------------*/
void DriversInit(void)
{
#ifdef MF
  IupSetFunction("cmdMF", (Icallback) fMF);
  IupSetFunction("cmdPlayMF", (Icallback) fPlayMF);
#endif
#ifdef PS
  IupSetAttribute(IupGetHandle("itPS"), IUP_ACTIVE, IUP_YES);
  IupSetAttribute(IupGetHandle("itEPS"), IUP_ACTIVE, IUP_YES);
  IupSetFunction("cmdPS", (Icallback) fPS);
  IupSetFunction("cmdEPS", (Icallback) fEPS);
#endif
#ifdef SVG
  IupSetAttribute(IupGetHandle("itSVG"), IUP_ACTIVE, IUP_YES);
  IupSetFunction("cmdSVG", (Icallback) fSVG);
#endif
#ifdef PDF
  IupSetAttribute(IupGetHandle("itPDF"), IUP_ACTIVE, IUP_YES);
  IupSetFunction("cmdPDF", (Icallback) fPDF);
#endif
#ifdef CLIPBOARD
  IupSetAttribute(IupGetHandle("itClipBoardMetafile"), IUP_ACTIVE, IUP_YES);
  IupSetAttribute(IupGetHandle("itClipBoardPaste"), IUP_ACTIVE, IUP_YES);
  IupSetFunction("cmdClipBoardMetafile", (Icallback)fClipBoardMetafile);
  IupSetFunction("cmdClipBoardPaste", (Icallback)fClipBoardPaste);
#endif
#ifdef CLIPBOARD_WIN32
  IupSetAttribute(IupGetHandle("itClipBoardBitmap"), IUP_ACTIVE, IUP_YES);
  IupSetAttribute(IupGetHandle("itClipBoard"), IUP_ACTIVE, IUP_YES);
  IupSetFunction("cmdClipBoardBitmap", (Icallback)fClipBoardBitmap);
  IupSetFunction("cmdClipBoard", (Icallback)fClipBoard);
#endif
#ifdef DXF
  IupSetAttribute(IupGetHandle("itDXF"), IUP_ACTIVE, IUP_YES);
  IupSetFunction("cmdDXF", (Icallback) fDXF);
#endif
#ifdef DGN
  IupSetAttribute(IupGetHandle("itDGN"), IUP_ACTIVE, IUP_YES);
  IupSetFunction("cmdDGN", (Icallback) fDGN);
#endif
#ifdef CDDBG
  IupSetFunction("cmdDebug", (Icallback) fDebug);
#endif
#ifdef CGM
  IupSetAttribute(IupGetHandle("itCGMb"), IUP_ACTIVE, IUP_YES);
  IupSetFunction("cmdCGMb", (Icallback) fCGMb);
  IupSetAttribute(IupGetHandle("itCGMt"), IUP_ACTIVE, IUP_YES);
  IupSetFunction("cmdCGMt", (Icallback) fCGMt);
  IupSetAttribute(IupGetHandle("itPlayCGM"), IUP_ACTIVE, IUP_YES);
  IupSetFunction("cmdPlayCGM", (Icallback) fPlayCGM);
#endif
#ifdef WMF
  IupSetAttribute(IupGetHandle("itEMF"), IUP_ACTIVE, IUP_YES);
  IupSetAttribute(IupGetHandle("itWMF"), IUP_ACTIVE, IUP_YES);
  IupSetFunction("cmdEMF", (Icallback) fEMF);
  IupSetFunction("cmdWMF", (Icallback) fWMF);
  IupSetAttribute(IupGetHandle("itPlayEMF"), IUP_ACTIVE, IUP_YES);
  IupSetAttribute(IupGetHandle("itPlayWMF"), IUP_ACTIVE, IUP_YES);
  IupSetFunction("cmdPlayEMF", (Icallback) fPlayEMF);
  IupSetFunction("cmdPlayWMF", (Icallback) fPlayWMF);
#endif
#ifdef PRINTER
  IupSetAttribute(IupGetHandle("itPrint"), IUP_ACTIVE, IUP_YES);
  IupSetFunction("cmdPrint", (Icallback) fPrint);
#endif
}