source: git/src/printwx.cc @ 18ff765

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

src/export.cc,src/export.h,src/printwx.cc: For export formats where
scaling is supporting, aven now actually uses the scale specified in
the export dialog (previously it ignored this and used 1:500).

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