Show 1 page when "One Page" selected
[survex.git] / src / printing.cc
blobe0ba5caa82c886adc5ff5a315e9cb02be0544169
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|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 svxPrintDlg::svxPrintDlg(MainFrm* mainfrm_, const wxString & filename,
343 const wxString & title, const wxString & cs_proj,
344 const wxString & datestamp, time_t datestamp_numeric,
345 double angle, double tilt_angle,
346 bool labels, bool crosses, bool legs, bool surf,
347 bool splays, bool tubes, bool ents, bool fixes,
348 bool exports, bool printing, bool close_after_)
349 : wxDialog(mainfrm_, -1, wxString(printing ?
350 /* TRANSLATORS: Title of the print
351 * dialog */
352 wmsg(/*Print*/399) :
353 /* TRANSLATORS: Title of the export
354 * dialog */
355 wmsg(/*Export*/383))),
356 m_layout(printing ? wxGetApp().GetPageSetupDialogData() : NULL),
357 m_File(filename), mainfrm(mainfrm_), close_after(close_after_)
359 m_scale = NULL;
360 m_printSize = NULL;
361 m_bearing = NULL;
362 m_tilt = NULL;
363 m_format = NULL;
364 int show_mask = 0;
365 if (labels)
366 show_mask |= LABELS;
367 if (crosses)
368 show_mask |= STNS;
369 if (legs)
370 show_mask |= LEGS;
371 if (surf)
372 show_mask |= SURF;
373 if (splays)
374 show_mask |= SPLAYS;
375 if (tubes)
376 show_mask |= XSECT|WALLS|PASG;
377 if (ents)
378 show_mask |= ENTS;
379 if (fixes)
380 show_mask |= FIXES;
381 if (exports)
382 show_mask |= EXPORTS;
383 m_layout.show_mask = show_mask;
384 m_layout.datestamp = datestamp;
385 m_layout.datestamp_numeric = datestamp_numeric;
386 m_layout.rot = angle;
387 m_layout.title = title;
388 m_layout.cs_proj = cs_proj;
389 if (mainfrm->IsExtendedElevation()) {
390 m_layout.view = layout::EXTELEV;
391 if (m_layout.rot != 0.0 && m_layout.rot != 180.0) m_layout.rot = 0;
392 m_layout.tilt = 0;
393 } else {
394 m_layout.tilt = tilt_angle;
395 if (m_layout.tilt == -90.0) {
396 m_layout.view = layout::PLAN;
397 } else if (m_layout.tilt == 0.0) {
398 m_layout.view = layout::ELEV;
399 } else {
400 m_layout.view = layout::TILT;
404 /* setup our print dialog*/
405 wxBoxSizer* v1 = new wxBoxSizer(wxVERTICAL);
406 wxBoxSizer* h1 = new wxBoxSizer(wxHORIZONTAL); // holds controls
407 /* TRANSLATORS: Used as a label for the surrounding box for the "Bearing"
408 * and "Tilt angle" fields, and the "Plan view" and "Elevation" buttons in
409 * the "what to print/export" dialog. */
410 m_viewbox = new wxStaticBoxSizer(new wxStaticBox(this, -1, wmsg(/*View*/283)), wxVERTICAL);
411 /* TRANSLATORS: Used as a label for the surrounding box for the "survey
412 * legs" "stations" "names" etc checkboxes in the "what to print" dialog.
413 * "Elements" isn’t a good name for this but nothing better has yet come to
414 * mind! */
415 wxBoxSizer* v3 = new wxStaticBoxSizer(new wxStaticBox(this, -1, wmsg(/*Elements*/256)), wxVERTICAL);
416 wxBoxSizer* h2 = new wxBoxSizer(wxHORIZONTAL);
417 wxBoxSizer* h3 = new wxBoxSizer(wxHORIZONTAL); // holds buttons
419 if (!printing) {
420 wxStaticText* label;
421 label = new wxStaticText(this, -1, wxString(wmsg(/*Export format*/410)));
422 const size_t n_formats = sizeof(formats) / sizeof(formats[0]);
423 m_format = new wxChoice(this, svx_FORMAT,
424 wxDefaultPosition, wxDefaultSize,
425 n_formats, formats);
426 unsigned current_format = 0;
427 wxConfigBase * cfg = wxConfigBase::Get();
428 wxString s;
429 if (cfg->Read(wxT("export_format"), &s, wxString())) {
430 for (unsigned i = 0; i != n_formats; ++i) {
431 if (s == formats[i]) {
432 current_format = i;
433 break;
437 m_format->SetSelection(current_format);
438 wxBoxSizer* formatbox = new wxBoxSizer(wxHORIZONTAL);
439 formatbox->Add(label, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
440 formatbox->Add(m_format, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
442 v1->Add(formatbox, 0, wxALIGN_LEFT|wxALL, 0);
445 wxStaticText* label;
446 label = new wxStaticText(this, -1, wxString(wmsg(/*Scale*/154)) + wxT(" 1:"));
447 if (scales[0].empty()) {
448 if (printing) {
449 /* TRANSLATORS: used in the scale drop down selector in the print
450 * dialog the implicit meaning is "choose a suitable scale to fit
451 * the plot on a single page", but we need something shorter */
452 scales[0].assign(wmsg(/*One page*/258));
453 } else {
454 scales[0].assign(wxT("1000"));
457 m_scale = new wxComboBox(this, svx_SCALE, scales[0], wxDefaultPosition,
458 wxDefaultSize, sizeof(scales) / sizeof(scales[0]),
459 scales);
460 m_scalebox = new wxBoxSizer(wxHORIZONTAL);
461 m_scalebox->Add(label, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
462 m_scalebox->Add(m_scale, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
464 m_viewbox->Add(m_scalebox, 0, wxALIGN_LEFT|wxALL, 0);
466 if (printing) {
467 // Make the dummy string wider than any sane value and use that to
468 // fix the width of the control so the sizers allow space for bigger
469 // page layouts.
470 m_printSize = new wxStaticText(this, -1, wxString::Format(wmsg(/*%d pages (%dx%d)*/257), 9604, 98, 98));
471 m_viewbox->Add(m_printSize, 0, wxALIGN_LEFT|wxALL, 5);
474 /* FIXME:
475 * svx_GRID, // double - spacing, default: 100m
476 * svx_TEXT_HEIGHT, // default 0.6
477 * svx_MARKER_SIZE // default 0.8
480 if (m_layout.view != layout::EXTELEV) {
481 wxFlexGridSizer* anglebox = new wxFlexGridSizer(2);
482 wxStaticText * brg_label, * tilt_label;
483 brg_label = new wxStaticText(this, -1, wmsg(/*Bearing*/259));
484 anglebox->Add(brg_label, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxALL, 5);
485 // wSP_WRAP means that you can scroll past 360 to 0, and vice versa.
486 m_bearing = new wxSpinCtrlDouble(this, svx_BEARING, wxEmptyString,
487 wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS|wxSP_WRAP);
488 m_bearing->SetRange(0.0, 360.0);
489 m_bearing->SetDigits(ANGLE_DP);
490 anglebox->Add(m_bearing, 0, wxALIGN_CENTER|wxALL, 5);
491 /* TRANSLATORS: Used in the print dialog: */
492 tilt_label = new wxStaticText(this, -1, wmsg(/*Tilt angle*/263));
493 anglebox->Add(tilt_label, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxALL, 5);
494 m_tilt = new wxSpinCtrlDouble(this, svx_TILT);
495 m_tilt->SetRange(-90.0, 90.0);
496 m_tilt->SetDigits(ANGLE_DP);
497 anglebox->Add(m_tilt, 0, wxALIGN_CENTER|wxALL, 5);
499 m_viewbox->Add(anglebox, 0, wxALIGN_LEFT|wxALL, 0);
501 wxBoxSizer * planelevsizer = new wxBoxSizer(wxHORIZONTAL);
502 planelevsizer->Add(new wxButton(this, svx_PLAN, wmsg(/*P&lan view*/117)),
503 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
504 planelevsizer->Add(new wxButton(this, svx_ELEV, wmsg(/*&Elevation*/285)),
505 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
507 m_viewbox->Add(planelevsizer, 0, wxALIGN_LEFT|wxALL, 5);
510 /* TRANSLATORS: Here a "survey leg" is a set of measurements between two
511 * "survey stations". */
512 v3->Add(new wxCheckBox(this, svx_LEGS, wmsg(/*Underground Survey Legs*/262),
513 wxDefaultPosition, wxDefaultSize, 0,
514 BitValidator(&m_layout.show_mask, LEGS)),
515 0, wxALIGN_LEFT|wxALL, 2);
516 /* TRANSLATORS: Here a "survey leg" is a set of measurements between two
517 * "survey stations". */
518 v3->Add(new wxCheckBox(this, svx_SURFACE, wmsg(/*Sur&face Survey Legs*/403),
519 wxDefaultPosition, wxDefaultSize, 0,
520 BitValidator(&m_layout.show_mask, SURF)),
521 0, wxALIGN_LEFT|wxALL, 2);
522 v3->Add(new wxCheckBox(this, svx_SPLAYS, wmsg(/*Spla&y Legs*/406),
523 wxDefaultPosition, wxDefaultSize, 0,
524 BitValidator(&m_layout.show_mask, SPLAYS)),
525 0, wxALIGN_LEFT|wxALL, 2);
526 v3->Add(new wxCheckBox(this, svx_STATIONS, wmsg(/*Crosses*/261),
527 wxDefaultPosition, wxDefaultSize, 0,
528 BitValidator(&m_layout.show_mask, STNS)),
529 0, wxALIGN_LEFT|wxALL, 2);
530 v3->Add(new wxCheckBox(this, svx_NAMES, wmsg(/*Station Names*/260),
531 wxDefaultPosition, wxDefaultSize, 0,
532 BitValidator(&m_layout.show_mask, LABELS)),
533 0, wxALIGN_LEFT|wxALL, 2);
534 v3->Add(new wxCheckBox(this, svx_ENTS, wmsg(/*Entrances*/418),
535 wxDefaultPosition, wxDefaultSize, 0,
536 BitValidator(&m_layout.show_mask, ENTS)),
537 0, wxALIGN_LEFT|wxALL, 2);
538 v3->Add(new wxCheckBox(this, svx_FIXES, wmsg(/*Fixed Points*/419),
539 wxDefaultPosition, wxDefaultSize, 0,
540 BitValidator(&m_layout.show_mask, FIXES)),
541 0, wxALIGN_LEFT|wxALL, 2);
542 v3->Add(new wxCheckBox(this, svx_EXPORTS, wmsg(/*Exported Stations*/420),
543 wxDefaultPosition, wxDefaultSize, 0,
544 BitValidator(&m_layout.show_mask, EXPORTS)),
545 0, wxALIGN_LEFT|wxALL, 2);
546 v3->Add(new wxCheckBox(this, svx_XSECT, wmsg(/*Cross-sections*/393),
547 wxDefaultPosition, wxDefaultSize, 0,
548 BitValidator(&m_layout.show_mask, XSECT)),
549 0, wxALIGN_LEFT|wxALL, 2);
550 if (!printing) {
551 v3->Add(new wxCheckBox(this, svx_WALLS, wmsg(/*Walls*/394),
552 wxDefaultPosition, wxDefaultSize, 0,
553 BitValidator(&m_layout.show_mask, WALLS)),
554 0, wxALIGN_LEFT|wxALL, 2);
555 // TRANSLATORS: Label for checkbox which controls whether there's a
556 // layer in the exported file (for formats such as DXF and SVG)
557 // containing polygons for the inside of cave passages).
558 v3->Add(new wxCheckBox(this, svx_PASSAGES, wmsg(/*Passages*/395),
559 wxDefaultPosition, wxDefaultSize, 0,
560 BitValidator(&m_layout.show_mask, PASG)),
561 0, wxALIGN_LEFT|wxALL, 2);
562 v3->Add(new wxCheckBox(this, svx_CENTRED, wmsg(/*Origin in centre*/421),
563 wxDefaultPosition, wxDefaultSize, 0,
564 BitValidator(&m_layout.show_mask, CENTRED)),
565 0, wxALIGN_LEFT|wxALL, 2);
566 v3->Add(new wxCheckBox(this, svx_FULLCOORDS, wmsg(/*Full coordinates*/422),
567 wxDefaultPosition, wxDefaultSize, 0,
568 BitValidator(&m_layout.show_mask, FULL_COORDS)),
569 0, wxALIGN_LEFT|wxALL, 2);
571 if (printing) {
572 /* TRANSLATORS: used in the print dialog - controls drawing lines
573 * around each page */
574 v3->Add(new wxCheckBox(this, svx_BORDERS, wmsg(/*Page Borders*/264),
575 wxDefaultPosition, wxDefaultSize, 0,
576 wxGenericValidator(&m_layout.Border)),
577 0, wxALIGN_LEFT|wxALL, 2);
578 /* TRANSLATORS: will be used in the print dialog - check this to print
579 * blank pages (otherwise they’ll be skipped to save paper) */
580 // m_blanks = new wxCheckBox(this, svx_BLANKS, wmsg(/*Blank Pages*/266));
581 // v3->Add(m_blanks, 0, wxALIGN_LEFT|wxALL, 2);
582 /* TRANSLATORS: As in the legend on a map. Used in the print dialog -
583 * controls drawing the box at the lower left with survey name, view
584 * angles, etc */
585 v3->Add(new wxCheckBox(this, svx_LEGEND, wmsg(/*Legend*/265),
586 wxDefaultPosition, wxDefaultSize, 0,
587 wxGenericValidator(&m_layout.Legend)),
588 0, wxALIGN_LEFT|wxALL, 2);
591 h1->Add(v3, 0, wxALIGN_LEFT|wxALL, 5);
592 h1->Add(m_viewbox, 0, wxALIGN_LEFT|wxLEFT, 5);
594 if (!printing) {
595 /* TRANSLATORS: The PROJ library is used to do coordinate
596 * transformations (https://trac.osgeo.org/proj/) - if the .3d file
597 * doesn't contain details of the coordinate projection in use, the
598 * user must specify it here for export formats which need to know it
599 * (e.g. GPX).
601 h2->Add(new wxStaticText(this, svx_PROJ_LABEL, wmsg(/*Coordinate projection*/440)),
602 0, wxLEFT|wxALIGN_CENTRE_VERTICAL, 5);
603 long style = 0;
604 if (!m_layout.cs_proj.empty()) {
605 // If the input file specified the coordinate system, don't let the
606 // user mess with it.
607 style = wxTE_READONLY;
608 } else {
609 #if 0 // FIXME: Is it a good idea to save this?
610 wxConfigBase * cfg = wxConfigBase::Get();
611 wxString input_projection;
612 cfg->Read(wxT("input_projection"), &input_projection);
613 if (!input_projection.empty())
614 proj_edit.SetValue(input_projection);
615 #endif
617 wxTextCtrl * proj_edit = new wxTextCtrl(this, svx_PROJ, m_layout.cs_proj,
618 wxDefaultPosition, wxDefaultSize,
619 style);
620 h2->Add(proj_edit, 1, wxALL|wxEXPAND|wxALIGN_CENTRE_VERTICAL, 5);
621 v1->Add(h2, 0, wxALIGN_LEFT|wxEXPAND, 5);
624 v1->Add(h1, 0, wxALIGN_LEFT|wxALL, 5);
626 // When we enable/disable checkboxes in the export dialog, ideally we'd
627 // like the dialog to resize, but not sure how to achieve that, so we
628 // add a stretchable spacer here so at least the buttons stay in the
629 // lower right corner.
630 v1->AddStretchSpacer();
632 wxButton * but;
633 but = new wxButton(this, wxID_CANCEL);
634 h3->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
635 if (printing) {
636 #ifdef AVEN_PRINT_PREVIEW
637 but = new wxButton(this, wxID_PREVIEW);
638 h3->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
639 but = new wxButton(this, wxID_PRINT);
640 #else
641 but = new wxButton(this, wxID_PRINT, wmsg(/*&Print...*/400));
642 #endif
643 } else {
644 /* TRANSLATORS: The text on the action button in the "Export" settings
645 * dialog */
646 but = new wxButton(this, svx_EXPORT, wmsg(/*&Export...*/230));
648 but->SetDefault();
649 h3->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
650 v1->Add(h3, 0, wxALIGN_RIGHT|wxALL, 5);
652 SetAutoLayout(true);
653 SetSizer(v1);
654 v1->SetSizeHints(this);
656 LayoutToUI();
657 SomethingChanged(0);
660 void
661 svxPrintDlg::OnPrint(wxCommandEvent&) {
662 SomethingChanged(0);
663 TransferDataFromWindow();
664 wxPageSetupDialogData * psdd = wxGetApp().GetPageSetupDialogData();
665 wxPrintDialogData pd(psdd->GetPrintData());
666 wxPrinter pr(&pd);
667 svxPrintout po(mainfrm, &m_layout, psdd, m_File);
668 if (m_layout.SkipBlank) {
669 // FIXME: wx's printing requires a contiguous range of valid page
670 // numbers. To achieve that, we need to run a scan for blank pages
671 // here, so that GetPageInfo() knows what range to return, and so
672 // that OnPrintPage() can map a page number back to where in the
673 // MxN multi-page layout.
674 #if 0
675 po.scan_for_blank_pages = true;
676 for (int page = 1; page <= m_layout->pages; ++page) {
677 po.fBlankPage = fTrue;
678 po.OnPrintPage(page);
679 // FIXME: Do something with po.fBlankPage
681 po.scan_for_blank_pages = false;
682 #endif
684 if (pr.Print(this, &po, true)) {
685 // Close the print dialog if printing succeeded.
686 Destroy();
690 void
691 svxPrintDlg::OnExport(wxCommandEvent&) {
692 UIToLayout();
693 TransferDataFromWindow();
694 wxString leaf;
695 wxFileName::SplitPath(m_File, NULL, NULL, &leaf, NULL, wxPATH_NATIVE);
696 unsigned format_idx = ((wxChoice*)FindWindow(svx_FORMAT))->GetSelection();
697 leaf += wxString::FromUTF8(extension[format_idx]);
699 wxString filespec = wmsg(msg_filetype[format_idx]);
700 filespec += wxT("|*");
701 filespec += wxString::FromUTF8(extension[format_idx]);
702 filespec += wxT("|");
703 filespec += wmsg(/*All files*/208);
704 filespec += wxT("|");
705 filespec += wxFileSelectorDefaultWildcardStr;
707 /* TRANSLATORS: Title of file dialog to choose name and type of exported
708 * file. */
709 wxFileDialog dlg(this, wmsg(/*Export as:*/401), wxString(), leaf,
710 filespec, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
711 if (dlg.ShowModal() == wxID_OK) {
712 wxString input_projection = ((wxTextCtrl*)FindWindow(svx_PROJ))->GetValue();
713 double grid = 100; // metres
714 double text_height = 0.6;
715 double marker_size = 0.8;
717 try {
718 if (!Export(dlg.GetPath(), m_layout.title,
719 m_layout.datestamp, m_layout.datestamp_numeric, mainfrm,
720 m_layout.rot, m_layout.tilt,
721 m_layout.get_effective_show_mask(),
722 export_format(format_idx), input_projection.utf8_str(),
723 grid, text_height, marker_size, m_layout.Scale)) {
724 wxString m = wxString::Format(wmsg(/*Couldn’t write file “%s”*/402).c_str(),
725 m_File.c_str());
726 wxGetApp().ReportError(m);
728 } catch (const wxString & m) {
729 wxGetApp().ReportError(m);
732 Destroy();
735 #ifdef AVEN_PRINT_PREVIEW
736 void
737 svxPrintDlg::OnPreview(wxCommandEvent&) {
738 SomethingChanged(0);
739 TransferDataFromWindow();
740 wxPageSetupDialogData * psdd = wxGetApp().GetPageSetupDialogData();
741 wxPrintDialogData pd(psdd->GetPrintData());
742 wxPrintPreview* pv;
743 pv = new wxPrintPreview(new svxPrintout(mainfrm, &m_layout, psdd, m_File),
744 new svxPrintout(mainfrm, &m_layout, psdd, m_File),
745 &pd);
746 // TRANSLATORS: Title of the print preview dialog
747 wxPreviewFrame *frame = new wxPreviewFrame(pv, mainfrm, wmsg(/*Print Preview*/398));
748 frame->Initialize();
750 // Size preview frame so that all of the controlbar and canvas can be seen
751 // if possible.
752 int w, h;
753 // GetBestSize gives us the width needed to show the whole controlbar.
754 frame->GetBestSize(&w, &h);
755 if (h < w) {
756 // On wxGTK at least, GetBestSize() returns much too small a height.
757 h = w * 6 / 5;
759 // Ensure that we don't make the window bigger than the screen.
760 // Use wxGetClientDisplayRect() so we don't cover the MS Windows
761 // task bar either.
762 wxRect disp = wxGetClientDisplayRect();
763 if (w > disp.GetWidth()) w = disp.GetWidth();
764 if (h > disp.GetHeight()) h = disp.GetHeight();
765 // Centre the window within the "ClientDisplayRect".
766 int x = disp.GetLeft() + (disp.GetWidth() - w) / 2;
767 int y = disp.GetTop() + (disp.GetHeight() - h) / 2;
768 frame->SetSize(x, y, w, h);
770 frame->Show();
772 #endif
774 void
775 svxPrintDlg::OnPlan(wxCommandEvent&) {
776 m_tilt->SetValue(-90.0);
777 SomethingChanged(svx_TILT);
780 void
781 svxPrintDlg::OnElevation(wxCommandEvent&) {
782 m_tilt->SetValue(0.0);
783 SomethingChanged(svx_TILT);
786 void
787 svxPrintDlg::OnPlanUpdate(wxUpdateUIEvent& e) {
788 e.Enable(m_tilt->GetValue() != -90.0);
791 void
792 svxPrintDlg::OnElevationUpdate(wxUpdateUIEvent& e) {
793 e.Enable(m_tilt->GetValue() != 0.0);
796 void
797 svxPrintDlg::OnChangeSpin(wxSpinDoubleEvent& e) {
798 SomethingChanged(e.GetId());
801 void
802 svxPrintDlg::OnChange(wxCommandEvent& e) {
803 SomethingChanged(e.GetId());
806 void
807 svxPrintDlg::OnCancel(wxCommandEvent&) {
808 if (close_after)
809 mainfrm->Close();
810 Destroy();
813 void
814 svxPrintDlg::SomethingChanged(int control_id) {
815 if ((control_id == 0 || control_id == svx_FORMAT) && m_format) {
816 // Update the shown/hidden fields for the newly selected export filter.
817 int new_filter_idx = m_format->GetSelection();
818 if (new_filter_idx != wxNOT_FOUND) {
819 unsigned mask = format_info[new_filter_idx];
820 static const struct { int id; unsigned mask; } controls[] = {
821 { svx_LEGS, LEGS },
822 { svx_SURFACE, SURF },
823 { svx_SPLAYS, SPLAYS },
824 { svx_STATIONS, STNS },
825 { svx_NAMES, LABELS },
826 { svx_XSECT, XSECT },
827 { svx_WALLS, WALLS },
828 { svx_PASSAGES, PASG },
829 { svx_ENTS, ENTS },
830 { svx_FIXES, FIXES },
831 { svx_EXPORTS, EXPORTS },
832 { svx_CENTRED, CENTRED },
833 { svx_FULLCOORDS, FULL_COORDS },
834 { svx_PROJ_LABEL, PROJ },
835 { svx_PROJ, PROJ },
837 static unsigned n_controls = sizeof(controls) / sizeof(controls[0]);
838 for (unsigned i = 0; i != n_controls; ++i) {
839 wxWindow * control = FindWindow(controls[i].id);
840 if (control) control->Show(mask & controls[i].mask);
842 m_scalebox->Show(bool(mask & SCALE));
843 m_viewbox->Show(!bool(mask & EXPORT_3D));
844 GetSizer()->Layout();
845 if (control_id == svx_FORMAT) {
846 wxConfigBase * cfg = wxConfigBase::Get();
847 cfg->Write(wxT("export_format"), formats[new_filter_idx]);
852 UIToLayout();
854 if (m_printSize || m_scale) {
855 // Update the bounding box.
856 RecalcBounds();
858 if (m_scale) {
859 if (!(m_scale->GetValue()).ToDouble(&(m_layout.Scale)) ||
860 m_layout.Scale == 0.0) {
861 m_layout.pick_scale(1, 1);
866 if (m_printSize && m_layout.xMax >= m_layout.xMin) {
867 m_layout.pages_required();
868 m_printSize->SetLabel(wxString::Format(wmsg(/*%d pages (%dx%d)*/257), m_layout.pages, m_layout.pagesX, m_layout.pagesY));
872 void
873 svxPrintDlg::LayoutToUI()
875 // m_blanks->SetValue(m_layout.SkipBlank);
876 if (m_layout.view != layout::EXTELEV) {
877 m_tilt->SetValue(m_layout.tilt);
878 m_bearing->SetValue(m_layout.rot);
881 // Do this last as it causes an OnChange message which calls UIToLayout
882 if (m_scale) {
883 if (m_layout.Scale != 0) {
884 wxString temp;
885 temp << m_layout.Scale;
886 m_scale->SetValue(temp);
887 } else {
888 if (scales[0].empty()) scales[0].assign(wmsg(/*One page*/258));
889 m_scale->SetValue(scales[0]);
894 void
895 svxPrintDlg::UIToLayout()
897 // m_layout.SkipBlank = m_blanks->IsChecked();
899 if (m_layout.view != layout::EXTELEV && m_tilt) {
900 m_layout.tilt = m_tilt->GetValue();
901 if (m_layout.tilt == -90.0) {
902 m_layout.view = layout::PLAN;
903 } else if (m_layout.tilt == 0.0) {
904 m_layout.view = layout::ELEV;
905 } else {
906 m_layout.view = layout::TILT;
909 bool enable_passage_opts = (m_layout.view != layout::TILT);
910 wxWindow * win;
911 win = FindWindow(svx_XSECT);
912 if (win) win->Enable(enable_passage_opts);
913 win = FindWindow(svx_WALLS);
914 if (win) win->Enable(enable_passage_opts);
915 win = FindWindow(svx_PASSAGES);
916 if (win) win->Enable(enable_passage_opts);
918 m_layout.rot = m_bearing->GetValue();
922 void
923 svxPrintDlg::RecalcBounds()
925 m_layout.yMax = m_layout.xMax = -DBL_MAX;
926 m_layout.yMin = m_layout.xMin = DBL_MAX;
928 double SIN = sin(rad(m_layout.rot));
929 double COS = cos(rad(m_layout.rot));
930 double SINT = sin(rad(m_layout.tilt));
931 double COST = cos(rad(m_layout.tilt));
933 int show_mask = m_layout.get_effective_show_mask();
934 if (show_mask & LEGS) {
935 list<traverse>::const_iterator trav = mainfrm->traverses_begin();
936 list<traverse>::const_iterator tend = mainfrm->traverses_end();
937 for ( ; trav != tend; ++trav) {
938 if (trav->isSplay && !(show_mask & SPLAYS))
939 continue;
940 vector<PointInfo>::const_iterator pos = trav->begin();
941 vector<PointInfo>::const_iterator end = trav->end();
942 for ( ; pos != end; ++pos) {
943 double x = pos->GetX();
944 double y = pos->GetY();
945 double z = pos->GetZ();
946 double X = x * COS - y * SIN;
947 if (X > m_layout.xMax) m_layout.xMax = X;
948 if (X < m_layout.xMin) m_layout.xMin = X;
949 double Y = z * COST - (x * SIN + y * COS) * SINT;
950 if (Y > m_layout.yMax) m_layout.yMax = Y;
951 if (Y < m_layout.yMin) m_layout.yMin = Y;
956 if ((show_mask & XSECT) &&
957 (m_layout.tilt == 0.0 || m_layout.tilt == 90.0 || m_layout.tilt == -90.0)) {
958 list<vector<XSect> >::const_iterator trav = mainfrm->tubes_begin();
959 list<vector<XSect> >::const_iterator tend = mainfrm->tubes_end();
960 for ( ; trav != tend; ++trav) {
961 XSect prev_pt_v;
962 Vector3 last_right(1.0, 0.0, 0.0);
964 vector<XSect>::const_iterator i = trav->begin();
965 vector<XSect>::size_type segment = 0;
966 while (i != trav->end()) {
967 // get the coordinates of this vertex
968 const XSect & pt_v = *i++;
969 if (m_layout.tilt == 0.0) {
970 Double u = pt_v.GetU();
971 Double d = pt_v.GetD();
973 if (u >= 0 || d >= 0) {
974 double x = pt_v.GetX();
975 double y = pt_v.GetY();
976 double z = pt_v.GetZ();
977 double X = x * COS - y * SIN;
978 double Y = z * COST - (x * SIN + y * COS) * SINT;
980 if (X > m_layout.xMax) m_layout.xMax = X;
981 if (X < m_layout.xMin) m_layout.xMin = X;
982 double U = Y + max(0.0, pt_v.GetU());
983 if (U > m_layout.yMax) m_layout.yMax = U;
984 double D = Y - max(0.0, pt_v.GetD());
985 if (D < m_layout.yMin) m_layout.yMin = D;
987 } else {
988 // More complex, and this duplicates the algorithm from
989 // PlotLR() - we should try to share that, maybe via a
990 // template.
991 Vector3 right;
993 const Vector3 up_v(0.0, 0.0, 1.0);
995 if (segment == 0) {
996 assert(i != trav->end());
997 // first segment
999 // get the coordinates of the next vertex
1000 const XSect & next_pt_v = *i;
1002 // calculate vector from this pt to the next one
1003 Vector3 leg_v = next_pt_v - pt_v;
1005 // obtain a vector in the LRUD plane
1006 right = leg_v * up_v;
1007 if (right.magnitude() == 0) {
1008 right = last_right;
1009 } else {
1010 last_right = right;
1012 } else if (segment + 1 == trav->size()) {
1013 // last segment
1015 // Calculate vector from the previous pt to this one.
1016 Vector3 leg_v = pt_v - prev_pt_v;
1018 // Obtain a horizontal vector in the LRUD plane.
1019 right = leg_v * up_v;
1020 if (right.magnitude() == 0) {
1021 right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
1022 } else {
1023 last_right = right;
1025 } else {
1026 assert(i != trav->end());
1027 // Intermediate segment.
1029 // Get the coordinates of the next vertex.
1030 const XSect & next_pt_v = *i;
1032 // Calculate vectors from this vertex to the
1033 // next vertex, and from the previous vertex to
1034 // this one.
1035 Vector3 leg1_v = pt_v - prev_pt_v;
1036 Vector3 leg2_v = next_pt_v - pt_v;
1038 // Obtain horizontal vectors perpendicular to
1039 // both legs, then normalise and average to get
1040 // a horizontal bisector.
1041 Vector3 r1 = leg1_v * up_v;
1042 Vector3 r2 = leg2_v * up_v;
1043 r1.normalise();
1044 r2.normalise();
1045 right = r1 + r2;
1046 if (right.magnitude() == 0) {
1047 // This is the "mid-pitch" case...
1048 right = last_right;
1050 last_right = right;
1053 // Scale to unit vectors in the LRUD plane.
1054 right.normalise();
1056 Double l = pt_v.GetL();
1057 Double r = pt_v.GetR();
1059 if (l >= 0 || r >= 0) {
1060 // Get the x and y coordinates of the survey station
1061 double pt_X = pt_v.GetX() * COS - pt_v.GetY() * SIN;
1062 double pt_Y = pt_v.GetX() * SIN + pt_v.GetY() * COS;
1064 double X, Y;
1065 if (l >= 0) {
1066 // Get the x and y coordinates of the end of the left arrow
1067 Vector3 p = pt_v - right * l;
1068 X = p.GetX() * COS - p.GetY() * SIN;
1069 Y = (p.GetX() * SIN + p.GetY() * COS);
1070 } else {
1071 X = pt_X;
1072 Y = pt_Y;
1074 if (X > m_layout.xMax) m_layout.xMax = X;
1075 if (X < m_layout.xMin) m_layout.xMin = X;
1076 if (Y > m_layout.yMax) m_layout.yMax = Y;
1077 if (Y < m_layout.yMin) m_layout.yMin = Y;
1079 if (r >= 0) {
1080 // Get the x and y coordinates of the end of the right arrow
1081 Vector3 p = pt_v + right * r;
1082 X = p.GetX() * COS - p.GetY() * SIN;
1083 Y = (p.GetX() * SIN + p.GetY() * COS);
1084 } else {
1085 X = pt_X;
1086 Y = pt_Y;
1088 if (X > m_layout.xMax) m_layout.xMax = X;
1089 if (X < m_layout.xMin) m_layout.xMin = X;
1090 if (Y > m_layout.yMax) m_layout.yMax = Y;
1091 if (Y < m_layout.yMin) m_layout.yMin = Y;
1094 prev_pt_v = pt_v;
1096 ++segment;
1102 if (show_mask & SURF) {
1103 list<traverse>::const_iterator trav = mainfrm->surface_traverses_begin();
1104 list<traverse>::const_iterator tend = mainfrm->surface_traverses_end();
1105 for ( ; trav != tend; ++trav) {
1106 if (trav->isSplay && !(show_mask & SPLAYS))
1107 continue;
1108 vector<PointInfo>::const_iterator pos = trav->begin();
1109 vector<PointInfo>::const_iterator end = trav->end();
1110 for ( ; pos != end; ++pos) {
1111 double x = pos->GetX();
1112 double y = pos->GetY();
1113 double z = pos->GetZ();
1114 double X = x * COS - y * SIN;
1115 if (X > m_layout.xMax) m_layout.xMax = X;
1116 if (X < m_layout.xMin) m_layout.xMin = X;
1117 double Y = z * COST - (x * SIN + y * COS) * SINT;
1118 if (Y > m_layout.yMax) m_layout.yMax = Y;
1119 if (Y < m_layout.yMin) m_layout.yMin = Y;
1123 if (show_mask & (LABELS|STNS)) {
1124 list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
1125 while (label != mainfrm->GetLabelsEnd()) {
1126 double x = (*label)->GetX();
1127 double y = (*label)->GetY();
1128 double z = (*label)->GetZ();
1129 if ((show_mask & SURF) || (*label)->IsUnderground()) {
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;
1137 ++label;
1142 static int xpPageWidth, ypPageDepth;
1143 static long x_offset, y_offset;
1144 static int fontsize, fontsize_labels;
1146 /* FIXME: allow the font to be set */
1148 static const char *fontname = "Arial", *fontname_labels = "Arial";
1150 svxPrintout::svxPrintout(MainFrm *mainfrm_, layout *l,
1151 wxPageSetupDialogData *data, const wxString & title)
1152 : wxPrintout(title), font_labels(NULL), font_default(NULL),
1153 scan_for_blank_pages(false)
1155 mainfrm = mainfrm_;
1156 m_layout = l;
1157 m_data = data;
1160 void
1161 svxPrintout::draw_info_box()
1163 layout *l = m_layout;
1164 int boxwidth = 70;
1165 int boxheight = 30;
1167 pdc->SetPen(*pen_frame);
1169 int div = boxwidth;
1170 if (l->view != layout::EXTELEV) {
1171 boxwidth += boxheight;
1172 MOVEMM(div, boxheight);
1173 DRAWMM(div, 0);
1174 MOVEMM(0, 30); DRAWMM(div, 30);
1177 MOVEMM(0, boxheight);
1178 DRAWMM(boxwidth, boxheight);
1179 DRAWMM(boxwidth, 0);
1180 if (!l->Border) {
1181 DRAWMM(0, 0);
1182 DRAWMM(0, boxheight);
1185 MOVEMM(0, 20); DRAWMM(div, 20);
1186 MOVEMM(0, 10); DRAWMM(div, 10);
1188 switch (l->view) {
1189 case layout::PLAN: {
1190 long ax, ay, bx, by, cx, cy, dx, dy;
1192 long xc = boxwidth - boxheight / 2;
1193 long yc = boxheight / 2;
1194 const double RADIUS = boxheight / 3;
1195 DrawEllipse(long(xc * l->scX), long(yc * l->scY),
1196 long(RADIUS * l->scX), long(RADIUS * l->scY));
1198 ax = (long)((xc - (RADIUS - 1) * sin(rad(000.0 + l->rot))) * l->scX);
1199 ay = (long)((yc + (RADIUS - 1) * cos(rad(000.0 + l->rot))) * l->scY);
1200 bx = (long)((xc - RADIUS * 0.5 * sin(rad(180.0 + l->rot))) * l->scX);
1201 by = (long)((yc + RADIUS * 0.5 * cos(rad(180.0 + l->rot))) * l->scY);
1202 cx = (long)((xc - (RADIUS - 1) * sin(rad(160.0 + l->rot))) * l->scX);
1203 cy = (long)((yc + (RADIUS - 1) * cos(rad(160.0 + l->rot))) * l->scY);
1204 dx = (long)((xc - (RADIUS - 1) * sin(rad(200.0 + l->rot))) * l->scX);
1205 dy = (long)((yc + (RADIUS - 1) * cos(rad(200.0 + l->rot))) * l->scY);
1207 MoveTo(ax, ay);
1208 DrawTo(bx, by);
1209 DrawTo(cx, cy);
1210 DrawTo(ax, ay);
1211 DrawTo(dx, dy);
1212 DrawTo(bx, by);
1214 pdc->SetTextForeground(colour_text);
1215 MOVEMM(div + 0.5, boxheight - 5.5);
1216 WriteString(wmsg(/*North*/115));
1218 wxString angle = format_angle(ANGLE_FMT, l->rot);
1219 wxString s;
1220 /* TRANSLATORS: This is used on printouts of plans, with %s replaced by
1221 * something like "123°". The bearing is up the page. */
1222 s.Printf(wmsg(/*Plan view, %s up page*/168), angle.c_str());
1223 MOVEMM(2, 12); WriteString(s);
1224 break;
1226 case layout::ELEV: case layout::TILT: {
1227 const int L = div + 2;
1228 const int R = boxwidth - 2;
1229 const int H = boxheight / 2;
1230 MOVEMM(L, H); DRAWMM(L + 5, H - 3); DRAWMM(L + 3, H); DRAWMM(L + 5, H + 3);
1232 DRAWMM(L, H); DRAWMM(R, H);
1234 DRAWMM(R - 5, H + 3); DRAWMM(R - 3, H); DRAWMM(R - 5, H - 3); DRAWMM(R, H);
1236 MOVEMM((L + R) / 2, H - 2); DRAWMM((L + R) / 2, H + 2);
1238 pdc->SetTextForeground(colour_text);
1239 MOVEMM(div + 2, boxheight - 8);
1240 /* TRANSLATORS: "Elevation on" 020 <-> 200 degrees */
1241 WriteString(wmsg(/*Elevation on*/116));
1243 MOVEMM(L, 2);
1244 WriteString(format_angle(ANGLE_FMT, fmod(l->rot + 270.0, 360.0)));
1245 MOVEMM(R - 10, 2);
1246 WriteString(format_angle(ANGLE_FMT, fmod(l->rot + 90.0, 360.0)));
1248 wxString angle = format_angle(ANGLE_FMT, l->rot);
1249 wxString s;
1250 if (l->view == layout::ELEV) {
1251 /* TRANSLATORS: This is used on printouts of elevations, with %s
1252 * replaced by something like "123°". The bearing is the direction
1253 * we’re looking. */
1254 s.Printf(wmsg(/*Elevation facing %s*/169), angle.c_str());
1255 } else {
1256 wxString a2 = format_angle(ANGLE2_FMT, l->tilt);
1257 /* TRANSLATORS: This is used on printouts of tilted elevations, with
1258 * the first %s replaced by something like "123°", and the second by
1259 * something like "-45°". The bearing is the direction we’re
1260 * looking. */
1261 s.Printf(wmsg(/*Elevation facing %s, tilted %s*/284), angle.c_str(), a2.c_str());
1263 MOVEMM(2, 12); WriteString(s);
1264 break;
1266 case layout::EXTELEV:
1267 pdc->SetTextForeground(colour_text);
1268 MOVEMM(2, 12);
1269 /* TRANSLATORS: This is used on printouts of extended elevations. */
1270 WriteString(wmsg(/*Extended elevation*/191));
1271 break;
1274 MOVEMM(2, boxheight - 8); WriteString(l->title);
1276 MOVEMM(2, 2);
1277 // FIXME: "Original Scale" better?
1278 WriteString(wxString::Format(wmsg(/*Scale*/154) + wxT(" 1:%.0f"),
1279 l->Scale));
1281 /* This used to be a copyright line, but it was occasionally
1282 * mis-interpreted as us claiming copyright on the survey, so let's
1283 * give the website URL instead */
1284 MOVEMM(boxwidth + 2, 2);
1285 WriteString(wxT("Survex " VERSION " - https://survex.com/"));
1287 draw_scale_bar(boxwidth + 10.0, 17.0, l->PaperWidth - boxwidth - 18.0);
1290 /* Draw fancy scale bar with bottom left at (x,y) (both in mm) and at most */
1291 /* MaxLength mm long. The scaling in use is 1:scale */
1292 void
1293 svxPrintout::draw_scale_bar(double x, double y, double MaxLength)
1295 double StepEst, d;
1296 int E, Step, n, c;
1297 wxString buf;
1298 /* Limit scalebar to 20cm to stop people with A0 plotters complaining */
1299 if (MaxLength > 200.0) MaxLength = 200.0;
1301 #define dmin 10.0 /* each division >= dmin mm long */
1302 #define StepMax 5 /* number in steps of at most StepMax (x 10^N) */
1303 #define epsilon (1e-4) /* fudge factor to prevent rounding problems */
1305 E = (int)ceil(log10((dmin * 0.001 * m_layout->Scale) / StepMax));
1306 StepEst = pow(10.0, -(double)E) * (dmin * 0.001) * m_layout->Scale - epsilon;
1308 /* Force labelling to be in multiples of 1, 2, or 5 */
1309 Step = (StepEst <= 1.0 ? 1 : (StepEst <= 2.0 ? 2 : 5));
1311 /* Work out actual length of each scale bar division */
1312 d = Step * pow(10.0, (double)E) / m_layout->Scale * 1000.0;
1314 /* FIXME: Non-metric units here... */
1315 /* Choose appropriate units, s.t. if possible E is >=0 and minimized */
1316 int units;
1317 if (E >= 3) {
1318 E -= 3;
1319 units = /*km*/423;
1320 } else if (E >= 0) {
1321 units = /*m*/424;
1322 } else {
1323 E += 2;
1324 units = /*cm*/425;
1327 buf = wmsg(/*Scale*/154);
1329 /* Add units used - eg. "Scale (10m)" */
1330 double pow10_E = pow(10.0, (double)E);
1331 if (E >= 0) {
1332 buf += wxString::Format(wxT(" (%.f%s)"), pow10_E, wmsg(units).c_str());
1333 } else {
1334 int sf = -(int)floor(E);
1335 buf += wxString::Format(wxT(" (%.*f%s)"), sf, pow10_E, wmsg(units).c_str());
1337 pdc->SetTextForeground(colour_text);
1338 MOVEMM(x, y + 4); WriteString(buf);
1340 /* Work out how many divisions there will be */
1341 n = (int)(MaxLength / d);
1343 pdc->SetPen(*pen_frame);
1345 long Y = long(y * m_layout->scY);
1346 long Y2 = long((y + 3) * m_layout->scY);
1347 long X = long(x * m_layout->scX);
1348 long X2 = long((x + n * d) * m_layout->scX);
1350 /* Draw top of scale bar */
1351 MoveTo(X2, Y2);
1352 DrawTo(X, Y2);
1353 #if 0
1354 DrawTo(X2, Y);
1355 DrawTo(X, Y);
1356 MOVEMM(x + n * d, y); DRAWMM(x, y);
1357 #endif
1358 /* Draw divisions and label them */
1359 for (c = 0; c <= n; c++) {
1360 pdc->SetPen(*pen_frame);
1361 X = long((x + c * d) * m_layout->scX);
1362 MoveTo(X, Y);
1363 DrawTo(X, Y2);
1364 #if 0 // Don't waste toner!
1365 /* Draw a "zebra crossing" scale bar. */
1366 if (c < n && (c & 1) == 0) {
1367 X2 = long((x + (c + 1) * d) * m_layout->scX);
1368 SolidRectangle(X, Y, X2 - X, Y2 - Y);
1370 #endif
1371 buf.Printf(wxT("%d"), c * Step);
1372 pdc->SetTextForeground(colour_text);
1373 MOVEMM(x + c * d - buf.length(), y - 5);
1374 WriteString(buf);
1378 #if 0
1379 void
1380 make_calibration(layout *l) {
1381 img_point pt = { 0.0, 0.0, 0.0 };
1382 l->xMax = l->yMax = 0.1;
1383 l->xMin = l->yMin = 0;
1385 stack(l, img_MOVE, NULL, &pt);
1386 pt.x = 0.1;
1387 stack(l, img_LINE, NULL, &pt);
1388 pt.y = 0.1;
1389 stack(l, img_LINE, NULL, &pt);
1390 pt.x = 0.0;
1391 stack(l, img_LINE, NULL, &pt);
1392 pt.y = 0.0;
1393 stack(l, img_LINE, NULL, &pt);
1394 pt.x = 0.05;
1395 pt.y = 0.001;
1396 stack(l, img_LABEL, "10cm", &pt);
1397 pt.x = 0.001;
1398 pt.y = 0.05;
1399 stack(l, img_LABEL, "10cm", &pt);
1400 l->Scale = 1.0;
1402 #endif
1405 svxPrintout::next_page(int *pstate, char **q, int pageLim)
1407 char *p;
1408 int page;
1409 int c;
1410 p = *q;
1411 if (*pstate > 0) {
1412 /* doing a range */
1413 (*pstate)++;
1414 wxASSERT(*p == '-');
1415 p++;
1416 while (isspace((unsigned char)*p)) p++;
1417 if (sscanf(p, "%u%n", &page, &c) > 0) {
1418 p += c;
1419 } else {
1420 page = pageLim;
1422 if (*pstate > page) goto err;
1423 if (*pstate < page) return *pstate;
1424 *q = p;
1425 *pstate = 0;
1426 return page;
1429 while (isspace((unsigned char)*p) || *p == ',') p++;
1431 if (!*p) return 0; /* done */
1433 if (*p == '-') {
1434 *q = p;
1435 *pstate = 1;
1436 return 1; /* range with initial parameter omitted */
1438 if (sscanf(p, "%u%n", &page, &c) > 0) {
1439 p += c;
1440 while (isspace((unsigned char)*p)) p++;
1441 *q = p;
1442 if (0 < page && page <= pageLim) {
1443 if (*p == '-') *pstate = page; /* range with start */
1444 return page;
1447 err:
1448 *pstate = -1;
1449 return 0;
1452 /* Draws in alignment marks on each page or borders on edge pages */
1453 void
1454 svxPrintout::drawticks(int tsize, int x, int y)
1456 long i;
1457 int s = tsize * 4;
1458 int o = s / 8;
1459 bool fAtCorner = fFalse;
1460 pdc->SetPen(*pen_frame);
1461 if (x == 0 && m_layout->Border) {
1462 /* solid left border */
1463 MoveTo(clip.x_min, clip.y_min);
1464 DrawTo(clip.x_min, clip.y_max);
1465 fAtCorner = fTrue;
1466 } else {
1467 if (x > 0 || y > 0) {
1468 MoveTo(clip.x_min, clip.y_min);
1469 DrawTo(clip.x_min, clip.y_min + tsize);
1471 if (s && x > 0 && m_layout->Cutlines) {
1472 /* dashed left border */
1473 i = (clip.y_max - clip.y_min) -
1474 (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1475 for ( ; i > tsize; i -= s) {
1476 MoveTo(clip.x_min, clip.y_max - (i + o));
1477 DrawTo(clip.x_min, clip.y_max - (i - o));
1480 if (x > 0 || y < m_layout->pagesY - 1) {
1481 MoveTo(clip.x_min, clip.y_max - tsize);
1482 DrawTo(clip.x_min, clip.y_max);
1483 fAtCorner = fTrue;
1487 if (y == m_layout->pagesY - 1 && m_layout->Border) {
1488 /* solid top border */
1489 if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1490 DrawTo(clip.x_max, clip.y_max);
1491 fAtCorner = fTrue;
1492 } else {
1493 if (y < m_layout->pagesY - 1 || x > 0) {
1494 if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1495 DrawTo(clip.x_min + tsize, clip.y_max);
1497 if (s && y < m_layout->pagesY - 1 && m_layout->Cutlines) {
1498 /* dashed top border */
1499 i = (clip.x_max - clip.x_min) -
1500 (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1501 for ( ; i > tsize; i -= s) {
1502 MoveTo(clip.x_max - (i + o), clip.y_max);
1503 DrawTo(clip.x_max - (i - o), clip.y_max);
1506 if (y < m_layout->pagesY - 1 || x < m_layout->pagesX - 1) {
1507 MoveTo(clip.x_max - tsize, clip.y_max);
1508 DrawTo(clip.x_max, clip.y_max);
1509 fAtCorner = fTrue;
1510 } else {
1511 fAtCorner = fFalse;
1515 if (x == m_layout->pagesX - 1 && m_layout->Border) {
1516 /* solid right border */
1517 if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1518 DrawTo(clip.x_max, clip.y_min);
1519 fAtCorner = fTrue;
1520 } else {
1521 if (x < m_layout->pagesX - 1 || y < m_layout->pagesY - 1) {
1522 if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1523 DrawTo(clip.x_max, clip.y_max - tsize);
1525 if (s && x < m_layout->pagesX - 1 && m_layout->Cutlines) {
1526 /* dashed right border */
1527 i = (clip.y_max - clip.y_min) -
1528 (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1529 for ( ; i > tsize; i -= s) {
1530 MoveTo(clip.x_max, clip.y_min + (i + o));
1531 DrawTo(clip.x_max, clip.y_min + (i - o));
1534 if (x < m_layout->pagesX - 1 || y > 0) {
1535 MoveTo(clip.x_max, clip.y_min + tsize);
1536 DrawTo(clip.x_max, clip.y_min);
1537 fAtCorner = fTrue;
1538 } else {
1539 fAtCorner = fFalse;
1543 if (y == 0 && m_layout->Border) {
1544 /* solid bottom border */
1545 if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1546 DrawTo(clip.x_min, clip.y_min);
1547 } else {
1548 if (y > 0 || x < m_layout->pagesX - 1) {
1549 if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1550 DrawTo(clip.x_max - tsize, clip.y_min);
1552 if (s && y > 0 && m_layout->Cutlines) {
1553 /* dashed bottom border */
1554 i = (clip.x_max - clip.x_min) -
1555 (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1556 for ( ; i > tsize; i -= s) {
1557 MoveTo(clip.x_min + (i + o), clip.y_min);
1558 DrawTo(clip.x_min + (i - o), clip.y_min);
1561 if (y > 0 || x > 0) {
1562 MoveTo(clip.x_min + tsize, clip.y_min);
1563 DrawTo(clip.x_min, clip.y_min);
1568 bool
1569 svxPrintout::OnPrintPage(int pageNum) {
1570 GetPageSizePixels(&xpPageWidth, &ypPageDepth);
1571 pdc = GetDC();
1572 pdc->SetBackgroundMode(wxTRANSPARENT);
1573 #ifdef AVEN_PRINT_PREVIEW
1574 if (IsPreview()) {
1575 int dcx, dcy;
1576 pdc->GetSize(&dcx, &dcy);
1577 pdc->SetUserScale((double)dcx / xpPageWidth, (double)dcy / ypPageDepth);
1579 #endif
1581 layout * l = m_layout;
1583 int pwidth, pdepth;
1584 GetPageSizeMM(&pwidth, &pdepth);
1585 l->scX = (double)xpPageWidth / pwidth;
1586 l->scY = (double)ypPageDepth / pdepth;
1587 font_scaling_x = l->scX * (25.4 / 72.0);
1588 font_scaling_y = l->scY * (25.4 / 72.0);
1589 long MarginLeft = m_data->GetMarginTopLeft().x;
1590 long MarginTop = m_data->GetMarginTopLeft().y;
1591 long MarginBottom = m_data->GetMarginBottomRight().y;
1592 long MarginRight = m_data->GetMarginBottomRight().x;
1593 xpPageWidth -= (int)(l->scX * (MarginLeft + MarginRight));
1594 ypPageDepth -= (int)(l->scY * (FOOTER_HEIGHT_MM + MarginBottom + MarginTop));
1595 // xpPageWidth -= 1;
1596 pdepth -= FOOTER_HEIGHT_MM;
1597 x_offset = (long)(l->scX * MarginLeft);
1598 y_offset = (long)(l->scY * MarginTop);
1599 l->PaperWidth = pwidth -= MarginLeft + MarginRight;
1600 l->PaperDepth = pdepth -= MarginTop + MarginBottom;
1603 double SIN = sin(rad(l->rot));
1604 double COS = cos(rad(l->rot));
1605 double SINT = sin(rad(l->tilt));
1606 double COST = cos(rad(l->tilt));
1608 NewPage(pageNum, l->pagesX, l->pagesY);
1610 if (l->Legend && pageNum == (l->pagesY - 1) * l->pagesX + 1) {
1611 SetFont(font_default);
1612 draw_info_box();
1615 pdc->SetClippingRegion(x_offset, y_offset, xpPageWidth + 1, ypPageDepth + 1);
1617 const double Sc = 1000 / l->Scale;
1619 int show_mask = l->get_effective_show_mask();
1620 if (show_mask & LEGS) {
1621 list<traverse>::const_iterator trav = mainfrm->traverses_begin();
1622 list<traverse>::const_iterator tend = mainfrm->traverses_end();
1623 for ( ; trav != tend; ++trav) {
1624 if (trav->isSplay) {
1625 if (!(show_mask & SPLAYS))
1626 continue;
1627 pdc->SetPen(*pen_splay);
1628 } else {
1629 pdc->SetPen(*pen_leg);
1631 vector<PointInfo>::const_iterator pos = trav->begin();
1632 vector<PointInfo>::const_iterator end = trav->end();
1633 for ( ; pos != end; ++pos) {
1634 double x = pos->GetX();
1635 double y = pos->GetY();
1636 double z = pos->GetZ();
1637 double X = x * COS - y * SIN;
1638 double Y = z * COST - (x * SIN + y * COS) * SINT;
1639 long px = (long)((X * Sc + l->xOrg) * l->scX);
1640 long py = (long)((Y * Sc + l->yOrg) * l->scY);
1641 if (pos == trav->begin()) {
1642 MoveTo(px, py);
1643 } else {
1644 DrawTo(px, py);
1650 if ((show_mask & XSECT) &&
1651 (l->tilt == 0.0 || l->tilt == 90.0 || l->tilt == -90.0)) {
1652 pdc->SetPen(*pen_splay);
1653 list<vector<XSect> >::const_iterator trav = mainfrm->tubes_begin();
1654 list<vector<XSect> >::const_iterator tend = mainfrm->tubes_end();
1655 for ( ; trav != tend; ++trav) {
1656 if (l->tilt == 0.0) {
1657 PlotUD(*trav);
1658 } else {
1659 // m_layout.tilt is 90.0 or -90.0 due to check above.
1660 PlotLR(*trav);
1665 if (show_mask & SURF) {
1666 list<traverse>::const_iterator trav = mainfrm->surface_traverses_begin();
1667 list<traverse>::const_iterator tend = mainfrm->surface_traverses_end();
1668 for ( ; trav != tend; ++trav) {
1669 if (trav->isSplay) {
1670 if (!(show_mask & SPLAYS))
1671 continue;
1672 pdc->SetPen(*pen_splay);
1673 } else {
1674 pdc->SetPen(*pen_surface_leg);
1676 vector<PointInfo>::const_iterator pos = trav->begin();
1677 vector<PointInfo>::const_iterator end = trav->end();
1678 for ( ; pos != end; ++pos) {
1679 double x = pos->GetX();
1680 double y = pos->GetY();
1681 double z = pos->GetZ();
1682 double X = x * COS - y * SIN;
1683 double Y = z * COST - (x * SIN + y * COS) * SINT;
1684 long px = (long)((X * Sc + l->xOrg) * l->scX);
1685 long py = (long)((Y * Sc + l->yOrg) * l->scY);
1686 if (pos == trav->begin()) {
1687 MoveTo(px, py);
1688 } else {
1689 DrawTo(px, py);
1695 if (show_mask & (LABELS|STNS)) {
1696 if (show_mask & LABELS) SetFont(font_labels);
1697 list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
1698 while (label != mainfrm->GetLabelsEnd()) {
1699 double px = (*label)->GetX();
1700 double py = (*label)->GetY();
1701 double pz = (*label)->GetZ();
1702 if ((show_mask & SURF) || (*label)->IsUnderground()) {
1703 double X = px * COS - py * SIN;
1704 double Y = pz * COST - (px * SIN + py * COS) * SINT;
1705 long xnew, ynew;
1706 xnew = (long)((X * Sc + l->xOrg) * l->scX);
1707 ynew = (long)((Y * Sc + l->yOrg) * l->scY);
1708 if (show_mask & STNS) {
1709 pdc->SetPen(*pen_cross);
1710 DrawCross(xnew, ynew);
1712 if (show_mask & LABELS) {
1713 pdc->SetTextForeground(colour_labels);
1714 MoveTo(xnew, ynew);
1715 WriteString((*label)->GetText());
1718 ++label;
1722 return true;
1725 void
1726 svxPrintout::GetPageInfo(int *minPage, int *maxPage,
1727 int *pageFrom, int *pageTo)
1729 *minPage = *pageFrom = 1;
1730 *maxPage = *pageTo = m_layout->pages;
1733 bool
1734 svxPrintout::HasPage(int pageNum) {
1735 return (pageNum <= m_layout->pages);
1738 void
1739 svxPrintout::OnBeginPrinting() {
1740 /* Initialise printer routines */
1741 fontsize_labels = 10;
1742 fontsize = 10;
1744 colour_text = colour_labels = *wxBLACK;
1746 wxColour colour_frame, colour_cross, colour_leg, colour_surface_leg;
1747 colour_frame = colour_cross = colour_leg = colour_surface_leg = *wxBLACK;
1749 pen_frame = new wxPen(colour_frame);
1750 pen_cross = new wxPen(colour_cross);
1751 pen_leg = new wxPen(colour_leg);
1752 pen_surface_leg = new wxPen(colour_surface_leg);
1753 pen_splay = new wxPen(wxColour(192, 192, 192));
1755 m_layout->scX = 1;
1756 m_layout->scY = 1;
1758 font_labels = new wxFont(fontsize_labels, wxDEFAULT, wxNORMAL, wxNORMAL,
1759 false, wxString(fontname_labels, wxConvUTF8),
1760 wxFONTENCODING_ISO8859_1);
1761 font_default = new wxFont(fontsize, wxDEFAULT, wxNORMAL, wxNORMAL,
1762 false, wxString(fontname, wxConvUTF8),
1763 wxFONTENCODING_ISO8859_1);
1766 void
1767 svxPrintout::OnEndPrinting() {
1768 delete font_labels;
1769 delete font_default;
1770 delete pen_frame;
1771 delete pen_cross;
1772 delete pen_leg;
1773 delete pen_surface_leg;
1774 delete pen_splay;
1778 svxPrintout::check_intersection(long x_p, long y_p)
1780 #define U 1
1781 #define D 2
1782 #define L 4
1783 #define R 8
1784 int mask_p = 0, mask_t = 0;
1785 if (x_p < 0)
1786 mask_p = L;
1787 else if (x_p > xpPageWidth)
1788 mask_p = R;
1790 if (y_p < 0)
1791 mask_p |= D;
1792 else if (y_p > ypPageDepth)
1793 mask_p |= U;
1795 if (x_t < 0)
1796 mask_t = L;
1797 else if (x_t > xpPageWidth)
1798 mask_t = R;
1800 if (y_t < 0)
1801 mask_t |= D;
1802 else if (y_t > ypPageDepth)
1803 mask_t |= U;
1805 #if 0
1806 /* approximation to correct answer */
1807 return !(mask_t & mask_p);
1808 #else
1809 /* One end of the line is on the page */
1810 if (!mask_t || !mask_p) return 1;
1812 /* whole line is above, left, right, or below page */
1813 if (mask_t & mask_p) return 0;
1815 if (mask_t == 0) mask_t = mask_p;
1816 if (mask_t & U) {
1817 double v = (double)(y_p - ypPageDepth) / (y_p - y_t);
1818 return v >= 0 && v <= 1;
1820 if (mask_t & D) {
1821 double v = (double)y_p / (y_p - y_t);
1822 return v >= 0 && v <= 1;
1824 if (mask_t & R) {
1825 double v = (double)(x_p - xpPageWidth) / (x_p - x_t);
1826 return v >= 0 && v <= 1;
1828 wxASSERT(mask_t & L);
1830 double v = (double)x_p / (x_p - x_t);
1831 return v >= 0 && v <= 1;
1833 #endif
1834 #undef U
1835 #undef D
1836 #undef L
1837 #undef R
1840 void
1841 svxPrintout::MoveTo(long x, long y)
1843 x_t = x_offset + x - clip.x_min;
1844 y_t = y_offset + clip.y_max - y;
1847 void
1848 svxPrintout::DrawTo(long x, long y)
1850 long x_p = x_t, y_p = y_t;
1851 x_t = x_offset + x - clip.x_min;
1852 y_t = y_offset + clip.y_max - y;
1853 if (!scan_for_blank_pages) {
1854 pdc->DrawLine(x_p, y_p, x_t, y_t);
1855 } else {
1856 if (check_intersection(x_p, y_p)) fBlankPage = fFalse;
1860 #define POINTS_PER_INCH 72.0
1861 #define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH)
1862 #define PWX_CROSS_SIZE (int)(2 * m_layout->scX / POINTS_PER_MM)
1864 void
1865 svxPrintout::DrawCross(long x, long y)
1867 if (!scan_for_blank_pages) {
1868 MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1869 DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1870 MoveTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1871 DrawTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1872 MoveTo(x, y);
1873 } else {
1874 if ((x + PWX_CROSS_SIZE > clip.x_min &&
1875 x - PWX_CROSS_SIZE < clip.x_max) ||
1876 (y + PWX_CROSS_SIZE > clip.y_min &&
1877 y - PWX_CROSS_SIZE < clip.y_max)) {
1878 fBlankPage = fFalse;
1883 void
1884 svxPrintout::WriteString(const wxString & s)
1886 double xsc, ysc;
1887 pdc->GetUserScale(&xsc, &ysc);
1888 pdc->SetUserScale(xsc * font_scaling_x, ysc * font_scaling_y);
1889 if (!scan_for_blank_pages) {
1890 pdc->DrawText(s,
1891 long(x_t / font_scaling_x),
1892 long(y_t / font_scaling_y) - pdc->GetCharHeight());
1893 } else {
1894 int w, h;
1895 pdc->GetTextExtent(s, &w, &h);
1896 if ((y_t + h > 0 && y_t - h < clip.y_max - clip.y_min) ||
1897 (x_t < clip.x_max - clip.x_min && x_t + w > 0)) {
1898 fBlankPage = fFalse;
1901 pdc->SetUserScale(xsc, ysc);
1904 void
1905 svxPrintout::DrawEllipse(long x, long y, long r, long R)
1907 if (!scan_for_blank_pages) {
1908 x_t = x_offset + x - clip.x_min;
1909 y_t = y_offset + clip.y_max - y;
1910 const wxBrush & save_brush = pdc->GetBrush();
1911 pdc->SetBrush(*wxTRANSPARENT_BRUSH);
1912 pdc->DrawEllipse(x_t - r, y_t - R, 2 * r, 2 * R);
1913 pdc->SetBrush(save_brush);
1914 } else {
1915 /* No need to check - this is only used in the legend. */
1919 void
1920 svxPrintout::SolidRectangle(long x, long y, long w, long h)
1922 long X = x_offset + x - clip.x_min;
1923 long Y = y_offset + clip.y_max - y;
1924 pdc->SetBrush(*wxBLACK_BRUSH);
1925 pdc->DrawRectangle(X, Y - h, w, h);
1928 void
1929 svxPrintout::NewPage(int pg, int pagesX, int pagesY)
1931 pdc->DestroyClippingRegion();
1933 int x, y;
1934 x = (pg - 1) % pagesX;
1935 y = pagesY - 1 - ((pg - 1) / pagesX);
1937 clip.x_min = (long)x * xpPageWidth;
1938 clip.y_min = (long)y * ypPageDepth;
1939 clip.x_max = clip.x_min + xpPageWidth; /* dm/pcl/ps had -1; */
1940 clip.y_max = clip.y_min + ypPageDepth; /* dm/pcl/ps had -1; */
1942 const int FOOTERS = 4;
1943 wxString footer[FOOTERS];
1944 footer[0] = m_layout->title;
1946 double rot = m_layout->rot;
1947 double tilt = m_layout->tilt;
1948 double scale = m_layout->Scale;
1949 switch (m_layout->view) {
1950 case layout::PLAN:
1951 // TRANSLATORS: Used in the footer of printouts to compactly
1952 // indicate this is a plan view and what the viewing angle is.
1953 // Aven will replace %s with the bearing, and %.0f with the scale.
1955 // This message probably doesn't need translating for most languages.
1956 footer[1].Printf(wmsg(/*↑%s 1:%.0f*/233),
1957 format_angle(ANGLE_FMT, rot).c_str(),
1958 scale);
1959 break;
1960 case layout::ELEV:
1961 // TRANSLATORS: Used in the footer of printouts to compactly
1962 // indicate this is an elevation view and what the viewing angle
1963 // is. Aven will replace the %s codes with the bearings to the
1964 // left and right of the viewer, and %.0f with the scale.
1966 // This message probably doesn't need translating for most languages.
1967 footer[1].Printf(wmsg(/*%s↔%s 1:%.0f*/235),
1968 format_angle(ANGLE_FMT, fmod(rot + 270.0, 360.0)).c_str(),
1969 format_angle(ANGLE_FMT, fmod(rot + 90.0, 360.0)).c_str(),
1970 scale);
1971 break;
1972 case layout::TILT:
1973 // TRANSLATORS: Used in the footer of printouts to compactly
1974 // indicate this is a tilted elevation view and what the viewing
1975 // angles are. Aven will replace the %s codes with the bearings to
1976 // the left and right of the viewer and the angle the view is
1977 // tilted at, and %.0f with the scale.
1979 // This message probably doesn't need translating for most languages.
1980 footer[1].Printf(wmsg(/*%s↔%s ∡%s 1:%.0f*/236),
1981 format_angle(ANGLE_FMT, fmod(rot + 270.0, 360.0)).c_str(),
1982 format_angle(ANGLE_FMT, fmod(rot + 90.0, 360.0)).c_str(),
1983 format_angle(ANGLE2_FMT, tilt).c_str(),
1984 scale);
1985 break;
1986 case layout::EXTELEV:
1987 // TRANSLATORS: Used in the footer of printouts to compactly
1988 // indicate this is an extended elevation view. Aven will replace
1989 // %.0f with the scale.
1991 // Try to keep the translation short (for example, in English we
1992 // use "Extended" not "Extended elevation") - there is limited room
1993 // in the footer, and the details there are mostly to make it easy
1994 // to check that you have corresponding pages from a multiple page
1995 // printout.
1996 footer[1].Printf(wmsg(/*Extended 1:%.0f*/244), scale);
1997 break;
2000 // TRANSLATORS: N/M meaning page N of M in the page footer of a printout.
2001 footer[2].Printf(wmsg(/*%d/%d*/232), pg, m_layout->pagesX * m_layout->pagesY);
2003 wxString datestamp = m_layout->datestamp;
2004 if (!datestamp.empty()) {
2005 // Remove any timezone suffix (e.g. " UTC" or " +1200").
2006 wxChar ch = datestamp[datestamp.size() - 1];
2007 if (ch >= 'A' && ch <= 'Z') {
2008 for (size_t i = datestamp.size() - 1; i; --i) {
2009 ch = datestamp[i];
2010 if (ch < 'A' || ch > 'Z') {
2011 if (ch == ' ') datestamp.resize(i);
2012 break;
2015 } else if (ch >= '0' && ch <= '9') {
2016 for (size_t i = datestamp.size() - 1; i; --i) {
2017 ch = datestamp[i];
2018 if (ch < '0' || ch > '9') {
2019 if ((ch == '-' || ch == '+') && datestamp[--i] == ' ')
2020 datestamp.resize(i);
2021 break;
2026 // Remove any day prefix (e.g. "Mon,").
2027 for (size_t i = 0; i != datestamp.size(); ++i) {
2028 if (datestamp[i] == ',' && i + 1 != datestamp.size()) {
2029 // Also skip a space after the comma.
2030 if (datestamp[i + 1] == ' ') ++i;
2031 datestamp.erase(0, i + 1);
2032 break;
2037 // TRANSLATORS: Used in the footer of printouts to compactly indicate that
2038 // the date which follows is the date that the survey data was processed.
2040 // Aven will replace %s with a string giving the date and time (e.g.
2041 // "2015-06-09 12:40:44").
2042 footer[3].Printf(wmsg(/*Processed: %s*/167), datestamp.c_str());
2044 const wxChar * footer_sep = wxT(" ");
2045 int fontsize_footer = fontsize_labels;
2046 wxFont * font_footer;
2047 font_footer = new wxFont(fontsize_footer, wxDEFAULT, wxNORMAL, wxNORMAL,
2048 false, wxString(fontname_labels, wxConvUTF8),
2049 wxFONTENCODING_UTF8);
2050 font_footer->Scale(font_scaling_x);
2051 SetFont(font_footer);
2052 int w[FOOTERS], ws, h;
2053 pdc->GetTextExtent(footer_sep, &ws, &h);
2054 int wtotal = ws * (FOOTERS - 1);
2055 for (int i = 0; i < FOOTERS; ++i) {
2056 pdc->GetTextExtent(footer[i], &w[i], &h);
2057 wtotal += w[i];
2060 long X = x_offset;
2061 long Y = y_offset + ypPageDepth + (long)(7 * m_layout->scY) - pdc->GetCharHeight();
2063 if (wtotal > xpPageWidth) {
2064 // Rescale the footer so it fits.
2065 double rescale = double(wtotal) / xpPageWidth;
2066 double xsc, ysc;
2067 pdc->GetUserScale(&xsc, &ysc);
2068 pdc->SetUserScale(xsc / rescale, ysc / rescale);
2069 SetFont(font_footer);
2070 wxString fullfooter = footer[0];
2071 for (int i = 1; i < FOOTERS - 1; ++i) {
2072 fullfooter += footer_sep;
2073 fullfooter += footer[i];
2075 pdc->DrawText(fullfooter, X * rescale, Y * rescale);
2076 // Draw final item right aligned to avoid misaligning.
2077 wxRect rect(x_offset * rescale, Y * rescale,
2078 xpPageWidth * rescale, pdc->GetCharHeight() * rescale);
2079 pdc->DrawLabel(footer[FOOTERS - 1], rect, wxALIGN_RIGHT|wxALIGN_TOP);
2080 pdc->SetUserScale(xsc, ysc);
2081 } else {
2082 // Space out the elements of the footer to fill the line.
2083 double extra = double(xpPageWidth - wtotal) / (FOOTERS - 1);
2084 for (int i = 0; i < FOOTERS - 1; ++i) {
2085 pdc->DrawText(footer[i], X + extra * i, Y);
2086 X += ws + w[i];
2088 // Draw final item right aligned to avoid misaligning.
2089 wxRect rect(x_offset, Y, xpPageWidth, pdc->GetCharHeight());
2090 pdc->DrawLabel(footer[FOOTERS - 1], rect, wxALIGN_RIGHT|wxALIGN_TOP);
2092 drawticks((int)(9 * m_layout->scX / POINTS_PER_MM), x, y);
2095 void
2096 svxPrintout::PlotLR(const vector<XSect> & centreline)
2098 assert(centreline.size() > 1);
2099 XSect prev_pt_v;
2100 Vector3 last_right(1.0, 0.0, 0.0);
2102 const double Sc = 1000 / m_layout->Scale;
2103 const double SIN = sin(rad(m_layout->rot));
2104 const double COS = cos(rad(m_layout->rot));
2106 vector<XSect>::const_iterator i = centreline.begin();
2107 vector<XSect>::size_type segment = 0;
2108 while (i != centreline.end()) {
2109 // get the coordinates of this vertex
2110 const XSect & pt_v = *i++;
2112 Vector3 right;
2114 const Vector3 up_v(0.0, 0.0, 1.0);
2116 if (segment == 0) {
2117 assert(i != centreline.end());
2118 // first segment
2120 // get the coordinates of the next vertex
2121 const XSect & next_pt_v = *i;
2123 // calculate vector from this pt to the next one
2124 Vector3 leg_v = next_pt_v - pt_v;
2126 // obtain a vector in the LRUD plane
2127 right = leg_v * up_v;
2128 if (right.magnitude() == 0) {
2129 right = last_right;
2130 } else {
2131 last_right = right;
2133 } else if (segment + 1 == centreline.size()) {
2134 // last segment
2136 // Calculate vector from the previous pt to this one.
2137 Vector3 leg_v = pt_v - prev_pt_v;
2139 // Obtain a horizontal vector in the LRUD plane.
2140 right = leg_v * up_v;
2141 if (right.magnitude() == 0) {
2142 right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
2143 } else {
2144 last_right = right;
2146 } else {
2147 assert(i != centreline.end());
2148 // Intermediate segment.
2150 // Get the coordinates of the next vertex.
2151 const XSect & next_pt_v = *i;
2153 // Calculate vectors from this vertex to the
2154 // next vertex, and from the previous vertex to
2155 // this one.
2156 Vector3 leg1_v = pt_v - prev_pt_v;
2157 Vector3 leg2_v = next_pt_v - pt_v;
2159 // Obtain horizontal vectors perpendicular to
2160 // both legs, then normalise and average to get
2161 // a horizontal bisector.
2162 Vector3 r1 = leg1_v * up_v;
2163 Vector3 r2 = leg2_v * up_v;
2164 r1.normalise();
2165 r2.normalise();
2166 right = r1 + r2;
2167 if (right.magnitude() == 0) {
2168 // This is the "mid-pitch" case...
2169 right = last_right;
2171 last_right = right;
2174 // Scale to unit vectors in the LRUD plane.
2175 right.normalise();
2177 Double l = pt_v.GetL();
2178 Double r = pt_v.GetR();
2180 if (l >= 0 || r >= 0) {
2181 // Get the x and y coordinates of the survey station
2182 double pt_X = pt_v.GetX() * COS - pt_v.GetY() * SIN;
2183 double pt_Y = pt_v.GetX() * SIN + pt_v.GetY() * COS;
2184 long pt_x = (long)((pt_X * Sc + m_layout->xOrg) * m_layout->scX);
2185 long pt_y = (long)((pt_Y * Sc + m_layout->yOrg) * m_layout->scY);
2187 // Calculate dimensions for the right arrow
2188 double COSR = right.GetX();
2189 double SINR = right.GetY();
2190 long CROSS_MAJOR = (COSR + SINR) * PWX_CROSS_SIZE;
2191 long CROSS_MINOR = (COSR - SINR) * PWX_CROSS_SIZE;
2193 if (l >= 0) {
2194 // Get the x and y coordinates of the end of the left arrow
2195 Vector3 p = pt_v - right * l;
2196 double X = p.GetX() * COS - p.GetY() * SIN;
2197 double Y = (p.GetX() * SIN + p.GetY() * COS);
2198 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2199 long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
2201 // Draw the arrow stem
2202 MoveTo(pt_x, pt_y);
2203 DrawTo(x, y);
2205 // Rotate the arrow by the page rotation
2206 long dx1 = (+CROSS_MINOR) * COS - (+CROSS_MAJOR) * SIN;
2207 long dy1 = (+CROSS_MINOR) * SIN + (+CROSS_MAJOR) * COS;
2208 long dx2 = (+CROSS_MAJOR) * COS - (-CROSS_MINOR) * SIN;
2209 long dy2 = (+CROSS_MAJOR) * SIN + (-CROSS_MINOR) * COS;
2211 // Draw the arrow
2212 MoveTo(x + dx1, y + dy1);
2213 DrawTo(x, y);
2214 DrawTo(x + dx2, y + dy2);
2217 if (r >= 0) {
2218 // Get the x and y coordinates of the end of the right arrow
2219 Vector3 p = pt_v + right * r;
2220 double X = p.GetX() * COS - p.GetY() * SIN;
2221 double Y = (p.GetX() * SIN + p.GetY() * COS);
2222 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2223 long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
2225 // Draw the arrow stem
2226 MoveTo(pt_x, pt_y);
2227 DrawTo(x, y);
2229 // Rotate the arrow by the page rotation
2230 long dx1 = (-CROSS_MINOR) * COS - (-CROSS_MAJOR) * SIN;
2231 long dy1 = (-CROSS_MINOR) * SIN + (-CROSS_MAJOR) * COS;
2232 long dx2 = (-CROSS_MAJOR) * COS - (+CROSS_MINOR) * SIN;
2233 long dy2 = (-CROSS_MAJOR) * SIN + (+CROSS_MINOR) * COS;
2235 // Draw the arrow
2236 MoveTo(x + dx1, y + dy1);
2237 DrawTo(x, y);
2238 DrawTo(x + dx2, y + dy2);
2242 prev_pt_v = pt_v;
2244 ++segment;
2248 void
2249 svxPrintout::PlotUD(const vector<XSect> & centreline)
2251 assert(centreline.size() > 1);
2252 const double Sc = 1000 / m_layout->Scale;
2254 vector<XSect>::const_iterator i = centreline.begin();
2255 while (i != centreline.end()) {
2256 // get the coordinates of this vertex
2257 const XSect & pt_v = *i++;
2259 Double u = pt_v.GetU();
2260 Double d = pt_v.GetD();
2262 if (u >= 0 || d >= 0) {
2263 // Get the coordinates of the survey point
2264 Vector3 p = pt_v;
2265 double SIN = sin(rad(m_layout->rot));
2266 double COS = cos(rad(m_layout->rot));
2267 double X = p.GetX() * COS - p.GetY() * SIN;
2268 double Y = p.GetZ();
2269 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2270 long pt_y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scX);
2272 if (u >= 0) {
2273 // Get the y coordinate of the up arrow
2274 long y = (long)(((Y + u) * Sc + m_layout->yOrg) * m_layout->scY);
2276 // Draw the arrow stem
2277 MoveTo(x, pt_y);
2278 DrawTo(x, y);
2280 // Draw the up arrow
2281 MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
2282 DrawTo(x, y);
2283 DrawTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
2286 if (d >= 0) {
2287 // Get the y coordinate of the down arrow
2288 long y = (long)(((Y - d) * Sc + m_layout->yOrg) * m_layout->scY);
2290 // Draw the arrow stem
2291 MoveTo(x, pt_y);
2292 DrawTo(x, y);
2294 // Draw the down arrow
2295 MoveTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
2296 DrawTo(x, y);
2297 DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);