KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_print.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <pgm_base.h>
22#include <confirm.h>
23#include <sch_screen.h>
24#include <sch_edit_frame.h>
25#include <math/vector2wx.h>
28#include <wx/print.h>
29#include <wx/printdlg.h>
30#include <wx/filename.h>
31#include "dialog_print.h"
32
33
35
36#include <advanced_config.h>
37#include <reporter.h>
38#include <gestfich.h>
39#include <printing.h>
40#include <sch_plotter.h>
41
42#include "sch_printout.h"
43
44
49class SCH_PREVIEW_FRAME : public wxPreviewFrame
50{
51public:
52 SCH_PREVIEW_FRAME( wxPrintPreview* aPreview, wxWindow* aParent, const wxString& aTitle ) :
53 wxPreviewFrame( aPreview, aParent, aTitle )
54 {
55 }
56
57 bool Show( bool show ) override
58 {
59 bool ret;
60
61 // Show or hide the window. If hiding, save current position and size.
62 // If showing, use previous position and size.
63 if( show )
64 {
65 ret = wxPreviewFrame::Show( show );
66
67 if( s_size.x != 0 && s_size.y != 0 )
68 SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
69 }
70 else
71 {
72 // Save the dialog's position & size before hiding
73 s_size = GetSize();
74 s_pos = GetPosition();
75
76 ret = wxPreviewFrame::Show( show );
77 }
78
79 return ret;
80 }
81
82private:
83 static wxPoint s_pos;
84 static wxSize s_size;
85};
86
87
90
91
93 DIALOG_PRINT_BASE( aParent ),
94 m_parent( aParent )
95{
96 wxASSERT( aParent );
97
98 // Show m_panelPrinters only if there are printers to list:
99 m_panelPrinters->Show( m_panelPrinters->HasPrintersAvailable() );
100
101 SetupStandardButtons( { { wxID_OK, _( "Print" ) },
102 { wxID_APPLY, _( "Print Preview" ) },
103 { wxID_CANCEL, _( "Close" ) } } );
104
105#ifdef __WXMAC__
106 // Problems with modal on wx-2.9 - Anyway preview is standard for OSX
107 m_sdbSizerApply->Hide();
108#endif
109#if defined(__WXGTK__)
110 // Preview using Cairo does not work on GTK,
111 // but this platform provide native print preview
112 m_sdbSizerApply->Hide();
113#endif
114
115#ifndef __MINGW32__
116 if( ADVANCED_CFG::GetCfg().m_UsePdfPrint )
117 {
118 // The native print dialog owns the printer, paper and preview
119 m_panelPrinters->Hide();
120 m_buttonPageSetup->Hide();
121 m_sdbSizerApply->Hide();
122 }
123#endif
124
125 m_sdbSizerOK->SetFocus();
126
127 Layout();
128
130}
131
132
137
138
140{
141 EESCHEMA_SETTINGS* cfg = m_parent->eeconfig();
142
143 if( cfg->m_Printing.monochrome )
144 {
145 m_checkBackgroundColor->SetValue( false );
146 m_checkBackgroundColor->Enable( false );
147 }
148
149 m_checkReference->SetValue( cfg->m_Printing.title_block );
150 m_colorPrint->SetSelection( cfg->m_Printing.monochrome ? 1 : 0 );
152 m_checkUseColorTheme->SetValue( cfg->m_Printing.use_theme );
153
154 m_colorTheme->Clear();
155
156 int width = 0;
157 int height = 0;
158 int minwidth = width;
159
160 wxString target = cfg->m_Printing.use_theme ? cfg->m_Printing.color_theme : cfg->m_ColorTheme;
161
162 for( COLOR_SETTINGS* settings : Pgm().GetSettingsManager().GetColorSettingsList() )
163 {
164 int pos = m_colorTheme->Append( settings->GetName(), static_cast<void*>( settings ) );
165
166 if( settings->GetFilename() == target )
167 m_colorTheme->SetSelection( pos );
168
169 m_colorTheme->GetTextExtent( settings->GetName(), &width, &height );
170 minwidth = std::max( minwidth, width );
171 }
172
173 m_colorTheme->SetMinSize( wxSize( minwidth + 50, -1 ) );
174
175 m_colorTheme->Enable( cfg->m_Printing.use_theme );
176
177 // Initialize page specific print setup dialog settings.
178 const PAGE_INFO& pageInfo = m_parent->GetScreen()->GetPageSettings();
179 wxPageSetupDialogData& pageSetupDialogData = m_parent->GetPageSetupData();
180
181 pageSetupDialogData.SetPaperId( pageInfo.GetPaperId() );
182
183 if( pageInfo.IsCustom() )
184 {
185 if( pageInfo.IsPortrait() )
186 {
187 pageSetupDialogData.SetPaperSize( wxSize( EDA_UNIT_UTILS::Mils2mm( pageInfo.GetWidthMils() ),
188 EDA_UNIT_UTILS::Mils2mm( pageInfo.GetHeightMils() ) ) );
189 }
190 else
191 {
192 pageSetupDialogData.SetPaperSize( wxSize( EDA_UNIT_UTILS::Mils2mm( pageInfo.GetHeightMils() ),
193 EDA_UNIT_UTILS::Mils2mm( pageInfo.GetWidthMils() ) ) );
194 }
195 }
196
197 pageSetupDialogData.GetPrintData().SetOrientation( pageInfo.GetWxOrientation() );
198
199 Layout();
200
201 return true;
202}
203
204
205void DIALOG_PRINT::OnUseColorThemeChecked( wxCommandEvent& event )
206{
207 m_colorTheme->Enable( m_checkUseColorTheme->GetValue() );
208}
209
210
211void DIALOG_PRINT::OnOutputChoice( wxCommandEvent& event )
212{
213 long sel = event.GetSelection();
214 m_checkBackgroundColor->Enable( sel == 0 );
215
216 if( sel )
217 m_checkBackgroundColor->SetValue( false );
218 else
219 m_checkBackgroundColor->SetValue( m_parent->eeconfig()->m_Printing.background );
220}
221
222
224{
225 EESCHEMA_SETTINGS* cfg = m_parent->eeconfig();
226
227 cfg->m_Printing.monochrome = !!m_colorPrint->GetSelection();
228 cfg->m_Printing.title_block = m_checkReference->IsChecked();
229
230 if( m_checkBackgroundColor->IsEnabled() )
231 cfg->m_Printing.background = m_checkBackgroundColor->IsChecked();
232 else
233 cfg->m_Printing.background = false;
234
235 cfg->m_Printing.use_theme = m_checkUseColorTheme->IsChecked();
236
237 COLOR_SETTINGS* theme = static_cast<COLOR_SETTINGS*>( m_colorTheme->GetClientData( m_colorTheme->GetSelection() ) );
238
239 if( theme && m_checkUseColorTheme->IsChecked() )
240 cfg->m_Printing.color_theme = theme->GetFilename();
241}
242
243
244void DIALOG_PRINT::OnPageSetup( wxCommandEvent& event )
245{
246 wxPageSetupDialog pageSetupDialog( this, &m_parent->GetPageSetupData() );
247 pageSetupDialog.ShowModal();
248
249 m_parent->GetPageSetupData() = pageSetupDialog.GetPageSetupDialogData();
250}
251
252
253void DIALOG_PRINT::OnPrintPreview( wxCommandEvent& event )
254{
256 wxPrintData& prn_data = m_parent->GetPageSetupData().GetPrintData();
257
258 wxString selectedPrinterName;
259
260 if( m_panelPrinters )
261 selectedPrinterName = m_panelPrinters->GetSelectedPrinterName();
262
263 prn_data.SetPrinterName( selectedPrinterName );
264
265 // Pass two printout objects: for preview, and possible printing.
266 wxString title = _( "Preview" );
267 wxPrintPreview* preview = new wxPrintPreview( new SCH_PRINTOUT( m_parent, title ),
268 new SCH_PRINTOUT( m_parent, title ), &prn_data );
269
270 preview->SetZoom( 100 );
271
272 SCH_PREVIEW_FRAME* frame = new SCH_PREVIEW_FRAME( preview, this, title );
273
274 // On wxGTK, set the flag wxTOPLEVEL_EX_DIALOG is mandatory, if we want
275 // close the frame using the X box in caption, when the preview frame is run
276 // from a dialog
277 frame->SetExtraStyle( frame->GetExtraStyle() | wxTOPLEVEL_EX_DIALOG );
278
279 // We use here wxPreviewFrame_WindowModal option to make the wxPrintPreview frame
280 // modal for its caller only.
281 // another reason is the fact when closing the frame without this option,
282 // all top level frames are reenabled.
283 // With this option, only the parent is reenabled.
284 // Reenabling all top level frames should be made by the parent dialog.
285 frame->InitializeWithModality( wxPreviewFrame_WindowModal );
286
287 // on first invocation in this runtime session, set to 3/4 size of parent,
288 // but will be changed in Show() if not first time as will position.
289 // Must be called after InitializeWithModality because otherwise in some wxWidget
290 // versions it is not always taken in account
291 frame->SetMinSize( wxSize( 650, 500 ) );
292 frame->SetSize( (m_parent->GetSize() * 3) / 4 );
293
294 frame->Raise(); // Needed on Ubuntu/Unity to display the frame
295 frame->Show( true );
296}
297
298
300{
301 if( Pgm().m_Printing )
302 {
303 DisplayError( this, _( "Previous print job not yet complete." ) );
304 return false;
305 }
306
308
309#ifndef __MINGW32__
310 if( ADVANCED_CFG::GetCfg().m_UsePdfPrint )
311 return printViaPdf();
312#endif
313
314 int sheet_count = m_parent->Schematic().Root().CountSheets();
315
316 wxPrintData& data = m_parent->GetPageSetupData().GetPrintData();
317
318#if defined( __WXGTK__ ) && !wxCHECK_VERSION( 3, 2, 3 )
319 // In GTK, the default bottom margin is bigger by 0.31 inches for
320 // Letter, Legal, A4 paper sizes (see gtk_paper_size_get_default_bottom_margin).
321 //
322 // wxWidgets doesn't handle this properly when paper is in
323 // landscape orientation.
324 //
325 // Using custom page size avoids the problematic
326 // gtk_page_setup_set_paper_size_and_default_margins call in wxWidgets.
327
328 wxPaperSize paperId = data.GetPaperId();
329 PAGE_SIZE_TYPE paperType = PAGE_SIZE_TYPE::A4; // default; overwritten below when matched
330 bool havePaperType = false;
331
332 // clang-format off
333 std::set<wxPaperSize> letterSizes =
334 {
335 // na_letter
336 wxPAPER_LETTER,
337 wxPAPER_LETTERSMALL,
338 wxPAPER_NOTE,
339 wxPAPER_LETTER_TRANSVERSE,
340 wxPAPER_LETTER_ROTATED
341 };
342
343 std::set<wxPaperSize> legalSizes =
344 {
345 // na_legal
346 wxPAPER_LEGAL
347 };
348
349 std::set<wxPaperSize> a4Sizes =
350 {
351 // iso_a4
352 wxPAPER_A4,
353 wxPAPER_A4SMALL,
354 wxPAPER_A4_TRANSVERSE,
355 wxPAPER_A4_ROTATED
356 };
357 // clang-format on
358
359 if( letterSizes.count( paperId ) )
360 {
361 paperType = PAGE_SIZE_TYPE::USLetter;
362 havePaperType = true;
363 }
364 else if( legalSizes.count( paperId ) )
365 {
366 paperType = PAGE_SIZE_TYPE::USLegal;
367 havePaperType = true;
368 }
369 else if( a4Sizes.count( paperId ) )
370 {
371 paperType = PAGE_SIZE_TYPE::A4;
372 havePaperType = true;
373 }
374
375 if( havePaperType )
376 {
377 PAGE_INFO pageInfo( paperType, data.GetOrientation() == wxPORTRAIT );
378
379 if( pageInfo.IsPortrait() )
380 {
381 data.SetPaperSize( wxSize( EDA_UNIT_UTILS::Mils2mm( pageInfo.GetWidthMils() ),
382 EDA_UNIT_UTILS::Mils2mm( pageInfo.GetHeightMils() ) ) );
383 }
384 else
385 {
386 data.SetPaperSize( wxSize( EDA_UNIT_UTILS::Mils2mm( pageInfo.GetHeightMils() ),
387 EDA_UNIT_UTILS::Mils2mm( pageInfo.GetWidthMils() ) ) );
388 }
389
390 data.SetOrientation( pageInfo.GetWxOrientation() );
391 data.SetPaperId( wxPAPER_NONE );
392 }
393#endif
394
395 wxString selectedPrinterName;
396
397 if( m_panelPrinters )
398 selectedPrinterName = m_panelPrinters->GetSelectedPrinterName();
399
400 data.SetPrinterName( selectedPrinterName );
401
402 wxPrintDialogData printDialogData( data );
403 printDialogData.SetMaxPage( sheet_count );
404
405 if( sheet_count > 1 )
406 printDialogData.EnablePageNumbers( true );
407
408 wxPrinter printer( &printDialogData );
409 SCH_PRINTOUT printout( m_parent, _( "Print Schematic" ) );
410
411 Pgm().m_Printing = true;
412 {
413 if( !printer.Print( this, &printout, true ) )
414 {
415 if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
416 DisplayError( this, _( "An error occurred attempting to print the schematic." ) );
417 }
418 else
419 {
420 m_parent->GetPageSetupData() = printer.GetPrintDialogData().GetPrintData();
421 KIPLATFORM::PRINTING::ResetPrintToFilePath( m_parent->GetPageSetupData().GetPrintData() );
422 }
423 }
424
425 Pgm().m_Printing = false;
426
427 return true;
428}
429
430
432{
433#ifdef __MINGW32__
434 return false;
435#else
436 EESCHEMA_SETTINGS* cfg = m_parent->eeconfig();
437
438 SCH_RENDER_SETTINGS renderSettings( *m_parent->GetRenderSettings() );
439 renderSettings.m_ShowHiddenPins = false;
440 renderSettings.m_ShowHiddenFields = false;
441
442 wxString themeName = cfg->m_Printing.use_theme ? cfg->m_Printing.color_theme : cfg->m_ColorTheme;
443 renderSettings.LoadColors( ::GetColorSettings( themeName ) );
444
445 SCH_PLOT_OPTS plotOpts;
447 plotOpts.m_blackAndWhite = cfg->m_Printing.monochrome;
449 plotOpts.m_theme = themeName;
450
451 // The platform names the print job and any print-to-file output after the PDF
452 SCOPED_TEMP_PATH tmpDir;
453 wxString jobName = wxFileName( m_parent->Schematic().GetFileName() ).GetName();
454
455 if( !tmpDir.MakeDirectory( wxS( "eeschema_print" ), wxS_IRUSR | wxS_IWUSR | wxS_IXUSR ) )
456 {
457 DisplayError( this, _( "Failed to create a temporary folder for printing." ) );
458 return false;
459 }
460
461 wxFileName tmp( tmpDir.Path(), jobName.IsEmpty() ? wxString( wxS( "schematic" ) ) : jobName, wxS( "pdf" ) );
462 plotOpts.m_outputFile = tmp.GetFullPath();
463
464 // Locate the edited sheet in the plotter's page order so the system dialog can offer it as the current page
465 SCH_SHEET_LIST sheets;
466 sheets.BuildSheetList( &m_parent->Schematic().Root(), true );
467 sheets.SortByHierarchicalPageNumbers( false );
468
469 auto sheetIt = std::find( sheets.begin(), sheets.end(), m_parent->GetCurrentSheet() );
470 int currentPage = sheetIt == sheets.end() ? 0 : static_cast<int>( sheetIt - sheets.begin() ) + 1;
471
472 SCH_PLOTTER plotter( m_parent );
474
475 Pgm().m_Printing = true;
476 plotter.Plot( PLOT_FORMAT::PDF, plotOpts, &renderSettings, &reporter );
477 Pgm().m_Printing = false;
478
479 if( reporter.HasMessageOfSeverity( RPT_SEVERITY_ERROR ) )
480 {
481 DisplayErrorMessage( this, _( "Failed to prepare the schematic for printing." ), reporter.GetMessages() );
482 return false;
483 }
484
486 KIPLATFORM::PRINTING::PrintPDF( TO_UTF8( tmp.GetFullPath() ), currentPage );
487
489 {
491 return false;
492 }
493
494 return true;
495#endif
496}
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
wxString m_ColorTheme
Active color theme name.
Color settings are a bit different than most of the settings objects in that there can be more than o...
wxButton * m_buttonPageSetup
wxCheckBox * m_checkBackgroundColor
DIALOG_PRINT_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Print"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
PANEL_PRINTER_LIST * m_panelPrinters
wxCheckBox * m_checkUseColorTheme
wxCheckBox * m_checkReference
void OnPrintPreview(wxCommandEvent &event) override
void SavePrintOptions()
void OnPageSetup(wxCommandEvent &event) override
~DIALOG_PRINT() override
void OnOutputChoice(wxCommandEvent &event) override
bool TransferDataFromWindow() override
SCH_EDIT_FRAME * m_parent
DIALOG_PRINT(SCH_EDIT_FRAME *aParent)
void OnUseColorThemeChecked(wxCommandEvent &event) override
bool TransferDataToWindow() override
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
wxString GetFilename() const
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:75
wxPrintOrientation GetWxOrientation() const
Definition page_info.h:128
double GetHeightMils() const
Definition page_info.h:142
wxPaperSize GetPaperId() const
Definition page_info.h:133
double GetWidthMils() const
Definition page_info.h:137
bool IsCustom() const
bool IsPortrait() const
Definition page_info.h:123
bool m_Printing
wxWidgets on MSW tends to crash if you spool up more than one print job at a time.
Definition pgm_base.h:421
Schematic editor (Eeschema) main window.
Schematic plotting class.
void Plot(PLOT_FORMAT aPlotFormat, const SCH_PLOT_OPTS &aPlotOpts, SCH_RENDER_SETTINGS *aRenderSettings, REPORTER *aReporter=nullptr)
Perform the plotting of the schematic using the given aPlotFormat and a\ aPlotSettings.
Custom schematic print preview frame.
SCH_PREVIEW_FRAME(wxPrintPreview *aPreview, wxWindow *aParent, const wxString &aTitle)
static wxSize s_size
static wxPoint s_pos
bool Show(bool show) override
Custom print out for printing schematics.
void LoadColors(const COLOR_SETTINGS *aSettings) override
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void SortByHierarchicalPageNumbers(bool aUpdateVirtualPageNums=true)
This works like SortByPageNumbers, but it sorts the sheets first by their hierarchical depth and then...
void BuildSheetList(SCH_SHEET *aSheet, bool aCheckIntegrity)
Build the list of sheets and their sheet path from aSheet.
A temporary file or directory that is removed when the object goes out of scope.
Definition gestfich.h:63
bool MakeDirectory(const wxString &aPrefix, int aPermissions=wxS_DIR_DEFAULT)
Create an empty directory with a unique name after aPrefix.
const wxString & Path() const
Definition gestfich.h:77
A wrapper for reporting to a wxString object.
Definition reporter.h:242
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:192
This file is part of the common library.
#define _(s)
KICOMMON_API int Mils2mm(double aVal)
Convert mils to mm.
Definition eda_units.cpp:78
PRINT_RESULT PrintPDF(const std::string &aFile, int aCurrentPage=0)
Show the platform print dialog for the PDF aFile and print it.
const wxString PrintResultToString(PRINT_RESULT aResult)
Definition printing.h:44
void ResetPrintToFilePath(wxPrintData &aData)
Discard a "print to file" destination in aData that the platform print backend generated for its own ...
PAGE_SIZE_TYPE
Definition page_info.h:46
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
@ RPT_SEVERITY_ERROR
COLOR_SETTINGS * GetColorSettings(const wxString &aName)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
bool monochrome
Whether or not to print in monochrome.
bool background
Whether or not to print background color.
wxString color_theme
Color theme to use for printing.
bool title_block
Whether or not to print title block.
bool use_theme
If false, display color theme will be used.
wxString m_theme
Definition sch_plotter.h:69
wxString m_outputFile
Definition sch_plotter.h:72
bool m_blackAndWhite
Definition sch_plotter.h:63
bool m_useBackgroundColor
Definition sch_plotter.h:65
bool m_plotDrawingSheet
Definition sch_plotter.h:56
IbisParser parser & reporter
wxString result
Test unit parsing edge cases and error handling.