kicad/common/common.cpp

525 lines
15 KiB
C++
Raw Normal View History

2007-08-22 05:11:01 +00:00
/**********************************************************/
/* Routines d'affichage de parametres et caracteristiques */
/**********************************************************/
2007-08-22 05:11:01 +00:00
/* Fichier 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"
2008-04-24 16:55:35 +00:00
#include <wx/process.h>
2008-01-05 17:20:46 +00:00
/*****************************/
2007-09-01 12:00:30 +00:00
wxString GetBuildVersion()
/*****************************/
2007-08-22 05:11:01 +00:00
/* Return the build date
2007-08-22 05:11:01 +00:00
*/
{
2007-08-22 05:11:01 +00:00
return g_BuildVersion;
}
2007-08-22 05:11:01 +00:00
2008-06-06 16:39:45 +00:00
/********************************/
void SetLocaleTo_C_standard(void)
/********************************/
/** function SetLocaleTo_C_standard
because kicad is internationalized, switch internatization to "C" standard
i.e. uses the . (dot) as separator in print/read float numbers
(some contries (France, Germany ..) use , (comma) as separator)
This function must be called before read or write ascii files using float numbers in data
the SetLocaleTo_C_standard function must be called after reading or writing the file
This is wrapper to the C setlocale( LC_NUMERIC, "C" ) function,
but could make more easier an optional use of locale in kicad
*/
{
setlocale( LC_NUMERIC, "C" ); // Switch the locale to standard C
}
/********************************/
void SetLocaleTo_Default(void)
/********************************/
/** function SetLocaleTo_Default
because kicad is internationalized, switch internatization to default
to use the default separator in print/read float numbers
(. (dot) but some contries (France, Germany ..) use , (comma) as separator)
This function must be called after a call to SetLocaleTo_C_standard
This is wrapper to the C setlocale( LC_NUMERIC, "" ) function,
but could make more easier an optional use of locale in kicad
*/
{
setlocale( LC_NUMERIC, "" ); // revert to the current locale
}
/*********************************************************************************************/
2007-08-22 05:11:01 +00:00
Ki_PageDescr::Ki_PageDescr( const wxSize& size, const wxPoint& offset, const wxString& name )
/*********************************************************************************************/
{
2007-08-22 05:11:01 +00:00
// All sizes are in 1/1000 inch
2008-04-24 16:55:35 +00:00
m_Size = size;
m_Offset = offset;
m_Name = name;
2007-08-22 05:11:01 +00:00
// Adjust the default value for margins to 400 mils (0,4 inch or 10 mm)
m_LeftMargin = m_RightMargin = m_TopMargin = m_BottomMargin = 400;
2007-05-06 16:03:28 +00:00
}
2007-08-22 05:11:01 +00:00
/************************************/
2007-08-22 05:11:01 +00:00
wxString ReturnUnitSymbol( int Units )
/************************************/
{
2007-08-22 05:11:01 +00:00
wxString label;
switch( Units )
{
case INCHES:
label = _( " (\"):" );
break;
case MILLIMETRE:
label = _( " (mm):" );
break;
default:
break;
}
return label;
}
2007-08-22 05:11:01 +00:00
/**************************************************/
2007-08-22 05:11:01 +00:00
void AddUnitSymbol( wxStaticText& Stext, int Units )
/**************************************************/
2007-08-22 05:11:01 +00:00
/* Add string " (mm):" or " ("):" to the static text Stext.
2007-08-22 05:11:01 +00:00
* Used in dialog boxes for entering values depending on selected units
*/
{
2007-08-22 05:11:01 +00:00
wxString msg = Stext.GetLabel() + ReturnUnitSymbol( Units );
Stext.SetLabel( msg );
}
2007-08-22 05:11:01 +00:00
/****************************************************************************/
2007-08-22 05:11:01 +00:00
void PutValueInLocalUnits( wxTextCtrl& TextCtr, int Value, int Internal_Unit )
/****************************************************************************/
2007-08-22 05:11:01 +00:00
/* Convert the number Value in a string according to the internal units
2007-08-22 05:11:01 +00:00
* and the selected unit (g_UnitMetric) and put it in the wxTextCtrl TextCtrl
*/
{
2007-08-22 05:11:01 +00:00
wxString msg = ReturnStringFromValue( g_UnitMetric, Value, Internal_Unit );
TextCtr.SetValue( msg );
}
2007-08-22 05:11:01 +00:00
/*******************************************************************/
2007-08-22 05:11:01 +00:00
int ReturnValueFromTextCtrl( const wxTextCtrl& TextCtr, int Internal_Unit )
/********************************************************************/
2007-08-22 05:11:01 +00:00
/* Convert the Value in the wxTextCtrl TextCtrl in an integer,
2007-08-22 05:11:01 +00:00
* according to the internal units and the selected unit (g_UnitMetric)
*/
{
2007-08-22 05:11:01 +00:00
int value;
wxString msg = TextCtr.GetValue();
value = ReturnValueFromString( g_UnitMetric, msg, Internal_Unit );
return value;
}
2007-08-22 05:11:01 +00:00
/****************************************************************************/
2007-08-22 05:11:01 +00:00
wxString ReturnStringFromValue( int Units, int Value, int Internal_Unit )
/****************************************************************************/
2007-08-22 05:11:01 +00:00
/* Return the string from Value, according to units (inch, mm ...) for display,
2007-08-22 05:11:01 +00:00
* and the initial unit for value
* Unit = display units (INCH, MM ..)
* Value = value in Internal_Unit
* Internal_Unit = units per inch for Value
*/
{
2007-08-22 05:11:01 +00:00
wxString StringValue;
double value_to_print;
if( Units >= CENTIMETRE )
StringValue << Value;
else
{
value_to_print = To_User_Unit( Units, Value, Internal_Unit );
StringValue.Printf( ( Internal_Unit > 1000 ) ? wxT( "%.4f" ) : wxT( "%.3f" ),
value_to_print );
}
return StringValue;
}
2007-08-22 05:11:01 +00:00
/****************************************************************************/
2007-08-22 05:11:01 +00:00
int ReturnValueFromString( int Units, const wxString& TextValue, int Internal_Unit )
/****************************************************************************/
2007-08-22 05:11:01 +00:00
/* Return the string from Value, according to units (inch, mm ...) for display,
2007-08-22 05:11:01 +00:00
* and the initial unit for value
* Unit = display units (INCH, MM ..)
* Value = text
* Internal_Unit = units per inch for computed value
*/
{
2007-08-22 05:11:01 +00:00
int Value;
double dtmp = 0;
TextValue.ToDouble( &dtmp );
if( Units >= CENTIMETRE )
Value = (int) round( dtmp );
else
Value = From_User_Unit( Units, dtmp, Internal_Unit );
return Value;
}
2007-08-22 05:11:01 +00:00
/******************************************************************/
2007-08-22 05:11:01 +00:00
double To_User_Unit( bool is_metric, int val, int internal_unit_value )
/******************************************************************/
2007-08-22 05:11:01 +00:00
/* Convert in inch or mm the variable "val" given in internal units
2007-08-22 05:11:01 +00:00
*/
{
2007-08-22 05:11:01 +00:00
double value;
2007-08-22 05:11:01 +00:00
if( is_metric )
value = (double) (val) * 25.4 / internal_unit_value;
else
value = (double) (val) / internal_unit_value;
2007-08-22 05:11:01 +00:00
return value;
}
2007-08-22 05:11:01 +00:00
/**********************************************************************/
2007-08-22 05:11:01 +00:00
int From_User_Unit( bool is_metric, double val, int internal_unit_value )
/**********************************************************************/
2007-08-22 05:11:01 +00:00
/* Return in internal units the value "val" given in inch or mm
2007-08-22 05:11:01 +00:00
*/
{
2007-08-22 05:11:01 +00:00
double value;
2007-08-22 05:11:01 +00:00
if( is_metric )
value = val * internal_unit_value / 25.4;
else
value = val * internal_unit_value;
2007-08-22 05:11:01 +00:00
return (int) round( value );
}
2007-08-22 05:11:01 +00:00
/**********************/
2007-09-01 12:00:30 +00:00
wxString GenDate()
/**********************/
2007-08-22 05:11:01 +00:00
/* Return the string date "day month year" like "23 jun 2005"
2007-08-22 05:11:01 +00:00
*/
{
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
2007-08-22 05:11:01 +00:00
time_t buftime;
struct tm* Date;
wxString string_date;
time( &buftime );
Date = gmtime( &buftime );
string_date.Printf( wxT( "%d %s %d" ), Date->tm_mday,
mois[Date->tm_mon].GetData(),
Date->tm_year + 1900 );
return string_date;
}
2007-08-22 05:11:01 +00:00
/***********************************/
2007-08-22 05:11:01 +00:00
void* MyMalloc( size_t nb_octets )
/***********************************/
/* My memory allocation */
{
2007-08-22 05:11:01 +00:00
void* pt_mem;
if( nb_octets == 0 )
{
DisplayError( NULL, wxT( "Allocate 0 bytes !!" ) );
return NULL;
}
pt_mem = malloc( nb_octets );
if( pt_mem == NULL )
{
wxString msg;
msg.Printf( wxT( "Out of memory: allocation %d bytes" ), nb_octets );
DisplayError( NULL, msg );
}
return pt_mem;
}
2007-08-22 05:11:01 +00:00
2008-06-29 18:51:38 +00:00
/**************************************************************/
2008-04-24 16:55:35 +00:00
bool ProcessExecute( const wxString& aCommandLine, int aFlags )
2008-06-29 18:51:38 +00:00
/**************************************************************/
/**
* Function ProcessExecute
* runs a child process.
* @param aCommandLine The process and any arguments to it all in a single string.
* @param aFlags The same args as allowed for wxExecute()
* @return bool - true if success, else false
*/
2008-04-24 16:55:35 +00:00
{
#ifdef __WINDOWS__
2008-06-29 18:51:38 +00:00
int pid = wxExecute(aCommandLine);
return pid ? true : false;
#else
2008-04-24 16:55:35 +00:00
wxProcess* process = wxProcess::Open( aCommandLine, aFlags );
2008-06-29 18:51:38 +00:00
return (process != NULL) ? true : false;
#endif
2008-04-24 16:55:35 +00:00
}
/************************************/
2007-08-22 05:11:01 +00:00
void* MyZMalloc( size_t nb_octets )
/************************************/
2007-08-22 05:11:01 +00:00
/* My memory allocation, memory space is cleared
2007-08-22 05:11:01 +00:00
*/
{
2007-08-22 05:11:01 +00:00
void* pt_mem = MyMalloc( nb_octets );
if( pt_mem )
memset( pt_mem, 0, nb_octets );
return pt_mem;
}
2007-08-22 05:11:01 +00:00
/*******************************/
2007-08-22 05:11:01 +00:00
void MyFree( void* pt_mem )
/*******************************/
{
2007-08-22 05:11:01 +00:00
if( pt_mem )
free( pt_mem );
}
/**************************************************************/
wxString ReturnPcbLayerName( int layer_number, bool omitSpacePadding )
/**************************************************************/
2007-08-22 05:11:01 +00:00
/* Return the name of the layer number "layer_number".
2008-06-06 16:39:45 +00:00
* if omitSpacePadding == TRUE, the name can be used for a file name
* (no spaces, replaced by _)
2007-08-22 05:11:01 +00:00
*/
{
2008-01-05 17:20:46 +00:00
const unsigned LAYER_LIMIT = 29;
2008-03-04 04:22:27 +00:00
// These are only default layer names. For PCBNEW, the copper names
// may be over-ridden in the BOARD file *.brd.
2008-02-19 00:35:45 +00:00
2008-01-05 17:20:46 +00:00
static const wxString layer_name_list[] = {
_( "Copper " ), _( "Inner L1 " ), _( "Inner L2 " ), _( "Inner L3 " ),
_( "Inner L4 " ), _( "Inner L5 " ), _( "Inner L6 " ), _( "Inner L7 " ),
_( "Inner L8 " ), _( "Inner L9 " ), _( "Inner L10" ), _( "Inner L11" ),
_( "Inner L12" ), _( "Inner L13" ), _( "Inner L14" ), _( "Component" ),
_( "Adhes Cop" ), _( "Adhes Cmp" ), _( "SoldP Cop" ), _( "SoldP Cmp" ),
_( "SilkS Cop" ), _( "SilkS Cmp" ), _( "Mask Cop " ), _( "Mask Cmp " ),
_( "Drawings " ), _( "Comments " ), _( "Eco1 " ), _( "Eco2 " ),
2008-06-06 16:39:45 +00:00
_( "Edges Pcb" ), _( "BAD INDEX" )
2008-01-05 17:20:46 +00:00
};
if( (unsigned) layer_number > LAYER_LIMIT )
layer_number = LAYER_LIMIT;
const wxString* p = &layer_name_list[layer_number];
2008-02-19 00:35:45 +00:00
2008-01-05 17:20:46 +00:00
if( omitSpacePadding )
{
wxString ret = *p; // copy the string
2008-02-19 00:35:45 +00:00
2008-01-05 17:20:46 +00:00
// modify the copy
ret.Trim();
ret.Replace( wxT(" "), wxT("_") );
2008-02-19 00:35:45 +00:00
2008-01-05 17:20:46 +00:00
return ret;
}
else
return *p;
}
enum textbox {
2007-08-22 05:11:01 +00:00
ID_TEXTBOX_LIST = 8010
};
2007-08-22 05:11:01 +00:00
BEGIN_EVENT_TABLE( WinEDA_TextFrame, wxDialog )
EVT_LISTBOX_DCLICK( ID_TEXTBOX_LIST, WinEDA_TextFrame::D_ClickOnList )
EVT_LISTBOX( ID_TEXTBOX_LIST, WinEDA_TextFrame::D_ClickOnList )
EVT_CLOSE( WinEDA_TextFrame::OnClose )
END_EVENT_TABLE()
/***************************************************************************/
2007-08-22 05:11:01 +00:00
WinEDA_TextFrame::WinEDA_TextFrame( wxWindow* parent, const wxString& title ) :
wxDialog( parent, -1, title, wxPoint( -1, -1 ), wxSize( 250, 350 ),
2007-10-02 16:00:55 +00:00
wxDEFAULT_DIALOG_STYLE | wxFRAME_FLOAT_ON_PARENT | MAYBE_RESIZE_BORDER )
/***************************************************************************/
{
2007-08-22 05:11:01 +00:00
wxSize size;
m_Parent = parent;
2007-08-22 05:11:01 +00:00
CentreOnParent();
2007-08-22 05:11:01 +00:00
size = GetClientSize();
m_List = new wxListBox( this, ID_TEXTBOX_LIST,
wxPoint( 0, 0 ), size,
0, NULL,
wxLB_ALWAYS_SB | wxLB_SINGLE );
2007-08-22 05:11:01 +00:00
m_List->SetBackgroundColour( wxColour( 200, 255, 255 ) );
SetReturnCode( -1 );
}
2007-08-22 05:11:01 +00:00
/***************************************************/
2007-08-22 05:11:01 +00:00
void WinEDA_TextFrame::Append( const wxString& text )
/***************************************************/
{
2007-08-22 05:11:01 +00:00
m_List->Append( text );
}
2007-08-22 05:11:01 +00:00
/**********************************************************/
2007-08-22 05:11:01 +00:00
void WinEDA_TextFrame::D_ClickOnList( wxCommandEvent& event )
/**********************************************************/
{
2007-08-22 05:11:01 +00:00
int ii = m_List->GetSelection();
EndModal( ii );
}
2007-08-22 05:11:01 +00:00
/*************************************************/
2007-08-22 05:11:01 +00:00
void WinEDA_TextFrame::OnClose( wxCloseEvent& event )
/*************************************************/
{
2007-08-22 05:11:01 +00:00
EndModal( -1 );
}
/*****************************************************************************/
2007-08-22 05:11:01 +00:00
void Affiche_1_Parametre( WinEDA_DrawFrame* frame, int pos_X,
const wxString& texte_H, const wxString& texte_L, int color )
/*****************************************************************************/
2007-08-22 05:11:01 +00:00
/*
2007-08-22 05:11:01 +00:00
* Routine d'affichage d'un parametre.
* pos_X = cadrage horizontal
* si pos_X < 0 : la position horizontale est la derniere
* valeur demandee >= 0
* texte_H = texte a afficher en ligne superieure.
* si "", par d'affichage sur cette ligne
* texte_L = texte a afficher en ligne inferieure.
* si "", par d'affichage sur cette ligne
* color = couleur d'affichage
*/
{
2007-08-22 05:11:01 +00:00
frame->MsgPanel->Affiche_1_Parametre( pos_X, texte_H, texte_L, color );
}
2007-08-22 05:11:01 +00:00
/****************************************************************************/
2007-08-22 05:11:01 +00:00
void AfficheDoc( WinEDA_DrawFrame* frame, const wxString& Doc, const wxString& KeyW )
/****************************************************************************/
2007-08-22 05:11:01 +00:00
/*
2007-08-22 05:11:01 +00:00
* Routine d'affichage de la documentation associee a un composant
*/
{
2007-08-22 05:11:01 +00:00
wxString Line1( wxT( "Doc: " ) ), Line2( wxT( "KeyW: " ) );
2007-08-22 05:11:01 +00:00
int color = BLUE;
2007-08-22 05:11:01 +00:00
if( frame && frame->MsgPanel )
{
frame->MsgPanel->EraseMsgBox();
Line1 += Doc;
Line2 += KeyW;
frame->MsgPanel->Affiche_1_Parametre( 10, Line1, Line2, color );
}
}
/***********************/
2007-09-01 12:00:30 +00:00
int GetTimeStamp()
/***********************/
2007-08-22 05:11:01 +00:00
/*
2007-08-22 05:11:01 +00:00
* Retourne une identification temporelle (Time stamp) differente a chaque appel
*/
{
2007-08-22 05:11:01 +00:00
static int OldTimeStamp, NewTimeStamp;
2007-08-22 05:11:01 +00:00
NewTimeStamp = time( NULL );
if( NewTimeStamp <= OldTimeStamp )
NewTimeStamp = OldTimeStamp + 1;
OldTimeStamp = NewTimeStamp;
return NewTimeStamp;
}
2007-08-22 05:11:01 +00:00
2008-06-06 16:39:45 +00:00
/**************************************************************/
2007-10-13 06:18:44 +00:00
const wxString& valeur_param( int valeur, wxString& buf_texte )
2008-06-06 16:39:45 +00:00
/**************************************************************/
2007-08-22 05:11:01 +00:00
/* Retourne pour affichage la valeur d'un parametre, selon type d'unites choisies
2007-08-22 05:11:01 +00:00
* entree : valeur en mils , buffer de texte
* retourne en buffer : texte : valeur exprimee en pouces ou millimetres
* suivie de " ou mm
*/
{
2007-08-22 05:11:01 +00:00
if( g_UnitMetric )
{
2007-10-11 00:11:59 +00:00
buf_texte.Printf( wxT( "%3.3f mm" ), valeur * 0.00254 );
2007-08-22 05:11:01 +00:00
}
else
{
2007-10-11 00:11:59 +00:00
buf_texte.Printf( wxT( "%2.4f \"" ), valeur * 0.0001 );
2007-08-22 05:11:01 +00:00
}
2008-02-19 00:35:45 +00:00
2007-10-13 06:18:44 +00:00
return buf_texte;
}
2008-05-05 19:43:56 +00:00
wxString& operator << ( wxString& aString, const wxPoint& aPos )
{
wxString temp;
aString << wxT("@ (") << valeur_param( aPos.x, temp );
aString << wxT(",") << valeur_param( aPos.y, temp );
aString << wxT(")");
return aString;
}