source: git/src/printwx.cc @ cca2ce1

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

src/printwx.cc,src/printwx.h: Don't compile in the preview code when
we disable the preview feature.

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

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