source: git/src/printwx.cc @ a72ed95

line_contentsstereotravis-osx
Last change on this file since a72ed95 was a72ed95, checked in by Olly Betts <olly@…>, 5 years ago
  • .gitignore,Makefile.am,debian/survex.install,doc/,lib/,src/: Aven's

support for reading colours and font sizes for printouts from
print.ini has never worked - the contents of the ini files are
ignored due to a bug which has been there since the code was added
in 2005 - but nobody has ever complained. So just strip out that
code entirely - we should support setting the colours and font sizes,
but a GUI interface for setting them would be better.

  • Property mode set to 100644
File size: 57.3 KB
Line 
1/* printwx.cc */
2/* wxWidgets specific parts of Survex wxWidgets printing code */
3/* Copyright (C) 1993-2003,2004,2005,2006,2010,2011,2012,2013,2014,2015 Olly Betts
4 * Copyright (C) 2001,2004 Philip Underwood
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 */
20
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
24
25#include <wx/confbase.h>
26#include <wx/filename.h>
27#include <wx/print.h>
28#include <wx/printdlg.h>
29#include <wx/spinctrl.h>
30#include <wx/radiobox.h>
31#include <wx/statbox.h>
32#include <wx/valgen.h>
33
34#include <vector>
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <math.h>
39#include <string.h>
40#include <ctype.h>
41#include <float.h>
42#include <limits.h>
43
44#include "debug.h" /* for BUG and SVX_ASSERT */
45#include "export.h"
46#include "filelist.h"
47#include "filename.h"
48#include "message.h"
49#include "useful.h"
50
51#include "aven.h"
52#include "avenprcore.h"
53#include "mainfrm.h"
54#include "printwx.h"
55
56using namespace std;
57
58// How many decimal points to show on angles:
59#define ANGLE_DP 1
60
61#if ANGLE_DP == 0
62# define ANGLE_FMT wxT("%03.f")
63# define ANGLE2_FMT wxT("%.f")
64#elif ANGLE_DP == 1
65# define ANGLE_FMT wxT("%05.1f")
66# define ANGLE2_FMT wxT("%.1f")
67#elif ANGLE_DP == 2
68# define ANGLE_FMT wxT("%06.2f")
69# define ANGLE2_FMT wxT("%.2f")
70#else
71# error Need to add ANGLE_FMT and ANGLE2_FMT for the currently set ANGLE_DP
72#endif
73
74static wxString
75format_angle(const wxChar * fmt, double angle)
76{
77    wxString s;
78    s.Printf(fmt, angle);
79    size_t dot = s.find('.');
80    size_t i = s.size();
81    while (i > dot) {
82        --i;
83        if (s[i] != '0') {
84            if (i != dot) ++i;
85            s.resize(i);
86            break;
87        }
88    }
89    s += wmsg(/*°*/344);
90    return s;
91}
92
93enum {
94        svx_EXPORT = 1200,
95        svx_FORMAT,
96        svx_SCALE,
97        svx_BEARING,
98        svx_TILT,
99        svx_LEGS,
100        svx_STATIONS,
101        svx_NAMES,
102        svx_XSECT,
103        svx_WALLS,
104        svx_PASSAGES,
105        svx_BORDERS,
106        svx_BLANKS,
107        svx_LEGEND,
108        svx_SURFACE,
109        svx_PLAN,
110        svx_ELEV,
111        svx_ENTS,
112        svx_FIXES,
113        svx_EXPORTS,
114        svx_PROJ_LABEL,
115        svx_PROJ,
116        svx_GRID,
117        svx_TEXT_HEIGHT,
118        svx_MARKER_SIZE,
119        svx_CENTRED,
120        svx_FULLCOORDS
121};
122
123class BitValidator : public wxValidator {
124    // Disallow assignment.
125    BitValidator & operator=(const BitValidator&);
126
127  protected:
128    int * val;
129
130    int mask;
131
132  public:
133    BitValidator(int * val_, int mask_)
134        : val(val_), mask(mask_) { }
135
136    BitValidator(const BitValidator &o) : wxValidator() {
137        Copy(o);
138    }
139
140    ~BitValidator() { }
141
142    wxObject *Clone() const { return new BitValidator(val, mask); }
143
144    bool Copy(const BitValidator& o) {
145        wxValidator::Copy(o);
146        val = o.val;
147        mask = o.mask;
148        return true;
149    }
150
151    bool Validate(wxWindow *) { return true; }
152
153    bool TransferToWindow() {
154        if (!m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)))
155            return false;
156        ((wxCheckBox*)m_validatorWindow)->SetValue(*val & mask);
157        return true;
158    }
159
160    bool TransferFromWindow() {
161        if (!m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)))
162            return false;
163        if (((wxCheckBox*)m_validatorWindow)->IsChecked())
164            *val |= mask;
165        else
166            *val &= ~mask;
167        return true;
168    }
169};
170
171class svxPrintout : public wxPrintout {
172    MainFrm *mainfrm;
173    layout *m_layout;
174    wxPageSetupDialogData* m_data;
175    wxDC* pdc;
176    // Currently unused, but "skip blank pages" would use it.
177    static const int cur_pass = 0;
178
179    wxPen *pen_frame, *pen_cross, *pen_surface_leg, *pen_leg;
180    wxColour colour_text, colour_labels, colour_frame, colour_leg;
181    wxColour colour_cross,colour_surface_leg;
182
183    long x_t, y_t;
184    double font_scaling_x, font_scaling_y;
185    wxFont * current_font;
186
187    int check_intersection(long x_p, long y_p);
188    void draw_info_box();
189    void draw_scale_bar(double x, double y, double MaxLength);
190    int next_page(int *pstate, char **q, int pageLim);
191    void drawticks(border clip, int tsize, int x, int y);
192
193    void MOVEMM(double X, double Y) {
194        MoveTo((long)(X * m_layout->scX), (long)(Y * m_layout->scY));
195    }
196    void DRAWMM(double X, double Y) {
197        DrawTo((long)(X * m_layout->scX), (long)(Y * m_layout->scY));
198    }
199    void MoveTo(long x, long y);
200    void DrawTo(long x, long y);
201    void DrawCross(long x, long y);
202    void SetFont(int fontcode);
203    void SetColour(int colourcode);
204    void WriteString(const wxString & s);
205    void DrawEllipse(long x, long y, long r, long R);
206    void SolidRectangle(long x, long y, long w, long h);
207    int Pre();
208    void NewPage(int pg, int pagesX, int pagesY);
209    void PlotLR(const vector<XSect> & centreline);
210    void PlotUD(const vector<XSect> & centreline);
211    void Init();
212  public:
213    svxPrintout(MainFrm *mainfrm, layout *l, wxPageSetupDialogData *data, const wxString & title);
214    bool OnPrintPage(int pageNum);
215    void GetPageInfo(int *minPage, int *maxPage,
216                     int *pageFrom, int *pageTo);
217    bool HasPage(int pageNum);
218    void OnBeginPrinting();
219    void OnEndPrinting();
220};
221
222BEGIN_EVENT_TABLE(svxPrintDlg, wxDialog)
223    EVT_CHOICE(svx_FORMAT, svxPrintDlg::OnChange)
224    EVT_TEXT(svx_SCALE, svxPrintDlg::OnChange)
225    EVT_COMBOBOX(svx_SCALE, svxPrintDlg::OnChange)
226    EVT_SPINCTRL(svx_BEARING, svxPrintDlg::OnChangeSpin)
227    EVT_SPINCTRL(svx_TILT, svxPrintDlg::OnChangeSpin)
228    EVT_BUTTON(wxID_PRINT, svxPrintDlg::OnPrint)
229    EVT_BUTTON(svx_EXPORT, svxPrintDlg::OnExport)
230    EVT_BUTTON(wxID_CANCEL, svxPrintDlg::OnCancel)
231#ifdef AVEN_PRINT_PREVIEW
232    EVT_BUTTON(wxID_PREVIEW, svxPrintDlg::OnPreview)
233#endif
234    EVT_BUTTON(svx_PLAN, svxPrintDlg::OnPlan)
235    EVT_BUTTON(svx_ELEV, svxPrintDlg::OnElevation)
236    EVT_UPDATE_UI(svx_PLAN, svxPrintDlg::OnPlanUpdate)
237    EVT_UPDATE_UI(svx_ELEV, svxPrintDlg::OnElevationUpdate)
238    EVT_CHECKBOX(svx_LEGS, svxPrintDlg::OnChange)
239    EVT_CHECKBOX(svx_STATIONS, svxPrintDlg::OnChange)
240    EVT_CHECKBOX(svx_NAMES, svxPrintDlg::OnChange)
241    EVT_CHECKBOX(svx_SURFACE, svxPrintDlg::OnChange)
242    EVT_CHECKBOX(svx_ENTS, svxPrintDlg::OnChange)
243    EVT_CHECKBOX(svx_FIXES, svxPrintDlg::OnChange)
244    EVT_CHECKBOX(svx_EXPORTS, svxPrintDlg::OnChange)
245END_EVENT_TABLE()
246
247static wxString scales[] = {
248    wxT(""),
249    wxT("25"),
250    wxT("50"),
251    wxT("100"),
252    wxT("250"),
253    wxT("500"),
254    wxT("1000"),
255    wxT("2500"),
256    wxT("5000"),
257    wxT("10000"),
258    wxT("25000"),
259    wxT("50000"),
260    wxT("100000")
261};
262
263static wxString formats[] = {
264    wxT("DXF"),
265    wxT("EPS"),
266    wxT("GPX"),
267    wxT("HPGL"),
268    wxT("JSON"),
269    wxT("KML"),
270    wxT("Plot"),
271    wxT("Skencil"),
272    wxT("SVG")
273};
274
275#if 0
276static wxString projs[] = {
277    /* CUCC Austria: */
278    wxT("+proj=tmerc +lat_0=0 +lon_0=13d20 +k=1 +x_0=0 +y_0=-5200000 +ellps=bessel +towgs84=577.326,90.129,463.919,5.137,1.474,5.297,2.4232"),
279    /* British grid SD (Yorkshire): */
280    wxT("+proj=tmerc +lat_0=49d +lon_0=-2d +k=0.999601 +x_0=100000 +y_0=-500000 +ellps=airy +towgs84=375,-111,431,0,0,0,0"),
281    /* British full grid reference: */
282    wxT("+proj=tmerc +lat_0=49d +lon_0=-2d +k=0.999601 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=375,-111,431,0,0,0,0")
283};
284#endif
285
286static unsigned format_info[] = {
287    LABELS|LEGS|SURF|STNS|PASG|XSECT|WALLS|MARKER_SIZE|TEXT_HEIGHT|GRID|FULL_COORDS,
288    LABELS|LEGS|SURF|STNS,
289    LABELS|LEGS|SURF|ENTS|FIXES|EXPORTS|PROJ|EXPORT_3D,
290    LABELS|LEGS|SURF|STNS|CENTRED,
291    LEGS|CENTRED|EXPORT_3D,
292    LABELS|ENTS|FIXES|EXPORTS|PROJ|EXPORT_3D,
293    LABELS|LEGS|SURF,
294    LABELS|LEGS|SURF|STNS|MARKER_SIZE|GRID|SCALE,
295    LABELS|LEGS|SURF|STNS|PASG|XSECT|WALLS|MARKER_SIZE|TEXT_HEIGHT|SCALE
296};
297
298static const char * extension[] = {
299    ".dxf",
300    ".eps",
301    ".gpx",
302    ".hpgl",
303    ".json",
304    ".kml",
305    ".plt",
306    ".sk",
307    ".svg"
308};
309
310static int msg_filetype[] = {
311    /*DXF files*/411,
312    /*EPS files*/412,
313    /*GPX files*/413,
314    /* TRANSLATORS: Here "plotter" refers to a machine which draws a printout
315     * on a (usually large) sheet of paper using a pen mounted in a motorised
316     * mechanism. */
317    /*HPGL for plotters*/414,
318    /*JSON files*/445,
319    /*KML files*/444,
320    /* TRANSLATORS: "Compass" and "Carto" are the names of software packages,
321     * so should not be translated:
322     * http://www.fountainware.com/compass/
323     * http://www.psc-cavers.org/carto/ */
324    /*Compass PLT for use with Carto*/415,
325    /* TRANSLATORS: "Skencil" is the name of a software package, so should not be
326     * translated: http://www.skencil.org/ */
327    /*Skencil files*/416,
328    /*SVG files*/417
329};
330
331// there are three jobs to do here...
332// User <-> wx - this should possibly be done in a separate file
333svxPrintDlg::svxPrintDlg(MainFrm* mainfrm_, const wxString & filename,
334                         const wxString & title, const wxString & cs_proj,
335                         const wxString & datestamp, time_t datestamp_numeric,
336                         double angle, double tilt_angle,
337                         bool labels, bool crosses, bool legs, bool surf,
338                         bool tubes, bool ents, bool fixes, bool exports,
339                         bool printing, bool close_after_)
340        : wxDialog(mainfrm_, -1, wxString(printing ?
341                                          /* TRANSLATORS: Title of the print
342                                           * dialog */
343                                          wmsg(/*Print*/399) :
344                                          /* TRANSLATORS: Title of the export
345                                           * dialog */
346                                          wmsg(/*Export*/383))),
347          m_layout(printing ? wxGetApp().GetPageSetupDialogData() : NULL),
348          m_File(filename), mainfrm(mainfrm_), close_after(close_after_)
349{
350    m_scale = NULL;
351    m_printSize = NULL;
352    m_bearing = NULL;
353    m_tilt = NULL;
354    m_format = NULL;
355    int show_mask = 0;
356    if (labels)
357        show_mask |= LABELS;
358    if (crosses)
359        show_mask |= STNS;
360    if (legs)
361        show_mask |= LEGS;
362    if (surf)
363        show_mask |= SURF;
364    if (tubes)
365        show_mask |= XSECT|WALLS|PASG;
366    if (ents)
367        show_mask |= ENTS;
368    if (fixes)
369        show_mask |= FIXES;
370    if (exports)
371        show_mask |= EXPORTS;
372    m_layout.show_mask = show_mask;
373    m_layout.datestamp = datestamp;
374    m_layout.datestamp_numeric = datestamp_numeric;
375    m_layout.rot = angle;
376    m_layout.title = title;
377    m_layout.cs_proj = cs_proj;
378    if (mainfrm->IsExtendedElevation()) {
379        m_layout.view = layout::EXTELEV;
380        if (m_layout.rot != 0.0 && m_layout.rot != 180.0) m_layout.rot = 0;
381        m_layout.tilt = 0;
382    } else {
383        m_layout.tilt = tilt_angle;
384        if (m_layout.tilt == -90.0) {
385            m_layout.view = layout::PLAN;
386        } else if (m_layout.tilt == 0.0) {
387            m_layout.view = layout::ELEV;
388        } else {
389            m_layout.view = layout::TILT;
390        }
391    }
392
393    /* setup our print dialog*/
394    wxBoxSizer* v1 = new wxBoxSizer(wxVERTICAL);
395    wxBoxSizer* h1 = new wxBoxSizer(wxHORIZONTAL); // holds controls
396    /* TRANSLATORS: Used as a label for the surrounding box for the "Bearing"
397     * and "Tilt angle" fields, and the "Plan view" and "Elevation" buttons in
398     * the "what to print/export" dialog. */
399    m_viewbox = new wxStaticBoxSizer(new wxStaticBox(this, -1, wmsg(/*View*/283)), wxVERTICAL);
400    /* TRANSLATORS: Used as a label for the surrounding box for the "survey
401     * legs" "stations" "names" etc checkboxes in the "what to print" dialog.
402     * "Elements" isn’t a good name for this but nothing better has yet come to
403     * mind! */
404    wxBoxSizer* v3 = new wxStaticBoxSizer(new wxStaticBox(this, -1, wmsg(/*Elements*/256)), wxVERTICAL);
405    wxBoxSizer* h2 = new wxBoxSizer(wxHORIZONTAL);
406    wxBoxSizer* h3 = new wxBoxSizer(wxHORIZONTAL); // holds buttons
407
408    if (!printing) {
409        wxStaticText* label;
410        label = new wxStaticText(this, -1, wxString(wmsg(/*Export format*/410)));
411        const size_t n_formats = sizeof(formats) / sizeof(formats[0]);
412        m_format = new wxChoice(this, svx_FORMAT,
413                                wxDefaultPosition, wxDefaultSize,
414                                n_formats, formats);
415        unsigned current_format = 0;
416        wxConfigBase * cfg = wxConfigBase::Get();
417        wxString s;
418        if (cfg->Read(wxT("export_format"), &s, wxString())) {
419            for (unsigned i = 0; i != n_formats; ++i) {
420                if (s == formats[i]) {
421                    current_format = i;
422                    break;
423                }
424            }
425        }
426        m_format->SetSelection(current_format);
427        wxBoxSizer* formatbox = new wxBoxSizer(wxHORIZONTAL);
428        formatbox->Add(label, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
429        formatbox->Add(m_format, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
430
431        v1->Add(formatbox, 0, wxALIGN_LEFT|wxALL, 0);
432    }
433
434    wxStaticText* label;
435    label = new wxStaticText(this, -1, wxString(wmsg(/*Scale*/154)) + wxT(" 1:"));
436    if (scales[0].empty()) {
437        if (printing) {
438            /* TRANSLATORS: used in the scale drop down selector in the print
439             * dialog the implicit meaning is "choose a suitable scale to fit
440             * the plot on a single page", but we need something shorter */
441            scales[0].assign(wmsg(/*One page*/258));
442        } else {
443            scales[0].assign(wxT("1000"));
444        }
445    }
446    m_scale = new wxComboBox(this, svx_SCALE, scales[0], wxDefaultPosition,
447                             wxDefaultSize, sizeof(scales) / sizeof(scales[0]),
448                             scales);
449    m_scalebox = new wxBoxSizer(wxHORIZONTAL);
450    m_scalebox->Add(label, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
451    m_scalebox->Add(m_scale, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
452
453    m_viewbox->Add(m_scalebox, 0, wxALIGN_LEFT|wxALL, 0);
454
455    if (printing) {
456        // Make the dummy string wider than any sane value and use that to
457        // fix the width of the control so the sizers allow space for bigger
458        // page layouts.
459        m_printSize = new wxStaticText(this, -1, wxString::Format(wmsg(/*%d pages (%dx%d)*/257), 9604, 98, 98));
460        m_viewbox->Add(m_printSize, 0, wxALIGN_LEFT|wxALL, 5);
461    }
462
463    /* FIXME:
464     * svx_GRID, // double - spacing, default: 100m
465     * svx_TEXT_HEIGHT, // default 0.6
466     * svx_MARKER_SIZE // default 0.8
467     */
468
469    if (m_layout.view != layout::EXTELEV) {
470        wxFlexGridSizer* anglebox = new wxFlexGridSizer(2);
471        wxStaticText * brg_label, * tilt_label;
472        brg_label = new wxStaticText(this, -1, wmsg(/*Bearing*/259));
473        anglebox->Add(brg_label, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxALL, 5);
474        m_bearing = new wxSpinCtrlDouble(this, svx_BEARING);
475        m_bearing->SetRange(0.0, 360.0);
476        m_bearing->SetDigits(ANGLE_DP);
477        anglebox->Add(m_bearing, 0, wxALIGN_CENTER|wxALL, 5);
478        /* TRANSLATORS: Used in the print dialog: */
479        tilt_label = new wxStaticText(this, -1, wmsg(/*Tilt angle*/263));
480        anglebox->Add(tilt_label, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxALL, 5);
481        m_tilt = new wxSpinCtrlDouble(this, svx_TILT);
482        m_tilt->SetRange(-90.0, 90.0);
483        m_tilt->SetDigits(ANGLE_DP);
484        anglebox->Add(m_tilt, 0, wxALIGN_CENTER|wxALL, 5);
485
486        m_viewbox->Add(anglebox, 0, wxALIGN_LEFT|wxALL, 0);
487
488        wxBoxSizer * planelevsizer = new wxBoxSizer(wxHORIZONTAL);
489        planelevsizer->Add(new wxButton(this, svx_PLAN, wmsg(/*P&lan view*/117)),
490                           0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
491        planelevsizer->Add(new wxButton(this, svx_ELEV, wmsg(/*&Elevation*/285)),
492                           0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
493
494        m_viewbox->Add(planelevsizer, 0, wxALIGN_LEFT|wxALL, 5);
495    }
496
497    /* TRANSLATORS: Here a "survey leg" is a set of measurements between two
498     * "survey stations". */
499    v3->Add(new wxCheckBox(this, svx_LEGS, wmsg(/*Underground Survey Legs*/262),
500                           wxDefaultPosition, wxDefaultSize, 0,
501                           BitValidator(&m_layout.show_mask, LEGS)),
502            0, wxALIGN_LEFT|wxALL, 2);
503    /* TRANSLATORS: Here a "survey leg" is a set of measurements between two
504     * "survey stations". */
505    v3->Add(new wxCheckBox(this, svx_SURFACE, wmsg(/*Sur&face Survey Legs*/403),
506                           wxDefaultPosition, wxDefaultSize, 0,
507                           BitValidator(&m_layout.show_mask, SURF)),
508            0, wxALIGN_LEFT|wxALL, 2);
509    v3->Add(new wxCheckBox(this, svx_STATIONS, wmsg(/*Crosses*/261),
510                           wxDefaultPosition, wxDefaultSize, 0,
511                           BitValidator(&m_layout.show_mask, STNS)),
512            0, wxALIGN_LEFT|wxALL, 2);
513    v3->Add(new wxCheckBox(this, svx_NAMES, wmsg(/*Station Names*/260),
514                           wxDefaultPosition, wxDefaultSize, 0,
515                           BitValidator(&m_layout.show_mask, LABELS)),
516            0, wxALIGN_LEFT|wxALL, 2);
517    v3->Add(new wxCheckBox(this, svx_ENTS, wmsg(/*Entrances*/418),
518                           wxDefaultPosition, wxDefaultSize, 0,
519                           BitValidator(&m_layout.show_mask, ENTS)),
520            0, wxALIGN_LEFT|wxALL, 2);
521    v3->Add(new wxCheckBox(this, svx_FIXES, wmsg(/*Fixed Points*/419),
522                           wxDefaultPosition, wxDefaultSize, 0,
523                           BitValidator(&m_layout.show_mask, FIXES)),
524            0, wxALIGN_LEFT|wxALL, 2);
525    v3->Add(new wxCheckBox(this, svx_EXPORTS, wmsg(/*Exported Stations*/420),
526                           wxDefaultPosition, wxDefaultSize, 0,
527                           BitValidator(&m_layout.show_mask, EXPORTS)),
528            0, wxALIGN_LEFT|wxALL, 2);
529    v3->Add(new wxCheckBox(this, svx_XSECT, wmsg(/*Cross-sections*/393),
530                           wxDefaultPosition, wxDefaultSize, 0,
531                           BitValidator(&m_layout.show_mask, XSECT)),
532            0, wxALIGN_LEFT|wxALL, 2);
533    if (!printing) {
534        v3->Add(new wxCheckBox(this, svx_WALLS, wmsg(/*Walls*/394),
535                               wxDefaultPosition, wxDefaultSize, 0,
536                               BitValidator(&m_layout.show_mask, WALLS)),
537                0, wxALIGN_LEFT|wxALL, 2);
538        v3->Add(new wxCheckBox(this, svx_PASSAGES, wmsg(/*Passages*/395),
539                               wxDefaultPosition, wxDefaultSize, 0,
540                               BitValidator(&m_layout.show_mask, PASG)),
541                0, wxALIGN_LEFT|wxALL, 2);
542        v3->Add(new wxCheckBox(this, svx_CENTRED, wmsg(/*Origin in centre*/421),
543                               wxDefaultPosition, wxDefaultSize, 0,
544                               BitValidator(&m_layout.show_mask, CENTRED)),
545                0, wxALIGN_LEFT|wxALL, 2);
546        v3->Add(new wxCheckBox(this, svx_FULLCOORDS, wmsg(/*Full coordinates*/422),
547                               wxDefaultPosition, wxDefaultSize, 0,
548                               BitValidator(&m_layout.show_mask, FULL_COORDS)),
549                0, wxALIGN_LEFT|wxALL, 2);
550    }
551    if (printing) {
552        /* TRANSLATORS: used in the print dialog - controls drawing lines
553         * around each page */
554        v3->Add(new wxCheckBox(this, svx_BORDERS, wmsg(/*Page Borders*/264),
555                               wxDefaultPosition, wxDefaultSize, 0,
556                               wxGenericValidator(&m_layout.Border)),
557                0, wxALIGN_LEFT|wxALL, 2);
558        /* TRANSLATORS: will be used in the print dialog - check this to print
559         * blank pages (otherwise they’ll be skipped to save paper) */
560//      m_blanks = new wxCheckBox(this, svx_BLANKS, wmsg(/*Blank Pages*/266));
561//      v3->Add(m_blanks, 0, wxALIGN_LEFT|wxALL, 2);
562        /* TRANSLATORS: As in the legend on a map.  Used in the print dialog -
563         * controls drawing the box at the lower left with survey name, view
564         * angles, etc */
565        v3->Add(new wxCheckBox(this, svx_LEGEND, wmsg(/*Legend*/265),
566                               wxDefaultPosition, wxDefaultSize, 0,
567                               wxGenericValidator(&m_layout.Legend)),
568                0, wxALIGN_LEFT|wxALL, 2);
569    }
570
571    h1->Add(v3, 0, wxALIGN_LEFT|wxALL, 5);
572    h1->Add(m_viewbox, 0, wxALIGN_LEFT|wxLEFT, 5);
573
574    /* TRANSLATORS: The PROJ library is used to do coordinate transformations
575     * (https://trac.osgeo.org/proj/) - if the .3d file doesn't contain details
576     * of the coordinate projection in use, the user must specify it here for
577     * export formats which need to know it (e.g. GPX).
578     */
579    h2->Add(new wxStaticText(this, svx_PROJ_LABEL, wmsg(/*Coordinate projection*/440)),
580            0, wxLEFT|wxALIGN_CENTRE_VERTICAL, 5);
581    long style = 0;
582    if (!m_layout.cs_proj.empty()) {
583        // If the input file specified the coordinate system, don't let the
584        // user mess with it.
585        style = wxTE_READONLY;
586    } else {
587#if 0 // FIXME: Is it a good idea to save this?
588        wxConfigBase * cfg = wxConfigBase::Get();
589        wxString input_projection;
590        cfg->Read(wxT("input_projection"), &input_projection);
591        if (!input_projection.empty())
592            proj_edit.SetValue(input_projection);
593#endif
594    }
595    wxTextCtrl * proj_edit = new wxTextCtrl(this, svx_PROJ, m_layout.cs_proj,
596                                            wxDefaultPosition, wxDefaultSize,
597                                            style);
598    h2->Add(proj_edit, 1, wxALL|wxEXPAND|wxALIGN_CENTRE_VERTICAL, 5);
599    v1->Add(h2, 0, wxALIGN_LEFT|wxEXPAND, 5);
600
601    v1->Add(h1, 0, wxALIGN_LEFT|wxALL, 5);
602
603    // When we enable/disable checkboxes in the export dialog, ideally we'd
604    // like the dialog to resize, but not sure how to achieve that, so we
605    // add a stretchable spacer here so at least the buttons stay in the
606    // lower right corner.
607    v1->AddStretchSpacer();
608
609    wxButton * but;
610    but = new wxButton(this, wxID_CANCEL);
611    h3->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
612    if (printing) {
613#ifdef AVEN_PRINT_PREVIEW
614        but = new wxButton(this, wxID_PREVIEW);
615        h3->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
616        but = new wxButton(this, wxID_PRINT);
617#else
618        but = new wxButton(this, wxID_PRINT, wmsg(/*&Print…*/400));
619#endif
620    } else {
621        /* TRANSLATORS: The text on the action button in the "Export" settings
622         * dialog */
623        but = new wxButton(this, svx_EXPORT, wmsg(/*&Export…*/230));
624    }
625    but->SetDefault();
626    h3->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
627    v1->Add(h3, 0, wxALIGN_RIGHT|wxALL, 5);
628
629    SetAutoLayout(true);
630    SetSizer(v1);
631    v1->SetSizeHints(this);
632
633    LayoutToUI();
634    SomethingChanged(0);
635}
636
637void
638svxPrintDlg::OnPrint(wxCommandEvent&) {
639    SomethingChanged(0);
640    TransferDataFromWindow();
641    wxPageSetupDialogData * psdd = wxGetApp().GetPageSetupDialogData();
642    wxPrintDialogData pd(psdd->GetPrintData());
643    wxPrinter pr(&pd);
644    svxPrintout po(mainfrm, &m_layout, psdd, m_File);
645    if (pr.Print(this, &po, true)) {
646        // Close the print dialog if printing succeeded.
647        Destroy();
648    }
649}
650
651void
652svxPrintDlg::OnExport(wxCommandEvent&) {
653    UIToLayout();
654    TransferDataFromWindow();
655    wxString leaf;
656    wxFileName::SplitPath(m_File, NULL, NULL, &leaf, NULL, wxPATH_NATIVE);
657    unsigned format_idx = ((wxChoice*)FindWindow(svx_FORMAT))->GetSelection();
658    leaf += wxString::FromUTF8(extension[format_idx]);
659
660    wxString filespec = wmsg(msg_filetype[format_idx]);
661    filespec += wxT("|*");
662    filespec += wxString::FromUTF8(extension[format_idx]);
663    filespec += wxT("|");
664    filespec += wmsg(/*All files*/208);
665    filespec += wxT("|");
666    filespec += wxFileSelectorDefaultWildcardStr;
667
668    /* TRANSLATORS: Title of file dialog to choose name and type of exported
669     * file. */
670    wxFileDialog dlg(this, wmsg(/*Export as:*/401), wxString(), leaf,
671                     filespec, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
672    if (dlg.ShowModal() == wxID_OK) {
673        wxString input_projection = ((wxTextCtrl*)FindWindow(svx_PROJ))->GetValue();
674        double grid = 100; // metres
675        double text_height = 0.6;
676        double marker_size = 0.8;
677
678        try {
679            if (!Export(dlg.GetPath(), m_layout.title,
680                        m_layout.datestamp, m_layout.datestamp_numeric, mainfrm,
681                        m_layout.rot, m_layout.tilt, m_layout.show_mask,
682                        export_format(format_idx), input_projection.mb_str(),
683                        grid, text_height, marker_size, m_layout.Scale)) {
684                wxString m = wxString::Format(wmsg(/*Couldn’t write file “%s”*/402).c_str(),
685                                              m_File.c_str());
686                wxGetApp().ReportError(m);
687            }
688        } catch (const wxString & m) {
689            wxGetApp().ReportError(m);
690        }
691    }
692    Destroy();
693}
694
695#ifdef AVEN_PRINT_PREVIEW
696void
697svxPrintDlg::OnPreview(wxCommandEvent&) {
698    SomethingChanged(0);
699    TransferDataFromWindow();
700    wxPageSetupDialogData * psdd = wxGetApp().GetPageSetupDialogData();
701    wxPrintDialogData pd(psdd->GetPrintData());
702    wxPrintPreview* pv;
703    pv = new wxPrintPreview(new svxPrintout(mainfrm, &m_layout, psdd, m_File),
704                            new svxPrintout(mainfrm, &m_layout, psdd, m_File),
705                            &pd);
706    // TRANSLATORS: Title of the print preview dialog
707    wxPreviewFrame *frame = new wxPreviewFrame(pv, mainfrm, wmsg(/*Print Preview*/398));
708    frame->Initialize();
709
710    // Size preview frame so that all of the controlbar and canvas can be seen
711    // if possible.
712    int w, h;
713    // GetBestSize gives us the width needed to show the whole controlbar.
714    frame->GetBestSize(&w, &h);
715#ifdef __WXMAC__
716    // wxMac opens the preview window at minimum size by default.
717    // 360x480 is apparently enough to show A4 portrait.
718    if (h < 480 || w < 360) {
719        if (h < 480) h = 480;
720        if (w < 360) w = 360;
721    }
722#else
723    if (h < w) {
724        // On wxGTK at least, GetBestSize() returns much too small a height.
725        h = w * 6 / 5;
726    }
727#endif
728    // Ensure that we don't make the window bigger than the screen.
729    // Use wxGetClientDisplayRect() so we don't cover the MS Windows
730    // task bar either.
731    wxRect disp = wxGetClientDisplayRect();
732    if (w > disp.GetWidth()) w = disp.GetWidth();
733    if (h > disp.GetHeight()) h = disp.GetHeight();
734    // Centre the window within the "ClientDisplayRect".
735    int x = disp.GetLeft() + (disp.GetWidth() - w) / 2;
736    int y = disp.GetTop() + (disp.GetHeight() - h) / 2;
737    frame->SetSize(x, y, w, h);
738
739    frame->Show();
740}
741#endif
742
743void
744svxPrintDlg::OnPlan(wxCommandEvent&) {
745    m_tilt->SetValue(-90.0);
746    SomethingChanged(svx_TILT);
747}
748
749void
750svxPrintDlg::OnElevation(wxCommandEvent&) {
751    m_tilt->SetValue(0.0);
752    SomethingChanged(svx_TILT);
753}
754
755void
756svxPrintDlg::OnPlanUpdate(wxUpdateUIEvent& e) {
757    e.Enable(m_tilt->GetValue() != -90.0);
758}
759
760void
761svxPrintDlg::OnElevationUpdate(wxUpdateUIEvent& e) {
762    e.Enable(m_tilt->GetValue() != 0.0);
763}
764
765void
766svxPrintDlg::OnChangeSpin(wxSpinEvent& e) {
767    SomethingChanged(e.GetId());
768}
769
770void
771svxPrintDlg::OnChange(wxCommandEvent& e) {
772    SomethingChanged(e.GetId());
773}
774
775void
776svxPrintDlg::OnCancel(wxCommandEvent&) {
777    if (close_after)
778        mainfrm->Close();
779    Destroy();
780}
781
782void
783svxPrintDlg::SomethingChanged(int control_id) {
784    if ((control_id == 0 || control_id == svx_FORMAT) && m_format) {
785        // Update the shown/hidden fields for the newly selected export filter.
786        int new_filter_idx = m_format->GetSelection();
787        if (new_filter_idx != wxNOT_FOUND) {
788            unsigned mask = format_info[new_filter_idx];
789            static const struct { int id; unsigned mask; } controls[] = {
790                { svx_LEGS, LEGS },
791                { svx_SURFACE, SURF },
792                { svx_STATIONS, STNS },
793                { svx_NAMES, LABELS },
794                { svx_XSECT, XSECT },
795                { svx_WALLS, WALLS },
796                { svx_PASSAGES, PASG },
797                { svx_ENTS, ENTS },
798                { svx_FIXES, FIXES },
799                { svx_EXPORTS, EXPORTS },
800                { svx_CENTRED, CENTRED },
801                { svx_FULLCOORDS, FULL_COORDS },
802                { svx_PROJ_LABEL, PROJ },
803                { svx_PROJ, PROJ },
804            };
805            static unsigned n_controls = sizeof(controls) / sizeof(controls[0]);
806            for (unsigned i = 0; i != n_controls; ++i) {
807                wxWindow * control = FindWindow(controls[i].id);
808                if (control) control->Show(mask & controls[i].mask);
809            }
810            m_scalebox->Show(bool(mask & SCALE));
811            m_viewbox->Show(!bool(mask & EXPORT_3D));
812            GetSizer()->Layout();
813            if (control_id == svx_FORMAT) {
814                wxConfigBase * cfg = wxConfigBase::Get();
815                cfg->Write(wxT("export_format"), formats[new_filter_idx]);
816            }
817        }
818    }
819
820    UIToLayout();
821
822    if (m_printSize || m_scale) {
823        // Update the bounding box.
824        RecalcBounds();
825
826        if (m_scale) {
827            (m_scale->GetValue()).ToDouble(&(m_layout.Scale));
828            if (m_layout.Scale == 0.0) {
829                m_layout.pick_scale(1, 1);
830            }
831        }
832    }
833
834    if (m_printSize && m_layout.xMax >= m_layout.xMin) {
835        m_layout.pages_required();
836        m_printSize->SetLabel(wxString::Format(wmsg(/*%d pages (%dx%d)*/257), m_layout.pages, m_layout.pagesX, m_layout.pagesY));
837    }
838}
839
840void
841svxPrintDlg::LayoutToUI(){
842//    m_blanks->SetValue(m_layout.SkipBlank);
843    if (m_layout.view != layout::EXTELEV) {
844        m_tilt->SetValue(m_layout.tilt);
845        m_bearing->SetValue(m_layout.rot);
846    }
847
848    // Do this last as it causes an OnChange message which calls UIToLayout
849    if (m_scale) {
850        if (m_layout.Scale != 0) {
851            wxString temp;
852            temp << m_layout.Scale;
853            m_scale->SetValue(temp);
854        } else {
855            if (scales[0].empty()) scales[0].assign(wmsg(/*One page*/258));
856            m_scale->SetValue(scales[0]);
857        }
858    }
859}
860
861void
862svxPrintDlg::UIToLayout(){
863//    m_layout.SkipBlank = m_blanks->IsChecked();
864
865    if (m_layout.view != layout::EXTELEV && m_tilt) {
866        m_layout.tilt = m_tilt->GetValue();
867        if (m_layout.tilt == -90.0) {
868            m_layout.view = layout::PLAN;
869        } else if (m_layout.tilt == 0.0) {
870            m_layout.view = layout::ELEV;
871        } else {
872            m_layout.view = layout::TILT;
873        }
874
875        bool enable_passage_opts = (m_layout.view != layout::TILT);
876        wxWindow * win;
877        win = FindWindow(svx_XSECT);
878        if (win) win->Enable(enable_passage_opts);
879        win = FindWindow(svx_WALLS);
880        if (win) win->Enable(enable_passage_opts);
881        win = FindWindow(svx_PASSAGES);
882        if (win) win->Enable(enable_passage_opts);
883
884        m_layout.rot = m_bearing->GetValue();
885    }
886}
887
888void
889svxPrintDlg::RecalcBounds()
890{
891    m_layout.yMax = m_layout.xMax = -DBL_MAX;
892    m_layout.yMin = m_layout.xMin = DBL_MAX;
893
894    double SIN = sin(rad(m_layout.rot));
895    double COS = cos(rad(m_layout.rot));
896    double SINT = sin(rad(m_layout.tilt));
897    double COST = cos(rad(m_layout.tilt));
898
899    if (m_layout.show_mask & LEGS) {
900        list<traverse>::const_iterator trav = mainfrm->traverses_begin();
901        list<traverse>::const_iterator tend = mainfrm->traverses_end();
902        for ( ; trav != tend; ++trav) {
903            vector<PointInfo>::const_iterator pos = trav->begin();
904            vector<PointInfo>::const_iterator end = trav->end();
905            for ( ; pos != end; ++pos) {
906                double x = pos->GetX();
907                double y = pos->GetY();
908                double z = pos->GetZ();
909                double X = x * COS - y * SIN;
910                if (X > m_layout.xMax) m_layout.xMax = X;
911                if (X < m_layout.xMin) m_layout.xMin = X;
912                double Y = z * COST - (x * SIN + y * COS) * SINT;
913                if (Y > m_layout.yMax) m_layout.yMax = Y;
914                if (Y < m_layout.yMin) m_layout.yMin = Y;
915            }
916        }
917    }
918    if (m_layout.show_mask & SURF) {
919        list<traverse>::const_iterator trav = mainfrm->surface_traverses_begin();
920        list<traverse>::const_iterator tend = mainfrm->surface_traverses_end();
921        for ( ; trav != tend; ++trav) {
922            vector<PointInfo>::const_iterator pos = trav->begin();
923            vector<PointInfo>::const_iterator end = trav->end();
924            for ( ; pos != end; ++pos) {
925                double x = pos->GetX();
926                double y = pos->GetY();
927                double z = pos->GetZ();
928                double X = x * COS - y * SIN;
929                if (X > m_layout.xMax) m_layout.xMax = X;
930                if (X < m_layout.xMin) m_layout.xMin = X;
931                double Y = z * COST - (x * SIN + y * COS) * SINT;
932                if (Y > m_layout.yMax) m_layout.yMax = Y;
933                if (Y < m_layout.yMin) m_layout.yMin = Y;
934            }
935        }
936    }
937    if (m_layout.show_mask & (LABELS|STNS)) {
938        list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
939        while (label != mainfrm->GetLabelsEnd()) {
940            double x = (*label)->GetX();
941            double y = (*label)->GetY();
942            double z = (*label)->GetZ();
943            if ((m_layout.show_mask & SURF) || (*label)->IsUnderground()) {
944                double X = x * COS - y * SIN;
945                if (X > m_layout.xMax) m_layout.xMax = X;
946                if (X < m_layout.xMin) m_layout.xMin = X;
947                double Y = z * COST - (x * SIN + y * COS) * SINT;
948                if (Y > m_layout.yMax) m_layout.yMax = Y;
949                if (Y < m_layout.yMin) m_layout.yMin = Y;
950            }
951            ++label;
952        }
953    }
954}
955
956static int xpPageWidth, ypPageDepth;
957static long MarginLeft, MarginRight, MarginTop, MarginBottom;
958static long x_offset, y_offset;
959static wxFont *font_labels, *font_default;
960static int fontsize, fontsize_labels;
961
962/* FIXME: allow the font to be set */
963
964static const char *fontname = "Arial", *fontname_labels = "Arial";
965
966// wx <-> prcore (calls to print_page etc...)
967svxPrintout::svxPrintout(MainFrm *mainfrm_, layout *l,
968                         wxPageSetupDialogData *data, const wxString & title)
969    : wxPrintout(title)
970{
971    mainfrm = mainfrm_;
972    m_layout = l;
973    m_data = data;
974}
975
976void
977svxPrintout::draw_info_box()
978{
979   layout *l = m_layout;
980   int boxwidth = 70;
981   int boxheight = 30;
982
983   SetColour(PR_COLOUR_FRAME);
984
985   int div = boxwidth;
986   if (l->view != layout::EXTELEV) {
987      boxwidth += boxheight;
988      MOVEMM(div, boxheight);
989      DRAWMM(div, 0);
990      MOVEMM(0, 30); DRAWMM(div, 30);
991   }
992
993   MOVEMM(0, boxheight);
994   DRAWMM(boxwidth, boxheight);
995   DRAWMM(boxwidth, 0);
996   if (!l->Border) {
997      DRAWMM(0, 0);
998      DRAWMM(0, boxheight);
999   }
1000
1001   MOVEMM(0, 20); DRAWMM(div, 20);
1002   MOVEMM(0, 10); DRAWMM(div, 10);
1003
1004   switch (l->view) {
1005    case layout::PLAN: {
1006      long ax, ay, bx, by, cx, cy, dx, dy;
1007
1008      long xc = boxwidth - boxheight / 2;
1009      long yc = boxheight / 2;
1010      const double RADIUS = boxheight / 3;
1011      DrawEllipse(long(xc * l->scX), long(yc * l->scY),
1012                  long(RADIUS * l->scX), long(RADIUS * l->scY));
1013
1014      ax = (long)((xc - (RADIUS - 1) * sin(rad(000.0 + l->rot))) * l->scX);
1015      ay = (long)((yc + (RADIUS - 1) * cos(rad(000.0 + l->rot))) * l->scY);
1016      bx = (long)((xc - RADIUS * 0.5 * sin(rad(180.0 + l->rot))) * l->scX);
1017      by = (long)((yc + RADIUS * 0.5 * cos(rad(180.0 + l->rot))) * l->scY);
1018      cx = (long)((xc - (RADIUS - 1) * sin(rad(160.0 + l->rot))) * l->scX);
1019      cy = (long)((yc + (RADIUS - 1) * cos(rad(160.0 + l->rot))) * l->scY);
1020      dx = (long)((xc - (RADIUS - 1) * sin(rad(200.0 + l->rot))) * l->scX);
1021      dy = (long)((yc + (RADIUS - 1) * cos(rad(200.0 + l->rot))) * l->scY);
1022
1023      MoveTo(ax, ay);
1024      DrawTo(bx, by);
1025      DrawTo(cx, cy);
1026      DrawTo(ax, ay);
1027      DrawTo(dx, dy);
1028      DrawTo(bx, by);
1029
1030      SetColour(PR_COLOUR_TEXT);
1031      MOVEMM(div + 0.5, boxheight - 5.5);
1032      WriteString(wmsg(/*North*/115));
1033
1034      wxString angle = format_angle(ANGLE_FMT, l->rot);
1035      wxString s;
1036      /* TRANSLATORS: This is used on printouts of plans, with %s replaced by
1037       * something like "123°".  The bearing is up the page. */
1038      s.Printf(wmsg(/*Plan view, %s up page*/168), angle.c_str());
1039      MOVEMM(2, 12); WriteString(s);
1040      break;
1041    }
1042    case layout::ELEV: case layout::TILT: {
1043      const int L = div + 2;
1044      const int R = boxwidth - 2;
1045      const int H = boxheight / 2;
1046      MOVEMM(L, H); DRAWMM(L + 5, H - 3); DRAWMM(L + 3, H); DRAWMM(L + 5, H + 3);
1047
1048      DRAWMM(L, H); DRAWMM(R, H);
1049
1050      DRAWMM(R - 5, H + 3); DRAWMM(R - 3, H); DRAWMM(R - 5, H - 3); DRAWMM(R, H);
1051
1052      MOVEMM((L + R) / 2, H - 2); DRAWMM((L + R) / 2, H + 2);
1053
1054      SetColour(PR_COLOUR_TEXT);
1055      MOVEMM(div + 2, boxheight - 8);
1056      /* TRANSLATORS: "Elevation on" 020 <-> 200 degrees */
1057      WriteString(wmsg(/*Elevation on*/116));
1058     
1059      MOVEMM(L, 2);
1060      WriteString(format_angle(ANGLE_FMT, fmod(l->rot + 270.0, 360.0)));
1061      MOVEMM(R - 10, 2);
1062      WriteString(format_angle(ANGLE_FMT, fmod(l->rot + 90.0, 360.0)));
1063
1064      wxString angle = format_angle(ANGLE_FMT, l->rot);
1065      wxString s;
1066      if (l->view == layout::ELEV) {
1067          /* TRANSLATORS: This is used on printouts of elevations, with %s
1068           * replaced by something like "123°".  The bearing is the direction
1069           * we’re looking. */
1070          s.Printf(wmsg(/*Elevation facing %s*/169), angle.c_str());
1071      } else {
1072          wxString a2 = format_angle(ANGLE2_FMT, l->tilt);
1073          /* TRANSLATORS: This is used on printouts of tilted elevations, with
1074           * the first %s replaced by something like "123°", and the second by
1075           * something like "-45°".  The bearing is the direction we’re
1076           * looking. */
1077          s.Printf(wmsg(/*Elevation facing %s, tilted %s*/284), angle.c_str(), a2.c_str());
1078      }
1079      MOVEMM(2, 12); WriteString(s);
1080      break;
1081    }
1082    case layout::EXTELEV:
1083      SetColour(PR_COLOUR_TEXT);
1084      MOVEMM(2, 12);
1085      /* TRANSLATORS: This is used on printouts of extended elevations. */
1086      WriteString(wmsg(/*Extended elevation*/191));
1087      break;
1088   }
1089
1090   MOVEMM(2, boxheight - 8); WriteString(l->title);
1091
1092   MOVEMM(2, 2);
1093   // FIXME: "Original Scale" better?
1094   WriteString(wxString::Format(wmsg(/*Scale*/154) + wxT(" 1:%.0f"),
1095                                l->Scale));
1096
1097   /* This used to be a copyright line, but it was occasionally
1098    * mis-interpreted as us claiming copyright on the survey, so let's
1099    * give the website URL instead */
1100   MOVEMM(boxwidth + 2, 2);
1101   WriteString(wxT("Survex "VERSION" - http://survex.com/"));
1102
1103   draw_scale_bar(boxwidth + 10.0, 17.0, l->PaperWidth - boxwidth - 18.0);
1104}
1105
1106/* Draw fancy scale bar with bottom left at (x,y) (both in mm) and at most */
1107/* MaxLength mm long. The scaling in use is 1:scale */
1108void
1109svxPrintout::draw_scale_bar(double x, double y, double MaxLength)
1110{
1111   double StepEst, d;
1112   int E, Step, n, c;
1113   wxString buf;
1114   /* Limit scalebar to 20cm to stop people with A0 plotters complaining */
1115   if (MaxLength > 200.0) MaxLength = 200.0;
1116
1117#define dmin 10.0      /* each division >= dmin mm long */
1118#define StepMax 5      /* number in steps of at most StepMax (x 10^N) */
1119#define epsilon (1e-4) /* fudge factor to prevent rounding problems */
1120
1121   E = (int)ceil(log10((dmin * 0.001 * m_layout->Scale) / StepMax));
1122   StepEst = pow(10.0, -(double)E) * (dmin * 0.001) * m_layout->Scale - epsilon;
1123
1124   /* Force labelling to be in multiples of 1, 2, or 5 */
1125   Step = (StepEst <= 1.0 ? 1 : (StepEst <= 2.0 ? 2 : 5));
1126
1127   /* Work out actual length of each scale bar division */
1128   d = Step * pow(10.0, (double)E) / m_layout->Scale * 1000.0;
1129
1130   /* FIXME: Non-metric units here... */
1131   /* Choose appropriate units, s.t. if possible E is >=0 and minimized */
1132   int units;
1133   if (E >= 3) {
1134      E -= 3;
1135      units = /*km*/423;
1136   } else if (E >= 0) {
1137      units = /*m*/424;
1138   } else {
1139      E += 2;
1140      units = /*cm*/425;
1141   }
1142
1143   buf = wmsg(/*Scale*/154);
1144
1145   /* Add units used - eg. "Scale (10m)" */
1146   double pow10_E = pow(10.0, (double)E);
1147   if (E >= 0) {
1148      buf += wxString::Format(wxT(" (%.f%s)"), pow10_E, wmsg(units).c_str());
1149   } else {
1150      int sf = -(int)floor(E);
1151      buf += wxString::Format(wxT(" (%.*f%s)"), sf, pow10_E, wmsg(units).c_str());
1152   }
1153   SetColour(PR_COLOUR_TEXT);
1154   MOVEMM(x, y + 4); WriteString(buf);
1155
1156   /* Work out how many divisions there will be */
1157   n = (int)(MaxLength / d);
1158
1159   SetColour(PR_COLOUR_FRAME);
1160
1161   long Y = long(y * m_layout->scY);
1162   long Y2 = long((y + 3) * m_layout->scY);
1163   long X = long(x * m_layout->scX);
1164   long X2 = long((x + n * d) * m_layout->scX);
1165
1166   /* Draw top of scale bar */
1167   MoveTo(X2, Y2);
1168   DrawTo(X, Y2);
1169#if 0
1170   DrawTo(X2, Y);
1171   DrawTo(X, Y);
1172   MOVEMM(x + n * d, y); DRAWMM(x, y);
1173#endif
1174   /* Draw divisions and label them */
1175   for (c = 0; c <= n; c++) {
1176      SetColour(PR_COLOUR_FRAME);
1177      X = long((x + c * d) * m_layout->scX);
1178      MoveTo(X, Y);
1179      DrawTo(X, Y2);
1180#if 0 // Don't waste toner!
1181      /* Draw a "zebra crossing" scale bar. */
1182      if (c < n && (c & 1) == 0) {
1183          X2 = long((x + (c + 1) * d) * m_layout->scX);
1184          SolidRectangle(X, Y, X2 - X, Y2 - Y);
1185      }
1186#endif
1187      buf.Printf(wxT("%d"), c * Step);
1188      SetColour(PR_COLOUR_TEXT);
1189      MOVEMM(x + c * d - buf.length(), y - 5);
1190      WriteString(buf);
1191   }
1192}
1193
1194#if 0
1195void
1196make_calibration(layout *l) {
1197      img_point pt = { 0.0, 0.0, 0.0 };
1198      l->xMax = l->yMax = 0.1;
1199      l->xMin = l->yMin = 0;
1200
1201      stack(l,img_MOVE, NULL, &pt);
1202      pt.x = 0.1;
1203      stack(l,img_LINE, NULL, &pt);
1204      pt.y = 0.1;
1205      stack(l,img_LINE, NULL, &pt);
1206      pt.x = 0.0;
1207      stack(l,img_LINE, NULL, &pt);
1208      pt.y = 0.0;
1209      stack(l,img_LINE, NULL, &pt);
1210      pt.x = 0.05;
1211      pt.y = 0.001;
1212      stack(l,img_LABEL, "10cm", &pt);
1213      pt.x = 0.001;
1214      pt.y = 0.05;
1215      stack(l,img_LABEL, "10cm", &pt);
1216      l->Scale = 1.0;
1217}
1218#endif
1219
1220int
1221svxPrintout::next_page(int *pstate, char **q, int pageLim)
1222{
1223   char *p;
1224   int page;
1225   int c;
1226   p = *q;
1227   if (*pstate > 0) {
1228      /* doing a range */
1229      (*pstate)++;
1230      SVX_ASSERT(*p == '-');
1231      p++;
1232      while (isspace((unsigned char)*p)) p++;
1233      if (sscanf(p, "%u%n", &page, &c) > 0) {
1234         p += c;
1235      } else {
1236         page = pageLim;
1237      }
1238      if (*pstate > page) goto err;
1239      if (*pstate < page) return *pstate;
1240      *q = p;
1241      *pstate = 0;
1242      return page;
1243   }
1244
1245   while (isspace((unsigned char)*p) || *p == ',') p++;
1246
1247   if (!*p) return 0; /* done */
1248
1249   if (*p == '-') {
1250      *q = p;
1251      *pstate = 1;
1252      return 1; /* range with initial parameter omitted */
1253   }
1254   if (sscanf(p, "%u%n", &page, &c) > 0) {
1255      p += c;
1256      while (isspace((unsigned char)*p)) p++;
1257      *q = p;
1258      if (0 < page && page <= pageLim) {
1259         if (*p == '-') *pstate = page; /* range with start */
1260         return page;
1261      }
1262   }
1263   err:
1264   *pstate = -1;
1265   return 0;
1266}
1267
1268/* Draws in alignment marks on each page or borders on edge pages */
1269void
1270svxPrintout::drawticks(border clip, int tsize, int x, int y)
1271{
1272   long i;
1273   int s = tsize * 4;
1274   int o = s / 8;
1275   bool fAtCorner = fFalse;
1276   SetColour(PR_COLOUR_FRAME);
1277   if (x == 0 && m_layout->Border) {
1278      /* solid left border */
1279      MoveTo(clip.x_min, clip.y_min);
1280      DrawTo(clip.x_min, clip.y_max);
1281      fAtCorner = fTrue;
1282   } else {
1283      if (x > 0 || y > 0) {
1284         MoveTo(clip.x_min, clip.y_min);
1285         DrawTo(clip.x_min, clip.y_min + tsize);
1286      }
1287      if (s && x > 0 && m_layout->Cutlines) {
1288         /* dashed left border */
1289         i = (clip.y_max - clip.y_min) -
1290             (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1291         for ( ; i > tsize; i -= s) {
1292            MoveTo(clip.x_min, clip.y_max - (i + o));
1293            DrawTo(clip.x_min, clip.y_max - (i - o));
1294         }
1295      }
1296      if (x > 0 || y < m_layout->pagesY - 1) {
1297         MoveTo(clip.x_min, clip.y_max - tsize);
1298         DrawTo(clip.x_min, clip.y_max);
1299         fAtCorner = fTrue;
1300      }
1301   }
1302
1303   if (y == m_layout->pagesY - 1 && m_layout->Border) {
1304      /* solid top border */
1305      if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1306      DrawTo(clip.x_max, clip.y_max);
1307      fAtCorner = fTrue;
1308   } else {
1309      if (y < m_layout->pagesY - 1 || x > 0) {
1310         if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1311         DrawTo(clip.x_min + tsize, clip.y_max);
1312      }
1313      if (s && y < m_layout->pagesY - 1 && m_layout->Cutlines) {
1314         /* dashed top border */
1315         i = (clip.x_max - clip.x_min) -
1316             (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1317         for ( ; i > tsize; i -= s) {
1318            MoveTo(clip.x_max - (i + o), clip.y_max);
1319            DrawTo(clip.x_max - (i - o), clip.y_max);
1320         }
1321      }
1322      if (y < m_layout->pagesY - 1 || x < m_layout->pagesX - 1) {
1323         MoveTo(clip.x_max - tsize, clip.y_max);
1324         DrawTo(clip.x_max, clip.y_max);
1325         fAtCorner = fTrue;
1326      } else {
1327         fAtCorner = fFalse;
1328      }
1329   }
1330
1331   if (x == m_layout->pagesX - 1 && m_layout->Border) {
1332      /* solid right border */
1333      if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1334      DrawTo(clip.x_max, clip.y_min);
1335      fAtCorner = fTrue;
1336   } else {
1337      if (x < m_layout->pagesX - 1 || y < m_layout->pagesY - 1) {
1338         if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1339         DrawTo(clip.x_max, clip.y_max - tsize);
1340      }
1341      if (s && x < m_layout->pagesX - 1 && m_layout->Cutlines) {
1342         /* dashed right border */
1343         i = (clip.y_max - clip.y_min) -
1344             (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1345         for ( ; i > tsize; i -= s) {
1346            MoveTo(clip.x_max, clip.y_min + (i + o));
1347            DrawTo(clip.x_max, clip.y_min + (i - o));
1348         }
1349      }
1350      if (x < m_layout->pagesX - 1 || y > 0) {
1351         MoveTo(clip.x_max, clip.y_min + tsize);
1352         DrawTo(clip.x_max, clip.y_min);
1353         fAtCorner = fTrue;
1354      } else {
1355         fAtCorner = fFalse;
1356      }
1357   }
1358
1359   if (y == 0 && m_layout->Border) {
1360      /* solid bottom border */
1361      if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1362      DrawTo(clip.x_min, clip.y_min);
1363   } else {
1364      if (y > 0 || x < m_layout->pagesX - 1) {
1365         if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1366         DrawTo(clip.x_max - tsize, clip.y_min);
1367      }
1368      if (s && y > 0 && m_layout->Cutlines) {
1369         /* dashed bottom border */
1370         i = (clip.x_max - clip.x_min) -
1371             (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1372         for ( ; i > tsize; i -= s) {
1373            MoveTo(clip.x_min + (i + o), clip.y_min);
1374            DrawTo(clip.x_min + (i - o), clip.y_min);
1375         }
1376      }
1377      if (y > 0 || x > 0) {
1378         MoveTo(clip.x_min + tsize, clip.y_min);
1379         DrawTo(clip.x_min, clip.y_min);
1380      }
1381   }
1382}
1383
1384bool
1385svxPrintout::OnPrintPage(int pageNum) {
1386    GetPageSizePixels(&xpPageWidth, &ypPageDepth);
1387    pdc = GetDC();
1388#ifdef AVEN_PRINT_PREVIEW
1389    if (IsPreview()) {
1390        int dcx, dcy;
1391        pdc->GetSize(&dcx, &dcy);
1392        pdc->SetUserScale((double)dcx / xpPageWidth, (double)dcy / ypPageDepth);
1393    }
1394#endif
1395
1396    layout * l = m_layout;
1397    {
1398        int pwidth, pdepth;
1399        GetPageSizeMM(&pwidth, &pdepth);
1400        l->scX = (double)xpPageWidth / pwidth;
1401        l->scY = (double)ypPageDepth / pdepth;
1402        font_scaling_x = l->scX * (25.4 / 72.0);
1403        font_scaling_y = l->scY * (25.4 / 72.0);
1404        MarginLeft = m_data->GetMarginTopLeft().x;
1405        MarginTop = m_data->GetMarginTopLeft().y;
1406        MarginBottom = m_data->GetMarginBottomRight().y;
1407        MarginRight = m_data->GetMarginBottomRight().x;
1408        xpPageWidth -= (int)(l->scX * (MarginLeft + MarginRight));
1409        ypPageDepth -= (int)(l->scY * (10 + MarginBottom + MarginRight));
1410        // xpPageWidth -= 1;
1411        pdepth -= 10;
1412        x_offset = (long)(l->scX * MarginLeft);
1413        y_offset = (long)(l->scY * MarginTop);
1414        l->PaperWidth = pwidth -= MarginLeft + MarginRight;
1415        l->PaperDepth = pdepth -= MarginTop + MarginBottom;
1416    }
1417
1418    double SIN = sin(rad(l->rot));
1419    double COS = cos(rad(l->rot));
1420    double SINT = sin(rad(l->tilt));
1421    double COST = cos(rad(l->tilt));
1422
1423    NewPage(pageNum, l->pagesX, l->pagesY);
1424
1425    if (l->Legend && pageNum == (l->pagesY - 1) * l->pagesX + 1) {
1426        SetFont(PR_FONT_DEFAULT);
1427        draw_info_box();
1428    }
1429
1430    pdc->SetClippingRegion(x_offset, y_offset,xpPageWidth+1, ypPageDepth+1);
1431
1432    const double Sc = 1000 / l->Scale;
1433
1434    if (l->show_mask & LEGS) {
1435        SetColour(PR_COLOUR_LEG);
1436        list<traverse>::const_iterator trav = mainfrm->traverses_begin();
1437        list<traverse>::const_iterator tend = mainfrm->traverses_end();
1438        for ( ; trav != tend; ++trav) {
1439            vector<PointInfo>::const_iterator pos = trav->begin();
1440            vector<PointInfo>::const_iterator end = trav->end();
1441            for ( ; pos != end; ++pos) {
1442                double x = pos->GetX();
1443                double y = pos->GetY();
1444                double z = pos->GetZ();
1445                double X = x * COS - y * SIN;
1446                double Y = z * COST - (x * SIN + y * COS) * SINT;
1447                long px = (long)((X * Sc + l->xOrg) * l->scX);
1448                long py = (long)((Y * Sc + l->yOrg) * l->scY);
1449                if (pos == trav->begin()) {
1450                    MoveTo(px, py);
1451                } else {
1452                    DrawTo(px, py);
1453                }
1454            }
1455        }
1456    }
1457
1458    if ((l->show_mask & XSECT) &&
1459        (l->tilt == 0.0 || l->tilt == 90.0 || l->tilt == -90.0)) {
1460        list<vector<XSect> >::const_iterator trav = mainfrm->tubes_begin();
1461        list<vector<XSect> >::const_iterator tend = mainfrm->tubes_end();
1462        for ( ; trav != tend; ++trav) {
1463            if (l->tilt == 90.0 || l->tilt == -90.0) PlotLR(*trav);
1464            if (l->tilt == 0.0) PlotUD(*trav);
1465        }
1466    }
1467
1468    if (l->show_mask & SURF) {
1469        SetColour(PR_COLOUR_SURFACE_LEG);
1470        list<traverse>::const_iterator trav = mainfrm->surface_traverses_begin();
1471        list<traverse>::const_iterator tend = mainfrm->surface_traverses_end();
1472        for ( ; trav != tend; ++trav) {
1473            vector<PointInfo>::const_iterator pos = trav->begin();
1474            vector<PointInfo>::const_iterator end = trav->end();
1475            for ( ; pos != end; ++pos) {
1476                double x = pos->GetX();
1477                double y = pos->GetY();
1478                double z = pos->GetZ();
1479                double X = x * COS - y * SIN;
1480                double Y = z * COST - (x * SIN + y * COS) * SINT;
1481                long px = (long)((X * Sc + l->xOrg) * l->scX);
1482                long py = (long)((Y * Sc + l->yOrg) * l->scY);
1483                if (pos == trav->begin()) {
1484                    MoveTo(px, py);
1485                } else {
1486                    DrawTo(px, py);
1487                }
1488            }
1489        }
1490    }
1491
1492    if (l->show_mask & (LABELS|STNS)) {
1493        if (l->show_mask & LABELS) SetFont(PR_FONT_LABELS);
1494        list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
1495        while (label != mainfrm->GetLabelsEnd()) {
1496            double px = (*label)->GetX();
1497            double py = (*label)->GetY();
1498            double pz = (*label)->GetZ();
1499            if ((l->show_mask & SURF) || (*label)->IsUnderground()) {
1500                double X = px * COS - py * SIN;
1501                double Y = pz * COST - (px * SIN + py * COS) * SINT;
1502                long xnew, ynew;
1503                xnew = (long)((X * Sc + l->xOrg) * l->scX);
1504                ynew = (long)((Y * Sc + l->yOrg) * l->scY);
1505                if (l->show_mask & STNS) {
1506                    SetColour(PR_COLOUR_CROSS);
1507                    DrawCross(xnew, ynew);
1508                }
1509                if (l->show_mask & LABELS) {
1510                    SetColour(PR_COLOUR_LABELS);
1511                    MoveTo(xnew, ynew);
1512                    WriteString((*label)->GetText());
1513                }
1514            }
1515            ++label;
1516        }
1517    }
1518
1519    return true;
1520}
1521
1522void
1523svxPrintout::GetPageInfo(int *minPage, int *maxPage,
1524                         int *pageFrom, int *pageTo)
1525{
1526    *minPage = *pageFrom = 1;
1527    *maxPage = *pageTo = m_layout->pages;
1528}
1529
1530bool
1531svxPrintout::HasPage(int pageNum) {
1532    return (pageNum <= m_layout->pages);
1533}
1534
1535void
1536svxPrintout::OnBeginPrinting() {
1537    Init();
1538    Pre();
1539    m_layout->footer = wmsg(/*Survey “%s”   Page %d (of %d)   Processed on %s*/167);
1540}
1541
1542void
1543svxPrintout::OnEndPrinting() {
1544    delete font_labels;
1545    delete font_default;
1546    delete pen_frame;
1547    delete pen_leg;
1548    delete pen_surface_leg;
1549    delete pen_cross;
1550}
1551
1552
1553// prcore -> wx.grafx (calls to move pens around and stuff - low level)
1554// this seems to have been done...
1555
1556
1557
1558static border clip;
1559
1560
1561int
1562svxPrintout::check_intersection(long x_p, long y_p)
1563{
1564#define U 1
1565#define D 2
1566#define L 4
1567#define R 8
1568   int mask_p = 0, mask_t = 0;
1569   if (x_p < 0)
1570      mask_p = L;
1571   else if (x_p > xpPageWidth)
1572      mask_p = R;
1573
1574   if (y_p < 0)
1575      mask_p |= D;
1576   else if (y_p > ypPageDepth)
1577      mask_p |= U;
1578
1579   if (x_t < 0)
1580      mask_t = L;
1581   else if (x_t > xpPageWidth)
1582      mask_t = R;
1583
1584   if (y_t < 0)
1585      mask_t |= D;
1586   else if (y_t > ypPageDepth)
1587      mask_t |= U;
1588
1589#if 0
1590   /* approximation to correct answer */
1591   return !(mask_t & mask_p);
1592#else
1593   /* One end of the line is on the page */
1594   if (!mask_t || !mask_p) return 1;
1595
1596   /* whole line is above, left, right, or below page */
1597   if (mask_t & mask_p) return 0;
1598
1599   if (mask_t == 0) mask_t = mask_p;
1600   if (mask_t & U) {
1601      double v = (double)(y_p - ypPageDepth) / (y_p - y_t);
1602      return v >= 0 && v <= 1;
1603   }
1604   if (mask_t & D) {
1605      double v = (double)y_p / (y_p - y_t);
1606      return v >= 0 && v <= 1;
1607   }
1608   if (mask_t & R) {
1609      double v = (double)(x_p - xpPageWidth) / (x_p - x_t);
1610      return v >= 0 && v <= 1;
1611   }
1612   SVX_ASSERT(mask_t & L);
1613   {
1614      double v = (double)x_p / (x_p - x_t);
1615      return v >= 0 && v <= 1;
1616   }
1617#endif
1618#undef U
1619#undef D
1620#undef L
1621#undef R
1622}
1623
1624void
1625svxPrintout::MoveTo(long x, long y)
1626{
1627    x_t = x_offset + x - clip.x_min;
1628    y_t = y_offset + clip.y_max - y;
1629}
1630
1631void
1632svxPrintout::DrawTo(long x, long y)
1633{
1634    long x_p = x_t, y_p = y_t;
1635    x_t = x_offset + x - clip.x_min;
1636    y_t = y_offset + clip.y_max - y;
1637    if (cur_pass != -1) {
1638        pdc->DrawLine(x_p, y_p, x_t, y_t);
1639    } else {
1640        if (check_intersection(x_p, y_p)) fBlankPage = fFalse;
1641    }
1642}
1643
1644#define POINTS_PER_INCH 72.0
1645#define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH)
1646#define PWX_CROSS_SIZE (int)(2 * m_layout->scX / POINTS_PER_MM)
1647
1648void
1649svxPrintout::DrawCross(long x, long y)
1650{
1651   if (cur_pass != -1) {
1652      MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1653      DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1654      MoveTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1655      DrawTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1656      MoveTo(x, y);
1657   } else {
1658      if ((x + PWX_CROSS_SIZE > clip.x_min &&
1659           x - PWX_CROSS_SIZE < clip.x_max) ||
1660          (y + PWX_CROSS_SIZE > clip.y_min &&
1661           y - PWX_CROSS_SIZE < clip.y_max)) {
1662         fBlankPage = fFalse;
1663      }
1664   }
1665}
1666
1667void
1668svxPrintout::SetFont(int fontcode)
1669{
1670    switch (fontcode) {
1671        case PR_FONT_DEFAULT:
1672            current_font = font_default;
1673            break;
1674        case PR_FONT_LABELS:
1675            current_font = font_labels;
1676            break;
1677        default:
1678            BUG("unknown font code");
1679    }
1680}
1681
1682void
1683svxPrintout::SetColour(int colourcode)
1684{
1685    switch (colourcode) {
1686        case PR_COLOUR_TEXT:
1687            pdc->SetTextForeground(colour_text);
1688            break;
1689        case PR_COLOUR_LABELS:
1690            pdc->SetTextForeground(colour_labels);
1691            pdc->SetBackgroundMode(wxTRANSPARENT);
1692            break;
1693        case PR_COLOUR_FRAME:
1694            pdc->SetPen(*pen_frame);
1695            break;
1696        case PR_COLOUR_LEG:
1697            pdc->SetPen(*pen_leg);
1698            break;
1699        case PR_COLOUR_CROSS:
1700            pdc->SetPen(*pen_cross);
1701            break;
1702        case PR_COLOUR_SURFACE_LEG:
1703            pdc->SetPen(*pen_surface_leg);
1704            break;
1705        default:
1706            BUG("unknown colour code");
1707    }
1708}
1709
1710void
1711svxPrintout::WriteString(const wxString & s)
1712{
1713    double xsc, ysc;
1714    pdc->GetUserScale(&xsc, &ysc);
1715    pdc->SetUserScale(xsc * font_scaling_x, ysc * font_scaling_y);
1716    pdc->SetFont(*current_font);
1717    int w, h;
1718    if (cur_pass != -1) {
1719        pdc->GetTextExtent(wxT("My"), &w, &h);
1720        pdc->DrawText(s,
1721                      long(x_t / font_scaling_x),
1722                      long(y_t / font_scaling_y) - h);
1723    } else {
1724        pdc->GetTextExtent(s, &w, &h);
1725        if ((y_t + h > 0 && y_t - h < clip.y_max - clip.y_min) ||
1726            (x_t < clip.x_max - clip.x_min && x_t + w > 0)) {
1727            fBlankPage = fFalse;
1728        }
1729    }
1730    pdc->SetUserScale(xsc, ysc);
1731}
1732
1733void
1734svxPrintout::DrawEllipse(long x, long y, long r, long R)
1735{
1736    /* Don't need to check in first-pass - circle is only used in title box */
1737    if (cur_pass != -1) {
1738        x_t = x_offset + x - clip.x_min;
1739        y_t = y_offset + clip.y_max - y;
1740        const wxBrush & save_brush = pdc->GetBrush();
1741        pdc->SetBrush(*wxTRANSPARENT_BRUSH);
1742        pdc->DrawEllipse(x_t - r, y_t - R, 2 * r, 2 * R);
1743        pdc->SetBrush(save_brush);
1744    }
1745}
1746
1747void
1748svxPrintout::SolidRectangle(long x, long y, long w, long h)
1749{
1750    long X = x_offset + x - clip.x_min;
1751    long Y = y_offset + clip.y_max - y;
1752    pdc->SetBrush(*wxBLACK_BRUSH);
1753    pdc->DrawRectangle(X, Y - h, w, h);
1754}
1755
1756int
1757svxPrintout::Pre()
1758{
1759    font_labels = new wxFont(fontsize_labels, wxDEFAULT, wxNORMAL, wxNORMAL,
1760                             false, wxString(fontname_labels, wxConvUTF8),
1761                             wxFONTENCODING_ISO8859_1);
1762    font_default = new wxFont(fontsize, wxDEFAULT, wxNORMAL, wxNORMAL,
1763                              false, wxString(fontname, wxConvUTF8),
1764                              wxFONTENCODING_ISO8859_1);
1765    current_font = font_default;
1766    pen_leg = new wxPen(colour_leg);
1767    pen_surface_leg = new wxPen(colour_surface_leg);
1768    pen_cross = new wxPen(colour_cross);
1769    pen_frame = new wxPen(colour_frame);
1770    return 1; /* only need 1 pass */
1771}
1772
1773void
1774svxPrintout::NewPage(int pg, int pagesX, int pagesY)
1775{
1776    int x, y;
1777    x = (pg - 1) % pagesX;
1778    y = pagesY - 1 - ((pg - 1) / pagesX);
1779
1780    clip.x_min = (long)x * xpPageWidth;
1781    clip.y_min = (long)y * ypPageDepth;
1782    clip.x_max = clip.x_min + xpPageWidth; /* dm/pcl/ps had -1; */
1783    clip.y_max = clip.y_min + ypPageDepth; /* dm/pcl/ps had -1; */
1784
1785    //we have to write the footer here. PostScript is being weird. Really weird.
1786    pdc->SetFont(*font_labels);
1787    MoveTo(clip.x_min, clip.y_min - (long)(7 * m_layout->scY));
1788    wxString footer;
1789    footer.Printf(m_layout->footer,
1790                  m_layout->title.c_str(),
1791                  pg,
1792                  m_layout->pagesX * m_layout->pagesY,
1793                  m_layout->datestamp.c_str());
1794    WriteString(footer);
1795    pdc->DestroyClippingRegion();
1796    drawticks(clip, (int)(9 * m_layout->scX / POINTS_PER_MM), x, y);
1797}
1798
1799void
1800svxPrintout::PlotLR(const vector<XSect> & centreline)
1801{
1802    assert(centreline.size() > 1);
1803    XSect prev_pt_v;
1804    Vector3 last_right(1.0, 0.0, 0.0);
1805
1806    const double Sc = 1000 / m_layout->Scale;
1807    const double SIN = sin(rad(m_layout->rot));
1808    const double COS = cos(rad(m_layout->rot));
1809
1810    vector<XSect>::const_iterator i = centreline.begin();
1811    vector<XSect>::size_type segment = 0;
1812    while (i != centreline.end()) {
1813        // get the coordinates of this vertex
1814        const XSect & pt_v = *i++;
1815
1816        Vector3 right;
1817
1818        const Vector3 up_v(0.0, 0.0, 1.0);
1819
1820        if (segment == 0) {
1821            assert(i != centreline.end());
1822            // first segment
1823
1824            // get the coordinates of the next vertex
1825            const XSect & next_pt_v = *i;
1826
1827            // calculate vector from this pt to the next one
1828            Vector3 leg_v = next_pt_v - pt_v;
1829
1830            // obtain a vector in the LRUD plane
1831            right = leg_v * up_v;
1832            if (right.magnitude() == 0) {
1833                right = last_right;
1834            } else {
1835                last_right = right;
1836            }
1837        } else if (segment + 1 == centreline.size()) {
1838            // last segment
1839
1840            // Calculate vector from the previous pt to this one.
1841            Vector3 leg_v = pt_v - prev_pt_v;
1842
1843            // Obtain a horizontal vector in the LRUD plane.
1844            right = leg_v * up_v;
1845            if (right.magnitude() == 0) {
1846                right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
1847            } else {
1848                last_right = right;
1849            }
1850        } else {
1851            assert(i != centreline.end());
1852            // Intermediate segment.
1853
1854            // Get the coordinates of the next vertex.
1855            const XSect & next_pt_v = *i;
1856
1857            // Calculate vectors from this vertex to the
1858            // next vertex, and from the previous vertex to
1859            // this one.
1860            Vector3 leg1_v = pt_v - prev_pt_v;
1861            Vector3 leg2_v = next_pt_v - pt_v;
1862
1863            // Obtain horizontal vectors perpendicular to
1864            // both legs, then normalise and average to get
1865            // a horizontal bisector.
1866            Vector3 r1 = leg1_v * up_v;
1867            Vector3 r2 = leg2_v * up_v;
1868            r1.normalise();
1869            r2.normalise();
1870            right = r1 + r2;
1871            if (right.magnitude() == 0) {
1872                // This is the "mid-pitch" case...
1873                right = last_right;
1874            }
1875            last_right = right;
1876        }
1877
1878        // Scale to unit vectors in the LRUD plane.
1879        right.normalise();
1880
1881        Double l = pt_v.GetL();
1882        Double r = pt_v.GetR();
1883
1884        if (l >= 0) {
1885            Vector3 p = pt_v - right * l;
1886            double X = p.GetX() * COS - p.GetY() * SIN;
1887            double Y = (p.GetX() * SIN + p.GetY() * COS);
1888            long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
1889            long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
1890            MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1891            DrawTo(x, y);
1892            DrawTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1893        }
1894        if (r >= 0) {
1895            Vector3 p = pt_v + right * r;
1896            double X = p.GetX() * COS - p.GetY() * SIN;
1897            double Y = (p.GetX() * SIN + p.GetY() * COS);
1898            long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
1899            long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
1900            MoveTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1901            DrawTo(x, y);
1902            DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1903        }
1904
1905        prev_pt_v = pt_v;
1906
1907        ++segment;
1908    }
1909}
1910
1911void
1912svxPrintout::PlotUD(const vector<XSect> & centreline)
1913{
1914    assert(centreline.size() > 1);
1915    const double Sc = 1000 / m_layout->Scale;
1916
1917    vector<XSect>::const_iterator i = centreline.begin();
1918    while (i != centreline.end()) {
1919        // get the coordinates of this vertex
1920        const XSect & pt_v = *i++;
1921
1922        Double u = pt_v.GetU();
1923        Double d = pt_v.GetD();
1924
1925        if (u >= 0 || d >= 0) {
1926            Vector3 p = pt_v;
1927            double SIN = sin(rad(m_layout->rot));
1928            double COS = cos(rad(m_layout->rot));
1929            double X = p.GetX() * COS - p.GetY() * SIN;
1930            double Y = p.GetZ();
1931            long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
1932            if (u >= 0) {
1933                long y = (long)(((Y + u) * Sc + m_layout->yOrg) * m_layout->scY);
1934                MoveTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1935                DrawTo(x, y);
1936                DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1937            }
1938            if (d >= 0) {
1939                long y = (long)(((Y - d) * Sc + m_layout->yOrg) * m_layout->scY);
1940                MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1941                DrawTo(x, y);
1942                DrawTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1943            }
1944        }
1945    }
1946}
1947
1948/* Initialise printer routines */
1949void
1950svxPrintout::Init()
1951{
1952   fontsize_labels = 10;
1953   fontsize = 10;
1954
1955   colour_text = colour_labels = colour_frame = colour_leg = colour_cross = colour_surface_leg = *wxBLACK;
1956   m_layout->scX = 1;
1957   m_layout->scY = 1;
1958}
Note: See TracBrowser for help on using the repository browser.