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