source: git/src/printwx.cc @ 0b8c321

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since 0b8c321 was 0b8c321, checked in by Olly Betts <olly@…>, 9 years ago

lib/survex.pot,src/: Add more TRANSLATOR comments.

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