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

line_contentsstereotravis-osx
Last change on this file since 0b8c321 was 0b8c321, checked in by Olly Betts <olly@…>, 5 years ago

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

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