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