27#include <wx/filename.h>
30#include <wx/stdpaths.h>
32#include <wx/cmdline.h>
52#if defined( KICAD_NATIVE_MODEL_PREVIEW ) && defined( __WINDOWS__ )
65#include <wx/evtloop.h>
66#include <wx/weakref.h>
86 wxT(
"Unexpected call to Kiface() in kicad/kicad.cpp — a "
87 "project-manager-process code path is reaching into a "
88 "kiface stub. Re-run with KICAD_TRACE=KICAD for a "
104 App().SetAppDisplayName( wxT(
"KiCad" ) );
107 wxString absoluteArgv0 = wxStandardPaths::Get().GetExecutablePath();
109 if( !wxIsAbsolutePath( absoluteArgv0 ) )
111 wxLogError( wxT(
"No meaningful argv[0]" ) );
120 static const wxCmdLineEntryDesc desc[] = {
121 { wxCMD_LINE_OPTION,
"f",
"frame",
"Frame to load", wxCMD_LINE_VAL_STRING, 0 },
122 { wxCMD_LINE_SWITCH,
"n",
"new",
"New instance of KiCad, does not attempt to load previously open files",
123 wxCMD_LINE_VAL_NONE, 0 },
124 { wxCMD_LINE_SWITCH,
nullptr,
"mergetool",
125 "Launch the 3-way merge tool. Expects four positional args: "
126 "ANCESTOR OURS THEIRS MERGED. Intended as a `git mergetool` driver.",
127 wxCMD_LINE_VAL_NONE, 0 },
129 { wxCMD_LINE_SWITCH,
nullptr,
"software-rendering",
"Use software rendering instead of OpenGL",
130 wxCMD_LINE_VAL_NONE, 0 },
132 { wxCMD_LINE_PARAM,
nullptr,
nullptr,
"File to load", wxCMD_LINE_VAL_STRING,
133 wxCMD_LINE_PARAM_MULTIPLE | wxCMD_LINE_PARAM_OPTIONAL },
134 { wxCMD_LINE_NONE,
nullptr,
nullptr,
nullptr, wxCMD_LINE_VAL_NONE, 0 }
137 wxCmdLineParser parser(
App().argc,
App().argv );
138 parser.SetDesc( desc );
139 parser.Parse(
false );
141 bool mergetoolMode = parser.FoundSwitch(
"mergetool" ) == wxCMD_SWITCH_ON;
143 if( mergetoolMode && parser.GetParamCount() != 4 )
146 wxT(
"kicad --mergetool expects four positional arguments: "
147 "ANCESTOR OURS THEIRS MERGED\n" ) );
168 if( parser.Found(
"frame", &frameName ) )
172 for(
const auto& it : frameTypes )
174 if( it.name == frameName )
180 wxLogError( wxT(
"Unknown frame: %s" ), frameName );
197 if( parser.Found(
"software-rendering" ) )
199 wxSetEnv(
"KICAD_SOFTWARE_RENDERING",
"1" );
227 for(
unsigned i = 0; i < bases.GetCount(); ++i )
229 wxFileName fn( bases[i], wxEmptyString );
232 fn.AppendDir( wxT(
"template" ) );
235 if( fn.DirExists() && fn.IsDirReadable() )
236 m_bm.m_search.AddPaths( fn.GetPath() );
239 auto insertExpanded = [&](
const wxString& aValue )
245 if( resolved.Contains( wxT(
"${" ) ) || resolved.Contains( wxT(
"$(" ) ) )
248 m_bm.m_search.Insert( resolved, 0 );
253 wxT(
"TEMPLATE_DIR" ) ) )
256 insertExpanded( *v );
268 insertExpanded( it->second.GetValue() );
271 wxFrame* frame =
nullptr;
292 parser.GetParam( 2 ), parser.GetParam( 3 ) };
294 frame = mergetoolFrame;
299 wxWindow::FromDIP( wxSize( 775, -1 ), NULL ) );
300 frame = managerFrame;
310 playerFrame =
Kiway.Player( appType,
true );
313 if( frame ==
nullptr )
319 App().SetTopWindow( frame );
324 Kiway.SetTop( frame );
337 if( playerFrame && parser.GetParamCount() )
340 std::vector<wxString> fileArgs;
350 for(
size_t i = 0; i < parser.GetParamCount(); i++ )
351 fileArgs.push_back( parser.GetParam( i ) );
354 if( fileArgs.size() == 1 )
356 wxFileName argv1( fileArgs[0] );
358#if defined( PGM_DATA_FILE_EXT )
363 if( !argv1.GetExt() )
364 argv1.SetExt( wxT( PGM_DATA_FILE_EXT ) );
366 argv1.MakeAbsolute();
368 fileArgs[0] = argv1.GetFullPath();
388 else if( managerFrame )
390 if( parser.GetParamCount() > 0 )
392 wxFileName tmp = parser.GetParam( 0 );
397 "does not appear to be a KiCad project file." ),
398 tmp.GetFullPath() ) );
402 projToLoad = tmp.GetFullPath();
407 if( projToLoad.IsEmpty() && settings->
m_OpenProjects.size() && !parser.FoundSwitch(
"new" ) )
412 if( wxFileExists( last_pro ) )
416 projToLoad = last_pro;
423 if( !projToLoad.empty() )
425 wxFileName fn( projToLoad );
449 wxWeakRef<MERGETOOL_FRAME> mergeToolFrameRef( mergetoolFrame );
451 mergetoolFrame->CallAfter(
452 [mergeToolFrameRef]()
mutable
454 if( !mergeToolFrameRef || mergeToolFrameRef->IsBeingDeleted() )
457 int exitCode = mergeToolFrameRef->RunMerge();
462 if( wxEventLoopBase* loop = wxTheApp->GetMainLoop() )
463 loop->ScheduleExit( exitCode );
465 mergeToolFrameRef->Close(
true );
473#if defined( KICAD_NATIVE_MODEL_PREVIEW ) && defined( __WINDOWS__ )
474 frame->CallAfter( [frame] { MaybeShowModelPreviewSetupPrompt( frame ); } );
522#if defined(__WXMAC__)
526 if( !aFileName.empty() && wxFileExists( aFileName ) )
548void CustomAssertHandler(
const wxString& file,
550 const wxString& func,
551 const wxString& cond,
576 wxSizerFlags::DisableConsistencyChecks();
577 wxDISABLE_DEBUG_SUPPORT();
578 wxSetAssertHandler( CustomAssertHandler );
589 if( wxGetEnv( wxS(
"KICAD_ENABLE_WXTRACE" ),
nullptr ) )
591 wxLog::EnableLogging(
true );
592 wxLog::SetLogLevel( wxLOG_Trace );
612 int ret = wxApp::OnExit();
615 wxLog::EnableLogging(
false );
626 return wxApp::OnRun();
645 if( aEvent.GetEventType() == wxEVT_SHOW )
647 wxShowEvent&
event =
static_cast<wxShowEvent&
>( aEvent );
648 wxDialog* dialog =
dynamic_cast<wxDialog*
>(
event.GetEventObject() );
654 if( event.IsShown() && dialog->IsModal() )
656 dlgs.push_back( dialog );
659 else if( !event.IsShown() && !dlgs.empty() )
662 if( dlgs.back() == dialog )
665 else if(
auto it = std::find( dlgs.begin(), dlgs.end(), dialog ) ; it != dlgs.end() )
666 dlgs.erase( it, dlgs.end() );
678 bool ProcessEvent( wxEvent& aEvent )
override
680 if( aEvent.GetEventType() == wxEVT_CHAR || aEvent.GetEventType() == wxEVT_CHAR_HOOK )
682 wxKeyEvent* keyEvent =
static_cast<wxKeyEvent*
>( &aEvent );
690 return wxApp::ProcessEvent( aEvent );
700 bool OnExceptionInMainLoop()
override
720#if defined( __WXMAC__ )
721 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()
std::unique_ptr< API_HANDLER_LIBRARIES > m_api_libraries_handler
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.