Move LOCALE_IO out of common.h

This commit is contained in:
Marek Roszko 2020-10-23 21:38:50 -04:00
parent 472ea039dd
commit 1d559108c8
53 changed files with 154 additions and 109 deletions

View File

@ -34,6 +34,7 @@
#include <common.h>
#include <layers_id_colors_and_visibility.h>
#include <locale_io.h>
#include <potracelib.h>
#include "bitmap2component.h"

View File

@ -354,6 +354,7 @@ set( COMMON_SRCS
lib_table_base.cpp
lib_tree_model.cpp
lib_tree_model_adapter.cpp
locale_io.cpp
lockfile.cpp
lset.cpp
marker_base.cpp

View File

@ -238,87 +238,6 @@ bool IsMetricUnit( EDA_UNITS aUnit )
}
/**
* Global variables definitions.
*
* TODO: All of these variables should be moved into the class were they
* are defined and used. Most of them probably belong in the
* application class.
*/
// When reading/writing files, we need to swtich to setlocale( LC_NUMERIC, "C" ).
// Works fine to read/write files with floating point numbers.
// We can call setlocale( LC_NUMERIC, "C" ) of wxLocale( "C", "C", "C", false )
// wxWidgets discourage a direct call to setlocale
// However, for us, calling wxLocale( "C", "C", "C", false ) has a unwanted effect:
// The I18N translations are no longer active, because the English dixtionary is selected.
// To read files, this is not a major issues, but the resul can differ
// from using setlocale(xx, "C").
// Previouly, we called setlocale( LC_NUMERIC, "C" )
// The old code will be removed when calling wxLocale( "C", "C", "C", false )
// is fully tested, and all issues fixed
#define USE_WXLOCALE 1 /* 0 to call setlocale, 1 to call wxLocale */
// On Windows, when using setlocale, a wx alert is generated
// in some cases (reading a bitmap for instance)
// So we disable alerts during the time a file is read or written
#if !USE_WXLOCALE
#if defined( _WIN32 ) && defined( DEBUG )
// a wxAssertHandler_t function to filter wxWidgets alert messages when reading/writing a file
// when switching the locale to LC_NUMERIC, "C"
// It is used in class LOCALE_IO to hide a useless (in kicad) wxWidgets alert message
void KiAssertFilter( const wxString &file, int line,
const wxString &func, const wxString &cond,
const wxString &msg)
{
if( !msg.Contains( "Decimal separator mismatch" ) )
wxTheApp->OnAssertFailure( file.c_str(), line, func.c_str(), cond.c_str(), msg.c_str() );
}
#endif
#endif
std::atomic<unsigned int> LOCALE_IO::m_c_count( 0 );
LOCALE_IO::LOCALE_IO() : m_wxLocale( nullptr )
{
// use thread safe, atomic operation
if( m_c_count++ == 0 )
{
#if USE_WXLOCALE
m_wxLocale = new wxLocale( "C", "C", "C", false );
#else
// Store the user locale name, to restore this locale later, in dtor
m_user_locale = setlocale( LC_NUMERIC, nullptr );
#if defined( _WIN32 ) && defined( DEBUG )
// Disable wxWidgets alerts
wxSetAssertHandler( KiAssertFilter );
#endif
// Switch the locale to C locale, to read/write files with fp numbers
setlocale( LC_NUMERIC, "C" );
#endif
}
}
LOCALE_IO::~LOCALE_IO()
{
// use thread safe, atomic operation
if( --m_c_count == 0 )
{
// revert to the user locale
#if USE_WXLOCALE
delete m_wxLocale; // Deleting m_wxLocale restored previous locale
m_wxLocale = nullptr;
#else
setlocale( LC_NUMERIC, m_user_locale.c_str() );
#if defined( _WIN32 ) && defined( DEBUG )
// Enable wxWidgets alerts
wxSetDefaultAssertHandler();
#endif
#endif
}
}
wxSize GetTextSize( const wxString& aSingleLine, wxWindow* aWindow )
{
wxCoord width;

View File

@ -24,8 +24,8 @@
*/
#include <common.h> // for LOCALE_IO
#include <config_params.h> // for PARAM_CFG_INT_WITH_SCALE, PARAM_CFG_...
#include <locale_io.h>
#include <gal/color4d.h> // for COLOR4D
#include <math/util.h> // for KiROUND
#include <wx/config.h> // for wxConfigBase

48
common/locale_io.cpp Normal file
View File

@ -0,0 +1,48 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 1992-2020 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
*/
#include <locale_io.h>
#include <wx/intl.h>
std::atomic<unsigned int> LOCALE_IO::m_c_count( 0 );
LOCALE_IO::LOCALE_IO() : m_wxLocale( nullptr )
{
// use thread safe, atomic operation
if( m_c_count++ == 0 )
{
m_wxLocale = new wxLocale( "C", "C", "C", false );
}
}
LOCALE_IO::~LOCALE_IO()
{
// use thread safe, atomic operation
if( --m_c_count == 0 )
{
delete m_wxLocale; // Deleting m_wxLocale restored previous locale
m_wxLocale = nullptr;
}
}

View File

@ -30,6 +30,7 @@
*/
#include <eda_item.h>
#include <locale_io.h>
#include <page_layout/ws_data_item.h>
#include <page_layout/ws_data_model.h>
#include <page_layout/ws_draw_item.h>

View File

@ -31,6 +31,7 @@
*/
#include <eda_item.h>
#include <locale_io.h>
#include <page_layout/ws_painter.h>
#include <page_layout/ws_draw_item.h>
#include <page_layout/ws_data_item.h>

View File

@ -24,13 +24,14 @@
#include <utility>
#include <sstream>
#include <common.h>
#include <locale_io.h>
#include <gal/color4d.h>
#include <settings/json_settings.h>
#include <settings/nested_settings.h>
#include <settings/parameters.h>
#include <wx/config.h>
#include <wx/debug.h>
#include <wx/fileconf.h>
#include <wx/filename.h>
const wxChar* const traceSettings = wxT( "KICAD_SETTINGS" );

View File

@ -24,6 +24,7 @@
#include <sch_painter.h>
#include <lib_edit_frame.h>
#include <locale_io.h>
#include <class_libentry.h>
#include <class_library.h>
#include <plotters_specific.h>

View File

@ -25,6 +25,7 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <locale_io.h>
#include <plotters_specific.h>
#include <sch_edit_frame.h>
#include <sch_sheet_path.h>

View File

@ -28,6 +28,7 @@
#include <plotters_specific.h>
#include <sch_edit_frame.h>
#include <base_units.h>
#include <locale_io.h>
#include <sch_sheet_path.h>
#include <schematic.h>
#include <project.h>

View File

@ -28,6 +28,7 @@
#include <plotters_specific.h>
#include <sch_edit_frame.h>
#include <base_units.h>
#include <locale_io.h>
#include <sch_sheet_path.h>
#include <sch_painter.h>
#include <schematic.h>

View File

@ -28,6 +28,7 @@
#include <sch_edit_frame.h>
#include <base_units.h>
#include <sch_sheet_path.h>
#include <locale_io.h>
#include <pgm_base.h>
#include <project.h>
#include <reporter.h>

View File

@ -31,6 +31,7 @@
#include <sch_draw_panel.h>
#include <sch_edit_frame.h>
#include <base_units.h>
#include <locale_io.h>
#include <lib_edit_frame.h>
#include <sch_sheet_path.h>
#include <schematic.h>

View File

@ -25,6 +25,7 @@
#include <sch_plugins/eagle/sch_eagle_plugin.h>
#include <kiway.h>
#include <locale_io.h>
#include <properties.h>
#include <algorithm>

View File

@ -29,6 +29,7 @@
#include <advanced_config.h>
#include <pgm_base.h>
#include <trace_helpers.h>
#include <locale_io.h>
#include <sch_bitmap.h>
#include <sch_bus_entry.h>
#include <sch_component.h>

View File

@ -33,6 +33,7 @@
#include <gr_text.h>
#include <kiway.h>
#include <kicad_string.h>
#include <locale_io.h>
#include <richio.h>
#include <core/typeinfo.h>
#include <properties.h>

View File

@ -32,6 +32,7 @@
#include "spice_reporter.h"
#include <common.h>
#include <locale_io.h>
#include <wx/stdpaths.h>
#include <wx/dir.h>

View File

@ -32,6 +32,7 @@
#include <confirm.h>
#include <common.h>
#include <ki_exception.h>
#include <locale_io.h>
SPICE_VALUE::SPICE_VALUE( const wxString& aString )
{

View File

@ -70,6 +70,7 @@
#include <gerber_file_image_list.h>
#include <excellon_image.h>
#include <kicad_string.h>
#include <locale_io.h>
#include <X2_gerber_attributes.h>
#include <view/view.h>

View File

@ -27,6 +27,7 @@
#include <export_to_pcbnew.h>
#include <confirm.h>
#include <locale_io.h>
#include <macros.h>
#include <trigo.h>
#include <gerbview_frame.h>

View File

@ -32,6 +32,7 @@
#include <wildcards_and_files_ext.h>
#include <gerbview.h>
#include <richio.h>
#include <locale_io.h>
#include <gerber_file_image.h>
#include <gerber_file_image_list.h>
#include <gerbview_frame.h>

View File

@ -23,6 +23,7 @@
*/
#include <kicad_string.h>
#include <locale_io.h>
#include <gerbview.h>
#include <gerbview_frame.h>
#include <gerber_file_image.h>

View File

@ -212,31 +212,6 @@ enum class EDA_UNITS
bool IsImperialUnit( EDA_UNITS aUnit );
bool IsMetricUnit( EDA_UNITS aUnit );
/**
* Instantiate the current locale within a scope in which you are expecting
* exceptions to be thrown.
*
* The constructor sets a "C" language locale option, to read/print files with floating
* point numbers. The destructor insures that the default locale is restored if an
* exception is thrown or not.
*/
class LOCALE_IO
{
public:
LOCALE_IO();
~LOCALE_IO();
private:
// allow for nesting of LOCALE_IO instantiations
static std::atomic<unsigned int> m_c_count;
// The locale in use before switching to the "C" locale
// (the locale can be set by user, and is not always the system locale)
std::string m_user_locale;
wxLocale* m_wxLocale;
};
/**
* Return the size of @a aSingleLine of text when it is rendered in @a aWindow
* using whatever font is currently set in that window.

56
include/locale_io.h Normal file
View File

@ -0,0 +1,56 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 1992-2020 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
*/
#ifndef LOCALE_IO_H
#define LOCALE_IO_H
#include <atomic>
#include <string>
class wxLocale;
/**
* Instantiate the current locale within a scope in which you are expecting
* exceptions to be thrown.
*
* The constructor sets a "C" language locale option, to read/print files with floating
* point numbers. The destructor insures that the default locale is restored if an
* exception is thrown or not.
*/
class LOCALE_IO
{
public:
LOCALE_IO();
~LOCALE_IO();
private:
// allow for nesting of LOCALE_IO instantiations
static std::atomic<unsigned int> m_c_count;
// The locale in use before switching to the "C" locale
// (the locale can be set by user, and is not always the system locale)
std::string m_user_locale;
wxLocale* m_wxLocale;
};
#endif

View File

@ -27,6 +27,7 @@
#include <common.h>
#include <datafile_read_write.h>
#include <kicad_string.h>
#include <locale_io.h>
#include <macros.h>
#include <pcb_calculator_datafile_lexer.h>
#include <pcb_calculator_frame.h>

View File

@ -29,6 +29,7 @@
#include "wx/string.h"
#include <base_units.h>
#include <locale_io.h>
#include "class_board_stackup.h"
#include "stackup_predefined_prms.h"

View File

@ -32,6 +32,7 @@
#include "reporter.h"
#include "class_board.h"
#include "dialog_export_step_base.h"
#include <locale_io.h>
#include <pcbnew_settings.h>
#include <project/project_file.h> // LAST_PATH_TYPE
#include <widgets/text_ctrl_eval.h>

View File

@ -29,6 +29,7 @@
#include <reporter.h>
#include <confirm.h>
#include <pcbplot.h>
#include <locale_io.h>
#include <class_board.h>
#include <dialog_export_svg_base.h>
#include <wx_html_report_panel.h>

View File

@ -32,6 +32,7 @@
#include <reporter.h>
#include <wildcards_and_files_ext.h>
#include <layers_id_colors_and_visibility.h>
#include <locale_io.h>
#include <bitmaps.h>
#include <class_board.h>
#include <dialog_plot.h>

View File

@ -35,6 +35,7 @@
#include <build_version.h>
#include <macros.h>
#include <wildcards_and_files_ext.h>
#include <locale_io.h>
#include <pcbnew.h>
#include <class_board.h>
#include <class_module.h>

View File

@ -27,6 +27,7 @@
*/
#include <kicad_string.h>
#include <locale_io.h>
#include <build_version.h>
#include <export_footprints_placefile.h>

View File

@ -36,6 +36,7 @@
#include <class_track.h>
#include <confirm.h>
#include <dialogs/dialog_gencad_export_options.h>
#include <locale_io.h>
#include <hash_eda.h>
#include <pcb_edit_frame.h>
#include <pcbnew_settings.h>

View File

@ -31,6 +31,7 @@
#include <cstdio>
#include <vector>
#include <ki_exception.h>
#include <locale_io.h>
#include <reporter.h>
#include <exporters/board_exporter_base.h>

View File

@ -27,6 +27,7 @@
#include <list>
#include <locale_io.h>
#include <pcb_edit_frame.h>
#include <pcbnew.h>
#include <class_board.h>

View File

@ -32,6 +32,7 @@
#include <gr_text.h>
#include <confirm.h>
#include <kicad_string.h>
#include <locale_io.h>
#include <math/util.h> // for KiROUND
#include <class_board.h>

View File

@ -37,6 +37,7 @@
#include <plotter.h>
#include <kicad_string.h>
#include <locale_io.h>
#include <pcb_edit_frame.h>
#include <pgm_base.h>
#include <build_version.h>

View File

@ -31,6 +31,7 @@
#include <plotter.h>
#include <kicad_string.h>
#include <locale_io.h>
#include <pcb_edit_frame.h>
#include <pgm_base.h>
#include <build_version.h>

View File

@ -32,6 +32,7 @@
#include <vector>
#include <build_version.h>
#include <locale_io.h>
#include <pcb_edit_frame.h>
#include <plotter.h>

View File

@ -29,6 +29,7 @@
#include <plotter.h>
#include <kicad_string.h>
#include <locale_io.h>
#include <pcb_edit_frame.h>
#include <pgm_base.h>

View File

@ -28,6 +28,7 @@
#include <html_messagebox.h>
#include <io_mgr.h>
#include <kicad_string.h>
#include <locale_io.h>
#include <kiface_ids.h>
#include <kiway.h>
#include <lib_id.h>

View File

@ -26,6 +26,7 @@
#include "dialog_import_gfx.h"
#include <kiface_i.h>
#include <locale_io.h>
#include <pcb_layer_box_selector.h>
#include <wildcards_and_files_ext.h>
#include <class_board.h>

View File

@ -30,7 +30,7 @@
#include <pcb_shape.h>
#include <pcb_text.h>
#include <fp_text.h>
#include <common.h>
#include <locale_io.h>
#include <netinfo.h>
#include <plugins/kicad/pcb_parser.h>

View File

@ -30,6 +30,7 @@
#include <gestfich.h>
#include <pcb_edit_frame.h>
#include <dialog_helpers.h>
#include <locale_io.h>
#include <richio.h>
#include <filter_reader.h>
#include <base_units.h>

View File

@ -30,6 +30,7 @@
#include <plotter.h>
#include <pcbplot.h>
#include <base_units.h>
#include <locale_io.h>
#include <reporter.h>
#include <class_board.h>
#include <plotcontroller.h>

View File

@ -59,6 +59,7 @@ Load() TODO's
#include <convert_basic_shapes_to_polygon.h>
#include <geometry/geometry_utils.h>
#include <kicad_string.h>
#include <locale_io.h>
#include <properties.h>
#include <trigo.h>
#include <math/util.h> // for KiROUND

View File

@ -34,6 +34,7 @@
#include <class_board.h>
#include <class_module.h>
#include <locale_io.h>
#include <pcb_text.h>
#include <pcb_shape.h>
#include <fp_shape.h>

View File

@ -37,6 +37,7 @@
#include <class_pcb_target.h>
#include <fp_shape.h>
#include <confirm.h>
#include <locale_io.h>
#include <zones.h>
#include <plugins/kicad/kicad_plugin.h>
#include <plugins/kicad/pcb_parser.h>

View File

@ -49,6 +49,7 @@
#include <plugins/kicad/kicad_plugin.h>
#include <pcb_plot_params_parser.h>
#include <pcb_plot_params.h>
#include <locale_io.h>
#include <zones.h>
#include <plugins/kicad/pcb_parser.h>
#include <convert_basic_shapes_to_polygon.h> // for RECT_CHAMFER_POSITIONS definition

View File

@ -67,6 +67,7 @@
#include <wx/string.h>
#include <kicad_string.h>
#include <locale_io.h>
#include <macros.h>
#include <properties.h>
#include <zones.h>

View File

@ -33,6 +33,7 @@
#include <wx/filename.h>
#include <wx/xml/xml.h>
#include <locale_io.h>
#include <pcad_plugin.h>
#include <s_expr_loader.h>
#include <pcb.h>

View File

@ -35,6 +35,7 @@
#include <confirm.h> // DisplayError()
#include <gestfich.h> // EDA_FileSelector()
#include <trigo.h> // RotatePoint()
#include <locale_io.h>
#include <macros.h>
#include <math/util.h> // for KiROUND

View File

@ -35,6 +35,7 @@
#include <confirm.h> // DisplayError()
#include <gestfich.h> // EDA_FileSelector()
#include <pcb_edit_frame.h>
#include <locale_io.h>
#include <macros.h>
#include <class_board.h>
#include <class_module.h>