27#include <wx/filename.h>
30#include <wx/stdpaths.h>
32#include <wx/cmdline.h>
62#include <wx/evtloop.h>
63#include <wx/weakref.h>
83 wxT(
"Unexpected call to Kiface() in kicad/kicad.cpp — a "
84 "project-manager-process code path is reaching into a "
85 "kiface stub. Re-run with KICAD_TRACE=KICAD for a "
101 App().SetAppDisplayName( wxT(
"KiCad" ) );
104 wxString absoluteArgv0 = wxStandardPaths::Get().GetExecutablePath();
106 if( !wxIsAbsolutePath( absoluteArgv0 ) )
108 wxLogError( wxT(
"No meaningful argv[0]" ) );
117 static const wxCmdLineEntryDesc desc[] = {
118 { wxCMD_LINE_OPTION,
"f",
"frame",
"Frame to load", wxCMD_LINE_VAL_STRING, 0 },
119 { wxCMD_LINE_SWITCH,
"n",
"new",
"New instance of KiCad, does not attempt to load previously open files",
120 wxCMD_LINE_VAL_NONE, 0 },
121 { wxCMD_LINE_SWITCH,
nullptr,
"mergetool",
122 "Launch the 3-way merge tool. Expects four positional args: "
123 "ANCESTOR OURS THEIRS MERGED. Intended as a `git mergetool` driver.",
124 wxCMD_LINE_VAL_NONE, 0 },
126 { wxCMD_LINE_SWITCH,
nullptr,
"software-rendering",
"Use software rendering instead of OpenGL",
127 wxCMD_LINE_VAL_NONE, 0 },
129 { wxCMD_LINE_PARAM,
nullptr,
nullptr,
"File to load", wxCMD_LINE_VAL_STRING,
130 wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
131 { wxCMD_LINE_NONE,
nullptr,
nullptr,
nullptr, wxCMD_LINE_VAL_NONE, 0 }
134 wxCmdLineParser parser(
App().argc,
App().argv );
135 parser.SetDesc( desc );
136 parser.Parse(
false );
138 bool mergetoolMode = parser.FoundSwitch(
"mergetool" ) == wxCMD_SWITCH_ON;
140 if( mergetoolMode && parser.GetParamCount() != 4 )
143 wxT(
"kicad --mergetool expects four positional arguments: "
144 "ANCESTOR OURS THEIRS MERGED\n" ) );
165 if( parser.Found(
"frame", &frameName ) )
169 for(
const auto& it : frameTypes )
171 if( it.name == frameName )
177 wxLogError( wxT(
"Unknown frame: %s" ), frameName );
194 if( parser.Found(
"software-rendering" ) )
196 wxSetEnv(
"KICAD_SOFTWARE_RENDERING",
"1" );
224 for(
unsigned i = 0; i < bases.GetCount(); ++i )
226 wxFileName fn( bases[i], wxEmptyString );
229 fn.AppendDir( wxT(
"template" ) );
232 if( fn.DirExists() && fn.IsDirReadable() )
233 m_bm.m_search.AddPaths( fn.GetPath() );
236 auto insertExpanded = [&](
const wxString& aValue )
242 if( resolved.Contains( wxT(
"${" ) ) || resolved.Contains( wxT(
"$(" ) ) )
245 m_bm.m_search.Insert( resolved, 0 );
250 wxT(
"TEMPLATE_DIR" ) ) )
253 insertExpanded( *v );
265 insertExpanded( it->second.GetValue() );
268 wxFrame* frame =
nullptr;
286 parser.GetParam( 2 ), parser.GetParam( 3 ) };
288 frame = mergetoolFrame;
293 wxWindow::FromDIP( wxSize( 775, -1 ), NULL ) );
294 frame = managerFrame;
304 playerFrame =
Kiway.Player( appType,
true );
307 if( frame ==
nullptr )
313 App().SetTopWindow( frame );
318 Kiway.SetTop( frame );
331 if( playerFrame && parser.GetParamCount() )
334 std::vector<wxString> fileArgs;
344 for(
size_t i = 0; i < parser.GetParamCount(); i++ )
345 fileArgs.push_back( parser.GetParam( i ) );
348 if( fileArgs.size() == 1 )
350 wxFileName argv1( fileArgs[0] );
352#if defined( PGM_DATA_FILE_EXT )
357 if( !argv1.GetExt() )
358 argv1.SetExt( wxT( PGM_DATA_FILE_EXT ) );
360 argv1.MakeAbsolute();
362 fileArgs[0] = argv1.GetFullPath();
382 else if( managerFrame )
384 if( parser.GetParamCount() > 0 )
386 wxFileName tmp = parser.GetParam( 0 );
391 "does not appear to be a KiCad project file." ),
392 tmp.GetFullPath() ) );
396 projToLoad = tmp.GetFullPath();
401 if( projToLoad.IsEmpty() && settings->
m_OpenProjects.size() && !parser.FoundSwitch(
"new" ) )
406 if( wxFileExists( last_pro ) )
410 projToLoad = last_pro;
417 if( !projToLoad.empty() )
419 wxFileName fn( projToLoad );
443 wxWeakRef<MERGETOOL_FRAME> f( mergetoolFrame );
445 mergetoolFrame->CallAfter(
450 if( !frame || frame->IsBeingDeleted() )
458 if( wxEventLoopBase* loop = wxTheApp->GetMainLoop() )
459 loop->ScheduleExit( exitCode );
461 frame->Close(
true );
516#if defined(__WXMAC__)
520 if( !aFileName.empty() && wxFileExists( aFileName ) )
542void CustomAssertHandler(
const wxString& file,
544 const wxString& func,
545 const wxString& cond,
570 wxSizerFlags::DisableConsistencyChecks();
571 wxDISABLE_DEBUG_SUPPORT();
572 wxSetAssertHandler( CustomAssertHandler );
583 if( wxGetEnv( wxS(
"KICAD_ENABLE_WXTRACE" ),
nullptr ) )
585 wxLog::EnableLogging(
true );
586 wxLog::SetLogLevel( wxLOG_Trace );
606 int ret = wxApp::OnExit();
609 wxLog::EnableLogging(
false );
620 return wxApp::OnRun();
639 if( aEvent.GetEventType() == wxEVT_SHOW )
641 wxShowEvent&
event =
static_cast<wxShowEvent&
>( aEvent );
642 wxDialog* dialog =
dynamic_cast<wxDialog*
>(
event.GetEventObject() );
648 if( event.IsShown() && dialog->IsModal() )
650 dlgs.push_back( dialog );
653 else if( !event.IsShown() && !dlgs.empty() )
656 if( dlgs.back() == dialog )
659 else if(
auto it = std::find( dlgs.begin(), dlgs.end(), dialog ) ; it != dlgs.end() )
660 dlgs.erase( it, dlgs.end() );
672 bool ProcessEvent( wxEvent& aEvent )
override
674 if( aEvent.GetEventType() == wxEVT_CHAR || aEvent.GetEventType() == wxEVT_CHAR_HOOK )
676 wxKeyEvent* keyEvent =
static_cast<wxKeyEvent*
>( &aEvent );
695 bool OnExceptionInMainLoop()
override
715#if defined( __WXMAC__ )
716 void MacOpenFile(
const wxString& aFileName )
override
wxString GetAboutTitle() const
virtual void Shutdown()=0
The main KiCad project manager frame.
void PreloadAllLibraries()
bool LoadProject(const wxFileName &aProjectFileName)
Loads a new project.
std::vector< wxString > m_OpenProjects
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
virtual bool OpenProjectFiles(const std::vector< wxString > &aFileList, int aCtl=0)
Open a project or set of files given by aFileList.
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
void LoadGlobalTables(std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
(Re)loads the global library tables in the given list, or all tables if no list is given
static wxString GetUserTemplatesPath()
Gets the user path for custom templates.
virtual COMMON_SETTINGS * GetCommonSettings() const
virtual wxApp & App()
Return a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
virtual ENV_VAR_MAP & GetLocalEnvVariables() const
virtual void MacOpenFile(const wxString &aFileName)=0
Specific to MacOSX (not used under Linux or Windows).
std::unique_ptr< SETTINGS_MANAGER > m_settings_manager
void HandleException(std::exception_ptr aPtr, bool aUnhandled=false)
A exception handler to be used at the top level if exceptions bubble up that for.
std::atomic_bool m_libraryPreloadAbort
std::vector< void * > m_ModalDialogs
bool InitPgm(bool aHeadless=false, bool aIsUnitTest=false)
Initialize this program.
std::unique_ptr< KICAD_API_SERVER > m_api_server
void HandleAssert(const wxString &aFile, int aLine, const wxString &aFunc, const wxString &aCond, const wxString &aMsg)
A common assert handler to be used between single_top and kicad.
virtual const wxString & GetExecutablePath() const
virtual SETTINGS_MANAGER & GetSettingsManager() const
virtual LIBRARY_MANAGER & GetLibraryManager() const
void SaveCommonSettings()
Save the program (process) settings subset which are stored .kicad_common.
PGM_KICAD extends PGM_BASE to bring in FileHistory() and PdfBrowser() which were moved from EDA_APP i...
std::unique_ptr< API_HANDLER_COMMON > m_api_common_handler
void MacOpenFile(const wxString &aFileName) override
Specific to MacOSX (not used under Linux or Windows).
APP_SETTINGS_BASE * PgmSettings()
Container for project specific data.
Look for files in a number of paths.
T * RegisterSettings(T *aSettings, bool aLoadNow=true)
Take ownership of the pointer passed in.
void SetKiway(KIWAY *aKiway)
Associate this setting manager with the given Kiway.
void CheckAndRun(wxWindow *parent)
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject)
Replace any environment variable & text variable references with their values.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
This file is part of the common library.
Functions related to environment variables, including help functions.
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
@ FRAME_MERGETOOL
Top-level host for the 3-way merge resolution dialog.
void SetGitBackend(GIT_BACKEND *aBackend)
GIT_BACKEND * GetGitBackend()
static const std::string ProjectFileExtension
static const std::string LegacyProjectFileExtension
const wxChar *const kicadTraceKeyEvent
Flag to enable wxKeyEvent debug tracing.
std::map< wxString, ENV_VAR_ITEM >::const_iterator ENV_VAR_MAP_CITER
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
KIWAY Kiway(KFCTL_CPP_PROJECT_SUITE)
#define KFCTL_CPP_PROJECT_SUITE
Running under C++ project mgr, possibly with others.
#define KFCTL_STANDALONE
Running as a standalone Top.
This file contains miscellaneous commonly used macros and functions.
KICOMMON_API std::optional< wxString > GetVersionedEnvVarValue(const std::map< wxString, ENV_VAR_ITEM > &aMap, const wxString &aBaseName)
Attempt to retrieve the value of a versioned environment variable, such as KICAD8_TEMPLATE_DIR.
void SetPgm(PGM_BASE *pgm)
PGM_BASE & Pgm()
The global program "get" accessor.
KIWAY Kiway(KFCTL_STANDALONE)
Not publicly visible because most of the action is in PGM_KICAD these days.
void OnUnhandledException() override
int FilterEvent(wxEvent &aEvent) override
void SystemDirsAppend(SEARCH_STACK *aSearchStack)
Append system places to aSearchStack in a platform specific way and pertinent to KiCad programs.
System directories search utilities.
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
wxLogTrace helper definitions.
Definition of file extensions used in Kicad.