kicad/pcbnew/dialog_print_using_printer.cpp

524 lines
16 KiB
C++
Raw Normal View History

2009-01-17 17:32:20 +00:00
/****************************************/
/* File: dialog_print_using_printer.cpp */
/****************************************/
2007-05-06 16:03:28 +00:00
// Set this to 1 if you want to test PostScript printing under MSW.
#define wxTEST_POSTSCRIPT_IN_MSW 1
#include "fctsys.h"
#include "appl_wxstruct.h"
2007-05-06 16:03:28 +00:00
#include "common.h"
#include "class_drawpanel.h"
#include "confirm.h"
2007-05-06 16:03:28 +00:00
2009-01-17 17:32:20 +00:00
#include "dialog_print_using_printer_base.h"
#include "printout_controler.h"
2007-05-06 16:03:28 +00:00
#include "pcbnew.h"
#include "wxPcbStruct.h"
#include "pcbplot.h"
#include "class_board_design_settings.h"
2008-04-18 13:28:56 +00:00
#define WIDTH_MAX_VALUE 1000
#define WIDTH_MIN_VALUE 1
2007-05-06 16:03:28 +00:00
extern int g_DrawDefaultLineThickness;
// Local variables
2008-04-18 13:28:56 +00:00
static long s_SelectedLayers;
static double s_ScaleList[] =
{ 0, 0.5, 0.7, 0.999, 1.0, 1.4, 2.0, 3.0, 4.0 };
// Define min et max reasonnable values for print scale
#define MIN_SCALE 0.01
#define MAX_SCALE 100.0
2008-04-18 13:28:56 +00:00
2007-05-06 16:03:28 +00:00
// static print data and page setup data, to remember settings during the session
2008-03-04 04:22:27 +00:00
static wxPrintData* g_PrintData;
2007-05-06 16:03:28 +00:00
static PRINT_PARAMETERS s_Parameters;
2007-05-06 16:03:28 +00:00
2009-01-17 17:32:20 +00:00
/* Dialog to print schematic. Class derived from DIALOG_PRINT_USING_PRINTER_base
* created by wxFormBuilder
*/
class DIALOG_PRINT_USING_PRINTER : public DIALOG_PRINT_USING_PRINTER_base
{
private:
WinEDA_PcbFrame* m_Parent;
2009-01-17 17:32:20 +00:00
wxConfig* m_Config;
wxCheckBox* m_BoxSelectLayer[32];
static bool m_ExcludeEdgeLayer;
static wxPoint s_LastPos;
static wxSize s_LastSize;
2009-01-17 17:32:20 +00:00
public:
DIALOG_PRINT_USING_PRINTER( WinEDA_PcbFrame* parent );
2009-01-17 17:32:20 +00:00
~DIALOG_PRINT_USING_PRINTER() {};
private:
void OnCloseWindow( wxCloseEvent& event );
void OnPageSetup( wxCommandEvent& event );
2009-01-17 17:32:20 +00:00
void OnPrintPreview( wxCommandEvent& event );
void OnPrintButtonClick( wxCommandEvent& event );
void OnButtonCancelClick( wxCommandEvent& event ) { Close(); }
void SetPrintParameters( );
2009-01-17 17:32:20 +00:00
void SetPenWidth();
void InitValues( );
bool Show( bool show ); // overload stock function
2009-01-17 17:32:20 +00:00
public:
bool IsMirrored() { return m_Print_Mirror->IsChecked(); }
bool ExcludeEdges() { return m_Exclude_Edges_Pcb->IsChecked(); }
bool PrintUsingSinglePage() { return m_PagesOption->GetSelection(); }
int SetLayerMaskFromListSelection();
};
2007-05-06 16:03:28 +00:00
bool DIALOG_PRINT_USING_PRINTER::m_ExcludeEdgeLayer;
// We want our dialog to remember its previous screen position
wxPoint DIALOG_PRINT_USING_PRINTER::s_LastPos( -1, -1 );
wxSize DIALOG_PRINT_USING_PRINTER::s_LastSize;
2008-03-04 04:22:27 +00:00
2008-04-18 13:28:56 +00:00
2007-05-06 16:03:28 +00:00
/*******************************************************/
void WinEDA_PcbFrame::ToPrinter( wxCommandEvent& event )
2007-05-06 16:03:28 +00:00
/*******************************************************/
2008-03-04 04:22:27 +00:00
/* Virtual function:
* Display the print dialog
2008-03-04 04:22:27 +00:00
*/
2007-05-06 16:03:28 +00:00
{
2008-03-04 04:22:27 +00:00
if( g_PrintData == NULL ) // First print
{
2007-05-06 16:03:28 +00:00
g_PrintData = new wxPrintData();
2008-03-04 04:22:27 +00:00
if( !g_PrintData->Ok() )
{
DisplayError( this, _( "Error Init Printer info" ) );
}
g_PrintData->SetQuality( wxPRINT_QUALITY_HIGH ); // Default resolution = HIGHT;
g_PrintData->SetOrientation( DEFAULT_ORIENTATION_PAPER );
}
2007-05-06 16:03:28 +00:00
2009-01-17 17:32:20 +00:00
DIALOG_PRINT_USING_PRINTER* frame = new DIALOG_PRINT_USING_PRINTER( this );
2008-03-04 04:22:27 +00:00
frame->ShowModal(); frame->Destroy();
2007-05-06 16:03:28 +00:00
}
2009-01-17 17:32:20 +00:00
/*************************************************************************************/
DIALOG_PRINT_USING_PRINTER::DIALOG_PRINT_USING_PRINTER( WinEDA_PcbFrame* parent ) :
2009-01-17 17:32:20 +00:00
DIALOG_PRINT_USING_PRINTER_base( parent )
/*************************************************************************************/
{
2009-01-17 17:32:20 +00:00
m_Parent = parent;
m_Config = wxGetApp().m_EDA_Config;
InitValues( );
if( GetSizer() )
{
GetSizer()->SetSizeHints( this );
}
Center();
2009-01-17 17:32:20 +00:00
}
2007-05-06 16:03:28 +00:00
2009-01-17 17:32:20 +00:00
/************************************************************************/
void DIALOG_PRINT_USING_PRINTER::InitValues( )
2009-01-17 17:32:20 +00:00
/************************************************************************/
{
SetFocus();
int layer_max = NB_LAYERS;
2009-01-17 17:32:20 +00:00
wxString msg;
BOARD* board = m_Parent->GetBoard();
// Create layer list.
int layer;
wxString layerKey;
for( layer = 0; layer < NB_LAYERS; ++layer )
{
if( !board->IsLayerEnabled( layer ) )
m_BoxSelectLayer[layer] = NULL;
else
m_BoxSelectLayer[layer] =
new wxCheckBox( this, -1, board->GetLayerName( layer ) );
}
2009-01-17 17:32:20 +00:00
// Add wxCheckBoxes in layers lists dialog
// List layers in same order than in setup layers dialog
// (Front or Top to Back or Bottom)
DECLARE_LAYERS_ORDER_LIST(layersOrder);
for( int layer_idx = 0; layer_idx < NB_LAYERS; ++layer_idx )
2008-03-04 04:22:27 +00:00
{
layer = layersOrder[layer_idx];
wxASSERT(layer < NB_LAYERS);
if( m_BoxSelectLayer[layer] == NULL )
continue;
if( layer < NB_COPPER_LAYERS )
m_CopperLayersBoxSizer->Add( m_BoxSelectLayer[layer],
0, wxGROW | wxALL, 1 );
else
m_TechnicalLayersBoxSizer->Add( m_BoxSelectLayer[layer],
0, wxGROW | wxALL, 1 );
layerKey.Printf( OPTKEY_LAYERBASE, layer );
bool option;
if( m_Config->Read( layerKey, &option ) )
m_BoxSelectLayer[layer]->SetValue( option );
2008-03-04 04:22:27 +00:00
else
{
long mask = 1 << layer;
if( mask & s_SelectedLayers )
m_BoxSelectLayer[layer]->SetValue( true );
}
2008-03-04 04:22:27 +00:00
}
// Option for excluding contents of "Edges Pcb" layer
m_Exclude_Edges_Pcb->Show( true );
2008-03-04 04:22:27 +00:00
// Read the scale adjust option
int scale_idx = 4; // default selected scale = ScaleList[4] = 1.000
if( m_Config )
2008-03-04 04:22:27 +00:00
{
m_Config->Read( OPTKEY_PRINT_X_FINESCALE_ADJ, &s_Parameters.m_XScaleAdjust );
m_Config->Read( OPTKEY_PRINT_Y_FINESCALE_ADJ, &s_Parameters.m_YScaleAdjust );
m_Config->Read( OPTKEY_PRINT_SCALE, &scale_idx );
m_Config->Read( OPTKEY_PRINT_PAGE_FRAME, &s_Parameters.m_Print_Sheet_Ref, 1);
m_Config->Read( OPTKEY_PRINT_MONOCHROME_MODE, &s_Parameters.m_Print_Black_and_White, 1);
int tmp;
m_Config->Read( OPTKEY_PRINT_PADS_DRILL, &tmp, PRINT_PARAMETERS::SMALL_DRILL_SHAPE );
s_Parameters.m_DrillShapeOpt = (PRINT_PARAMETERS::DrillShapeOptT) tmp;
2008-04-18 13:28:56 +00:00
// Test for a reasonnable scale value. Set to 1 if problem
if( s_Parameters.m_XScaleAdjust < MIN_SCALE ||
s_Parameters.m_YScaleAdjust < MIN_SCALE ||
s_Parameters.m_XScaleAdjust > MAX_SCALE ||
s_Parameters.m_YScaleAdjust > MAX_SCALE )
s_Parameters.m_XScaleAdjust = s_Parameters.m_YScaleAdjust = 1.0;
2008-04-18 13:28:56 +00:00
s_SelectedLayers = 0;
2009-01-17 17:32:20 +00:00
for( int layer = 0; layer<layer_max; ++layer )
2008-04-18 13:28:56 +00:00
{
if( m_BoxSelectLayer[layer] == NULL )
continue;
2008-04-18 13:28:56 +00:00
wxString layerKey;
bool option;
layerKey.Printf( OPTKEY_LAYERBASE, layer );
option = false;
if( m_Config->Read( layerKey, &option ) )
2008-04-18 13:28:56 +00:00
{
2009-01-17 17:32:20 +00:00
m_BoxSelectLayer[layer]->SetValue( option );
2008-04-18 13:28:56 +00:00
if( option )
s_SelectedLayers |= 1 << layer;
}
}
2008-03-04 04:22:27 +00:00
}
m_ScaleOption->SetSelection( scale_idx );
m_Print_Mirror->SetValue(s_Parameters.m_PrintMirror);
m_Exclude_Edges_Pcb->SetValue(m_ExcludeEdgeLayer);
m_Print_Sheet_Ref->SetValue( s_Parameters.m_Print_Sheet_Ref );
2008-03-04 04:22:27 +00:00
// Options to plot pads and vias holes
m_Drill_Shape_Opt->SetSelection( s_Parameters.m_DrillShapeOpt );
if( s_Parameters.m_Print_Black_and_White )
2009-01-17 17:32:20 +00:00
m_ModeColorOption->SetSelection( 1 );
else
m_ModeColorOption->SetSelection( 0 );
2009-01-17 17:32:20 +00:00
s_Parameters.m_PenDefaultSize = g_DrawDefaultLineThickness;
2009-01-17 17:32:20 +00:00
AddUnitSymbol( *m_TextPenWidth, g_UnitMetric );
m_DialogPenWidth->SetValue(
ReturnStringFromValue( g_UnitMetric, s_Parameters.m_PenDefaultSize, m_Parent->m_InternalUnits ) );
2009-01-17 17:32:20 +00:00
2008-03-04 04:22:27 +00:00
// Create scale adjust option
msg.Printf( wxT( "%f" ), s_Parameters.m_XScaleAdjust );
2008-03-04 04:22:27 +00:00
m_FineAdjustXscaleOpt->SetValue( msg );
msg.Printf( wxT( "%f" ), s_Parameters.m_YScaleAdjust );
2008-03-04 04:22:27 +00:00
m_FineAdjustYscaleOpt->SetValue( msg );
2007-05-06 16:03:28 +00:00
}
2008-03-04 04:22:27 +00:00
/*************************************************/
bool DIALOG_PRINT_USING_PRINTER::Show( bool show )
/*************************************************/
{
bool ret;
if( show )
{
if( s_LastPos.x != -1 )
{
SetSize( s_LastPos.x, s_LastPos.y, s_LastSize.x, s_LastSize.y, 0 );
}
ret = DIALOG_PRINT_USING_PRINTER_base::Show( show );
}
else
{
// Save the dialog's position before hiding
s_LastPos = GetPosition();
s_LastSize = GetSize();
ret = DIALOG_PRINT_USING_PRINTER_base::Show( show );
}
return ret;
}
2009-01-17 17:32:20 +00:00
/**************************************************************/
int DIALOG_PRINT_USING_PRINTER::SetLayerMaskFromListSelection()
/**************************************************************/
2007-05-06 16:03:28 +00:00
{
2008-03-04 04:22:27 +00:00
int page_count;
2009-01-17 17:32:20 +00:00
int layers_count = NB_LAYERS;
s_Parameters.m_PrintMaskLayer = 0;
2008-03-04 04:22:27 +00:00
int ii;
2009-01-17 17:32:20 +00:00
for( ii = 0, page_count = 0; ii < layers_count; ii++ )
2008-03-04 04:22:27 +00:00
{
if( m_BoxSelectLayer[ii] == NULL )
continue;
2009-01-17 17:32:20 +00:00
if( m_BoxSelectLayer[ii]->IsChecked() )
2008-03-04 04:22:27 +00:00
{
page_count++;
s_Parameters.m_PrintMaskLayer |= 1 << ii;
2008-03-04 04:22:27 +00:00
}
}
// In pcbnew: force the EDGE layer to be printed or not with the other layers
m_ExcludeEdgeLayer = m_Exclude_Edges_Pcb->IsChecked();
if( m_ExcludeEdgeLayer )
s_Parameters.m_Flags = 0;
else
s_Parameters.m_Flags = 1;
s_Parameters.m_PageCount = page_count;
2008-03-04 04:22:27 +00:00
return page_count;
2007-05-06 16:03:28 +00:00
}
2008-03-04 04:22:27 +00:00
2009-01-17 17:32:20 +00:00
/********************************************************************/
void DIALOG_PRINT_USING_PRINTER::OnCloseWindow( wxCloseEvent& event )
/********************************************************************/
2007-05-06 16:03:28 +00:00
{
SetPrintParameters();
2009-01-17 17:32:20 +00:00
if( m_Config )
2008-03-04 04:22:27 +00:00
{
m_Config->Write( OPTKEY_PRINT_X_FINESCALE_ADJ, s_Parameters.m_XScaleAdjust );
m_Config->Write( OPTKEY_PRINT_Y_FINESCALE_ADJ, s_Parameters.m_YScaleAdjust );
m_Config->Write( OPTKEY_PRINT_SCALE, m_ScaleOption->GetSelection() );
m_Config->Write( OPTKEY_PRINT_PAGE_FRAME, s_Parameters.m_Print_Sheet_Ref);
m_Config->Write( OPTKEY_PRINT_MONOCHROME_MODE, s_Parameters.m_Print_Black_and_White);
m_Config->Write( OPTKEY_PRINT_PADS_DRILL, (long) s_Parameters.m_DrillShapeOpt );
2008-04-18 13:28:56 +00:00
wxString layerKey;
for( int layer = 0; layer < NB_LAYERS; ++layer )
2008-04-18 13:28:56 +00:00
{
if( m_BoxSelectLayer[layer] == NULL )
continue;
2008-04-18 13:28:56 +00:00
layerKey.Printf( OPTKEY_LAYERBASE, layer );
2009-01-17 17:32:20 +00:00
m_Config->Write( layerKey, m_BoxSelectLayer[layer]->IsChecked() );
2008-04-18 13:28:56 +00:00
}
2008-03-04 04:22:27 +00:00
}
EndModal( 0 );
2007-05-06 16:03:28 +00:00
}
2009-01-17 17:32:20 +00:00
/******************************************************************/
void DIALOG_PRINT_USING_PRINTER::SetPrintParameters( )
2009-01-17 17:32:20 +00:00
/******************************************************************/
2007-05-06 16:03:28 +00:00
{
s_Parameters.m_PrintMirror = m_Print_Mirror->GetValue();
s_Parameters.m_Print_Sheet_Ref = m_Print_Sheet_Ref->GetValue();
s_Parameters.m_Print_Black_and_White =
m_ModeColorOption->GetSelection() != 0;
s_Parameters.m_DrillShapeOpt =
(PRINT_PARAMETERS::DrillShapeOptT) m_Drill_Shape_Opt->GetSelection();
if( m_PagesOption )
s_Parameters.m_OptionPrintPage = m_PagesOption->GetSelection() != 0;
SetLayerMaskFromListSelection();
int idx = m_ScaleOption->GetSelection();
s_Parameters.m_PrintScale = s_ScaleList[idx];
g_pcb_plot_options.Scale = s_Parameters.m_PrintScale;
2008-03-04 04:22:27 +00:00
if( m_FineAdjustXscaleOpt )
{
if( s_Parameters.m_XScaleAdjust > MAX_SCALE ||
s_Parameters.m_YScaleAdjust > MAX_SCALE )
DisplayInfoMessage( NULL, _( "Warning: Scale option set to a very large value" ) );
m_FineAdjustXscaleOpt->GetValue().ToDouble( &s_Parameters.m_XScaleAdjust );
}
2008-03-04 04:22:27 +00:00
if( m_FineAdjustYscaleOpt )
{
// Test for a reasonnable scale value
if( s_Parameters.m_XScaleAdjust < MIN_SCALE ||
s_Parameters.m_YScaleAdjust < MIN_SCALE )
DisplayInfoMessage( NULL, _( "Warning: Scale option set to a very small value" ) );
m_FineAdjustYscaleOpt->GetValue().ToDouble( &s_Parameters.m_YScaleAdjust );
}
g_pcb_plot_options.ScaleAdjX = s_Parameters.m_XScaleAdjust;
g_pcb_plot_options.ScaleAdjX = s_Parameters.m_YScaleAdjust;
SetPenWidth();
2007-05-06 16:03:28 +00:00
}
2008-03-04 04:22:27 +00:00
2009-01-17 17:32:20 +00:00
/**********************************************/
void DIALOG_PRINT_USING_PRINTER::SetPenWidth()
/***********************************************/
2008-03-04 04:22:27 +00:00
/* Get the new pen width value, and verify min et max value
* NOTE: s_Parameters.m_PenDefaultSize is in internal units
2008-03-04 04:22:27 +00:00
*/
2007-05-06 16:03:28 +00:00
{
s_Parameters.m_PenDefaultSize = ReturnValueFromTextCtrl( *m_DialogPenWidth, m_Parent->m_InternalUnits );
2009-01-17 17:32:20 +00:00
if( s_Parameters.m_PenDefaultSize > WIDTH_MAX_VALUE )
2008-03-04 04:22:27 +00:00
{
s_Parameters.m_PenDefaultSize = WIDTH_MAX_VALUE;
2008-03-04 04:22:27 +00:00
}
2009-01-17 17:32:20 +00:00
if( s_Parameters.m_PenDefaultSize < WIDTH_MIN_VALUE )
2008-03-04 04:22:27 +00:00
{
s_Parameters.m_PenDefaultSize = WIDTH_MIN_VALUE;
2008-03-04 04:22:27 +00:00
}
2009-01-17 17:32:20 +00:00
g_DrawDefaultLineThickness = s_Parameters.m_PenDefaultSize;
2009-01-17 17:32:20 +00:00
m_DialogPenWidth->SetValue(
ReturnStringFromValue( g_UnitMetric, s_Parameters.m_PenDefaultSize, m_Parent->m_InternalUnits ) );
2007-05-06 16:03:28 +00:00
}
/**********************************************************/
void DIALOG_PRINT_USING_PRINTER::OnPageSetup( wxCommandEvent& event )
2007-05-06 16:03:28 +00:00
/**********************************************************/
2008-03-04 04:22:27 +00:00
2007-05-06 16:03:28 +00:00
/* Open a dialog box for printer setup (printer options, page size ...)
2008-03-04 04:22:27 +00:00
*/
2007-05-06 16:03:28 +00:00
{
2008-04-18 13:28:56 +00:00
wxPrintDialogData printDialogData( *g_PrintData );
2007-05-06 16:03:28 +00:00
2008-03-04 04:22:27 +00:00
if( printDialogData.Ok() )
{
2008-04-18 13:28:56 +00:00
wxPrintDialog printerDialog( this, &printDialogData );
2008-03-04 04:22:27 +00:00
printerDialog.ShowModal();
*g_PrintData = printerDialog.GetPrintDialogData().GetPrintData();
}
else
DisplayError( this, _( "Printer Problem!" ) );
2007-05-06 16:03:28 +00:00
}
/************************************************************/
2009-01-17 17:32:20 +00:00
void DIALOG_PRINT_USING_PRINTER::OnPrintPreview( wxCommandEvent& event )
2007-05-06 16:03:28 +00:00
/************************************************************/
2008-03-04 04:22:27 +00:00
2007-05-06 16:03:28 +00:00
/* Open and display a previewer frame for printing
2008-03-04 04:22:27 +00:00
*/
2007-05-06 16:03:28 +00:00
{
SetPrintParameters( );
2008-03-04 04:22:27 +00:00
// Pass two printout objects: for preview, and possible printing.
wxString title = _( "Print Preview" );
2008-03-04 04:22:27 +00:00
wxPrintPreview* preview =
new wxPrintPreview( new BOARD_PRINTOUT_CONTROLER( s_Parameters, m_Parent, title ),
new BOARD_PRINTOUT_CONTROLER( s_Parameters, m_Parent, title ),
2009-01-17 17:32:20 +00:00
g_PrintData );
2008-03-04 04:22:27 +00:00
if( preview == NULL )
{
2009-01-17 17:32:20 +00:00
DisplayError( this, wxT( "OnPrintPreview() problem" ) );
2008-03-04 04:22:27 +00:00
return;
}
SetLayerMaskFromListSelection();
// If no layer selected, we have no plot. prompt user if it happens
// because he could think there is a bug in pcbnew:
if( s_Parameters.m_PrintMaskLayer == 0 )
{
DisplayError( this, _( "No layer selected" ) );
return;
}
2007-05-06 16:03:28 +00:00
// Uses the parent position and size.
// @todo uses last position and size ans store them when exit in m_Config
wxPoint WPos = m_Parent->GetPosition();
wxSize WSize = m_Parent->GetSize();
2007-05-06 16:03:28 +00:00
wxPreviewFrame* frame = new wxPreviewFrame( preview, this, title, WPos, WSize );
2008-03-04 04:22:27 +00:00
frame->Initialize();
frame->Show( TRUE );
2007-05-06 16:03:28 +00:00
}
2009-01-17 17:32:20 +00:00
/***************************************************************************/
void DIALOG_PRINT_USING_PRINTER::OnPrintButtonClick( wxCommandEvent& event )
/***************************************************************************/
2008-03-04 04:22:27 +00:00
2009-01-17 17:32:20 +00:00
/* Called on activate Print button
2008-03-04 04:22:27 +00:00
*/
2007-05-06 16:03:28 +00:00
{
SetPrintParameters( );
// If no layer selected, we have no plot. prompt user if it happens
// because he could think there is a bug in pcbnew:
if( s_Parameters.m_PrintMaskLayer == 0 )
{
DisplayError( this, _( "No layer selected" ) );
return;
}
2008-04-18 13:28:56 +00:00
wxPrintDialogData printDialogData( *g_PrintData );
2007-05-06 16:03:28 +00:00
2008-04-18 13:28:56 +00:00
wxPrinter printer( &printDialogData );
2007-05-06 16:03:28 +00:00
wxString title = _( "Print" );
BOARD_PRINTOUT_CONTROLER printout( s_Parameters, m_Parent, title );
2007-05-06 16:03:28 +00:00
#if !defined(__WINDOWS__) && !wxCHECK_VERSION(2,9,0)
2008-04-18 13:28:56 +00:00
wxDC* dc = printout.GetDC();
2008-03-04 04:22:27 +00:00
( (wxPostScriptDC*) dc )->SetResolution( 600 ); // Postscript DC resolution is 600 ppi
2007-05-06 16:03:28 +00:00
#endif
2008-03-04 04:22:27 +00:00
if( !printer.Print( this, &printout, TRUE ) )
{
if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
DisplayError( this, _( "There was a problem printing" ) );
2007-05-06 16:03:28 +00:00
return;
2008-03-04 04:22:27 +00:00
}
else
{
*g_PrintData = printer.GetPrintDialogData().GetPrintData();
}
2007-05-06 16:03:28 +00:00
}