dialog_print_using_printer.cpp 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2010-2014 Jean-Pierre Charras  jp.charras at wanadoo.fr
 * Copyright (C) 1992-2014 KiCad Developers, see change_log.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
 */
24 25 26 27 28

// Set this to 1 if you want to test PostScript printing under MSW.
#define wxTEST_POSTSCRIPT_IN_MSW 1

#include <fctsys.h>
29

30
#include <kiface_i.h>
31 32 33 34 35 36 37 38
#include <common.h>
#include <class_drawpanel.h>
#include <confirm.h>

#include <dialog_print_using_printer_base.h>
#include <printout_controler.h>

#include <gerbview.h>
39
#include <gerbview_frame.h>
40 41 42 43 44 45 46 47 48 49
#include <pcbplot.h>

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

// static print data and page setup data, to remember settings during the session
50 51
static wxPrintData* s_printData;
static wxPageSetupDialogData* s_pageSetupData = (wxPageSetupDialogData*) NULL;
52 53 54 55 56

// Variables locales
static PRINT_PARAMETERS  s_Parameters;


57
/* Dialog to print schematic. Class derived from DIALOG_PRINT_USING_PRINTER_BASE
58 59
 *  created by wxFormBuilder
 */
60
class DIALOG_PRINT_USING_PRINTER : public DIALOG_PRINT_USING_PRINTER_BASE
61 62 63
{
private:
    GERBVIEW_FRAME* m_Parent;
64 65
    wxConfigBase*   m_Config;
    wxCheckBox*     m_BoxSelectLayer[32];
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

public:
    DIALOG_PRINT_USING_PRINTER( GERBVIEW_FRAME* parent );
    ~DIALOG_PRINT_USING_PRINTER() {};

private:
    void OnCloseWindow( wxCloseEvent& event );
    void OnInitDialog( wxInitDialogEvent& event );
    void OnPageSetup( wxCommandEvent& event );
    void OnPrintPreview( wxCommandEvent& event );
    void OnPrintButtonClick( wxCommandEvent& event );
    void OnScaleSelectionClick( wxCommandEvent& event );

    void OnButtonCancelClick( wxCommandEvent& event ) { Close(); }
    void SetPrintParameters( );
    void InitValues( );

public:
    bool IsMirrored() { return m_Print_Mirror->IsChecked(); }
    bool PrintUsingSinglePage() { return true; }
86 87 88 89
    int  SetLayerSetFromListSelection();
    // Prepare print parameters. return true if OK,
    // false if there is an issue (mainly no printable layers)
    bool PreparePrintPrms();
90 91 92 93 94 95 96 97 98 99 100
};


/*******************************************************/
void GERBVIEW_FRAME::ToPrinter( wxCommandEvent& event )
/*******************************************************/

/* Virtual function:
 * Display the print dialog
 */
{
101 102
    if( s_printData == NULL )  // First print
        s_printData = new wxPrintData();
103

104
    if( !s_printData->Ok() )
105 106 107
    {
        DisplayError( this, _( "Error Init Printer info" ) );
        return;
108 109
    }

110 111
    s_printData->SetQuality( wxPRINT_QUALITY_HIGH );
    s_printData->SetOrientation( GetPageSettings().IsPortrait() ?
112
                                 wxPORTRAIT : wxLANDSCAPE );
113 114 115 116 117 118 119 120 121 122

    DIALOG_PRINT_USING_PRINTER* frame = new DIALOG_PRINT_USING_PRINTER( this );

    frame->ShowModal();
    frame->Destroy();
}


/*************************************************************************************/
DIALOG_PRINT_USING_PRINTER::DIALOG_PRINT_USING_PRINTER( GERBVIEW_FRAME* parent ) :
123
    DIALOG_PRINT_USING_PRINTER_BASE( parent )
124 125 126
/*************************************************************************************/
{
    m_Parent = parent;
127
    m_Config = Kiface().KifaceSettings();
128 129

    InitValues( );
130
    GetSizer()->SetSizeHints( this );
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

#ifdef __WXMAC__
    /* Problems with modal on wx-2.9 - Anyway preview is standard for OSX */
   m_buttonPreview->Hide();
#endif
}


/************************************************************************/
void DIALOG_PRINT_USING_PRINTER::InitValues( )
/************************************************************************/
{
    SetFocus();
    wxString msg;

146
    if( s_pageSetupData == NULL )
147
    {
148
        s_pageSetupData = new wxPageSetupDialogData;
149
        // Set initial page margins.
150 151 152
        // Margins are already set in Gerbview, so we can use 0
        s_pageSetupData->SetMarginTopLeft(wxPoint(0, 0));
        s_pageSetupData->SetMarginBottomRight(wxPoint(0, 0));
153 154
    }

155
    s_Parameters.m_PageSetupData = s_pageSetupData;
156

157
    // Create layer list
158
    for( int ii = 0; ii < GERBER_DRAWLAYERS_COUNT; ++ii )
159 160 161 162 163
    {
        msg = _( "Layer" );
        msg << wxT( " " ) << ii + 1;
        m_BoxSelectLayer[ii] = new wxCheckBox( this, -1, msg );

164 165 166
        if( g_GERBER_List[ii] == NULL )     // Nothing loaded on this draw layer
            m_BoxSelectLayer[ii]->Enable( false );

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
        if( ii < 16 )
            m_leftLayersBoxSizer->Add( m_BoxSelectLayer[ii],
                                         wxGROW | wxLEFT | wxRIGHT | wxTOP );
        else
            m_rightLayersBoxSizer->Add( m_BoxSelectLayer[ii],
                                            wxGROW | wxLEFT | wxRIGHT | wxTOP );
    }


    // Read the scale adjust option
    int scale_idx = 4; // default selected scale = ScaleList[4] = 1.000

    if( m_Config )
    {
        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);

        // 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;

194
        for( int layer = 0; layer < GERBER_DRAWLAYERS_COUNT; ++layer )
195 196 197 198 199
        {
            wxString layerKey;
            bool     option;

            layerKey.Printf( OPTKEY_LAYERBASE, layer );
200 201
            m_Config->Read( layerKey, &option, false );
            m_BoxSelectLayer[layer]->SetValue( option );
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
        }
    }

    m_ScaleOption->SetSelection( scale_idx );
    scale_idx = m_ScaleOption->GetSelection();
    s_Parameters.m_PrintScale =  s_ScaleList[scale_idx];
    m_Print_Mirror->SetValue(s_Parameters.m_PrintMirror);


    if( s_Parameters.m_Print_Black_and_White )
        m_ModeColorOption->SetSelection( 1 );
    else
        m_ModeColorOption->SetSelection( 0 );

    s_Parameters.m_PenDefaultSize = 0;

    // Create scale adjust option
    msg.Printf( wxT( "%f" ), s_Parameters.m_XScaleAdjust );
    m_FineAdjustXscaleOpt->SetValue( msg );
    msg.Printf( wxT( "%f" ), s_Parameters.m_YScaleAdjust );
    m_FineAdjustYscaleOpt->SetValue( msg );

    bool enable = (s_Parameters.m_PrintScale == 1.0);
    if( m_FineAdjustXscaleOpt )
        m_FineAdjustXscaleOpt->Enable(enable);
    if( m_FineAdjustYscaleOpt )
        m_FineAdjustYscaleOpt->Enable(enable);
}

Dick Hollenbeck's avatar
Dick Hollenbeck committed
231
int DIALOG_PRINT_USING_PRINTER::SetLayerSetFromListSelection()
232
{
233
    int page_count = 0;
234 235
    std::bitset <GERBER_DRAWLAYERS_COUNT> layerMask;
    for( int ii = 0; ii < GERBER_DRAWLAYERS_COUNT; ++ii )
236
    {
237
        if( m_BoxSelectLayer[ii]->IsChecked() && m_BoxSelectLayer[ii]->IsEnabled() )
238 239
        {
            page_count++;
240
            layerMask[ii] = true;
241
        }
242 243
        else
            layerMask[ii] = false;
244 245
    }

246
    m_Parent->GetGerberLayout()->SetPrintableLayers( layerMask );
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    s_Parameters.m_PageCount = page_count;

    return page_count;
}


void DIALOG_PRINT_USING_PRINTER::OnCloseWindow( wxCloseEvent& event )
{
    SetPrintParameters();

    if( m_Config )
    {
        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);
        wxString layerKey;
265
        for( int layer = 0; layer < GERBER_DRAWLAYERS_COUNT; ++layer )
266 267 268 269 270
        {
            layerKey.Printf( OPTKEY_LAYERBASE, layer );
            m_Config->Write( layerKey, m_BoxSelectLayer[layer]->IsChecked() );
        }
    }
271

272 273 274 275 276 277 278 279 280 281 282 283 284 285
    EndModal( 0 );
}


void DIALOG_PRINT_USING_PRINTER::SetPrintParameters( )
{
    s_Parameters.m_PrintMirror = m_Print_Mirror->GetValue();
    s_Parameters.m_Print_Black_and_White =
        m_ModeColorOption->GetSelection() != 0;

    // Due to negative objects in gerber objects, always use one page per image,
    // because these objects create artefact when they are printed on an existing image.
    s_Parameters.m_OptionPrintPage = false;

Dick Hollenbeck's avatar
Dick Hollenbeck committed
286
    SetLayerSetFromListSelection();
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

    int idx = m_ScaleOption->GetSelection();
    s_Parameters.m_PrintScale =  s_ScaleList[idx];

    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 );
    }
    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 );
    }
}

void DIALOG_PRINT_USING_PRINTER::OnScaleSelectionClick( wxCommandEvent& event )
{
    double scale = s_ScaleList[m_ScaleOption->GetSelection()];
    bool enable = (scale == 1.0);
    if( m_FineAdjustXscaleOpt )
        m_FineAdjustXscaleOpt->Enable(enable);
    if( m_FineAdjustYscaleOpt )
        m_FineAdjustYscaleOpt->Enable(enable);
}

318
// Open a dialog box for printer setup (printer options, page size ...)
319 320
void DIALOG_PRINT_USING_PRINTER::OnPageSetup( wxCommandEvent& event )
{
321
    *s_pageSetupData = *s_printData;
322

323
    wxPageSetupDialog pageSetupDialog(this, s_pageSetupData);
324 325
    pageSetupDialog.ShowModal();

326 327
    (*s_printData) = pageSetupDialog.GetPageSetupDialogData().GetPrintData();
    (*s_pageSetupData) = pageSetupDialog.GetPageSetupDialogData();
328 329
}

330 331 332
bool DIALOG_PRINT_USING_PRINTER::PreparePrintPrms()
{
    SetPrintParameters( );
333

334 335 336 337 338 339 340
    // If no layer selected, we have no plot. prompt user if it happens
    // because he could think there is a bug in Pcbnew:
    if( m_Parent->GetGerberLayout()->GetPrintableLayers().none() )
    {
        DisplayError( this, _( "No layer selected" ) );
        return false;
    }
341

342 343 344 345 346
    return true;
}

// Open and display a previewer frame for printing
void DIALOG_PRINT_USING_PRINTER::OnPrintPreview( wxCommandEvent& event )
347
{
348 349
    if( !PreparePrintPrms() )
        return;
350 351 352 353

    // Pass two printout objects: for preview, and possible printing.
    wxString        title   = _( "Print Preview" );
    wxPrintPreview* preview =
354 355
        new wxPrintPreview( new BOARD_PRINTOUT_CONTROLLER( s_Parameters, m_Parent, title ),
                            new BOARD_PRINTOUT_CONTROLLER( s_Parameters, m_Parent, title ),
356
                            s_printData );
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

    if( preview == NULL )
    {
        DisplayError( this, wxT( "OnPrintPreview() problem" ) );
        return;
    }


    // 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();

    wxPreviewFrame* frame = new wxPreviewFrame( preview, this, title, WPos, WSize );

    frame->Initialize();
    frame->Show( true );
}


void DIALOG_PRINT_USING_PRINTER::OnPrintButtonClick( wxCommandEvent& event )
{
379
    if( !PreparePrintPrms() )
380 381
        return;

382
    wxPrintDialogData printDialogData( *s_printData );
383

384 385 386
    wxPrinter printer( &printDialogData );
    wxString title = _( "Print" );
    BOARD_PRINTOUT_CONTROLLER printout( s_Parameters, m_Parent, title );
387 388 389 390 391 392 393 394 395 396 397 398 399 400

#if !defined(__WINDOWS__) && !wxCHECK_VERSION(2,9,0)
    wxDC*             dc = printout.GetDC();
    ( (wxPostScriptDC*) dc )->SetResolution( 600 );  // Postscript DC resolution is 600 ppi
#endif

    if( !printer.Print( this, &printout, true ) )
    {
        if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
            DisplayError( this, _( "There was a problem printing" ) );
        return;
    }
    else
    {
401
        *s_printData = printer.GetPrintDialogData().GetPrintData();
402 403 404
    }
}