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 <printing.h>
38#include <sch_plotter.h>
39
40#include "sch_printout.h"
41
42
47class SCH_PREVIEW_FRAME : public wxPreviewFrame
48{
49public:
50 SCH_PREVIEW_FRAME( wxPrintPreview* aPreview, wxWindow* aParent, const wxString& aTitle ) :
51 wxPreviewFrame( aPreview, aParent, aTitle )
52 {
53 }
54
55 bool Show( bool show ) override
56 {
57 bool ret;
58
59 // Show or hide the window. If hiding, save current position and size.
60 // If showing, use previous position and size.
61 if( show )
62 {
63 ret = wxPreviewFrame::Show( show );
64
65 if( s_size.x != 0 && s_size.y != 0 )
66 SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
67 }
68 else
69 {
70 // Save the dialog's position & size before hiding
71 s_size = GetSize();
72 s_pos = GetPosition();
73
74 ret = wxPreviewFrame::Show( show );
75 }
76
77 return ret;
78 }
79
80private:
81 static wxPoint s_pos;
82 static wxSize s_size;
83};
84
85
88
89
91 DIALOG_PRINT_BASE( aParent ),
92 m_parent( aParent )
93{
94 wxASSERT( aParent );
95
96 // Show m_panelPrinters only if there are printers to list:
97 m_panelPrinters->Show( m_panelPrinters->HasPrintersAvailable() );
98
99 SetupStandardButtons( { { wxID_OK, _( "Print" ) },
100 { wxID_APPLY, _( "Print Preview" ) },
101 { wxID_CANCEL, _( "Close" ) } } );
102
103#ifdef __WXMAC__
104 // Problems with modal on wx-2.9 - Anyway preview is standard for OSX
105 m_sdbSizerApply->Hide();
106#endif
107#if defined(__WXGTK__)
108 // Preview using Cairo does not work on GTK,
109 // but this platform provide native print preview
110 m_sdbSizerApply->Hide();
111#endif
112
113 // New printing subsystem has print preview on all platforms
114#if defined( _MSC_VER )
115 if( ADVANCED_CFG::GetCfg().m_UsePdfPrint )
116 {
117 m_sdbSizerApply->Hide();
118 }
119#endif
120
121 m_sdbSizerOK->SetFocus();
122
123 Layout();
124
126}
127
128
133
134
136{
137 EESCHEMA_SETTINGS* cfg = m_parent->eeconfig();
138
139 if( cfg->m_Printing.monochrome )
140 {
141 m_checkBackgroundColor->SetValue( false );
142 m_checkBackgroundColor->Enable( false );
143 }
144
145 m_checkReference->SetValue( cfg->m_Printing.title_block );
146 m_colorPrint->SetSelection( cfg->m_Printing.monochrome ? 1 : 0 );
148 m_checkUseColorTheme->SetValue( cfg->m_Printing.use_theme );
149
150 m_colorTheme->Clear();
151
152 int width = 0;
153 int height = 0;
154 int minwidth = width;
155
156 wxString target = cfg->m_Printing.use_theme ? cfg->m_Printing.color_theme : cfg->m_ColorTheme;
157
158 for( COLOR_SETTINGS* settings : Pgm().GetSettingsManager().GetColorSettingsList() )
159 {
160 int pos = m_colorTheme->Append( settings->GetName(), static_cast<void*>( settings ) );
161
162 if( settings->GetFilename() == target )
163 m_colorTheme->SetSelection( pos );
164
165 m_colorTheme->GetTextExtent( settings->GetName(), &width, &height );
166 minwidth = std::max( minwidth, width );
167 }
168
169 m_colorTheme->SetMinSize( wxSize( minwidth + 50, -1 ) );
170
171 m_colorTheme->Enable( cfg->m_Printing.use_theme );
172
173 // Initialize page specific print setup dialog settings.
174 const PAGE_INFO& pageInfo = m_parent->GetScreen()->GetPageSettings();
175 wxPageSetupDialogData& pageSetupDialogData = m_parent->GetPageSetupData();
176
177 pageSetupDialogData.SetPaperId( pageInfo.GetPaperId() );
178
179 if( pageInfo.IsCustom() )
180 {
181 if( pageInfo.IsPortrait() )
182 {
183 pageSetupDialogData.SetPaperSize( wxSize( EDA_UNIT_UTILS::Mils2mm( pageInfo.GetWidthMils() ),
184 EDA_UNIT_UTILS::Mils2mm( pageInfo.GetHeightMils() ) ) );
185 }
186 else
187 {
188 pageSetupDialogData.SetPaperSize( wxSize( EDA_UNIT_UTILS::Mils2mm( pageInfo.GetHeightMils() ),
189 EDA_UNIT_UTILS::Mils2mm( pageInfo.GetWidthMils() ) ) );
190 }
191 }
192
193 pageSetupDialogData.GetPrintData().SetOrientation( pageInfo.GetWxOrientation() );
194
195 Layout();
196
197 return true;
198}
199
200
201void DIALOG_PRINT::OnUseColorThemeChecked( wxCommandEvent& event )
202{
203 m_colorTheme->Enable( m_checkUseColorTheme->GetValue() );
204}
205
206
207void DIALOG_PRINT::OnOutputChoice( wxCommandEvent& event )
208{
209 long sel = event.GetSelection();
210 m_checkBackgroundColor->Enable( sel == 0 );
211
212 if( sel )
213 m_checkBackgroundColor->SetValue( false );
214 else
215 m_checkBackgroundColor->SetValue( m_parent->eeconfig()->m_Printing.background );
216}
217
218
220{
221 EESCHEMA_SETTINGS* cfg = m_parent->eeconfig();
222
223 cfg->m_Printing.monochrome = !!m_colorPrint->GetSelection();
224 cfg->m_Printing.title_block = m_checkReference->IsChecked();
225
226 if( m_checkBackgroundColor->IsEnabled() )
227 cfg->m_Printing.background = m_checkBackgroundColor->IsChecked();
228 else
229 cfg->m_Printing.background = false;
230
231 cfg->m_Printing.use_theme = m_checkUseColorTheme->IsChecked();
232
233 COLOR_SETTINGS* theme = static_cast<COLOR_SETTINGS*>( m_colorTheme->GetClientData( m_colorTheme->GetSelection() ) );
234
235 if( theme && m_checkUseColorTheme->IsChecked() )
236 cfg->m_Printing.color_theme = theme->GetFilename();
237}
238
239
240void DIALOG_PRINT::OnPageSetup( wxCommandEvent& event )
241{
242 wxPageSetupDialog pageSetupDialog( this, &m_parent->GetPageSetupData() );
243 pageSetupDialog.ShowModal();
244
245 m_parent->GetPageSetupData() = pageSetupDialog.GetPageSetupDialogData();
246}
247
248
249void DIALOG_PRINT::OnPrintPreview( wxCommandEvent& event )
250{
252 wxPrintData& prn_data = m_parent->GetPageSetupData().GetPrintData();
253
254 wxString selectedPrinterName;
255
256 if( m_panelPrinters )
257 selectedPrinterName = m_panelPrinters->GetSelectedPrinterName();
258
259 prn_data.SetPrinterName( selectedPrinterName );
260
261 // Pass two printout objects: for preview, and possible printing.
262 wxString title = _( "Preview" );
263 wxPrintPreview* preview = new wxPrintPreview( new SCH_PRINTOUT( m_parent, title ),
264 new SCH_PRINTOUT( m_parent, title ), &prn_data );
265
266 preview->SetZoom( 100 );
267
268 SCH_PREVIEW_FRAME* frame = new SCH_PREVIEW_FRAME( preview, this, title );
269
270 // On wxGTK, set the flag wxTOPLEVEL_EX_DIALOG is mandatory, if we want
271 // close the frame using the X box in caption, when the preview frame is run
272 // from a dialog
273 frame->SetExtraStyle( frame->GetExtraStyle() | wxTOPLEVEL_EX_DIALOG );
274
275 // We use here wxPreviewFrame_WindowModal option to make the wxPrintPreview frame
276 // modal for its caller only.
277 // another reason is the fact when closing the frame without this option,
278 // all top level frames are reenabled.
279 // With this option, only the parent is reenabled.
280 // Reenabling all top level frames should be made by the parent dialog.
281 frame->InitializeWithModality( wxPreviewFrame_WindowModal );
282
283 // on first invocation in this runtime session, set to 3/4 size of parent,
284 // but will be changed in Show() if not first time as will position.
285 // Must be called after InitializeWithModality because otherwise in some wxWidget
286 // versions it is not always taken in account
287 frame->SetMinSize( wxSize( 650, 500 ) );
288 frame->SetSize( (m_parent->GetSize() * 3) / 4 );
289
290 frame->Raise(); // Needed on Ubuntu/Unity to display the frame
291 frame->Show( true );
292}
293
294
296{
297 if( Pgm().m_Printing )
298 {
299 DisplayError( this, _( "Previous print job not yet complete." ) );
300 return false;
301 }
302
304
305#ifndef __MINGW32__
306 if( ADVANCED_CFG::GetCfg().m_UsePdfPrint )
307 {
308 EESCHEMA_SETTINGS* cfg = m_parent->eeconfig();
309
310 SCH_RENDER_SETTINGS renderSettings( *m_parent->GetRenderSettings() );
311 renderSettings.m_ShowHiddenPins = false;
312 renderSettings.m_ShowHiddenFields = false;
313
315 : cfg->m_ColorTheme );
316 renderSettings.LoadColors( cs );
317
318 SCH_PLOT_OPTS plotOpts;
320 plotOpts.m_blackAndWhite = cfg->m_Printing.monochrome;
322 plotOpts.m_theme = cfg->m_Printing.use_theme ? cfg->m_Printing.color_theme
323 : cfg->m_ColorTheme;
324
325 wxFileName tmp = wxFileName::CreateTempFileName( wxS( "eeschema_print" ) );
326 wxRemoveFile( tmp.GetFullPath() );
327 tmp.SetExt( wxS( "pdf" ) );
328 plotOpts.m_outputFile = tmp.GetFullPath();
329
330 SCH_PLOTTER plotter( m_parent );
331
332 Pgm().m_Printing = true;
333 plotter.Plot( PLOT_FORMAT::PDF, plotOpts, &renderSettings, nullptr );
334 Pgm().m_Printing = false;
335
338
341 {
343 }
344
345 return true;
346 }
347#endif
348
349 int sheet_count = m_parent->Schematic().Root().CountSheets();
350
351 wxPrintData& data = m_parent->GetPageSetupData().GetPrintData();
352
353#if defined( __WXGTK__ ) && !wxCHECK_VERSION( 3, 2, 3 )
354 // In GTK, the default bottom margin is bigger by 0.31 inches for
355 // Letter, Legal, A4 paper sizes (see gtk_paper_size_get_default_bottom_margin).
356 //
357 // wxWidgets doesn't handle this properly when paper is in
358 // landscape orientation.
359 //
360 // Using custom page size avoids the problematic
361 // gtk_page_setup_set_paper_size_and_default_margins call in wxWidgets.
362
363 wxPaperSize paperId = data.GetPaperId();
364 PAGE_SIZE_TYPE paperType = PAGE_SIZE_TYPE::A4; // default; overwritten below when matched
365 bool havePaperType = false;
366
367 // clang-format off
368 std::set<wxPaperSize> letterSizes =
369 {
370 // na_letter
371 wxPAPER_LETTER,
372 wxPAPER_LETTERSMALL,
373 wxPAPER_NOTE,
374 wxPAPER_LETTER_TRANSVERSE,
375 wxPAPER_LETTER_ROTATED
376 };
377
378 std::set<wxPaperSize> legalSizes =
379 {
380 // na_legal
381 wxPAPER_LEGAL
382 };
383
384 std::set<wxPaperSize> a4Sizes =
385 {
386 // iso_a4
387 wxPAPER_A4,
388 wxPAPER_A4SMALL,
389 wxPAPER_A4_TRANSVERSE,
390 wxPAPER_A4_ROTATED
391 };
392 // clang-format on
393
394 if( letterSizes.count( paperId ) )
395 {
396 paperType = PAGE_SIZE_TYPE::USLetter;
397 havePaperType = true;
398 }
399 else if( legalSizes.count( paperId ) )
400 {
401 paperType = PAGE_SIZE_TYPE::USLegal;
402 havePaperType = true;
403 }
404 else if( a4Sizes.count( paperId ) )
405 {
406 paperType = PAGE_SIZE_TYPE::A4;
407 havePaperType = true;
408 }
409
410 if( havePaperType )
411 {
412 PAGE_INFO pageInfo( paperType, data.GetOrientation() == wxPORTRAIT );
413
414 if( pageInfo.IsPortrait() )
415 {
416 data.SetPaperSize( wxSize( EDA_UNIT_UTILS::Mils2mm( pageInfo.GetWidthMils() ),
417 EDA_UNIT_UTILS::Mils2mm( pageInfo.GetHeightMils() ) ) );
418 }
419 else
420 {
421 data.SetPaperSize( wxSize( EDA_UNIT_UTILS::Mils2mm( pageInfo.GetHeightMils() ),
422 EDA_UNIT_UTILS::Mils2mm( pageInfo.GetWidthMils() ) ) );
423 }
424
425 data.SetOrientation( pageInfo.GetWxOrientation() );
426 data.SetPaperId( wxPAPER_NONE );
427 }
428#endif
429
430 wxString selectedPrinterName;
431
432 if( m_panelPrinters )
433 selectedPrinterName = m_panelPrinters->GetSelectedPrinterName();
434
435 data.SetPrinterName( selectedPrinterName );
436
437 wxPrintDialogData printDialogData( data );
438 printDialogData.SetMaxPage( sheet_count );
439
440 if( sheet_count > 1 )
441 printDialogData.EnablePageNumbers( true );
442
443 wxPrinter printer( &printDialogData );
444 SCH_PRINTOUT printout( m_parent, _( "Print Schematic" ) );
445
446 Pgm().m_Printing = true;
447 {
448 if( !printer.Print( this, &printout, true ) )
449 {
450 if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
451 DisplayError( this, _( "An error occurred attempting to print the schematic." ) );
452 }
453 else
454 {
455 m_parent->GetPageSetupData() = printer.GetPrintDialogData().GetPrintData();
456 }
457 }
458
459 Pgm().m_Printing = false;
460
461 return true;
462}
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...
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:129
double GetHeightMils() const
Definition page_info.h:143
wxPaperSize GetPaperId() const
Definition page_info.h:134
double GetWidthMils() const
Definition page_info.h:138
bool IsCustom() const
bool IsPortrait() const
Definition page_info.h:124
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:382
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.
wxString GetLastOutputFilePath() const
Get the last output file path, this is mainly intended for PDFs with the open after plot GUI option.
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
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
const wxString PrintResultToString(PRINT_RESULT aResult)
Definition printing.h:42
PRINT_RESULT PrintPDF(const std::string &aFile)
PAGE_SIZE_TYPE
Definition page_info.h:46
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
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:64
wxString m_outputFile
Definition sch_plotter.h:67
bool m_blackAndWhite
Definition sch_plotter.h:58
bool m_useBackgroundColor
Definition sch_plotter.h:60
bool m_plotDrawingSheet
Definition sch_plotter.h:54
wxString result
Test unit parsing edge cases and error handling.