source: git/src/printwx.cc @ f10cf8f

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

src/: Pass the numeric datestamp to ExportFilter::header(), and use
it to set a timestamp in exported GPX files.

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