source: git/src/printwx.cc @ da6367cd

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

src/printwx.cc: Fix incorrect message in comment.

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