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)
 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)
 
void KiCopyFile (const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
 
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=nullptr)
 A helper function that wraps a call to wxFileSelector. More...
 
int ExecuteFile (wxWindow *frame, const wxString &ExecFile, const wxString &param=wxEmptyString, wxProcess *callback=nullptr)
 Call the executable file ExecFile with the command line parameters param. More...
 
void AddDelimiterString (wxString &string)
 Add un " to the start and the end of string (if not already done). More...
 
wxString FindKicadFile (const wxString &shortname)
 Search 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)

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

Parameters
stringstring 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(), LaunchExternal(), DIALOG_BOM::OnEditGenerator(), PROJECT_TREE_PANE::onOpenDirectory(), PROJECT_TREE_PANE::onOpenSelectedFileWithTextEditor(), and PCB_EDIT_FRAME::PythonPluginsShowFolder().

◆ CanPrintFile()

bool CanPrintFile ( const wxString &  file)

Definition at line 357 of file gestfich.cpp.

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

References doPrintFile().

Referenced by PROJECT_TREE_PANE::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 = nullptr 
)

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  fullfilename = wxFileSelector( aTitle, defaultpath, defaultname,
85  dotted_Ext, aWildcard,
86  aStyle, // open mode wxFD_OPEN, wxFD_SAVE ..
87  aParent, aPosition.x, aPosition.y );
88 
89  if( aKeepWorkingDirectory )
90  wxSetWorkingDirectory( curr_cwd );
91 
92  if( !fullfilename.IsEmpty() && aMruPath )
93  {
94  wxFileName fn = fullfilename;
95  *aMruPath = fn.GetPath();
96  }
97 
98  return fullfilename;
99 }
#define NULL

References NULL.

Referenced by PGM_BASE::AskUserForPreferredEditor(), DIALOG_BOM::chooseGenerator(), PANEL_HOTKEYS_EDITOR::dumpHotkeys(), BOARD_EDITOR_CONTROL::ExportSpecctraDSN(), SYMBOL_EDITOR_CONTROL::ExportSymbolAsSVG(), GetAssociatedDocument(), PANEL_HOTKEYS_EDITOR::ImportHotKeys(), BOARD_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 = nullptr 
)

Call the executable file ExecFile with the command line parameters param.

Definition at line 165 of file gestfich.cpp.

167 {
168  wxString fullFileName = FindKicadFile( ExecFile );
169 
170  if( wxFileExists( fullFileName ) )
171  {
172  if( !param.IsEmpty() )
173  fullFileName += wxT( " " ) + param;
174 
175  return ProcessExecute( fullFileName, wxEXEC_ASYNC, callback );
176  }
177 #ifdef __WXMAC__
178  else
179  {
180  AddDelimiterString( fullFileName );
181 
182  if( !param.IsEmpty() )
183  fullFileName += wxT( " " ) + param;
184 
185  return ProcessExecute( wxT( "/usr/bin/open -a " ) + fullFileName, wxEXEC_ASYNC, callback );
186  }
187 #endif
188 
189  wxString msg;
190  msg.Printf( _( "Command \"%s\" could not found" ), fullFileName );
191  DisplayError( frame, msg, 20 );
192  return -1;
193 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:253
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:40
void AddDelimiterString(wxString &string)
Add un " to the start and the end of string (if not already done).
Definition: gestfich.cpp:42
wxString FindKicadFile(const wxString &shortname)
Search the executable file shortname in KiCad binary path and return full file name if found or short...
Definition: gestfich.cpp:102
#define _(s)
Definition: 3d_actions.cpp:33

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

Referenced by KICAD_MANAGER_CONTROL::Execute(), DIALOG_CONFIG_EQUFILES::OnEditEquFile(), DIALOG_BOM::OnEditGenerator(), SCH_EDIT_FRAME::OnOpenPcbnew(), PROJECT_TREE_PANE::onOpenSelectedFileWithTextEditor(), PCB_EDIT_FRAME::RunEeschema(), and GERBVIEW_INSPECTION_TOOL::ShowSource().

◆ FindKicadFile()

wxString FindKicadFile ( const wxString &  shortname)

Search 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.

The binary path is found from:

  • binary path.
  • KICAD environment variable.
  • c:\kicad or /usr/local/kicad (the default).
  • default binary path.

Definition at line 102 of file gestfich.cpp.

103 {
104  // Test the presence of the file in the directory shortname of
105  // the KiCad binary path.
106 #ifndef __WXMAC__
107  wxString fullFileName = Pgm().GetExecutablePath() + shortname;
108 #else
109  wxString fullFileName = Pgm().GetExecutablePath() + wxT( "Contents/MacOS/" ) + shortname;
110 #endif
111  if( wxFileExists( fullFileName ) )
112  return fullFileName;
113 
114  // Test the presence of the file in the directory shortname
115  // defined by the environment variable KiCad.
116  if( Pgm().IsKicadEnvVariableDefined() )
117  {
118  fullFileName = Pgm().GetKicadEnvVariable() + shortname;
119 
120  if( wxFileExists( fullFileName ) )
121  return fullFileName;
122  }
123 
124 #if defined( __WINDOWS__ )
125  // kicad can be installed highly portably on Windows, anywhere and concurrently
126  // either the "kicad file" is immediately adjacent to the exe or it's not a valid install
127  return shortname;
128 #endif
129 
130  // Path list for KiCad binary files
131  const static wxChar* possibilities[] = {
132 #if defined( __WXMAC__ )
133  // all internal paths are relative to main bundle kicad.app
134  wxT( "Contents/Applications/pcbnew.app/Contents/MacOS/" ),
135  wxT( "Contents/Applications/eeschema.app/Contents/MacOS/" ),
136  wxT( "Contents/Applications/gerbview.app/Contents/MacOS/" ),
137  wxT( "Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
138  wxT( "Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
139  wxT( "Contents/Applications/pl_editor.app/Contents/MacOS/" ),
140 #else
141  wxT( "/usr/bin/" ),
142  wxT( "/usr/local/bin/" ),
143  wxT( "/usr/local/kicad/bin/" ),
144 #endif
145  };
146 
147  // find binary file from possibilities list:
148  for( unsigned i=0; i<arrayDim(possibilities); ++i )
149  {
150 #ifndef __WXMAC__
151  fullFileName = possibilities[i] + shortname;
152 #else
153  // make relative paths absolute
154  fullFileName = Pgm().GetExecutablePath() + possibilities[i] + shortname;
155 #endif
156 
157  if( wxFileExists( fullFileName ) )
158  return fullFileName;
159  }
160 
161  return shortname;
162 }
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31

References arrayDim(), and Pgm().

Referenced by ExecuteFile().

◆ KiCopyFile()

void KiCopyFile ( const wxString &  aSrcPath,
const wxString &  aDestPath,
wxString &  aErrors 
)
Parameters
aSrcPathis the full filename of the source.
aDestPathis the full filename of the target
aErrorsa wxString to append any errors to

Definition at line 363 of file gestfich.cpp.

364 {
365  if( !wxCopyFile( aSrcPath, aDestPath ) )
366  {
367  wxString msg;
368 
369  if( !aErrors.IsEmpty() )
370  aErrors += "\n";
371 
372  msg.Printf( _( "Cannot copy file \"%s\"." ), aDestPath );
373  aErrors += msg;
374  }
375 }
#define _(s)
Definition: 3d_actions.cpp:33

References _.

Referenced by CreateWindow(), IfaceOrAddress(), SAVE_AS_TRAVERSER::OnFile(), MIGRATION_TRAVERSER::OnFile(), and SCH::IFACE::SaveFileAs().

◆ OpenFile()

void OpenFile ( const wxString &  file)

Definition at line 257 of file gestfich.cpp.

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

References ProcessExecute().

Referenced by PROJECT_TREE_ITEM::Activate().

◆ OpenPDF()

bool OpenPDF ( const wxString &  file)

Run the PDF viewer and display a PDF file.

Parameters
filethe PDF file to open.
Returns
true is success or false if no PDF viewer found.

Definition at line 196 of file gestfich.cpp.

197 {
198  wxString command;
199  wxString filename = file;
200 
201  Pgm().ReadPdfBrowserInfos();
202 
203  if( !Pgm().UseSystemPdfBrowser() ) // Run the preferred PDF Browser
204  {
205 #ifdef __WXMSW__
206  // Windows requires double quotes around the filename to handle spaces
207  command = Pgm().GetPdfBrowserName() + wxT( " \"" ) + filename + wxT( "\"" );
208 #else
209  command = Pgm().GetPdfBrowserName() + wxT( " '" ) + filename + wxT( "'" );
210 #endif
211  }
212  else
213  {
214  // wxLaunchDefaultApplication on Unix systems is run as an external process passing
215  // the filename to the appropriate application as a command argument. Spaces in the
216  // path and/or file name will cause argument parsing issues so always quote the file
217  // name and path. This is applicable to all Unix platforms.
218  // See https://github.com/wxWidgets/wxWidgets/blob/master/src/unix/utilsx11.cpp#L2654
219 #ifdef __WXGTK__
220  filename = wxT( "\"" ) + filename + wxT( "\"" );
221 #endif
222 
223  if( wxLaunchDefaultApplication( filename ) )
224  return true;
225 
226 #ifdef __WXMAC__
227  command = wxT( "/usr/bin/open -a '" ) + file + wxT( "'" );
228 #endif
229  // If launching the system default PDF viewer fails, fall through with empty command
230  // string so the error message is displayed.
231  }
232 
233  if( !command.IsEmpty() )
234  {
235  if( ProcessExecute( command ) != -1 )
236  {
237  return true;
238  }
239  else
240  {
241  wxString msg;
242  msg.Printf( _( "Problem while running the PDF viewer\nCommand is \"%s\"" ), command );
243  DisplayError( NULL, msg );
244  }
245  }
246  else
247  {
248  wxString msg;
249  msg.Printf( _( "Unable to find a PDF viewer for \"%s\"" ), file );
250  DisplayError( NULL, msg );
251  }
252 
253  return false;
254 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:253
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:40
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
#define NULL
#define _(s)
Definition: 3d_actions.cpp:33

References _, DisplayError(), NULL, Pgm(), and ProcessExecute().

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

◆ PrintFile()

void PrintFile ( const wxString &  file)

Definition at line 351 of file gestfich.cpp.

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

References doPrintFile().

Referenced by PROJECT_TREE_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 378 of file gestfich.cpp.

379 {
380  return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
381 }

Referenced by SCH_EDIT_FRAME::OnOpenPcbnew(), and GERBVIEW_INSPECTION_TOOL::ShowSource().