common.cpp 6.75 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
// Show warning if wxWidgets Gnome or GTK printing was not configured.
44
// Since wxWidgets 3.0, this is no more needed (build in printing works!)
45
#if defined( __WXGTK__ )
46 47 48 49 50
    #if !wxCHECK_VERSION( 3, 0, 0 )
    #   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
51 52
#endif

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

charras's avatar
charras committed
61 62
bool           g_ShowPageLimits = true;
wxString       g_UserLibDirBuffer;
63

64
EDA_UNITS_T    g_UserUnit;
65
EDA_COLOR_T    g_GhostColor;
66

67

68 69
/**
 * Function to use local notation or C standard notation for floating point numbers
70 71 72 73 74
 * 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
75
 * Very annoying and we detect this and use a stupid but necessary workaround
76 77 78
*/
bool g_DisableFloatingPointLocalNotation = false;

79

Dick Hollenbeck's avatar
Dick Hollenbeck committed
80 81 82 83
int LOCALE_IO::C_count;


void SetLocaleTo_C_standard()
charras's avatar
charras committed
84 85 86 87
{
    setlocale( LC_NUMERIC, "C" );    // Switch the locale to standard C
}

Dick Hollenbeck's avatar
Dick Hollenbeck committed
88
void SetLocaleTo_Default()
charras's avatar
charras committed
89
{
Dick Hollenbeck's avatar
Dick Hollenbeck committed
90
    if( !g_DisableFloatingPointLocalNotation )
91
        setlocale( LC_NUMERIC, "" );      // revert to the current locale
charras's avatar
charras committed
92 93
}

94

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
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 );
}


110
bool EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString )
111
{
charras's avatar
charras committed
112 113
    wxWindow* window = aCtrl->GetParent();

114 115 116
    if( !window )
        window = aCtrl;

charras's avatar
charras committed
117
    wxString ctrlText;
118 119 120 121

    if( !aString )
    {
        ctrlText = aCtrl->GetValue();
charras's avatar
charras committed
122
        aString  = &ctrlText;
123 124
    }

125 126
    wxSize  textz = GetTextSize( *aString, window );
    wxSize  ctrlz = aCtrl->GetSize();
127

128
    if( ctrlz.GetWidth() < textz.GetWidth() + 10 )
129
    {
130 131
        ctrlz.SetWidth( textz.GetWidth() + 10 );
        aCtrl->SetSizeHints( ctrlz );
132 133
        return true;
    }
134

135 136 137 138
    return false;
}


139
wxString ReturnUnitSymbol( EDA_UNITS_T aUnit, const wxString& formatString )
140
{
141
    wxString tmp;
142 143
    wxString label;

144
    switch( aUnit )
145 146
    {
    case INCHES:
147
        tmp = _( "\"" );
148 149
        break;

150
    case MILLIMETRES:
151
        tmp = _( "mm" );
152 153
        break;

154
    case UNSCALED_UNITS:
155 156 157
        break;
    }

158 159 160 161 162
    if( formatString.IsEmpty() )
        return tmp;

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

163
    return label;
164 165
}

166

167
wxString GetUnitsLabel( EDA_UNITS_T aUnit )
168 169 170
{
    wxString label;

171
    switch( aUnit )
172 173 174 175 176
    {
    case INCHES:
        label = _( "inches" );
        break;

177
    case MILLIMETRES:
178 179 180
        label = _( "millimeters" );
        break;

181 182
    case UNSCALED_UNITS:
        label = _( "units" );
183 184 185 186 187 188
        break;
    }

    return label;
}

189

190
wxString GetAbbreviatedUnitsLabel( EDA_UNITS_T aUnit )
191 192 193
{
    wxString label;

194
    switch( aUnit )
195 196 197 198 199
    {
    case INCHES:
        label = _( "in" );
        break;

200
    case MILLIMETRES:
201 202 203
        label = _( "mm" );
        break;

204
    case UNSCALED_UNITS:
205 206 207 208 209 210 211
        break;
    }

    return label;
}


212
void AddUnitSymbol( wxStaticText& Stext, EDA_UNITS_T aUnit )
213
{
charras's avatar
charras committed
214
    wxString msg = Stext.GetLabel();
215 216

    msg += ReturnUnitSymbol( aUnit );
217 218

    Stext.SetLabel( msg );
219 220
}

221

222
wxArrayString* wxStringSplit( wxString aString, wxChar aSplitter )
223 224
{
    wxArrayString* list = new wxArrayString();
charras's avatar
charras committed
225 226

    while( 1 )
227
    {
228 229
        int index = aString.Find( aSplitter );

charras's avatar
charras committed
230 231
        if( index == wxNOT_FOUND )
            break;
232

charras's avatar
charras committed
233
        wxString tmp;
234 235
        tmp = aString.Mid( 0, index );
        aString = aString.Mid( index + 1, aString.size() - index );
charras's avatar
charras committed
236
        list->Add( tmp );
237
    }
238

239
    if( !aString.IsEmpty() )
240
    {
241
        list->Add( aString );
242
    }
243

244 245 246
    return list;
}

247

248
int ProcessExecute( const wxString& aCommandLine, int aFlags, wxProcess *callback )
249
{
250
    return wxExecute( aCommandLine, aFlags, callback );
251 252 253
}


254
time_t GetNewTimeStamp()
255
{
256 257
    static time_t oldTimeStamp;
    time_t newTimeStamp;
258

259
    newTimeStamp = time( NULL );
260

261 262
    if( newTimeStamp <= oldTimeStamp )
        newTimeStamp = oldTimeStamp + 1;
263

264
    oldTimeStamp = newTimeStamp;
265

266
    return newTimeStamp;
267 268
}

269

270 271 272
double RoundTo0( double x, double precision )
{
    assert( precision != 0 );
273

274
    long long ix = KiROUND( x * precision );
275

276 277
    if ( x < 0.0 )
        NEGATE( ix );
278

279
    int remainder = ix % 10;   // remainder is in precision mm
280

281 282 283 284
    if ( remainder <= 2 )
        ix -= remainder;       // truncate to the near number
    else if (remainder >= 8 )
        ix += 10 - remainder;  // round to near number
285

286 287 288 289
    if ( x < 0 )
        NEGATE( ix );

    return (double) ix / precision;
290
}
291 292 293 294 295 296 297 298 299 300 301 302

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
}