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