source: git/src/printwx.cc @ 5f97258

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

src/printwx.cc: Use wxString::FromAscii?() for the units on the scale
bar which we know to be ASCII. No need to use msgPerm() for a
message we assign to a wxString.

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