source: git/src/printwx.cc @ ecf9c4c1

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

src/mainfrm.cc,src/printwx.cc: When using a standard wxID_xxx id with
wxButton, use the implicit default label, as the wx documentation
recommends.

git-svn-id: file:///home/survex-svn/survex/trunk@3606 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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