KiCad PCB EDA Suite
common.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2014-2017 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2007-2015 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 2008-2015 Wayne Stambaugh <stambaughw@verizon.net>
7  * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
32 #ifndef INCLUDE__COMMON_H_
33 #define INCLUDE__COMMON_H_
34 
35 #include <vector>
36 #include <boost/cstdint.hpp>
37 
38 #include <wx/wx.h>
39 #include <wx/confbase.h>
40 #include <wx/fileconf.h>
41 
42 #include <richio.h>
43 #include <gal/color4d.h>
44 
45 #include <atomic>
46 
47 // C++11 "polyfill" for the C++14 std::make_unique function
48 #include "make_unique.h"
49 
50 class wxAboutDialogInfo;
51 class SEARCH_STACK;
52 class REPORTER;
53 
54 
55 // Flag for special keys
56 // This type could be extended to 64 bits to add room for more flags.
57 // For compatibility with old code, keep flag bits out of the least
58 // significant nibble (0xF).
59 typedef uint32_t EDA_KEY;
60 #define EDA_KEY_C UINT32_C
61 
62 #define GR_KB_RIGHTSHIFT ( EDA_KEY_C( 0x01000000 ) )
63 #define GR_KB_LEFTSHIFT ( EDA_KEY_C( 0x02000000 ) )
64 #define GR_KB_CTRL ( EDA_KEY_C( 0x04000000 ) )
65 #define GR_KB_ALT ( EDA_KEY_C( 0x08000000 ) )
66 #define GR_KB_SHIFT ( GR_KB_LEFTSHIFT | GR_KB_RIGHTSHIFT )
67 #define GR_KB_SHIFTCTRL ( GR_KB_SHIFT | GR_KB_CTRL )
68 #define MOUSE_MIDDLE ( EDA_KEY_C( 0x10000000 ) )
69 #define GR_KEY_INVALID ( EDA_KEY_C( 0x80000000 ) )
70 #define GR_KEY_NONE ( EDA_KEY_C( 0 ) )
71 
73 #define NAMELESS_PROJECT wxT( "noname" )
74 
75 
77 enum pseudokeys {
85 };
86 
87 #define ESC 27
88 
91 #define TEXT_ANGLE_HORIZ 0
92 #define TEXT_ANGLE_VERT 900
93 
94 //-----<KiROUND KIT>------------------------------------------------------------
95 
103 #if !defined( DEBUG )
104 
107 static inline int KiROUND( double v )
108 {
109  return int( v < 0 ? v - 0.5 : v + 0.5 );
110 }
111 
114 #define KIROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 )
115 
116 #else
117 
118 // DEBUG: KiROUND() is a macro to capture line and file, then calls this inline
119 
120 static inline int kiRound_( double v, int line, const char* filename )
121 {
122  v = v < 0 ? v - 0.5 : v + 0.5;
123  if( v > INT_MAX + 0.5 )
124  {
125  printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v );
126  }
127  else if( v < INT_MIN - 0.5 )
128  {
129  printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v );
130  }
131  return int( v );
132 }
133 
134 #define KiROUND( v ) kiRound_( v, __LINE__, __FILE__ )
135 
136 // in Debug build, use the overflow catcher since code size is immaterial
137 #define KIROUND( v ) KiROUND( v )
138 
139 #endif
140 
141 //-----</KiROUND KIT>-----------------------------------------------------------
142 
143 
145  INCHES = 0,
148  DEGREES = 3,
149 };
150 
151 
152 extern EDA_UNITS_T g_UserUnit;
153 
156 
157 
167 {
168 public:
169  LOCALE_IO();
170  ~LOCALE_IO();
171 
172 private:
173  void setUserLocale( const char* aUserLocale );
174 
175  // allow for nesting of LOCALE_IO instantiations
176  static std::atomic<unsigned int> m_c_count;
177 
178  // The locale in use before switching to the "C" locale
179  // (the locale can be set by user, and is not always the system locale)
180  std::string m_user_locale;
181 };
182 
183 
189 wxSize GetTextSize( const wxString& aSingleLine, wxWindow* aWindow );
190 
204 bool EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString = NULL );
205 
206 
218 int ProcessExecute( const wxString& aCommandLine, int aFlags = wxEXEC_ASYNC,
219  wxProcess *callback = NULL );
220 
221 
222 /**************/
223 /* common.cpp */
224 /**************/
225 
229 time_t GetNewTimeStamp();
230 
231 int GetCommandOptions( const int argc, const char** argv,
232  const char* stringtst, const char** optarg,
233  int* optind );
234 
243 double RoundTo0( double x, double precision );
244 
252 void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
253 
274 wxString SearchHelpFileFullPath( const SEARCH_STACK& aSearchStack, const wxString& aBaseName );
275 
285 bool EnsureFileDirectoryExists( wxFileName* aTargetFullFileName,
286  const wxString& aBaseFilename,
287  REPORTER* aReporter = NULL );
288 
290 const wxString PrePendPath( const wxString& aEnvVar, const wxString& aPriorityPath );
291 
306 wxConfigBase* GetNewConfig( const wxString& aProgName );
307 
308 
313 wxString GetKicadConfigPath();
314 
321 const wxString ExpandEnvVarSubstitutions( const wxString& aString );
322 
323 
324 #ifdef __WXMAC__
325 
329 wxString GetOSXKicadUserDataDir();
330 
335 wxString GetOSXKicadMachineDataDir();
336 
341 wxString GetOSXKicadDataDir();
342 #endif
343 
344 #endif // INCLUDE__COMMON_H_
~LOCALE_IO()
Definition: common.cpp:82
static int KiROUND(double v)
KiROUND rounds a floating point number to an int using "round halfway cases away from zero"...
Definition: common.h:107
Class LOCALE_IO is a class that can be instantiated within a scope in which you are expecting excepti...
Definition: common.h:166
const wxString PrePendPath(const wxString &aEnvVar, const wxString &aPriorityPath)
Put aPriorityPath in front of all paths in the value of aEnvVar.
Definition: prependpath.cpp:32
EDA_UNITS_T g_UserUnit
display units
Definition: common.cpp:56
KIGFX::COLOR4D g_GhostColor
Draw color for moving objects.
Definition: common.cpp:57
pseudokeys
Pseudo key codes for command panning.
Definition: common.h:77
Class SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
Class REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
wxString GetKicadConfigPath()
Function GetKicadConfigPath.
Definition: common.cpp:217
static std::atomic< unsigned int > m_c_count
Definition: common.h:176
int ProcessExecute(const wxString &aCommandLine, int aFlags=wxEXEC_ASYNC, wxProcess *callback=NULL)
Function ProcessExecute runs a child process.
Definition: common.cpp:160
wxString SearchHelpFileFullPath(const SEARCH_STACK &aSearchStack, const wxString &aBaseName)
Function SearchHelpFileFullPath returns the help file's full path.
wxSize GetTextSize(const wxString &aSingleLine, wxWindow *aWindow)
Function GetTextSize returns the size of aSingleLine of text when it is rendered in aWindow using wha...
Definition: common.cpp:93
wxConfigBase * GetNewConfig(const wxString &aProgName)
Function GetNewConfig.
Definition: common.cpp:205
void setUserLocale(const char *aUserLocale)
Definition: common.h:145
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Function wxStringSplit splits aString to a string list separated at aSplitter.
Definition: common.cpp:137
uint32_t EDA_KEY
Definition: common.h:52
bool EnsureTextCtrlWidth(wxTextCtrl *aCtrl, const wxString *aString=NULL)
Function EnsureTextCtrlWidth sets the minimum pixel width on a text control in order to make a text s...
Definition: common.cpp:108
LOCALE_IO()
Definition: common.cpp:70
time_t GetNewTimeStamp()
Definition: common.cpp:166
const wxString ExpandEnvVarSubstitutions(const wxString &aString)
Function ExpandEnvVarSubstitutions replaces any environment variable references with their values...
Definition: common.cpp:254
double RoundTo0(double x, double precision)
Round to the nearest precision.
Definition: common.cpp:182
Implementation of std::make_unique for pre C++14 compilation environments.
bool EnsureFileDirectoryExists(wxFileName *aTargetFullFileName, const wxString &aBaseFilename, REPORTER *aReporter=NULL)
Helper function EnsureFileDirectoryExists make aTargetFullFileName absolute and creates the path of t...
Definition: common.cpp:267
int GetCommandOptions(const int argc, const char **argv, const char *stringtst, const char **optarg, int *optind)
EDA_UNITS_T
Definition: common.h:144
std::string m_user_locale
Definition: common.h:180
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39