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