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-2020 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 <fctsys.h>
26 #include <pgm_base.h>
27 #include <gr_basic.h>
28 #include <confirm.h>
29 #include <sch_screen.h>
30 #include <sch_edit_frame.h>
31 #include <base_units.h>
32 #include <general.h>
35 #include <sch_sheet.h>
36 #include <schematic.h>
37 #include <sch_sheet_path.h>
39 #include <sch_painter.h>
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 private:
72 
73 public:
74  SCH_PRINTOUT( SCH_EDIT_FRAME* aParent, const wxString& aTitle ) :
75  wxPrintout( aTitle )
76  {
77  wxASSERT( aParent != NULL );
78  m_parent = aParent;
79  }
80 
81  bool OnPrintPage( int page ) override;
82  bool HasPage( int page ) override;
83  bool OnBeginDocument( int startPage, int endPage ) override;
84  void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo ) override;
85  void PrintPage( SCH_SCREEN* aScreen );
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 /* Open a dialog box for printer setup (printer options, page size ...)
261  */
262 void DIALOG_PRINT_USING_PRINTER::OnPageSetup( wxCommandEvent& event )
263 {
264  wxPageSetupDialog pageSetupDialog( this, &m_parent->GetPageSetupData() );
265  pageSetupDialog.ShowModal();
266 
267  m_parent->GetPageSetupData() = pageSetupDialog.GetPageSetupDialogData();
268 }
269 
270 
271 /* Open and display a previewer frame for printing
272  */
273 void DIALOG_PRINT_USING_PRINTER::OnPrintPreview( wxCommandEvent& event )
274 {
276 
277  // Pass two printout objects: for preview, and possible printing.
278  wxString title = _( "Preview" );
279  wxPrintPreview* preview = new wxPrintPreview( new SCH_PRINTOUT( m_parent, title ),
280  new SCH_PRINTOUT( m_parent, title ),
281  &m_parent->GetPageSetupData().GetPrintData() );
282 
283  preview->SetZoom( 100 );
284 
285  SCH_PREVIEW_FRAME* frame = new SCH_PREVIEW_FRAME( preview, this, title );
286  frame->SetMinSize( wxSize( 550, 350 ) );
287 
288  // on first invocation in this runtime session, set to 2/3 size of my parent,
289  // but will be changed in Show() if not first time as will position.
290  frame->SetSize( (m_parent->GetSize() * 2) / 3 );
291  frame->Center();
292 
293  // On wxGTK, set the flag wxTOPLEVEL_EX_DIALOG is mandatory, if we want
294  // close the frame using the X box in caption, when the preview frame is run
295  // from a dialog
296  frame->SetExtraStyle( frame->GetExtraStyle() | wxTOPLEVEL_EX_DIALOG );
297 
298  // We use here wxPreviewFrame_WindowModal option to make the wxPrintPreview frame
299  // modal for its caller only.
300  // another reason is the fact when closing the frame without this option,
301  // all top level frames are reenabled.
302  // With this option, only the parent is reenabled.
303  // Reenabling all top level frames should be made by the parent dialog.
304  frame->InitializeWithModality( wxPreviewFrame_WindowModal );
305 
306  frame->Raise(); // Needed on Ubuntu/Unity to display the frame
307  frame->Show( true );
308 }
309 
310 
312 {
313  if( Pgm().m_Printing )
314  {
315  DisplayError( this, _( "Previous print job not yet complete." ) );
316  return false;
317  }
318 
320 
321  int sheet_count = m_parent->Schematic().Root().CountSheets();
322 
323  wxPrintDialogData printDialogData( m_parent->GetPageSetupData().GetPrintData() );
324  printDialogData.SetMaxPage( sheet_count );
325 
326  if( sheet_count > 1 )
327  printDialogData.EnablePageNumbers( true );
328 
329  wxPrinter printer( &printDialogData );
330  SCH_PRINTOUT printout( m_parent, _( "Print Schematic" ) );
331 
332  Pgm().m_Printing = true;
333  {
334  if( !printer.Print( this, &printout, true ) )
335  {
336  if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
337  DisplayError( this, _( "An error occurred attempting to print the schematic." ) );
338  }
339  else
340  {
341  m_parent->GetPageSetupData() = printer.GetPrintDialogData().GetPrintData();
342  }
343  }
344  Pgm().m_Printing = false;
345 
346  return true;
347 }
348 
349 
351 {
352  SCH_SHEET_LIST sheetList = m_parent->Schematic().GetSheets();
353 
354  wxCHECK_MSG( page >= 1 && page <= (int)sheetList.size(), false,
355  wxT( "Cannot print invalid page number." ) );
356 
357  wxCHECK_MSG( sheetList[ page - 1].LastScreen() != NULL, false,
358  wxT( "Cannot print page with NULL screen." ) );
359 
360  wxString msg;
361  msg.Printf( _( "Print page %d" ), page );
363  m_parent->AppendMsgPanel( msg, wxEmptyString, CYAN );
364 
365  SCH_SCREEN* screen = m_parent->GetScreen();
366  SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
367  m_parent->SetCurrentSheet( sheetList[ page - 1 ] );
370  screen = m_parent->GetCurrentSheet().LastScreen();
371  PrintPage( screen );
372  m_parent->SetCurrentSheet( oldsheetpath );
375 
376  return true;
377 }
378 
379 
380 void SCH_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
381 {
382  *minPage = *selPageFrom = 1;
383  *maxPage = *selPageTo = m_parent->Schematic().Root().CountSheets();
384 }
385 
386 
387 bool SCH_PRINTOUT::HasPage( int pageNum )
388 {
389  return m_parent->Schematic().Root().CountSheets() >= pageNum;
390 }
391 
392 
393 bool SCH_PRINTOUT::OnBeginDocument( int startPage, int endPage )
394 {
395  if( !wxPrintout::OnBeginDocument( startPage, endPage ) )
396  return false;
397 
398  return true;
399 }
400 
401 
402 /*
403  * This is the real print function: print the active screen
404  */
406 {
407  wxPoint tmp_startvisu;
408  wxSize pageSizeIU; // Page size in internal units
409  wxPoint old_org;
410  wxRect fitRect;
411  wxDC* dc = GetDC();
412 
413  wxBusyCursor dummy;
414 
415  // Save current offsets and clip box.
416  tmp_startvisu = aScreen->m_StartVisu;
417  old_org = aScreen->m_DrawOrg;
418 
419  SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
422 
423  // Change scale factor and offset to print the whole page.
424  bool printReference = cfg->m_Printing.title_block;
425 
426  pageSizeIU = aScreen->GetPageSettings().GetSizeIU();
427  FitThisSizeToPaper( pageSizeIU );
428 
429  fitRect = GetLogicalPaperRect();
430 
431  // When is the actual paper size does not match the schematic page size, the drawing will
432  // not be centered on X or Y axis. Give a draw offset to center the schematic page on the
433  // paper draw area.
434  int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
435  int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
436 
437  if( dc->CanUseTransformMatrix() )
438  {
439  wxAffineMatrix2D matrix = dc->GetTransformMatrix();
440 
441  // Check for portrait/landscape mismatch:
442  if( ( fitRect.width > fitRect.height ) != ( pageSizeIU.x > pageSizeIU.y ) )
443  {
444  matrix.Rotate( M_PI_2 );
445  xoffset = ( fitRect.height - pageSizeIU.x ) / 2;
446  yoffset = ( fitRect.width - pageSizeIU.y ) / 2;
447  }
448 
449  matrix.Translate( xoffset, yoffset );
450  dc->SetTransformMatrix( matrix );
451  }
452  else
453  {
454  // wxWidgets appears to have a bug when OffsetLogicalOrigin()'s yoffset changes from
455  // page to page.
456  // NB: this is a workaround, not a fix. The Y centering will be off, but this is less
457  // annoying than a blank page. See https://bugs.launchpad.net/kicad/+bug/1464773.
458  yoffset = 0;
459 
460  OffsetLogicalOrigin( xoffset, yoffset );
461  }
462 
463  dc->SetLogicalFunction( wxCOPY );
464  GRResetPenAndBrush( dc );
465 
466  COLOR4D savedBgColor = m_parent->GetDrawBgColor();
468 
469  if( cfg->m_Printing.background )
470  {
471  if( cfg->m_Printing.use_theme && theme )
472  bgColor = theme->GetColor( LAYER_SCHEMATIC_BACKGROUND );
473  }
474  else
475  {
476  bgColor = COLOR4D::WHITE;
477  }
478 
479  m_parent->SetDrawBgColor( bgColor );
480 
481  GRSFilledRect( nullptr, dc, fitRect.GetX(), fitRect.GetY(), fitRect.GetRight(),
482  fitRect.GetBottom(), 0, bgColor, bgColor );
483 
484  if( cfg->m_Printing.monochrome )
485  GRForceBlackPen( true );
486 
488  renderSettings.SetPrintDC( dc );
489 
490  if( cfg->m_Printing.use_theme && theme )
491  renderSettings.LoadColors( theme );
492 
493  // The worksheet item print code is shared between PCBNew and EESchema, so it's easier
494  // if they just use the PCB layer.
495  renderSettings.SetLayerColor( LAYER_WORKSHEET,
496  renderSettings.GetLayerColor( LAYER_SCHEMATIC_WORKSHEET ) );
497 
498  if( printReference )
499  {
500  m_parent->PrintWorkSheet( &renderSettings, aScreen, IU_PER_MILS, aScreen->GetFileName(),
501  wxEmptyString );
502  }
503 
504  aScreen->Print( &renderSettings );
505 
506  m_parent->SetDrawBgColor( savedBgColor );
507 
508  GRForceBlackPen( false );
509 
510  aScreen->m_StartVisu = tmp_startvisu;
511  aScreen->m_DrawOrg = old_org;
512 }
513 
514 
516 {
517  DIALOG_PRINT_USING_PRINTER dlg( aCaller );
518 
519  return dlg.ShowModal();
520 }
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:239
SCH_SHEET_LIST.
void GRResetPenAndBrush(wxDC *DC)
Definition: gr_basic.cpp:122
const wxString & GetFileName() const
Definition: sch_screen.h:185
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
wxPrintOrientation GetWxOrientation() const
Function GetWxOrientation.
Definition: page_info.h:128
SCH_SHEET_LIST GetSheets() const
Builds and returns an updated schematic hierarchy TODO: can this be cached?
Definition: schematic.h:89
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:921
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:141
wxString GetFilename() const
Definition: json_settings.h:56
#define M_PI_2
Definition: transline.cpp:37
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
virtual void SetDrawBgColor(COLOR4D aColor)
void PrintWorkSheet(RENDER_SETTINGS *aSettings, BASE_SCREEN *aScreen, double aScale, const wxString &aFilename, const wxString &aSheetLayer=wxEmptyString)
Prints the page layout with the frame and the basic inscriptions.
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.
COLOR_SETTINGS * GetColorSettings() override
Returns a pointer to the active color theme settings.
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
Function IsCustom returns true if the type is Custom.
Definition: page_info.cpp:180
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) override
void UpdateAllScreenReferences()
Function UpdateAllScreenReferences updates the reference and the m_Multi parameter (part selection) f...
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:180
bool monochrome
Whether or not to print in monochrome.
Definition: app_settings.h:134
wxPaperSize GetPaperId() const
Function GetPaperId.
Definition: page_info.h:135
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:65
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 GRForceBlackPen(bool flagforce)
Function GRForceBlackPen.
Definition: gr_basic.cpp:203
PAGE_INFO describes the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:54
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
void Print(RENDER_SETTINGS *aSettings)
Print all the items in the screen to aDC.
Definition: sch_screen.cpp:740
Definition: color4d.h:58
Custom schematic print preview frame.
SCH_PRINTOUT(SCH_EDIT_FRAME *aParent, const wxString &aTitle)
SCH_SHEET_PATH.
wxPageSetupDialogData & GetPageSetupData()
virtual void ClearMsgPanel()
Clear all messages from the message panel.
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:693
COLOR_SETTINGS * GetColorSettings(const wxString &aName="user")
Retrieves a color settings object that applications can read colors from.
void AppendMsgPanel(const wxString &textUpper, const wxString &textLower, COLOR4D color, int pad=6)
Append a message to the message panel.
COLOR4D GetColor(int aLayer) const
SCH_SHEET & Root() const
Definition: schematic.h:94
bool Show(bool show) override
see class PGM_BASE
SCH_SCREEN * LastScreen()
Function LastScreen.
void SetSheetNumberAndCount()
Set the m_ScreenNumber and m_NumberOfScreens members for screens.
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
bool OnBeginDocument(int startPage, int endPage) override
bool IsPortrait() const
Definition: page_info.h:122
#define _(s)
Definition: 3d_actions.cpp:33
wxPoint m_DrawOrg
offsets for drawing the circuit on the screen
Definition: base_screen.h:60
#define IU_PER_MILS
Definition: plotter.cpp:138
SCH_RENDER_SETTINGS Stores 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
virtual COLOR4D GetDrawBgColor() const
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:138
wxString m_ColorTheme
Active color theme name.
Definition: app_settings.h:174
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99