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 Wayne Stambaugh <stambaughw@gmail.com>
7  * Copyright (C) 1992-2018 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 
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 
66 typedef long timestamp_t;
67 
68 
69 // Flag for special keys
70 // This type could be extended to 64 bits to add room for more flags.
71 // For compatibility with old code, keep flag bits out of the least
72 // significant nibble (0xF).
73 typedef uint32_t EDA_KEY;
74 #define EDA_KEY_C UINT32_C
75 
76 #define GR_KB_RIGHTSHIFT ( EDA_KEY_C( 0x01000000 ) )
77 #define GR_KB_LEFTSHIFT ( EDA_KEY_C( 0x02000000 ) )
78 #define GR_KB_CTRL ( EDA_KEY_C( 0x04000000 ) )
79 #define GR_KB_ALT ( EDA_KEY_C( 0x08000000 ) )
80 #define GR_KB_SHIFT ( GR_KB_LEFTSHIFT | GR_KB_RIGHTSHIFT )
81 #define GR_KB_SHIFTCTRL ( GR_KB_SHIFT | GR_KB_CTRL )
82 #define MOUSE_MIDDLE ( EDA_KEY_C( 0x10000000 ) )
83 #define GR_KEY_INVALID ( EDA_KEY_C( 0x80000000 ) )
84 #define GR_KEY_NONE ( EDA_KEY_C( 0 ) )
85 
87 #define NAMELESS_PROJECT wxT( "noname" )
88 
89 
91 enum pseudokeys {
99 };
100 
101 #define ESC 27
102 
105 #define TEXT_ANGLE_HORIZ 0
106 #define TEXT_ANGLE_VERT 900
107 
108 //-----<KiROUND KIT>------------------------------------------------------------
109 
116 #if !defined( DEBUG )
117 
120 static inline int KiROUND( double v )
121 {
122  return int( v < 0 ? v - 0.5 : v + 0.5 );
123 }
124 
127 #define KIROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 )
128 
129 #else
130 
131 // DEBUG: KiROUND() is a macro to capture line and file, then calls this inline
132 
133 static inline int kiRound_( double v, int line, const char* filename )
134 {
135  v = v < 0 ? v - 0.5 : v + 0.5;
136  if( v > INT_MAX + 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  else if( v < INT_MIN - 0.5 )
142  {
143  printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n",
144  __FUNCTION__, filename, line, v );
145  }
146  return int( v );
147 }
148 
149 #define KiROUND( v ) kiRound_( v, __LINE__, __FILE__ )
150 
151 // in Debug build, use the overflow catcher since code size is immaterial
152 #define KIROUND( v ) KiROUND( v )
153 
154 #endif
155 
156 //-----</KiROUND KIT>-----------------------------------------------------------
157 
158 
160  INCHES = 0,
163  DEGREES = 3,
164 };
165 
166 
169 
170 
180 {
181 public:
182  LOCALE_IO();
183  ~LOCALE_IO();
184 
185 private:
186  // allow for nesting of LOCALE_IO instantiations
187  static std::atomic<unsigned int> m_c_count;
188 
189  // The locale in use before switching to the "C" locale
190  // (the locale can be set by user, and is not always the system locale)
191  std::string m_user_locale;
192 };
193 
198 wxSize GetTextSize( const wxString& aSingleLine, wxWindow* aWindow );
199 
214 bool EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString = NULL );
215 
219 void SelectReferenceNumber( wxTextEntry* aTextEntry );
220 
232 int ProcessExecute( const wxString& aCommandLine, int aFlags = wxEXEC_ASYNC,
233  wxProcess *callback = NULL );
234 
239 
240 int GetCommandOptions( const int argc, const char** argv,
241  const char* stringtst, const char** optarg,
242  int* optind );
243 
252 double RoundTo0( double x, double precision );
253 
261 void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
262 
282 wxString SearchHelpFileFullPath( const SEARCH_STACK& aSearchStack, const wxString& aBaseName );
283 
293 bool EnsureFileDirectoryExists( wxFileName* aTargetFullFileName,
294  const wxString& aBaseFilename,
295  REPORTER* aReporter = NULL );
296 
298 const wxString PrePendPath( const wxString& aEnvVar, const wxString& aPriorityPath );
299 
313 wxConfigBase* GetNewConfig( const wxString& aProgName );
314 
327 wxString GetKicadConfigPath();
328 
335 const wxString ExpandEnvVarSubstitutions( const wxString& aString );
336 
340 const wxString ResolveUriByEnvVars( const wxString& aUri );
341 
342 
343 #ifdef __WXMAC__
344 
348 wxString GetOSXKicadUserDataDir();
349 
354 wxString GetOSXKicadMachineDataDir();
355 
360 wxString GetOSXKicadDataDir();
361 #endif
362 
363 // Some wxWidgets versions (for instance before 3.1.0) do not include
364 // this function, so add it if missing
365 #if !wxCHECK_VERSION( 3, 1, 0 )
366 #define USE_KICAD_WXSTRING_HASH // for common.cpp
367 namespace std
369 {
370  template<> struct hash<wxString>
371  {
372  size_t operator()( const wxString& s ) const;
373  };
374 }
375 #endif
376 
378 #define USE_KICAD_WXPOINT_LESS // for common.cpp
379 namespace std
380 {
381  template<> struct less<wxPoint>
382  {
383  bool operator()( const wxPoint& aA, const wxPoint& aB ) const;
384  };
385 }
386 
387 
392 {
393 public:
394  WX_FILENAME( const wxString& aPath, const wxString& aFilename );
395 
396  void SetFullName( const wxString& aFileNameAndExtension );
397 
398  wxString GetName() const;
399  wxString GetFullName() const;
400  wxString GetPath() const;
401  wxString GetFullPath() const;
402 
403  // Avoid multiple calls to stat() on POSIX kernels.
404  long long GetTimestamp();
405 
406 private:
407  // Write cached values to the wrapped wxFileName. MUST be called before using m_fn.
408  void resolve();
409 
410  wxFileName m_fn;
411  wxString m_path;
412  wxString m_fullName;
413 };
414 
415 
416 long long TimestampDir( const wxString& aDirPath, const wxString& aFilespec );
417 
418 
419 
420 
421 #endif // INCLUDE__COMMON_H_
~LOCALE_IO()
Definition: common.cpp:75
long timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition: common.h:52
static int KiROUND(double v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: common.h:120
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown...
Definition: common.h:179
const wxString ResolveUriByEnvVars(const wxString &aUri)
Replace any environment variables in file-path uris (leaving network-path URIs alone).
Definition: common.cpp:431
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:57
pseudokeys
Pseudo key codes for command panning.
Definition: common.h:91
Template specialization to enable wxStrings for certain containers (e.g. unordered_map) ...
Definition: bitmap.cpp:54
wxString m_path
Definition: common.h:411
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()
Return the user configuration path used to store KiCad&#39;s configuration files.
Definition: common.cpp:238
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:779
wxFileName m_fn
Definition: common.h:410
static std::atomic< unsigned int > m_c_count
Definition: common.h:187
int ProcessExecute(const wxString &aCommandLine, int aFlags=wxEXEC_ASYNC, wxProcess *callback=NULL)
Run a command in a child process.
Definition: common.cpp:181
wxString SearchHelpFileFullPath(const SEARCH_STACK &aSearchStack, const wxString &aBaseName)
Return the help file&#39;s full path.
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:86
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:226
Definition: common.h:160
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: common.cpp:158
uint32_t EDA_KEY
Definition: common.h:73
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:101
LOCALE_IO()
Definition: common.cpp:62
const wxString ExpandEnvVarSubstitutions(const wxString &aString)
Replace any environment variable references with their values.
Definition: common.cpp:417
double RoundTo0(double x, double precision)
Round to the nearest precision.
Definition: common.cpp:203
timestamp_t GetNewTimeStamp()
Definition: common.cpp:187
A wrapper around a wxFileName which is much more performant with a subset of the API.
Definition: common.h:391
Implementation of std::make_unique for pre C++14 compilation environments.
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: common.cpp:130
bool EnsureFileDirectoryExists(wxFileName *aTargetFullFileName, const wxString &aBaseFilename, REPORTER *aReporter=NULL)
Make aTargetFullFileName absolute and create the path of this file if it doesn&#39;t yet exist...
Definition: common.cpp:444
int GetCommandOptions(const int argc, const char **argv, const char *stringtst, const char **optarg, int *optind)
wxString m_fullName
Definition: common.h:412
EDA_UNITS_T
Definition: common.h:159
std::string m_user_locale
Definition: common.h:191
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39