source: git/src/printwx.cc @ 70462c8

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

src/: Pass the datestamp from the 3d file to the export code.

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