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 <memory>
47 
48 class wxAboutDialogInfo;
49 class SEARCH_STACK;
50 class REPORTER;
51 
52 
61 typedef uint32_t timestamp_t;
62 
63 
64 // Flag for special keys
65 // This type could be extended to 64 bits to add room for more flags.
66 // For compatibility with old code, keep flag bits out of the least
67 // significant nibble (0xF).
68 typedef uint32_t EDA_KEY;
69 #define EDA_KEY_C UINT32_C
70 
71 #define GR_KB_RIGHTSHIFT ( EDA_KEY_C( 0x01000000 ) )
72 #define GR_KB_LEFTSHIFT ( EDA_KEY_C( 0x02000000 ) )
73 #define GR_KB_CTRL ( EDA_KEY_C( 0x04000000 ) )
74 #define GR_KB_ALT ( EDA_KEY_C( 0x08000000 ) )
75 #define GR_KB_SHIFT ( GR_KB_LEFTSHIFT | GR_KB_RIGHTSHIFT )
76 #define GR_KB_SHIFTCTRL ( GR_KB_SHIFT | GR_KB_CTRL )
77 #define MOUSE_MIDDLE ( EDA_KEY_C( 0x10000000 ) )
78 #define GR_KEY_INVALID ( EDA_KEY_C( 0x80000000 ) )
79 #define GR_KEY_NONE ( EDA_KEY_C( 0 ) )
80 
82 #define NAMELESS_PROJECT wxT( "noname" )
83 
84 
86 enum pseudokeys {
94 };
95 
96 #define ESC 27
97 
100 #define TEXT_ANGLE_HORIZ 0
101 #define TEXT_ANGLE_VERT 900
102 
103 //-----<KiROUND KIT>------------------------------------------------------------
104 
111 #if !defined( DEBUG )
112 
115 static inline int KiROUND( double v )
116 {
117  return int( v < 0 ? v - 0.5 : v + 0.5 );
118 }
119 
122 #define KIROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 )
123 
124 #else
125 
126 // DEBUG: KiROUND() is a macro to capture line and file, then calls this inline
127 
128 static inline int kiRound_( double v, int line, const char* filename )
129 {
130  v = v < 0 ? v - 0.5 : v + 0.5;
131  if( v > INT_MAX + 0.5 )
132  {
133  printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n",
134  __FUNCTION__, filename, line, v );
135  }
136  else if( v < INT_MIN - 0.5 )
137  {
138  printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n",
139  __FUNCTION__, filename, line, v );
140  }
141  return int( v );
142 }
143 
144 #define KiROUND( v ) kiRound_( v, __LINE__, __FILE__ )
145 
146 // in Debug build, use the overflow catcher since code size is immaterial
147 #define KIROUND( v ) KiROUND( v )
148 
149 #endif
150 
151 //-----</KiROUND KIT>-----------------------------------------------------------
152 
153 
155  INCHES = 0,
158  DEGREES = 3,
159  PERCENT = 4,
160 };
161 
162 
165 
166 
176 {
177 public:
178  LOCALE_IO();
179  ~LOCALE_IO();
180 
181 private:
182  // allow for nesting of LOCALE_IO instantiations
183  static std::atomic<unsigned int> m_c_count;
184 
185  // The locale in use before switching to the "C" locale
186  // (the locale can be set by user, and is not always the system locale)
187  std::string m_user_locale;
188 };
189 
194 wxSize GetTextSize( const wxString& aSingleLine, wxWindow* aWindow );
195 
210 bool EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString = NULL );
211 
215 void SelectReferenceNumber( wxTextEntry* aTextEntry );
216 
228 int ProcessExecute( const wxString& aCommandLine, int aFlags = wxEXEC_ASYNC,
229  wxProcess *callback = NULL );
230 
235 
236 int GetCommandOptions( const int argc, const char** argv,
237  const char* stringtst, const char** optarg,
238  int* optind );
239 
248 double RoundTo0( double x, double precision );
249 
257 void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
258 
278 wxString SearchHelpFileFullPath( const SEARCH_STACK& aSearchStack, const wxString& aBaseName );
279 
289 bool EnsureFileDirectoryExists( wxFileName* aTargetFullFileName,
290  const wxString& aBaseFilename,
291  REPORTER* aReporter = NULL );
292 
294 const wxString PrePendPath( const wxString& aEnvVar, const wxString& aPriorityPath );
295 
309 std::unique_ptr<wxConfigBase> GetNewConfig( const wxString& aProgName );
310 
323 wxString GetKicadConfigPath();
324 
331 const wxString ExpandEnvVarSubstitutions( const wxString& aString );
332 
336 const wxString ResolveUriByEnvVars( const wxString& aUri );
337 
338 
339 #ifdef __WXMAC__
340 
344 wxString GetOSXKicadUserDataDir();
345 
350 wxString GetOSXKicadMachineDataDir();
351 
356 wxString GetOSXKicadDataDir();
357 #endif
358 
359 // Some wxWidgets versions (for instance before 3.1.0) do not include
360 // this function, so add it if missing
361 #if !wxCHECK_VERSION( 3, 1, 0 )
362 #define USE_KICAD_WXSTRING_HASH // for common.cpp
363 namespace std
365 {
366  template<> struct hash<wxString>
367  {
368  size_t operator()( const wxString& s ) const;
369  };
370 }
371 #endif
372 
374 #define USE_KICAD_WXPOINT_LESS_AND_HASH // for common.cpp
375 namespace std
376 {
377  template <> struct hash<wxPoint>
378  {
379  size_t operator() ( const wxPoint& k ) const;
380  };
381 }
382 
383 namespace std
384 {
385  template<> struct less<wxPoint>
386  {
387  bool operator()( const wxPoint& aA, const wxPoint& aB ) const;
388  };
389 }
390 
397 std::ostream& operator<<( std::ostream& out, const wxSize& size );
398 
405 std::ostream& operator<<( std::ostream& out, const wxPoint& pt );
406 
407 
412 {
413 public:
414  WX_FILENAME( const wxString& aPath, const wxString& aFilename );
415 
416  void SetFullName( const wxString& aFileNameAndExtension );
417 
418  wxString GetName() const;
419  wxString GetFullName() const;
420  wxString GetPath() const;
421  wxString GetFullPath() const;
422 
423  // Avoid multiple calls to stat() on POSIX kernels.
424  long long GetTimestamp();
425 
426 private:
427  // Write cached values to the wrapped wxFileName. MUST be called before using m_fn.
428  void resolve();
429 
430  wxFileName m_fn;
431  wxString m_path;
432  wxString m_fullName;
433 };
434 
435 
436 long long TimestampDir( const wxString& aDirPath, const wxString& aFilespec );
437 
438 
439 
440 
441 #endif // INCLUDE__COMMON_H_
WX_FILENAME(const wxString &aPath, const wxString &aFilename)
Performance enhancements to file and directory operations.
Definition: common.cpp:644
~LOCALE_IO()
Definition: common.cpp:100
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:115
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:175
const wxString ResolveUriByEnvVars(const wxString &aUri)
Replace any environment variables in file-path uris (leaving network-path URIs alone).
Definition: common.cpp:471
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
KIGFX::COLOR4D g_GhostColor
Draw color for moving objects.
Definition: common.cpp:58
pseudokeys
Pseudo key codes for command panning.
Definition: common.h:86
wxString GetName() const
Definition: common.cpp:657
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
wxString GetFullName() const
Definition: common.cpp:664
wxString m_path
Definition: common.h:431
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
void SetFullName(const wxString &aFileNameAndExtension)
Definition: common.cpp:651
wxString GetKicadConfigPath()
Return the user configuration path used to store KiCad's configuration files.
Definition: common.cpp:267
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:850
wxFileName m_fn
Definition: common.h:430
static std::atomic< unsigned int > m_c_count
Definition: common.h:183
int ProcessExecute(const wxString &aCommandLine, int aFlags=wxEXEC_ASYNC, wxProcess *callback=NULL)
Run a command in a child process.
Definition: common.cpp:211
long long GetTimestamp()
Definition: common.cpp:691
wxString SearchHelpFileFullPath(const SEARCH_STACK &aSearchStack, const wxString &aBaseName)
Return the help file's full path.
void resolve()
Definition: common.cpp:683
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:256
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:115
std::ostream & operator<<(std::ostream &out, const wxSize &size)
Helper function to print the given wxSize to a stream.
Definition: common.cpp:615
Definition: common.h:155
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: common.cpp:188
uint32_t EDA_KEY
Definition: common.h:68
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:130
LOCALE_IO()
Definition: common.cpp:83
const wxString ExpandEnvVarSubstitutions(const wxString &aString)
Replace any environment variable references with their values.
Definition: common.cpp:458
wxString GetPath() const
Definition: common.cpp:670
double RoundTo0(double x, double precision)
Round to the nearest precision.
Definition: common.cpp:233
timestamp_t GetNewTimeStamp()
Definition: common.cpp:217
wxString GetFullPath() const
Definition: common.cpp:676
A wrapper around a wxFileName which is much more performant with a subset of the API.
Definition: common.h:411
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: common.cpp:159
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:485
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:50
wxString m_fullName
Definition: common.h:432
EDA_UNITS_T
Definition: common.h:154
std::string m_user_locale
Definition: common.h:187
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39