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