Support exporting walls and cross-sections in KML
[survex.git] / src / printing.cc
blobca9df4c74d2d7a5e87cc8e20fd0e528ae32b0dab
1 /* printing.cc */
2 /* Aven printing code */
3 /* Copyright (C) 1993-2003,2004,2005,2006,2010,2011,2012,2013,2014,2015,2016 Olly Betts
4 * Copyright (C) 2001,2004 Philip Underwood
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.
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.
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
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <wx/confbase.h>
26 #include <wx/filename.h>
27 #include <wx/print.h>
28 #include <wx/printdlg.h>
29 #include <wx/spinctrl.h>
30 #include <wx/radiobox.h>
31 #include <wx/statbox.h>
32 #include <wx/valgen.h>
34 #include <vector>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <math.h>
39 #include <string.h>
40 #include <ctype.h>
41 #include <float.h>
42 #include <limits.h>
44 #include "export.h"
45 #include "filelist.h"
46 #include "filename.h"
47 #include "message.h"
48 #include "useful.h"
50 #include "aven.h"
51 #include "avenprcore.h"
52 #include "mainfrm.h"
53 #include "printing.h"
55 using namespace std;
57 // How many decimal points to show on angles:
58 #define ANGLE_DP 1
60 #if ANGLE_DP == 0
61 # define ANGLE_FMT wxT("%03.f")
62 # define ANGLE2_FMT wxT("%.f")
63 #elif ANGLE_DP == 1
64 # define ANGLE_FMT wxT("%05.1f")
65 # define ANGLE2_FMT wxT("%.1f")
66 #elif ANGLE_DP == 2
67 # define ANGLE_FMT wxT("%06.2f")
68 # define ANGLE2_FMT wxT("%.2f")
69 #else
70 # error Need to add ANGLE_FMT and ANGLE2_FMT for the currently set ANGLE_DP
71 #endif
73 static wxString
74 format_angle(const wxChar * fmt, double angle)
76 wxString s;
77 s.Printf(fmt, angle);
78 size_t dot = s.find('.');
79 size_t i = s.size();
80 while (i > dot) {
81 --i;
82 if (s[i] != '0') {
83 if (i != dot) ++i;
84 s.resize(i);
85 break;
88 s += wmsg(/*°*/344);
89 return s;
92 enum {
93 svx_EXPORT = 1200,
94 svx_FORMAT,
95 svx_SCALE,
96 svx_BEARING,
97 svx_TILT,
98 svx_LEGS,
99 svx_STATIONS,
100 svx_NAMES,
101 svx_XSECT,
102 svx_WALLS,
103 svx_PASSAGES,
104 svx_BORDERS,
105 svx_BLANKS,
106 svx_LEGEND,
107 svx_SURFACE,
108 svx_SPLAYS,
109 svx_PLAN,
110 svx_ELEV,
111 svx_ENTS,
112 svx_FIXES,
113 svx_EXPORTS,
114 svx_PROJ_LABEL,
115 svx_PROJ,
116 svx_GRID,
117 svx_TEXT_HEIGHT,
118 svx_MARKER_SIZE,
119 svx_CENTRED,
120 svx_FULLCOORDS
123 class BitValidator : public wxValidator {
124 // Disallow assignment.
125 BitValidator & operator=(const BitValidator&);
127 protected:
128 int * val;
130 int mask;
132 public:
133 BitValidator(int * val_, int mask_)
134 : val(val_), mask(mask_) { }
136 BitValidator(const BitValidator &o) : wxValidator() {
137 Copy(o);
140 ~BitValidator() { }
142 wxObject *Clone() const { return new BitValidator(val, mask); }
144 bool Copy(const BitValidator& o) {
145 wxValidator::Copy(o);
146 val = o.val;
147 mask = o.mask;
148 return true;
151 bool Validate(wxWindow *) { return true; }
153 bool TransferToWindow() {
154 if (!m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)))
155 return false;
156 ((wxCheckBox*)m_validatorWindow)->SetValue(*val & mask);
157 return true;
160 bool TransferFromWindow() {
161 if (!m_validatorWindow->IsKindOf(CLASSINFO(wxCheckBox)))
162 return false;
163 if (((wxCheckBox*)m_validatorWindow)->IsChecked())
164 *val |= mask;
165 else
166 *val &= ~mask;
167 return true;
171 class svxPrintout : public wxPrintout {
172 MainFrm *mainfrm;
173 layout *m_layout;
174 wxPageSetupDialogData* m_data;
175 wxDC* pdc;
176 wxFont *font_labels, *font_default;
177 // Currently unused, but "skip blank pages" would use it.
178 bool scan_for_blank_pages;
180 wxPen *pen_frame, *pen_cross, *pen_leg, *pen_surface_leg, *pen_splay;
181 wxColour colour_text, colour_labels;
183 long x_t, y_t;
184 double font_scaling_x, font_scaling_y;
186 struct {
187 long x_min, y_min, x_max, y_max;
188 } clip;
190 bool fBlankPage;
192 int check_intersection(long x_p, long y_p);
193 void draw_info_box();
194 void draw_scale_bar(double x, double y, double MaxLength);
195 int next_page(int *pstate, char **q, int pageLim);
196 void drawticks(int tsize, int x, int y);
198 void MOVEMM(double X, double Y) {
199 MoveTo((long)(X * m_layout->scX), (long)(Y * m_layout->scY));
201 void DRAWMM(double X, double Y) {
202 DrawTo((long)(X * m_layout->scX), (long)(Y * m_layout->scY));
204 void MoveTo(long x, long y);
205 void DrawTo(long x, long y);
206 void DrawCross(long x, long y);
207 void SetFont(wxFont * font) {
208 pdc->SetFont(*font);
210 void WriteString(const wxString & s);
211 void DrawEllipse(long x, long y, long r, long R);
212 void SolidRectangle(long x, long y, long w, long h);
213 void NewPage(int pg, int pagesX, int pagesY);
214 void PlotLR(const vector<XSect> & centreline);
215 void PlotUD(const vector<XSect> & centreline);
216 public:
217 svxPrintout(MainFrm *mainfrm, layout *l, wxPageSetupDialogData *data, const wxString & title);
218 bool OnPrintPage(int pageNum);
219 void GetPageInfo(int *minPage, int *maxPage,
220 int *pageFrom, int *pageTo);
221 bool HasPage(int pageNum);
222 void OnBeginPrinting();
223 void OnEndPrinting();
226 BEGIN_EVENT_TABLE(svxPrintDlg, wxDialog)
227 EVT_CHOICE(svx_FORMAT, svxPrintDlg::OnChange)
228 EVT_TEXT(svx_SCALE, svxPrintDlg::OnChange)
229 EVT_COMBOBOX(svx_SCALE, svxPrintDlg::OnChange)
230 EVT_SPINCTRLDOUBLE(svx_BEARING, svxPrintDlg::OnChangeSpin)
231 EVT_SPINCTRLDOUBLE(svx_TILT, svxPrintDlg::OnChangeSpin)
232 EVT_BUTTON(wxID_PRINT, svxPrintDlg::OnPrint)
233 EVT_BUTTON(svx_EXPORT, svxPrintDlg::OnExport)
234 EVT_BUTTON(wxID_CANCEL, svxPrintDlg::OnCancel)
235 #ifdef AVEN_PRINT_PREVIEW
236 EVT_BUTTON(wxID_PREVIEW, svxPrintDlg::OnPreview)
237 #endif
238 EVT_BUTTON(svx_PLAN, svxPrintDlg::OnPlan)
239 EVT_BUTTON(svx_ELEV, svxPrintDlg::OnElevation)
240 EVT_UPDATE_UI(svx_PLAN, svxPrintDlg::OnPlanUpdate)
241 EVT_UPDATE_UI(svx_ELEV, svxPrintDlg::OnElevationUpdate)
242 EVT_CHECKBOX(svx_LEGS, svxPrintDlg::OnChange)
243 EVT_CHECKBOX(svx_STATIONS, svxPrintDlg::OnChange)
244 EVT_CHECKBOX(svx_NAMES, svxPrintDlg::OnChange)
245 EVT_CHECKBOX(svx_SURFACE, svxPrintDlg::OnChange)
246 EVT_CHECKBOX(svx_SPLAYS, svxPrintDlg::OnChange)
247 EVT_CHECKBOX(svx_ENTS, svxPrintDlg::OnChange)
248 EVT_CHECKBOX(svx_FIXES, svxPrintDlg::OnChange)
249 EVT_CHECKBOX(svx_EXPORTS, svxPrintDlg::OnChange)
250 END_EVENT_TABLE()
252 static wxString scales[] = {
253 wxT(""),
254 wxT("25"),
255 wxT("50"),
256 wxT("100"),
257 wxT("250"),
258 wxT("500"),
259 wxT("1000"),
260 wxT("2500"),
261 wxT("5000"),
262 wxT("10000"),
263 wxT("25000"),
264 wxT("50000"),
265 wxT("100000")
268 static wxString formats[] = {
269 wxT("DXF"),
270 wxT("EPS"),
271 wxT("GPX"),
272 wxT("HPGL"),
273 wxT("JSON"),
274 wxT("KML"),
275 wxT("Plot"),
276 wxT("Skencil"),
277 wxT("Survex pos"),
278 wxT("SVG")
281 #if 0
282 static wxString projs[] = {
283 /* CUCC Austria: */
284 wxT("+proj=tmerc +lat_0=0 +lon_0=13d20 +k=1 +x_0=0 +y_0=-5200000 +ellps=bessel +towgs84=577.326,90.129,463.919,5.137,1.474,5.297,2.4232"),
285 /* British grid SD (Yorkshire): */
286 wxT("+proj=tmerc +lat_0=49d +lon_0=-2d +k=0.999601 +x_0=100000 +y_0=-500000 +ellps=airy +towgs84=375,-111,431,0,0,0,0"),
287 /* British full grid reference: */
288 wxT("+proj=tmerc +lat_0=49d +lon_0=-2d +k=0.999601 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=375,-111,431,0,0,0,0")
290 #endif
292 static const unsigned format_info[] = {
293 LABELS|LEGS|SURF|SPLAYS|STNS|PASG|XSECT|WALLS|MARKER_SIZE|TEXT_HEIGHT|GRID|FULL_COORDS,
294 LABELS|LEGS|SURF|SPLAYS|STNS|PASG|XSECT|WALLS,
295 LABELS|LEGS|SURF|SPLAYS|ENTS|FIXES|EXPORTS|PROJ|EXPORT_3D,
296 LABELS|LEGS|SURF|SPLAYS|STNS|CENTRED,
297 LEGS|SPLAYS|CENTRED|EXPORT_3D,
298 LABELS|LEGS|SPLAYS|PASG|XSECT|WALLS|ENTS|FIXES|EXPORTS|PROJ|EXPORT_3D,
299 LABELS|LEGS|SURF|SPLAYS,
300 LABELS|LEGS|SURF|SPLAYS|STNS|MARKER_SIZE|GRID|SCALE,
301 LABELS|ENTS|FIXES|EXPORTS|EXPORT_3D,
302 LABELS|LEGS|SURF|SPLAYS|STNS|PASG|XSECT|WALLS|MARKER_SIZE|TEXT_HEIGHT|SCALE
305 static const char * extension[] = {
306 ".dxf",
307 ".eps",
308 ".gpx",
309 ".hpgl",
310 ".json",
311 ".kml",
312 ".plt",
313 ".sk",
314 ".pos",
315 ".svg"
318 static const int msg_filetype[] = {
319 /*DXF files*/411,
320 /*EPS files*/412,
321 /*GPX files*/413,
322 /* TRANSLATORS: Here "plotter" refers to a machine which draws a printout
323 * on a (usually large) sheet of paper using a pen mounted in a motorised
324 * mechanism. */
325 /*HPGL for plotters*/414,
326 /*JSON files*/445,
327 /*KML files*/444,
328 /* TRANSLATORS: "Compass" and "Carto" are the names of software packages,
329 * so should not be translated:
330 * http://www.fountainware.com/compass/
331 * http://www.psc-cavers.org/carto/ */
332 /*Compass PLT for use with Carto*/415,
333 /* TRANSLATORS: "Skencil" is the name of a software package, so should not be
334 * translated: http://www.skencil.org/ */
335 /*Skencil files*/416,
336 /* TRANSLATORS: Survex is the name of the software, and "pos" refers to a
337 * file extension, so neither should be translated. */
338 /*Survex pos files*/166,
339 /*SVG files*/417
342 // We discriminate as "One Page" isn't valid for exporting.
343 static wxString default_scale_print;
344 static wxString default_scale_export;
346 svxPrintDlg::svxPrintDlg(MainFrm* mainfrm_, const wxString & filename,
347 const wxString & title, const wxString & cs_proj,
348 const wxString & datestamp, time_t datestamp_numeric,
349 double angle, double tilt_angle,
350 bool labels, bool crosses, bool legs, bool surf,
351 bool splays, bool tubes, bool ents, bool fixes,
352 bool exports, bool printing, bool close_after_)
353 : wxDialog(mainfrm_, -1, wxString(printing ?
354 /* TRANSLATORS: Title of the print
355 * dialog */
356 wmsg(/*Print*/399) :
357 /* TRANSLATORS: Title of the export
358 * dialog */
359 wmsg(/*Export*/383))),
360 m_layout(printing ? wxGetApp().GetPageSetupDialogData() : NULL),
361 m_File(filename), mainfrm(mainfrm_), close_after(close_after_)
363 m_scale = NULL;
364 m_printSize = NULL;
365 m_bearing = NULL;
366 m_tilt = NULL;
367 m_format = NULL;
368 int show_mask = 0;
369 if (labels)
370 show_mask |= LABELS;
371 if (crosses)
372 show_mask |= STNS;
373 if (legs)
374 show_mask |= LEGS;
375 if (surf)
376 show_mask |= SURF;
377 if (splays)
378 show_mask |= SPLAYS;
379 if (tubes)
380 show_mask |= XSECT|WALLS|PASG;
381 if (ents)
382 show_mask |= ENTS;
383 if (fixes)
384 show_mask |= FIXES;
385 if (exports)
386 show_mask |= EXPORTS;
387 m_layout.show_mask = show_mask;
388 m_layout.datestamp = datestamp;
389 m_layout.datestamp_numeric = datestamp_numeric;
390 m_layout.rot = angle;
391 m_layout.title = title;
392 m_layout.cs_proj = cs_proj;
393 if (mainfrm->IsExtendedElevation()) {
394 m_layout.view = layout::EXTELEV;
395 if (m_layout.rot != 0.0 && m_layout.rot != 180.0) m_layout.rot = 0;
396 m_layout.tilt = 0;
397 } else {
398 m_layout.tilt = tilt_angle;
399 if (m_layout.tilt == -90.0) {
400 m_layout.view = layout::PLAN;
401 } else if (m_layout.tilt == 0.0) {
402 m_layout.view = layout::ELEV;
403 } else {
404 m_layout.view = layout::TILT;
408 /* setup our print dialog*/
409 wxBoxSizer* v1 = new wxBoxSizer(wxVERTICAL);
410 wxBoxSizer* h1 = new wxBoxSizer(wxHORIZONTAL); // holds controls
411 /* TRANSLATORS: Used as a label for the surrounding box for the "Bearing"
412 * and "Tilt angle" fields, and the "Plan view" and "Elevation" buttons in
413 * the "what to print/export" dialog. */
414 m_viewbox = new wxStaticBoxSizer(new wxStaticBox(this, -1, wmsg(/*View*/283)), wxVERTICAL);
415 /* TRANSLATORS: Used as a label for the surrounding box for the "survey
416 * legs" "stations" "names" etc checkboxes in the "what to print" dialog.
417 * "Elements" isn’t a good name for this but nothing better has yet come to
418 * mind! */
419 wxBoxSizer* v3 = new wxStaticBoxSizer(new wxStaticBox(this, -1, wmsg(/*Elements*/256)), wxVERTICAL);
420 wxBoxSizer* h2 = new wxBoxSizer(wxHORIZONTAL);
421 wxBoxSizer* h3 = new wxBoxSizer(wxHORIZONTAL); // holds buttons
423 if (!printing) {
424 wxStaticText* label;
425 label = new wxStaticText(this, -1, wxString(wmsg(/*Export format*/410)));
426 const size_t n_formats = sizeof(formats) / sizeof(formats[0]);
427 m_format = new wxChoice(this, svx_FORMAT,
428 wxDefaultPosition, wxDefaultSize,
429 n_formats, formats);
430 unsigned current_format = 0;
431 wxConfigBase * cfg = wxConfigBase::Get();
432 wxString s;
433 if (cfg->Read(wxT("export_format"), &s, wxString())) {
434 for (unsigned i = 0; i != n_formats; ++i) {
435 if (s == formats[i]) {
436 current_format = i;
437 break;
441 m_format->SetSelection(current_format);
442 wxBoxSizer* formatbox = new wxBoxSizer(wxHORIZONTAL);
443 formatbox->Add(label, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
444 formatbox->Add(m_format, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
446 v1->Add(formatbox, 0, wxALIGN_LEFT|wxALL, 0);
449 wxStaticText* label;
450 label = new wxStaticText(this, -1, wxString(wmsg(/*Scale*/154)) + wxT(" 1:"));
451 if (printing && scales[0].empty()) {
452 /* TRANSLATORS: used in the scale drop down selector in the print
453 * dialog the implicit meaning is "choose a suitable scale to fit
454 * the plot on a single page", but we need something shorter */
455 scales[0].assign(wmsg(/*One page*/258));
457 wxString default_scale;
458 if (printing) {
459 default_scale = default_scale_print;
460 if (default_scale.empty()) default_scale = scales[0];
461 } else {
462 default_scale = default_scale_export;
463 if (default_scale.empty()) default_scale = wxT("1000");
465 const wxString* scale_list = scales;
466 size_t n_scales = sizeof(scales) / sizeof(scales[0]);
467 if (!printing) {
468 ++scale_list;
469 --n_scales;
471 m_scale = new wxComboBox(this, svx_SCALE, default_scale, wxDefaultPosition,
472 wxDefaultSize, n_scales, scale_list);
473 m_scalebox = new wxBoxSizer(wxHORIZONTAL);
474 m_scalebox->Add(label, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
475 m_scalebox->Add(m_scale, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
477 m_viewbox->Add(m_scalebox, 0, wxALIGN_LEFT|wxALL, 0);
479 if (printing) {
480 // Make the dummy string wider than any sane value and use that to
481 // fix the width of the control so the sizers allow space for bigger
482 // page layouts.
483 m_printSize = new wxStaticText(this, -1, wxString::Format(wmsg(/*%d pages (%dx%d)*/257), 9604, 98, 98));
484 m_viewbox->Add(m_printSize, 0, wxALIGN_LEFT|wxALL, 5);
487 /* FIXME:
488 * svx_GRID, // double - spacing, default: 100m
489 * svx_TEXT_HEIGHT, // default 0.6
490 * svx_MARKER_SIZE // default 0.8
493 if (m_layout.view != layout::EXTELEV) {
494 wxFlexGridSizer* anglebox = new wxFlexGridSizer(2);
495 wxStaticText * brg_label, * tilt_label;
496 brg_label = new wxStaticText(this, -1, wmsg(/*Bearing*/259));
497 anglebox->Add(brg_label, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxALL, 5);
498 // wSP_WRAP means that you can scroll past 360 to 0, and vice versa.
499 m_bearing = new wxSpinCtrlDouble(this, svx_BEARING, wxEmptyString,
500 wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS|wxSP_WRAP);
501 m_bearing->SetRange(0.0, 360.0);
502 m_bearing->SetDigits(ANGLE_DP);
503 anglebox->Add(m_bearing, 0, wxALIGN_CENTER|wxALL, 5);
504 /* TRANSLATORS: Used in the print dialog: */
505 tilt_label = new wxStaticText(this, -1, wmsg(/*Tilt angle*/263));
506 anglebox->Add(tilt_label, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxALL, 5);
507 m_tilt = new wxSpinCtrlDouble(this, svx_TILT);
508 m_tilt->SetRange(-90.0, 90.0);
509 m_tilt->SetDigits(ANGLE_DP);
510 anglebox->Add(m_tilt, 0, wxALIGN_CENTER|wxALL, 5);
512 m_viewbox->Add(anglebox, 0, wxALIGN_LEFT|wxALL, 0);
514 wxBoxSizer * planelevsizer = new wxBoxSizer(wxHORIZONTAL);
515 planelevsizer->Add(new wxButton(this, svx_PLAN, wmsg(/*P&lan view*/117)),
516 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
517 planelevsizer->Add(new wxButton(this, svx_ELEV, wmsg(/*&Elevation*/285)),
518 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
520 m_viewbox->Add(planelevsizer, 0, wxALIGN_LEFT|wxALL, 5);
523 /* TRANSLATORS: Here a "survey leg" is a set of measurements between two
524 * "survey stations". */
525 v3->Add(new wxCheckBox(this, svx_LEGS, wmsg(/*Underground Survey Legs*/262),
526 wxDefaultPosition, wxDefaultSize, 0,
527 BitValidator(&m_layout.show_mask, LEGS)),
528 0, wxALIGN_LEFT|wxALL, 2);
529 /* TRANSLATORS: Here a "survey leg" is a set of measurements between two
530 * "survey stations". */
531 v3->Add(new wxCheckBox(this, svx_SURFACE, wmsg(/*Sur&face Survey Legs*/403),
532 wxDefaultPosition, wxDefaultSize, 0,
533 BitValidator(&m_layout.show_mask, SURF)),
534 0, wxALIGN_LEFT|wxALL, 2);
535 v3->Add(new wxCheckBox(this, svx_SPLAYS, wmsg(/*Spla&y Legs*/406),
536 wxDefaultPosition, wxDefaultSize, 0,
537 BitValidator(&m_layout.show_mask, SPLAYS)),
538 0, wxALIGN_LEFT|wxALL, 2);
539 v3->Add(new wxCheckBox(this, svx_STATIONS, wmsg(/*Crosses*/261),
540 wxDefaultPosition, wxDefaultSize, 0,
541 BitValidator(&m_layout.show_mask, STNS)),
542 0, wxALIGN_LEFT|wxALL, 2);
543 v3->Add(new wxCheckBox(this, svx_NAMES, wmsg(/*Station Names*/260),
544 wxDefaultPosition, wxDefaultSize, 0,
545 BitValidator(&m_layout.show_mask, LABELS)),
546 0, wxALIGN_LEFT|wxALL, 2);
547 v3->Add(new wxCheckBox(this, svx_ENTS, wmsg(/*Entrances*/418),
548 wxDefaultPosition, wxDefaultSize, 0,
549 BitValidator(&m_layout.show_mask, ENTS)),
550 0, wxALIGN_LEFT|wxALL, 2);
551 v3->Add(new wxCheckBox(this, svx_FIXES, wmsg(/*Fixed Points*/419),
552 wxDefaultPosition, wxDefaultSize, 0,
553 BitValidator(&m_layout.show_mask, FIXES)),
554 0, wxALIGN_LEFT|wxALL, 2);
555 v3->Add(new wxCheckBox(this, svx_EXPORTS, wmsg(/*Exported Stations*/420),
556 wxDefaultPosition, wxDefaultSize, 0,
557 BitValidator(&m_layout.show_mask, EXPORTS)),
558 0, wxALIGN_LEFT|wxALL, 2);
559 v3->Add(new wxCheckBox(this, svx_XSECT, wmsg(/*Cross-sections*/393),
560 wxDefaultPosition, wxDefaultSize, 0,
561 BitValidator(&m_layout.show_mask, XSECT)),
562 0, wxALIGN_LEFT|wxALL, 2);
563 if (!printing) {
564 v3->Add(new wxCheckBox(this, svx_WALLS, wmsg(/*Walls*/394),
565 wxDefaultPosition, wxDefaultSize, 0,
566 BitValidator(&m_layout.show_mask, WALLS)),
567 0, wxALIGN_LEFT|wxALL, 2);
568 // TRANSLATORS: Label for checkbox which controls whether there's a
569 // layer in the exported file (for formats such as DXF and SVG)
570 // containing polygons for the inside of cave passages).
571 v3->Add(new wxCheckBox(this, svx_PASSAGES, wmsg(/*Passages*/395),
572 wxDefaultPosition, wxDefaultSize, 0,
573 BitValidator(&m_layout.show_mask, PASG)),
574 0, wxALIGN_LEFT|wxALL, 2);
575 v3->Add(new wxCheckBox(this, svx_CENTRED, wmsg(/*Origin in centre*/421),
576 wxDefaultPosition, wxDefaultSize, 0,
577 BitValidator(&m_layout.show_mask, CENTRED)),
578 0, wxALIGN_LEFT|wxALL, 2);
579 v3->Add(new wxCheckBox(this, svx_FULLCOORDS, wmsg(/*Full coordinates*/422),
580 wxDefaultPosition, wxDefaultSize, 0,
581 BitValidator(&m_layout.show_mask, FULL_COORDS)),
582 0, wxALIGN_LEFT|wxALL, 2);
584 if (printing) {
585 /* TRANSLATORS: used in the print dialog - controls drawing lines
586 * around each page */
587 v3->Add(new wxCheckBox(this, svx_BORDERS, wmsg(/*Page Borders*/264),
588 wxDefaultPosition, wxDefaultSize, 0,
589 wxGenericValidator(&m_layout.Border)),
590 0, wxALIGN_LEFT|wxALL, 2);
591 /* TRANSLATORS: will be used in the print dialog - check this to print
592 * blank pages (otherwise they’ll be skipped to save paper) */
593 // m_blanks = new wxCheckBox(this, svx_BLANKS, wmsg(/*Blank Pages*/266));
594 // v3->Add(m_blanks, 0, wxALIGN_LEFT|wxALL, 2);
595 /* TRANSLATORS: As in the legend on a map. Used in the print dialog -
596 * controls drawing the box at the lower left with survey name, view
597 * angles, etc */
598 v3->Add(new wxCheckBox(this, svx_LEGEND, wmsg(/*Legend*/265),
599 wxDefaultPosition, wxDefaultSize, 0,
600 wxGenericValidator(&m_layout.Legend)),
601 0, wxALIGN_LEFT|wxALL, 2);
604 h1->Add(v3, 0, wxALIGN_LEFT|wxALL, 5);
605 h1->Add(m_viewbox, 0, wxALIGN_LEFT|wxLEFT, 5);
607 if (!printing) {
608 /* TRANSLATORS: The PROJ library is used to do coordinate
609 * transformations (https://trac.osgeo.org/proj/) - if the .3d file
610 * doesn't contain details of the coordinate projection in use, the
611 * user must specify it here for export formats which need to know it
612 * (e.g. GPX).
614 h2->Add(new wxStaticText(this, svx_PROJ_LABEL, wmsg(/*Coordinate projection*/440)),
615 0, wxLEFT|wxALIGN_CENTRE_VERTICAL, 5);
616 long style = 0;
617 if (!m_layout.cs_proj.empty()) {
618 // If the input file specified the coordinate system, don't let the
619 // user mess with it.
620 style = wxTE_READONLY;
621 } else {
622 #if 0 // FIXME: Is it a good idea to save this?
623 wxConfigBase * cfg = wxConfigBase::Get();
624 wxString input_projection;
625 cfg->Read(wxT("input_projection"), &input_projection);
626 if (!input_projection.empty())
627 proj_edit.SetValue(input_projection);
628 #endif
630 wxTextCtrl * proj_edit = new wxTextCtrl(this, svx_PROJ, m_layout.cs_proj,
631 wxDefaultPosition, wxDefaultSize,
632 style);
633 h2->Add(proj_edit, 1, wxALL|wxEXPAND|wxALIGN_CENTRE_VERTICAL, 5);
634 v1->Add(h2, 0, wxALIGN_LEFT|wxEXPAND, 5);
637 v1->Add(h1, 0, wxALIGN_LEFT|wxALL, 5);
639 // When we enable/disable checkboxes in the export dialog, ideally we'd
640 // like the dialog to resize, but not sure how to achieve that, so we
641 // add a stretchable spacer here so at least the buttons stay in the
642 // lower right corner.
643 v1->AddStretchSpacer();
645 wxButton * but;
646 but = new wxButton(this, wxID_CANCEL);
647 h3->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
648 if (printing) {
649 #ifdef AVEN_PRINT_PREVIEW
650 but = new wxButton(this, wxID_PREVIEW);
651 h3->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
652 but = new wxButton(this, wxID_PRINT);
653 #else
654 but = new wxButton(this, wxID_PRINT, wmsg(/*&Print...*/400));
655 #endif
656 } else {
657 /* TRANSLATORS: The text on the action button in the "Export" settings
658 * dialog */
659 but = new wxButton(this, svx_EXPORT, wmsg(/*&Export...*/230));
661 but->SetDefault();
662 h3->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
663 v1->Add(h3, 0, wxALIGN_RIGHT|wxALL, 5);
665 SetAutoLayout(true);
666 SetSizer(v1);
667 v1->SetSizeHints(this);
669 LayoutToUI();
670 SomethingChanged(0);
673 void
674 svxPrintDlg::OnPrint(wxCommandEvent&) {
675 SomethingChanged(0);
676 TransferDataFromWindow();
677 wxPageSetupDialogData * psdd = wxGetApp().GetPageSetupDialogData();
678 wxPrintDialogData pd(psdd->GetPrintData());
679 wxPrinter pr(&pd);
680 svxPrintout po(mainfrm, &m_layout, psdd, m_File);
681 if (m_layout.SkipBlank) {
682 // FIXME: wx's printing requires a contiguous range of valid page
683 // numbers. To achieve that, we need to run a scan for blank pages
684 // here, so that GetPageInfo() knows what range to return, and so
685 // that OnPrintPage() can map a page number back to where in the
686 // MxN multi-page layout.
687 #if 0
688 po.scan_for_blank_pages = true;
689 for (int page = 1; page <= m_layout->pages; ++page) {
690 po.fBlankPage = fTrue;
691 po.OnPrintPage(page);
692 // FIXME: Do something with po.fBlankPage
694 po.scan_for_blank_pages = false;
695 #endif
697 if (pr.Print(this, &po, true)) {
698 // Close the print dialog if printing succeeded.
699 Destroy();
703 void
704 svxPrintDlg::OnExport(wxCommandEvent&) {
705 UIToLayout();
706 TransferDataFromWindow();
707 wxString leaf;
708 wxFileName::SplitPath(m_File, NULL, NULL, &leaf, NULL, wxPATH_NATIVE);
709 unsigned format_idx = ((wxChoice*)FindWindow(svx_FORMAT))->GetSelection();
710 leaf += wxString::FromUTF8(extension[format_idx]);
712 wxString filespec = wmsg(msg_filetype[format_idx]);
713 filespec += wxT("|*");
714 filespec += wxString::FromUTF8(extension[format_idx]);
715 filespec += wxT("|");
716 filespec += wmsg(/*All files*/208);
717 filespec += wxT("|");
718 filespec += wxFileSelectorDefaultWildcardStr;
720 /* TRANSLATORS: Title of file dialog to choose name and type of exported
721 * file. */
722 wxFileDialog dlg(this, wmsg(/*Export as:*/401), wxString(), leaf,
723 filespec, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
724 if (dlg.ShowModal() == wxID_OK) {
725 wxString input_projection = ((wxTextCtrl*)FindWindow(svx_PROJ))->GetValue();
726 double grid = 100; // metres
727 double text_height = 0.6;
728 double marker_size = 0.8;
730 try {
731 const wxString& export_fnm = dlg.GetPath();
732 if (!Export(export_fnm, m_layout.title,
733 m_layout.datestamp, m_layout.datestamp_numeric, mainfrm,
734 m_layout.rot, m_layout.tilt,
735 m_layout.get_effective_show_mask(),
736 export_format(format_idx), input_projection.utf8_str(),
737 grid, text_height, marker_size, m_layout.Scale)) {
738 wxString m = wxString::Format(wmsg(/*Couldn’t write file “%s”*/402).c_str(),
739 export_fnm.c_str());
740 wxGetApp().ReportError(m);
742 } catch (const wxString & m) {
743 wxGetApp().ReportError(m);
746 Destroy();
749 #ifdef AVEN_PRINT_PREVIEW
750 void
751 svxPrintDlg::OnPreview(wxCommandEvent&) {
752 SomethingChanged(0);
753 TransferDataFromWindow();
754 wxPageSetupDialogData * psdd = wxGetApp().GetPageSetupDialogData();
755 wxPrintDialogData pd(psdd->GetPrintData());
756 wxPrintPreview* pv;
757 pv = new wxPrintPreview(new svxPrintout(mainfrm, &m_layout, psdd, m_File),
758 new svxPrintout(mainfrm, &m_layout, psdd, m_File),
759 &pd);
760 // TRANSLATORS: Title of the print preview dialog
761 wxPreviewFrame *frame = new wxPreviewFrame(pv, mainfrm, wmsg(/*Print Preview*/398));
762 frame->Initialize();
764 // Size preview frame so that all of the controlbar and canvas can be seen
765 // if possible.
766 int w, h;
767 // GetBestSize gives us the width needed to show the whole controlbar.
768 frame->GetBestSize(&w, &h);
769 if (h < w) {
770 // On wxGTK at least, GetBestSize() returns much too small a height.
771 h = w * 6 / 5;
773 // Ensure that we don't make the window bigger than the screen.
774 // Use wxGetClientDisplayRect() so we don't cover the MS Windows
775 // task bar either.
776 wxRect disp = wxGetClientDisplayRect();
777 if (w > disp.GetWidth()) w = disp.GetWidth();
778 if (h > disp.GetHeight()) h = disp.GetHeight();
779 // Centre the window within the "ClientDisplayRect".
780 int x = disp.GetLeft() + (disp.GetWidth() - w) / 2;
781 int y = disp.GetTop() + (disp.GetHeight() - h) / 2;
782 frame->SetSize(x, y, w, h);
784 frame->Show();
786 #endif
788 void
789 svxPrintDlg::OnPlan(wxCommandEvent&) {
790 m_tilt->SetValue(-90.0);
791 SomethingChanged(svx_TILT);
794 void
795 svxPrintDlg::OnElevation(wxCommandEvent&) {
796 m_tilt->SetValue(0.0);
797 SomethingChanged(svx_TILT);
800 void
801 svxPrintDlg::OnPlanUpdate(wxUpdateUIEvent& e) {
802 e.Enable(m_tilt->GetValue() != -90.0);
805 void
806 svxPrintDlg::OnElevationUpdate(wxUpdateUIEvent& e) {
807 e.Enable(m_tilt->GetValue() != 0.0);
810 void
811 svxPrintDlg::OnChangeSpin(wxSpinDoubleEvent& e) {
812 SomethingChanged(e.GetId());
815 void
816 svxPrintDlg::OnChange(wxCommandEvent& e) {
817 if (e.GetId() == svx_SCALE && m_scale) {
818 default_scale_print = m_scale->GetValue();
819 if (default_scale_print != scales[0]) {
820 // Don't store "One Page" for use when exporting.
821 default_scale_export = default_scale_print;
824 SomethingChanged(e.GetId());
827 void
828 svxPrintDlg::OnCancel(wxCommandEvent&) {
829 if (close_after)
830 mainfrm->Close();
831 Destroy();
834 void
835 svxPrintDlg::SomethingChanged(int control_id) {
836 if ((control_id == 0 || control_id == svx_FORMAT) && m_format) {
837 // Update the shown/hidden fields for the newly selected export filter.
838 int new_filter_idx = m_format->GetSelection();
839 if (new_filter_idx != wxNOT_FOUND) {
840 unsigned mask = format_info[new_filter_idx];
841 static const struct { int id; unsigned mask; } controls[] = {
842 { svx_LEGS, LEGS },
843 { svx_SURFACE, SURF },
844 { svx_SPLAYS, SPLAYS },
845 { svx_STATIONS, STNS },
846 { svx_NAMES, LABELS },
847 { svx_XSECT, XSECT },
848 { svx_WALLS, WALLS },
849 { svx_PASSAGES, PASG },
850 { svx_ENTS, ENTS },
851 { svx_FIXES, FIXES },
852 { svx_EXPORTS, EXPORTS },
853 { svx_CENTRED, CENTRED },
854 { svx_FULLCOORDS, FULL_COORDS },
855 { svx_PROJ_LABEL, PROJ },
856 { svx_PROJ, PROJ },
858 static unsigned n_controls = sizeof(controls) / sizeof(controls[0]);
859 for (unsigned i = 0; i != n_controls; ++i) {
860 wxWindow * control = FindWindow(controls[i].id);
861 if (control) control->Show(mask & controls[i].mask);
863 m_scalebox->Show(bool(mask & SCALE));
864 m_viewbox->Show(!bool(mask & EXPORT_3D));
865 GetSizer()->Layout();
866 if (control_id == svx_FORMAT) {
867 wxConfigBase * cfg = wxConfigBase::Get();
868 cfg->Write(wxT("export_format"), formats[new_filter_idx]);
873 UIToLayout();
875 if (m_printSize || m_scale) {
876 // Update the bounding box.
877 RecalcBounds();
879 if (m_scale) {
880 if (!(m_scale->GetValue()).ToDouble(&(m_layout.Scale)) ||
881 m_layout.Scale == 0.0) {
882 m_layout.pick_scale(1, 1);
887 if (m_printSize && m_layout.xMax >= m_layout.xMin) {
888 m_layout.pages_required();
889 m_printSize->SetLabel(wxString::Format(wmsg(/*%d pages (%dx%d)*/257), m_layout.pages, m_layout.pagesX, m_layout.pagesY));
893 void
894 svxPrintDlg::LayoutToUI()
896 // m_blanks->SetValue(m_layout.SkipBlank);
897 if (m_layout.view != layout::EXTELEV) {
898 m_tilt->SetValue(m_layout.tilt);
899 m_bearing->SetValue(m_layout.rot);
902 if (m_scale && m_layout.Scale != 0) {
903 // Do this last as it causes an OnChange message which calls UIToLayout
904 wxString temp;
905 temp << m_layout.Scale;
906 m_scale->SetValue(temp);
910 void
911 svxPrintDlg::UIToLayout()
913 // m_layout.SkipBlank = m_blanks->IsChecked();
915 if (m_layout.view != layout::EXTELEV && m_tilt) {
916 m_layout.tilt = m_tilt->GetValue();
917 if (m_layout.tilt == -90.0) {
918 m_layout.view = layout::PLAN;
919 } else if (m_layout.tilt == 0.0) {
920 m_layout.view = layout::ELEV;
921 } else {
922 m_layout.view = layout::TILT;
925 bool enable_passage_opts = (m_layout.view != layout::TILT);
926 wxWindow * win;
927 win = FindWindow(svx_XSECT);
928 if (win) win->Enable(enable_passage_opts);
929 win = FindWindow(svx_WALLS);
930 if (win) win->Enable(enable_passage_opts);
931 win = FindWindow(svx_PASSAGES);
932 if (win) win->Enable(enable_passage_opts);
934 m_layout.rot = m_bearing->GetValue();
938 void
939 svxPrintDlg::RecalcBounds()
941 m_layout.yMax = m_layout.xMax = -DBL_MAX;
942 m_layout.yMin = m_layout.xMin = DBL_MAX;
944 double SIN = sin(rad(m_layout.rot));
945 double COS = cos(rad(m_layout.rot));
946 double SINT = sin(rad(m_layout.tilt));
947 double COST = cos(rad(m_layout.tilt));
949 int show_mask = m_layout.get_effective_show_mask();
950 if (show_mask & LEGS) {
951 list<traverse>::const_iterator trav = mainfrm->traverses_begin();
952 list<traverse>::const_iterator tend = mainfrm->traverses_end();
953 for ( ; trav != tend; ++trav) {
954 if (trav->isSplay && !(show_mask & SPLAYS))
955 continue;
956 vector<PointInfo>::const_iterator pos = trav->begin();
957 vector<PointInfo>::const_iterator end = trav->end();
958 for ( ; pos != end; ++pos) {
959 double x = pos->GetX();
960 double y = pos->GetY();
961 double z = pos->GetZ();
962 double X = x * COS - y * SIN;
963 if (X > m_layout.xMax) m_layout.xMax = X;
964 if (X < m_layout.xMin) m_layout.xMin = X;
965 double Y = z * COST - (x * SIN + y * COS) * SINT;
966 if (Y > m_layout.yMax) m_layout.yMax = Y;
967 if (Y < m_layout.yMin) m_layout.yMin = Y;
972 if ((show_mask & XSECT) &&
973 (m_layout.tilt == 0.0 || m_layout.tilt == 90.0 || m_layout.tilt == -90.0)) {
974 list<vector<XSect> >::const_iterator trav = mainfrm->tubes_begin();
975 list<vector<XSect> >::const_iterator tend = mainfrm->tubes_end();
976 for ( ; trav != tend; ++trav) {
977 XSect prev_pt_v;
978 Vector3 last_right(1.0, 0.0, 0.0);
980 vector<XSect>::const_iterator i = trav->begin();
981 vector<XSect>::size_type segment = 0;
982 while (i != trav->end()) {
983 // get the coordinates of this vertex
984 const XSect & pt_v = *i++;
985 if (m_layout.tilt == 0.0) {
986 Double u = pt_v.GetU();
987 Double d = pt_v.GetD();
989 if (u >= 0 || d >= 0) {
990 double x = pt_v.GetX();
991 double y = pt_v.GetY();
992 double z = pt_v.GetZ();
993 double X = x * COS - y * SIN;
994 double Y = z * COST - (x * SIN + y * COS) * SINT;
996 if (X > m_layout.xMax) m_layout.xMax = X;
997 if (X < m_layout.xMin) m_layout.xMin = X;
998 double U = Y + max(0.0, pt_v.GetU());
999 if (U > m_layout.yMax) m_layout.yMax = U;
1000 double D = Y - max(0.0, pt_v.GetD());
1001 if (D < m_layout.yMin) m_layout.yMin = D;
1003 } else {
1004 // More complex, and this duplicates the algorithm from
1005 // PlotLR() - we should try to share that, maybe via a
1006 // template.
1007 Vector3 right;
1009 const Vector3 up_v(0.0, 0.0, 1.0);
1011 if (segment == 0) {
1012 assert(i != trav->end());
1013 // first segment
1015 // get the coordinates of the next vertex
1016 const XSect & next_pt_v = *i;
1018 // calculate vector from this pt to the next one
1019 Vector3 leg_v = next_pt_v - pt_v;
1021 // obtain a vector in the LRUD plane
1022 right = leg_v * up_v;
1023 if (right.magnitude() == 0) {
1024 right = last_right;
1025 } else {
1026 last_right = right;
1028 } else if (segment + 1 == trav->size()) {
1029 // last segment
1031 // Calculate vector from the previous pt to this one.
1032 Vector3 leg_v = pt_v - prev_pt_v;
1034 // Obtain a horizontal vector in the LRUD plane.
1035 right = leg_v * up_v;
1036 if (right.magnitude() == 0) {
1037 right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
1038 } else {
1039 last_right = right;
1041 } else {
1042 assert(i != trav->end());
1043 // Intermediate segment.
1045 // Get the coordinates of the next vertex.
1046 const XSect & next_pt_v = *i;
1048 // Calculate vectors from this vertex to the
1049 // next vertex, and from the previous vertex to
1050 // this one.
1051 Vector3 leg1_v = pt_v - prev_pt_v;
1052 Vector3 leg2_v = next_pt_v - pt_v;
1054 // Obtain horizontal vectors perpendicular to
1055 // both legs, then normalise and average to get
1056 // a horizontal bisector.
1057 Vector3 r1 = leg1_v * up_v;
1058 Vector3 r2 = leg2_v * up_v;
1059 r1.normalise();
1060 r2.normalise();
1061 right = r1 + r2;
1062 if (right.magnitude() == 0) {
1063 // This is the "mid-pitch" case...
1064 right = last_right;
1066 last_right = right;
1069 // Scale to unit vectors in the LRUD plane.
1070 right.normalise();
1072 Double l = pt_v.GetL();
1073 Double r = pt_v.GetR();
1075 if (l >= 0 || r >= 0) {
1076 // Get the x and y coordinates of the survey station
1077 double pt_X = pt_v.GetX() * COS - pt_v.GetY() * SIN;
1078 double pt_Y = pt_v.GetX() * SIN + pt_v.GetY() * COS;
1080 double X, Y;
1081 if (l >= 0) {
1082 // Get the x and y coordinates of the end of the left arrow
1083 Vector3 p = pt_v - right * l;
1084 X = p.GetX() * COS - p.GetY() * SIN;
1085 Y = (p.GetX() * SIN + p.GetY() * COS);
1086 } else {
1087 X = pt_X;
1088 Y = pt_Y;
1090 if (X > m_layout.xMax) m_layout.xMax = X;
1091 if (X < m_layout.xMin) m_layout.xMin = X;
1092 if (Y > m_layout.yMax) m_layout.yMax = Y;
1093 if (Y < m_layout.yMin) m_layout.yMin = Y;
1095 if (r >= 0) {
1096 // Get the x and y coordinates of the end of the right arrow
1097 Vector3 p = pt_v + right * r;
1098 X = p.GetX() * COS - p.GetY() * SIN;
1099 Y = (p.GetX() * SIN + p.GetY() * COS);
1100 } else {
1101 X = pt_X;
1102 Y = pt_Y;
1104 if (X > m_layout.xMax) m_layout.xMax = X;
1105 if (X < m_layout.xMin) m_layout.xMin = X;
1106 if (Y > m_layout.yMax) m_layout.yMax = Y;
1107 if (Y < m_layout.yMin) m_layout.yMin = Y;
1110 prev_pt_v = pt_v;
1112 ++segment;
1118 if (show_mask & SURF) {
1119 list<traverse>::const_iterator trav = mainfrm->surface_traverses_begin();
1120 list<traverse>::const_iterator tend = mainfrm->surface_traverses_end();
1121 for ( ; trav != tend; ++trav) {
1122 if (trav->isSplay && !(show_mask & SPLAYS))
1123 continue;
1124 vector<PointInfo>::const_iterator pos = trav->begin();
1125 vector<PointInfo>::const_iterator end = trav->end();
1126 for ( ; pos != end; ++pos) {
1127 double x = pos->GetX();
1128 double y = pos->GetY();
1129 double z = pos->GetZ();
1130 double X = x * COS - y * SIN;
1131 if (X > m_layout.xMax) m_layout.xMax = X;
1132 if (X < m_layout.xMin) m_layout.xMin = X;
1133 double Y = z * COST - (x * SIN + y * COS) * SINT;
1134 if (Y > m_layout.yMax) m_layout.yMax = Y;
1135 if (Y < m_layout.yMin) m_layout.yMin = Y;
1139 if (show_mask & (LABELS|STNS)) {
1140 list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
1141 while (label != mainfrm->GetLabelsEnd()) {
1142 double x = (*label)->GetX();
1143 double y = (*label)->GetY();
1144 double z = (*label)->GetZ();
1145 if ((show_mask & SURF) || (*label)->IsUnderground()) {
1146 double X = x * COS - y * SIN;
1147 if (X > m_layout.xMax) m_layout.xMax = X;
1148 if (X < m_layout.xMin) m_layout.xMin = X;
1149 double Y = z * COST - (x * SIN + y * COS) * SINT;
1150 if (Y > m_layout.yMax) m_layout.yMax = Y;
1151 if (Y < m_layout.yMin) m_layout.yMin = Y;
1153 ++label;
1158 static int xpPageWidth, ypPageDepth;
1159 static long x_offset, y_offset;
1160 static int fontsize, fontsize_labels;
1162 /* FIXME: allow the font to be set */
1164 static const char *fontname = "Arial", *fontname_labels = "Arial";
1166 svxPrintout::svxPrintout(MainFrm *mainfrm_, layout *l,
1167 wxPageSetupDialogData *data, const wxString & title)
1168 : wxPrintout(title), font_labels(NULL), font_default(NULL),
1169 scan_for_blank_pages(false)
1171 mainfrm = mainfrm_;
1172 m_layout = l;
1173 m_data = data;
1176 void
1177 svxPrintout::draw_info_box()
1179 layout *l = m_layout;
1180 int boxwidth = 70;
1181 int boxheight = 30;
1183 pdc->SetPen(*pen_frame);
1185 int div = boxwidth;
1186 if (l->view != layout::EXTELEV) {
1187 boxwidth += boxheight;
1188 MOVEMM(div, boxheight);
1189 DRAWMM(div, 0);
1190 MOVEMM(0, 30); DRAWMM(div, 30);
1193 MOVEMM(0, boxheight);
1194 DRAWMM(boxwidth, boxheight);
1195 DRAWMM(boxwidth, 0);
1196 if (!l->Border) {
1197 DRAWMM(0, 0);
1198 DRAWMM(0, boxheight);
1201 MOVEMM(0, 20); DRAWMM(div, 20);
1202 MOVEMM(0, 10); DRAWMM(div, 10);
1204 switch (l->view) {
1205 case layout::PLAN: {
1206 long ax, ay, bx, by, cx, cy, dx, dy;
1208 long xc = boxwidth - boxheight / 2;
1209 long yc = boxheight / 2;
1210 const double RADIUS = boxheight / 3;
1211 DrawEllipse(long(xc * l->scX), long(yc * l->scY),
1212 long(RADIUS * l->scX), long(RADIUS * l->scY));
1214 ax = (long)((xc - (RADIUS - 1) * sin(rad(000.0 + l->rot))) * l->scX);
1215 ay = (long)((yc + (RADIUS - 1) * cos(rad(000.0 + l->rot))) * l->scY);
1216 bx = (long)((xc - RADIUS * 0.5 * sin(rad(180.0 + l->rot))) * l->scX);
1217 by = (long)((yc + RADIUS * 0.5 * cos(rad(180.0 + l->rot))) * l->scY);
1218 cx = (long)((xc - (RADIUS - 1) * sin(rad(160.0 + l->rot))) * l->scX);
1219 cy = (long)((yc + (RADIUS - 1) * cos(rad(160.0 + l->rot))) * l->scY);
1220 dx = (long)((xc - (RADIUS - 1) * sin(rad(200.0 + l->rot))) * l->scX);
1221 dy = (long)((yc + (RADIUS - 1) * cos(rad(200.0 + l->rot))) * l->scY);
1223 MoveTo(ax, ay);
1224 DrawTo(bx, by);
1225 DrawTo(cx, cy);
1226 DrawTo(ax, ay);
1227 DrawTo(dx, dy);
1228 DrawTo(bx, by);
1230 pdc->SetTextForeground(colour_text);
1231 MOVEMM(div + 0.5, boxheight - 5.5);
1232 WriteString(wmsg(/*North*/115));
1234 wxString angle = format_angle(ANGLE_FMT, l->rot);
1235 wxString s;
1236 /* TRANSLATORS: This is used on printouts of plans, with %s replaced by
1237 * something like "123°". The bearing is up the page. */
1238 s.Printf(wmsg(/*Plan view, %s up page*/168), angle.c_str());
1239 MOVEMM(2, 12); WriteString(s);
1240 break;
1242 case layout::ELEV: case layout::TILT: {
1243 const int L = div + 2;
1244 const int R = boxwidth - 2;
1245 const int H = boxheight / 2;
1246 MOVEMM(L, H); DRAWMM(L + 5, H - 3); DRAWMM(L + 3, H); DRAWMM(L + 5, H + 3);
1248 DRAWMM(L, H); DRAWMM(R, H);
1250 DRAWMM(R - 5, H + 3); DRAWMM(R - 3, H); DRAWMM(R - 5, H - 3); DRAWMM(R, H);
1252 MOVEMM((L + R) / 2, H - 2); DRAWMM((L + R) / 2, H + 2);
1254 pdc->SetTextForeground(colour_text);
1255 MOVEMM(div + 2, boxheight - 8);
1256 /* TRANSLATORS: "Elevation on" 020 <-> 200 degrees */
1257 WriteString(wmsg(/*Elevation on*/116));
1259 MOVEMM(L, 2);
1260 WriteString(format_angle(ANGLE_FMT, fmod(l->rot + 270.0, 360.0)));
1261 MOVEMM(R - 10, 2);
1262 WriteString(format_angle(ANGLE_FMT, fmod(l->rot + 90.0, 360.0)));
1264 wxString angle = format_angle(ANGLE_FMT, l->rot);
1265 wxString s;
1266 if (l->view == layout::ELEV) {
1267 /* TRANSLATORS: This is used on printouts of elevations, with %s
1268 * replaced by something like "123°". The bearing is the direction
1269 * we’re looking. */
1270 s.Printf(wmsg(/*Elevation facing %s*/169), angle.c_str());
1271 } else {
1272 wxString a2 = format_angle(ANGLE2_FMT, l->tilt);
1273 /* TRANSLATORS: This is used on printouts of tilted elevations, with
1274 * the first %s replaced by something like "123°", and the second by
1275 * something like "-45°". The bearing is the direction we’re
1276 * looking. */
1277 s.Printf(wmsg(/*Elevation facing %s, tilted %s*/284), angle.c_str(), a2.c_str());
1279 MOVEMM(2, 12); WriteString(s);
1280 break;
1282 case layout::EXTELEV:
1283 pdc->SetTextForeground(colour_text);
1284 MOVEMM(2, 12);
1285 /* TRANSLATORS: This is used on printouts of extended elevations. */
1286 WriteString(wmsg(/*Extended elevation*/191));
1287 break;
1290 MOVEMM(2, boxheight - 8); WriteString(l->title);
1292 MOVEMM(2, 2);
1293 // FIXME: "Original Scale" better?
1294 WriteString(wxString::Format(wmsg(/*Scale*/154) + wxT(" 1:%.0f"),
1295 l->Scale));
1297 /* This used to be a copyright line, but it was occasionally
1298 * mis-interpreted as us claiming copyright on the survey, so let's
1299 * give the website URL instead */
1300 MOVEMM(boxwidth + 2, 2);
1301 WriteString(wxT("Survex " VERSION " - https://survex.com/"));
1303 draw_scale_bar(boxwidth + 10.0, 17.0, l->PaperWidth - boxwidth - 18.0);
1306 /* Draw fancy scale bar with bottom left at (x,y) (both in mm) and at most */
1307 /* MaxLength mm long. The scaling in use is 1:scale */
1308 void
1309 svxPrintout::draw_scale_bar(double x, double y, double MaxLength)
1311 double StepEst, d;
1312 int E, Step, n, c;
1313 wxString buf;
1314 /* Limit scalebar to 20cm to stop people with A0 plotters complaining */
1315 if (MaxLength > 200.0) MaxLength = 200.0;
1317 #define dmin 10.0 /* each division >= dmin mm long */
1318 #define StepMax 5 /* number in steps of at most StepMax (x 10^N) */
1319 #define epsilon (1e-4) /* fudge factor to prevent rounding problems */
1321 E = (int)ceil(log10((dmin * 0.001 * m_layout->Scale) / StepMax));
1322 StepEst = pow(10.0, -(double)E) * (dmin * 0.001) * m_layout->Scale - epsilon;
1324 /* Force labelling to be in multiples of 1, 2, or 5 */
1325 Step = (StepEst <= 1.0 ? 1 : (StepEst <= 2.0 ? 2 : 5));
1327 /* Work out actual length of each scale bar division */
1328 d = Step * pow(10.0, (double)E) / m_layout->Scale * 1000.0;
1330 /* FIXME: Non-metric units here... */
1331 /* Choose appropriate units, s.t. if possible E is >=0 and minimized */
1332 int units;
1333 if (E >= 3) {
1334 E -= 3;
1335 units = /*km*/423;
1336 } else if (E >= 0) {
1337 units = /*m*/424;
1338 } else {
1339 E += 2;
1340 units = /*cm*/425;
1343 buf = wmsg(/*Scale*/154);
1345 /* Add units used - eg. "Scale (10m)" */
1346 double pow10_E = pow(10.0, (double)E);
1347 if (E >= 0) {
1348 buf += wxString::Format(wxT(" (%.f%s)"), pow10_E, wmsg(units).c_str());
1349 } else {
1350 int sf = -(int)floor(E);
1351 buf += wxString::Format(wxT(" (%.*f%s)"), sf, pow10_E, wmsg(units).c_str());
1353 pdc->SetTextForeground(colour_text);
1354 MOVEMM(x, y + 4); WriteString(buf);
1356 /* Work out how many divisions there will be */
1357 n = (int)(MaxLength / d);
1359 pdc->SetPen(*pen_frame);
1361 long Y = long(y * m_layout->scY);
1362 long Y2 = long((y + 3) * m_layout->scY);
1363 long X = long(x * m_layout->scX);
1364 long X2 = long((x + n * d) * m_layout->scX);
1366 /* Draw top of scale bar */
1367 MoveTo(X2, Y2);
1368 DrawTo(X, Y2);
1369 #if 0
1370 DrawTo(X2, Y);
1371 DrawTo(X, Y);
1372 MOVEMM(x + n * d, y); DRAWMM(x, y);
1373 #endif
1374 /* Draw divisions and label them */
1375 for (c = 0; c <= n; c++) {
1376 pdc->SetPen(*pen_frame);
1377 X = long((x + c * d) * m_layout->scX);
1378 MoveTo(X, Y);
1379 DrawTo(X, Y2);
1380 #if 0 // Don't waste toner!
1381 /* Draw a "zebra crossing" scale bar. */
1382 if (c < n && (c & 1) == 0) {
1383 X2 = long((x + (c + 1) * d) * m_layout->scX);
1384 SolidRectangle(X, Y, X2 - X, Y2 - Y);
1386 #endif
1387 buf.Printf(wxT("%d"), c * Step);
1388 pdc->SetTextForeground(colour_text);
1389 MOVEMM(x + c * d - buf.length(), y - 5);
1390 WriteString(buf);
1394 #if 0
1395 void
1396 make_calibration(layout *l) {
1397 img_point pt = { 0.0, 0.0, 0.0 };
1398 l->xMax = l->yMax = 0.1;
1399 l->xMin = l->yMin = 0;
1401 stack(l, img_MOVE, NULL, &pt);
1402 pt.x = 0.1;
1403 stack(l, img_LINE, NULL, &pt);
1404 pt.y = 0.1;
1405 stack(l, img_LINE, NULL, &pt);
1406 pt.x = 0.0;
1407 stack(l, img_LINE, NULL, &pt);
1408 pt.y = 0.0;
1409 stack(l, img_LINE, NULL, &pt);
1410 pt.x = 0.05;
1411 pt.y = 0.001;
1412 stack(l, img_LABEL, "10cm", &pt);
1413 pt.x = 0.001;
1414 pt.y = 0.05;
1415 stack(l, img_LABEL, "10cm", &pt);
1416 l->Scale = 1.0;
1418 #endif
1421 svxPrintout::next_page(int *pstate, char **q, int pageLim)
1423 char *p;
1424 int page;
1425 int c;
1426 p = *q;
1427 if (*pstate > 0) {
1428 /* doing a range */
1429 (*pstate)++;
1430 wxASSERT(*p == '-');
1431 p++;
1432 while (isspace((unsigned char)*p)) p++;
1433 if (sscanf(p, "%u%n", &page, &c) > 0) {
1434 p += c;
1435 } else {
1436 page = pageLim;
1438 if (*pstate > page) goto err;
1439 if (*pstate < page) return *pstate;
1440 *q = p;
1441 *pstate = 0;
1442 return page;
1445 while (isspace((unsigned char)*p) || *p == ',') p++;
1447 if (!*p) return 0; /* done */
1449 if (*p == '-') {
1450 *q = p;
1451 *pstate = 1;
1452 return 1; /* range with initial parameter omitted */
1454 if (sscanf(p, "%u%n", &page, &c) > 0) {
1455 p += c;
1456 while (isspace((unsigned char)*p)) p++;
1457 *q = p;
1458 if (0 < page && page <= pageLim) {
1459 if (*p == '-') *pstate = page; /* range with start */
1460 return page;
1463 err:
1464 *pstate = -1;
1465 return 0;
1468 /* Draws in alignment marks on each page or borders on edge pages */
1469 void
1470 svxPrintout::drawticks(int tsize, int x, int y)
1472 long i;
1473 int s = tsize * 4;
1474 int o = s / 8;
1475 bool fAtCorner = fFalse;
1476 pdc->SetPen(*pen_frame);
1477 if (x == 0 && m_layout->Border) {
1478 /* solid left border */
1479 MoveTo(clip.x_min, clip.y_min);
1480 DrawTo(clip.x_min, clip.y_max);
1481 fAtCorner = fTrue;
1482 } else {
1483 if (x > 0 || y > 0) {
1484 MoveTo(clip.x_min, clip.y_min);
1485 DrawTo(clip.x_min, clip.y_min + tsize);
1487 if (s && x > 0 && m_layout->Cutlines) {
1488 /* dashed left border */
1489 i = (clip.y_max - clip.y_min) -
1490 (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1491 for ( ; i > tsize; i -= s) {
1492 MoveTo(clip.x_min, clip.y_max - (i + o));
1493 DrawTo(clip.x_min, clip.y_max - (i - o));
1496 if (x > 0 || y < m_layout->pagesY - 1) {
1497 MoveTo(clip.x_min, clip.y_max - tsize);
1498 DrawTo(clip.x_min, clip.y_max);
1499 fAtCorner = fTrue;
1503 if (y == m_layout->pagesY - 1 && m_layout->Border) {
1504 /* solid top border */
1505 if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1506 DrawTo(clip.x_max, clip.y_max);
1507 fAtCorner = fTrue;
1508 } else {
1509 if (y < m_layout->pagesY - 1 || x > 0) {
1510 if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1511 DrawTo(clip.x_min + tsize, clip.y_max);
1513 if (s && y < m_layout->pagesY - 1 && m_layout->Cutlines) {
1514 /* dashed top border */
1515 i = (clip.x_max - clip.x_min) -
1516 (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1517 for ( ; i > tsize; i -= s) {
1518 MoveTo(clip.x_max - (i + o), clip.y_max);
1519 DrawTo(clip.x_max - (i - o), clip.y_max);
1522 if (y < m_layout->pagesY - 1 || x < m_layout->pagesX - 1) {
1523 MoveTo(clip.x_max - tsize, clip.y_max);
1524 DrawTo(clip.x_max, clip.y_max);
1525 fAtCorner = fTrue;
1526 } else {
1527 fAtCorner = fFalse;
1531 if (x == m_layout->pagesX - 1 && m_layout->Border) {
1532 /* solid right border */
1533 if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1534 DrawTo(clip.x_max, clip.y_min);
1535 fAtCorner = fTrue;
1536 } else {
1537 if (x < m_layout->pagesX - 1 || y < m_layout->pagesY - 1) {
1538 if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1539 DrawTo(clip.x_max, clip.y_max - tsize);
1541 if (s && x < m_layout->pagesX - 1 && m_layout->Cutlines) {
1542 /* dashed right border */
1543 i = (clip.y_max - clip.y_min) -
1544 (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1545 for ( ; i > tsize; i -= s) {
1546 MoveTo(clip.x_max, clip.y_min + (i + o));
1547 DrawTo(clip.x_max, clip.y_min + (i - o));
1550 if (x < m_layout->pagesX - 1 || y > 0) {
1551 MoveTo(clip.x_max, clip.y_min + tsize);
1552 DrawTo(clip.x_max, clip.y_min);
1553 fAtCorner = fTrue;
1554 } else {
1555 fAtCorner = fFalse;
1559 if (y == 0 && m_layout->Border) {
1560 /* solid bottom border */
1561 if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1562 DrawTo(clip.x_min, clip.y_min);
1563 } else {
1564 if (y > 0 || x < m_layout->pagesX - 1) {
1565 if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1566 DrawTo(clip.x_max - tsize, clip.y_min);
1568 if (s && y > 0 && m_layout->Cutlines) {
1569 /* dashed bottom border */
1570 i = (clip.x_max - clip.x_min) -
1571 (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1572 for ( ; i > tsize; i -= s) {
1573 MoveTo(clip.x_min + (i + o), clip.y_min);
1574 DrawTo(clip.x_min + (i - o), clip.y_min);
1577 if (y > 0 || x > 0) {
1578 MoveTo(clip.x_min + tsize, clip.y_min);
1579 DrawTo(clip.x_min, clip.y_min);
1584 bool
1585 svxPrintout::OnPrintPage(int pageNum) {
1586 GetPageSizePixels(&xpPageWidth, &ypPageDepth);
1587 pdc = GetDC();
1588 pdc->SetBackgroundMode(wxTRANSPARENT);
1589 #ifdef AVEN_PRINT_PREVIEW
1590 if (IsPreview()) {
1591 int dcx, dcy;
1592 pdc->GetSize(&dcx, &dcy);
1593 pdc->SetUserScale((double)dcx / xpPageWidth, (double)dcy / ypPageDepth);
1595 #endif
1597 layout * l = m_layout;
1599 int pwidth, pdepth;
1600 GetPageSizeMM(&pwidth, &pdepth);
1601 l->scX = (double)xpPageWidth / pwidth;
1602 l->scY = (double)ypPageDepth / pdepth;
1603 font_scaling_x = l->scX * (25.4 / 72.0);
1604 font_scaling_y = l->scY * (25.4 / 72.0);
1605 long MarginLeft = m_data->GetMarginTopLeft().x;
1606 long MarginTop = m_data->GetMarginTopLeft().y;
1607 long MarginBottom = m_data->GetMarginBottomRight().y;
1608 long MarginRight = m_data->GetMarginBottomRight().x;
1609 xpPageWidth -= (int)(l->scX * (MarginLeft + MarginRight));
1610 ypPageDepth -= (int)(l->scY * (FOOTER_HEIGHT_MM + MarginBottom + MarginTop));
1611 // xpPageWidth -= 1;
1612 pdepth -= FOOTER_HEIGHT_MM;
1613 x_offset = (long)(l->scX * MarginLeft);
1614 y_offset = (long)(l->scY * MarginTop);
1615 l->PaperWidth = pwidth -= MarginLeft + MarginRight;
1616 l->PaperDepth = pdepth -= MarginTop + MarginBottom;
1619 double SIN = sin(rad(l->rot));
1620 double COS = cos(rad(l->rot));
1621 double SINT = sin(rad(l->tilt));
1622 double COST = cos(rad(l->tilt));
1624 NewPage(pageNum, l->pagesX, l->pagesY);
1626 if (l->Legend && pageNum == (l->pagesY - 1) * l->pagesX + 1) {
1627 SetFont(font_default);
1628 draw_info_box();
1631 pdc->SetClippingRegion(x_offset, y_offset, xpPageWidth + 1, ypPageDepth + 1);
1633 const double Sc = 1000 / l->Scale;
1635 int show_mask = l->get_effective_show_mask();
1636 if (show_mask & LEGS) {
1637 list<traverse>::const_iterator trav = mainfrm->traverses_begin();
1638 list<traverse>::const_iterator tend = mainfrm->traverses_end();
1639 for ( ; trav != tend; ++trav) {
1640 if (trav->isSplay) {
1641 if (!(show_mask & SPLAYS))
1642 continue;
1643 pdc->SetPen(*pen_splay);
1644 } else {
1645 pdc->SetPen(*pen_leg);
1647 vector<PointInfo>::const_iterator pos = trav->begin();
1648 vector<PointInfo>::const_iterator end = trav->end();
1649 for ( ; pos != end; ++pos) {
1650 double x = pos->GetX();
1651 double y = pos->GetY();
1652 double z = pos->GetZ();
1653 double X = x * COS - y * SIN;
1654 double Y = z * COST - (x * SIN + y * COS) * SINT;
1655 long px = (long)((X * Sc + l->xOrg) * l->scX);
1656 long py = (long)((Y * Sc + l->yOrg) * l->scY);
1657 if (pos == trav->begin()) {
1658 MoveTo(px, py);
1659 } else {
1660 DrawTo(px, py);
1666 if ((show_mask & XSECT) &&
1667 (l->tilt == 0.0 || l->tilt == 90.0 || l->tilt == -90.0)) {
1668 pdc->SetPen(*pen_splay);
1669 list<vector<XSect> >::const_iterator trav = mainfrm->tubes_begin();
1670 list<vector<XSect> >::const_iterator tend = mainfrm->tubes_end();
1671 for ( ; trav != tend; ++trav) {
1672 if (l->tilt == 0.0) {
1673 PlotUD(*trav);
1674 } else {
1675 // m_layout.tilt is 90.0 or -90.0 due to check above.
1676 PlotLR(*trav);
1681 if (show_mask & SURF) {
1682 list<traverse>::const_iterator trav = mainfrm->surface_traverses_begin();
1683 list<traverse>::const_iterator tend = mainfrm->surface_traverses_end();
1684 for ( ; trav != tend; ++trav) {
1685 if (trav->isSplay) {
1686 if (!(show_mask & SPLAYS))
1687 continue;
1688 pdc->SetPen(*pen_splay);
1689 } else {
1690 pdc->SetPen(*pen_surface_leg);
1692 vector<PointInfo>::const_iterator pos = trav->begin();
1693 vector<PointInfo>::const_iterator end = trav->end();
1694 for ( ; pos != end; ++pos) {
1695 double x = pos->GetX();
1696 double y = pos->GetY();
1697 double z = pos->GetZ();
1698 double X = x * COS - y * SIN;
1699 double Y = z * COST - (x * SIN + y * COS) * SINT;
1700 long px = (long)((X * Sc + l->xOrg) * l->scX);
1701 long py = (long)((Y * Sc + l->yOrg) * l->scY);
1702 if (pos == trav->begin()) {
1703 MoveTo(px, py);
1704 } else {
1705 DrawTo(px, py);
1711 if (show_mask & (LABELS|STNS)) {
1712 if (show_mask & LABELS) SetFont(font_labels);
1713 list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
1714 while (label != mainfrm->GetLabelsEnd()) {
1715 double px = (*label)->GetX();
1716 double py = (*label)->GetY();
1717 double pz = (*label)->GetZ();
1718 if ((show_mask & SURF) || (*label)->IsUnderground()) {
1719 double X = px * COS - py * SIN;
1720 double Y = pz * COST - (px * SIN + py * COS) * SINT;
1721 long xnew, ynew;
1722 xnew = (long)((X * Sc + l->xOrg) * l->scX);
1723 ynew = (long)((Y * Sc + l->yOrg) * l->scY);
1724 if (show_mask & STNS) {
1725 pdc->SetPen(*pen_cross);
1726 DrawCross(xnew, ynew);
1728 if (show_mask & LABELS) {
1729 pdc->SetTextForeground(colour_labels);
1730 MoveTo(xnew, ynew);
1731 WriteString((*label)->GetText());
1734 ++label;
1738 return true;
1741 void
1742 svxPrintout::GetPageInfo(int *minPage, int *maxPage,
1743 int *pageFrom, int *pageTo)
1745 *minPage = *pageFrom = 1;
1746 *maxPage = *pageTo = m_layout->pages;
1749 bool
1750 svxPrintout::HasPage(int pageNum) {
1751 return (pageNum <= m_layout->pages);
1754 void
1755 svxPrintout::OnBeginPrinting() {
1756 /* Initialise printer routines */
1757 fontsize_labels = 10;
1758 fontsize = 10;
1760 colour_text = colour_labels = *wxBLACK;
1762 wxColour colour_frame, colour_cross, colour_leg, colour_surface_leg;
1763 colour_frame = colour_cross = colour_leg = colour_surface_leg = *wxBLACK;
1765 pen_frame = new wxPen(colour_frame);
1766 pen_cross = new wxPen(colour_cross);
1767 pen_leg = new wxPen(colour_leg);
1768 pen_surface_leg = new wxPen(colour_surface_leg);
1769 pen_splay = new wxPen(wxColour(192, 192, 192));
1771 m_layout->scX = 1;
1772 m_layout->scY = 1;
1774 font_labels = new wxFont(fontsize_labels, wxDEFAULT, wxNORMAL, wxNORMAL,
1775 false, wxString(fontname_labels, wxConvUTF8),
1776 wxFONTENCODING_ISO8859_1);
1777 font_default = new wxFont(fontsize, wxDEFAULT, wxNORMAL, wxNORMAL,
1778 false, wxString(fontname, wxConvUTF8),
1779 wxFONTENCODING_ISO8859_1);
1782 void
1783 svxPrintout::OnEndPrinting() {
1784 delete font_labels;
1785 delete font_default;
1786 delete pen_frame;
1787 delete pen_cross;
1788 delete pen_leg;
1789 delete pen_surface_leg;
1790 delete pen_splay;
1794 svxPrintout::check_intersection(long x_p, long y_p)
1796 #define U 1
1797 #define D 2
1798 #define L 4
1799 #define R 8
1800 int mask_p = 0, mask_t = 0;
1801 if (x_p < 0)
1802 mask_p = L;
1803 else if (x_p > xpPageWidth)
1804 mask_p = R;
1806 if (y_p < 0)
1807 mask_p |= D;
1808 else if (y_p > ypPageDepth)
1809 mask_p |= U;
1811 if (x_t < 0)
1812 mask_t = L;
1813 else if (x_t > xpPageWidth)
1814 mask_t = R;
1816 if (y_t < 0)
1817 mask_t |= D;
1818 else if (y_t > ypPageDepth)
1819 mask_t |= U;
1821 #if 0
1822 /* approximation to correct answer */
1823 return !(mask_t & mask_p);
1824 #else
1825 /* One end of the line is on the page */
1826 if (!mask_t || !mask_p) return 1;
1828 /* whole line is above, left, right, or below page */
1829 if (mask_t & mask_p) return 0;
1831 if (mask_t == 0) mask_t = mask_p;
1832 if (mask_t & U) {
1833 double v = (double)(y_p - ypPageDepth) / (y_p - y_t);
1834 return v >= 0 && v <= 1;
1836 if (mask_t & D) {
1837 double v = (double)y_p / (y_p - y_t);
1838 return v >= 0 && v <= 1;
1840 if (mask_t & R) {
1841 double v = (double)(x_p - xpPageWidth) / (x_p - x_t);
1842 return v >= 0 && v <= 1;
1844 wxASSERT(mask_t & L);
1846 double v = (double)x_p / (x_p - x_t);
1847 return v >= 0 && v <= 1;
1849 #endif
1850 #undef U
1851 #undef D
1852 #undef L
1853 #undef R
1856 void
1857 svxPrintout::MoveTo(long x, long y)
1859 x_t = x_offset + x - clip.x_min;
1860 y_t = y_offset + clip.y_max - y;
1863 void
1864 svxPrintout::DrawTo(long x, long y)
1866 long x_p = x_t, y_p = y_t;
1867 x_t = x_offset + x - clip.x_min;
1868 y_t = y_offset + clip.y_max - y;
1869 if (!scan_for_blank_pages) {
1870 pdc->DrawLine(x_p, y_p, x_t, y_t);
1871 } else {
1872 if (check_intersection(x_p, y_p)) fBlankPage = fFalse;
1876 #define POINTS_PER_INCH 72.0
1877 #define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH)
1878 #define PWX_CROSS_SIZE (int)(2 * m_layout->scX / POINTS_PER_MM)
1880 void
1881 svxPrintout::DrawCross(long x, long y)
1883 if (!scan_for_blank_pages) {
1884 MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1885 DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1886 MoveTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1887 DrawTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1888 MoveTo(x, y);
1889 } else {
1890 if ((x + PWX_CROSS_SIZE > clip.x_min &&
1891 x - PWX_CROSS_SIZE < clip.x_max) ||
1892 (y + PWX_CROSS_SIZE > clip.y_min &&
1893 y - PWX_CROSS_SIZE < clip.y_max)) {
1894 fBlankPage = fFalse;
1899 void
1900 svxPrintout::WriteString(const wxString & s)
1902 double xsc, ysc;
1903 pdc->GetUserScale(&xsc, &ysc);
1904 pdc->SetUserScale(xsc * font_scaling_x, ysc * font_scaling_y);
1905 if (!scan_for_blank_pages) {
1906 pdc->DrawText(s,
1907 long(x_t / font_scaling_x),
1908 long(y_t / font_scaling_y) - pdc->GetCharHeight());
1909 } else {
1910 int w, h;
1911 pdc->GetTextExtent(s, &w, &h);
1912 if ((y_t + h > 0 && y_t - h < clip.y_max - clip.y_min) ||
1913 (x_t < clip.x_max - clip.x_min && x_t + w > 0)) {
1914 fBlankPage = fFalse;
1917 pdc->SetUserScale(xsc, ysc);
1920 void
1921 svxPrintout::DrawEllipse(long x, long y, long r, long R)
1923 if (!scan_for_blank_pages) {
1924 x_t = x_offset + x - clip.x_min;
1925 y_t = y_offset + clip.y_max - y;
1926 const wxBrush & save_brush = pdc->GetBrush();
1927 pdc->SetBrush(*wxTRANSPARENT_BRUSH);
1928 pdc->DrawEllipse(x_t - r, y_t - R, 2 * r, 2 * R);
1929 pdc->SetBrush(save_brush);
1930 } else {
1931 /* No need to check - this is only used in the legend. */
1935 void
1936 svxPrintout::SolidRectangle(long x, long y, long w, long h)
1938 long X = x_offset + x - clip.x_min;
1939 long Y = y_offset + clip.y_max - y;
1940 pdc->SetBrush(*wxBLACK_BRUSH);
1941 pdc->DrawRectangle(X, Y - h, w, h);
1944 void
1945 svxPrintout::NewPage(int pg, int pagesX, int pagesY)
1947 pdc->DestroyClippingRegion();
1949 int x, y;
1950 x = (pg - 1) % pagesX;
1951 y = pagesY - 1 - ((pg - 1) / pagesX);
1953 clip.x_min = (long)x * xpPageWidth;
1954 clip.y_min = (long)y * ypPageDepth;
1955 clip.x_max = clip.x_min + xpPageWidth; /* dm/pcl/ps had -1; */
1956 clip.y_max = clip.y_min + ypPageDepth; /* dm/pcl/ps had -1; */
1958 const int FOOTERS = 4;
1959 wxString footer[FOOTERS];
1960 footer[0] = m_layout->title;
1962 double rot = m_layout->rot;
1963 double tilt = m_layout->tilt;
1964 double scale = m_layout->Scale;
1965 switch (m_layout->view) {
1966 case layout::PLAN:
1967 // TRANSLATORS: Used in the footer of printouts to compactly
1968 // indicate this is a plan view and what the viewing angle is.
1969 // Aven will replace %s with the bearing, and %.0f with the scale.
1971 // This message probably doesn't need translating for most languages.
1972 footer[1].Printf(wmsg(/*↑%s 1:%.0f*/233),
1973 format_angle(ANGLE_FMT, rot).c_str(),
1974 scale);
1975 break;
1976 case layout::ELEV:
1977 // TRANSLATORS: Used in the footer of printouts to compactly
1978 // indicate this is an elevation view and what the viewing angle
1979 // is. Aven will replace the %s codes with the bearings to the
1980 // left and right of the viewer, and %.0f with the scale.
1982 // This message probably doesn't need translating for most languages.
1983 footer[1].Printf(wmsg(/*%s↔%s 1:%.0f*/235),
1984 format_angle(ANGLE_FMT, fmod(rot + 270.0, 360.0)).c_str(),
1985 format_angle(ANGLE_FMT, fmod(rot + 90.0, 360.0)).c_str(),
1986 scale);
1987 break;
1988 case layout::TILT:
1989 // TRANSLATORS: Used in the footer of printouts to compactly
1990 // indicate this is a tilted elevation view and what the viewing
1991 // angles are. Aven will replace the %s codes with the bearings to
1992 // the left and right of the viewer and the angle the view is
1993 // tilted at, and %.0f with the scale.
1995 // This message probably doesn't need translating for most languages.
1996 footer[1].Printf(wmsg(/*%s↔%s ∡%s 1:%.0f*/236),
1997 format_angle(ANGLE_FMT, fmod(rot + 270.0, 360.0)).c_str(),
1998 format_angle(ANGLE_FMT, fmod(rot + 90.0, 360.0)).c_str(),
1999 format_angle(ANGLE2_FMT, tilt).c_str(),
2000 scale);
2001 break;
2002 case layout::EXTELEV:
2003 // TRANSLATORS: Used in the footer of printouts to compactly
2004 // indicate this is an extended elevation view. Aven will replace
2005 // %.0f with the scale.
2007 // Try to keep the translation short (for example, in English we
2008 // use "Extended" not "Extended elevation") - there is limited room
2009 // in the footer, and the details there are mostly to make it easy
2010 // to check that you have corresponding pages from a multiple page
2011 // printout.
2012 footer[1].Printf(wmsg(/*Extended 1:%.0f*/244), scale);
2013 break;
2016 // TRANSLATORS: N/M meaning page N of M in the page footer of a printout.
2017 footer[2].Printf(wmsg(/*%d/%d*/232), pg, m_layout->pagesX * m_layout->pagesY);
2019 wxString datestamp = m_layout->datestamp;
2020 if (!datestamp.empty()) {
2021 // Remove any timezone suffix (e.g. " UTC" or " +1200").
2022 wxChar ch = datestamp[datestamp.size() - 1];
2023 if (ch >= 'A' && ch <= 'Z') {
2024 for (size_t i = datestamp.size() - 1; i; --i) {
2025 ch = datestamp[i];
2026 if (ch < 'A' || ch > 'Z') {
2027 if (ch == ' ') datestamp.resize(i);
2028 break;
2031 } else if (ch >= '0' && ch <= '9') {
2032 for (size_t i = datestamp.size() - 1; i; --i) {
2033 ch = datestamp[i];
2034 if (ch < '0' || ch > '9') {
2035 if ((ch == '-' || ch == '+') && datestamp[--i] == ' ')
2036 datestamp.resize(i);
2037 break;
2042 // Remove any day prefix (e.g. "Mon,").
2043 for (size_t i = 0; i != datestamp.size(); ++i) {
2044 if (datestamp[i] == ',' && i + 1 != datestamp.size()) {
2045 // Also skip a space after the comma.
2046 if (datestamp[i + 1] == ' ') ++i;
2047 datestamp.erase(0, i + 1);
2048 break;
2053 // TRANSLATORS: Used in the footer of printouts to compactly indicate that
2054 // the date which follows is the date that the survey data was processed.
2056 // Aven will replace %s with a string giving the date and time (e.g.
2057 // "2015-06-09 12:40:44").
2058 footer[3].Printf(wmsg(/*Processed: %s*/167), datestamp.c_str());
2060 const wxChar * footer_sep = wxT(" ");
2061 int fontsize_footer = fontsize_labels;
2062 wxFont * font_footer;
2063 font_footer = new wxFont(fontsize_footer, wxDEFAULT, wxNORMAL, wxNORMAL,
2064 false, wxString(fontname_labels, wxConvUTF8),
2065 wxFONTENCODING_UTF8);
2066 font_footer->Scale(font_scaling_x);
2067 SetFont(font_footer);
2068 int w[FOOTERS], ws, h;
2069 pdc->GetTextExtent(footer_sep, &ws, &h);
2070 int wtotal = ws * (FOOTERS - 1);
2071 for (int i = 0; i < FOOTERS; ++i) {
2072 pdc->GetTextExtent(footer[i], &w[i], &h);
2073 wtotal += w[i];
2076 long X = x_offset;
2077 long Y = y_offset + ypPageDepth + (long)(7 * m_layout->scY) - pdc->GetCharHeight();
2079 if (wtotal > xpPageWidth) {
2080 // Rescale the footer so it fits.
2081 double rescale = double(wtotal) / xpPageWidth;
2082 double xsc, ysc;
2083 pdc->GetUserScale(&xsc, &ysc);
2084 pdc->SetUserScale(xsc / rescale, ysc / rescale);
2085 SetFont(font_footer);
2086 wxString fullfooter = footer[0];
2087 for (int i = 1; i < FOOTERS - 1; ++i) {
2088 fullfooter += footer_sep;
2089 fullfooter += footer[i];
2091 pdc->DrawText(fullfooter, X * rescale, Y * rescale);
2092 // Draw final item right aligned to avoid misaligning.
2093 wxRect rect(x_offset * rescale, Y * rescale,
2094 xpPageWidth * rescale, pdc->GetCharHeight() * rescale);
2095 pdc->DrawLabel(footer[FOOTERS - 1], rect, wxALIGN_RIGHT|wxALIGN_TOP);
2096 pdc->SetUserScale(xsc, ysc);
2097 } else {
2098 // Space out the elements of the footer to fill the line.
2099 double extra = double(xpPageWidth - wtotal) / (FOOTERS - 1);
2100 for (int i = 0; i < FOOTERS - 1; ++i) {
2101 pdc->DrawText(footer[i], X + extra * i, Y);
2102 X += ws + w[i];
2104 // Draw final item right aligned to avoid misaligning.
2105 wxRect rect(x_offset, Y, xpPageWidth, pdc->GetCharHeight());
2106 pdc->DrawLabel(footer[FOOTERS - 1], rect, wxALIGN_RIGHT|wxALIGN_TOP);
2108 drawticks((int)(9 * m_layout->scX / POINTS_PER_MM), x, y);
2111 void
2112 svxPrintout::PlotLR(const vector<XSect> & centreline)
2114 assert(centreline.size() > 1);
2115 XSect prev_pt_v;
2116 Vector3 last_right(1.0, 0.0, 0.0);
2118 const double Sc = 1000 / m_layout->Scale;
2119 const double SIN = sin(rad(m_layout->rot));
2120 const double COS = cos(rad(m_layout->rot));
2122 vector<XSect>::const_iterator i = centreline.begin();
2123 vector<XSect>::size_type segment = 0;
2124 while (i != centreline.end()) {
2125 // get the coordinates of this vertex
2126 const XSect & pt_v = *i++;
2128 Vector3 right;
2130 const Vector3 up_v(0.0, 0.0, 1.0);
2132 if (segment == 0) {
2133 assert(i != centreline.end());
2134 // first segment
2136 // get the coordinates of the next vertex
2137 const XSect & next_pt_v = *i;
2139 // calculate vector from this pt to the next one
2140 Vector3 leg_v = next_pt_v - pt_v;
2142 // obtain a vector in the LRUD plane
2143 right = leg_v * up_v;
2144 if (right.magnitude() == 0) {
2145 right = last_right;
2146 } else {
2147 last_right = right;
2149 } else if (segment + 1 == centreline.size()) {
2150 // last segment
2152 // Calculate vector from the previous pt to this one.
2153 Vector3 leg_v = pt_v - prev_pt_v;
2155 // Obtain a horizontal vector in the LRUD plane.
2156 right = leg_v * up_v;
2157 if (right.magnitude() == 0) {
2158 right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
2159 } else {
2160 last_right = right;
2162 } else {
2163 assert(i != centreline.end());
2164 // Intermediate segment.
2166 // Get the coordinates of the next vertex.
2167 const XSect & next_pt_v = *i;
2169 // Calculate vectors from this vertex to the
2170 // next vertex, and from the previous vertex to
2171 // this one.
2172 Vector3 leg1_v = pt_v - prev_pt_v;
2173 Vector3 leg2_v = next_pt_v - pt_v;
2175 // Obtain horizontal vectors perpendicular to
2176 // both legs, then normalise and average to get
2177 // a horizontal bisector.
2178 Vector3 r1 = leg1_v * up_v;
2179 Vector3 r2 = leg2_v * up_v;
2180 r1.normalise();
2181 r2.normalise();
2182 right = r1 + r2;
2183 if (right.magnitude() == 0) {
2184 // This is the "mid-pitch" case...
2185 right = last_right;
2187 last_right = right;
2190 // Scale to unit vectors in the LRUD plane.
2191 right.normalise();
2193 Double l = pt_v.GetL();
2194 Double r = pt_v.GetR();
2196 if (l >= 0 || r >= 0) {
2197 // Get the x and y coordinates of the survey station
2198 double pt_X = pt_v.GetX() * COS - pt_v.GetY() * SIN;
2199 double pt_Y = pt_v.GetX() * SIN + pt_v.GetY() * COS;
2200 long pt_x = (long)((pt_X * Sc + m_layout->xOrg) * m_layout->scX);
2201 long pt_y = (long)((pt_Y * Sc + m_layout->yOrg) * m_layout->scY);
2203 // Calculate dimensions for the right arrow
2204 double COSR = right.GetX();
2205 double SINR = right.GetY();
2206 long CROSS_MAJOR = (COSR + SINR) * PWX_CROSS_SIZE;
2207 long CROSS_MINOR = (COSR - SINR) * PWX_CROSS_SIZE;
2209 if (l >= 0) {
2210 // Get the x and y coordinates of the end of the left arrow
2211 Vector3 p = pt_v - right * l;
2212 double X = p.GetX() * COS - p.GetY() * SIN;
2213 double Y = (p.GetX() * SIN + p.GetY() * COS);
2214 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2215 long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
2217 // Draw the arrow stem
2218 MoveTo(pt_x, pt_y);
2219 DrawTo(x, y);
2221 // Rotate the arrow by the page rotation
2222 long dx1 = (+CROSS_MINOR) * COS - (+CROSS_MAJOR) * SIN;
2223 long dy1 = (+CROSS_MINOR) * SIN + (+CROSS_MAJOR) * COS;
2224 long dx2 = (+CROSS_MAJOR) * COS - (-CROSS_MINOR) * SIN;
2225 long dy2 = (+CROSS_MAJOR) * SIN + (-CROSS_MINOR) * COS;
2227 // Draw the arrow
2228 MoveTo(x + dx1, y + dy1);
2229 DrawTo(x, y);
2230 DrawTo(x + dx2, y + dy2);
2233 if (r >= 0) {
2234 // Get the x and y coordinates of the end of the right arrow
2235 Vector3 p = pt_v + right * r;
2236 double X = p.GetX() * COS - p.GetY() * SIN;
2237 double Y = (p.GetX() * SIN + p.GetY() * COS);
2238 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2239 long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
2241 // Draw the arrow stem
2242 MoveTo(pt_x, pt_y);
2243 DrawTo(x, y);
2245 // Rotate the arrow by the page rotation
2246 long dx1 = (-CROSS_MINOR) * COS - (-CROSS_MAJOR) * SIN;
2247 long dy1 = (-CROSS_MINOR) * SIN + (-CROSS_MAJOR) * COS;
2248 long dx2 = (-CROSS_MAJOR) * COS - (+CROSS_MINOR) * SIN;
2249 long dy2 = (-CROSS_MAJOR) * SIN + (+CROSS_MINOR) * COS;
2251 // Draw the arrow
2252 MoveTo(x + dx1, y + dy1);
2253 DrawTo(x, y);
2254 DrawTo(x + dx2, y + dy2);
2258 prev_pt_v = pt_v;
2260 ++segment;
2264 void
2265 svxPrintout::PlotUD(const vector<XSect> & centreline)
2267 assert(centreline.size() > 1);
2268 const double Sc = 1000 / m_layout->Scale;
2270 vector<XSect>::const_iterator i = centreline.begin();
2271 while (i != centreline.end()) {
2272 // get the coordinates of this vertex
2273 const XSect & pt_v = *i++;
2275 Double u = pt_v.GetU();
2276 Double d = pt_v.GetD();
2278 if (u >= 0 || d >= 0) {
2279 // Get the coordinates of the survey point
2280 Vector3 p = pt_v;
2281 double SIN = sin(rad(m_layout->rot));
2282 double COS = cos(rad(m_layout->rot));
2283 double X = p.GetX() * COS - p.GetY() * SIN;
2284 double Y = p.GetZ();
2285 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2286 long pt_y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scX);
2288 if (u >= 0) {
2289 // Get the y coordinate of the up arrow
2290 long y = (long)(((Y + u) * Sc + m_layout->yOrg) * m_layout->scY);
2292 // Draw the arrow stem
2293 MoveTo(x, pt_y);
2294 DrawTo(x, y);
2296 // Draw the up arrow
2297 MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
2298 DrawTo(x, y);
2299 DrawTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
2302 if (d >= 0) {
2303 // Get the y coordinate of the down arrow
2304 long y = (long)(((Y - d) * Sc + m_layout->yOrg) * m_layout->scY);
2306 // Draw the arrow stem
2307 MoveTo(x, pt_y);
2308 DrawTo(x, y);
2310 // Draw the down arrow
2311 MoveTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
2312 DrawTo(x, y);
2313 DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);