2013-07-19 18:27:22 +00:00
|
|
|
/**
|
|
|
|
* @file dialogs_for_printing.cpp
|
|
|
|
*/
|
|
|
|
|
2013-08-13 17:51:22 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
2021-07-18 14:06:48 +00:00
|
|
|
* Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-10-18 20:30:37 +00:00
|
|
|
*
|
2013-08-13 17:51:22 +00:00
|
|
|
* @author Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2013-07-19 18:27:22 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <base_units.h>
|
2020-10-14 02:46:39 +00:00
|
|
|
#include <gr_basic.h>
|
2021-02-22 23:47:17 +00:00
|
|
|
#include <drawing_sheet/ds_data_item.h>
|
|
|
|
#include <drawing_sheet/ds_data_model.h>
|
|
|
|
#include <drawing_sheet/ds_painter.h>
|
2020-12-17 13:12:18 +00:00
|
|
|
#include <dialogs/dialog_page_settings.h>
|
2020-10-13 05:04:31 +00:00
|
|
|
|
|
|
|
#include "invoke_pl_editor_dialog.h"
|
|
|
|
#include "pl_editor_frame.h"
|
|
|
|
#include "pl_editor_id.h"
|
2013-07-19 18:27:22 +00:00
|
|
|
|
2020-10-18 18:24:20 +00:00
|
|
|
#include <wx/msgdlg.h>
|
2021-06-03 22:32:32 +00:00
|
|
|
#include <wx/print.h>
|
2020-10-18 18:24:20 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
/**
|
|
|
|
* Custom print out for printing schematics.
|
|
|
|
*/
|
|
|
|
class PLEDITOR_PRINTOUT : public wxPrintout
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
PL_EDITOR_FRAME* m_parent;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PLEDITOR_PRINTOUT( PL_EDITOR_FRAME* aParent, const wxString& aTitle ) :
|
|
|
|
wxPrintout( aTitle )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
wxASSERT( aParent != nullptr );
|
2013-07-19 18:27:22 +00:00
|
|
|
m_parent = aParent;
|
|
|
|
}
|
|
|
|
|
2016-09-24 18:53:15 +00:00
|
|
|
bool OnPrintPage( int aPageNum ) override;
|
2016-09-25 17:06:49 +00:00
|
|
|
bool HasPage( int aPageNum ) override { return ( aPageNum <= 2 ); }
|
2016-09-24 18:53:15 +00:00
|
|
|
void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo ) override;
|
2019-05-31 17:37:07 +00:00
|
|
|
void PrintPage( int aPageNum );
|
2013-07-19 18:27:22 +00:00
|
|
|
};
|
|
|
|
|
2020-10-18 20:30:37 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
/**
|
|
|
|
* Custom print preview frame.
|
|
|
|
*/
|
|
|
|
class PLEDITOR_PREVIEW_FRAME : public wxPreviewFrame
|
|
|
|
{
|
2013-07-20 19:36:19 +00:00
|
|
|
PL_EDITOR_FRAME* m_parent;
|
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
public:
|
|
|
|
PLEDITOR_PREVIEW_FRAME( wxPrintPreview* aPreview, PL_EDITOR_FRAME* aParent,
|
2020-11-29 10:49:02 +00:00
|
|
|
const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
|
|
|
|
const wxSize& aSize = wxDefaultSize ) :
|
2013-07-19 18:27:22 +00:00
|
|
|
wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
|
|
|
|
{
|
2013-07-20 19:36:19 +00:00
|
|
|
m_parent = aParent;
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
bool Show( bool show ) override
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
// Show or hide the window. If hiding, save current position and size.
|
|
|
|
// If showing, use previous position and size.
|
|
|
|
if( show )
|
|
|
|
{
|
2013-07-20 19:36:19 +00:00
|
|
|
bool centre = false;
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2013-07-20 19:36:19 +00:00
|
|
|
if( s_size.x == 0 || s_size.y == 0 )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
s_size = ( m_parent->GetSize() * 3 ) / 4;
|
2013-07-20 19:36:19 +00:00
|
|
|
s_pos = wxDefaultPosition;
|
|
|
|
centre = true;
|
|
|
|
}
|
2013-07-19 18:27:22 +00:00
|
|
|
|
2013-07-20 19:36:19 +00:00
|
|
|
SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
|
|
|
|
|
|
|
|
if( centre )
|
|
|
|
Center();
|
|
|
|
|
|
|
|
ret = wxPreviewFrame::Show( show );
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Save the dialog's position & size before hiding
|
|
|
|
s_size = GetSize();
|
|
|
|
s_pos = GetPosition();
|
|
|
|
|
|
|
|
ret = wxPreviewFrame::Show( show );
|
|
|
|
}
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static wxPoint s_pos;
|
|
|
|
static wxSize s_size;
|
|
|
|
|
|
|
|
DECLARE_CLASS( PLEDITOR_PREVIEW_FRAME )
|
|
|
|
DECLARE_EVENT_TABLE()
|
|
|
|
DECLARE_NO_COPY_CLASS( PLEDITOR_PREVIEW_FRAME )
|
|
|
|
};
|
|
|
|
|
2020-10-18 20:30:37 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
wxPoint PLEDITOR_PREVIEW_FRAME::s_pos;
|
|
|
|
wxSize PLEDITOR_PREVIEW_FRAME::s_size;
|
|
|
|
|
2020-10-18 20:30:37 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
IMPLEMENT_CLASS( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
|
|
|
|
|
2020-10-18 20:30:37 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
BEGIN_EVENT_TABLE( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
|
|
|
|
EVT_CLOSE( PLEDITOR_PREVIEW_FRAME::OnCloseWindow )
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
|
|
|
|
bool PLEDITOR_PRINTOUT::OnPrintPage( int aPageNum )
|
|
|
|
{
|
2019-05-31 17:37:07 +00:00
|
|
|
PrintPage( aPageNum );
|
2013-07-19 18:27:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PLEDITOR_PRINTOUT::GetPageInfo( int* minPage, int* maxPage,
|
|
|
|
int* selPageFrom, int* selPageTo )
|
|
|
|
{
|
|
|
|
*minPage = *selPageFrom = 1;
|
|
|
|
*maxPage = *selPageTo = 2;
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2019-05-31 12:15:25 +00:00
|
|
|
void PLEDITOR_PRINTOUT::PrintPage( int aPageNum )
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
2022-01-01 01:36:41 +00:00
|
|
|
VECTOR2I tmp_startvisu;
|
2013-07-19 18:27:22 +00:00
|
|
|
wxSize pageSizeIU; // Page size in internal units
|
2022-01-01 01:36:41 +00:00
|
|
|
VECTOR2I old_org;
|
2013-07-19 18:27:22 +00:00
|
|
|
wxRect fitRect;
|
|
|
|
wxDC* dc = GetDC();
|
2020-07-13 11:21:40 +00:00
|
|
|
BASE_SCREEN* screen = m_parent->GetScreen();
|
2013-07-19 18:27:22 +00:00
|
|
|
|
2020-06-13 21:09:02 +00:00
|
|
|
// Save current offsets and clip box.
|
2013-07-19 18:27:22 +00:00
|
|
|
tmp_startvisu = screen->m_StartVisu;
|
|
|
|
old_org = screen->m_DrawOrg;
|
|
|
|
|
|
|
|
// Change scale factor and offset to print the whole page.
|
|
|
|
|
|
|
|
pageSizeIU = m_parent->GetPageSettings().GetSizeIU();
|
|
|
|
FitThisSizeToPaper( pageSizeIU );
|
|
|
|
fitRect = GetLogicalPaperRect();
|
|
|
|
|
|
|
|
int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
|
|
|
|
int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
|
|
|
|
|
|
|
|
OffsetLogicalOrigin( xoffset, yoffset );
|
|
|
|
|
|
|
|
GRResetPenAndBrush( dc );
|
|
|
|
GRForceBlackPen( true );
|
|
|
|
|
2017-02-20 16:57:41 +00:00
|
|
|
COLOR4D bg_color = m_parent->GetDrawBgColor();
|
2020-06-19 02:32:14 +00:00
|
|
|
m_parent->SetDrawBgColor( WHITE );
|
2013-07-19 18:27:22 +00:00
|
|
|
|
2020-10-18 20:30:37 +00:00
|
|
|
screen->SetVirtualPageNumber( aPageNum );
|
2020-04-14 12:25:00 +00:00
|
|
|
|
2021-02-22 23:47:17 +00:00
|
|
|
KIGFX::DS_RENDER_SETTINGS renderSettings;
|
2020-04-14 12:25:00 +00:00
|
|
|
renderSettings.SetDefaultPenWidth( 1 );
|
2021-02-22 16:37:43 +00:00
|
|
|
renderSettings.SetLayerColor( LAYER_DRAWINGSHEET, COLOR4D( RED ) );
|
2020-04-14 12:25:00 +00:00
|
|
|
renderSettings.SetPrintDC( dc );
|
|
|
|
|
2020-05-25 07:42:03 +00:00
|
|
|
// Ensure the scaling factor (used only in printing) of bitmaps is up to date
|
2021-02-22 23:47:17 +00:00
|
|
|
DS_DATA_MODEL& model = DS_DATA_MODEL::GetTheInstance();
|
2020-05-25 07:42:03 +00:00
|
|
|
|
2021-02-22 23:47:17 +00:00
|
|
|
for( DS_DATA_ITEM* dataItem : model.GetItems() )
|
2020-05-25 07:42:03 +00:00
|
|
|
{
|
2021-02-22 23:47:17 +00:00
|
|
|
if( dataItem->GetType() == DS_DATA_ITEM::DS_BITMAP )
|
2020-11-29 10:49:02 +00:00
|
|
|
{
|
2021-02-22 23:47:17 +00:00
|
|
|
BITMAP_BASE* bitmap = static_cast<DS_DATA_ITEM_BITMAP*>( dataItem )->m_ImageBitmap;
|
2020-11-29 10:49:02 +00:00
|
|
|
bitmap->SetPixelSizeIu( IU_PER_MILS * 1000 / bitmap->GetPPI() );
|
|
|
|
}
|
2020-05-25 07:42:03 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 16:37:43 +00:00
|
|
|
m_parent->PrintDrawingSheet( &renderSettings, screen, IU_PER_MILS, wxEmptyString );
|
2013-07-19 18:27:22 +00:00
|
|
|
|
2014-06-20 11:13:04 +00:00
|
|
|
m_parent->SetDrawBgColor( bg_color );
|
2013-07-19 18:27:22 +00:00
|
|
|
|
|
|
|
GRForceBlackPen( false );
|
|
|
|
|
|
|
|
screen->m_StartVisu = tmp_startvisu;
|
|
|
|
screen->m_DrawOrg = old_org;
|
2020-05-25 07:42:03 +00:00
|
|
|
|
2021-02-22 16:37:43 +00:00
|
|
|
// PrintDrawingSheet clears the current display list when calling BuildDrawItemsList()
|
2020-05-25 07:42:03 +00:00
|
|
|
// So rebuild and redraw it.
|
2021-05-30 22:56:24 +00:00
|
|
|
m_parent->GetCanvas()->DisplayDrawingSheet();
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int InvokeDialogPrint( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData,
|
|
|
|
wxPageSetupDialogData* aPageSetupData )
|
|
|
|
{
|
|
|
|
int pageCount = 2;
|
|
|
|
|
|
|
|
wxPrintDialogData printDialogData( *aPrintData );
|
|
|
|
printDialogData.SetMaxPage( pageCount );
|
|
|
|
|
|
|
|
if( pageCount > 1 )
|
|
|
|
printDialogData.EnablePageNumbers( true );
|
|
|
|
|
|
|
|
wxPrinter printer( &printDialogData );
|
2021-05-30 22:56:24 +00:00
|
|
|
PLEDITOR_PRINTOUT printout( aCaller, _( "Print Drawing Sheet" ) );
|
2013-07-19 18:27:22 +00:00
|
|
|
|
|
|
|
if( !printer.Print( aCaller, &printout, true ) )
|
|
|
|
{
|
|
|
|
if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
|
2021-05-30 22:56:24 +00:00
|
|
|
wxMessageBox( _( "An error occurred attempting to print the drawing sheet." ),
|
2013-07-19 18:27:22 +00:00
|
|
|
_( "Printing" ), wxOK );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aPageSetupData = printer.GetPrintDialogData().GetPrintData();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-10-18 20:30:37 +00:00
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
int InvokeDialogPrintPreview( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData )
|
|
|
|
{
|
|
|
|
// Pass two printout objects: for preview, and possible printing.
|
|
|
|
wxString title = _( "Preview" );
|
|
|
|
wxPrintPreview* preview = new wxPrintPreview( new PLEDITOR_PRINTOUT( aCaller, title ),
|
|
|
|
new PLEDITOR_PRINTOUT( aCaller, title ),
|
|
|
|
aPrintData );
|
|
|
|
|
2013-07-20 19:36:19 +00:00
|
|
|
preview->SetZoom( 70 );
|
2013-07-19 18:27:22 +00:00
|
|
|
|
|
|
|
PLEDITOR_PREVIEW_FRAME* frame = new PLEDITOR_PREVIEW_FRAME( preview, aCaller, title );
|
|
|
|
|
|
|
|
frame->Initialize();
|
|
|
|
frame->Show( true );
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|