source: git/trunk/src/printwx.cc @ 7bb8184

Last change on this file since 7bb8184 was 7bb8184, checked in by Olly Betts <olly@…>, 13 years ago

Retagging 1.2.0

git-svn-id: file:///home/survex-svn/survex/tags/1.2.0@3664 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

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