1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2015 KiCad Developers, see CHANGELOG.TXT for contributors.
*
* 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 <fctsys.h>
#include <pgm_base.h>
#include <gr_basic.h>
#include <class_drawpanel.h>
#include <confirm.h>
#include <class_sch_screen.h>
#include <schframe.h>
#include <base_units.h>
#include <general.h>
#include <eeschema_config.h>
#include <sch_sheet.h>
#include <sch_sheet_path.h>
#include <invoke_sch_dialog.h>
#include <dialog_print_using_printer_base.h>
/**
* Class DIALOG_PRINT_USING_PRINTER
* offers to print a schematic dialog.
*
* Derived from DIALOG_PRINT_USING_PRINTER_base created by wxFormBuilder
*/
class DIALOG_PRINT_USING_PRINTER : public DIALOG_PRINT_USING_PRINTER_BASE
{
public:
DIALOG_PRINT_USING_PRINTER( SCH_EDIT_FRAME* aParent );
SCH_EDIT_FRAME* GetParent() const
{
return ( SCH_EDIT_FRAME* ) wxWindow::GetParent();
}
private:
void OnCloseWindow( wxCloseEvent& event );
void OnPageSetup( wxCommandEvent& event );
void OnPrintPreview( wxCommandEvent& event );
void OnPrintButtonClick( wxCommandEvent& event );
void OnButtonCancelClick( wxCommandEvent& event ) { Close(); }
void initDialog();
void GetPrintOptions();
};
/**
* Custom print out for printing schematics.
*/
class SCH_PRINTOUT : public wxPrintout
{
private:
SCH_EDIT_FRAME* m_parent;
public:
SCH_PRINTOUT( SCH_EDIT_FRAME* aParent, const wxString& aTitle ) :
wxPrintout( aTitle )
{
wxASSERT( aParent != NULL );
m_parent = aParent;
}
bool OnPrintPage( int page );
bool HasPage( int page );
bool OnBeginDocument( int startPage, int endPage );
void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo );
void DrawPage( SCH_SCREEN* aScreen );
};
/**
* Custom schematic print preview frame.
* This derived preview frame remembers its size and position during a session
*/
class SCH_PREVIEW_FRAME : public wxPreviewFrame
{
public:
SCH_PREVIEW_FRAME( wxPrintPreview* aPreview, wxWindow* aParent,
const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
const wxSize& aSize = wxDefaultSize ) :
wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
{
}
bool Show( bool show ) // overload
{
bool ret;
// Show or hide the window. If hiding, save current position and size.
// If showing, use previous position and size.
if( show )
{
ret = wxPreviewFrame::Show( show );
if( s_size.x != 0 && s_size.y != 0 )
SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
}
else
{
// Save the dialog's position & size before hiding
s_size = GetSize();
s_pos = GetPosition();
ret = wxPreviewFrame::Show( show );
}
return ret;
}
private:
static wxPoint s_pos;
static wxSize s_size;
};
wxPoint SCH_PREVIEW_FRAME::s_pos;
wxSize SCH_PREVIEW_FRAME::s_size;
DIALOG_PRINT_USING_PRINTER::DIALOG_PRINT_USING_PRINTER( SCH_EDIT_FRAME* aParent ) :
DIALOG_PRINT_USING_PRINTER_BASE( aParent )
{
wxASSERT( aParent != NULL );
m_checkReference->SetValue( aParent->GetPrintSheetReference() );
m_checkMonochrome->SetValue( aParent->GetPrintMonochrome() );
initDialog();
#ifdef __WXMAC__
// Problems with modal on wx-2.9 - Anyway preview is standard for OSX
m_buttonPreview->Hide();
#endif
GetSizer()->Fit( this );
}
void DIALOG_PRINT_USING_PRINTER::initDialog()
{
SCH_EDIT_FRAME* parent = GetParent();
// Initialize page specific print setup dialog settings.
const PAGE_INFO& pageInfo = parent->GetScreen()->GetPageSettings();
wxPageSetupDialogData& pageSetupDialogData = parent->GetPageSetupData();
pageSetupDialogData.SetPaperId( pageInfo.GetPaperId() );
if( pageInfo.IsCustom() )
{
if( pageInfo.IsPortrait() )
pageSetupDialogData.SetPaperSize( wxSize( Mils2mm( pageInfo.GetWidthMils() ),
Mils2mm( pageInfo.GetHeightMils() ) ) );
else
pageSetupDialogData.SetPaperSize( wxSize( Mils2mm( pageInfo.GetHeightMils() ),
Mils2mm( pageInfo.GetWidthMils() ) ) );
}
pageSetupDialogData.GetPrintData().SetOrientation( pageInfo.GetWxOrientation() );
if ( GetSizer() )
GetSizer()->SetSizeHints( this );
// Rely on the policy in class DIALOG_SHIM, which centers the dialog
// initially during a runtime session but gives user the ability to move it in
// that session.
// This dialog may get moved and resized in Show(), but in case this is
// the first time, center it for starters.
Center();
m_buttonPrint->SetDefault(); // on linux, this is inadequate to determine
// what ENTER does. Must also SetFocus().
m_buttonPrint->SetFocus();
}
void DIALOG_PRINT_USING_PRINTER::GetPrintOptions()
{
SCH_EDIT_FRAME* parent = GetParent();
parent->SetPrintMonochrome( m_checkMonochrome->IsChecked() );
parent->SetPrintSheetReference( m_checkReference->IsChecked() );
}
void DIALOG_PRINT_USING_PRINTER::OnCloseWindow( wxCloseEvent& event )
{
SCH_EDIT_FRAME* parent = GetParent();
if( !IsIconized() )
{
parent->SetPrintDialogPosition( GetPosition() );
parent->SetPrintDialogSize( GetSize() );
}
GetPrintOptions();
EndDialog( wxID_CANCEL );
}
/* Open a dialog box for printer setup (printer options, page size ...)
*/
void DIALOG_PRINT_USING_PRINTER::OnPageSetup( wxCommandEvent& event )
{
SCH_EDIT_FRAME* parent = GetParent();
wxPageSetupDialog pageSetupDialog( this, &parent->GetPageSetupData() );
pageSetupDialog.ShowModal();
parent->GetPageSetupData() = pageSetupDialog.GetPageSetupDialogData();
}
/* Open and display a previewer frame for printing
*/
void DIALOG_PRINT_USING_PRINTER::OnPrintPreview( wxCommandEvent& event )
{
SCH_EDIT_FRAME* parent = GetParent();
GetPrintOptions();
// Pass two printout objects: for preview, and possible printing.
wxString title = _( "Preview" );
wxPrintPreview* preview = new wxPrintPreview( new SCH_PRINTOUT( parent, title ),
new SCH_PRINTOUT( parent, title ),
&parent->GetPageSetupData().GetPrintData() );
if( preview == NULL )
{
DisplayError( this, wxT( "Print preview error!" ) );
return;
}
preview->SetZoom( 100 );
SCH_PREVIEW_FRAME* frame = new SCH_PREVIEW_FRAME( preview, this, title );
frame->SetMinSize( wxSize( 550, 350 ) );
// on first invocation in this runtime session, set to 2/3 size of my parent,
// but will be changed in Show() if not first time as will position.
frame->SetSize( (parent->GetSize() * 2) / 3 );
frame->Center();
// On wxGTK, set the flag wxTOPLEVEL_EX_DIALOG is mandatory, if we want
// close the frame using the X box in caption, when the preview frame is run
// from a dialog
frame->SetExtraStyle( frame->GetExtraStyle() | wxTOPLEVEL_EX_DIALOG );
// We use here wxPreviewFrame_WindowModal option to make the wxPrintPreview frame
// modal for its caller only.
// An other reason is the fact when closing the frame without this option,
// all top level frames are reenabled.
// With this option, only the parent is reenabled.
// Reenabling all top level frames should be made by the parent dialog.
frame->InitializeWithModality( wxPreviewFrame_WindowModal );
frame->Raise(); // Needed on Ubuntu/Unity to display the frame
frame->Show( true );
}
void DIALOG_PRINT_USING_PRINTER::OnPrintButtonClick( wxCommandEvent& event )
{
SCH_EDIT_FRAME* parent = GetParent();
GetPrintOptions();
wxPrintDialogData printDialogData( parent->GetPageSetupData().GetPrintData() );
printDialogData.SetMaxPage( g_RootSheet->CountSheets() );
if( g_RootSheet->CountSheets() > 1 )
printDialogData.EnablePageNumbers( true );
wxPrinter printer( &printDialogData );
SCH_PRINTOUT printout( parent, _( "Print Schematic" ) );
if( !printer.Print( this, &printout, true ) )
{
if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
wxMessageBox( _( "An error occurred attempting to print the schematic." ),
_( "Printing" ), wxOK );
}
else
{
parent->GetPageSetupData() = printer.GetPrintDialogData().GetPrintData();
}
}
bool SCH_PRINTOUT::OnPrintPage( int page )
{
wxString msg;
msg.Printf( _( "Print page %d" ), page );
m_parent->ClearMsgPanel();
m_parent->AppendMsgPanel( msg, wxEmptyString, CYAN );
SCH_SCREEN* screen = m_parent->GetScreen();
SCH_SHEET_PATH oldsheetpath = m_parent->GetCurrentSheet();
SCH_SHEET_PATH list;
SCH_SHEET_LIST SheetList( NULL );
SCH_SHEET_PATH* sheetpath = SheetList.GetSheet( page - 1 );
if( list.BuildSheetPathInfoFromSheetPathValue( sheetpath->Path() ) )
{
m_parent->SetCurrentSheet( list );
m_parent->GetCurrentSheet().UpdateAllScreenReferences();
m_parent->SetSheetNumberAndCount();
screen = m_parent->GetCurrentSheet().LastScreen();
}
else
{
screen = NULL;
}
if( screen == NULL )
return false;
DrawPage( screen );
m_parent->SetCurrentSheet( oldsheetpath );
m_parent->GetCurrentSheet().UpdateAllScreenReferences();
m_parent->SetSheetNumberAndCount();
return true;
}
void SCH_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
{
*minPage = *selPageFrom = 1;
*maxPage = *selPageTo = g_RootSheet->CountSheets();
}
bool SCH_PRINTOUT::HasPage( int pageNum )
{
int pageCount;
pageCount = g_RootSheet->CountSheets();
if( pageCount >= pageNum )
return true;
return false;
}
bool SCH_PRINTOUT::OnBeginDocument( int startPage, int endPage )
{
if( !wxPrintout::OnBeginDocument( startPage, endPage ) )
return false;
#ifdef __WXDEBUG__
wxLogDebug( wxT( "Printer name: " ) +
m_parent->GetPageSetupData().GetPrintData().GetPrinterName() );
wxLogDebug( wxT( "Paper ID: %d" ),
m_parent->GetPageSetupData().GetPrintData().GetPaperId() );
wxLogDebug( wxT( "Color: %d" ),
(int)m_parent->GetPageSetupData().GetPrintData().GetColour() );
wxLogDebug( wxT( "Monochrome: %d" ), m_parent->GetPrintMonochrome() );
wxLogDebug( wxT( "Orientation: %d:" ),
m_parent->GetPageSetupData().GetPrintData().GetOrientation() );
wxLogDebug( wxT( "Quality: %d"),
m_parent->GetPageSetupData().GetPrintData().GetQuality() );
#endif
return true;
}
/*
* This is the real print function: print the active screen
*/
void SCH_PRINTOUT::DrawPage( SCH_SCREEN* aScreen )
{
int oldZoom;
wxPoint tmp_startvisu;
wxSize pageSizeIU; // Page size in internal units
wxPoint old_org;
EDA_RECT oldClipBox;
wxRect fitRect;
wxDC* dc = GetDC();
EDA_DRAW_PANEL* panel = m_parent->GetCanvas();
wxBusyCursor dummy;
// Save current scale factor, offsets, and clip box.
tmp_startvisu = aScreen->m_StartVisu;
oldZoom = aScreen->GetZoom();
old_org = aScreen->m_DrawOrg;
oldClipBox = *panel->GetClipBox();
// Change clip box to print the whole page.
#define MAX_VALUE (INT_MAX/2) // MAX_VALUE is the max we can use in an integer
// and that allows calculations without overflow
panel->SetClipBox( EDA_RECT( wxPoint( 0, 0 ), wxSize( MAX_VALUE, MAX_VALUE ) ) );
// Change scale factor and offset to print the whole page.
bool printReference = m_parent->GetPrintSheetReference();
pageSizeIU = aScreen->GetPageSettings().GetSizeIU();
FitThisSizeToPaper( pageSizeIU );
fitRect = GetLogicalPaperRect();
wxLogDebug( wxT( "Fit rectangle: x = %d, y = %d, w = %d, h = %d" ),
fitRect.x, fitRect.y, fitRect.width, fitRect.height );
// When is the actual paper size does not match the schematic page
// size, the drawing is not perfectly centered on X or Y axis.
// Give a draw offset centers the schematic page on the paper draw area
// Because the sizes are fitted, only an Y or X offset is needed
// and both are 0 when sizes are identical.
// Y or Y offset is not null when the X/Y size ratio differs between
// the actual paper size and the schematic page
int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
// For an obscure reason, OffsetLogicalOrigin creates issues,
// under some circumstances, when yoffset is not always null
// and changes from a page to an other page
// This is only a workaround, not a fix
// see https://bugs.launchpad.net/kicad/+bug/1464773
// xoffset does not create issues.
#if 0 // FIX ME
int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
#else
// the Y centering will be not perfect, but this is less annoying
// than a blank page or a buggy centering
int yoffset = 0;
#endif
OffsetLogicalOrigin( xoffset, yoffset );
GRResetPenAndBrush( dc );
if( m_parent->GetPrintMonochrome() )
GRForceBlackPen( true );
aScreen->m_IsPrinting = true;
EDA_COLOR_T bg_color = m_parent->GetDrawBgColor();
aScreen->Draw( panel, dc, (GR_DRAWMODE) 0 );
if( printReference )
m_parent->DrawWorkSheet( dc, aScreen, GetDefaultLineThickness(),
IU_PER_MILS, aScreen->GetFileName() );
m_parent->SetDrawBgColor( bg_color );
aScreen->m_IsPrinting = false;
panel->SetClipBox( oldClipBox );
GRForceBlackPen( false );
aScreen->m_StartVisu = tmp_startvisu;
aScreen->m_DrawOrg = old_org;
aScreen->SetZoom( oldZoom );
}
int InvokeDialogPrintUsingPrinter( SCH_EDIT_FRAME* aCaller )
{
DIALOG_PRINT_USING_PRINTER dlg( aCaller );
return dlg.ShowModal();
}
|