source: git/src/printwx.cc @ ccb83b7

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

lib/,src/: Make all uses of unit names translatable.

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