You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
6.8 KiB

  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. /**
  26. * a helper to handle the real device context used in KiCad
  27. * @file kicad_device_context.h
  28. */
  29. #ifndef __KICAD_DEVICE_CONTEXT_H__
  30. #define __KICAD_DEVICE_CONTEXT_H__
  31. #include <wx/dcbuffer.h>
  32. #if defined(KICAD_USE_BUFFERED_PAINTDC)
  33. #undef KICAD_USE_BUFFERED_PAINTDC
  34. #endif
  35. #if defined(KICAD_USE_BUFFERED_DC)
  36. #undef KICAD_USE_BUFFERED_DC
  37. #endif
  38. // wxWidgets defines the platforms where device context buffering is well behaved. These
  39. // definitions take advantage of their experience in this area. See <wx/dcbuffer.h> for
  40. // more information.
  41. #if wxALWAYS_NATIVE_DOUBLE_BUFFER
  42. #define KICAD_USE_BUFFERED_PAINTDC 1
  43. #define KICAD_USE_BUFFERED_DC_HELPER 0
  44. #define KICAD_USE_BUFFERED_DC 0
  45. #else
  46. #define KICAD_USE_BUFFERED_PAINTDC 1
  47. #define KICAD_USE_BUFFERED_DC_HELPER 1
  48. #define KICAD_USE_BUFFERED_DC 1
  49. #endif
  50. /**
  51. * Class BUFFERED_DC_HELPER
  52. * fixes a bug on Windows when using buffered device context.
  53. *
  54. * <p>
  55. * When using buffered device context drawing in Windows, the user scaling must be set to 1
  56. * and the logical offset must be set to zero before the bitmap blit operation occurs in
  57. * the destructor of wxBufferdDC but after the normal drawing takes place.
  58. * </p>
  59. */
  60. class BUFFERED_DC_HELPER
  61. {
  62. public:
  63. BUFFERED_DC_HELPER( wxBufferedDC* aDC )
  64. : m_dc( aDC ) {}
  65. virtual ~BUFFERED_DC_HELPER()
  66. {
  67. if( m_dc )
  68. {
  69. m_dc->SetLogicalOrigin( 0, 0 );
  70. m_dc->SetUserScale( 1.0, 1.0 );
  71. }
  72. }
  73. private:
  74. wxBufferedDC* m_dc;
  75. };
  76. /**
  77. * Class EDA_BLIT_NORMALIZER
  78. * is a helper class for clearing a device context scale and offset parameters before
  79. * performing a Blit operation.
  80. * <p>
  81. * This class keeps a temporary copy of the scale and offset parameters of a device
  82. * context and then restores them when it goes out of scope.
  83. * </p>
  84. */
  85. class EDA_BLIT_NORMALIZER
  86. {
  87. public:
  88. EDA_BLIT_NORMALIZER( wxDC* aDC )
  89. : m_dc( aDC )
  90. {
  91. if( aDC )
  92. {
  93. aDC->GetUserScale( &m_userScaleX, &m_userScaleY );
  94. aDC->GetLogicalOrigin( &m_logicalOriginX, &m_logicalOriginY );
  95. aDC->GetDeviceOrigin( &m_deviceOriginX, &m_deviceOriginY );
  96. aDC->SetUserScale( 1.0, 1.0 );
  97. aDC->SetLogicalOrigin( 0, 0 );
  98. aDC->SetDeviceOrigin( 0, 0 );
  99. }
  100. }
  101. ~EDA_BLIT_NORMALIZER()
  102. {
  103. if( m_dc )
  104. {
  105. m_dc->SetUserScale( m_userScaleX, m_userScaleY );
  106. m_dc->SetLogicalOrigin( m_logicalOriginX, m_logicalOriginY );
  107. m_dc->SetDeviceOrigin( m_deviceOriginX, m_deviceOriginY );
  108. }
  109. }
  110. private:
  111. wxDC* m_dc;
  112. double m_userScaleX;
  113. double m_userScaleY;
  114. int m_logicalOriginX;
  115. int m_logicalOriginY;
  116. int m_deviceOriginX;
  117. int m_deviceOriginY;
  118. DECLARE_NO_COPY_CLASS( EDA_BLIT_NORMALIZER )
  119. };
  120. #if USE_WX_GRAPHICS_CONTEXT
  121. #include <wx/dcgraph.h>
  122. #endif
  123. // Macro used to declare a device context in KiCad:
  124. #if USE_WX_GRAPHICS_CONTEXT
  125. //#pragma message( "INSTALL_DC is wxClientDC with wxGCDC" )
  126. #define INSTALL_DC( name, parent ) \
  127. wxClientDC _cDC( parent ); \
  128. wxGCDC name( _cDC ); \
  129. parent->DoPrepareDC( name ); \
  130. name.GetGraphicsContext()->Translate( 0.5, 0.5 );
  131. #else
  132. #if KICAD_USE_BUFFERED_DC && !KICAD_USE_BUFFERED_DC_HELPER
  133. //#pragma message( "INSTALL_DC is wxClientDC with wxBufferedDC" )
  134. #define INSTALL_DC( name, parent ) \
  135. wxClientDC _cDC( parent ); \
  136. wxBufferedDC name(&_cDC, _cDC.GetSize() ); \
  137. parent->DoPrepareDC( name );
  138. #elif KICAD_USE_BUFFERED_DC && KICAD_USE_BUFFERED_DC_HELPER
  139. //#pragma message( "INSTALL_DC is wxBufferedDC with BUFFERED_DC_HELPER" )
  140. #define INSTALL_DC( name, parent ) \
  141. wxClientDC _cDC( parent ); \
  142. wxBufferedDC name(&_cDC, _cDC.GetSize() ); \
  143. parent->DoPrepareDC( name ); \
  144. BUFFERED_DC_HELPER helper( &name );
  145. #else
  146. //#pragma message( "INSTALL_DC is wxClientDC" )
  147. #define INSTALL_DC( name, parent ) \
  148. wxClientDC name( parent ); \
  149. parent->DoPrepareDC( name );
  150. #endif
  151. #endif
  152. #if USE_WX_GRAPHICS_CONTEXT
  153. //#pragma message( "INSTALL_PAINTDC is wxPaintDC with wxGCDC" )
  154. #define INSTALL_PAINTDC( name, parent) \
  155. wxPaintDC _pDC( parent ); \
  156. wxGCDC name( _pDC ); \
  157. parent->DoPrepareDC( name ); \
  158. name.GetGraphicsContext()->Translate( 0.5, 0.5 );
  159. #elif KICAD_USE_BUFFERED_PAINTDC && !KICAD_USE_BUFFERED_DC_HELPER
  160. //#pragma message( "INSTALL_PAINTDC is wxAutoBufferedPaintDC" )
  161. #define INSTALL_PAINTDC( name, parent ) \
  162. wxAutoBufferedPaintDC name( parent ); \
  163. parent->DoPrepareDC( name );
  164. #elif KICAD_USE_BUFFERED_PAINTDC && KICAD_USE_BUFFERED_DC_HELPER
  165. //#pragma message( "INSTALL_PAINTDC is wxBufferedPaintDC with BUFFERED_DC_HELPER" )
  166. #define INSTALL_PAINTDC( name, parent ) \
  167. wxBufferedPaintDC name( parent ); \
  168. parent->DoPrepareDC( name ); \
  169. BUFFERED_DC_HELPER help( &name );
  170. #else
  171. //#pragma message( "INSTALL_PAINTDC is wxPaintDC" )
  172. #define INSTALL_PAINTDC(name,parent) \
  173. wxPaintDC name( parent ); \
  174. parent->DoPrepareDC( name );
  175. #endif
  176. // This macro should be used when drawing objects directly without drawing the background.
  177. #define INSTALL_UNBUFFERED_DC( name, parent ) \
  178. wxClientDC name( parent ); \
  179. parent->DoPrepareDC( name );
  180. #endif // __KICAD_DEVICE_CONTEXT_H__