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 <wx/metafile.h>
31 #include <fctsys.h>
32 #include <gr_basic.h>
33 #include <common.h>
34 #include <id.h>
35 #include <class_drawpanel.h>
36 #include <class_base_screen.h>
37 #include <confirm.h>
38 #include <draw_frame.h>
39 
40 static bool DrawPageOnClipboard( EDA_DRAW_FRAME* aFrame );
41 
42 
43 void EDA_DRAW_FRAME::CopyToClipboard( wxCommandEvent& event )
44 {
45  DrawPageOnClipboard( this );
46 
47  if( event.GetId() == ID_GEN_COPY_BLOCK_TO_CLIPBOARD )
48  {
49  if( GetScreen()->IsBlockActive() )
50  m_canvas->SetCursor( wxCursor( (wxStockCursor) m_canvas->GetDefaultCursor() ) );
51 
53  }
54 }
55 
56 
57 /* copy the current page or block to the clipboard ,
58  * to export drawings to other applications (word processing ...)
59  * This is not suitable for copy command within Eeschema or Pcbnew
60  */
62 {
63  bool DrawBlock = false;
64  wxRect DrawArea;
65  BASE_SCREEN* screen = aFrame->GetCanvas()->GetScreen();
66 
67  if( screen->IsBlockActive() )
68  {
69  DrawBlock = true;
70  DrawArea.SetX( screen->m_BlockLocate.GetX() );
71  DrawArea.SetY( screen->m_BlockLocate.GetY() );
72  DrawArea.SetWidth( screen->m_BlockLocate.GetWidth() );
73  DrawArea.SetHeight( screen->m_BlockLocate.GetHeight() );
74  }
75  else
76  DrawArea.SetSize( aFrame->GetPageSizeIU() );
77 
78  // Calculate a reasonable dc size, in pixels, and the dc scale to fit
79  // the drawings into the dc size
80  // scale is the ratio resolution (in PPI) / internal units
81  double ppi = 300; // Use 300 pixels per inch to create bitmap images on start
82  double inch2Iu = 1000.0 * (double) screen->MilsToIuScalar();
83  double scale = ppi / inch2Iu;
84 
85  wxSize dcsize = DrawArea.GetSize();
86 
87  int maxdim = std::max( dcsize.x, dcsize.y );
88  // the max size in pixels of the bitmap used to byuild the sheet copy
89  const int maxbitmapsize = 3000;
90 
91  while( int( maxdim * scale ) > maxbitmapsize )
92  {
93  ppi = ppi / 1.5;
94  scale = ppi / inch2Iu;
95  }
96 
97  dcsize.x *= scale;
98  dcsize.y *= scale;
99 
100  // Set draw offset, zoom... to values needed to draw in the memory DC
101  // after saving initial values:
102  wxPoint tmp_startvisu = screen->m_StartVisu;
103  double tmpzoom = screen->GetZoom();
104  wxPoint old_org = screen->m_DrawOrg;
105  screen->m_DrawOrg.x = screen->m_DrawOrg.y = 0;
106  screen->m_StartVisu.x = screen->m_StartVisu.y = 0;
107 
108  screen->SetZoom( 1 ); // we use zoom = 1 in draw functions.
109 
110  wxMemoryDC dc;
111  wxBitmap image( dcsize );
112  dc.SelectObject( image );
113 
114  EDA_RECT tmp = *aFrame->GetCanvas()->GetClipBox();
115  GRResetPenAndBrush( &dc );
116  GRForceBlackPen( false );
117  screen->m_IsPrinting = true;
118  dc.SetUserScale( scale, scale );
119 
120  aFrame->GetCanvas()->SetClipBox( EDA_RECT( wxPoint( 0, 0 ),
121  wxSize( 0x7FFFFF0, 0x7FFFFF0 ) ) );
122 
123  if( DrawBlock )
124  {
125  dc.SetClippingRegion( DrawArea );
126  }
127 
128  dc.Clear();
129  aFrame->GetCanvas()->EraseScreen( &dc );
130  const LSET allLayersMask = LSET().set();
131  aFrame->PrintPage( &dc, allLayersMask, false );
132  screen->m_IsPrinting = false;
133  aFrame->GetCanvas()->SetClipBox( tmp );
134 
135  bool success = true;
136 
137  if( wxTheClipboard->Open() )
138  {
139  // This data objects are held by the clipboard,
140  // so do not delete them in the app.
141  wxBitmapDataObject* clipbrd_data = new wxBitmapDataObject( image );
142  wxTheClipboard->SetData( clipbrd_data );
143  wxTheClipboard->Close();
144  }
145  else
146  success = false;
147 
148  // Deselect Bitmap from DC in order to delete the MemoryDC safely
149  // without deleting the bitmap
150  dc.SelectObject( wxNullBitmap );
151 
152  GRForceBlackPen( false );
153 
154  screen->m_StartVisu = tmp_startvisu;
155  screen->m_DrawOrg = old_org;
156  screen->SetZoom( tmpzoom );
157 
158  return success;
159 }
void GRResetPenAndBrush(wxDC *DC)
Definition: gr_basic.cpp:196
BLOCK_SELECTOR m_BlockLocate
Block description for block commands.
This file is part of the common library.
EDA_DRAW_PANEL * GetCanvas()
Definition: draw_frame.h:299
double GetZoom() const
Function GetZoom returns the current "zoom factor", which is a measure of "internal units per device ...
int GetHeight() const
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:53
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) ...
bool IsBlockActive() const
virtual BASE_SCREEN * GetScreen() const
Function GetScreen returns a pointer to a BASE_SCREEN or one of its derivatives.
Definition: draw_frame.h:309
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:394
void GRForceBlackPen(bool flagforce)
Function GRForceBlackPen.
Definition: gr_basic.cpp:271
Class BASE_SCREEN handles how to draw a screen (a board, a schematic ...)
EDA_RECT * GetClipBox()
virtual int MilsToIuScalar()
Function MilsToIuScalar returns the scalar required to convert mils to internal units.
bool SetZoom(double iu_per_du)
Function SetZoom adjusts the current zoom factor.
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:92
static bool DrawPageOnClipboard(EDA_DRAW_FRAME *aFrame)
const int scale
BASE_SCREEN class implementation.
#define max(a, b)
Definition: auxiliary.h:86
Class EDA_RECT handles the component boundary box.
wxPoint m_DrawOrg
offsets for drawing the circuit on the screen
int GetX() const
BASE_SCREEN * GetScreen()
Definition: draw_panel.cpp:188
The common library.
int GetWidth() const
int GetY() const
int GetDefaultCursor() const
Function GetDefaultCursor return the default cursor shape.
void EraseScreen(wxDC *DC)
Definition: draw_panel.cpp:554