source: git/src/printwx.cc @ ac7e4da

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-datawalls-data-hanging-as-warning
Last change on this file since ac7e4da was 1324d6c, checked in by Olly Betts <olly@…>, 15 years ago

Say "wxWidgets" instead of "wxWindows" consistently.
Require wxWidgets 2.6.0 or newer - 2.4 is really old now.

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