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 <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 private:
69 
70 public:
71  SCH_PRINTOUT( SCH_EDIT_FRAME* aParent, const wxString& aTitle ) :
72  wxPrintout( aTitle )
73  {
74  wxASSERT( aParent != NULL );
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 
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 /* Open a dialog box for printer setup (printer options, page size ...)
258  */
259 void DIALOG_PRINT_USING_PRINTER::OnPageSetup( wxCommandEvent& event )
260 {
261  wxPageSetupDialog pageSetupDialog( this, &m_parent->GetPageSetupData() );
262  pageSetupDialog.ShowModal();
263 
264  m_parent->GetPageSetupData() = pageSetupDialog.GetPageSetupDialogData();
265 }
266 
267 
268 /* Open and display a previewer frame for printing
269  */
270 void DIALOG_PRINT_USING_PRINTER::OnPrintPreview( wxCommandEvent& event )
271 {
273 
274  // Pass two printout objects: for preview, and possible printing.
275  wxString title = _( "Preview" );
276  wxPrintPreview* preview = new wxPrintPreview( new SCH_PRINTOUT( m_parent, title ),
277  new SCH_PRINTOUT( m_parent, title ),
278  &m_parent->GetPageSetupData().GetPrintData() );
279 
280  preview->SetZoom( 100 );
281 
282  SCH_PREVIEW_FRAME* frame = new SCH_PREVIEW_FRAME( preview, this, title );
283  frame->SetMinSize( wxSize( 550, 350 ) );
284 
285  // on first invocation in this runtime session, set to 2/3 size of my parent,
286  // but will be changed in Show() if not first time as will position.
287  frame->SetSize( (m_parent->GetSize() * 2) / 3 );
288  frame->Center();
289 
290  // On wxGTK, set the flag wxTOPLEVEL_EX_DIALOG is mandatory, if we want
291  // close the frame using the X box in caption, when the preview frame is run
292  // from a dialog
293  frame->SetExtraStyle( frame->GetExtraStyle() | wxTOPLEVEL_EX_DIALOG );
294 
295  // We use here wxPreviewFrame_WindowModal option to make the wxPrintPreview frame
296  // modal for its caller only.
297  // another reason is the fact when closing the frame without this option,
298  // all top level frames are reenabled.
299  // With this option, only the parent is reenabled.
300  // Reenabling all top level frames should be made by the parent dialog.
301  frame->InitializeWithModality( wxPreviewFrame_WindowModal );
302 
303  frame->Raise(); // Needed on Ubuntu/Unity to display the frame
304  frame->Show( true );
305 }
306 
307 
309 {
310  if( Pgm().m_Printing )
311  {
312  DisplayError( this, _( "Previous print job not yet complete." ) );
313  return false;
314  }
315 
317 
318  int sheet_count = m_parent->Schematic().Root().CountSheets();
319 
320  wxPrintDialogData printDialogData( m_parent->GetPageSetupData().GetPrintData() );
321  printDialogData.SetMaxPage( sheet_count );
322 
323  if( sheet_count > 1 )
324  printDialogData.EnablePageNumbers( true );
325 
326  wxPrinter printer( &printDialogData );
327  SCH_PRINTOUT printout( m_parent, _( "Print Schematic" ) );
328 
329  Pgm().m_Printing = true;
330  {
331  if( !printer.Print( this, &printout, true ) )
332  {
333  if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
334  DisplayError( this, _( "An error occurred attempting to print the schematic." ) );
335  }
336  else
337  {
338  m_parent->GetPageSetupData() = printer.GetPrintDialogData().GetPrintData();
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() != NULL, false,
355  wxT( "Cannot print page with NULL screen." ) );
356 
357  wxString msg;
358  msg.Printf( _( "Print page %d" ), page );
360  m_parent->AppendMsgPanel( msg, wxEmptyString, CYAN );
361 
362  SCH_SCREEN* screen = m_parent->GetScreen();
363  SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
364  m_parent->SetCurrentSheet( sheetList[ page - 1 ] );
367  screen = m_parent->GetCurrentSheet().LastScreen();
368  PrintPage( screen );
369  m_parent->SetCurrentSheet( oldsheetpath );
372 
373  return true;
374 }
375 
376 
377 void SCH_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
378 {
379  *minPage = *selPageFrom = 1;
380  *maxPage = *selPageTo = m_parent->Schematic().Root().CountSheets();
381 }
382 
383 
384 bool SCH_PRINTOUT::HasPage( int pageNum )
385 {
386  return m_parent->Schematic().Root().CountSheets() >= pageNum;
387 }
388 
389 
390 bool SCH_PRINTOUT::OnBeginDocument( int startPage, int endPage )
391 {
392  if( !wxPrintout::OnBeginDocument( startPage, endPage ) )
393  return false;
394 
395  return true;
396 }
397 
398 
399 /*
400  * This is the real print function: print the active screen
401  */
403 {
404  wxPoint tmp_startvisu;
405  wxSize pageSizeIU; // Page size in internal units
406  wxPoint old_org;
407  wxRect fitRect;
408  wxDC* dc = GetDC();
409 
410  wxBusyCursor dummy;
411 
412  // Save current offsets and clip box.
413  tmp_startvisu = aScreen->m_StartVisu;
414  old_org = aScreen->m_DrawOrg;
415 
416  SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
419 
420  // Change scale factor and offset to print the whole page.
421  bool printReference = cfg->m_Printing.title_block;
422 
423  pageSizeIU = aScreen->GetPageSettings().GetSizeIU();
424  FitThisSizeToPaper( pageSizeIU );
425 
426  fitRect = GetLogicalPaperRect();
427 
428  // When is the actual paper size does not match the schematic page size, the drawing will
429  // not be centered on X or Y axis. Give a draw offset to center the schematic page on the
430  // paper draw area.
431  int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
432  int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
433 
434  if( dc->CanUseTransformMatrix() )
435  {
436  wxAffineMatrix2D matrix = dc->GetTransformMatrix();
437 
438  // Check for portrait/landscape mismatch:
439  if( ( fitRect.width > fitRect.height ) != ( pageSizeIU.x > pageSizeIU.y ) )
440  {
441  matrix.Rotate( M_PI_2 );
442  xoffset = ( fitRect.height - pageSizeIU.x ) / 2;
443  yoffset = ( fitRect.width - pageSizeIU.y ) / 2;
444  }
445 
446  matrix.Translate( xoffset, yoffset );
447  dc->SetTransformMatrix( matrix );
448  }
449  else
450  {
451  // wxWidgets appears to have a bug when OffsetLogicalOrigin()'s yoffset changes from
452  // page to page.
453  // NB: this is a workaround, not a fix. The Y centering will be off, but this is less
454  // annoying than a blank page. See https://bugs.launchpad.net/kicad/+bug/1464773.
455  yoffset = 0;
456 
457  OffsetLogicalOrigin( xoffset, yoffset );
458  }
459 
460  dc->SetLogicalFunction( wxCOPY );
461  GRResetPenAndBrush( dc );
462 
463  COLOR4D savedBgColor = m_parent->GetDrawBgColor();
465 
466  if( cfg->m_Printing.background )
467  {
468  if( cfg->m_Printing.use_theme && theme )
469  bgColor = theme->GetColor( LAYER_SCHEMATIC_BACKGROUND );
470  }
471  else
472  {
473  bgColor = COLOR4D::WHITE;
474  }
475 
476  m_parent->SetDrawBgColor( bgColor );
477 
478  GRSFilledRect( nullptr, dc, fitRect.GetX(), fitRect.GetY(), fitRect.GetRight(),
479  fitRect.GetBottom(), 0, bgColor, bgColor );
480 
481  if( cfg->m_Printing.monochrome )
482  GRForceBlackPen( true );
483 
485  renderSettings.SetPrintDC( dc );
486 
487  if( cfg->m_Printing.use_theme && theme )
488  renderSettings.LoadColors( theme );
489 
490  // The worksheet item print code is shared between PCBNew and EESchema, so it's easier
491  // if they just use the PCB layer.
492  renderSettings.SetLayerColor( LAYER_WORKSHEET,
493  renderSettings.GetLayerColor( LAYER_SCHEMATIC_WORKSHEET ) );
494 
495  if( printReference )
496  {
497  m_parent->PrintWorkSheet( &renderSettings, aScreen, IU_PER_MILS, aScreen->GetFileName(),
498  wxEmptyString );
499  }
500 
501  aScreen->Print( &renderSettings );
502 
503  m_parent->SetDrawBgColor( savedBgColor );
504 
505  GRForceBlackPen( false );
506 
507  aScreen->m_StartVisu = tmp_startvisu;
508  aScreen->m_DrawOrg = old_org;
509 }
510 
511 
513 {
514  DIALOG_PRINT_USING_PRINTER dlg( aCaller );
515 
516  return dlg.ShowModal();
517 }
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:240
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:186
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:97
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:141
wxString GetFilename() const
Definition: json_settings.h:64
#define M_PI_2
Definition: transline.cpp:37
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()
Update all the symbol references for this sheet path.
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:102
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:181
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:92
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:199
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:738
Definition: color4d.h:59
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()
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:49
int CountSheets() const
Count the number of sheets found in "this" sheet including all of the subsheets.
Definition: sch_sheet.cpp:693
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.
COLOR4D GetColor(int aLayer) const
SCH_SHEET & Root() const
Definition: schematic.h:102
bool Show(bool show) override
see class PGM_BASE
SCH_SCREEN * 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:87
#define IU_PER_MILS
Definition: plotter.cpp:137
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:176
void AppendMsgPanel(const wxString &aTextUpper, const wxString &aTextLower, int aPadding=6)
Append a message to the message panel.
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:100