Update for 1.2.31
[survex.git] / src / printing.cc
blobd726b01147b0a754e063ccfaa08610c13ad1384f
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 for (int f = 0; f != 8; ++f) {
952 if ((show_mask & (f & img_FLAG_SURFACE) ? SURF : LEGS) == 0) {
953 // Not showing traverse because of surface/underground status.
954 continue;
956 if ((f & img_FLAG_SPLAY) && (show_mask & SPLAYS) == 0) {
957 // Not showing because it's a splay.
958 continue;
960 list<traverse>::const_iterator trav = mainfrm->traverses_begin(f);
961 list<traverse>::const_iterator tend = mainfrm->traverses_end(f);
962 for ( ; trav != tend; ++trav) {
963 vector<PointInfo>::const_iterator pos = trav->begin();
964 vector<PointInfo>::const_iterator end = trav->end();
965 for ( ; pos != end; ++pos) {
966 double x = pos->GetX();
967 double y = pos->GetY();
968 double z = pos->GetZ();
969 double X = x * COS - y * SIN;
970 if (X > m_layout.xMax) m_layout.xMax = X;
971 if (X < m_layout.xMin) m_layout.xMin = X;
972 double Y = z * COST - (x * SIN + y * COS) * SINT;
973 if (Y > m_layout.yMax) m_layout.yMax = Y;
974 if (Y < m_layout.yMin) m_layout.yMin = Y;
980 if ((show_mask & XSECT) &&
981 (m_layout.tilt == 0.0 || m_layout.tilt == 90.0 || m_layout.tilt == -90.0)) {
982 list<vector<XSect> >::const_iterator trav = mainfrm->tubes_begin();
983 list<vector<XSect> >::const_iterator tend = mainfrm->tubes_end();
984 for ( ; trav != tend; ++trav) {
985 XSect prev_pt_v;
986 Vector3 last_right(1.0, 0.0, 0.0);
988 vector<XSect>::const_iterator i = trav->begin();
989 vector<XSect>::size_type segment = 0;
990 while (i != trav->end()) {
991 // get the coordinates of this vertex
992 const XSect & pt_v = *i++;
993 if (m_layout.tilt == 0.0) {
994 Double u = pt_v.GetU();
995 Double d = pt_v.GetD();
997 if (u >= 0 || d >= 0) {
998 double x = pt_v.GetX();
999 double y = pt_v.GetY();
1000 double z = pt_v.GetZ();
1001 double X = x * COS - y * SIN;
1002 double Y = z * COST - (x * SIN + y * COS) * SINT;
1004 if (X > m_layout.xMax) m_layout.xMax = X;
1005 if (X < m_layout.xMin) m_layout.xMin = X;
1006 double U = Y + max(0.0, pt_v.GetU());
1007 if (U > m_layout.yMax) m_layout.yMax = U;
1008 double D = Y - max(0.0, pt_v.GetD());
1009 if (D < m_layout.yMin) m_layout.yMin = D;
1011 } else {
1012 // More complex, and this duplicates the algorithm from
1013 // PlotLR() - we should try to share that, maybe via a
1014 // template.
1015 Vector3 right;
1017 const Vector3 up_v(0.0, 0.0, 1.0);
1019 if (segment == 0) {
1020 assert(i != trav->end());
1021 // first segment
1023 // get the coordinates of the next vertex
1024 const XSect & next_pt_v = *i;
1026 // calculate vector from this pt to the next one
1027 Vector3 leg_v = next_pt_v - pt_v;
1029 // obtain a vector in the LRUD plane
1030 right = leg_v * up_v;
1031 if (right.magnitude() == 0) {
1032 right = last_right;
1033 } else {
1034 last_right = right;
1036 } else if (segment + 1 == trav->size()) {
1037 // last segment
1039 // Calculate vector from the previous pt to this one.
1040 Vector3 leg_v = pt_v - prev_pt_v;
1042 // Obtain a horizontal vector in the LRUD plane.
1043 right = leg_v * up_v;
1044 if (right.magnitude() == 0) {
1045 right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
1046 } else {
1047 last_right = right;
1049 } else {
1050 assert(i != trav->end());
1051 // Intermediate segment.
1053 // Get the coordinates of the next vertex.
1054 const XSect & next_pt_v = *i;
1056 // Calculate vectors from this vertex to the
1057 // next vertex, and from the previous vertex to
1058 // this one.
1059 Vector3 leg1_v = pt_v - prev_pt_v;
1060 Vector3 leg2_v = next_pt_v - pt_v;
1062 // Obtain horizontal vectors perpendicular to
1063 // both legs, then normalise and average to get
1064 // a horizontal bisector.
1065 Vector3 r1 = leg1_v * up_v;
1066 Vector3 r2 = leg2_v * up_v;
1067 r1.normalise();
1068 r2.normalise();
1069 right = r1 + r2;
1070 if (right.magnitude() == 0) {
1071 // This is the "mid-pitch" case...
1072 right = last_right;
1074 last_right = right;
1077 // Scale to unit vectors in the LRUD plane.
1078 right.normalise();
1080 Double l = pt_v.GetL();
1081 Double r = pt_v.GetR();
1083 if (l >= 0 || r >= 0) {
1084 // Get the x and y coordinates of the survey station
1085 double pt_X = pt_v.GetX() * COS - pt_v.GetY() * SIN;
1086 double pt_Y = pt_v.GetX() * SIN + pt_v.GetY() * COS;
1088 double X, Y;
1089 if (l >= 0) {
1090 // Get the x and y coordinates of the end of the left arrow
1091 Vector3 p = pt_v - right * l;
1092 X = p.GetX() * COS - p.GetY() * SIN;
1093 Y = (p.GetX() * SIN + p.GetY() * COS);
1094 } else {
1095 X = pt_X;
1096 Y = pt_Y;
1098 if (X > m_layout.xMax) m_layout.xMax = X;
1099 if (X < m_layout.xMin) m_layout.xMin = X;
1100 if (Y > m_layout.yMax) m_layout.yMax = Y;
1101 if (Y < m_layout.yMin) m_layout.yMin = Y;
1103 if (r >= 0) {
1104 // Get the x and y coordinates of the end of the right arrow
1105 Vector3 p = pt_v + right * r;
1106 X = p.GetX() * COS - p.GetY() * SIN;
1107 Y = (p.GetX() * SIN + p.GetY() * COS);
1108 } else {
1109 X = pt_X;
1110 Y = pt_Y;
1112 if (X > m_layout.xMax) m_layout.xMax = X;
1113 if (X < m_layout.xMin) m_layout.xMin = X;
1114 if (Y > m_layout.yMax) m_layout.yMax = Y;
1115 if (Y < m_layout.yMin) m_layout.yMin = Y;
1118 prev_pt_v = pt_v;
1120 ++segment;
1126 if (show_mask & (LABELS|STNS)) {
1127 list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
1128 while (label != mainfrm->GetLabelsEnd()) {
1129 double x = (*label)->GetX();
1130 double y = (*label)->GetY();
1131 double z = (*label)->GetZ();
1132 if ((show_mask & SURF) || (*label)->IsUnderground()) {
1133 double X = x * COS - y * SIN;
1134 if (X > m_layout.xMax) m_layout.xMax = X;
1135 if (X < m_layout.xMin) m_layout.xMin = X;
1136 double Y = z * COST - (x * SIN + y * COS) * SINT;
1137 if (Y > m_layout.yMax) m_layout.yMax = Y;
1138 if (Y < m_layout.yMin) m_layout.yMin = Y;
1140 ++label;
1145 static int xpPageWidth, ypPageDepth;
1146 static long x_offset, y_offset;
1147 static int fontsize, fontsize_labels;
1149 /* FIXME: allow the font to be set */
1151 static const char *fontname = "Arial", *fontname_labels = "Arial";
1153 svxPrintout::svxPrintout(MainFrm *mainfrm_, layout *l,
1154 wxPageSetupDialogData *data, const wxString & title)
1155 : wxPrintout(title), font_labels(NULL), font_default(NULL),
1156 scan_for_blank_pages(false)
1158 mainfrm = mainfrm_;
1159 m_layout = l;
1160 m_data = data;
1163 void
1164 svxPrintout::draw_info_box()
1166 layout *l = m_layout;
1167 int boxwidth = 70;
1168 int boxheight = 30;
1170 pdc->SetPen(*pen_frame);
1172 int div = boxwidth;
1173 if (l->view != layout::EXTELEV) {
1174 boxwidth += boxheight;
1175 MOVEMM(div, boxheight);
1176 DRAWMM(div, 0);
1177 MOVEMM(0, 30); DRAWMM(div, 30);
1180 MOVEMM(0, boxheight);
1181 DRAWMM(boxwidth, boxheight);
1182 DRAWMM(boxwidth, 0);
1183 if (!l->Border) {
1184 DRAWMM(0, 0);
1185 DRAWMM(0, boxheight);
1188 MOVEMM(0, 20); DRAWMM(div, 20);
1189 MOVEMM(0, 10); DRAWMM(div, 10);
1191 switch (l->view) {
1192 case layout::PLAN: {
1193 long ax, ay, bx, by, cx, cy, dx, dy;
1195 long xc = boxwidth - boxheight / 2;
1196 long yc = boxheight / 2;
1197 const double RADIUS = boxheight / 3;
1198 DrawEllipse(long(xc * l->scX), long(yc * l->scY),
1199 long(RADIUS * l->scX), long(RADIUS * l->scY));
1201 ax = (long)((xc - (RADIUS - 1) * sin(rad(000.0 + l->rot))) * l->scX);
1202 ay = (long)((yc + (RADIUS - 1) * cos(rad(000.0 + l->rot))) * l->scY);
1203 bx = (long)((xc - RADIUS * 0.5 * sin(rad(180.0 + l->rot))) * l->scX);
1204 by = (long)((yc + RADIUS * 0.5 * cos(rad(180.0 + l->rot))) * l->scY);
1205 cx = (long)((xc - (RADIUS - 1) * sin(rad(160.0 + l->rot))) * l->scX);
1206 cy = (long)((yc + (RADIUS - 1) * cos(rad(160.0 + l->rot))) * l->scY);
1207 dx = (long)((xc - (RADIUS - 1) * sin(rad(200.0 + l->rot))) * l->scX);
1208 dy = (long)((yc + (RADIUS - 1) * cos(rad(200.0 + l->rot))) * l->scY);
1210 MoveTo(ax, ay);
1211 DrawTo(bx, by);
1212 DrawTo(cx, cy);
1213 DrawTo(ax, ay);
1214 DrawTo(dx, dy);
1215 DrawTo(bx, by);
1217 pdc->SetTextForeground(colour_text);
1218 MOVEMM(div + 0.5, boxheight - 5.5);
1219 WriteString(wmsg(/*North*/115));
1221 wxString angle = format_angle(ANGLE_FMT, l->rot);
1222 wxString s;
1223 /* TRANSLATORS: This is used on printouts of plans, with %s replaced by
1224 * something like "123°". The bearing is up the page. */
1225 s.Printf(wmsg(/*Plan view, %s up page*/168), angle.c_str());
1226 MOVEMM(2, 12); WriteString(s);
1227 break;
1229 case layout::ELEV: case layout::TILT: {
1230 const int L = div + 2;
1231 const int R = boxwidth - 2;
1232 const int H = boxheight / 2;
1233 MOVEMM(L, H); DRAWMM(L + 5, H - 3); DRAWMM(L + 3, H); DRAWMM(L + 5, H + 3);
1235 DRAWMM(L, H); DRAWMM(R, H);
1237 DRAWMM(R - 5, H + 3); DRAWMM(R - 3, H); DRAWMM(R - 5, H - 3); DRAWMM(R, H);
1239 MOVEMM((L + R) / 2, H - 2); DRAWMM((L + R) / 2, H + 2);
1241 pdc->SetTextForeground(colour_text);
1242 MOVEMM(div + 2, boxheight - 8);
1243 /* TRANSLATORS: "Elevation on" 020 <-> 200 degrees */
1244 WriteString(wmsg(/*Elevation on*/116));
1246 MOVEMM(L, 2);
1247 WriteString(format_angle(ANGLE_FMT, fmod(l->rot + 270.0, 360.0)));
1248 MOVEMM(R - 10, 2);
1249 WriteString(format_angle(ANGLE_FMT, fmod(l->rot + 90.0, 360.0)));
1251 wxString angle = format_angle(ANGLE_FMT, l->rot);
1252 wxString s;
1253 if (l->view == layout::ELEV) {
1254 /* TRANSLATORS: This is used on printouts of elevations, with %s
1255 * replaced by something like "123°". The bearing is the direction
1256 * we’re looking. */
1257 s.Printf(wmsg(/*Elevation facing %s*/169), angle.c_str());
1258 } else {
1259 wxString a2 = format_angle(ANGLE2_FMT, l->tilt);
1260 /* TRANSLATORS: This is used on printouts of tilted elevations, with
1261 * the first %s replaced by something like "123°", and the second by
1262 * something like "-45°". The bearing is the direction we’re
1263 * looking. */
1264 s.Printf(wmsg(/*Elevation facing %s, tilted %s*/284), angle.c_str(), a2.c_str());
1266 MOVEMM(2, 12); WriteString(s);
1267 break;
1269 case layout::EXTELEV:
1270 pdc->SetTextForeground(colour_text);
1271 MOVEMM(2, 12);
1272 /* TRANSLATORS: This is used on printouts of extended elevations. */
1273 WriteString(wmsg(/*Extended elevation*/191));
1274 break;
1277 MOVEMM(2, boxheight - 8); WriteString(l->title);
1279 MOVEMM(2, 2);
1280 // FIXME: "Original Scale" better?
1281 WriteString(wxString::Format(wmsg(/*Scale*/154) + wxT(" 1:%.0f"),
1282 l->Scale));
1284 /* This used to be a copyright line, but it was occasionally
1285 * mis-interpreted as us claiming copyright on the survey, so let's
1286 * give the website URL instead */
1287 MOVEMM(boxwidth + 2, 2);
1288 WriteString(wxT("Survex " VERSION " - https://survex.com/"));
1290 draw_scale_bar(boxwidth + 10.0, 17.0, l->PaperWidth - boxwidth - 18.0);
1293 /* Draw fancy scale bar with bottom left at (x,y) (both in mm) and at most */
1294 /* MaxLength mm long. The scaling in use is 1:scale */
1295 void
1296 svxPrintout::draw_scale_bar(double x, double y, double MaxLength)
1298 double StepEst, d;
1299 int E, Step, n, c;
1300 wxString buf;
1301 /* Limit scalebar to 20cm to stop people with A0 plotters complaining */
1302 if (MaxLength > 200.0) MaxLength = 200.0;
1304 #define dmin 10.0 /* each division >= dmin mm long */
1305 #define StepMax 5 /* number in steps of at most StepMax (x 10^N) */
1306 #define epsilon (1e-4) /* fudge factor to prevent rounding problems */
1308 E = (int)ceil(log10((dmin * 0.001 * m_layout->Scale) / StepMax));
1309 StepEst = pow(10.0, -(double)E) * (dmin * 0.001) * m_layout->Scale - epsilon;
1311 /* Force labelling to be in multiples of 1, 2, or 5 */
1312 Step = (StepEst <= 1.0 ? 1 : (StepEst <= 2.0 ? 2 : 5));
1314 /* Work out actual length of each scale bar division */
1315 d = Step * pow(10.0, (double)E) / m_layout->Scale * 1000.0;
1317 /* FIXME: Non-metric units here... */
1318 /* Choose appropriate units, s.t. if possible E is >=0 and minimized */
1319 int units;
1320 if (E >= 3) {
1321 E -= 3;
1322 units = /*km*/423;
1323 } else if (E >= 0) {
1324 units = /*m*/424;
1325 } else {
1326 E += 2;
1327 units = /*cm*/425;
1330 buf = wmsg(/*Scale*/154);
1332 /* Add units used - eg. "Scale (10m)" */
1333 double pow10_E = pow(10.0, (double)E);
1334 if (E >= 0) {
1335 buf += wxString::Format(wxT(" (%.f%s)"), pow10_E, wmsg(units).c_str());
1336 } else {
1337 int sf = -(int)floor(E);
1338 buf += wxString::Format(wxT(" (%.*f%s)"), sf, pow10_E, wmsg(units).c_str());
1340 pdc->SetTextForeground(colour_text);
1341 MOVEMM(x, y + 4); WriteString(buf);
1343 /* Work out how many divisions there will be */
1344 n = (int)(MaxLength / d);
1346 pdc->SetPen(*pen_frame);
1348 long Y = long(y * m_layout->scY);
1349 long Y2 = long((y + 3) * m_layout->scY);
1350 long X = long(x * m_layout->scX);
1351 long X2 = long((x + n * d) * m_layout->scX);
1353 /* Draw top of scale bar */
1354 MoveTo(X2, Y2);
1355 DrawTo(X, Y2);
1356 #if 0
1357 DrawTo(X2, Y);
1358 DrawTo(X, Y);
1359 MOVEMM(x + n * d, y); DRAWMM(x, y);
1360 #endif
1361 /* Draw divisions and label them */
1362 for (c = 0; c <= n; c++) {
1363 pdc->SetPen(*pen_frame);
1364 X = long((x + c * d) * m_layout->scX);
1365 MoveTo(X, Y);
1366 DrawTo(X, Y2);
1367 #if 0 // Don't waste toner!
1368 /* Draw a "zebra crossing" scale bar. */
1369 if (c < n && (c & 1) == 0) {
1370 X2 = long((x + (c + 1) * d) * m_layout->scX);
1371 SolidRectangle(X, Y, X2 - X, Y2 - Y);
1373 #endif
1374 buf.Printf(wxT("%d"), c * Step);
1375 pdc->SetTextForeground(colour_text);
1376 MOVEMM(x + c * d - buf.length(), y - 5);
1377 WriteString(buf);
1381 #if 0
1382 void
1383 make_calibration(layout *l) {
1384 img_point pt = { 0.0, 0.0, 0.0 };
1385 l->xMax = l->yMax = 0.1;
1386 l->xMin = l->yMin = 0;
1388 stack(l, img_MOVE, NULL, &pt);
1389 pt.x = 0.1;
1390 stack(l, img_LINE, NULL, &pt);
1391 pt.y = 0.1;
1392 stack(l, img_LINE, NULL, &pt);
1393 pt.x = 0.0;
1394 stack(l, img_LINE, NULL, &pt);
1395 pt.y = 0.0;
1396 stack(l, img_LINE, NULL, &pt);
1397 pt.x = 0.05;
1398 pt.y = 0.001;
1399 stack(l, img_LABEL, "10cm", &pt);
1400 pt.x = 0.001;
1401 pt.y = 0.05;
1402 stack(l, img_LABEL, "10cm", &pt);
1403 l->Scale = 1.0;
1405 #endif
1408 svxPrintout::next_page(int *pstate, char **q, int pageLim)
1410 char *p;
1411 int page;
1412 int c;
1413 p = *q;
1414 if (*pstate > 0) {
1415 /* doing a range */
1416 (*pstate)++;
1417 wxASSERT(*p == '-');
1418 p++;
1419 while (isspace((unsigned char)*p)) p++;
1420 if (sscanf(p, "%u%n", &page, &c) > 0) {
1421 p += c;
1422 } else {
1423 page = pageLim;
1425 if (*pstate > page) goto err;
1426 if (*pstate < page) return *pstate;
1427 *q = p;
1428 *pstate = 0;
1429 return page;
1432 while (isspace((unsigned char)*p) || *p == ',') p++;
1434 if (!*p) return 0; /* done */
1436 if (*p == '-') {
1437 *q = p;
1438 *pstate = 1;
1439 return 1; /* range with initial parameter omitted */
1441 if (sscanf(p, "%u%n", &page, &c) > 0) {
1442 p += c;
1443 while (isspace((unsigned char)*p)) p++;
1444 *q = p;
1445 if (0 < page && page <= pageLim) {
1446 if (*p == '-') *pstate = page; /* range with start */
1447 return page;
1450 err:
1451 *pstate = -1;
1452 return 0;
1455 /* Draws in alignment marks on each page or borders on edge pages */
1456 void
1457 svxPrintout::drawticks(int tsize, int x, int y)
1459 long i;
1460 int s = tsize * 4;
1461 int o = s / 8;
1462 bool fAtCorner = fFalse;
1463 pdc->SetPen(*pen_frame);
1464 if (x == 0 && m_layout->Border) {
1465 /* solid left border */
1466 MoveTo(clip.x_min, clip.y_min);
1467 DrawTo(clip.x_min, clip.y_max);
1468 fAtCorner = fTrue;
1469 } else {
1470 if (x > 0 || y > 0) {
1471 MoveTo(clip.x_min, clip.y_min);
1472 DrawTo(clip.x_min, clip.y_min + tsize);
1474 if (s && x > 0 && m_layout->Cutlines) {
1475 /* dashed left border */
1476 i = (clip.y_max - clip.y_min) -
1477 (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1478 for ( ; i > tsize; i -= s) {
1479 MoveTo(clip.x_min, clip.y_max - (i + o));
1480 DrawTo(clip.x_min, clip.y_max - (i - o));
1483 if (x > 0 || y < m_layout->pagesY - 1) {
1484 MoveTo(clip.x_min, clip.y_max - tsize);
1485 DrawTo(clip.x_min, clip.y_max);
1486 fAtCorner = fTrue;
1490 if (y == m_layout->pagesY - 1 && m_layout->Border) {
1491 /* solid top border */
1492 if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1493 DrawTo(clip.x_max, clip.y_max);
1494 fAtCorner = fTrue;
1495 } else {
1496 if (y < m_layout->pagesY - 1 || x > 0) {
1497 if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1498 DrawTo(clip.x_min + tsize, clip.y_max);
1500 if (s && y < m_layout->pagesY - 1 && m_layout->Cutlines) {
1501 /* dashed top border */
1502 i = (clip.x_max - clip.x_min) -
1503 (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1504 for ( ; i > tsize; i -= s) {
1505 MoveTo(clip.x_max - (i + o), clip.y_max);
1506 DrawTo(clip.x_max - (i - o), clip.y_max);
1509 if (y < m_layout->pagesY - 1 || x < m_layout->pagesX - 1) {
1510 MoveTo(clip.x_max - tsize, clip.y_max);
1511 DrawTo(clip.x_max, clip.y_max);
1512 fAtCorner = fTrue;
1513 } else {
1514 fAtCorner = fFalse;
1518 if (x == m_layout->pagesX - 1 && m_layout->Border) {
1519 /* solid right border */
1520 if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1521 DrawTo(clip.x_max, clip.y_min);
1522 fAtCorner = fTrue;
1523 } else {
1524 if (x < m_layout->pagesX - 1 || y < m_layout->pagesY - 1) {
1525 if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1526 DrawTo(clip.x_max, clip.y_max - tsize);
1528 if (s && x < m_layout->pagesX - 1 && m_layout->Cutlines) {
1529 /* dashed right border */
1530 i = (clip.y_max - clip.y_min) -
1531 (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1532 for ( ; i > tsize; i -= s) {
1533 MoveTo(clip.x_max, clip.y_min + (i + o));
1534 DrawTo(clip.x_max, clip.y_min + (i - o));
1537 if (x < m_layout->pagesX - 1 || y > 0) {
1538 MoveTo(clip.x_max, clip.y_min + tsize);
1539 DrawTo(clip.x_max, clip.y_min);
1540 fAtCorner = fTrue;
1541 } else {
1542 fAtCorner = fFalse;
1546 if (y == 0 && m_layout->Border) {
1547 /* solid bottom border */
1548 if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1549 DrawTo(clip.x_min, clip.y_min);
1550 } else {
1551 if (y > 0 || x < m_layout->pagesX - 1) {
1552 if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1553 DrawTo(clip.x_max - tsize, clip.y_min);
1555 if (s && y > 0 && m_layout->Cutlines) {
1556 /* dashed bottom border */
1557 i = (clip.x_max - clip.x_min) -
1558 (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1559 for ( ; i > tsize; i -= s) {
1560 MoveTo(clip.x_min + (i + o), clip.y_min);
1561 DrawTo(clip.x_min + (i - o), clip.y_min);
1564 if (y > 0 || x > 0) {
1565 MoveTo(clip.x_min + tsize, clip.y_min);
1566 DrawTo(clip.x_min, clip.y_min);
1571 bool
1572 svxPrintout::OnPrintPage(int pageNum) {
1573 GetPageSizePixels(&xpPageWidth, &ypPageDepth);
1574 pdc = GetDC();
1575 pdc->SetBackgroundMode(wxTRANSPARENT);
1576 #ifdef AVEN_PRINT_PREVIEW
1577 if (IsPreview()) {
1578 int dcx, dcy;
1579 pdc->GetSize(&dcx, &dcy);
1580 pdc->SetUserScale((double)dcx / xpPageWidth, (double)dcy / ypPageDepth);
1582 #endif
1584 layout * l = m_layout;
1586 int pwidth, pdepth;
1587 GetPageSizeMM(&pwidth, &pdepth);
1588 l->scX = (double)xpPageWidth / pwidth;
1589 l->scY = (double)ypPageDepth / pdepth;
1590 font_scaling_x = l->scX * (25.4 / 72.0);
1591 font_scaling_y = l->scY * (25.4 / 72.0);
1592 long MarginLeft = m_data->GetMarginTopLeft().x;
1593 long MarginTop = m_data->GetMarginTopLeft().y;
1594 long MarginBottom = m_data->GetMarginBottomRight().y;
1595 long MarginRight = m_data->GetMarginBottomRight().x;
1596 xpPageWidth -= (int)(l->scX * (MarginLeft + MarginRight));
1597 ypPageDepth -= (int)(l->scY * (FOOTER_HEIGHT_MM + MarginBottom + MarginTop));
1598 // xpPageWidth -= 1;
1599 pdepth -= FOOTER_HEIGHT_MM;
1600 x_offset = (long)(l->scX * MarginLeft);
1601 y_offset = (long)(l->scY * MarginTop);
1602 l->PaperWidth = pwidth -= MarginLeft + MarginRight;
1603 l->PaperDepth = pdepth -= MarginTop + MarginBottom;
1606 double SIN = sin(rad(l->rot));
1607 double COS = cos(rad(l->rot));
1608 double SINT = sin(rad(l->tilt));
1609 double COST = cos(rad(l->tilt));
1611 NewPage(pageNum, l->pagesX, l->pagesY);
1613 if (l->Legend && pageNum == (l->pagesY - 1) * l->pagesX + 1) {
1614 SetFont(font_default);
1615 draw_info_box();
1618 pdc->SetClippingRegion(x_offset, y_offset, xpPageWidth + 1, ypPageDepth + 1);
1620 const double Sc = 1000 / l->Scale;
1622 int show_mask = l->get_effective_show_mask();
1623 if (show_mask & (LEGS|SURF)) {
1624 for (int f = 0; f != 8; ++f) {
1625 if ((show_mask & (f & img_FLAG_SURFACE) ? SURF : LEGS) == 0) {
1626 // Not showing traverse because of surface/underground status.
1627 continue;
1629 if ((f & img_FLAG_SPLAY) && (show_mask & SPLAYS) == 0) {
1630 // Not showing because it's a splay.
1631 continue;
1633 if (f & img_FLAG_SPLAY) {
1634 pdc->SetPen(*pen_splay);
1635 } else if (f & img_FLAG_SURFACE) {
1636 pdc->SetPen(*pen_surface_leg);
1637 } else {
1638 pdc->SetPen(*pen_leg);
1640 list<traverse>::const_iterator trav = mainfrm->traverses_begin(f);
1641 list<traverse>::const_iterator tend = mainfrm->traverses_end(f);
1642 for ( ; trav != tend; ++trav) {
1643 vector<PointInfo>::const_iterator pos = trav->begin();
1644 vector<PointInfo>::const_iterator end = trav->end();
1645 for ( ; pos != end; ++pos) {
1646 double x = pos->GetX();
1647 double y = pos->GetY();
1648 double z = pos->GetZ();
1649 double X = x * COS - y * SIN;
1650 double Y = z * COST - (x * SIN + y * COS) * SINT;
1651 long px = (long)((X * Sc + l->xOrg) * l->scX);
1652 long py = (long)((Y * Sc + l->yOrg) * l->scY);
1653 if (pos == trav->begin()) {
1654 MoveTo(px, py);
1655 } else {
1656 DrawTo(px, py);
1663 if ((show_mask & XSECT) &&
1664 (l->tilt == 0.0 || l->tilt == 90.0 || l->tilt == -90.0)) {
1665 pdc->SetPen(*pen_splay);
1666 list<vector<XSect> >::const_iterator trav = mainfrm->tubes_begin();
1667 list<vector<XSect> >::const_iterator tend = mainfrm->tubes_end();
1668 for ( ; trav != tend; ++trav) {
1669 if (l->tilt == 0.0) {
1670 PlotUD(*trav);
1671 } else {
1672 // m_layout.tilt is 90.0 or -90.0 due to check above.
1673 PlotLR(*trav);
1678 if (show_mask & (LABELS|STNS)) {
1679 if (show_mask & LABELS) SetFont(font_labels);
1680 list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
1681 while (label != mainfrm->GetLabelsEnd()) {
1682 double px = (*label)->GetX();
1683 double py = (*label)->GetY();
1684 double pz = (*label)->GetZ();
1685 if ((show_mask & SURF) || (*label)->IsUnderground()) {
1686 double X = px * COS - py * SIN;
1687 double Y = pz * COST - (px * SIN + py * COS) * SINT;
1688 long xnew, ynew;
1689 xnew = (long)((X * Sc + l->xOrg) * l->scX);
1690 ynew = (long)((Y * Sc + l->yOrg) * l->scY);
1691 if (show_mask & STNS) {
1692 pdc->SetPen(*pen_cross);
1693 DrawCross(xnew, ynew);
1695 if (show_mask & LABELS) {
1696 pdc->SetTextForeground(colour_labels);
1697 MoveTo(xnew, ynew);
1698 WriteString((*label)->GetText());
1701 ++label;
1705 return true;
1708 void
1709 svxPrintout::GetPageInfo(int *minPage, int *maxPage,
1710 int *pageFrom, int *pageTo)
1712 *minPage = *pageFrom = 1;
1713 *maxPage = *pageTo = m_layout->pages;
1716 bool
1717 svxPrintout::HasPage(int pageNum) {
1718 return (pageNum <= m_layout->pages);
1721 void
1722 svxPrintout::OnBeginPrinting() {
1723 /* Initialise printer routines */
1724 fontsize_labels = 10;
1725 fontsize = 10;
1727 colour_text = colour_labels = *wxBLACK;
1729 wxColour colour_frame, colour_cross, colour_leg, colour_surface_leg;
1730 colour_frame = colour_cross = colour_leg = colour_surface_leg = *wxBLACK;
1732 pen_frame = new wxPen(colour_frame);
1733 pen_cross = new wxPen(colour_cross);
1734 pen_leg = new wxPen(colour_leg);
1735 pen_surface_leg = new wxPen(colour_surface_leg);
1736 pen_splay = new wxPen(wxColour(192, 192, 192));
1738 m_layout->scX = 1;
1739 m_layout->scY = 1;
1741 font_labels = new wxFont(fontsize_labels, wxDEFAULT, wxNORMAL, wxNORMAL,
1742 false, wxString(fontname_labels, wxConvUTF8),
1743 wxFONTENCODING_ISO8859_1);
1744 font_default = new wxFont(fontsize, wxDEFAULT, wxNORMAL, wxNORMAL,
1745 false, wxString(fontname, wxConvUTF8),
1746 wxFONTENCODING_ISO8859_1);
1749 void
1750 svxPrintout::OnEndPrinting() {
1751 delete font_labels;
1752 delete font_default;
1753 delete pen_frame;
1754 delete pen_cross;
1755 delete pen_leg;
1756 delete pen_surface_leg;
1757 delete pen_splay;
1761 svxPrintout::check_intersection(long x_p, long y_p)
1763 #define U 1
1764 #define D 2
1765 #define L 4
1766 #define R 8
1767 int mask_p = 0, mask_t = 0;
1768 if (x_p < 0)
1769 mask_p = L;
1770 else if (x_p > xpPageWidth)
1771 mask_p = R;
1773 if (y_p < 0)
1774 mask_p |= D;
1775 else if (y_p > ypPageDepth)
1776 mask_p |= U;
1778 if (x_t < 0)
1779 mask_t = L;
1780 else if (x_t > xpPageWidth)
1781 mask_t = R;
1783 if (y_t < 0)
1784 mask_t |= D;
1785 else if (y_t > ypPageDepth)
1786 mask_t |= U;
1788 #if 0
1789 /* approximation to correct answer */
1790 return !(mask_t & mask_p);
1791 #else
1792 /* One end of the line is on the page */
1793 if (!mask_t || !mask_p) return 1;
1795 /* whole line is above, left, right, or below page */
1796 if (mask_t & mask_p) return 0;
1798 if (mask_t == 0) mask_t = mask_p;
1799 if (mask_t & U) {
1800 double v = (double)(y_p - ypPageDepth) / (y_p - y_t);
1801 return v >= 0 && v <= 1;
1803 if (mask_t & D) {
1804 double v = (double)y_p / (y_p - y_t);
1805 return v >= 0 && v <= 1;
1807 if (mask_t & R) {
1808 double v = (double)(x_p - xpPageWidth) / (x_p - x_t);
1809 return v >= 0 && v <= 1;
1811 wxASSERT(mask_t & L);
1813 double v = (double)x_p / (x_p - x_t);
1814 return v >= 0 && v <= 1;
1816 #endif
1817 #undef U
1818 #undef D
1819 #undef L
1820 #undef R
1823 void
1824 svxPrintout::MoveTo(long x, long y)
1826 x_t = x_offset + x - clip.x_min;
1827 y_t = y_offset + clip.y_max - y;
1830 void
1831 svxPrintout::DrawTo(long x, long y)
1833 long x_p = x_t, y_p = y_t;
1834 x_t = x_offset + x - clip.x_min;
1835 y_t = y_offset + clip.y_max - y;
1836 if (!scan_for_blank_pages) {
1837 pdc->DrawLine(x_p, y_p, x_t, y_t);
1838 } else {
1839 if (check_intersection(x_p, y_p)) fBlankPage = fFalse;
1843 #define POINTS_PER_INCH 72.0
1844 #define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH)
1845 #define PWX_CROSS_SIZE (int)(2 * m_layout->scX / POINTS_PER_MM)
1847 void
1848 svxPrintout::DrawCross(long x, long y)
1850 if (!scan_for_blank_pages) {
1851 MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1852 DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1853 MoveTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1854 DrawTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1855 MoveTo(x, y);
1856 } else {
1857 if ((x + PWX_CROSS_SIZE > clip.x_min &&
1858 x - PWX_CROSS_SIZE < clip.x_max) ||
1859 (y + PWX_CROSS_SIZE > clip.y_min &&
1860 y - PWX_CROSS_SIZE < clip.y_max)) {
1861 fBlankPage = fFalse;
1866 void
1867 svxPrintout::WriteString(const wxString & s)
1869 double xsc, ysc;
1870 pdc->GetUserScale(&xsc, &ysc);
1871 pdc->SetUserScale(xsc * font_scaling_x, ysc * font_scaling_y);
1872 if (!scan_for_blank_pages) {
1873 pdc->DrawText(s,
1874 long(x_t / font_scaling_x),
1875 long(y_t / font_scaling_y) - pdc->GetCharHeight());
1876 } else {
1877 int w, h;
1878 pdc->GetTextExtent(s, &w, &h);
1879 if ((y_t + h > 0 && y_t - h < clip.y_max - clip.y_min) ||
1880 (x_t < clip.x_max - clip.x_min && x_t + w > 0)) {
1881 fBlankPage = fFalse;
1884 pdc->SetUserScale(xsc, ysc);
1887 void
1888 svxPrintout::DrawEllipse(long x, long y, long r, long R)
1890 if (!scan_for_blank_pages) {
1891 x_t = x_offset + x - clip.x_min;
1892 y_t = y_offset + clip.y_max - y;
1893 const wxBrush & save_brush = pdc->GetBrush();
1894 pdc->SetBrush(*wxTRANSPARENT_BRUSH);
1895 pdc->DrawEllipse(x_t - r, y_t - R, 2 * r, 2 * R);
1896 pdc->SetBrush(save_brush);
1897 } else {
1898 /* No need to check - this is only used in the legend. */
1902 void
1903 svxPrintout::SolidRectangle(long x, long y, long w, long h)
1905 long X = x_offset + x - clip.x_min;
1906 long Y = y_offset + clip.y_max - y;
1907 pdc->SetBrush(*wxBLACK_BRUSH);
1908 pdc->DrawRectangle(X, Y - h, w, h);
1911 void
1912 svxPrintout::NewPage(int pg, int pagesX, int pagesY)
1914 pdc->DestroyClippingRegion();
1916 int x, y;
1917 x = (pg - 1) % pagesX;
1918 y = pagesY - 1 - ((pg - 1) / pagesX);
1920 clip.x_min = (long)x * xpPageWidth;
1921 clip.y_min = (long)y * ypPageDepth;
1922 clip.x_max = clip.x_min + xpPageWidth; /* dm/pcl/ps had -1; */
1923 clip.y_max = clip.y_min + ypPageDepth; /* dm/pcl/ps had -1; */
1925 const int FOOTERS = 4;
1926 wxString footer[FOOTERS];
1927 footer[0] = m_layout->title;
1929 double rot = m_layout->rot;
1930 double tilt = m_layout->tilt;
1931 double scale = m_layout->Scale;
1932 switch (m_layout->view) {
1933 case layout::PLAN:
1934 // TRANSLATORS: Used in the footer of printouts to compactly
1935 // indicate this is a plan view and what the viewing angle is.
1936 // Aven will replace %s with the bearing, and %.0f with the scale.
1938 // This message probably doesn't need translating for most languages.
1939 footer[1].Printf(wmsg(/*↑%s 1:%.0f*/233),
1940 format_angle(ANGLE_FMT, rot).c_str(),
1941 scale);
1942 break;
1943 case layout::ELEV:
1944 // TRANSLATORS: Used in the footer of printouts to compactly
1945 // indicate this is an elevation view and what the viewing angle
1946 // is. Aven will replace the %s codes with the bearings to the
1947 // left and right of the viewer, and %.0f with the scale.
1949 // This message probably doesn't need translating for most languages.
1950 footer[1].Printf(wmsg(/*%s↔%s 1:%.0f*/235),
1951 format_angle(ANGLE_FMT, fmod(rot + 270.0, 360.0)).c_str(),
1952 format_angle(ANGLE_FMT, fmod(rot + 90.0, 360.0)).c_str(),
1953 scale);
1954 break;
1955 case layout::TILT:
1956 // TRANSLATORS: Used in the footer of printouts to compactly
1957 // indicate this is a tilted elevation view and what the viewing
1958 // angles are. Aven will replace the %s codes with the bearings to
1959 // the left and right of the viewer and the angle the view is
1960 // tilted at, and %.0f with the scale.
1962 // This message probably doesn't need translating for most languages.
1963 footer[1].Printf(wmsg(/*%s↔%s ∡%s 1:%.0f*/236),
1964 format_angle(ANGLE_FMT, fmod(rot + 270.0, 360.0)).c_str(),
1965 format_angle(ANGLE_FMT, fmod(rot + 90.0, 360.0)).c_str(),
1966 format_angle(ANGLE2_FMT, tilt).c_str(),
1967 scale);
1968 break;
1969 case layout::EXTELEV:
1970 // TRANSLATORS: Used in the footer of printouts to compactly
1971 // indicate this is an extended elevation view. Aven will replace
1972 // %.0f with the scale.
1974 // Try to keep the translation short (for example, in English we
1975 // use "Extended" not "Extended elevation") - there is limited room
1976 // in the footer, and the details there are mostly to make it easy
1977 // to check that you have corresponding pages from a multiple page
1978 // printout.
1979 footer[1].Printf(wmsg(/*Extended 1:%.0f*/244), scale);
1980 break;
1983 // TRANSLATORS: N/M meaning page N of M in the page footer of a printout.
1984 footer[2].Printf(wmsg(/*%d/%d*/232), pg, m_layout->pagesX * m_layout->pagesY);
1986 wxString datestamp = m_layout->datestamp;
1987 if (!datestamp.empty()) {
1988 // Remove any timezone suffix (e.g. " UTC" or " +1200").
1989 wxChar ch = datestamp[datestamp.size() - 1];
1990 if (ch >= 'A' && ch <= 'Z') {
1991 for (size_t i = datestamp.size() - 1; i; --i) {
1992 ch = datestamp[i];
1993 if (ch < 'A' || ch > 'Z') {
1994 if (ch == ' ') datestamp.resize(i);
1995 break;
1998 } else if (ch >= '0' && ch <= '9') {
1999 for (size_t i = datestamp.size() - 1; i; --i) {
2000 ch = datestamp[i];
2001 if (ch < '0' || ch > '9') {
2002 if ((ch == '-' || ch == '+') && datestamp[--i] == ' ')
2003 datestamp.resize(i);
2004 break;
2009 // Remove any day prefix (e.g. "Mon,").
2010 for (size_t i = 0; i != datestamp.size(); ++i) {
2011 if (datestamp[i] == ',' && i + 1 != datestamp.size()) {
2012 // Also skip a space after the comma.
2013 if (datestamp[i + 1] == ' ') ++i;
2014 datestamp.erase(0, i + 1);
2015 break;
2020 // TRANSLATORS: Used in the footer of printouts to compactly indicate that
2021 // the date which follows is the date that the survey data was processed.
2023 // Aven will replace %s with a string giving the date and time (e.g.
2024 // "2015-06-09 12:40:44").
2025 footer[3].Printf(wmsg(/*Processed: %s*/167), datestamp.c_str());
2027 const wxChar * footer_sep = wxT(" ");
2028 int fontsize_footer = fontsize_labels;
2029 wxFont * font_footer;
2030 font_footer = new wxFont(fontsize_footer, wxDEFAULT, wxNORMAL, wxNORMAL,
2031 false, wxString(fontname_labels, wxConvUTF8),
2032 wxFONTENCODING_UTF8);
2033 font_footer->Scale(font_scaling_x);
2034 SetFont(font_footer);
2035 int w[FOOTERS], ws, h;
2036 pdc->GetTextExtent(footer_sep, &ws, &h);
2037 int wtotal = ws * (FOOTERS - 1);
2038 for (int i = 0; i < FOOTERS; ++i) {
2039 pdc->GetTextExtent(footer[i], &w[i], &h);
2040 wtotal += w[i];
2043 long X = x_offset;
2044 long Y = y_offset + ypPageDepth + (long)(7 * m_layout->scY) - pdc->GetCharHeight();
2046 if (wtotal > xpPageWidth) {
2047 // Rescale the footer so it fits.
2048 double rescale = double(wtotal) / xpPageWidth;
2049 double xsc, ysc;
2050 pdc->GetUserScale(&xsc, &ysc);
2051 pdc->SetUserScale(xsc / rescale, ysc / rescale);
2052 SetFont(font_footer);
2053 wxString fullfooter = footer[0];
2054 for (int i = 1; i < FOOTERS - 1; ++i) {
2055 fullfooter += footer_sep;
2056 fullfooter += footer[i];
2058 pdc->DrawText(fullfooter, X * rescale, Y * rescale);
2059 // Draw final item right aligned to avoid misaligning.
2060 wxRect rect(x_offset * rescale, Y * rescale,
2061 xpPageWidth * rescale, pdc->GetCharHeight() * rescale);
2062 pdc->DrawLabel(footer[FOOTERS - 1], rect, wxALIGN_RIGHT|wxALIGN_TOP);
2063 pdc->SetUserScale(xsc, ysc);
2064 } else {
2065 // Space out the elements of the footer to fill the line.
2066 double extra = double(xpPageWidth - wtotal) / (FOOTERS - 1);
2067 for (int i = 0; i < FOOTERS - 1; ++i) {
2068 pdc->DrawText(footer[i], X + extra * i, Y);
2069 X += ws + w[i];
2071 // Draw final item right aligned to avoid misaligning.
2072 wxRect rect(x_offset, Y, xpPageWidth, pdc->GetCharHeight());
2073 pdc->DrawLabel(footer[FOOTERS - 1], rect, wxALIGN_RIGHT|wxALIGN_TOP);
2075 drawticks((int)(9 * m_layout->scX / POINTS_PER_MM), x, y);
2078 void
2079 svxPrintout::PlotLR(const vector<XSect> & centreline)
2081 assert(centreline.size() > 1);
2082 XSect prev_pt_v;
2083 Vector3 last_right(1.0, 0.0, 0.0);
2085 const double Sc = 1000 / m_layout->Scale;
2086 const double SIN = sin(rad(m_layout->rot));
2087 const double COS = cos(rad(m_layout->rot));
2089 vector<XSect>::const_iterator i = centreline.begin();
2090 vector<XSect>::size_type segment = 0;
2091 while (i != centreline.end()) {
2092 // get the coordinates of this vertex
2093 const XSect & pt_v = *i++;
2095 Vector3 right;
2097 const Vector3 up_v(0.0, 0.0, 1.0);
2099 if (segment == 0) {
2100 assert(i != centreline.end());
2101 // first segment
2103 // get the coordinates of the next vertex
2104 const XSect & next_pt_v = *i;
2106 // calculate vector from this pt to the next one
2107 Vector3 leg_v = next_pt_v - pt_v;
2109 // obtain a vector in the LRUD plane
2110 right = leg_v * up_v;
2111 if (right.magnitude() == 0) {
2112 right = last_right;
2113 } else {
2114 last_right = right;
2116 } else if (segment + 1 == centreline.size()) {
2117 // last segment
2119 // Calculate vector from the previous pt to this one.
2120 Vector3 leg_v = pt_v - prev_pt_v;
2122 // Obtain a horizontal vector in the LRUD plane.
2123 right = leg_v * up_v;
2124 if (right.magnitude() == 0) {
2125 right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
2126 } else {
2127 last_right = right;
2129 } else {
2130 assert(i != centreline.end());
2131 // Intermediate segment.
2133 // Get the coordinates of the next vertex.
2134 const XSect & next_pt_v = *i;
2136 // Calculate vectors from this vertex to the
2137 // next vertex, and from the previous vertex to
2138 // this one.
2139 Vector3 leg1_v = pt_v - prev_pt_v;
2140 Vector3 leg2_v = next_pt_v - pt_v;
2142 // Obtain horizontal vectors perpendicular to
2143 // both legs, then normalise and average to get
2144 // a horizontal bisector.
2145 Vector3 r1 = leg1_v * up_v;
2146 Vector3 r2 = leg2_v * up_v;
2147 r1.normalise();
2148 r2.normalise();
2149 right = r1 + r2;
2150 if (right.magnitude() == 0) {
2151 // This is the "mid-pitch" case...
2152 right = last_right;
2154 last_right = right;
2157 // Scale to unit vectors in the LRUD plane.
2158 right.normalise();
2160 Double l = pt_v.GetL();
2161 Double r = pt_v.GetR();
2163 if (l >= 0 || r >= 0) {
2164 // Get the x and y coordinates of the survey station
2165 double pt_X = pt_v.GetX() * COS - pt_v.GetY() * SIN;
2166 double pt_Y = pt_v.GetX() * SIN + pt_v.GetY() * COS;
2167 long pt_x = (long)((pt_X * Sc + m_layout->xOrg) * m_layout->scX);
2168 long pt_y = (long)((pt_Y * Sc + m_layout->yOrg) * m_layout->scY);
2170 // Calculate dimensions for the right arrow
2171 double COSR = right.GetX();
2172 double SINR = right.GetY();
2173 long CROSS_MAJOR = (COSR + SINR) * PWX_CROSS_SIZE;
2174 long CROSS_MINOR = (COSR - SINR) * PWX_CROSS_SIZE;
2176 if (l >= 0) {
2177 // Get the x and y coordinates of the end of the left arrow
2178 Vector3 p = pt_v - right * l;
2179 double X = p.GetX() * COS - p.GetY() * SIN;
2180 double Y = (p.GetX() * SIN + p.GetY() * COS);
2181 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2182 long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
2184 // Draw the arrow stem
2185 MoveTo(pt_x, pt_y);
2186 DrawTo(x, y);
2188 // Rotate the arrow by the page rotation
2189 long dx1 = (+CROSS_MINOR) * COS - (+CROSS_MAJOR) * SIN;
2190 long dy1 = (+CROSS_MINOR) * SIN + (+CROSS_MAJOR) * COS;
2191 long dx2 = (+CROSS_MAJOR) * COS - (-CROSS_MINOR) * SIN;
2192 long dy2 = (+CROSS_MAJOR) * SIN + (-CROSS_MINOR) * COS;
2194 // Draw the arrow
2195 MoveTo(x + dx1, y + dy1);
2196 DrawTo(x, y);
2197 DrawTo(x + dx2, y + dy2);
2200 if (r >= 0) {
2201 // Get the x and y coordinates of the end of the right arrow
2202 Vector3 p = pt_v + right * r;
2203 double X = p.GetX() * COS - p.GetY() * SIN;
2204 double Y = (p.GetX() * SIN + p.GetY() * COS);
2205 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2206 long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
2208 // Draw the arrow stem
2209 MoveTo(pt_x, pt_y);
2210 DrawTo(x, y);
2212 // Rotate the arrow by the page rotation
2213 long dx1 = (-CROSS_MINOR) * COS - (-CROSS_MAJOR) * SIN;
2214 long dy1 = (-CROSS_MINOR) * SIN + (-CROSS_MAJOR) * COS;
2215 long dx2 = (-CROSS_MAJOR) * COS - (+CROSS_MINOR) * SIN;
2216 long dy2 = (-CROSS_MAJOR) * SIN + (+CROSS_MINOR) * COS;
2218 // Draw the arrow
2219 MoveTo(x + dx1, y + dy1);
2220 DrawTo(x, y);
2221 DrawTo(x + dx2, y + dy2);
2225 prev_pt_v = pt_v;
2227 ++segment;
2231 void
2232 svxPrintout::PlotUD(const vector<XSect> & centreline)
2234 assert(centreline.size() > 1);
2235 const double Sc = 1000 / m_layout->Scale;
2237 vector<XSect>::const_iterator i = centreline.begin();
2238 while (i != centreline.end()) {
2239 // get the coordinates of this vertex
2240 const XSect & pt_v = *i++;
2242 Double u = pt_v.GetU();
2243 Double d = pt_v.GetD();
2245 if (u >= 0 || d >= 0) {
2246 // Get the coordinates of the survey point
2247 Vector3 p = pt_v;
2248 double SIN = sin(rad(m_layout->rot));
2249 double COS = cos(rad(m_layout->rot));
2250 double X = p.GetX() * COS - p.GetY() * SIN;
2251 double Y = p.GetZ();
2252 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2253 long pt_y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scX);
2255 if (u >= 0) {
2256 // Get the y coordinate of the up arrow
2257 long y = (long)(((Y + u) * Sc + m_layout->yOrg) * m_layout->scY);
2259 // Draw the arrow stem
2260 MoveTo(x, pt_y);
2261 DrawTo(x, y);
2263 // Draw the up arrow
2264 MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
2265 DrawTo(x, y);
2266 DrawTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
2269 if (d >= 0) {
2270 // Get the y coordinate of the down arrow
2271 long y = (long)(((Y - d) * Sc + m_layout->yOrg) * m_layout->scY);
2273 // Draw the arrow stem
2274 MoveTo(x, pt_y);
2275 DrawTo(x, y);
2277 // Draw the down arrow
2278 MoveTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
2279 DrawTo(x, y);
2280 DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);