source: git/src/printwx.cc @ 193efb1

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

src/printwx.cc: The Gnome print dialog has its own preview window
so suppress ours if using the Gnome one.

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

  • Property mode set to 100644
File size: 46.3 KB
Line 
1/* printwx.cc */
2/* wxWidgets specific parts of Survex wxWidgets printing code */
3/* Copyright (C) 1993-2003,2004,2005,2006 Olly Betts
4 * Copyright (C) 2001,2004 Philip Underwood
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 */
20
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
24
25#include <vector>
26
27using namespace std;
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <math.h>
32#include <string.h>
33#include <time.h>
34#include <ctype.h>
35#include <float.h>
36#include <limits.h>
37#include <wx/filename.h>
38#include <wx/print.h>
39#include <wx/printdlg.h>
40#include <wx/spinctrl.h>
41#include <wx/radiobox.h>
42#include <wx/statbox.h>
43
44#include "debug.h" /* for BUG and SVX_ASSERT */
45#include "export.h"
46#include "filelist.h"
47#include "filename.h"
48#include "ini.h"
49#include "message.h"
50#include "useful.h"
51
52#include "aven.h"
53#include "avenprcore.h"
54#include "mainfrm.h"
55#include "printwx.h"
56
57enum {
58        svx_PRINT = 1200,
59        svx_EXPORT,
60        svx_PREVIEW,
61        svx_SCALE,
62        svx_BEARING,
63        svx_TILT,
64        svx_LEGS,
65        svx_STATIONS,
66        svx_NAMES,
67        svx_BORDERS,
68        svx_BLANKS,
69        svx_INFOBOX,
70        svx_SURFACE,
71        svx_PLAN,
72        svx_ELEV
73};
74
75class svxPrintout : public wxPrintout {
76    MainFrm *mainfrm;
77    layout *m_layout;
78    wxString m_title;
79    wxPageSetupDialogData* m_data;
80    wxDC* pdc;
81    static const int cur_pass = 0;
82
83    wxPen *pen_frame, *pen_cross, *pen_surface_leg, *pen_leg;
84    wxColour colour_text, colour_labels, colour_frame, colour_leg;
85    wxColour colour_cross,colour_surface_leg;
86
87    long x_t, y_t;
88    double font_scaling_x, font_scaling_y;
89    wxFont * current_font;
90
91    int check_intersection(long x_p, long y_p);
92    void draw_info_box();
93    void draw_scale_bar(double x, double y, double MaxLength);
94    int next_page(int *pstate, char **q, int pageLim);
95    void drawticks(border clip, int tsize, int x, int y);
96
97    void MOVEMM(double X, double Y) {
98        MoveTo((long)(X * m_layout->scX), (long)(Y * m_layout->scY));
99    }
100    void DRAWMM(double X, double Y) {
101        DrawTo((long)(X * m_layout->scX), (long)(Y * m_layout->scY));
102    }
103    void MoveTo(long x, long y);
104    void DrawTo(long x, long y);
105    void DrawCross(long x, long y);
106    void SetFont(int fontcode);
107    void SetColour(int colourcode);
108    void WriteString(const wxString & s);
109    void DrawEllipse(long x, long y, long r, long R);
110    void SolidRectangle(long x, long y, long w, long h);
111    int Charset(void);
112    int Pre();
113    void NewPage(int pg, int pagesX, int pagesY);
114    void ShowPage(const char *szPageDetails);
115    void PlotLR(const vector<XSect> & centreline);
116    void PlotUD(const vector<XSect> & centreline);
117    char * Init(FILE **fh_list, bool fCalibrate);
118  public:
119    svxPrintout(MainFrm *mainfrm, layout *l, wxPageSetupDialogData *data, const wxString & title);
120    bool OnPrintPage(int pageNum);
121    void GetPageInfo(int *minPage, int *maxPage,
122                     int *pageFrom, int *pageTo);
123    wxString GetTitle();
124    bool HasPage(int pageNum);
125    void OnBeginPrinting();
126    void OnEndPrinting();
127};
128
129BEGIN_EVENT_TABLE(svxPrintDlg, wxDialog)
130    EVT_TEXT(svx_SCALE, svxPrintDlg::OnChange)
131    EVT_COMBOBOX(svx_SCALE, svxPrintDlg::OnChange)
132    EVT_SPINCTRL(svx_BEARING, svxPrintDlg::OnChangeSpin)
133    EVT_SPINCTRL(svx_TILT, svxPrintDlg::OnChangeSpin)
134    EVT_BUTTON(svx_PRINT, svxPrintDlg::OnPrint)
135    EVT_BUTTON(svx_EXPORT, svxPrintDlg::OnExport)
136    EVT_BUTTON(svx_PREVIEW, svxPrintDlg::OnPreview)
137    EVT_BUTTON(svx_PLAN, svxPrintDlg::OnPlan)
138    EVT_BUTTON(svx_ELEV, svxPrintDlg::OnElevation)
139    EVT_CHECKBOX(svx_LEGS, svxPrintDlg::OnChange)
140    EVT_CHECKBOX(svx_STATIONS, svxPrintDlg::OnChange)
141    EVT_CHECKBOX(svx_NAMES, svxPrintDlg::OnChange)
142    EVT_CHECKBOX(svx_SURFACE, svxPrintDlg::OnChange)
143END_EVENT_TABLE()
144
145static wxString scales[] = {
146    wxT(""),
147    wxT("25"),
148    wxT("50"),
149    wxT("100"),
150    wxT("250"),
151    wxT("500"),
152    wxT("1000"),
153    wxT("2500"),
154    wxT("5000"),
155    wxT("10000"),
156    wxT("25000"),
157    wxT("50000"),
158    wxT("100000")
159};
160
161// there are three jobs to do here...
162// User <-> wx - this should possibly be done in a separate file
163svxPrintDlg::svxPrintDlg(MainFrm* mainfrm_, const wxString & filename,
164                         const wxString & title, const wxString & datestamp,
165                         double angle, double tilt_angle,
166                         bool labels, bool crosses, bool legs, bool surf,
167                         bool printing)
168        : wxDialog(mainfrm_, -1, wxString(printing ? wmsg(/*Print*/399) : wmsg(/*Export*/383))),
169          m_layout(wxGetApp().GetPageSetupDialogData()),
170          m_File(filename), mainfrm(mainfrm_)
171{
172    m_scale = NULL;
173    m_printSize = NULL;
174    m_tilttext = NULL;
175    m_bearing = NULL;
176    m_tilt = NULL;
177    m_legs = NULL;
178    m_stations = NULL;
179    m_names = NULL;
180    m_borders = NULL;
181    m_infoBox = NULL;
182    m_surface = NULL;
183    m_layout.Labels = labels;
184    m_layout.Crosses = crosses;
185    m_layout.Shots = legs;
186    m_layout.Surface = surf;
187    m_layout.datestamp = datestamp;
188    m_layout.rot = int(angle + .001);
189    m_layout.title = title;
190    if (mainfrm->IsExtendedElevation()) {
191        m_layout.view = layout::EXTELEV;
192        if (m_layout.rot != 0 && m_layout.rot != 180) m_layout.rot = 0;
193        m_layout.tilt = 0;
194    } else {
195        // FIXME rot and tilt shouldn't be integers, but for now add a small
196        // fraction before forcing to int as otherwise plan view ends up being
197        // 89 degrees!
198        m_layout.tilt = int(tilt_angle + .001);
199        if (m_layout.tilt == 90) {
200            m_layout.view = layout::PLAN;
201        } else if (m_layout.tilt == 0) {
202            m_layout.view = layout::ELEV;
203        } else {
204            m_layout.view = layout::TILT;
205        }
206    }
207
208    /* setup our print dialog*/
209    wxBoxSizer* v1 = new wxBoxSizer(wxVERTICAL);
210    wxBoxSizer* h1 = new wxBoxSizer(wxHORIZONTAL); // holds controls
211    wxBoxSizer* v2 = new wxStaticBoxSizer(new wxStaticBox(this, -1, wmsg(/*View*/255)), wxVERTICAL);
212    wxBoxSizer* v3 = new wxStaticBoxSizer(new wxStaticBox(this, -1, wmsg(/*Elements*/256)), wxVERTICAL);
213    wxBoxSizer* h2 = new wxBoxSizer(wxHORIZONTAL); // holds buttons
214
215    if (printing) {
216        wxStaticText* label;
217        label = new wxStaticText(this, -1, wxString(wmsg(/*Scale*/154)) + wxT(" 1:"));
218        if (scales[0].empty()) scales[0].assign(wmsg(/*One page*/258));
219        m_scale = new wxComboBox(this, svx_SCALE, scales[0], wxDefaultPosition,
220                                 wxDefaultSize, sizeof(scales) / sizeof(scales[0]),
221                                 scales);
222        wxBoxSizer* scalebox = new wxBoxSizer(wxHORIZONTAL);
223        scalebox->Add(label, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
224        scalebox->Add(m_scale, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
225
226        v2->Add(scalebox, 0, wxALIGN_LEFT|wxALL, 0);
227
228        // Make the dummy string wider than any sane value and use that to
229        // fix the width of the control so the sizers allow space for bigger
230        // page layouts.
231        m_printSize = new wxStaticText(this, -1, wxString::Format(wmsg(/*%d pages (%dx%d)*/257), 9604, 98, 98));
232        v2->Add(m_printSize, 0, wxALIGN_LEFT|wxALL, 5);
233    }
234
235    if (m_layout.view != layout::EXTELEV) {
236        wxFlexGridSizer* anglebox = new wxFlexGridSizer(2);
237        wxStaticText * brg_label, * tilt_label;
238        brg_label = new wxStaticText(this, -1, wmsg(/*Bearing*/259));
239        anglebox->Add(brg_label, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxALL, 5);
240        m_bearing = new wxSpinCtrl(this, svx_BEARING);
241        m_bearing->SetRange(0, 359);
242        anglebox->Add(m_bearing, 0, wxALIGN_CENTER|wxALL, 5);
243        tilt_label = new wxStaticText(this, -1, wmsg(/*Tilt angle*/263));
244        anglebox->Add(tilt_label, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxALL, 5);
245        m_tilt = new wxSpinCtrl(this, svx_TILT);
246        m_tilt->SetRange(-90, 90);
247        anglebox->Add(m_tilt, 0, wxALIGN_CENTER|wxALL, 5);
248
249        v2->Add(anglebox, 0, wxALIGN_LEFT|wxALL, 0);
250
251        wxBoxSizer * planelevsizer = new wxBoxSizer(wxHORIZONTAL);
252        planelevsizer->Add(new wxButton(this, svx_PLAN, wmsg(/*Plan*/117)),
253                           0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
254        planelevsizer->Add(new wxButton(this, svx_ELEV, wmsg(/*Elevation*/118)),
255                           0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
256
257        v2->Add(planelevsizer, 0, wxALIGN_LEFT|wxALL, 5);
258    }
259
260    h1->Add(v2, 0, wxALIGN_LEFT|wxALL, 5);
261
262    m_legs = new wxCheckBox(this, svx_LEGS, wmsg(/*Underground Survey Legs*/262));
263    v3->Add(m_legs, 0, wxALIGN_LEFT|wxALL, 2);
264    m_surface = new wxCheckBox(this, svx_SURFACE, wmsg(/*Sur&amp;face Survey Legs*/403));
265    v3->Add(m_surface, 0, wxALIGN_LEFT|wxALL, 2);
266    m_stations = new wxCheckBox(this, svx_STATIONS, wmsg(/*Crosses*/261));
267    v3->Add(m_stations, 0, wxALIGN_LEFT|wxALL, 2);
268    m_names = new wxCheckBox(this, svx_NAMES, wmsg(/*Station Names*/260));
269    v3->Add(m_names, 0, wxALIGN_LEFT|wxALL, 2);
270    if (printing) {
271        m_borders = new wxCheckBox(this, svx_BORDERS, wmsg(/*Page Borders*/264));
272        v3->Add(m_borders, 0, wxALIGN_LEFT|wxALL, 2);
273//      m_blanks = new wxCheckBox(this, svx_BLANKS, wmsg(/*Blank Pages*/266));
274//      v3->Add(m_blanks, 0, wxALIGN_LEFT|wxALL, 2);
275        m_infoBox = new wxCheckBox(this, svx_INFOBOX, wmsg(/*Info Box*/265));
276        v3->Add(m_infoBox, 0, wxALIGN_LEFT|wxALL, 2);
277    }
278
279    h1->Add(v3, 0, wxALIGN_LEFT|wxALL, 5);
280
281    v1->Add(h1, 0, wxALIGN_LEFT|wxALL, 5);
282
283    wxButton * but;
284    but = new wxButton(this, wxID_CANCEL, wmsg(/*&Cancel*/402));
285    h2->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
286    if (printing) {
287#if !defined __WXMAC__ && !defined wxUSE_LIBGNOMEPRINT
288        // The Gnome print dialog provides its own preview.
289        but = new wxButton(this, svx_PREVIEW, wmsg(/*Pre&view*/401));
290        h2->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
291        but = new wxButton(this, svx_PRINT, wmsg(/*&Print*/400));
292#else
293        but = new wxButton(this, svx_PRINT, wmsg(/*&Print*/400) + wxT("..."));
294#endif
295    } else {
296        but = new wxButton(this, svx_EXPORT, wmsg(/*&Export...*/230));
297    }
298    but->SetDefault();
299    h2->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
300    v1->Add(h2, 0, wxALIGN_RIGHT|wxALL, 5);
301
302    SetAutoLayout(true);
303    SetSizer(v1);
304    v1->Fit(this);
305    v1->SetSizeHints(this);
306
307    LayoutToUI();
308    SomethingChanged();
309}
310
311void
312svxPrintDlg::OnPrint(wxCommandEvent&) {
313    SomethingChanged();
314    wxPageSetupDialogData * psdd = wxGetApp().GetPageSetupDialogData();
315    wxPrintDialogData pd(psdd->GetPrintData());
316    wxPrinter pr(&pd);
317    svxPrintout po(mainfrm, &m_layout, psdd, m_File);
318    if (pr.Print(this, &po, true)) {
319        // Close the print dialog if printing succeeded.
320        Destroy();
321    }
322}
323
324void
325svxPrintDlg::OnExport(wxCommandEvent&) {
326    UIToLayout();
327    wxString baseleaf;
328    wxFileName::SplitPath(m_File, NULL, NULL, &baseleaf, NULL, wxPATH_NATIVE);
329    wxFileDialog dlg(this, wxT("Export as:"), wxString(), baseleaf,
330                     wxT("DXF files|*.dxf|SVG files|*.svg|Sketch files|*.sk|EPS files|*.eps|Compass PLT for use with Carto|*.plt|HPGL for plotters|*.hpgl"),
331                     wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
332    if (dlg.ShowModal() == wxID_OK) {
333        if (!Export(dlg.GetPath(), m_layout.title, mainfrm,
334                    m_layout.rot, m_layout.tilt,
335                    m_layout.Labels, m_layout.Crosses,
336                    m_layout.Shots, m_layout.Surface)) {
337            wxGetApp().ReportError(wxString::Format(wxT("Couldn't write file `%s'"), m_File.c_str()));
338        }
339    }
340    Destroy();
341}
342
343void
344svxPrintDlg::OnPreview(wxCommandEvent&) {
345    SomethingChanged();
346    wxPageSetupDialogData * psdd = wxGetApp().GetPageSetupDialogData();
347    wxPrintDialogData pd(psdd->GetPrintData());
348    wxPrintPreview* pv;
349    pv = new wxPrintPreview(new svxPrintout(mainfrm, &m_layout, psdd, m_File),
350                            new svxPrintout(mainfrm, &m_layout, psdd, m_File),
351                            &pd);
352    wxPreviewFrame *frame = new wxPreviewFrame(pv, mainfrm, wmsg(/*Print Preview*/398));
353    frame->Initialize();
354
355    // Size preview frame so that all of the controlbar and canvas can be seen
356    // if possible.
357    int w, h;
358    // GetBestSize gives us the width needed to show the whole controlbar.
359    frame->GetBestSize(&w, &h);
360#ifdef __WXMAC__
361    // wxMac opens the preview window at minimum size by default.
362    // 360x480 is apparently enough to show A4 portrait.
363    if (h < 480 || w < 360) {
364        if (h < 480) h = 480;
365        if (w < 360) w = 360;
366    }
367#else
368    if (h < w) {
369        // On wxGTK at least, GetBestSize() returns much too small a height.
370        h = w * 6 / 5;
371    }
372#endif
373    // Ensure that we don't make the window bigger than the screen.
374    // Use wxGetClientDisplayRect() so we don't cover the MS Windows
375    // task bar either.
376    wxRect disp = wxGetClientDisplayRect();
377    if (w > disp.GetWidth()) w = disp.GetWidth();
378    if (h > disp.GetHeight()) h = disp.GetHeight();
379    // Centre the window within the "ClientDisplayRect".
380    int x = disp.GetLeft() + (disp.GetWidth() - w) / 2;
381    int y = disp.GetTop() + (disp.GetHeight() - h) / 2;
382    frame->SetSize(x, y, w, h);
383
384    frame->Show();
385}
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://www.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    if (IsPreview()) {
969        int dcx, dcy;
970        pdc->GetSize(&dcx, &dcy);
971        pdc->SetUserScale((double)dcx / xpPageWidth, (double)dcy / ypPageDepth);
972    }
973
974    layout * l = m_layout;
975    {
976        int pwidth, pdepth;
977        GetPageSizeMM(&pwidth, &pdepth);
978        l->scX = (double)xpPageWidth / pwidth;
979        l->scY = (double)ypPageDepth / pdepth;
980        font_scaling_x = l->scX * (25.4 / 72.0);
981        font_scaling_y = l->scY * (25.4 / 72.0);
982        MarginLeft = m_data->GetMarginTopLeft().x;
983        MarginTop = m_data->GetMarginTopLeft().y;
984        MarginBottom = m_data->GetMarginBottomRight().y;
985        MarginRight = m_data->GetMarginBottomRight().x;
986        xpPageWidth -= (int)(l->scX * (MarginLeft + MarginRight));
987        ypPageDepth -= (int)(l->scY * (10 + MarginBottom + MarginRight));
988        // xpPageWidth -= 1;
989        pdepth -= 10;
990        x_offset = (long)(l->scX * MarginLeft);
991        y_offset = (long)(l->scY * MarginTop);
992        l->PaperWidth = pwidth -= MarginLeft + MarginRight;
993        l->PaperDepth = pdepth -= MarginTop + MarginBottom;
994    }
995
996    double SIN = sin(rad(l->rot));
997    double COS = cos(rad(l->rot));
998    double SINT = sin(rad(l->tilt));
999    double COST = cos(rad(l->tilt));
1000
1001    NewPage(pageNum, l->pagesX, l->pagesY);
1002
1003    if (!l->Raw && pageNum == (l->pagesY - 1) * l->pagesX + 1) {
1004        SetFont(PR_FONT_DEFAULT);
1005        draw_info_box();
1006    }
1007
1008    const double Sc = 1000 / l->Scale;
1009
1010    if (l->Shots) {
1011        SetColour(PR_COLOUR_LEG);
1012        list<traverse>::const_iterator trav = mainfrm->traverses_begin();
1013        list<traverse>::const_iterator tend = mainfrm->traverses_end();
1014        for ( ; trav != tend; ++trav) {
1015            vector<PointInfo>::const_iterator pos = trav->begin();
1016            vector<PointInfo>::const_iterator end = trav->end();
1017            for ( ; pos != end; ++pos) {
1018                double x = pos->GetX();
1019                double y = pos->GetY();
1020                double z = pos->GetZ();
1021                double X = x * COS - y * SIN;
1022                double Y = (x * SIN + y * COS) * SINT + z * COST;
1023                long px = (long)((X * Sc + l->xOrg) * l->scX);
1024                long py = (long)((Y * Sc + l->yOrg) * l->scY);
1025                if (pos == trav->begin()) {
1026                    MoveTo(px, py);
1027                } else {
1028                    DrawTo(px, py);
1029                }
1030            }
1031        }
1032    }
1033
1034    if (l->Shots && (l->tilt == 0.0 || l->tilt == 90.0 || l->tilt == -90.0)) {
1035        list<vector<XSect> >::const_iterator trav = mainfrm->tubes_begin();
1036        list<vector<XSect> >::const_iterator tend = mainfrm->tubes_end();
1037        for ( ; trav != tend; ++trav) {
1038            if (l->tilt == 90.0 || l->tilt == -90.0) PlotLR(*trav);
1039            if (l->tilt == 0.0) PlotUD(*trav);
1040        }
1041    }
1042
1043    if (l->Surface) {
1044        SetColour(PR_COLOUR_SURFACE_LEG);
1045        list<traverse>::const_iterator trav = mainfrm->surface_traverses_begin();
1046        list<traverse>::const_iterator tend = mainfrm->surface_traverses_end();
1047        for ( ; trav != tend; ++trav) {
1048            vector<PointInfo>::const_iterator pos = trav->begin();
1049            vector<PointInfo>::const_iterator end = trav->end();
1050            for ( ; pos != end; ++pos) {
1051                double x = pos->GetX();
1052                double y = pos->GetY();
1053                double z = pos->GetZ();
1054                double X = x * COS - y * SIN;
1055                double Y = (x * SIN + y * COS) * SINT + z * COST;
1056                long px = (long)((X * Sc + l->xOrg) * l->scX);
1057                long py = (long)((Y * Sc + l->yOrg) * l->scY);
1058                if (pos == trav->begin()) {
1059                    MoveTo(px, py);
1060                } else {
1061                    DrawTo(px, py);
1062                }
1063            }
1064        }
1065    }
1066
1067    if (l->Labels || l->Crosses) {
1068        if (l->Labels) SetFont(PR_FONT_LABELS);
1069        list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
1070        while (label != mainfrm->GetLabelsEnd()) {
1071            double px = (*label)->GetX();
1072            double py = (*label)->GetY();
1073            double pz = (*label)->GetZ();
1074            if (l->Surface || (*label)->IsUnderground()) {
1075                double X = px * COS - py * SIN;
1076                double Y = (px * SIN + py * COS) * SINT + pz * COST;
1077                long xnew, ynew;
1078                xnew = (long)((X * Sc + l->xOrg) * l->scX);
1079                ynew = (long)((Y * Sc + l->yOrg) * l->scY);
1080                if (l->Crosses) {
1081                    SetColour(PR_COLOUR_CROSS);
1082                    DrawCross(xnew, ynew);
1083                }
1084                if (l->Labels) {
1085                    SetColour(PR_COLOUR_LABELS);
1086                    MoveTo(xnew, ynew);
1087                    WriteString((*label)->GetText());
1088                }
1089            }
1090            ++label;
1091        }
1092    }
1093
1094    if (!l->Raw) {
1095        char szTmp[256];
1096        SetColour(PR_COLOUR_TEXT);
1097        sprintf(szTmp,(const char*) l->footer.mb_str(), (const char*)l->title.mb_str(), pageNum, l->pagesX * l->pagesY,
1098                (const char*)l->datestamp.mb_str());
1099        ShowPage(szTmp);
1100    } else {
1101        ShowPage("");
1102    }
1103
1104    return true;
1105}
1106
1107void
1108svxPrintout::GetPageInfo(int *minPage, int *maxPage,
1109                         int *pageFrom, int *pageTo)
1110{
1111    *minPage = *pageFrom = 1;
1112    *maxPage = *pageTo = m_layout->pages;
1113}
1114
1115wxString
1116svxPrintout::GetTitle() {
1117    return m_title;
1118}
1119
1120bool
1121svxPrintout::HasPage(int pageNum) {
1122    return (pageNum <= m_layout->pages);
1123}
1124
1125void
1126svxPrintout::OnBeginPrinting() {
1127    FILE *fh_list[4];
1128
1129    FILE **pfh = fh_list;
1130    FILE *fh;
1131    const char *pth_cfg;
1132    char *print_ini;
1133
1134    /* ini files searched in this order:
1135     * ~/.survex/print.ini [unix only]
1136     * /etc/survex/print.ini [unix only]
1137     * <support file directory>/myprint.ini [not unix]
1138     * <support file directory>/print.ini [must exist]
1139     */
1140
1141#ifdef __UNIX__
1142    pth_cfg = getenv("HOME");
1143    if (pth_cfg) {
1144        fh = fopenWithPthAndExt(pth_cfg, ".survex/print."EXT_INI, NULL,
1145                "rb", NULL);
1146        if (fh) *pfh++ = fh;
1147    }
1148    pth_cfg = msg_cfgpth();
1149    fh = fopenWithPthAndExt(NULL, "/etc/survex/print."EXT_INI, NULL, "rb",
1150            NULL);
1151    if (fh) *pfh++ = fh;
1152#else
1153    pth_cfg = msg_cfgpth();
1154    print_ini = add_ext("myprint", EXT_INI);
1155    fh = fopenWithPthAndExt(pth_cfg, print_ini, NULL, "rb", NULL);
1156    if (fh) *pfh++ = fh;
1157#endif
1158    print_ini = add_ext("print", EXT_INI);
1159    fh = fopenWithPthAndExt(pth_cfg, print_ini, NULL, "rb", NULL);
1160    if (!fh) fatalerror(/*Couldn't open data file `%s'*/24, print_ini);
1161    *pfh++ = fh;
1162    *pfh = NULL;
1163    Init(pfh, false);
1164    for (pfh = fh_list; *pfh; pfh++) (void)fclose(*pfh);
1165    Pre();
1166    m_layout->footer = wmsg(/*Survey `%s'   Page %d (of %d)   Processed on %s*/167);
1167}
1168
1169void
1170svxPrintout::OnEndPrinting() {
1171    delete(font_labels);
1172    delete(font_default);
1173    delete(pen_frame);
1174    delete(pen_leg);
1175    delete(pen_surface_leg);
1176    delete(pen_cross);
1177}
1178
1179
1180// prcore -> wx.grafx (calls to move pens around and stuff - low level)
1181// this seems to have been done...
1182
1183
1184
1185static border clip;
1186
1187
1188int
1189svxPrintout::check_intersection(long x_p, long y_p)
1190{
1191#define U 1
1192#define D 2
1193#define L 4
1194#define R 8
1195   int mask_p = 0, mask_t = 0;
1196   if (x_p < 0)
1197      mask_p = L;
1198   else if (x_p > xpPageWidth)
1199      mask_p = R;
1200
1201   if (y_p < 0)
1202      mask_p |= D;
1203   else if (y_p > ypPageDepth)
1204      mask_p |= U;
1205
1206   if (x_t < 0)
1207      mask_t = L;
1208   else if (x_t > xpPageWidth)
1209      mask_t = R;
1210
1211   if (y_t < 0)
1212      mask_t |= D;
1213   else if (y_t > ypPageDepth)
1214      mask_t |= U;
1215
1216#if 0
1217   /* approximation to correct answer */
1218   return !(mask_t & mask_p);
1219#else
1220   /* One end of the line is on the page */
1221   if (!mask_t || !mask_p) return 1;
1222
1223   /* whole line is above, left, right, or below page */
1224   if (mask_t & mask_p) return 0;
1225
1226   if (mask_t == 0) mask_t = mask_p;
1227   if (mask_t & U) {
1228      double v = (double)(y_p - ypPageDepth) / (y_p - y_t);
1229      return v >= 0 && v <= 1;
1230   }
1231   if (mask_t & D) {
1232      double v = (double)y_p / (y_p - y_t);
1233      return v >= 0 && v <= 1;
1234   }
1235   if (mask_t & R) {
1236      double v = (double)(x_p - xpPageWidth) / (x_p - x_t);
1237      return v >= 0 && v <= 1;
1238   }
1239   SVX_ASSERT(mask_t & L);
1240   {
1241      double v = (double)x_p / (x_p - x_t);
1242      return v >= 0 && v <= 1;
1243   }
1244#endif
1245#undef U
1246#undef D
1247#undef L
1248#undef R
1249}
1250
1251void
1252svxPrintout::MoveTo(long x, long y)
1253{
1254    x_t = x_offset + x - clip.x_min;
1255    y_t = y_offset + clip.y_max - y;
1256}
1257
1258void
1259svxPrintout::DrawTo(long x, long y)
1260{
1261    long x_p = x_t, y_p = y_t;
1262    x_t = x_offset + x - clip.x_min;
1263    y_t = y_offset + clip.y_max - y;
1264    if (cur_pass != -1) {
1265        pdc->DrawLine(x_p, y_p, x_t, y_t);
1266    } else {
1267        if (check_intersection(x_p, y_p)) fBlankPage = fFalse;
1268    }
1269}
1270
1271#define POINTS_PER_INCH 72.0
1272#define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH)
1273#define PWX_CROSS_SIZE (int)(2 * m_layout->scX / POINTS_PER_MM)
1274
1275void
1276svxPrintout::DrawCross(long x, long y)
1277{
1278   if (cur_pass != -1) {
1279      MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1280      DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1281      MoveTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1282      DrawTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1283      MoveTo(x, y);
1284   } else {
1285      if ((x + PWX_CROSS_SIZE > clip.x_min &&
1286           x - PWX_CROSS_SIZE < clip.x_max) ||
1287          (y + PWX_CROSS_SIZE > clip.y_min &&
1288           y - PWX_CROSS_SIZE < clip.y_max)) {
1289         fBlankPage = fFalse;
1290      }
1291   }
1292}
1293
1294void
1295svxPrintout::SetFont(int fontcode)
1296{
1297    switch (fontcode) {
1298        case PR_FONT_DEFAULT:
1299            current_font = font_default;
1300            break;
1301        case PR_FONT_LABELS:
1302            current_font = font_labels;
1303            break;
1304        default:
1305            BUG("unknown font code");
1306    }
1307}
1308
1309void
1310svxPrintout::SetColour(int colourcode)
1311{
1312    switch (colourcode) {
1313        case PR_COLOUR_TEXT:
1314            pdc->SetTextForeground(colour_text);
1315            break;
1316        case PR_COLOUR_LABELS:
1317            pdc->SetTextForeground(colour_labels);
1318            pdc->SetBackgroundMode(wxTRANSPARENT);
1319            break;
1320        case PR_COLOUR_FRAME:
1321            pdc->SetPen(*pen_frame);
1322            break;
1323        case PR_COLOUR_LEG:
1324            pdc->SetPen(*pen_leg);
1325            break;
1326        case PR_COLOUR_CROSS:
1327            pdc->SetPen(*pen_cross);
1328            break;
1329        case PR_COLOUR_SURFACE_LEG:
1330            pdc->SetPen(*pen_surface_leg);
1331            break;
1332        default:
1333            BUG("unknown colour code");
1334    }
1335}
1336
1337void
1338svxPrintout::WriteString(const wxString & s)
1339{
1340    double xsc, ysc;
1341    pdc->GetUserScale(&xsc, &ysc);
1342    pdc->SetUserScale(xsc * font_scaling_x, ysc * font_scaling_y);
1343    pdc->SetFont(*current_font);
1344    int w, h;
1345    if (cur_pass != -1) {
1346        pdc->GetTextExtent(wxT("My"), &w, &h);
1347        pdc->DrawText(s,
1348                      long(x_t / font_scaling_x),
1349                      long(y_t / font_scaling_y) - h);
1350    } else {
1351        pdc->GetTextExtent(s, &w, &h);
1352        if ((y_t + h > 0 && y_t - h < clip.y_max - clip.y_min) ||
1353            (x_t < clip.x_max - clip.x_min && x_t + w > 0)) {
1354            fBlankPage = fFalse;
1355        }
1356    }
1357    pdc->SetUserScale(xsc, ysc);
1358}
1359
1360void
1361svxPrintout::DrawEllipse(long x, long y, long r, long R)
1362{
1363    /* Don't need to check in first-pass - circle is only used in title box */
1364    if (cur_pass != -1) {
1365        x_t = x_offset + x - clip.x_min;
1366        y_t = y_offset + clip.y_max - y;
1367        pdc->SetBrush(*wxTRANSPARENT_BRUSH);
1368        pdc->DrawEllipse(x_t - r, y_t - R, 2 * r, 2 * R);
1369    }
1370}
1371
1372void
1373svxPrintout::SolidRectangle(long x, long y, long w, long h)
1374{
1375    long X = x_offset + x - clip.x_min;
1376    long Y = y_offset + clip.y_max - y;
1377    pdc->SetBrush(*wxBLACK_BRUSH);
1378    pdc->DrawRectangle(X, Y - h, w, h);
1379}
1380
1381int
1382svxPrintout::Charset(void)
1383{
1384   return CHARSET_ISO_8859_1;
1385}
1386
1387int
1388svxPrintout::Pre()
1389{
1390    font_labels = new wxFont(fontsize_labels, wxDEFAULT, wxNORMAL, wxNORMAL,
1391                             false, wxString(fontname_labels, wxConvUTF8),
1392                             wxFONTENCODING_ISO8859_1);
1393    font_default = new wxFont(fontsize, wxDEFAULT, wxNORMAL, wxNORMAL,
1394                              false, wxString(fontname, wxConvUTF8),
1395                              wxFONTENCODING_ISO8859_1);
1396    current_font = font_default;
1397    pen_leg = new wxPen(colour_leg,0,wxSOLID);
1398    pen_surface_leg = new wxPen(colour_surface_leg,0,wxSOLID);
1399    pen_cross = new wxPen(colour_cross,0,wxSOLID);
1400    pen_frame = new wxPen(colour_frame,0,wxSOLID);
1401    return 1; /* only need 1 pass */
1402}
1403
1404void
1405svxPrintout::NewPage(int pg, int pagesX, int pagesY)
1406{
1407    int x, y;
1408    x = (pg - 1) % pagesX;
1409    y = pagesY - 1 - ((pg - 1) / pagesX);
1410
1411    clip.x_min = (long)x * xpPageWidth;
1412    clip.y_min = (long)y * ypPageDepth;
1413    clip.x_max = clip.x_min + xpPageWidth; /* dm/pcl/ps had -1; */
1414    clip.y_max = clip.y_min + ypPageDepth; /* dm/pcl/ps had -1; */
1415
1416    //we have to write the footer here. PostScript is being weird. Really weird.
1417    pdc->SetFont(*font_labels);
1418    MoveTo((long)(6 * m_layout->scX) + clip.x_min,
1419           clip.y_min - (long)(7 * m_layout->scY));
1420    wxString szFooter;
1421    szFooter.Printf(m_layout->footer,
1422                    m_layout->title.c_str(),
1423                    pg,
1424                    m_layout->pagesX * m_layout->pagesY,
1425                    m_layout->datestamp.c_str());
1426    WriteString(szFooter);
1427    pdc->DestroyClippingRegion();
1428    pdc->SetClippingRegion(x_offset, y_offset,xpPageWidth+1, ypPageDepth+1);
1429    drawticks(clip, (int)(9 * m_layout->scX / POINTS_PER_MM), x, y);
1430}
1431
1432void
1433svxPrintout::ShowPage(const char *szPageDetails)
1434{
1435}
1436
1437void
1438svxPrintout::PlotLR(const vector<XSect> & centreline)
1439{
1440    assert(centreline.size() > 1);
1441    XSect prev_pt_v;
1442    Vector3 last_right(1.0, 0.0, 0.0);
1443
1444    const double Sc = 1000 / m_layout->Scale;
1445    const double SIN = sin(rad(m_layout->rot));
1446    const double COS = cos(rad(m_layout->rot));
1447
1448    vector<XSect>::const_iterator i = centreline.begin();
1449    vector<XSect>::size_type segment = 0;
1450    while (i != centreline.end()) {
1451        // get the coordinates of this vertex
1452        const XSect & pt_v = *i++;
1453
1454        Vector3 right;
1455
1456        const Vector3 up_v(0.0, 0.0, 1.0);
1457
1458        if (segment == 0) {
1459            assert(i != centreline.end());
1460            // first segment
1461
1462            // get the coordinates of the next vertex
1463            const XSect & next_pt_v = *i;
1464
1465            // calculate vector from this pt to the next one
1466            Vector3 leg_v = next_pt_v - pt_v;
1467
1468            // obtain a vector in the LRUD plane
1469            right = leg_v * up_v;
1470            if (right.magnitude() == 0) {
1471                right = last_right;
1472            } else {
1473                last_right = right;
1474            }
1475        } else if (segment + 1 == centreline.size()) {
1476            // last segment
1477
1478            // Calculate vector from the previous pt to this one.
1479            Vector3 leg_v = pt_v - prev_pt_v;
1480
1481            // Obtain a horizontal vector in the LRUD plane.
1482            right = leg_v * up_v;
1483            if (right.magnitude() == 0) {
1484                right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
1485            } else {
1486                last_right = right;
1487            }
1488        } else {
1489            assert(i != centreline.end());
1490            // Intermediate segment.
1491
1492            // Get the coordinates of the next vertex.
1493            const XSect & next_pt_v = *i;
1494
1495            // Calculate vectors from this vertex to the
1496            // next vertex, and from the previous vertex to
1497            // this one.
1498            Vector3 leg1_v = pt_v - prev_pt_v;
1499            Vector3 leg2_v = next_pt_v - pt_v;
1500
1501            // Obtain horizontal vectors perpendicular to
1502            // both legs, then normalise and average to get
1503            // a horizontal bisector.
1504            Vector3 r1 = leg1_v * up_v;
1505            Vector3 r2 = leg2_v * up_v;
1506            r1.normalise();
1507            r2.normalise();
1508            right = r1 + r2;
1509            if (right.magnitude() == 0) {
1510                // This is the "mid-pitch" case...
1511                right = last_right;
1512            }
1513            last_right = right;
1514        }
1515
1516        // Scale to unit vectors in the LRUD plane.
1517        right.normalise();
1518
1519        Double l = pt_v.GetL();
1520        Double r = pt_v.GetR();
1521
1522        if (l >= 0) {
1523            Vector3 p = pt_v - right * l;
1524            double X = p.GetX() * COS - p.GetY() * SIN;
1525            double Y = (p.GetX() * SIN + p.GetY() * COS);
1526            long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
1527            long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
1528            MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1529            DrawTo(x, y);
1530            DrawTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1531        }
1532        if (r >= 0) {
1533            Vector3 p = pt_v + right * r;
1534            double X = p.GetX() * COS - p.GetY() * SIN;
1535            double Y = (p.GetX() * SIN + p.GetY() * COS);
1536            long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
1537            long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
1538            MoveTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1539            DrawTo(x, y);
1540            DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1541        }
1542
1543        prev_pt_v = pt_v;
1544
1545        ++segment;
1546    }
1547}
1548
1549void
1550svxPrintout::PlotUD(const vector<XSect> & centreline)
1551{
1552    assert(centreline.size() > 1);
1553    const double Sc = 1000 / m_layout->Scale;
1554
1555    vector<XSect>::const_iterator i = centreline.begin();
1556    while (i != centreline.end()) {
1557        // get the coordinates of this vertex
1558        const XSect & pt_v = *i++;
1559
1560        Double u = pt_v.GetU();
1561        Double d = pt_v.GetD();
1562
1563        if (u >= 0 || d >= 0) {
1564            Vector3 p = pt_v;
1565            double SIN = sin(rad(m_layout->rot));
1566            double COS = cos(rad(m_layout->rot));
1567            double X = p.GetX() * COS - p.GetY() * SIN;
1568            double Y = p.GetZ();
1569            long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
1570            if (u >= 0) {
1571                long y = (long)(((Y + u) * Sc + m_layout->yOrg) * m_layout->scY);
1572                MoveTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1573                DrawTo(x, y);
1574                DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1575            }
1576            if (d >= 0) {
1577                long y = (long)(((Y - d) * Sc + m_layout->yOrg) * m_layout->scY);
1578                MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1579                DrawTo(x, y);
1580                DrawTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1581            }
1582        }
1583    }
1584}
1585
1586static wxColour
1587to_rgb(const char *var, char *val)
1588{
1589   unsigned long rgb;
1590   if (!val) return *wxBLACK;
1591   rgb = as_colour(var, val);
1592   return wxColour((rgb & 0xff0000) >> 16, (rgb & 0xff00) >> 8, rgb & 0xff);
1593}
1594
1595/* Initialise printer routines */
1596char *
1597svxPrintout::Init(FILE **fh_list, bool fCalibrate)
1598{
1599   static const char *vars[] = {
1600      "font_size_labels",
1601      "colour_text",
1602      "colour_labels",
1603      "colour_frame",
1604      "colour_legs",
1605      "colour_crosses",
1606      "colour_surface_legs",
1607      NULL
1608   };
1609   char **vals;
1610
1611   fCalibrate = fCalibrate; /* suppress unused argument warning */
1612
1613   vals = ini_read(fh_list, "aven", vars);
1614   fontsize_labels = 10;
1615   if (vals[0]) fontsize_labels = as_int(vars[0], vals[0], 1, INT_MAX);
1616   fontsize = 10;
1617
1618   colour_text = colour_labels = colour_frame = colour_leg = colour_cross = colour_surface_leg = *wxBLACK;
1619   if (vals[1]) colour_text = to_rgb(vars[1], vals[1]);
1620   if (vals[2]) colour_labels = to_rgb(vars[2], vals[2]);
1621   if (vals[3]) colour_frame = to_rgb(vars[3], vals[3]);
1622   if (vals[4]) colour_leg = to_rgb(vars[4], vals[4]);
1623   if (vals[5]) colour_cross = to_rgb(vars[5], vals[5]);
1624   if (vals[6]) colour_surface_leg = to_rgb(vars[6], vals[6]);
1625   m_layout->scX = 1;
1626   m_layout->scY = 1;
1627   return NULL;
1628}
Note: See TracBrowser for help on using the repository browser.