]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dcps.cpp
New PostScript code
[wxWidgets.git] / src / gtk / dcps.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dcps.cpp
3 // Purpose: wxPostScriptDC implementation
4 // Author: Julian Smart, Robert Roebling, Markus Holzhem
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "postscrp.h"
14 #pragma implementation
15 #pragma interface
16 #endif
17
18 //-------------------------------------------------------------------------------
19 // compiler funnies
20 //-------------------------------------------------------------------------------
21
22 // For compilers that support precompilation, includes "wx.h".
23 #include "wx/wxprec.h"
24
25 #ifdef __BORLANDC__
26 #pragma hdrstop
27 #endif
28
29 #include "wx/defs.h"
30
31 #if wxUSE_POSTSCRIPT
32
33 #ifndef WX_PRECOMP
34 #include "wx/intl.h"
35 #include "wx/frame.h"
36 #include "wx/utils.h"
37 #include "wx/filedlg.h"
38 #include "wx/msgdlg.h"
39 #include "wx/app.h"
40 #include "wx/button.h"
41 #include "wx/radiobox.h"
42 #include "wx/textctrl.h"
43 #include "wx/stattext.h"
44 #include "wx/icon.h"
45 #include "wx/list.h"
46 #endif
47
48 #include "wx/postscrp.h"
49 #include "wx/dcmemory.h"
50
51 #ifdef __WXMSW__
52 #include "wx/msw/private.h"
53 #endif
54
55 #if wxUSE_IOSTREAMH
56 #include <iostream.h>
57 #include <fstream.h>
58 #else
59 #include <iostream>
60 #include <fstream>
61 # ifdef _MSC_VER
62 using namespace std;
63 # endif
64 #endif
65
66 #include <math.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <limits.h>
71 #include <assert.h>
72
73 #ifdef __WXGTK__
74
75 #include "gdk/gdkx.h" // GDK_DISPLAY
76 #include "gdk/gdkprivate.h" // XImage
77 #include <X11/Xlib.h>
78 #include <X11/Xutil.h>
79
80 #endif
81
82 #ifdef __WXMOTIF__
83 #include <X11/Xlib.h>
84 #include <X11/Xutil.h>
85 #endif
86
87 #ifdef __WXMSW__
88
89 #ifdef DrawText
90 #undef DrawText
91 #endif
92
93 #ifdef StartDoc
94 #undef StartDoc
95 #endif
96
97 #ifdef GetCharWidth
98 #undef GetCharWidth
99 #endif
100
101 #ifdef FindWindow
102 #undef FindWindow
103 #endif
104
105 #endif
106
107 //-------------------------------------------------------------------------------
108 // Postscript viewer
109 //-------------------------------------------------------------------------------
110
111 // Determine the Default Postscript Previewer
112 // available on the platform
113 #if defined(__SUN__) && defined(__XVIEW__)
114 // OpenWindow/NeWS's Postscript Previewer
115 # define PS_VIEWER_PROG "pageview"
116 #elif defined(__VMS__)
117 #define PS_VIEWER_PROG "view/format=ps/select=x_display"
118 #elif defined(__SGI__)
119 // SGI's Display Postscript Previewer
120 //# define PS_VIEWER_PROG "dps"
121 # define PS_VIEWER_PROG "xpsview"
122 #elif defined(__X__) || defined(__WXGTK__)
123 // Front-end to ghostscript
124 # define PS_VIEWER_PROG "ghostview"
125 #else
126 // Windows ghostscript/ghostview
127 # define PS_VIEWER_PROG NULL
128 #endif
129
130 //-------------------------------------------------------------------------------
131 // global data
132 //-------------------------------------------------------------------------------
133
134 wxPrintSetupData *wxThePrintSetupData = (wxPrintSetupData *) NULL;
135
136 //-----------------------------------------------------------------------------
137 // start and end of document/page
138 //-----------------------------------------------------------------------------
139
140 static const char *wxPostScriptHeaderEllipse = "\
141 /ellipsedict 8 dict def\n\
142 ellipsedict /mtrx matrix put\n\
143 /ellipse {\n\
144 ellipsedict begin\n\
145 /endangle exch def\n\
146 /startangle exch def\n\
147 /yrad exch def\n\
148 /xrad exch def\n\
149 /y exch def\n\
150 /x exch def\n\
151 /savematrix mtrx currentmatrix def\n\
152 x y translate\n\
153 xrad yrad scale\n\
154 0 0 1 startangle endangle arc\n\
155 savematrix setmatrix\n\
156 end\n\
157 } def\n\
158 ";
159
160 static const char *wxPostScriptHeaderEllipticArc= "\
161 /ellipticarcdict 8 dict def\n\
162 ellipticarcdict /mtrx matrix put\n\
163 /ellipticarc\n\
164 { ellipticarcdict begin\n\
165 /do_fill exch def\n\
166 /endangle exch def\n\
167 /startangle exch def\n\
168 /yrad exch def\n\
169 /xrad exch def \n\
170 /y exch def\n\
171 /x exch def\n\
172 /savematrix mtrx currentmatrix def\n\
173 x y translate\n\
174 xrad yrad scale\n\
175 do_fill { 0 0 moveto } if\n\
176 0 0 1 startangle endangle arc\n\
177 savematrix setmatrix\n\
178 do_fill { fill }{ stroke } ifelse\n\
179 end\n\
180 } def\n";
181
182 static const char *wxPostScriptHeaderSpline = "\
183 /DrawSplineSection {\n\
184 /y3 exch def\n\
185 /x3 exch def\n\
186 /y2 exch def\n\
187 /x2 exch def\n\
188 /y1 exch def\n\
189 /x1 exch def\n\
190 /xa x1 x2 x1 sub 0.666667 mul add def\n\
191 /ya y1 y2 y1 sub 0.666667 mul add def\n\
192 /xb x3 x2 x3 sub 0.666667 mul add def\n\
193 /yb y3 y2 y3 sub 0.666667 mul add def\n\
194 x1 y1 lineto\n\
195 xa ya xb yb x3 y3 curveto\n\
196 } def\n\
197 ";
198
199 static const char *wxPostScriptHeaderColourImage = "\
200 % define 'colorimage' if it isn't defined\n\
201 % ('colortogray' and 'mergeprocs' come from xwd2ps\n\
202 % via xgrab)\n\
203 /colorimage where % do we know about 'colorimage'?\n\
204 { pop } % yes: pop off the 'dict' returned\n\
205 { % no: define one\n\
206 /colortogray { % define an RGB->I function\n\
207 /rgbdata exch store % call input 'rgbdata'\n\
208 rgbdata length 3 idiv\n\
209 /npixls exch store\n\
210 /rgbindx 0 store\n\
211 0 1 npixls 1 sub {\n\
212 grays exch\n\
213 rgbdata rgbindx get 20 mul % Red\n\
214 rgbdata rgbindx 1 add get 32 mul % Green\n\
215 rgbdata rgbindx 2 add get 12 mul % Blue\n\
216 add add 64 idiv % I = .5G + .31R + .18B\n\
217 put\n\
218 /rgbindx rgbindx 3 add store\n\
219 } for\n\
220 grays 0 npixls getinterval\n\
221 } bind def\n\
222 \n\
223 % Utility procedure for colorimage operator.\n\
224 % This procedure takes two procedures off the\n\
225 % stack and merges them into a single procedure.\n\
226 \n\
227 /mergeprocs { % def\n\
228 dup length\n\
229 3 -1 roll\n\
230 dup\n\
231 length\n\
232 dup\n\
233 5 1 roll\n\
234 3 -1 roll\n\
235 add\n\
236 array cvx\n\
237 dup\n\
238 3 -1 roll\n\
239 0 exch\n\
240 putinterval\n\
241 dup\n\
242 4 2 roll\n\
243 putinterval\n\
244 } bind def\n\
245 \n\
246 /colorimage { % def\n\
247 pop pop % remove 'false 3' operands\n\
248 {colortogray} mergeprocs\n\
249 image\n\
250 } bind def\n\
251 } ifelse % end of 'false' case\n\
252 ";
253
254 static char wxPostScriptHeaderReencodeISO1[] =
255 "\n/reencodeISO {\n"
256 "dup dup findfont dup length dict begin\n"
257 "{ 1 index /FID ne { def }{ pop pop } ifelse } forall\n"
258 "/Encoding ISOLatin1Encoding def\n"
259 "currentdict end definefont\n"
260 "} def\n"
261 "/ISOLatin1Encoding [\n"
262 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
263 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
264 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
265 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
266 "/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright\n"
267 "/parenleft/parenright/asterisk/plus/comma/minus/period/slash\n"
268 "/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon\n"
269 "/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N\n"
270 "/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright\n"
271 "/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m\n"
272 "/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n"
273 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
274 "/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef\n"
275 "/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve\n"
276 "/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut\n";
277
278 static char wxPostScriptHeaderReencodeISO2[] =
279 "/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar\n"
280 "/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot\n"
281 "/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior\n"
282 "/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine\n"
283 "/guillemotright/onequarter/onehalf/threequarters/questiondown\n"
284 "/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla\n"
285 "/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex\n"
286 "/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis\n"
287 "/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute\n"
288 "/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis\n"
289 "/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave\n"
290 "/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex\n"
291 "/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis\n"
292 "/yacute/thorn/ydieresis\n"
293 "] def\n\n";
294
295 //-------------------------------------------------------------------------------
296 // class information
297 //-------------------------------------------------------------------------------
298
299 #if !USE_SHARED_LIBRARY
300 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptModule, wxModule)
301 IMPLEMENT_DYNAMIC_CLASS(wxPostScriptDC, wxDC)
302 IMPLEMENT_DYNAMIC_CLASS(wxPrintSetupData, wxObject)
303 IMPLEMENT_DYNAMIC_CLASS(wxPrintPaperType, wxObject)
304 #endif
305
306 //-------------------------------------------------------------------------------
307 // wxPostScriptDC
308 //-------------------------------------------------------------------------------
309
310 wxPostScriptDC::wxPostScriptDC ()
311 {
312 m_pstream = (ofstream*) NULL;
313
314 m_currentRed = 0;
315 m_currentGreen = 0;
316 m_currentBlue = 0;
317
318 m_pageNumber = 0;
319
320 m_clipping = FALSE;
321
322 m_underlinePosition = 0.0;
323 m_underlineThickness = 0.0;
324
325 m_signX = 1; // default x-axis left to right
326 m_signY = -1; // default y-axis bottom up -> top down
327 }
328
329 wxPostScriptDC::wxPostScriptDC (const wxString& file, bool interactive, wxWindow *parent)
330 {
331 m_pstream = (ofstream*) NULL;
332
333 m_currentRed = 0;
334 m_currentGreen = 0;
335 m_currentBlue = 0;
336
337 m_pageNumber = 0;
338
339 m_clipping = FALSE;
340
341 m_underlinePosition = 0.0;
342 m_underlineThickness = 0.0;
343
344 m_signX = 1; // default x-axis left to right
345 m_signY = -1; // default y-axis bottom up -> top down
346
347 Create(file, interactive, parent);
348 }
349
350 bool wxPostScriptDC::Create(const wxString& file, bool interactive, wxWindow *parent)
351 {
352 m_isInteractive = interactive;
353
354 m_title = "";
355 m_filename = file;
356
357 #ifdef __WXMSW__
358 // Can only send to file in Windows
359 wxThePrintSetupData->SetPrinterMode(PS_FILE);
360 #endif
361
362 if (m_isInteractive)
363 {
364 if ((m_ok = PrinterDialog (parent) ) == FALSE) return FALSE;
365 }
366 else
367 {
368 m_ok = TRUE;
369 }
370
371 return m_ok;
372 }
373
374 wxPostScriptDC::~wxPostScriptDC ()
375 {
376 if (m_pstream) delete m_pstream;
377 }
378
379 bool wxPostScriptDC::PrinterDialog(wxWindow *parent)
380 {
381 wxPostScriptPrintDialog dialog( parent, _("Printer Settings"), wxPoint(150, 150), wxSize(400, 400),
382 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL );
383 m_ok = (dialog.ShowModal () == wxID_OK);
384
385 if (!m_ok) return FALSE;
386
387 if ((m_filename == "") &&
388 (wxThePrintSetupData->GetPrinterMode() == PS_PREVIEW ||
389 wxThePrintSetupData->GetPrinterMode() == PS_PRINTER))
390 {
391 // steve, 05.09.94
392 #ifdef __VMS__
393 wxThePrintSetupData->SetPrinterFile("preview");
394 #else
395 // For PS_PRINTER action this depends on a Unix-style print spooler
396 // since the wx_printer_file can be destroyed during a session
397 // @@@ TODO: a Windows-style answer for non-Unix
398 char userId[256];
399 wxGetUserId (userId, sizeof (userId) / sizeof (char));
400 char tmp[256];
401 strcpy (tmp, "/tmp/preview_");
402 strcat (tmp, userId);
403 wxThePrintSetupData->SetPrinterFile(tmp);
404 #endif
405 char tmp2[256];
406 strcpy(tmp2, wxThePrintSetupData->GetPrinterFile());
407 strcat (tmp2, ".ps");
408 wxThePrintSetupData->SetPrinterFile(tmp2);
409 m_filename = tmp2;
410 }
411 else if ((m_filename == "") && (wxThePrintSetupData->GetPrinterMode() == PS_FILE))
412 {
413 char *file = wxSaveFileSelector (_("PostScript"), "ps");
414 if (!file)
415 {
416 m_ok = FALSE;
417 return FALSE;
418 }
419 wxThePrintSetupData->SetPrinterFile(file);
420 m_filename = file;
421 m_ok = TRUE;
422 }
423
424 return m_ok;
425 }
426
427 void wxPostScriptDC::SetClippingRegion (long x, long y, long w, long h)
428 {
429 if (m_clipping) return;
430
431 if (!m_pstream) return;
432
433 wxDC::SetClippingRegion( x, y, w, h );
434
435 m_clipping = TRUE;
436 *m_pstream << "gsave\n"
437 << "newpath\n"
438 << XLOG2DEV(x) << " " << YLOG2DEV(y) << " moveto\n"
439 << XLOG2DEV(x+w) << " " << YLOG2DEV(y) << " lineto\n"
440 << XLOG2DEV(x+w) << " " << YLOG2DEV(y+h) << " lineto\n"
441 << XLOG2DEV(x) << " " << YLOG2DEV(y+h) << " lineto\n"
442 << "closepath clip newpath\n";
443 }
444
445 void wxPostScriptDC::SetClippingRegion( const wxRegion &WXUNUSED(region) )
446 {
447 }
448
449 void wxPostScriptDC::DestroyClippingRegion()
450 {
451 if (!m_pstream) return;
452
453 wxDC::DestroyClippingRegion();
454
455 if (m_clipping)
456 {
457 m_clipping = FALSE;
458 *m_pstream << "grestore\n";
459 }
460 }
461
462 void wxPostScriptDC::Clear()
463 {
464 wxFAIL_MSG( "wxPostScriptDC::Clear not implemented." );
465 }
466
467 void wxPostScriptDC::FloodFill (long WXUNUSED(x), long WXUNUSED(y), const wxColour &WXUNUSED(col), int WXUNUSED(style))
468 {
469 wxFAIL_MSG( "wxPostScriptDC::FloodFill not implemented." );
470 }
471
472 bool wxPostScriptDC::GetPixel (long WXUNUSED(x), long WXUNUSED(y), wxColour * WXUNUSED(col)) const
473 {
474 wxFAIL_MSG( "wxPostScriptDC::GetPixel not implemented." );
475 return FALSE;
476 }
477
478 void wxPostScriptDC::CrossHair (long WXUNUSED(x), long WXUNUSED(y))
479 {
480 wxFAIL_MSG( "wxPostScriptDC::CrossHair not implemented." );
481 }
482
483 void wxPostScriptDC::DrawLine (long x1, long y1, long x2, long y2)
484 {
485 if (!m_pstream) return;
486
487 if (m_pen.GetStyle() == wxTRANSPARENT) return;
488
489 SetPen( m_pen );
490
491 *m_pstream << "newpath\n"
492 << XLOG2DEV(x1) << " " << YLOG2DEV (y1) << " moveto\n"
493 << XLOG2DEV(x2) << " " << YLOG2DEV (y2) << " lineto\n"
494 << "stroke\n";
495
496 CalcBoundingBox( x1, y1 );
497 CalcBoundingBox( x2, y2 );
498 }
499
500 #define RAD2DEG 57.29577951308
501
502 void wxPostScriptDC::DrawArc (long x1, long y1, long x2, long y2, long xc, long yc)
503 {
504 if (!m_pstream) return;
505
506 long dx = x1 - xc;
507 long dy = y1 - yc;
508 long radius = (long) sqrt(dx*dx+dy*dy);
509 double alpha1, alpha2;
510
511 if (x1 == x2 && y1 == y2)
512 {
513 alpha1 = 0.0;
514 alpha2 = 360.0;
515 } else if (radius == 0.0)
516 {
517 alpha1 = alpha2 = 0.0;
518 } else
519 {
520 alpha1 = (x1 - xc == 0) ?
521 (y1 - yc < 0) ? 90.0 : -90.0 :
522 -atan2(double(y1-yc), double(x1-xc)) * RAD2DEG;
523 alpha2 = (x2 - xc == 0) ?
524 (y2 - yc < 0) ? 90.0 : -90.0 :
525 -atan2(double(y2-yc), double(x2-xc)) * RAD2DEG;
526 }
527 while (alpha1 <= 0) alpha1 += 360;
528 while (alpha2 <= 0) alpha2 += 360; // adjust angles to be between
529 while (alpha1 > 360) alpha1 -= 360; // 0 and 360 degree
530 while (alpha2 > 360) alpha2 -= 360;
531
532 if (m_brush.GetStyle() != wxTRANSPARENT)
533 {
534 SetBrush( m_brush );
535 *m_pstream << "newpath\n"
536 << XLOG2DEV(xc) << " "
537 << YLOG2DEV(yc) << " "
538 << XLOG2DEVREL(radius) << " "
539 << YLOG2DEVREL(radius) << " "
540 << alpha1 << " "
541 << alpha2 << " ellipse\n"
542 << XLOG2DEV(xc) << " "
543 << YLOG2DEV(yc) << " lineto\n"
544 << "closepath\n"
545 << "fill\n";
546 }
547
548 if (m_pen.GetStyle() != wxTRANSPARENT)
549 {
550 SetPen( m_pen );
551 *m_pstream << "newpath\n"
552 << XLOG2DEV(xc) << " "
553 << YLOG2DEV(yc) << " "
554 << XLOG2DEVREL(radius) << " "
555 << YLOG2DEVREL(radius) << " "
556 << alpha1 << " "
557 << alpha2 << " ellipse\n"
558 << "stroke\n";
559 }
560
561 CalcBoundingBox( xc-radius, yc-radius );
562 CalcBoundingBox( xc+radius, yc+radius );
563 }
564
565 void wxPostScriptDC::DrawEllipticArc(long x,long y,long w,long h,double sa,double ea)
566 {
567 if (!m_pstream) return;
568
569 if (sa>=360 || sa<=-360) sa=sa-int(sa/360)*360;
570 if (ea>=360 || ea<=-360) ea=ea-int(ea/360)*360;
571 if (sa<0) sa+=360;
572 if (ea<0) ea+=360;
573
574 if (sa==ea)
575 {
576 DrawEllipse(x,y,w,h);
577 return;
578 }
579
580 if (m_brush.GetStyle () != wxTRANSPARENT)
581 {
582 SetBrush( m_brush );
583
584 *m_pstream << "newpath\n"
585 << XLOG2DEV(x+w/2) << " " << YLOG2DEV(y+h/2) << " "
586 << XLOG2DEVREL(w/2) << " " << YLOG2DEVREL(h/2) << " "
587 << int(sa) <<" "<< int(ea) << " true ellipticarc\n";
588
589 CalcBoundingBox( x ,y );
590 CalcBoundingBox( x+w, y+h );
591 }
592
593 if (m_pen.GetStyle () != wxTRANSPARENT)
594 {
595 SetPen( m_pen );
596
597 *m_pstream << "newpath\n"
598 << XLOG2DEV(x+w/2) << " " << YLOG2DEV(y+h/2) << " "
599 << XLOG2DEVREL(w/2) << " " << XLOG2DEVREL(h/2) << " "
600 << int(sa) <<" "<< int(ea) << " false ellipticarc\n";
601
602 CalcBoundingBox( x, y );
603 CalcBoundingBox( x+w, y+h );
604 }
605 }
606
607 void wxPostScriptDC::DrawPoint (long x, long y)
608 {
609 if (!m_pstream) return;
610
611 if (m_pen.GetStyle() == wxTRANSPARENT) return;
612
613 SetPen (m_pen);
614
615 *m_pstream << "newpath\n"
616 << XLOG2DEV(x) << " " << YLOG2DEV (y) << " moveto\n"
617 << XLOG2DEV(x+1) << " " << YLOG2DEV (y) << " lineto\n"
618 << "stroke\n";
619
620 CalcBoundingBox( x, y );
621 }
622
623 void wxPostScriptDC::DrawPolygon (int n, wxPoint points[], long xoffset, long yoffset, int WXUNUSED(fillStyle))
624 {
625 if (!m_pstream) return;
626
627 if (n <= 0) return;
628
629 if (m_brush.GetStyle () != wxTRANSPARENT)
630 {
631 SetBrush( m_brush );
632
633 *m_pstream << "newpath\n";
634
635 long xx = XLOG2DEV(points[0].x + xoffset);
636 long yy = YLOG2DEV(points[0].y + yoffset);
637 *m_pstream << xx << " " << yy << " moveto\n";
638 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
639
640 for (int i = 1; i < n; i++)
641 {
642 xx = XLOG2DEV(points[i].x + xoffset);
643 yy = YLOG2DEV(points[i].y + yoffset);
644 *m_pstream << xx << " " << yy << " lineto\n";
645 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
646 }
647 *m_pstream << "fill\n";
648 }
649
650 if (m_pen.GetStyle () != wxTRANSPARENT)
651 {
652 SetPen( m_pen );
653
654 *m_pstream << "newpath\n";
655
656 long xx = XLOG2DEV(points[0].x + xoffset);
657 long yy = YLOG2DEV(points[0].y + yoffset);
658 *m_pstream << xx << " " << yy << " moveto\n";
659 CalcBoundingBox( points[0].x + xoffset, points[0].y + yoffset );
660
661 for (int i = 1; i < n; i++)
662 {
663 xx = XLOG2DEV(points[i].x + xoffset);
664 yy = YLOG2DEV(points[i].y + yoffset);
665 *m_pstream << xx << " " << yy << " lineto\n";
666 CalcBoundingBox( points[i].x + xoffset, points[i].y + yoffset);
667 }
668
669 *m_pstream << "stroke\n";
670 }
671 }
672
673 void wxPostScriptDC::DrawLines (int n, wxPoint points[], long xoffset, long yoffset)
674 {
675 if (!m_pstream) return;
676
677 if (m_pen.GetStyle() == wxTRANSPARENT) return;
678 if (n <= 0) return;
679
680 SetPen (m_pen);
681
682 for (int i=0; i<n ; ++i)
683 {
684 CalcBoundingBox( XLOG2DEV(points[i].x+xoffset), YLOG2DEV(points[i].y+yoffset));
685 }
686
687 *m_pstream << "newpath\n"
688 << XLOG2DEV(points[0].x+xoffset) << " "
689 << YLOG2DEV(points[0].y+yoffset) << " moveto\n";
690
691 for (int i = 1; i < n; i++)
692 {
693 *m_pstream << XLOG2DEV(points[i].x+xoffset) << " "
694 << YLOG2DEV(points[i].y+yoffset) << " lineto\n";
695 }
696
697 *m_pstream << "stroke\n";
698 }
699
700 void wxPostScriptDC::DrawRectangle (long x, long y, long width, long height)
701 {
702 if (!m_pstream) return;
703
704 if (m_brush.GetStyle () != wxTRANSPARENT)
705 {
706 SetBrush( m_brush );
707
708 *m_pstream << "newpath\n"
709 << XLOG2DEV(x) << " " << YLOG2DEV(y) << " moveto\n"
710 << XLOG2DEV(x + width) << " " << YLOG2DEV(y) << " lineto\n"
711 << XLOG2DEV(x + width) << " " << YLOG2DEV(y + height) << " lineto\n"
712 << XLOG2DEV(x) << " " << YLOG2DEV(y + height) << " lineto\n"
713 << "closepath\n"
714 << "fill\n";
715
716 CalcBoundingBox( x, y );
717 CalcBoundingBox( x + width, y + height );
718 }
719
720 if (m_pen.GetStyle () != wxTRANSPARENT)
721 {
722 SetPen (m_pen);
723
724 *m_pstream << "newpath\n"
725 << XLOG2DEV(x) << " " << YLOG2DEV(y) << " moveto\n"
726 << XLOG2DEV(x + width) << " " << YLOG2DEV(y) << " lineto\n"
727 << XLOG2DEV(x + width) << " " << YLOG2DEV(y + height) << " lineto\n"
728 << XLOG2DEV(x) << " " << YLOG2DEV(y + height) << " lineto\n"
729 << "closepath\n"
730 << "stroke\n";
731
732 CalcBoundingBox( x, y );
733 CalcBoundingBox( x + width, y + height );
734 }
735 }
736
737 void wxPostScriptDC::DrawRoundedRectangle (long x, long y, long width, long height, double radius)
738 {
739 if (!m_pstream) return;
740
741 if (radius < 0.0)
742 {
743 // Now, a negative radius is interpreted to mean
744 // 'the proportion of the smallest X or Y dimension'
745 double smallest = 0.0;
746 if (width < height)
747 smallest = width;
748 else
749 smallest = height;
750 radius = (-radius * smallest);
751 }
752
753 long rad = (long) radius;
754
755 if (m_brush.GetStyle () != wxTRANSPARENT)
756 {
757 SetBrush( m_brush );
758
759 // Draw rectangle anticlockwise
760 *m_pstream << "newpath\n"
761 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y + rad) << " " << XLOG2DEVREL(rad) << " 90 180 arc\n"
762 << XLOG2DEV(x) << " " << YLOG2DEV(y + rad) << " moveto\n"
763 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y + height - rad) << " " << XLOG2DEVREL(rad) << " 180 270 arc\n"
764 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + height) << " lineto\n"
765 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + height - rad) << " " << XLOG2DEVREL(rad) << " 270 0 arc\n"
766 << XLOG2DEV(x + width) << " " << YLOG2DEV(y + rad) << " lineto\n"
767 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + rad) << " " << XLOG2DEVREL(rad) << " 0 90 arc\n"
768 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y) << " lineto\n"
769 << "closepath\n"
770 << "fill\n";
771
772 CalcBoundingBox( x, y );
773 CalcBoundingBox( x + width, y + height );
774 }
775
776 if (m_pen.GetStyle () != wxTRANSPARENT)
777 {
778 SetPen (m_pen);
779
780 // Draw rectangle anticlockwise
781 *m_pstream << "newpath\n"
782 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y + rad) << " " << XLOG2DEVREL(rad) << " 90 180 arc\n"
783 << XLOG2DEV(x) << " " << YLOG2DEV(y + rad) << " moveto\n"
784 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y + height - rad) << " " << XLOG2DEVREL(rad) << " 180 270 arc\n"
785 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + height) << " lineto\n"
786 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + height - rad) << " " << XLOG2DEVREL(rad) << " 270 0 arc\n"
787 << XLOG2DEV(x + width) << " " << YLOG2DEV(y + rad) << " lineto\n"
788 << XLOG2DEV(x + width - rad) << " " << YLOG2DEV(y + rad) << " " << XLOG2DEVREL(rad) << " 0 90 arc\n"
789 << XLOG2DEV(x + rad) << " " << YLOG2DEV(y) << " lineto\n"
790 << "closepath\n"
791 << "stroke\n";
792
793 CalcBoundingBox( x, y );
794 CalcBoundingBox( x + width, y + height );
795 }
796 }
797
798 void wxPostScriptDC::DrawEllipse (long x, long y, long width, long height)
799 {
800 if (!m_pstream) return;
801
802 if (m_brush.GetStyle () != wxTRANSPARENT)
803 {
804 SetBrush (m_brush);
805
806 *m_pstream << "newpath\n"
807 << XLOG2DEV(x + width / 2) << " " << YLOG2DEV(y + height / 2) << " "
808 << XLOG2DEV(width / 2) << " " << YLOG2DEVREL(height / 2) << " 0 360 ellipse\n"
809 << "fill\n";
810
811 CalcBoundingBox( x - width, y - height );
812 CalcBoundingBox( x + width, y + height );
813 }
814
815 if (m_pen.Ok() && m_pen.GetStyle () != wxTRANSPARENT)
816 {
817 SetPen (m_pen);
818
819 *m_pstream << "newpath\n"
820 << XLOG2DEV(x + width / 2) << " " << YLOG2DEV(y + height / 2) << " "
821 << XLOG2DEV(width / 2) << " " << YLOG2DEVREL(height / 2) << " 0 360 ellipse\n"
822 << "stroke\n";
823
824 CalcBoundingBox( x - width, y - height );
825 CalcBoundingBox( x + width, y + height );
826 }
827 }
828
829 void wxPostScriptDC::DrawIcon (const wxIcon& icon, long x, long y)
830 {
831 wxMemoryDC memDC;
832 memDC.SelectObject( icon );
833 Blit(x, y, icon.GetWidth(), icon.GetHeight(), &memDC, 0, 0);
834 }
835
836 void wxPostScriptDC::DrawBitmap( const wxBitmap& bitmap, long x, long y, bool useMask )
837 {
838 wxMemoryDC memDC;
839 memDC.SelectObject( bitmap );
840 Blit( x, y, bitmap.GetWidth(), bitmap.GetHeight(), &memDC, 0, 0, useMask );
841 }
842
843 void wxPostScriptDC::SetFont (const wxFont& font)
844 {
845 if (!m_pstream) return;
846
847 if (!font.Ok()) return;
848
849 m_font = font;
850
851 char *name = wxTheFontNameDirectory->GetPostScriptName( m_font.GetFamily(),
852 m_font.GetWeight(),
853 m_font.GetStyle() );
854 if (!name) name = "Times-Roman";
855
856 *m_pstream << "/" << name << " reencodeISO def\n"
857 << "/" << name << " findfont\n"
858 << YLOG2DEVREL(font.GetPointSize())
859 << " scalefont setfont\n";
860 }
861
862 void wxPostScriptDC::SetPen( const wxPen& pen )
863 {
864 if (!m_pstream) return;
865
866 if (!pen.Ok()) return;
867
868 int oldStyle = m_pen.GetStyle();
869
870 m_pen = pen;
871
872 *m_pstream << XLOG2DEVREL(m_pen.GetWidth()) << " setlinewidth\n";
873
874 /*
875 Line style - WRONG: 2nd arg is OFFSET
876
877 Here, I'm afraid you do not conceive meaning of parameters of 'setdash'
878 operator correctly. You should look-up this in the Red Book: the 2nd parame-
879 ter is not number of values in the array of the first one, but an offset
880 into this description of the pattern. I mean a real *offset* not index
881 into array. I.e. If the command is [3 4] 1 setdash is used, then there
882 will be first black line *2* units long, then space 4 units, then the
883 pattern of *3* units black, 4 units space will be repeated.
884 */
885
886 static const char *dotted = "[2 5] 2";
887 static const char *short_dashed = "[4 4] 2";
888 static const char *long_dashed = "[4 8] 2";
889 static const char *dotted_dashed = "[6 6 2 6] 4";
890
891 const char *psdash = (char *) NULL;
892 switch (m_pen.GetStyle ())
893 {
894 case wxDOT: psdash = dotted; break;
895 case wxSHORT_DASH: psdash = short_dashed; break;
896 case wxLONG_DASH: psdash = long_dashed; break;
897 case wxDOT_DASH: psdash = dotted_dashed; break;
898 case wxSOLID:
899 case wxTRANSPARENT:
900 default: psdash = "[] 0"; break;
901 }
902
903 if (oldStyle != m_pen.GetStyle())
904 {
905 *m_pstream << psdash << " setdash\n";
906 }
907
908 // Line colour
909 unsigned char red = m_pen.GetColour().Red();
910 unsigned char blue = m_pen.GetColour().Blue();
911 unsigned char green = m_pen.GetColour().Green();
912
913 if (!m_colour)
914 {
915 // Anything not white is black
916 if (!(red == (unsigned char) 255 && blue == (unsigned char) 255
917 && green == (unsigned char) 255))
918 {
919 red = (unsigned char) 0;
920 green = (unsigned char) 0;
921 blue = (unsigned char) 0;
922 }
923
924 // setgray here ?
925 }
926
927 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
928 {
929 long redPS = (long) (((int) red) / 255.0);
930 long bluePS = (long) (((int) blue) / 255.0);
931 long greenPS = (long) (((int) green) / 255.0);
932
933 *m_pstream << redPS << " " << greenPS << " " << bluePS << " setrgbcolor\n";
934
935 m_currentRed = red;
936 m_currentBlue = blue;
937 m_currentGreen = green;
938 }
939 }
940
941 void wxPostScriptDC::SetBrush( const wxBrush& brush )
942 {
943 if (!m_pstream) return;
944
945 if (!brush.Ok()) return;
946
947 m_brush = brush;
948
949 // Brush colour
950 unsigned char red = m_brush.GetColour ().Red ();
951 unsigned char blue = m_brush.GetColour ().Blue ();
952 unsigned char green = m_brush.GetColour ().Green ();
953
954 if (!m_colour)
955 {
956 // Anything not black is white
957 if (!(red == (unsigned char) 0 && blue == (unsigned char) 0
958 && green == (unsigned char) 0))
959 {
960 red = (unsigned char) 255;
961 green = (unsigned char) 255;
962 blue = (unsigned char) 255;
963 }
964
965 // setgray here ?
966 }
967
968 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
969 {
970 long redPS = (long) (((int) red) / 255.0);
971 long bluePS = (long) (((int) blue) / 255.0);
972 long greenPS = (long) (((int) green) / 255.0);
973 *m_pstream << redPS << " " << greenPS << " " << bluePS << " setrgbcolor\n";
974 m_currentRed = red;
975 m_currentBlue = blue;
976 m_currentGreen = green;
977 }
978 }
979
980 void wxPostScriptDC::DrawText( const wxString& text, long x, long y, bool WXUNUSED(use16bit) )
981 {
982 if (!m_pstream) return;
983
984 SetFont( m_font );
985
986 if (m_textForegroundColour.Ok ())
987 {
988 unsigned char red = m_textForegroundColour.Red ();
989 unsigned char blue = m_textForegroundColour.Blue ();
990 unsigned char green = m_textForegroundColour.Green ();
991
992 if (!m_colour)
993 {
994 // Anything not white is black
995 if (!(red == (unsigned char) 255 && blue == (unsigned char) 255
996 && green == (unsigned char) 255))
997 {
998 red = (unsigned char) 0;
999 green = (unsigned char) 0;
1000 blue = (unsigned char) 0;
1001 }
1002 }
1003
1004 // maybe setgray here ?
1005
1006 if (!(red == m_currentRed && green == m_currentGreen && blue == m_currentBlue))
1007 {
1008 long redPS = (long) (((int) red) / 255.0);
1009 long bluePS = (long) (((int) blue) / 255.0);
1010 long greenPS = (long) (((int) green) / 255.0);
1011 *m_pstream << redPS << " " << greenPS << " " << bluePS << " setrgbcolor\n";
1012
1013 m_currentRed = red;
1014 m_currentBlue = blue;
1015 m_currentGreen = green;
1016 }
1017 }
1018
1019 int size = m_font.GetPointSize();
1020
1021 long by = y + (long)floor( float(size) * 2.0 / 3.0 ); // approximate baseline
1022 *m_pstream << XLOG2DEV(x) << " " << YLOG2DEV(by) << " moveto\n";
1023
1024 *m_pstream << "(";
1025 int len = strlen ((char *)(const char *)text);
1026 int i;
1027 for (i = 0; i < len; i++)
1028 {
1029 int c = (unsigned char) text[i];
1030 if ( c == ')' || c == '(' || c == '\\')
1031 {
1032 *m_pstream << "\\" << (char) c;
1033 }
1034 else if ( c >= 128 )
1035 {
1036 // Cope with character codes > 127
1037 char tmp[5];
1038 sprintf(tmp, "\\%o", c);
1039 *m_pstream << tmp;
1040 }
1041 else
1042 *m_pstream << (char) c;
1043 }
1044
1045 *m_pstream << ")" << " show\n";
1046
1047 if (m_font.GetUnderlined())
1048 {
1049 long uy = (long)(y + size - m_underlinePosition);
1050 long w, h;
1051 GetTextExtent(text, &w, &h);
1052
1053 *m_pstream << "gsave " << XLOG2DEV(x) << " " << YLOG2DEV(uy)
1054 << " moveto\n"
1055 << (long)m_underlineThickness << " setlinewidth "
1056 << XLOG2DEV(x + w) << " " << YLOG2DEV(uy)
1057 << " lineto stroke grestore\n";
1058 }
1059
1060 CalcBoundingBox( x, y );
1061 CalcBoundingBox( x + size * text.Length() * 2/3 , y );
1062 }
1063
1064
1065 void wxPostScriptDC::SetBackground (const wxBrush& brush)
1066 {
1067 m_backgroundBrush = brush;
1068 }
1069
1070 void wxPostScriptDC::SetLogicalFunction (int WXUNUSED(function))
1071 {
1072 wxFAIL_MSG( "wxPostScriptDC::SetLogicalFunction not implemented." );
1073 }
1074
1075 void wxPostScriptDC::DrawSpline( wxList *points )
1076 {
1077 if (!m_pstream) return;
1078
1079 SetPen( m_pen );
1080
1081 double a, b, c, d, x1, y1, x2, y2, x3, y3;
1082 wxPoint *p, *q;
1083
1084 wxNode *node = points->First();
1085 p = (wxPoint *)node->Data();
1086 x1 = p->x;
1087 y1 = p->y;
1088
1089 node = node->Next();
1090 p = (wxPoint *)node->Data();
1091 c = p->x;
1092 d = p->y;
1093 x3 = a = (double)(x1 + c) / 2;
1094 y3 = b = (double)(y1 + d) / 2;
1095
1096 *m_pstream << "newpath "
1097 << XLOG2DEV((long)x1) << " " << YLOG2DEV((long)y1) << " moveto "
1098 << XLOG2DEV((long)x3) << " " << YLOG2DEV((long)y3) << " lineto\n";
1099
1100 CalcBoundingBox( (long)x1, (long)y1 );
1101 CalcBoundingBox( (long)x3, (long)y3 );
1102
1103 while ((node = node->Next()) != NULL)
1104 {
1105 q = (wxPoint *)node->Data();
1106
1107 x1 = x3;
1108 y1 = y3;
1109 x2 = c;
1110 y2 = d;
1111 c = q->x;
1112 d = q->y;
1113 x3 = (double)(x2 + c) / 2;
1114 y3 = (double)(y2 + d) / 2;
1115 *m_pstream << XLOG2DEV((long)x1) << " " << YLOG2DEV((long)y1) << " "
1116 << XLOG2DEV((long)x2) << " " << YLOG2DEV((long)y2) << " "
1117 << XLOG2DEV((long)x3) << " " << YLOG2DEV((long)y3) << " DrawSplineSection\n";
1118
1119 CalcBoundingBox( (long)x1, (long)y1 );
1120 CalcBoundingBox( (long)x3, (long)y3 );
1121 }
1122
1123 /*
1124 At this point, (x2,y2) and (c,d) are the position of the
1125 next-to-last and last point respectively, in the point list
1126 */
1127
1128 *m_pstream << XLOG2DEV((long)c) << " " << YLOG2DEV((long)d) << " lineto stroke\n";
1129 }
1130
1131 long wxPostScriptDC::GetCharWidth ()
1132 {
1133 // Chris Breeze: reasonable approximation using wxMODERN/Courier
1134 return (long) (GetCharHeight() * 72.0 / 120.0);
1135 }
1136
1137
1138 void wxPostScriptDC::SetAxisOrientation( bool xLeftRight, bool yBottomUp )
1139 {
1140 m_signX = (xLeftRight ? 1 : -1);
1141 m_signY = (yBottomUp ? 1 : -1);
1142
1143 ComputeScaleAndOrigin();
1144 }
1145
1146 void wxPostScriptDC::SetDeviceOrigin( long x, long y )
1147 {
1148 int h = 0;
1149 int w = 0;
1150 GetSize( &w, &h );
1151
1152 wxDC::SetDeviceOrigin( x, h-y );
1153 }
1154
1155 void wxPostScriptDC::GetSize(int* width, int* height) const
1156 {
1157 const char *paperType = wxThePrintSetupData->GetPaperName();
1158
1159 if (!paperType) paperType = _("A4 210 x 297 mm");
1160
1161 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
1162
1163 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(_("A4 210 x 297 mm"));
1164
1165 if (paper)
1166 {
1167 *width = paper->widthPixels;
1168 *height = paper->heightPixels;
1169 }
1170 else
1171 {
1172 *width = 595;
1173 *height = 842;
1174 }
1175 }
1176
1177 bool wxPostScriptDC::StartDoc (const wxString& message)
1178 {
1179 if (m_filename == "")
1180 {
1181 m_filename = wxGetTempFileName("ps");
1182 wxThePrintSetupData->SetPrinterFile((char *)(const char *)m_filename);
1183 m_ok = TRUE;
1184 }
1185 else
1186 {
1187 wxThePrintSetupData->SetPrinterFile((char *)(const char *)m_filename);
1188 }
1189
1190 m_pstream = new ofstream (wxThePrintSetupData->GetPrinterFile());
1191
1192 if (!m_pstream || !m_pstream->good())
1193 {
1194 wxMessageBox (_("Cannot open file!"), _("Error"), wxOK);
1195 m_ok = FALSE;
1196 return FALSE;
1197 }
1198
1199 m_ok = TRUE;
1200
1201 SetBrush( *wxBLACK_BRUSH );
1202 SetPen( *wxBLACK_PEN );
1203 SetBackground( *wxWHITE_BRUSH );
1204 SetTextForeground( *wxBLACK );
1205
1206 // set origin according to paper size
1207 SetDeviceOrigin( 0,0 );
1208
1209 wxPageNumber = 1;
1210 m_pageNumber = 1;
1211 m_title = message;
1212 return TRUE;
1213 }
1214
1215 void wxPostScriptDC::EndDoc ()
1216 {
1217 if (!m_pstream) return;
1218
1219 if (m_clipping)
1220 {
1221 m_clipping = FALSE;
1222 *m_pstream << "grestore\n";
1223 }
1224
1225 if (m_pstream)
1226 {
1227 delete m_pstream;
1228 m_pstream = (ofstream *) NULL;
1229 }
1230
1231 char *header_file = wxGetTempFileName("ps");
1232
1233 m_pstream = new ofstream( header_file );
1234
1235 *m_pstream << "%!PS-Adobe-2.0\n"; /* PostScript magic strings */
1236 *m_pstream << "%%Title: " << (const char *) m_title << "\n";
1237 *m_pstream << "%%Creator: " << wxTheApp->argv[0] << "\n";
1238 *m_pstream << "%%CreationDate: " << wxNow() << "\n";
1239
1240 char userID[256];
1241 if ( wxGetEmailAddress(userID, sizeof(userID)) )
1242 {
1243 *m_pstream << "%%For: " << (char *)userID;
1244 char userName[245];
1245 if (wxGetUserName(userName, sizeof(userName)))
1246 *m_pstream << " (" << (char *)userName << ")";
1247 *m_pstream << "\n";
1248 }
1249 else if ( wxGetUserName(userID, sizeof(userID)) )
1250 {
1251 *m_pstream << "%%For: " << (char *)userID << "\n";
1252 }
1253
1254 // THE FOLLOWING HAS BEEN CONTRIBUTED BY Andy Fyfe <andy@hyperparallel.com>
1255
1256 long wx_printer_translate_x, wx_printer_translate_y;
1257 double wx_printer_scale_x, wx_printer_scale_y;
1258 wxThePrintSetupData->GetPrinterTranslation(&wx_printer_translate_x, &wx_printer_translate_y);
1259 wxThePrintSetupData->GetPrinterScaling(&wx_printer_scale_x, &wx_printer_scale_y);
1260
1261 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1262 {
1263 *m_pstream << "%%Orientation: Landscape\n";
1264 }
1265 else
1266 {
1267 *m_pstream << "%%Orientation: Portrait\n";
1268 }
1269
1270 // Compute the bounding box. Note that it is in the default user
1271 // coordinate system, thus we have to convert the values.
1272 long llx = (long) ((XLOG2DEV(m_minX)+wx_printer_translate_x)*wx_printer_scale_x);
1273 long lly = (long) ((YLOG2DEV(m_minY)+wx_printer_translate_y)*wx_printer_scale_y);
1274 long urx = (long) ((XLOG2DEV(m_maxX)+wx_printer_translate_x)*wx_printer_scale_x);
1275 long ury = (long) ((YLOG2DEV(m_maxY)+wx_printer_translate_y)*wx_printer_scale_y);
1276
1277 // If we're landscape, our sense of "x" and "y" is reversed.
1278 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1279 {
1280 long tmp;
1281 tmp = llx; llx = lly; lly = tmp;
1282 tmp = urx; urx = ury; ury = tmp;
1283
1284 // We need either the two lines that follow, or we need to subtract
1285 // min_x from real_translate_y, which is commented out below.
1286 llx = llx - (long)(m_minX*wx_printer_scale_y);
1287 urx = urx - (long)(m_minX*wx_printer_scale_y);
1288 }
1289
1290 // The Adobe specifications call for integers; we round as to make
1291 // the bounding larger.
1292 *m_pstream << "%%BoundingBox: "
1293 << floor((double)llx) << " " << floor((double)lly) << " "
1294 << ceil((double)urx) << " " << ceil((double)ury) << "\n";
1295 *m_pstream << "%%Pages: " << (wxPageNumber - 1) << "\n";
1296 *m_pstream << "%%EndComments\n\n";
1297
1298 // To check the correctness of the bounding box, postscript commands
1299 // to draw a box corresponding to the bounding box are generated below.
1300 // But since we typically don't want to print such a box, the postscript
1301 // commands are generated within comments. These lines appear before any
1302 // adjustment of scale, rotation, or translation, and hence are in the
1303 // default user coordinates.
1304 *m_pstream << "% newpath\n";
1305 *m_pstream << "% " << llx << " " << lly << " moveto\n";
1306 *m_pstream << "% " << urx << " " << lly << " lineto\n";
1307 *m_pstream << "% " << urx << " " << ury << " lineto\n";
1308 *m_pstream << "% " << llx << " " << ury << " lineto closepath stroke\n";
1309
1310 *m_pstream << "%%BeginProlog\n";
1311 *m_pstream << wxPostScriptHeaderEllipse;
1312 *m_pstream << wxPostScriptHeaderEllipticArc;
1313 *m_pstream << wxPostScriptHeaderReencodeISO1;
1314 *m_pstream << wxPostScriptHeaderReencodeISO2;
1315
1316 if (wxPostScriptHeaderSpline)
1317 *m_pstream << wxPostScriptHeaderSpline;
1318 *m_pstream << "%%EndProlog\n";
1319
1320 delete m_pstream;
1321 m_pstream = (ofstream *) NULL;
1322
1323 char *tmp_file = wxGetTempFileName("ps");
1324
1325 // Paste header Before wx_printer_file
1326 wxConcatFiles (header_file, wxThePrintSetupData->GetPrinterFile(), tmp_file);
1327 wxRemoveFile (header_file);
1328 wxRemoveFile (wxThePrintSetupData->GetPrinterFile());
1329 wxRenameFile(tmp_file, wxThePrintSetupData->GetPrinterFile());
1330
1331 #if defined(__X__) || defined(__WXGTK__)
1332 if (m_ok)
1333 {
1334 switch (wxThePrintSetupData->GetPrinterMode()) {
1335 case PS_PREVIEW:
1336 {
1337 char *argv[3];
1338 argv[0] = wxThePrintSetupData->GetPrintPreviewCommand();
1339 argv[1] = wxThePrintSetupData->GetPrinterFile();
1340 argv[2] = (char *) NULL;
1341 wxExecute (argv, TRUE);
1342 wxRemoveFile(wxThePrintSetupData->GetPrinterFile());
1343 }
1344 break;
1345
1346 case PS_PRINTER:
1347 {
1348 char *argv[4];
1349 int argc = 0;
1350 argv[argc++] = wxThePrintSetupData->GetPrinterCommand();
1351
1352 // !SM! If we simply assign to argv[1] here, if printer options
1353 // are blank, we get an annoying and confusing message from lpr.
1354 char * opts = wxThePrintSetupData->GetPrinterOptions();
1355 if (opts && *opts)
1356 argv[argc++] = opts;
1357
1358 argv[argc++] = wxThePrintSetupData->GetPrinterFile();
1359 argv[argc++] = (char *) NULL;
1360 wxExecute (argv, TRUE);
1361 wxRemoveFile(wxThePrintSetupData->GetPrinterFile());
1362 }
1363 break;
1364
1365 case PS_FILE:
1366 break;
1367 }
1368 }
1369 #endif
1370 }
1371
1372 void wxPostScriptDC::StartPage ()
1373 {
1374 if (!m_pstream) return;
1375
1376 *m_pstream << "%%Page: " << (wxPageNumber++) << "\n";
1377
1378 // *m_pstream << "matrix currentmatrix\n";
1379
1380 // Added by Chris Breeze
1381
1382 // Each page starts with an "initgraphics" which resets the
1383 // transformation and so we need to reset the origin
1384 // (and rotate the page for landscape printing)
1385
1386 /*
1387 m_scaleFactor = 1.0;
1388 m_logicalOriginX = 0;
1389 m_logicalOriginY = 0;
1390 */
1391
1392 // Output scaling
1393 long translate_x, translate_y;
1394 double scale_x, scale_y;
1395 wxThePrintSetupData->GetPrinterTranslation(&translate_x, &translate_y);
1396 wxThePrintSetupData->GetPrinterScaling(&scale_x, &scale_y);
1397
1398 if (wxThePrintSetupData->GetPrinterOrientation() == PS_LANDSCAPE)
1399 {
1400 translate_y -= m_maxY;
1401 *m_pstream << "90 rotate\n";
1402 }
1403
1404 *m_pstream << scale_x << " " << scale_y << " scale\n";
1405 *m_pstream << translate_x << " " << translate_y << " translate\n";
1406 }
1407
1408 void wxPostScriptDC::EndPage ()
1409 {
1410 if (!m_pstream) return;
1411
1412 *m_pstream << "showpage\n";
1413 }
1414
1415 bool wxPostScriptDC::Blit (long xdest, long ydest, long fwidth, long fheight,
1416 wxDC *source, long xsrc, long ysrc, int WXUNUSED(rop), bool WXUNUSED(useMask))
1417 {
1418 return TRUE;
1419 }
1420
1421 long wxPostScriptDC::GetCharHeight ()
1422 {
1423 if (m_font.Ok())
1424 return m_font.GetPointSize();
1425 else
1426 return 12;
1427 }
1428
1429 void wxPostScriptDC::GetTextExtent (const wxString& string, long *x, long *y,
1430 long *descent, long *externalLeading, wxFont *theFont,
1431 bool WXUNUSED(use16))
1432 {
1433 // if (!m_pstream) return;
1434
1435 wxFont *fontToUse = theFont;
1436
1437 if (!fontToUse) fontToUse = (wxFont*) &m_font;
1438
1439 #if !USE_AFM_FOR_POSTSCRIPT
1440 // Provide a VERY rough estimate (avoid using it)
1441 // Chris Breeze 5/11/97: produces accurate results for mono-spaced
1442 // font such as Courier (aka wxMODERN)
1443 int height = 12;
1444 if (fontToUse)
1445 {
1446 height = fontToUse->GetPointSize();
1447 }
1448 *x = strlen (string) * height * 72 / 120;
1449 *y = (long) (height * 1.32); // allow for descender
1450
1451 if (descent)
1452 *descent = 0;
1453 if (externalLeading)
1454 *externalLeading = 0;
1455 #else
1456 // +++++ start of contributed code +++++
1457
1458 // ************************************************************
1459 // method for calculating string widths in postscript:
1460 // read in the AFM (adobe font metrics) file for the
1461 // actual font, parse it and extract the character widths
1462 // and also the descender. this may be improved, but for now
1463 // it works well. the AFM file is only read in if the
1464 // font is changed. this may be chached in the future.
1465 // calls to GetTextExtent with the font unchanged are rather
1466 // efficient!!!
1467 //
1468 // for each font and style used there is an AFM file necessary.
1469 // currently i have only files for the roman font family.
1470 // i try to get files for the other ones!
1471 //
1472 // CAVE: the size of the string is currently always calculated
1473 // in 'points' (1/72 of an inch). this should later on be
1474 // changed to depend on the mapping mode.
1475 // CAVE: the path to the AFM files must be set before calling this
1476 // function. this is usually done by a call like the following:
1477 // wxSetAFMPath("d:\\wxw161\\afm\\");
1478 //
1479 // example:
1480 //
1481 // wxPostScriptDC dc(NULL, TRUE);
1482 // if (dc.Ok()){
1483 // wxSetAFMPath("d:\\wxw161\\afm\\");
1484 // dc.StartDoc("Test");
1485 // dc.StartPage();
1486 // long w,h;
1487 // dc.SetFont(new wxFont(10, wxROMAN, wxNORMAL, wxNORMAL));
1488 // dc.GetTextExtent("Hallo",&w,&h);
1489 // dc.EndPage();
1490 // dc.EndDoc();
1491 // }
1492 //
1493 // by steve (stefan.hammes@urz.uni-heidelberg.de)
1494 // created: 10.09.94
1495 // updated: 14.05.95
1496
1497 assert(fontToUse && "void wxPostScriptDC::GetTextExtent: no font defined");
1498 assert(x && "void wxPostScriptDC::GetTextExtent: x == NULL");
1499 assert(y && "void wxPostScriptDC::GetTextExtent: y == NULL");
1500
1501 // these static vars are for storing the state between calls
1502 static int lastFamily= INT_MIN;
1503 static int lastSize= INT_MIN;
1504 static int lastStyle= INT_MIN;
1505 static int lastWeight= INT_MIN;
1506 static int lastDescender = INT_MIN;
1507 static int lastWidths[256]; // widths of the characters
1508
1509 // get actual parameters
1510 const int Family = fontToUse->GetFamily();
1511 const int Size = fontToUse->GetPointSize();
1512 const int Style = fontToUse->GetStyle();
1513 const int Weight = fontToUse->GetWeight();
1514
1515 // if we have another font, read the font-metrics
1516 if(Family!=lastFamily||Size!=lastSize||Style!=lastStyle||Weight!=lastWeight){
1517 // store actual values
1518 lastFamily = Family;
1519 lastSize = Size;
1520 lastStyle = Style;
1521 lastWeight = Weight;
1522
1523 // read in new font metrics **************************************
1524
1525 // 1. construct filename ******************************************
1526 /* MATTHEW: [2] Use wxTheFontNameDirectory */
1527 const char *name;
1528
1529 // Julian - we'll need to do this a different way now we've removed the
1530 // font directory system. Must find Stefan's original code.
1531
1532 name = wxTheFontNameDirectory->GetAFMName(Family, Weight, Style);
1533 if (!name)
1534 name = "unknown";
1535
1536 // get the directory of the AFM files
1537 char afmName[256];
1538 afmName[0] = 0;
1539 if (wxGetAFMPath())
1540 strcpy(afmName,wxGetAFMPath());
1541
1542 // 2. open and process the file **********************************
1543
1544 // a short explanation of the AFM format:
1545 // we have for each character a line, which gives its size
1546 // e.g.:
1547 //
1548 // C 63 ; WX 444 ; N question ; B 49 -14 395 676 ;
1549 //
1550 // that means, we have a character with ascii code 63, and width
1551 // (444/1000 * fontSize) points.
1552 // the other data is ignored for now!
1553 //
1554 // when the font has changed, we read in the right AFM file and store the
1555 // character widths in an array, which is processed below (see point 3.).
1556
1557 // new elements JC Sun Aug 25 23:21:44 MET DST 1996
1558
1559
1560 strcat(afmName,name);
1561 strcat(afmName,".afm");
1562 FILE *afmFile = fopen(afmName,"r");
1563 if(afmFile==NULL){
1564 wxDebugMsg("GetTextExtent: can't open AFM file '%s'\n",afmName);
1565 wxDebugMsg(" using approximate values\n");
1566 int i;
1567 for (i=0; i<256; i++) lastWidths[i] = 500; // an approximate value
1568 lastDescender = -150; // dito.
1569 }else{
1570 int i;
1571 // init the widths array
1572 for(i=0; i<256; i++) lastWidths[i]= INT_MIN;
1573 // some variables for holding parts of a line
1574 char cString[10],semiString[10],WXString[10],descString[20];
1575 char upString[30], utString[30], encString[50];
1576 char line[256];
1577 int ascii,cWidth;
1578 // read in the file and parse it
1579 while(fgets(line,sizeof(line),afmFile)!=NULL){
1580 // A.) check for descender definition
1581 if(strncmp(line,"Descender",9)==0){
1582 if((sscanf(line,"%s%d",descString,&lastDescender)!=2)
1583 || (strcmp(descString,"Descender")!=0)) {
1584 wxDebugMsg("AFM-file '%s': line '%s' has error (bad descender)\n",
1585 afmName,line);
1586 }
1587 }
1588 // JC 1.) check for UnderlinePosition
1589 else if(strncmp(line,"UnderlinePosition",17)==0){
1590 if((sscanf(line,"%s%lf",upString,&UnderlinePosition)!=2)
1591 || (strcmp(upString,"UnderlinePosition")!=0)) {
1592 wxDebugMsg("AFM-file '%s': line '%s' has error (bad UnderlinePosition)\n",
1593 afmName,line);
1594 }
1595 }
1596 // JC 2.) check for UnderlineThickness
1597 else if(strncmp(line,"UnderlineThickness",18)==0){
1598 if((sscanf(line,"%s%lf",utString,&UnderlineThickness)!=2)
1599 || (strcmp(utString,"UnderlineThickness")!=0)) {
1600 wxDebugMsg("AFM-file '%s': line '%s' has error (bad UnderlineThickness)\n",
1601 afmName,line);
1602 }
1603 }
1604 // JC 3.) check for EncodingScheme
1605 else if(strncmp(line,"EncodingScheme",14)==0){
1606 if((sscanf(line,"%s%s",utString,encString)!=2)
1607 || (strcmp(utString,"EncodingScheme")!=0)) {
1608 wxDebugMsg("AFM-file '%s': line '%s' has error (bad EncodingScheme)\n",
1609 afmName,line);
1610 }
1611 else if (strncmp(encString, "AdobeStandardEncoding", 21))
1612 {
1613 wxDebugMsg("AFM-file '%s': line '%s' has error (unsupported EncodingScheme %s)\n",
1614 afmName,line, encString);
1615 }
1616 }
1617 // B.) check for char-width
1618 else if(strncmp(line,"C ",2)==0){
1619 if(sscanf(line,"%s%d%s%s%d",
1620 cString,&ascii,semiString,WXString,&cWidth)!=5){
1621 wxDebugMsg("AFM-file '%s': line '%s' has an error (bad character width)\n",afmName,line);
1622 }
1623 if(strcmp(cString,"C")!=0 || strcmp(semiString,";")!=0 ||
1624 strcmp(WXString,"WX")!=0){
1625 wxDebugMsg("AFM-file '%s': line '%s' has a format error\n",afmName,line);
1626 }
1627 //printf(" char '%c'=%d has width '%d'\n",ascii,ascii,cWidth);
1628 if(ascii>=0 && ascii<256){
1629 lastWidths[ascii] = cWidth; // store width
1630 }else{
1631 /* MATTHEW: this happens a lot; don't print an error */
1632 // wxDebugMsg("AFM-file '%s': ASCII value %d out of range\n",afmName,ascii);
1633 }
1634 }
1635 // C.) ignore other entries.
1636 }
1637 fclose(afmFile);
1638 }
1639 // hack to compute correct values for german 'Umlaute'
1640 // the correct way would be to map the character names
1641 // like 'adieresis' to corresp. positions of ISOEnc and read
1642 // these values from AFM files, too. Maybe later ...
1643 lastWidths[196] = lastWidths['A']; // Ä
1644 lastWidths[228] = lastWidths['a']; // ä
1645 lastWidths[214] = lastWidths['O']; // Ö
1646 lastWidths[246] = lastWidths['o']; // ö
1647 lastWidths[220] = lastWidths['U']; // Ü
1648 lastWidths[252] = lastWidths['u']; // ü
1649 lastWidths[223] = lastWidths[251]; // ß
1650 }
1651
1652 // JC: calculate UnderlineThickness/UnderlinePosition
1653 m_underlinePosition = m_underlinePosition * fontToUse->GetPointSize() / 1000.0f;
1654 m_underlineThickness = m_underlineThickness * fontToUse->GetPointSize() / 1000.0f * m_scaleFactor;
1655
1656 // 3. now the font metrics are read in, calc size *******************
1657 // this is done by adding the widths of the characters in the
1658 // string. they are given in 1/1000 of the size!
1659
1660 long widthSum=0;
1661 long height=Size; // by default
1662 unsigned char *p;
1663 for(p=(unsigned char *)(const char *)string; *p; p++){
1664 if(lastWidths[*p]== INT_MIN){
1665 wxDebugMsg("GetTextExtent: undefined width for character '%c' (%d)\n",
1666 *p,*p);
1667 widthSum += (long)(lastWidths[' ']/1000.0F * Size); // assume space
1668 }else{
1669 widthSum += (long)((lastWidths[*p]/1000.0F)*Size);
1670 }
1671 }
1672 // add descender to height (it is usually a negative value)
1673 if(lastDescender!=INT_MIN){
1674 height += (long)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
1675 }
1676
1677 // return size values
1678 *x = widthSum;
1679 *y = height;
1680
1681 // return other parameters
1682 if (descent){
1683 if(lastDescender!=INT_MIN){
1684 *descent = (long)(((-lastDescender)/1000.0F) * Size); /* MATTHEW: forgot scale */
1685 }else{
1686 *descent = 0;
1687 }
1688 }
1689
1690 // currently no idea how to calculate this!
1691 // if (externalLeading) *externalLeading = 0;
1692 if (externalLeading)
1693 *externalLeading = 0;
1694
1695 // ----- end of contributed code -----
1696 #endif
1697 }
1698
1699 void wxPostScriptDC::GetSizeMM(long *width, long *height) const
1700 {
1701 const char *paperType = wxThePrintSetupData->GetPaperName();
1702
1703 if (!paperType) paperType = _("A4 210 x 297 mm");
1704
1705 wxPrintPaperType *paper = wxThePrintPaperDatabase->FindPaperType(paperType);
1706
1707 if (!paper) paper = wxThePrintPaperDatabase->FindPaperType(_("A4 210 x 297 mm"));
1708
1709 if (paper)
1710 {
1711 *width = paper->widthMM;
1712 *height = paper->heightMM;
1713 }
1714 else
1715 {
1716 *width = 210;
1717 *height = 297;
1718 }
1719 }
1720
1721 //-------------------------------------------------------------------------------
1722 // remaining PostScript classes
1723 //-------------------------------------------------------------------------------
1724
1725 IMPLEMENT_CLASS(wxPostScriptPrintDialog, wxDialog)
1726
1727 wxPostScriptPrintDialog::wxPostScriptPrintDialog (wxWindow *parent, const wxString& title,
1728 const wxPoint& pos, const wxSize& size, long style):
1729 wxDialog(parent, -1, title, pos, size, style)
1730 {
1731 wxBeginBusyCursor();
1732
1733 char buf[100];
1734 int yPos = 40;
1735 wxString
1736 *orientation = new wxString[2],
1737 *print_modes = new wxString[3];
1738 int features;
1739 long wx_printer_translate_x, wx_printer_translate_y;
1740 double wx_printer_scale_x, wx_printer_scale_y;
1741
1742 orientation[0] = _("Portrait");
1743 orientation[1] = _("Landscape");
1744
1745 print_modes[0] = _("Send to Printer");
1746 print_modes[1] = _("Print to File");
1747 print_modes[2] = _("Preview Only");
1748
1749
1750
1751 wxButton *okBut = new wxButton (this, wxID_OK, _("OK"), wxPoint(5, 5));
1752 (void) new wxButton (this, wxID_CANCEL, _("Cancel"), wxPoint(40, 5));
1753 okBut->SetDefault();
1754
1755
1756 #if defined(__WXGTK__) || defined (__WXMOTIF__)
1757 (void) new wxStaticText( this, -1, _("Printer Command: "),
1758 wxPoint(5, yPos) );
1759 (void) new wxTextCtrl( this, wxID_PRINTER_COMMAND, wxThePrintSetupData->GetPrinterCommand(),
1760 wxPoint(100, yPos), wxSize(100, -1) );
1761
1762 (void) new wxStaticText( this, -1, _("Printer Options: "),
1763 wxPoint(210, yPos) );
1764 (void) new wxTextCtrl( this, wxID_PRINTER_OPTIONS, wxThePrintSetupData->GetPrinterOptions(),
1765 wxPoint(305, yPos), wxSize(150, -1) );
1766
1767 yPos += 40;
1768 #endif
1769
1770
1771 wxRadioBox *radio0 = new wxRadioBox(this, wxID_PRINTER_ORIENTATION, "Orientation: ", wxPoint(5, yPos), wxSize(-1,-1),
1772 2,orientation,2,0);
1773 radio0->SetSelection((int)wxThePrintSetupData->GetPrinterOrientation() - 1);
1774
1775 // @@@ Configuration hook
1776 if (wxThePrintSetupData->GetPrintPreviewCommand() == NULL)
1777 wxThePrintSetupData->SetPrintPreviewCommand(PS_VIEWER_PROG);
1778
1779 wxGetResource ("wxWindows", "PSView", &wxThePrintSetupData->previewCommand);
1780
1781 features = (wxThePrintSetupData->GetPrintPreviewCommand() &&
1782 *wxThePrintSetupData->GetPrintPreviewCommand()) ? 3 : 2;
1783
1784 wxRadioBox *radio1 = new wxRadioBox(this, wxID_PRINTER_MODES, _("PostScript:"),
1785 wxPoint(150, yPos),
1786 wxSize(-1,-1), features,
1787 print_modes, features, 0);
1788
1789 #ifdef __WXMSW__
1790 radio1->Enable(0, FALSE);
1791 if (wxThePrintSetupData->GetPrintPreviewCommand() && *wxThePrintSetupData->GetPrintPreviewCommand())
1792 radio1->Enable(2, FALSE);
1793 #endif
1794
1795 radio1->SetSelection((int)wxThePrintSetupData->GetPrinterMode());
1796 wxThePrintSetupData->GetPrinterTranslation(&wx_printer_translate_x, &wx_printer_translate_y);
1797 wxThePrintSetupData->GetPrinterScaling(&wx_printer_scale_x, &wx_printer_scale_y);
1798
1799 sprintf (buf, "%.2f", wx_printer_scale_x);
1800
1801 yPos += 90;
1802 (void) new wxStaticText(this, -1, _("X Scaling"), wxPoint(5, yPos));
1803 /* wxTextCtrl *text1 = */ (void) new wxTextCtrl(this, wxID_PRINTER_X_SCALE, buf, wxPoint(100, yPos), wxSize(100, -1));
1804
1805 sprintf (buf, "%.2f", wx_printer_scale_y);
1806 (void) new wxStaticText(this, -1, _("Y Scaling"), wxPoint(220, yPos));
1807 /* wxTextCtrl *text2 = */ (void) new wxTextCtrl(this, wxID_PRINTER_Y_SCALE, buf, wxPoint(320, yPos), wxSize(100, -1));
1808
1809 yPos += 25;
1810
1811 (void) new wxStaticText(this, -1, _("X Translation"), wxPoint(5, yPos));
1812 sprintf (buf, "%.2ld", wx_printer_translate_x);
1813 /* wxTextCtrl *text3 = */ (void) new wxTextCtrl(this, wxID_PRINTER_X_TRANS, buf, wxPoint(100, yPos), wxSize(100, -1));
1814
1815 (void) new wxStaticText(this, -1, _("Y Translation"), wxPoint(220, yPos));
1816 sprintf (buf, "%.2ld", wx_printer_translate_y);
1817 /* wxTextCtrl *text4 = */ (void) new wxTextCtrl(this, wxID_PRINTER_Y_TRANS, buf, wxPoint(320, yPos), wxSize(100, -1));
1818
1819 Fit ();
1820
1821 delete[] orientation;
1822 delete[] print_modes;
1823
1824 wxEndBusyCursor();
1825 }
1826
1827 int wxPostScriptPrintDialog::ShowModal ()
1828 {
1829 if ( wxDialog::ShowModal() == wxID_OK )
1830 {
1831 // wxTextCtrl *text0 = (wxTextCtrl *)FindWindow(wxID_PRINTER_OPTIONS);
1832 wxTextCtrl *text1 = (wxTextCtrl *)FindWindow(wxID_PRINTER_X_SCALE);
1833 wxTextCtrl *text2 = (wxTextCtrl *)FindWindow(wxID_PRINTER_Y_SCALE);
1834 wxTextCtrl *text3 = (wxTextCtrl *)FindWindow(wxID_PRINTER_X_TRANS);
1835 wxTextCtrl *text4 = (wxTextCtrl *)FindWindow(wxID_PRINTER_Y_TRANS);
1836 // wxTextCtrl *text_prt = (wxTextCtrl *)FindWindow(wxID_PRINTER_COMMAND);
1837 wxRadioBox *radio0 = (wxRadioBox *)FindWindow(wxID_PRINTER_ORIENTATION);
1838 wxRadioBox *radio1 = (wxRadioBox *)FindWindow(wxID_PRINTER_MODES);
1839
1840 StringToDouble (WXSTRINGCAST text1->GetValue (), &wxThePrintSetupData->printerScaleX);
1841 StringToDouble (WXSTRINGCAST text2->GetValue (), &wxThePrintSetupData->printerScaleY);
1842 StringToLong (WXSTRINGCAST text3->GetValue (), &wxThePrintSetupData->printerTranslateX);
1843 StringToLong (WXSTRINGCAST text4->GetValue (), &wxThePrintSetupData->printerTranslateY);
1844
1845 #ifdef __X__
1846 // wxThePrintSetupData->SetPrinterOptions(WXSTRINGCAST text0->GetValue ());
1847 // wxThePrintSetupData->SetPrinterCommand(WXSTRINGCAST text_prt->GetValue ());
1848 #endif
1849
1850 wxThePrintSetupData->SetPrinterOrientation((radio0->GetSelection() == 1 ? PS_LANDSCAPE : PS_PORTRAIT));
1851
1852 // C++ wants this
1853 switch ( radio1->GetSelection() ) {
1854 case 0: wxThePrintSetupData->SetPrinterMode(PS_PRINTER); break;
1855 case 1: wxThePrintSetupData->SetPrinterMode(PS_FILE); break;
1856 case 2: wxThePrintSetupData->SetPrinterMode(PS_PREVIEW); break;
1857 }
1858 return wxID_OK;
1859 }
1860 return wxID_CANCEL;
1861 }
1862
1863 // PostScript printer settings
1864 // RETAINED FOR BACKWARD COMPATIBILITY
1865 void wxSetPrinterCommand(const char *cmd)
1866 {
1867 wxThePrintSetupData->SetPrinterCommand(cmd);
1868 }
1869
1870 void wxSetPrintPreviewCommand(const char *cmd)
1871 {
1872 wxThePrintSetupData->SetPrintPreviewCommand(cmd);
1873 }
1874
1875 void wxSetPrinterOptions(const char *flags)
1876 {
1877 wxThePrintSetupData->SetPrinterOptions(flags);
1878 }
1879
1880 void wxSetPrinterFile(const char *f)
1881 {
1882 wxThePrintSetupData->SetPrinterFile(f);
1883 }
1884
1885 void wxSetPrinterOrientation(int orient)
1886 {
1887 wxThePrintSetupData->SetPrinterOrientation(orient);
1888 }
1889
1890 void wxSetPrinterScaling(double x, double y)
1891 {
1892 wxThePrintSetupData->SetPrinterScaling(x, y);
1893 }
1894
1895 void wxSetPrinterTranslation(long x, long y)
1896 {
1897 wxThePrintSetupData->SetPrinterTranslation(x, y);
1898 }
1899
1900 // 1 = Preview, 2 = print to file, 3 = send to printer
1901 void wxSetPrinterMode(int mode)
1902 {
1903 wxThePrintSetupData->SetPrinterMode(mode);
1904 }
1905
1906 void wxSetAFMPath(const char *f)
1907 {
1908 wxThePrintSetupData->SetAFMPath(f);
1909 }
1910
1911 // Get current values
1912 char *wxGetPrinterCommand()
1913 {
1914 return wxThePrintSetupData->GetPrinterCommand();
1915 }
1916
1917 char *wxGetPrintPreviewCommand()
1918 {
1919 return wxThePrintSetupData->GetPrintPreviewCommand();
1920 }
1921
1922 char *wxGetPrinterOptions()
1923 {
1924 return wxThePrintSetupData->GetPrinterOptions();
1925 }
1926
1927 char *wxGetPrinterFile()
1928 {
1929 return wxThePrintSetupData->GetPrinterFile();
1930 }
1931
1932 int wxGetPrinterOrientation()
1933 {
1934 return wxThePrintSetupData->GetPrinterOrientation();
1935 }
1936
1937 void wxGetPrinterScaling(double* x, double* y)
1938 {
1939 wxThePrintSetupData->GetPrinterScaling(x, y);
1940 }
1941
1942 void wxGetPrinterTranslation(long *x, long *y)
1943 {
1944 wxThePrintSetupData->GetPrinterTranslation(x, y);
1945 }
1946
1947 int wxGetPrinterMode()
1948 {
1949 return wxThePrintSetupData->GetPrinterMode();
1950 }
1951
1952 char *wxGetAFMPath()
1953 {
1954 return wxThePrintSetupData->GetAFMPath();
1955 }
1956
1957 /*
1958 * Print setup data
1959 */
1960
1961 wxPrintSetupData::wxPrintSetupData()
1962 {
1963 printerCommand = (char *) NULL;
1964 previewCommand = (char *) NULL;
1965 printerFlags = (char *) NULL;
1966 printerOrient = PS_PORTRAIT;
1967 printerScaleX = (double)1.0;
1968 printerScaleY = (double)1.0;
1969 printerTranslateX = 0;
1970 printerTranslateY = 0;
1971 // 1 = Preview, 2 = print to file, 3 = send to printer
1972 printerMode = 3;
1973 afmPath = (char *) NULL;
1974 paperName = (char *) NULL;
1975 printColour = TRUE;
1976 printerFile = (char *) NULL;
1977 }
1978
1979 wxPrintSetupData::~wxPrintSetupData()
1980 {
1981 if (printerCommand)
1982 delete[] printerCommand;
1983 if (previewCommand)
1984 delete[] previewCommand;
1985 if (printerFlags)
1986 delete[] printerFlags;
1987 if (afmPath)
1988 delete[] afmPath;
1989 if (paperName)
1990 delete[] paperName;
1991 if (printerFile)
1992 delete[] printerFile;
1993 }
1994
1995 void wxPrintSetupData::SetPrinterCommand(const char *cmd)
1996 {
1997 if (cmd == printerCommand)
1998 return;
1999
2000 if (printerCommand)
2001 delete[] printerCommand;
2002 if (cmd)
2003 printerCommand = copystring(cmd);
2004 else
2005 printerCommand = (char *) NULL;
2006 }
2007
2008 void wxPrintSetupData::SetPrintPreviewCommand(const char *cmd)
2009 {
2010 if (cmd == previewCommand)
2011 return;
2012
2013 if (previewCommand)
2014 delete[] previewCommand;
2015 if (cmd)
2016 previewCommand = copystring(cmd);
2017 else
2018 previewCommand = (char *) NULL;
2019 }
2020
2021 void wxPrintSetupData::SetPaperName(const char *name)
2022 {
2023 if (name == paperName)
2024 return;
2025
2026 if (paperName)
2027 delete[] paperName;
2028 if (name)
2029 paperName = copystring(name);
2030 else
2031 paperName = (char *) NULL;
2032 }
2033
2034 void wxPrintSetupData::SetPrinterOptions(const char *flags)
2035 {
2036 if (printerFlags == flags)
2037 return;
2038
2039 if (printerFlags)
2040 delete[] printerFlags;
2041 if (flags)
2042 printerFlags = copystring(flags);
2043 else
2044 printerFlags = (char *) NULL;
2045 }
2046
2047 void wxPrintSetupData::SetPrinterFile(const char *f)
2048 {
2049 if (f == printerFile)
2050 return;
2051
2052 if (printerFile)
2053 delete[] printerFile;
2054 if (f)
2055 printerFile = copystring(f);
2056 else
2057 printerFile = (char *) NULL;
2058 }
2059
2060 void wxPrintSetupData::SetPrinterOrientation(int orient)
2061 {
2062 printerOrient = orient;
2063 }
2064
2065 void wxPrintSetupData::SetPrinterScaling(double x, double y)
2066 {
2067 printerScaleX = x;
2068 printerScaleY = y;
2069 }
2070
2071 void wxPrintSetupData::SetPrinterTranslation(long x, long y)
2072 {
2073 printerTranslateX = x;
2074 printerTranslateY = y;
2075 }
2076
2077 // 1 = Preview, 2 = print to file, 3 = send to printer
2078 void wxPrintSetupData::SetPrinterMode(int mode)
2079 {
2080 printerMode = mode;
2081 }
2082
2083 void wxPrintSetupData::SetAFMPath(const char *f)
2084 {
2085 if (f == afmPath)
2086 return;
2087
2088 if (afmPath)
2089 delete[] afmPath;
2090 if (f)
2091 afmPath = copystring(f);
2092 else
2093 afmPath = (char *) NULL;
2094 }
2095
2096 void wxPrintSetupData::SetColour(bool col)
2097 {
2098 printColour = col;
2099 }
2100
2101 // Get current values
2102 char *wxPrintSetupData::GetPrinterCommand()
2103 {
2104 return printerCommand;
2105 }
2106
2107 char *wxPrintSetupData::GetPrintPreviewCommand()
2108 {
2109 return previewCommand;
2110 }
2111
2112 char *wxPrintSetupData::GetPrinterOptions()
2113 {
2114 return printerFlags;
2115 }
2116
2117 char *wxPrintSetupData::GetPrinterFile()
2118 {
2119 return printerFile;
2120 }
2121
2122 char *wxPrintSetupData::GetPaperName()
2123 {
2124 return paperName;
2125 }
2126
2127 int wxPrintSetupData::GetPrinterOrientation()
2128 {
2129 return printerOrient;
2130 }
2131
2132 void wxPrintSetupData::GetPrinterScaling(double *x, double *y)
2133 {
2134 *x = printerScaleX;
2135 *y = printerScaleY;
2136 }
2137
2138 void wxPrintSetupData::GetPrinterTranslation(long *x, long *y)
2139 {
2140 *x = printerTranslateX;
2141 *y = printerTranslateY;
2142 }
2143
2144 int wxPrintSetupData::GetPrinterMode()
2145 {
2146 return printerMode;
2147 }
2148
2149 char *wxPrintSetupData::GetAFMPath()
2150 {
2151 return afmPath;
2152 }
2153
2154 bool wxPrintSetupData::GetColour()
2155 {
2156 return printColour;
2157 }
2158
2159 void wxPrintSetupData::operator=(wxPrintSetupData& data)
2160 {
2161 SetPrinterCommand(data.GetPrinterCommand());
2162 SetPrintPreviewCommand(data.GetPrintPreviewCommand());
2163 SetPrinterOptions(data.GetPrinterOptions());
2164 long x, y;
2165 data.GetPrinterTranslation(&x, &y);
2166 SetPrinterTranslation(x, y);
2167
2168 double x1, y1;
2169 data.GetPrinterScaling(&x1, &y1);
2170 SetPrinterScaling(x1, y1);
2171
2172 SetPrinterOrientation(data.GetPrinterOrientation());
2173 SetPrinterMode(data.GetPrinterMode());
2174 SetAFMPath(data.GetAFMPath());
2175 SetPaperName(data.GetPaperName());
2176 SetColour(data.GetColour());
2177 }
2178
2179 void wxInitializePrintSetupData(bool init)
2180 {
2181 if (init)
2182 {
2183 wxThePrintSetupData = new wxPrintSetupData;
2184
2185 wxThePrintSetupData->SetPrintPreviewCommand(PS_VIEWER_PROG);
2186 wxThePrintSetupData->SetPrinterOrientation(PS_PORTRAIT);
2187 wxThePrintSetupData->SetPrinterMode(PS_PREVIEW);
2188 wxThePrintSetupData->SetPaperName(_("A4 210 x 297 mm"));
2189
2190 // Could have a .ini file to read in some defaults
2191 // - and/or use environment variables, e.g. WXWIN
2192 #ifdef __VMS__
2193 wxThePrintSetupData->SetPrinterCommand("print");
2194 wxThePrintSetupData->SetPrinterOptions("/nonotify/queue=psqueue");
2195 wxThePrintSetupData->SetAFMPath("sys$ps_font_metrics:");
2196 #endif
2197 #ifdef __WXMSW__
2198 wxThePrintSetupData->SetPrinterCommand("print");
2199 wxThePrintSetupData->SetAFMPath("c:\\windows\\system\\");
2200 wxThePrintSetupData->SetPrinterOptions(NULL);
2201 #endif
2202 #if !defined(__VMS__) && !defined(__WXMSW__)
2203 wxThePrintSetupData->SetPrinterCommand("lpr");
2204 wxThePrintSetupData->SetPrinterOptions((char *) NULL);
2205 wxThePrintSetupData->SetAFMPath((char *) NULL);
2206 #endif
2207 }
2208 else
2209 {
2210 if (wxThePrintSetupData)
2211 delete wxThePrintSetupData;
2212 wxThePrintSetupData = (wxPrintSetupData *) NULL;
2213 }
2214 }
2215
2216 /*
2217 * Paper size database for PostScript
2218 */
2219
2220 wxPrintPaperType::wxPrintPaperType(const char *name, int wmm, int hmm, int wp, int hp)
2221 {
2222 widthMM = wmm;
2223 heightMM = hmm;
2224 widthPixels = wp;
2225 heightPixels = hp;
2226 pageName = copystring(name);
2227 }
2228
2229 wxPrintPaperType::~wxPrintPaperType()
2230 {
2231 delete[] pageName;
2232 }
2233
2234 /*
2235 * Print paper database for PostScript
2236 */
2237
2238 #if !USE_SHARED_LIBRARIES
2239 IMPLEMENT_DYNAMIC_CLASS(wxPrintPaperDatabase, wxList)
2240 #endif
2241
2242 wxPrintPaperDatabase::wxPrintPaperDatabase():wxList(wxKEY_STRING)
2243 {
2244 DeleteContents(TRUE);
2245 }
2246
2247 wxPrintPaperDatabase::~wxPrintPaperDatabase()
2248 {
2249 }
2250
2251 void wxPrintPaperDatabase::CreateDatabase()
2252 {
2253 // Need correct values for page size in pixels.
2254 // Each unit is one 'point' = 1/72 of an inch.
2255 // NOTE: WE NEED ALSO TO MAKE ADJUSTMENTS WHEN TRANSLATING
2256 // in wxPostScriptDC code, so we can start from top left.
2257 // So access this database and translate by appropriate number
2258 // of points for this paper size. OR IS IT OK ALREADY?
2259 // Can't remember where the PostScript origin is by default.
2260 // Heck, someone will know how to make it hunky-dory...
2261 // JACS 25/5/95
2262
2263 AddPaperType(_("A4 210 x 297 mm"), 210, 297, 595, 842);
2264 AddPaperType(_("A3 297 x 420 mm"), 297, 420, 842, 1191);
2265 AddPaperType(_("Letter 8 1/2 x 11 in"), 216, 279, 612, 791);
2266 AddPaperType(_("Legal 8 1/2 x 14 in"), 216, 356, 612, 1009);
2267 }
2268
2269 void wxPrintPaperDatabase::ClearDatabase()
2270 {
2271 Clear();
2272 }
2273
2274 void wxPrintPaperDatabase::AddPaperType(const char *name, int wmm, int hmm, int wp, int hp)
2275 {
2276 Append(name, new wxPrintPaperType(name, wmm, hmm, wp, hp));
2277 }
2278
2279 wxPrintPaperType *wxPrintPaperDatabase::FindPaperType(const char *name)
2280 {
2281 wxNode *node = Find(name);
2282 if (node)
2283 return (wxPrintPaperType *)node->Data();
2284 else
2285 return (wxPrintPaperType *) NULL;
2286 }
2287
2288 /*
2289 * Initialization/cleanup module
2290 */
2291
2292 bool wxPostScriptModule::OnInit()
2293 {
2294 wxInitializePrintSetupData();
2295 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
2296 wxThePrintPaperDatabase->CreateDatabase();
2297
2298 return TRUE;
2299 }
2300
2301 void wxPostScriptModule::OnExit()
2302 {
2303 wxInitializePrintSetupData(FALSE);
2304 delete wxThePrintPaperDatabase;
2305 wxThePrintPaperDatabase = NULL;
2306 }
2307
2308
2309 #endif