KiCad PCB EDA Suite
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-2021 KiCad Developers, see CHANGELOG.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>
32 #include <sch_sheet.h>
33 #include <schematic.h>
34 #include <sch_sheet_path.h>
36 #include <sch_painter.h>
37 
39 {
40 public:
42  ~DIALOG_PRINT_USING_PRINTER() override;
43 
44 protected:
45  void OnMonochromeChecked( wxCommandEvent& event ) override;
46  void OnUseColorThemeChecked( wxCommandEvent& event ) override;
47 
48 private:
49  bool TransferDataToWindow() override;
50  bool TransferDataFromWindow() override;
51 
52  void OnPageSetup( wxCommandEvent& event ) override;
53  void OnPrintPreview( wxCommandEvent& event ) override;
54 
55  void SavePrintOptions();
56 
58 };
59 
60 
61 
65 class SCH_PRINTOUT : public wxPrintout
66 {
67 public:
68  SCH_PRINTOUT( SCH_EDIT_FRAME* aParent, const wxString& aTitle ) :
69  wxPrintout( aTitle )
70  {
71  wxASSERT( aParent != NULL );
72  m_parent = aParent;
73  }
74 
75  bool OnPrintPage( int page ) override;
76  bool HasPage( int page ) override;
77  bool OnBeginDocument( int startPage, int endPage ) override;
78  void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo ) override;
79  void PrintPage( SCH_SCREEN* aScreen );
80 
81 private:
83 };
84 
85 
90 class SCH_PREVIEW_FRAME : public wxPreviewFrame
91 {
92 public:
93  SCH_PREVIEW_FRAME( wxPrintPreview* aPreview, wxWindow* aParent,
94  const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
95  const wxSize& aSize = wxDefaultSize ) :
96  wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
97  {
98  }
99 
100  bool Show( bool show ) override
101  {
102  bool ret;
103 
104  // Show or hide the window. If hiding, save current position and size.
105  // If showing, use previous position and size.
106  if( show )
107  {
108  ret = wxPreviewFrame::Show( show );
109 
110  if( s_size.x != 0 && s_size.y != 0 )
111  SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
112  }
113  else
114  {
115  // Save the dialog's position & size before hiding
116  s_size = GetSize();
117  s_pos = GetPosition();
118 
119  ret = wxPreviewFrame::Show( show );
120  }
121 
122  return ret;
123  }
124 
125 private:
126  static wxPoint s_pos;
127  static wxSize s_size;
128 };
129 
130 
133 
134 
137  m_parent( aParent )
138 {
139  wxASSERT( aParent );
140 
141  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
142  // that requires us to correct the button labels here.
143  m_sdbSizer1OK->SetLabel( _( "Print" ) );
144  m_sdbSizer1Apply->SetLabel( _( "Preview" ) );
145  m_sdbSizer1Cancel->SetLabel( _( "Close" ) );
146  m_sdbSizer1->Layout();
147 
148 #ifdef __WXMAC__
149  // Problems with modal on wx-2.9 - Anyway preview is standard for OSX
150  m_sdbSizer1Apply->Hide();
151 #endif
152 
153  m_sdbSizer1OK->SetDefault(); // on linux, this is inadequate to determine
154  // what ENTER does. Must also SetFocus().
155  m_sdbSizer1OK->SetFocus();
156 
158 }
159 
160 
162 {
164 }
165 
166 
168 {
170 
171  m_checkReference->SetValue( cfg->m_Printing.title_block );
172  m_checkMonochrome->SetValue( cfg->m_Printing.monochrome );
173  m_checkBackgroundColor->SetValue( cfg->m_Printing.background );
174  m_checkUseColorTheme->SetValue( cfg->m_Printing.use_theme );
175 
176  m_colorTheme->Clear();
177 
178  int width = 0;
179  int height = 0;
180  int minwidth = width;
181 
182  wxString target = cfg->m_Printing.use_theme ? cfg->m_Printing.color_theme : cfg->m_ColorTheme;
183 
184  for( COLOR_SETTINGS* settings : Pgm().GetSettingsManager().GetColorSettingsList() )
185  {
186  int pos = m_colorTheme->Append( settings->GetName(), static_cast<void*>( settings ) );
187 
188  if( settings->GetFilename() == target )
189  m_colorTheme->SetSelection( pos );
190 
191  m_colorTheme->GetTextExtent( settings->GetName(), &width, &height );
192  minwidth = std::max( minwidth, width );
193  }
194 
195  m_colorTheme->SetMinSize( wxSize( minwidth + 50, -1 ) );
196 
197  m_colorTheme->Enable( cfg->m_Printing.use_theme );
198 
199  // Initialize page specific print setup dialog settings.
200  const PAGE_INFO& pageInfo = m_parent->GetScreen()->GetPageSettings();
201  wxPageSetupDialogData& pageSetupDialogData = m_parent->GetPageSetupData();
202 
203  pageSetupDialogData.SetPaperId( pageInfo.GetPaperId() );
204 
205  if( pageInfo.IsCustom() )
206  {
207  if( pageInfo.IsPortrait() )
208  pageSetupDialogData.SetPaperSize( wxSize( Mils2mm( pageInfo.GetWidthMils() ),
209  Mils2mm( pageInfo.GetHeightMils() ) ) );
210  else
211  pageSetupDialogData.SetPaperSize( wxSize( Mils2mm( pageInfo.GetHeightMils() ),
212  Mils2mm( pageInfo.GetWidthMils() ) ) );
213  }
214 
215  pageSetupDialogData.GetPrintData().SetOrientation( pageInfo.GetWxOrientation() );
216 
217  Layout();
218 
219  return true;
220 }
221 
222 
224 {
225  m_colorTheme->Enable( m_checkUseColorTheme->GetValue() );
226 }
227 
228 
230 {
231  m_checkBackgroundColor->Enable( !m_checkMonochrome->GetValue() );
232 
233  if( m_checkMonochrome->GetValue() )
234  m_checkBackgroundColor->SetValue( false );
235  else
237 }
238 
239 
241 {
243 
244  cfg->m_Printing.monochrome = m_checkMonochrome->IsChecked();
245  cfg->m_Printing.title_block = m_checkReference->IsChecked();
246  cfg->m_Printing.background = m_checkBackgroundColor->IsChecked();
247  cfg->m_Printing.use_theme = m_checkUseColorTheme->IsChecked();
248 
249  COLOR_SETTINGS* theme = static_cast<COLOR_SETTINGS*>(
250  m_colorTheme->GetClientData( m_colorTheme->GetSelection() ) );
251 
252  if( theme && m_checkUseColorTheme->IsChecked() )
253  cfg->m_Printing.color_theme = theme->GetFilename();
254 }
255 
256 
257 void DIALOG_PRINT_USING_PRINTER::OnPageSetup( wxCommandEvent& event )
258 {
259  wxPageSetupDialog pageSetupDialog( this, &m_parent->GetPageSetupData() );
260  pageSetupDialog.ShowModal();
261 
262  m_parent->GetPageSetupData() = pageSetupDialog.GetPageSetupDialogData();
263 }
264 
265 
266 void DIALOG_PRINT_USING_PRINTER::OnPrintPreview( wxCommandEvent& event )
267 {
269 
270  // Pass two printout objects: for preview, and possible printing.
271  wxString title = _( "Preview" );
272  wxPrintPreview* preview = new wxPrintPreview( new SCH_PRINTOUT( m_parent, title ),
273  new SCH_PRINTOUT( m_parent, title ),
274  &m_parent->GetPageSetupData().GetPrintData() );
275 
276  preview->SetZoom( 100 );
277 
278  SCH_PREVIEW_FRAME* frame = new SCH_PREVIEW_FRAME( preview, this, title );
279  frame->SetMinSize( wxSize( 550, 350 ) );
280 
281  // on first invocation in this runtime session, set to 2/3 size of my parent,
282  // but will be changed in Show() if not first time as will position.
283  frame->SetSize( (m_parent->GetSize() * 2) / 3 );
284  frame->Center();
285 
286  // On wxGTK, set the flag wxTOPLEVEL_EX_DIALOG is mandatory, if we want
287  // close the frame using the X box in caption, when the preview frame is run
288  // from a dialog
289  frame->SetExtraStyle( frame->GetExtraStyle() | wxTOPLEVEL_EX_DIALOG );
290 
291  // We use here wxPreviewFrame_WindowModal option to make the wxPrintPreview frame
292  // modal for its caller only.
293  // another reason is the fact when closing the frame without this option,
294  // all top level frames are reenabled.
295  // With this option, only the parent is reenabled.
296  // Reenabling all top level frames should be made by the parent dialog.
297  frame->InitializeWithModality( wxPreviewFrame_WindowModal );
298 
299  frame->Raise(); // Needed on Ubuntu/Unity to display the frame
300  frame->Show( true );
301 }
302 
303 
305 {
306  if( Pgm().m_Printing )
307  {
308  DisplayError( this, _( "Previous print job not yet complete." ) );
309  return false;
310  }
311 
313 
314  int sheet_count = m_parent->Schematic().Root().CountSheets();
315 
316  wxPrintDialogData printDialogData( m_parent->GetPageSetupData().GetPrintData() );
317  printDialogData.SetMaxPage( sheet_count );
318 
319  if( sheet_count > 1 )
320  printDialogData.EnablePageNumbers( true );
321 
322  wxPrinter printer( &printDialogData );
323  SCH_PRINTOUT printout( m_parent, _( "Print Schematic" ) );
324 
325  Pgm().m_Printing = true;
326  {
327  if( !printer.Print( this, &printout, true ) )
328  {
329  if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
330  DisplayError( this, _( "An error occurred attempting to print the schematic." ) );
331  }
332  else
333  {
334  m_parent->GetPageSetupData() = printer.GetPrintDialogData().GetPrintData();
335  }
336  }
337  Pgm().m_Printing = false;
338 
339  return true;
340 }
341 
342 
344 {
345  SCH_SHEET_LIST sheetList = m_parent->Schematic().GetSheets();
346 
347  wxCHECK_MSG( page >= 1 && page <= (int)sheetList.size(), false,
348  wxT( "Cannot print invalid page number." ) );
349 
350  wxCHECK_MSG( sheetList[ page - 1].LastScreen() != NULL, false,
351  wxT( "Cannot print page with NULL screen." ) );
352 
353  wxString msg;
354  msg.Printf( _( "Print page %d" ), page );
355  m_parent->SetMsgPanel( msg, wxEmptyString );
356 
357  SCH_SCREEN* screen = m_parent->GetScreen();
358  SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
359  m_parent->SetCurrentSheet( sheetList[ page - 1 ] );
362  screen = m_parent->GetCurrentSheet().LastScreen();
363  PrintPage( screen );
364  m_parent->SetCurrentSheet( oldsheetpath );
367 
368  return true;
369 }
370 
371 
372 void SCH_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
373 {
374  *minPage = *selPageFrom = 1;
375  *maxPage = *selPageTo = m_parent->Schematic().Root().CountSheets();
376 }
377 
378 
379 bool SCH_PRINTOUT::HasPage( int pageNum )
380 {
381  return m_parent->Schematic().Root().CountSheets() >= pageNum;
382 }
383 
384 
385 bool SCH_PRINTOUT::OnBeginDocument( int startPage, int endPage )
386 {
387  if( !wxPrintout::OnBeginDocument( startPage, endPage ) )
388  return false;
389 
390  return true;
391 }
392 
393 
394 /*
395  * This is the real print function: print the active screen
396  */
398 {
399  wxPoint tmp_startvisu;
400  wxSize pageSizeIU; // Page size in internal units
401  wxPoint old_org;
402  wxRect fitRect;
403  wxDC* dc = GetDC();
404 
405  wxBusyCursor dummy;
406 
407  // Save current offsets and clip box.
408  tmp_startvisu = aScreen->m_StartVisu;
409  old_org = aScreen->m_DrawOrg;
410 
411  SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
414 
415  // Change scale factor and offset to print the whole page.
416  bool printReference = cfg->m_Printing.title_block;
417 
418  pageSizeIU = aScreen->GetPageSettings().GetSizeIU();
419  FitThisSizeToPaper( pageSizeIU );
420 
421  fitRect = GetLogicalPaperRect();
422 
423  // When is the actual paper size does not match the schematic page size, the drawing will
424  // not be centered on X or Y axis. Give a draw offset to center the schematic page on the
425  // paper draw area.
426  int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
427  int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
428 
429  if( dc->CanUseTransformMatrix() )
430  {
431  wxAffineMatrix2D matrix = dc->GetTransformMatrix();
432 
433  // Check for portrait/landscape mismatch:
434  if( ( fitRect.width > fitRect.height ) != ( pageSizeIU.x > pageSizeIU.y ) )
435  {
436  matrix.Rotate( M_PI_2 );
437  xoffset = ( fitRect.height - pageSizeIU.x ) / 2;
438  yoffset = ( fitRect.width - pageSizeIU.y ) / 2;
439  }
440 
441  matrix.Translate( xoffset, yoffset );
442  dc->SetTransformMatrix( matrix );
443  }
444  else
445  {
446  // wxWidgets appears to have a bug when OffsetLogicalOrigin()'s yoffset changes from
447  // page to page.
448  // NB: this is a workaround, not a fix. The Y centering will be off, but this is less
449  // annoying than a blank page. See https://bugs.launchpad.net/kicad/+bug/1464773.
450  yoffset = 0;
451 
452  OffsetLogicalOrigin( xoffset, yoffset );
453  }
454 
455  dc->SetLogicalFunction( wxCOPY );
456  GRResetPenAndBrush( dc );
457 
458  COLOR4D savedBgColor = m_parent->GetDrawBgColor();
460 
461  if( cfg->m_Printing.background )
462  {
463  if( cfg->m_Printing.use_theme && theme )
464  bgColor = theme->GetColor( LAYER_SCHEMATIC_BACKGROUND );
465  }
466  else
467  {
468  bgColor = COLOR4D::WHITE;
469  }
470 
471  m_parent->SetDrawBgColor( bgColor );
472 
473  GRSFilledRect( nullptr, dc, fitRect.GetX(), fitRect.GetY(), fitRect.GetRight(),
474  fitRect.GetBottom(), 0, bgColor, bgColor );
475 
476  if( cfg->m_Printing.monochrome )
477  GRForceBlackPen( true );
478 
480  renderSettings.SetPrintDC( dc );
481 
482  if( cfg->m_Printing.use_theme && theme )
483  renderSettings.LoadColors( theme );
484 
485  // The drawing-sheet-item print code is shared between PCBNew and Eeschema, so it's easier
486  // if they just use the PCB layer.
487  renderSettings.SetLayerColor( LAYER_DRAWINGSHEET,
488  renderSettings.GetLayerColor( LAYER_SCHEMATIC_DRAWINGSHEET ) );
489 
490  if( printReference )
491  {
492  m_parent->PrintDrawingSheet( &renderSettings, aScreen, IU_PER_MILS, aScreen->GetFileName(),
493  wxEmptyString );
494  }
495 
496  renderSettings.SetIsPrinting( true );
497 
498  aScreen->Print( &renderSettings );
499 
500  m_parent->SetDrawBgColor( savedBgColor );
501 
502  GRForceBlackPen( false );
503 
504  aScreen->m_StartVisu = tmp_startvisu;
505  aScreen->m_DrawOrg = old_org;
506 }
507 
508 
510 {
511  DIALOG_PRINT_USING_PRINTER dlg( aCaller );
512 
513  return dlg.ShowModal();
514 }
void SetCurrentSheet(const SCH_SHEET_PATH &aSheet)
void OnMonochromeChecked(wxCommandEvent &event) override
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:252
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
void GRResetPenAndBrush(wxDC *DC)
Definition: gr_basic.cpp:118
const wxString & GetFileName() const
Definition: sch_screen.h:133
wxPrintOrientation GetWxOrientation() const
Definition: page_info.h:122
void GRSFilledRect(EDA_RECT *aClipBox, wxDC *aDC, int x1, int y1, int x2, int y2, int aWidth, COLOR4D aColor, COLOR4D aBgColor)
Definition: gr_basic.cpp:917
Implementation of conversion functions that require both schematic and board internal units.
This file is part of the common library.
int GetHeightMils() const
Definition: page_info.h:133
wxString GetFilename() const
Definition: json_settings.h:66
#define M_PI_2
Definition: transline.cpp:37
virtual void SetDrawBgColor(COLOR4D aColor)
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
Definition: sch_symbol.cpp:69
wxString color_theme
Color theme to use for printing.
Definition: app_settings.h:137
bool title_block
Whether or not to print title block.
Definition: app_settings.h:138
KIGFX::SCH_RENDER_SETTINGS * GetRenderSettings()
Schematic editor (Eeschema) main window.
SCH_PREVIEW_FRAME(wxPrintPreview *aPreview, wxWindow *aParent, const wxString &aTitle, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize)
bool use_theme
If false, display color theme will be used.
Definition: app_settings.h:136
bool IsCustom() const
Definition: page_info.cpp:180
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) override
void UpdateAllScreenReferences()
Update all the symbol references for this sheet path.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:128
bool monochrome
Whether or not to print in monochrome.
Definition: app_settings.h:134
wxPaperSize GetPaperId() const
Definition: page_info.h:127
EESCHEMA_SETTINGS * eeconfig() const
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
SCH_EDIT_FRAME * m_parent
wxPoint m_StartVisu
Coordinates in drawing units of the current view position (upper left corner of device)
Definition: base_screen.h:97
void PrintPage(SCH_SCREEN *aScreen)
Custom print out for printing schematics.
int InvokeDialogPrintUsingPrinter(SCH_EDIT_FRAME *aCaller)
Create and show DIALOG_PRINT_USING_PRINTER and return whatever DIALOG_PRINT_USING_PRINTER::ShowModal(...
#define NULL
void SetMsgPanel(const std::vector< MSG_PANEL_ITEM > &aList)
Clear the message panel and populates it with the contents of aList.
void GRForceBlackPen(bool flagforce)
Function GRForceBlackPen.
Definition: gr_basic.cpp:199
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:53
bool OnPrintPage(int page) override
int Mils2mm(double x)
Convert mils to mm.
Definition: base_units.h:65
Class DIALOG_PRINT_USING_PRINTER_BASE.
void OnUseColorThemeChecked(wxCommandEvent &event) override
SCHEMATIC & Schematic() const
Custom schematic print preview frame.
SCH_PRINTOUT(SCH_EDIT_FRAME *aParent, const wxString &aTitle)
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
wxPageSetupDialogData & GetPageSetupData()
void PrintDrawingSheet(const RENDER_SETTINGS *aSettings, BASE_SCREEN *aScreen, double aMils2Iu, const wxString &aFilename, const wxString &aSheetLayer=wxEmptyString)
Prints the drawing-sheet (frame and title block).
COLOR4D GetDrawBgColor() const override
COLOR_SETTINGS * GetColorSettings() const override
Returns a pointer to the active color theme settings.
bool background
Whether or not to print background color.
Definition: app_settings.h:133
SETTINGS_MANAGER * GetSettingsManager()
void OnPageSetup(wxCommandEvent &event) override
Definition: color4d.h:48
int CountSheets() const
Count the number of sheets found in "this" sheet including all of the subsheets.
Definition: sch_sheet.cpp:691
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieves a color settings object that applications can read colors from.
SCH_SHEET_LIST GetSheets() const override
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:87
COLOR4D GetColor(int aLayer) const
SCH_SHEET & Root() const
Definition: schematic.h:92
bool Show(bool show) override
see class PGM_BASE
SCH_SCREEN * LastScreen()
void Print(const RENDER_SETTINGS *aSettings)
Print all the items in the screen to aDC.
Definition: sch_screen.cpp:714
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
bool OnBeginDocument(int startPage, int endPage) override
bool IsPortrait() const
Definition: page_info.h:117
#define _(s)
Definition: 3d_actions.cpp:33
wxPoint m_DrawOrg
offsets for drawing the circuit on the screen
Definition: base_screen.h:92
#define IU_PER_MILS
Definition: plotter.cpp:137
Store schematic specific render settings.
Definition: sch_painter.h:70
void OnPrintPreview(wxCommandEvent &event) override
Color settings are a bit different than most of the settings objects in that there can be more than o...
SCH_SHEET_PATH & GetCurrentSheet() const
drawingsheet frame and titleblock
DIALOG_PRINT_USING_PRINTER(SCH_EDIT_FRAME *aParent)
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
bool HasPage(int page) override
void SetPrintDC(wxDC *aDC)
int GetWidthMils() const
Definition: page_info.h:130
wxString m_ColorTheme
Active color theme name.
Definition: app_settings.h:184
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:98