KiCad PCB EDA Suite
gestfich.h File Reference

This file is part of the common library TODO brief description. More...

#include <wx/filename.h>
#include <wx/process.h>

Go to the source code of this file.

Macros

#define UNIX_STRING_DIR_SEP   wxT( "/" )
 
#define WIN_STRING_DIR_SEP   wxT( "\\" )
 

Functions

bool OpenPDF (const wxString &file)
 Function OpenPDF run the PDF viewer and display a PDF file. More...
 
void OpenFile (const wxString &file)
 
void PrintFile (const wxString &file)
 
bool CanPrintFile (const wxString &file)
 
wxString EDA_FILE_SELECTOR (const wxString &aTitle, const wxString &aPath, const wxString &aFileName, const wxString &aExtension, const wxString &aWildcard, wxWindow *aParent, int aStyle, const bool aKeepWorkingDirectory, const wxPoint &aPosition=wxDefaultPosition, wxString *aMruPath=NULL)
 Function EDA_FILE_SELECTOR. More...
 
EDA_LIST_DIALOGGetFileNames (char *Directory, char *Mask)
 
int ExecuteFile (wxWindow *frame, const wxString &ExecFile, const wxString &param=wxEmptyString, wxProcess *callback=NULL)
 Function ExecuteFile calls the executable file ExecFile with the command line parameters param. More...
 
void AddDelimiterString (wxString &string)
 Function AddDelimiterString Add un " to the start and the end of string (if not already done). More...
 
wxString KicadDatasPath ()
 Function KicadDatasPath returns the data path common to KiCad. More...
 
wxString FindKicadFile (const wxString &shortname)
 Function FindKicadFile searches the executable file shortname in KiCad binary path and return full file name if found or shortname if the kicad binary path is kicad/bin. More...
 
wxString QuoteFullPath (wxFileName &fn, wxPathFormat format=wxPATH_NATIVE)
 Quote return value of wxFileName::GetFullPath(). More...
 

Detailed Description

This file is part of the common library TODO brief description.

See also
common.h

Definition in file gestfich.h.

Macro Definition Documentation

◆ UNIX_STRING_DIR_SEP

#define UNIX_STRING_DIR_SEP   wxT( "/" )
Note
Do we really need these defined?

Definition at line 43 of file gestfich.h.

◆ WIN_STRING_DIR_SEP

#define WIN_STRING_DIR_SEP   wxT( "\\" )

Definition at line 44 of file gestfich.h.

Function Documentation

◆ AddDelimiterString()

void AddDelimiterString ( wxString &  string)

Function AddDelimiterString Add un " to the start and the end of string (if not already done).

Parameters
string= string to modify

Definition at line 42 of file gestfich.cpp.

43 {
44  if( !string.StartsWith( wxT( "\"" ) ) )
45  {
46  string.Prepend ( wxT( "\"" ) );
47  string.Append ( wxT( "\"" ) );
48  }
49 }

Referenced by KICAD_MANAGER_CONTROL::Execute(), ExecuteFile(), KICAD_MANAGER_FRAME::OnBrowseInFileExplorer(), DIALOG_BOM::OnEditGenerator(), TREE_PROJECT_FRAME::OnOpenDirectory(), and TREE_PROJECT_FRAME::OnOpenSelectedFileWithTextEditor().

◆ CanPrintFile()

bool CanPrintFile ( const wxString &  file)

Definition at line 356 of file gestfich.cpp.

357 {
358  return doPrintFile( file, true );
359 }
bool doPrintFile(const wxString &file, bool aDryRun)
Definition: gestfich.cpp:275

References doPrintFile().

Referenced by TREE_PROJECT_FRAME::OnRight().

◆ EDA_FILE_SELECTOR()

wxString EDA_FILE_SELECTOR ( const wxString &  aTitle,
const wxString &  aPath,
const wxString &  aFileName,
const wxString &  aExtension,
const wxString &  aWildcard,
wxWindow *  aParent,
int  aStyle,
const bool  aKeepWorkingDirectory,
const wxPoint &  aPosition = wxDefaultPosition,
wxString *  aMruPath = NULL 
)

Function EDA_FILE_SELECTOR.

is a helper function that wraps a call to wxFileSelector.

Parameters
aTitleis a string to display in the dialog title bar.
aPathis a string contain the default path for the path dialog.
aFileNameis a string containing the default file name.
aExtensionis a string containing the default file extension.
aWildcardis a string containing the default wildcard.
aParentis the parent window of the dialog.
aStyleis the style of the path dialog, wxFD_???.
aKeepWorkingDirectorydetermines if current working directory should be set to the user selected path.
aPositionis the position of the dialog.
aMruPathis a pointer to a string to copy the path selected by the user when the OK button is pressed to dismiss the dialog. This can be NULL.
Returns
the full path and file name of the selected file or wxEmptyString if the user pressed the cancel button to dismiss the dialog.

Definition at line 52 of file gestfich.cpp.

62 {
63  wxString fullfilename;
64  wxString curr_cwd = wxGetCwd();
65  wxString defaultname = aFileName;
66  wxString defaultpath = aPath;
67  wxString dotted_Ext = wxT(".") + aExtension;
68 
69 #ifdef __WINDOWS__
70  defaultname.Replace( wxT( "/" ), wxT( "\\" ) );
71  defaultpath.Replace( wxT( "/" ), wxT( "\\" ) );
72 #endif
73 
74  if( defaultpath.IsEmpty() )
75  {
76  if( aMruPath == NULL )
77  defaultpath = wxGetCwd();
78  else
79  defaultpath = *aMruPath;
80  }
81 
82  wxSetWorkingDirectory( defaultpath );
83 
84 #if 0 && defined (DEBUG)
85  printf( "defaultpath=\"%s\" defaultname=\"%s\" Ext=\"%s\" Mask=\"%s\" flag=%d keep_working_directory=%d\n",
86  TO_UTF8( defaultpath ),
87  TO_UTF8( defaultname ),
88  TO_UTF8( aExtension ),
89  TO_UTF8( aWildcard ),
90  aStyle,
91  aKeepWorkingDirectory );
92 #endif
93 
94  fullfilename = wxFileSelector( aTitle,
95  defaultpath,
96  defaultname,
97  dotted_Ext,
98  aWildcard,
99  aStyle, // open mode wxFD_OPEN, wxFD_SAVE ..
100  aParent,
101  aPosition.x, aPosition.y );
102 
103  if( aKeepWorkingDirectory )
104  wxSetWorkingDirectory( curr_cwd );
105 
106  if( !fullfilename.IsEmpty() && aMruPath )
107  {
108  wxFileName fn = fullfilename;
109  *aMruPath = fn.GetPath();
110  }
111 
112  return fullfilename;
113 }
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48

References TO_UTF8.

Referenced by PGM_BASE::AskUserForPreferredEditor(), DIALOG_BOM::chooseGenerator(), PCB_EDITOR_CONTROL::ExportSpecctraDSN(), LIB_CONTROL::ExportSymbolAsSVG(), GetAssociatedDocument(), PANEL_HOTKEYS_EDITOR::ImportHotKeys(), PCB_EDITOR_CONTROL::ImportSpecctraSession(), NETLIST_DIALOG_ADD_GENERATOR::OnBrowseGenerators(), and MWAVE_POLYGONAL_SHAPE_DLG::ReadDataShapeDescr().

◆ ExecuteFile()

int ExecuteFile ( wxWindow *  frame,
const wxString &  ExecFile,
const wxString &  param = wxEmptyString,
wxProcess *  callback = NULL 
)

Function ExecuteFile calls the executable file ExecFile with the command line parameters param.

Definition at line 178 of file gestfich.cpp.

180 {
181  wxString fullFileName = FindKicadFile( ExecFile );
182 
183  if( wxFileExists( fullFileName ) )
184  {
185  if( !param.IsEmpty() )
186  fullFileName += wxT( " " ) + param;
187 
188  return ProcessExecute( fullFileName, wxEXEC_ASYNC, callback );
189  }
190 #ifdef __WXMAC__
191  else
192  {
193  AddDelimiterString( fullFileName );
194 
195  if( !param.IsEmpty() )
196  fullFileName += wxT( " " ) + param;
197 
198  return ProcessExecute( wxT( "/usr/bin/open -a " ) + fullFileName, wxEXEC_ASYNC, callback );
199  }
200 #endif
201 
202  wxString msg;
203  msg.Printf( _( "Command \"%s\" could not found" ), GetChars( fullFileName ) );
204  DisplayError( frame, msg, 20 );
205  return -1;
206 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:211
void AddDelimiterString(wxString &string)
Function AddDelimiterString Add un " to the start and the end of string (if not already done).
Definition: gestfich.cpp:42
wxString FindKicadFile(const wxString &shortname)
Function FindKicadFile searches the executable file shortname in KiCad binary path and return full fi...
Definition: gestfich.cpp:116
#define _(s)
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:101

References _, AddDelimiterString(), DisplayError(), FindKicadFile(), GetChars(), and ProcessExecute().

Referenced by KICAD_MANAGER_CONTROL::Execute(), DIALOG_CONFIG_EQUFILES::OnEditEquFile(), DIALOG_BOM::OnEditGenerator(), SCH_EDIT_FRAME::OnOpenPcbnew(), TREE_PROJECT_FRAME::OnOpenSelectedFileWithTextEditor(), GERBVIEW_FRAME::OnShowGerberSourceFile(), PCB_EDIT_FRAME::RunEeschema(), and DIALOG_ERC::TestErc().

◆ FindKicadFile()

wxString FindKicadFile ( const wxString &  shortname)

Function FindKicadFile searches the executable file shortname in KiCad binary path and return full file name if found or shortname if the kicad binary path is kicad/bin.

kicad binary path is found from: BinDir or environment variable KICAD or (default) c:\kicad or /usr/local/kicad or default binary path

Definition at line 116 of file gestfich.cpp.

117 {
118  // Test the presence of the file in the directory shortname of
119  // the KiCad binary path.
120 #ifndef __WXMAC__
121  wxString fullFileName = Pgm().GetExecutablePath() + shortname;
122 #else
123  wxString fullFileName = Pgm().GetExecutablePath() + wxT( "Contents/MacOS/" ) + shortname;
124 #endif
125  if( wxFileExists( fullFileName ) )
126  return fullFileName;
127 
128  // Test the presence of the file in the directory shortname
129  // defined by the environment variable KiCad.
130  if( Pgm().IsKicadEnvVariableDefined() )
131  {
132  fullFileName = Pgm().GetKicadEnvVariable() + shortname;
133 
134  if( wxFileExists( fullFileName ) )
135  return fullFileName;
136  }
137 
138  // Path list for KiCad binary files
139  const static wxChar* possibilities[] = {
140 #if defined( __WINDOWS__ )
141  wxT( "c:/kicad/bin/" ),
142  wxT( "d:/kicad/bin/" ),
143  wxT( "c:/Program Files/kicad/bin/" ),
144  wxT( "d:/Program Files/kicad/bin/" ),
145 #elif defined( __WXMAC__ )
146  // all internal paths are relative to main bundle kicad.app
147  wxT( "Contents/Applications/pcbnew.app/Contents/MacOS/" ),
148  wxT( "Contents/Applications/eeschema.app/Contents/MacOS/" ),
149  wxT( "Contents/Applications/gerbview.app/Contents/MacOS/" ),
150  wxT( "Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
151  wxT( "Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
152  wxT( "Contents/Applications/pl_editor.app/Contents/MacOS/" ),
153 #else
154  wxT( "/usr/bin/" ),
155  wxT( "/usr/local/bin/" ),
156  wxT( "/usr/local/kicad/bin/" ),
157 #endif
158  };
159 
160  // find binary file from possibilities list:
161  for( unsigned i=0; i<arrayDim(possibilities); ++i )
162  {
163 #ifndef __WXMAC__
164  fullFileName = possibilities[i] + shortname;
165 #else
166  // make relative paths absolute
167  fullFileName = Pgm().GetExecutablePath() + possibilities[i] + shortname;
168 #endif
169 
170  if( wxFileExists( fullFileName ) )
171  return fullFileName;
172  }
173 
174  return shortname;
175 }
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:65
VTBL_ENTRY const wxString & GetKicadEnvVariable() const
Definition: pgm_base.h:230
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:108
VTBL_ENTRY const wxString & GetExecutablePath() const
Definition: pgm_base.h:232
size_t i
Definition: json11.cpp:597

References arrayDim(), PGM_BASE::GetExecutablePath(), PGM_BASE::GetKicadEnvVariable(), i, and Pgm().

Referenced by ExecuteFile().

◆ GetFileNames()

EDA_LIST_DIALOG* GetFileNames ( char *  Directory,
char *  Mask 
)

◆ KicadDatasPath()

wxString KicadDatasPath ( )

Function KicadDatasPath returns the data path common to KiCad.

If environment variable KICAD is defined (KICAD = path to kicad) Returns <KICAD> /; Otherwise returns <path of binaries> / (if "kicad" is in the path name) Otherwise returns /usr /share/kicad/

Note: The \ are replaced by / (a la Unix)

◆ OpenFile()

void OpenFile ( const wxString &  file)

Definition at line 256 of file gestfich.cpp.

257 {
258  wxFileName fileName( file );
259  wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( fileName.GetExt() );
260 
261  if( !filetype )
262  return;
263 
264  wxString command;
265  wxFileType::MessageParameters params( file );
266 
267  filetype->GetOpenCommand( &command, params );
268  delete filetype;
269 
270  if( !command.IsEmpty() )
271  ProcessExecute( command );
272 }
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:211

References ProcessExecute().

Referenced by TREEPROJECT_ITEM::Activate().

◆ OpenPDF()

bool OpenPDF ( const wxString &  file)

Function OpenPDF run the PDF viewer and display a PDF file.

Parameters
file= PDF file to open
Returns
true is success, false if no PDF viewer found

Definition at line 209 of file gestfich.cpp.

210 {
211  wxString command;
212  wxString filename = file;
213 
215 
216  if( !Pgm().UseSystemPdfBrowser() ) // Run the preferred PDF Browser
217  {
218  command = Pgm().GetPdfBrowserName() + wxT( " '" ) + filename + wxT( "'" );
219  }
220  else
221  {
222  if( wxLaunchDefaultApplication( filename ) )
223  return true;
224 
225 #ifdef __WXMAC__
226  command = wxT( "/usr/bin/open -a '" ) + filename + wxT( "'" );
227 #endif
228  // If launching the system default PDF viewer fails, fall through with empty command
229  // string so the error message is displayed.
230  }
231 
232  if( !command.IsEmpty() )
233  {
234  if( ProcessExecute( command ) != -1 )
235  {
236  return true;
237  }
238  else
239  {
240  wxString msg;
241  msg.Printf( _( "Problem while running the PDF viewer\nCommand is \"%s\"" ), command );
242  DisplayError( NULL, msg );
243  }
244  }
245  else
246  {
247  wxString msg;
248  msg.Printf( _( "Unable to find a PDF viewer for \"%s\"" ), file );
249  DisplayError( NULL, msg );
250  }
251 
252  return false;
253 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:236
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:211
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: kicad.cpp:65
VTBL_ENTRY const wxString & GetPdfBrowserName() const
Definition: pgm_base.h:236
#define _(s)
VTBL_ENTRY void ReadPdfBrowserInfos()
Function ReadPdfBrowserInfos reads the PDF browser choice from the common configuration.
Definition: eda_doc.cpp:42

References _, DisplayError(), PGM_BASE::GetPdfBrowserName(), Pgm(), ProcessExecute(), and PGM_BASE::ReadPdfBrowserInfos().

Referenced by TREEPROJECT_ITEM::Activate(), and GetAssociatedDocument().

◆ PrintFile()

void PrintFile ( const wxString &  file)

Definition at line 350 of file gestfich.cpp.

351 {
352  doPrintFile( file, false );
353 }
bool doPrintFile(const wxString &file, bool aDryRun)
Definition: gestfich.cpp:275

References doPrintFile().

Referenced by TREEPROJECT_ITEM::Print().

◆ QuoteFullPath()

wxString QuoteFullPath ( wxFileName &  fn,
wxPathFormat  format = wxPATH_NATIVE 
)

Quote return value of wxFileName::GetFullPath().

This allows file name paths with spaces to be used as parameters to ProcessExecute function calls.

Parameters
fnis the filename to wrap
formatif provided, can be used to transform the nature of the wrapped filename to another platform.

Definition at line 362 of file gestfich.cpp.

363 {
364  return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
365 }

Referenced by SCH_EDIT_FRAME::OnOpenPcbnew(), GERBVIEW_FRAME::OnShowGerberSourceFile(), and DIALOG_ERC::TestErc().