KiCad PCB EDA Suite
kicad_device_context.h
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) 2009 jean-pierre.charras@gipsa-lab.inpg.fr
5  * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 2007 KiCad Developers, see change_log.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 
31 #ifndef __KICAD_DEVICE_CONTEXT_H__
32 #define __KICAD_DEVICE_CONTEXT_H__
33 
34 #include <wx/dcbuffer.h>
35 
36 #if defined(KICAD_USE_BUFFERED_PAINTDC)
37 #undef KICAD_USE_BUFFERED_PAINTDC
38 #endif
39 
40 #if defined(KICAD_USE_BUFFERED_DC)
41 #undef KICAD_USE_BUFFERED_DC
42 #endif
43 
44 // wxWidgets defines the platforms where device context buffering is well behaved. These
45 // definitions take advantage of their experience in this area. See <wx/dcbuffer.h> for
46 // more information.
47 #if wxALWAYS_NATIVE_DOUBLE_BUFFER
48 #define KICAD_USE_BUFFERED_PAINTDC 1
49 #define KICAD_USE_BUFFERED_DC_HELPER 0
50 #define KICAD_USE_BUFFERED_DC 0
51 #else
52 #define KICAD_USE_BUFFERED_PAINTDC 1
53 #define KICAD_USE_BUFFERED_DC_HELPER 1
54 #define KICAD_USE_BUFFERED_DC 1
55 #endif
56 
57 
69 {
70 public:
71  BUFFERED_DC_HELPER( wxBufferedDC* aDC )
72  : m_dc( aDC ) {}
73 
75  {
76  if( m_dc )
77  {
78  m_dc->SetLogicalOrigin( 0, 0 );
79  m_dc->SetUserScale( 1.0, 1.0 );
80  }
81  }
82 
83 private:
84  wxBufferedDC* m_dc;
85 };
86 
87 
98 {
99 public:
100  EDA_BLIT_NORMALIZER( wxDC* aDC )
101  : m_dc( aDC )
102  {
103  if( aDC )
104  {
105  aDC->GetUserScale( &m_userScaleX, &m_userScaleY );
106  aDC->GetLogicalOrigin( &m_logicalOriginX, &m_logicalOriginY );
107  aDC->GetDeviceOrigin( &m_deviceOriginX, &m_deviceOriginY );
108  aDC->SetUserScale( 1.0, 1.0 );
109  aDC->SetLogicalOrigin( 0, 0 );
110  aDC->SetDeviceOrigin( 0, 0 );
111  }
112  }
113 
115  {
116  if( m_dc )
117  {
118  m_dc->SetUserScale( m_userScaleX, m_userScaleY );
119  m_dc->SetLogicalOrigin( m_logicalOriginX, m_logicalOriginY );
120  m_dc->SetDeviceOrigin( m_deviceOriginX, m_deviceOriginY );
121  }
122  }
123 
124 private:
125  wxDC* m_dc;
126  double m_userScaleX;
127  double m_userScaleY;
132 
133  DECLARE_NO_COPY_CLASS( EDA_BLIT_NORMALIZER )
134 };
135 
136 
137 #if USE_WX_GRAPHICS_CONTEXT
138  #include <wx/dcgraph.h>
139 #endif
140 
141 // Macro used to declare a device context in KiCad:
142 #if USE_WX_GRAPHICS_CONTEXT
143 //#pragma message( "INSTALL_DC is wxClientDC with wxGCDC" )
144 #define INSTALL_DC( name, parent ) \
145  wxClientDC _cDC( parent ); \
146  wxGCDC name( _cDC ); \
147  parent->DoPrepareDC( name ); \
148  name.GetGraphicsContext()->Translate( 0.5, 0.5 );
149 #else
150 #if KICAD_USE_BUFFERED_DC && !KICAD_USE_BUFFERED_DC_HELPER
151 //#pragma message( "INSTALL_DC is wxClientDC with wxBufferedDC" )
152 #define INSTALL_DC( name, parent ) \
153  wxClientDC _cDC( parent ); \
154  wxBufferedDC name(&_cDC, _cDC.GetSize() ); \
155  parent->DoPrepareDC( name );
156 #elif KICAD_USE_BUFFERED_DC && KICAD_USE_BUFFERED_DC_HELPER
157 //#pragma message( "INSTALL_DC is wxBufferedDC with BUFFERED_DC_HELPER" )
158 #define INSTALL_DC( name, parent ) \
159  wxClientDC _cDC( parent ); \
160  wxBufferedDC name(&_cDC, _cDC.GetSize() ); \
161  parent->DoPrepareDC( name ); \
162  BUFFERED_DC_HELPER helper( &name );
163 #else
164 //#pragma message( "INSTALL_DC is wxClientDC" )
165 #define INSTALL_DC( name, parent ) \
166  wxClientDC name( parent ); \
167  parent->DoPrepareDC( name );
168 #endif
169 #endif
170 
171 #if USE_WX_GRAPHICS_CONTEXT
172 //#pragma message( "INSTALL_PAINTDC is wxPaintDC with wxGCDC" )
173 #define INSTALL_PAINTDC( name, parent) \
174  wxPaintDC _pDC( parent ); \
175  wxGCDC name( _pDC ); \
176  parent->DoPrepareDC( name ); \
177  name.GetGraphicsContext()->Translate( 0.5, 0.5 );
178 #elif KICAD_USE_BUFFERED_PAINTDC && !KICAD_USE_BUFFERED_DC_HELPER
179 //#pragma message( "INSTALL_PAINTDC is wxAutoBufferedPaintDC" )
180 #define INSTALL_PAINTDC( name, parent ) \
181  wxAutoBufferedPaintDC name( parent ); \
182  parent->DoPrepareDC( name );
183 #elif KICAD_USE_BUFFERED_PAINTDC && KICAD_USE_BUFFERED_DC_HELPER
184 //#pragma message( "INSTALL_PAINTDC is wxBufferedPaintDC with BUFFERED_DC_HELPER" )
185 #define INSTALL_PAINTDC( name, parent ) \
186  wxBufferedPaintDC name( parent ); \
187  parent->DoPrepareDC( name ); \
188  BUFFERED_DC_HELPER help( &name );
189 #else
190 //#pragma message( "INSTALL_PAINTDC is wxPaintDC" )
191 #define INSTALL_PAINTDC(name,parent) \
192  wxPaintDC name( parent ); \
193  parent->DoPrepareDC( name );
194 #endif
195 
196 
197 // This macro should be used when drawing objects directly without drawing the background.
198 #define INSTALL_UNBUFFERED_DC( name, parent ) \
199  wxClientDC name( parent ); \
200  parent->DoPrepareDC( name );
201 
202 
203 #endif // __KICAD_DEVICE_CONTEXT_H__
Class BUFFERED_DC_HELPER fixes a bug on Windows when using buffered device context.
BUFFERED_DC_HELPER(wxBufferedDC *aDC)
Class EDA_BLIT_NORMALIZER is a helper class for clearing a device context scale and offset parameters...