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