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-2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2007-2015 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
7  * Copyright (C) 1992-2019 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 
37 #include <wx/wx.h>
38 #include <wx/confbase.h>
39 #include <wx/fileconf.h>
40 #include <wx/dir.h>
41 
42 #include <richio.h>
43 #include <gal/color4d.h>
44 
45 #include <atomic>
46 #include <limits>
47 #include <memory>
48 #include <type_traits>
49 #include <typeinfo>
50 
51 class SEARCH_STACK;
52 class REPORTER;
53 
62 typedef uint32_t timestamp_t;
63 
65 #define NAMELESS_PROJECT wxT( "noname" )
66 
69 #define TEXT_ANGLE_HORIZ 0
70 #define TEXT_ANGLE_VERT 900
71 
72 enum class EDA_UNITS
73 {
74  INCHES = 0,
75  MILLIMETRES = 1,
76  UNSCALED = 2,
77  DEGREES = 3,
78  PERCENT = 4,
79 };
80 
81 
90 class LOCALE_IO
91 {
92 public:
93  LOCALE_IO();
94  ~LOCALE_IO();
95 
96 private:
97  // allow for nesting of LOCALE_IO instantiations
98  static std::atomic<unsigned int> m_c_count;
99 
100  // The locale in use before switching to the "C" locale
101  // (the locale can be set by user, and is not always the system locale)
102  std::string m_user_locale;
103 };
104 
109 wxSize GetTextSize( const wxString& aSingleLine, wxWindow* aWindow );
110 
125 bool EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString = NULL );
126 
130 void SelectReferenceNumber( wxTextEntry* aTextEntry );
131 
143 int ProcessExecute( const wxString& aCommandLine, int aFlags = wxEXEC_ASYNC,
144  wxProcess *callback = NULL );
145 
150 
151 int GetCommandOptions( const int argc, const char** argv,
152  const char* stringtst, const char** optarg,
153  int* optind );
154 
162 void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
163 
183 wxString SearchHelpFileFullPath( const SEARCH_STACK& aSearchStack, const wxString& aBaseName );
184 
194 bool EnsureFileDirectoryExists( wxFileName* aTargetFullFileName,
195  const wxString& aBaseFilename,
196  REPORTER* aReporter = NULL );
197 
199 const wxString PrePendPath( const wxString& aEnvVar, const wxString& aPriorityPath );
200 
214 std::unique_ptr<wxConfigBase> GetNewConfig( const wxString& aProgName );
215 
228 wxString GetKicadConfigPath();
229 
236 const wxString ExpandEnvVarSubstitutions( const wxString& aString );
237 
241 const wxString ResolveUriByEnvVars( const wxString& aUri );
242 
243 
244 #ifdef __WXMAC__
245 
249 wxString GetOSXKicadUserDataDir();
250 
255 wxString GetOSXKicadMachineDataDir();
256 
261 wxString GetOSXKicadDataDir();
262 #endif
263 
264 // Some wxWidgets versions (for instance before 3.1.0) do not include
265 // this function, so add it if missing
266 #if !wxCHECK_VERSION( 3, 1, 0 )
267 #define USE_KICAD_WXSTRING_HASH // for common.cpp
268 namespace std
270 {
271  template<> struct hash<wxString>
272  {
273  size_t operator()( const wxString& s ) const;
274  };
275 }
276 #endif
277 
279 #define USE_KICAD_WXPOINT_LESS_AND_HASH // for common.cpp
280 namespace std
281 {
282  template <> struct hash<wxPoint>
283  {
284  size_t operator() ( const wxPoint& k ) const;
285  };
286 }
287 
288 namespace std
289 {
290  template<> struct less<wxPoint>
291  {
292  bool operator()( const wxPoint& aA, const wxPoint& aB ) const;
293  };
294 }
295 
302 std::ostream& operator<<( std::ostream& out, const wxSize& size );
303 
310 std::ostream& operator<<( std::ostream& out, const wxPoint& pt );
311 
312 
317 {
318 public:
319  WX_FILENAME( const wxString& aPath, const wxString& aFilename );
320 
321  void SetFullName( const wxString& aFileNameAndExtension );
322 
323  wxString GetName() const;
324  wxString GetFullName() const;
325  wxString GetPath() const;
326  wxString GetFullPath() const;
327 
328  // Avoid multiple calls to stat() on POSIX kernels.
329  long long GetTimestamp();
330 
331 private:
332  // Write cached values to the wrapped wxFileName. MUST be called before using m_fn.
333  void resolve();
334 
335  wxFileName m_fn;
336  wxString m_path;
337  wxString m_fullName;
338 };
339 
340 
341 long long TimestampDir( const wxString& aDirPath, const wxString& aFilespec );
342 
343 
344 
345 
346 #endif // INCLUDE__COMMON_H_
EDA_UNITS
Definition: common.h:72
WX_FILENAME(const wxString &aPath, const wxString &aFilename)
Performance enhancements to file and directory operations.
Definition: common.cpp:615
~LOCALE_IO()
Definition: common.cpp:98
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:90
const wxString ResolveUriByEnvVars(const wxString &aUri)
Replace any environment variables in file-path uris (leaving network-path URIs alone).
Definition: common.cpp:442
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
wxString GetName() const
Definition: common.cpp:628
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
Definition: bitmap.cpp:55
wxString GetFullName() const
Definition: common.cpp:635
wxString m_path
Definition: common.h:336
SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:61
void SetFullName(const wxString &aFileNameAndExtension)
Definition: common.cpp:622
wxString GetKicadConfigPath()
Return the user configuration path used to store KiCad's configuration files.
Definition: common.cpp:242
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
A copy of ConvertFileTimeToWx() because wxWidgets left it as a static function private to src/common/...
Definition: common.cpp:821
wxFileName m_fn
Definition: common.h:335
static std::atomic< unsigned int > m_c_count
Global variables definitions.
Definition: common.h:98
int ProcessExecute(const wxString &aCommandLine, int aFlags=wxEXEC_ASYNC, wxProcess *callback=NULL)
Run a command in a child process.
Definition: common.cpp:209
long long GetTimestamp()
Definition: common.cpp:662
wxString SearchHelpFileFullPath(const SEARCH_STACK &aSearchStack, const wxString &aBaseName)
Return the help file's full path.
void resolve()
Definition: common.cpp:654
std::unique_ptr< wxConfigBase > GetNewConfig(const wxString &aProgName)
Create a new wxConfig so we can put configuration files in a more proper place for each platform.
Definition: common.cpp:231
#define NULL
wxSize GetTextSize(const wxString &aSingleLine, wxWindow *aWindow)
Return the size of aSingleLine of text when it is rendered in aWindow using whatever font is currentl...
Definition: common.cpp:113
std::ostream & operator<<(std::ostream &out, const wxSize &size)
Helper function to print the given wxSize to a stream.
Definition: common.cpp:586
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: common.cpp:186
bool EnsureTextCtrlWidth(wxTextCtrl *aCtrl, const wxString *aString=NULL)
Set the minimum pixel width on a text control in order to make a text string be fully visible within ...
Definition: common.cpp:128
LOCALE_IO()
Definition: common.cpp:81
const wxString ExpandEnvVarSubstitutions(const wxString &aString)
Replace any environment variable references with their values.
Definition: common.cpp:429
wxString GetPath() const
Definition: common.cpp:641
timestamp_t GetNewTimeStamp()
Definition: common.cpp:215
wxString GetFullPath() const
Definition: common.cpp:647
A wrapper around a wxFileName which is much more performant with a subset of the API.
Definition: common.h:316
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: common.cpp:157
bool EnsureFileDirectoryExists(wxFileName *aTargetFullFileName, const wxString &aBaseFilename, REPORTER *aReporter=NULL)
Make aTargetFullFileName absolute and create the path of this file if it doesn't yet exist.
Definition: common.cpp:456
int GetCommandOptions(const int argc, const char **argv, const char *stringtst, const char **optarg, int *optind)
uint32_t timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition: common.h:52
wxString m_fullName
Definition: common.h:337
std::string m_user_locale
Definition: common.h:102