2
* @file dialogs_for_printing.cpp
5
/* Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
6
* Copyright (C) 1992-2013 KiCad Developers, see AUTHORS.txt for contributors.
8
* This program is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU General Public License
10
* as published by the Free Software Foundation; either version 2
11
* of the License, or (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, you may find one here:
20
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21
* or you may search the http://www.gnu.org website for the version 2 license,
22
* or you may write to the Free Software Foundation, Inc.,
23
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28
#include <class_drawpanel.h>
29
#include <base_units.h>
31
#include <pl_editor_frame.h>
32
#include <pl_editor_id.h>
33
#include <dialog_helpers.h>
34
#include <worksheet_shape_builder.h>
35
#include <class_worksheet_dataitem.h>
36
#include <dialog_page_settings.h>
37
#include <invoke_pl_editor_dialog.h>
40
* Custom print out for printing schematics.
42
class PLEDITOR_PRINTOUT : public wxPrintout
45
PL_EDITOR_FRAME* m_parent;
48
PLEDITOR_PRINTOUT( PL_EDITOR_FRAME* aParent, const wxString& aTitle ) :
51
wxASSERT( aParent != NULL );
55
bool OnPrintPage( int aPageNum );
56
bool HasPage( int aPageNum ) { return ( aPageNum <= 2 ); }
57
void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo );
58
void DrawPage( int aPageNum );
62
* Custom print preview frame.
64
class PLEDITOR_PREVIEW_FRAME : public wxPreviewFrame
66
PL_EDITOR_FRAME* m_parent;
69
PLEDITOR_PREVIEW_FRAME( wxPrintPreview* aPreview, PL_EDITOR_FRAME* aParent,
70
const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
71
const wxSize& aSize = wxDefaultSize ) :
72
wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
77
bool Show( bool show ) // overload
81
// Show or hide the window. If hiding, save current position and size.
82
// If showing, use previous position and size.
86
if( s_size.x == 0 || s_size.y == 0 )
88
s_size = (m_parent->GetSize() * 3) / 4;
89
s_pos = wxDefaultPosition;
93
SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
98
ret = wxPreviewFrame::Show( show );
102
// Save the dialog's position & size before hiding
104
s_pos = GetPosition();
106
ret = wxPreviewFrame::Show( show );
112
static wxPoint s_pos;
113
static wxSize s_size;
115
DECLARE_CLASS( PLEDITOR_PREVIEW_FRAME )
116
DECLARE_EVENT_TABLE()
117
DECLARE_NO_COPY_CLASS( PLEDITOR_PREVIEW_FRAME )
120
wxPoint PLEDITOR_PREVIEW_FRAME::s_pos;
121
wxSize PLEDITOR_PREVIEW_FRAME::s_size;
123
IMPLEMENT_CLASS( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
125
BEGIN_EVENT_TABLE( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
126
EVT_CLOSE( PLEDITOR_PREVIEW_FRAME::OnCloseWindow )
130
bool PLEDITOR_PRINTOUT::OnPrintPage( int aPageNum )
132
DrawPage( aPageNum );
137
void PLEDITOR_PRINTOUT::GetPageInfo( int* minPage, int* maxPage,
138
int* selPageFrom, int* selPageTo )
140
*minPage = *selPageFrom = 1;
141
*maxPage = *selPageTo = 2;
145
* This is the real print function: print the active screen
147
void PLEDITOR_PRINTOUT::DrawPage( int aPageNum )
150
wxPoint tmp_startvisu;
151
wxSize pageSizeIU; // Page size in internal units
156
EDA_DRAW_PANEL* panel = m_parent->GetCanvas();
157
PL_EDITOR_SCREEN* screen = m_parent->GetScreen();
159
// Save current scale factor, offsets, and clip box.
160
tmp_startvisu = screen->m_StartVisu;
161
oldZoom = screen->GetZoom();
162
old_org = screen->m_DrawOrg;
164
oldClipBox = *panel->GetClipBox();
166
// Change clip box to print the whole page.
167
#define MAX_VALUE (INT_MAX/2) // MAX_VALUE is the max we can use in an integer
168
// and that allows calculations without overflow
169
panel->SetClipBox( EDA_RECT( wxPoint( 0, 0 ), wxSize( MAX_VALUE, MAX_VALUE ) ) );
171
// Change scale factor and offset to print the whole page.
173
pageSizeIU = m_parent->GetPageSettings().GetSizeIU();
174
FitThisSizeToPaper( pageSizeIU );
175
fitRect = GetLogicalPaperRect();
177
int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
178
int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
180
OffsetLogicalOrigin( xoffset, yoffset );
182
GRResetPenAndBrush( dc );
183
GRForceBlackPen( true );
184
screen->m_IsPrinting = true;
186
EDA_COLOR_T bg_color = g_DrawBgColor;
187
g_DrawBgColor = WHITE;
189
screen->m_ScreenNumber = aPageNum;
190
m_parent->DrawWorkSheet( dc, screen, 0, IU_PER_MILS, wxEmptyString );
192
g_DrawBgColor = bg_color;
193
screen->m_IsPrinting = false;
194
panel->SetClipBox( oldClipBox );
196
GRForceBlackPen( false );
198
screen->m_StartVisu = tmp_startvisu;
199
screen->m_DrawOrg = old_org;
200
screen->SetZoom( oldZoom );
204
int InvokeDialogPrint( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData,
205
wxPageSetupDialogData* aPageSetupData )
209
wxPrintDialogData printDialogData( *aPrintData );
210
printDialogData.SetMaxPage( pageCount );
213
printDialogData.EnablePageNumbers( true );
215
wxPrinter printer( &printDialogData );
216
PLEDITOR_PRINTOUT printout( aCaller, _( "Print Page Layout" ) );
218
if( !printer.Print( aCaller, &printout, true ) )
220
if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
221
wxMessageBox( _( "An error occurred attempting to print the page layout." ),
222
_( "Printing" ), wxOK );
226
*aPageSetupData = printer.GetPrintDialogData().GetPrintData();
231
int InvokeDialogPrintPreview( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData )
233
// Pass two printout objects: for preview, and possible printing.
234
wxString title = _( "Preview" );
235
wxPrintPreview* preview = new wxPrintPreview( new PLEDITOR_PRINTOUT( aCaller, title ),
236
new PLEDITOR_PRINTOUT( aCaller, title ),
239
preview->SetZoom( 70 );
241
PLEDITOR_PREVIEW_FRAME* frame = new PLEDITOR_PREVIEW_FRAME( preview, aCaller, title );