source: git/src/printwx.cc @ 1fce809

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-datawalls-data-hanging-as-warning
Last change on this file since 1fce809 was f1d77c8, checked in by Olly Betts <olly@…>, 10 years ago

src/aboutdlg.cc,src/mainfrm.cc,src/printwx.cc: Calling Fit() on a
sizer is redundant where we also call SetSizeHints?() on it.

  • 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 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)) {
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    if (!m_printSize) return;
774    // Update the bounding box.
775    RecalcBounds();
776
777    if (m_scale) {
778        (m_scale->GetValue()).ToDouble(&(m_layout.Scale));
779        if (m_layout.Scale == 0.0) {
780            m_layout.pick_scale(1, 1);
781        }
782    }
783
784    if (m_layout.xMax >= m_layout.xMin) {
785        m_layout.pages_required();
786        m_printSize->SetLabel(wxString::Format(wmsg(/*%d pages (%dx%d)*/257), m_layout.pages, m_layout.pagesX, m_layout.pagesY));
787    }
788}
789
790void
791svxPrintDlg::LayoutToUI(){
792//    m_blanks->SetValue(m_layout.SkipBlank);
793    if (m_layout.view != layout::EXTELEV) {
794        m_tilt->SetValue(m_layout.tilt);
795        m_bearing->SetValue(m_layout.rot);
796    }
797
798    // Do this last as it causes an OnChange message which calls UIToLayout
799    if (m_scale) {
800        if (m_layout.Scale != 0) {
801            wxString temp;
802            temp << m_layout.Scale;
803            m_scale->SetValue(temp);
804        } else {
805            if (scales[0].empty()) scales[0].assign(wmsg(/*One page*/258));
806            m_scale->SetValue(scales[0]);
807        }
808    }
809}
810
811void
812svxPrintDlg::UIToLayout(){
813//    m_layout.SkipBlank = m_blanks->IsChecked();
814
815    if (m_layout.view != layout::EXTELEV && m_tilt) {
816        m_layout.tilt = m_tilt->GetValue();
817        if (m_layout.tilt == -90) {
818            m_layout.view = layout::PLAN;
819        } else if (m_layout.tilt == 0) {
820            m_layout.view = layout::ELEV;
821        } else {
822            m_layout.view = layout::TILT;
823        }
824
825        bool enable_passage_opts = (m_layout.view != layout::TILT);
826        wxWindow * win;
827        win = FindWindow(svx_XSECT);
828        if (win) win->Enable(enable_passage_opts);
829        win = FindWindow(svx_WALLS);
830        if (win) win->Enable(enable_passage_opts);
831        win = FindWindow(svx_PASSAGES);
832        if (win) win->Enable(enable_passage_opts);
833
834        m_layout.rot = m_bearing->GetValue();
835    }
836}
837
838void
839svxPrintDlg::RecalcBounds()
840{
841    m_layout.yMax = m_layout.xMax = -DBL_MAX;
842    m_layout.yMin = m_layout.xMin = DBL_MAX;
843
844    double SIN = sin(rad(m_layout.rot));
845    double COS = cos(rad(m_layout.rot));
846    double SINT = sin(rad(m_layout.tilt));
847    double COST = cos(rad(m_layout.tilt));
848
849    if (m_layout.show_mask & LEGS) {
850        list<traverse>::const_iterator trav = mainfrm->traverses_begin();
851        list<traverse>::const_iterator tend = mainfrm->traverses_end();
852        for ( ; trav != tend; ++trav) {
853            vector<PointInfo>::const_iterator pos = trav->begin();
854            vector<PointInfo>::const_iterator end = trav->end();
855            for ( ; pos != end; ++pos) {
856                double x = pos->GetX();
857                double y = pos->GetY();
858                double z = pos->GetZ();
859                double X = x * COS - y * SIN;
860                if (X > m_layout.xMax) m_layout.xMax = X;
861                if (X < m_layout.xMin) m_layout.xMin = X;
862                double Y = z * COST - (x * SIN + y * COS) * SINT;
863                if (Y > m_layout.yMax) m_layout.yMax = Y;
864                if (Y < m_layout.yMin) m_layout.yMin = Y;
865            }
866        }
867    }
868    if (m_layout.show_mask & SURF) {
869        list<traverse>::const_iterator trav = mainfrm->surface_traverses_begin();
870        list<traverse>::const_iterator tend = mainfrm->surface_traverses_end();
871        for ( ; trav != tend; ++trav) {
872            vector<PointInfo>::const_iterator pos = trav->begin();
873            vector<PointInfo>::const_iterator end = trav->end();
874            for ( ; pos != end; ++pos) {
875                double x = pos->GetX();
876                double y = pos->GetY();
877                double z = pos->GetZ();
878                double X = x * COS - y * SIN;
879                if (X > m_layout.xMax) m_layout.xMax = X;
880                if (X < m_layout.xMin) m_layout.xMin = X;
881                double Y = z * COST - (x * SIN + y * COS) * SINT;
882                if (Y > m_layout.yMax) m_layout.yMax = Y;
883                if (Y < m_layout.yMin) m_layout.yMin = Y;
884            }
885        }
886    }
887    if (m_layout.show_mask & (LABELS|STNS)) {
888        list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
889        while (label != mainfrm->GetLabelsEnd()) {
890            double x = (*label)->GetX();
891            double y = (*label)->GetY();
892            double z = (*label)->GetZ();
893            if ((m_layout.show_mask & SURF) || (*label)->IsUnderground()) {
894                double X = x * COS - y * SIN;
895                if (X > m_layout.xMax) m_layout.xMax = X;
896                if (X < m_layout.xMin) m_layout.xMin = X;
897                double Y = z * COST - (x * SIN + y * COS) * SINT;
898                if (Y > m_layout.yMax) m_layout.yMax = Y;
899                if (Y < m_layout.yMin) m_layout.yMin = Y;
900            }
901            ++label;
902        }
903    }
904}
905
906static int xpPageWidth, ypPageDepth;
907static long MarginLeft, MarginRight, MarginTop, MarginBottom;
908static long x_offset, y_offset;
909static wxFont *font_labels, *font_default;
910static int fontsize, fontsize_labels;
911
912/* FIXME: allow the font to be set */
913
914static const char *fontname = "Arial", *fontname_labels = "Arial";
915
916// wx <-> prcore (calls to print_page etc...)
917svxPrintout::svxPrintout(MainFrm *mainfrm_, layout *l,
918                         wxPageSetupDialogData *data, const wxString & title)
919    : wxPrintout(title)
920{
921    mainfrm = mainfrm_;
922    m_layout = l;
923    m_data = data;
924}
925
926void
927svxPrintout::draw_info_box()
928{
929   layout *l = m_layout;
930   int boxwidth = 70;
931   int boxheight = 30;
932
933   SetColour(PR_COLOUR_FRAME);
934
935   int div = boxwidth;
936   if (l->view != layout::EXTELEV) {
937      boxwidth += boxheight;
938      MOVEMM(div, boxheight);
939      DRAWMM(div, 0);
940      MOVEMM(0, 30); DRAWMM(div, 30);
941   }
942
943   MOVEMM(0, boxheight);
944   DRAWMM(boxwidth, boxheight);
945   DRAWMM(boxwidth, 0);
946   if (!l->Border) {
947      DRAWMM(0, 0);
948      DRAWMM(0, boxheight);
949   }
950
951   MOVEMM(0, 20); DRAWMM(div, 20);
952   MOVEMM(0, 10); DRAWMM(div, 10);
953
954   switch (l->view) {
955    case layout::PLAN: {
956      long ax, ay, bx, by, cx, cy, dx, dy;
957
958      long xc = boxwidth - boxheight / 2;
959      long yc = boxheight / 2;
960      const double RADIUS = boxheight / 3;
961      DrawEllipse(long(xc * l->scX), long(yc * l->scY),
962                  long(RADIUS * l->scX), long(RADIUS * l->scY));
963
964      ax = (long)((xc - (RADIUS - 1) * sin(rad(000.0 + l->rot))) * l->scX);
965      ay = (long)((yc + (RADIUS - 1) * cos(rad(000.0 + l->rot))) * l->scY);
966      bx = (long)((xc - RADIUS * 0.5 * sin(rad(180.0 + l->rot))) * l->scX);
967      by = (long)((yc + RADIUS * 0.5 * cos(rad(180.0 + l->rot))) * l->scY);
968      cx = (long)((xc - (RADIUS - 1) * sin(rad(160.0 + l->rot))) * l->scX);
969      cy = (long)((yc + (RADIUS - 1) * cos(rad(160.0 + l->rot))) * l->scY);
970      dx = (long)((xc - (RADIUS - 1) * sin(rad(200.0 + l->rot))) * l->scX);
971      dy = (long)((yc + (RADIUS - 1) * cos(rad(200.0 + l->rot))) * l->scY);
972
973      MoveTo(ax, ay);
974      DrawTo(bx, by);
975      DrawTo(cx, cy);
976      DrawTo(ax, ay);
977      DrawTo(dx, dy);
978      DrawTo(bx, by);
979
980      SetColour(PR_COLOUR_TEXT);
981      MOVEMM(div + 0.5, boxheight - 5.5);
982      WriteString(wmsg(/*North*/115));
983
984      wxString angle;
985      angle.Printf(wxT("%03d"), l->rot);
986      angle += wmsg(/*°*/344);
987      wxString s;
988      /* TRANSLATORS: This is used on printouts of plans, with %s replaced by
989       * something like "123°".  The bearing is up the page. */
990      s.Printf(wmsg(/*Plan view, %s up page*/168), angle.c_str());
991      MOVEMM(2, 12); WriteString(s);
992      break;
993    }
994    case layout::ELEV: case layout::TILT: {
995      const int L = div + 2;
996      const int R = boxwidth - 2;
997      const int H = boxheight / 2;
998      MOVEMM(L, H); DRAWMM(L + 5, H - 3); DRAWMM(L + 3, H); DRAWMM(L + 5, H + 3);
999
1000      DRAWMM(L, H); DRAWMM(R, H);
1001
1002      DRAWMM(R - 5, H + 3); DRAWMM(R - 3, H); DRAWMM(R - 5, H - 3); DRAWMM(R, H);
1003
1004      MOVEMM((L + R) / 2, H - 2); DRAWMM((L + R) / 2, H + 2);
1005
1006      SetColour(PR_COLOUR_TEXT);
1007      MOVEMM(div + 2, boxheight - 8);
1008      /* TRANSLATORS: "Elevation on" 020 <-> 200 degrees */
1009      WriteString(wmsg(/*Elevation on*/116));
1010     
1011      MOVEMM(L, 2);
1012      WriteString(wxString::Format(wxT("%03d%s"),
1013                                   (l->rot + 270) % 360,
1014                                   wmsg(/*°*/344).c_str()));
1015      MOVEMM(R - 10, 2);
1016      WriteString(wxString::Format(wxT("%03d%s"),
1017                                   (l->rot + 90) % 360,
1018                                   wmsg(/*°*/344).c_str()));
1019
1020      wxString angle;
1021      angle.Printf(wxT("%03d"), l->rot);
1022      angle += wmsg(/*°*/344);
1023      wxString s;
1024      if (l->view == layout::ELEV) {
1025          /* TRANSLATORS: This is used on printouts of elevations, with %s
1026           * replaced by something like "123°".  The bearing is the direction
1027           * we’re looking. */
1028          s.Printf(wmsg(/*Elevation facing %s*/169), angle.c_str());
1029      } else {
1030          wxString a2;
1031          a2.Printf(wxT("%d"), l->tilt);
1032          a2 += wmsg(/*°*/344);
1033          /* TRANSLATORS: This is used on printouts of tilted elevations, with
1034           * the first %s replaced by something like "123°", and the second by
1035           * something like "-45°".  The bearing is the direction we’re
1036           * looking. */
1037          s.Printf(wmsg(/*Elevation facing %s, tilted %s*/284), angle.c_str(), a2.c_str());
1038      }
1039      MOVEMM(2, 12); WriteString(s);
1040      break;
1041    }
1042    case layout::EXTELEV:
1043      SetColour(PR_COLOUR_TEXT);
1044      MOVEMM(2, 12);
1045      /* TRANSLATORS: This is used on printouts of extended elevations. */
1046      WriteString(wmsg(/*Extended elevation*/191));
1047      break;
1048   }
1049
1050   MOVEMM(2, boxheight - 8); WriteString(l->title);
1051
1052   MOVEMM(2, 2);
1053   // FIXME: "Original Scale" better?
1054   WriteString(wxString::Format(wmsg(/*Scale*/154) + wxT(" 1:%.0f"),
1055                                l->Scale));
1056
1057   /* This used to be a copyright line, but it was occasionally
1058    * mis-interpreted as us claiming copyright on the survey, so let's
1059    * give the website URL instead */
1060   MOVEMM(boxwidth + 2, 2);
1061   WriteString(wxT("Survex "VERSION" - http://survex.com/"));
1062
1063   draw_scale_bar(boxwidth + 10.0, 17.0, l->PaperWidth - boxwidth - 18.0);
1064}
1065
1066/* Draw fancy scale bar with bottom left at (x,y) (both in mm) and at most */
1067/* MaxLength mm long. The scaling in use is 1:scale */
1068void
1069svxPrintout::draw_scale_bar(double x, double y, double MaxLength)
1070{
1071   double StepEst, d;
1072   int E, Step, n, c;
1073   wxString buf;
1074   /* Limit scalebar to 20cm to stop people with A0 plotters complaining */
1075   if (MaxLength > 200.0) MaxLength = 200.0;
1076
1077#define dmin 10.0      /* each division >= dmin mm long */
1078#define StepMax 5      /* number in steps of at most StepMax (x 10^N) */
1079#define epsilon (1e-4) /* fudge factor to prevent rounding problems */
1080
1081   E = (int)ceil(log10((dmin * 0.001 * m_layout->Scale) / StepMax));
1082   StepEst = pow(10.0, -(double)E) * (dmin * 0.001) * m_layout->Scale - epsilon;
1083
1084   /* Force labelling to be in multiples of 1, 2, or 5 */
1085   Step = (StepEst <= 1.0 ? 1 : (StepEst <= 2.0 ? 2 : 5));
1086
1087   /* Work out actual length of each scale bar division */
1088   d = Step * pow(10.0, (double)E) / m_layout->Scale * 1000.0;
1089
1090   /* FIXME: Non-metric units here... */
1091   /* Choose appropriate units, s.t. if possible E is >=0 and minimized */
1092   int units;
1093   if (E >= 3) {
1094      E -= 3;
1095      units = /*km*/423;
1096   } else if (E >= 0) {
1097      units = /*m*/424;
1098   } else {
1099      E += 2;
1100      units = /*cm*/425;
1101   }
1102
1103   buf = wmsg(/*Scale*/154);
1104
1105   /* Add units used - eg. "Scale (10m)" */
1106   double pow10_E = pow(10.0, (double)E);
1107   if (E >= 0) {
1108      buf += wxString::Format(wxT(" (%.f%s)"), pow10_E, wmsg(units).c_str());
1109   } else {
1110      int sf = -(int)floor(E);
1111      buf += wxString::Format(wxT(" (%.*f%s)"), sf, pow10_E, wmsg(units).c_str());
1112   }
1113   SetColour(PR_COLOUR_TEXT);
1114   MOVEMM(x, y + 4); WriteString(buf);
1115
1116   /* Work out how many divisions there will be */
1117   n = (int)(MaxLength / d);
1118
1119   SetColour(PR_COLOUR_FRAME);
1120
1121   long Y = long(y * m_layout->scY);
1122   long Y2 = long((y + 3) * m_layout->scY);
1123   long X = long(x * m_layout->scX);
1124   long X2 = long((x + n * d) * m_layout->scX);
1125
1126   /* Draw top of scale bar */
1127   MoveTo(X2, Y2);
1128   DrawTo(X, Y2);
1129#if 0
1130   DrawTo(X2, Y);
1131   DrawTo(X, Y);
1132   MOVEMM(x + n * d, y); DRAWMM(x, y);
1133#endif
1134   /* Draw divisions and label them */
1135   for (c = 0; c <= n; c++) {
1136      SetColour(PR_COLOUR_FRAME);
1137      X = long((x + c * d) * m_layout->scX);
1138      MoveTo(X, Y);
1139      DrawTo(X, Y2);
1140#if 0 // Don't waste toner!
1141      /* Draw a "zebra crossing" scale bar. */
1142      if (c < n && (c & 1) == 0) {
1143          X2 = long((x + (c + 1) * d) * m_layout->scX);
1144          SolidRectangle(X, Y, X2 - X, Y2 - Y);
1145      }
1146#endif
1147      buf.Printf(wxT("%d"), c * Step);
1148      SetColour(PR_COLOUR_TEXT);
1149      MOVEMM(x + c * d - buf.length(), y - 5);
1150      WriteString(buf);
1151   }
1152}
1153
1154#if 0
1155void
1156make_calibration(layout *l) {
1157      img_point pt = { 0.0, 0.0, 0.0 };
1158      l->xMax = l->yMax = 0.1;
1159      l->xMin = l->yMin = 0;
1160
1161      stack(l,img_MOVE, NULL, &pt);
1162      pt.x = 0.1;
1163      stack(l,img_LINE, NULL, &pt);
1164      pt.y = 0.1;
1165      stack(l,img_LINE, NULL, &pt);
1166      pt.x = 0.0;
1167      stack(l,img_LINE, NULL, &pt);
1168      pt.y = 0.0;
1169      stack(l,img_LINE, NULL, &pt);
1170      pt.x = 0.05;
1171      pt.y = 0.001;
1172      stack(l,img_LABEL, "10cm", &pt);
1173      pt.x = 0.001;
1174      pt.y = 0.05;
1175      stack(l,img_LABEL, "10cm", &pt);
1176      l->Scale = 1.0;
1177}
1178#endif
1179
1180int
1181svxPrintout::next_page(int *pstate, char **q, int pageLim)
1182{
1183   char *p;
1184   int page;
1185   int c;
1186   p = *q;
1187   if (*pstate > 0) {
1188      /* doing a range */
1189      (*pstate)++;
1190      SVX_ASSERT(*p == '-');
1191      p++;
1192      while (isspace((unsigned char)*p)) p++;
1193      if (sscanf(p, "%u%n", &page, &c) > 0) {
1194         p += c;
1195      } else {
1196         page = pageLim;
1197      }
1198      if (*pstate > page) goto err;
1199      if (*pstate < page) return *pstate;
1200      *q = p;
1201      *pstate = 0;
1202      return page;
1203   }
1204
1205   while (isspace((unsigned char)*p) || *p == ',') p++;
1206
1207   if (!*p) return 0; /* done */
1208
1209   if (*p == '-') {
1210      *q = p;
1211      *pstate = 1;
1212      return 1; /* range with initial parameter omitted */
1213   }
1214   if (sscanf(p, "%u%n", &page, &c) > 0) {
1215      p += c;
1216      while (isspace((unsigned char)*p)) p++;
1217      *q = p;
1218      if (0 < page && page <= pageLim) {
1219         if (*p == '-') *pstate = page; /* range with start */
1220         return page;
1221      }
1222   }
1223   err:
1224   *pstate = -1;
1225   return 0;
1226}
1227
1228/* Draws in alignment marks on each page or borders on edge pages */
1229void
1230svxPrintout::drawticks(border clip, int tsize, int x, int y)
1231{
1232   long i;
1233   int s = tsize * 4;
1234   int o = s / 8;
1235   bool fAtCorner = fFalse;
1236   SetColour(PR_COLOUR_FRAME);
1237   if (x == 0 && m_layout->Border) {
1238      /* solid left border */
1239      MoveTo(clip.x_min, clip.y_min);
1240      DrawTo(clip.x_min, clip.y_max);
1241      fAtCorner = fTrue;
1242   } else {
1243      if (x > 0 || y > 0) {
1244         MoveTo(clip.x_min, clip.y_min);
1245         DrawTo(clip.x_min, clip.y_min + tsize);
1246      }
1247      if (s && x > 0 && m_layout->Cutlines) {
1248         /* dashed left border */
1249         i = (clip.y_max - clip.y_min) -
1250             (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1251         for ( ; i > tsize; i -= s) {
1252            MoveTo(clip.x_min, clip.y_max - (i + o));
1253            DrawTo(clip.x_min, clip.y_max - (i - o));
1254         }
1255      }
1256      if (x > 0 || y < m_layout->pagesY - 1) {
1257         MoveTo(clip.x_min, clip.y_max - tsize);
1258         DrawTo(clip.x_min, clip.y_max);
1259         fAtCorner = fTrue;
1260      }
1261   }
1262
1263   if (y == m_layout->pagesY - 1 && m_layout->Border) {
1264      /* solid top border */
1265      if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1266      DrawTo(clip.x_max, clip.y_max);
1267      fAtCorner = fTrue;
1268   } else {
1269      if (y < m_layout->pagesY - 1 || x > 0) {
1270         if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1271         DrawTo(clip.x_min + tsize, clip.y_max);
1272      }
1273      if (s && y < m_layout->pagesY - 1 && m_layout->Cutlines) {
1274         /* dashed top border */
1275         i = (clip.x_max - clip.x_min) -
1276             (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1277         for ( ; i > tsize; i -= s) {
1278            MoveTo(clip.x_max - (i + o), clip.y_max);
1279            DrawTo(clip.x_max - (i - o), clip.y_max);
1280         }
1281      }
1282      if (y < m_layout->pagesY - 1 || x < m_layout->pagesX - 1) {
1283         MoveTo(clip.x_max - tsize, clip.y_max);
1284         DrawTo(clip.x_max, clip.y_max);
1285         fAtCorner = fTrue;
1286      } else {
1287         fAtCorner = fFalse;
1288      }
1289   }
1290
1291   if (x == m_layout->pagesX - 1 && m_layout->Border) {
1292      /* solid right border */
1293      if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1294      DrawTo(clip.x_max, clip.y_min);
1295      fAtCorner = fTrue;
1296   } else {
1297      if (x < m_layout->pagesX - 1 || y < m_layout->pagesY - 1) {
1298         if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1299         DrawTo(clip.x_max, clip.y_max - tsize);
1300      }
1301      if (s && x < m_layout->pagesX - 1 && m_layout->Cutlines) {
1302         /* dashed right border */
1303         i = (clip.y_max - clip.y_min) -
1304             (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1305         for ( ; i > tsize; i -= s) {
1306            MoveTo(clip.x_max, clip.y_min + (i + o));
1307            DrawTo(clip.x_max, clip.y_min + (i - o));
1308         }
1309      }
1310      if (x < m_layout->pagesX - 1 || y > 0) {
1311         MoveTo(clip.x_max, clip.y_min + tsize);
1312         DrawTo(clip.x_max, clip.y_min);
1313         fAtCorner = fTrue;
1314      } else {
1315         fAtCorner = fFalse;
1316      }
1317   }
1318
1319   if (y == 0 && m_layout->Border) {
1320      /* solid bottom border */
1321      if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1322      DrawTo(clip.x_min, clip.y_min);
1323   } else {
1324      if (y > 0 || x < m_layout->pagesX - 1) {
1325         if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1326         DrawTo(clip.x_max - tsize, clip.y_min);
1327      }
1328      if (s && y > 0 && m_layout->Cutlines) {
1329         /* dashed bottom border */
1330         i = (clip.x_max - clip.x_min) -
1331             (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1332         for ( ; i > tsize; i -= s) {
1333            MoveTo(clip.x_min + (i + o), clip.y_min);
1334            DrawTo(clip.x_min + (i - o), clip.y_min);
1335         }
1336      }
1337      if (y > 0 || x > 0) {
1338         MoveTo(clip.x_min + tsize, clip.y_min);
1339         DrawTo(clip.x_min, clip.y_min);
1340      }
1341   }
1342}
1343
1344bool
1345svxPrintout::OnPrintPage(int pageNum) {
1346    GetPageSizePixels(&xpPageWidth, &ypPageDepth);
1347    pdc = GetDC();
1348#ifdef AVEN_PRINT_PREVIEW
1349    if (IsPreview()) {
1350        int dcx, dcy;
1351        pdc->GetSize(&dcx, &dcy);
1352        pdc->SetUserScale((double)dcx / xpPageWidth, (double)dcy / ypPageDepth);
1353    }
1354#endif
1355
1356    layout * l = m_layout;
1357    {
1358        int pwidth, pdepth;
1359        GetPageSizeMM(&pwidth, &pdepth);
1360        l->scX = (double)xpPageWidth / pwidth;
1361        l->scY = (double)ypPageDepth / pdepth;
1362        font_scaling_x = l->scX * (25.4 / 72.0);
1363        font_scaling_y = l->scY * (25.4 / 72.0);
1364        MarginLeft = m_data->GetMarginTopLeft().x;
1365        MarginTop = m_data->GetMarginTopLeft().y;
1366        MarginBottom = m_data->GetMarginBottomRight().y;
1367        MarginRight = m_data->GetMarginBottomRight().x;
1368        xpPageWidth -= (int)(l->scX * (MarginLeft + MarginRight));
1369        ypPageDepth -= (int)(l->scY * (10 + MarginBottom + MarginRight));
1370        // xpPageWidth -= 1;
1371        pdepth -= 10;
1372        x_offset = (long)(l->scX * MarginLeft);
1373        y_offset = (long)(l->scY * MarginTop);
1374        l->PaperWidth = pwidth -= MarginLeft + MarginRight;
1375        l->PaperDepth = pdepth -= MarginTop + MarginBottom;
1376    }
1377
1378    double SIN = sin(rad(l->rot));
1379    double COS = cos(rad(l->rot));
1380    double SINT = sin(rad(l->tilt));
1381    double COST = cos(rad(l->tilt));
1382
1383    NewPage(pageNum, l->pagesX, l->pagesY);
1384
1385    if (l->Legend && pageNum == (l->pagesY - 1) * l->pagesX + 1) {
1386        SetFont(PR_FONT_DEFAULT);
1387        draw_info_box();
1388    }
1389
1390    pdc->SetClippingRegion(x_offset, y_offset,xpPageWidth+1, ypPageDepth+1);
1391
1392    const double Sc = 1000 / l->Scale;
1393
1394    if (l->show_mask & LEGS) {
1395        SetColour(PR_COLOUR_LEG);
1396        list<traverse>::const_iterator trav = mainfrm->traverses_begin();
1397        list<traverse>::const_iterator tend = mainfrm->traverses_end();
1398        for ( ; trav != tend; ++trav) {
1399            vector<PointInfo>::const_iterator pos = trav->begin();
1400            vector<PointInfo>::const_iterator end = trav->end();
1401            for ( ; pos != end; ++pos) {
1402                double x = pos->GetX();
1403                double y = pos->GetY();
1404                double z = pos->GetZ();
1405                double X = x * COS - y * SIN;
1406                double Y = z * COST - (x * SIN + y * COS) * SINT;
1407                long px = (long)((X * Sc + l->xOrg) * l->scX);
1408                long py = (long)((Y * Sc + l->yOrg) * l->scY);
1409                if (pos == trav->begin()) {
1410                    MoveTo(px, py);
1411                } else {
1412                    DrawTo(px, py);
1413                }
1414            }
1415        }
1416    }
1417
1418    if ((l->show_mask & XSECT) &&
1419        (l->tilt == 0.0 || l->tilt == 90.0 || l->tilt == -90.0)) {
1420        list<vector<XSect> >::const_iterator trav = mainfrm->tubes_begin();
1421        list<vector<XSect> >::const_iterator tend = mainfrm->tubes_end();
1422        for ( ; trav != tend; ++trav) {
1423            if (l->tilt == 90.0 || l->tilt == -90.0) PlotLR(*trav);
1424            if (l->tilt == 0.0) PlotUD(*trav);
1425        }
1426    }
1427
1428    if (l->show_mask & SURF) {
1429        SetColour(PR_COLOUR_SURFACE_LEG);
1430        list<traverse>::const_iterator trav = mainfrm->surface_traverses_begin();
1431        list<traverse>::const_iterator tend = mainfrm->surface_traverses_end();
1432        for ( ; trav != tend; ++trav) {
1433            vector<PointInfo>::const_iterator pos = trav->begin();
1434            vector<PointInfo>::const_iterator end = trav->end();
1435            for ( ; pos != end; ++pos) {
1436                double x = pos->GetX();
1437                double y = pos->GetY();
1438                double z = pos->GetZ();
1439                double X = x * COS - y * SIN;
1440                double Y = z * COST - (x * SIN + y * COS) * SINT;
1441                long px = (long)((X * Sc + l->xOrg) * l->scX);
1442                long py = (long)((Y * Sc + l->yOrg) * l->scY);
1443                if (pos == trav->begin()) {
1444                    MoveTo(px, py);
1445                } else {
1446                    DrawTo(px, py);
1447                }
1448            }
1449        }
1450    }
1451
1452    if (l->show_mask & (LABELS|STNS)) {
1453        if (l->show_mask & LABELS) SetFont(PR_FONT_LABELS);
1454        list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
1455        while (label != mainfrm->GetLabelsEnd()) {
1456            double px = (*label)->GetX();
1457            double py = (*label)->GetY();
1458            double pz = (*label)->GetZ();
1459            if ((l->show_mask & SURF) || (*label)->IsUnderground()) {
1460                double X = px * COS - py * SIN;
1461                double Y = pz * COST - (px * SIN + py * COS) * SINT;
1462                long xnew, ynew;
1463                xnew = (long)((X * Sc + l->xOrg) * l->scX);
1464                ynew = (long)((Y * Sc + l->yOrg) * l->scY);
1465                if (l->show_mask & STNS) {
1466                    SetColour(PR_COLOUR_CROSS);
1467                    DrawCross(xnew, ynew);
1468                }
1469                if (l->show_mask & LABELS) {
1470                    SetColour(PR_COLOUR_LABELS);
1471                    MoveTo(xnew, ynew);
1472                    WriteString((*label)->GetText());
1473                }
1474            }
1475            ++label;
1476        }
1477    }
1478
1479    return true;
1480}
1481
1482void
1483svxPrintout::GetPageInfo(int *minPage, int *maxPage,
1484                         int *pageFrom, int *pageTo)
1485{
1486    *minPage = *pageFrom = 1;
1487    *maxPage = *pageTo = m_layout->pages;
1488}
1489
1490bool
1491svxPrintout::HasPage(int pageNum) {
1492    return (pageNum <= m_layout->pages);
1493}
1494
1495void
1496svxPrintout::OnBeginPrinting() {
1497    FILE *fh_list[4];
1498
1499    FILE **pfh = fh_list;
1500    FILE *fh;
1501    const char *pth_cfg;
1502    char *print_ini;
1503
1504    /* ini files searched in this order:
1505     * ~/.survex/print.ini [unix only]
1506     * /etc/survex/print.ini [unix only]
1507     * <support file directory>/myprint.ini [not unix]
1508     * <support file directory>/print.ini [must exist]
1509     */
1510
1511#ifdef __UNIX__
1512    pth_cfg = getenv("HOME");
1513    if (pth_cfg) {
1514        fh = fopenWithPthAndExt(pth_cfg, ".survex/print."EXT_INI, NULL,
1515                "rb", NULL);
1516        if (fh) *pfh++ = fh;
1517    }
1518    pth_cfg = msg_cfgpth();
1519    fh = fopenWithPthAndExt(NULL, "/etc/survex/print."EXT_INI, NULL, "rb",
1520            NULL);
1521    if (fh) *pfh++ = fh;
1522#else
1523    pth_cfg = msg_cfgpth();
1524    print_ini = add_ext("myprint", EXT_INI);
1525    fh = fopenWithPthAndExt(pth_cfg, print_ini, NULL, "rb", NULL);
1526    if (fh) *pfh++ = fh;
1527#endif
1528    print_ini = add_ext("print", EXT_INI);
1529    fh = fopenWithPthAndExt(pth_cfg, print_ini, NULL, "rb", NULL);
1530    if (!fh) fatalerror(/*Couldn’t open file “%s”*/24, print_ini);
1531    *pfh++ = fh;
1532    *pfh = NULL;
1533    Init(pfh, false);
1534    for (pfh = fh_list; *pfh; pfh++) (void)fclose(*pfh);
1535    Pre();
1536    m_layout->footer = wmsg(/*Survey “%s”   Page %d (of %d)   Processed on %s*/167);
1537}
1538
1539void
1540svxPrintout::OnEndPrinting() {
1541    delete font_labels;
1542    delete font_default;
1543    delete pen_frame;
1544    delete pen_leg;
1545    delete pen_surface_leg;
1546    delete pen_cross;
1547}
1548
1549
1550// prcore -> wx.grafx (calls to move pens around and stuff - low level)
1551// this seems to have been done...
1552
1553
1554
1555static border clip;
1556
1557
1558int
1559svxPrintout::check_intersection(long x_p, long y_p)
1560{
1561#define U 1
1562#define D 2
1563#define L 4
1564#define R 8
1565   int mask_p = 0, mask_t = 0;
1566   if (x_p < 0)
1567      mask_p = L;
1568   else if (x_p > xpPageWidth)
1569      mask_p = R;
1570
1571   if (y_p < 0)
1572      mask_p |= D;
1573   else if (y_p > ypPageDepth)
1574      mask_p |= U;
1575
1576   if (x_t < 0)
1577      mask_t = L;
1578   else if (x_t > xpPageWidth)
1579      mask_t = R;
1580
1581   if (y_t < 0)
1582      mask_t |= D;
1583   else if (y_t > ypPageDepth)
1584      mask_t |= U;
1585
1586#if 0
1587   /* approximation to correct answer */
1588   return !(mask_t & mask_p);
1589#else
1590   /* One end of the line is on the page */
1591   if (!mask_t || !mask_p) return 1;
1592
1593   /* whole line is above, left, right, or below page */
1594   if (mask_t & mask_p) return 0;
1595
1596   if (mask_t == 0) mask_t = mask_p;
1597   if (mask_t & U) {
1598      double v = (double)(y_p - ypPageDepth) / (y_p - y_t);
1599      return v >= 0 && v <= 1;
1600   }
1601   if (mask_t & D) {
1602      double v = (double)y_p / (y_p - y_t);
1603      return v >= 0 && v <= 1;
1604   }
1605   if (mask_t & R) {
1606      double v = (double)(x_p - xpPageWidth) / (x_p - x_t);
1607      return v >= 0 && v <= 1;
1608   }
1609   SVX_ASSERT(mask_t & L);
1610   {
1611      double v = (double)x_p / (x_p - x_t);
1612      return v >= 0 && v <= 1;
1613   }
1614#endif
1615#undef U
1616#undef D
1617#undef L
1618#undef R
1619}
1620
1621void
1622svxPrintout::MoveTo(long x, long y)
1623{
1624    x_t = x_offset + x - clip.x_min;
1625    y_t = y_offset + clip.y_max - y;
1626}
1627
1628void
1629svxPrintout::DrawTo(long x, long y)
1630{
1631    long x_p = x_t, y_p = y_t;
1632    x_t = x_offset + x - clip.x_min;
1633    y_t = y_offset + clip.y_max - y;
1634    if (cur_pass != -1) {
1635        pdc->DrawLine(x_p, y_p, x_t, y_t);
1636    } else {
1637        if (check_intersection(x_p, y_p)) fBlankPage = fFalse;
1638    }
1639}
1640
1641#define POINTS_PER_INCH 72.0
1642#define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH)
1643#define PWX_CROSS_SIZE (int)(2 * m_layout->scX / POINTS_PER_MM)
1644
1645void
1646svxPrintout::DrawCross(long x, long y)
1647{
1648   if (cur_pass != -1) {
1649      MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1650      DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1651      MoveTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1652      DrawTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1653      MoveTo(x, y);
1654   } else {
1655      if ((x + PWX_CROSS_SIZE > clip.x_min &&
1656           x - PWX_CROSS_SIZE < clip.x_max) ||
1657          (y + PWX_CROSS_SIZE > clip.y_min &&
1658           y - PWX_CROSS_SIZE < clip.y_max)) {
1659         fBlankPage = fFalse;
1660      }
1661   }
1662}
1663
1664void
1665svxPrintout::SetFont(int fontcode)
1666{
1667    switch (fontcode) {
1668        case PR_FONT_DEFAULT:
1669            current_font = font_default;
1670            break;
1671        case PR_FONT_LABELS:
1672            current_font = font_labels;
1673            break;
1674        default:
1675            BUG("unknown font code");
1676    }
1677}
1678
1679void
1680svxPrintout::SetColour(int colourcode)
1681{
1682    switch (colourcode) {
1683        case PR_COLOUR_TEXT:
1684            pdc->SetTextForeground(colour_text);
1685            break;
1686        case PR_COLOUR_LABELS:
1687            pdc->SetTextForeground(colour_labels);
1688            pdc->SetBackgroundMode(wxTRANSPARENT);
1689            break;
1690        case PR_COLOUR_FRAME:
1691            pdc->SetPen(*pen_frame);
1692            break;
1693        case PR_COLOUR_LEG:
1694            pdc->SetPen(*pen_leg);
1695            break;
1696        case PR_COLOUR_CROSS:
1697            pdc->SetPen(*pen_cross);
1698            break;
1699        case PR_COLOUR_SURFACE_LEG:
1700            pdc->SetPen(*pen_surface_leg);
1701            break;
1702        default:
1703            BUG("unknown colour code");
1704    }
1705}
1706
1707void
1708svxPrintout::WriteString(const wxString & s)
1709{
1710    double xsc, ysc;
1711    pdc->GetUserScale(&xsc, &ysc);
1712    pdc->SetUserScale(xsc * font_scaling_x, ysc * font_scaling_y);
1713    pdc->SetFont(*current_font);
1714    int w, h;
1715    if (cur_pass != -1) {
1716        pdc->GetTextExtent(wxT("My"), &w, &h);
1717        pdc->DrawText(s,
1718                      long(x_t / font_scaling_x),
1719                      long(y_t / font_scaling_y) - h);
1720    } else {
1721        pdc->GetTextExtent(s, &w, &h);
1722        if ((y_t + h > 0 && y_t - h < clip.y_max - clip.y_min) ||
1723            (x_t < clip.x_max - clip.x_min && x_t + w > 0)) {
1724            fBlankPage = fFalse;
1725        }
1726    }
1727    pdc->SetUserScale(xsc, ysc);
1728}
1729
1730void
1731svxPrintout::DrawEllipse(long x, long y, long r, long R)
1732{
1733    /* Don't need to check in first-pass - circle is only used in title box */
1734    if (cur_pass != -1) {
1735        x_t = x_offset + x - clip.x_min;
1736        y_t = y_offset + clip.y_max - y;
1737        const wxBrush & save_brush = pdc->GetBrush();
1738        pdc->SetBrush(*wxTRANSPARENT_BRUSH);
1739        pdc->DrawEllipse(x_t - r, y_t - R, 2 * r, 2 * R);
1740        pdc->SetBrush(save_brush);
1741    }
1742}
1743
1744void
1745svxPrintout::SolidRectangle(long x, long y, long w, long h)
1746{
1747    long X = x_offset + x - clip.x_min;
1748    long Y = y_offset + clip.y_max - y;
1749    pdc->SetBrush(*wxBLACK_BRUSH);
1750    pdc->DrawRectangle(X, Y - h, w, h);
1751}
1752
1753int
1754svxPrintout::Pre()
1755{
1756    font_labels = new wxFont(fontsize_labels, wxDEFAULT, wxNORMAL, wxNORMAL,
1757                             false, wxString(fontname_labels, wxConvUTF8),
1758                             wxFONTENCODING_ISO8859_1);
1759    font_default = new wxFont(fontsize, wxDEFAULT, wxNORMAL, wxNORMAL,
1760                              false, wxString(fontname, wxConvUTF8),
1761                              wxFONTENCODING_ISO8859_1);
1762    current_font = font_default;
1763    pen_leg = new wxPen(colour_leg);
1764    pen_surface_leg = new wxPen(colour_surface_leg);
1765    pen_cross = new wxPen(colour_cross);
1766    pen_frame = new wxPen(colour_frame);
1767    return 1; /* only need 1 pass */
1768}
1769
1770void
1771svxPrintout::NewPage(int pg, int pagesX, int pagesY)
1772{
1773    int x, y;
1774    x = (pg - 1) % pagesX;
1775    y = pagesY - 1 - ((pg - 1) / pagesX);
1776
1777    clip.x_min = (long)x * xpPageWidth;
1778    clip.y_min = (long)y * ypPageDepth;
1779    clip.x_max = clip.x_min + xpPageWidth; /* dm/pcl/ps had -1; */
1780    clip.y_max = clip.y_min + ypPageDepth; /* dm/pcl/ps had -1; */
1781
1782    //we have to write the footer here. PostScript is being weird. Really weird.
1783    pdc->SetFont(*font_labels);
1784    MoveTo(clip.x_min, clip.y_min - (long)(7 * m_layout->scY));
1785    wxString footer;
1786    footer.Printf(m_layout->footer,
1787                  m_layout->title.c_str(),
1788                  pg,
1789                  m_layout->pagesX * m_layout->pagesY,
1790                  m_layout->datestamp.c_str());
1791    WriteString(footer);
1792    pdc->DestroyClippingRegion();
1793    drawticks(clip, (int)(9 * m_layout->scX / POINTS_PER_MM), x, y);
1794}
1795
1796void
1797svxPrintout::PlotLR(const vector<XSect> & centreline)
1798{
1799    assert(centreline.size() > 1);
1800    XSect prev_pt_v;
1801    Vector3 last_right(1.0, 0.0, 0.0);
1802
1803    const double Sc = 1000 / m_layout->Scale;
1804    const double SIN = sin(rad(m_layout->rot));
1805    const double COS = cos(rad(m_layout->rot));
1806
1807    vector<XSect>::const_iterator i = centreline.begin();
1808    vector<XSect>::size_type segment = 0;
1809    while (i != centreline.end()) {
1810        // get the coordinates of this vertex
1811        const XSect & pt_v = *i++;
1812
1813        Vector3 right;
1814
1815        const Vector3 up_v(0.0, 0.0, 1.0);
1816
1817        if (segment == 0) {
1818            assert(i != centreline.end());
1819            // first segment
1820
1821            // get the coordinates of the next vertex
1822            const XSect & next_pt_v = *i;
1823
1824            // calculate vector from this pt to the next one
1825            Vector3 leg_v = next_pt_v - pt_v;
1826
1827            // obtain a vector in the LRUD plane
1828            right = leg_v * up_v;
1829            if (right.magnitude() == 0) {
1830                right = last_right;
1831            } else {
1832                last_right = right;
1833            }
1834        } else if (segment + 1 == centreline.size()) {
1835            // last segment
1836
1837            // Calculate vector from the previous pt to this one.
1838            Vector3 leg_v = pt_v - prev_pt_v;
1839
1840            // Obtain a horizontal vector in the LRUD plane.
1841            right = leg_v * up_v;
1842            if (right.magnitude() == 0) {
1843                right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
1844            } else {
1845                last_right = right;
1846            }
1847        } else {
1848            assert(i != centreline.end());
1849            // Intermediate segment.
1850
1851            // Get the coordinates of the next vertex.
1852            const XSect & next_pt_v = *i;
1853
1854            // Calculate vectors from this vertex to the
1855            // next vertex, and from the previous vertex to
1856            // this one.
1857            Vector3 leg1_v = pt_v - prev_pt_v;
1858            Vector3 leg2_v = next_pt_v - pt_v;
1859
1860            // Obtain horizontal vectors perpendicular to
1861            // both legs, then normalise and average to get
1862            // a horizontal bisector.
1863            Vector3 r1 = leg1_v * up_v;
1864            Vector3 r2 = leg2_v * up_v;
1865            r1.normalise();
1866            r2.normalise();
1867            right = r1 + r2;
1868            if (right.magnitude() == 0) {
1869                // This is the "mid-pitch" case...
1870                right = last_right;
1871            }
1872            last_right = right;
1873        }
1874
1875        // Scale to unit vectors in the LRUD plane.
1876        right.normalise();
1877
1878        Double l = pt_v.GetL();
1879        Double r = pt_v.GetR();
1880
1881        if (l >= 0) {
1882            Vector3 p = pt_v - right * l;
1883            double X = p.GetX() * COS - p.GetY() * SIN;
1884            double Y = (p.GetX() * SIN + p.GetY() * COS);
1885            long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
1886            long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
1887            MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1888            DrawTo(x, y);
1889            DrawTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1890        }
1891        if (r >= 0) {
1892            Vector3 p = pt_v + right * r;
1893            double X = p.GetX() * COS - p.GetY() * SIN;
1894            double Y = (p.GetX() * SIN + p.GetY() * COS);
1895            long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
1896            long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
1897            MoveTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1898            DrawTo(x, y);
1899            DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1900        }
1901
1902        prev_pt_v = pt_v;
1903
1904        ++segment;
1905    }
1906}
1907
1908void
1909svxPrintout::PlotUD(const vector<XSect> & centreline)
1910{
1911    assert(centreline.size() > 1);
1912    const double Sc = 1000 / m_layout->Scale;
1913
1914    vector<XSect>::const_iterator i = centreline.begin();
1915    while (i != centreline.end()) {
1916        // get the coordinates of this vertex
1917        const XSect & pt_v = *i++;
1918
1919        Double u = pt_v.GetU();
1920        Double d = pt_v.GetD();
1921
1922        if (u >= 0 || d >= 0) {
1923            Vector3 p = pt_v;
1924            double SIN = sin(rad(m_layout->rot));
1925            double COS = cos(rad(m_layout->rot));
1926            double X = p.GetX() * COS - p.GetY() * SIN;
1927            double Y = p.GetZ();
1928            long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
1929            if (u >= 0) {
1930                long y = (long)(((Y + u) * Sc + m_layout->yOrg) * m_layout->scY);
1931                MoveTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1932                DrawTo(x, y);
1933                DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1934            }
1935            if (d >= 0) {
1936                long y = (long)(((Y - d) * Sc + m_layout->yOrg) * m_layout->scY);
1937                MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1938                DrawTo(x, y);
1939                DrawTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1940            }
1941        }
1942    }
1943}
1944
1945static wxColour
1946to_rgb(const char *var, char *val)
1947{
1948   unsigned long rgb;
1949   if (!val) return *wxBLACK;
1950   rgb = as_colour(var, val);
1951   return wxColour((rgb & 0xff0000) >> 16, (rgb & 0xff00) >> 8, rgb & 0xff);
1952}
1953
1954/* Initialise printer routines */
1955char *
1956svxPrintout::Init(FILE **fh_list, bool fCalibrate)
1957{
1958   static const char *vars[] = {
1959      "font_size_labels",
1960      "colour_text",
1961      "colour_labels",
1962      "colour_frame",
1963      "colour_legs",
1964      "colour_crosses",
1965      "colour_surface_legs",
1966      NULL
1967   };
1968   char **vals;
1969
1970   (void)fCalibrate; /* suppress unused argument warning */
1971
1972   vals = ini_read(fh_list, "aven", vars);
1973   fontsize_labels = 10;
1974   if (vals[0]) fontsize_labels = as_int(vars[0], vals[0], 1, INT_MAX);
1975   fontsize = 10;
1976
1977   colour_text = colour_labels = colour_frame = colour_leg = colour_cross = colour_surface_leg = *wxBLACK;
1978   if (vals[1]) colour_text = to_rgb(vars[1], vals[1]);
1979   if (vals[2]) colour_labels = to_rgb(vars[2], vals[2]);
1980   if (vals[3]) colour_frame = to_rgb(vars[3], vals[3]);
1981   if (vals[4]) colour_leg = to_rgb(vars[4], vals[4]);
1982   if (vals[5]) colour_cross = to_rgb(vars[5], vals[5]);
1983   if (vals[6]) colour_surface_leg = to_rgb(vars[6], vals[6]);
1984   m_layout->scX = 1;
1985   m_layout->scY = 1;
1986   return NULL;
1987}
Note: See TracBrowser for help on using the repository browser.