KiCad PCB EDA Suite
dialogs_for_printing.cpp
Go to the documentation of this file.
1 
5 /*
6  * This program source code file is part of KiCad, a free EDA CAD application.
7  *
8  * Copyright (C) 2013 CERN
9  * Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
10  *
11  * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, you may find one here:
25  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
26  * or you may search the http://www.gnu.org website for the version 2 license,
27  * or you may write to the Free Software Foundation, Inc.,
28  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29  */
30 
31 #include <base_units.h>
32 #include <gr_basic.h>
37 
39 #include "pl_editor_frame.h"
40 #include "pl_editor_id.h"
41 
42 #include <wx/msgdlg.h>
43 #include <wx/print.h>
44 
48 class PLEDITOR_PRINTOUT : public wxPrintout
49 {
50 private:
52 
53 public:
54  PLEDITOR_PRINTOUT( PL_EDITOR_FRAME* aParent, const wxString& aTitle ) :
55  wxPrintout( aTitle )
56  {
57  wxASSERT( aParent != nullptr );
58  m_parent = aParent;
59  }
60 
61  bool OnPrintPage( int aPageNum ) override;
62  bool HasPage( int aPageNum ) override { return ( aPageNum <= 2 ); }
63  void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo ) override;
64  void PrintPage( int aPageNum );
65 };
66 
67 
71 class PLEDITOR_PREVIEW_FRAME : public wxPreviewFrame
72 {
74 
75 public:
76  PLEDITOR_PREVIEW_FRAME( wxPrintPreview* aPreview, PL_EDITOR_FRAME* aParent,
77  const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
78  const wxSize& aSize = wxDefaultSize ) :
79  wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
80  {
81  m_parent = aParent;
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  bool centre = false;
93 
94  if( s_size.x == 0 || s_size.y == 0 )
95  {
96  s_size = ( m_parent->GetSize() * 3 ) / 4;
97  s_pos = wxDefaultPosition;
98  centre = true;
99  }
100 
101  SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
102 
103  if( centre )
104  Center();
105 
106  ret = wxPreviewFrame::Show( show );
107  }
108  else
109  {
110  // Save the dialog's position & size before hiding
111  s_size = GetSize();
112  s_pos = GetPosition();
113 
114  ret = wxPreviewFrame::Show( show );
115  }
116 
117  return ret;
118  }
119 
120 private:
121  static wxPoint s_pos;
122  static wxSize s_size;
123 
124  DECLARE_CLASS( PLEDITOR_PREVIEW_FRAME )
125  DECLARE_EVENT_TABLE()
126  DECLARE_NO_COPY_CLASS( PLEDITOR_PREVIEW_FRAME )
127 };
128 
129 
132 
133 
134 IMPLEMENT_CLASS( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
135 
136 
137 BEGIN_EVENT_TABLE( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
138  EVT_CLOSE( PLEDITOR_PREVIEW_FRAME::OnCloseWindow )
139 END_EVENT_TABLE()
140 
141 
142 bool PLEDITOR_PRINTOUT::OnPrintPage( int aPageNum )
143 {
144  PrintPage( aPageNum );
145  return true;
146 }
147 
148 
149 void PLEDITOR_PRINTOUT::GetPageInfo( int* minPage, int* maxPage,
150  int* selPageFrom, int* selPageTo )
151 {
152  *minPage = *selPageFrom = 1;
153  *maxPage = *selPageTo = 2;
154 }
155 
156 
157 void PLEDITOR_PRINTOUT::PrintPage( int aPageNum )
158 {
159  wxPoint tmp_startvisu;
160  wxSize pageSizeIU; // Page size in internal units
161  wxPoint old_org;
162  wxRect fitRect;
163  wxDC* dc = GetDC();
164  BASE_SCREEN* screen = m_parent->GetScreen();
165 
166  // Save current offsets and clip box.
167  tmp_startvisu = screen->m_StartVisu;
168  old_org = screen->m_DrawOrg;
169 
170  // Change scale factor and offset to print the whole page.
171 
172  pageSizeIU = m_parent->GetPageSettings().GetSizeIU();
173  FitThisSizeToPaper( pageSizeIU );
174  fitRect = GetLogicalPaperRect();
175 
176  int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
177  int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
178 
179  OffsetLogicalOrigin( xoffset, yoffset );
180 
181  GRResetPenAndBrush( dc );
182  GRForceBlackPen( true );
183 
184  COLOR4D bg_color = m_parent->GetDrawBgColor();
186 
187  screen->SetVirtualPageNumber( aPageNum );
188 
189  KIGFX::DS_RENDER_SETTINGS renderSettings;
190  renderSettings.SetDefaultPenWidth( 1 );
191  renderSettings.SetLayerColor( LAYER_DRAWINGSHEET, COLOR4D( RED ) );
192  renderSettings.SetPrintDC( dc );
193 
194  // Ensure the scaling factor (used only in printing) of bitmaps is up to date
196 
197  for( DS_DATA_ITEM* dataItem : model.GetItems() )
198  {
199  if( dataItem->GetType() == DS_DATA_ITEM::DS_BITMAP )
200  {
201  BITMAP_BASE* bitmap = static_cast<DS_DATA_ITEM_BITMAP*>( dataItem )->m_ImageBitmap;
202  bitmap->SetPixelSizeIu( IU_PER_MILS * 1000 / bitmap->GetPPI() );
203  }
204  }
205 
206  m_parent->PrintDrawingSheet( &renderSettings, screen, IU_PER_MILS, wxEmptyString );
207 
208  m_parent->SetDrawBgColor( bg_color );
209 
210  GRForceBlackPen( false );
211 
212  screen->m_StartVisu = tmp_startvisu;
213  screen->m_DrawOrg = old_org;
214 
215  // PrintDrawingSheet clears the current display list when calling BuildDrawItemsList()
216  // So rebuild and redraw it.
218 }
219 
220 
221 int InvokeDialogPrint( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData,
222  wxPageSetupDialogData* aPageSetupData )
223 {
224  int pageCount = 2;
225 
226  wxPrintDialogData printDialogData( *aPrintData );
227  printDialogData.SetMaxPage( pageCount );
228 
229  if( pageCount > 1 )
230  printDialogData.EnablePageNumbers( true );
231 
232  wxPrinter printer( &printDialogData );
233  PLEDITOR_PRINTOUT printout( aCaller, _( "Print Drawing Sheet" ) );
234 
235  if( !printer.Print( aCaller, &printout, true ) )
236  {
237  if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
238  wxMessageBox( _( "An error occurred attempting to print the drawing sheet." ),
239  _( "Printing" ), wxOK );
240  return 0;
241  }
242 
243  *aPageSetupData = printer.GetPrintDialogData().GetPrintData();
244 
245  return 1;
246 }
247 
248 
249 int InvokeDialogPrintPreview( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData )
250 {
251  // Pass two printout objects: for preview, and possible printing.
252  wxString title = _( "Preview" );
253  wxPrintPreview* preview = new wxPrintPreview( new PLEDITOR_PRINTOUT( aCaller, title ),
254  new PLEDITOR_PRINTOUT( aCaller, title ),
255  aPrintData );
256 
257  preview->SetZoom( 70 );
258 
259  PLEDITOR_PREVIEW_FRAME* frame = new PLEDITOR_PREVIEW_FRAME( preview, aCaller, title );
260 
261  frame->Initialize();
262  frame->Show( true );
263 
264  return 1;
265 }
266 
void GRResetPenAndBrush(wxDC *DC)
Definition: gr_basic.cpp:111
Handle the graphic items list to draw/plot the frame and title block.
Definition: ds_data_model.h:38
void SetVirtualPageNumber(int aPageNumber)
Definition: base_screen.h:76
PL_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
const PAGE_INFO & GetPageSettings() const override
Implementation of conversion functions that require both schematic and board internal units.
bool OnPrintPage(int aPageNum) override
void PrintPage(int aPageNum)
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) override
The main window used in the drawing sheet editor.
int InvokeDialogPrint(PL_EDITOR_FRAME *aCaller, wxPrintData *aPrintData, wxPageSetupDialogData *aPageSetupData)
Create and show a print dialog returns 1 if OK, 0 , there is a problem.
int InvokeDialogPrintPreview(PL_EDITOR_FRAME *aCaller, wxPrintData *aPrintData)
Create and show a print preview dialog returns 1 if OK, 0 , there is a problem.
std::vector< DS_DATA_ITEM * > & GetItems()
void DisplayDrawingSheet()
Build and update the list of WS_DRAW_ITEM_xxx showing the frame layout.
bool Show(bool show) override
Store page-layout-specific render settings.
Definition: ds_painter.h:46
This class handle bitmap images in KiCad.
Definition: bitmap_base.h:51
wxPoint m_StartVisu
Coordinates in drawing units of the current view position (upper left corner of device)
Definition: base_screen.h:93
static DS_DATA_MODEL & GetTheInstance()
static function: returns the instance of DS_DATA_MODEL used in the application
void GRForceBlackPen(bool flagforce)
Definition: gr_basic.cpp:190
Handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:40
Custom print preview frame.
bool HasPage(int aPageNum) override
#define _(s)
void SetLayerColor(int aLayer, const COLOR4D &aColor)
Change the color used to draw a layer.
PLEDITOR_PRINTOUT(PL_EDITOR_FRAME *aParent, const wxString &aTitle)
drawingsheet frame and titleblock
Definition: layer_ids.h:213
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).
Definition: color4d.h:59
void SetPixelSizeIu(double aPixSize)
Definition: bitmap_base.h:68
Definition: color4d.h:48
PLEDITOR_PREVIEW_FRAME(wxPrintPreview *aPreview, PL_EDITOR_FRAME *aParent, const wxString &aTitle, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize)
PL_EDITOR_FRAME * m_parent
Drawing sheet structure type definitions.
Definition: ds_data_item.h:95
wxPoint m_DrawOrg
offsets for drawing the circuit on the screen
Definition: base_screen.h:88
#define IU_PER_MILS
Definition: plotter.cpp:136
Custom print out for printing schematics.
virtual COLOR4D GetDrawBgColor() const
void SetDefaultPenWidth(int aWidth)
void SetPrintDC(wxDC *aDC)
virtual BASE_SCREEN * GetScreen() const
Return a pointer to a BASE_SCREEN or one of its derivatives.
virtual void SetDrawBgColor(const COLOR4D &aColor)
int GetPPI() const
Definition: bitmap_base.h:135
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:103