kicad/common/common.cpp

359 lines
9.2 KiB
C++
Raw Normal View History

/*
* 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
*/
#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>
#include <base_units.h>
2008-04-24 16:55:35 +00:00
#include <wx/process.h>
2008-01-05 17:20:46 +00:00
/**
* Global variables definitions.
*
* TODO: All if these variables should be moved into the class were they
* are defined and used. Most of them probably belong in the
* application class.
*/
2009-06-13 17:06:07 +00:00
wxString g_ProductName = wxT( "KiCad E.D.A. " );
bool g_ShowPageLimits = true;
wxString g_UserLibDirBuffer;
2009-06-13 17:06:07 +00:00
wxString g_Prj_Default_Config_FullFilename;
wxString g_Prj_Config_LocalFilename;
EDA_UNITS_T g_UserUnit;
EDA_COLOR_T g_GhostColor;
2012-03-26 21:45:05 +00:00
#if defined(KICAD_GOST)
static const bool s_gost = true;
2012-03-26 21:45:05 +00:00
#else
static const bool s_gost = false;
2012-03-26 21:45:05 +00:00
#endif
bool IsGOST()
{
return s_gost;
}
/**
* The predefined colors used in KiCad.
* Please: if you change a value, remember these values are carefully chosen
* to have good results in Pcbnew, that uses the ORed value of basic colors
* when displaying superimposed objects
* This list must have exactly NBCOLOR items
*/
StructColors ColorRefs[NBCOLOR] =
{
2009-06-13 17:06:07 +00:00
{ 0, 0, 0, BLACK, wxT( "BLACK" ), DARKDARKGRAY },
{ 192, 0, 0, BLUE, wxT( "BLUE" ), LIGHTBLUE },
{ 0, 160, 0, GREEN, wxT( "GREEN" ), LIGHTGREEN },
{ 160, 160, 0, CYAN, wxT( "CYAN" ), LIGHTCYAN },
{ 0, 0, 160, RED, wxT( "RED" ), LIGHTRED },
{ 160, 0, 160, MAGENTA, wxT( "MAGENTA" ), LIGHTMAGENTA },
{ 0, 128, 128, BROWN, wxT( "BROWN" ), YELLOW },
{ 192, 192, 192, LIGHTGRAY, wxT( "GRAY" ), WHITE },
{ 128, 128, 128, DARKGRAY, wxT( "DARKGRAY" ), LIGHTGRAY },
{ 255, 0, 0, LIGHTBLUE, wxT( "LIGHTBLUE" ), LIGHTBLUE },
{ 0, 255, 0, LIGHTGREEN, wxT( "LIGHTGREEN" ), LIGHTGREEN },
{ 255, 255, 0, LIGHTCYAN, wxT( "LIGHTCYAN" ), LIGHTCYAN },
{ 0, 0, 255, LIGHTRED, wxT( "LIGHTRED" ), LIGHTRED },
{ 255, 0, 255, LIGHTMAGENTA, wxT( "LIGHTMAGENTA" ), LIGHTMAGENTA },
{ 0, 255, 255, YELLOW, wxT( "YELLOW" ), YELLOW },
{ 255, 255, 255, WHITE, wxT( "WHITE" ), WHITE },
{ 64, 64, 64, DARKDARKGRAY, wxT( "DARKDARKGRAY" ), DARKGRAY },
{ 64, 0, 0, DARKBLUE, wxT( "DARKBLUE" ), BLUE },
{ 0, 64, 0, DARKGREEN, wxT( "DARKGREEN" ), GREEN },
{ 64, 64, 0, DARKCYAN, wxT( "DARKCYAN" ), CYAN },
{ 0, 0, 80, DARKRED, wxT( "DARKRED" ), RED },
{ 64, 0, 64, DARKMAGENTA, wxT( "DARKMAGENTA" ), MAGENTA },
{ 0, 64, 64, DARKBROWN, wxT( "DARKBROWN" ), BROWN },
{ 128, 255, 255, LIGHTYELLOW, wxT( "LIGHTYELLOW" ), LIGHTYELLOW }
};
/**
* Function to use local notation or C standard notation for floating point numbers
* 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
* Very annoying and we detect this and use a stupid but necessary workarount
*/
bool g_DisableFloatingPointLocalNotation = false;
2007-08-22 05:11:01 +00:00
2012-04-17 06:13:22 +00:00
int LOCALE_IO::C_count;
void SetLocaleTo_C_standard()
2008-06-06 16:39:45 +00:00
{
setlocale( LC_NUMERIC, "C" ); // Switch the locale to standard C
}
2012-04-17 06:13:22 +00:00
void SetLocaleTo_Default()
2008-06-06 16:39:45 +00:00
{
2012-04-17 06:13:22 +00:00
if( !g_DisableFloatingPointLocalNotation )
setlocale( LC_NUMERIC, "" ); // revert to the current locale
2008-06-06 16:39:45 +00:00
}
2008-09-09 11:32:21 +00:00
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 );
}
bool EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString )
{
2009-06-13 17:06:07 +00:00
wxWindow* window = aCtrl->GetParent();
if( !window )
window = aCtrl;
2009-06-13 17:06:07 +00:00
wxString ctrlText;
if( !aString )
{
ctrlText = aCtrl->GetValue();
2009-06-13 17:06:07 +00:00
aString = &ctrlText;
}
wxSize textz = GetTextSize( *aString, window );
wxSize ctrlz = aCtrl->GetSize();
if( ctrlz.GetWidth() < textz.GetWidth() + 10 )
{
ctrlz.SetWidth( textz.GetWidth() + 10 );
aCtrl->SetSizeHints( ctrlz );
return true;
}
return false;
}
wxString ReturnUnitSymbol( EDA_UNITS_T aUnit, const wxString& formatString )
{
wxString tmp;
2007-08-22 05:11:01 +00:00
wxString label;
switch( aUnit )
2007-08-22 05:11:01 +00:00
{
case INCHES:
tmp = _( "\"" );
2007-08-22 05:11:01 +00:00
break;
case MILLIMETRES:
tmp = _( "mm" );
2007-08-22 05:11:01 +00:00
break;
case UNSCALED_UNITS:
2007-08-22 05:11:01 +00:00
break;
}
if( formatString.IsEmpty() )
return tmp;
label.Printf( formatString, GetChars( tmp ) );
2007-08-22 05:11:01 +00:00
return label;
}
2007-08-22 05:11:01 +00:00
wxString GetUnitsLabel( EDA_UNITS_T aUnit )
{
wxString label;
switch( aUnit )
{
case INCHES:
label = _( "inches" );
break;
case MILLIMETRES:
label = _( "millimeters" );
break;
case UNSCALED_UNITS:
label = _( "units" );
break;
}
return label;
}
wxString GetAbbreviatedUnitsLabel( EDA_UNITS_T aUnit )
{
wxString label;
switch( aUnit )
{
case INCHES:
label = _( "in" );
break;
case MILLIMETRES:
label = _( "mm" );
break;
case UNSCALED_UNITS:
break;
}
return label;
}
void AddUnitSymbol( wxStaticText& Stext, EDA_UNITS_T aUnit )
{
2009-08-11 10:27:21 +00:00
wxString msg = Stext.GetLabel();
msg += ReturnUnitSymbol( aUnit );
2007-08-22 05:11:01 +00:00
Stext.SetLabel( msg );
}
2007-08-22 05:11:01 +00:00
wxArrayString* wxStringSplit( wxString aString, wxChar aSplitter )
{
wxArrayString* list = new wxArrayString();
2009-06-13 17:06:07 +00:00
while( 1 )
{
int index = aString.Find( aSplitter );
2009-06-13 17:06:07 +00:00
if( index == wxNOT_FOUND )
break;
2009-05-02 07:35:04 +00:00
2009-06-13 17:06:07 +00:00
wxString tmp;
tmp = aString.Mid( 0, index );
aString = aString.Mid( index + 1, aString.size() - index );
2009-06-13 17:06:07 +00:00
list->Add( tmp );
}
2009-05-02 07:35:04 +00:00
if( !aString.IsEmpty() )
{
list->Add( aString );
}
2009-05-02 07:35:04 +00:00
return list;
}
2007-08-22 05:11:01 +00:00
/*
* Return the string date "day month year" like "23 jun 2005"
*/
wxString GenDate()
{
2007-08-22 05:11:01 +00:00
static const wxString mois[12] =
{
wxT( "jan" ), wxT( "feb" ), wxT( "mar" ), wxT( "apr" ), wxT( "may" ), wxT( "jun" ),
wxT( "jul" ), wxT( "aug" ), wxT( "sep" ), wxT( "oct" ), wxT( "nov" ), wxT( "dec" )
};
2008-04-24 16:55:35 +00:00
2008-09-09 11:32:21 +00:00
time_t buftime;
struct tm* Date;
wxString string_date;
2007-08-22 05:11:01 +00:00
time( &buftime );
2007-08-22 05:11:01 +00:00
Date = gmtime( &buftime );
string_date.Printf( wxT( "%d %s %d" ), Date->tm_mday,
GetChars( mois[Date->tm_mon] ),
2009-06-13 17:06:07 +00:00
Date->tm_year + 1900 );
2007-08-22 05:11:01 +00:00
return string_date;
}
2007-08-22 05:11:01 +00:00
int ProcessExecute( const wxString& aCommandLine, int aFlags, wxProcess *callback )
2008-04-24 16:55:35 +00:00
{
return wxExecute( aCommandLine, aFlags, callback );
2008-04-24 16:55:35 +00:00
}
time_t GetNewTimeStamp()
{
static time_t oldTimeStamp;
time_t newTimeStamp;
newTimeStamp = time( NULL );
if( newTimeStamp <= oldTimeStamp )
newTimeStamp = oldTimeStamp + 1;
oldTimeStamp = newTimeStamp;
return newTimeStamp;
}
2007-08-22 05:11:01 +00:00
double RoundTo0( double x, double precision )
{
assert( precision != 0 );
// Dick Hollenbeck's KiROUND R&D // This provides better project control over rounding to int from double // than wxRound() did. This scheme provides better logging in Debug builds // and it provides for compile time calculation of constants. #include <stdio.h> #include <assert.h> #include <limits.h> //-----<KiROUND KIT>------------------------------------------------------------ /** * KiROUND * rounds a floating point number to an int using * "round halfway cases away from zero". * In Debug build an assert fires if will not fit into an int. */ #if defined( DEBUG ) // DEBUG: a macro to capture line and file, then calls this inline static inline int KiRound( double v, int line, const char* filename ) { v = v < 0 ? v - 0.5 : v + 0.5; if( v > INT_MAX + 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v ); } else if( v < INT_MIN - 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v ); } return int( v ); } #define KiROUND( v ) KiRound( v, __LINE__, __FILE__ ) #else // RELEASE: a macro so compile can pre-compute constants. #define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) #endif //-----</KiROUND KIT>----------------------------------------------------------- // Only a macro is compile time calculated, an inline function causes a static constructor // in a situation like this. // Therefore the Release build is best done with a MACRO not an inline function. int Computed = KiROUND( 14.3 * 8 ); int main( int argc, char** argv ) { for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 ) { int i = KiROUND( d ); printf( "t: %d %.16g\n", i, d ); } return 0; }
2012-04-19 06:55:45 +00:00
long long ix = KiROUND( x * precision );
if ( x < 0.0 )
NEGATE( ix );
int remainder = ix % 10; // remainder is in precision mm
2008-05-05 19:43:56 +00:00
if ( remainder <= 2 )
ix -= remainder; // truncate to the near number
else if (remainder >= 8 )
ix += 10 - remainder; // round to near number
2008-05-05 19:43:56 +00:00
if ( x < 0 )
NEGATE( ix );
return (double) ix / precision;
2008-05-05 19:43:56 +00:00
}