KiCad PCB EDA Suite
copy_to_clipboard.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2006 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
29 #include <wx/clipbrd.h>
30 #include <fctsys.h>
31 #include <gr_basic.h>
32 #include <common.h>
33 #include <id.h>
34 #include <class_drawpanel.h>
35 #include <base_screen.h>
36 #include <confirm.h>
37 #include <draw_frame.h>
38 
39 static bool DrawPageOnClipboard( EDA_DRAW_FRAME* aFrame );
40 
41 
42 void EDA_DRAW_FRAME::CopyToClipboard( wxCommandEvent& event )
43 {
44  DrawPageOnClipboard( this );
45 
46  if( event.GetId() == ID_GEN_COPY_BLOCK_TO_CLIPBOARD )
47  {
48  if( GetScreen()->IsBlockActive() )
49  m_canvas->SetCursor( wxCursor( (wxStockCursor) m_canvas->GetDefaultCursor() ) );
50 
52  }
53 }
54 
55 
56 /* copy the current page or block to the clipboard ,
57  * to export drawings to other applications (word processing ...)
58  * This is not suitable for copy command within Eeschema or Pcbnew
59  */
61 {
62  bool DrawBlock = false;
63  wxRect DrawArea;
64  BASE_SCREEN* screen = aFrame->GetCanvas()->GetScreen();
65 
66  if( screen->IsBlockActive() )
67  {
68  DrawBlock = true;
69  DrawArea.SetX( screen->m_BlockLocate.GetX() );
70  DrawArea.SetY( screen->m_BlockLocate.GetY() );
71  DrawArea.SetWidth( screen->m_BlockLocate.GetWidth() );
72  DrawArea.SetHeight( screen->m_BlockLocate.GetHeight() );
73  }
74  else
75  DrawArea.SetSize( aFrame->GetPageSizeIU() );
76 
77  // Calculate a reasonable dc size, in pixels, and the dc scale to fit
78  // the drawings into the dc size
79  // scale is the ratio resolution (in PPI) / internal units
80  double ppi = 300; // Use 300 pixels per inch to create bitmap images on start
81  double inch2Iu = 1000.0 * (double) screen->MilsToIuScalar();
82  double scale = ppi / inch2Iu;
83 
84  wxSize dcsize = DrawArea.GetSize();
85 
86  int maxdim = std::max( dcsize.x, dcsize.y );
87  // the max size in pixels of the bitmap used to byuild the sheet copy
88  const int maxbitmapsize = 3000;
89 
90  while( int( maxdim * scale ) > maxbitmapsize )
91  {
92  ppi = ppi / 1.5;
93  scale = ppi / inch2Iu;
94  }
95 
96  dcsize.x *= scale;
97  dcsize.y *= scale;
98 
99  // Set draw offset, zoom... to values needed to draw in the memory DC
100  // after saving initial values:
101  wxPoint tmp_startvisu = screen->m_StartVisu;
102  double tmpzoom = screen->GetZoom();
103  wxPoint old_org = screen->m_DrawOrg;
104  screen->m_DrawOrg.x = screen->m_DrawOrg.y = 0;
105  screen->m_StartVisu.x = screen->m_StartVisu.y = 0;
106 
107  screen->SetZoom( 1 ); // we use zoom = 1 in draw functions.
108 
109  wxMemoryDC dc;
110  wxBitmap image( dcsize );
111  dc.SelectObject( image );
112 
113  EDA_RECT tmp = *aFrame->GetCanvas()->GetClipBox();
114  GRResetPenAndBrush( &dc );
115  GRForceBlackPen( false );
116  screen->m_IsPrinting = true;
117  dc.SetUserScale( scale, scale );
118 
119  aFrame->GetCanvas()->SetClipBox( EDA_RECT( wxPoint( 0, 0 ),
120  wxSize( 0x7FFFFF0, 0x7FFFFF0 ) ) );
121 
122  if( DrawBlock )
123  {
124  dc.SetClippingRegion( DrawArea );
125  }
126 
127  dc.Clear();
128  aFrame->GetCanvas()->EraseScreen( &dc );
129  const LSET allLayersMask = LSET().set();
130  aFrame->PrintPage( &dc, allLayersMask, false );
131  screen->m_IsPrinting = false;
132  aFrame->GetCanvas()->SetClipBox( tmp );
133 
134  bool success = true;
135 
136  if( wxTheClipboard->Open() )
137  {
138  // This data objects are held by the clipboard,
139  // so do not delete them in the app.
140  wxBitmapDataObject* clipbrd_data = new wxBitmapDataObject( image );
141  wxTheClipboard->SetData( clipbrd_data );
142  wxTheClipboard->Close();
143  }
144  else
145  success = false;
146 
147  // Deselect Bitmap from DC in order to delete the MemoryDC safely
148  // without deleting the bitmap
149  dc.SelectObject( wxNullBitmap );
150 
151  GRForceBlackPen( false );
152 
153  screen->m_StartVisu = tmp_startvisu;
154  screen->m_DrawOrg = old_org;
155  screen->SetZoom( tmpzoom );
156 
157  return success;
158 }
void GRResetPenAndBrush(wxDC *DC)
Definition: gr_basic.cpp:218
BLOCK_SELECTOR m_BlockLocate
Block description for block commands.
Definition: base_screen.h:214
This file is part of the common library.
EDA_DRAW_PANEL * GetCanvas()
Definition: draw_frame.h:377
double GetZoom() const
Function GetZoom returns the current "zoom factor", which is a measure of "internal units per device ...
Definition: base_screen.h:340
int GetHeight() const
Definition: eda_rect.h:118
void EndMouseCapture(int aId=-1, int aCursorId=-1, const wxString &aTitle=wxEmptyString, bool aCallEndFunc=true)
Function EndMouseCapture ends mouse a capture.
void CopyToClipboard(wxCommandEvent &event)
Function CopyToClipboard copies the current page or the current block to the clipboard.
Class EDA_DRAW_FRAME is the base class for create windows for drawing purpose.
Definition: draw_frame.h:63
virtual const wxSize GetPageSizeIU() const =0
Function GetPageSizeIU works off of GetPageSettings() to return the size of the paper page in the int...
void SetClipBox(const EDA_RECT &aRect)
wxPoint m_StartVisu
Coordinates in drawing units of the current view position (upper left corner of device) ...
Definition: base_screen.h:198
bool IsBlockActive() const
Definition: base_screen.h:499
virtual BASE_SCREEN * GetScreen() const
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
Definition: draw_frame.h:387
Class LSET is a set of PCB_LAYER_IDs.
virtual void PrintPage(wxDC *aDC, LSET aPrintMask, bool aPrintMirrorMode, void *aData=NULL)
Function PrintPage used to print a page Print the page pointed by current screen, set by the calling ...
Definition: draw_frame.cpp:468
void GRForceBlackPen(bool flagforce)
Function GRForceBlackPen.
Definition: gr_basic.cpp:299
Class BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:76
EDA_RECT * GetClipBox()
virtual int MilsToIuScalar()
Function MilsToIuScalar returns the scalar required to convert mils to internal units.
Definition: base_screen.h:245
bool SetZoom(double iu_per_du)
Function SetZoom adjusts the current zoom factor.
bool m_IsPrinting
Definition: base_screen.h:220
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:107
static bool DrawPageOnClipboard(EDA_DRAW_FRAME *aFrame)
const int scale
#define max(a, b)
Definition: auxiliary.h:86
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
wxPoint m_DrawOrg
offsets for drawing the circuit on the screen
Definition: base_screen.h:183
int GetX() const
Definition: eda_rect.h:109
BASE_SCREEN * GetScreen()
Definition: draw_panel.cpp:176
The common library.
int GetWidth() const
Definition: eda_rect.h:117
int GetY() const
Definition: eda_rect.h:110
BASE_SCREEN class implementation.
int GetDefaultCursor() const
Function GetDefaultCursor.
void EraseScreen(wxDC *DC)
Definition: draw_panel.cpp:557