macros.h 7.57 KB
Newer Older
1 2 3
/**************************************/
/* Useful macros and inline functions */
/**************************************/
plyatov's avatar
plyatov committed
4 5 6

#ifndef MACROS_H
#define MACROS_H
7 8

#if wxUSE_UNICODE
9 10
#define CONV_TO_UTF8( wxstring )     ( (const char*) wxConvCurrent->cWX2MB( wxstring ) )
#define CONV_FROM_UTF8( utf8string ) ( wxConvCurrent->cMB2WC( utf8string ) )
11
#else
dickelbeck's avatar
dickelbeck committed
12
#define CONV_TO_UTF8( wxstring )     ( (const char*) ( (wxstring).c_str() ) )
13
#define CONV_FROM_UTF8( utf8string ) (utf8string)
14
#endif
plyatov's avatar
plyatov committed
15 16


dickelbeck's avatar
dickelbeck committed
17 18
/**
 * Function GetChars
19 20 21 22 23 24 25 26 27 28 29 30
 * returns a wxChar* to the actual character data within a wxString, and is
 * helpful for passing strings to wxString::Printf(wxT("%s"), GetChars(wxString) )
 * <p>
 * wxChar is defined to be <ul>
 * <li> standard C style char when wxUSE_UNICODE==0 </li>
 * <li> wchar_t when wxUSE_UNICODE==1 (the default). </li>
 * <ul>
 * i.e. it depends on how the wxWidgets library was compiled.  There was a period
 * during the development of wxWidgets 2.9 when GetData() was missing, so this
 * function was used to provide insulation from that design change.  It may
 * no longer be needed, and is harmless.  GetData() seems to be an acceptable
 * alternative in all cases now.
dickelbeck's avatar
dickelbeck committed
31
 */
32
static inline const wxChar* GetChars( const wxString& s )
dickelbeck's avatar
dickelbeck committed
33
{
34 35
#if wxCHECK_VERSION( 2, 9, 0 )
    return (const wxChar*) s.c_str();
dickelbeck's avatar
dickelbeck committed
36 37 38 39 40 41
#else
    return s.GetData();
#endif
}


plyatov's avatar
plyatov committed
42
#ifndef MIN
43
#define MIN( x, y ) ( (x) > (y) ? (y) : (x) )
plyatov's avatar
plyatov committed
44 45
#endif
#ifndef MAX
46
#define MAX( x, y ) ( (x) > (y) ? (x) : (y) )
plyatov's avatar
plyatov committed
47 48 49
#endif

#ifndef ABS
50
#define ABS( y ) ( (y) >= 0 ? (y) : ( -(y) ) )
plyatov's avatar
plyatov committed
51 52
#endif

53 54
#define NEGATE( x ) (x = -x)

55
/// # of elements in an arrray
56
#define DIM( x )    unsigned( sizeof(x) / sizeof( (x)[0] ) )    // not size_t
57 58


59 60
#define DEG2RAD( Deg ) ( (Deg) * M_PI / 180.0 )
#define RAD2DEG( Rad ) ( (Rad) * 180.0 / M_PI )
plyatov's avatar
plyatov committed
61 62

/* Normalize angle to be in the -360.0 .. 360.0 range or 0 .. 360.0: */
63 64 65
#define NORMALIZE_ANGLE( Angle ) { while( Angle < 0 ) \
                                       Angle += 3600;\
                                   while( Angle > 3600 ) \
66
                                       Angle -= 3600;}
plyatov's avatar
plyatov committed
67 68

/* Normalize angle to be in the 0.0 .. 360.0 range: */
69 70
#define NORMALIZE_ANGLE_POS( Angle ) { while( Angle < 0 ) \
                                           Angle += 3600;while( Angle >= 3600 ) \
71
                                           Angle -= 3600;}
72 73 74
#define NEGATE_AND_NORMALIZE_ANGLE_POS( Angle ) \
    { Angle = -Angle; while( Angle < 0 ) \
          Angle += 3600;while( Angle >= 3600 ) \
75
          Angle -= 3600;}
plyatov's avatar
plyatov committed
76

77
/* Normalize angle to be in the -90.0 .. 90.0 range */
78 79 80
#define NORMALIZE_ANGLE_90( Angle ) { while( Angle < -900 ) \
                                          Angle += 1800;\
                                      while( Angle > 900 ) \
81
                                          Angle -= 1800;}
82

83 84
/* Normalize angle to be in the -180.0 .. 180.0 range */
#define NORMALIZE_ANGLE_180( Angle ) { while( Angle <= -1800 ) \
85 86 87
                                           Angle += 3600;\
                                       while( Angle > 1800 ) \
                                           Angle -= 3600;}
88 89 90 91 92

/*****************************/
/* macro to exchange 2 items */
/*****************************/

93 94 95 96 97 98 99
/*
 * The EXCHG macro uses BOOST_TYPEOF for compilers that do not have native
 * typeof support (MSVC).  Please do not attempt to qualify these macros
 * within #ifdef compiler definitions pragmas.  BOOST_TYPEOF is smart enough
 * to check for native typeof support and use it instead of it's own
 * implementation.  These macros effectively compile to nothing on platforms
 * with native typeof support.
100
 */
101

102 103 104
#include "boost/typeof/typeof.hpp"

// we have to register the types used with the typeof keyword with boost
jean-pierre charras's avatar
jean-pierre charras committed
105 106 107
BOOST_TYPEOF_REGISTER_TYPE( wxPoint )
BOOST_TYPEOF_REGISTER_TYPE( wxSize )
BOOST_TYPEOF_REGISTER_TYPE( wxString )
108
class DrawSheetLabelStruct;
jean-pierre charras's avatar
jean-pierre charras committed
109
BOOST_TYPEOF_REGISTER_TYPE( DrawSheetLabelStruct* )
110
class EDA_BaseStruct;
jean-pierre charras's avatar
jean-pierre charras committed
111
BOOST_TYPEOF_REGISTER_TYPE( EDA_BaseStruct* )
112
class D_PAD;
jean-pierre charras's avatar
jean-pierre charras committed
113 114
BOOST_TYPEOF_REGISTER_TYPE( D_PAD* )
BOOST_TYPEOF_REGISTER_TYPE( const D_PAD* )
115
class BOARD_ITEM;
jean-pierre charras's avatar
jean-pierre charras committed
116
BOOST_TYPEOF_REGISTER_TYPE( BOARD_ITEM* )
117

118
#define EXCHG( a, b ) { BOOST_TYPEOF( a ) __temp__ = (a);      \
119 120
                        (a) = (b);                           \
                        (b) = __temp__; }
121

122 123

/*****************************************************/
124
/* inline functions to insert menuitems with a icon: */
125
/*****************************************************/
126 127
static inline void ADD_MENUITEM( wxMenu* menu, int id,
                                 const wxString& text,
128
                                 const wxBitmap& icon )
129 130 131 132
{
    wxMenuItem* l_item;

    l_item = new wxMenuItem( menu, id, text );
133 134

#if !defined( __WXMAC__ )
135
    l_item->SetBitmap( icon );
136 137
#endif /* !defined( __WXMAC__ ) */

138
    menu->Append( l_item );
jean-pierre charras's avatar
jean-pierre charras committed
139
}
140 141 142 143 144 145 146 147 148

static inline void ADD_MENUITEM_WITH_HELP( wxMenu* menu, int id,
                                           const wxString& text,
                                           const wxString& help,
                                           const wxBitmap& icon )
{
    wxMenuItem* l_item;

    l_item = new wxMenuItem( menu, id, text, help );
149 150

#if !defined( __WXMAC__ )
151
    l_item->SetBitmap( icon );
152 153
#endif /* !defined( __WXMAC__ ) */

154
    menu->Append( l_item );
jean-pierre charras's avatar
jean-pierre charras committed
155
}
156

plyatov's avatar
plyatov committed
157
#ifdef __WINDOWS__
158 159 160 161
static inline void ADD_MENUITEM_WITH_SUBMENU( wxMenu* menu, wxMenu* submenu,
                                              int id, const wxString& text,
                                              const wxBitmap& icon )
{
162
    wxMenuItem* l_item;
163 164 165 166 167 168 169

    l_item = new wxMenuItem( menu, id, text );
    l_item->SetSubMenu( submenu );
    l_item->SetBitmap( icon );
    menu->Append( l_item );
};

170 171
static inline void ADD_MENUITEM_WITH_HELP_AND_SUBMENU( wxMenu*         menu,
                                                       wxMenu*         submenu,
172 173 174
                                                       int             id,
                                                       const wxString& text,
                                                       const wxString& help,
175
                                                       const wxBitmap& icon )
176
{
177
    wxMenuItem* l_item;
178 179 180 181 182 183 184

    l_item = new wxMenuItem( menu, id, text, help );
    l_item->SetSubMenu( submenu );
    l_item->SetBitmap( icon );
    menu->Append( l_item );
};

plyatov's avatar
plyatov committed
185
#else
186 187 188 189 190 191 192 193 194
static inline void ADD_MENUITEM_WITH_SUBMENU( wxMenu* menu, wxMenu* submenu,
                                              int id,
                                              const wxString& text,
                                              const wxBitmap& icon )
{
    wxMenuItem* l_item;

    l_item = new wxMenuItem( menu, id, text );
    l_item->SetSubMenu( submenu );
195 196

#if !defined( __WXMAC__ )
197
    l_item->SetBitmap( icon );
198 199
#endif /* !defined( __WXMAC__ ) */

200
    menu->Append( l_item );
jean-pierre charras's avatar
jean-pierre charras committed
201
}
202 203 204 205 206 207

static inline void ADD_MENUITEM_WITH_HELP_AND_SUBMENU( wxMenu*         menu,
                                                       wxMenu*         submenu,
                                                       int             id,
                                                       const wxString& text,
                                                       const wxString& help,
208
                                                       const wxBitmap& icon )
209 210 211 212 213
{
    wxMenuItem* l_item;

    l_item = new wxMenuItem( menu, id, text, help );
    l_item->SetSubMenu( submenu );
214 215

#if !defined( __WXMAC__ )
216
    l_item->SetBitmap( icon );
217 218
#endif /* !defined( __WXMAC__ ) */

219
    menu->Append( l_item );
jean-pierre charras's avatar
jean-pierre charras committed
220
}
221

plyatov's avatar
plyatov committed
222 223 224
#endif

#ifdef __WINDOWS__
225
#  define SETBITMAPS( icon ) item->SetBitmaps( apply_xpm, (icon) )
plyatov's avatar
plyatov committed
226
#else
227
#  define SETBITMAPS( icon )
plyatov's avatar
plyatov committed
228 229 230
#endif

#endif /* ifdef MACRO_H */