From ec2cd994052fca2153ad0dcdd40391dae6c92ec7 Mon Sep 17 00:00:00 2001
From: Jon Evans <jon@craftyjon.com>
Date: Sun, 11 Feb 2024 09:36:17 -0500
Subject: [PATCH] Guard actions that change project against running in
 non-standalone mode

Fixes https://gitlab.com/kicad/code/kicad/-/issues/16942
---
 eeschema/sch_edit_frame.cpp |  8 ++++++++
 pcbnew/files.cpp            | 14 +++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/eeschema/sch_edit_frame.cpp b/eeschema/sch_edit_frame.cpp
index 9ecc569f1f..1c2adc4bec 100644
--- a/eeschema/sch_edit_frame.cpp
+++ b/eeschema/sch_edit_frame.cpp
@@ -1260,6 +1260,10 @@ void SCH_EDIT_FRAME::OnClearFileHistory( wxCommandEvent& aEvent )
 
 void SCH_EDIT_FRAME::NewProject()
 {
+    // Only standalone mode can directly load a new document
+    if( !Kiface().IsSingle() )
+        return;
+
     wxString pro_dir = m_mruPath;
 
     wxFileDialog dlg( this, _( "New Schematic" ), pro_dir, wxEmptyString,
@@ -1290,6 +1294,10 @@ void SCH_EDIT_FRAME::NewProject()
 
 void SCH_EDIT_FRAME::LoadProject()
 {
+    // Only standalone mode can directly load a new document
+    if( !Kiface().IsSingle() )
+        return;
+
     wxString pro_dir = m_mruPath;
     wxString wildcards = FILEEXT::AllSchematicFilesWildcard()
                             + wxS( "|" ) + FILEEXT::KiCadSchematicFileWildcard()
diff --git a/pcbnew/files.cpp b/pcbnew/files.cpp
index d0634dead9..2e9f95e8fc 100644
--- a/pcbnew/files.cpp
+++ b/pcbnew/files.cpp
@@ -265,6 +265,10 @@ bool PCB_EDIT_FRAME::Files_io_from_id( int id )
     {
     case ID_LOAD_FILE:
     {
+        // Only standalone mode can directly load a new document
+        if( !Kiface().IsSingle() )
+            return false;
+
         int      open_ctl = KICTL_KICAD_ONLY;
         wxString fileName = Prj().AbsolutePath( GetBoard()->GetFileName() );
 
@@ -274,6 +278,10 @@ bool PCB_EDIT_FRAME::Files_io_from_id( int id )
 
     case ID_IMPORT_NON_KICAD_BOARD:
     {
+        // Only standalone mode can directly load a new document
+        if( !Kiface().IsSingle() )
+            return false;
+
         int      open_ctl = KICTL_NONKICAD_ONLY;
         wxString fileName; // = Prj().AbsolutePath( GetBoard()->GetFileName() );
 
@@ -332,6 +340,10 @@ bool PCB_EDIT_FRAME::Files_io_from_id( int id )
 
     case ID_NEW_BOARD:
     {
+        // Only standalone mode can directly load a new document
+        if( !Kiface().IsSingle() )
+            return false;
+
         if( IsContentModified() )
         {
             wxFileName fileName = GetBoard()->GetFileName();
@@ -1353,4 +1365,4 @@ void PCB_EDIT_FRAME::GenIPC2581File( wxCommandEvent& event )
     }
 
     GetScreen()->SetContentModified( false );
-}
\ No newline at end of file
+}