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