source: git/src/printwx.cc @ e24b7fb

RELEASE/1.2debug-cidebug-ci-sanitisersfaster-cavernlogstereowalls-datawalls-data-hanging-as-warning
Last change on this file since e24b7fb was a6dddd1, checked in by Olly Betts <olly@…>, 11 years ago

src/gpx.cc,src/gpx.h,src/printwx.cc: Add support for exporting legs
as tracks in GPX.

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