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