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  * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 #include <fctsys.h>
30 #include <gr_basic.h>
31 #include <class_drawpanel.h>
32 #include <base_units.h>
33 
34 #include <pl_editor_frame.h>
35 #include <pl_editor_id.h>
36 #include <dialog_helpers.h>
39 #include <dialog_page_settings.h>
41 
45 class PLEDITOR_PRINTOUT : public wxPrintout
46 {
47 private:
49 
50 public:
51  PLEDITOR_PRINTOUT( PL_EDITOR_FRAME* aParent, const wxString& aTitle ) :
52  wxPrintout( aTitle )
53  {
54  wxASSERT( aParent != NULL );
55  m_parent = aParent;
56  }
57 
58  bool OnPrintPage( int aPageNum ) override;
59  bool HasPage( int aPageNum ) override { return ( aPageNum <= 2 ); }
60  void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo ) override;
61  void DrawPage( int aPageNum );
62 };
63 
67 class PLEDITOR_PREVIEW_FRAME : public wxPreviewFrame
68 {
70 
71 public:
72  PLEDITOR_PREVIEW_FRAME( wxPrintPreview* aPreview, PL_EDITOR_FRAME* aParent,
73  const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
74  const wxSize& aSize = wxDefaultSize ) :
75  wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
76  {
77  m_parent = aParent;
78  }
79 
80  bool Show( bool show ) override
81  {
82  bool ret;
83 
84  // Show or hide the window. If hiding, save current position and size.
85  // If showing, use previous position and size.
86  if( show )
87  {
88  bool centre = false;
89  if( s_size.x == 0 || s_size.y == 0 )
90  {
91  s_size = (m_parent->GetSize() * 3) / 4;
92  s_pos = wxDefaultPosition;
93  centre = true;
94  }
95 
96  SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
97 
98  if( centre )
99  Center();
100 
101  ret = wxPreviewFrame::Show( show );
102  }
103  else
104  {
105  // Save the dialog's position & size before hiding
106  s_size = GetSize();
107  s_pos = GetPosition();
108 
109  ret = wxPreviewFrame::Show( show );
110  }
111  return ret;
112  }
113 
114 private:
115  static wxPoint s_pos;
116  static wxSize s_size;
117 
118  DECLARE_CLASS( PLEDITOR_PREVIEW_FRAME )
119  DECLARE_EVENT_TABLE()
120  DECLARE_NO_COPY_CLASS( PLEDITOR_PREVIEW_FRAME )
121 };
122 
123 wxPoint PLEDITOR_PREVIEW_FRAME::s_pos;
124 wxSize PLEDITOR_PREVIEW_FRAME::s_size;
125 
126 IMPLEMENT_CLASS( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
127 
128 BEGIN_EVENT_TABLE( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
129  EVT_CLOSE( PLEDITOR_PREVIEW_FRAME::OnCloseWindow )
130 END_EVENT_TABLE()
131 
132 
133 bool PLEDITOR_PRINTOUT::OnPrintPage( int aPageNum )
134 {
135  DrawPage( aPageNum );
136  return true;
137 }
138 
139 
140 void PLEDITOR_PRINTOUT::GetPageInfo( int* minPage, int* maxPage,
141  int* selPageFrom, int* selPageTo )
142 {
143  *minPage = *selPageFrom = 1;
144  *maxPage = *selPageTo = 2;
145 }
146 
147 /*
148  * This is the real print function: print the active screen
149  */
150 void PLEDITOR_PRINTOUT::DrawPage( int aPageNum )
151 {
152  int oldZoom;
153  wxPoint tmp_startvisu;
154  wxSize pageSizeIU; // Page size in internal units
155  wxPoint old_org;
156  EDA_RECT oldClipBox;
157  wxRect fitRect;
158  wxDC* dc = GetDC();
159  EDA_DRAW_PANEL* panel = m_parent->GetCanvas();
160  PL_EDITOR_SCREEN* screen = m_parent->GetScreen();
161 
162  // Save current scale factor, offsets, and clip box.
163  tmp_startvisu = screen->m_StartVisu;
164  oldZoom = screen->GetZoom();
165  old_org = screen->m_DrawOrg;
166 
167  oldClipBox = *panel->GetClipBox();
168 
169  // Change clip box to print the whole page.
170  #define MAX_VALUE (INT_MAX/2) // MAX_VALUE is the max we can use in an integer
171  // and that allows calculations without overflow
172  panel->SetClipBox( EDA_RECT( wxPoint( 0, 0 ), wxSize( MAX_VALUE, MAX_VALUE ) ) );
173 
174  // Change scale factor and offset to print the whole page.
175 
176  pageSizeIU = m_parent->GetPageSettings().GetSizeIU();
177  FitThisSizeToPaper( pageSizeIU );
178  fitRect = GetLogicalPaperRect();
179 
180  int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
181  int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
182 
183  OffsetLogicalOrigin( xoffset, yoffset );
184 
185  GRResetPenAndBrush( dc );
186  GRForceBlackPen( true );
187  screen->m_IsPrinting = true;
188 
189  COLOR4D bg_color = m_parent->GetDrawBgColor();
190  m_parent->SetDrawBgColor( MakeColour( WHITE ) );
191 
192  screen->m_ScreenNumber = aPageNum;
193  m_parent->DrawWorkSheet( dc, screen, 0, IU_PER_MILS, wxEmptyString );
194 
195  m_parent->SetDrawBgColor( bg_color );
196  screen->m_IsPrinting = false;
197  panel->SetClipBox( oldClipBox );
198 
199  GRForceBlackPen( false );
200 
201  screen->m_StartVisu = tmp_startvisu;
202  screen->m_DrawOrg = old_org;
203  screen->SetZoom( oldZoom );
204 }
205 
206 
207 int InvokeDialogPrint( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData,
208  wxPageSetupDialogData* aPageSetupData )
209 {
210  int pageCount = 2;
211 
212  wxPrintDialogData printDialogData( *aPrintData );
213  printDialogData.SetMaxPage( pageCount );
214 
215  if( pageCount > 1 )
216  printDialogData.EnablePageNumbers( true );
217 
218  wxPrinter printer( &printDialogData );
219  PLEDITOR_PRINTOUT printout( aCaller, _( "Print Page Layout" ) );
220 
221  if( !printer.Print( aCaller, &printout, true ) )
222  {
223  if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
224  wxMessageBox( _( "An error occurred attempting to print the page layout." ),
225  _( "Printing" ), wxOK );
226  return 0;
227  }
228 
229  *aPageSetupData = printer.GetPrintDialogData().GetPrintData();
230 
231  return 1;
232 }
233 
234 int InvokeDialogPrintPreview( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData )
235 {
236  // Pass two printout objects: for preview, and possible printing.
237  wxString title = _( "Preview" );
238  wxPrintPreview* preview = new wxPrintPreview( new PLEDITOR_PRINTOUT( aCaller, title ),
239  new PLEDITOR_PRINTOUT( aCaller, title ),
240  aPrintData );
241 
242  preview->SetZoom( 70 );
243 
244  PLEDITOR_PREVIEW_FRAME* frame = new PLEDITOR_PREVIEW_FRAME( preview, aCaller, title );
245 
246  frame->Initialize();
247  frame->Show( true );
248 
249  return 1;
250 }
251 
void GRResetPenAndBrush(wxDC *DC)
Definition: gr_basic.cpp:196
const PAGE_INFO & GetPageSettings() const override
Implementation of conversion functions that require both schematic and board internal units...
bool OnPrintPage(int aPageNum) override
EDA_DRAW_PANEL * GetCanvas()
Definition: draw_frame.h:324
void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) override
double GetZoom() const
Function GetZoom returns the current "zoom factor", which is a measure of "internal units per device ...
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.
virtual void SetDrawBgColor(COLOR4D aColor)
Definition: draw_frame.h:316
int InvokeDialogPrintPreview(PL_EDITOR_FRAME *aCaller, wxPrintData *aPrintData)
Create and show a print preview dialog returns 1 if OK, 0 , there is a problem.
description of graphic items and texts to build a title block
bool Show(bool show) override
#define MAX_VALUE
void SetClipBox(const EDA_RECT &aRect)
wxPoint m_StartVisu
Coordinates in drawing units of the current view position (upper left corner of device) ...
wxColour MakeColour(EDA_COLOR_T aColor)
Function MakeColour returns a wxWidgets wxColor from a KiCad color index with alpha value...
Definition: colors.h:208
virtual COLOR4D GetDrawBgColor() const
Definition: draw_frame.h:311
void GRForceBlackPen(bool flagforce)
Function GRForceBlackPen.
Definition: gr_basic.cpp:271
Custom print preview frame.
bool HasPage(int aPageNum) override
Helper dialog and control classes.
PL_EDITOR_SCREEN * GetScreen() const override
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
EDA_RECT * GetClipBox()
PLEDITOR_PRINTOUT(PL_EDITOR_FRAME *aParent, const wxString &aTitle)
bool SetZoom(double iu_per_du)
Function SetZoom adjusts the current zoom factor.
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
Class EDA_RECT handles the component boundary box.
wxPoint m_DrawOrg
offsets for drawing the circuit on the screen
Custom print out for printing schematics.
Definition: colors.h:49
classes and function to generate graphics to plt or draw titles blocks and frame references ...
void DrawWorkSheet(wxDC *aDC, BASE_SCREEN *aScreen, int aLineWidth, double aScale, const wxString &aFilename, const wxString &aSheetLayer=wxEmptyString)
Function DrawWorkSheet Draws on screen the page layout with the frame and the basic inscriptions...
Definition: worksheet.cpp:77
void DrawPage(int aPageNum)
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39