~cern-kicad/kicad/kicad-pns-tom

« back to all changes in this revision

Viewing changes to pagelayout_editor/dialogs/dialogs_for_printing.cpp

  • Committer: Maciej Suminski
  • Date: 2013-08-02 13:57:24 UTC
  • mfrom: (4024.1.238 kicad)
  • mto: This revision was merged to the branch mainline in revision 4221.
  • Revision ID: maciej.suminski@cern.ch-20130802135724-gix6orezshkukodv
Upstream merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file dialogs_for_printing.cpp
 
3
 */
 
4
 
 
5
/* Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
 
6
 * Copyright (C) 1992-2013 KiCad Developers, see AUTHORS.txt for contributors.
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License
 
10
 * as published by the Free Software Foundation; either version 2
 
11
 * of the License, or (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, you may find one here:
 
20
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 
21
 * or you may search the http://www.gnu.org website for the version 2 license,
 
22
 * or you may write to the Free Software Foundation, Inc.,
 
23
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 
24
 */
 
25
 
 
26
#include <fctsys.h>
 
27
#include <gr_basic.h>
 
28
#include <class_drawpanel.h>
 
29
#include <base_units.h>
 
30
 
 
31
#include <pl_editor_frame.h>
 
32
#include <pl_editor_id.h>
 
33
#include <dialog_helpers.h>
 
34
#include <worksheet_shape_builder.h>
 
35
#include <class_worksheet_dataitem.h>
 
36
#include <dialog_page_settings.h>
 
37
#include <invoke_pl_editor_dialog.h>
 
38
 
 
39
/**
 
40
 * Custom print out for printing schematics.
 
41
 */
 
42
class PLEDITOR_PRINTOUT : public wxPrintout
 
43
{
 
44
private:
 
45
    PL_EDITOR_FRAME* m_parent;
 
46
 
 
47
public:
 
48
    PLEDITOR_PRINTOUT( PL_EDITOR_FRAME* aParent, const wxString& aTitle ) :
 
49
        wxPrintout( aTitle )
 
50
    {
 
51
        wxASSERT( aParent != NULL );
 
52
        m_parent = aParent;
 
53
    }
 
54
 
 
55
    bool OnPrintPage( int aPageNum );
 
56
    bool HasPage( int aPageNum ) { return ( aPageNum <= 2 ); }
 
57
    void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo );
 
58
    void DrawPage( int aPageNum );
 
59
};
 
60
 
 
61
/**
 
62
 * Custom print preview frame.
 
63
 */
 
64
class PLEDITOR_PREVIEW_FRAME : public wxPreviewFrame
 
65
{
 
66
    PL_EDITOR_FRAME* m_parent;
 
67
 
 
68
public:
 
69
    PLEDITOR_PREVIEW_FRAME( wxPrintPreview* aPreview, PL_EDITOR_FRAME* aParent,
 
70
                       const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
 
71
                       const wxSize& aSize = wxDefaultSize ) :
 
72
        wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
 
73
    {
 
74
        m_parent = aParent;
 
75
    }
 
76
 
 
77
    bool Show( bool show )      // overload
 
78
    {
 
79
        bool        ret;
 
80
 
 
81
        // Show or hide the window.  If hiding, save current position and size.
 
82
        // If showing, use previous position and size.
 
83
        if( show )
 
84
        {
 
85
            bool centre = false;
 
86
            if( s_size.x == 0 || s_size.y == 0 )
 
87
            {
 
88
                s_size = (m_parent->GetSize() * 3) / 4;
 
89
                s_pos = wxDefaultPosition;
 
90
                centre = true;
 
91
            }
 
92
 
 
93
            SetSize( s_pos.x, s_pos.y, s_size.x, s_size.y, 0 );
 
94
 
 
95
            if( centre )
 
96
                Center();
 
97
 
 
98
            ret = wxPreviewFrame::Show( show );
 
99
        }
 
100
        else
 
101
        {
 
102
            // Save the dialog's position & size before hiding
 
103
            s_size = GetSize();
 
104
            s_pos  = GetPosition();
 
105
 
 
106
            ret = wxPreviewFrame::Show( show );
 
107
        }
 
108
        return ret;
 
109
    }
 
110
 
 
111
private:
 
112
    static wxPoint  s_pos;
 
113
    static wxSize   s_size;
 
114
 
 
115
    DECLARE_CLASS( PLEDITOR_PREVIEW_FRAME )
 
116
    DECLARE_EVENT_TABLE()
 
117
    DECLARE_NO_COPY_CLASS( PLEDITOR_PREVIEW_FRAME )
 
118
};
 
119
 
 
120
wxPoint PLEDITOR_PREVIEW_FRAME::s_pos;
 
121
wxSize  PLEDITOR_PREVIEW_FRAME::s_size;
 
122
 
 
123
IMPLEMENT_CLASS( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
 
124
 
 
125
BEGIN_EVENT_TABLE( PLEDITOR_PREVIEW_FRAME, wxPreviewFrame )
 
126
    EVT_CLOSE( PLEDITOR_PREVIEW_FRAME::OnCloseWindow )
 
127
END_EVENT_TABLE()
 
128
 
 
129
 
 
130
bool PLEDITOR_PRINTOUT::OnPrintPage( int aPageNum )
 
131
{
 
132
    DrawPage( aPageNum );
 
133
    return true;
 
134
}
 
135
 
 
136
 
 
137
void PLEDITOR_PRINTOUT::GetPageInfo( int* minPage, int* maxPage,
 
138
                                     int* selPageFrom, int* selPageTo )
 
139
{
 
140
    *minPage = *selPageFrom = 1;
 
141
    *maxPage = *selPageTo   = 2;
 
142
}
 
143
 
 
144
/*
 
145
 * This is the real print function: print the active screen
 
146
 */
 
147
void PLEDITOR_PRINTOUT::DrawPage( int aPageNum )
 
148
{
 
149
    int      oldZoom;
 
150
    wxPoint  tmp_startvisu;
 
151
    wxSize   pageSizeIU;             // Page size in internal units
 
152
    wxPoint  old_org;
 
153
    EDA_RECT oldClipBox;
 
154
    wxRect   fitRect;
 
155
    wxDC*    dc = GetDC();
 
156
    EDA_DRAW_PANEL* panel = m_parent->GetCanvas();
 
157
    PL_EDITOR_SCREEN* screen = m_parent->GetScreen();
 
158
 
 
159
    // Save current scale factor, offsets, and clip box.
 
160
    tmp_startvisu = screen->m_StartVisu;
 
161
    oldZoom = screen->GetZoom();
 
162
    old_org = screen->m_DrawOrg;
 
163
 
 
164
    oldClipBox = *panel->GetClipBox();
 
165
 
 
166
    // Change clip box to print the whole page.
 
167
    #define MAX_VALUE (INT_MAX/2)   // MAX_VALUE is the max we can use in an integer
 
168
                                    // and that allows calculations without overflow
 
169
    panel->SetClipBox( EDA_RECT( wxPoint( 0, 0 ), wxSize( MAX_VALUE, MAX_VALUE ) ) );
 
170
 
 
171
    // Change scale factor and offset to print the whole page.
 
172
 
 
173
    pageSizeIU =  m_parent->GetPageSettings().GetSizeIU();
 
174
    FitThisSizeToPaper( pageSizeIU );
 
175
    fitRect = GetLogicalPaperRect();
 
176
 
 
177
    int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
 
178
    int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
 
179
 
 
180
    OffsetLogicalOrigin( xoffset, yoffset );
 
181
 
 
182
    GRResetPenAndBrush( dc );
 
183
    GRForceBlackPen( true );
 
184
    screen->m_IsPrinting = true;
 
185
 
 
186
    EDA_COLOR_T bg_color = g_DrawBgColor;
 
187
    g_DrawBgColor = WHITE;
 
188
 
 
189
    screen->m_ScreenNumber = aPageNum;
 
190
    m_parent->DrawWorkSheet( dc, screen, 0, IU_PER_MILS, wxEmptyString );
 
191
 
 
192
    g_DrawBgColor = bg_color;
 
193
    screen->m_IsPrinting = false;
 
194
    panel->SetClipBox( oldClipBox );
 
195
 
 
196
    GRForceBlackPen( false );
 
197
 
 
198
    screen->m_StartVisu = tmp_startvisu;
 
199
    screen->m_DrawOrg   = old_org;
 
200
    screen->SetZoom( oldZoom );
 
201
}
 
202
 
 
203
 
 
204
int InvokeDialogPrint( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData,
 
205
                       wxPageSetupDialogData* aPageSetupData )
 
206
{
 
207
    int pageCount = 2;
 
208
 
 
209
    wxPrintDialogData printDialogData( *aPrintData );
 
210
    printDialogData.SetMaxPage( pageCount );
 
211
 
 
212
    if( pageCount > 1 )
 
213
        printDialogData.EnablePageNumbers( true );
 
214
 
 
215
    wxPrinter printer( &printDialogData );
 
216
    PLEDITOR_PRINTOUT printout( aCaller, _( "Print Page Layout" ) );
 
217
 
 
218
    if( !printer.Print( aCaller, &printout, true ) )
 
219
    {
 
220
        if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
 
221
            wxMessageBox( _( "An error occurred attempting to print the page layout." ),
 
222
                          _( "Printing" ), wxOK );
 
223
        return 0;
 
224
    }
 
225
 
 
226
    *aPageSetupData = printer.GetPrintDialogData().GetPrintData();
 
227
 
 
228
    return 1;
 
229
}
 
230
 
 
231
int InvokeDialogPrintPreview( PL_EDITOR_FRAME* aCaller, wxPrintData* aPrintData )
 
232
{
 
233
    // Pass two printout objects: for preview, and possible printing.
 
234
    wxString        title   = _( "Preview" );
 
235
    wxPrintPreview* preview = new wxPrintPreview( new PLEDITOR_PRINTOUT( aCaller, title ),
 
236
                                                  new PLEDITOR_PRINTOUT( aCaller, title ),
 
237
                                                  aPrintData );
 
238
 
 
239
    preview->SetZoom( 70 );
 
240
 
 
241
    PLEDITOR_PREVIEW_FRAME* frame = new PLEDITOR_PREVIEW_FRAME( preview, aCaller, title );
 
242
 
 
243
    frame->Initialize();
 
244
    frame->Show( true );
 
245
 
 
246
    return 1;
 
247
}
 
248