Use modern wxFont constants
[survex.git] / src / printing.cc
blob5a8c13dbeb64287d2f6c0a001b08452388d04d5b
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_CENTER_VERTICAL|wxALL, 5);
446 formatbox->Add(m_format, 0, wxALIGN_CENTER_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_CENTER_VERTICAL|wxALL, 5);
477 m_scalebox->Add(m_scale, 0, wxALIGN_CENTER_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_CENTER_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, wxSP_ARROW_KEYS|wxSP_WRAP);
503 m_bearing->SetRange(0.0, 360.0);
504 m_bearing->SetDigits(ANGLE_DP);
505 anglebox->Add(m_bearing, 0, wxALIGN_CENTER|wxALL, 5);
506 /* TRANSLATORS: Used in the print dialog: */
507 tilt_label = new wxStaticText(this, -1, wmsg(/*Tilt angle*/263));
508 anglebox->Add(tilt_label, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_LEFT|wxALL, 5);
509 m_tilt = new wxSpinCtrlDouble(this, svx_TILT);
510 m_tilt->SetRange(-90.0, 90.0);
511 m_tilt->SetDigits(ANGLE_DP);
512 anglebox->Add(m_tilt, 0, wxALIGN_CENTER|wxALL, 5);
514 m_viewbox->Add(anglebox, 0, wxALIGN_LEFT|wxALL, 0);
516 wxBoxSizer * planelevsizer = new wxBoxSizer(wxHORIZONTAL);
517 planelevsizer->Add(new wxButton(this, svx_PLAN, wmsg(/*P&lan view*/117)),
518 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
519 planelevsizer->Add(new wxButton(this, svx_ELEV, wmsg(/*&Elevation*/285)),
520 0, wxALIGN_CENTRE_VERTICAL|wxALL, 5);
522 m_viewbox->Add(planelevsizer, 0, wxALIGN_LEFT|wxALL, 5);
525 /* TRANSLATORS: Here a "survey leg" is a set of measurements between two
526 * "survey stations". */
527 v3->Add(new wxCheckBox(this, svx_LEGS, wmsg(/*Underground Survey Legs*/262),
528 wxDefaultPosition, wxDefaultSize, 0,
529 BitValidator(&m_layout.show_mask, LEGS)),
530 0, wxALIGN_LEFT|wxALL, 2);
531 /* TRANSLATORS: Here a "survey leg" is a set of measurements between two
532 * "survey stations". */
533 v3->Add(new wxCheckBox(this, svx_SURFACE, wmsg(/*Sur&face Survey Legs*/403),
534 wxDefaultPosition, wxDefaultSize, 0,
535 BitValidator(&m_layout.show_mask, SURF)),
536 0, wxALIGN_LEFT|wxALL, 2);
537 v3->Add(new wxCheckBox(this, svx_SPLAYS, wmsg(/*Spla&y Legs*/406),
538 wxDefaultPosition, wxDefaultSize, 0,
539 BitValidator(&m_layout.show_mask, SPLAYS)),
540 0, wxALIGN_LEFT|wxALL, 2);
541 v3->Add(new wxCheckBox(this, svx_STATIONS, wmsg(/*Crosses*/261),
542 wxDefaultPosition, wxDefaultSize, 0,
543 BitValidator(&m_layout.show_mask, STNS)),
544 0, wxALIGN_LEFT|wxALL, 2);
545 v3->Add(new wxCheckBox(this, svx_NAMES, wmsg(/*Station Names*/260),
546 wxDefaultPosition, wxDefaultSize, 0,
547 BitValidator(&m_layout.show_mask, LABELS)),
548 0, wxALIGN_LEFT|wxALL, 2);
549 v3->Add(new wxCheckBox(this, svx_ENTS, wmsg(/*Entrances*/418),
550 wxDefaultPosition, wxDefaultSize, 0,
551 BitValidator(&m_layout.show_mask, ENTS)),
552 0, wxALIGN_LEFT|wxALL, 2);
553 v3->Add(new wxCheckBox(this, svx_FIXES, wmsg(/*Fixed Points*/419),
554 wxDefaultPosition, wxDefaultSize, 0,
555 BitValidator(&m_layout.show_mask, FIXES)),
556 0, wxALIGN_LEFT|wxALL, 2);
557 v3->Add(new wxCheckBox(this, svx_EXPORTS, wmsg(/*Exported Stations*/420),
558 wxDefaultPosition, wxDefaultSize, 0,
559 BitValidator(&m_layout.show_mask, EXPORTS)),
560 0, wxALIGN_LEFT|wxALL, 2);
561 v3->Add(new wxCheckBox(this, svx_XSECT, wmsg(/*Cross-sections*/393),
562 wxDefaultPosition, wxDefaultSize, 0,
563 BitValidator(&m_layout.show_mask, XSECT)),
564 0, wxALIGN_LEFT|wxALL, 2);
565 if (!printing) {
566 v3->Add(new wxCheckBox(this, svx_WALLS, wmsg(/*Walls*/394),
567 wxDefaultPosition, wxDefaultSize, 0,
568 BitValidator(&m_layout.show_mask, WALLS)),
569 0, wxALIGN_LEFT|wxALL, 2);
570 // TRANSLATORS: Label for checkbox which controls whether there's a
571 // layer in the exported file (for formats such as DXF and SVG)
572 // containing polygons for the inside of cave passages).
573 v3->Add(new wxCheckBox(this, svx_PASSAGES, wmsg(/*Passages*/395),
574 wxDefaultPosition, wxDefaultSize, 0,
575 BitValidator(&m_layout.show_mask, PASG)),
576 0, wxALIGN_LEFT|wxALL, 2);
577 v3->Add(new wxCheckBox(this, svx_CENTRED, wmsg(/*Origin in centre*/421),
578 wxDefaultPosition, wxDefaultSize, 0,
579 BitValidator(&m_layout.show_mask, CENTRED)),
580 0, wxALIGN_LEFT|wxALL, 2);
581 v3->Add(new wxCheckBox(this, svx_FULLCOORDS, wmsg(/*Full coordinates*/422),
582 wxDefaultPosition, wxDefaultSize, 0,
583 BitValidator(&m_layout.show_mask, FULL_COORDS)),
584 0, wxALIGN_LEFT|wxALL, 2);
586 if (printing) {
587 /* TRANSLATORS: used in the print dialog - controls drawing lines
588 * around each page */
589 v3->Add(new wxCheckBox(this, svx_BORDERS, wmsg(/*Page Borders*/264),
590 wxDefaultPosition, wxDefaultSize, 0,
591 wxGenericValidator(&m_layout.Border)),
592 0, wxALIGN_LEFT|wxALL, 2);
593 /* TRANSLATORS: will be used in the print dialog - check this to print
594 * blank pages (otherwise they’ll be skipped to save paper) */
595 // m_blanks = new wxCheckBox(this, svx_BLANKS, wmsg(/*Blank Pages*/266));
596 // v3->Add(m_blanks, 0, wxALIGN_LEFT|wxALL, 2);
597 /* TRANSLATORS: As in the legend on a map. Used in the print dialog -
598 * controls drawing the box at the lower left with survey name, view
599 * angles, etc */
600 v3->Add(new wxCheckBox(this, svx_LEGEND, wmsg(/*Legend*/265),
601 wxDefaultPosition, wxDefaultSize, 0,
602 wxGenericValidator(&m_layout.Legend)),
603 0, wxALIGN_LEFT|wxALL, 2);
606 h1->Add(v3, 0, wxALIGN_LEFT|wxALL, 5);
607 h1->Add(m_viewbox, 0, wxALIGN_LEFT|wxLEFT, 5);
609 if (!printing) {
610 /* TRANSLATORS: The PROJ library is used to do coordinate
611 * transformations (https://trac.osgeo.org/proj/) - if the .3d file
612 * doesn't contain details of the coordinate projection in use, the
613 * user must specify it here for export formats which need to know it
614 * (e.g. GPX).
616 h2->Add(new wxStaticText(this, svx_PROJ_LABEL, wmsg(/*Coordinate projection*/440)),
617 0, wxLEFT|wxALIGN_CENTRE_VERTICAL, 5);
618 long style = 0;
619 if (!m_layout.cs_proj.empty()) {
620 // If the input file specified the coordinate system, don't let the
621 // user mess with it.
622 style = wxTE_READONLY;
623 } else {
624 #if 0 // FIXME: Is it a good idea to save this?
625 wxConfigBase * cfg = wxConfigBase::Get();
626 wxString input_projection;
627 cfg->Read(wxT("input_projection"), &input_projection);
628 if (!input_projection.empty())
629 proj_edit.SetValue(input_projection);
630 #endif
632 wxTextCtrl * proj_edit = new wxTextCtrl(this, svx_PROJ, m_layout.cs_proj,
633 wxDefaultPosition, wxDefaultSize,
634 style);
635 h2->Add(proj_edit, 1, wxALL|wxEXPAND|wxALIGN_CENTRE_VERTICAL, 5);
636 v1->Add(h2, 0, wxALIGN_LEFT|wxEXPAND, 5);
639 v1->Add(h1, 0, wxALIGN_LEFT|wxALL, 5);
641 // When we enable/disable checkboxes in the export dialog, ideally we'd
642 // like the dialog to resize, but not sure how to achieve that, so we
643 // add a stretchable spacer here so at least the buttons stay in the
644 // lower right corner.
645 v1->AddStretchSpacer();
647 wxButton * but;
648 but = new wxButton(this, wxID_CANCEL);
649 h3->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
650 if (printing) {
651 #ifdef AVEN_PRINT_PREVIEW
652 but = new wxButton(this, wxID_PREVIEW);
653 h3->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
654 but = new wxButton(this, wxID_PRINT);
655 #else
656 but = new wxButton(this, wxID_PRINT, wmsg(/*&Print...*/400));
657 #endif
658 } else {
659 /* TRANSLATORS: The text on the action button in the "Export" settings
660 * dialog */
661 but = new wxButton(this, svx_EXPORT, wmsg(/*&Export...*/230));
663 but->SetDefault();
664 h3->Add(but, 0, wxALIGN_RIGHT|wxALL, 5);
665 v1->Add(h3, 0, wxALIGN_RIGHT|wxALL, 5);
667 SetAutoLayout(true);
668 SetSizer(v1);
669 v1->SetSizeHints(this);
671 LayoutToUI();
672 SomethingChanged(0);
675 void
676 svxPrintDlg::OnPrint(wxCommandEvent&) {
677 SomethingChanged(0);
678 TransferDataFromWindow();
679 wxPageSetupDialogData * psdd = wxGetApp().GetPageSetupDialogData();
680 wxPrintDialogData pd(psdd->GetPrintData());
681 wxPrinter pr(&pd);
682 svxPrintout po(mainfrm, &m_layout, psdd, m_File);
683 if (m_layout.SkipBlank) {
684 // FIXME: wx's printing requires a contiguous range of valid page
685 // numbers. To achieve that, we need to run a scan for blank pages
686 // here, so that GetPageInfo() knows what range to return, and so
687 // that OnPrintPage() can map a page number back to where in the
688 // MxN multi-page layout.
689 #if 0
690 po.scan_for_blank_pages = true;
691 for (int page = 1; page <= m_layout->pages; ++page) {
692 po.fBlankPage = fTrue;
693 po.OnPrintPage(page);
694 // FIXME: Do something with po.fBlankPage
696 po.scan_for_blank_pages = false;
697 #endif
699 if (pr.Print(this, &po, true)) {
700 // Close the print dialog if printing succeeded.
701 Destroy();
705 void
706 svxPrintDlg::OnExport(wxCommandEvent&) {
707 UIToLayout();
708 TransferDataFromWindow();
709 wxString leaf;
710 wxFileName::SplitPath(m_File, NULL, NULL, &leaf, NULL, wxPATH_NATIVE);
711 unsigned format_idx = ((wxChoice*)FindWindow(svx_FORMAT))->GetSelection();
712 leaf += wxString::FromUTF8(extension[format_idx]);
714 wxString filespec = wmsg(msg_filetype[format_idx]);
715 filespec += wxT("|*");
716 filespec += wxString::FromUTF8(extension[format_idx]);
717 filespec += wxT("|");
718 filespec += wmsg(/*All files*/208);
719 filespec += wxT("|");
720 filespec += wxFileSelectorDefaultWildcardStr;
722 /* TRANSLATORS: Title of file dialog to choose name and type of exported
723 * file. */
724 wxFileDialog dlg(this, wmsg(/*Export as:*/401), wxString(), leaf,
725 filespec, wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
726 if (dlg.ShowModal() == wxID_OK) {
727 wxString input_projection = ((wxTextCtrl*)FindWindow(svx_PROJ))->GetValue();
728 double grid = 100; // metres
729 double text_height = 0.6;
730 double marker_size = 0.8;
732 try {
733 const wxString& export_fnm = dlg.GetPath();
734 unsigned mask = format_info[format_idx];
735 double rot, tilt;
736 if (mask & EXPORT_3D) {
737 rot = 0.0;
738 tilt = -90.0;
739 } else {
740 rot = m_layout.rot;
741 tilt = m_layout.tilt;
743 if (!Export(export_fnm, m_layout.title,
744 m_layout.datestamp, m_layout.datestamp_numeric, mainfrm,
745 rot, tilt, m_layout.get_effective_show_mask(),
746 export_format(format_idx), input_projection.utf8_str(),
747 grid, text_height, marker_size, m_layout.Scale)) {
748 wxString m = wxString::Format(wmsg(/*Couldn’t write file “%s”*/402).c_str(),
749 export_fnm.c_str());
750 wxGetApp().ReportError(m);
752 } catch (const wxString & m) {
753 wxGetApp().ReportError(m);
756 Destroy();
759 #ifdef AVEN_PRINT_PREVIEW
760 void
761 svxPrintDlg::OnPreview(wxCommandEvent&) {
762 SomethingChanged(0);
763 TransferDataFromWindow();
764 wxPageSetupDialogData * psdd = wxGetApp().GetPageSetupDialogData();
765 wxPrintDialogData pd(psdd->GetPrintData());
766 wxPrintPreview* pv;
767 pv = new wxPrintPreview(new svxPrintout(mainfrm, &m_layout, psdd, m_File),
768 new svxPrintout(mainfrm, &m_layout, psdd, m_File),
769 &pd);
770 // TRANSLATORS: Title of the print preview dialog
771 wxPreviewFrame *frame = new wxPreviewFrame(pv, mainfrm, wmsg(/*Print Preview*/398));
772 frame->Initialize();
774 // Size preview frame so that all of the controlbar and canvas can be seen
775 // if possible.
776 int w, h;
777 // GetBestSize gives us the width needed to show the whole controlbar.
778 frame->GetBestSize(&w, &h);
779 if (h < w) {
780 // On wxGTK at least, GetBestSize() returns much too small a height.
781 h = w * 6 / 5;
783 // Ensure that we don't make the window bigger than the screen.
784 // Use wxGetClientDisplayRect() so we don't cover the MS Windows
785 // task bar either.
786 wxRect disp = wxGetClientDisplayRect();
787 if (w > disp.GetWidth()) w = disp.GetWidth();
788 if (h > disp.GetHeight()) h = disp.GetHeight();
789 // Centre the window within the "ClientDisplayRect".
790 int x = disp.GetLeft() + (disp.GetWidth() - w) / 2;
791 int y = disp.GetTop() + (disp.GetHeight() - h) / 2;
792 frame->SetSize(x, y, w, h);
794 frame->Show();
796 #endif
798 void
799 svxPrintDlg::OnPlan(wxCommandEvent&) {
800 m_tilt->SetValue(-90.0);
801 SomethingChanged(svx_TILT);
804 void
805 svxPrintDlg::OnElevation(wxCommandEvent&) {
806 m_tilt->SetValue(0.0);
807 SomethingChanged(svx_TILT);
810 void
811 svxPrintDlg::OnPlanUpdate(wxUpdateUIEvent& e) {
812 e.Enable(m_tilt->GetValue() != -90.0);
815 void
816 svxPrintDlg::OnElevationUpdate(wxUpdateUIEvent& e) {
817 e.Enable(m_tilt->GetValue() != 0.0);
820 void
821 svxPrintDlg::OnChangeSpin(wxSpinDoubleEvent& e) {
822 SomethingChanged(e.GetId());
825 void
826 svxPrintDlg::OnChange(wxCommandEvent& e) {
827 if (e.GetId() == svx_SCALE && m_scale) {
828 default_scale_print = m_scale->GetValue();
829 if (default_scale_print != scales[0]) {
830 // Don't store "One Page" for use when exporting.
831 default_scale_export = default_scale_print;
834 SomethingChanged(e.GetId());
837 void
838 svxPrintDlg::OnCancel(wxCommandEvent&) {
839 if (close_after)
840 mainfrm->Close();
841 Destroy();
844 void
845 svxPrintDlg::SomethingChanged(int control_id) {
846 if ((control_id == 0 || control_id == svx_FORMAT) && m_format) {
847 // Update the shown/hidden fields for the newly selected export filter.
848 int new_filter_idx = m_format->GetSelection();
849 if (new_filter_idx != wxNOT_FOUND) {
850 unsigned mask = format_info[new_filter_idx];
851 static const struct { int id; unsigned mask; } controls[] = {
852 { svx_LEGS, LEGS },
853 { svx_SURFACE, SURF },
854 { svx_SPLAYS, SPLAYS },
855 { svx_STATIONS, STNS },
856 { svx_NAMES, LABELS },
857 { svx_XSECT, XSECT },
858 { svx_WALLS, WALLS },
859 { svx_PASSAGES, PASG },
860 { svx_ENTS, ENTS },
861 { svx_FIXES, FIXES },
862 { svx_EXPORTS, EXPORTS },
863 { svx_CENTRED, CENTRED },
864 { svx_FULLCOORDS, FULL_COORDS },
865 { svx_PROJ_LABEL, PROJ },
866 { svx_PROJ, PROJ },
868 static unsigned n_controls = sizeof(controls) / sizeof(controls[0]);
869 for (unsigned i = 0; i != n_controls; ++i) {
870 wxWindow * control = FindWindow(controls[i].id);
871 if (control) control->Show(mask & controls[i].mask);
873 m_scalebox->Show(bool(mask & SCALE));
874 m_viewbox->Show(!bool(mask & EXPORT_3D));
875 GetSizer()->Layout();
876 if (control_id == svx_FORMAT) {
877 wxConfigBase * cfg = wxConfigBase::Get();
878 cfg->Write(wxT("export_format"), formats[new_filter_idx]);
883 UIToLayout();
885 if (m_printSize || m_scale) {
886 // Update the bounding box.
887 RecalcBounds();
889 if (m_scale) {
890 if (!(m_scale->GetValue()).ToDouble(&(m_layout.Scale)) ||
891 m_layout.Scale == 0.0) {
892 m_layout.pick_scale(1, 1);
897 if (m_printSize && m_layout.xMax >= m_layout.xMin) {
898 m_layout.pages_required();
899 m_printSize->SetLabel(wxString::Format(wmsg(/*%d pages (%dx%d)*/257), m_layout.pages, m_layout.pagesX, m_layout.pagesY));
903 void
904 svxPrintDlg::LayoutToUI()
906 // m_blanks->SetValue(m_layout.SkipBlank);
907 if (m_layout.view != layout::EXTELEV) {
908 m_tilt->SetValue(m_layout.tilt);
909 m_bearing->SetValue(m_layout.rot);
912 if (m_scale && m_layout.Scale != 0) {
913 // Do this last as it causes an OnChange message which calls UIToLayout
914 wxString temp;
915 temp << m_layout.Scale;
916 m_scale->SetValue(temp);
920 void
921 svxPrintDlg::UIToLayout()
923 // m_layout.SkipBlank = m_blanks->IsChecked();
925 if (m_layout.view != layout::EXTELEV && m_tilt) {
926 m_layout.tilt = m_tilt->GetValue();
927 if (m_layout.tilt == -90.0) {
928 m_layout.view = layout::PLAN;
929 } else if (m_layout.tilt == 0.0) {
930 m_layout.view = layout::ELEV;
931 } else {
932 m_layout.view = layout::TILT;
935 bool enable_passage_opts = (m_layout.view != layout::TILT);
936 wxWindow * win;
937 win = FindWindow(svx_XSECT);
938 if (win) win->Enable(enable_passage_opts);
939 win = FindWindow(svx_WALLS);
940 if (win) win->Enable(enable_passage_opts);
941 win = FindWindow(svx_PASSAGES);
942 if (win) win->Enable(enable_passage_opts);
944 m_layout.rot = m_bearing->GetValue();
948 void
949 svxPrintDlg::RecalcBounds()
951 m_layout.yMax = m_layout.xMax = -DBL_MAX;
952 m_layout.yMin = m_layout.xMin = DBL_MAX;
954 double SIN = sin(rad(m_layout.rot));
955 double COS = cos(rad(m_layout.rot));
956 double SINT = sin(rad(m_layout.tilt));
957 double COST = cos(rad(m_layout.tilt));
959 int show_mask = m_layout.get_effective_show_mask();
960 if (show_mask & LEGS) {
961 for (int f = 0; f != 8; ++f) {
962 if ((show_mask & (f & img_FLAG_SURFACE) ? SURF : LEGS) == 0) {
963 // Not showing traverse because of surface/underground status.
964 continue;
966 if ((f & img_FLAG_SPLAY) && (show_mask & SPLAYS) == 0) {
967 // Not showing because it's a splay.
968 continue;
970 list<traverse>::const_iterator trav = mainfrm->traverses_begin(f);
971 list<traverse>::const_iterator tend = mainfrm->traverses_end(f);
972 for ( ; trav != tend; ++trav) {
973 vector<PointInfo>::const_iterator pos = trav->begin();
974 vector<PointInfo>::const_iterator end = trav->end();
975 for ( ; pos != end; ++pos) {
976 double x = pos->GetX();
977 double y = pos->GetY();
978 double z = pos->GetZ();
979 double X = x * COS - y * SIN;
980 if (X > m_layout.xMax) m_layout.xMax = X;
981 if (X < m_layout.xMin) m_layout.xMin = X;
982 double Y = z * COST - (x * SIN + y * COS) * SINT;
983 if (Y > m_layout.yMax) m_layout.yMax = Y;
984 if (Y < m_layout.yMin) m_layout.yMin = Y;
990 if ((show_mask & XSECT) &&
991 (m_layout.tilt == 0.0 || m_layout.tilt == 90.0 || m_layout.tilt == -90.0)) {
992 list<vector<XSect> >::const_iterator trav = mainfrm->tubes_begin();
993 list<vector<XSect> >::const_iterator tend = mainfrm->tubes_end();
994 for ( ; trav != tend; ++trav) {
995 XSect prev_pt_v;
996 Vector3 last_right(1.0, 0.0, 0.0);
998 vector<XSect>::const_iterator i = trav->begin();
999 vector<XSect>::size_type segment = 0;
1000 while (i != trav->end()) {
1001 // get the coordinates of this vertex
1002 const XSect & pt_v = *i++;
1003 if (m_layout.tilt == 0.0) {
1004 Double u = pt_v.GetU();
1005 Double d = pt_v.GetD();
1007 if (u >= 0 || d >= 0) {
1008 double x = pt_v.GetX();
1009 double y = pt_v.GetY();
1010 double z = pt_v.GetZ();
1011 double X = x * COS - y * SIN;
1012 double Y = z * COST - (x * SIN + y * COS) * SINT;
1014 if (X > m_layout.xMax) m_layout.xMax = X;
1015 if (X < m_layout.xMin) m_layout.xMin = X;
1016 double U = Y + max(0.0, pt_v.GetU());
1017 if (U > m_layout.yMax) m_layout.yMax = U;
1018 double D = Y - max(0.0, pt_v.GetD());
1019 if (D < m_layout.yMin) m_layout.yMin = D;
1021 } else {
1022 // More complex, and this duplicates the algorithm from
1023 // PlotLR() - we should try to share that, maybe via a
1024 // template.
1025 Vector3 right;
1027 const Vector3 up_v(0.0, 0.0, 1.0);
1029 if (segment == 0) {
1030 assert(i != trav->end());
1031 // first segment
1033 // get the coordinates of the next vertex
1034 const XSect & next_pt_v = *i;
1036 // calculate vector from this pt to the next one
1037 Vector3 leg_v = next_pt_v - pt_v;
1039 // obtain a vector in the LRUD plane
1040 right = leg_v * up_v;
1041 if (right.magnitude() == 0) {
1042 right = last_right;
1043 } else {
1044 last_right = right;
1046 } else if (segment + 1 == trav->size()) {
1047 // last segment
1049 // Calculate vector from the previous pt to this one.
1050 Vector3 leg_v = pt_v - prev_pt_v;
1052 // Obtain a horizontal vector in the LRUD plane.
1053 right = leg_v * up_v;
1054 if (right.magnitude() == 0) {
1055 right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
1056 } else {
1057 last_right = right;
1059 } else {
1060 assert(i != trav->end());
1061 // Intermediate segment.
1063 // Get the coordinates of the next vertex.
1064 const XSect & next_pt_v = *i;
1066 // Calculate vectors from this vertex to the
1067 // next vertex, and from the previous vertex to
1068 // this one.
1069 Vector3 leg1_v = pt_v - prev_pt_v;
1070 Vector3 leg2_v = next_pt_v - pt_v;
1072 // Obtain horizontal vectors perpendicular to
1073 // both legs, then normalise and average to get
1074 // a horizontal bisector.
1075 Vector3 r1 = leg1_v * up_v;
1076 Vector3 r2 = leg2_v * up_v;
1077 r1.normalise();
1078 r2.normalise();
1079 right = r1 + r2;
1080 if (right.magnitude() == 0) {
1081 // This is the "mid-pitch" case...
1082 right = last_right;
1084 last_right = right;
1087 // Scale to unit vectors in the LRUD plane.
1088 right.normalise();
1090 Double l = pt_v.GetL();
1091 Double r = pt_v.GetR();
1093 if (l >= 0 || r >= 0) {
1094 // Get the x and y coordinates of the survey station
1095 double pt_X = pt_v.GetX() * COS - pt_v.GetY() * SIN;
1096 double pt_Y = pt_v.GetX() * SIN + pt_v.GetY() * COS;
1098 double X, Y;
1099 if (l >= 0) {
1100 // Get the x and y coordinates of the end of the left arrow
1101 Vector3 p = pt_v - right * l;
1102 X = p.GetX() * COS - p.GetY() * SIN;
1103 Y = (p.GetX() * SIN + p.GetY() * COS);
1104 } else {
1105 X = pt_X;
1106 Y = pt_Y;
1108 if (X > m_layout.xMax) m_layout.xMax = X;
1109 if (X < m_layout.xMin) m_layout.xMin = X;
1110 if (Y > m_layout.yMax) m_layout.yMax = Y;
1111 if (Y < m_layout.yMin) m_layout.yMin = Y;
1113 if (r >= 0) {
1114 // Get the x and y coordinates of the end of the right arrow
1115 Vector3 p = pt_v + right * r;
1116 X = p.GetX() * COS - p.GetY() * SIN;
1117 Y = (p.GetX() * SIN + p.GetY() * COS);
1118 } else {
1119 X = pt_X;
1120 Y = pt_Y;
1122 if (X > m_layout.xMax) m_layout.xMax = X;
1123 if (X < m_layout.xMin) m_layout.xMin = X;
1124 if (Y > m_layout.yMax) m_layout.yMax = Y;
1125 if (Y < m_layout.yMin) m_layout.yMin = Y;
1128 prev_pt_v = pt_v;
1130 ++segment;
1136 if (show_mask & (LABELS|STNS)) {
1137 list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
1138 while (label != mainfrm->GetLabelsEnd()) {
1139 double x = (*label)->GetX();
1140 double y = (*label)->GetY();
1141 double z = (*label)->GetZ();
1142 if ((show_mask & SURF) || (*label)->IsUnderground()) {
1143 double X = x * COS - y * SIN;
1144 if (X > m_layout.xMax) m_layout.xMax = X;
1145 if (X < m_layout.xMin) m_layout.xMin = X;
1146 double Y = z * COST - (x * SIN + y * COS) * SINT;
1147 if (Y > m_layout.yMax) m_layout.yMax = Y;
1148 if (Y < m_layout.yMin) m_layout.yMin = Y;
1150 ++label;
1155 static int xpPageWidth, ypPageDepth;
1156 static long x_offset, y_offset;
1157 static int fontsize, fontsize_labels;
1159 /* FIXME: allow the font to be set */
1161 static const char *fontname = "Arial", *fontname_labels = "Arial";
1163 svxPrintout::svxPrintout(MainFrm *mainfrm_, layout *l,
1164 wxPageSetupDialogData *data, const wxString & title)
1165 : wxPrintout(title), font_labels(NULL), font_default(NULL),
1166 scan_for_blank_pages(false)
1168 mainfrm = mainfrm_;
1169 m_layout = l;
1170 m_data = data;
1173 void
1174 svxPrintout::draw_info_box()
1176 layout *l = m_layout;
1177 int boxwidth = 70;
1178 int boxheight = 30;
1180 pdc->SetPen(*pen_frame);
1182 int div = boxwidth;
1183 if (l->view != layout::EXTELEV) {
1184 boxwidth += boxheight;
1185 MOVEMM(div, boxheight);
1186 DRAWMM(div, 0);
1187 MOVEMM(0, 30); DRAWMM(div, 30);
1190 MOVEMM(0, boxheight);
1191 DRAWMM(boxwidth, boxheight);
1192 DRAWMM(boxwidth, 0);
1193 if (!l->Border) {
1194 DRAWMM(0, 0);
1195 DRAWMM(0, boxheight);
1198 MOVEMM(0, 20); DRAWMM(div, 20);
1199 MOVEMM(0, 10); DRAWMM(div, 10);
1201 switch (l->view) {
1202 case layout::PLAN: {
1203 long ax, ay, bx, by, cx, cy, dx, dy;
1205 long xc = boxwidth - boxheight / 2;
1206 long yc = boxheight / 2;
1207 const double RADIUS = boxheight / 3;
1208 DrawEllipse(long(xc * l->scX), long(yc * l->scY),
1209 long(RADIUS * l->scX), long(RADIUS * l->scY));
1211 ax = (long)((xc - (RADIUS - 1) * sin(rad(000.0 + l->rot))) * l->scX);
1212 ay = (long)((yc + (RADIUS - 1) * cos(rad(000.0 + l->rot))) * l->scY);
1213 bx = (long)((xc - RADIUS * 0.5 * sin(rad(180.0 + l->rot))) * l->scX);
1214 by = (long)((yc + RADIUS * 0.5 * cos(rad(180.0 + l->rot))) * l->scY);
1215 cx = (long)((xc - (RADIUS - 1) * sin(rad(160.0 + l->rot))) * l->scX);
1216 cy = (long)((yc + (RADIUS - 1) * cos(rad(160.0 + l->rot))) * l->scY);
1217 dx = (long)((xc - (RADIUS - 1) * sin(rad(200.0 + l->rot))) * l->scX);
1218 dy = (long)((yc + (RADIUS - 1) * cos(rad(200.0 + l->rot))) * l->scY);
1220 MoveTo(ax, ay);
1221 DrawTo(bx, by);
1222 DrawTo(cx, cy);
1223 DrawTo(ax, ay);
1224 DrawTo(dx, dy);
1225 DrawTo(bx, by);
1227 pdc->SetTextForeground(colour_text);
1228 MOVEMM(div + 0.5, boxheight - 5.5);
1229 WriteString(wmsg(/*North*/115));
1231 wxString angle = format_angle(ANGLE_FMT, l->rot);
1232 wxString s;
1233 /* TRANSLATORS: This is used on printouts of plans, with %s replaced by
1234 * something like "123°". The bearing is up the page. */
1235 s.Printf(wmsg(/*Plan view, %s up page*/168), angle.c_str());
1236 MOVEMM(2, 12); WriteString(s);
1237 break;
1239 case layout::ELEV: case layout::TILT: {
1240 const int L = div + 2;
1241 const int R = boxwidth - 2;
1242 const int H = boxheight / 2;
1243 MOVEMM(L, H); DRAWMM(L + 5, H - 3); DRAWMM(L + 3, H); DRAWMM(L + 5, H + 3);
1245 DRAWMM(L, H); DRAWMM(R, H);
1247 DRAWMM(R - 5, H + 3); DRAWMM(R - 3, H); DRAWMM(R - 5, H - 3); DRAWMM(R, H);
1249 MOVEMM((L + R) / 2, H - 2); DRAWMM((L + R) / 2, H + 2);
1251 pdc->SetTextForeground(colour_text);
1252 MOVEMM(div + 2, boxheight - 8);
1253 /* TRANSLATORS: "Elevation on" 020 <-> 200 degrees */
1254 WriteString(wmsg(/*Elevation on*/116));
1256 MOVEMM(L, 2);
1257 WriteString(format_angle(ANGLE_FMT, fmod(l->rot + 270.0, 360.0)));
1258 MOVEMM(R - 10, 2);
1259 WriteString(format_angle(ANGLE_FMT, fmod(l->rot + 90.0, 360.0)));
1261 wxString angle = format_angle(ANGLE_FMT, l->rot);
1262 wxString s;
1263 if (l->view == layout::ELEV) {
1264 /* TRANSLATORS: This is used on printouts of elevations, with %s
1265 * replaced by something like "123°". The bearing is the direction
1266 * we’re looking. */
1267 s.Printf(wmsg(/*Elevation facing %s*/169), angle.c_str());
1268 } else {
1269 wxString a2 = format_angle(ANGLE2_FMT, l->tilt);
1270 /* TRANSLATORS: This is used on printouts of tilted elevations, with
1271 * the first %s replaced by something like "123°", and the second by
1272 * something like "-45°". The bearing is the direction we’re
1273 * looking. */
1274 s.Printf(wmsg(/*Elevation facing %s, tilted %s*/284), angle.c_str(), a2.c_str());
1276 MOVEMM(2, 12); WriteString(s);
1277 break;
1279 case layout::EXTELEV:
1280 pdc->SetTextForeground(colour_text);
1281 MOVEMM(2, 12);
1282 /* TRANSLATORS: This is used on printouts of extended elevations. */
1283 WriteString(wmsg(/*Extended elevation*/191));
1284 break;
1287 MOVEMM(2, boxheight - 8); WriteString(l->title);
1289 MOVEMM(2, 2);
1290 // FIXME: "Original Scale" better?
1291 WriteString(wxString::Format(wmsg(/*Scale*/154) + wxT(" 1:%.0f"),
1292 l->Scale));
1294 /* This used to be a copyright line, but it was occasionally
1295 * mis-interpreted as us claiming copyright on the survey, so let's
1296 * give the website URL instead */
1297 MOVEMM(boxwidth + 2, 2);
1298 WriteString(wxT("Survex " VERSION " - https://survex.com/"));
1300 draw_scale_bar(boxwidth + 10.0, 17.0, l->PaperWidth - boxwidth - 18.0);
1303 /* Draw fancy scale bar with bottom left at (x,y) (both in mm) and at most */
1304 /* MaxLength mm long. The scaling in use is 1:scale */
1305 void
1306 svxPrintout::draw_scale_bar(double x, double y, double MaxLength)
1308 double StepEst, d;
1309 int E, Step, n, c;
1310 wxString buf;
1311 /* Limit scalebar to 20cm to stop people with A0 plotters complaining */
1312 if (MaxLength > 200.0) MaxLength = 200.0;
1314 #define dmin 10.0 /* each division >= dmin mm long */
1315 #define StepMax 5 /* number in steps of at most StepMax (x 10^N) */
1316 #define epsilon (1e-4) /* fudge factor to prevent rounding problems */
1318 E = (int)ceil(log10((dmin * 0.001 * m_layout->Scale) / StepMax));
1319 StepEst = pow(10.0, -(double)E) * (dmin * 0.001) * m_layout->Scale - epsilon;
1321 /* Force labelling to be in multiples of 1, 2, or 5 */
1322 Step = (StepEst <= 1.0 ? 1 : (StepEst <= 2.0 ? 2 : 5));
1324 /* Work out actual length of each scale bar division */
1325 d = Step * pow(10.0, (double)E) / m_layout->Scale * 1000.0;
1327 /* FIXME: Non-metric units here... */
1328 /* Choose appropriate units, s.t. if possible E is >=0 and minimized */
1329 int units;
1330 if (E >= 3) {
1331 E -= 3;
1332 units = /*km*/423;
1333 } else if (E >= 0) {
1334 units = /*m*/424;
1335 } else {
1336 E += 2;
1337 units = /*cm*/425;
1340 buf = wmsg(/*Scale*/154);
1342 /* Add units used - eg. "Scale (10m)" */
1343 double pow10_E = pow(10.0, (double)E);
1344 if (E >= 0) {
1345 buf += wxString::Format(wxT(" (%.f%s)"), pow10_E, wmsg(units).c_str());
1346 } else {
1347 int sf = -(int)floor(E);
1348 buf += wxString::Format(wxT(" (%.*f%s)"), sf, pow10_E, wmsg(units).c_str());
1350 pdc->SetTextForeground(colour_text);
1351 MOVEMM(x, y + 4); WriteString(buf);
1353 /* Work out how many divisions there will be */
1354 n = (int)(MaxLength / d);
1356 pdc->SetPen(*pen_frame);
1358 long Y = long(y * m_layout->scY);
1359 long Y2 = long((y + 3) * m_layout->scY);
1360 long X = long(x * m_layout->scX);
1361 long X2 = long((x + n * d) * m_layout->scX);
1363 /* Draw top of scale bar */
1364 MoveTo(X2, Y2);
1365 DrawTo(X, Y2);
1366 #if 0
1367 DrawTo(X2, Y);
1368 DrawTo(X, Y);
1369 MOVEMM(x + n * d, y); DRAWMM(x, y);
1370 #endif
1371 /* Draw divisions and label them */
1372 for (c = 0; c <= n; c++) {
1373 pdc->SetPen(*pen_frame);
1374 X = long((x + c * d) * m_layout->scX);
1375 MoveTo(X, Y);
1376 DrawTo(X, Y2);
1377 #if 0 // Don't waste toner!
1378 /* Draw a "zebra crossing" scale bar. */
1379 if (c < n && (c & 1) == 0) {
1380 X2 = long((x + (c + 1) * d) * m_layout->scX);
1381 SolidRectangle(X, Y, X2 - X, Y2 - Y);
1383 #endif
1384 buf.Printf(wxT("%d"), c * Step);
1385 pdc->SetTextForeground(colour_text);
1386 MOVEMM(x + c * d - buf.length(), y - 5);
1387 WriteString(buf);
1391 #if 0
1392 void
1393 make_calibration(layout *l) {
1394 img_point pt = { 0.0, 0.0, 0.0 };
1395 l->xMax = l->yMax = 0.1;
1396 l->xMin = l->yMin = 0;
1398 stack(l, img_MOVE, NULL, &pt);
1399 pt.x = 0.1;
1400 stack(l, img_LINE, NULL, &pt);
1401 pt.y = 0.1;
1402 stack(l, img_LINE, NULL, &pt);
1403 pt.x = 0.0;
1404 stack(l, img_LINE, NULL, &pt);
1405 pt.y = 0.0;
1406 stack(l, img_LINE, NULL, &pt);
1407 pt.x = 0.05;
1408 pt.y = 0.001;
1409 stack(l, img_LABEL, "10cm", &pt);
1410 pt.x = 0.001;
1411 pt.y = 0.05;
1412 stack(l, img_LABEL, "10cm", &pt);
1413 l->Scale = 1.0;
1415 #endif
1418 svxPrintout::next_page(int *pstate, char **q, int pageLim)
1420 char *p;
1421 int page;
1422 int c;
1423 p = *q;
1424 if (*pstate > 0) {
1425 /* doing a range */
1426 (*pstate)++;
1427 wxASSERT(*p == '-');
1428 p++;
1429 while (isspace((unsigned char)*p)) p++;
1430 if (sscanf(p, "%u%n", &page, &c) > 0) {
1431 p += c;
1432 } else {
1433 page = pageLim;
1435 if (*pstate > page) goto err;
1436 if (*pstate < page) return *pstate;
1437 *q = p;
1438 *pstate = 0;
1439 return page;
1442 while (isspace((unsigned char)*p) || *p == ',') p++;
1444 if (!*p) return 0; /* done */
1446 if (*p == '-') {
1447 *q = p;
1448 *pstate = 1;
1449 return 1; /* range with initial parameter omitted */
1451 if (sscanf(p, "%u%n", &page, &c) > 0) {
1452 p += c;
1453 while (isspace((unsigned char)*p)) p++;
1454 *q = p;
1455 if (0 < page && page <= pageLim) {
1456 if (*p == '-') *pstate = page; /* range with start */
1457 return page;
1460 err:
1461 *pstate = -1;
1462 return 0;
1465 /* Draws in alignment marks on each page or borders on edge pages */
1466 void
1467 svxPrintout::drawticks(int tsize, int x, int y)
1469 long i;
1470 int s = tsize * 4;
1471 int o = s / 8;
1472 bool fAtCorner = fFalse;
1473 pdc->SetPen(*pen_frame);
1474 if (x == 0 && m_layout->Border) {
1475 /* solid left border */
1476 MoveTo(clip.x_min, clip.y_min);
1477 DrawTo(clip.x_min, clip.y_max);
1478 fAtCorner = fTrue;
1479 } else {
1480 if (x > 0 || y > 0) {
1481 MoveTo(clip.x_min, clip.y_min);
1482 DrawTo(clip.x_min, clip.y_min + tsize);
1484 if (s && x > 0 && m_layout->Cutlines) {
1485 /* dashed left border */
1486 i = (clip.y_max - clip.y_min) -
1487 (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1488 for ( ; i > tsize; i -= s) {
1489 MoveTo(clip.x_min, clip.y_max - (i + o));
1490 DrawTo(clip.x_min, clip.y_max - (i - o));
1493 if (x > 0 || y < m_layout->pagesY - 1) {
1494 MoveTo(clip.x_min, clip.y_max - tsize);
1495 DrawTo(clip.x_min, clip.y_max);
1496 fAtCorner = fTrue;
1500 if (y == m_layout->pagesY - 1 && m_layout->Border) {
1501 /* solid top border */
1502 if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1503 DrawTo(clip.x_max, clip.y_max);
1504 fAtCorner = fTrue;
1505 } else {
1506 if (y < m_layout->pagesY - 1 || x > 0) {
1507 if (!fAtCorner) MoveTo(clip.x_min, clip.y_max);
1508 DrawTo(clip.x_min + tsize, clip.y_max);
1510 if (s && y < m_layout->pagesY - 1 && m_layout->Cutlines) {
1511 /* dashed top border */
1512 i = (clip.x_max - clip.x_min) -
1513 (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1514 for ( ; i > tsize; i -= s) {
1515 MoveTo(clip.x_max - (i + o), clip.y_max);
1516 DrawTo(clip.x_max - (i - o), clip.y_max);
1519 if (y < m_layout->pagesY - 1 || x < m_layout->pagesX - 1) {
1520 MoveTo(clip.x_max - tsize, clip.y_max);
1521 DrawTo(clip.x_max, clip.y_max);
1522 fAtCorner = fTrue;
1523 } else {
1524 fAtCorner = fFalse;
1528 if (x == m_layout->pagesX - 1 && m_layout->Border) {
1529 /* solid right border */
1530 if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1531 DrawTo(clip.x_max, clip.y_min);
1532 fAtCorner = fTrue;
1533 } else {
1534 if (x < m_layout->pagesX - 1 || y < m_layout->pagesY - 1) {
1535 if (!fAtCorner) MoveTo(clip.x_max, clip.y_max);
1536 DrawTo(clip.x_max, clip.y_max - tsize);
1538 if (s && x < m_layout->pagesX - 1 && m_layout->Cutlines) {
1539 /* dashed right border */
1540 i = (clip.y_max - clip.y_min) -
1541 (tsize + ((clip.y_max - clip.y_min - tsize * 2L) % s) / 2);
1542 for ( ; i > tsize; i -= s) {
1543 MoveTo(clip.x_max, clip.y_min + (i + o));
1544 DrawTo(clip.x_max, clip.y_min + (i - o));
1547 if (x < m_layout->pagesX - 1 || y > 0) {
1548 MoveTo(clip.x_max, clip.y_min + tsize);
1549 DrawTo(clip.x_max, clip.y_min);
1550 fAtCorner = fTrue;
1551 } else {
1552 fAtCorner = fFalse;
1556 if (y == 0 && m_layout->Border) {
1557 /* solid bottom border */
1558 if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1559 DrawTo(clip.x_min, clip.y_min);
1560 } else {
1561 if (y > 0 || x < m_layout->pagesX - 1) {
1562 if (!fAtCorner) MoveTo(clip.x_max, clip.y_min);
1563 DrawTo(clip.x_max - tsize, clip.y_min);
1565 if (s && y > 0 && m_layout->Cutlines) {
1566 /* dashed bottom border */
1567 i = (clip.x_max - clip.x_min) -
1568 (tsize + ((clip.x_max - clip.x_min - tsize * 2L) % s) / 2);
1569 for ( ; i > tsize; i -= s) {
1570 MoveTo(clip.x_min + (i + o), clip.y_min);
1571 DrawTo(clip.x_min + (i - o), clip.y_min);
1574 if (y > 0 || x > 0) {
1575 MoveTo(clip.x_min + tsize, clip.y_min);
1576 DrawTo(clip.x_min, clip.y_min);
1581 bool
1582 svxPrintout::OnPrintPage(int pageNum) {
1583 GetPageSizePixels(&xpPageWidth, &ypPageDepth);
1584 pdc = GetDC();
1585 pdc->SetBackgroundMode(wxTRANSPARENT);
1586 #ifdef AVEN_PRINT_PREVIEW
1587 if (IsPreview()) {
1588 int dcx, dcy;
1589 pdc->GetSize(&dcx, &dcy);
1590 pdc->SetUserScale((double)dcx / xpPageWidth, (double)dcy / ypPageDepth);
1592 #endif
1594 layout * l = m_layout;
1596 int pwidth, pdepth;
1597 GetPageSizeMM(&pwidth, &pdepth);
1598 l->scX = (double)xpPageWidth / pwidth;
1599 l->scY = (double)ypPageDepth / pdepth;
1600 font_scaling_x = l->scX * (25.4 / 72.0);
1601 font_scaling_y = l->scY * (25.4 / 72.0);
1602 long MarginLeft = m_data->GetMarginTopLeft().x;
1603 long MarginTop = m_data->GetMarginTopLeft().y;
1604 long MarginBottom = m_data->GetMarginBottomRight().y;
1605 long MarginRight = m_data->GetMarginBottomRight().x;
1606 xpPageWidth -= (int)(l->scX * (MarginLeft + MarginRight));
1607 ypPageDepth -= (int)(l->scY * (FOOTER_HEIGHT_MM + MarginBottom + MarginTop));
1608 // xpPageWidth -= 1;
1609 pdepth -= FOOTER_HEIGHT_MM;
1610 x_offset = (long)(l->scX * MarginLeft);
1611 y_offset = (long)(l->scY * MarginTop);
1612 l->PaperWidth = pwidth -= MarginLeft + MarginRight;
1613 l->PaperDepth = pdepth -= MarginTop + MarginBottom;
1616 double SIN = sin(rad(l->rot));
1617 double COS = cos(rad(l->rot));
1618 double SINT = sin(rad(l->tilt));
1619 double COST = cos(rad(l->tilt));
1621 NewPage(pageNum, l->pagesX, l->pagesY);
1623 if (l->Legend && pageNum == (l->pagesY - 1) * l->pagesX + 1) {
1624 SetFont(font_default);
1625 draw_info_box();
1628 pdc->SetClippingRegion(x_offset, y_offset, xpPageWidth + 1, ypPageDepth + 1);
1630 const double Sc = 1000 / l->Scale;
1632 int show_mask = l->get_effective_show_mask();
1633 if (show_mask & (LEGS|SURF)) {
1634 for (int f = 0; f != 8; ++f) {
1635 if ((show_mask & (f & img_FLAG_SURFACE) ? SURF : LEGS) == 0) {
1636 // Not showing traverse because of surface/underground status.
1637 continue;
1639 if ((f & img_FLAG_SPLAY) && (show_mask & SPLAYS) == 0) {
1640 // Not showing because it's a splay.
1641 continue;
1643 if (f & img_FLAG_SPLAY) {
1644 pdc->SetPen(*pen_splay);
1645 } else if (f & img_FLAG_SURFACE) {
1646 pdc->SetPen(*pen_surface_leg);
1647 } else {
1648 pdc->SetPen(*pen_leg);
1650 list<traverse>::const_iterator trav = mainfrm->traverses_begin(f);
1651 list<traverse>::const_iterator tend = mainfrm->traverses_end(f);
1652 for ( ; trav != tend; ++trav) {
1653 vector<PointInfo>::const_iterator pos = trav->begin();
1654 vector<PointInfo>::const_iterator end = trav->end();
1655 for ( ; pos != end; ++pos) {
1656 double x = pos->GetX();
1657 double y = pos->GetY();
1658 double z = pos->GetZ();
1659 double X = x * COS - y * SIN;
1660 double Y = z * COST - (x * SIN + y * COS) * SINT;
1661 long px = (long)((X * Sc + l->xOrg) * l->scX);
1662 long py = (long)((Y * Sc + l->yOrg) * l->scY);
1663 if (pos == trav->begin()) {
1664 MoveTo(px, py);
1665 } else {
1666 DrawTo(px, py);
1673 if ((show_mask & XSECT) &&
1674 (l->tilt == 0.0 || l->tilt == 90.0 || l->tilt == -90.0)) {
1675 pdc->SetPen(*pen_splay);
1676 list<vector<XSect> >::const_iterator trav = mainfrm->tubes_begin();
1677 list<vector<XSect> >::const_iterator tend = mainfrm->tubes_end();
1678 for ( ; trav != tend; ++trav) {
1679 if (l->tilt == 0.0) {
1680 PlotUD(*trav);
1681 } else {
1682 // m_layout.tilt is 90.0 or -90.0 due to check above.
1683 PlotLR(*trav);
1688 if (show_mask & (LABELS|STNS)) {
1689 if (show_mask & LABELS) SetFont(font_labels);
1690 list<LabelInfo*>::const_iterator label = mainfrm->GetLabels();
1691 while (label != mainfrm->GetLabelsEnd()) {
1692 double px = (*label)->GetX();
1693 double py = (*label)->GetY();
1694 double pz = (*label)->GetZ();
1695 if ((show_mask & SURF) || (*label)->IsUnderground()) {
1696 double X = px * COS - py * SIN;
1697 double Y = pz * COST - (px * SIN + py * COS) * SINT;
1698 long xnew, ynew;
1699 xnew = (long)((X * Sc + l->xOrg) * l->scX);
1700 ynew = (long)((Y * Sc + l->yOrg) * l->scY);
1701 if (show_mask & STNS) {
1702 pdc->SetPen(*pen_cross);
1703 DrawCross(xnew, ynew);
1705 if (show_mask & LABELS) {
1706 pdc->SetTextForeground(colour_labels);
1707 MoveTo(xnew, ynew);
1708 WriteString((*label)->GetText());
1711 ++label;
1715 return true;
1718 void
1719 svxPrintout::GetPageInfo(int *minPage, int *maxPage,
1720 int *pageFrom, int *pageTo)
1722 *minPage = *pageFrom = 1;
1723 *maxPage = *pageTo = m_layout->pages;
1726 bool
1727 svxPrintout::HasPage(int pageNum) {
1728 return (pageNum <= m_layout->pages);
1731 void
1732 svxPrintout::OnBeginPrinting() {
1733 /* Initialise printer routines */
1734 fontsize_labels = 10;
1735 fontsize = 10;
1737 colour_text = colour_labels = *wxBLACK;
1739 wxColour colour_frame, colour_cross, colour_leg, colour_surface_leg;
1740 colour_frame = colour_cross = colour_leg = colour_surface_leg = *wxBLACK;
1742 pen_frame = new wxPen(colour_frame);
1743 pen_cross = new wxPen(colour_cross);
1744 pen_leg = new wxPen(colour_leg);
1745 pen_surface_leg = new wxPen(colour_surface_leg);
1746 pen_splay = new wxPen(wxColour(128, 128, 128));
1748 m_layout->scX = 1;
1749 m_layout->scY = 1;
1751 font_labels = new wxFont(fontsize_labels, wxFONTFAMILY_DEFAULT,
1752 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
1753 false, wxString(fontname_labels, wxConvUTF8),
1754 wxFONTENCODING_ISO8859_1);
1755 font_default = new wxFont(fontsize, wxFONTFAMILY_DEFAULT,
1756 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
1757 false, wxString(fontname, wxConvUTF8),
1758 wxFONTENCODING_ISO8859_1);
1761 void
1762 svxPrintout::OnEndPrinting() {
1763 delete font_labels;
1764 delete font_default;
1765 delete pen_frame;
1766 delete pen_cross;
1767 delete pen_leg;
1768 delete pen_surface_leg;
1769 delete pen_splay;
1773 svxPrintout::check_intersection(long x_p, long y_p)
1775 #define U 1
1776 #define D 2
1777 #define L 4
1778 #define R 8
1779 int mask_p = 0, mask_t = 0;
1780 if (x_p < 0)
1781 mask_p = L;
1782 else if (x_p > xpPageWidth)
1783 mask_p = R;
1785 if (y_p < 0)
1786 mask_p |= D;
1787 else if (y_p > ypPageDepth)
1788 mask_p |= U;
1790 if (x_t < 0)
1791 mask_t = L;
1792 else if (x_t > xpPageWidth)
1793 mask_t = R;
1795 if (y_t < 0)
1796 mask_t |= D;
1797 else if (y_t > ypPageDepth)
1798 mask_t |= U;
1800 #if 0
1801 /* approximation to correct answer */
1802 return !(mask_t & mask_p);
1803 #else
1804 /* One end of the line is on the page */
1805 if (!mask_t || !mask_p) return 1;
1807 /* whole line is above, left, right, or below page */
1808 if (mask_t & mask_p) return 0;
1810 if (mask_t == 0) mask_t = mask_p;
1811 if (mask_t & U) {
1812 double v = (double)(y_p - ypPageDepth) / (y_p - y_t);
1813 return v >= 0 && v <= 1;
1815 if (mask_t & D) {
1816 double v = (double)y_p / (y_p - y_t);
1817 return v >= 0 && v <= 1;
1819 if (mask_t & R) {
1820 double v = (double)(x_p - xpPageWidth) / (x_p - x_t);
1821 return v >= 0 && v <= 1;
1823 wxASSERT(mask_t & L);
1825 double v = (double)x_p / (x_p - x_t);
1826 return v >= 0 && v <= 1;
1828 #endif
1829 #undef U
1830 #undef D
1831 #undef L
1832 #undef R
1835 void
1836 svxPrintout::MoveTo(long x, long y)
1838 x_t = x_offset + x - clip.x_min;
1839 y_t = y_offset + clip.y_max - y;
1842 void
1843 svxPrintout::DrawTo(long x, long y)
1845 long x_p = x_t, y_p = y_t;
1846 x_t = x_offset + x - clip.x_min;
1847 y_t = y_offset + clip.y_max - y;
1848 if (!scan_for_blank_pages) {
1849 pdc->DrawLine(x_p, y_p, x_t, y_t);
1850 } else {
1851 if (check_intersection(x_p, y_p)) fBlankPage = fFalse;
1855 #define POINTS_PER_INCH 72.0
1856 #define POINTS_PER_MM (POINTS_PER_INCH / MM_PER_INCH)
1857 #define PWX_CROSS_SIZE (int)(2 * m_layout->scX / POINTS_PER_MM)
1859 void
1860 svxPrintout::DrawCross(long x, long y)
1862 if (!scan_for_blank_pages) {
1863 MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1864 DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1865 MoveTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
1866 DrawTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
1867 MoveTo(x, y);
1868 } else {
1869 if ((x + PWX_CROSS_SIZE > clip.x_min &&
1870 x - PWX_CROSS_SIZE < clip.x_max) ||
1871 (y + PWX_CROSS_SIZE > clip.y_min &&
1872 y - PWX_CROSS_SIZE < clip.y_max)) {
1873 fBlankPage = fFalse;
1878 void
1879 svxPrintout::WriteString(const wxString & s)
1881 double xsc, ysc;
1882 pdc->GetUserScale(&xsc, &ysc);
1883 pdc->SetUserScale(xsc * font_scaling_x, ysc * font_scaling_y);
1884 if (!scan_for_blank_pages) {
1885 pdc->DrawText(s,
1886 long(x_t / font_scaling_x),
1887 long(y_t / font_scaling_y) - pdc->GetCharHeight());
1888 } else {
1889 int w, h;
1890 pdc->GetTextExtent(s, &w, &h);
1891 if ((y_t + h > 0 && y_t - h < clip.y_max - clip.y_min) ||
1892 (x_t < clip.x_max - clip.x_min && x_t + w > 0)) {
1893 fBlankPage = fFalse;
1896 pdc->SetUserScale(xsc, ysc);
1899 void
1900 svxPrintout::DrawEllipse(long x, long y, long r, long R)
1902 if (!scan_for_blank_pages) {
1903 x_t = x_offset + x - clip.x_min;
1904 y_t = y_offset + clip.y_max - y;
1905 const wxBrush & save_brush = pdc->GetBrush();
1906 pdc->SetBrush(*wxTRANSPARENT_BRUSH);
1907 pdc->DrawEllipse(x_t - r, y_t - R, 2 * r, 2 * R);
1908 pdc->SetBrush(save_brush);
1909 } else {
1910 /* No need to check - this is only used in the legend. */
1914 void
1915 svxPrintout::SolidRectangle(long x, long y, long w, long h)
1917 long X = x_offset + x - clip.x_min;
1918 long Y = y_offset + clip.y_max - y;
1919 pdc->SetBrush(*wxBLACK_BRUSH);
1920 pdc->DrawRectangle(X, Y - h, w, h);
1923 void
1924 svxPrintout::NewPage(int pg, int pagesX, int pagesY)
1926 pdc->DestroyClippingRegion();
1928 int x, y;
1929 x = (pg - 1) % pagesX;
1930 y = pagesY - 1 - ((pg - 1) / pagesX);
1932 clip.x_min = (long)x * xpPageWidth;
1933 clip.y_min = (long)y * ypPageDepth;
1934 clip.x_max = clip.x_min + xpPageWidth; /* dm/pcl/ps had -1; */
1935 clip.y_max = clip.y_min + ypPageDepth; /* dm/pcl/ps had -1; */
1937 const int FOOTERS = 4;
1938 wxString footer[FOOTERS];
1939 footer[0] = m_layout->title;
1941 double rot = m_layout->rot;
1942 double tilt = m_layout->tilt;
1943 double scale = m_layout->Scale;
1944 switch (m_layout->view) {
1945 case layout::PLAN:
1946 // TRANSLATORS: Used in the footer of printouts to compactly
1947 // indicate this is a plan view and what the viewing angle is.
1948 // Aven will replace %s with the bearing, and %.0f with the scale.
1950 // This message probably doesn't need translating for most languages.
1951 footer[1].Printf(wmsg(/*↑%s 1:%.0f*/233),
1952 format_angle(ANGLE_FMT, rot).c_str(),
1953 scale);
1954 break;
1955 case layout::ELEV:
1956 // TRANSLATORS: Used in the footer of printouts to compactly
1957 // indicate this is an elevation view and what the viewing angle
1958 // is. Aven will replace the %s codes with the bearings to the
1959 // left and right of the viewer, and %.0f with the scale.
1961 // This message probably doesn't need translating for most languages.
1962 footer[1].Printf(wmsg(/*%s↔%s 1:%.0f*/235),
1963 format_angle(ANGLE_FMT, fmod(rot + 270.0, 360.0)).c_str(),
1964 format_angle(ANGLE_FMT, fmod(rot + 90.0, 360.0)).c_str(),
1965 scale);
1966 break;
1967 case layout::TILT:
1968 // TRANSLATORS: Used in the footer of printouts to compactly
1969 // indicate this is a tilted elevation view and what the viewing
1970 // angles are. Aven will replace the %s codes with the bearings to
1971 // the left and right of the viewer and the angle the view is
1972 // tilted at, and %.0f with the scale.
1974 // This message probably doesn't need translating for most languages.
1975 footer[1].Printf(wmsg(/*%s↔%s ∡%s 1:%.0f*/236),
1976 format_angle(ANGLE_FMT, fmod(rot + 270.0, 360.0)).c_str(),
1977 format_angle(ANGLE_FMT, fmod(rot + 90.0, 360.0)).c_str(),
1978 format_angle(ANGLE2_FMT, tilt).c_str(),
1979 scale);
1980 break;
1981 case layout::EXTELEV:
1982 // TRANSLATORS: Used in the footer of printouts to compactly
1983 // indicate this is an extended elevation view. Aven will replace
1984 // %.0f with the scale.
1986 // Try to keep the translation short (for example, in English we
1987 // use "Extended" not "Extended elevation") - there is limited room
1988 // in the footer, and the details there are mostly to make it easy
1989 // to check that you have corresponding pages from a multiple page
1990 // printout.
1991 footer[1].Printf(wmsg(/*Extended 1:%.0f*/244), scale);
1992 break;
1995 // TRANSLATORS: N/M meaning page N of M in the page footer of a printout.
1996 footer[2].Printf(wmsg(/*%d/%d*/232), pg, m_layout->pagesX * m_layout->pagesY);
1998 wxString datestamp = m_layout->datestamp;
1999 if (!datestamp.empty()) {
2000 // Remove any timezone suffix (e.g. " UTC" or " +1200").
2001 wxChar ch = datestamp[datestamp.size() - 1];
2002 if (ch >= 'A' && ch <= 'Z') {
2003 for (size_t i = datestamp.size() - 1; i; --i) {
2004 ch = datestamp[i];
2005 if (ch < 'A' || ch > 'Z') {
2006 if (ch == ' ') datestamp.resize(i);
2007 break;
2010 } else if (ch >= '0' && ch <= '9') {
2011 for (size_t i = datestamp.size() - 1; i; --i) {
2012 ch = datestamp[i];
2013 if (ch < '0' || ch > '9') {
2014 if ((ch == '-' || ch == '+') && datestamp[--i] == ' ')
2015 datestamp.resize(i);
2016 break;
2021 // Remove any day prefix (e.g. "Mon,").
2022 for (size_t i = 0; i != datestamp.size(); ++i) {
2023 if (datestamp[i] == ',' && i + 1 != datestamp.size()) {
2024 // Also skip a space after the comma.
2025 if (datestamp[i + 1] == ' ') ++i;
2026 datestamp.erase(0, i + 1);
2027 break;
2032 // TRANSLATORS: Used in the footer of printouts to compactly indicate that
2033 // the date which follows is the date that the survey data was processed.
2035 // Aven will replace %s with a string giving the date and time (e.g.
2036 // "2015-06-09 12:40:44").
2037 footer[3].Printf(wmsg(/*Processed: %s*/167), datestamp.c_str());
2039 const wxChar * footer_sep = wxT(" ");
2040 int fontsize_footer = fontsize_labels;
2041 wxFont * font_footer;
2042 font_footer = new wxFont(fontsize_footer, wxFONTFAMILY_DEFAULT,
2043 wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
2044 false, wxString(fontname_labels, wxConvUTF8),
2045 wxFONTENCODING_UTF8);
2046 font_footer->Scale(font_scaling_x);
2047 SetFont(font_footer);
2048 int w[FOOTERS], ws, h;
2049 pdc->GetTextExtent(footer_sep, &ws, &h);
2050 int wtotal = ws * (FOOTERS - 1);
2051 for (int i = 0; i < FOOTERS; ++i) {
2052 pdc->GetTextExtent(footer[i], &w[i], &h);
2053 wtotal += w[i];
2056 long X = x_offset;
2057 long Y = y_offset + ypPageDepth + (long)(7 * m_layout->scY) - pdc->GetCharHeight();
2059 if (wtotal > xpPageWidth) {
2060 // Rescale the footer so it fits.
2061 double rescale = double(wtotal) / xpPageWidth;
2062 double xsc, ysc;
2063 pdc->GetUserScale(&xsc, &ysc);
2064 pdc->SetUserScale(xsc / rescale, ysc / rescale);
2065 SetFont(font_footer);
2066 wxString fullfooter = footer[0];
2067 for (int i = 1; i < FOOTERS - 1; ++i) {
2068 fullfooter += footer_sep;
2069 fullfooter += footer[i];
2071 pdc->DrawText(fullfooter, X * rescale, Y * rescale);
2072 // Draw final item right aligned to avoid misaligning.
2073 wxRect rect(x_offset * rescale, Y * rescale,
2074 xpPageWidth * rescale, pdc->GetCharHeight() * rescale);
2075 pdc->DrawLabel(footer[FOOTERS - 1], rect, wxALIGN_RIGHT|wxALIGN_TOP);
2076 pdc->SetUserScale(xsc, ysc);
2077 } else {
2078 // Space out the elements of the footer to fill the line.
2079 double extra = double(xpPageWidth - wtotal) / (FOOTERS - 1);
2080 for (int i = 0; i < FOOTERS - 1; ++i) {
2081 pdc->DrawText(footer[i], X + extra * i, Y);
2082 X += ws + w[i];
2084 // Draw final item right aligned to avoid misaligning.
2085 wxRect rect(x_offset, Y, xpPageWidth, pdc->GetCharHeight());
2086 pdc->DrawLabel(footer[FOOTERS - 1], rect, wxALIGN_RIGHT|wxALIGN_TOP);
2088 drawticks((int)(9 * m_layout->scX / POINTS_PER_MM), x, y);
2091 void
2092 svxPrintout::PlotLR(const vector<XSect> & centreline)
2094 assert(centreline.size() > 1);
2095 XSect prev_pt_v;
2096 Vector3 last_right(1.0, 0.0, 0.0);
2098 const double Sc = 1000 / m_layout->Scale;
2099 const double SIN = sin(rad(m_layout->rot));
2100 const double COS = cos(rad(m_layout->rot));
2102 vector<XSect>::const_iterator i = centreline.begin();
2103 vector<XSect>::size_type segment = 0;
2104 while (i != centreline.end()) {
2105 // get the coordinates of this vertex
2106 const XSect & pt_v = *i++;
2108 Vector3 right;
2110 const Vector3 up_v(0.0, 0.0, 1.0);
2112 if (segment == 0) {
2113 assert(i != centreline.end());
2114 // first segment
2116 // get the coordinates of the next vertex
2117 const XSect & next_pt_v = *i;
2119 // calculate vector from this pt to the next one
2120 Vector3 leg_v = next_pt_v - pt_v;
2122 // obtain a vector in the LRUD plane
2123 right = leg_v * up_v;
2124 if (right.magnitude() == 0) {
2125 right = last_right;
2126 } else {
2127 last_right = right;
2129 } else if (segment + 1 == centreline.size()) {
2130 // last segment
2132 // Calculate vector from the previous pt to this one.
2133 Vector3 leg_v = pt_v - prev_pt_v;
2135 // Obtain a horizontal vector in the LRUD plane.
2136 right = leg_v * up_v;
2137 if (right.magnitude() == 0) {
2138 right = Vector3(last_right.GetX(), last_right.GetY(), 0.0);
2139 } else {
2140 last_right = right;
2142 } else {
2143 assert(i != centreline.end());
2144 // Intermediate segment.
2146 // Get the coordinates of the next vertex.
2147 const XSect & next_pt_v = *i;
2149 // Calculate vectors from this vertex to the
2150 // next vertex, and from the previous vertex to
2151 // this one.
2152 Vector3 leg1_v = pt_v - prev_pt_v;
2153 Vector3 leg2_v = next_pt_v - pt_v;
2155 // Obtain horizontal vectors perpendicular to
2156 // both legs, then normalise and average to get
2157 // a horizontal bisector.
2158 Vector3 r1 = leg1_v * up_v;
2159 Vector3 r2 = leg2_v * up_v;
2160 r1.normalise();
2161 r2.normalise();
2162 right = r1 + r2;
2163 if (right.magnitude() == 0) {
2164 // This is the "mid-pitch" case...
2165 right = last_right;
2167 last_right = right;
2170 // Scale to unit vectors in the LRUD plane.
2171 right.normalise();
2173 Double l = pt_v.GetL();
2174 Double r = pt_v.GetR();
2176 if (l >= 0 || r >= 0) {
2177 // Get the x and y coordinates of the survey station
2178 double pt_X = pt_v.GetX() * COS - pt_v.GetY() * SIN;
2179 double pt_Y = pt_v.GetX() * SIN + pt_v.GetY() * COS;
2180 long pt_x = (long)((pt_X * Sc + m_layout->xOrg) * m_layout->scX);
2181 long pt_y = (long)((pt_Y * Sc + m_layout->yOrg) * m_layout->scY);
2183 // Calculate dimensions for the right arrow
2184 double COSR = right.GetX();
2185 double SINR = right.GetY();
2186 long CROSS_MAJOR = (COSR + SINR) * PWX_CROSS_SIZE;
2187 long CROSS_MINOR = (COSR - SINR) * PWX_CROSS_SIZE;
2189 if (l >= 0) {
2190 // Get the x and y coordinates of the end of the left arrow
2191 Vector3 p = pt_v - right * l;
2192 double X = p.GetX() * COS - p.GetY() * SIN;
2193 double Y = (p.GetX() * SIN + p.GetY() * COS);
2194 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2195 long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
2197 // Draw the arrow stem
2198 MoveTo(pt_x, pt_y);
2199 DrawTo(x, y);
2201 // Rotate the arrow by the page rotation
2202 long dx1 = (+CROSS_MINOR) * COS - (+CROSS_MAJOR) * SIN;
2203 long dy1 = (+CROSS_MINOR) * SIN + (+CROSS_MAJOR) * COS;
2204 long dx2 = (+CROSS_MAJOR) * COS - (-CROSS_MINOR) * SIN;
2205 long dy2 = (+CROSS_MAJOR) * SIN + (-CROSS_MINOR) * COS;
2207 // Draw the arrow
2208 MoveTo(x + dx1, y + dy1);
2209 DrawTo(x, y);
2210 DrawTo(x + dx2, y + dy2);
2213 if (r >= 0) {
2214 // Get the x and y coordinates of the end of the right arrow
2215 Vector3 p = pt_v + right * r;
2216 double X = p.GetX() * COS - p.GetY() * SIN;
2217 double Y = (p.GetX() * SIN + p.GetY() * COS);
2218 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2219 long y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scY);
2221 // Draw the arrow stem
2222 MoveTo(pt_x, pt_y);
2223 DrawTo(x, y);
2225 // Rotate the arrow by the page rotation
2226 long dx1 = (-CROSS_MINOR) * COS - (-CROSS_MAJOR) * SIN;
2227 long dy1 = (-CROSS_MINOR) * SIN + (-CROSS_MAJOR) * COS;
2228 long dx2 = (-CROSS_MAJOR) * COS - (+CROSS_MINOR) * SIN;
2229 long dy2 = (-CROSS_MAJOR) * SIN + (+CROSS_MINOR) * COS;
2231 // Draw the arrow
2232 MoveTo(x + dx1, y + dy1);
2233 DrawTo(x, y);
2234 DrawTo(x + dx2, y + dy2);
2238 prev_pt_v = pt_v;
2240 ++segment;
2244 void
2245 svxPrintout::PlotUD(const vector<XSect> & centreline)
2247 assert(centreline.size() > 1);
2248 const double Sc = 1000 / m_layout->Scale;
2250 vector<XSect>::const_iterator i = centreline.begin();
2251 while (i != centreline.end()) {
2252 // get the coordinates of this vertex
2253 const XSect & pt_v = *i++;
2255 Double u = pt_v.GetU();
2256 Double d = pt_v.GetD();
2258 if (u >= 0 || d >= 0) {
2259 // Get the coordinates of the survey point
2260 Vector3 p = pt_v;
2261 double SIN = sin(rad(m_layout->rot));
2262 double COS = cos(rad(m_layout->rot));
2263 double X = p.GetX() * COS - p.GetY() * SIN;
2264 double Y = p.GetZ();
2265 long x = (long)((X * Sc + m_layout->xOrg) * m_layout->scX);
2266 long pt_y = (long)((Y * Sc + m_layout->yOrg) * m_layout->scX);
2268 if (u >= 0) {
2269 // Get the y coordinate of the up arrow
2270 long y = (long)(((Y + u) * Sc + m_layout->yOrg) * m_layout->scY);
2272 // Draw the arrow stem
2273 MoveTo(x, pt_y);
2274 DrawTo(x, y);
2276 // Draw the up arrow
2277 MoveTo(x - PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
2278 DrawTo(x, y);
2279 DrawTo(x + PWX_CROSS_SIZE, y - PWX_CROSS_SIZE);
2282 if (d >= 0) {
2283 // Get the y coordinate of the down arrow
2284 long y = (long)(((Y - d) * Sc + m_layout->yOrg) * m_layout->scY);
2286 // Draw the arrow stem
2287 MoveTo(x, pt_y);
2288 DrawTo(x, y);
2290 // Draw the down arrow
2291 MoveTo(x - PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);
2292 DrawTo(x, y);
2293 DrawTo(x + PWX_CROSS_SIZE, y + PWX_CROSS_SIZE);