1
/****************************************/
2
/* File: dialog_print_using_printer.cpp */
3
/****************************************/
5
// Set this to 1 if you want to test PostScript printing under MSW.
6
#define wxTEST_POSTSCRIPT_IN_MSW 1
9
#include "appl_wxstruct.h"
11
#include "class_drawpanel.h"
14
#include "dialog_print_using_printer_base.h"
15
#include "printout_controler.h"
18
#include "wxGerberFrame.h"
19
#include "wxPcbStruct.h"
21
#include "class_board_design_settings.h"
23
#define WIDTH_MAX_VALUE 1000
24
#define WIDTH_MIN_VALUE 1
26
static long s_SelectedLayers;
27
static double s_ScaleList[] =
28
{ 0, 0.5, 0.7, 0.999, 1.0, 1.4, 2.0, 3.0, 4.0 };
30
// Define min et max reasonnable values for print scale
31
#define MIN_SCALE 0.01
32
#define MAX_SCALE 100.0
34
// static print data and page setup data, to remember settings during the session
35
static wxPrintData* g_PrintData;
36
static wxPageSetupDialogData* g_pageSetupData = (wxPageSetupDialogData*) NULL;
39
static PRINT_PARAMETERS s_Parameters;
42
/* Dialog to print schematic. Class derived from DIALOG_PRINT_USING_PRINTER_base
43
* created by wxFormBuilder
45
class DIALOG_PRINT_USING_PRINTER : public DIALOG_PRINT_USING_PRINTER_base
48
WinEDA_GerberFrame* m_Parent;
50
wxCheckBox* m_BoxSelectLayer[32];
51
static wxPoint s_LastPos;
52
static wxSize s_LastSize;
55
DIALOG_PRINT_USING_PRINTER( WinEDA_GerberFrame* parent );
56
~DIALOG_PRINT_USING_PRINTER() {};
59
void OnCloseWindow( wxCloseEvent& event );
60
void OnInitDialog( wxInitDialogEvent& event );
61
void OnPageSetup( wxCommandEvent& event );
62
void OnPrintPreview( wxCommandEvent& event );
63
void OnPrintButtonClick( wxCommandEvent& event );
64
void OnScaleSelectionClick( wxCommandEvent& event );
66
void OnButtonCancelClick( wxCommandEvent& event ) { Close(); }
67
void SetPrintParameters( );
70
bool Show( bool show ); // overload stock function
73
bool IsMirrored() { return m_Print_Mirror->IsChecked(); }
74
bool PrintUsingSinglePage() { return m_PagesOption->GetSelection(); }
75
int SetLayerMaskFromListSelection();
78
// We want our dialog to remember its previous screen position
79
wxPoint DIALOG_PRINT_USING_PRINTER::s_LastPos( -1, -1 );
80
wxSize DIALOG_PRINT_USING_PRINTER::s_LastSize;
83
/*******************************************************/
84
void WinEDA_GerberFrame::ToPrinter( wxCommandEvent& event )
85
/*******************************************************/
88
* Display the print dialog
91
if( g_PrintData == NULL ) // First print
93
g_PrintData = new wxPrintData();
95
if( !g_PrintData->Ok() )
97
DisplayError( this, _( "Error Init Printer info" ) );
99
g_PrintData->SetQuality( wxPRINT_QUALITY_HIGH ); // Default resolution = HIGHT;
100
g_PrintData->SetOrientation( DEFAULT_ORIENTATION_PAPER );
103
DIALOG_PRINT_USING_PRINTER* frame = new DIALOG_PRINT_USING_PRINTER( this );
105
frame->ShowModal(); frame->Destroy();
109
/*************************************************************************************/
110
DIALOG_PRINT_USING_PRINTER::DIALOG_PRINT_USING_PRINTER( WinEDA_GerberFrame* parent ) :
111
DIALOG_PRINT_USING_PRINTER_base( parent )
112
/*************************************************************************************/
115
m_Config = wxGetApp().m_EDA_Config;
121
GetSizer()->SetSizeHints( this );
126
/************************************************************************/
127
void DIALOG_PRINT_USING_PRINTER::InitValues( )
128
/************************************************************************/
131
int layer_max = NB_LAYERS;
134
if( g_pageSetupData == NULL )
136
g_pageSetupData = new wxPageSetupDialogData;
137
// Set initial page margins.
138
// Margins are already set in Pcbnew, so we cans use 0
139
g_pageSetupData->SetMarginTopLeft(wxPoint(0, 0));
140
g_pageSetupData->SetMarginBottomRight(wxPoint(0, 0));
143
s_Parameters.m_PageSetupData = g_pageSetupData;
146
/* Create layer list */
148
for( ii = 0; ii < layer_max; ii++, mask <<= 1 )
151
msg << wxT( " " ) << ii + 1;
152
m_BoxSelectLayer[ii] = new wxCheckBox( this, -1, msg );
154
if( mask & s_SelectedLayers )
155
m_BoxSelectLayer[ii]->SetValue( TRUE );
157
m_leftLayersBoxSizer->Add( m_BoxSelectLayer[ii],
158
wxGROW | wxLEFT | wxRIGHT | wxTOP | wxADJUST_MINSIZE );
160
m_rightLayersBoxSizer->Add( m_BoxSelectLayer[ii],
161
wxGROW | wxLEFT | wxRIGHT | wxTOP | wxADJUST_MINSIZE );
165
// Read the scale adjust option
166
int scale_idx = 4; // default selected scale = ScaleList[4] = 1.000
170
m_Config->Read( OPTKEY_PRINT_X_FINESCALE_ADJ, &s_Parameters.m_XScaleAdjust );
171
m_Config->Read( OPTKEY_PRINT_Y_FINESCALE_ADJ, &s_Parameters.m_YScaleAdjust );
172
m_Config->Read( OPTKEY_PRINT_SCALE, &scale_idx );
173
m_Config->Read( OPTKEY_PRINT_PAGE_FRAME, &s_Parameters.m_Print_Sheet_Ref, 1);
174
m_Config->Read( OPTKEY_PRINT_MONOCHROME_MODE, &s_Parameters.m_Print_Black_and_White, 1);
176
// Test for a reasonnable scale value. Set to 1 if problem
177
if( s_Parameters.m_XScaleAdjust < MIN_SCALE ||
178
s_Parameters.m_YScaleAdjust < MIN_SCALE ||
179
s_Parameters.m_XScaleAdjust > MAX_SCALE ||
180
s_Parameters.m_YScaleAdjust > MAX_SCALE )
181
s_Parameters.m_XScaleAdjust = s_Parameters.m_YScaleAdjust = 1.0;
183
s_SelectedLayers = 0;
184
for( int layer = 0; layer<layer_max; ++layer )
189
layerKey.Printf( OPTKEY_LAYERBASE, layer );
192
if( m_Config->Read( layerKey, &option ) )
194
m_BoxSelectLayer[layer]->SetValue( option );
196
s_SelectedLayers |= 1 << layer;
201
// Disable checkboxes if the corresponding layer is not enabled
202
BOARD* board = ((WinEDA_BasePcbFrame*)m_Parent)->GetBoard();
203
for( int layer = 0; layer<NB_LAYERS; layer++, mask <<= 1 )
205
if( ! board->IsLayerEnabled( layer ) )
207
m_BoxSelectLayer[layer]->Enable( false );
208
m_BoxSelectLayer[layer]->SetValue( false );
212
m_ScaleOption->SetSelection( scale_idx );
213
scale_idx = m_ScaleOption->GetSelection();
214
s_Parameters.m_PrintScale = s_ScaleList[scale_idx];
215
m_Print_Mirror->SetValue(s_Parameters.m_PrintMirror);
218
if( s_Parameters.m_Print_Black_and_White )
219
m_ModeColorOption->SetSelection( 1 );
221
m_ModeColorOption->SetSelection( 0 );
223
s_Parameters.m_PenDefaultSize = 0;
225
// Create scale adjust option
226
msg.Printf( wxT( "%f" ), s_Parameters.m_XScaleAdjust );
227
m_FineAdjustXscaleOpt->SetValue( msg );
228
msg.Printf( wxT( "%f" ), s_Parameters.m_YScaleAdjust );
229
m_FineAdjustYscaleOpt->SetValue( msg );
231
bool enable = (s_Parameters.m_PrintScale == 1.0);
232
if( m_FineAdjustXscaleOpt )
233
m_FineAdjustXscaleOpt->Enable(enable);
234
if( m_FineAdjustYscaleOpt )
235
m_FineAdjustYscaleOpt->Enable(enable);
238
/*************************************************/
239
bool DIALOG_PRINT_USING_PRINTER::Show( bool show )
240
/*************************************************/
246
if( s_LastPos.x != -1 )
248
SetSize( s_LastPos.x, s_LastPos.y, s_LastSize.x, s_LastSize.y, 0 );
250
ret = DIALOG_PRINT_USING_PRINTER_base::Show( show );
254
// Save the dialog's position before hiding
255
s_LastPos = GetPosition();
256
s_LastSize = GetSize();
258
ret = DIALOG_PRINT_USING_PRINTER_base::Show( show );
264
/**************************************************************/
265
int DIALOG_PRINT_USING_PRINTER::SetLayerMaskFromListSelection()
266
/**************************************************************/
269
s_Parameters.m_PrintMaskLayer = 0;
271
for( ii = 0, page_count = 0; ii < LAYER_COUNT; ii++ )
273
if( m_BoxSelectLayer[ii]->IsChecked() )
276
s_Parameters.m_PrintMaskLayer |= 1 << ii;
280
s_Parameters.m_PageCount = page_count;
286
/********************************************************************/
287
void DIALOG_PRINT_USING_PRINTER::OnCloseWindow( wxCloseEvent& event )
288
/********************************************************************/
290
SetPrintParameters();
294
m_Config->Write( OPTKEY_PRINT_X_FINESCALE_ADJ, s_Parameters.m_XScaleAdjust );
295
m_Config->Write( OPTKEY_PRINT_Y_FINESCALE_ADJ, s_Parameters.m_YScaleAdjust );
296
m_Config->Write( OPTKEY_PRINT_SCALE, m_ScaleOption->GetSelection() );
297
m_Config->Write( OPTKEY_PRINT_PAGE_FRAME, s_Parameters.m_Print_Sheet_Ref);
298
m_Config->Write( OPTKEY_PRINT_MONOCHROME_MODE, s_Parameters.m_Print_Black_and_White);
300
for( int layer = 0; layer < LAYER_COUNT; ++layer )
302
layerKey.Printf( OPTKEY_LAYERBASE, layer );
303
m_Config->Write( layerKey, m_BoxSelectLayer[layer]->IsChecked() );
310
/******************************************************************/
311
void DIALOG_PRINT_USING_PRINTER::SetPrintParameters( )
312
/******************************************************************/
314
s_Parameters.m_PrintMirror = m_Print_Mirror->GetValue();
315
s_Parameters.m_Print_Black_and_White =
316
m_ModeColorOption->GetSelection() != 0;
319
s_Parameters.m_OptionPrintPage = m_PagesOption->GetSelection() != 0;
322
SetLayerMaskFromListSelection();
324
int idx = m_ScaleOption->GetSelection();
325
s_Parameters.m_PrintScale = s_ScaleList[idx];
326
g_pcb_plot_options.Scale = s_Parameters.m_PrintScale;
328
if( m_FineAdjustXscaleOpt )
330
if( s_Parameters.m_XScaleAdjust > MAX_SCALE ||
331
s_Parameters.m_YScaleAdjust > MAX_SCALE )
332
DisplayInfoMessage( NULL, _( "Warning: Scale option set to a very large value" ) );
333
m_FineAdjustXscaleOpt->GetValue().ToDouble( &s_Parameters.m_XScaleAdjust );
335
if( m_FineAdjustYscaleOpt )
337
// Test for a reasonnable scale value
338
if( s_Parameters.m_XScaleAdjust < MIN_SCALE ||
339
s_Parameters.m_YScaleAdjust < MIN_SCALE )
340
DisplayInfoMessage( NULL, _( "Warning: Scale option set to a very small value" ) );
341
m_FineAdjustYscaleOpt->GetValue().ToDouble( &s_Parameters.m_YScaleAdjust );
343
g_pcb_plot_options.ScaleAdjX = s_Parameters.m_XScaleAdjust;
344
g_pcb_plot_options.ScaleAdjX = s_Parameters.m_YScaleAdjust;
347
void DIALOG_PRINT_USING_PRINTER::OnScaleSelectionClick( wxCommandEvent& event )
349
double scale = s_ScaleList[m_ScaleOption->GetSelection()];
350
bool enable = (scale == 1.0);
351
if( m_FineAdjustXscaleOpt )
352
m_FineAdjustXscaleOpt->Enable(enable);
353
if( m_FineAdjustYscaleOpt )
354
m_FineAdjustYscaleOpt->Enable(enable);
357
/**********************************************************/
358
void DIALOG_PRINT_USING_PRINTER::OnPageSetup( wxCommandEvent& event )
359
/**********************************************************/
361
/* Open a dialog box for printer setup (printer options, page size ...)
364
*g_pageSetupData = *g_PrintData;
366
wxPageSetupDialog pageSetupDialog(this, g_pageSetupData);
367
pageSetupDialog.ShowModal();
369
(*g_PrintData) = pageSetupDialog.GetPageSetupDialogData().GetPrintData();
370
(*g_pageSetupData) = pageSetupDialog.GetPageSetupDialogData();
374
/************************************************************/
375
void DIALOG_PRINT_USING_PRINTER::OnPrintPreview( wxCommandEvent& event )
376
/************************************************************/
378
/* Open and display a previewer frame for printing
381
SetPrintParameters( );
383
// Pass two printout objects: for preview, and possible printing.
384
wxString title = _( "Print Preview" );
385
wxPrintPreview* preview =
386
new wxPrintPreview( new BOARD_PRINTOUT_CONTROLER( s_Parameters, m_Parent, title ),
387
new BOARD_PRINTOUT_CONTROLER( s_Parameters, m_Parent, title ),
390
if( preview == NULL )
392
DisplayError( this, wxT( "OnPrintPreview() problem" ) );
396
SetLayerMaskFromListSelection();
398
// If no layer selected, we have no plot. prompt user if it happens
399
// because he could think there is a bug in pcbnew:
400
if( s_Parameters.m_PrintMaskLayer == 0 )
402
DisplayError( this, _( "No layer selected" ) );
407
// Uses the parent position and size.
408
// @todo uses last position and size ans store them when exit in m_Config
409
wxPoint WPos = m_Parent->GetPosition();
410
wxSize WSize = m_Parent->GetSize();
412
wxPreviewFrame* frame = new wxPreviewFrame( preview, this, title, WPos, WSize );
419
/***************************************************************************/
420
void DIALOG_PRINT_USING_PRINTER::OnPrintButtonClick( wxCommandEvent& event )
421
/***************************************************************************/
423
/* Called on activate Print button
426
SetPrintParameters( );
428
// If no layer selected, we have no plot. prompt user if it happens
429
// because he could think there is a bug in pcbnew:
430
if( s_Parameters.m_PrintMaskLayer == 0 )
432
DisplayError( this, _( "No layer selected" ) );
436
wxPrintDialogData printDialogData( *g_PrintData );
438
wxPrinter printer( &printDialogData );
440
wxString title = _( "Print" );
441
BOARD_PRINTOUT_CONTROLER printout( s_Parameters, m_Parent, title );
443
#if !defined(__WINDOWS__) && !wxCHECK_VERSION(2,9,0)
444
wxDC* dc = printout.GetDC();
445
( (wxPostScriptDC*) dc )->SetResolution( 600 ); // Postscript DC resolution is 600 ppi
448
if( !printer.Print( this, &printout, TRUE ) )
450
if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
451
DisplayError( this, _( "There was a problem printing" ) );
456
*g_PrintData = printer.GetPrintDialogData().GetPrintData();