source: git/src/printwx.cc @ 6e63fd3

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

lib/codes.po,src/log.cc,src/mainfrm.cc,src/printwx.cc: Make more
messages translatable.

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