common.cpp 9.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
 * Copyright (C) 2008-2011 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

/**
 * @file common.cpp
 */
29

30 31 32 33 34 35 36 37 38
#include <fctsys.h>
#include <gr_basic.h>
#include <trigo.h>
#include <wxstruct.h>
#include <base_struct.h>
#include <common.h>
#include <macros.h>
#include <build_version.h>
#include <confirm.h>
39 40
#include <base_units.h>

41
#include <wx/process.h>
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56

// Fail build if wxWidgets printing was not configured.
#if !wxUSE_PRINTING_ARCHITECTURE && !SWIG
#   error "You must use '--enable-printarch' in your wx library configuration."
#endif

// Show warning if wxWidgets Gnome or GTK printing was not configured.
#if defined( __WXGTK__ )
#   if !wxUSE_LIBGNOMEPRINT && !wxUSE_GTKPRINT && !SWIG
#       warning "You must use '--with-gnomeprint' or '--with-gtkprint' in your wx library configuration for full print capabilities."
#   endif
#endif


57 58 59
/**
 * Global variables definitions.
 *
60
 * TODO: All of these variables should be moved into the class were they
61 62 63 64
 *       are defined and used.  Most of them probably belong in the
 *       application class.
 */

charras's avatar
charras committed
65 66
bool           g_ShowPageLimits = true;
wxString       g_UserLibDirBuffer;
67

68
EDA_UNITS_T    g_UserUnit;
69
EDA_COLOR_T    g_GhostColor;
70

71

72 73
/**
 * The predefined colors used in KiCad.
74
 * Please: if you change a value, remember these values are carefully chosen
75
 * to have good results in Pcbnew, that uses the ORed value of basic colors
76
 * when displaying superimposed objects
77
 * This list must have exactly NBCOLORS items
78
 */
79
const StructColors g_ColorRefs[NBCOLORS] =
80
{
Lorenzo Marcantonio's avatar
Lorenzo Marcantonio committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    { 0,    0,   0,   BLACK,         wxT( "Black" ),     DARKDARKGRAY      },
    { 72,   72,  72,  DARKDARKGRAY,  wxT( "Gray 1" ),    DARKGRAY          },
    { 132,  132, 132, DARKGRAY,      wxT( "Gray 2" ),    LIGHTGRAY         },
    { 194,  194, 194, LIGHTGRAY,     wxT( "Gray 3" ),    WHITE             },
    { 255,  255, 255, WHITE,         wxT( "White" ),     WHITE             },
    { 194,  255, 255, LIGHTYELLOW,   wxT( "L.Yellow" ),  WHITE             },
    { 72,   0,   0,   DARKBLUE,      wxT( "Blue 1" ),    BLUE              },
    { 0,    72,  0,   DARKGREEN,     wxT( "Green 1" ),   GREEN             },
    { 72,   72,  0,   DARKCYAN,      wxT( "Cyan 1" ),    CYAN              },
    { 0,    0,   72,  DARKRED,       wxT( "Red 1" ),     RED               },
    { 72,   0,   72,  DARKMAGENTA,   wxT( "Magenta 1" ), MAGENTA           },
    { 0,    72,  72,  DARKBROWN,     wxT( "Brown 1" ),   BROWN             },
    { 132,  0,   0,   BLUE,          wxT( "Blue 2" ),    LIGHTBLUE         },
    { 0,    132, 0,   GREEN,         wxT( "Green 2" ),   LIGHTGREEN        },
    { 132,  132, 0,   CYAN,          wxT( "Cyan 2" ),    LIGHTCYAN         },
    { 0,    0,   132, RED,           wxT( "Red 2" ),     LIGHTRED          },
    { 132,  0,   132, MAGENTA,       wxT( "Magenta 2" ), LIGHTMAGENTA      },
    { 0,    132, 132, BROWN,         wxT( "Brown 2" ),   YELLOW            },
    { 194,  0,   0,   LIGHTBLUE,     wxT( "Blue 3" ),    PUREBLUE,         },
    { 0,    194, 0,   LIGHTGREEN,    wxT( "Green 3" ),   PUREGREEN         },
    { 194,  194, 0,   LIGHTCYAN,     wxT( "Cyan 3" ),    PURECYAN          },
    { 0,    0,   194, LIGHTRED,      wxT( "Red 3" ),     PURERED           },
    { 194,  0,   194, LIGHTMAGENTA,  wxT( "Magenta 3" ), PUREMAGENTA       },
    { 0,    194, 194, YELLOW,        wxT( "Yellow 3" ),  PUREYELLOW        },
    { 255,  0,   0,   PUREBLUE,      wxT( "Blue 4" ),    WHITE             },
    { 0,    255, 0,   PUREGREEN,     wxT( "Green 4" ),   WHITE             },
    { 255,  255, 0,   PURECYAN,      wxT( "Cyan 4" ),    WHITE             },
    { 0,    0,   255, PURERED,       wxT( "Red 4" ),     WHITE             },
    { 255,  0,   255, PUREMAGENTA,   wxT( "Magenta 4" ), WHITE             },
    { 0,    255, 255, PUREYELLOW,    wxT( "Yellow 4" ),  WHITE             },
111 112
};

113

114 115
/**
 * Function to use local notation or C standard notation for floating point numbers
116 117 118 119 120
 * some countries use 1,5 and others (and C) 1.5
 * so we switch from local to C and C to local when reading or writing files
 * And other problem is a bug when cross compiling under linux:
 * a printf print 1,5 and the read functions expects 1.5
 * (depending on version print = 1.5 and read = 1,5
121
 * Very annoying and we detect this and use a stupid but necessary workaround
122 123 124
*/
bool g_DisableFloatingPointLocalNotation = false;

125

Dick Hollenbeck's avatar
Dick Hollenbeck committed
126 127 128 129
int LOCALE_IO::C_count;


void SetLocaleTo_C_standard()
charras's avatar
charras committed
130 131 132 133
{
    setlocale( LC_NUMERIC, "C" );    // Switch the locale to standard C
}

Dick Hollenbeck's avatar
Dick Hollenbeck committed
134
void SetLocaleTo_Default()
charras's avatar
charras committed
135
{
Dick Hollenbeck's avatar
Dick Hollenbeck committed
136
    if( !g_DisableFloatingPointLocalNotation )
137
        setlocale( LC_NUMERIC, "" );      // revert to the current locale
charras's avatar
charras committed
138 139
}

140

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
wxSize GetTextSize( const wxString& aSingleLine, wxWindow* aWindow )
{
    wxCoord width;
    wxCoord height;

    {
        wxClientDC dc( aWindow );
        dc.SetFont( aWindow->GetFont() );
        dc.GetTextExtent( aSingleLine, &width, &height );
    }

    return wxSize( width, height );
}


156
bool EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString )
157
{
charras's avatar
charras committed
158 159
    wxWindow* window = aCtrl->GetParent();

160 161 162
    if( !window )
        window = aCtrl;

charras's avatar
charras committed
163
    wxString ctrlText;
164 165 166 167

    if( !aString )
    {
        ctrlText = aCtrl->GetValue();
charras's avatar
charras committed
168
        aString  = &ctrlText;
169 170
    }

171 172
    wxSize  textz = GetTextSize( *aString, window );
    wxSize  ctrlz = aCtrl->GetSize();
173

174
    if( ctrlz.GetWidth() < textz.GetWidth() + 10 )
175
    {
176 177
        ctrlz.SetWidth( textz.GetWidth() + 10 );
        aCtrl->SetSizeHints( ctrlz );
178 179
        return true;
    }
180

181 182 183 184
    return false;
}


185
wxString ReturnUnitSymbol( EDA_UNITS_T aUnit, const wxString& formatString )
186
{
187
    wxString tmp;
188 189
    wxString label;

190
    switch( aUnit )
191 192
    {
    case INCHES:
193
        tmp = _( "\"" );
194 195
        break;

196
    case MILLIMETRES:
197
        tmp = _( "mm" );
198 199
        break;

200
    case UNSCALED_UNITS:
201 202 203
        break;
    }

204 205 206 207 208
    if( formatString.IsEmpty() )
        return tmp;

    label.Printf( formatString, GetChars( tmp ) );

209
    return label;
210 211
}

212

213
wxString GetUnitsLabel( EDA_UNITS_T aUnit )
214 215 216
{
    wxString label;

217
    switch( aUnit )
218 219 220 221 222
    {
    case INCHES:
        label = _( "inches" );
        break;

223
    case MILLIMETRES:
224 225 226
        label = _( "millimeters" );
        break;

227 228
    case UNSCALED_UNITS:
        label = _( "units" );
229 230 231 232 233 234
        break;
    }

    return label;
}

235

236
wxString GetAbbreviatedUnitsLabel( EDA_UNITS_T aUnit )
237 238 239
{
    wxString label;

240
    switch( aUnit )
241 242 243 244 245
    {
    case INCHES:
        label = _( "in" );
        break;

246
    case MILLIMETRES:
247 248 249
        label = _( "mm" );
        break;

250
    case UNSCALED_UNITS:
251 252 253 254 255 256 257
        break;
    }

    return label;
}


258
void AddUnitSymbol( wxStaticText& Stext, EDA_UNITS_T aUnit )
259
{
charras's avatar
charras committed
260
    wxString msg = Stext.GetLabel();
261 262

    msg += ReturnUnitSymbol( aUnit );
263 264

    Stext.SetLabel( msg );
265 266
}

267

268
wxArrayString* wxStringSplit( wxString aString, wxChar aSplitter )
269 270
{
    wxArrayString* list = new wxArrayString();
charras's avatar
charras committed
271 272

    while( 1 )
273
    {
274 275
        int index = aString.Find( aSplitter );

charras's avatar
charras committed
276 277
        if( index == wxNOT_FOUND )
            break;
278

charras's avatar
charras committed
279
        wxString tmp;
280 281
        tmp = aString.Mid( 0, index );
        aString = aString.Mid( index + 1, aString.size() - index );
charras's avatar
charras committed
282
        list->Add( tmp );
283
    }
284

285
    if( !aString.IsEmpty() )
286
    {
287
        list->Add( aString );
288
    }
289

290 291 292
    return list;
}

293

294
int ProcessExecute( const wxString& aCommandLine, int aFlags, wxProcess *callback )
295
{
296
    return wxExecute( aCommandLine, aFlags, callback );
297 298 299
}


300
time_t GetNewTimeStamp()
301
{
302 303
    static time_t oldTimeStamp;
    time_t newTimeStamp;
304

305
    newTimeStamp = time( NULL );
306

307 308
    if( newTimeStamp <= oldTimeStamp )
        newTimeStamp = oldTimeStamp + 1;
309

310
    oldTimeStamp = newTimeStamp;
311

312
    return newTimeStamp;
313 314
}

315

316 317 318
double RoundTo0( double x, double precision )
{
    assert( precision != 0 );
319

320
    long long ix = KiROUND( x * precision );
321

322 323
    if ( x < 0.0 )
        NEGATE( ix );
324

325
    int remainder = ix % 10;   // remainder is in precision mm
326

327 328 329 330
    if ( remainder <= 2 )
        ix -= remainder;       // truncate to the near number
    else if (remainder >= 8 )
        ix += 10 - remainder;  // round to near number
331

332 333 334 335
    if ( x < 0 )
        NEGATE( ix );

    return (double) ix / precision;
336
}
337 338 339 340 341 342 343 344 345 346 347 348

wxString FormatDateLong( const wxDateTime &aDate )
{
    /* GetInfo was introduced only on wx 2.9; for portability reason an
     * hardcoded format is used on wx 2.8 */
#if wxCHECK_VERSION( 2, 9, 0 )
    return aDate.Format( wxLocale::GetInfo( wxLOCALE_LONG_DATE_FMT ) );
#else
    return aDate.Format( wxT("%d %b %Y") );
#endif
}