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 #include <wx/print.h>
38 #include <wx/printdlg.h>
39 
40 
42 {
43 public:
45  ~DIALOG_PRINT_USING_PRINTER() override;
46 
47 protected:
48  void OnMonochromeChecked( wxCommandEvent& event ) override;
49  void OnUseColorThemeChecked( wxCommandEvent& event ) override;
50 
51 private:
52  bool TransferDataToWindow() override;
53  bool TransferDataFromWindow() override;
54 
55  void OnPageSetup( wxCommandEvent& event ) override;
56  void OnPrintPreview( wxCommandEvent& event ) override;
57 
58  void SavePrintOptions();
59 
61 };
62 
63 
64 
68 class SCH_PRINTOUT : public wxPrintout
69 {
70 public:
71  SCH_PRINTOUT( SCH_EDIT_FRAME* aParent, const wxString& aTitle ) :
72  wxPrintout( aTitle )
73  {
74  wxASSERT( aParent != nullptr );
75  m_parent = aParent;
76  }
77 
78  bool OnPrintPage( int page ) override;
79  bool HasPage( int page ) override;
80  bool OnBeginDocument( int startPage, int endPage ) override;
81  void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo ) override;
82  void PrintPage( SCH_SCREEN* aScreen );
83 
84 private:
86 };
87 
88 
93 class SCH_PREVIEW_FRAME : public wxPreviewFrame
94 {
95 public:
96  SCH_PREVIEW_FRAME( wxPrintPreview* aPreview, wxWindow* aParent,
97  const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
98  const wxSize& aSize = wxDefaultSize ) :
99  wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
100  {
101  }
102 
103  bool Show( bool show ) override
104  {
105  bool ret;
106 
107  // Show or hide the window. If hiding, save current position and size.
108  // If showing, use previous position and size.
109  if( show )
110  {
111  ret = wxPreviewFrame::Show( show );
112 
113  if( s_size.x != 0 && s_size.y != 0 )
114  SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
115  }
116  else
117  {
118  // Save the dialog's position & size before hiding
119  s_size = GetSize();
120  s_pos = GetPosition();
121 
122  ret = wxPreviewFrame::Show( show );
123  }
124 
125  return ret;
126  }
127 
128 private:
129  static wxPoint s_pos;
130  static wxSize s_size;
131 };
132 
133 
136 
137 
140  m_parent( aParent )
141 {
142  wxASSERT( aParent );
143 
144  // We use a sdbSizer to get platform-dependent ordering of the action buttons, but
145  // that requires us to correct the button labels here.
146  m_sdbSizer1OK->SetLabel( _( "Print" ) );
147  m_sdbSizer1Apply->SetLabel( _( "Preview" ) );
148  m_sdbSizer1Cancel->SetLabel( _( "Close" ) );
149  m_sdbSizer1->Layout();
150 
151 #ifdef __WXMAC__
152  // Problems with modal on wx-2.9 - Anyway preview is standard for OSX
153  m_sdbSizer1Apply->Hide();
154 #endif
155 
156  m_sdbSizer1OK->SetDefault(); // on linux, this is inadequate to determine
157  // what ENTER does. Must also SetFocus().
158  m_sdbSizer1OK->SetFocus();
159 
161 }
162 
163 
165 {
167 }
168 
169 
171 {
173 
174  m_checkReference->SetValue( cfg->m_Printing.title_block );
175  m_checkMonochrome->SetValue( cfg->m_Printing.monochrome );
176  m_checkBackgroundColor->SetValue( cfg->m_Printing.background );
177  m_checkUseColorTheme->SetValue( cfg->m_Printing.use_theme );
178 
179  m_colorTheme->Clear();
180 
181  int width = 0;
182  int height = 0;
183  int minwidth = width;
184 
185  wxString target = cfg->m_Printing.use_theme ? cfg->m_Printing.color_theme : cfg->m_ColorTheme;
186 
187  for( COLOR_SETTINGS* settings : Pgm().GetSettingsManager().GetColorSettingsList() )
188  {
189  int pos = m_colorTheme->Append( settings->GetName(), static_cast<void*>( settings ) );
190 
191  if( settings->GetFilename() == target )
192  m_colorTheme->SetSelection( pos );
193 
194  m_colorTheme->GetTextExtent( settings->GetName(), &width, &height );
195  minwidth = std::max( minwidth, width );
196  }
197 
198  m_colorTheme->SetMinSize( wxSize( minwidth + 50, -1 ) );
199 
200  m_colorTheme->Enable( cfg->m_Printing.use_theme );
201 
202  // Initialize page specific print setup dialog settings.
203  const PAGE_INFO& pageInfo = m_parent->GetScreen()->GetPageSettings();
204  wxPageSetupDialogData& pageSetupDialogData = m_parent->GetPageSetupData();
205 
206  pageSetupDialogData.SetPaperId( pageInfo.GetPaperId() );
207 
208  if( pageInfo.IsCustom() )
209  {
210  if( pageInfo.IsPortrait() )
211  pageSetupDialogData.SetPaperSize( wxSize( Mils2mm( pageInfo.GetWidthMils() ),
212  Mils2mm( pageInfo.GetHeightMils() ) ) );
213  else
214  pageSetupDialogData.SetPaperSize( wxSize( Mils2mm( pageInfo.GetHeightMils() ),
215  Mils2mm( pageInfo.GetWidthMils() ) ) );
216  }
217 
218  pageSetupDialogData.GetPrintData().SetOrientation( pageInfo.GetWxOrientation() );
219 
220  Layout();
221 
222  return true;
223 }
224 
225 
227 {
228  m_colorTheme->Enable( m_checkUseColorTheme->GetValue() );
229 }
230 
231 
233 {
234  m_checkBackgroundColor->Enable( !m_checkMonochrome->GetValue() );
235 
236  if( m_checkMonochrome->GetValue() )
237  m_checkBackgroundColor->SetValue( false );
238  else
240 }
241 
242 
244 {
246 
247  cfg->m_Printing.monochrome = m_checkMonochrome->IsChecked();
248  cfg->m_Printing.title_block = m_checkReference->IsChecked();
249  cfg->m_Printing.background = m_checkBackgroundColor->IsChecked();
250  cfg->m_Printing.use_theme = m_checkUseColorTheme->IsChecked();
251 
252  COLOR_SETTINGS* theme = static_cast<COLOR_SETTINGS*>(
253  m_colorTheme->GetClientData( m_colorTheme->GetSelection() ) );
254 
255  if( theme && m_checkUseColorTheme->IsChecked() )
256  cfg->m_Printing.color_theme = theme->GetFilename();
257 }
258 
259 
260 void DIALOG_PRINT_USING_PRINTER::OnPageSetup( wxCommandEvent& event )
261 {
262  wxPageSetupDialog pageSetupDialog( this, &m_parent->GetPageSetupData() );
263  pageSetupDialog.ShowModal();
264 
265  m_parent->GetPageSetupData() = pageSetupDialog.GetPageSetupDialogData();
266 }
267 
268 
269 void DIALOG_PRINT_USING_PRINTER::OnPrintPreview( wxCommandEvent& event )
270 {
272 
273  // Pass two printout objects: for preview, and possible printing.
274  wxString title = _( "Preview" );
275  wxPrintPreview* preview = new wxPrintPreview( new SCH_PRINTOUT( m_parent, title ),
276  new SCH_PRINTOUT( m_parent, title ),
277  &m_parent->GetPageSetupData().GetPrintData() );
278 
279  preview->SetZoom( 100 );
280 
281  SCH_PREVIEW_FRAME* frame = new SCH_PREVIEW_FRAME( preview, this, title );
282  frame->SetMinSize( wxSize( 550, 350 ) );
283 
284  // on first invocation in this runtime session, set to 2/3 size of my parent,
285  // but will be changed in Show() if not first time as will position.
286  frame->SetSize( (m_parent->GetSize() * 2) / 3 );
287  frame->Center();
288 
289  // On wxGTK, set the flag wxTOPLEVEL_EX_DIALOG is mandatory, if we want
290  // close the frame using the X box in caption, when the preview frame is run
291  // from a dialog
292  frame->SetExtraStyle( frame->GetExtraStyle() | wxTOPLEVEL_EX_DIALOG );
293 
294  // We use here wxPreviewFrame_WindowModal option to make the wxPrintPreview frame
295  // modal for its caller only.
296  // another reason is the fact when closing the frame without this option,
297  // all top level frames are reenabled.
298  // With this option, only the parent is reenabled.
299  // Reenabling all top level frames should be made by the parent dialog.
300  frame->InitializeWithModality( wxPreviewFrame_WindowModal );
301 
302  frame->Raise(); // Needed on Ubuntu/Unity to display the frame
303  frame->Show( true );
304 }
305 
306 
308 {
309  if( Pgm().m_Printing )
310  {
311  DisplayError( this, _( "Previous print job not yet complete." ) );
312  return false;
313  }
314 
316 
317  int sheet_count = m_parent->Schematic().Root().CountSheets();
318 
319  wxPrintDialogData printDialogData( m_parent->GetPageSetupData().GetPrintData() );
320  printDialogData.SetMaxPage( sheet_count );
321 
322  if( sheet_count > 1 )
323  printDialogData.EnablePageNumbers( true );
324 
325  wxPrinter printer( &printDialogData );
326  SCH_PRINTOUT printout( m_parent, _( "Print Schematic" ) );
327 
328  Pgm().m_Printing = true;
329  {
330  if( !printer.Print( this, &printout, true ) )
331  {
332  if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
333  DisplayError( this, _( "An error occurred attempting to print the schematic." ) );
334  }
335  else
336  {
337  m_parent->GetPageSetupData() = printer.GetPrintDialogData().GetPrintData();
338  }
339  }
340 
341  Pgm().m_Printing = false;
342 
343  return true;
344 }
345 
346 
348 {
349  SCH_SHEET_LIST sheetList = m_parent->Schematic().GetSheets();
350 
351  wxCHECK_MSG( page >= 1 && page <= (int)sheetList.size(), false,
352  wxT( "Cannot print invalid page number." ) );
353 
354  wxCHECK_MSG( sheetList[ page - 1].LastScreen() != nullptr, false,
355  wxT( "Cannot print page with NULL screen." ) );
356 
357  wxString msg;
358  msg.Printf( _( "Print page %d" ), page );
359  m_parent->SetMsgPanel( msg, wxEmptyString );
360 
361  SCH_SCREEN* screen = m_parent->GetScreen();
362  SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
363  m_parent->SetCurrentSheet( sheetList[ page - 1 ] );
366  screen = m_parent->GetCurrentSheet().LastScreen();
367  PrintPage( screen );
368  m_parent->SetCurrentSheet( oldsheetpath );
371 
372  return true;
373 }
374 
375 
376 void SCH_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
377 {
378  *minPage = *selPageFrom = 1;
379  *maxPage = *selPageTo = m_parent->Schematic().Root().CountSheets();
380 }
381 
382 
383 bool SCH_PRINTOUT::HasPage( int pageNum )
384 {
385  return m_parent->Schematic().Root().CountSheets() >= pageNum;
386 }
387 
388 
389 bool SCH_PRINTOUT::OnBeginDocument( int startPage, int endPage )
390 {
391  if( !wxPrintout::OnBeginDocument( startPage, endPage ) )
392  return false;
393 
394  return true;
395 }
396 
397 
398 /*
399  * This is the real print function: print the active screen
400  */
402 {
403  wxPoint tmp_startvisu;
404  wxSize pageSizeIU; // Page size in internal units
405  wxPoint old_org;
406  wxRect fitRect;
407  wxDC* dc = GetDC();
408 
409  wxBusyCursor dummy;
410 
411  // Save current offsets and clip box.
412  tmp_startvisu = aScreen->m_StartVisu;
413  old_org = aScreen->m_DrawOrg;
414 
415  SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
418 
419  // Change scale factor and offset to print the whole page.
420  bool printReference = cfg->m_Printing.title_block;
421 
422  pageSizeIU = aScreen->GetPageSettings().GetSizeIU();
423  FitThisSizeToPaper( pageSizeIU );
424 
425  fitRect = GetLogicalPaperRect();
426 
427  // When is the actual paper size does not match the schematic page size, the drawing will
428  // not be centered on X or Y axis. Give a draw offset to center the schematic page on the
429  // paper draw area.
430  int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
431  int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
432 
433  if( dc->CanUseTransformMatrix() )
434  {
435  wxAffineMatrix2D matrix = dc->GetTransformMatrix();
436 
437  // Check for portrait/landscape mismatch:
438  if( ( fitRect.width > fitRect.height ) != ( pageSizeIU.x > pageSizeIU.y ) )
439  {
440  matrix.Rotate( M_PI_2 );
441  xoffset = ( fitRect.height - pageSizeIU.x ) / 2;
442  yoffset = ( fitRect.width - pageSizeIU.y ) / 2;
443  }
444 
445  matrix.Translate( xoffset, yoffset );
446  dc->SetTransformMatrix( matrix );
447  }
448  else
449  {
450  // wxWidgets appears to have a bug when OffsetLogicalOrigin()'s yoffset changes from
451  // page to page.
452  // NB: this is a workaround, not a fix. The Y centering will be off, but this is less
453  // annoying than a blank page. See https://bugs.launchpad.net/kicad/+bug/1464773.
454  yoffset = 0;
455 
456  OffsetLogicalOrigin( xoffset, yoffset );
457  }
458 
459  dc->SetLogicalFunction( wxCOPY );
460  GRResetPenAndBrush( dc );
461 
462  COLOR4D savedBgColor = m_parent->GetDrawBgColor();
464 
465  if( cfg->m_Printing.background )
466  {
467  if( cfg->m_Printing.use_theme && theme )
468  bgColor = theme->GetColor( LAYER_SCHEMATIC_BACKGROUND );
469  }
470  else
471  {
472  bgColor = COLOR4D::WHITE;
473  }
474 
475  m_parent->SetDrawBgColor( bgColor );
476 
477  GRSFilledRect( nullptr, dc, fitRect.GetX(), fitRect.GetY(), fitRect.GetRight(),
478  fitRect.GetBottom(), 0, bgColor, bgColor );
479 
480  if( cfg->m_Printing.monochrome )
481  GRForceBlackPen( true );
482 
484  renderSettings.SetPrintDC( dc );
485 
486  if( cfg->m_Printing.use_theme && theme )
487  renderSettings.LoadColors( theme );
488 
489  // The drawing-sheet-item print code is shared between PCBNew and Eeschema, so it's easier
490  // if they just use the PCB layer.
491  renderSettings.SetLayerColor( LAYER_DRAWINGSHEET,
492  renderSettings.GetLayerColor( LAYER_SCHEMATIC_DRAWINGSHEET ) );
493 
494  if( printReference )
495  {
496  m_parent->PrintDrawingSheet( &renderSettings, aScreen, IU_PER_MILS, aScreen->GetFileName(),
497  wxEmptyString );
498  }
499 
500  renderSettings.SetIsPrinting( true );
501 
502  aScreen->Print( &renderSettings );
503 
504  m_parent->SetDrawBgColor( savedBgColor );
505 
506  GRForceBlackPen( false );
507 
508  aScreen->m_StartVisu = tmp_startvisu;
509  aScreen->m_DrawOrg = old_org;
510 }
511 
512 
514 {
515  DIALOG_PRINT_USING_PRINTER dlg( aCaller );
516 
517  return dlg.ShowModal();
518 }
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:119
const wxString & GetFileName() const
Definition: sch_screen.h:137
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:923
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:71
#define M_PI_2
Definition: transline.cpp:40
virtual void SetDrawBgColor(COLOR4D aColor)
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:124
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
static LIB_SYMBOL * dummy()
Used to draw a dummy shape when a LIB_SYMBOL is not found in library.
Definition: sch_symbol.cpp:71
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:93
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(...
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)
Definition: gr_basic.cpp:203
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
Class DIALOG_PRINT_USING_PRINTER_BASE.
void OnUseColorThemeChecked(wxCommandEvent &event) override
SCHEMATIC & Schematic() const
#define _(s)
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:692
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:729
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
int Mils2mm(double x)
Convert mils to mm.
Definition: base_units.cpp:62
wxPoint m_DrawOrg
offsets for drawing the circuit on the screen
Definition: base_screen.h:88
#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:103