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 "dialog_print.h"
35#include "sch_printout.h"
36
37
42class SCH_PREVIEW_FRAME : public wxPreviewFrame
43{
44public:
45 SCH_PREVIEW_FRAME( wxPrintPreview* aPreview, wxWindow* aParent,
46 const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
47 const wxSize& aSize = wxDefaultSize ) :
48 wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
49 {
50 }
51
52 bool Show( bool show ) override
53 {
54 bool ret;
55
56 // Show or hide the window. If hiding, save current position and size.
57 // If showing, use previous position and size.
58 if( show )
59 {
60 ret = wxPreviewFrame::Show( show );
61
62 if( s_size.x != 0 && s_size.y != 0 )
63 SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
64 }
65 else
66 {
67 // Save the dialog's position & size before hiding
68 s_size = GetSize();
69 s_pos = GetPosition();
70
71 ret = wxPreviewFrame::Show( show );
72 }
73
74 return ret;
75 }
76
77private:
78 static wxPoint s_pos;
79 static wxSize s_size;
80};
81
82
85
86
88 DIALOG_PRINT_BASE( aParent ),
89 m_parent( aParent )
90{
91 wxASSERT( aParent );
93
94 SetupStandardButtons( { { wxID_OK, _( "Print" ) },
95 { wxID_APPLY, _( "Print Preview" ) },
96 { wxID_CANCEL, _( "Close" ) } } );
97
98#ifdef __WXMAC__
99 // Problems with modal on wx-2.9 - Anyway preview is standard for OSX
100 m_sdbSizer1Apply->Hide();
101#endif
102#if defined(__WXGTK__)
103 // Preview using Cairo does not work on GTK,
104 // but this platform provide native print preview
105 if( m_useCairo )
106 m_sdbSizer1Apply->Hide();
107#endif
108
109 m_sdbSizer1OK->SetFocus();
110
112}
113
114
116{
118}
119
120
122{
124
125 if( cfg->m_Printing.monochrome )
126 {
127 m_checkBackgroundColor->SetValue( false );
128 m_checkBackgroundColor->Enable( false );
129 }
130
131 m_checkReference->SetValue( cfg->m_Printing.title_block );
132 m_colorPrint->SetSelection( cfg->m_Printing.monochrome ? 1 : 0 );
134 m_checkUseColorTheme->SetValue( cfg->m_Printing.use_theme );
135
136 m_colorTheme->Clear();
137
138 int width = 0;
139 int height = 0;
140 int minwidth = width;
141
142 wxString target = cfg->m_Printing.use_theme ? cfg->m_Printing.color_theme : cfg->m_ColorTheme;
143
144 for( COLOR_SETTINGS* settings : Pgm().GetSettingsManager().GetColorSettingsList() )
145 {
146 int pos = m_colorTheme->Append( settings->GetName(), static_cast<void*>( settings ) );
147
148 if( settings->GetFilename() == target )
149 m_colorTheme->SetSelection( pos );
150
151 m_colorTheme->GetTextExtent( settings->GetName(), &width, &height );
152 minwidth = std::max( minwidth, width );
153 }
154
155 m_colorTheme->SetMinSize( wxSize( minwidth + 50, -1 ) );
156
157 m_colorTheme->Enable( cfg->m_Printing.use_theme );
158
159 // Initialize page specific print setup dialog settings.
160 const PAGE_INFO& pageInfo = m_parent->GetScreen()->GetPageSettings();
161 wxPageSetupDialogData& pageSetupDialogData = m_parent->GetPageSetupData();
162
163 pageSetupDialogData.SetPaperId( pageInfo.GetPaperId() );
164
165 if( pageInfo.IsCustom() )
166 {
167 if( pageInfo.IsPortrait() )
168 {
169 pageSetupDialogData.SetPaperSize( wxSize( EDA_UNIT_UTILS::Mils2mm( pageInfo.GetWidthMils() ),
170 EDA_UNIT_UTILS::Mils2mm( pageInfo.GetHeightMils() ) ) );
171 }
172 else
173 {
174 pageSetupDialogData.SetPaperSize( wxSize( EDA_UNIT_UTILS::Mils2mm( pageInfo.GetHeightMils() ),
175 EDA_UNIT_UTILS::Mils2mm( pageInfo.GetWidthMils() ) ) );
176 }
177 }
178
179 pageSetupDialogData.GetPrintData().SetOrientation( pageInfo.GetWxOrientation() );
180
181 Layout();
182
183 return true;
184}
185
186
187void DIALOG_PRINT::OnUseColorThemeChecked( wxCommandEvent& event )
188{
189 m_colorTheme->Enable( m_checkUseColorTheme->GetValue() );
190}
191
192
193void DIALOG_PRINT::OnOutputChoice( wxCommandEvent& event )
194{
195 long sel = event.GetSelection();
196 m_checkBackgroundColor->Enable( sel == 0 );
197
198 if( sel )
199 m_checkBackgroundColor->SetValue( false );
200 else
202}
203
204
206{
208
209 cfg->m_Printing.monochrome = !!m_colorPrint->GetSelection();
210 cfg->m_Printing.title_block = m_checkReference->IsChecked();
211
212 if( m_checkBackgroundColor->IsEnabled() )
213 cfg->m_Printing.background = m_checkBackgroundColor->IsChecked();
214 else
215 cfg->m_Printing.background = false;
216
217 cfg->m_Printing.use_theme = m_checkUseColorTheme->IsChecked();
218
219 COLOR_SETTINGS* theme = static_cast<COLOR_SETTINGS*>(
220 m_colorTheme->GetClientData( m_colorTheme->GetSelection() ) );
221
222 if( theme && m_checkUseColorTheme->IsChecked() )
223 cfg->m_Printing.color_theme = theme->GetFilename();
224}
225
226
227void DIALOG_PRINT::OnPageSetup( wxCommandEvent& event )
228{
229 wxPageSetupDialog pageSetupDialog( this, &m_parent->GetPageSetupData() );
230 pageSetupDialog.ShowModal();
231
232 m_parent->GetPageSetupData() = pageSetupDialog.GetPageSetupDialogData();
233}
234
235
236void DIALOG_PRINT::OnPrintPreview( wxCommandEvent& event )
237{
239
240 // Pass two printout objects: for preview, and possible printing.
241 wxString title = _( "Preview" );
242 wxPrintPreview* preview = new wxPrintPreview( new SCH_PRINTOUT( m_parent, title, m_useCairo ),
243 new SCH_PRINTOUT( m_parent, title, m_useCairo ),
244 &m_parent->GetPageSetupData().GetPrintData() );
245
246 preview->SetZoom( 100 );
247
248 SCH_PREVIEW_FRAME* frame = new SCH_PREVIEW_FRAME( preview, this, title );
249
250 // On wxGTK, set the flag wxTOPLEVEL_EX_DIALOG is mandatory, if we want
251 // close the frame using the X box in caption, when the preview frame is run
252 // from a dialog
253 frame->SetExtraStyle( frame->GetExtraStyle() | wxTOPLEVEL_EX_DIALOG );
254
255 // We use here wxPreviewFrame_WindowModal option to make the wxPrintPreview frame
256 // modal for its caller only.
257 // another reason is the fact when closing the frame without this option,
258 // all top level frames are reenabled.
259 // With this option, only the parent is reenabled.
260 // Reenabling all top level frames should be made by the parent dialog.
261 frame->InitializeWithModality( wxPreviewFrame_WindowModal );
262
263 // on first invocation in this runtime session, set to 3/4 size of parent,
264 // but will be changed in Show() if not first time as will position.
265 // Must be called after InitializeWithModality because otherwise in some wxWidget
266 // versions it is not always taken in account
267 frame->SetMinSize( wxSize( 650, 500 ) );
268 frame->SetSize( (m_parent->GetSize() * 3) / 4 );
269
270 frame->Raise(); // Needed on Ubuntu/Unity to display the frame
271 frame->Show( true );
272}
273
274
276{
277 if( Pgm().m_Printing )
278 {
279 DisplayError( this, _( "Previous print job not yet complete." ) );
280 return false;
281 }
282
284
285 int sheet_count = m_parent->Schematic().Root().CountSheets();
286
287 wxPrintData data = m_parent->GetPageSetupData().GetPrintData();
288
289#if defined( __WXGTK__ ) && !wxCHECK_VERSION( 3, 2, 3 )
290 // In GTK, the default bottom margin is bigger by 0.31 inches for
291 // Letter, Legal, A4 paper sizes (see gtk_paper_size_get_default_bottom_margin).
292 //
293 // wxWidgets doesn't handle this properly when paper is in
294 // landscape orientation.
295 //
296 // Using custom page size avoids the problematic
297 // gtk_page_setup_set_paper_size_and_default_margins call in wxWidgets.
298
299 wxPaperSize paperId = data.GetPaperId();
300 const wxChar* paperType = nullptr;
301
302 // clang-format off
303 std::set<wxPaperSize> letterSizes = {
304 // na_letter
305 wxPAPER_LETTER,
306 wxPAPER_LETTERSMALL,
307 wxPAPER_NOTE,
308 wxPAPER_LETTER_TRANSVERSE,
309 wxPAPER_LETTER_ROTATED
310 };
311
312 std::set<wxPaperSize> legalSizes = {
313 // na_legal
314 wxPAPER_LEGAL
315 };
316
317 std::set<wxPaperSize> a4Sizes = {
318 // iso_a4
319 wxPAPER_A4,
320 wxPAPER_A4SMALL,
321 wxPAPER_A4_TRANSVERSE,
322 wxPAPER_A4_ROTATED
323 };
324 // clang-format on
325
326 if( letterSizes.count( paperId ) )
327 paperType = PAGE_INFO::USLetter;
328 else if( legalSizes.count( paperId ) )
329 paperType = PAGE_INFO::USLegal;
330 else if( a4Sizes.count( paperId ) )
331 paperType = PAGE_INFO::A4;
332
333 if( paperType )
334 {
335 PAGE_INFO pageInfo( paperType, data.GetOrientation() == wxPORTRAIT );
336
337 if( pageInfo.IsPortrait() )
338 data.SetPaperSize( wxSize( EDA_UNIT_UTILS::Mils2mm( pageInfo.GetWidthMils() ),
339 EDA_UNIT_UTILS::Mils2mm( pageInfo.GetHeightMils() ) ) );
340 else
341 data.SetPaperSize( wxSize( EDA_UNIT_UTILS::Mils2mm( pageInfo.GetHeightMils() ),
342 EDA_UNIT_UTILS::Mils2mm( pageInfo.GetWidthMils() ) ) );
343
344 data.SetOrientation( pageInfo.GetWxOrientation() );
345 data.SetPaperId( wxPAPER_NONE );
346 }
347#endif
348
349 wxPrintDialogData printDialogData( data );
350 printDialogData.SetMaxPage( sheet_count );
351
352 if( sheet_count > 1 )
353 printDialogData.EnablePageNumbers( true );
354
355 wxPrinter printer( &printDialogData );
356 SCH_PRINTOUT printout( m_parent, _( "Print Schematic" ), m_useCairo );
357
358 Pgm().m_Printing = true;
359 {
360 if( !printer.Print( this, &printout, true ) )
361 {
362 if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
363 DisplayError( this, _( "An error occurred attempting to print the schematic." ) );
364 }
365 else
366 {
367 m_parent->GetPageSetupData() = printer.GetPrintDialogData().GetPrintData();
368 }
369 }
370
371 Pgm().m_Printing = false;
372
373 return true;
374}
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
wxString m_ColorTheme
Active color theme name.
Definition: app_settings.h:199
Color settings are a bit different than most of the settings objects in that there can be more than o...
Class DIALOG_PRINT_BASE.
wxCheckBox * m_checkBackgroundColor
wxButton * m_sdbSizer1OK
wxButton * m_sdbSizer1Apply
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
Definition: dialog_print.h:49
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
Definition: json_settings.h:80
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:59
static const wxChar USLetter[]
Definition: page_info.h:79
static const wxChar USLegal[]
Definition: page_info.h:80
static const wxChar A4[]
Definition: page_info.h:68
wxPrintOrientation GetWxOrientation() const
Definition: page_info.h:127
double GetHeightMils() const
Definition: page_info.h:141
wxPaperSize GetPaperId() const
Definition: page_info.h:132
double GetWidthMils() const
Definition: page_info.h:136
bool IsCustom() const
Definition: page_info.cpp:182
bool IsPortrait() const
Definition: page_info.h:122
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:378
SCH_SHEET & Root() const
Definition: schematic.h:130
EESCHEMA_SETTINGS * eeconfig() const
Schematic editor (Eeschema) main window.
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
wxPageSetupDialogData & GetPageSetupData()
SCHEMATIC & Schematic() const
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.
Definition: sch_printout.h:39
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:130
int CountSheets() const
Count the number of sheets found in "this" sheet including all of the subsheets.
Definition: sch_sheet.cpp:835
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:170
This file is part of the common library.
#define _(s)
bool m_EnableEeschemaPrintCairo
Enable Eeschema printing using Cairo.
KICOMMON_API int Mils2mm(double aVal)
Convert mils to mm.
Definition: eda_units.cpp:82
SETTINGS_MANAGER * GetSettingsManager()
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:1073
see class PGM_BASE
bool monochrome
Whether or not to print in monochrome.
Definition: app_settings.h:141
bool background
Whether or not to print background color.
Definition: app_settings.h:140
wxString color_theme
Color theme to use for printing.
Definition: app_settings.h:144
bool title_block
Whether or not to print title block.
Definition: app_settings.h:145
bool use_theme
If false, display color theme will be used.
Definition: app_settings.h:143