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