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