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.

137 lines
4.9 KiB

17 years ago
17 years ago
17 years ago
17 years ago
15 years ago
15 years ago
15 years ago
15 years ago
  1. /**
  2. * @file macros.h
  3. * @brief This file contains miscellaneous helper definitions and functions.
  4. */
  5. #ifndef MACROS_H
  6. #define MACROS_H
  7. #include <wx/wx.h>
  8. /**
  9. * Macro TO_UTF8
  10. * converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
  11. * wxstring is a wxString, not a wxT() or _(). The scope of the return value
  12. * is very limited and volatile, but can be used with printf() style functions well.
  13. */
  14. #define TO_UTF8( wxstring ) ( (const char*) (wxstring).utf8_str() )
  15. /**
  16. * Macro FROM_UTF8
  17. * converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
  18. */
  19. //#define FROM_UTF8( cstring ) wxString::FromUTF8( cstring )
  20. static inline wxString FROM_UTF8( const char* cstring )
  21. {
  22. wxString line = wxString::FromUTF8( cstring );
  23. if( line.IsEmpty() ) // happens when cstring is not a valid UTF8 sequence
  24. line = wxConvCurrent->cMB2WC( cstring ); // try to use locale conversion
  25. return line;
  26. }
  27. /**
  28. * Function GetChars
  29. * returns a wxChar* to the actual character data within a wxString, and is
  30. * helpful for passing strings to wxString::Printf(wxT("%s"), GetChars(wxString) )
  31. * <p>
  32. * wxChar is defined to be
  33. * <ul>
  34. * <li> standard C style char when wxUSE_UNICODE==0 </li>
  35. * <li> wchar_t when wxUSE_UNICODE==1 (the default). </li>
  36. * </ul>
  37. * i.e. it depends on how the wxWidgets library was compiled. There was a period
  38. * during the development of wxWidgets 2.9 when GetData() was missing, so this
  39. * function was used to provide insulation from that design change. It may
  40. * no longer be needed, and is harmless. GetData() seems to be an acceptable
  41. * alternative in all cases now.
  42. */
  43. static inline const wxChar* GetChars( const wxString& s )
  44. {
  45. #if wxCHECK_VERSION( 2, 9, 0 )
  46. return (const wxChar*) s.c_str();
  47. #else
  48. return s.GetData();
  49. #endif
  50. }
  51. #define NEGATE( x ) (x = -x)
  52. /// # of elements in an array
  53. #define DIM( x ) unsigned( sizeof(x) / sizeof( (x)[0] ) ) // not size_t
  54. #define DEG2RAD( Deg ) ( (Deg) * M_PI / 180.0 )
  55. #define RAD2DEG( Rad ) ( (Rad) * 180.0 / M_PI )
  56. // Normalize angle to be in the -360.0 .. 360.0:
  57. #define NORMALIZE_ANGLE_360( Angle ) { \
  58. while( Angle < -3600 ) \
  59. Angle += 3600; \
  60. while( Angle > 3600 ) \
  61. Angle -= 3600; }
  62. /* Normalize angle to be in the 0.0 .. 360.0 range: */
  63. #define NORMALIZE_ANGLE_POS( Angle ) { \
  64. while( Angle < 0 ) \
  65. Angle += 3600; \
  66. while( Angle >= 3600 ) \
  67. Angle -= 3600; }
  68. #define NEGATE_AND_NORMALIZE_ANGLE_POS( Angle ) { \
  69. Angle = -Angle; \
  70. while( Angle < 0 ) \
  71. Angle += 3600; \
  72. while( Angle >= 3600 ) \
  73. Angle -= 3600; }
  74. /* Normalize angle to be in the -90.0 .. 90.0 range */
  75. #define NORMALIZE_ANGLE_90( Angle ) { \
  76. while( Angle < -900 ) \
  77. Angle += 1800; \
  78. while( Angle > 900 ) \
  79. Angle -= 1800; }
  80. /* Normalize angle to be in the -180.0 .. 180.0 range */
  81. #define NORMALIZE_ANGLE_180( Angle ) { \
  82. while( Angle <= -1800 ) \
  83. Angle += 3600; \
  84. while( Angle > 1800 ) \
  85. Angle -= 3600; }
  86. /*****************************/
  87. /* macro to exchange 2 items */
  88. /*****************************/
  89. /*
  90. * The EXCHG macro uses BOOST_TYPEOF for compilers that do not have native
  91. * typeof support (MSVC). Please do not attempt to qualify these macros
  92. * within #ifdef compiler definitions pragmas. BOOST_TYPEOF is smart enough
  93. * to check for native typeof support and use it instead of it's own
  94. * implementation. These macros effectively compile to nothing on platforms
  95. * with native typeof support.
  96. */
  97. #include <boost/typeof/typeof.hpp>
  98. // we have to register the types used with the typeof keyword with boost
  99. BOOST_TYPEOF_REGISTER_TYPE( wxPoint )
  100. BOOST_TYPEOF_REGISTER_TYPE( wxSize )
  101. BOOST_TYPEOF_REGISTER_TYPE( wxString )
  102. class DrawSheetLabelStruct;
  103. BOOST_TYPEOF_REGISTER_TYPE( DrawSheetLabelStruct* )
  104. class EDA_ITEM;
  105. BOOST_TYPEOF_REGISTER_TYPE( EDA_ITEM* )
  106. class D_PAD;
  107. BOOST_TYPEOF_REGISTER_TYPE( D_PAD* )
  108. BOOST_TYPEOF_REGISTER_TYPE( const D_PAD* )
  109. class BOARD_ITEM;
  110. BOOST_TYPEOF_REGISTER_TYPE( BOARD_ITEM* )
  111. #define EXCHG( a, b ) { BOOST_TYPEOF( a ) __temp__ = (a); \
  112. (a) = (b); \
  113. (b) = __temp__; }
  114. #endif /* ifdef MACRO_H */