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.

254 lines
8.3 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
15 years ago
15 years ago
15 years ago
15 years ago
  1. /**************************************/
  2. /* Useful macros and inline functions */
  3. /**************************************/
  4. #ifndef MACROS_H
  5. #define MACROS_H
  6. #include <wx/wx.h>
  7. /**
  8. * Macro TO_UTF8
  9. * converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
  10. * wxstring is a wxString, not a wxT() or _(). The scope of the return value
  11. * is very limited and volatile, but can be used with printf() style functions well.
  12. */
  13. #define TO_UTF8( wxstring ) ( (const char*) (wxstring).utf8_str() )
  14. /**
  15. * Macro FROM_UTF8
  16. * converts a UTF8 encoded C string to a wxString for all wxWidgets build modes.
  17. */
  18. //#define FROM_UTF8( cstring ) wxString::FromUTF8( cstring )
  19. static inline wxString FROM_UTF8( const char* cstring )
  20. {
  21. wxString line = wxString::FromUTF8( cstring );
  22. if( line.IsEmpty() ) // happens when cstring is not a valid UTF8 sequence
  23. line = wxConvCurrent->cMB2WC( cstring ); // try to use locale conversion
  24. return line;
  25. }
  26. /**
  27. * Function GetChars
  28. * returns a wxChar* to the actual character data within a wxString, and is
  29. * helpful for passing strings to wxString::Printf(wxT("%s"), GetChars(wxString) )
  30. * <p>
  31. * wxChar is defined to be
  32. * <ul>
  33. * <li> standard C style char when wxUSE_UNICODE==0 </li>
  34. * <li> wchar_t when wxUSE_UNICODE==1 (the default). </li>
  35. * </ul>
  36. * i.e. it depends on how the wxWidgets library was compiled. There was a period
  37. * during the development of wxWidgets 2.9 when GetData() was missing, so this
  38. * function was used to provide insulation from that design change. It may
  39. * no longer be needed, and is harmless. GetData() seems to be an acceptable
  40. * alternative in all cases now.
  41. */
  42. static inline const wxChar* GetChars( const wxString& s )
  43. {
  44. #if wxCHECK_VERSION( 2, 9, 0 )
  45. return (const wxChar*) s.c_str();
  46. #else
  47. return s.GetData();
  48. #endif
  49. }
  50. #ifndef MIN
  51. #define MIN( x, y ) ( (x) > (y) ? (y) : (x) )
  52. #endif
  53. #ifndef MAX
  54. #define MAX( x, y ) ( (x) > (y) ? (x) : (y) )
  55. #endif
  56. #ifndef ABS
  57. #define ABS( y ) ( (y) >= 0 ? (y) : ( -(y) ) )
  58. #endif
  59. #define NEGATE( x ) (x = -x)
  60. /// # of elements in an arrray
  61. #define DIM( x ) unsigned( sizeof(x) / sizeof( (x)[0] ) ) // not size_t
  62. #define DEG2RAD( Deg ) ( (Deg) * M_PI / 180.0 )
  63. #define RAD2DEG( Rad ) ( (Rad) * 180.0 / M_PI )
  64. // Normalize angle to be in the -360.0 .. 360.0:
  65. #define NORMALIZE_ANGLE_360( Angle ) { while( Angle < -3600 ) \
  66. Angle += 3600;\
  67. while( Angle > 3600 ) \
  68. Angle -= 3600;}
  69. /* Normalize angle to be in the 0.0 .. 360.0 range: */
  70. #define NORMALIZE_ANGLE_POS( Angle ) { while( Angle < 0 ) \
  71. Angle += 3600;\
  72. while( Angle >= 3600 ) \
  73. Angle -= 3600;}
  74. #define NEGATE_AND_NORMALIZE_ANGLE_POS( Angle ) \
  75. { Angle = -Angle; while( Angle < 0 ) \
  76. Angle += 3600;while( Angle >= 3600 ) \
  77. Angle -= 3600;}
  78. /* Normalize angle to be in the -90.0 .. 90.0 range */
  79. #define NORMALIZE_ANGLE_90( Angle ) { while( Angle < -900 ) \
  80. Angle += 1800;\
  81. while( Angle > 900 ) \
  82. Angle -= 1800;}
  83. /* Normalize angle to be in the -180.0 .. 180.0 range */
  84. #define NORMALIZE_ANGLE_180( Angle ) { while( Angle <= -1800 ) \
  85. Angle += 3600;\
  86. while( Angle > 1800 ) \
  87. Angle -= 3600;}
  88. /*****************************/
  89. /* macro to exchange 2 items */
  90. /*****************************/
  91. /*
  92. * The EXCHG macro uses BOOST_TYPEOF for compilers that do not have native
  93. * typeof support (MSVC). Please do not attempt to qualify these macros
  94. * within #ifdef compiler definitions pragmas. BOOST_TYPEOF is smart enough
  95. * to check for native typeof support and use it instead of it's own
  96. * implementation. These macros effectively compile to nothing on platforms
  97. * with native typeof support.
  98. */
  99. #include "boost/typeof/typeof.hpp"
  100. // we have to register the types used with the typeof keyword with boost
  101. BOOST_TYPEOF_REGISTER_TYPE( wxPoint )
  102. BOOST_TYPEOF_REGISTER_TYPE( wxSize )
  103. BOOST_TYPEOF_REGISTER_TYPE( wxString )
  104. class DrawSheetLabelStruct;
  105. BOOST_TYPEOF_REGISTER_TYPE( DrawSheetLabelStruct* )
  106. class EDA_ITEM;
  107. BOOST_TYPEOF_REGISTER_TYPE( EDA_ITEM* )
  108. class D_PAD;
  109. BOOST_TYPEOF_REGISTER_TYPE( D_PAD* )
  110. BOOST_TYPEOF_REGISTER_TYPE( const D_PAD* )
  111. class BOARD_ITEM;
  112. BOOST_TYPEOF_REGISTER_TYPE( BOARD_ITEM* )
  113. #define EXCHG( a, b ) { BOOST_TYPEOF( a ) __temp__ = (a); \
  114. (a) = (b); \
  115. (b) = __temp__; }
  116. /*****************************************************/
  117. /* inline functions to insert menuitems with a icon: */
  118. /*****************************************************/
  119. static inline void ADD_MENUITEM( wxMenu* menu, int id,
  120. const wxString& text,
  121. const wxBitmap& icon )
  122. {
  123. wxMenuItem* l_item;
  124. l_item = new wxMenuItem( menu, id, text );
  125. #if !defined( __WXMAC__ )
  126. l_item->SetBitmap( icon );
  127. #endif /* !defined( __WXMAC__ ) */
  128. menu->Append( l_item );
  129. }
  130. static inline void ADD_MENUITEM_WITH_HELP( wxMenu* menu, int id,
  131. const wxString& text,
  132. const wxString& help,
  133. const wxBitmap& icon )
  134. {
  135. wxMenuItem* l_item;
  136. l_item = new wxMenuItem( menu, id, text, help );
  137. #if !defined( __WXMAC__ )
  138. l_item->SetBitmap( icon );
  139. #endif /* !defined( __WXMAC__ ) */
  140. menu->Append( l_item );
  141. }
  142. #ifdef __WINDOWS__
  143. static inline void ADD_MENUITEM_WITH_SUBMENU( wxMenu* menu, wxMenu* submenu,
  144. int id, const wxString& text,
  145. const wxBitmap& icon )
  146. {
  147. wxMenuItem* l_item;
  148. l_item = new wxMenuItem( menu, id, text );
  149. l_item->SetSubMenu( submenu );
  150. l_item->SetBitmap( icon );
  151. menu->Append( l_item );
  152. };
  153. static inline void ADD_MENUITEM_WITH_HELP_AND_SUBMENU( wxMenu* menu,
  154. wxMenu* submenu,
  155. int id,
  156. const wxString& text,
  157. const wxString& help,
  158. const wxBitmap& icon )
  159. {
  160. wxMenuItem* l_item;
  161. l_item = new wxMenuItem( menu, id, text, help );
  162. l_item->SetSubMenu( submenu );
  163. l_item->SetBitmap( icon );
  164. menu->Append( l_item );
  165. };
  166. #else
  167. static inline void ADD_MENUITEM_WITH_SUBMENU( wxMenu* menu, wxMenu* submenu,
  168. int id,
  169. const wxString& text,
  170. const wxBitmap& icon )
  171. {
  172. wxMenuItem* l_item;
  173. l_item = new wxMenuItem( menu, id, text );
  174. l_item->SetSubMenu( submenu );
  175. #if !defined( __WXMAC__ )
  176. l_item->SetBitmap( icon );
  177. #endif /* !defined( __WXMAC__ ) */
  178. menu->Append( l_item );
  179. }
  180. static inline void ADD_MENUITEM_WITH_HELP_AND_SUBMENU( wxMenu* menu,
  181. wxMenu* submenu,
  182. int id,
  183. const wxString& text,
  184. const wxString& help,
  185. const wxBitmap& icon )
  186. {
  187. wxMenuItem* l_item;
  188. l_item = new wxMenuItem( menu, id, text, help );
  189. l_item->SetSubMenu( submenu );
  190. #if !defined( __WXMAC__ )
  191. l_item->SetBitmap( icon );
  192. #endif /* !defined( __WXMAC__ ) */
  193. menu->Append( l_item );
  194. }
  195. #endif
  196. // macro to add a bitmap list to check menus (do not use with normal menus)
  197. #ifdef __WINDOWS__
  198. # define SETBITMAPS( icon ) item->SetBitmaps( apply_xpm, (icon) )
  199. #else
  200. # define SETBITMAPS( icon )
  201. #endif
  202. // macro to add a bitmap menus (do not use with check menus)
  203. #ifdef __WXMAC__
  204. # define SET_BITMAP( icon )
  205. #else
  206. # define SET_BITMAP( icon ) item->SetBitmap( (icon) )
  207. #endif
  208. #endif /* ifdef MACRO_H */