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, const wxString& aTitle ) :
55 wxPreviewFrame( aPreview, aParent, aTitle )
56 {
57 }
58
59 bool Show( bool show ) override
60 {
61 bool ret;
62
63 // Show or hide the window. If hiding, save current position and size.
64 // If showing, use previous position and size.
65 if( show )
66 {
67 ret = wxPreviewFrame::Show( show );
68
69 if( s_size.x != 0 && s_size.y != 0 )
70 SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
71 }
72 else
73 {
74 // Save the dialog's position & size before hiding
75 s_size = GetSize();
76 s_pos = GetPosition();
77
78 ret = wxPreviewFrame::Show( show );
79 }
80
81 return ret;
82 }
83
84private:
85 static wxPoint s_pos;
86 static wxSize s_size;
87};
88
89
92
93
95 DIALOG_PRINT_BASE( aParent ),
96 m_parent( aParent )
97{
98 wxASSERT( aParent );
99
100 // Show m_panelPrinters only if there are printers to list:
101 m_panelPrinters->Show( m_panelPrinters->HasPrintersAvailable() );
102
103 SetupStandardButtons( { { wxID_OK, _( "Print" ) },
104 { wxID_APPLY, _( "Print Preview" ) },
105 { wxID_CANCEL, _( "Close" ) } } );
106
107#ifdef __WXMAC__
108 // Problems with modal on wx-2.9 - Anyway preview is standard for OSX
109 m_sdbSizerApply->Hide();
110#endif
111#if defined(__WXGTK__)
112 // Preview using Cairo does not work on GTK,
113 // but this platform provide native print preview
114 m_sdbSizerApply->Hide();
115#endif
116
117 // New printing subsystem has print preview on all platforms
118#if defined( _MSC_VER )
119 if( ADVANCED_CFG::GetCfg().m_UsePdfPrint )
120 {
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 {
312 EESCHEMA_SETTINGS* cfg = m_parent->eeconfig();
313
314 SCH_RENDER_SETTINGS renderSettings( *m_parent->GetRenderSettings() );
315 renderSettings.m_ShowHiddenPins = false;
316 renderSettings.m_ShowHiddenFields = false;
317
319 : cfg->m_ColorTheme );
320 renderSettings.LoadColors( cs );
321
322 SCH_PLOT_OPTS plotOpts;
324 plotOpts.m_blackAndWhite = cfg->m_Printing.monochrome;
326 plotOpts.m_theme = cfg->m_Printing.use_theme ? cfg->m_Printing.color_theme
327 : cfg->m_ColorTheme;
328
329 wxFileName tmp = wxFileName::CreateTempFileName( wxS( "eeschema_print" ) );
330 wxRemoveFile( tmp.GetFullPath() );
331 tmp.SetExt( wxS( "pdf" ) );
332 plotOpts.m_outputFile = tmp.GetFullPath();
333
334 SCH_PLOTTER plotter( m_parent );
335
336 Pgm().m_Printing = true;
337 plotter.Plot( PLOT_FORMAT::PDF, plotOpts, &renderSettings, nullptr );
338 Pgm().m_Printing = false;
339
342
345 {
347 }
348
349 return true;
350 }
351#endif
352
353 int sheet_count = m_parent->Schematic().Root().CountSheets();
354
355 wxPrintData& data = m_parent->GetPageSetupData().GetPrintData();
356
357#if defined( __WXGTK__ ) && !wxCHECK_VERSION( 3, 2, 3 )
358 // In GTK, the default bottom margin is bigger by 0.31 inches for
359 // Letter, Legal, A4 paper sizes (see gtk_paper_size_get_default_bottom_margin).
360 //
361 // wxWidgets doesn't handle this properly when paper is in
362 // landscape orientation.
363 //
364 // Using custom page size avoids the problematic
365 // gtk_page_setup_set_paper_size_and_default_margins call in wxWidgets.
366
367 wxPaperSize paperId = data.GetPaperId();
368 PAGE_SIZE_TYPE paperType = PAGE_SIZE_TYPE::A4; // default; overwritten below when matched
369 bool havePaperType = false;
370
371 // clang-format off
372 std::set<wxPaperSize> letterSizes =
373 {
374 // na_letter
375 wxPAPER_LETTER,
376 wxPAPER_LETTERSMALL,
377 wxPAPER_NOTE,
378 wxPAPER_LETTER_TRANSVERSE,
379 wxPAPER_LETTER_ROTATED
380 };
381
382 std::set<wxPaperSize> legalSizes =
383 {
384 // na_legal
385 wxPAPER_LEGAL
386 };
387
388 std::set<wxPaperSize> a4Sizes =
389 {
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 {
400 paperType = PAGE_SIZE_TYPE::USLetter;
401 havePaperType = true;
402 }
403 else if( legalSizes.count( paperId ) )
404 {
405 paperType = PAGE_SIZE_TYPE::USLegal;
406 havePaperType = true;
407 }
408 else if( a4Sizes.count( paperId ) )
409 {
410 paperType = PAGE_SIZE_TYPE::A4;
411 havePaperType = true;
412 }
413
414 if( havePaperType )
415 {
416 PAGE_INFO pageInfo( paperType, data.GetOrientation() == wxPORTRAIT );
417
418 if( pageInfo.IsPortrait() )
419 {
420 data.SetPaperSize( wxSize( EDA_UNIT_UTILS::Mils2mm( pageInfo.GetWidthMils() ),
421 EDA_UNIT_UTILS::Mils2mm( pageInfo.GetHeightMils() ) ) );
422 }
423 else
424 {
425 data.SetPaperSize( wxSize( EDA_UNIT_UTILS::Mils2mm( pageInfo.GetHeightMils() ),
426 EDA_UNIT_UTILS::Mils2mm( pageInfo.GetWidthMils() ) ) );
427 }
428
429 data.SetOrientation( pageInfo.GetWxOrientation() );
430 data.SetPaperId( wxPAPER_NONE );
431 }
432#endif
433
434 wxString selectedPrinterName;
435
436 if( m_panelPrinters )
437 selectedPrinterName = m_panelPrinters->GetSelectedPrinterName();
438
439 data.SetPrinterName( selectedPrinterName );
440
441 wxPrintDialogData printDialogData( data );
442 printDialogData.SetMaxPage( sheet_count );
443
444 if( sheet_count > 1 )
445 printDialogData.EnablePageNumbers( true );
446
447 wxPrinter printer( &printDialogData );
448 SCH_PRINTOUT printout( m_parent, _( "Print Schematic" ) );
449
450 Pgm().m_Printing = true;
451 {
452 if( !printer.Print( this, &printout, true ) )
453 {
454 if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
455 DisplayError( this, _( "An error occurred attempting to print the schematic." ) );
456 }
457 else
458 {
459 m_parent->GetPageSetupData() = printer.GetPrintDialogData().GetPrintData();
460 }
461 }
462
463 Pgm().m_Printing = false;
464
465 return true;
466}
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:390
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:196
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)
PAGE_SIZE_TYPE
Definition page_info.h:50
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:68
wxString m_outputFile
Definition sch_plotter.h:71
bool m_blackAndWhite
Definition sch_plotter.h:62
bool m_useBackgroundColor
Definition sch_plotter.h:64
bool m_plotDrawingSheet
Definition sch_plotter.h:58
wxString result
Test unit parsing edge cases and error handling.