Right-align bearing widget in print/export dialog
[survex.git] / src / printing.cc
blob414297866bc82162803de86690af0f0c4e238680
1 /* printing.cc */
2 /* Aven printing code */
3 /* Copyright (C) 1993-2003,2004,2005,2006,2010,2011,2012,2013,2014,2015,2016,2017 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 // The order of these arrays must match export_format in export.h.
270 static wxString formats[] = {
271 wxT("DXF"),
272 wxT("EPS"),
273 wxT("GPX"),
274 wxT("HPGL"),
275 wxT("JSON"),
276 wxT("KML"),
277 wxT("Plot"),
278 wxT("Skencil"),
279 wxT("Survex pos"),
280 wxT("SVG")
283 #if 0
284 static wxString projs[] = {
285 /* CUCC Austria: */
286 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"),
287 /* British grid SD (Yorkshire): */
288 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"),
289 /* British full grid reference: */
290 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")
292 #endif
294 static const unsigned format_info[] = {
295 LABELS|LEGS|SURF|SPLAYS|STNS|PASG|XSECT|WALLS|MARKER_SIZE|TEXT_HEIGHT|GRID|FULL_COORDS,
296 LABELS|LEGS|SURF|SPLAYS|STNS|PASG|XSECT|WALLS,
297 LABELS|LEGS|SURF|SPLAYS|ENTS|FIXES|EXPORTS|PROJ|EXPORT_3D,
298 LABELS|LEGS|SURF|SPLAYS|STNS|CENTRED,
299 LEGS|SPLAYS|CENTRED|EXPORT_3D,
300 LABELS|LEGS|SPLAYS|PASG|XSECT|WALLS|ENTS|FIXES|EXPORTS|PROJ|EXPORT_3D,
301 LABELS|LEGS|SURF|SPLAYS,
302 LABELS|LEGS|SURF|SPLAYS|STNS|MARKER_SIZE|GRID|SCALE,
303 LABELS|ENTS|FIXES|EXPORTS|EXPORT_3D,
304 LABELS|LEGS|SURF|SPLAYS|STNS|PASG|XSECT|WALLS|MARKER_SIZE|TEXT_HEIGHT|SCALE
307 static const char * extension[] = {
308 ".dxf",
309 ".eps",
310 ".gpx",
311 ".hpgl",
312 ".json",
313 ".kml",
314 ".plt",
315 ".sk",
316 ".pos",
317 ".svg"
320 static const int msg_filetype[] = {
321 /*DXF files*/411,
322 /*EPS files*/412,
323 /*GPX files*/413,
324 /* TRANSLATORS: Here "plotter" refers to a machine which draws a printout
325 * on a (usually large) sheet of paper using a pen mounted in a motorised
326 * mechanism. */
327 /*HPGL for plotters*/414,
328 /*JSON files*/445,
329 /*KML files*/444,
330 /* TRANSLATORS: "Compass" and "Carto" are the names of software packages,
331 * so should not be translated:
332 * http://www.fountainware.com/compass/
333 * http://www.psc-cavers.org/carto/ */
334 /*Compass PLT for use with Carto*/415,
335 /* TRANSLATORS: "Skencil" is the name of a software package, so should not be
336 * translated: http://www.skencil.org/ */
337 /*Skencil files*/416,
338 /* TRANSLATORS: Survex is the name of the software, and "pos" refers to a
339 * file extension, so neither should be translated. */
340 /*Survex pos files*/166,
341 /*SVG files*/417
344 // We discriminate as "One Page" isn't valid for exporting.
345 static wxString default_scale_print;
346 static wxString default_scale_export;
348 svxPrintDlg::svxPrintDlg(MainFrm* mainfrm_, const wxString & filename,
349 const wxString & title, const wxString & cs_proj,
350 const wxString & datestamp, time_t datestamp_numeric,
351 double angle, double tilt_angle,
352 bool labels, bool crosses, bool legs, bool surf,
353 bool splays, bool tubes, bool ents, bool fixes,
354 bool exports, bool printing, bool close_after_)
355 : wxDialog(mainfrm_, -1, wxString(printing ?
356 /* TRANSLATORS: Title of the print
357 * dialog */
358 wmsg(/*Print*/399) :
359 /* TRANSLATORS: Title of the export
360 * dialog */
361 wmsg(/*Export*/383))),
362 m_layout(printing ? wxGetApp().GetPageSetupDialogData() : NULL),
363 m_File(filename), mainfrm(mainfrm_), close_after(close_after_)
365 m_scale = NULL;
366 m_printSize = NULL;
367 m_bearing = NULL;
368 m_tilt = NULL;
369 m_format = NULL;
370 int show_mask = 0;
371 if (labels)
372 show_mask |= LABELS;
373 if (crosses)
374 show_mask |= STNS;
375 if (legs)
376 show_mask |= LEGS;
377 if (surf)
378 show_mask |= SURF;
379 if (splays)
380 show_mask |= SPLAYS;
381 if (tubes)
382 show_mask |= XSECT|WALLS|PASG;
383 if (ents)
384 show_mask |= ENTS;
385 if (fixes)
386 show_mask |= FIXES;
387 if (exports)
388 show_mask |= EXPORTS;
389 m_layout.show_mask = show_mask;
390 m_layout.datestamp = datestamp;
391 m_layout.datestamp_numeric = datestamp_numeric;
392 m_layout.rot = angle;
393 m_layout.title = title;
394 m_layout.cs_proj = cs_proj;
395 if (mainfrm->IsExtendedElevation()) {
396 m_layout.view = layout::EXTELEV;
397 if (m_layout.rot != 0.0 && m_layout.rot != 180.0) m_layout.rot = 0;
398 m_layout.tilt = 0;
399 } else {
400 m_layout.tilt = tilt_angle;
401 if (m_layout.tilt == -90.0) {
402 m_layout.view = layout::PLAN;
403 } else if (m_layout.tilt == 0.0) {
404 m_layout.view = layout::ELEV;
405 } else {
406 m_layout.view = layout::TILT;
410 /* setup our print dialog*/
411 wxBoxSizer* v1 = new wxBoxSizer(wxVERTICAL);
412 wxBoxSizer* h1 = new wxBoxSizer(wxHORIZONTAL); // holds controls
413 /* TRANSLATORS: Used as a label for the surrounding box for the "Bearing"
414 * and "Tilt angle" fields, and the "Plan view" and "Elevation" buttons in
415 * the "what to print/export" dialog. */
416 m_viewbox = new wxStaticBoxSizer(new wxStaticBox(this, -1, wmsg(/*View*/283)), wxVERTICAL);
417 /* TRANSLATORS: Used as a label for the surrounding box for the "survey
418 * legs" "stations" "names" etc checkboxes in the "what to print" dialog.
419 * "Elements" isn’t a good name for this but nothing better has yet come to
420 * mind! */
421 wxBoxSizer* v3 = new wxStaticBoxSizer(new wxStaticBox(this, -1, wmsg(/*Elements*/256)), wxVERTICAL);
422 wxBoxSizer* h2 = new wxBoxSizer(wxHORIZONTAL);
423 wxBoxSizer* h3 = new wxBoxSizer(wxHORIZONTAL); // holds buttons
425 if (!printing) {
426 wxStaticText* label;
427 label = new wxStaticText(this, -1, wxString(wmsg(/*Export format*/410)));
428 const size_t n_formats = sizeof(formats) / sizeof(formats[0]);
429 m_format = new wxChoice(this, svx_FORMAT,
430 wxDefaultPosition, wxDefaultSize,
431 n_formats, formats);
432 unsigned current_format = 0;
433 wxConfigBase * cfg = wxConfigBase::Get();
434 wxString s;
435 if (cfg->Read(wxT("export_format"), &s, wxString())) {
436 for (unsigned i = 0; i != n_formats; ++i) {
437 if (s == formats[i]) {
438 current_format = i;
439 break;
443 m_format->SetSelection(current_format);
444 wxBoxSizer* formatbox = new wxBoxSizer(wxHORIZONTAL);
445 formatbox->Add(label, 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
446 formatbox->Add(m_format, 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
448 v1->Add(formatbox, 0, wxALIGN_LEFT|wxALL, 0);
451 wxStaticText* label;
452 label = new wxStaticText(this, -1, wxString(wmsg(/*Scale*/154)) + wxT(" 1:"));
453 if (printing && scales[0].empty()) {
454 /* TRANSLATORS: used in the scale drop down selector in the print
455 * dialog the implicit meaning is "choose a suitable scale to fit
456 * the plot on a single page", but we need something shorter */
457 scales[0].assign(wmsg(/*One page*/258));
459 wxString default_scale;
460 if (printing) {
461 default_scale = default_scale_print;
462 if (default_scale.empty()) default_scale = scales[0];
463 } else {
464 default_scale = default_scale_export;
465 if (default_scale.empty()) default_scale = wxT("1000");
467 const wxString* scale_list = scales;
468 size_t n_scales = sizeof(scales) / sizeof(scales[0]);
469 if (!printing) {
470 ++scale_list;
471 --n_scales;
473 m_scale = new wxComboBox(this, svx_SCALE, default_scale, wxDefaultPosition,
474 wxDefaultSize, n_scales, scale_list);
475 m_scalebox = new wxBoxSizer(wxHORIZONTAL);
476 m_scalebox->Add(label, 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
477 m_scalebox->Add(m_scale, 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
479 m_viewbox->Add(m_scalebox, 0, wxALIGN_LEFT|wxALL, 0);
481 if (printing) {
482 // Make the dummy string wider than any sane value and use that to
483 // fix the width of the control so the sizers allow space for bigger
484 // page layouts.
485 m_printSize = new wxStaticText(this, -1, wxString::Format(wmsg(/*%d pages (%dx%d)*/257), 9604, 98, 98));
486 m_viewbox->Add(m_printSize, 0, wxALIGN_LEFT|wxALL, 5);
489 /* FIXME:
490 * svx_GRID, // double - spacing, default: 100m
491 * svx_TEXT_HEIGHT, // default 0.6
492 * svx_MARKER_SIZE // default 0.8
495 if (m_layout.view != layout::EXTELEV) {
496 wxFlexGridSizer* anglebox = new wxFlexGridSizer(2);
497 wxStaticText * brg_label, * tilt_label;
498 brg_label = new wxStaticText(this, -1, wmsg(/*Bearing*/259));
499 anglebox->Add(brg_label, 0, wxALIGN_CENTRE_VERTICAL|wxALIGN_LEFT|wxALL, 5);
500 // wSP_WRAP means that you can scroll past 360 to 0, and vice versa.
501 m_bearing = new wxSpinCtrlDouble(this, svx_BEARING, wxEmptyString,
502 wxDefaultPosition, wxDefaultSize,
503 wxSP_ARROW_KEYS|wxALIGN_RIGHT|wxSP_WRAP);
504 m_bearing->SetRange(0.0, 360.0);
505 m_bearing->SetDigits(ANGLE_DP);
506 anglebox->Add(m_bearing, 0, wxALIGN_CENTRE|wxALL, 5);
507 /* TRANSLATORS: Used in the print dialog: */
508 tilt_label = new wxStaticText(this, -1, wmsg(/*Tilt angle*/263));
509 anglebox->Add(tilt_label, 0, wxALIGN_CENTRE_VERTICAL|wxALIGN_LEFT|wxALL, 5);
510 m_tilt = new wxSpinCtrlDouble(this, svx_TILT);
511 m_tilt->SetRange(-90.0, 90.0);
512 m_tilt->SetDigits(ANGLE_DP);
513 anglebox->Add(m_tilt, 0, wxALIGN_CENTRE|wxALL, 5);
515 m_viewbox->Add(anglebox, 0, wxALIGN_LEFT|wxALL, 0);
517 wxBoxSizer * planelevsizer = new wxBoxSizer(wxHORIZONTAL);
518 planelevsizer->Add(new wxButton(this, svx_PLAN, wmsg(/*P&lan view*/117)),
519 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
520 planelevsizer->Add(new wxButton(this, svx_ELEV, wmsg(/*&Elevation*/285)),
521 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
523 m_viewbox->Add(planelevsizer, 0, wxALIGN_LEFT|wxALL, 5);
526 /* TRANSLATORS: Here a "survey leg" is a set of measurements between two
527 * "survey stations". */
528 v3->Add(new wxCheckBox(this, svx_LEGS, wmsg(/*Underground Survey Legs*/262),
529 wxDefaultPosition, wxDefaultSize, 0,
530 BitValidator(&m_layout.show_mask, LEGS)),
531 0, wxALIGN_LEFT|wxALL, 2);
532 /* TRANSLATORS: Here a "survey leg" is a set of measurements between two
533 * "survey stations". */
534 v3->Add(new wxCheckBox(this, svx_SURFACE, wmsg(/*Sur&face Survey Legs*/403),
535 wxDefaultPosition, wxDefaultSize, 0,
536 BitValidator(&m_layout.show_mask, SURF)),
537 0, wxALIGN_LEFT|wxALL, 2);
538 v3->Add(new wxCheckBox(this, svx_SPLAYS, wmsg(/*Spla&y Legs*/406),
539 wxDefaultPosition, wxDefaultSize, 0,
540 BitValidator(&m_layout.show_mask, SPLAYS)),
541 0, wxALIGN_LEFT|wxALL, 2);
542 v3->Add(new wxCheckBox(this, svx_STATIONS, wmsg(/*Crosses*/261),
543 wxDefaultPosition, wxDefaultSize, 0,
544 BitValidator(&m_layout.show_mask, STNS)),
545 0, wxALIGN_LEFT|wxALL, 2);
546 v3->Add(new wxCheckBox(this, svx_NAMES, wmsg(/*Station Names*/260),
547 wxDefaultPosition, wxDefaultSize, 0,
548 BitValidator(&m_layout.show_mask, LABELS)),
549 0, wxALIGN_LEFT|wxALL, 2);
550 v3->Add(new wxCheckBox(this, svx_ENTS, wmsg(/*Entrances*/418),
551 wxDefaultPosition, wxDefaultSize, 0,
552 BitValidator(&m_layout.show_mask, ENTS)),
553 0, wxALIGN_LEFT|wxALL, 2);
554 v3->Add(new wxCheckBox(this, svx_FIXES, wmsg(/*Fixed Points*/419),
555 wxDefaultPosition, wxDefaultSize, 0,
556 BitValidator(&m_layout.show_mask, FIXES)),
557 0, wxALIGN_LEFT|wxALL, 2);
558 v3->Add(new wxCheckBox(this, svx_EXPORTS, wmsg(/*Exported Stations*/420),
559 wxDefaultPosition, wxDefaultSize, 0,
560 BitValidator(&m_layout.show_mask, EXPORTS)),
561 0, wxALIGN_LEFT|wxALL, 2);
562 v3->Add(new wxCheckBox(this, svx_XSECT, wmsg(/*Cross-sections*/393),
563 wxDefaultPosition, wxDefaultSize, 0,
564 BitValidator(&m_layout.show_mask, XSECT)),
565 0, wxALIGN_LEFT|wxALL, 2);
566 if (!printing) {
567 v3->Add(new wxCheckBox(this, svx_WALLS, wmsg(/*Walls*/394),
568 wxDefaultPosition, wxDefaultSize, 0,
569 BitValidator(&m_layout.show_mask, WALLS)),
570 0, wxALIGN_LEFT|wxALL, 2);
571 // TRANSLATORS: Label for checkbox which controls whether there's a
572 // layer in the exported file (for formats such as DXF and SVG)
573 // containing polygons for the inside of cave passages).
574 v3->Add(new wxCheckBox(this, svx_PASSAGES, wmsg(/*Passages*/395),
575 wxDefaultPosition, wxDefaultSize, 0,
576 BitValidator(&m_layout.show_mask, PASG)),
577 0, wxALIGN_LEFT|wxALL, 2);
578 v3->Add(new wxCheckBox(this, svx_CENTRED, wmsg(/*Origin in centre*/421),
579 wxDefaultPosition, wxDefaultSize, 0,
580 BitValidator(&m_layout.show_mask, CENTRED)),
581 0, wxALIGN_LEFT|wxALL, 2);
582 v3->Add(new wxCheckBox(this, svx_FULLCOORDS, wmsg(/*Full coordinates*/422),
583 wxDefaultPosition, wxDefaultSize, 0,
584 BitValidator(&m_layout.show_mask, FULL_COORDS)),
585 0, wxALIGN_LEFT|wxALL, 2);
587 if (printing) {
588 /* TRANSLATORS: used in the print dialog - controls drawing lines
589 * around each page */
590 v3->Add(new wxCheckBox(this, svx_BORDERS, wmsg(/*Page Borders*/264),
591 wxDefaultPosition, wxDefaultSize, 0,
592 wxGenericValidator(&m_layout.Border)),
593 0, wxALIGN_LEFT|wxALL, 2);
594 /* TRANSLATORS: will be used in the print dialog - check this to print
595 * blank pages (otherwise they’ll be skipped to save paper) */
596 // m_blanks = new wxCheckBox(this, svx_BLANKS, wmsg(/*Blank Pages*/266));
597 // v3->Add(m_blanks, 0, wxALIGN_LEFT|wxALL, 2);
598 /* TRANSLATORS: As in the legend on a map. Used in the print dialog -
599 * controls drawing the box at the lower left with survey name, view
600 * angles, etc */
601 v3->Add(new wxCheckBox(this, svx_LEGEND, wmsg(/*Legend*/265),
602 wxDefaultPosition, wxDefaultSize, 0,
603 wxGenericValidator(&m_layout.Legend)),
604 0, wxALIGN_LEFT|wxALL, 2);
607 h1->Add(v3, 0, wxALIGN_LEFT|wxALL, 5);
608 h1->Add(m_viewbox, 0, wxALIGN_LEFT|wxLEFT, 5);
610 if (!printing) {
611 /* TRANSLATORS: The PROJ library is used to do coordinate
612 * transformations (https://trac.osgeo.org/proj/) - if the .3d file
613 * doesn't contain details of the coordinate projection in use, the
614 * user must specify it here for export formats which need to know it
615 * (e.g. GPX).
617 h2->Add(new wxStaticText(this, svx_PROJ_LABEL, wmsg(/*Coordinate projection*/440)),
618 0, wxLEFT|wxALIGN_CENTRE_VERTICAL, 5);
619 long style = 0;
620 if (!m_layout.cs_proj.empty()) {
621 // If the input file specified the coordinate system, don't let the
622 // user mess with it.
623 style = wxTE_READONLY;
624 } else {
625 #if 0 // FIXME: Is it a good idea to save this?
626 wxConfigBase * cfg = wxConfigBase::Get();
627 wxString input_projection;
628 cfg->Read(wxT("input_projection"), &input_projection);
629 if (!input_projection.empty())
630 proj_edit.SetValue(input_projection);
631 #endif
633 wxTextCtrl * proj_edit = new wxTextCtrl(this, svx_PROJ, m_layout.cs_proj,
634 wxDefaultPosition, wxDefaultSize,
635 style);
636 h2->Add(proj_edit, 1, wxALL|wxEXPAND, 5);
637 v1->Add(h2, 0, wxALIGN_LEFT|wxEXPAND, 5);
640 v1->Add(h1, 0, wxALIGN_LEFT|wxALL, 5);
642 // When we enable/disable checkboxes in the export dialog, ideally we'd
643 // like the dialog to resize, but not sure how to achieve that, so we
644 // add a stretchable spacer here so at least the buttons stay in the
645 // lower right corner.
646 v1->AddStretchSpacer();
648 wxButton * but;
649 but = new wxButton(this, wxID_CANCEL);
650 h3->Add(but, 0, wxALL, 5);
651 if (printing) {
652 #ifdef AVEN_PRINT_PREVIEW
653 but = new wxButton(this, wxID_PREVIEW);
654 h3->Add(but, 0, wxALL, 5);
655 but = new wxButton(this, wxID_PRINT);
656 #else
657 but = new wxButton(this, wxID_PRINT, wmsg(/*&Print...*/400));
658 #endif
659 } else {
660 /* TRANSLATORS: The text on the action button in the "Export" settings
661 * dialog */
662 but = new wxButton(this, svx_EXPORT, wmsg(/*&Export...*/230));
664 but->SetDefault();
665 h3->Add(but, 0, wxALL, 5);
666 v1->Add(h3, 0, wxALIGN_RIGHT|wxALL, 5);
668 SetAutoLayout(true);
669 SetSizer(v1);
670 v1->SetSizeHints(this);
672 LayoutToUI();
673 SomethingChanged(0);
676 void
677 svxPrintDlg::OnPrint(wxCommandEvent&) {
678 SomethingChanged(0);
679 TransferDataFromWindow();
680 wxPageSetupDialogData * psdd = wxGetApp().GetPageSetupDialogData();
681 wxPrintDialogData pd(psdd->GetPrintData());
682 wxPrinter pr(&pd);
683 svxPrintout po(mainfrm, &m_layout, psdd, m_File);
684 if (m_layout.SkipBlank) {
685 // FIXME: wx's printing requires a contiguous range of valid page
686 // numbers. To achieve that, we need to run a scan for blank pages
687 // here, so that GetPageInfo() knows what range to return, and so
688 // that OnPrintPage() can map a page number back to where in the
689 // MxN multi-page layout.
690 #if 0
691 po.scan_for_blank_pages = true;
692 for (int page = 1; page <= m_layout->pages; ++page) {
693 po.fBlankPage = fTrue;
694 po.OnPrintPage(page);
695 // FIXME: Do something with po.fBlankPage
697 po.scan_for_blank_pages = false;
698 #endif
700 if (pr.Print(this, &po, true)) {
701 // Close the print dialog if printing succeeded.
702 Destroy();
706 void
707 svxPrintDlg::OnExport(wxCommandEvent&) {
708 UIToLayout();
709 TransferDataFromWindow();
710 wxString leaf;
711 wxFileName::SplitPath(m_File, NULL, NULL, &leaf, NULL, wxPATH_NATIVE);
712 unsigned format_idx = ((wxChoice*)FindWindow(svx_FORMAT))->GetSelection();
713 leaf += wxString::FromUTF8(extension[format_idx]);
715 wxString filespec = wmsg(msg_filetype[format_idx]);
716 filespec += wxT("|*");
717 filespec += wxString::FromUTF8(extension[format_idx]);
718 filespec += wxT("|");
719 filespec += wmsg(/*All files*/208);
720 filespec += wxT("|");
721 filespec += wxFileSelectorDefaultWildcardStr;
723 /* TRANSLATORS: Title of file dialog to choose name and type of exported
724 * file. */
725 wxFileDialog dlg(this, wmsg(/*Export as:*/401), wxString(), leaf,
726 filespec, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
727 if (dlg.ShowModal() == wxID_OK) {
728 wxString input_projection = ((wxTextCtrl*)FindWindow(svx_PROJ))->GetValue();
729 double grid = 100; // metres
730 double text_height = 0.6;
731 double marker_size = 0.8;
733 try {
734 const wxString& export_fnm = dlg.GetPath();
735 unsigned mask = format_info[format_idx];
736 double rot, tilt;
737 if (mask & EXPORT_3D) {
738 rot = 0.0;
739 tilt = -90.0;
740 } else {
741 rot = m_layout.rot;
742 tilt = m_layout.tilt;
744 if (!Export(export_fnm, m_layout.title,
745 m_layout.datestamp, m_layout.datestamp_numeric, mainfrm,
746 rot, tilt, m_layout.get_effective_show_mask(),
747 export_format(format_idx), input_projection.utf8_str(),
748 grid, text_height, marker_size, m_layout.Scale)) {
749 wxString m = wxString::Format(wmsg(/*Couldn’t write file “%s”*/402).c_str(),
750 export_fnm.c_str());
751 wxGetApp().ReportError(m);
753 } catch (const wxString & m) {
754 wxGetApp().ReportError(m);
757 Destroy();
760 #ifdef AVEN_PRINT_PREVIEW
761 void
762 svxPrintDlg::OnPreview(wxCommandEvent&) {
763 SomethingChanged(0);
764 TransferDataFromWindow();
765 wxPageSetupDialogData * psdd = wxGetApp().GetPageSetupDialogData();
766 wxPrintDialogData pd(psdd->GetPrintData());
767 wxPrintPreview* pv;
768 pv = new wxPrintPreview(new svxPrintout(mainfrm, &m_layout, psdd, m_File),
769 new svxPrintout(mainfrm, &m_layout, psdd, m_File),
770 &pd);
771 // TRANSLATORS: Title of the print preview dialog
772 wxPreviewFrame *frame = new wxPreviewFrame(pv, mainfrm, wmsg(/*Print Preview*/398));
773 frame->Initialize();
775 // Size preview frame so that all of the controlbar and canvas can be seen
776 // if possible.
777 int w, h;
778 // GetBestSize gives us the width needed to show the whole controlbar.
779 frame->GetBestSize(&w, &h);
780 if (h < w) {
781 // On wxGTK at least, GetBestSize() returns much too small a height.
782 h = w * 6 / 5;
784 // Ensure that we don't make the window bigger than the screen.
785 // Use wxGetClientDisplayRect() so we don't cover the MS Windows
786 // task bar either.
787 wxRect disp = wxGetClientDisplayRect();
788 if (w > disp.GetWidth()) w = disp.GetWidth();
789 if (h > disp.GetHeight()) h = disp.GetHeight();
790 // Centre the window within the "ClientDisplayRect".
791 int x = disp.GetLeft() + (disp.GetWidth() - w) / 2;
792 int y = disp.GetTop() + (disp.GetHeight() - h) / 2;
793 frame->SetSize(x, y, w, h);
795 frame->Show();
797 #endif
799 void
800 svxPrintDlg::OnPlan(wxCommandEvent&) {
801 m_tilt->SetValue(-90.0);
802 SomethingChanged(svx_TILT);
805 void
806 svxPrintDlg::OnElevation(wxCommandEvent&) {
807 m_tilt->SetValue(0.0);
808 SomethingChanged(svx_TILT);
811 void
812 svxPrintDlg::OnPlanUpdate(wxUpdateUIEvent& e) {
813 e.Enable(m_tilt->GetValue() != -90.0);
816 void
817 svxPrintDlg::OnElevationUpdate(wxUpdateUIEvent& e) {
818 e.Enable(m_tilt->GetValue() != 0.0);
821 void
822 svxPrintDlg::OnChangeSpin(wxSpinDoubleEvent& e) {
823 SomethingChanged(e.GetId());
826 void
827 svxPrintDlg::OnChange(wxCommandEvent& e) {
828 if (e.GetId() == svx_SCALE && m_scale) {
829 default_scale_print = m_scale->GetValue();
830 if (default_scale_print != scales[0]) {
831 // Don't store "One Page" for use when exporting.
832 default_scale_export = default_scale_print;
835 SomethingChanged(e.GetId());
838 void
839 svxPrintDlg::OnCancel(wxCommandEvent&) {
840 if (close_after)
841 mainfrm->Close();
842 Destroy();
845 void
846 svxPrintDlg::SomethingChanged(int control_id) {
847 if ((control_id == 0 || control_id == svx_FORMAT) && m_format) {
848 // Update the shown/hidden fields for the newly selected export filter.
849 int new_filter_idx = m_format->GetSelection();
850 if (new_filter_idx != wxNOT_FOUND) {
851 unsigned mask = format_info[new_filter_idx];
852 static const struct { int id; unsigned mask; } controls[] = {
853 { svx_LEGS, LEGS },
854 { svx_SURFACE, SURF },
855 { svx_SPLAYS, SPLAYS },
856 { svx_STATIONS, STNS },
857 { svx_NAMES, LABELS },
858 { svx_XSECT, XSECT },
859 { svx_WALLS, WALLS },
860 { svx_PASSAGES, PASG },
861 { svx_ENTS, ENTS },
862 { svx_FIXES, FIXES },
863 { svx_EXPORTS, EXPORTS },
864 { svx_CENTRED, CENTRED },
865 { svx_FULLCOORDS, FULL_COORDS },
866 { svx_PROJ_LABEL, PROJ },
867 { svx_PROJ, PROJ },
869 static unsigned n_controls = sizeof(controls) / sizeof(controls[0]);
870 for (unsigned i = 0; i != n_controls; ++i) {
871 wxWindow * control = FindWindow(controls[i].id);
872 if (control) control->Show(mask & controls[i].mask);
874 m_scalebox->Show(bool(mask & SCALE));
875 m_viewbox->Show(!bool(mask & EXPORT_3D));
876 GetSizer()->Layout();
877 if (control_id == svx_FORMAT) {
878 wxConfigBase * cfg = wxConfigBase::Get();
879 cfg->Write(wxT("export_format"), formats[new_filter_idx]);
884 UIToLayout();
886 if (m_printSize || m_scale) {
887 // Update the bounding box.
888 RecalcBounds();
890 if (m_scale) {
891 if (!(m_scale->GetValue()).ToDouble(&(m_layout.Scale)) ||
892 m_layout.Scale == 0.0) {
893 m_layout.pick_scale(1, 1);
898 if (m_printSize && m_layout.xMax >= m_layout.xMin) {
899 m_layout.pages_required();
900 m_printSize->SetLabel(wxString::Format(wmsg(/*%d pages (%dx%d)*/257), m_layout.pages, m_layout.pagesX, m_layout.pagesY));
904 void
905 svxPrintDlg::LayoutToUI()
907 // m_blanks->SetValue(m_layout.SkipBlank);
908 if (m_layout.view != layout::EXTELEV) {
909 m_tilt->SetValue(m_layout.tilt);
910 m_bearing->SetValue(m_layout.rot);
913 if (m_scale && m_layout.Scale != 0) {
914 // Do this last as it causes an OnChange message which calls UIToLayout
915 wxString temp;
916 temp << m_layout.Scale;
917 m_scale->SetValue(temp);
921 void
922 svxPrintDlg::UIToLayout()
924 // m_layout.SkipBlank = m_blanks->IsChecked();
926 if (m_layout.view != layout::EXTELEV && m_tilt) {
927 m_layout.tilt = m_tilt->GetValue();
928 if (m_layout.tilt == -90.0) {
929 m_layout.view = layout::PLAN;
930 } else if (m_layout.tilt == 0.0) {
931 m_layout.view = layout::ELEV;
932 } else {
933 m_layout.view = layout::TILT;
936 bool enable_passage_opts = (m_layout.view != layout::TILT);
937 wxWindow * win;
938 win = FindWindow(svx_XSECT);
939 if (win) win->Enable(enable_passage_opts);
940 win = FindWindow(svx_WALLS);
941 if (win) win->Enable(enable_passage_opts);
942 win = FindWindow(svx_PASSAGES);
943 if (win) win->Enable(enable_passage_opts);
945 m_layout.rot = m_bearing->GetValue();
949 void
950 svxPrintDlg::RecalcBounds()
952 m_layout.yMax = m_layout.xMax = -DBL_MAX;
953 m_layout.yMin = m_layout.xMin = DBL_MAX;
955 double SIN = sin(rad(m_layout.rot));
956 double COS = cos(rad(m_layout.rot));
957 double SINT = sin(rad(m_layout.tilt));
958 double COST = cos(rad(m_layout.tilt));
960 int show_mask = m_layout.get_effective_show_mask();
961 if (show_mask & LEGS) {
962 for (int f = 0; f != 8; ++f) {
963 if ((show_mask & (f & img_FLAG_SURFACE) ? SURF : LEGS) == 0) {
964 // Not showing traverse because of surface/underground status.
965 continue;
967 if ((f & img_FLAG_SPLAY) && (show_mask & SPLAYS) == 0) {
968 // Not showing because it's a splay.
969 continue;
971 list<traverse>::const_iterator trav = mainfrm->traverses_begin(f);
972 list<traverse>::const_iterator tend = mainfrm->traverses_end(f);
973 for ( ; trav != tend; ++trav) {
974 vector<PointInfo>::const_iterator pos = trav->begin();
975 vector<PointInfo>::const_iterator end = trav->end();
976 for ( ; pos != end; ++pos) {
977 double x = pos->GetX();
978 double y = pos->GetY();
979 double z = pos->GetZ();
980 double X = x * COS - y * SIN;
981 if (X > m_layout.xMax) m_layout.xMax = X;
982 if (X < m_layout.xMin) m_layout.xMin = X;
983 double Y = z * COST - (x * SIN + y * COS) * SINT;
984 if (Y > m_layout.yMax) m_layout.yMax = Y;
985 if (Y < m_layout.yMin) m_layout.yMin = Y;
991 if ((show_mask & XSECT) &&
992 (m_layout.tilt == 0.0 || m_layout.tilt == 90.0 || m_layout.tilt == -90.0)) {
993 list<vector<XSect> >::const_iterator trav = mainfrm->tubes_begin();
994 list<vector<XSect> >::const_iterator tend = mainfrm->tubes_end();
995 for ( ; trav != tend; ++trav) {
996 XSect prev_pt_v;
997 Vector3 last_right(1.0, 0.0, 0.0);
999 vector<XSect>::const_iterator i = trav->begin();
1000 vector<XSect>::size_type segment = 0;
1001 while (i != trav->end()) {
1002 // get the coordinates of this vertex
1003 const XSect & pt_v = *i++;
1004 if (m_layout.tilt == 0.0) {
1005 Double u = pt_v.GetU();
1006 Double d = pt_v.GetD();
1008 if (u >= 0 || d >= 0) {
1009 double x = pt_v.GetX();
1010 double y = pt_v.GetY();
1011 double z = pt_v.GetZ();
1012 double X = x * COS - y * SIN;
1013 double Y = z * COST - (x * SIN + y * COS) * SINT;
1015 if (X > m_layout.xMax) m_layout.xMax = X;
1016 if (X < m_layout.xMin) m_layout.xMin = X;
1017 double U = Y + max(0.0, pt_v.GetU());
1018 if (U > m_layout.yMax) m_layout.yMax = U;
1019 double D = Y - max(0.0, pt_v.GetD());
1020 if (D < m_layout.yMin) m_layout.yMin = D;
1022 } else {
1023 // More complex, and this duplicates the algorithm from
1024 // PlotLR() - we should try to share that, maybe via a
1025 // template.
1026 Vector3 right;
1028 const Vector3 up_v(0.0, 0.0, 1.0);
1030 if (segment == 0) {
1031 assert(i != trav->end());
1032 // first segment
1034 // get the coordinates of the next vertex
1035 const XSect & next_pt_v = *i;
1037 // calculate vector from this pt to the next one
1038 Vector3 leg_v = next_pt_v - pt_v;
1040 // obtain a vector in the LRUD plane
1041 right = leg_v * up_v;
1042 if (right.magnitude() == 0) {
1043 right = last_right;
1044 } else {
1045 last_right = right;
1047 } else if (segment + 1 == trav->size()) {
1048 // last segment
1050 // Calculate vector from the previous pt to this one.
1051 Vector3 leg_v = pt_v - prev_pt_v;
1053 // Obtain a horizontal vector in the LRUD plane.
1054 right = leg_v * up_v;
1055 if (right.magnitude() == 0) {
1056 right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
1057 } else {
1058 last_right = right;
1060 } else {
1061 assert(i != trav->end());
1062 // Intermediate segment.
1064 // Get the coordinates of the next vertex.
1065 const XSect & next_pt_v = *i;
1067 // Calculate vectors from this vertex to the
1068 // next vertex, and from the previous vertex to
1069 // this one.
1070 Vector3 leg1_v = pt_v - prev_pt_v;
1071 Vector3 leg2_v = next_pt_v - pt_v;
1073 // Obtain horizontal vectors perpendicular to
1074 // both legs, then normalise and average to get
1075 // a horizontal bisector.
1076 Vector3 r1 = leg1_v * up_v;
1077 Vector3 r2 = leg2_v * up_v;
1078 r1.normalise();
1079 r2.normalise();
1080 right = r1 + r2;
1081 if (right.magnitude() == 0) {
1082 // This is the "mid-pitch" case...
1083 right = last_right;
1085 last_right = right;
1088 // Scale to unit vectors in the LRUD plane.
1089 right.normalise();
1091 Double l = pt_v.GetL();
1092 Double r = pt_v.GetR();
1094 if (l >= 0 || r >= 0) {
1095 // Get the x and y coordinates of the survey station
1096 double pt_X = pt_v.GetX() * COS - pt_v.GetY() * SIN;
1097 double pt_Y = pt_v.GetX() * SIN + pt_v.GetY() * COS;
1099 double X, Y;
1100 if (l >= 0) {
1101 // Get the x and y coordinates of the end of the left arrow
1102 Vector3 p = pt_v - right * l;
1103 X = p.GetX() * COS - p.GetY() * SIN;
1104 Y = (p.GetX() * SIN + p.GetY() * COS);
1105 } else {
1106 X = pt_X;
1107 Y = pt_Y;
1109 if (X > m_layout.xMax) m_layout.xMax = X;
1110 if (X < m_layout.xMin) m_layout.xMin = X;
1111 if (Y > m_layout.yMax) m_layout.yMax = Y;
1112 if (Y < m_layout.yMin) m_layout.yMin = Y;
1114 if (r >= 0) {
1115 // Get the x and y coordinates of the end of the right arrow
1116 Vector3 p = pt_v + right * r;
1117 X = p.GetX() * COS - p.GetY() * SIN;
1118 Y = (p.GetX() * SIN + p.GetY() * COS);
1119 } else {
1120 X = pt_X;
1121 Y = pt_Y;
1123 if (X > m_layout.xMax) m_layout.xMax = X;
1124 if (X < m_layout.xMin) m_layout.xMin = X;
1125 if (Y > m_layout.yMax) m_layout.yMax = Y;
1126 if (Y < m_layout.yMin) m_layout.yMin = Y;
1129 prev_pt_v = pt_v;
1131 ++segment;
1137 if (show_mask & (LABELS|STNS)) {
1138 list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
1139 while (label != mainfrm->GetLabelsEnd()) {
1140 double x = (*label)->GetX();
1141 double y = (*label)->GetY();
1142 double z = (*label)->GetZ();
1143 if ((show_mask & SURF) || (*label)->IsUnderground()) {
1144 double X = x * COS - y * SIN;
1145 if (X > m_layout.xMax) m_layout.xMax = X;
1146 if (X < m_layout.xMin) m_layout.xMin = X;
1147 double Y = z * COST - (x * SIN + y * COS) * SINT;
1148 if (Y > m_layout.yMax) m_layout.yMax = Y;
1149 if (Y < m_layout.yMin) m_layout.yMin = Y;
1151 ++label;
1156 static int xpPageWidth, ypPageDepth;
1157 static long x_offset, y_offset;
1158 static int fontsize, fontsize_labels;
1160 /* FIXME: allow the font to be set */
1162 static const char *fontname = "Arial", *fontname_labels = "Arial";
1164 svxPrintout::svxPrintout(MainFrm *mainfrm_, layout *l,
1165 wxPageSetupDialogData *data, const wxString & title)
1166 : wxPrintout(title), font_labels(NULL), font_default(NULL),
1167 scan_for_blank_pages(false)
1169 mainfrm = mainfrm_;
1170 m_layout = l;
1171 m_data = data;
1174 void
1175 svxPrintout::draw_info_box()
1177 layout *l = m_layout;
1178 int boxwidth = 70;
1179 int boxheight = 30;
1181 pdc->SetPen(*pen_frame);
1183 int div = boxwidth;
1184 if (l->view != layout::EXTELEV) {
1185 boxwidth += boxheight;
1186 MOVEMM(div, boxheight);
1187 DRAWMM(div, 0);
1188 MOVEMM(0, 30); DRAWMM(div, 30);
1191 MOVEMM(0, boxheight);
1192 DRAWMM(boxwidth, boxheight);
1193 DRAWMM(boxwidth, 0);
1194 if (!l->Border) {
1195 DRAWMM(0, 0);
1196 DRAWMM(0, boxheight);
1199 MOVEMM(0, 20); DRAWMM(div, 20);
1200 MOVEMM(0, 10); DRAWMM(div, 10);
1202 switch (l->view) {
1203 case layout::PLAN: {
1204 long ax, ay, bx, by, cx, cy, dx, dy;
1206 long xc = boxwidth - boxheight / 2;
1207 long yc = boxheight / 2;
1208 const double RADIUS = boxheight / 3;
1209 DrawEllipse(long(xc * l->scX), long(yc * l->scY),
1210 long(RADIUS * l->scX), long(RADIUS * l->scY));
1212 ax = (long)((xc - (RADIUS - 1) * sin(rad(000.0 + l->rot))) * l->scX);
1213 ay = (long)((yc + (RADIUS - 1) * cos(rad(000.0 + l->rot))) * l->scY);
1214 bx = (long)((xc - RADIUS * 0.5 * sin(rad(180.0 + l->rot))) * l->scX);
1215 by = (long)((yc + RADIUS * 0.5 * cos(rad(180.0 + l->rot))) * l->scY);
1216 cx = (long)((xc - (RADIUS - 1) * sin(rad(160.0 + l->rot))) * l->scX);
1217 cy = (long)((yc + (RADIUS - 1) * cos(rad(160.0 + l->rot))) * l->scY);
1218 dx = (long)((xc - (RADIUS - 1) * sin(rad(200.0 + l->rot))) * l->scX);
1219 dy = (long)((yc + (RADIUS - 1) * cos(rad(200.0 + l->rot))) * l->scY);
1221 MoveTo(ax, ay);
1222 DrawTo(bx, by);
1223 DrawTo(cx, cy);
1224 DrawTo(ax, ay);
1225 DrawTo(dx, dy);
1226 DrawTo(bx, by);
1228 pdc->SetTextForeground(colour_text);
1229 MOVEMM(div + 0.5, boxheight - 5.5);
1230 WriteString(wmsg(/*North*/115));
1232 wxString angle = format_angle(ANGLE_FMT, l->rot);
1233 wxString s;
1234 /* TRANSLATORS: This is used on printouts of plans, with %s replaced by
1235 * something like "123°". The bearing is up the page. */
1236 s.Printf(wmsg(/*Plan view, %s up page*/168), angle.c_str());
1237 MOVEMM(2, 12); WriteString(s);
1238 break;
1240 case layout::ELEV: case layout::TILT: {
1241 const int L = div + 2;
1242 const int R = boxwidth - 2;
1243 const int H = boxheight / 2;
1244 MOVEMM(L, H); DRAWMM(L + 5, H - 3); DRAWMM(L + 3, H); DRAWMM(L + 5, H + 3);
1246 DRAWMM(L, H); DRAWMM(R, H);
1248 DRAWMM(R - 5, H + 3); DRAWMM(R - 3, H); DRAWMM(R - 5, H - 3); DRAWMM(R, H);
1250 MOVEMM((L + R) / 2, H - 2); DRAWMM((L + R) / 2, H + 2);
1252 pdc->SetTextForeground(colour_text);
1253 MOVEMM(div + 2, boxheight - 8);
1254 /* TRANSLATORS: "Elevation on" 020 <-> 200 degrees */
1255 WriteString(wmsg(/*Elevation on*/116));
1257 MOVEMM(L, 2);
1258 WriteString(format_angle(ANGLE_FMT, fmod(l->rot + 270.0, 360.0)));
1259 MOVEMM(R - 10, 2);
1260 WriteString(format_angle(ANGLE_FMT, fmod(l->rot + 90.0, 360.0)));
1262 wxString angle = format_angle(ANGLE_FMT, l->rot);
1263 wxString s;
1264 if (l->view == layout::ELEV) {
1265 /* TRANSLATORS: This is used on printouts of elevations, with %s
1266 * replaced by something like "123°". The bearing is the direction
1267 * we’re looking. */
1268 s.Printf(wmsg(/*Elevation facing %s*/169), angle.c_str());
1269 } else {
1270 wxString a2 = format_angle(ANGLE2_FMT, l->tilt);
1271 /* TRANSLATORS: This is used on printouts of tilted elevations, with
1272 * the first %s replaced by something like "123°", and the second by
1273 * something like "-45°". The bearing is the direction we’re
1274 * looking. */
1275 s.Printf(wmsg(/*Elevation facing %s, tilted %s*/284), angle.c_str(), a2.c_str());
1277 MOVEMM(2, 12); WriteString(s);
1278 break;
1280 case layout::EXTELEV:
1281 pdc->SetTextForeground(colour_text);
1282 MOVEMM(2, 12);
1283 /* TRANSLATORS: This is used on printouts of extended elevations. */
1284 WriteString(wmsg(/*Extended elevation*/191));
1285 break;
1288 MOVEMM(2, boxheight - 8); WriteString(l->title);
1290 MOVEMM(2, 2);
1291 // FIXME: "Original Scale" better?
1292 WriteString(wxString::Format(wmsg(/*Scale*/154) + wxT(" 1:%.0f"),
1293 l->Scale));
1295 /* This used to be a copyright line, but it was occasionally
1296 * mis-interpreted as us claiming copyright on the survey, so let's
1297 * give the website URL instead */
1298 MOVEMM(boxwidth + 2, 2);
1299 WriteString(wxT("Survex " VERSION " - https://survex.com/"));
1301 draw_scale_bar(boxwidth + 10.0, 17.0, l->PaperWidth - boxwidth - 18.0);
1304 /* Draw fancy scale bar with bottom left at (x,y) (both in mm) and at most */
1305 /* MaxLength mm long. The scaling in use is 1:scale */
1306 void
1307 svxPrintout::draw_scale_bar(double x, double y, double MaxLength)
1309 double StepEst, d;
1310 int E, Step, n, c;
1311 wxString buf;
1312 /* Limit scalebar to 20cm to stop people with A0 plotters complaining */
1313 if (MaxLength > 200.0) MaxLength = 200.0;
1315 #define dmin 10.0 /* each division >= dmin mm long */
1316 #define StepMax 5 /* number in steps of at most StepMax (x 10^N) */
1317 #define epsilon (1e-4) /* fudge factor to prevent rounding problems */
1319 E = (int)ceil(log10((dmin * 0.001 * m_layout->Scale) / StepMax));
1320 StepEst = pow(10.0, -(double)E) * (dmin * 0.001) * m_layout->Scale - epsilon;
1322 /* Force labelling to be in multiples of 1, 2, or 5 */
1323 Step = (StepEst <= 1.0 ? 1 : (StepEst <= 2.0 ? 2 : 5));
1325 /* Work out actual length of each scale bar division */
1326 d = Step * pow(10.0, (double)E) / m_layout->Scale * 1000.0;
1328 /* FIXME: Non-metric units here... */
1329 /* Choose appropriate units, s.t. if possible E is >=0 and minimized */
1330 int units;
1331 if (E >= 3) {
1332 E -= 3;
1333 units = /*km*/423;
1334 } else if (E >= 0) {
1335 units = /*m*/424;
1336 } else {
1337 E += 2;
1338 units = /*cm*/425;
1341 buf = wmsg(/*Scale*/154);
1343 /* Add units used - eg. "Scale (10m)" */
1344 double pow10_E = pow(10.0, (double)E);
1345 if (E >= 0) {
1346 buf += wxString::Format(wxT(" (%.f%s)"), pow10_E, wmsg(units).c_str());
1347 } else {
1348 int sf = -(int)floor(E);
1349 buf += wxString::Format(wxT(" (%.*f%s)"), sf, pow10_E, wmsg(units).c_str());
1351 pdc->SetTextForeground(colour_text);
1352 MOVEMM(x, y + 4); WriteString(buf);
1354 /* Work out how many divisions there will be */
1355 n = (int)(MaxLength / d);
1357 pdc->SetPen(*pen_frame);
1359 long Y = long(y * m_layout->scY);
1360 long Y2 = long((y + 3) * m_layout->scY);
1361 long X = long(x * m_layout->scX);
1362 long X2 = long((x + n * d) * m_layout->scX);
1364 /* Draw top of scale bar */
1365 MoveTo(X2, Y2);
1366 DrawTo(X, Y2);
1367 #if 0
1368 DrawTo(X2, Y);
1369 DrawTo(X, Y);
1370 MOVEMM(x + n * d, y); DRAWMM(x, y);
1371 #endif
1372 /* Draw divisions and label them */
1373 for (c = 0; c <= n; c++) {
1374 pdc->SetPen(*pen_frame);
1375 X = long((x + c * d) * m_layout->scX);
1376 MoveTo(X, Y);
1377 DrawTo(X, Y2);
1378 #if 0 // Don't waste toner!
1379 /* Draw a "zebra crossing" scale bar. */
1380 if (c < n && (c & 1) == 0) {
1381 X2 = long((x + (c + 1) * d) * m_layout->scX);
1382 SolidRectangle(X, Y, X2 - X, Y2 - Y);
1384 #endif
1385 buf.Printf(wxT("%d"), c * Step);
1386 pdc->SetTextForeground(colour_text);
1387 MOVEMM(x + c * d - buf.length(), y - 5);
1388 WriteString(buf);
1392 #if 0
1393 void
1394 make_calibration(layout *l) {
1395 img_point pt = { 0.0, 0.0, 0.0 };
1396 l->xMax = l->yMax = 0.1;
1397 l->xMin = l->yMin = 0;
1399 stack(l, img_MOVE, NULL, &pt);
1400 pt.x = 0.1;
1401 stack(l, img_LINE, NULL, &pt);
1402 pt.y = 0.1;
1403 stack(l, img_LINE, NULL, &pt);
1404 pt.x = 0.0;
1405 stack(l, img_LINE, NULL, &pt);
1406 pt.y = 0.0;
1407 stack(l, img_LINE, NULL, &pt);
1408 pt.x = 0.05;
1409 pt.y = 0.001;
1410 stack(l, img_LABEL, "10cm", &pt);
1411 pt.x = 0.001;
1412 pt.y = 0.05;
1413 stack(l, img_LABEL, "10cm", &pt);
1414 l->Scale = 1.0;
1416 #endif
1419 svxPrintout::next_page(int *pstate, char **q, int pageLim)
1421 char *p;
1422 int page;
1423 int c;
1424 p = *q;
1425 if (*pstate > 0) {
1426 /* doing a range */
1427 (*pstate)++;
1428 wxASSERT(*p == '-');
1429 p++;
1430 while (isspace((unsigned char)*p)) p++;
1431 if (sscanf(p, "%u%n", &page, &c) > 0) {
1432 p += c;
1433 } else {
1434 page = pageLim;
1436 if (*pstate > page) goto err;
1437 if (*pstate < page) return *pstate;
1438 *q = p;
1439 *pstate = 0;
1440 return page;
1443 while (isspace((unsigned char)*p) || *p == ',') p++;
1445 if (!*p) return 0; /* done */
1447 if (*p == '-') {
1448 *q = p;
1449 *pstate = 1;
1450 return 1; /* range with initial parameter omitted */
1452 if (sscanf(p, "%u%n", &page, &c) > 0) {
1453 p += c;
1454 while (isspace((unsigned char)*p)) p++;
1455 *q = p;
1456 if (0 < page && page <= pageLim) {
1457 if (*p == '-') *pstate = page; /* range with start */
1458 return page;
1461 err:
1462 *pstate = -1;
1463 return 0;
1466 /* Draws in alignment marks on each page or borders on edge pages */
1467 void
1468 svxPrintout::drawticks(int tsize, int x, int y)
1470 long i;
1471 int s = tsize * 4;
1472 int o = s / 8;
1473 bool fAtCorner = fFalse;
1474 pdc->SetPen(*pen_frame);
1475 if (x == 0 && m_layout->Border) {
1476 /* solid left border */
1477 MoveTo(clip.x_min, clip.y_min);
1478 DrawTo(clip.x_min, clip.y_max);
1479 fAtCorner = fTrue;
1480 } else {
1481 if (x > 0 || y > 0) {
1482 MoveTo(clip.x_min, clip.y_min);
1483 DrawTo(clip.x_min, clip.y_min + tsize);
1485 if (s && x > 0 && m_layout->Cutlines) {
1486 /* dashed left border */
1487 i = (clip.y_max - clip.y_min) -
1488 (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1489 for ( ; i > tsize; i -= s) {
1490 MoveTo(clip.x_min, clip.y_max - (i + o));
1491 DrawTo(clip.x_min, clip.y_max - (i - o));
1494 if (x > 0 || y < m_layout->pagesY - 1) {
1495 MoveTo(clip.x_min, clip.y_max - tsize);
1496 DrawTo(clip.x_min, clip.y_max);
1497 fAtCorner = fTrue;
1501 if (y == m_layout->pagesY - 1 && m_layout->Border) {
1502 /* solid top border */
1503 if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1504 DrawTo(clip.x_max, clip.y_max);
1505 fAtCorner = fTrue;
1506 } else {
1507 if (y < m_layout->pagesY - 1 || x > 0) {
1508 if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1509 DrawTo(clip.x_min + tsize, clip.y_max);
1511 if (s && y < m_layout->pagesY - 1 && m_layout->Cutlines) {
1512 /* dashed top border */
1513 i = (clip.x_max - clip.x_min) -
1514 (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1515 for ( ; i > tsize; i -= s) {
1516 MoveTo(clip.x_max - (i + o), clip.y_max);
1517 DrawTo(clip.x_max - (i - o), clip.y_max);
1520 if (y < m_layout->pagesY - 1 || x < m_layout->pagesX - 1) {
1521 MoveTo(clip.x_max - tsize, clip.y_max);
1522 DrawTo(clip.x_max, clip.y_max);
1523 fAtCorner = fTrue;
1524 } else {
1525 fAtCorner = fFalse;
1529 if (x == m_layout->pagesX - 1 && m_layout->Border) {
1530 /* solid right border */
1531 if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1532 DrawTo(clip.x_max, clip.y_min);
1533 fAtCorner = fTrue;
1534 } else {
1535 if (x < m_layout->pagesX - 1 || y < m_layout->pagesY - 1) {
1536 if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1537 DrawTo(clip.x_max, clip.y_max - tsize);
1539 if (s && x < m_layout->pagesX - 1 && m_layout->Cutlines) {
1540 /* dashed right border */
1541 i = (clip.y_max - clip.y_min) -
1542 (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1543 for ( ; i > tsize; i -= s) {
1544 MoveTo(clip.x_max, clip.y_min + (i + o));
1545 DrawTo(clip.x_max, clip.y_min + (i - o));
1548 if (x < m_layout->pagesX - 1 || y > 0) {
1549 MoveTo(clip.x_max, clip.y_min + tsize);
1550 DrawTo(clip.x_max, clip.y_min);
1551 fAtCorner = fTrue;
1552 } else {
1553 fAtCorner = fFalse;
1557 if (y == 0 && m_layout->Border) {
1558 /* solid bottom border */
1559 if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1560 DrawTo(clip.x_min, clip.y_min);
1561 } else {
1562 if (y > 0 || x < m_layout->pagesX - 1) {
1563 if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1564 DrawTo(clip.x_max - tsize, clip.y_min);
1566 if (s && y > 0 && m_layout->Cutlines) {
1567 /* dashed bottom border */
1568 i = (clip.x_max - clip.x_min) -
1569 (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1570 for ( ; i > tsize; i -= s) {
1571 MoveTo(clip.x_min + (i + o), clip.y_min);
1572 DrawTo(clip.x_min + (i - o), clip.y_min);
1575 if (y > 0 || x > 0) {
1576 MoveTo(clip.x_min + tsize, clip.y_min);
1577 DrawTo(clip.x_min, clip.y_min);
1582 bool
1583 svxPrintout::OnPrintPage(int pageNum) {
1584 GetPageSizePixels(&xpPageWidth, &ypPageDepth);
1585 pdc = GetDC();
1586 pdc->SetBackgroundMode(wxTRANSPARENT);
1587 #ifdef AVEN_PRINT_PREVIEW
1588 if (IsPreview()) {
1589 int dcx, dcy;
1590 pdc->GetSize(&dcx, &dcy);
1591 pdc->SetUserScale((double)dcx / xpPageWidth, (double)dcy / ypPageDepth);
1593 #endif
1595 layout * l = m_layout;
1597 int pwidth, pdepth;
1598 GetPageSizeMM(&pwidth, &pdepth);
1599 l->scX = (double)xpPageWidth / pwidth;
1600 l->scY = (double)ypPageDepth / pdepth;
1601 font_scaling_x = l->scX * (25.4 / 72.0);
1602 font_scaling_y = l->scY * (25.4 / 72.0);
1603 long MarginLeft = m_data->GetMarginTopLeft().x;
1604 long MarginTop = m_data->GetMarginTopLeft().y;
1605 long MarginBottom = m_data->GetMarginBottomRight().y;
1606 long MarginRight = m_data->GetMarginBottomRight().x;
1607 xpPageWidth -= (int)(l->scX * (MarginLeft + MarginRight));
1608 ypPageDepth -= (int)(l->scY * (FOOTER_HEIGHT_MM + MarginBottom + MarginTop));
1609 // xpPageWidth -= 1;
1610 pdepth -= FOOTER_HEIGHT_MM;
1611 x_offset = (long)(l->scX * MarginLeft);
1612 y_offset = (long)(l->scY * MarginTop);
1613 l->PaperWidth = pwidth -= MarginLeft + MarginRight;
1614 l->PaperDepth = pdepth -= MarginTop + MarginBottom;
1617 double SIN = sin(rad(l->rot));
1618 double COS = cos(rad(l->rot));
1619 double SINT = sin(rad(l->tilt));
1620 double COST = cos(rad(l->tilt));
1622 NewPage(pageNum, l->pagesX, l->pagesY);
1624 if (l->Legend && pageNum == (l->pagesY - 1) * l->pagesX + 1) {
1625 SetFont(font_default);
1626 draw_info_box();
1629 pdc->SetClippingRegion(x_offset, y_offset, xpPageWidth + 1, ypPageDepth + 1);
1631 const double Sc = 1000 / l->Scale;
1633 int show_mask = l->get_effective_show_mask();
1634 if (show_mask & (LEGS|SURF)) {
1635 for (int f = 0; f != 8; ++f) {
1636 if ((show_mask & (f & img_FLAG_SURFACE) ? SURF : LEGS) == 0) {
1637 // Not showing traverse because of surface/underground status.
1638 continue;
1640 if ((f & img_FLAG_SPLAY) && (show_mask & SPLAYS) == 0) {
1641 // Not showing because it's a splay.
1642 continue;
1644 if (f & img_FLAG_SPLAY) {
1645 pdc->SetPen(*pen_splay);
1646 } else if (f & img_FLAG_SURFACE) {
1647 pdc->SetPen(*pen_surface_leg);
1648 } else {
1649 pdc->SetPen(*pen_leg);
1651 list<traverse>::const_iterator trav = mainfrm->traverses_begin(f);
1652 list<traverse>::const_iterator tend = mainfrm->traverses_end(f);
1653 for ( ; trav != tend; ++trav) {
1654 vector<PointInfo>::const_iterator pos = trav->begin();
1655 vector<PointInfo>::const_iterator end = trav->end();
1656 for ( ; pos != end; ++pos) {
1657 double x = pos->GetX();
1658 double y = pos->GetY();
1659 double z = pos->GetZ();
1660 double X = x * COS - y * SIN;
1661 double Y = z * COST - (x * SIN + y * COS) * SINT;
1662 long px = (long)((X * Sc + l->xOrg) * l->scX);
1663 long py = (long)((Y * Sc + l->yOrg) * l->scY);
1664 if (pos == trav->begin()) {
1665 MoveTo(px, py);
1666 } else {
1667 DrawTo(px, py);
1674 if ((show_mask & XSECT) &&
1675 (l->tilt == 0.0 || l->tilt == 90.0 || l->tilt == -90.0)) {
1676 pdc->SetPen(*pen_splay);
1677 list<vector<XSect> >::const_iterator trav = mainfrm->tubes_begin();
1678 list<vector<XSect> >::const_iterator tend = mainfrm->tubes_end();
1679 for ( ; trav != tend; ++trav) {
1680 if (l->tilt == 0.0) {
1681 PlotUD(*trav);
1682 } else {
1683 // m_layout.tilt is 90.0 or -90.0 due to check above.
1684 PlotLR(*trav);
1689 if (show_mask & (LABELS|STNS)) {
1690 if (show_mask & LABELS) SetFont(font_labels);
1691 list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
1692 while (label != mainfrm->GetLabelsEnd()) {
1693 double px = (*label)->GetX();
1694 double py = (*label)->GetY();
1695 double pz = (*label)->GetZ();
1696 if ((show_mask & SURF) || (*label)->IsUnderground()) {
1697 double X = px * COS - py * SIN;
1698 double Y = pz * COST - (px * SIN + py * COS) * SINT;
1699 long xnew, ynew;
1700 xnew = (long)((X * Sc + l->xOrg) * l->scX);
1701 ynew = (long)((Y * Sc + l->yOrg) * l->scY);
1702 if (show_mask & STNS) {
1703 pdc->SetPen(*pen_cross);
1704 DrawCross(xnew, ynew);
1706 if (show_mask & LABELS) {
1707 pdc->SetTextForeground(colour_labels);
1708 MoveTo(xnew, ynew);
1709 WriteString((*label)->GetText());
1712 ++label;
1716 return true;
1719 void
1720 svxPrintout::GetPageInfo(int *minPage, int *maxPage,
1721 int *pageFrom, int *pageTo)
1723 *minPage = *pageFrom = 1;
1724 *maxPage = *pageTo = m_layout->pages;
1727 bool
1728 svxPrintout::HasPage(int pageNum) {
1729 return (pageNum <= m_layout->pages);
1732 void
1733 svxPrintout::OnBeginPrinting() {
1734 /* Initialise printer routines */
1735 fontsize_labels = 10;
1736 fontsize = 10;
1738 colour_text = colour_labels = *wxBLACK;
1740 wxColour colour_frame, colour_cross, colour_leg, colour_surface_leg;
1741 colour_frame = colour_cross = colour_leg = colour_surface_leg = *wxBLACK;
1743 pen_frame = new wxPen(colour_frame);
1744 pen_cross = new wxPen(colour_cross);
1745 pen_leg = new wxPen(colour_leg);
1746 pen_surface_leg = new wxPen(colour_surface_leg);
1747 pen_splay = new wxPen(wxColour(128, 128, 128));
1749 m_layout->scX = 1;
1750 m_layout->scY = 1;
1752 font_labels = new wxFont(fontsize_labels, wxFONTFAMILY_DEFAULT,
1753 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
1754 false, wxString(fontname_labels, wxConvUTF8),
1755 wxFONTENCODING_ISO8859_1);
1756 font_default = new wxFont(fontsize, wxFONTFAMILY_DEFAULT,
1757 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
1758 false, wxString(fontname, wxConvUTF8),
1759 wxFONTENCODING_ISO8859_1);
1762 void
1763 svxPrintout::OnEndPrinting() {
1764 delete font_labels;
1765 delete font_default;
1766 delete pen_frame;
1767 delete pen_cross;
1768 delete pen_leg;
1769 delete pen_surface_leg;
1770 delete pen_splay;
1774 svxPrintout::check_intersection(long x_p, long y_p)
1776 #define U 1
1777 #define D 2
1778 #define L 4
1779 #define R 8
1780 int mask_p = 0, mask_t = 0;
1781 if (x_p < 0)
1782 mask_p = L;
1783 else if (x_p > xpPageWidth)
1784 mask_p = R;
1786 if (y_p < 0)
1787 mask_p |= D;
1788 else if (y_p > ypPageDepth)
1789 mask_p |= U;
1791 if (x_t < 0)
1792 mask_t = L;
1793 else if (x_t > xpPageWidth)
1794 mask_t = R;
1796 if (y_t < 0)
1797 mask_t |= D;
1798 else if (y_t > ypPageDepth)
1799 mask_t |= U;
1801 #if 0
1802 /* approximation to correct answer */
1803 return !(mask_t & mask_p);
1804 #else
1805 /* One end of the line is on the page */
1806 if (!mask_t || !mask_p) return 1;
1808 /* whole line is above, left, right, or below page */
1809 if (mask_t & mask_p) return 0;
1811 if (mask_t == 0) mask_t = mask_p;
1812 if (mask_t & U) {
1813 double v = (double)(y_p - ypPageDepth) / (y_p - y_t);
1814 return v >= 0 && v <= 1;
1816 if (mask_t & D) {
1817 double v = (double)y_p / (y_p - y_t);
1818 return v >= 0 && v <= 1;
1820 if (mask_t & R) {
1821 double v = (double)(x_p - xpPageWidth) / (x_p - x_t);
1822 return v >= 0 && v <= 1;
1824 wxASSERT(mask_t & L);
1826 double v = (double)x_p / (x_p - x_t);
1827 return v >= 0 && v <= 1;
1829 #endif
1830 #undef U
1831 #undef D
1832 #undef L
1833 #undef R
1836 void
1837 svxPrintout::MoveTo(long x, long y)
1839 x_t = x_offset + x - clip.x_min;
1840 y_t = y_offset + clip.y_max - y;
1843 void
1844 svxPrintout::DrawTo(long x, long y)
1846 long x_p = x_t, y_p = y_t;
1847 x_t = x_offset + x - clip.x_min;
1848 y_t = y_offset + clip.y_max - y;
1849 if (!scan_for_blank_pages) {
1850 pdc->DrawLine(x_p, y_p, x_t, y_t);
1851 } else {
1852 if (check_intersection(x_p, y_p)) fBlankPage = fFalse;
1856 #define POINTS_PER_INCH 72.0
1857 #define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH)
1858 #define PWX_CROSS_SIZE (int)(2 * m_layout->scX / POINTS_PER_MM)
1860 void
1861 svxPrintout::DrawCross(long x, long y)
1863 if (!scan_for_blank_pages) {
1864 MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1865 DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1866 MoveTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1867 DrawTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1868 MoveTo(x, y);
1869 } else {
1870 if ((x + PWX_CROSS_SIZE > clip.x_min &&
1871 x - PWX_CROSS_SIZE < clip.x_max) ||
1872 (y + PWX_CROSS_SIZE > clip.y_min &&
1873 y - PWX_CROSS_SIZE < clip.y_max)) {
1874 fBlankPage = fFalse;
1879 void
1880 svxPrintout::WriteString(const wxString & s)
1882 double xsc, ysc;
1883 pdc->GetUserScale(&xsc, &ysc);
1884 pdc->SetUserScale(xsc * font_scaling_x, ysc * font_scaling_y);
1885 if (!scan_for_blank_pages) {
1886 pdc->DrawText(s,
1887 long(x_t / font_scaling_x),
1888 long(y_t / font_scaling_y) - pdc->GetCharHeight());
1889 } else {
1890 int w, h;
1891 pdc->GetTextExtent(s, &w, &h);
1892 if ((y_t + h > 0 && y_t - h < clip.y_max - clip.y_min) ||
1893 (x_t < clip.x_max - clip.x_min && x_t + w > 0)) {
1894 fBlankPage = fFalse;
1897 pdc->SetUserScale(xsc, ysc);
1900 void
1901 svxPrintout::DrawEllipse(long x, long y, long r, long R)
1903 if (!scan_for_blank_pages) {
1904 x_t = x_offset + x - clip.x_min;
1905 y_t = y_offset + clip.y_max - y;
1906 const wxBrush & save_brush = pdc->GetBrush();
1907 pdc->SetBrush(*wxTRANSPARENT_BRUSH);
1908 pdc->DrawEllipse(x_t - r, y_t - R, 2 * r, 2 * R);
1909 pdc->SetBrush(save_brush);
1910 } else {
1911 /* No need to check - this is only used in the legend. */
1915 void
1916 svxPrintout::SolidRectangle(long x, long y, long w, long h)
1918 long X = x_offset + x - clip.x_min;
1919 long Y = y_offset + clip.y_max - y;
1920 pdc->SetBrush(*wxBLACK_BRUSH);
1921 pdc->DrawRectangle(X, Y - h, w, h);
1924 void
1925 svxPrintout::NewPage(int pg, int pagesX, int pagesY)
1927 pdc->DestroyClippingRegion();
1929 int x, y;
1930 x = (pg - 1) % pagesX;
1931 y = pagesY - 1 - ((pg - 1) / pagesX);
1933 clip.x_min = (long)x * xpPageWidth;
1934 clip.y_min = (long)y * ypPageDepth;
1935 clip.x_max = clip.x_min + xpPageWidth; /* dm/pcl/ps had -1; */
1936 clip.y_max = clip.y_min + ypPageDepth; /* dm/pcl/ps had -1; */
1938 const int FOOTERS = 4;
1939 wxString footer[FOOTERS];
1940 footer[0] = m_layout->title;
1942 double rot = m_layout->rot;
1943 double tilt = m_layout->tilt;
1944 double scale = m_layout->Scale;
1945 switch (m_layout->view) {
1946 case layout::PLAN:
1947 // TRANSLATORS: Used in the footer of printouts to compactly
1948 // indicate this is a plan view and what the viewing angle is.
1949 // Aven will replace %s with the bearing, and %.0f with the scale.
1951 // This message probably doesn't need translating for most languages.
1952 footer[1].Printf(wmsg(/*↑%s 1:%.0f*/233),
1953 format_angle(ANGLE_FMT, rot).c_str(),
1954 scale);
1955 break;
1956 case layout::ELEV:
1957 // TRANSLATORS: Used in the footer of printouts to compactly
1958 // indicate this is an elevation view and what the viewing angle
1959 // is. Aven will replace the %s codes with the bearings to the
1960 // left and right of the viewer, and %.0f with the scale.
1962 // This message probably doesn't need translating for most languages.
1963 footer[1].Printf(wmsg(/*%s↔%s 1:%.0f*/235),
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 scale);
1967 break;
1968 case layout::TILT:
1969 // TRANSLATORS: Used in the footer of printouts to compactly
1970 // indicate this is a tilted elevation view and what the viewing
1971 // angles are. Aven will replace the %s codes with the bearings to
1972 // the left and right of the viewer and the angle the view is
1973 // tilted at, and %.0f with the scale.
1975 // This message probably doesn't need translating for most languages.
1976 footer[1].Printf(wmsg(/*%s↔%s ∡%s 1:%.0f*/236),
1977 format_angle(ANGLE_FMT, fmod(rot + 270.0, 360.0)).c_str(),
1978 format_angle(ANGLE_FMT, fmod(rot + 90.0, 360.0)).c_str(),
1979 format_angle(ANGLE2_FMT, tilt).c_str(),
1980 scale);
1981 break;
1982 case layout::EXTELEV:
1983 // TRANSLATORS: Used in the footer of printouts to compactly
1984 // indicate this is an extended elevation view. Aven will replace
1985 // %.0f with the scale.
1987 // Try to keep the translation short (for example, in English we
1988 // use "Extended" not "Extended elevation") - there is limited room
1989 // in the footer, and the details there are mostly to make it easy
1990 // to check that you have corresponding pages from a multiple page
1991 // printout.
1992 footer[1].Printf(wmsg(/*Extended 1:%.0f*/244), scale);
1993 break;
1996 // TRANSLATORS: N/M meaning page N of M in the page footer of a printout.
1997 footer[2].Printf(wmsg(/*%d/%d*/232), pg, m_layout->pagesX * m_layout->pagesY);
1999 wxString datestamp = m_layout->datestamp;
2000 if (!datestamp.empty()) {
2001 // Remove any timezone suffix (e.g. " UTC" or " +1200").
2002 wxChar ch = datestamp[datestamp.size() - 1];
2003 if (ch >= 'A' && ch <= 'Z') {
2004 for (size_t i = datestamp.size() - 1; i; --i) {
2005 ch = datestamp[i];
2006 if (ch < 'A' || ch > 'Z') {
2007 if (ch == ' ') datestamp.resize(i);
2008 break;
2011 } else if (ch >= '0' && ch <= '9') {
2012 for (size_t i = datestamp.size() - 1; i; --i) {
2013 ch = datestamp[i];
2014 if (ch < '0' || ch > '9') {
2015 if ((ch == '-' || ch == '+') && datestamp[--i] == ' ')
2016 datestamp.resize(i);
2017 break;
2022 // Remove any day prefix (e.g. "Mon,").
2023 for (size_t i = 0; i != datestamp.size(); ++i) {
2024 if (datestamp[i] == ',' && i + 1 != datestamp.size()) {
2025 // Also skip a space after the comma.
2026 if (datestamp[i + 1] == ' ') ++i;
2027 datestamp.erase(0, i + 1);
2028 break;
2033 // TRANSLATORS: Used in the footer of printouts to compactly indicate that
2034 // the date which follows is the date that the survey data was processed.
2036 // Aven will replace %s with a string giving the date and time (e.g.
2037 // "2015-06-09 12:40:44").
2038 footer[3].Printf(wmsg(/*Processed: %s*/167), datestamp.c_str());
2040 const wxChar * footer_sep = wxT(" ");
2041 int fontsize_footer = fontsize_labels;
2042 wxFont * font_footer;
2043 font_footer = new wxFont(fontsize_footer, wxFONTFAMILY_DEFAULT,
2044 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
2045 false, wxString(fontname_labels, wxConvUTF8),
2046 wxFONTENCODING_UTF8);
2047 font_footer->Scale(font_scaling_x);
2048 SetFont(font_footer);
2049 int w[FOOTERS], ws, h;
2050 pdc->GetTextExtent(footer_sep, &ws, &h);
2051 int wtotal = ws * (FOOTERS - 1);
2052 for (int i = 0; i < FOOTERS; ++i) {
2053 pdc->GetTextExtent(footer[i], &w[i], &h);
2054 wtotal += w[i];
2057 long X = x_offset;
2058 long Y = y_offset + ypPageDepth + (long)(7 * m_layout->scY) - pdc->GetCharHeight();
2060 if (wtotal > xpPageWidth) {
2061 // Rescale the footer so it fits.
2062 double rescale = double(wtotal) / xpPageWidth;
2063 double xsc, ysc;
2064 pdc->GetUserScale(&xsc, &ysc);
2065 pdc->SetUserScale(xsc / rescale, ysc / rescale);
2066 SetFont(font_footer);
2067 wxString fullfooter = footer[0];
2068 for (int i = 1; i < FOOTERS - 1; ++i) {
2069 fullfooter += footer_sep;
2070 fullfooter += footer[i];
2072 pdc->DrawText(fullfooter, X * rescale, Y * rescale);
2073 // Draw final item right aligned to avoid misaligning.
2074 wxRect rect(x_offset * rescale, Y * rescale,
2075 xpPageWidth * rescale, pdc->GetCharHeight() * rescale);
2076 pdc->DrawLabel(footer[FOOTERS - 1], rect, wxALIGN_RIGHT|wxALIGN_TOP);
2077 pdc->SetUserScale(xsc, ysc);
2078 } else {
2079 // Space out the elements of the footer to fill the line.
2080 double extra = double(xpPageWidth - wtotal) / (FOOTERS - 1);
2081 for (int i = 0; i < FOOTERS - 1; ++i) {
2082 pdc->DrawText(footer[i], X + extra * i, Y);
2083 X += ws + w[i];
2085 // Draw final item right aligned to avoid misaligning.
2086 wxRect rect(x_offset, Y, xpPageWidth, pdc->GetCharHeight());
2087 pdc->DrawLabel(footer[FOOTERS - 1], rect, wxALIGN_RIGHT|wxALIGN_TOP);
2089 drawticks((int)(9 * m_layout->scX / POINTS_PER_MM), x, y);
2092 void
2093 svxPrintout::PlotLR(const vector<XSect> & centreline)
2095 assert(centreline.size() > 1);
2096 XSect prev_pt_v;
2097 Vector3 last_right(1.0, 0.0, 0.0);
2099 const double Sc = 1000 / m_layout->Scale;
2100 const double SIN = sin(rad(m_layout->rot));
2101 const double COS = cos(rad(m_layout->rot));
2103 vector<XSect>::const_iterator i = centreline.begin();
2104 vector<XSect>::size_type segment = 0;
2105 while (i != centreline.end()) {
2106 // get the coordinates of this vertex
2107 const XSect & pt_v = *i++;
2109 Vector3 right;
2111 const Vector3 up_v(0.0, 0.0, 1.0);
2113 if (segment == 0) {
2114 assert(i != centreline.end());
2115 // first segment
2117 // get the coordinates of the next vertex
2118 const XSect & next_pt_v = *i;
2120 // calculate vector from this pt to the next one
2121 Vector3 leg_v = next_pt_v - pt_v;
2123 // obtain a vector in the LRUD plane
2124 right = leg_v * up_v;
2125 if (right.magnitude() == 0) {
2126 right = last_right;
2127 } else {
2128 last_right = right;
2130 } else if (segment + 1 == centreline.size()) {
2131 // last segment
2133 // Calculate vector from the previous pt to this one.
2134 Vector3 leg_v = pt_v - prev_pt_v;
2136 // Obtain a horizontal vector in the LRUD plane.
2137 right = leg_v * up_v;
2138 if (right.magnitude() == 0) {
2139 right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
2140 } else {
2141 last_right = right;
2143 } else {
2144 assert(i != centreline.end());
2145 // Intermediate segment.
2147 // Get the coordinates of the next vertex.
2148 const XSect & next_pt_v = *i;
2150 // Calculate vectors from this vertex to the
2151 // next vertex, and from the previous vertex to
2152 // this one.
2153 Vector3 leg1_v = pt_v - prev_pt_v;
2154 Vector3 leg2_v = next_pt_v - pt_v;
2156 // Obtain horizontal vectors perpendicular to
2157 // both legs, then normalise and average to get
2158 // a horizontal bisector.
2159 Vector3 r1 = leg1_v * up_v;
2160 Vector3 r2 = leg2_v * up_v;
2161 r1.normalise();
2162 r2.normalise();
2163 right = r1 + r2;
2164 if (right.magnitude() == 0) {
2165 // This is the "mid-pitch" case...
2166 right = last_right;
2168 last_right = right;
2171 // Scale to unit vectors in the LRUD plane.
2172 right.normalise();
2174 Double l = pt_v.GetL();
2175 Double r = pt_v.GetR();
2177 if (l >= 0 || r >= 0) {
2178 // Get the x and y coordinates of the survey station
2179 double pt_X = pt_v.GetX() * COS - pt_v.GetY() * SIN;
2180 double pt_Y = pt_v.GetX() * SIN + pt_v.GetY() * COS;
2181 long pt_x = (long)((pt_X * Sc + m_layout->xOrg) * m_layout->scX);
2182 long pt_y = (long)((pt_Y * Sc + m_layout->yOrg) * m_layout->scY);
2184 // Calculate dimensions for the right arrow
2185 double COSR = right.GetX();
2186 double SINR = right.GetY();
2187 long CROSS_MAJOR = (COSR + SINR) * PWX_CROSS_SIZE;
2188 long CROSS_MINOR = (COSR - SINR) * PWX_CROSS_SIZE;
2190 if (l >= 0) {
2191 // Get the x and y coordinates of the end of the left arrow
2192 Vector3 p = pt_v - right * l;
2193 double X = p.GetX() * COS - p.GetY() * SIN;
2194 double Y = (p.GetX() * SIN + p.GetY() * COS);
2195 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2196 long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
2198 // Draw the arrow stem
2199 MoveTo(pt_x, pt_y);
2200 DrawTo(x, y);
2202 // Rotate the arrow by the page rotation
2203 long dx1 = (+CROSS_MINOR) * COS - (+CROSS_MAJOR) * SIN;
2204 long dy1 = (+CROSS_MINOR) * SIN + (+CROSS_MAJOR) * COS;
2205 long dx2 = (+CROSS_MAJOR) * COS - (-CROSS_MINOR) * SIN;
2206 long dy2 = (+CROSS_MAJOR) * SIN + (-CROSS_MINOR) * COS;
2208 // Draw the arrow
2209 MoveTo(x + dx1, y + dy1);
2210 DrawTo(x, y);
2211 DrawTo(x + dx2, y + dy2);
2214 if (r >= 0) {
2215 // Get the x and y coordinates of the end of the right arrow
2216 Vector3 p = pt_v + right * r;
2217 double X = p.GetX() * COS - p.GetY() * SIN;
2218 double Y = (p.GetX() * SIN + p.GetY() * COS);
2219 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2220 long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
2222 // Draw the arrow stem
2223 MoveTo(pt_x, pt_y);
2224 DrawTo(x, y);
2226 // Rotate the arrow by the page rotation
2227 long dx1 = (-CROSS_MINOR) * COS - (-CROSS_MAJOR) * SIN;
2228 long dy1 = (-CROSS_MINOR) * SIN + (-CROSS_MAJOR) * COS;
2229 long dx2 = (-CROSS_MAJOR) * COS - (+CROSS_MINOR) * SIN;
2230 long dy2 = (-CROSS_MAJOR) * SIN + (+CROSS_MINOR) * COS;
2232 // Draw the arrow
2233 MoveTo(x + dx1, y + dy1);
2234 DrawTo(x, y);
2235 DrawTo(x + dx2, y + dy2);
2239 prev_pt_v = pt_v;
2241 ++segment;
2245 void
2246 svxPrintout::PlotUD(const vector<XSect> & centreline)
2248 assert(centreline.size() > 1);
2249 const double Sc = 1000 / m_layout->Scale;
2251 vector<XSect>::const_iterator i = centreline.begin();
2252 while (i != centreline.end()) {
2253 // get the coordinates of this vertex
2254 const XSect & pt_v = *i++;
2256 Double u = pt_v.GetU();
2257 Double d = pt_v.GetD();
2259 if (u >= 0 || d >= 0) {
2260 // Get the coordinates of the survey point
2261 Vector3 p = pt_v;
2262 double SIN = sin(rad(m_layout->rot));
2263 double COS = cos(rad(m_layout->rot));
2264 double X = p.GetX() * COS - p.GetY() * SIN;
2265 double Y = p.GetZ();
2266 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2267 long pt_y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scX);
2269 if (u >= 0) {
2270 // Get the y coordinate of the up arrow
2271 long y = (long)(((Y + u) * Sc + m_layout->yOrg) * m_layout->scY);
2273 // Draw the arrow stem
2274 MoveTo(x, pt_y);
2275 DrawTo(x, y);
2277 // Draw the up 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);
2283 if (d >= 0) {
2284 // Get the y coordinate of the down arrow
2285 long y = (long)(((Y - d) * Sc + m_layout->yOrg) * m_layout->scY);
2287 // Draw the arrow stem
2288 MoveTo(x, pt_y);
2289 DrawTo(x, y);
2291 // Draw the down arrow
2292 MoveTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
2293 DrawTo(x, y);
2294 DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);